Phase 2 of the extern/export stream verifies `export` (define + expose a
C-ABI sx symbol) end-to-end. C->sx-by-name linkage cannot work under the
corpus's `sx run` JIT mode — a JIT-resident symbol is invisible to a
dlopen'd C dylib's flat-namespace lookup — so this lands a new AOT
execution mode for the corpus: an `expected/<name>.aot` marker switches an
example from JIT `sx run` to a `sx build` + execute flow, linking the sx
object with its C `#source` companions into a native binary.
example/1226 defines `sx_square :: (n: i32) -> i32 export { ... }` and a
companion .c that declares `extern int sx_square(int)` and calls it back.
RED: with `export` not yet lowered, the AOT link fails with an undefined
`_sx_square` (the define path still emits it `internal` + with an implicit
ctx slot, and lazy lowering leaves an uncalled export fn as a bodiless
declare). Phase 2.1 greens it.
Also retires the standalone `tests/run_examples.sh` runner — `zig build
test` (src/corpus_run.test.zig) is now the sole corpus runner, and the
shell mirror would have needed its own AOT-mode port to stay in lockstep.
verify-step.sh drops its redundant step (zig build test already runs the
corpus); CLAUDE.md documents the `.aot` mode.
29 lines
1.1 KiB
Plaintext
29 lines
1.1 KiB
Plaintext
// export function (FFI-linkage stream, Phase 2): define an sx function with
|
||
// the bare `export` linkage modifier — external linkage + C ABI + no sx ctx —
|
||
// so a companion C translation unit can call back into it by its plain symbol
|
||
// name. The C side (`#source`) declares `sx_square` as a normal `extern int`
|
||
// and calls it; sx `main` drives the C side via `call_sx_square`. Mirrors the
|
||
// import-direction `extern` examples (1223–1225) for the define direction.
|
||
//
|
||
// Without `export`, an sx-defined fn is `internal` linkage + carries the
|
||
// implicit `__sx_ctx` slot, so the C object can neither resolve nor correctly
|
||
// call the symbol — this is the gap `export` fills.
|
||
#import "modules/std.sx";
|
||
|
||
#import c {
|
||
#include "1226-ffi-export-fn.h";
|
||
#source "1226-ffi-export-fn.c";
|
||
};
|
||
|
||
// sx-defined, exported to C: external linkage + C ABI + no implicit ctx.
|
||
sx_square :: (n: i32) -> i32 export {
|
||
return n * n;
|
||
}
|
||
|
||
main :: () -> i32 {
|
||
// call_sx_square (C) calls back into sx_square, adds 1.
|
||
print("call_sx_square(6) = {}\n", call_sx_square(6));
|
||
print("call_sx_square(9) = {}\n", call_sx_square(9));
|
||
0
|
||
}
|