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

Commit 4d9bd64

Browse files
committed
feat(compiler): classify all bytecode entry rejections
Extend stable admission results to program, generator, and async-function compilation. Preserve the existing error-union APIs while making every entry rejection distinguish invalid shape, explicit unsupported forms, and residual lowering gaps. Verified all 110 compiler-root tests. Flips 0 test262 cases.
1 parent ab66016 commit 4d9bd64

1 file changed

Lines changed: 170 additions & 3 deletions

File tree

‎src/compiler.zig‎

Lines changed: 170 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,15 +596,44 @@ pub const Compiler = struct {
596596
/// Compile a whole program into a fresh chunk. The chunk ends with `halt`;
597597
/// the VM returns its completion accumulator. Program scope is null, so all
598598
/// top-level bindings are globals.
599+
pub const ProgramRejection = enum {
600+
invalid_root,
601+
unsupported_lowering,
602+
};
603+
604+
pub const ProgramAdmission = union(enum) {
605+
compiled: *Chunk,
606+
rejected: ProgramRejection,
607+
};
608+
609+
pub fn admitProgram(arena: std.mem.Allocator, program: *Node) error{OutOfMemory}!ProgramAdmission {
610+
var rejection: ?ProgramRejection = null;
611+
const chunk = compileProgramInner(arena, program, &rejection) catch |err| switch (err) {
612+
error.Unsupported => return .{ .rejected = rejection orelse .unsupported_lowering },
613+
error.OutOfMemory => return error.OutOfMemory,
614+
};
615+
return .{ .compiled = chunk };
616+
}
617+
599618
pub fn compileProgram(arena: std.mem.Allocator, program: *Node) CompileError!*Chunk {
619+
return switch (try admitProgram(arena, program)) {
620+
.compiled => |chunk| chunk,
621+
.rejected => error.Unsupported,
622+
};
623+
}
624+
625+
fn compileProgramInner(arena: std.mem.Allocator, program: *Node, rejection: *?ProgramRejection) CompileError!*Chunk {
600626
const chunk = try arena.create(Chunk);
601627
chunk.* = Chunk.init(arena);
602628
// Keep latent source-node checkpoints in every chunk. With no debugger
603629
// hook the VM performs no checkpoint work; retaining the metadata lets a
604630
// later attachment inspect already-compiled functions without rebuilding
605631
// their frame/upvalue layout.
606632
var c = Compiler{ .arena = arena, .chunk = chunk, .mode = .program, .debug_checkpoints = true };
607-
if (program.* != .program) return error.Unsupported;
633+
if (program.* != .program) {
634+
rejection.* = .invalid_root;
635+
return error.Unsupported;
636+
}
608637
try c.compileStmtList(program.program);
609638
_ = try chunk.emit(.halt, 0);
610639
try chunk.finalize();
@@ -620,12 +649,41 @@ pub const Compiler = struct {
620649
/// operand stack. Returns `error.Unsupported` for bodies (or parameter forms)
621650
/// outside the VM's lowered subset, so the generator is reported unsupported
622651
/// rather than run incorrectly.
652+
pub const GeneratorRejection = enum {
653+
expression_body,
654+
unsupported_lowering,
655+
};
656+
657+
pub const GeneratorAdmission = union(enum) {
658+
compiled: *Chunk,
659+
rejected: GeneratorRejection,
660+
};
661+
662+
pub fn admitGenerator(arena: std.mem.Allocator, fnode: *const ast.FunctionNode, debug_checkpoints: bool) error{OutOfMemory}!GeneratorAdmission {
663+
var rejection: ?GeneratorRejection = null;
664+
const chunk = compileGeneratorInner(arena, fnode, debug_checkpoints, &rejection) catch |err| switch (err) {
665+
error.Unsupported => return .{ .rejected = rejection orelse .unsupported_lowering },
666+
error.OutOfMemory => return error.OutOfMemory,
667+
};
668+
return .{ .compiled = chunk };
669+
}
670+
623671
pub fn compileGenerator(arena: std.mem.Allocator, fnode: *const ast.FunctionNode, debug_checkpoints: bool) CompileError!*Chunk {
672+
return switch (try admitGenerator(arena, fnode, debug_checkpoints)) {
673+
.compiled => |chunk| chunk,
674+
.rejected => error.Unsupported,
675+
};
676+
}
677+
678+
fn compileGeneratorInner(arena: std.mem.Allocator, fnode: *const ast.FunctionNode, debug_checkpoints: bool, rejection: *?GeneratorRejection) CompileError!*Chunk {
624679
// Parameters (including default/rest/destructuring) are bound at runtime
625680
// by `makeGenerator` into the generator's environment — env-mode name
626681
// resolution means the body's references resolve there — so the param
627682
// shape never blocks compilation; only the body must lower.
628-
if (fnode.is_expr_body) return error.Unsupported; // generators always have a block body
683+
if (fnode.is_expr_body) {
684+
rejection.* = .expression_body;
685+
return error.Unsupported; // generators always have a block body
686+
}
629687
const chunk = try arena.create(Chunk);
630688
chunk.* = Chunk.init(arena);
631689
// An async generator body may also `await` (in_async enables await_op).
@@ -640,8 +698,37 @@ pub const Compiler = struct {
640698
/// like a generator). `await` lowers to a suspend (`gen_yield`); the async
641699
/// driver promisifies the suspended value, resumes on settlement, and
642700
/// settles the function's result promise on completion.
701+
pub const AsyncRejection = enum {
702+
async_generator,
703+
unsupported_lowering,
704+
};
705+
706+
pub const AsyncAdmission = union(enum) {
707+
compiled: *Chunk,
708+
rejected: AsyncRejection,
709+
};
710+
711+
pub fn admitAsync(arena: std.mem.Allocator, fnode: *const ast.FunctionNode, debug_checkpoints: bool) error{OutOfMemory}!AsyncAdmission {
712+
var rejection: ?AsyncRejection = null;
713+
const chunk = compileAsyncInner(arena, fnode, debug_checkpoints, &rejection) catch |err| switch (err) {
714+
error.Unsupported => return .{ .rejected = rejection orelse .unsupported_lowering },
715+
error.OutOfMemory => return error.OutOfMemory,
716+
};
717+
return .{ .compiled = chunk };
718+
}
719+
643720
pub fn compileAsync(arena: std.mem.Allocator, fnode: *const ast.FunctionNode, debug_checkpoints: bool) CompileError!*Chunk {
644-
if (fnode.is_generator) return error.Unsupported; // async generators not lowered yet
721+
return switch (try admitAsync(arena, fnode, debug_checkpoints)) {
722+
.compiled => |chunk| chunk,
723+
.rejected => error.Unsupported,
724+
};
725+
}
726+
727+
fn compileAsyncInner(arena: std.mem.Allocator, fnode: *const ast.FunctionNode, debug_checkpoints: bool, rejection: *?AsyncRejection) CompileError!*Chunk {
728+
if (fnode.is_generator) {
729+
rejection.* = .async_generator;
730+
return error.Unsupported; // async generators not lowered yet
731+
}
645732
const chunk = try arena.create(Chunk);
646733
chunk.* = Chunk.init(arena);
647734
var c = Compiler{ .arena = arena, .chunk = chunk, .mode = .function, .scope = null, .in_async = true, .debug_checkpoints = debug_checkpoints };
@@ -2947,3 +3034,83 @@ test "compiler reports stable plain-function admission reasons" {
29473034
.rejected => return error.TestUnexpectedResult,
29483035
}
29493036
}
3037+
3038+
test "compiler reports stable program admission reasons" {
3039+
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
3040+
defer arena.deinit();
3041+
3042+
var invalid_parser = try @import("parser.zig").Parser.init(arena.allocator(), "1;");
3043+
const invalid_program = try invalid_parser.parseProgram();
3044+
switch (try Compiler.admitProgram(arena.allocator(), invalid_program.program[0])) {
3045+
.compiled => return error.TestUnexpectedResult,
3046+
.rejected => |reason| try std.testing.expectEqual(Compiler.ProgramRejection.invalid_root, reason),
3047+
}
3048+
3049+
var unsupported_parser = try @import("parser.zig").Parser.init(arena.allocator(), "null ?? 1;");
3050+
const unsupported_program = try unsupported_parser.parseProgram();
3051+
switch (try Compiler.admitProgram(arena.allocator(), unsupported_program)) {
3052+
.compiled => return error.TestUnexpectedResult,
3053+
.rejected => |reason| try std.testing.expectEqual(Compiler.ProgramRejection.unsupported_lowering, reason),
3054+
}
3055+
3056+
var compiled_parser = try @import("parser.zig").Parser.init(arena.allocator(), "1 + 2;");
3057+
const compiled_program = try compiled_parser.parseProgram();
3058+
switch (try Compiler.admitProgram(arena.allocator(), compiled_program)) {
3059+
.compiled => |chunk| try std.testing.expect(chunk.code.items.len != 0),
3060+
.rejected => return error.TestUnexpectedResult,
3061+
}
3062+
}
3063+
3064+
test "compiler reports stable generator admission reasons" {
3065+
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
3066+
defer arena.deinit();
3067+
3068+
var invalid_parser = try @import("parser.zig").Parser.init(arena.allocator(), "function* invalid(){}");
3069+
const invalid_program = try invalid_parser.parseProgram();
3070+
const invalid = invalid_program.program[0].func_decl;
3071+
invalid.is_expr_body = true;
3072+
switch (try Compiler.admitGenerator(arena.allocator(), invalid, true)) {
3073+
.compiled => return error.TestUnexpectedResult,
3074+
.rejected => |reason| try std.testing.expectEqual(Compiler.GeneratorRejection.expression_body, reason),
3075+
}
3076+
3077+
var unsupported_parser = try @import("parser.zig").Parser.init(arena.allocator(), "function* unsupported(){ yield (null ?? 1); }");
3078+
const unsupported_program = try unsupported_parser.parseProgram();
3079+
switch (try Compiler.admitGenerator(arena.allocator(), unsupported_program.program[0].func_decl, true)) {
3080+
.compiled => return error.TestUnexpectedResult,
3081+
.rejected => |reason| try std.testing.expectEqual(Compiler.GeneratorRejection.unsupported_lowering, reason),
3082+
}
3083+
3084+
var compiled_parser = try @import("parser.zig").Parser.init(arena.allocator(), "function* compiled(){ yield 1; }");
3085+
const compiled_program = try compiled_parser.parseProgram();
3086+
switch (try Compiler.admitGenerator(arena.allocator(), compiled_program.program[0].func_decl, true)) {
3087+
.compiled => |chunk| try std.testing.expect(chunk.code.items.len != 0),
3088+
.rejected => return error.TestUnexpectedResult,
3089+
}
3090+
}
3091+
3092+
test "compiler reports stable async admission reasons" {
3093+
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
3094+
defer arena.deinit();
3095+
3096+
var generator_parser = try @import("parser.zig").Parser.init(arena.allocator(), "async function* invalid(){}");
3097+
const generator_program = try generator_parser.parseProgram();
3098+
switch (try Compiler.admitAsync(arena.allocator(), generator_program.program[0].func_decl, true)) {
3099+
.compiled => return error.TestUnexpectedResult,
3100+
.rejected => |reason| try std.testing.expectEqual(Compiler.AsyncRejection.async_generator, reason),
3101+
}
3102+
3103+
var unsupported_parser = try @import("parser.zig").Parser.init(arena.allocator(), "async function unsupported(){ return null ?? 1; }");
3104+
const unsupported_program = try unsupported_parser.parseProgram();
3105+
switch (try Compiler.admitAsync(arena.allocator(), unsupported_program.program[0].func_decl, true)) {
3106+
.compiled => return error.TestUnexpectedResult,
3107+
.rejected => |reason| try std.testing.expectEqual(Compiler.AsyncRejection.unsupported_lowering, reason),
3108+
}
3109+
3110+
var compiled_parser = try @import("parser.zig").Parser.init(arena.allocator(), "async function compiled(){ return await 1; }");
3111+
const compiled_program = try compiled_parser.parseProgram();
3112+
switch (try Compiler.admitAsync(arena.allocator(), compiled_program.program[0].func_decl, true)) {
3113+
.compiled => |chunk| try std.testing.expect(chunk.code.items.len != 0),
3114+
.rejected => return error.TestUnexpectedResult,
3115+
}
3116+
}

0 commit comments

Comments
 (0)