-
Notifications
You must be signed in to change notification settings - Fork 1
/
implicit_closures.c
303 lines (253 loc) · 9.05 KB
/
implicit_closures.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* implicit_closures extension for PHP */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_implicit_closures.h"
void (*original_ast_process_function)(zend_ast *ast);
typedef struct {
HashTable uses; // variables used by closure
HashTable locals; // variables created by closure
} closure_info;
static zend_string *get_var_name_from_ast(zend_ast *ast) {
if (!ast) {
return NULL;
}
zend_ast *name_ast = ast->child[0];
if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
return NULL;
}
zend_string *name = zend_ast_get_str(name_ast);
if (zend_is_auto_global(name) || zend_string_equals_literal(name, "this")) {
return NULL;
}
return name;
}
static void check_ast_for_locals(zend_ast *ast, closure_info *info) {
if (!ast || !info) {
return;
}
if (ast->kind != ZEND_AST_VAR) {
return;
}
zend_string *name = get_var_name_from_ast(ast);
if (!name) {
return;
}
zend_hash_add_empty_element(&info->locals, name);
}
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
if (!ast) {
return;
}
switch (ast->kind) {
case ZEND_AST_ASSIGN:
case ZEND_AST_ASSIGN_REF:
if (ast->child[0]->kind == ZEND_AST_VAR) {
check_ast_for_locals(ast->child[0], info);
} else if (ast->child[0]->kind == ZEND_AST_ARRAY) {
zend_ast_list *list = zend_ast_get_list(ast->child[0]);
for (uint32_t i = 0; i < list->children; i++) {
if (list->child[i] && list->child[i]->kind == ZEND_AST_ARRAY_ELEM) {
check_ast_for_locals(list->child[i]->child[0], info);
}
}
} else {
find_implicit_binds_recursively(info, ast->child[0]);
}
find_implicit_binds_recursively(info, ast->child[1]);
break;
case ZEND_AST_CATCH:
if (ast->child[1]) {
check_ast_for_locals(ast->child[1], info);
}
break;
case ZEND_AST_FOREACH:
find_implicit_binds_recursively(info, ast->child[0]);
check_ast_for_locals(ast->child[1], info);
if (ast->child[2]) {
check_ast_for_locals(ast->child[2], info);
}
break;
case ZEND_AST_VAR: {
zend_string *name = get_var_name_from_ast(ast);
if (name) {
zval lineno;
ZVAL_LONG(&lineno, ast->lineno);
zend_hash_add(&info->uses, name, &lineno);
} else {
zend_ast *name_ast = ast->child[0];
if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
find_implicit_binds_recursively(info, name_ast);
}
}
break;
}
case ZEND_AST_CLOSURE: {
zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
zend_ast *uses_ast = closure_ast->child[1];
if (uses_ast) {
zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
for (uint32_t i = 0; i < uses_list->children; i++) {
zval lineno;
ZVAL_LONG(&lineno, ast->lineno);
zend_hash_add(&info->uses, zend_ast_get_str(uses_list->child[i]), &lineno);
}
}
break;
}
case ZEND_AST_ARROW_FUNC: {
zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
zend_ast_list *params_list = zend_ast_get_list(closure_ast->child[0]);
for (uint32_t i = 0; i < params_list->children; i++) {
zend_string *name = zend_ast_get_str(params_list->child[i]->child[1]);
if (name) {
zend_hash_add_empty_element(&info->locals, name);
}
}
find_implicit_binds_recursively(info, closure_ast->child[2]);
break;
}
default:
if (zend_ast_is_list(ast)) {
zend_ast_list *list = zend_ast_get_list(ast);
for (uint32_t i = 0; i < list->children; i++) {
find_implicit_binds_recursively(info, list->child[i]);
}
} else if (!zend_ast_is_special(ast)) {
uint32_t children = zend_ast_get_num_children(ast);
for (uint32_t i = 0; i < children; i++) {
find_implicit_binds_recursively(info, ast->child[i]);
}
}
}
}
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast, zend_ast *uses_ast) {
zend_ast_list *param_list = zend_ast_get_list(params_ast);
zend_ast_list *use_list = zend_ast_get_list(uses_ast);
zend_hash_init(&info->uses, param_list->children + use_list->children, NULL, NULL, 0);
zend_hash_init(&info->locals, HT_MIN_SIZE, NULL, NULL, 0);
find_implicit_binds_recursively(info, stmt_ast);
// remove parameter variables
for (uint32_t i = 0; i < param_list->children; i++) {
zend_ast *param_ast = param_list->child[i];
zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
}
// remove use variables
for (uint32_t i = 0; i < use_list->children; i++) {
zend_ast *use_ast = use_list->child[i];
zend_hash_del(&info->uses, zend_ast_get_str(use_ast));
}
// remove closure's local variables
zend_string *var_name;
ZEND_HASH_FOREACH_STR_KEY(&info->locals, var_name) {
zend_hash_del(&info->uses, var_name);
} ZEND_HASH_FOREACH_END();
}
#if PHP_VERSION_ID >= 80100
static void make_implicit_bindings(zend_ast **ast_ptr, void *context) {
#else
static void make_implicit_bindings(zend_ast **ast_ptr) {
#endif
zend_ast_decl *decl;
zend_ast *ast = *ast_ptr;
if (ast == NULL) {
return;
}
switch (ast->kind) {
case ZEND_AST_CLOSURE:
decl = (zend_ast_decl *) ast;
zend_ast *params_ast = decl->child[0];
zend_ast *uses_ast = decl->child[1];
zend_ast *stmt_ast = decl->child[2];
// go deeper to handle nested closures first
#if PHP_VERSION_ID >= 80100
make_implicit_bindings(&stmt_ast, NULL);
#else
make_implicit_bindings(&stmt_ast);
#endif
closure_info info;
if (!uses_ast) {
uses_ast = zend_ast_create_list(0, ZEND_AST_CLOSURE_USES);
}
find_implicit_binds(&info, params_ast, stmt_ast, uses_ast);
zend_string *var_name;
zval *lineno;
ZEND_HASH_FOREACH_STR_KEY_VAL(&info.uses, var_name, lineno) {
zend_ast *use_var = zend_ast_create_zval_from_str(zend_string_dup(var_name, 0));
Z_LINENO(((zend_ast_zval *)use_var)->val) = Z_LVAL_P(lineno);
uses_ast = zend_ast_list_add(uses_ast, use_var);
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&info.uses);
zend_hash_destroy(&info.locals);
decl->child[1] = uses_ast;
break;
case ZEND_AST_FUNC_DECL:
case ZEND_AST_METHOD:
case ZEND_AST_CLASS:
case ZEND_AST_ARROW_FUNC:
decl = (zend_ast_decl *) ast;
if (decl->child[2]) {
#if PHP_VERSION_ID >= 80100
zend_ast_apply(decl->child[2], make_implicit_bindings, NULL);
#else
zend_ast_apply(decl->child[2], make_implicit_bindings);
#endif
}
break;
default:
#if PHP_VERSION_ID >= 80100
zend_ast_apply(ast, make_implicit_bindings, NULL);
#else
zend_ast_apply(ast, make_implicit_bindings);
#endif
}
}
void implicit_closures_ast_process(zend_ast *ast) {
#if PHP_VERSION_ID >= 80100
make_implicit_bindings(&ast, NULL);
#else
make_implicit_bindings(&ast);;
#endif
if (original_ast_process_function) {
original_ast_process_function(ast);
}
}
PHP_MINIT_FUNCTION(implicit_closures) {
original_ast_process_function = zend_ast_process;
zend_ast_process = implicit_closures_ast_process;
return SUCCESS;
}
PHP_RINIT_FUNCTION(implicit_closures) {
#if defined(ZTS) && defined(COMPILE_DL_IMPLICIT_CLOSURES)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
PHP_MINFO_FUNCTION(implicit_closures) {
php_info_print_table_start();
php_info_print_table_header(2, "implicit_closures support", "enabled");
php_info_print_table_end();
}
static const zend_function_entry ext_functions[] = {
ZEND_FE_END
};
zend_module_entry implicit_closures_module_entry = {
STANDARD_MODULE_HEADER,
"implicit_closures",
ext_functions,
PHP_MINIT(implicit_closures),
NULL,
PHP_RINIT(implicit_closures),
NULL,
PHP_MINFO(implicit_closures),
PHP_IMPLICIT_CLOSURES_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_IMPLICIT_CLOSURES
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(implicit_closures)
#endif