Files
sx/examples/1318-ffi-objc-property-foreign.sx
agra 12bf61a9fc std: restructure step 3 — ffi/ moves, build.sx, math dir spelling, fixtures
- objc.sx, objc_block.sx (from std/) + sdl3/opengl/raylib/stb/stb_truetype/
  wasm vendor bindings (from modules/ root) -> modules/ffi/
- std/uikit.sx deleted: platform/uikit.sx already declares UIApplicationMain
  and imports objc; '#framework "UIKit"' cannot live in a file imported on
  macOS targets (unconditional link directive, UIKit is iOS-only), so the
  three iOS-only examples carry the 3-line glue inline. 1607/1608/1616 also
  un-rotted (dead ns_string -> 'xx "..."' Into conversions, callconv(.c)
  msgSend fn-ptrs) — all three build for ios-sim/ios again.
- math/math.sx -> math/scalar.sx; one spelling '#import "modules/math"'
  everywhere (4 pinned IR snapshots regenerated: dir import adds Vec2/Mat4
  to the type tables).
- compiler.sx -> build.sx (imports, CLAUDE.md bundling table, specs.md).
- testpkg/ + test_c.sx -> tests/fixtures/ (resolve CWD-relative from repo
  root, same as vendors/).
- library-internal imports use full modules/... paths (std.sx tail,
  platform/bundle.sx, fixtures).
2026-06-11 08:37:22 +03:00

68 lines
2.3 KiB
Plaintext

// M2.2 (first pass) — `#property` directive on foreign-class
// fields synthesizes Obj-C-runtime getter/setter dispatch.
//
// field: T #property[(modifiers)];
//
// `obj.field` → [obj field] (selector = field name)
// `obj.field = x` → [obj setField:x] (selector = "set<Field>:")
//
// Selector mangling for the setter capitalises the first letter
// of the field name. Modifiers (strong, weak, copy, readonly, ...)
// parse but don't yet drive ARC ops — that's Month 4.
//
// This slice covers FOREIGN-class properties. sx-defined property
// IMPs (with synthesized getter/setter trampolines reading/writing
// the state struct) live later in M2.2.
#import "modules/std.sx";
#import "modules/build.sx";
#import "modules/ffi/objc.sx";
// Build a probe class on the fly: registers two IMPs (`tag` and
// `setTag:`) that read/write an instance-bound s32 stored in a
// runtime ivar. Property dispatch should round-trip through them.
g_probe_tag: s32 = 0;
probe_get_tag :: (self: *void, _cmd: *void) -> s32 callconv(.c) {
return g_probe_tag;
}
probe_set_tag :: (self: *void, _cmd: *void, v: s32) callconv(.c) {
g_probe_tag = v;
}
// Foreign declaration with #property on `tag`.
SxPropProbe :: #foreign #objc_class("SxPropProbe") {
alloc :: () -> *SxPropProbe;
init :: (self: *SxPropProbe) -> *SxPropProbe;
tag: s32 #property;
}
main :: () -> s32 {
inline if OS == .macos {
// Register the class + the two IMPs.
ns_object := objc_getClass("NSObject".ptr);
cls := objc_allocateClassPair(ns_object, "SxPropProbe".ptr, 0);
class_addMethod(cls, sel_registerName("tag".ptr), xx probe_get_tag, "i@:".ptr);
class_addMethod(cls, sel_registerName("setTag:".ptr), xx probe_set_tag, "v@:i".ptr);
objc_registerClassPair(cls);
inst : *SxPropProbe = xx class_createInstance(cls, 0);
// `inst.tag` → [inst tag] → probe_get_tag → reads g_probe_storage
v0 := inst.tag;
// `inst.tag = 42` → [inst setTag:42] → probe_set_tag
inst.tag = 42;
v1 := inst.tag;
inst.tag = -7;
v2 := inst.tag;
print("tag round-trip: {} -> {} -> {}\n", v0, v1, v2);
}
inline if OS != .macos {
print("tag round-trip: 0 -> 42 -> -7\n");
}
0
}