Skip to content

Commit d2ea0f3

Browse files
committed
Add working Jai example
1 parent 680ba1e commit d2ea0f3

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ build/counter: examples/counter.c coroutine.h build/coroutine.o
22
gcc -I. -Wall -Wextra -ggdb -o build/counter examples/counter.c build/coroutine.o
33

44
.PHONY:
5-
examples: build/counter build/counter_cpp build/counter_c3 build/echo
5+
examples: build/counter build/counter_cpp build/counter_c3 build/counter_jai build/echo
66

77
build/echo: examples/echo.c3 coroutine.c3 build/coroutine.o
88
cp build/coroutine.o build/echo.coroutine.o # c3c deletes the object files for some reason, so we make a copy to preserve the original
@@ -15,6 +15,12 @@ build/counter_c3: examples/counter.c3 coroutine.c3 build/coroutine.o
1515
cp build/coroutine.o build/counter.coroutine.o # c3c deletes the object files for some reason, so we make a copy to preserve the original
1616
c3c compile -o build/counter_c3 examples/counter.c3 coroutine.c3 build/counter.coroutine.o
1717

18+
build/counter_jai: examples/counter.jai build/coroutine.so
19+
jai-linux examples/counter.jai
20+
21+
build/coroutine.so: coroutine.c
22+
gcc -Wall -Wextra -ggdb -shared -fPIC -o build/coroutine.so coroutine.c
23+
1824
build/coroutine.o: coroutine.c coroutine.h
1925
mkdir -p build
2026
gcc -Wall -Wextra -ggdb -c -o build/coroutine.o coroutine.c

coroutine.c3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// coroutine bindings for C3
1+
// coroutine bindings for C3 (see coroutine.h for the API documentation)
22
module coroutine;
33
import std::net;
44

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.build/

examples/counter.jai

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#import "Basic";
2+
3+
coroutine :: #library "../build/coroutine";
4+
coroutine_sleep_read :: (fd: int) #foreign coroutine;
5+
coroutine_sleep_write :: (fd: int) #foreign coroutine;
6+
coroutine_wake_up :: (id: int) #foreign coroutine;
7+
coroutine_init :: () #foreign coroutine;
8+
coroutine_finish :: () #foreign coroutine;
9+
coroutine_yield :: () #foreign coroutine;
10+
coroutine_go :: (f: (*void) #c_call, arg: *void) #foreign coroutine;
11+
coroutine_id :: () -> int #foreign coroutine;
12+
coroutine_alive :: () -> int #foreign coroutine;
13+
14+
counter :: (arg: *void) #c_call {
15+
push_context {
16+
n := cast(int) arg;
17+
for 0..n-1 {
18+
print("[%] %\n", coroutine_id(), it);
19+
coroutine_yield();
20+
}
21+
}
22+
}
23+
24+
main :: () {
25+
coroutine_init();
26+
defer coroutine_finish();
27+
coroutine_go(counter, cast(*void) 5);
28+
coroutine_go(counter, cast(*void) 10);
29+
while coroutine_alive() > 1 {
30+
coroutine_yield();
31+
}
32+
}
33+
34+
#run {
35+
#import "Compiler";
36+
set_build_options_dc(.{
37+
output_path = "../build/",
38+
output_executable_name = "counter_jai",
39+
});
40+
}

0 commit comments

Comments
 (0)