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:
26
examples/1635-cfnptr-qsort.sx
Normal file
26
examples/1635-cfnptr-qsort.sx
Normal 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;
|
||||||
|
}
|
||||||
29
examples/1636-cfnptr-pthread-reentry.sx
Normal file
29
examples/1636-cfnptr-pthread-reentry.sx
Normal 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;
|
||||||
|
}
|
||||||
1
examples/expected/1635-cfnptr-qsort.exit
Normal file
1
examples/expected/1635-cfnptr-qsort.exit
Normal file
@@ -0,0 +1 @@
|
|||||||
|
0
|
||||||
0
examples/expected/1635-cfnptr-qsort.stderr
Normal file
0
examples/expected/1635-cfnptr-qsort.stderr
Normal file
1
examples/expected/1635-cfnptr-qsort.stdout
Normal file
1
examples/expected/1635-cfnptr-qsort.stdout
Normal file
@@ -0,0 +1 @@
|
|||||||
|
qsort via sx comparator ok
|
||||||
1
examples/expected/1636-cfnptr-pthread-reentry.exit
Normal file
1
examples/expected/1636-cfnptr-pthread-reentry.exit
Normal file
@@ -0,0 +1 @@
|
|||||||
|
0
|
||||||
1
examples/expected/1636-cfnptr-pthread-reentry.stdout
Normal file
1
examples/expected/1636-cfnptr-pthread-reentry.stdout
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pthread into sx ok: 42
|
||||||
Reference in New Issue
Block a user