@@ -178,6 +178,11 @@ const FnScope = struct {
178178 /// name resolution must stop at this boundary before falling back to an
179179 /// enclosing non-deletable slot.
180180 parent_environment_depth : u32 = 0 ,
181+ /// Complete runtime Environment segment captured between this frame and
182+ /// its defining parent frame for direct eval. Unlike name-resolution's
183+ /// `parent_environment_depth`, this includes a named function expression's
184+ /// immutable self-name record, which also sits outside the frame slots.
185+ parent_direct_eval_environment_depth : u32 = 0 ,
181186 /// Object Environment Records between this frame and its parent frame at
182187 /// closure creation. Kept separate from declarative environments so
183188 /// ordinary no-`with` slot accesses retain their direct bytecodes.
@@ -343,24 +348,20 @@ fn directEvalBindings(
343348/// The current declaration target is retained even when empty; functions with
344349/// parameter expressions additionally retain their outer parameter record.
345350/// Empty lexical scopes are semantically inert and omitted. Runtime Environment
346- /// Records must be interleaved by a later admission-aware layer rather than
347- /// guessed here .
351+ /// Records are retained only as exact frame-boundary counts; their live binding
352+ /// identity must be recovered from Frame closure edges during materialization .
348353fn buildDirectEvalPlan (
349354 arena : std.mem.Allocator ,
350355 scope : * const FnScope ,
351356 current_target : bc.DirectEvalScopeKind ,
357+ current_environment_depth : u32 ,
352358) CompileError ! bc.DirectEvalPlan {
353359 if (current_target == .parameter and scope .parameter_names == null ) return error .Unsupported ;
354360 var frame_count : usize = 0 ;
355361 var scope_count : usize = 0 ;
356362 var cursor : ? * const FnScope = scope ;
357363 var cursor_phase : FunctionBindingPhase = if (current_target == .parameter ) .parameters else .body ;
358364 while (cursor ) | frame_scope | : (cursor = frame_scope .parent ) {
359- // A runtime Environment Record between two defining frames must appear
360- // at its exact lexical position. Do not flatten or reorder it into the
361- // static plan; admission stays causal until interleaving is represented.
362- if (frame_scope .parent != null and frame_scope .parent_environment_depth != 0 )
363- return error .Unsupported ;
364365 frame_count += 1 ;
365366 if (cursor_phase == .parameters ) {
366367 if (frame_scope .parameter_names == null ) return error .Unsupported ;
@@ -375,13 +376,25 @@ fn buildDirectEvalPlan(
375376 }
376377
377378 const scopes = try arena .alloc (bc .DirectEvalScope , scope_count );
379+ const frame_boundaries = try arena .alloc (bc .DirectEvalFrameBoundary , frame_count - 1 );
378380 const FrameScope = struct { scope : * const FnScope , phase : FunctionBindingPhase };
379381 const frames = try arena .alloc (FrameScope , frame_count );
380382 cursor = scope ;
381383 cursor_phase = if (current_target == .parameter ) .parameters else .body ;
382384 var current_depth : usize = 0 ;
383385 while (cursor ) | frame_scope | : (cursor = frame_scope .parent ) {
384386 frames [frame_count - current_depth - 1 ] = .{ .scope = frame_scope , .phase = cursor_phase };
387+ if (frame_scope .parent != null ) {
388+ const active_depth = if (current_depth == 0 ) current_environment_depth else 0 ;
389+ frame_boundaries [current_depth ] = .{
390+ .child_frame_depth = @intCast (current_depth ),
391+ .environment_depth = std .math .add (
392+ u32 ,
393+ frame_scope .parent_direct_eval_environment_depth ,
394+ active_depth ,
395+ ) catch return error .Unsupported ,
396+ };
397+ }
385398 current_depth += 1 ;
386399 cursor_phase = frame_scope .parent_binding_phase ;
387400 }
@@ -419,7 +432,7 @@ fn buildDirectEvalPlan(
419432 }
420433 }
421434 }
422- return .{ .scopes = scopes };
435+ return .{ .scopes = scopes , . frame_boundaries = frame_boundaries };
423436}
424437
425438/// Every ordinary function that can observe its own arguments object receives
@@ -5853,11 +5866,19 @@ pub const Compiler = struct {
58535866 if (self .environment_depth != 0 ) return error .Unsupported ;
58545867 if (self .function_binding_phase == .parameters )
58555868 if (self .chunk .parameter_direct_eval_plan ) | plan_index | return plan_index ;
5856- const plan_index = try self . chunk . addDirectEvalPlan ( try buildDirectEvalPlan (
5869+ const plan = try buildDirectEvalPlan (
58575870 self .arena ,
58585871 scope ,
58595872 if (self .function_binding_phase == .parameters ) .parameter else .variable ,
5860- ));
5873+ self .environment_depth ,
5874+ );
5875+ // Boundary metadata is frozen now, but materialization must not admit a
5876+ // captured gap until it can interleave the exact live records. Retain
5877+ // the causal barrier rather than silently flattening them around frame
5878+ // activation views.
5879+ for (plan .frame_boundaries ) | boundary |
5880+ if (boundary .environment_depth != 0 ) return error .Unsupported ;
5881+ const plan_index = try self .chunk .addDirectEvalPlan (plan );
58615882 if (self .function_binding_phase == .parameters )
58625883 self .chunk .parameter_direct_eval_plan = plan_index ;
58635884 return plan_index ;
@@ -5882,13 +5903,20 @@ pub const Compiler = struct {
58825903 // function-scoped declaration in the body (not descending into nested
58835904 // functions). The scope chains to the enclosing function for upvalues.
58845905 const scope = try self .arena .create (FnScope );
5906+ const self_environment_depth : u32 = @intFromBool (named_expr and fnode .has_name_binding and ! fnode .is_arrow );
5907+ const parent_direct_eval_environment_depth = std .math .add (
5908+ u32 ,
5909+ self .environment_depth ,
5910+ self_environment_depth ,
5911+ ) catch return error .Unsupported ;
58855912 const tdz_checks = ! fnode .is_generator and try functionNeedsTdzChecks (self .arena , self .hash_state , fnode );
58865913 scope .* = .{
58875914 .parent = self .scope ,
58885915 .parent_binding_phase = self .function_binding_phase ,
58895916 .hash_state = self .hash_state ,
58905917 .names = .{ .state = self .hash_state },
58915918 .parent_environment_depth = self .environment_depth ,
5919+ .parent_direct_eval_environment_depth = parent_direct_eval_environment_depth ,
58925920 .parent_with_depth = self .with_depth ,
58935921 .tdz_checks = tdz_checks ,
58945922 };
@@ -6214,7 +6242,7 @@ test "compiler direct eval plan retains ordered live binding identity" {
62146242 try scope .pushLexicalScope (allocator );
62156243 const inner_slot = try scope .addLexicalChecked (allocator , "value" , true );
62166244
6217- const plan = try buildDirectEvalPlan (allocator , & scope , .variable );
6245+ const plan = try buildDirectEvalPlan (allocator , & scope , .variable , 0 );
62186246 try std .testing .expectEqual (@as (usize , 3 ), plan .scopes .len );
62196247
62206248 const function_scope = plan .scopes [0 ];
@@ -6249,7 +6277,7 @@ test "compiler direct eval plan retains ordered live binding identity" {
62496277 try std .testing .expect (inner .bindings [0 ].immutable );
62506278}
62516279
6252- test "compiler direct eval plan preserves defining-frame depth and rejects environment gaps " {
6280+ test "compiler direct eval plan preserves defining-frame and runtime- environment depths " {
62536281 var arena = std .heap .ArenaAllocator .init (std .testing .allocator );
62546282 defer arena .deinit ();
62556283 const allocator = arena .allocator ();
@@ -6273,7 +6301,7 @@ test "compiler direct eval plan preserves defining-frame depth and rejects envir
62736301 };
62746302 _ = try inner .addLocal (allocator , "innerVariable" , false , false );
62756303
6276- const plan = try buildDirectEvalPlan (allocator , & inner , .variable );
6304+ const plan = try buildDirectEvalPlan (allocator , & inner , .variable , 0 );
62776305 try std .testing .expectEqual (@as (usize , 3 ), plan .scopes .len );
62786306 try std .testing .expectEqualStrings ("outerVariable" , plan .scopes [0 ].bindings [0 ].name );
62796307 try std .testing .expectEqual (bc .DirectEvalScopeKind .variable , plan .scopes [0 ].kind );
@@ -6285,9 +6313,16 @@ test "compiler direct eval plan preserves defining-frame depth and rejects envir
62856313 try std .testing .expectEqual (bc .DirectEvalScopeKind .variable , plan .scopes [2 ].kind );
62866314 try std .testing .expect (plan .scopes [2 ].declaration_target );
62876315 try std .testing .expectEqual (@as (u32 , 0 ), plan .scopes [2 ].frame_depth );
6316+ try std .testing .expectEqual (@as (usize , 1 ), plan .frame_boundaries .len );
6317+ try std .testing .expectEqual (@as (u32 , 0 ), plan .frame_boundaries [0 ].child_frame_depth );
6318+ try std .testing .expectEqual (@as (u32 , 0 ), plan .frame_boundaries [0 ].environment_depth );
62886319
62896320 inner .parent_environment_depth = 1 ;
6290- try std .testing .expectError (error .Unsupported , buildDirectEvalPlan (allocator , & inner , .variable ));
6321+ inner .parent_direct_eval_environment_depth = 1 ;
6322+ const interleaved = try buildDirectEvalPlan (allocator , & inner , .variable , 2 );
6323+ try std .testing .expectEqual (@as (usize , 1 ), interleaved .frame_boundaries .len );
6324+ try std .testing .expectEqual (@as (u32 , 0 ), interleaved .frame_boundaries [0 ].child_frame_depth );
6325+ try std .testing .expectEqual (@as (u32 , 3 ), interleaved .frame_boundaries [0 ].environment_depth );
62916326}
62926327
62936328test "compiler threads activation plans through fixed spread and tail eval calls" {
0 commit comments