Files
sx/examples/1301-ffi-objc-class.sx
agra d8076b9333 lang: rename signed integer types sN -> iN
Surface rename of the signed integer family: s1..s64 become i1..i64
(u1..u64, usize, isize unchanged). 'string' keeps the s-prefix arm in
name classification; width parsing moves to the i-prefix arm next to
isize.

Internal TypeId tags follow the surface (.s8/.s16/.s32/.s64 ->
.i8/.i16/.i32/.i64), as do mono-key mangle fragments (ptr_i64,
tu_i64_bool) and all display/diagnostic formatting (i{d}).

Migrated in the same sweep: stdlib + examples + issue repros + FFI C
companions (shared symbol names like ffi_id_i64), expected
stdout/stderr/ir snapshots, specs.md, readme.md, CLAUDE.md/AGENTS.md,
implementation_plan.md, docs/, issue writeups. Vendored stb_image and
historical flow state left untouched.

zig build test: 426/426; examples suite: 595/595.
2026-06-12 09:31:53 +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/ffi/objc.sx";
g_marker : i32 = 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 :: () -> i32 {
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
}