Files
sx/examples/ffi-objc-dsl-07-mangling-table.sx
agra a32cc2dc27 ffi 3.2 B: locked-in golden test for the Obj-C selector mangling table
`examples/ffi-objc-dsl-07-mangling-table.sx` exercises every common
mangling shape in one fixture and pins the resolved selectors via
both `.txt` and `.ir` snapshots:

| sx method                          | derived selector            |
|-----------------------------------|----------------------------|
| `length`                           | `length`                    |
| `addObject(o)`                     | `addObject:`                |
| `combine_and(a, b)`                | `combine:and:`              |
| `insert_after_index(a, b, c)`      | `insert:after:index:`       |
| `add_observer_for_event(a, b, c, d)` | `add:observer:for:event:`   |
| `initWithFrame_options(f, o)`      | `initWithFrame:options:`    |
| `custom_name #selector("actualSelectorName")` | `actualSelectorName` |

The class is synthesized at runtime via `objc_allocateClassPair` +
`class_addMethod` per selector (mirrors the pattern in
`ffi-objc-dsl-{01..05}.sx`), so the test actually dispatches through
the real Obj-C runtime on macOS.

Single commit because the implementation already shipped in 3.0/3.2;
this is a new regression that locks in current behavior, not a
test-then-make-green pair.

The `.ir` snapshot opts in via the existing run_examples.sh mechanism
(presence of a `.ir` file for the same name triggers capture). The
captured `OBJC_METH_VAR_NAME_*` constants surface every selector
string change at a glance.

166/166 tests.
2026-05-25 17:03:16 +03:00

71 lines
3.4 KiB
Plaintext

// Phase 3 step 3.2 — locked-in golden test for the default Obj-C
// selector mangling rule (Phase 3.0). One fixture covers the common
// shapes (niladic, 1-arg through 4-arg, camelCase across pieces, and
// the `#selector(...)` override). The accompanying `.ir` snapshot
// records each resolved selector string as an `OBJC_METH_VAR_NAME_*`
// constant — a change to `deriveObjcSelector` produces ONE diff that
// surfaces every affected case at once.
//
// Per the rule:
// - Niladic (arity 0): name verbatim. `length` → "length".
// - Arity N (1..): split the sx name on `_`; each piece becomes a
// keyword with a trailing `:`. Piece count must equal arity.
// - `#selector("...")` overrides the mangling entirely; the literal
// string is used as the selector. Arity is the user's contract.
#import "modules/std.sx";
#import "modules/compiler.sx";
#import "modules/std/objc.sx";
SxManglingProbe :: #foreign #objc_class("SxManglingProbe") {
length :: (self: *Self) -> s32;
addObject :: (self: *Self, a: s32) -> s32;
combine_and :: (self: *Self, a: s32, b: s32) -> s32;
insert_after_index :: (self: *Self, a: s32, b: s32, c: s32) -> s32;
add_observer_for_event :: (self: *Self, a: s32, b: s32, c: s32, d: s32) -> s32;
initWithFrame_options :: (self: *Self, f: s32, o: s32) -> s32;
custom_name :: (self: *Self) -> s32 #selector("actualSelectorName");
}
universal_imp :: (self: *void, _cmd: *void, a: s32, b: s32, c: s32, d: s32) -> s32 callconv(.c) {
// Returns the arg count's witness; the test doesn't check return
// values, only that dispatch succeeds for each selector shape.
a + b + c + d;
}
main :: () -> s32 {
inline if OS == .macos {
ns_object := objc_getClass("NSObject".ptr);
cls := objc_allocateClassPair(ns_object, "SxManglingProbe".ptr, 0);
// Register one IMP per selector we'll dispatch to.
class_addMethod(cls, sel_registerName("length".ptr), xx universal_imp, "i@:".ptr);
class_addMethod(cls, sel_registerName("addObject:".ptr), xx universal_imp, "i@:i".ptr);
class_addMethod(cls, sel_registerName("combine:and:".ptr), xx universal_imp, "i@:ii".ptr);
class_addMethod(cls, sel_registerName("insert:after:index:".ptr), xx universal_imp, "i@:iii".ptr);
class_addMethod(cls, sel_registerName("add:observer:for:event:".ptr), xx universal_imp, "i@:iiii".ptr);
class_addMethod(cls, sel_registerName("initWithFrame:options:".ptr), xx universal_imp, "i@:ii".ptr);
class_addMethod(cls, sel_registerName("actualSelectorName".ptr), xx universal_imp, "i@:".ptr);
objc_registerClassPair(cls);
inst : *SxManglingProbe = xx class_createInstance(cls, 0);
// One call per mangling shape; the IR snapshot pins what
// selector string each sx name resolves to.
_ = inst.length();
_ = inst.addObject(1);
_ = inst.combine_and(1, 2);
_ = inst.insert_after_index(1, 2, 3);
_ = inst.add_observer_for_event(1, 2, 3, 4);
_ = inst.initWithFrame_options(1, 2);
_ = inst.custom_name();
print("mangling table OK\n");
}
inline if OS != .macos {
print("skipped (not macos)\n");
}
0;
}