P5.3: on_build(cb) build-callback registrar; callback takes BuildOptions

Per user design: on_build(build) is the build-callback registrar (a free fn),
generalizing set_post_link_callback — the callback is (opt: BuildOptions) ->
bool and the compiler invokes it post-codegen WITH the BuildOptions handle.

- VM: callCompilerFn 'on_build' arm + legacy handleOnBuild, both set
  post_link_callback_fn + a new BuildConfig.post_link_takes_options flag.
- comptime_vm: runEntry refactored to runEntryArgs(extra) (implicit ctx +
  explicit args); new public runBuildCallback(..., pass_options) passes the
  opaque BuildOptions handle (one word) after the ctx. The fat-config
  marshaling fear is moot — the handle is a single null-sentinel word.
- core.invokeByFuncId/invokeByName take pass_options (was an unused args
  slice); main.zig passes comp.getPostLinkTakesOptions().
- build.sx: on_build decl (set_post_link_callback kept for now).

Smoke test examples/1664-platform-on-build-callback (AOT): #run on_build(build)
with build :: (opt: BuildOptions) -> bool; the callback is invoked with the
handle arg (runEntryArgs param-count match) and runs the primitives.

Benign .ir churn (37 snapshots: type table +1 for the on_build fn type +
global renumber; behavior identical). 705/0 both gates.
This commit is contained in:
agra
2026-06-19 08:47:05 +03:00
parent d8affd45e8
commit 9cbee5e4bd
48 changed files with 35175 additions and 34932 deletions

View File

@@ -61,6 +61,7 @@ pub const bound_fns = [_]BoundFn{
// ── BuildOptions (migrated off `#compiler` onto `abi(.compiler)`) ─────────
.{ .sx_name = "build_options", .handler = handleBuildOptions },
.{ .sx_name = "set_post_link_callback", .handler = handleSetPostLinkCallback },
.{ .sx_name = "on_build", .handler = handleOnBuild },
// ── build-pipeline metadata queries (Phase 5.2) ──────────────────────────
// VM-only: the post-link callback that calls these always runs on the VM
// (`core.invokeByFuncId`), so `comptime_vm.callCompilerFn` services them and
@@ -353,3 +354,20 @@ fn handleSetPostLinkCallback(interp: *Interpreter, args: []const Value) InterpEr
}
return .void_val;
}
/// `on_build(cb)` — register the build callback (the Phase 5 form, a free fn; cb
/// is arg 0, and `cb: (opt: BuildOptions) -> bool` so the callback is invoked with
/// the `BuildOptions` handle). Sets `post_link_takes_options` to distinguish it
/// from the legacy `set_post_link_callback` (`() -> bool`).
fn handleOnBuild(interp: *Interpreter, args: []const Value) InterpError!Value {
if (args.len != 1) return error.TypeError;
const bc = interp.build_config orelse return error.CannotEvalComptime;
switch (args[0]) {
.func_ref => |id| {
bc.post_link_callback_fn = id;
bc.post_link_takes_options = true;
},
else => return error.TypeError,
}
return .void_val;
}