2016-09-21 10:40:16 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// Flags: --expose-wasm
|
|
|
|
|
2016-10-13 16:17:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-17 11:39:04 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
|
2016-09-21 10:40:16 +00:00
|
|
|
// Basic tests.
|
|
|
|
|
2019-03-01 14:26:16 +00:00
|
|
|
const outOfUint32RangeValue = 1e12;
|
|
|
|
const kV8MaxWasmTableSize = 10000000;
|
2016-09-21 10:40:16 +00:00
|
|
|
|
2017-07-10 11:18:45 +00:00
|
|
|
function assertTableIsValid(table, length) {
|
2016-09-26 13:07:44 +00:00
|
|
|
assertSame(WebAssembly.Table.prototype, table.__proto__);
|
|
|
|
assertSame(WebAssembly.Table, table.constructor);
|
|
|
|
assertTrue(table instanceof Object);
|
|
|
|
assertTrue(table instanceof WebAssembly.Table);
|
2017-07-10 11:18:45 +00:00
|
|
|
assertEquals(length, table.length);
|
2016-09-26 13:07:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 10:40:16 +00:00
|
|
|
(function TestConstructor() {
|
|
|
|
assertTrue(WebAssembly.Table instanceof Function);
|
|
|
|
assertSame(WebAssembly.Table, WebAssembly.Table.prototype.constructor);
|
|
|
|
assertTrue(WebAssembly.Table.prototype.grow instanceof Function);
|
|
|
|
assertTrue(WebAssembly.Table.prototype.get instanceof Function);
|
|
|
|
assertTrue(WebAssembly.Table.prototype.set instanceof Function);
|
|
|
|
let desc = Object.getOwnPropertyDescriptor(WebAssembly.Table.prototype, 'length');
|
|
|
|
assertTrue(desc.get instanceof Function);
|
|
|
|
assertSame(undefined, desc.set);
|
|
|
|
|
|
|
|
assertThrows(() => new WebAssembly.Table(), TypeError);
|
|
|
|
assertThrows(() => new WebAssembly.Table(1), TypeError);
|
|
|
|
assertThrows(() => new WebAssembly.Table(""), TypeError);
|
|
|
|
|
|
|
|
assertThrows(() => new WebAssembly.Table({}), TypeError);
|
|
|
|
assertThrows(() => new WebAssembly.Table({initial: 10}), TypeError);
|
|
|
|
|
|
|
|
assertThrows(() => new WebAssembly.Table({element: 0, initial: 10}), TypeError);
|
|
|
|
assertThrows(() => new WebAssembly.Table({element: "any", initial: 10}), TypeError);
|
|
|
|
|
2017-01-17 19:15:59 +00:00
|
|
|
assertThrows(() => new WebAssembly.Table(
|
2018-12-08 09:58:18 +00:00
|
|
|
{element: "anyfunc", initial: -1}), TypeError);
|
2017-01-17 19:15:59 +00:00
|
|
|
assertThrows(() => new WebAssembly.Table(
|
2018-12-08 09:58:18 +00:00
|
|
|
{element: "anyfunc", initial: outOfUint32RangeValue}), TypeError);
|
2016-09-21 10:40:16 +00:00
|
|
|
|
2017-01-17 19:15:59 +00:00
|
|
|
assertThrows(() => new WebAssembly.Table(
|
2018-12-08 09:58:18 +00:00
|
|
|
{element: "anyfunc", initial: 10, maximum: -1}), TypeError);
|
2017-01-17 19:15:59 +00:00
|
|
|
assertThrows(() => new WebAssembly.Table(
|
2018-12-08 09:58:18 +00:00
|
|
|
{element: "anyfunc", initial: 10, maximum: outOfUint32RangeValue}), TypeError);
|
2017-01-17 19:15:59 +00:00
|
|
|
assertThrows(() => new WebAssembly.Table(
|
|
|
|
{element: "anyfunc", initial: 10, maximum: 9}), RangeError);
|
2016-09-26 10:15:03 +00:00
|
|
|
|
2016-10-13 14:43:23 +00:00
|
|
|
let table;
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 1});
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 1);
|
2016-10-13 14:43:23 +00:00
|
|
|
assertEquals(null, table.get(0));
|
2016-11-17 12:54:46 +00:00
|
|
|
assertEquals(undefined, table[0]);
|
2016-09-21 10:40:16 +00:00
|
|
|
|
2016-10-13 14:43:23 +00:00
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: "2"});
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 2);
|
2016-10-13 14:43:23 +00:00
|
|
|
assertEquals(null, table.get(0));
|
|
|
|
assertEquals(null, table.get(1));
|
2016-11-17 12:54:46 +00:00
|
|
|
assertEquals(undefined, table[0]);
|
|
|
|
assertEquals(undefined, table[1]);
|
2016-09-26 13:07:44 +00:00
|
|
|
|
2016-10-13 14:43:23 +00:00
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: {valueOf() { return "1" }}});
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 1);
|
2016-10-13 14:43:23 +00:00
|
|
|
assertEquals(null, table.get(0));
|
2016-11-17 12:54:46 +00:00
|
|
|
assertEquals(undefined, table[0]);
|
2016-10-13 14:43:23 +00:00
|
|
|
|
2018-12-08 09:58:18 +00:00
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 0, maximum: 10});
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 0);
|
2016-10-13 14:43:23 +00:00
|
|
|
|
2018-12-08 09:58:18 +00:00
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 0, maximum: "10"});
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 0);
|
2016-10-13 14:43:23 +00:00
|
|
|
|
2018-12-08 09:58:18 +00:00
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 0, maximum: {valueOf() { return "10" }}});
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 0);
|
2016-10-13 14:43:23 +00:00
|
|
|
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 0, maximum: undefined});
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 0);
|
|
|
|
|
2019-03-01 14:26:16 +00:00
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 0, maximum: 1000000});
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 0);
|
2017-01-17 19:15:59 +00:00
|
|
|
|
2019-03-01 14:26:16 +00:00
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 0, maximum: kV8MaxWasmTableSize});
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 0);
|
2017-01-17 19:15:59 +00:00
|
|
|
|
2019-03-01 14:26:16 +00:00
|
|
|
assertThrows(
|
|
|
|
() => new WebAssembly.Table(
|
|
|
|
{element: "anyfunc", initial: 0, maximum: kV8MaxWasmTableSize + 1}),
|
|
|
|
RangeError, /above the upper bound/);
|
2016-09-26 13:07:44 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestMaximumIsReadOnce() {
|
|
|
|
var a = true;
|
|
|
|
var desc = {element: "anyfunc", initial: 10};
|
|
|
|
Object.defineProperty(desc, 'maximum', {get: function() {
|
|
|
|
if (a) {
|
|
|
|
a = false;
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Change the return value on the second call so it throws.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}});
|
|
|
|
let table = new WebAssembly.Table(desc);
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 10);
|
2016-09-26 13:07:44 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestMaximumDoesHasProperty() {
|
|
|
|
var hasPropertyWasCalled = false;
|
|
|
|
var desc = {element: "anyfunc", initial: 10};
|
|
|
|
var proxy = new Proxy({maximum: 16}, {
|
|
|
|
has: function(target, name) { hasPropertyWasCalled = true; }
|
|
|
|
});
|
|
|
|
Object.setPrototypeOf(desc, proxy);
|
|
|
|
let table = new WebAssembly.Table(desc);
|
2017-07-10 11:18:45 +00:00
|
|
|
assertTableIsValid(table, 10);
|
2016-09-21 10:40:16 +00:00
|
|
|
})();
|
2016-10-13 14:43:23 +00:00
|
|
|
|
|
|
|
(function TestLength() {
|
|
|
|
for (let i = 0; i < 10; ++i) {
|
|
|
|
let table = new WebAssembly.Table({element: "anyfunc", initial: i});
|
|
|
|
assertEquals(i, table.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThrows(() => WebAssembly.Table.prototype.length.call([]), TypeError);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestGet() {
|
|
|
|
let table = new WebAssembly.Table({element: "anyfunc", initial: 10});
|
|
|
|
|
2016-10-17 11:39:04 +00:00
|
|
|
for (let i = 0; i < table.length; ++i) {
|
2016-10-13 14:43:23 +00:00
|
|
|
assertEquals(null, table.get(i));
|
|
|
|
assertEquals(null, table.get(String(i)));
|
|
|
|
}
|
2018-12-19 15:11:06 +00:00
|
|
|
for (let key of [0.4, "", []]) {
|
2016-10-17 11:39:04 +00:00
|
|
|
assertEquals(null, table.get(key));
|
|
|
|
}
|
2018-12-19 15:11:06 +00:00
|
|
|
for (let key of [-1, NaN, {}, () => {}]) {
|
|
|
|
assertThrows(() => table.get(key), TypeError);
|
|
|
|
}
|
|
|
|
for (let key of [table.length, table.length * 10]) {
|
2016-10-17 11:39:04 +00:00
|
|
|
assertThrows(() => table.get(key), RangeError);
|
|
|
|
}
|
2016-10-13 14:43:23 +00:00
|
|
|
assertThrows(() => table.get(Symbol()), TypeError);
|
|
|
|
assertThrows(() => WebAssembly.Table.prototype.get.call([], 0), TypeError);
|
|
|
|
})();
|
2016-10-17 11:39:04 +00:00
|
|
|
|
|
|
|
(function TestSet() {
|
|
|
|
let builder = new WasmModuleBuilder;
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addExport("host", builder.addImport("test", "f", kSig_v_v));
|
2018-09-11 09:03:27 +00:00
|
|
|
builder.addExport("wasm", builder.addFunction("", kSig_v_v).addBody([]));
|
2016-10-17 11:39:04 +00:00
|
|
|
let {wasm, host} = builder.instantiate({test: {f() {}}}).exports;
|
|
|
|
|
|
|
|
let table = new WebAssembly.Table({element: "anyfunc", initial: 10});
|
|
|
|
|
|
|
|
for (let f of [wasm, host]) {
|
|
|
|
for (let i = 0; i < table.length; ++i) table.set(i, null);
|
|
|
|
for (let i = 0; i < table.length; ++i) {
|
|
|
|
assertSame(null, table.get(i));
|
|
|
|
assertSame(undefined, table.set(i, f));
|
|
|
|
assertSame(f, table.get(i));
|
2016-11-17 12:54:46 +00:00
|
|
|
assertSame(undefined, table[i]);
|
2016-10-17 11:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < table.length; ++i) table.set(i, null);
|
|
|
|
for (let i = 0; i < table.length; ++i) {
|
|
|
|
assertSame(null, table.get(i));
|
|
|
|
assertSame(undefined, table.set(String(i), f));
|
|
|
|
assertSame(f, table.get(i));
|
2016-11-17 12:54:46 +00:00
|
|
|
assertSame(undefined, table[i]);
|
2016-10-17 11:39:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 14:07:49 +00:00
|
|
|
for (let key of [0.4, "", []]) {
|
2016-10-17 11:39:04 +00:00
|
|
|
assertSame(undefined, table.set(0, null));
|
|
|
|
assertSame(undefined, table.set(key, f));
|
|
|
|
assertSame(f, table.get(0));
|
2016-11-17 12:54:46 +00:00
|
|
|
assertSame(undefined, table[key]);
|
2016-10-17 11:39:04 +00:00
|
|
|
}
|
2019-01-16 14:07:49 +00:00
|
|
|
for (let key of [NaN, {}, () => {}]) {
|
|
|
|
assertSame(undefined, table[key]);
|
|
|
|
assertThrows(() => table.set(key, f), TypeError);
|
|
|
|
}
|
2016-10-17 11:39:04 +00:00
|
|
|
|
2019-01-16 14:07:49 +00:00
|
|
|
assertThrows(() => table.set(-1, f), TypeError);
|
|
|
|
for (let key of [table.length, table.length * 10]) {
|
2016-10-17 11:39:04 +00:00
|
|
|
assertThrows(() => table.set(key, f), RangeError);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThrows(() => table.set(0), TypeError);
|
|
|
|
for (let val of [undefined, 0, "", {}, [], () => {}]) {
|
|
|
|
assertThrows(() => table.set(0, val), TypeError);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThrows(() => table.set(Symbol(), f), TypeError);
|
|
|
|
assertThrows(() => WebAssembly.Table.prototype.set.call([], 0, f),
|
|
|
|
TypeError);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2016-11-17 12:54:46 +00:00
|
|
|
|
|
|
|
(function TestIndexing() {
|
|
|
|
let builder = new WasmModuleBuilder;
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addExport("host", builder.addImport("test", "f", kSig_v_v));
|
2018-09-11 09:03:27 +00:00
|
|
|
builder.addExport("wasm", builder.addFunction("", kSig_v_v).addBody([]));
|
2016-11-17 12:54:46 +00:00
|
|
|
let {wasm, host} = builder.instantiate({test: {f() {}}}).exports;
|
|
|
|
|
|
|
|
let table = new WebAssembly.Table({element: "anyfunc", initial: 10});
|
|
|
|
|
|
|
|
for (let f of [wasm, host, () => {}, 5, {}, ""]) {
|
|
|
|
for (let i = 0; i < table.length; ++i) table[i] = f;
|
|
|
|
for (let i = 0; i < table.length; ++i) {
|
|
|
|
assertSame(null, table.get(i));
|
|
|
|
assertSame(f, table[i]);
|
|
|
|
}
|
|
|
|
|
2018-12-19 15:11:06 +00:00
|
|
|
for (let key of [NaN, {}, () => {}]) {
|
|
|
|
assertSame(f, table[key] = f);
|
|
|
|
assertSame(f, table[key]);
|
|
|
|
assertThrows(() => table.get(key), TypeError);
|
|
|
|
}
|
|
|
|
for (let key of [0.4, "", []]) {
|
2016-11-17 12:54:46 +00:00
|
|
|
assertSame(f, table[key] = f);
|
|
|
|
assertSame(f, table[key]);
|
|
|
|
assertSame(null, table.get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2016-10-17 11:39:04 +00:00
|
|
|
(function TestGrow() {
|
|
|
|
let builder = new WasmModuleBuilder;
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addExport("host", builder.addImport("test", "f", kSig_v_v));
|
2018-09-11 09:03:27 +00:00
|
|
|
builder.addExport("wasm", builder.addFunction("", kSig_v_v).addBody([]));
|
2016-10-17 11:39:04 +00:00
|
|
|
let {wasm, host} = builder.instantiate({test: {f() {}}}).exports;
|
|
|
|
|
|
|
|
function init(table) {
|
|
|
|
for (let i = 0; i < 5; ++i) table.set(i, wasm);
|
|
|
|
for (let i = 15; i < 20; ++i) table.set(i, host);
|
|
|
|
}
|
|
|
|
function check(table) {
|
|
|
|
for (let i = 0; i < 5; ++i) assertSame(wasm, table.get(i));
|
|
|
|
for (let i = 6; i < 15; ++i) assertSame(null, table.get(i));
|
|
|
|
for (let i = 15; i < 20; ++i) assertSame(host, table.get(i));
|
|
|
|
for (let i = 21; i < table.length; ++i) assertSame(null, table.get(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
let table = new WebAssembly.Table({element: "anyfunc", initial: 20});
|
|
|
|
init(table);
|
|
|
|
check(table);
|
|
|
|
table.grow(0);
|
|
|
|
check(table);
|
|
|
|
table.grow(10);
|
|
|
|
check(table);
|
2018-12-19 15:11:06 +00:00
|
|
|
assertThrows(() => table.grow(-10), TypeError);
|
2016-10-17 11:39:04 +00:00
|
|
|
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 20, maximum: 25});
|
|
|
|
init(table);
|
|
|
|
check(table);
|
|
|
|
table.grow(0);
|
|
|
|
check(table);
|
|
|
|
table.grow(5);
|
|
|
|
check(table);
|
|
|
|
table.grow(0);
|
|
|
|
check(table);
|
|
|
|
assertThrows(() => table.grow(1), RangeError);
|
2018-12-19 15:11:06 +00:00
|
|
|
assertThrows(() => table.grow(-10), TypeError);
|
2016-10-17 11:39:04 +00:00
|
|
|
|
|
|
|
assertThrows(() => WebAssembly.Table.prototype.grow.call([], 0), TypeError);
|
2017-01-17 19:15:59 +00:00
|
|
|
|
|
|
|
table = new WebAssembly.Table(
|
|
|
|
{element: "anyfunc", initial: 0, maximum: kV8MaxWasmTableSize});
|
|
|
|
table.grow(kV8MaxWasmTableSize);
|
|
|
|
assertThrows(() => table.grow(1), RangeError);
|
2017-08-25 18:57:21 +00:00
|
|
|
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 0});
|
|
|
|
table.grow({valueOf: () => {table.grow(2); return 1;}});
|
|
|
|
assertEquals(3, table.length);
|
2016-10-17 11:39:04 +00:00
|
|
|
})();
|