Files
sx/examples/152-objc-property-sx-defined.sx
agra 239e7df27c ffi M2.2 (sx-defined): property getter/setter IMPs
Properties on sx-defined #objc_class declarations now synthesize
getter (always) and setter (unless 'readonly') IMPs that GEP into
the hidden state struct and load / store the corresponding field.
The state struct already holds every user-declared field
(objcDefinedStateStructType), so no new layout work — the IMPs
just dispatch a struct_gep + load/store through the __sx_state
ivar.

For each '#property' field on a sx-defined class:

  Getter '__<Cls>_<field>_imp(self, _cmd) -> T':
    state = object_getIvar(self, load(__<Cls>_state_ivar))
    return state.<field>

  Setter '__<Cls>_set<Field>_imp(self, _cmd, val) -> void':
    state = object_getIvar(self, load(__<Cls>_state_ivar))
    state.<field> = val

Both IMPs land in the cache's methods slice (mirroring the
method-IMP wiring from M1.2 A.4b.iii) so emit_llvm's
class_addMethod loop registers them on the class without
special-casing. Selector mangling:
  getter: <field>            (e.g. 'width')
  setter: set<Field>:        (e.g. 'setWidth:')
Type encoding derived from the field's resolved IR TypeId.

'readonly' (the only modifier honored in this slice) skips the
setter emission AND the corresponding method entry — so the
runtime reports the selector as absent. Other modifiers
(strong, weak, copy, assign) parse fine but stay no-ops until
M4.2 wires up ARC ops in the setter body.

152-objc-property-sx-defined.sx round-trips on macOS:
  b.width = 10; b.height = 7;
  read back through getter IMPs.
  area is readonly — class_getInstanceMethod(SxBox, sel(setArea:))
  returns NULL, confirming the setter is absent.

182 example tests pass (+1). zig build test green.
2026-05-26 01:49:31 +03:00

73 lines
2.1 KiB
Plaintext

// M2.2 second pass — `#property` on sx-defined `#objc_class`
// synthesizes getter/setter IMPs that read/write the hidden
// state struct.
//
// User writes:
// field: T #property[(modifiers)];
//
// Compiler emits:
// __<Cls>_<field>_imp(self, _cmd) -> T // load state.field
// __<Cls>_set<Field>_imp(self, _cmd, v) -> void // store state.field
// (setter skipped for `readonly`.)
//
// Both register on the class via class_addMethod with auto-derived
// selectors and type encodings.
//
// Verifies dispatch through the real Obj-C runtime by direct
// objc_msgSend — round-trips a primitive property and confirms
// `readonly` skips setter registration.
#import "modules/std.sx";
#import "modules/compiler.sx";
#import "modules/std/objc.sx";
class_getInstanceMethod :: (cls: *void, sel: *void) -> *void #foreign objc;
SxBox :: #objc_class("SxBox") {
width: s32 #property;
height: s32 #property;
area: s32 #property(readonly); // setter omitted
alloc :: () -> *SxBox;
init :: (self: *SxBox) -> *SxBox;
}
main :: () -> s32 {
inline if OS == .macos {
b := SxBox.alloc().init();
// width / height round-trip through synthesized IMPs.
b.width = 10;
b.height = 7;
w := b.width;
h := b.height;
if w != 10 or h != 7 {
print("FAIL: width/height round-trip\n");
return 1;
}
// `area` is readonly — getter registered, setter NOT.
// area starts at 0 (state zero-init); read works:
a := b.area;
if a != 0 {
print("FAIL: area expected 0, got {}\n", a);
return 1;
}
// Confirm the setter selector is absent on the class.
cls : Class = objc_getClass("SxBox".ptr);
sel_set_area : SEL = sel_registerName("setArea:".ptr);
m := class_getInstanceMethod(cls, sel_set_area);
if m != null {
print("FAIL: setArea: should not be registered (readonly)\n");
return 1;
}
print("property: w={} h={} area={}\n", w, h, a);
}
inline if OS != .macos {
print("property: w=10 h=7 area=0\n");
}
0;
}