@@ -32,25 +32,48 @@ typedef struct {
3232 int32_t in_function_calls_stack;
3333 int32_t in_first_level_statement;
3434 int32_t after_extract_call;
35- /* memo props to reset (missing => -1) */
36- int32_t resolved_types;
37- int32_t truthy_scopes;
38- int32_t falsey_scopes;
39- int32_t node_callback_scope;
40- int32_t scope_out_of_first_level;
41- int32_t scope_with_promoted_native;
42- int32_t walk_scope; /* NodeCallbackScope */
43- int32_t seeded_walk_scope; /* NodeCallbackScope */
44- int32_t truthy_value_exprs; /* NodeCallbackScope */
45- int32_t falsey_value_exprs; /* NodeCallbackScope */
35+ /* the per-instance memos to reset: every typed instance property with a
36+ * declared default — the twin builds a new scope, where each of them
37+ * starts at that default. A promoted property never has one (typed; the
38+ * untyped promoted $nodeCallback's implicit null is why untyped ones do
39+ * not count), and the constructor assigns only default-less $namespace */
40+ uint32_t memo_count;
41+ uint32_t *memo_offsets;
4642} pt_scope_offsets;
4743
4844static HashTable pt_scope_offsets_cache;
4945static bool pt_scope_offsets_cache_inited = false ;
5046
5147static void pt_scope_offsets_free (zval *zv)
5248{
53- efree (Z_PTR_P (zv));
49+ pt_scope_offsets *off = (pt_scope_offsets *) Z_PTR_P (zv);
50+ if (off->memo_offsets != NULL ) efree (off->memo_offsets );
51+ efree (off);
52+ }
53+
54+ /* collects the memo slots of ce (see pt_scope_offsets::memo_offsets) — every
55+ * declaring class of the chain, so a parent's private memos count too */
56+ static void pt_scope_offsets_collect_memos (zend_class_entry *ce, pt_scope_offsets *off)
57+ {
58+ off->memo_count = 0 ;
59+ off->memo_offsets = ce->default_properties_count > 0
60+ ? (uint32_t *) safe_emalloc ((size_t ) ce->default_properties_count , sizeof (uint32_t ), 0 )
61+ : NULL ;
62+ for (zend_class_entry *declaring = ce; declaring != NULL ; declaring = declaring->parent ) {
63+ for (auto entry : zv::TableRef (&declaring->properties_info )) {
64+ zend_property_info *info = (zend_property_info *) Z_PTR_P (entry.value ().raw ());
65+ if (info->ce != declaring || (info->flags & ZEND_ACC_STATIC ) != 0 || !ZEND_TYPE_IS_SET (info->type )) continue ;
66+ if (Z_TYPE (CE_DEFAULT_PROPERTIES_TABLE (ce)[OBJ_PROP_TO_NUM (info->offset )]) == IS_UNDEF ) continue ;
67+ bool seen = false ;
68+ for (uint32_t i = 0 ; i < off->memo_count ; i++) {
69+ if (off->memo_offsets [i] == info->offset ) {
70+ seen = true ;
71+ break ;
72+ }
73+ }
74+ if (!seen && off->memo_count < (uint32_t ) ce->default_properties_count ) off->memo_offsets [off->memo_count ++] = info->offset ;
75+ }
76+ }
5477}
5578
5679static pt_scope_offsets *pt_scope_offsets_for (zend_class_entry *ce)
@@ -73,16 +96,7 @@ static pt_scope_offsets *pt_scope_offsets_for(zend_class_entry *ce)
7396 off->in_function_calls_stack = pt_instance_prop_offset (ce, " inFunctionCallsStack" , sizeof (" inFunctionCallsStack" ) - 1 );
7497 off->in_first_level_statement = pt_instance_prop_offset (ce, " inFirstLevelStatement" , sizeof (" inFirstLevelStatement" ) - 1 );
7598 off->after_extract_call = pt_instance_prop_offset (ce, " afterExtractCall" , sizeof (" afterExtractCall" ) - 1 );
76- off->resolved_types = pt_instance_prop_offset (ce, " resolvedTypes" , sizeof (" resolvedTypes" ) - 1 );
77- off->truthy_scopes = pt_instance_prop_offset (ce, " truthyScopes" , sizeof (" truthyScopes" ) - 1 );
78- off->falsey_scopes = pt_instance_prop_offset (ce, " falseyScopes" , sizeof (" falseyScopes" ) - 1 );
79- off->node_callback_scope = pt_instance_prop_offset (ce, " nodeCallbackScope" , sizeof (" nodeCallbackScope" ) - 1 );
80- off->scope_out_of_first_level = pt_instance_prop_offset (ce, " scopeOutOfFirstLevelStatement" , sizeof (" scopeOutOfFirstLevelStatement" ) - 1 );
81- off->scope_with_promoted_native = pt_instance_prop_offset (ce, " scopeWithPromotedNativeTypes" , sizeof (" scopeWithPromotedNativeTypes" ) - 1 );
82- off->walk_scope = pt_instance_prop_offset (ce, " walkScope" , sizeof (" walkScope" ) - 1 );
83- off->seeded_walk_scope = pt_instance_prop_offset (ce, " seededWalkScope" , sizeof (" seededWalkScope" ) - 1 );
84- off->truthy_value_exprs = pt_instance_prop_offset (ce, " truthyValueExprs" , sizeof (" truthyValueExprs" ) - 1 );
85- off->falsey_value_exprs = pt_instance_prop_offset (ce, " falseyValueExprs" , sizeof (" falseyValueExprs" ) - 1 );
99+ pt_scope_offsets_collect_memos (ce, off);
86100
87101 zend_hash_add_ptr (&pt_scope_offsets_cache, ce->name , off);
88102 return off;
@@ -258,16 +272,14 @@ class ScopeOps
258272 cloneObj.propAtOffset ((uint32_t ) off->after_extract_call ).assign (zv::Val::boolean (afterExtractCall));
259273
260274 /* fresh-constructor defaults for per-instance memos */
261- resetToEmptyArray (cloneObj, off->resolved_types );
262- resetToEmptyArray (cloneObj, off->truthy_scopes );
263- resetToEmptyArray (cloneObj, off->falsey_scopes );
264- resetToNull (cloneObj, off->node_callback_scope );
265- resetToNull (cloneObj, off->scope_out_of_first_level );
266- resetToNull (cloneObj, off->scope_with_promoted_native );
267- resetToNull (cloneObj, off->walk_scope );
268- resetToNull (cloneObj, off->seeded_walk_scope );
269- resetToEmptyArray (cloneObj, off->truthy_value_exprs );
270- resetToEmptyArray (cloneObj, off->falsey_value_exprs );
275+ zval *defaults = CE_DEFAULT_PROPERTIES_TABLE (Z_OBJCE_P (scope));
276+ for (uint32_t i = 0 ; i < off->memo_count ; i++) {
277+ uint32_t offset = off->memo_offsets [i];
278+ /* the engine's own default-property copy (a persistent default is duplicated) */
279+ zval fresh;
280+ ZVAL_COPY_OR_DUP (&fresh, &defaults[OBJ_PROP_TO_NUM (offset)]);
281+ cloneObj.propAtOffset (offset).assign (zv::Val::adopt (fresh));
282+ }
271283
272284 zval out;
273285 ZVAL_OBJ (&out, clone);
@@ -1159,20 +1171,6 @@ class ScopeOps
11591171 obj.propAtOffset ((uint32_t ) offset).assign (zv::Arr::copyOfTable (value));
11601172 }
11611173
1162- /* $obj->prop = [] — a memo reset to the fresh-constructor default */
1163- static void resetToEmptyArray (zv::ObjRef obj, int32_t offset)
1164- {
1165- if (offset < 0 ) return ;
1166- obj.propAtOffset ((uint32_t ) offset).assign (zv::Arr::empty ());
1167- }
1168-
1169- /* $obj->prop = null — a memo reset to the fresh-constructor default */
1170- static void resetToNull (zv::ObjRef obj, int32_t offset)
1171- {
1172- if (offset < 0 ) return ;
1173- obj.propAtOffset ((uint32_t ) offset).assign (zv::Val::null ());
1174- }
1175-
11761174 /* $holder->expr for a checked ExpressionTypeHolder */
11771175 static zend_object *holderExpr (zv::Ref holder)
11781176 {
0 commit comments