summaryrefslogtreecommitdiff
path: root/test/gen-spec-prefix.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/gen-spec-prefix.js')
-rw-r--r--test/gen-spec-prefix.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/test/gen-spec-prefix.js b/test/gen-spec-prefix.js
new file mode 100644
index 00000000..99cfab7a
--- /dev/null
+++ b/test/gen-spec-prefix.js
@@ -0,0 +1,93 @@
+/* Copied from *
+ * https://github.com/WebAssembly/spec/blob/master/interpreter/host/js.ml */
+'use strict';
+
+let soft_validate = true;
+
+let spectest = {
+ print: print || ((...xs) => console.log(...xs)),
+ global: 666,
+ table: new WebAssembly.Table({initial: 10, maximum: 20, element: 'anyfunc'}),
+ memory: new WebAssembly.Memory({initial: 1, maximum: 2}),};
+
+let registry = {spectest};
+let $$;
+
+function register(name, instance) {
+ registry[name] = instance.exports;
+}
+
+function module(bytes) {
+ let buffer = new ArrayBuffer(bytes.length);
+ let view = new Uint8Array(buffer);
+ for (let i = 0; i < bytes.length; ++i) {
+ view[i] = bytes.charCodeAt(i);
+ }
+ return new WebAssembly.Module(buffer);
+}
+
+function instance(bytes, imports = registry) {
+ return new WebAssembly.Instance(module(bytes), imports);
+}
+
+function assert_malformed(bytes) {
+ try { module(bytes) } catch (e) {
+ if (e instanceof WebAssembly.CompileError) return;
+ }
+ throw new Error("Wasm decoding failure expected");
+}
+
+function assert_invalid(bytes) {
+ try { module(bytes) } catch (e) {
+ if (e instanceof WebAssembly.CompileError) return;
+ }
+ throw new Error("Wasm validation failure expected");
+}
+
+function assert_soft_invalid(bytes) {
+ try { module(bytes) } catch (e) {
+ if (e instanceof WebAssembly.CompileError) return;
+ throw new Error("Wasm validation failure expected");
+ }
+ if (soft_validate)
+ throw new Error("Wasm validation failure expected");
+}
+
+function assert_unlinkable(bytes) {
+ let mod = module(bytes);
+ try { new WebAssembly.Instance(mod, registry) } catch (e) {
+ if (e instanceof TypeError) return;
+ }
+ throw new Error("Wasm linking failure expected");
+}
+
+function assert_uninstantiable(bytes) {
+ let mod = module(bytes);
+ try { new WebAssembly.Instance(mod, registry) } catch (e) {
+ if (e instanceof WebAssembly.RuntimeError) return;
+ }
+ throw new Error("Wasm trap expected");
+}
+
+function assert_trap(action) {
+ try { action() } catch (e) {
+ if (e instanceof WebAssembly.RuntimeError) return;
+ }
+ throw new Error("Wasm trap expected");
+}
+
+function assert_return(action, expected) {
+ let actual = action();
+ if (!Object.is(actual, expected)) {
+ throw new Error("Wasm return value " + expected + " expected, got " + actual);
+ };
+}
+
+function assert_return_nan(action) {
+ let actual = action();
+ if (!Number.isNaN(actual)) {
+ throw new Error("Wasm return value NaN expected, got " + actual);
+ };
+}
+
+let f32 = Math.fround;