镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit d2b4c49

Browse files
committed
feat(vm): retain direct-eval closure environments (#785)
Record each running ordinary activation exact defining Environment on its Frame so the runtime interleaver can recover captured record boundaries without mutating published parent links. Trace and relocate the immutable edge from captured functions, suspended generators, and active interpreter roots, and clear it before activation reuse. Relocation witnesses cover all three root categories. Validation: zig fmt --check and zig ast-check pass for src/vm.zig and src/gc.zig. Test262 flips: 0 by construction because compiler admission and evaluation semantics remain unchanged. Advances #771 and follows the activation model completed in #776.
1 parent 4487bf1 commit d2b4c49

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎src/gc.zig‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,7 @@ fn traceCapturedFrame(raw: ?*anyopaque, v: anytype) void {
15641564
var frame: ?*vm.Frame = if (raw) |pointer| @ptrCast(@alignCast(pointer)) else null;
15651565
const lock_slots = if (comptime @hasDecl(@TypeOf(v.*), "concurrent")) v.concurrent() else false;
15661566
while (frame) |current| : (frame = current.parent) {
1567+
if (current.closure_environment) |environment| markManaged(v, environment);
15671568
if (current.direct_eval_environment.load(.acquire)) |environment| markManaged(v, environment);
15681569
if (current.direct_eval_parameter_environment.load(.acquire)) |environment| markManaged(v, environment);
15691570
traceFrameMappedParameters(current, v);
@@ -1578,6 +1579,7 @@ fn traceCapturedFrame(raw: ?*anyopaque, v: anytype) void {
15781579
fn relocateCapturedFrame(raw: ?*anyopaque, v: anytype) void {
15791580
var frame: ?*vm.Frame = if (raw) |pointer| @ptrCast(@alignCast(pointer)) else null;
15801581
while (frame) |current| : (frame = current.parent) {
1582+
gc_relocation.rewriteOptionalSlot(v, Environment, &current.closure_environment);
15811583
gc_relocation.rewriteAtomicOptionalSlot(v, Environment, &current.direct_eval_environment);
15821584
gc_relocation.rewriteAtomicOptionalSlot(v, Environment, &current.direct_eval_parameter_environment);
15831585
relocateFrameMappedParameters(current, v);
@@ -1684,7 +1686,11 @@ test "Function marking and relocation cover every managed field" {
16841686
var parent_frame_slots = [_]Value{Value.obj(&old_objects[13])};
16851687
var parent_frame = vm.Frame{ .slots = &parent_frame_slots, .parent = null };
16861688
var captured_frame_slots = [_]Value{Value.obj(&old_objects[14])};
1687-
var captured_frame = vm.Frame{ .slots = &captured_frame_slots, .parent = &parent_frame };
1689+
var captured_frame = vm.Frame{
1690+
.slots = &captured_frame_slots,
1691+
.parent = &parent_frame,
1692+
.closure_environment = &old_environment,
1693+
};
16881694
var function = interp.Function{
16891695
.params = &.{},
16901696
.body = &body,
@@ -1766,6 +1772,7 @@ test "Function marking and relocation cover every managed field" {
17661772
try std.testing.expectEqual(&new_objects[12], async_constants[0].asObj());
17671773
try std.testing.expectEqual(&new_objects[13], parent_frame_slots[0].asObj());
17681774
try std.testing.expectEqual(&new_objects[14], captured_frame_slots[0].asObj());
1775+
try std.testing.expectEqual(&new_environment, captured_frame.closure_environment.?);
17691776
try std.testing.expect(Binding.traceOldOnMinor(.function));
17701777
}
17711778

@@ -2001,6 +2008,7 @@ pub fn traceGenerator(g: *vm.Generator, v: anytype) void {
20012008
if (handler.environment) |environment| markManaged(v, environment);
20022009
var frame = g.exec.frame;
20032010
while (frame) |current| : (frame = current.parent) {
2011+
if (current.closure_environment) |environment| markManaged(v, environment);
20042012
if (current.direct_eval_environment.load(.acquire)) |environment| markManaged(v, environment);
20052013
if (current.direct_eval_parameter_environment.load(.acquire)) |environment| markManaged(v, environment);
20062014
for (current.slots) |slot| markValue(v, slot);
@@ -2033,6 +2041,7 @@ pub fn relocateGenerator(g: *vm.Generator, v: anytype) void {
20332041
gc_relocation.rewriteOptionalSlot(v, interp.Environment, &handler.environment);
20342042
var frame = g.exec.frame;
20352043
while (frame) |current| : (frame = current.parent) {
2044+
gc_relocation.rewriteOptionalSlot(v, Environment, &current.closure_environment);
20362045
gc_relocation.rewriteAtomicOptionalSlot(v, Environment, &current.direct_eval_environment);
20372046
gc_relocation.rewriteAtomicOptionalSlot(v, Environment, &current.direct_eval_parameter_environment);
20382047
for (current.slots) |*slot| gc_relocation.rewriteValueSlot(v, slot);
@@ -2113,7 +2122,11 @@ test "Generator and IteratorHelper marking and relocation cover every managed sl
21132122
var parent_frame_slots = [_]Value{Value.obj(&old_objects[3])};
21142123
var child_frame_slots = [_]Value{Value.obj(&old_objects[4])};
21152124
var parent_frame = vm.Frame{ .slots = &parent_frame_slots, .parent = null };
2116-
var child_frame = vm.Frame{ .slots = &child_frame_slots, .parent = &parent_frame };
2125+
var child_frame = vm.Frame{
2126+
.slots = &child_frame_slots,
2127+
.parent = &parent_frame,
2128+
.closure_environment = &old_environment,
2129+
};
21172130
var handlers = [_]vm.Handler{.{
21182131
.catch_pc = 1,
21192132
.stack_depth = 0,
@@ -2209,6 +2222,7 @@ test "Generator and IteratorHelper marking and relocation cover every managed sl
22092222
try std.testing.expectEqual(&new_objects[2], generator.exec.acc.asObj());
22102223
try std.testing.expectEqual(&new_objects[3], parent_frame_slots[0].asObj());
22112224
try std.testing.expectEqual(&new_objects[4], child_frame_slots[0].asObj());
2225+
try std.testing.expectEqual(&new_environment, child_frame.closure_environment.?);
22122226
try std.testing.expectEqual(&new_objects[5], generator.this_value.asObj());
22132227
try std.testing.expectEqual(&new_objects[6], generator.home_object.?);
22142228
try std.testing.expectEqual(&new_objects[7], generator.super_ctor.?);
@@ -2553,6 +2567,7 @@ pub fn traceInterpreterRoots(machine: *interp.Interpreter, v: anytype) void {
25532567
const lock_slots = v.concurrent();
25542568
var fr: ?*vm.Frame = exec.frame;
25552569
while (fr) |f| : (fr = f.parent) {
2570+
if (f.closure_environment) |environment| markManaged(v, environment);
25562571
if (f.direct_eval_environment.load(.acquire)) |environment| markManaged(v, environment);
25572572
if (f.direct_eval_parameter_environment.load(.acquire)) |environment| markManaged(v, environment);
25582573
traceFrameMappedParameters(f, v);
@@ -2674,6 +2689,7 @@ pub fn relocateInterpreterRoots(machine: *interp.Interpreter, v: anytype) void {
26742689
gc_relocation.rewriteOptionalSlot(v, Object, &exec.saved_super_ctor);
26752690
var frame = exec.frame;
26762691
while (frame) |current| : (frame = current.parent) {
2692+
gc_relocation.rewriteOptionalSlot(v, Environment, &current.closure_environment);
26772693
gc_relocation.rewriteAtomicOptionalSlot(v, Environment, &current.direct_eval_environment);
26782694
gc_relocation.rewriteAtomicOptionalSlot(v, Environment, &current.direct_eval_parameter_environment);
26792695
relocateFrameMappedParameters(current, v);
@@ -2790,7 +2806,11 @@ test "realm root relocation rewrites active interpreter containers" {
27902806
var operand_stack = [_]Value{ Value.obj(&old_objects[16]), Value.obj(&old_objects[17]) };
27912807
var pending_rest_arguments = [_]Value{Value.obj(&old_objects[27])};
27922808
var frame_slots = [_]Value{Value.obj(&old_objects[19])};
2793-
var frame = vm.Frame{ .slots = &frame_slots, .parent = null };
2809+
var frame = vm.Frame{
2810+
.slots = &frame_slots,
2811+
.parent = null,
2812+
.closure_environment = &old_environment,
2813+
};
27942814
frame.direct_eval_environment.store(&old_environment, .release);
27952815
frame.direct_eval_parameter_environment.store(&old_environment, .release);
27962816
var execution = vm.Exec{
@@ -2899,6 +2919,7 @@ test "realm root relocation rewrites active interpreter containers" {
28992919
try std.testing.expectEqual(&new_objects[18], execution.acc.asObj());
29002920
try std.testing.expectEqual(&new_objects[27], pending_rest_arguments[0].asObj());
29012921
try std.testing.expectEqual(&new_objects[19], frame_slots[0].asObj());
2922+
try std.testing.expectEqual(&new_environment, frame.closure_environment.?);
29022923
try std.testing.expectEqual(&new_environment, frame.direct_eval_environment.load(.acquire).?);
29032924
try std.testing.expectEqual(&new_environment, frame.direct_eval_parameter_environment.load(.acquire).?);
29042925
try std.testing.expectEqual(&new_objects[25], execution.saved_home_object.?);

‎src/vm.zig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ fn bindThisForCall(vm: *Interpreter, func: *Function, this_val: Value) EvalError
8787
pub const Frame = struct {
8888
slots: []Value,
8989
parent: ?*Frame,
90+
/// Exact lexical Environment captured by the function whose activation
91+
/// owns this frame. Frame slots are deliberately absent from that chain;
92+
/// direct eval uses this immutable edge to recover the runtime Environment
93+
/// Records that must be interleaved before the defining parent frame.
94+
/// Null is permitted only for synthetic/test frames and inactive recycled
95+
/// activations. A running ordinary activation always publishes it before
96+
/// the frame becomes a GC root or can escape through a closure.
97+
closure_environment: ?*Environment = null,
9098
/// Sloppy simple parameters share atomic cells owned by this arguments
9199
/// object. The frame retains only an object edge plus immutable slot layout,
92100
/// never a raw pointer from an escaped object back into recyclable storage.
@@ -10322,6 +10330,7 @@ fn releaseActivation(vm: *Interpreter, act: *Activation) void {
1032210330
@memset(act.exec.binding_references, .empty);
1032310331
act.frame.mapped_arguments = null;
1032410332
act.frame.mapped_parameter_indices = &.{};
10333+
act.frame.closure_environment = null;
1032510334
act.frame.direct_eval_environment.store(null, .release);
1032610335
act.frame.direct_eval_parameter_environment.store(null, .release);
1032710336
act.next_free = if (vm.vm_activation_free) |raw| @ptrCast(@alignCast(raw)) else null;
@@ -10338,6 +10347,7 @@ fn acquireActivation(vm: *Interpreter, local_count: usize) EvalError!*Activation
1033810347
act.frame.parent = null;
1033910348
act.frame.mapped_arguments = null;
1034010349
act.frame.mapped_parameter_indices = &.{};
10350+
act.frame.closure_environment = null;
1034110351
act.frame.direct_eval_environment.store(null, .release);
1034210352
act.frame.direct_eval_parameter_environment.store(null, .release);
1034310353
act.frame.escaped.store(false, .monotonic);
@@ -10515,6 +10525,7 @@ fn buildActivation(vm: *Interpreter, func: *Function, fchunk: *Chunk, args: []co
1051510525
frame.* = .{
1051610526
.slots = slots,
1051710527
.parent = if (func.frame) |fp| @ptrCast(@alignCast(fp)) else null,
10528+
.closure_environment = func.closure,
1051810529
};
1051910530
if (!func.is_arrow) vm.current_private_map = func.private_map; // a direct eval here resolves the class's private names
1052010531
vm.strict = func.is_strict;

0 commit comments

Comments
 (0)