Files
sx/examples/1320-ffi-objc-extends-chain.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

63 lines
2.0 KiB
Plaintext

// M2.3 — `#extends ForeignClass` method-resolution chaining.
//
// When `obj.method()` is called on a foreign-class pointer and
// `method` isn't declared directly on the receiver's class, the
// compiler walks the `#extends` chain to find an ancestor that
// declared it. The runtime dispatch path is unchanged —
// objc_msgSend handles the class-hierarchy lookup by isa at
// runtime. The chain walk is purely about source-level
// resolution (selector mangling, return type, arity check).
#import "modules/std.sx";
#import "modules/build.sx";
#import "modules/ffi/objc.sx";
NSObjectBase :: #foreign #objc_class("NSObject") {
alloc :: () -> *NSObjectBase;
init :: (self: *NSObjectBase) -> *NSObjectBase;
hash :: (self: *NSObjectBase) -> u64;
}
// Sx-defined class that extends a foreign one. M1.2 registers
// the class at module init; `hash` is reached via the M2.3 chain
// walk through NSObjectBase, then dispatched by objc_msgSend.
SxThing :: #objc_class("SxThing") {
#extends NSObjectBase;
counter: s32;
alloc :: () -> *SxThing;
init :: (self: *SxThing) -> *SxThing;
}
// And a chain-of-three: SxLeaf → SxMiddle → NSObjectBase.
SxMiddle :: #objc_class("SxMiddle") {
#extends NSObjectBase;
alloc :: () -> *SxMiddle;
init :: (self: *SxMiddle) -> *SxMiddle;
}
SxLeaf :: #objc_class("SxLeaf") {
#extends SxMiddle;
alloc :: () -> *SxLeaf;
init :: (self: *SxLeaf) -> *SxLeaf;
}
main :: () -> s32 {
inline if OS == .macos {
// 1-level chain: SxThing → NSObjectBase.
t := SxThing.alloc().init();
h_t : u64 = t.hash();
if h_t == 0 { print("FAIL: SxThing.hash returned 0\n"); return 1; }
// 2-level chain: SxLeaf → SxMiddle → NSObjectBase.
l := SxLeaf.alloc().init();
h_l : u64 = l.hash();
if h_l == 0 { print("FAIL: SxLeaf.hash returned 0\n"); return 1; }
print("extends chain: SxThing.hash=ok, SxLeaf.hash=ok\n");
}
inline if OS != .macos {
print("extends chain: SxThing.hash=ok, SxLeaf.hash=ok\n");
}
0
}