zend_closures.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Christian Seiler <chris_se@gmx.net> |
  16. | Dmitry Stogov <dmitry@php.net> |
  17. | Marcus Boerger <helly@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "zend.h"
  21. #include "zend_API.h"
  22. #include "zend_closures.h"
  23. #include "zend_exceptions.h"
  24. #include "zend_interfaces.h"
  25. #include "zend_objects.h"
  26. #include "zend_objects_API.h"
  27. #include "zend_globals.h"
  28. #define ZEND_CLOSURE_PRINT_NAME "Closure object"
  29. #define ZEND_CLOSURE_PROPERTY_ERROR() \
  30. zend_throw_error(NULL, "Closure object cannot have properties")
  31. typedef struct _zend_closure {
  32. zend_object std;
  33. zend_function func;
  34. zval this_ptr;
  35. zend_class_entry *called_scope;
  36. zif_handler orig_internal_handler;
  37. } zend_closure;
  38. /* non-static since it needs to be referenced */
  39. ZEND_API zend_class_entry *zend_ce_closure;
  40. static zend_object_handlers closure_handlers;
  41. ZEND_METHOD(Closure, __invoke) /* {{{ */
  42. {
  43. zend_function *func = EX(func);
  44. zval *arguments = ZEND_CALL_ARG(execute_data, 1);
  45. if (call_user_function(CG(function_table), NULL, getThis(), return_value, ZEND_NUM_ARGS(), arguments) == FAILURE) {
  46. RETVAL_FALSE;
  47. }
  48. /* destruct the function also, then - we have allocated it in get_method */
  49. zend_string_release_ex(func->internal_function.function_name, 0);
  50. efree(func);
  51. #if ZEND_DEBUG
  52. execute_data->func = NULL;
  53. #endif
  54. }
  55. /* }}} */
  56. static zend_bool zend_valid_closure_binding(
  57. zend_closure *closure, zval *newthis, zend_class_entry *scope) /* {{{ */
  58. {
  59. zend_function *func = &closure->func;
  60. zend_bool is_fake_closure = (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0;
  61. if (newthis) {
  62. if (func->common.fn_flags & ZEND_ACC_STATIC) {
  63. zend_error(E_WARNING, "Cannot bind an instance to a static closure");
  64. return 0;
  65. }
  66. if (is_fake_closure && func->common.scope &&
  67. !instanceof_function(Z_OBJCE_P(newthis), func->common.scope)) {
  68. /* Binding incompatible $this to an internal method is not supported. */
  69. zend_error(E_WARNING, "Cannot bind method %s::%s() to object of class %s",
  70. ZSTR_VAL(func->common.scope->name),
  71. ZSTR_VAL(func->common.function_name),
  72. ZSTR_VAL(Z_OBJCE_P(newthis)->name));
  73. return 0;
  74. }
  75. } else if (!(func->common.fn_flags & ZEND_ACC_STATIC) && func->common.scope
  76. && func->type == ZEND_INTERNAL_FUNCTION) {
  77. zend_error(E_WARNING, "Cannot unbind $this of internal method");
  78. return 0;
  79. }
  80. if (scope && scope != func->common.scope && scope->type == ZEND_INTERNAL_CLASS) {
  81. /* rebinding to internal class is not allowed */
  82. zend_error(E_WARNING, "Cannot bind closure to scope of internal class %s",
  83. ZSTR_VAL(scope->name));
  84. return 0;
  85. }
  86. if (is_fake_closure && scope != func->common.scope) {
  87. zend_error(E_WARNING, "Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure()");
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. /* }}} */
  93. /* {{{ proto mixed Closure::call(object to [, mixed parameter] [, mixed ...] )
  94. Call closure, binding to a given object with its class as the scope */
  95. ZEND_METHOD(Closure, call)
  96. {
  97. zval *zclosure, *newthis, closure_result;
  98. zend_closure *closure;
  99. zend_fcall_info fci;
  100. zend_fcall_info_cache fci_cache;
  101. zend_function my_function;
  102. zend_object *newobj;
  103. fci.param_count = 0;
  104. fci.params = NULL;
  105. if (zend_parse_parameters(ZEND_NUM_ARGS(), "o*", &newthis, &fci.params, &fci.param_count) == FAILURE) {
  106. return;
  107. }
  108. zclosure = getThis();
  109. closure = (zend_closure *) Z_OBJ_P(zclosure);
  110. newobj = Z_OBJ_P(newthis);
  111. if (!zend_valid_closure_binding(closure, newthis, Z_OBJCE_P(newthis))) {
  112. return;
  113. }
  114. if (closure->func.common.fn_flags & ZEND_ACC_GENERATOR) {
  115. zval new_closure;
  116. zend_create_closure(&new_closure, &closure->func, Z_OBJCE_P(newthis), closure->called_scope, newthis);
  117. closure = (zend_closure *) Z_OBJ(new_closure);
  118. fci_cache.function_handler = &closure->func;
  119. } else {
  120. memcpy(&my_function, &closure->func, closure->func.type == ZEND_USER_FUNCTION ? sizeof(zend_op_array) : sizeof(zend_internal_function));
  121. my_function.common.fn_flags &= ~ZEND_ACC_CLOSURE;
  122. /* use scope of passed object */
  123. my_function.common.scope = Z_OBJCE_P(newthis);
  124. fci_cache.function_handler = &my_function;
  125. /* Runtime cache relies on bound scope to be immutable, hence we need a separate rt cache in case scope changed */
  126. if (ZEND_USER_CODE(my_function.type) && closure->func.common.scope != Z_OBJCE_P(newthis)) {
  127. my_function.op_array.run_time_cache = emalloc(my_function.op_array.cache_size);
  128. memset(my_function.op_array.run_time_cache, 0, my_function.op_array.cache_size);
  129. }
  130. }
  131. fci_cache.called_scope = newobj->ce;
  132. fci_cache.object = fci.object = newobj;
  133. fci.size = sizeof(fci);
  134. ZVAL_COPY_VALUE(&fci.function_name, zclosure);
  135. fci.retval = &closure_result;
  136. fci.no_separation = 1;
  137. if (zend_call_function(&fci, &fci_cache) == SUCCESS && Z_TYPE(closure_result) != IS_UNDEF) {
  138. if (Z_ISREF(closure_result)) {
  139. zend_unwrap_reference(&closure_result);
  140. }
  141. ZVAL_COPY_VALUE(return_value, &closure_result);
  142. }
  143. if (fci_cache.function_handler->common.fn_flags & ZEND_ACC_GENERATOR) {
  144. /* copied upon generator creation */
  145. GC_DELREF(&closure->std);
  146. } else if (ZEND_USER_CODE(my_function.type) && closure->func.common.scope != Z_OBJCE_P(newthis)) {
  147. efree(my_function.op_array.run_time_cache);
  148. }
  149. }
  150. /* }}} */
  151. /* {{{ proto Closure Closure::bind(callable old, object to [, mixed scope])
  152. Create a closure from another one and bind to another object and scope */
  153. ZEND_METHOD(Closure, bind)
  154. {
  155. zval *newthis, *zclosure, *scope_arg = NULL;
  156. zend_closure *closure;
  157. zend_class_entry *ce, *called_scope;
  158. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oo!|z", &zclosure, zend_ce_closure, &newthis, &scope_arg) == FAILURE) {
  159. return;
  160. }
  161. closure = (zend_closure *)Z_OBJ_P(zclosure);
  162. if (scope_arg != NULL) { /* scope argument was given */
  163. if (Z_TYPE_P(scope_arg) == IS_OBJECT) {
  164. ce = Z_OBJCE_P(scope_arg);
  165. } else if (Z_TYPE_P(scope_arg) == IS_NULL) {
  166. ce = NULL;
  167. } else {
  168. zend_string *tmp_class_name;
  169. zend_string *class_name = zval_get_tmp_string(scope_arg, &tmp_class_name);
  170. if (zend_string_equals_literal(class_name, "static")) {
  171. ce = closure->func.common.scope;
  172. } else if ((ce = zend_lookup_class_ex(class_name, NULL, 1)) == NULL) {
  173. zend_error(E_WARNING, "Class '%s' not found", ZSTR_VAL(class_name));
  174. zend_tmp_string_release(tmp_class_name);
  175. RETURN_NULL();
  176. }
  177. zend_tmp_string_release(tmp_class_name);
  178. }
  179. } else { /* scope argument not given; do not change the scope by default */
  180. ce = closure->func.common.scope;
  181. }
  182. if (!zend_valid_closure_binding(closure, newthis, ce)) {
  183. return;
  184. }
  185. if (newthis) {
  186. called_scope = Z_OBJCE_P(newthis);
  187. } else {
  188. called_scope = ce;
  189. }
  190. zend_create_closure(return_value, &closure->func, ce, called_scope, newthis);
  191. }
  192. /* }}} */
  193. static ZEND_NAMED_FUNCTION(zend_closure_call_magic) /* {{{ */ {
  194. zend_fcall_info fci;
  195. zend_fcall_info_cache fcc;
  196. zval params[2];
  197. memset(&fci, 0, sizeof(zend_fcall_info));
  198. memset(&fcc, 0, sizeof(zend_fcall_info_cache));
  199. fci.size = sizeof(zend_fcall_info);
  200. fci.retval = return_value;
  201. fcc.function_handler = (EX(func)->internal_function.fn_flags & ZEND_ACC_STATIC) ?
  202. EX(func)->internal_function.scope->__callstatic : EX(func)->internal_function.scope->__call;
  203. fci.params = params;
  204. fci.param_count = 2;
  205. ZVAL_STR(&fci.params[0], EX(func)->common.function_name);
  206. if (ZEND_NUM_ARGS()) {
  207. array_init_size(&fci.params[1], ZEND_NUM_ARGS());
  208. zend_copy_parameters_array(ZEND_NUM_ARGS(), &fci.params[1]);
  209. } else {
  210. ZVAL_EMPTY_ARRAY(&fci.params[1]);
  211. }
  212. fci.object = Z_OBJ(EX(This));
  213. fcc.object = Z_OBJ(EX(This));
  214. zend_call_function(&fci, &fcc);
  215. zval_ptr_dtor(&fci.params[0]);
  216. zval_ptr_dtor(&fci.params[1]);
  217. }
  218. /* }}} */
  219. static int zend_create_closure_from_callable(zval *return_value, zval *callable, char **error) /* {{{ */ {
  220. zend_fcall_info_cache fcc;
  221. zend_function *mptr;
  222. zval instance;
  223. zend_internal_function call;
  224. if (!zend_is_callable_ex(callable, NULL, 0, NULL, &fcc, error)) {
  225. return FAILURE;
  226. }
  227. mptr = fcc.function_handler;
  228. if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  229. /* For Closure::fromCallable([$closure, "__invoke"]) return $closure. */
  230. if (fcc.object && fcc.object->ce == zend_ce_closure
  231. && zend_string_equals_literal(mptr->common.function_name, "__invoke")) {
  232. ZVAL_OBJ(return_value, fcc.object);
  233. GC_ADDREF(fcc.object);
  234. zend_free_trampoline(mptr);
  235. return SUCCESS;
  236. }
  237. memset(&call, 0, sizeof(zend_internal_function));
  238. call.type = ZEND_INTERNAL_FUNCTION;
  239. call.fn_flags = mptr->common.fn_flags & ZEND_ACC_STATIC;
  240. call.handler = zend_closure_call_magic;
  241. call.function_name = mptr->common.function_name;
  242. call.scope = mptr->common.scope;
  243. zend_free_trampoline(mptr);
  244. mptr = (zend_function *) &call;
  245. }
  246. if (fcc.object) {
  247. ZVAL_OBJ(&instance, fcc.object);
  248. zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, &instance);
  249. } else {
  250. zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, NULL);
  251. }
  252. return SUCCESS;
  253. }
  254. /* }}} */
  255. /* {{{ proto Closure Closure::fromCallable(callable callable)
  256. Create a closure from a callable using the current scope. */
  257. ZEND_METHOD(Closure, fromCallable)
  258. {
  259. zval *callable;
  260. int success;
  261. char *error = NULL;
  262. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callable) == FAILURE) {
  263. return;
  264. }
  265. if (Z_TYPE_P(callable) == IS_OBJECT && instanceof_function(Z_OBJCE_P(callable), zend_ce_closure)) {
  266. /* It's already a closure */
  267. RETURN_ZVAL(callable, 1, 0);
  268. }
  269. /* create closure as if it were called from parent scope */
  270. EG(current_execute_data) = EX(prev_execute_data);
  271. success = zend_create_closure_from_callable(return_value, callable, &error);
  272. EG(current_execute_data) = execute_data;
  273. if (success == FAILURE || error) {
  274. if (error) {
  275. zend_throw_exception_ex(zend_ce_type_error, 0, "Failed to create closure from callable: %s", error);
  276. efree(error);
  277. } else {
  278. zend_throw_exception_ex(zend_ce_type_error, 0, "Failed to create closure from callable");
  279. }
  280. }
  281. }
  282. /* }}} */
  283. static ZEND_COLD zend_function *zend_closure_get_constructor(zend_object *object) /* {{{ */
  284. {
  285. zend_throw_error(NULL, "Instantiation of 'Closure' is not allowed");
  286. return NULL;
  287. }
  288. /* }}} */
  289. static int zend_closure_compare_objects(zval *o1, zval *o2) /* {{{ */
  290. {
  291. return (Z_OBJ_P(o1) != Z_OBJ_P(o2));
  292. }
  293. /* }}} */
  294. ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *object) /* {{{ */
  295. {
  296. zend_closure *closure = (zend_closure *)object;
  297. zend_function *invoke = (zend_function*)emalloc(sizeof(zend_function));
  298. const uint32_t keep_flags =
  299. ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_VARIADIC | ZEND_ACC_HAS_RETURN_TYPE;
  300. invoke->common = closure->func.common;
  301. /* We return ZEND_INTERNAL_FUNCTION, but arg_info representation is the
  302. * same as for ZEND_USER_FUNCTION (uses zend_string* instead of char*).
  303. * This is not a problem, because ZEND_ACC_HAS_TYPE_HINTS is never set,
  304. * and we won't check arguments on internal function. We also set
  305. * ZEND_ACC_USER_ARG_INFO flag to prevent invalid usage by Reflection */
  306. invoke->type = ZEND_INTERNAL_FUNCTION;
  307. invoke->internal_function.fn_flags =
  308. ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER | (closure->func.common.fn_flags & keep_flags);
  309. if (closure->func.type != ZEND_INTERNAL_FUNCTION || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
  310. invoke->internal_function.fn_flags |=
  311. ZEND_ACC_USER_ARG_INFO;
  312. }
  313. invoke->internal_function.handler = ZEND_MN(Closure___invoke);
  314. invoke->internal_function.module = 0;
  315. invoke->internal_function.scope = zend_ce_closure;
  316. invoke->internal_function.function_name = ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE);
  317. return invoke;
  318. }
  319. /* }}} */
  320. ZEND_API const zend_function *zend_get_closure_method_def(zval *obj) /* {{{ */
  321. {
  322. zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
  323. return &closure->func;
  324. }
  325. /* }}} */
  326. ZEND_API zval* zend_get_closure_this_ptr(zval *obj) /* {{{ */
  327. {
  328. zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
  329. return &closure->this_ptr;
  330. }
  331. /* }}} */
  332. static zend_function *zend_closure_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
  333. {
  334. if (zend_string_equals_literal_ci(method, ZEND_INVOKE_FUNC_NAME)) {
  335. return zend_get_closure_invoke_method(*object);
  336. }
  337. return zend_std_get_method(object, method, key);
  338. }
  339. /* }}} */
  340. static zval *zend_closure_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
  341. {
  342. ZEND_CLOSURE_PROPERTY_ERROR();
  343. return &EG(uninitialized_zval);
  344. }
  345. /* }}} */
  346. static void zend_closure_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
  347. {
  348. ZEND_CLOSURE_PROPERTY_ERROR();
  349. }
  350. /* }}} */
  351. static zval *zend_closure_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
  352. {
  353. ZEND_CLOSURE_PROPERTY_ERROR();
  354. return NULL;
  355. }
  356. /* }}} */
  357. static int zend_closure_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
  358. {
  359. if (has_set_exists != ZEND_PROPERTY_EXISTS) {
  360. ZEND_CLOSURE_PROPERTY_ERROR();
  361. }
  362. return 0;
  363. }
  364. /* }}} */
  365. static void zend_closure_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
  366. {
  367. ZEND_CLOSURE_PROPERTY_ERROR();
  368. }
  369. /* }}} */
  370. static void zend_closure_free_storage(zend_object *object) /* {{{ */
  371. {
  372. zend_closure *closure = (zend_closure *)object;
  373. zend_object_std_dtor(&closure->std);
  374. if (closure->func.type == ZEND_USER_FUNCTION) {
  375. if (closure->func.op_array.fn_flags & ZEND_ACC_NO_RT_ARENA) {
  376. efree(closure->func.op_array.run_time_cache);
  377. closure->func.op_array.run_time_cache = NULL;
  378. }
  379. destroy_op_array(&closure->func.op_array);
  380. }
  381. if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
  382. zval_ptr_dtor(&closure->this_ptr);
  383. }
  384. }
  385. /* }}} */
  386. static zend_object *zend_closure_new(zend_class_entry *class_type) /* {{{ */
  387. {
  388. zend_closure *closure;
  389. closure = emalloc(sizeof(zend_closure));
  390. memset(closure, 0, sizeof(zend_closure));
  391. zend_object_std_init(&closure->std, class_type);
  392. closure->std.handlers = &closure_handlers;
  393. return (zend_object*)closure;
  394. }
  395. /* }}} */
  396. static zend_object *zend_closure_clone(zval *zobject) /* {{{ */
  397. {
  398. zend_closure *closure = (zend_closure *)Z_OBJ_P(zobject);
  399. zval result;
  400. zend_create_closure(&result, &closure->func,
  401. closure->func.common.scope, closure->called_scope, &closure->this_ptr);
  402. return Z_OBJ(result);
  403. }
  404. /* }}} */
  405. int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr) /* {{{ */
  406. {
  407. zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
  408. *fptr_ptr = &closure->func;
  409. *ce_ptr = closure->called_scope;
  410. if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
  411. *obj_ptr = Z_OBJ(closure->this_ptr);
  412. } else {
  413. *obj_ptr = NULL;
  414. }
  415. return SUCCESS;
  416. }
  417. /* }}} */
  418. static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp) /* {{{ */
  419. {
  420. zend_closure *closure = (zend_closure *)Z_OBJ_P(object);
  421. zval val;
  422. struct _zend_arg_info *arg_info = closure->func.common.arg_info;
  423. HashTable *debug_info;
  424. zend_bool zstr_args = (closure->func.type == ZEND_USER_FUNCTION) || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO);
  425. *is_temp = 1;
  426. debug_info = zend_new_array(8);
  427. if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) {
  428. zval *var;
  429. HashTable *static_variables = closure->func.op_array.static_variables;
  430. ZVAL_ARR(&val, zend_array_dup(static_variables));
  431. zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_STATIC), &val);
  432. ZEND_HASH_FOREACH_VAL(Z_ARRVAL(val), var) {
  433. if (Z_TYPE_P(var) == IS_CONSTANT_AST) {
  434. zval_ptr_dtor(var);
  435. ZVAL_STRING(var, "<constant ast>");
  436. }
  437. } ZEND_HASH_FOREACH_END();
  438. }
  439. if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
  440. Z_ADDREF(closure->this_ptr);
  441. zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_THIS), &closure->this_ptr);
  442. }
  443. if (arg_info &&
  444. (closure->func.common.num_args ||
  445. (closure->func.common.fn_flags & ZEND_ACC_VARIADIC))) {
  446. uint32_t i, num_args, required = closure->func.common.required_num_args;
  447. array_init(&val);
  448. num_args = closure->func.common.num_args;
  449. if (closure->func.common.fn_flags & ZEND_ACC_VARIADIC) {
  450. num_args++;
  451. }
  452. for (i = 0; i < num_args; i++) {
  453. zend_string *name;
  454. zval info;
  455. if (arg_info->name) {
  456. if (zstr_args) {
  457. name = zend_strpprintf(0, "%s$%s",
  458. arg_info->pass_by_reference ? "&" : "",
  459. ZSTR_VAL(arg_info->name));
  460. } else {
  461. name = zend_strpprintf(0, "%s$%s",
  462. arg_info->pass_by_reference ? "&" : "",
  463. ((zend_internal_arg_info*)arg_info)->name);
  464. }
  465. } else {
  466. name = zend_strpprintf(0, "%s$param%d",
  467. arg_info->pass_by_reference ? "&" : "",
  468. i + 1);
  469. }
  470. ZVAL_NEW_STR(&info, zend_strpprintf(0, "%s", i >= required ? "<optional>" : "<required>"));
  471. zend_hash_update(Z_ARRVAL(val), name, &info);
  472. zend_string_release_ex(name, 0);
  473. arg_info++;
  474. }
  475. zend_hash_str_update(debug_info, "parameter", sizeof("parameter")-1, &val);
  476. }
  477. return debug_info;
  478. }
  479. /* }}} */
  480. static HashTable *zend_closure_get_gc(zval *obj, zval **table, int *n) /* {{{ */
  481. {
  482. zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
  483. *table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL;
  484. *n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0;
  485. return (closure->func.type == ZEND_USER_FUNCTION) ?
  486. closure->func.op_array.static_variables : NULL;
  487. }
  488. /* }}} */
  489. /* {{{ proto Closure::__construct()
  490. Private constructor preventing instantiation */
  491. ZEND_COLD ZEND_METHOD(Closure, __construct)
  492. {
  493. zend_throw_error(NULL, "Instantiation of 'Closure' is not allowed");
  494. }
  495. /* }}} */
  496. ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bindto, 0, 0, 1)
  497. ZEND_ARG_INFO(0, newthis)
  498. ZEND_ARG_INFO(0, newscope)
  499. ZEND_END_ARG_INFO()
  500. ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bind, 0, 0, 2)
  501. ZEND_ARG_INFO(0, closure)
  502. ZEND_ARG_INFO(0, newthis)
  503. ZEND_ARG_INFO(0, newscope)
  504. ZEND_END_ARG_INFO()
  505. ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_call, 0, 0, 1)
  506. ZEND_ARG_INFO(0, newthis)
  507. ZEND_ARG_VARIADIC_INFO(0, parameters)
  508. ZEND_END_ARG_INFO()
  509. ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_fromcallable, 0, 0, 1)
  510. ZEND_ARG_INFO(0, callable)
  511. ZEND_END_ARG_INFO()
  512. static const zend_function_entry closure_functions[] = {
  513. ZEND_ME(Closure, __construct, NULL, ZEND_ACC_PRIVATE)
  514. ZEND_ME(Closure, bind, arginfo_closure_bind, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  515. ZEND_MALIAS(Closure, bindTo, bind, arginfo_closure_bindto, ZEND_ACC_PUBLIC)
  516. ZEND_ME(Closure, call, arginfo_closure_call, ZEND_ACC_PUBLIC)
  517. ZEND_ME(Closure, fromCallable, arginfo_closure_fromcallable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  518. ZEND_FE_END
  519. };
  520. void zend_register_closure_ce(void) /* {{{ */
  521. {
  522. zend_class_entry ce;
  523. INIT_CLASS_ENTRY(ce, "Closure", closure_functions);
  524. zend_ce_closure = zend_register_internal_class(&ce);
  525. zend_ce_closure->ce_flags |= ZEND_ACC_FINAL;
  526. zend_ce_closure->create_object = zend_closure_new;
  527. zend_ce_closure->serialize = zend_class_serialize_deny;
  528. zend_ce_closure->unserialize = zend_class_unserialize_deny;
  529. memcpy(&closure_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  530. closure_handlers.free_obj = zend_closure_free_storage;
  531. closure_handlers.get_constructor = zend_closure_get_constructor;
  532. closure_handlers.get_method = zend_closure_get_method;
  533. closure_handlers.write_property = zend_closure_write_property;
  534. closure_handlers.read_property = zend_closure_read_property;
  535. closure_handlers.get_property_ptr_ptr = zend_closure_get_property_ptr_ptr;
  536. closure_handlers.has_property = zend_closure_has_property;
  537. closure_handlers.unset_property = zend_closure_unset_property;
  538. closure_handlers.compare_objects = zend_closure_compare_objects;
  539. closure_handlers.clone_obj = zend_closure_clone;
  540. closure_handlers.get_debug_info = zend_closure_get_debug_info;
  541. closure_handlers.get_closure = zend_closure_get_closure;
  542. closure_handlers.get_gc = zend_closure_get_gc;
  543. }
  544. /* }}} */
  545. static ZEND_NAMED_FUNCTION(zend_closure_internal_handler) /* {{{ */
  546. {
  547. zend_closure *closure = (zend_closure*)ZEND_CLOSURE_OBJECT(EX(func));
  548. closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  549. OBJ_RELEASE((zend_object*)closure);
  550. EX(func) = NULL;
  551. }
  552. /* }}} */
  553. ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
  554. {
  555. zend_closure *closure;
  556. object_init_ex(res, zend_ce_closure);
  557. closure = (zend_closure *)Z_OBJ_P(res);
  558. if ((scope == NULL) && this_ptr && (Z_TYPE_P(this_ptr) != IS_UNDEF)) {
  559. /* use dummy scope if we're binding an object without specifying a scope */
  560. /* maybe it would be better to create one for this purpose */
  561. scope = zend_ce_closure;
  562. }
  563. if (func->type == ZEND_USER_FUNCTION) {
  564. memcpy(&closure->func, func, sizeof(zend_op_array));
  565. closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
  566. closure->func.common.fn_flags &= ~ZEND_ACC_IMMUTABLE;
  567. if (closure->func.op_array.static_variables) {
  568. closure->func.op_array.static_variables =
  569. zend_array_dup(closure->func.op_array.static_variables);
  570. }
  571. /* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */
  572. if (!closure->func.op_array.run_time_cache
  573. || func->common.scope != scope
  574. || (func->common.fn_flags & ZEND_ACC_NO_RT_ARENA)
  575. ) {
  576. if (!func->op_array.run_time_cache
  577. && (func->common.fn_flags & ZEND_ACC_CLOSURE)
  578. && !(func->common.fn_flags & ZEND_ACC_IMMUTABLE)) {
  579. /* If a real closure is used for the first time, we create a shared runtime cache
  580. * and remember which scope it is for. */
  581. func->common.scope = scope;
  582. func->op_array.run_time_cache = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
  583. closure->func.op_array.run_time_cache = func->op_array.run_time_cache;
  584. } else {
  585. /* Otherwise, we use a non-shared runtime cache */
  586. closure->func.op_array.run_time_cache = emalloc(func->op_array.cache_size);
  587. closure->func.op_array.fn_flags |= ZEND_ACC_NO_RT_ARENA;
  588. }
  589. memset(closure->func.op_array.run_time_cache, 0, func->op_array.cache_size);
  590. }
  591. if (closure->func.op_array.refcount) {
  592. (*closure->func.op_array.refcount)++;
  593. }
  594. } else {
  595. memcpy(&closure->func, func, sizeof(zend_internal_function));
  596. closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
  597. /* wrap internal function handler to avoid memory leak */
  598. if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) {
  599. /* avoid infinity recursion, by taking handler from nested closure */
  600. zend_closure *nested = (zend_closure*)((char*)func - XtOffsetOf(zend_closure, func));
  601. ZEND_ASSERT(nested->std.ce == zend_ce_closure);
  602. closure->orig_internal_handler = nested->orig_internal_handler;
  603. } else {
  604. closure->orig_internal_handler = closure->func.internal_function.handler;
  605. }
  606. closure->func.internal_function.handler = zend_closure_internal_handler;
  607. if (!func->common.scope) {
  608. /* if it's a free function, we won't set scope & this since they're meaningless */
  609. this_ptr = NULL;
  610. scope = NULL;
  611. }
  612. }
  613. ZVAL_UNDEF(&closure->this_ptr);
  614. /* Invariant:
  615. * If the closure is unscoped or static, it has no bound object. */
  616. closure->func.common.scope = scope;
  617. closure->called_scope = called_scope;
  618. if (scope) {
  619. closure->func.common.fn_flags |= ZEND_ACC_PUBLIC;
  620. if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) {
  621. ZVAL_COPY(&closure->this_ptr, this_ptr);
  622. }
  623. }
  624. }
  625. /* }}} */
  626. ZEND_API void zend_create_fake_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
  627. {
  628. zend_closure *closure;
  629. zend_create_closure(res, func, scope, called_scope, this_ptr);
  630. closure = (zend_closure *)Z_OBJ_P(res);
  631. closure->func.common.fn_flags |= ZEND_ACC_FAKE_CLOSURE;
  632. }
  633. /* }}} */
  634. void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var) /* {{{ */
  635. {
  636. zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
  637. HashTable *static_variables = closure->func.op_array.static_variables;
  638. zend_hash_update(static_variables, var_name, var);
  639. }
  640. /* }}} */
  641. void zend_closure_bind_var_ex(zval *closure_zv, uint32_t offset, zval *val) /* {{{ */
  642. {
  643. zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
  644. HashTable *static_variables = closure->func.op_array.static_variables;
  645. zval *var = (zval*)((char*)static_variables->arData + offset);
  646. zval_ptr_dtor(var);
  647. ZVAL_COPY_VALUE(var, val);
  648. }
  649. /* }}} */
  650. /*
  651. * Local variables:
  652. * tab-width: 4
  653. * c-basic-offset: 4
  654. * indent-tabs-mode: t
  655. * End:
  656. * vim600: sw=4 ts=4 fdm=marker
  657. * vim<600: sw=4 ts=4
  658. */