Skip to content

Commit bc9a2cd

Browse files
committed
Wasmtest: Fix testing framework to import core::error
1 parent 1fbcf65 commit bc9a2cd

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

lib-backend-wasm/src/gc/cheney/wasmtest.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22
use wasm_test_harness::*;
33

44
pub fn wasmtest<C: TestContext>(c: &mut C) {
5-
c.add_test("no roots", |code_builder, wasm_module, t| {
5+
c.add_test("no roots", |code_builder, wasm_module, error_func, t| {
66
/*
77
In this test we will create a Cheney with the initial size (1MiB usuable size),
88
and add 32768 copies of 28-byte structs (which will exactly hit the maximum size (including 4-byte tag)).
@@ -27,6 +27,7 @@ pub fn wasmtest<C: TestContext>(c: &mut C) {
2727
mem,
2828
0,
2929
MEM_INITIAL_HEAP_SIZE,
30+
error_func,
3031
wasm_module,
3132
);
3233

@@ -135,7 +136,7 @@ pub fn wasmtest<C: TestContext>(c: &mut C) {
135136
t.i32_assert_eq(&mut scratch, expr_builder);
136137
});
137138

138-
c.add_test("all roots", |code_builder, wasm_module, t| {
139+
c.add_test("all roots", |code_builder, wasm_module, error_func, t| {
139140
/*
140141
In this test we will create a Cheney with the initial size (1MiB usuable size),
141142
and add 32768 copies of 28-byte structs (which will exactly hit the maximum size (including 4-byte tag)).
@@ -160,6 +161,7 @@ pub fn wasmtest<C: TestContext>(c: &mut C) {
160161
mem,
161162
0,
162163
MEM_INITIAL_HEAP_SIZE,
164+
error_func,
163165
wasm_module,
164166
);
165167

static-wasmtest/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ async function run() {
2727
let test_index = 0;
2828
let num_asserts = 0;
2929
for (const binary of binaries) {
30-
const imports = { platform: {
31-
assert_fail: (l, r, n) => { alert("Assertion " + (n+1) + " failed on test " + (test_index+1) + ": " + l + "==" + r); throw ""; },
32-
test_fail: () => { alert("Test failed on test " + (test_index+1) + "."); throw ""; },
33-
} };
30+
const imports = {
31+
platform: {
32+
assert_fail: (l, r, n) => { alert("Assertion " + (n+1) + " failed on test " + (test_index+1) + ": " + l + "==" + r); throw ""; },
33+
test_fail: () => { alert("Test failed on test " + (test_index+1) + "."); throw ""; },
34+
}, core: {
35+
error: (e, a, i, l, c) => { alert("Error code " + e + " raised"); throw ""; },
36+
test_fail: () => { alert("Test failed on test " + (test_index+1) + "."); throw ""; },
37+
}
38+
};
3439
const instance = await load_binary(binary, imports);
3540
const result = instance.main();
3641
++test_index;

wasm-test-harness/src/lib.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C" {
2121
}
2222

2323
pub trait TestContext {
24-
fn add_test<F: FnOnce(&mut CodeBuilder, &mut WasmModule, &NormalTester)>(
24+
fn add_test<F: FnOnce(&mut CodeBuilder, &mut WasmModule, wasmgen::FuncIdx, &NormalTester)>(
2525
&mut self,
2626
name_: &str,
2727
f: F,
@@ -80,7 +80,7 @@ impl<A: Fn(Box<[u8]>) -> ()> TestContext for NormalContext<A> {
8080
`f` should emit code that has net wasm stack [] -> [], even though the CodeBuilder might have a different signature. This is because the test harness might return different bookkeeping information.
8181
It should not emit the end() instruction to end the function.
8282
*/
83-
fn add_test<F: FnOnce(&mut CodeBuilder, &mut WasmModule, &NormalTester)>(
83+
fn add_test<F: FnOnce(&mut CodeBuilder, &mut WasmModule, wasmgen::FuncIdx, &NormalTester)>(
8484
&mut self,
8585
name_: &str,
8686
f: F,
@@ -101,6 +101,21 @@ impl<A: Fn(Box<[u8]>) -> ()> TestContext for NormalContext<A> {
101101
"test_fail".to_string(),
102102
&FuncType::new(Box::new([]), Box::new([])),
103103
);
104+
// generate the error function
105+
let error_func: wasmgen::FuncIdx = wasm_builder.import_func(
106+
"core".to_string(),
107+
"error".to_string(),
108+
&wasmgen::FuncType::new(
109+
Box::new([
110+
wasmgen::ValType::I32,
111+
wasmgen::ValType::I32,
112+
wasmgen::ValType::I32,
113+
wasmgen::ValType::I32,
114+
wasmgen::ValType::I32,
115+
]),
116+
Box::new([]),
117+
),
118+
);
104119
let mut wasm_module = wasm_builder.build();
105120
let globalidx_assert_count = wasm_module.add_i32_global(Mut::Var, 0);
106121

@@ -115,7 +130,7 @@ impl<A: Fn(Box<[u8]>) -> ()> TestContext for NormalContext<A> {
115130
};
116131

117132
// net wasm stack: [] -> []
118-
f(&mut code_builder, &mut wasm_module, &tester);
133+
f(&mut code_builder, &mut wasm_module, error_func, &tester);
119134

120135
// net wasm stack: [] -> [ret(i32)]
121136
{

0 commit comments

Comments
 (0)