Files
sx/examples/1635-cfnptr-qsort.sx
agra 4fa12853ed test: pin C function pointers into sx (PLAN-HTTPZ C2 — pre-existing)
Both halves of the C2 contract already work in JIT and AOT; these
examples pin them. 1635: libc qsort drives an sx callconv(.c)
comparator passed by name as a typed fn-pointer param. 1636: a real
pthread enters sx through a callconv(.c) entry, fabricates its own
Context (push Context with a local GPA), and runs default-conv sx code
that allocates through it — the re-entry contract std.thread (S6)
stands on. Also unblocks the sqlite callback APIs (hooks/UDFs) left
unbound by design in P5.1.
2026-06-12 22:14:14 +03:00

27 lines
768 B
Plaintext

// PLAN-HTTPZ C2: an sx `callconv(.c)` function passes by name as a
// typed C function pointer — libc qsort drives an sx comparator.
#import "modules/std.sx";
clib :: #library "c";
qsort :: (base: [*]u8, nel: usize, width: usize, compar: (*void, *void) -> i32 callconv(.c)) #foreign clib;
cmp_i32 :: (a: *void, b: *void) -> i32 callconv(.c) {
pa : *i32 = xx a;
pb : *i32 = xx b;
if pa.* < pb.* { return -1; }
if pa.* > pb.* { return 1; }
return 0;
}
main :: () -> i32 {
arr : [5]i32 = .[ 4, 1, 5, 2, 3 ];
qsort(xx @arr[0], 5, 4, cmp_i32);
i := 0;
while i < 5 {
if arr[i] != cast(i32)(i + 1) { print("not sorted at {}\n", i); return 1; }
i += 1;
}
print("qsort via sx comparator ok\n");
return 0;
}