| 13 |
| license@php.net so we can mail you a copy immediately. | |
| license@php.net so we can mail you a copy immediately. | |
| 14 |
+----------------------------------------------------------------------+ |
+----------------------------------------------------------------------+ |
| 15 |
| Author: Johannes Schlueter <johannes@php.net> | |
| Author: Johannes Schlueter <johannes@php.net> | |
| 16 |
| Marcelo Araujo <msaraujo@php.net> | |
| Marcelo Araujo <msaraujo@php.net> | |
| 17 |
|
| Bertrand Mansion <mansion@php.net> | |
| 18 |
+----------------------------------------------------------------------+ |
+----------------------------------------------------------------------+ |
| 19 |
*/ |
*/ |
| 20 |
|
|
| 27 |
#include "ext/standard/info.h" |
#include "ext/standard/info.h" |
| 28 |
#include "php_lua.h" |
#include "php_lua.h" |
| 29 |
|
|
| 30 |
#include "lua.h" |
#include <lua.h> |
| 31 |
#include "lauxlib.h" |
#include <lauxlib.h> |
| 32 |
#include "lualib.h" |
#include <lualib.h> |
| 33 |
|
|
| 34 |
#define EXT_LUA_VERSION PHP_LUA_VERSION |
#define EXT_LUA_VERSION PHP_LUA_VERSION |
| 35 |
|
|
| 89 |
HashPosition pointer; |
HashPosition pointer; |
| 90 |
int array_count; |
int array_count; |
| 91 |
char *key; |
char *key; |
| 92 |
int key_len; |
unsigned int key_len; |
| 93 |
long index; |
unsigned long index; |
| 94 |
int hash_key; |
int hash_key; |
| 95 |
|
|
| 96 |
zval *zvalue,*zkey; |
zval *zvalue,*zkey; |
| 102 |
|
|
| 103 |
lua_newtable(L); |
lua_newtable(L); |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer); zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS; zend_hash_move_forward_ex(arr_hash, &pointer)) |
for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer); zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS; zend_hash_move_forward_ex(arr_hash, &pointer)) |
| 108 |
|
|
| 109 |
{ |
{ |
| 110 |
hash_key=zend_hash_get_current_key_ex(arr_hash, &key, &key_len, &index, 0, &pointer); |
hash_key=zend_hash_get_current_key_ex(arr_hash, &key, &key_len, &index, 0, &pointer); |
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
if (hash_key == HASH_KEY_IS_LONG && index == 0) { |
if (hash_key == HASH_KEY_IS_LONG && index == 0) { |
| 117 |
|
|
| 118 |
|
|
| 119 |
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to push array index %d to lua which is unsupported in lua. Element has been discarded",index); |
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to push array index %d to lua which is unsupported in lua. Element has been discarded",index); |
| 120 |
} |
} |
| 121 |
else { |
else { |
| 135 |
|
|
| 136 |
php_lua_write_real_property(L,-3,zkey,zvalue TSRMLS_CC); |
php_lua_write_real_property(L,-3,zkey,zvalue TSRMLS_CC); |
| 137 |
} |
} |
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
} |
} |
| 144 |
zval_ptr_dtor(&zkey); |
zval_ptr_dtor(&zkey); |
| 145 |
} /* }}} */ |
} /* }}} */ |
| 185 |
|
|
| 186 |
} /* }}} */ |
} /* }}} */ |
| 187 |
|
|
| 188 |
|
|
| 189 |
static int php_lua_push_apply_func(void *pDest, void *argument TSRMLS_DC) /* {{{ */ |
static int php_lua_push_apply_func(void *pDest, void *argument TSRMLS_DC) /* {{{ */ |
| 190 |
{ |
{ |
| 191 |
php_lua_push_zval((lua_State*)argument, *(zval**)pDest TSRMLS_CC); |
php_lua_push_zval((lua_State*)argument, *(zval**)pDest TSRMLS_CC); |
| 192 |
return ZEND_HASH_APPLY_KEEP; |
return ZEND_HASH_APPLY_KEEP; |
| 193 |
} /* }}} */ |
} /* }}} */ |
| 194 |
|
|
| 195 |
|
|
| 196 |
static void php_lua_get_zval_from_stack(lua_State *L, int index, zval *ret TSRMLS_DC) /* {{{ */ |
static void php_lua_get_zval_from_stack(lua_State *L, int index, zval *ret TSRMLS_DC) /* {{{ */ |
| 197 |
{ |
{ |
|
char *value; |
|
|
size_t value_len; |
|
|
|
|
| 198 |
switch (lua_type(L, index)) { |
switch (lua_type(L, index)) { |
| 199 |
|
case LUA_TNIL: |
| 200 |
|
ZVAL_NULL(ret); |
| 201 |
|
break; |
| 202 |
|
|
| 203 |
case LUA_TBOOLEAN: |
case LUA_TBOOLEAN: |
| 204 |
ZVAL_BOOL(ret, lua_toboolean(L, index)); |
ZVAL_BOOL(ret, lua_toboolean(L, index)); |
| 205 |
break; |
break; |
| 206 |
|
|
| 207 |
case LUA_TNUMBER: |
case LUA_TNUMBER: { |
| 208 |
ZVAL_DOUBLE(ret, lua_tonumber(L, index)); |
lua_Number num = lua_tonumber(L, index); |
| 209 |
|
if (num != (long)num) { |
| 210 |
|
ZVAL_DOUBLE(ret, lua_tonumber(L, index)); |
| 211 |
|
} else { |
| 212 |
|
ZVAL_LONG(ret, num); |
| 213 |
|
} |
| 214 |
break; |
break; |
| 215 |
|
} |
| 216 |
|
|
| 217 |
case LUA_TSTRING: |
case LUA_TSTRING: { |
| 218 |
value = lua_tolstring(L, index, &value_len); |
size_t value_len; |
| 219 |
|
char *value = NULL; |
| 220 |
|
value = (char *)lua_tolstring(L, index, &value_len); |
| 221 |
ZVAL_STRINGL(ret, value, value_len, 1); |
ZVAL_STRINGL(ret, value, value_len, 1); |
| 222 |
break; |
break; |
| 223 |
|
} |
| 224 |
|
|
| 225 |
case LUA_TTABLE: |
case LUA_TTABLE: |
| 226 |
array_init(ret); |
array_init(ret); |
|
/* notify lua to traverse the table */ |
|
|
lua_pushnil(L); |
|
| 227 |
|
|
| 228 |
zval *akey,*aval; |
zval *akey,*aval; |
| 229 |
ALLOC_INIT_ZVAL(akey); |
ALLOC_INIT_ZVAL(akey); |
| 230 |
ALLOC_INIT_ZVAL(aval); |
ALLOC_INIT_ZVAL(aval); |
| 231 |
|
|
| 232 |
|
/* notify lua to traverse the table */ |
| 233 |
|
lua_pushnil(L); |
| 234 |
/* table has been moved by one because of the pushnil */ |
/* table has been moved by one because of the pushnil */ |
| 235 |
while (lua_next(L, index-1) != 0) |
while (lua_next(L, index-1) != 0) { |
| 236 |
{ |
|
| 237 |
MAKE_STD_ZVAL(akey); |
MAKE_STD_ZVAL(akey); |
| 238 |
MAKE_STD_ZVAL(aval); |
MAKE_STD_ZVAL(aval); |
| 239 |
|
|
| 240 |
/* `key' is at index -2 and `value' at index -1 */ |
/* `key' is at index -2 and `value' at index -1 */ |
| 241 |
php_lua_get_zval_from_stack(L,-2,akey); |
php_lua_get_zval_from_stack(L, -2, akey); |
| 242 |
php_lua_get_zval_from_stack(L,-1,aval); |
php_lua_get_zval_from_stack(L, -1, aval); |
| 243 |
|
|
| 244 |
switch(Z_TYPE_P(akey)) |
switch (Z_TYPE_P(akey)) { |
| 245 |
{ |
/* lua can use anything but nil as index */ |
| 246 |
/* lua can't use (at least when i tried ;) ) floats as array keys so that should be safe */ |
case IS_LONG: |
| 247 |
case IS_DOUBLE: |
add_index_zval(ret, Z_LVAL_P(akey), aval); |
|
add_index_zval(ret,(long)Z_DVAL_P(akey),aval); |
|
| 248 |
break; |
break; |
| 249 |
|
case IS_DOUBLE: |
| 250 |
|
convert_to_string(akey); |
| 251 |
default: |
default: |
| 252 |
add_assoc_zval(ret,Z_STRVAL_P(akey),aval); |
add_assoc_zval(ret, Z_STRVAL_P(akey), aval); |
| 253 |
break; |
break; |
| 254 |
} |
} |
| 255 |
lua_pop(L, 1); /* removes `value'; keeps `key' for next iteration */ |
lua_pop(L, 1); /* removes `value'; keeps `key' for next iteration */ |
| 256 |
} |
} |
| 257 |
break; |
break; |
| 258 |
|
|
|
|
|
| 259 |
case LUA_TFUNCTION: |
case LUA_TFUNCTION: |
| 260 |
/* TODO: Make this a PHP Function */ |
/* TODO: Make this a PHP Function */ |
| 261 |
|
|
| 262 |
default: |
default: |
| 263 |
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid type `%i' passed from lua.", lua_type(L, index)); |
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid type `%i' passed from lua.", lua_type(L, index)); |
| 264 |
/* fall-through */ |
/* fall-through */ |
|
|
|
|
case LUA_TNIL: |
|
|
ZVAL_NULL(ret); |
|
|
break; |
|
| 265 |
} |
} |
| 266 |
|
|
| 267 |
} /* }}} */ |
} /* }}} */ |
| 268 |
|
|
| 269 |
|
|
| 270 |
static int php_lua_print(lua_State *L) /* {{{ */ |
static int php_lua_print(lua_State *L) /* {{{ */ |
| 271 |
{ |
{ |
| 272 |
int i; |
int i; |
| 279 |
return 0; |
return 0; |
| 280 |
} /* }}} */ |
} /* }}} */ |
| 281 |
|
|
| 282 |
|
|
| 283 |
static void php_lua_object_dtor(void *object, zend_object_handle handle TSRMLS_DC) /* {{{ */ |
static void php_lua_object_dtor(void *object, zend_object_handle handle TSRMLS_DC) /* {{{ */ |
| 284 |
{ |
{ |
| 285 |
php_lua_object *intern = (php_lua_object*)object; |
php_lua_object *intern = (php_lua_object*)object; |
| 292 |
efree(intern); |
efree(intern); |
| 293 |
} /* }}} */ |
} /* }}} */ |
| 294 |
|
|
| 295 |
|
|
| 296 |
static zval *php_lua_read_property(zval *obj, zval *prop, int type TSRMLS_DC) /* {{{ */ |
static zval *php_lua_read_property(zval *obj, zval *prop, int type TSRMLS_DC) /* {{{ */ |
| 297 |
{ |
{ |
| 298 |
zval *retval; |
zval *retval; |
| 336 |
static void php_lua_write_property(zval *obj, zval *prop, zval *value TSRMLS_DC) /* {{{ */ |
static void php_lua_write_property(zval *obj, zval *prop, zval *value TSRMLS_DC) /* {{{ */ |
| 337 |
{ |
{ |
| 338 |
/* TODO: Use proper type for lua and separate only when needed */ |
/* TODO: Use proper type for lua and separate only when needed */ |
| 339 |
|
|
| 340 |
lua_State *L = getLuaZ(obj); |
lua_State *L = getLuaZ(obj); |
| 341 |
|
|
| 342 |
LUA_STACK_START(L); |
LUA_STACK_START(L); |
| 349 |
LUA_STACK_END(L); |
LUA_STACK_END(L); |
| 350 |
} /* }}} */ |
} /* }}} */ |
| 351 |
|
|
| 352 |
|
|
| 353 |
static int php_lua_atpanic(lua_State *L) { /* {{{ */ |
static int php_lua_atpanic(lua_State *L) { /* {{{ */ |
| 354 |
TSRMLS_FETCH(); |
TSRMLS_FETCH(); |
| 355 |
php_error_docref(NULL TSRMLS_CC, E_WARNING, "lua panic (%s)", lua_tostring(L, 1)); |
php_error_docref(NULL TSRMLS_CC, E_WARNING, "lua panic (%s)", lua_tostring(L, 1)); |
| 358 |
return 0; |
return 0; |
| 359 |
} /* }}} */ |
} /* }}} */ |
| 360 |
|
|
| 361 |
|
|
| 362 |
static zend_object_value php_lua_create_object(zend_class_entry *ce TSRMLS_DC) /* {{{ */ |
static zend_object_value php_lua_create_object(zend_class_entry *ce TSRMLS_DC) /* {{{ */ |
| 363 |
{ |
{ |
| 364 |
zval tmp; |
zval tmp; |
| 381 |
return retval; |
return retval; |
| 382 |
} /* }}} */ |
} /* }}} */ |
| 383 |
|
|
| 384 |
|
|
| 385 |
/* {{{ lua::__construct() |
/* {{{ lua::__construct() |
| 386 |
Create new LUA instance and opens lua base library*/ |
Create new LUA instance and opens lua base library*/ |
| 387 |
PHP_METHOD(lua, __construct) |
PHP_METHOD(lua, __construct) |
| 388 |
{ |
{ |
| 389 |
|
|
| 390 |
/* TODO: load all modules or exclusively load standard lua library? - check this later on*/ |
/* TODO: load all modules or exclusively load standard lua library? - check this later on*/ |
| 391 |
|
|
| 392 |
lua_State *L = getLua(); |
lua_State *L = getLua(); |
| 393 |
|
|
| 394 |
LUA_STACK_START(L); |
LUA_STACK_START(L); |
| 400 |
} |
} |
| 401 |
/* }}} */ |
/* }}} */ |
| 402 |
|
|
| 403 |
|
|
| 404 |
#define RETURN_STR "return %s" |
#define RETURN_STR "return %s" |
| 405 |
|
|
| 406 |
|
|
| 407 |
/* {{{ lua::__call(string method, array args [, int nresults]) |
/* {{{ lua::__call(string method, array args [, int nresults]) |
| 408 |
Call a lua function from within PHP */ |
Call a lua function from within PHP */ |
| 409 |
PHP_METHOD(lua, __call) |
PHP_METHOD(lua, __call) |
| 472 |
} |
} |
| 473 |
/* }}} */ |
/* }}} */ |
| 474 |
|
|
| 475 |
|
|
| 476 |
/* {{{ void lua::evaluate(string code) |
/* {{{ void lua::evaluate(string code) |
| 477 |
Evaluates code with lua */ |
Evaluates code with lua */ |
| 478 |
PHP_METHOD(lua, evaluate) |
PHP_METHOD(lua, evaluate) |
| 498 |
} |
} |
| 499 |
/* }}} */ |
/* }}} */ |
| 500 |
|
|
| 501 |
|
|
| 502 |
/* {{{ void lua::evaluatefile(string file) |
/* {{{ void lua::evaluatefile(string file) |
| 503 |
Evaluates a lua script */ |
Evaluates a lua script */ |
| 504 |
PHP_METHOD(lua, evaluatefile) |
PHP_METHOD(lua, evaluatefile) |
| 533 |
} |
} |
| 534 |
/* }}} */ |
/* }}} */ |
| 535 |
|
|
| 536 |
|
|
| 537 |
/* {{{ void lua::loadModule(string module) |
/* {{{ void lua::loadModule(string module) |
| 538 |
Load a lua's module at runtime */ |
Load a lua's module at runtime */ |
| 539 |
PHP_METHOD(lua, loadmodule) |
PHP_METHOD(lua, loadmodule) |
| 551 |
} |
} |
| 552 |
/* }}} */ |
/* }}} */ |
| 553 |
|
|
| 554 |
|
|
| 555 |
/* {{{ string lua::getVersion() |
/* {{{ string lua::getVersion() |
| 556 |
Return Lua's version */ |
Return Lua's version */ |
| 557 |
PHP_METHOD(lua, getversion) |
PHP_METHOD(lua, getversion) |
| 560 |
} |
} |
| 561 |
/* }}} */ |
/* }}} */ |
| 562 |
|
|
| 563 |
|
|
| 564 |
/* {{{ lua_functions[] */ |
/* {{{ lua_functions[] */ |
| 565 |
zend_function_entry lua_functions[] = { |
zend_function_entry lua_functions[] = { |
| 566 |
{NULL, NULL, NULL} |
{NULL, NULL, NULL} |
| 573 |
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_call, 0, 0, 2) |
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_call, 0, 0, 2) |
| 574 |
ZEND_ARG_INFO(0, method) |
ZEND_ARG_INFO(0, method) |
| 575 |
ZEND_ARG_INFO(0, args) |
ZEND_ARG_INFO(0, args) |
| 576 |
ZEND_END_ARG_INFO() |
ZEND_END_ARG_INFO(); |
| 577 |
|
|
| 578 |
static |
static |
| 579 |
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_evaluate, 0, 0, 1) |
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_evaluate, 0, 0, 1) |
| 580 |
ZEND_ARG_INFO(0, code) |
ZEND_ARG_INFO(0, code) |
| 581 |
ZEND_END_ARG_INFO() |
ZEND_END_ARG_INFO(); |
| 582 |
|
|
| 583 |
static |
static |
| 584 |
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_evaluatefile, 0, 0, 1) |
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_evaluatefile, 0, 0, 1) |
| 585 |
ZEND_ARG_INFO(0, file) |
ZEND_ARG_INFO(0, file) |
| 586 |
ZEND_END_ARG_INFO() |
ZEND_END_ARG_INFO(); |
| 587 |
|
|
| 588 |
static |
static |
| 589 |
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_loadmodule, 0, 0, 1) |
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_loadmodule, 0, 0, 1) |
| 590 |
ZEND_ARG_INFO(0, module) |
ZEND_ARG_INFO(0, module) |
| 591 |
ZEND_END_ARG_INFO() |
ZEND_END_ARG_INFO(); |
| 592 |
|
|
| 593 |
|
|
| 594 |
/* }}} */ |
/* }}} */ |