Files
sx/examples/1325-ffi-objc-arc-02-strong-property.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

79 lines
2.8 KiB
Plaintext

// ffi-objc-arc-02 — #property(strong) on sx-defined class.
//
// Strong contract:
// - setter retains the new value, releases the old.
// - -dealloc releases each strong property ivar before freeing state.
//
// Observation: a child instance assigned to a parent's strong property
// stays alive after we drop our own reference. Without the strong
// setter, the child's refcount drops to 0 immediately when we release,
// and the parent ends up with a dangling pointer.
//
// We observe via TrackingAllocator. The KEY check is the dealloc count
// AT THE MIDPOINT — between dropping our child reference and releasing
// the parent. With strong+dealloc-cleanup: child stays alive across
// midpoint (no extra dealloc). Without: child is dead at midpoint.
#import "modules/std.sx";
#import "modules/std/mem.sx";
#import "modules/ffi/objc.sx";
#import "modules/build.sx";
SxStrongChild :: #objc_class("SxStrongChild") {
#extends NSObject;
tag: i32;
alloc :: () -> *SxStrongChild;
}
SxStrongParent :: #objc_class("SxStrongParent") {
#extends NSObject;
child: *SxStrongChild #property(strong);
alloc :: () -> *SxStrongParent;
}
main :: () -> i32 {
inline if OS == .macos {
gpa := GPA.init();
tracker := TrackingAllocator.init(xx gpa);
push Context.{ allocator = xx tracker, data = null } {
d0 := tracker.dealloc_count;
a0 := tracker.alloc_count;
parent := SxStrongParent.alloc();
child := SxStrongChild.alloc();
parent.child = child; // dispatches setChild: via M2.2 property machinery
child.release();
// Midpoint: with strong setter, child is retained by parent;
// tracker.dealloc_count should NOT have advanced beyond d0.
// Without strong setter, child auto-dealloc'd on release.
d_mid := tracker.dealloc_count;
if d_mid != d0 {
print("FAIL: child dealloc'd at midpoint (strong setter not retaining); delta={}\n",
d_mid - d0);
return 1;
}
parent.release();
// After parent.release: parent.dealloc fires, releases the
// strong child ivar, child deallocs, state freed.
d_end := tracker.dealloc_count;
// Net: 2 allocs (parent + child state), 2 deallocs (both freed).
// Balance check: dealloc delta == alloc delta.
if d_end - d0 != tracker.alloc_count - a0 {
print("FAIL: unbalanced; alloc={} dealloc={}\n",
tracker.alloc_count - a0, d_end - d0);
return 1;
}
}
print("strong property: ok\n");
}
inline if OS != .macos {
print("skipped (not macos)\n");
}
0
}