Files
sx/examples/62-objc-class.sx
agra 92c6b47f12 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).
2026-05-25 08:41:50 +03:00

42 lines
1.5 KiB
Plaintext

// Register a brand-new Obj-C class from sx and prove a sx-defined method
// actually runs when dispatched through `objc_msgSend`.
//
// The flow:
// 1. `objc_allocateClassPair(NSObject, "SxThing", 0)` returns an unregistered Class.
// 2. `class_addMethod(cls, sel_hello, IMP, "v@:")` attaches our sx function as
// the `hello` method (type encoding "v@:" = void method(id self, SEL _cmd)).
// 3. `objc_registerClassPair(cls)` finalizes it.
// 4. `class_createInstance(cls, 0)` returns an `id` for a fresh instance.
// 5. typed-cast `objc_msgSend` for `void (id, SEL)` and dispatch `hello`.
// If the IMP ran, the global `g_marker` is non-zero and we return it as exit.
#import "modules/std.sx";
#import "modules/std/objc.sx";
g_marker : s32 = 0;
// IMP for `hello`. Must use C calling convention so `self` and `_cmd` land in
// x0 and x1 the way the Obj-C runtime expects.
hello_imp :: (self: *void, _cmd: *void) callconv(.c) {
g_marker = 42;
}
main :: () -> s32 {
NSObject := objc_getClass("NSObject".ptr);
SxThing := objc_allocateClassPair(NSObject, "SxThing".ptr, 0);
sel_hello := sel_registerName("hello".ptr);
ok := class_addMethod(SxThing, sel_hello, xx hello_imp, "v@:".ptr);
if !ok { return 1; }
objc_registerClassPair(SxThing);
obj := class_createInstance(SxThing, 0);
if obj == xx 0 { return 2; }
// [obj hello]
msg : (*void, *void) -> void callconv(.c) = xx objc_msgSend;
msg(obj, sel_hello);
return g_marker; // 42 if hello_imp ran
}