Merge pull request #13 from hjanuschka/hj

fix for latest php7 head
This commit is contained in:
Xinchen Hui
2015-02-02 22:09:42 +08:00
9 changed files with 314 additions and 182 deletions

21
.travis.yml Normal file
View File

@@ -0,0 +1,21 @@
language: php
php:
- 5.6
- 5.5
- 5.4
- 5.3
- 5.3.3
before_install:
- sudo apt-get update -qq
- sudo apt-get install -y libjson0-dev libjson0 php5-cli php5-dev liblua5.2-dev
before_script:
# Compile PHP
- ./travis/compile.sh
script:
- ./travis/build_check.sh

29
config.m4 Normal file → Executable file
View File

@@ -1,33 +1,48 @@
dnl $Id: config.m4 321796 2012-01-05 17:23:48Z laruence $
PHP_ARG_WITH(lua, for lua support,
[ --with-lua=[DIR] Include php lua support])
PHP_ARG_WITH(lua-version, to specify a custom lua version, [ --with-lua-version=[VERSION]] Use the specified lua version.)
if test "$PHP_LUA" != "no"; then
if test -r $PHP_LUA/include/lua.h; then
LUA_DIR=$PHP_LUA
LUA_INCLUDE_DIR=$PHP_LUA
else
AC_MSG_CHECKING(for lua in default path)
for i in /usr/local /usr; do
if test -r $i/include/lua/lua.h; then
LUA_DIR=$i
LUA_INCLUDE_DIR=$i/include/lua
AC_MSG_RESULT(found in $i)
break
fi
if test "$PHP_LUA_VERSION" != "yes"; then
if test -r $i/include/lua$PHP_LUA_VERSION/lua.h; then
LUA_INCLUDE_DIR=$i/include/lua$PHP_LUA_VERSION
AC_MSG_RESULT(found in a version-specific subdirectory of $i)
break
fi
fi
done
fi
if test -z "$LUA_DIR"; then
if test -z "$LUA_INCLUDE_DIR"; then
AC_MSG_RESULT(not found)
AC_MSG_ERROR(Please reinstall the lua distribution - lua.h should be in <lua-dir>/include/)
fi
LUA_LIB_NAME=liblua
if test "$PHP_LUA_VERSION" != "yes"; then
LUA_LIB_SUFFIX=lua$PHP_LUA_VERSION
else
LUA_LIB_SUFFIX=lua$PHP_LUA_VERSION
fi
LUA_LIB_NAME=lib$LUA_LIB_SUFFIX
if test -r $PHP_LUA/$PHP_LIBDIR/${LUA_LIB_NAME}.${SHLIB_SUFFIX_NAME} -o -r $PHP_LUA/$PHP_LIBDIR/${LUA_LIB_NAME}.a; then
LUA_LIB_DIR=$PHP_LUA/$PHP_LIBDIR
else
AC_MSG_CHECKING(for lua library in default path)
for i in /usr/$PHP_LIBDIR /usr/lib /usr/lib64; do
for i in /usr/$PHP_LIBDIR /usr/lib /usr/lib64 /usr/lib/x86_64-linux-gnu; do
if test -r $i/${LUA_LIB_NAME}.${SHLIB_SUFFIX_NAME} -o -r $i/${LUA_LIB_NAME}.a; then
LUA_LIB_DIR=$i
AC_MSG_RESULT(found in $i)
@@ -41,8 +56,8 @@ if test "$PHP_LUA" != "no"; then
AC_MSG_ERROR(Please reinstall the lua distribution - lua library should be in <lua-dir>/lib/)
fi
PHP_ADD_INCLUDE($LUA_DIR/include)
PHP_ADD_LIBRARY_WITH_PATH(lua, $LUA_LIB_DIR, LUA_SHARED_LIBADD)
PHP_ADD_INCLUDE($LUA_INCLUDE_DIR)
PHP_ADD_LIBRARY_WITH_PATH($LUA_LIB_SUFFIX, $LUA_LIB_DIR, LUA_SHARED_LIBADD)
PHP_SUBST(LUA_SHARED_LIBADD)
PHP_NEW_EXTENSION(lua, lua.c lua_closure.c, $ext_shared)
fi

306
lua.c Normal file → Executable file
View File

@@ -90,7 +90,6 @@ ZEND_GET_MODULE(lua)
/** {{{ static void php_lua_stack_dump(lua_State* L)
* just for debug
*/
#ifdef PHP_LUA_DEBUG
static void php_lua_stack_dump(lua_State* L) {
int i = 1;
int n = lua_gettop(L);
@@ -121,7 +120,6 @@ static void php_lua_stack_dump(lua_State* L) {
printf("\n");
}
}
#endif
/* }}} */
/** {{{ static int php_lua_atpanic(lua_State *L)
@@ -139,12 +137,22 @@ static int php_lua_atpanic(lua_State *L) {
*/
static int php_lua_print(lua_State *L) {
int i = 0;
zval p;
char *t;
TSRMLS_FETCH();
for (i = -lua_gettop(L) ; i<0; i++) {
int nargs = lua_gettop(L);
for (i=1; i <= nargs; ++i) {
//ZVAL_STRING(&p, lua_tostring(L, 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);
//printf("E: '%s'", lua_tostring(L, i));
}
return 0;
}
@@ -175,7 +183,7 @@ static void * php_lua_alloc_function(void *ud, void *ptr, size_t osize, size_t n
/** {{{ static void php_lua_dtor_object(void *object, zend_object_handle handle TSRMLS_DC)
* the dtor function for lua object
*/
static void php_lua_dtor_object(void *object, zend_object_handle handle TSRMLS_DC) {
static void php_lua_dtor_object(void *object, zend_object_handlers handle TSRMLS_DC) {
php_lua_object *lua_obj = (php_lua_object *)object;
zend_object_std_dtor(&(lua_obj->obj) TSRMLS_CC);
@@ -184,6 +192,8 @@ static void php_lua_dtor_object(void *object, zend_object_handle handle TSRMLS_D
lua_close(lua_obj->L);
}
efree(lua_obj);
}
/* }}} */
@@ -192,67 +202,61 @@ static void php_lua_dtor_object(void *object, zend_object_handle handle TSRMLS_D
*
* the create object handler for lua
*/
static zend_object_value php_lua_create_object(zend_class_entry *ce TSRMLS_DC) {
zend_object_value obj = {0};
php_lua_object *lua_obj = NULL;
zend_object *php_lua_create_object(zend_class_entry *ce)
{
php_lua_object* intern;
lua_State *L = NULL;
L = lua_newstate(php_lua_alloc_function, NULL);
L = luaL_newstate();
lua_atpanic(L, php_lua_atpanic);
lua_obj = emalloc(sizeof(php_lua_object));
intern = emalloc(sizeof(php_lua_object)+ sizeof(zval) * (ce->default_properties_count - 1));
if (!lua_obj) {
if (!intern) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "alloc memory for lua object failed");
}
lua_obj->L = L;
zend_object_std_init(&(lua_obj->obj), ce TSRMLS_CC);
intern->L = L;
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
zend_hash_copy(lua_obj->obj.properties, &ce->default_properties,
#if (PHP_MINOR_VERSION < 4)
(copy_ctor_func_t) zval_add_ref,
#else
zval_copy_property_ctor(ce),
#endif
(void *)0, sizeof(zval *));
#elif (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION > 3)
object_properties_init(&(lua_obj->obj), ce);
#endif
zend_object_std_init(&intern->obj, ce TSRMLS_CC);
object_properties_init(&intern->obj, ce TSRMLS_CC);
intern->obj.handlers = &lua_object_handlers;
return &intern->obj;
obj.handle = zend_objects_store_put(lua_obj, php_lua_dtor_object, NULL, NULL TSRMLS_CC);
obj.handlers = &lua_object_handlers;
return obj;
}
/* }}} */
/** {{{ static zval * php_lua_read_property(zval *object, zval *member, int type TSRMLS_DC)
*/
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)) || (PHP_MAJOR_VERSION < 5)
static zval * php_lua_read_property(zval *object, zval *member, int type TSRMLS_DC) {
#else
static zval * php_lua_read_property(zval *object, zval *member, int type, const struct _zend_literal *key TSRMLS_DC) {
#endif
zval *retval = NULL;
zval *php_lua_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv){
zval * retval = NULL;
lua_State *L = NULL;
zval *tmp_member = NULL;
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION > 3))
(void)key;
#endif
zval r;
ZVAL_NULL(&r);
retval = &r;
if (type != BP_VAR_R) {
MAKE_STD_ZVAL(retval);
ZVAL_NULL(retval);
return retval;
}
if (Z_TYPE_P(member) != IS_STRING) {
ALLOC_ZVAL(tmp_member);
//ALLOC_ZVAL(tmp_member);
*tmp_member = *member;
INIT_PZVAL(tmp_member);
zval_copy_ctor(tmp_member);
convert_to_string(tmp_member);
member = tmp_member;
@@ -265,12 +269,10 @@ static zval * php_lua_read_property(zval *object, zval *member, int type, const
lua_getglobal(L, Z_STRVAL_P(member));
#endif
retval = php_lua_get_zval_from_lua(L, -1, object TSRMLS_CC);
Z_DELREF_P(retval);
//Z_DELREF_P(retval);
lua_pop(L, 1);
if (tmp_member) {
zval_ptr_dtor(&tmp_member);
}
return retval;
}
@@ -278,21 +280,14 @@ static zval * php_lua_read_property(zval *object, zval *member, int type, const
/** {{{ static void php_lua_write_property(zval *object, zval *member, zval *value TSRMLS_DC)
*/
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)) || (PHP_MAJOR_VERSION < 5)
static void php_lua_write_property(zval *object, zval *member, zval *value TSRMLS_DC) {
#else
static void php_lua_write_property(zval *object, zval *member, zval *value, const struct _zend_literal *key TSRMLS_DC) {
#endif
static void php_lua_write_property(zval *object, zval *member, zval *value, void ** key TSRMLS_DC) {
lua_State *L = NULL;
zval *tmp_member = NULL;
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION > 3))
(void)key;
#endif
if (Z_TYPE_P(member) != IS_STRING) {
ALLOC_ZVAL(tmp_member);
//ALLOC_ZVAL(tmp_member);
*tmp_member = *member;
INIT_PZVAL(tmp_member);
//INIT_PZVAL(tmp_member);
zval_copy_ctor(tmp_member);
convert_to_string(tmp_member);
member = tmp_member;
@@ -311,7 +306,7 @@ static void php_lua_write_property(zval *object, zval *member, zval *value, cons
#endif
if (tmp_member) {
zval_ptr_dtor(&tmp_member);
zval_ptr_dtor(tmp_member);
}
}
/* }}} */
@@ -320,8 +315,8 @@ static void php_lua_write_property(zval *object, zval *member, zval *value, cons
*/
static int php_lua_call_callback(lua_State *L) {
int order = 0;
zval *return_value = NULL;
zval **func = NULL;
zval return_value;
zval *func = NULL;
zval *callbacks = NULL;
TSRMLS_FETCH();
@@ -329,19 +324,20 @@ static int php_lua_call_callback(lua_State *L) {
callbacks = zend_read_static_property(lua_ce, ZEND_STRL("_callbacks"), 1 TSRMLS_CC);
if (ZVAL_IS_NULL(callbacks)) {
return 0;
}
MAKE_STD_ZVAL(return_value);
func=zend_hash_index_find(Z_ARRVAL_P(callbacks), order);
if (zend_hash_index_find(Z_ARRVAL_P(callbacks), order, (void **)&func) == FAILURE) {
return 0;
}
if (!zend_is_callable(*func, 0, NULL TSRMLS_CC)) {
if (!zend_is_callable(func, 0, NULL TSRMLS_CC)) {
return 0;
} else {
zval **params = NULL;
int i = 0;
int arg_num = lua_gettop(L);
@@ -352,17 +348,21 @@ static int php_lua_call_callback(lua_State *L) {
params[i] = php_lua_get_zval_from_lua(L, -(arg_num-i), NULL TSRMLS_CC);
}
call_user_function(EG(function_table), NULL, *func, return_value, arg_num, params TSRMLS_CC);
call_user_function(EG(function_table), NULL, func, &return_value, arg_num, *params TSRMLS_CC);
php_lua_send_zval_to_lua(L, return_value TSRMLS_CC);
php_lua_send_zval_to_lua(L, &return_value TSRMLS_CC);
for (i=0; i<arg_num; i++) {
zval_ptr_dtor(&params[i]);
zval_ptr_dtor(params[i]);
}
efree(params);
zval_ptr_dtor(&return_value);
//php_lua_send_zval_to_lua(L, return_value TSRMLS_CC);
return 1;
}
@@ -372,11 +372,13 @@ static int php_lua_call_callback(lua_State *L) {
/** {{{ zval * php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj TSRMLS_DC)
*/
zval * php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj TSRMLS_DC) {
zval *retval;
zval * retval;
MAKE_STD_ZVAL(retval);
retval = ecalloc(sizeof(zval), 1);
//retval = ecalloc(sizeof(zval));
//MAKE_STD_ZVAL(retval);
ZVAL_NULL(retval);
switch (lua_type(L, index)) {
case LUA_TNIL:
ZVAL_NULL(retval);
@@ -393,7 +395,7 @@ zval * php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj TSRMLS_D
size_t len = 0;
val = (char *)lua_tolstring(L, index, &len);
ZVAL_STRINGL(retval, val, len, 1);
ZVAL_STRINGL(retval, val, len);
}
break;
case LUA_TTABLE:
@@ -426,7 +428,7 @@ zval * php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj TSRMLS_D
break;
}
lua_pop(L, 1);
zval_ptr_dtor(&key);
zval_ptr_dtor(key);
}
break;
case LUA_TFUNCTION:
@@ -461,9 +463,11 @@ zval * php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj TSRMLS_D
*/
int php_lua_send_zval_to_lua(lua_State *L, zval *val TSRMLS_DC) {
switch (Z_TYPE_P(val)) {
case IS_BOOL:
lua_pushboolean(L, Z_BVAL_P(val));
case IS_TRUE:
case IS_FALSE:
lua_pushboolean(L, Z_LVAL_P(val));
break;
case IS_NULL:
lua_pushnil(L);
@@ -479,7 +483,9 @@ int php_lua_send_zval_to_lua(lua_State *L, zval *val TSRMLS_DC) {
break;
case IS_OBJECT:
case IS_ARRAY:
{
if (zend_is_callable(val, 0, NULL TSRMLS_CC)) {
zval* callbacks = NULL;
@@ -492,7 +498,7 @@ int php_lua_send_zval_to_lua(lua_State *L, zval *val TSRMLS_DC) {
lua_pushnumber(L, zend_hash_num_elements(Z_ARRVAL_P(callbacks)));
lua_pushcclosure(L, php_lua_call_callback, 1);
zval_add_ref(&val);
zval_add_ref(val);
add_next_index_zval(callbacks, val);
} else {
HashTable *ht = NULL;
@@ -500,49 +506,55 @@ int php_lua_send_zval_to_lua(lua_State *L, zval *val TSRMLS_DC) {
ht = HASH_OF(val);
if (++ht->nApplyCount > 1) {
if (++ht->u.v.nApplyCount > 1) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "recursion found");
--ht->nApplyCount;
--ht->u.v.nApplyCount;
break;
}
lua_newtable(L);
for(zend_hash_internal_pointer_reset(ht);
zend_hash_get_current_data(ht, (void **)&ppzval) == SUCCESS;
zend_hash_move_forward(ht)) {
char *key = NULL;
uint len = 0;
ulong idx = 0;
zval *zkey= NULL;
switch(zend_hash_get_current_key_ex(ht, &key, &len, &idx, 0, NULL)) {
case HASH_KEY_IS_STRING :
MAKE_STD_ZVAL(zkey);
ZVAL_STRINGL(zkey, key, len - 1, 1);
break;
case HASH_KEY_IS_LONG:
if (idx == 0) {
php_error_docref(NULL TSRMLS_CC, E_STRICT,
"attempt to pass an array index begin with 0 to lua");
}
MAKE_STD_ZVAL(zkey);
ZVAL_LONG(zkey, idx);
break;
//LL1 ---> FIXME
long num_key;
zval * val;
zval zkey;
zend_string *key;
ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, key, val) {
if(Z_TYPE_P(val) == IS_STRING) {
ZVAL_STR(&zkey, key);
}
php_lua_send_zval_to_lua(L, zkey TSRMLS_CC);
php_lua_send_zval_to_lua(L, *ppzval TSRMLS_CC);
if(Z_TYPE_P(val) == IS_LONG) {
ZVAL_LONG(&zkey, Z_LVAL_P(val));
}
php_lua_send_zval_to_lua(L, &zkey TSRMLS_CC);
php_lua_send_zval_to_lua(L, val TSRMLS_CC);
lua_settable(L, -3);
zval_ptr_dtor(&zkey);
}
--ht->nApplyCount;
} ZEND_HASH_FOREACH_END();
--ht->u.v.nApplyCount;
}
}
break;
default:
php_error_docref(NULL TSRMLS_CC, E_ERROR, "unsupported type `%s' for lua"
, zend_zval_type_name(val));
lua_pushnil(L);
return 1;
}
@@ -554,7 +566,7 @@ int php_lua_send_zval_to_lua(lua_State *L, zval *val TSRMLS_DC) {
/*** {{{ static int php_lua_arg_apply_func(void *data, void *L TSRMLS_DC)
*/
static int php_lua_arg_apply_func(void *data, void *L TSRMLS_DC) {
php_lua_send_zval_to_lua((lua_State*)L, *(zval**)data TSRMLS_CC);
php_lua_send_zval_to_lua((lua_State*)L, (zval*)data TSRMLS_CC);
return ZEND_HASH_APPLY_KEEP;
} /* }}} */
@@ -564,39 +576,42 @@ static zval * php_lua_call_lua_function(zval *lua_obj, zval *func, zval *args, i
int bp = 0;
int sp = 0;
int arg_num = 0;
zval rv;
zval *ret = NULL;
lua_State *L = NULL;
L = Z_LUAVAL_P(lua_obj);
if (IS_ARRAY == Z_TYPE_P(func)) {
zval **t = NULL;
zval **f = NULL;
if (zend_hash_index_find(Z_ARRVAL_P(func), 0, (void **)&t) == FAILURE || Z_TYPE_PP(t) != IS_STRING
|| zend_hash_index_find(Z_ARRVAL_P(func), 1, (void **)&f) == FAILURE || Z_TYPE_PP(f) != IS_STRING) {
zval *t = NULL;
zval *f = NULL;
if ((t=zend_hash_index_find(Z_ARRVAL_P(func), 0)) == NULL || Z_TYPE_P(t) != IS_STRING
|| (f=zend_hash_index_find(Z_ARRVAL_P(func), 1)) == NULL || Z_TYPE_P(f) != IS_STRING) {
/* as johannes suggesting use exceptioni to distinguish the error from a lua function return false
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"invalid lua function, argument must be an array which contain two elements: array('table', 'method')");
*/
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC,
"invalid lua function, argument must be an array which contain two elements: array('table', 'method')");
return NULL;
}
#if (LUA_VERSION_NUM < 502)
lua_getfield(L, LUA_GLOBALSINDEX, Z_STRVAL_PP(t));
lua_getfield(L, LUA_GLOBALSINDEX, Z_STRVAL_P(t));
#else
lua_getglobal(L, Z_STRVAL_PP(t));
lua_getglobal(L, Z_STRVAL_P(t));
#endif
if (LUA_TTABLE != lua_type(L, lua_gettop(L))) {
lua_pop(L, -1);
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC, "invalid lua table '%s'", Z_STRVAL_PP(t));
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC, "invalid lua table '%s'", Z_STRVAL_P(t));
return NULL;
}
bp = lua_gettop(L);
lua_getfield(L, -1, Z_STRVAL_PP(f));
lua_getfield(L, -1, Z_STRVAL_P(f));
if (LUA_TFUNCTION != lua_type(L, lua_gettop(L))) {
lua_pop(L, -2);
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC, "invalid lua table function '%s'.%s", Z_STRVAL_PP(t), Z_STRVAL_PP(f));
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC, "invalid lua table function '%s'.%s", Z_STRVAL_P(t), Z_STRVAL_P(f));
return NULL;
}
} else if (IS_STRING == Z_TYPE_P(func)) {
@@ -613,7 +628,7 @@ static zval * php_lua_call_lua_function(zval *lua_obj, zval *func, zval *args, i
}
} else if (IS_OBJECT == Z_TYPE_P(func)
&& instanceof_function(Z_OBJCE_P(func), php_lua_get_closure_ce() TSRMLS_CC)) {
zval *closure = zend_read_property(php_lua_get_closure_ce(), func, ZEND_STRL("_closure"), 1 TSRMLS_CC);
zval *closure = zend_read_property(php_lua_get_closure_ce(), func, ZEND_STRL("_closure"), 1, &rv TSRMLS_CC);
if (!Z_LVAL_P(closure)) {
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC, "invalid lua closure");
return NULL;
@@ -635,7 +650,7 @@ static zval * php_lua_call_lua_function(zval *lua_obj, zval *func, zval *args, i
if (args) {
arg_num += zend_hash_num_elements(Z_ARRVAL_P(args));
zend_hash_apply_with_argument(Z_ARRVAL_P(args), php_lua_arg_apply_func, (void *)L TSRMLS_CC);
zend_hash_apply_with_argument(Z_ARRVAL_P(args), (apply_func_arg_t)php_lua_arg_apply_func, (void *)L TSRMLS_CC);
}
if (lua_pcall(L, arg_num, LUA_MULTRET, 0) != LUA_OK) {
@@ -648,13 +663,13 @@ static zval * php_lua_call_lua_function(zval *lua_obj, zval *func, zval *args, i
sp = lua_gettop(L) - bp;
if (!sp) {
MAKE_STD_ZVAL(ret);
ret = ecalloc(sizeof(zval), 1);
ZVAL_NULL(ret);
} else if (sp == 1) {
ret = php_lua_get_zval_from_lua(L, -1, lua_obj TSRMLS_CC);
} else {
int i = 0;
MAKE_STD_ZVAL(ret);
ret = ecalloc(sizeof(zval), 1);
array_init(ret);
for (i = -sp; i < 0; i++) {
zval *tmp = php_lua_get_zval_from_lua(L, i, lua_obj TSRMLS_CC);
@@ -685,12 +700,15 @@ PHP_METHOD(lua, eval) {
return;
}
bp = lua_gettop(L);
if ((ret = luaL_loadbuffer(L, statements, len, "line")) != LUA_OK || (ret = lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK)) {
zend_throw_exception_ex(lua_exception_ce, ret TSRMLS_CC, "%s", lua_tostring(L, -1));
lua_pop(L, 1);
RETURN_FALSE;
} else {
//php_lua_stack_dump(L);
zval *tmp = NULL;
int ret_count = 0;
int i = 0;
@@ -714,12 +732,22 @@ PHP_METHOD(lua, eval) {
/** {{{ proto Lua::include(string $file)
* run a lua script file
*/
PHP_METHOD(lua, ptest) {
lua_State * L=NULL;
L = Z_LUAVAL_P(getThis());
//php_lua_stack_dump(L);
printf("PP: %p\n", L);
}
PHP_METHOD(lua, include) {
lua_State *L = NULL;
char *file = NULL;
long bp, len = 0;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &len) == FAILURE) {
return;
}
@@ -732,15 +760,14 @@ PHP_METHOD(lua, include) {
) {
RETURN_FALSE;
}
L = Z_LUAVAL_P(getThis());
bp = lua_gettop(L);
bp=lua_gettop(L);
if ((ret = luaL_loadfile(L, file)) != LUA_OK || (ret = lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK)) {
zend_throw_exception_ex(lua_exception_ce, ret TSRMLS_CC, "%s", lua_tostring(L, -1));
lua_pop(L, 1);
RETURN_FALSE;
} else {
} else {
//php_lua_stack_dump(L);
zval *tmp = NULL;
int ret_count = 0;
int i = 0;
@@ -748,19 +775,17 @@ PHP_METHOD(lua, include) {
ret_count = lua_gettop(L) - bp;
if (ret_count > 1) {
array_init(return_value);
for (i = -ret_count; i<0; i++) {
tmp = php_lua_get_zval_from_lua(L, i, getThis() TSRMLS_CC);
add_next_index_zval(return_value, tmp);
}
} else if (ret_count) {
zval *tmp = php_lua_get_zval_from_lua(L, -1, getThis() TSRMLS_CC);
RETURN_ZVAL(tmp, 1, 1);
}
lua_pop(L, ret_count);
}
}
/* }}} */
@@ -787,22 +812,23 @@ PHP_METHOD(lua, call) {
/** {{{ proto Lua::assign(string $name, mix $value)
*/
PHP_METHOD(lua, assign) {
char *name = NULL;
zval *name = NULL;
zval *value = NULL;
lua_State *L = NULL;
int len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name, &len, &value) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &name, &value) == FAILURE) {
return;
}
L = Z_LUAVAL_P(getThis());
php_lua_send_zval_to_lua(L, value TSRMLS_CC);
#if (LUA_VERSION_NUM < 502)
lua_setfield(L, LUA_GLOBALSINDEX, name);
lua_setfield(L, LUA_GLOBALSINDEX, Z_STRVAL_P(name));
#else
lua_setglobal(L, name);
lua_setglobal(L, Z_STRVAL_P(name));
#endif
RETURN_ZVAL(getThis(), 1, 0);
@@ -838,7 +864,7 @@ PHP_METHOD(lua, registerCallback) {
RETURN_FALSE;
}
zval_add_ref(&func);
zval_add_ref(func);
add_next_index_zval(callbacks, func);
RETURN_ZVAL(getThis(), 1, 0);
@@ -848,14 +874,17 @@ PHP_METHOD(lua, registerCallback) {
/** {{{ proto Lua::getVersion()
*/
PHP_METHOD(lua, getVersion) {
RETURN_STRING(LUA_RELEASE, 1);
RETURN_STRING(LUA_RELEASE);
}
/* }}} */
/** {{{ proto Lua::__construct()
*/
PHP_METHOD(lua, __construct) {
lua_State *L = Z_LUAVAL_P(getThis());
//lua_State *L = Z_LUAVAL_P(getThis());
lua_State * L = Z_LUAVAL_P(getThis());
luaL_openlibs(L);
lua_register(L, "print", php_lua_print);
if (ZEND_NUM_ARGS()) {
@@ -871,6 +900,7 @@ zend_function_entry lua_class_methods[] = {
PHP_ME(lua, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(lua, eval, arginfo_lua_eval, ZEND_ACC_PUBLIC)
PHP_ME(lua, include, arginfo_lua_include, ZEND_ACC_PUBLIC)
PHP_ME(lua, ptest, arginfo_lua_include, ZEND_ACC_PUBLIC)
PHP_ME(lua, call, arginfo_lua_call, ZEND_ACC_PUBLIC)
PHP_ME(lua, assign, arginfo_lua_assign, ZEND_ACC_PUBLIC)
PHP_ME(lua, getVersion, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
@@ -900,8 +930,17 @@ PHP_MINIT_FUNCTION(lua) {
lua_ce = zend_register_internal_class(&ce TSRMLS_CC);
lua_ce->create_object = php_lua_create_object;
memcpy(&lua_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
lua_object_handlers.offset = XtOffsetOf(php_lua_object, obj);
lua_object_handlers.clone_obj = NULL;
//lua_object_handlers.dtor_obj = php_lua_dtor_object;
//lua_object_handlers.free_obj = XtOffsetOf(php_lua_object, L);
lua_object_handlers.write_property = php_lua_write_property;
lua_object_handlers.read_property = php_lua_read_property;
@@ -915,12 +954,7 @@ PHP_MINIT_FUNCTION(lua) {
INIT_CLASS_ENTRY(ce, "LuaException", NULL);
lua_exception_ce = zend_register_internal_class_ex(&ce,
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 2)
zend_exception_get_default(),
#else
zend_exception_get_default(TSRMLS_C),
#endif
NULL TSRMLS_CC);
zend_exception_get_default(TSRMLS_C));
return SUCCESS;
}

View File

@@ -63,16 +63,16 @@ PHP_METHOD(lua_closure, __construct) {
/** {{{ proto LuaClosure::__destruct()
*/
PHP_METHOD(lua_closure, __destruct) {
zval *lua_obj, *closure;
zval *lua_obj, *closure, rv;
lua_obj = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_lua_object"), 1 TSRMLS_CC);
lua_obj = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_lua_object"), 1, &rv TSRMLS_CC);
if (ZVAL_IS_NULL(lua_obj)
|| Z_TYPE_P(lua_obj) != IS_OBJECT
|| !instanceof_function(Z_OBJCE_P(lua_obj), lua_ce TSRMLS_CC)) {
RETURN_FALSE;
}
closure = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_closure"), 1 TSRMLS_CC);
closure = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_closure"), 1, &rv TSRMLS_CC);
if (!Z_LVAL_P(closure)) {
RETURN_FALSE;
}
@@ -85,32 +85,33 @@ PHP_METHOD(lua_closure, __destruct) {
*/
PHP_METHOD(lua_closure, invoke) {
int bp, sp;
zval ***arguments = NULL;
zval *arguments = NULL;
zval *lua_obj = NULL;
lua_State *L = NULL;
zval *closure = NULL;
zval rv;
if (ZEND_NUM_ARGS()) {
arguments = emalloc(sizeof(zval**) * 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(lua_exception_ce, 0 TSRMLS_CC, "cannot get arguments for calling closure");
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "cannot get arguments for calling closure");
return;
}
}
lua_obj = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_lua_object"), 1 TSRMLS_CC);
lua_obj = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_lua_object"), 1, &rv TSRMLS_CC);
if (ZVAL_IS_NULL(lua_obj)
|| Z_TYPE_P(lua_obj) != IS_OBJECT
|| !instanceof_function(Z_OBJCE_P(lua_obj), lua_ce TSRMLS_CC)) {
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC, "corrupted Lua object");
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "corrupted Lua object");
return;
}
closure = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_closure"), 1 TSRMLS_CC);
closure = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_closure"), 1, &rv TSRMLS_CC);
if (!Z_LVAL_P(closure)) {
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC, "invalid lua closure");
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "invalid lua closure");
return;
}
@@ -120,14 +121,14 @@ PHP_METHOD(lua_closure, invoke) {
lua_rawgeti(L, LUA_REGISTRYINDEX, Z_LVAL_P(closure));
if (LUA_TFUNCTION != lua_type(L, lua_gettop(L))) {
lua_pop(L, -1);
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC, "call to lua closure failed");
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "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]) TSRMLS_CC);
php_lua_send_zval_to_lua(L, &arguments[i] TSRMLS_CC);
}
}
@@ -136,7 +137,7 @@ PHP_METHOD(lua_closure, invoke) {
efree(arguments);
}
lua_pop(L, lua_gettop(L) - bp);
zend_throw_exception_ex(lua_exception_ce, 0 TSRMLS_CC,
zend_throw_exception_ex(NULL, 0 TSRMLS_CC,
"call to lua function %s failed", lua_tostring(L, -1));
return;
}
@@ -184,7 +185,7 @@ zend_function_entry lua_closure_methods[] = {
};
/* }}} */
static void php_lua_closure_dtor_object(void *object, zend_object_handle handle TSRMLS_DC) /* {{{ */
static void php_lua_closure_dtor_object(void *object, zend_object_handlers handle TSRMLS_DC) /* {{{ */
{
zend_object *obj = (zend_object*)object;
@@ -192,31 +193,28 @@ static void php_lua_closure_dtor_object(void *object, zend_object_handle handle
efree(obj);
} /* }}} */
static zend_object_value php_lua_closure_create_object(zend_class_entry *ce TSRMLS_DC) /* {{{ */
zend_object *php_lua_closure_create_object(zend_class_entry *ce)
{
zend_object_value obj = {0};
zend_object *lua_closure_obj = NULL;
zend_object* intern;
lua_closure_obj = emalloc(sizeof(zend_object));
intern = emalloc(sizeof(zend_object)+ sizeof(zval) * (ce->default_properties_count - 1));
zend_object_std_init(lua_closure_obj, ce TSRMLS_CC);
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
zend_hash_copy(lua_closure_obj->properties, &ce->default_properties,
#if (PHP_MINOR_VERSION < 4)
(copy_ctor_func_t) zval_add_ref,
#else
zval_copy_property_ctor(ce),
#endif
(void *)0, sizeof(zval *));
#elif (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION > 3)
object_properties_init(lua_closure_obj, ce);
#endif
if (!intern) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "alloc memory for lua object failed");
}
obj.handle = zend_objects_store_put(lua_closure_obj, php_lua_closure_dtor_object, NULL, NULL TSRMLS_CC);
obj.handlers = zend_get_std_object_handlers();
return obj;
zend_object_std_init(intern, ce TSRMLS_CC);
object_properties_init(intern, ce TSRMLS_CC);
intern->handlers = zend_get_std_object_handlers();
return intern;
} /* }}} */
void php_lua_closure_register(TSRMLS_D) /* {{{ */

View File

@@ -55,15 +55,26 @@ extern zend_module_entry lua_module_entry;
#endif
#define PHP_LUA_VERSION "1.1.1-dev"
#define Z_LUAVAL_P(obj) ((php_lua_object*)(zend_object_store_get_object(obj TSRMLS_CC)))->L
struct _php_lua_object {
zend_object obj;
lua_State *L;
lua_State *L;
zend_object obj;
};
typedef struct _php_lua_object php_lua_object;
static inline lua_State *php_lua_obj_from_obj(zend_object *obj) {
php_lua_object * o;
o=(php_lua_object*)((char*)(obj)-XtOffsetOf(php_lua_object, obj));
return o->L;
}
#define Z_LUAVAL_P(obj) php_lua_obj_from_obj(Z_OBJ_P((obj)))
zval * php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj TSRMLS_DC);
int php_lua_send_zval_to_lua(lua_State *L, zval *val TSRMLS_DC);

4
tests/001.phpt Normal file → Executable file
View File

@@ -15,7 +15,9 @@ try {
assert($e->getCode() == LUA_ERRSYNTAX);
echo "\n", $e->getMessage();
}
?>
--EXPECTF--
12
-0.53657291800043
[string "line"]:1: %s near 'code'
[string "line"]:1: syntax error near 'code'

30
tests/0014.phpt Normal file
View File

@@ -0,0 +1,30 @@
--TEST--
Check for Table Pass vic-a-verse
--SKIPIF--
<?php if (!extension_loaded("lua")) print "skip"; ?>
--FILE--
<?php
$l = new lua();
$l->eval(<<<CODE
function test(a)
lua_fcn(a)
end
CODE
);
$l->registerCallback("lua_fcn", function($a) {
ksort($a);
var_dump($a);
});
$l->test(array("key1"=>"v1",
"key2"=>"v2"));
--EXPECT--
array(2) {
["key1"]=>
string(2) "v1"
["key2"]=>
string(2) "v2"
}

7
travis/build_check.sh Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/sh
#exit 1 on error
echo "all ok ";
ls ./modules/bartlby.so
exit $?

14
travis/compile.sh Executable file
View File

@@ -0,0 +1,14 @@
phpize
./configure --with-lua-version=5.2
make
EX=$?;
if [ $EX != 0 ];
then
echo "compile failed";
exit $EX;
fi;
exit $EX;