ffi M4.A: stdlib NSObject + autoreleasepool helper + extends rooting
Declare `NSObject` in std/objc.sx as `#foreign #objc_class("NSObject")`
with the canonical instance + class-method surface every Obj-C class
inherits: `retain`/`release`/`autorelease`/`new`/`alloc`/`init`/
`description`/`hash`/`isEqual_`/`isKindOfClass_`/`respondsToSelector_`/
`class`. Root the foreign-class hierarchy in uikit.sx at NSObject by
adding `#extends NSObject;` to every previously-unrooted declaration
(NSValue, NSNumber, NSDictionary, NSSet, NSNotification, NSBundle,
NSNotificationCenter, NSRunLoop, CADisplayLink, CALayer, EAGLContext,
UIScreen, UIResponder) plus deeper chain fixes (NSMutableDictionary
extends NSDictionary; UIWindow extends UIView; UIViewController
extends UIResponder). After this, M2.3's extends-chain walk finds
`retain`/`release` on any UIKit-typed value:
view := UIView.alloc().init();
defer view.release(); // canonical sx idiom — no language magic
Plus `autoreleasepool(body: Closure())` stdlib helper that wraps
`body` in `objc_autoreleasePoolPush` / `defer objc_autoreleasePoolPop`.
Required for Foundation factory returns; closure-call frame is real
cost so hot loops should inline the push/defer-pop pattern manually.
Smoke test `ffi-objc-arc-01-autoreleasepool.sx` exercises both
patterns; refresh of two IR snapshots picks up the new stdlib decls
appearing in test outputs that include `modules/std/objc.sx`.
185/185 example tests pass; chess on iOS-sim green.
This commit is contained in:
50
examples/ffi-objc-arc-01-autoreleasepool.sx
Normal file
50
examples/ffi-objc-arc-01-autoreleasepool.sx
Normal file
@@ -0,0 +1,50 @@
|
||||
// ffi-objc-arc-01 — M4.A smoke test for NSObject + autoreleasepool.
|
||||
//
|
||||
// Exercises:
|
||||
// 1. NSObject is declared in std/objc.sx and reachable from user code.
|
||||
// `obj.retain()` / `obj.release()` dispatch via the M2.3 #extends-aware
|
||||
// method chain. Pattern: `defer obj.release();` as the canonical
|
||||
// sx idiom for owned Obj-C handles.
|
||||
// 2. `autoreleasepool(body)` stdlib helper wraps `body` in a
|
||||
// push/defer-pop pair so Foundation factory returns drain at block
|
||||
// end.
|
||||
//
|
||||
// macOS-only — libobjc + NSObject must be available at runtime.
|
||||
|
||||
#import "modules/std.sx";
|
||||
#import "modules/std/objc.sx";
|
||||
#import "modules/compiler.sx";
|
||||
|
||||
main :: () -> s32 {
|
||||
inline if OS == .macos {
|
||||
// Manual retain/release on an NSObject instance — the
|
||||
// `defer obj.release();` pattern is the canonical sx idiom.
|
||||
obj := NSObject.alloc().init();
|
||||
if obj == null { print("FAIL: alloc null\n"); return 1; }
|
||||
defer obj.release();
|
||||
|
||||
// Bump the count and drop the extra; refcount math stays balanced.
|
||||
_ = obj.retain();
|
||||
obj.release();
|
||||
|
||||
print("retain/release: ok\n");
|
||||
|
||||
// autoreleasepool helper round-trip — just exercise that the
|
||||
// push/pop pair executes. We don't have a side-effect to observe
|
||||
// (NSObject.new returns a +1 retained, NOT autoreleased), so this
|
||||
// is a smoke test of the helper's shape, not the runtime
|
||||
// behavior.
|
||||
autoreleasepool(() => {
|
||||
inner := NSObject.new();
|
||||
if inner != null {
|
||||
inner.release();
|
||||
}
|
||||
});
|
||||
|
||||
print("autoreleasepool: ok\n");
|
||||
}
|
||||
inline if OS != .macos {
|
||||
print("skipped (not macos)\n");
|
||||
}
|
||||
0;
|
||||
}
|
||||
Reference in New Issue
Block a user