Skip to content

Commit 9be466f

Browse files
authored
Test under wasmtime as well (#3)
* Make tests wasm compatible * Test wasm build too * Define tests in build.zig
1 parent 75bfb3c commit 9be466f

File tree

3 files changed

+68
-32
lines changed

3 files changed

+68
-32
lines changed

.github/workflows/deploy.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ jobs:
1919
- run: npm ci
2020
- uses: mlugg/setup-zig@v2
2121
with:
22-
version: 0.15.1
22+
version: "0.15.2"
23+
- uses: bytecodealliance/actions/wasmtime/setup@v1
24+
with:
25+
version: "38.0.3"
2326
- run: zig build activate
2427
- run: zig build
25-
- run: zig test -fllvm src/zorth.zig
28+
- run: zig build test --summary all
2629
- uses: actions/upload-pages-artifact@v3
2730
with:
2831
path: zig-out

build.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,34 @@ pub fn build(b: *std.Build) void {
7474
install.step.dependOn(&bootstrap.step);
7575

7676
b.getInstallStep().dependOn(&install.step);
77+
78+
const test_step = b.step("test", "Run unit tests");
79+
80+
const native_tests = b.addTest(.{
81+
.use_llvm = true,
82+
.root_module = b.createModule(.{
83+
.root_source_file = b.path("src/zorth.zig"),
84+
.target = b.resolveTargetQuery(.{}),
85+
}),
86+
});
87+
const run_native_tests = b.addRunArtifact(native_tests);
88+
test_step.dependOn(&run_native_tests.step);
89+
90+
const wasm_tests = b.addTest(.{
91+
.root_module = b.createModule(.{
92+
.root_source_file = b.path("src/zorth.zig"),
93+
.target = b.resolveTargetQuery(.{
94+
.cpu_arch = .wasm32,
95+
.os_tag = .wasi,
96+
.cpu_features_add = std.Target.wasm.featureSet(&.{
97+
.atomics,
98+
.bulk_memory,
99+
.tail_call,
100+
}),
101+
}),
102+
}),
103+
});
104+
wasm_tests.setExecCmd(&.{ "wasmtime", null });
105+
const run_wasm_tests = b.addRunArtifact(wasm_tests);
106+
test_step.dependOn(&run_wasm_tests.step);
77107
}

src/zorth.zig

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ fn forth(input: []const u8, expected: [:0]const u8) !void {
10431043
test forth {
10441044
// This is a stripped version of https://github.com/nornagon/jonesforth/blob/master/jonesforth.S
10451045
// to bootstrap just enough "high-level" Forth so this test achieves reasonable coverage.
1046-
const preamble =
1046+
const preamble = fmt.comptimePrint(
10471047
\\: / /MOD SWAP DROP ;
10481048
\\: '\n' 10 ;
10491049
\\: BL 32 ;
@@ -1069,26 +1069,26 @@ test forth {
10691069
\\: WHILE IMMEDIATE ' 0BRANCH , HERE @ 0 , ;
10701070
\\: REPEAT IMMEDIATE ' BRANCH , SWAP HERE @ - , DUP HERE @ SWAP - SWAP ! ;
10711071
\\: NIP SWAP DROP ;
1072-
\\: PICK 1+ 8 * DSP@ + @ ;
1072+
\\: PICK 1+ {d} * DSP@ + @ ;
10731073
\\: SPACES BEGIN DUP 0> WHILE SPACE 1- REPEAT DROP ;
10741074
\\: U. BASE @ /MOD ?DUP IF RECURSE THEN DUP 10 < IF '0' ELSE 10 - 'A' THEN + EMIT ;
1075-
\\: .S DSP@ BEGIN DUP S0 @ < WHILE DUP @ U. 8+ SPACE REPEAT DROP ;
1075+
\\: .S DSP@ BEGIN DUP S0 @ < WHILE DUP @ U. {d}+ SPACE REPEAT DROP ;
10761076
\\: UWIDTH BASE @ / ?DUP IF RECURSE 1+ ELSE 1 THEN ;
10771077
\\: U.R SWAP DUP UWIDTH ROT SWAP - SPACES U. ;
10781078
\\: .R SWAP DUP 0< IF NEGATE 1 SWAP ROT 1- ELSE 0 SWAP ROT THEN SWAP DUP UWIDTH ROT SWAP - SPACES SWAP IF '-' EMIT THEN U. ;
10791079
\\: . 0 .R SPACE ;
10801080
\\: U. U. SPACE ;
10811081
\\: WITHIN -ROT OVER <= IF > IF TRUE ELSE FALSE THEN ELSE 2DROP FALSE THEN ;
1082-
\\: ALIGNED 7 + -8 AND ;
1082+
\\: ALIGNED {d} 1- + -{d} AND ;
10831083
\\: ALIGN HERE @ ALIGNED HERE ! ;
10841084
\\: C, HERE @ C! 1 HERE +! ;
10851085
\\: S" IMMEDIATE STATE @ IF
1086-
\\ ' LITSTRING , HERE @ 0 , BEGIN KEY DUP '"' <> WHILE C, REPEAT DROP DUP HERE @ SWAP - 8- SWAP ! ALIGN ELSE
1086+
\\ ' LITSTRING , HERE @ 0 , BEGIN KEY DUP '"' <> WHILE C, REPEAT DROP DUP HERE @ SWAP - {d}- SWAP ! ALIGN ELSE
10871087
\\ HERE @ BEGIN KEY DUP '"' <> WHILE OVER C! 1+ REPEAT DROP HERE @ - HERE @ SWAP THEN ;
10881088
\\: ." IMMEDIATE STATE @ IF [COMPILE] S" ' TELL , ELSE BEGIN KEY DUP '"' = IF DROP EXIT THEN EMIT AGAIN THEN ;
1089-
\\: CELLS 8 * ;
1090-
\\: ID. 8+ DUP C@ F_LENMASK AND BEGIN DUP 0> WHILE SWAP 1+ DUP C@ EMIT SWAP 1- REPEAT 2DROP ;
1091-
\\: ?IMMEDIATE 8+ C@ F_IMMED AND ;
1089+
\\: CELLS {d} * ;
1090+
\\: ID. {d}+ DUP C@ F_LENMASK AND BEGIN DUP 0> WHILE SWAP 1+ DUP C@ EMIT SWAP 1- REPEAT 2DROP ;
1091+
\\: ?IMMEDIATE {d}+ C@ F_IMMED AND ;
10921092
\\: CASE IMMEDIATE 0 ;
10931093
\\: OF IMMEDIATE ' OVER , ' = , [COMPILE] IF ' DROP , ;
10941094
\\: ENDOF IMMEDIATE [COMPILE] ELSE ;
@@ -1098,30 +1098,27 @@ test forth {
10981098
\\ ':' EMIT SPACE DUP ID. SPACE DUP ?IMMEDIATE IF ." IMMEDIATE " THEN >DFA
10991099
\\ BEGIN 2DUP > WHILE DUP @
11001100
\\ CASE
1101-
\\ ' LIT OF 8+ DUP @ . ENDOF
1102-
\\ ' LITSTRING OF [ CHAR S ] LITERAL EMIT '"' EMIT SPACE 8+ DUP @ SWAP 8+ SWAP 2DUP TELL '"' EMIT SPACE + ALIGNED 8- ENDOF
1103-
\\ ' 0BRANCH OF ." 0BRANCH ( " 8+ DUP @ . ." ) " ENDOF
1104-
\\ ' BRANCH OF ." BRANCH ( " 8+ DUP @ . ." ) " ENDOF
1105-
\\ ' ' OF [ CHAR ' ] LITERAL EMIT SPACE 8+ DUP CFA> ID. SPACE ENDOF
1106-
\\ ' EXIT OF 2DUP 8+ <> IF ." EXIT " THEN ENDOF
1101+
\\ ' LIT OF {d}+ DUP @ . ENDOF
1102+
\\ ' LITSTRING OF [ CHAR S ] LITERAL EMIT '"' EMIT SPACE {d}+ DUP @ SWAP {d}+ SWAP 2DUP TELL '"' EMIT SPACE + ALIGNED {d}- ENDOF
1103+
\\ ' 0BRANCH OF ." 0BRANCH ( " {d}+ DUP @ . ." ) " ENDOF
1104+
\\ ' BRANCH OF ." BRANCH ( " {d}+ DUP @ . ." ) " ENDOF
1105+
\\ ' ' OF [ CHAR ' ] LITERAL EMIT SPACE {d}+ DUP CFA> ID. SPACE ENDOF
1106+
\\ ' EXIT OF 2DUP {d}+ <> IF ." EXIT " THEN ENDOF
11071107
\\ DUP CFA> ID. SPACE
11081108
\\ ENDCASE
1109-
\\ 8+
1109+
\\ {d}+
11101110
\\ REPEAT
11111111
\\ ';' EMIT CR 2DROP ;
11121112
\\: ['] IMMEDIATE ' LIT , ;
11131113
\\: EXCEPTION-MARKER RDROP 0 ;
1114-
\\: CATCH DSP@ 8+ >R ' EXCEPTION-MARKER 8+ >R EXECUTE ;
1115-
\\: THROW ?DUP IF RSP@ BEGIN DUP R0 8- < WHILE DUP @ ' EXCEPTION-MARKER 8+ = IF 8+ RSP! DUP DUP DUP R> 8- SWAP OVER ! DSP! EXIT THEN 8+ REPEAT
1114+
\\: CATCH DSP@ {d}+ >R ' EXCEPTION-MARKER {d}+ >R EXECUTE ;
1115+
\\: THROW ?DUP IF RSP@ BEGIN DUP R0 {d}- < WHILE DUP @ ' EXCEPTION-MARKER {d}+ = IF {d}+ RSP! DUP DUP DUP R> {d}- SWAP OVER ! DSP! EXIT THEN {d}+ REPEAT
11161116
\\ DROP CASE 0 1- OF ." ABORTED" CR ENDOF ." UNCAUGHT THROW " DUP . CR ENDCASE QUIT THEN ;
11171117
\\: STRLEN DUP BEGIN DUP C@ 0<> WHILE 1+ REPEAT SWAP - ;
1118-
\\: ARGC (ARGC) @ ;
1119-
\\: ENVIRON ARGC 2 + CELLS (ARGC) + ;
11201118
\\
1121-
;
1122-
const s = mem.readInt(usize, "SYSCALL0", .little);
1123-
const p = try fmt.allocPrintSentinel(testing.allocator, "{d} ", .{os.linux.getppid()}, 0);
1124-
defer testing.allocator.free(p);
1119+
, .{@sizeOf(usize)} ** 24);
1120+
1121+
const s = mem.readInt(usize, "F" ** @sizeOf(usize), .little);
11251122
const tests = .{
11261123
.{ "65 EMIT ", "A" },
11271124
.{ "777 65 EMIT ", "A" },
@@ -1130,8 +1127,8 @@ test forth {
11301127
.{ "8 DUP * 1+ EMIT ", "A" },
11311128
.{ "CHAR A EMIT ", "A" },
11321129
.{ ": SLOW WORD FIND >CFA EXECUTE ; 65 SLOW EMIT ", "A" },
1133-
.{ fmt.comptimePrint("{d} DSP@ 8 TELL ", .{s}), "SYSCALL0" },
1134-
.{ fmt.comptimePrint("{d} DSP@ HERE @ 8 CMOVE HERE @ 8 TELL ", .{s}), "SYSCALL0" },
1130+
.{ fmt.comptimePrint("{d} DSP@ {d} TELL ", .{ s, @sizeOf(usize) }), "F" ** @sizeOf(usize) },
1131+
.{ fmt.comptimePrint("{d} DSP@ HERE @ {d} CMOVE HERE @ {d} TELL ", .{ s, @sizeOf(usize), @sizeOf(usize) }), "F" ** @sizeOf(usize) },
11351132
.{ fmt.comptimePrint("{d} DSP@ 2 NUMBER DROP EMIT ", .{mem.readInt(u16, "65", .little)}), "A" },
11361133
.{ "64 >R RSP@ 1 TELL RDROP ", "@" },
11371134
.{ "64 DSP@ RSP@ SWAP C@C! RSP@ 1 TELL ", "@" },
@@ -1161,12 +1158,9 @@ test forth {
11611158
.{ preamble ++ "F_IMMED F_HIDDEN .S ", "32 128 " },
11621159
.{ preamble ++ ": CFA@ WORD FIND >CFA @ ; CFA@ >DFA DOCOL = . ", "-1 " },
11631160
.{ preamble ++ "3 4 5 WITHIN . ", "0 " },
1164-
.{ preamble ++ fmt.comptimePrint(": GETPPID {d} SYSCALL0 ; GETPPID . ", .{@intFromEnum(syscalls.X64.getppid)}), p },
1165-
.{ preamble ++ "ARGC . ", "2 " },
1166-
.{ preamble ++ "ENVIRON @ DUP STRLEN TELL ", mem.sliceTo(os.environ[0], 0) },
1167-
.{ preamble ++ "SEE >DFA ", ": >DFA >CFA 8+ EXIT ;\n" },
1161+
.{ preamble ++ "SEE >DFA ", fmt.comptimePrint(": >DFA >CFA {d}+ EXIT ;\n", .{@sizeOf(usize)}) },
11681162
.{ preamble ++ "SEE HIDE ", ": HIDE WORD FIND HIDDEN ;\n" },
1169-
.{ preamble ++ "SEE QUIT ", ": QUIT R0 RSP! INTERPRET BRANCH ( -16 ) ;\n" },
1163+
.{ preamble ++ "SEE QUIT ", fmt.comptimePrint(": QUIT R0 RSP! INTERPRET BRANCH ( -{d} ) ;\n", .{2 * @sizeOf(usize)}) },
11701164
.{
11711165
preamble ++
11721166
\\: FOO THROW ;
@@ -1179,4 +1173,13 @@ test forth {
11791173

11801174
inline for (tests) |t|
11811175
try forth(t.@"0", t.@"1");
1176+
1177+
if (!arch.isWasm()) {
1178+
const p = try fmt.allocPrintSentinel(testing.allocator, "{d} ", .{os.linux.getppid()}, 0);
1179+
defer testing.allocator.free(p);
1180+
1181+
try forth(preamble ++ ": ARGC (ARGC) @ ; ARGC . ", "4 ");
1182+
try forth(preamble ++ ": ARGC (ARGC) @ ; : ENVIRON ARGC 2 + CELLS (ARGC) + ; ENVIRON @ DUP STRLEN TELL ", mem.sliceTo(os.environ[0], 0));
1183+
try forth(preamble ++ fmt.comptimePrint(": GETPPID {d} SYSCALL0 ; GETPPID . ", .{@intFromEnum(syscalls.X64.getppid)}), p);
1184+
}
11821185
}

0 commit comments

Comments
 (0)