ffi M1.2 A.4b.i: __sx_state ivar registration
Class-pair init constructor now registers a single hidden ivar on each sx-defined class: class_addIvar(cls, "__sx_state", 8, 3, "^v") before objc_registerClassPair. After the class is registered, the constructor calls class_getInstanceVariable to fetch the runtime Ivar handle and stores it in a per-class global '__<ClassName>_state_ivar : *void'. Trampolines (A.4b.ii) will read this global to 'object_getIvar' the state struct pointer. lower.zig declares the per-class global at scan time (declareObjcDefinedStateIvarGlobal) so emit_llvm finds it by name when populating. Encoding '^v' = void* (a generic pointer — the runtime treats it as opaque storage). log2 alignment = 3 for 8-byte pointer alignment on 64-bit. 144-objc-class-ivar-registration.sx exercises the round-trip: after main starts, class_getInstanceVariable(SxFoo, "__sx_state") returns non-null. Runs against the real Obj-C runtime on macOS. 142's IR snapshot refreshed to include the new constructor body (class_addIvar + class_getInstanceVariable + ivar-global store). 173 example tests pass (+1 from 144). zig build test green.
This commit is contained in:
@@ -9617,11 +9617,31 @@ pub const Lowering = struct {
|
||||
if (!fcd.is_foreign and fcd.runtime == .objc_class) {
|
||||
if (self.module.lookupObjcDefinedClass(fcd.name) == null) {
|
||||
self.module.appendObjcDefinedClass(fcd.name, fcd);
|
||||
// M1.2 A.4b.i: per-class ivar handle global. The class-pair
|
||||
// init constructor (emit_llvm) populates it via
|
||||
// class_getInstanceVariable after the class is registered;
|
||||
// IMP trampolines read it to find the __sx_state ivar.
|
||||
self.declareObjcDefinedStateIvarGlobal(fcd.name);
|
||||
}
|
||||
self.registerObjcDefinedClassMethods(fcd);
|
||||
}
|
||||
}
|
||||
|
||||
/// Declare a per-class global `__<ClassName>_state_ivar : *void = null`.
|
||||
/// emit_llvm's `emitObjcDefinedClassInit` constructor fills it in via
|
||||
/// `class_getInstanceVariable(cls, "__sx_state")` once per module load.
|
||||
fn declareObjcDefinedStateIvarGlobal(self: *Lowering, class_name: []const u8) void {
|
||||
const gname = std.fmt.allocPrint(self.alloc, "__{s}_state_ivar", .{class_name}) catch return;
|
||||
const name_id = self.module.types.internString(gname);
|
||||
_ = self.module.addGlobal(.{
|
||||
.name = name_id,
|
||||
.ty = self.module.types.ptrTo(.void),
|
||||
.init_val = .null_val,
|
||||
.is_extern = false,
|
||||
.is_const = false,
|
||||
});
|
||||
}
|
||||
|
||||
/// For each bodied instance method on an sx-defined `#objc_class`,
|
||||
/// synthesize an `FnDecl` from the `ForeignMethodDecl`, register it
|
||||
/// in `fn_ast_map` under `<ClassName>.<methodName>`, and declare
|
||||
|
||||
Reference in New Issue
Block a user