mem: Step 3 — thread __sx_ctx through closure/fn-pointer/method dispatch

Continues the implicit-Context refactor. Bare-fn trampolines, lambda
trampolines, and protocol thunks now carry __sx_ctx at slot 0; call
sites for closures, fn-pointer variables, and method dispatch prepend
the caller's current ctx.

- emit_llvm.zig:1687 call_indirect treats `fp_ctx_slots` leading args
  as opaque ptr (the implicit ctx) when the fn-pointer is default-conv
  under has_implicit_ctx.
- lower.zig:fnPtrTypeWantsCtx predicate gates the prepend at both
  scope-local and global fn-pointer call sites.
- lower.zig:fixupMethodReceiver skips __sx_ctx when probing the
  receiver param's type.
- lower.zig:lowerLambda builds closure type from user-visible params
  only (skip ctx + env).
- lower.zig:closure(bare_fn) builds closure type from user-visible
  params only.
- module.zig: Module.has_implicit_ctx flag mirrors Lowering's switch
  so emit_llvm can read it without a back-pointer.

Tests updated:
- 5 ObjC-block/runtime tests get `callconv(.c)` on fn-ptr types
  cast from `objc_msgSend` / Block.invoke (C-side calls into sx).
- ffi-06-callback gets `callconv(.c)` on double_it/add_with_ctx —
  the registered C-side callbacks.
- 08-types snapshot regen (undefined-init drift from layout shift).
- 11 JNI/ObjC .ir snapshots regen for the ctx-prepended thunk
  signatures.

151/152 example tests pass. Remaining failure (05-run) is the
comptime/interp path that requires Step 7 (callWithDefaultContext).
This commit is contained in:
agra
2026-05-25 08:41:50 +03:00
parent 29784c22a8
commit 92c6b47f12
22 changed files with 1601 additions and 1196 deletions

View File

@@ -18,10 +18,10 @@ main :: () -> s32 {
sel_with_utf8 := sel_registerName("stringWithUTF8String:".ptr);
sel_utf8 := sel_registerName("UTF8String".ptr);
msg_3 : (*void, *void, [*]u8) -> *void = xx objc_msgSend;
msg_3 : (*void, *void, [*]u8) -> *void callconv(.c) = xx objc_msgSend;
ns_str := msg_3(ns_class, sel_with_utf8, "hi".ptr);
msg_2 : (*void, *void) -> [*]u8 = xx objc_msgSend;
msg_2 : (*void, *void) -> [*]u8 callconv(.c) = xx objc_msgSend;
back := msg_2(ns_str, sel_utf8);
return xx (back[0] + back[1]); // 'h' + 'i' = 104 + 105 = 209

View File

@@ -34,7 +34,7 @@ main :: () -> s32 {
if obj == xx 0 { return 2; }
// [obj hello]
msg : (*void, *void) -> void = xx objc_msgSend;
msg : (*void, *void) -> void callconv(.c) = xx objc_msgSend;
msg(obj, sel_hello);
return g_marker; // 42 if hello_imp ran

View File

@@ -11,7 +11,7 @@
main :: () -> s32 {
cl := () => { print("noop block ran\n"); };
b : Block = xx cl;
invoke_fn : (*Block) -> void = xx b.invoke;
invoke_fn : (*Block) -> void callconv(.c) = xx b.invoke;
invoke_fn(@b);
0;
}

View File

@@ -11,7 +11,7 @@ main :: () -> s32 {
y : s64 = 100;
cl := () => { print("x + y = {}\n", x + y); };
b : Block = xx cl;
invoke_fn : (*Block) -> void = xx b.invoke;
invoke_fn : (*Block) -> void callconv(.c) = xx b.invoke;
invoke_fn(@b);
0;
}

View File

@@ -6,7 +6,7 @@
#import "modules/std/objc_block.sx";
invoke_once :: (b: *Block) {
invoke_fn : (*Block) -> void = xx b.invoke;
invoke_fn : (*Block) -> void callconv(.c) = xx b.invoke;
invoke_fn(b);
}

View File

@@ -22,13 +22,13 @@ ffi_apply_callback2 :: (cb: (*void, s32) -> s32, ctx: *void, v: s32) -> s32 #for
g_callback_hits : s32 = 0;
g_callback_sum : s32 = 0;
double_it :: (x: s32) -> s32 {
double_it :: (x: s32) -> s32 callconv(.c) {
g_callback_hits += 1;
g_callback_sum += x;
x * 2;
}
add_with_ctx :: (ctx: *void, v: s32) -> s32 {
add_with_ctx :: (ctx: *void, v: s32) -> s32 callconv(.c) {
g_callback_hits += 1;
// Pass a sentinel via ctx to prove the pointer arg also survives the
// round-trip — read it back as an s32 through *s32.