示例#1
0
文件: lua.c 项目: chtombleson/php-lua
/** {{{ static int php_lua_print(lua_State *L)
*/
static int php_lua_print(lua_State *L)  {
	int i = 0;

	TSRMLS_FETCH();
	for (i = -lua_gettop(L) ; i<0; i++) {
		zval *tmp = php_lua_get_zval_from_lua(L, i, NULL TSRMLS_CC);
		zend_print_zval_r(tmp, 1 TSRMLS_CC);
		zval_ptr_dtor(&tmp);
	}
	return 0;
}
示例#2
0
/** {{{ proto LuaClosure::invoke(mxied $args)
*/
PHP_METHOD(lua_closure, invoke) {
	lua_closure_object *objval = php_lua_closure_object_from_zend_object(Z_OBJ_P(getThis()));
	int bp, sp;
	zval *arguments = NULL;
	lua_State *L  = NULL;
	zval rv;

	if (ZEND_NUM_ARGS()) {
		arguments = emalloc(sizeof(zval*) * ZEND_NUM_ARGS());
		if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), arguments) == FAILURE) {
			efree(arguments);
			zend_throw_exception_ex(NULL, 0, "cannot get arguments for calling closure");
			return;
		}
	}

	if (Z_TYPE(objval->lua) != IS_OBJECT
			|| !instanceof_function(Z_OBJCE(objval->lua), lua_ce)) {
		zend_throw_exception_ex(NULL, 0, "corrupted Lua object");
		return;
	}

	L = (Z_LUAVAL(objval->lua))->L;

	bp = lua_gettop(L);
	lua_rawgeti(L, LUA_REGISTRYINDEX, objval->closure);
	if (LUA_TFUNCTION != lua_type(L, lua_gettop(L))) {
		lua_pop(L, -1);
		zend_throw_exception_ex(NULL, 0, "call to lua closure failed");
		return;
	}

	if (ZEND_NUM_ARGS()) {
		int i = 0;
		for(;i<ZEND_NUM_ARGS();i++) {
			php_lua_send_zval_to_lua(L, &arguments[i]);
		}
	}

	if (lua_pcall(L, ZEND_NUM_ARGS(), LUA_MULTRET, 0) != 0) {
		if (arguments) {
			efree(arguments);
		}
		lua_pop(L, lua_gettop(L) - bp);
		zend_throw_exception_ex(NULL, 0, 
				"call to lua function %s failed", lua_tostring(L, -1));
		return;
	}

	sp = lua_gettop(L) - bp;

	if (!sp) {
		RETURN_NULL();
	} else if (sp == 1) {
		php_lua_get_zval_from_lua(L, -1, &(objval->lua), return_value);
	} else {
		zval rv;
		int  i = 0;
		array_init(return_value);
		for (i = -sp; i < 0; i++) {
			php_lua_get_zval_from_lua(L, i, &(objval->lua), &rv);
			add_next_index_zval(return_value, &rv);
		}
	}

	lua_pop(L, sp);

	if (arguments) {
		efree(arguments);
	}
}