Skip to content

Commit

Permalink
Synchronize js-api tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed May 8, 2020
1 parent b34e34e commit cc2d59b
Show file tree
Hide file tree
Showing 37 changed files with 1,075 additions and 334 deletions.
22 changes: 22 additions & 0 deletions test/js-api/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function assert_Instance(instance, expected_exports) {

assert_equals(Object.getPrototypeOf(exports), null, "exports prototype");
assert_false(Object.isExtensible(exports), "extensible exports");
assert_array_equals(Object.keys(exports), Object.keys(expected_exports), "matching export keys");
for (const [key, expected] of Object.entries(expected_exports)) {
const property = Object.getOwnPropertyDescriptor(exports, key);
assert_equals(typeof property, "object", `${key} should be present`);
Expand Down Expand Up @@ -71,3 +72,24 @@ function assert_Instance(instance, expected_exports) {
}
}
}

function assert_WebAssemblyInstantiatedSource(actual, expected_exports={}) {
assert_equals(Object.getPrototypeOf(actual), Object.prototype,
"Prototype");
assert_true(Object.isExtensible(actual), "Extensibility");

const module = Object.getOwnPropertyDescriptor(actual, "module");
assert_equals(typeof module, "object", "module: type of descriptor");
assert_true(module.writable, "module: writable");
assert_true(module.enumerable, "module: enumerable");
assert_true(module.configurable, "module: configurable");
assert_equals(Object.getPrototypeOf(module.value), WebAssembly.Module.prototype,
"module: prototype");

const instance = Object.getOwnPropertyDescriptor(actual, "instance");
assert_equals(typeof instance, "object", "instance: type of descriptor");
assert_true(instance.writable, "instance: writable");
assert_true(instance.enumerable, "instance: enumerable");
assert_true(instance.configurable, "instance: configurable");
assert_Instance(instance.value, expected_exports);
}
23 changes: 13 additions & 10 deletions test/js-api/bad-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* `t` should be a function that takes at least three arguments:
*
* - the name of the test;
* - the expected error (to be passed to `assert_throws` or similar);
* - the expected error (to be passed to `assert_throws_js`);
* - a function that takes a `WasmModuleBuilder` and initializes it;
* - (optionally) an options object.
*
Expand All @@ -13,7 +13,7 @@
function test_bad_imports(t) {
for (const value of [null, true, "", Symbol(), 1, 0.1, NaN]) {
t(`Non-object imports argument: ${format_value(value)}`,
new TypeError(),
TypeError,
builder => {},
value);
}
Expand All @@ -23,30 +23,30 @@ function test_bad_imports(t) {
"module": value,
};
t(`Non-object module: ${format_value(value)}`,
new TypeError(),
TypeError,
builder => {
builder.addImport("module", "fn", kSig_v_v);
},
imports);
}

t(`Missing imports argument`,
new TypeError(),
TypeError,
builder => {
builder.addImport("module", "fn", kSig_v_v);
});

for (const [value, name] of [[undefined, "undefined"], [{}, "empty object"], [{ "module\0": null }, "wrong property"]]) {
t(`Imports argument with missing property: ${name}`,
new TypeError(),
TypeError,
builder => {
builder.addImport("module", "fn", kSig_v_v);
},
value);
}

t(`Importing an i64 global`,
new WebAssembly.LinkError(),
WebAssembly.LinkError,
builder => {
builder.addImportedGlobal("module", "global", kWasmI64);
},
Expand All @@ -58,7 +58,7 @@ function test_bad_imports(t) {

for (const value of [undefined, null, true, "", Symbol(), 1, 0.1, NaN, {}]) {
t(`Importing a function with an incorrectly-typed value: ${format_value(value)}`,
new WebAssembly.LinkError(),
WebAssembly.LinkError,
builder => {
builder.addImport("module", "fn", kSig_v_v);
},
Expand All @@ -79,11 +79,12 @@ function test_bad_imports(t) {
[WebAssembly.Global, "WebAssembly.Global"],
[WebAssembly.Global.prototype, "WebAssembly.Global.prototype"],
[Object.create(WebAssembly.Global.prototype), "Object.create(WebAssembly.Global.prototype)"],
[new WebAssembly.Global({value: "f32"}), "WebAssembly.Global object (wrong value type)"],
];

for (const [value, name = format_value(value)] of nonGlobals) {
t(`Importing a global with an incorrectly-typed value: ${name}`,
new WebAssembly.LinkError(),
WebAssembly.LinkError,
builder => {
builder.addImportedGlobal("module", "global", kWasmI32);
},
Expand All @@ -107,11 +108,12 @@ function test_bad_imports(t) {
[WebAssembly.Memory, "WebAssembly.Memory"],
[WebAssembly.Memory.prototype, "WebAssembly.Memory.prototype"],
[Object.create(WebAssembly.Memory.prototype), "Object.create(WebAssembly.Memory.prototype)"],
[new WebAssembly.Memory({"initial": 256}), "WebAssembly.Memory object (too large)"],
];

for (const [value, name = format_value(value)] of nonMemories) {
t(`Importing memory with an incorrectly-typed value: ${name}`,
new WebAssembly.LinkError(),
WebAssembly.LinkError,
builder => {
builder.addImportedMemory("module", "memory", 0, 128);
},
Expand All @@ -135,11 +137,12 @@ function test_bad_imports(t) {
[WebAssembly.Table, "WebAssembly.Table"],
[WebAssembly.Table.prototype, "WebAssembly.Table.prototype"],
[Object.create(WebAssembly.Table.prototype), "Object.create(WebAssembly.Table.prototype)"],
[new WebAssembly.Table({"element": "anyfunc", "initial": 256}), "WebAssembly.Table object (too large)"],
];

for (const [value, name = format_value(value)] of nonTables) {
t(`Importing table with an incorrectly-typed value: ${name}`,
new WebAssembly.LinkError(),
WebAssembly.LinkError,
builder => {
builder.addImportedTable("module", "table", 0, 128);
},
Expand Down
13 changes: 9 additions & 4 deletions test/js-api/constructor/compile.any.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=jsshell
// META: global=window,dedicatedworker,jsshell
// META: script=/wasm/jsapi/wasm-module-builder.js

function assert_Module(module) {
Expand All @@ -13,7 +13,7 @@ setup(() => {
});

promise_test(t => {
return promise_rejects(t, new TypeError(), WebAssembly.compile());
return promise_rejects_js(t, TypeError, WebAssembly.compile());
}, "Missing argument");

promise_test(t => {
Expand All @@ -30,7 +30,7 @@ promise_test(t => {
Array.from(emptyModuleBinary),
];
return Promise.all(invalidArguments.map(argument => {
return promise_rejects(t, new TypeError(), WebAssembly.compile(argument),
return promise_rejects_js(t, TypeError, WebAssembly.compile(argument),
`compile(${format_value(argument)})`);
}));
}, "Invalid arguments");
Expand Down Expand Up @@ -60,7 +60,12 @@ test(() => {

promise_test(t => {
const buffer = new Uint8Array();
return promise_rejects(t, new WebAssembly.CompileError(), WebAssembly.compile(buffer));
return promise_rejects_js(t, WebAssembly.CompileError, WebAssembly.compile(buffer));
}, "Empty buffer");

promise_test(t => {
const buffer = new Uint8Array(Array.from(emptyModuleBinary).concat([0, 0]));
return promise_rejects_js(t, WebAssembly.CompileError, WebAssembly.compile(buffer));
}, "Invalid code");

promise_test(() => {
Expand Down
6 changes: 3 additions & 3 deletions test/js-api/constructor/instantiate-bad-imports.any.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=jsshell
// META: global=window,dedicatedworker,jsshell
// META: script=/wasm/jsapi/wasm-module-builder.js
// META: script=/wasm/jsapi/bad-imports.js

Expand All @@ -8,7 +8,7 @@ test_bad_imports((name, error, build, ...arguments) => {
build(builder);
const buffer = builder.toBuffer();
const module = new WebAssembly.Module(buffer);
return promise_rejects(t, error, WebAssembly.instantiate(module, ...arguments));
return promise_rejects_js(t, error, WebAssembly.instantiate(module, ...arguments));
}, `WebAssembly.instantiate(module): ${name}`);
});

Expand All @@ -17,6 +17,6 @@ test_bad_imports((name, error, build, ...arguments) => {
const builder = new WasmModuleBuilder();
build(builder);
const buffer = builder.toBuffer();
return promise_rejects(t, error, WebAssembly.instantiate(buffer, ...arguments));
return promise_rejects_js(t, error, WebAssembly.instantiate(buffer, ...arguments));
}, `WebAssembly.instantiate(buffer): ${name}`);
});
34 changes: 9 additions & 25 deletions test/js-api/constructor/instantiate.any.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
// META: global=jsshell
// META: global=window,dedicatedworker,jsshell
// META: script=/wasm/jsapi/wasm-module-builder.js
// META: script=/wasm/jsapi/assertions.js
// META: script=/wasm/jsapi/instanceTestFactory.js

function assert_WebAssemblyInstantiatedSource(actual, expected_exports={}) {
assert_equals(Object.getPrototypeOf(actual), Object.prototype,
"Prototype");
assert_true(Object.isExtensible(actual), "Extensibility");

const module = Object.getOwnPropertyDescriptor(actual, "module");
assert_equals(typeof module, "object", "module: type of descriptor");
assert_true(module.writable, "module: writable");
assert_true(module.enumerable, "module: enumerable");
assert_true(module.configurable, "module: configurable");
assert_equals(Object.getPrototypeOf(module.value), WebAssembly.Module.prototype,
"module: prototype");

const instance = Object.getOwnPropertyDescriptor(actual, "instance");
assert_equals(typeof instance, "object", "instance: type of descriptor");
assert_true(instance.writable, "instance: writable");
assert_true(instance.enumerable, "instance: enumerable");
assert_true(instance.configurable, "instance: configurable");
assert_Instance(instance.value, expected_exports);
}

let emptyModuleBinary;
setup(() => {
emptyModuleBinary = new WasmModuleBuilder().toBuffer();
});

promise_test(t => {
return promise_rejects(t, new TypeError(), WebAssembly.instantiate());
return promise_rejects_js(t, TypeError, WebAssembly.instantiate());
}, "Missing arguments");

promise_test(() => {
Expand Down Expand Up @@ -66,7 +45,7 @@ promise_test(t => {
Array.from(emptyModuleBinary),
];
return Promise.all(invalidArguments.map(argument => {
return promise_rejects(t, new TypeError(), WebAssembly.instantiate(argument),
return promise_rejects_js(t, TypeError, WebAssembly.instantiate(argument),
`instantiate(${format_value(argument)})`);
}));
}, "Invalid arguments");
Expand Down Expand Up @@ -156,7 +135,12 @@ promise_test(() => {

promise_test(t => {
const buffer = new Uint8Array();
return promise_rejects(t, new WebAssembly.CompileError(), WebAssembly.instantiate(buffer));
return promise_rejects_js(t, WebAssembly.CompileError, WebAssembly.instantiate(buffer));
}, "Empty buffer");

promise_test(t => {
const buffer = new Uint8Array(Array.from(emptyModuleBinary).concat([0, 0]));
return promise_rejects_js(t, WebAssembly.CompileError, WebAssembly.instantiate(buffer));
}, "Invalid code");

promise_test(() => {
Expand Down
Loading

0 comments on commit cc2d59b

Please sign in to comment.