Skip to content

Commit 26d0eb1

Browse files
committed
Omit FETCH_THIS in closures
Non-static closures are guaranteed to have $this. The existing comment highlights this, but fails to handle it correctly.
1 parent d2a9edf commit 26d0eb1

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

Zend/zend_compile.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,11 @@ static bool zend_is_not_imported(zend_string *name) {
313313
return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
314314
}
315315

316-
void zend_oparray_context_begin(zend_oparray_context *prev_context) /* {{{ */
316+
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
317317
{
318318
*prev_context = CG(context);
319+
CG(context).prev = CG(context).op_array ? prev_context : NULL;
320+
CG(context).op_array = op_array;
319321
CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
320322
CG(context).vars_size = 0;
321323
CG(context).literals_size = 0;
@@ -2920,11 +2922,21 @@ static bool is_global_var_fetch(zend_ast *ast)
29202922

29212923
static bool this_guaranteed_exists(void) /* {{{ */
29222924
{
2923-
zend_op_array *op_array = CG(active_op_array);
2924-
/* Instance methods always have a $this.
2925-
* This also includes closures that have a scope and use $this. */
2926-
return op_array->scope != NULL
2927-
&& (op_array->fn_flags & ZEND_ACC_STATIC) == 0;
2925+
zend_oparray_context *ctx = &CG(context);
2926+
while (ctx) {
2927+
/* Instance methods always have a $this.
2928+
* This also includes closures that have a scope and use $this. */
2929+
zend_op_array *op_array = ctx->op_array;
2930+
if (op_array->fn_flags & ZEND_ACC_STATIC) {
2931+
return false;
2932+
} else if (op_array->scope) {
2933+
return true;
2934+
} else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
2935+
return false;
2936+
}
2937+
ctx = ctx->prev;
2938+
}
2939+
return false;
29282940
}
29292941
/* }}} */
29302942

@@ -7869,7 +7881,7 @@ static void zend_compile_func_decl(znode *result, zend_ast *ast, bool toplevel)
78697881
op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
78707882
}
78717883

7872-
zend_oparray_context_begin(&orig_oparray_context);
7884+
zend_oparray_context_begin(&orig_oparray_context, op_array);
78737885

78747886
{
78757887
/* Push a separator to the loop variable stack */

Zend/zend_compile.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ typedef struct _zend_live_range {
190190

191191
/* Compilation context that is different for each op array. */
192192
typedef struct _zend_oparray_context {
193+
struct _zend_oparray_context *prev;
194+
zend_op_array *op_array;
193195
uint32_t opcodes_size;
194196
int vars_size;
195197
int literals_size;
@@ -802,7 +804,7 @@ void init_compiler(void);
802804
void shutdown_compiler(void);
803805
void zend_init_compiler_data_structures(void);
804806

805-
void zend_oparray_context_begin(zend_oparray_context *prev_context);
807+
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array);
806808
void zend_oparray_context_end(zend_oparray_context *prev_context);
807809
void zend_file_context_begin(zend_file_context *prev_context);
808810
void zend_file_context_end(zend_file_context *prev_context);

Zend/zend_language_scanner.l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ static zend_op_array *zend_compile(int type)
614614
}
615615

616616
zend_file_context_begin(&original_file_context);
617-
zend_oparray_context_begin(&original_oparray_context);
617+
zend_oparray_context_begin(&original_oparray_context, op_array);
618618
zend_compile_top_stmt(CG(ast));
619619
CG(zend_lineno) = last_lineno;
620620
zend_emit_final_return(type == ZEND_USER_FUNCTION);

0 commit comments

Comments
 (0)