extension.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <assert.h>
  6. /**
  7. * PHP includes
  8. */
  9. #include <php.h>
  10. #include <zend_exceptions.h>
  11. #include <zend_interfaces.h>
  12. #include <zend_ini.h>
  13. #include <SAPI.h>
  14. #include <zend_hash.h>
  15. #include <zend_API.h>
  16. #ifdef ZTS
  17. #include "TSRM.h"
  18. #endif
  19. #include "_cgo_export.h"
  20. // #include "sztypes.h"
  21. #include "../zend/goapi.h"
  22. #include "../zend/clog.h"
  23. #include "objectmap.h"
  24. #include "class.h"
  25. #include "../zend/customize.h"
  26. // TODO PHP7支持
  27. static phpgo_object_map* g_module_map = NULL;
  28. static zend_module_entry g_entry = {0};
  29. static phpgo_object_map* g_class_map = NULL;
  30. static phpgo_object_map* g_cbinfo_map = NULL;
  31. // ZEND_DECLARE_MODULE_GLOBALS(phpgo);
  32. // static void init_globals(zend_phpgo_globals *globals) {}
  33. static int(*phpgo_module_startup_cbfunc)(int, int) = 0;
  34. static int(*phpgo_module_shutdown_cbfunc)(int, int) = 0;
  35. static int(*phpgo_request_startup_cbfunc)(int, int) = 0;
  36. static int(*phpgo_request_shutdown_cbfunc)(int, int) = 0;
  37. int phpgo_module_startup_func(int type, int module_number TSRMLS_DC)
  38. {
  39. if (phpgo_module_startup_cbfunc) {
  40. return call_golang_function(phpgo_module_startup_cbfunc, type, module_number, 0, 0, 0, 0, 0, 0, 0, 0);
  41. }
  42. return 0;
  43. }
  44. int phpgo_module_shutdown_func(int type, int module_number TSRMLS_DC)
  45. {
  46. if (phpgo_module_shutdown_cbfunc) {
  47. return call_golang_function(phpgo_module_shutdown_cbfunc, type, module_number, 0, 0, 0, 0, 0, 0, 0, 0);
  48. }
  49. return 0;
  50. }
  51. int phpgo_request_startup_func(int type, int module_number TSRMLS_DC)
  52. {
  53. if (phpgo_request_startup_cbfunc) {
  54. return call_golang_function(phpgo_request_startup_cbfunc, type, module_number, 0, 0, 0, 0, 0, 0, 0, 0);
  55. }
  56. return 0;
  57. }
  58. int phpgo_request_shutdown_func(int type, int module_number TSRMLS_DC)
  59. {
  60. if (phpgo_request_shutdown_cbfunc) {
  61. return call_golang_function(phpgo_request_shutdown_cbfunc, type, module_number, 0, 0, 0, 0, 0, 0, 0, 0);
  62. }
  63. return 0;
  64. }
  65. void phpgo_register_init_functions(void *module_startup_func, void *module_shutdown_func,
  66. void *request_startup_func, void *request_shutdown_func)
  67. {
  68. phpgo_module_startup_cbfunc = (int (*)(int, int))module_startup_func;
  69. phpgo_module_shutdown_cbfunc = (int (*)(int, int))module_shutdown_func;
  70. phpgo_request_startup_cbfunc = (int (*)(int, int))request_startup_func;
  71. phpgo_request_shutdown_cbfunc = (int (*)(int, int))request_shutdown_func;
  72. return;
  73. }
  74. // TODO module name from args
  75. // TODO module version from args
  76. // TODO module startup/shutdown... function from args, or set seperator
  77. void *phpgo_get_module(char *name, char *version) {
  78. char *cname = (char*)GLOBAL_VCLASS_NAME;
  79. phpgo_class_entry* pce = (phpgo_class_entry*)phpgo_object_map_get(g_class_map, cname);
  80. zend_function_entry* funcs = phpgo_class_get_funcs(pce);
  81. zend_module_entry te = {
  82. STANDARD_MODULE_HEADER,
  83. name, // "phpgo",
  84. funcs, // g_funcs[GLOBAL_VCLASS_ID],
  85. phpgo_module_startup_func,
  86. phpgo_module_shutdown_func,
  87. phpgo_request_startup_func, /* Replace with NULL if there's nothing to do at request start */
  88. phpgo_request_shutdown_func, /* Replace with NULL if there's nothing to do at request end */
  89. NULL,
  90. version, // "1.0",
  91. STANDARD_MODULE_PROPERTIES
  92. };
  93. memcpy(&g_entry, &te, sizeof(zend_module_entry));
  94. return &g_entry;
  95. }
  96. int phpgo_get_module_number() {
  97. return g_entry.module_number;
  98. }
  99. // TODO 支持php callable类型,方便实现回调?
  100. // TODO 支持php array类型?有点复杂。
  101. char *type2name(int type)
  102. {
  103. switch (type) {
  104. case IS_ARRAY:
  105. return "array";
  106. case IS_STRING:
  107. return "string";
  108. case IS_DOUBLE:
  109. return "double";
  110. #ifdef ZEND_ENGINE_3
  111. case _IS_BOOL:
  112. #else
  113. case IS_BOOL:
  114. #endif
  115. return "bool";
  116. case IS_LONG:
  117. return "long";
  118. #ifdef ZEND_ENGINE_3
  119. case IS_UNDEF:
  120. #endif
  121. case IS_NULL:
  122. return "void";
  123. case IS_OBJECT:
  124. return "object";
  125. case IS_RESOURCE:
  126. return "pointer";
  127. case IS_CALLABLE:
  128. return "callable";
  129. #ifdef IS_LEXICAL_VAR
  130. case IS_LEXICAL_VAR:
  131. return "var";
  132. #endif
  133. default:
  134. return "unknown";
  135. }
  136. return NULL;
  137. }
  138. // 计算要转换的参数个数,有效的参数个数
  139. // php提供的参数个数要>=go函数需要的参数个数
  140. // 如果go是变长参数个数, 则计算出 最小 值,并把php提供的所有参数全部传递
  141. static int phpgo_function_num_args(int cbid, phpgo_callback_info* cbi, int supply_num_args)
  142. {
  143. char* arg_types = phpgo_callback_info_get_arg_types(cbi);
  144. int num_args = arg_types != NULL ? strlen(arg_types) : 0;
  145. return num_args;
  146. }
  147. // PHP函数参数转换为go类型的参数
  148. static void* phpgo_function_conv_arg(int cbid, int idx, char ch, int zty, zval *zarg)
  149. {
  150. int prmty = zty;
  151. void *rv = NULL;
  152. #ifdef ZEND_ENGINE_3
  153. zval *conv_zarg = zarg;
  154. zval *macro_zarg = zarg;
  155. #else
  156. zval **conv_zarg = &zarg;
  157. zval *macro_zarg = zarg;
  158. #endif
  159. if (ch == 's') {
  160. if (prmty != IS_STRING) {
  161. zend_error(E_WARNING, "Parameters type not match, need (%c), given: %s",
  162. ch, type2name(prmty));
  163. }
  164. convert_to_string_ex(conv_zarg);
  165. char *arg = Z_STRVAL_P(macro_zarg);
  166. dlog_debug("arg%d(%c), %s(%d)", idx, ch, arg, Z_STRLEN_P(macro_zarg));
  167. goapi_new_value(GT_String, (uint64_t)arg, &rv);
  168. } else if (ch == 'l') {
  169. if (prmty != IS_LONG) {
  170. zend_error(E_WARNING, "Parameters type not match, need (%c), given: %s",
  171. ch, type2name(prmty));
  172. dlog_error("Parameters type not match, need (%c), given: %s",
  173. ch, type2name(prmty));
  174. }
  175. convert_to_long_ex(conv_zarg);
  176. long arg = (long)Z_LVAL_P(macro_zarg);
  177. dlog_debug("arg%d(%c), %d", idx, ch, arg);
  178. goapi_new_value(GT_Int64, (uint64_t)arg, &rv);
  179. } else if (ch == 'b') {
  180. convert_to_boolean_ex(conv_zarg);
  181. #ifdef ZEND_ENGINE_3
  182. if (Z_TYPE_P(conv_zarg) == IS_TRUE) {
  183. goapi_new_value(GT_Bool, (uint64_t)1, &rv);
  184. } else {
  185. goapi_new_value(GT_Bool, (uint64_t)0, &rv);
  186. }
  187. #else
  188. zend_bool arg = (zend_bool)Z_BVAL_P(macro_zarg);
  189. goapi_new_value(GT_Bool, (uint64_t)arg, &rv);
  190. #endif
  191. } else if (ch == 'd') {
  192. convert_to_double_ex(conv_zarg);
  193. double arg = (double)Z_DVAL_P(macro_zarg);
  194. double* parg = calloc(1, sizeof(double));
  195. *parg = (double)Z_DVAL_P(macro_zarg);
  196. // argv[idx] = (void*)(ulong)arg; // error
  197. // memcpy(&argv[idx], &arg, sizeof(argv[idx])); // ok
  198. // float32(uintptr(ax))
  199. goapi_new_value(GT_Float64, (uint64_t)parg, &rv);
  200. } else if (ch == 'a') {
  201. if (Z_TYPE_P(macro_zarg) == IS_STRING) {
  202. char *arg = Z_STRVAL_P(macro_zarg);
  203. goapi_new_value(GT_String, (uint64_t)arg, &rv);
  204. #ifdef ZEND_ENGINE_3
  205. } else if (Z_TYPE_P(macro_zarg) == IS_TRUE) {
  206. goapi_new_value(GT_Bool, (uint64_t)1, &rv);
  207. } else if (Z_TYPE_P(macro_zarg) == IS_FALSE) {
  208. goapi_new_value(GT_Bool, (uint64_t)0, &rv);
  209. #else
  210. } else if (Z_TYPE_P(macro_zarg) == IS_BOOL) {
  211. zend_bool arg = (zend_bool)Z_BVAL_P(macro_zarg);
  212. goapi_new_value(GT_Bool, (uint64_t)arg, &rv);
  213. #endif
  214. } else if (Z_TYPE_P(macro_zarg) == IS_DOUBLE) {
  215. double* parg = calloc(1, sizeof(double));
  216. *parg = (double)Z_DVAL_P(macro_zarg);
  217. goapi_new_value(GT_Float64, (uint64_t)parg, &rv);
  218. } else if (Z_TYPE_P(macro_zarg) == IS_LONG) {
  219. long arg = (long)Z_LVAL_P(macro_zarg);
  220. goapi_new_value(GT_Int64, (uint64_t)arg, &rv);
  221. } else if (Z_TYPE_P(zarg) == IS_OBJECT) {
  222. void *parg = (void*)macro_zarg;
  223. goapi_new_value(GT_UnsafePointer, (uint64_t)parg, &rv);
  224. } else if (Z_TYPE_P(zarg) == IS_RESOURCE) {
  225. void *parg = (void*)macro_zarg;
  226. goapi_new_value(GT_UnsafePointer, (uint64_t)parg, &rv);
  227. } else {
  228. dlog_debug("arg%d(%c), %s(%d) unsported to Any", idx, ch,
  229. type2name(Z_TYPE_P(macro_zarg)), Z_TYPE_P(macro_zarg));
  230. }
  231. // TODO array/vector convert
  232. } else if (ch == 'v') {
  233. HashTable *arr_hash = Z_ARRVAL_P(macro_zarg);
  234. HashPosition pos;
  235. zval *edata = NULL;
  236. for (zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
  237. #ifdef ZEND_ENGINE_3
  238. (edata = zend_hash_get_current_data_ex(arr_hash, &pos)) != NULL;
  239. #else
  240. zend_hash_get_current_data_ex(arr_hash, (void**)&edata, &pos) == SUCCESS;
  241. #endif
  242. zend_hash_move_forward_ex(arr_hash, &pos)) {
  243. #ifdef ZEND_ENGINE_3
  244. zval *conv_edata = edata;
  245. #else
  246. zval **conv_edata = &edata;
  247. #endif
  248. if (rv == NULL) {
  249. switch (Z_TYPE_P(edata)) {
  250. case IS_LONG:
  251. goapi_array_new(GT_Int64, &rv);
  252. break;
  253. default:
  254. goapi_array_new(GT_String, &rv);
  255. break;
  256. }
  257. }
  258. switch (Z_TYPE_P(edata)) {
  259. case IS_LONG:
  260. goapi_array_push(rv, (void*)Z_LVAL_P(edata), &rv);
  261. break;
  262. default:
  263. convert_to_string_ex(conv_edata);
  264. dlog_debug("array idx(%d)=T(%d=%s, val=%s)", pos,
  265. Z_TYPE_P(edata), type2name(Z_TYPE_P(edata)), Z_STRVAL_P(edata));
  266. goapi_array_push(rv, Z_STRVAL_P(edata), &rv);
  267. break;
  268. }
  269. }
  270. // TODO array/map convert
  271. } else if (ch == 'm') {
  272. // 简化成string=>string映射吧
  273. goapi_map_new(&rv);
  274. HashTable *arr_hash = Z_ARRVAL_P(macro_zarg);
  275. HashPosition pos;
  276. zval *edata;
  277. for (zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
  278. #ifdef ZEND_ENGINE_3
  279. (edata = zend_hash_get_current_data_ex(arr_hash, &pos)) != NULL;
  280. #else
  281. zend_hash_get_current_data_ex(arr_hash, (void**)&edata, &pos) == SUCCESS;
  282. #endif
  283. zend_hash_move_forward_ex(arr_hash, &pos)) {
  284. zval vkey = {0};
  285. zval *ekey = &vkey;
  286. zend_hash_get_current_key_zval_ex(arr_hash, &vkey, &pos);
  287. #ifdef ZEND_ENGINE_3
  288. zval *conv_ekey = ekey;
  289. zval *conv_edata = edata;
  290. #else
  291. zval **conv_ekey = &ekey;
  292. zval **conv_edata = &edata;
  293. #endif
  294. switch (Z_TYPE_P(edata)) {
  295. default:
  296. convert_to_string_ex(conv_ekey);
  297. convert_to_string_ex(conv_edata);
  298. dlog_debug("array idx(%d)=K(%s)=T(%d=%s, val=%s)", pos, Z_STRVAL_P(ekey),
  299. Z_TYPE_P(edata), type2name(Z_TYPE_P(edata)), Z_STRVAL_P(edata));
  300. goapi_map_add(rv, Z_STRVAL_P(ekey), Z_STRVAL_P(edata));
  301. break;
  302. }
  303. }
  304. } else {
  305. dlog_debug("arg%d(%c), %s(%d)", idx, ch, type2name(prmty), prmty);
  306. zend_error(E_WARNING, "Parameters type not match, need (%c), given: %s",
  307. ch, type2name(prmty));
  308. dlog_error("Parameters type not match, need (%c), given: %s",
  309. ch, type2name(prmty));
  310. return NULL;
  311. }
  312. return rv;
  313. }
  314. static void phpgo_function_conv_args(int cbid, phpgo_callback_info* cbi, int supply_num_args, void *argv[])
  315. {
  316. int num_args = phpgo_function_num_args(cbid, cbi, supply_num_args);
  317. // int supply_num_args = ZEND_NUM_ARGS();
  318. #ifdef ZEND_ENGINE_3
  319. zval args[MAX_ARG_NUM] = {0};
  320. #else
  321. zval **args[MAX_ARG_NUM] = {0};
  322. #endif
  323. if (zend_get_parameters_array_ex(num_args, args) == FAILURE) {
  324. dlog_error("param count error: %d", num_args);
  325. WRONG_PARAM_COUNT;
  326. return;
  327. }
  328. // void *argv[MAX_ARG_NUM] = {0};
  329. dlog_debug("parse params: %d", num_args);
  330. char* arg_types = phpgo_callback_info_get_arg_types(cbi);
  331. // function has not this arg, don't -1
  332. for (int idx = 0; idx < num_args; idx ++) {
  333. #ifdef ZEND_ENGINE_3
  334. zval *zarg = &args[idx];
  335. #else
  336. zval *zarg = *(args[idx]);
  337. #endif
  338. dlog_debug("arg%d, type=%d", idx, Z_TYPE_P(zarg));
  339. int prmty = Z_TYPE_P(zarg);
  340. char ch = arg_types[idx];
  341. argv[idx] = phpgo_function_conv_arg(cbid, idx, ch, prmty, zarg);
  342. }
  343. return;
  344. }
  345. static void phpgo_method_conv_args(int cbid, phpgo_callback_info* cbi, int supply_num_args, void *argv[])
  346. {
  347. int num_args = phpgo_function_num_args(cbid, cbi, supply_num_args);
  348. // int supply_num_args = ZEND_NUM_ARGS();
  349. #ifdef ZEND_ENGINE_3
  350. zval args[MAX_ARG_NUM] = {0};
  351. #else
  352. zval **args[MAX_ARG_NUM] = {0};
  353. #endif
  354. if (zend_get_parameters_array_ex(num_args-1, args) == FAILURE) {
  355. dlog_error("param count error: %d", num_args);
  356. WRONG_PARAM_COUNT;
  357. return;
  358. }
  359. // void *argv[MAX_ARG_NUM] = {0};
  360. dlog_debug("parse params: %d", num_args);
  361. char* arg_types = phpgo_callback_info_get_arg_types(cbi);
  362. for (int idx = 0; idx < num_args-1; idx ++) {
  363. #ifdef ZEND_ENGINE_3
  364. zval *zarg = &args[idx];
  365. #else
  366. zval *zarg = *(args[idx]);
  367. #endif
  368. dlog_debug("arg%d, type=%d", idx, Z_TYPE_P(zarg));
  369. int prmty = Z_TYPE_P(zarg);
  370. char ch = arg_types[idx+1];
  371. argv[idx] = phpgo_function_conv_arg(cbid, idx, ch, prmty, zarg);
  372. }
  373. return;
  374. }
  375. static void phpgo_function_reutrn_php_array(void *p0, zval *return_value) {
  376. array_init(return_value);
  377. goapi_set_php_array(p0, (void**)&return_value);
  378. }
  379. // go类型的返回值转换为PHP类型的变量值
  380. static int phpgo_function_conv_ret(int cbid, phpgo_callback_info* cbi, void *p0, zval *return_value, zend_execute_data *execute_data)
  381. {
  382. RETVAL_LONG(5);
  383. int ret_type = phpgo_callback_info_get_ret_type(cbi);
  384. dlog_debug("convert ret: %p, type: %d(%s)", p0, ret_type, type2name(ret_type));
  385. int rv_type = 0;
  386. uint64_t rv = (uint64_t)goapi_get_value(p0, (void*)&rv_type);
  387. if (ret_type == IS_ZVAL) {
  388. if (rv_type == 0) {
  389. RETVAL_NULL();
  390. return 0;
  391. }
  392. ret_type = rv_type;
  393. }
  394. zval* self = getThis();
  395. // 返回值解析转换
  396. switch (ret_type) {
  397. case IS_STRING:
  398. #ifdef ZEND_ENGINE_3
  399. RETVAL_STRINGL((char*)rv, strlen((char*)rv));
  400. #else
  401. RETVAL_STRINGL((char*)rv, strlen((char*)rv), 1);
  402. #endif
  403. free((char*)rv);
  404. break;
  405. case IS_DOUBLE:
  406. RETVAL_DOUBLE(*(double*)rv);
  407. free((double*)rv);
  408. break;
  409. #ifdef ZEND_ENGINE_3
  410. case IS_TRUE:
  411. RETVAL_TRUE;
  412. break;
  413. case IS_FALSE:
  414. RETVAL_FALSE;
  415. break;
  416. case _IS_BOOL:
  417. #else
  418. case IS_BOOL:
  419. #endif
  420. RETVAL_BOOL(rv);
  421. break;
  422. case IS_LONG:
  423. RETVAL_LONG(rv);
  424. break;
  425. case IS_NULL:
  426. RETVAL_NULL();
  427. break;
  428. #ifdef ZEND_ENGINE_3
  429. case IS_UNDEF:
  430. RETVAL_NULL();
  431. break;
  432. #endif
  433. case IS_RESOURCE:
  434. RETVAL_NULL();
  435. break;
  436. case IS_ARRAY:
  437. phpgo_function_reutrn_php_array(p0, return_value);
  438. break;
  439. case IS_SELF:
  440. RETVAL_ZVAL(self, 1, 0);
  441. break;
  442. default:
  443. // wtf?
  444. zend_error(E_WARNING, "unrecognized return value: %d, %s.", ret_type, type2name(ret_type));
  445. dlog_error("unrecognized return value: %d, %s.", ret_type, type2name(ret_type));
  446. break;
  447. }
  448. return 0;
  449. }
  450. #ifdef ZEND_ENGINE_3
  451. void phpgo_function_handler7(int cbid, phpgo_callback_info* cbi, zend_execute_data *execute_data, zval *return_value)
  452. {
  453. char* arg_types = phpgo_callback_info_get_arg_types(cbi);
  454. zval *this_ptr = &execute_data->This; // always not null for php7
  455. zend_object* op = NULL;
  456. if (NULL != this_ptr && IS_OBJECT == execute_data->This.u1.v.type) {
  457. op = execute_data->This.value.obj;
  458. }
  459. zend_string *func_name = execute_data->func->common.function_name;
  460. char *class_name = ZEND_FN_SCOPE_NAME(execute_data->func);
  461. dlog_debug("function handler called.%d, this=%p, atys=%s, op=%p, func=%s, class=%s",
  462. cbid, this_ptr, arg_types, op, ZSTR_VAL(func_name), class_name);
  463. zend_function_entry *fe = phpgo_function_map_get(class_name, ZSTR_VAL(func_name));
  464. int cbid_dyn = phpgo_callback_map_get(class_name, ZSTR_VAL(func_name));
  465. dlog_debug("mapget: %p, %d=?%d", fe, cbid_dyn, cbid);
  466. cbid = cbid_dyn; // for transition
  467. void *argv[MAX_ARG_NUM] = {0};
  468. if (op == NULL) {
  469. phpgo_function_conv_args(cbid, cbi, (ZEND_NUM_ARGS()), argv);
  470. } else {
  471. phpgo_method_conv_args(cbid, cbi, (ZEND_NUM_ARGS()), argv);
  472. }
  473. void* rv = NULL;
  474. on_phpgo_function_callback_p(cbid, this_ptr, argv[0], argv[1],
  475. argv[2], argv[3], argv[4], argv[5],
  476. argv[6], argv[7], argv[8], argv[9], &rv, (void*) op);
  477. dlog_debug("inout ret:%p", rv);
  478. phpgo_function_conv_ret(cbid, cbi, rv, return_value, execute_data);
  479. }
  480. void phpgo_function_handler(zend_execute_data *execute_data, zval *return_value)
  481. {
  482. zend_string *zfunc_name = execute_data->func->common.function_name;
  483. char *func_name = ZSTR_VAL(zfunc_name);
  484. char *class_name = ZEND_FN_SCOPE_NAME(execute_data->func);
  485. int cbid = phpgo_callback_map_get(class_name, func_name);
  486. char fname[strlen((class_name == NULL || strlen(class_name) == 0) ? GLOBAL_VCLASS_NAME : class_name)
  487. + strlen(func_name) + 3];
  488. snprintf(fname, sizeof(fname), "%s::%s",
  489. (class_name == NULL || strlen(class_name) == 0) ? GLOBAL_VCLASS_NAME : class_name, func_name);
  490. phpgo_callback_info* cbi = (phpgo_callback_info*)phpgo_object_map_get(g_cbinfo_map, fname);
  491. if (cbi == NULL) {
  492. dlog_error("callback info can not be NULL: '%s=%p::%p'", fname, class_name, func_name);
  493. exit(-1);
  494. }
  495. phpgo_function_handler7(cbid, cbi, execute_data, return_value);
  496. }
  497. #else // php < 7.0
  498. void phpgo_function_handler5(int cbid, phpgo_callback_info* cbi, int ht, zval *return_value, zval **return_value_ptr,
  499. zval *this_ptr, int return_value_used TSRMLS_DC)
  500. {
  501. char* arg_types = phpgo_callback_info_get_arg_types(cbi);
  502. void* op = NULL;
  503. if (NULL != this_ptr && IS_OBJECT == this_ptr->type) {
  504. zend_object_handle handle = this_ptr->value.obj.handle;
  505. struct _store_object *obj;
  506. obj = &EG(objects_store).object_buckets[handle].bucket.obj;
  507. op = &obj->object;
  508. }
  509. const char *func_name = get_active_function_name();
  510. const char *class_name = NULL;
  511. if (NULL != this_ptr) {
  512. zend_class_entry *ce = zend_get_class_entry(this_ptr);
  513. class_name = ce->name;
  514. }
  515. dlog_debug("function handler called.%d, this=%p, atys=%s, op=%p, func=%s, class=%s",
  516. cbid, this_ptr, arg_types, op, func_name, class_name);
  517. void *argv[MAX_ARG_NUM] = {0};
  518. if (this_ptr == NULL) {
  519. phpgo_function_conv_args(cbid, cbi, (ZEND_NUM_ARGS()), argv);
  520. } else {
  521. phpgo_method_conv_args(cbid, cbi, (ZEND_NUM_ARGS()), argv);
  522. }
  523. void* rv = NULL;
  524. on_phpgo_function_callback_p(cbid, this_ptr, argv[0], argv[1],
  525. argv[2], argv[3], argv[4], argv[5],
  526. argv[6], argv[7], argv[8], argv[9], &rv, (void*) op);
  527. dlog_debug("inout ret:%p", rv);
  528. phpgo_function_conv_ret(cbid, cbi, rv, return_value, execute_data);
  529. }
  530. void phpgo_function_handler(int ht, zval *return_value, zval **return_value_ptr,
  531. zval *this_ptr, int return_value_used TSRMLS_DC)
  532. {
  533. const char *func_name = get_active_function_name(TSRMLS_C);
  534. const char *class_name = NULL;
  535. if (NULL != this_ptr) {
  536. zend_class_entry *ce = zend_get_class_entry(this_ptr);
  537. class_name = ce->name;
  538. }
  539. // object maybe destruct after script execute finished, and out of executed file scope
  540. if (!zend_is_executing()) {
  541. if (func_name == NULL && class_name != NULL) {
  542. func_name = "__destruct";
  543. }
  544. }
  545. int cbid = phpgo_callback_map_get(class_name, func_name);
  546. char fname[strlen((class_name == NULL || strlen(class_name) == 0)
  547. ? GLOBAL_VCLASS_NAME : class_name) + strlen(func_name) + 3];
  548. snprintf(fname, sizeof(fname), "%s::%s",
  549. (class_name == NULL || strlen(class_name) == 0) ? GLOBAL_VCLASS_NAME : class_name, func_name);
  550. phpgo_callback_info* cbi = (phpgo_callback_info*)phpgo_object_map_get(g_cbinfo_map, fname);
  551. if (cbi == NULL) {
  552. dlog_error("callback info can not be NULL");
  553. exit(-1);
  554. }
  555. phpgo_function_handler5(cbid, cbi, ht, return_value, return_value_ptr, this_ptr, return_value_used);
  556. }
  557. #endif // #ifdef ZEND_ENGINE_3
  558. int zend_add_class(int cidx, char *cname)
  559. {
  560. if (phpgo_object_map_get(g_class_map, cname) == NULL) {
  561. dlog_error("Class %s not added.", cname);
  562. return -1;
  563. }
  564. phpgo_class_entry* pce = phpgo_object_map_get(g_class_map, cname);
  565. zend_class_entry *ce = (zend_class_entry*)phpgo_class_get(pce);
  566. INIT_CLASS_ENTRY_EX((*ce), cname, strlen(cname), phpgo_class_get_funcs(pce));
  567. zend_register_internal_class(ce TSRMLS_CC);
  568. phpgo_class_map_add(cname, ce);
  569. return 0;
  570. }
  571. int zend_add_class_not_register(int cidx, char *cname)
  572. {
  573. if (phpgo_object_map_get(g_class_map, cname) != NULL) {
  574. dlog_error("Class %s already added.", cname);
  575. return -1;
  576. }
  577. // TODO thread safe?
  578. phpgo_class_entry* pce = phpgo_class_new(cname);
  579. phpgo_object_map_add(&g_class_map, cname, pce);
  580. int class_count = phpgo_object_map_count(g_class_map);
  581. assert(cidx == class_count);
  582. zend_class_entry *ce = (zend_class_entry*)phpgo_class_get(pce);
  583. phpgo_class_map_add(cname, ce);
  584. return 0;
  585. }
  586. int zend_add_function(int cidx, int fidx, int cbid, char *name, char *atys, int rety)
  587. {
  588. dlog_debug("add func %s at %d:%d=%d, atys=%s, rety=%d",
  589. name, cidx, fidx, cbid, atys, rety);
  590. // TODO 检测是否是phpgo注册的?
  591. if (gozend_function_registered(name) == 1) {
  592. dlog_error("function already exists: %s", name);
  593. }
  594. int cnlen = strlen(GLOBAL_VCLASS_NAME);
  595. char *cname = GLOBAL_VCLASS_NAME;
  596. char *mname = name;
  597. phpgo_class_entry* pce = (phpgo_class_entry*)phpgo_object_map_get(g_class_map, cname);
  598. if (pce == NULL) {
  599. dlog_debug("pce empty: %d for %s", phpgo_object_map_count(g_class_map), cname);
  600. zend_add_class_not_register(cidx, cname);
  601. pce = (phpgo_class_entry*)phpgo_object_map_get(g_class_map, cname);
  602. }
  603. phpgo_class_method_add(pce, mname);
  604. phpgo_function_entry* pfe = phpgo_class_method_get(pce, mname);
  605. zend_function_entry* fe = (zend_function_entry*)phpgo_function_get(pfe);
  606. zend_function_entry e = {strdup(name), phpgo_function_handler, NULL, 0, 0};
  607. memcpy(fe, &e, sizeof(e));
  608. (&g_entry)->functions = phpgo_class_get_funcs(pce);
  609. phpgo_callback_info* cbi = phpgo_callback_info_new(atys, rety);
  610. int fnlen = strlen(GLOBAL_VCLASS_NAME) + strlen(name) + 3;
  611. char* fname = (char*)calloc(1, fnlen);
  612. snprintf(fname, fnlen, "%s::%s", GLOBAL_VCLASS_NAME, name);
  613. phpgo_object_map_add(&g_cbinfo_map, fname, cbi);
  614. phpgo_function_map_add(NULL, name, fe);
  615. phpgo_callback_map_add(NULL, name, cbid);
  616. return 0;
  617. }
  618. int zend_add_method(int cidx, int fidx, int cbid, char *cname, char *mname, char *atys, int rety)
  619. {
  620. dlog_debug("add mth %s::%s at %d:%d=%d, atys=%s, rety=%d",
  621. cname, mname, cidx, fidx, cbid, atys, rety);
  622. phpgo_class_entry* pce = (phpgo_class_entry*)phpgo_object_map_get(g_class_map, cname);
  623. if (pce == NULL) {
  624. dlog_debug("pce empty: %d", phpgo_object_map_count(g_class_map));
  625. zend_add_class_not_register(cidx, cname);
  626. pce = (phpgo_class_entry*)phpgo_object_map_get(g_class_map, cname);
  627. }
  628. phpgo_class_method_add(pce, mname);
  629. phpgo_function_entry* pfe = phpgo_class_method_get(pce, mname);
  630. zend_function_entry* fe = (zend_function_entry*)phpgo_function_get(pfe);
  631. phpgo_callback_info* cbi = phpgo_callback_info_new(atys, rety);
  632. int fnlen = strlen(cname) + strlen(mname) + 3;
  633. char* fname = (char*)calloc(1, fnlen);
  634. snprintf(fname, fnlen, "%s::%s", cname, mname);
  635. phpgo_object_map_add(&g_cbinfo_map, fname, cbi);
  636. phpgo_function_map_add(cname, mname, fe);
  637. phpgo_callback_map_add(cname, mname, cbid);
  638. return 0;
  639. }