zend_closures.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 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. #include "zend_closures_arginfo.h"
  29. typedef struct _zend_closure {
  30. zend_object std;
  31. zend_function func;
  32. zval this_ptr;
  33. zend_class_entry *called_scope;
  34. zif_handler orig_internal_handler;
  35. } zend_closure;
  36. /* non-static since it needs to be referenced */
  37. ZEND_API zend_class_entry *zend_ce_closure;
  38. static zend_object_handlers closure_handlers;
  39. ZEND_METHOD(Closure, __invoke) /* {{{ */
  40. {
  41. zend_function *func = EX(func);
  42. zval *args;
  43. uint32_t num_args;
  44. HashTable *named_args;
  45. ZEND_PARSE_PARAMETERS_START(0, -1)
  46. Z_PARAM_VARIADIC_WITH_NAMED(args, num_args, named_args)
  47. ZEND_PARSE_PARAMETERS_END();
  48. if (call_user_function_named(CG(function_table), NULL, ZEND_THIS, return_value, num_args, args, named_args) == FAILURE) {
  49. RETVAL_FALSE;
  50. }
  51. /* destruct the function also, then - we have allocated it in get_method */
  52. zend_string_release_ex(func->internal_function.function_name, 0);
  53. efree(func);
  54. #if ZEND_DEBUG
  55. execute_data->func = NULL;
  56. #endif
  57. }
  58. /* }}} */
  59. static bool zend_valid_closure_binding(
  60. zend_closure *closure, zval *newthis, zend_class_entry *scope) /* {{{ */
  61. {
  62. zend_function *func = &closure->func;
  63. bool is_fake_closure = (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0;
  64. if (newthis) {
  65. if (func->common.fn_flags & ZEND_ACC_STATIC) {
  66. zend_error(E_WARNING, "Cannot bind an instance to a static closure");
  67. return 0;
  68. }
  69. if (is_fake_closure && func->common.scope &&
  70. !instanceof_function(Z_OBJCE_P(newthis), func->common.scope)) {
  71. /* Binding incompatible $this to an internal method is not supported. */
  72. zend_error(E_WARNING, "Cannot bind method %s::%s() to object of class %s",
  73. ZSTR_VAL(func->common.scope->name),
  74. ZSTR_VAL(func->common.function_name),
  75. ZSTR_VAL(Z_OBJCE_P(newthis)->name));
  76. return 0;
  77. }
  78. } else if (is_fake_closure && func->common.scope
  79. && !(func->common.fn_flags & ZEND_ACC_STATIC)) {
  80. zend_error(E_WARNING, "Cannot unbind $this of method");
  81. return 0;
  82. } else if (!is_fake_closure && !Z_ISUNDEF(closure->this_ptr)
  83. && (func->common.fn_flags & ZEND_ACC_USES_THIS)) {
  84. zend_error(E_WARNING, "Cannot unbind $this of closure using $this");
  85. return 0;
  86. }
  87. if (scope && scope != func->common.scope && scope->type == ZEND_INTERNAL_CLASS) {
  88. /* rebinding to internal class is not allowed */
  89. zend_error(E_WARNING, "Cannot bind closure to scope of internal class %s",
  90. ZSTR_VAL(scope->name));
  91. return 0;
  92. }
  93. if (is_fake_closure && scope != func->common.scope) {
  94. if (func->common.scope == NULL) {
  95. zend_error(E_WARNING, "Cannot rebind scope of closure created from function");
  96. } else {
  97. zend_error(E_WARNING, "Cannot rebind scope of closure created from method");
  98. }
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. /* }}} */
  104. /* {{{ Call closure, binding to a given object with its class as the scope */
  105. ZEND_METHOD(Closure, call)
  106. {
  107. zval *newthis, closure_result;
  108. zend_closure *closure;
  109. zend_fcall_info fci;
  110. zend_fcall_info_cache fci_cache;
  111. zend_function my_function;
  112. zend_object *newobj;
  113. zend_class_entry *newclass;
  114. fci.param_count = 0;
  115. fci.params = NULL;
  116. ZEND_PARSE_PARAMETERS_START(1, -1)
  117. Z_PARAM_OBJECT(newthis)
  118. Z_PARAM_VARIADIC_WITH_NAMED(fci.params, fci.param_count, fci.named_params)
  119. ZEND_PARSE_PARAMETERS_END();
  120. closure = (zend_closure *) Z_OBJ_P(ZEND_THIS);
  121. newobj = Z_OBJ_P(newthis);
  122. newclass = newobj->ce;
  123. if (!zend_valid_closure_binding(closure, newthis, newclass)) {
  124. return;
  125. }
  126. if (closure->func.common.fn_flags & ZEND_ACC_GENERATOR) {
  127. zval new_closure;
  128. zend_create_closure(&new_closure, &closure->func, newclass, closure->called_scope, newthis);
  129. closure = (zend_closure *) Z_OBJ(new_closure);
  130. fci_cache.function_handler = &closure->func;
  131. } else {
  132. memcpy(&my_function, &closure->func, closure->func.type == ZEND_USER_FUNCTION ? sizeof(zend_op_array) : sizeof(zend_internal_function));
  133. my_function.common.fn_flags &= ~ZEND_ACC_CLOSURE;
  134. /* use scope of passed object */
  135. my_function.common.scope = newclass;
  136. if (closure->func.type == ZEND_INTERNAL_FUNCTION) {
  137. my_function.internal_function.handler = closure->orig_internal_handler;
  138. }
  139. fci_cache.function_handler = &my_function;
  140. /* Runtime cache relies on bound scope to be immutable, hence we need a separate rt cache in case scope changed */
  141. if (ZEND_USER_CODE(my_function.type)
  142. && (closure->func.common.scope != newclass
  143. || (closure->func.common.fn_flags & ZEND_ACC_HEAP_RT_CACHE))) {
  144. void *ptr;
  145. my_function.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
  146. ptr = emalloc(sizeof(void*) + my_function.op_array.cache_size);
  147. ZEND_MAP_PTR_INIT(my_function.op_array.run_time_cache, ptr);
  148. ptr = (char*)ptr + sizeof(void*);
  149. ZEND_MAP_PTR_SET(my_function.op_array.run_time_cache, ptr);
  150. memset(ptr, 0, my_function.op_array.cache_size);
  151. }
  152. }
  153. fci_cache.called_scope = newclass;
  154. fci_cache.object = fci.object = newobj;
  155. fci.size = sizeof(fci);
  156. ZVAL_OBJ(&fci.function_name, &closure->std);
  157. fci.retval = &closure_result;
  158. if (zend_call_function(&fci, &fci_cache) == SUCCESS && Z_TYPE(closure_result) != IS_UNDEF) {
  159. if (Z_ISREF(closure_result)) {
  160. zend_unwrap_reference(&closure_result);
  161. }
  162. ZVAL_COPY_VALUE(return_value, &closure_result);
  163. }
  164. if (fci_cache.function_handler->common.fn_flags & ZEND_ACC_GENERATOR) {
  165. /* copied upon generator creation */
  166. GC_DELREF(&closure->std);
  167. } else if (ZEND_USER_CODE(my_function.type)
  168. && (fci_cache.function_handler->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE)) {
  169. efree(ZEND_MAP_PTR(my_function.op_array.run_time_cache));
  170. }
  171. }
  172. /* }}} */
  173. static void do_closure_bind(zval *return_value, zval *zclosure, zval *newthis, zend_object *scope_obj, zend_string *scope_str)
  174. {
  175. zend_class_entry *ce, *called_scope;
  176. zend_closure *closure = (zend_closure *) Z_OBJ_P(zclosure);
  177. if (scope_obj) {
  178. ce = scope_obj->ce;
  179. } else if (scope_str) {
  180. if (zend_string_equals(scope_str, ZSTR_KNOWN(ZEND_STR_STATIC))) {
  181. ce = closure->func.common.scope;
  182. } else if ((ce = zend_lookup_class(scope_str)) == NULL) {
  183. zend_error(E_WARNING, "Class \"%s\" not found", ZSTR_VAL(scope_str));
  184. RETURN_NULL();
  185. }
  186. } else {
  187. ce = NULL;
  188. }
  189. if (!zend_valid_closure_binding(closure, newthis, ce)) {
  190. return;
  191. }
  192. if (newthis) {
  193. called_scope = Z_OBJCE_P(newthis);
  194. } else {
  195. called_scope = ce;
  196. }
  197. zend_create_closure(return_value, &closure->func, ce, called_scope, newthis);
  198. }
  199. /* {{{ Create a closure from another one and bind to another object and scope */
  200. ZEND_METHOD(Closure, bind)
  201. {
  202. zval *zclosure, *newthis;
  203. zend_object *scope_obj = NULL;
  204. zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC);
  205. ZEND_PARSE_PARAMETERS_START(2, 3)
  206. Z_PARAM_OBJECT_OF_CLASS(zclosure, zend_ce_closure)
  207. Z_PARAM_OBJECT_OR_NULL(newthis)
  208. Z_PARAM_OPTIONAL
  209. Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str)
  210. ZEND_PARSE_PARAMETERS_END();
  211. do_closure_bind(return_value, zclosure, newthis, scope_obj, scope_str);
  212. }
  213. /* {{{ Create a closure from another one and bind to another object and scope */
  214. ZEND_METHOD(Closure, bindTo)
  215. {
  216. zval *newthis;
  217. zend_object *scope_obj = NULL;
  218. zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC);
  219. ZEND_PARSE_PARAMETERS_START(1, 2)
  220. Z_PARAM_OBJECT_OR_NULL(newthis)
  221. Z_PARAM_OPTIONAL
  222. Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str)
  223. ZEND_PARSE_PARAMETERS_END();
  224. do_closure_bind(return_value, getThis(), newthis, scope_obj, scope_str);
  225. }
  226. static ZEND_NAMED_FUNCTION(zend_closure_call_magic) /* {{{ */ {
  227. zend_fcall_info fci;
  228. zend_fcall_info_cache fcc;
  229. zval params[2];
  230. memset(&fci, 0, sizeof(zend_fcall_info));
  231. memset(&fcc, 0, sizeof(zend_fcall_info_cache));
  232. fci.size = sizeof(zend_fcall_info);
  233. fci.retval = return_value;
  234. fcc.function_handler = (EX(func)->internal_function.fn_flags & ZEND_ACC_STATIC) ?
  235. EX(func)->internal_function.scope->__callstatic : EX(func)->internal_function.scope->__call;
  236. fci.named_params = NULL;
  237. fci.params = params;
  238. fci.param_count = 2;
  239. ZVAL_STR(&fci.params[0], EX(func)->common.function_name);
  240. if (ZEND_NUM_ARGS()) {
  241. array_init_size(&fci.params[1], ZEND_NUM_ARGS());
  242. zend_copy_parameters_array(ZEND_NUM_ARGS(), &fci.params[1]);
  243. } else {
  244. ZVAL_EMPTY_ARRAY(&fci.params[1]);
  245. }
  246. fcc.object = fci.object = Z_OBJ_P(ZEND_THIS);
  247. fcc.called_scope = zend_get_called_scope(EG(current_execute_data));
  248. zend_call_function(&fci, &fcc);
  249. zval_ptr_dtor(&fci.params[1]);
  250. }
  251. /* }}} */
  252. static zend_result zend_create_closure_from_callable(zval *return_value, zval *callable, char **error) /* {{{ */ {
  253. zend_fcall_info_cache fcc;
  254. zend_function *mptr;
  255. zval instance;
  256. zend_internal_function call;
  257. if (!zend_is_callable_ex(callable, NULL, 0, NULL, &fcc, error)) {
  258. return FAILURE;
  259. }
  260. mptr = fcc.function_handler;
  261. if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  262. /* For Closure::fromCallable([$closure, "__invoke"]) return $closure. */
  263. if (fcc.object && fcc.object->ce == zend_ce_closure
  264. && zend_string_equals_literal(mptr->common.function_name, "__invoke")) {
  265. RETVAL_OBJ_COPY(fcc.object);
  266. zend_free_trampoline(mptr);
  267. return SUCCESS;
  268. }
  269. if (!mptr->common.scope) {
  270. return FAILURE;
  271. }
  272. if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
  273. if (!mptr->common.scope->__callstatic) {
  274. return FAILURE;
  275. }
  276. } else {
  277. if (!mptr->common.scope->__call) {
  278. return FAILURE;
  279. }
  280. }
  281. memset(&call, 0, sizeof(zend_internal_function));
  282. call.type = ZEND_INTERNAL_FUNCTION;
  283. call.fn_flags = mptr->common.fn_flags & ZEND_ACC_STATIC;
  284. call.handler = zend_closure_call_magic;
  285. call.function_name = mptr->common.function_name;
  286. call.scope = mptr->common.scope;
  287. zend_free_trampoline(mptr);
  288. mptr = (zend_function *) &call;
  289. }
  290. if (fcc.object) {
  291. ZVAL_OBJ(&instance, fcc.object);
  292. zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, &instance);
  293. } else {
  294. zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, NULL);
  295. }
  296. if (&mptr->internal_function == &call) {
  297. zend_string_release(mptr->common.function_name);
  298. }
  299. return SUCCESS;
  300. }
  301. /* }}} */
  302. /* {{{ Create a closure from a callable using the current scope. */
  303. ZEND_METHOD(Closure, fromCallable)
  304. {
  305. zval *callable;
  306. char *error = NULL;
  307. ZEND_PARSE_PARAMETERS_START(1, 1)
  308. Z_PARAM_ZVAL(callable)
  309. ZEND_PARSE_PARAMETERS_END();
  310. if (Z_TYPE_P(callable) == IS_OBJECT && instanceof_function(Z_OBJCE_P(callable), zend_ce_closure)) {
  311. /* It's already a closure */
  312. RETURN_COPY(callable);
  313. }
  314. if (zend_create_closure_from_callable(return_value, callable, &error) == FAILURE) {
  315. if (error) {
  316. zend_type_error("Failed to create closure from callable: %s", error);
  317. efree(error);
  318. } else {
  319. zend_type_error("Failed to create closure from callable");
  320. }
  321. }
  322. }
  323. /* }}} */
  324. static ZEND_COLD zend_function *zend_closure_get_constructor(zend_object *object) /* {{{ */
  325. {
  326. zend_throw_error(NULL, "Instantiation of class Closure is not allowed");
  327. return NULL;
  328. }
  329. /* }}} */
  330. /* int return due to Object Handler API */
  331. static int zend_closure_compare(zval *o1, zval *o2) /* {{{ */
  332. {
  333. ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
  334. zend_closure *lhs = (zend_closure*) Z_OBJ_P(o1);
  335. zend_closure *rhs = (zend_closure*) Z_OBJ_P(o2);
  336. if (!((lhs->func.common.fn_flags & ZEND_ACC_FAKE_CLOSURE) && (rhs->func.common.fn_flags & ZEND_ACC_FAKE_CLOSURE))) {
  337. return ZEND_UNCOMPARABLE;
  338. }
  339. if (Z_TYPE(lhs->this_ptr) != Z_TYPE(rhs->this_ptr)) {
  340. return ZEND_UNCOMPARABLE;
  341. }
  342. if (Z_TYPE(lhs->this_ptr) == IS_OBJECT && Z_OBJ(lhs->this_ptr) != Z_OBJ(rhs->this_ptr)) {
  343. return ZEND_UNCOMPARABLE;
  344. }
  345. if (lhs->called_scope != rhs->called_scope) {
  346. return ZEND_UNCOMPARABLE;
  347. }
  348. if (lhs->func.type != rhs->func.type) {
  349. return ZEND_UNCOMPARABLE;
  350. }
  351. if (lhs->func.common.scope != rhs->func.common.scope) {
  352. return ZEND_UNCOMPARABLE;
  353. }
  354. if (!zend_string_equals(lhs->func.common.function_name, rhs->func.common.function_name)) {
  355. return ZEND_UNCOMPARABLE;
  356. }
  357. return 0;
  358. }
  359. /* }}} */
  360. ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *object) /* {{{ */
  361. {
  362. zend_closure *closure = (zend_closure *)object;
  363. zend_function *invoke = (zend_function*)emalloc(sizeof(zend_function));
  364. const uint32_t keep_flags =
  365. ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_VARIADIC | ZEND_ACC_HAS_RETURN_TYPE;
  366. invoke->common = closure->func.common;
  367. /* We return ZEND_INTERNAL_FUNCTION, but arg_info representation is the
  368. * same as for ZEND_USER_FUNCTION (uses zend_string* instead of char*).
  369. * This is not a problem, because ZEND_ACC_HAS_TYPE_HINTS is never set,
  370. * and we won't check arguments on internal function. We also set
  371. * ZEND_ACC_USER_ARG_INFO flag to prevent invalid usage by Reflection */
  372. invoke->type = ZEND_INTERNAL_FUNCTION;
  373. invoke->internal_function.fn_flags =
  374. ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER | (closure->func.common.fn_flags & keep_flags);
  375. if (closure->func.type != ZEND_INTERNAL_FUNCTION || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
  376. invoke->internal_function.fn_flags |=
  377. ZEND_ACC_USER_ARG_INFO;
  378. }
  379. invoke->internal_function.handler = ZEND_MN(Closure___invoke);
  380. invoke->internal_function.module = 0;
  381. invoke->internal_function.scope = zend_ce_closure;
  382. invoke->internal_function.function_name = ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE);
  383. return invoke;
  384. }
  385. /* }}} */
  386. ZEND_API const zend_function *zend_get_closure_method_def(zend_object *obj) /* {{{ */
  387. {
  388. zend_closure *closure = (zend_closure *) obj;
  389. return &closure->func;
  390. }
  391. /* }}} */
  392. ZEND_API zval* zend_get_closure_this_ptr(zval *obj) /* {{{ */
  393. {
  394. zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
  395. return &closure->this_ptr;
  396. }
  397. /* }}} */
  398. static zend_function *zend_closure_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
  399. {
  400. if (zend_string_equals_literal_ci(method, ZEND_INVOKE_FUNC_NAME)) {
  401. return zend_get_closure_invoke_method(*object);
  402. }
  403. return zend_std_get_method(object, method, key);
  404. }
  405. /* }}} */
  406. static void zend_closure_free_storage(zend_object *object) /* {{{ */
  407. {
  408. zend_closure *closure = (zend_closure *)object;
  409. zend_object_std_dtor(&closure->std);
  410. if (closure->func.type == ZEND_USER_FUNCTION) {
  411. /* We don't own the static variables of fake closures. */
  412. if (!(closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE)) {
  413. zend_destroy_static_vars(&closure->func.op_array);
  414. }
  415. destroy_op_array(&closure->func.op_array);
  416. } else if (closure->func.type == ZEND_INTERNAL_FUNCTION) {
  417. zend_string_release(closure->func.common.function_name);
  418. }
  419. if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
  420. zval_ptr_dtor(&closure->this_ptr);
  421. }
  422. }
  423. /* }}} */
  424. static zend_object *zend_closure_new(zend_class_entry *class_type) /* {{{ */
  425. {
  426. zend_closure *closure;
  427. closure = emalloc(sizeof(zend_closure));
  428. memset(closure, 0, sizeof(zend_closure));
  429. zend_object_std_init(&closure->std, class_type);
  430. closure->std.handlers = &closure_handlers;
  431. return (zend_object*)closure;
  432. }
  433. /* }}} */
  434. static zend_object *zend_closure_clone(zend_object *zobject) /* {{{ */
  435. {
  436. zend_closure *closure = (zend_closure *)zobject;
  437. zval result;
  438. zend_create_closure(&result, &closure->func,
  439. closure->func.common.scope, closure->called_scope, &closure->this_ptr);
  440. return Z_OBJ(result);
  441. }
  442. /* }}} */
  443. int zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only) /* {{{ */
  444. {
  445. zend_closure *closure = (zend_closure*)obj;
  446. *fptr_ptr = &closure->func;
  447. *ce_ptr = closure->called_scope;
  448. if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
  449. *obj_ptr = Z_OBJ(closure->this_ptr);
  450. } else {
  451. *obj_ptr = NULL;
  452. }
  453. return SUCCESS;
  454. }
  455. /* }}} */
  456. /* *is_temp is int due to Object Handler API */
  457. static HashTable *zend_closure_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
  458. {
  459. zend_closure *closure = (zend_closure *)object;
  460. zval val;
  461. struct _zend_arg_info *arg_info = closure->func.common.arg_info;
  462. HashTable *debug_info;
  463. bool zstr_args = (closure->func.type == ZEND_USER_FUNCTION) || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO);
  464. *is_temp = 1;
  465. debug_info = zend_new_array(8);
  466. if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) {
  467. zval *var;
  468. zend_string *key;
  469. HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
  470. array_init(&val);
  471. ZEND_HASH_FOREACH_STR_KEY_VAL(static_variables, key, var) {
  472. zval copy;
  473. if (Z_TYPE_P(var) == IS_CONSTANT_AST) {
  474. ZVAL_STRING(&copy, "<constant ast>");
  475. } else {
  476. if (Z_ISREF_P(var) && Z_REFCOUNT_P(var) == 1) {
  477. var = Z_REFVAL_P(var);
  478. }
  479. ZVAL_COPY(&copy, var);
  480. }
  481. zend_hash_add_new(Z_ARRVAL(val), key, &copy);
  482. } ZEND_HASH_FOREACH_END();
  483. if (zend_hash_num_elements(Z_ARRVAL(val))) {
  484. zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_STATIC), &val);
  485. } else {
  486. zval_ptr_dtor(&val);
  487. }
  488. }
  489. if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
  490. Z_ADDREF(closure->this_ptr);
  491. zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_THIS), &closure->this_ptr);
  492. }
  493. if (arg_info &&
  494. (closure->func.common.num_args ||
  495. (closure->func.common.fn_flags & ZEND_ACC_VARIADIC))) {
  496. uint32_t i, num_args, required = closure->func.common.required_num_args;
  497. array_init(&val);
  498. num_args = closure->func.common.num_args;
  499. if (closure->func.common.fn_flags & ZEND_ACC_VARIADIC) {
  500. num_args++;
  501. }
  502. for (i = 0; i < num_args; i++) {
  503. zend_string *name;
  504. zval info;
  505. ZEND_ASSERT(arg_info->name && "Argument should have name");
  506. if (zstr_args) {
  507. name = zend_strpprintf(0, "%s$%s",
  508. ZEND_ARG_SEND_MODE(arg_info) ? "&" : "",
  509. ZSTR_VAL(arg_info->name));
  510. } else {
  511. name = zend_strpprintf(0, "%s$%s",
  512. ZEND_ARG_SEND_MODE(arg_info) ? "&" : "",
  513. ((zend_internal_arg_info*)arg_info)->name);
  514. }
  515. ZVAL_NEW_STR(&info, zend_strpprintf(0, "%s", i >= required ? "<optional>" : "<required>"));
  516. zend_hash_update(Z_ARRVAL(val), name, &info);
  517. zend_string_release_ex(name, 0);
  518. arg_info++;
  519. }
  520. zend_hash_str_update(debug_info, "parameter", sizeof("parameter")-1, &val);
  521. }
  522. return debug_info;
  523. }
  524. /* }}} */
  525. static HashTable *zend_closure_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */
  526. {
  527. zend_closure *closure = (zend_closure *)obj;
  528. *table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL;
  529. *n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0;
  530. /* Fake closures don't own the static variables they reference. */
  531. return (closure->func.type == ZEND_USER_FUNCTION
  532. && !(closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE)) ?
  533. ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr) : NULL;
  534. }
  535. /* }}} */
  536. /* {{{ Private constructor preventing instantiation */
  537. ZEND_COLD ZEND_METHOD(Closure, __construct)
  538. {
  539. zend_throw_error(NULL, "Instantiation of class Closure is not allowed");
  540. }
  541. /* }}} */
  542. void zend_register_closure_ce(void) /* {{{ */
  543. {
  544. zend_ce_closure = register_class_Closure();
  545. zend_ce_closure->create_object = zend_closure_new;
  546. memcpy(&closure_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  547. closure_handlers.free_obj = zend_closure_free_storage;
  548. closure_handlers.get_constructor = zend_closure_get_constructor;
  549. closure_handlers.get_method = zend_closure_get_method;
  550. closure_handlers.compare = zend_closure_compare;
  551. closure_handlers.clone_obj = zend_closure_clone;
  552. closure_handlers.get_debug_info = zend_closure_get_debug_info;
  553. closure_handlers.get_closure = zend_closure_get_closure;
  554. closure_handlers.get_gc = zend_closure_get_gc;
  555. }
  556. /* }}} */
  557. static ZEND_NAMED_FUNCTION(zend_closure_internal_handler) /* {{{ */
  558. {
  559. zend_closure *closure = (zend_closure*)ZEND_CLOSURE_OBJECT(EX(func));
  560. closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  561. OBJ_RELEASE((zend_object*)closure);
  562. EX(func) = NULL;
  563. }
  564. /* }}} */
  565. static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr, bool is_fake) /* {{{ */
  566. {
  567. zend_closure *closure;
  568. object_init_ex(res, zend_ce_closure);
  569. closure = (zend_closure *)Z_OBJ_P(res);
  570. if ((scope == NULL) && this_ptr && (Z_TYPE_P(this_ptr) != IS_UNDEF)) {
  571. /* use dummy scope if we're binding an object without specifying a scope */
  572. /* maybe it would be better to create one for this purpose */
  573. scope = zend_ce_closure;
  574. }
  575. if (func->type == ZEND_USER_FUNCTION) {
  576. memcpy(&closure->func, func, sizeof(zend_op_array));
  577. closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
  578. closure->func.common.fn_flags &= ~ZEND_ACC_IMMUTABLE;
  579. /* For fake closures, we want to reuse the static variables of the original function. */
  580. if (!is_fake) {
  581. if (closure->func.op_array.static_variables) {
  582. closure->func.op_array.static_variables =
  583. zend_array_dup(closure->func.op_array.static_variables);
  584. }
  585. ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr,
  586. &closure->func.op_array.static_variables);
  587. } else if (func->op_array.static_variables) {
  588. HashTable *ht = ZEND_MAP_PTR_GET(func->op_array.static_variables_ptr);
  589. if (!ht) {
  590. ht = zend_array_dup(func->op_array.static_variables);
  591. ZEND_MAP_PTR_SET(closure->func.op_array.static_variables_ptr, ht);
  592. }
  593. }
  594. /* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */
  595. if (!ZEND_MAP_PTR_GET(closure->func.op_array.run_time_cache)
  596. || func->common.scope != scope
  597. || (func->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE)
  598. ) {
  599. void *ptr;
  600. if (!ZEND_MAP_PTR_GET(func->op_array.run_time_cache)
  601. && (func->common.fn_flags & ZEND_ACC_CLOSURE)
  602. && (func->common.scope == scope ||
  603. !(func->common.fn_flags & ZEND_ACC_IMMUTABLE))) {
  604. /* If a real closure is used for the first time, we create a shared runtime cache
  605. * and remember which scope it is for. */
  606. if (func->common.scope != scope) {
  607. func->common.scope = scope;
  608. }
  609. closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE;
  610. ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
  611. ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr);
  612. ZEND_MAP_PTR_SET(closure->func.op_array.run_time_cache, ptr);
  613. } else {
  614. /* Otherwise, we use a non-shared runtime cache */
  615. closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
  616. ptr = emalloc(sizeof(void*) + func->op_array.cache_size);
  617. ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr);
  618. ptr = (char*)ptr + sizeof(void*);
  619. ZEND_MAP_PTR_SET(closure->func.op_array.run_time_cache, ptr);
  620. }
  621. memset(ptr, 0, func->op_array.cache_size);
  622. }
  623. zend_string_addref(closure->func.op_array.function_name);
  624. if (closure->func.op_array.refcount) {
  625. (*closure->func.op_array.refcount)++;
  626. }
  627. } else {
  628. memcpy(&closure->func, func, sizeof(zend_internal_function));
  629. closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
  630. /* wrap internal function handler to avoid memory leak */
  631. if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) {
  632. /* avoid infinity recursion, by taking handler from nested closure */
  633. zend_closure *nested = (zend_closure*)((char*)func - XtOffsetOf(zend_closure, func));
  634. ZEND_ASSERT(nested->std.ce == zend_ce_closure);
  635. closure->orig_internal_handler = nested->orig_internal_handler;
  636. } else {
  637. closure->orig_internal_handler = closure->func.internal_function.handler;
  638. }
  639. closure->func.internal_function.handler = zend_closure_internal_handler;
  640. zend_string_addref(closure->func.op_array.function_name);
  641. if (!func->common.scope) {
  642. /* if it's a free function, we won't set scope & this since they're meaningless */
  643. this_ptr = NULL;
  644. scope = NULL;
  645. }
  646. }
  647. ZVAL_UNDEF(&closure->this_ptr);
  648. /* Invariant:
  649. * If the closure is unscoped or static, it has no bound object. */
  650. closure->func.common.scope = scope;
  651. closure->called_scope = called_scope;
  652. if (scope) {
  653. closure->func.common.fn_flags |= ZEND_ACC_PUBLIC;
  654. if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) {
  655. ZVAL_OBJ_COPY(&closure->this_ptr, Z_OBJ_P(this_ptr));
  656. }
  657. }
  658. }
  659. /* }}} */
  660. ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr)
  661. {
  662. zend_create_closure_ex(res, func, scope, called_scope, this_ptr,
  663. /* is_fake */ (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0);
  664. }
  665. ZEND_API void zend_create_fake_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
  666. {
  667. zend_closure *closure;
  668. zend_create_closure_ex(res, func, scope, called_scope, this_ptr, /* is_fake */ true);
  669. closure = (zend_closure *)Z_OBJ_P(res);
  670. closure->func.common.fn_flags |= ZEND_ACC_FAKE_CLOSURE;
  671. }
  672. /* }}} */
  673. void zend_closure_from_frame(zval *return_value, zend_execute_data *call) { /* {{{ */
  674. zval instance;
  675. zend_internal_function trampoline;
  676. zend_function *mptr = call->func;
  677. if (ZEND_CALL_INFO(call) & ZEND_CALL_CLOSURE) {
  678. RETURN_OBJ(ZEND_CLOSURE_OBJECT(mptr));
  679. }
  680. if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  681. if ((ZEND_CALL_INFO(call) & ZEND_CALL_HAS_THIS) &&
  682. (Z_OBJCE(call->This) == zend_ce_closure)
  683. && zend_string_equals_literal(mptr->common.function_name, "__invoke")) {
  684. zend_free_trampoline(mptr);
  685. RETURN_OBJ_COPY(Z_OBJ(call->This));
  686. }
  687. memset(&trampoline, 0, sizeof(zend_internal_function));
  688. trampoline.type = ZEND_INTERNAL_FUNCTION;
  689. trampoline.fn_flags = mptr->common.fn_flags & ZEND_ACC_STATIC;
  690. trampoline.handler = zend_closure_call_magic;
  691. trampoline.function_name = mptr->common.function_name;
  692. trampoline.scope = mptr->common.scope;
  693. zend_free_trampoline(mptr);
  694. mptr = (zend_function *) &trampoline;
  695. }
  696. if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_THIS) {
  697. ZVAL_OBJ(&instance, Z_OBJ(call->This));
  698. zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_OBJCE(instance), &instance);
  699. } else {
  700. zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_CE(call->This), NULL);
  701. }
  702. if (&mptr->internal_function == &trampoline) {
  703. zend_string_release(mptr->common.function_name);
  704. }
  705. } /* }}} */
  706. void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var) /* {{{ */
  707. {
  708. zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
  709. HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
  710. zend_hash_update(static_variables, var_name, var);
  711. }
  712. /* }}} */
  713. void zend_closure_bind_var_ex(zval *closure_zv, uint32_t offset, zval *val) /* {{{ */
  714. {
  715. zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
  716. HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr);
  717. zval *var = (zval*)((char*)static_variables->arData + offset);
  718. zval_ptr_dtor(var);
  719. ZVAL_COPY_VALUE(var, val);
  720. }
  721. /* }}} */