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.
This commit is contained in:
agra
2026-06-12 22:14:14 +03:00
parent fe4f059a52
commit 4fa12853ed
8 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
// 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;
}

View File

@@ -0,0 +1,29 @@
// PLAN-HTTPZ C2: the C->sx re-entry contract. A real OS thread enters
// sx through a `callconv(.c)` entry (which has NO implicit context),
// fabricates its own Context via `push Context.{ allocator = xx gpa }`,
// and calls default-conv sx code that allocates through it.
#import "modules/std.sx";
clib :: #library "c";
pthread_create :: (thread: *usize, attr: *void, start: (*void) -> *void callconv(.c), arg: *void) -> i32 #foreign clib;
pthread_join :: (thread: usize, retval: **void) -> i32 #foreign clib;
entry :: (arg: *void) -> *void callconv(.c) {
p : *i64 = xx arg;
gpa := GPA.init();
push Context.{ allocator = xx gpa } {
s := concat("th", "read"); // allocate through the thread's OWN context
if s.len == 6 { p.* = p.* * 2; }
}
return null;
}
main :: () -> i32 {
v : i64 = 21;
th : usize = 0;
if pthread_create(@th, null, entry, xx @v) != 0 { print("create failed\n"); return 1; }
pthread_join(th, null);
if v != 42 { print("wrong result: {}\n", v); return 1; }
print("pthread into sx ok: {}\n", v);
return 0;
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
qsort via sx comparator ok

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
pthread into sx ok: 42