Skip to content

Commit c840c58

Browse files
authored
[interpreter] Add wast locations to JS-converted test cases (#1919)
1 parent 454c446 commit c840c58

File tree

3 files changed

+62
-54
lines changed

3 files changed

+62
-54
lines changed

interpreter/script/js.ml

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function register(name, instance) {
5353
registry[name] = instance.exports;
5454
}
5555

56-
function module(bytes, valid = true) {
56+
function module(bytes, loc, valid = true) {
5757
let buffer = new ArrayBuffer(bytes.length);
5858
let view = new Uint8Array(buffer);
5959
for (let i = 0; i < bytes.length; ++i) {
@@ -92,8 +92,8 @@ function run(action) {
9292
action();
9393
}
9494

95-
function assert_malformed(bytes) {
96-
try { module(bytes, false) } catch (e) {
95+
function assert_malformed(bytes, loc) {
96+
try { module(bytes, loc, false) } catch (e) {
9797
if (e instanceof WebAssembly.CompileError) return;
9898
}
9999
throw new Error("Wasm decoding failure expected");
@@ -103,8 +103,8 @@ function assert_malformed_custom(bytes) {
103103
return;
104104
}
105105

106-
function assert_invalid(bytes) {
107-
try { module(bytes, false) } catch (e) {
106+
function assert_invalid(bytes, loc) {
107+
try { module(bytes, loc, false) } catch (e) {
108108
if (e instanceof WebAssembly.CompileError) return;
109109
}
110110
throw new Error("Wasm validation failure expected");
@@ -128,7 +128,7 @@ function assert_uninstantiable(mod) {
128128
throw new Error("Wasm trap expected");
129129
}
130130

131-
function assert_trap(action) {
131+
function assert_trap(action, loc) {
132132
try { action() } catch (e) {
133133
if (e instanceof WebAssembly.RuntimeError) return;
134134
}
@@ -150,7 +150,7 @@ function assert_exhaustion(action) {
150150
throw new Error("Wasm resource exhaustion expected");
151151
}
152152

153-
function assert_return(action, ...expected) {
153+
function assert_return(action, loc, ...expected) {
154154
let actual = action();
155155
if (actual === undefined) {
156156
actual = [];
@@ -681,8 +681,12 @@ let of_string_with iter add_char s =
681681
Buffer.contents buf
682682

683683
let of_bytes = of_string_with String.iter add_hex_char
684+
let of_string = of_string_with String.iter add_char
684685
let of_name = of_string_with List.iter add_unicode_char
685686

687+
let of_loc at =
688+
of_string (Filename.basename at.left.file ^ ":" ^ string_of_int at.left.line)
689+
686690
let of_float z =
687691
match string_of_float z with
688692
| "nan" -> "NaN"
@@ -756,7 +760,7 @@ let rec of_definition def =
756760
let of_wrapper env x_opt name wrap_action wrap_assertion at =
757761
let x = of_inst_opt env x_opt in
758762
let bs = wrap name wrap_action wrap_assertion at in
759-
"call(instance(module(" ^ of_bytes bs ^ "), " ^
763+
"call(instance(module(" ^ of_bytes bs ^ ", \"wrapper\"), " ^
760764
"exports(" ^ x ^ ")), " ^ " \"run\", [])"
761765

762766
let of_action env act =
@@ -782,45 +786,46 @@ let of_action env act =
782786
| _ -> None
783787
)
784788

785-
let of_assertion' env act name args wrapper_opt =
789+
let of_assertion' env act loc name args wrapper_opt =
786790
let act_js, act_wrapper_opt = of_action env act in
787-
let js = name ^ "(() => " ^ act_js ^ String.concat ", " ("" :: args) ^ ")" in
791+
let js = name ^ "(() => " ^ act_js ^ loc ^ String.concat ", " ("" :: args) ^ ")" in
788792
match act_wrapper_opt with
789793
| None -> js ^ ";"
790794
| Some (act_wrapper, out) ->
791795
let run_name, wrapper =
792796
match wrapper_opt with
793797
| None -> name, run
794798
| Some wrapper -> "run", wrapper
795-
in run_name ^ "(() => " ^ act_wrapper (wrapper out) act.at ^ "); // " ^ js
799+
in run_name ^ "(() => " ^ act_wrapper (wrapper out) act.at ^ loc ^ "); // " ^ js
796800

797801
let of_assertion env ass =
802+
let loc = of_loc ass.at in
798803
match ass.it with
799804
| AssertMalformed (def, _) ->
800-
"assert_malformed(" ^ of_definition def ^ ");"
805+
"assert_malformed(" ^ of_definition def ^ ", " ^ loc ^ ");"
801806
| AssertMalformedCustom (def, _) ->
802-
"assert_malformed_custom(" ^ of_definition def ^ ");"
807+
"assert_malformed_custom(" ^ of_definition def ^ ", " ^ loc ^ ");"
803808
| AssertInvalid (def, _) ->
804-
"assert_invalid(" ^ of_definition def ^ ");"
809+
"assert_invalid(" ^ of_definition def ^ ", " ^ loc ^ ");"
805810
| AssertInvalidCustom (def, _) ->
806-
"assert_invalid_custom(" ^ of_definition def ^ ");"
811+
"assert_invalid_custom(" ^ of_definition def ^ ", " ^ loc ^ ");"
807812
| AssertUnlinkable (x_opt, _) ->
808813
"assert_unlinkable(" ^ of_mod_opt env x_opt ^ ");"
809814
| AssertUninstantiable (x_opt, _) ->
810815
"assert_uninstantiable(" ^ of_mod_opt env x_opt ^ ");"
811816
| AssertReturn (act, ress) ->
812-
of_assertion' env act "assert_return" (List.map of_result ress)
817+
of_assertion' env act loc "assert_return" (List.map of_result ress)
813818
(Some (assert_return ress))
814819
| AssertTrap (act, _) ->
815-
of_assertion' env act "assert_trap" [] None
820+
of_assertion' env act loc "assert_trap" [] None
816821
| AssertExhaustion (act, _) ->
817-
of_assertion' env act "assert_exhaustion" [] None
822+
of_assertion' env act loc "assert_exhaustion" [] None
818823
| AssertException act ->
819-
of_assertion' env act "assert_exception" [] None
824+
of_assertion' env act loc "assert_exception" [] None
820825

821826
let of_command env cmd =
822-
"\n// " ^ Filename.basename cmd.at.left.file ^
823-
":" ^ string_of_int cmd.at.left.line ^ "\n" ^
827+
let loc = of_loc cmd.at in
828+
"\n// " ^ loc ^ "\n" ^
824829
match cmd.it with
825830
| Module (x_opt, def) ->
826831
let rec unquote def =
@@ -830,7 +835,7 @@ let of_command env cmd =
830835
| Quoted (_, s) ->
831836
unquote (snd (Parse.Module.parse_string ~offset:s.at s.it))
832837
in bind_mod env x_opt (unquote def);
833-
"let " ^ current_mod env ^ " = module(" ^ of_definition def ^ ");\n" ^
838+
"let " ^ current_mod env ^ " = module(" ^ of_definition def ^ ", " ^ loc ^ ");\n" ^
834839
(if x_opt = None then "" else
835840
"let " ^ of_mod_opt env x_opt ^ " = " ^ current_mod env ^ ";\n")
836841
| Instance (x1_opt, x2_opt) ->
@@ -842,7 +847,7 @@ let of_command env cmd =
842847
| Register (name, x_opt) ->
843848
"register(" ^ of_name name ^ ", " ^ of_inst_opt env x_opt ^ ")\n"
844849
| Action act ->
845-
of_assertion' env act "run" [] None ^ "\n"
850+
of_assertion' env act loc "run" [] None ^ "\n"
846851
| Assertion ass ->
847852
of_assertion env ass ^ "\n"
848853
| Meta _ -> assert false

test/harness/async_index.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ function binary(bytes) {
150150
/**
151151
* Returns a compiled module, or throws if there was an error at compilation.
152152
*/
153-
function module(bytes, valid = true) {
154-
const test = valid
155-
? "Test that WebAssembly compilation succeeds"
156-
: "Test that WebAssembly compilation fails";
153+
function module(bytes, source, valid = true) {
154+
const test = `${ valid ? "Test that WebAssembly compilation succeeds" :
155+
"Test that WebAssembly compilation fails"} (${source})`;
156+
157157
const loc = new Error().stack.toString().replace("Error", "");
158158
let buffer = binary(bytes);
159159
let validated = WebAssembly.validate(buffer);
@@ -167,6 +167,7 @@ function module(bytes, valid = true) {
167167
uniqueTest(_ => {
168168
assert_true(valid, loc);
169169
}, test);
170+
module.source = source;
170171
return module;
171172
},
172173
error => {
@@ -181,26 +182,27 @@ function module(bytes, valid = true) {
181182
return chain;
182183
}
183184

184-
function assert_invalid(bytes) {
185-
module(bytes, EXPECT_INVALID);
185+
function assert_invalid(bytes, source) {
186+
module(bytes, source, EXPECT_INVALID);
186187
}
187188

188189
const assert_malformed = assert_invalid;
189190

190-
function assert_invalid_custom(bytes) {
191-
module(bytes);
191+
function assert_invalid_custom(bytes, source) {
192+
module(bytes, source);
192193
}
193194

194195
const assert_malformed_custom = assert_invalid_custom;
195196

196197
function instance(module, imports, valid = true) {
197-
const test = valid
198+
let test = valid
198199
? "Test that WebAssembly instantiation succeeds"
199200
: "Test that WebAssembly instantiation fails";
200201
const loc = new Error().stack.toString().replace("Error", "");
201202
chain = Promise.all([module, imports, chain])
202203
.then(values => {
203204
let imports = values[1] ? values[1] : registry;
205+
test += ` (${values[0].source})`;
204206
return WebAssembly.instantiate(values[0], imports);
205207
})
206208
.then(
@@ -235,8 +237,8 @@ function call(instance, name, args) {
235237
});
236238
}
237239

238-
function run(action) {
239-
const test = "Run a WebAssembly test without special assertions";
240+
function run(action, source) {
241+
const test = `Run a WebAssembly test without special assertions (${source})`;
240242
const loc = new Error().stack.toString().replace("Error", "");
241243
chain = Promise.all([chain, action()])
242244
.then(
@@ -256,8 +258,8 @@ function run(action) {
256258
.catch(_ => {});
257259
}
258260

259-
function assert_trap(action) {
260-
const test = "Test that a WebAssembly code traps";
261+
function assert_trap(action, source) {
262+
const test = `Test that a WebAssembly code traps (${source})`;
261263
const loc = new Error().stack.toString().replace("Error", "");
262264
chain = Promise.all([chain, action()])
263265
.then(
@@ -279,8 +281,8 @@ function assert_trap(action) {
279281
.catch(_ => {});
280282
}
281283

282-
function assert_return(action, ...expected) {
283-
const test = "Test that a WebAssembly code returns a specific result";
284+
function assert_return(action, source, ...expected) {
285+
const test = `Test that a WebAssembly code returns a specific result (${source})`;
284286
const loc = new Error().stack.toString().replace("Error", "");
285287
chain = Promise.all([action(), chain])
286288
.then(

test/harness/sync_index.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function binary(bytes) {
150150
/**
151151
* Returns a compiled module, or throws if there was an error at compilation.
152152
*/
153-
function module(bytes, valid = true) {
153+
function module(bytes, source, valid = true) {
154154
let buffer = binary(bytes);
155155
let validated;
156156

@@ -176,6 +176,7 @@ function module(bytes, valid = true) {
176176
let module;
177177
try {
178178
module = new WebAssembly.Module(buffer);
179+
module.source = source;
179180
} catch(e) {
180181
if (valid)
181182
throw new Error('WebAssembly.Module ctor unexpectedly throws ${typeof e}: ${e}${e.stack}');
@@ -189,32 +190,32 @@ function uniqueTest(func, desc) {
189190
test(func, testNum() + desc);
190191
}
191192

192-
function assert_invalid(bytes) {
193+
function assert_invalid(bytes, source) {
193194
uniqueTest(() => {
194195
try {
195-
module(bytes, /* valid */ false);
196+
module(bytes, source, /* valid */ false);
196197
throw new Error('did not fail');
197198
} catch(e) {
198199
assert_true(e instanceof WebAssembly.CompileError, "expected invalid failure:");
199200
}
200-
}, "A wast module that should be invalid or malformed.");
201+
}, `A wast module that should be invalid or malformed. (${source})`);
201202
}
202203

203204
const assert_malformed = assert_invalid;
204205

205-
function assert_invalid_custom(bytes) {
206+
function assert_invalid_custom(bytes, source) {
206207
uniqueTest(() => {
207208
try {
208-
module(bytes, /* valid */ true);
209+
module(bytes, source, /* valid */ true);
209210
} catch(e) {
210211
throw new Error('failed on custom section error');
211212
}
212-
}, "A wast module that should have an invalid or malformed custom section.");
213+
}, `A wast module that should have an invalid or malformed custom section. (${source})`);
213214
}
214215

215216
const assert_malformed_custom = assert_invalid_custom;
216217

217-
function instance(mod, imports = registry, valid = true) {
218+
function instance(module, imports = registry, valid = true) {
218219
if (imports instanceof Result) {
219220
if (imports.isError())
220221
return imports;
@@ -225,7 +226,7 @@ function instance(mod, imports = registry, valid = true) {
225226

226227
let i;
227228
try {
228-
i = new WebAssembly.Instance(mod, imports);
229+
i = new WebAssembly.Instance(module, imports);
229230
} catch(e) {
230231
err = e;
231232
}
@@ -234,7 +235,7 @@ function instance(mod, imports = registry, valid = true) {
234235
uniqueTest(() => {
235236
let instantiated = err === null;
236237
assert_true(instantiated, err);
237-
}, "module successfully instantiated");
238+
}, `module successfully instantiated (${module.source})`);
238239
}
239240

240241
return err !== null ? ErrorResult(err) : ValueResult(i);
@@ -285,15 +286,15 @@ function exports(instance) {
285286
return ValueResult({ module: instance.value.exports, spectest: registry.spectest });
286287
}
287288

288-
function run(action) {
289+
function run(action, source) {
289290
let result = action();
290291

291292
_assert(result instanceof Result);
292293

293294
uniqueTest(() => {
294295
if (result.isError())
295296
throw result.value;
296-
}, "A wast test that runs without any special assertion.");
297+
}, `A wast test that runs without any special assertion. (${source})`);
297298
}
298299

299300
function assert_unlinkable(bytes) {
@@ -324,7 +325,7 @@ function assert_uninstantiable(bytes) {
324325
}, "A wast module that is uninstantiable.");
325326
}
326327

327-
function assert_trap(action) {
328+
function assert_trap(action, source) {
328329
let result = action();
329330

330331
_assert(result instanceof Result);
@@ -335,7 +336,7 @@ function assert_trap(action) {
335336
let e = result.value;
336337
assert_true(e instanceof WebAssembly.RuntimeError, `expected runtime error, observed ${e}:`);
337338
}
338-
}, "A wast module that must trap at runtime.");
339+
}, `A wast module that must trap at runtime. (${source})`);
339340
}
340341

341342
let StackOverflow;
@@ -355,7 +356,7 @@ function assert_exhaustion(action) {
355356
}, "A wast module that must exhaust the stack space.");
356357
}
357358

358-
function assert_return(action, ...expected) {
359+
function assert_return(action, source, ...expected) {
359360
let result = action();
360361
_assert(result instanceof Result);
361362

@@ -408,7 +409,7 @@ function assert_return(action, ...expected) {
408409
assert_equals(actual[i], expected[i]);
409410
}
410411
}
411-
}, "A wast module that must return a particular value.");
412+
}, `A wast module that must return a particular value. (${source})`);
412413
}
413414

414415
function assert_return_nan(action) {

0 commit comments

Comments
 (0)