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-constants.js");
|
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
|
2016-09-21 10:40:16 +00:00
|
|
|
// Basic tests.
|
|
|
|
|
|
|
|
var outOfUint32RangeValue = 1e12;
|
2016-09-26 10:15:03 +00:00
|
|
|
var int32ButOob = 1073741824;
|
2016-09-21 10:40:16 +00:00
|
|
|
|
2016-09-26 13:07:44 +00:00
|
|
|
function assertTableIsValid(table) {
|
|
|
|
assertSame(WebAssembly.Table.prototype, table.__proto__);
|
|
|
|
assertSame(WebAssembly.Table, table.constructor);
|
|
|
|
assertTrue(table instanceof Object);
|
|
|
|
assertTrue(table instanceof WebAssembly.Table);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
assertThrows(() => new WebAssembly.Table({element: "anyfunc", initial: -1}), RangeError);
|
|
|
|
assertThrows(() => new WebAssembly.Table({element: "anyfunc", initial: outOfUint32RangeValue}), RangeError);
|
|
|
|
|
|
|
|
assertThrows(() => new WebAssembly.Table({element: "anyfunc", initial: 10, maximum: -1}), RangeError);
|
|
|
|
assertThrows(() => new WebAssembly.Table({element: "anyfunc", initial: 10, maximum: outOfUint32RangeValue}), RangeError);
|
|
|
|
assertThrows(() => new WebAssembly.Table({element: "anyfunc", initial: 10, maximum: 9}), RangeError);
|
|
|
|
|
2016-09-26 10:15:03 +00:00
|
|
|
assertThrows(() => new WebAssembly.Table({element: "anyfunc", initial: 0, maximum: int32ButOob}));
|
|
|
|
|
2016-10-13 14:43:23 +00:00
|
|
|
let table;
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 1});
|
2016-09-26 13:07:44 +00:00
|
|
|
assertTableIsValid(table);
|
2016-10-13 14:43:23 +00:00
|
|
|
assertEquals(1, table.length);
|
|
|
|
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"});
|
2016-09-26 13:07:44 +00:00
|
|
|
assertTableIsValid(table);
|
2016-10-13 14:43:23 +00:00
|
|
|
assertEquals(2, table.length);
|
|
|
|
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" }}});
|
2016-09-26 13:07:44 +00:00
|
|
|
assertTableIsValid(table);
|
2016-10-13 14:43:23 +00:00
|
|
|
assertEquals(1, table.length);
|
|
|
|
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
|
|
|
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: undefined});
|
|
|
|
assertTableIsValid(table);
|
|
|
|
assertEquals(0, table.length);
|
|
|
|
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc"});
|
|
|
|
assertTableIsValid(table);
|
|
|
|
assertEquals(0, table.length);
|
|
|
|
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc", maximum: 10});
|
|
|
|
assertTableIsValid(table);
|
|
|
|
assertEquals(0, table.length);
|
|
|
|
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc", maximum: "10"});
|
|
|
|
assertTableIsValid(table);
|
|
|
|
assertEquals(0, table.length);
|
2016-09-26 13:07:44 +00:00
|
|
|
|
2016-10-13 14:43:23 +00:00
|
|
|
table = new WebAssembly.Table({element: "anyfunc", maximum: {valueOf() { return "10" }}});
|
2016-09-26 13:07:44 +00:00
|
|
|
assertTableIsValid(table);
|
2016-10-13 14:43:23 +00:00
|
|
|
assertEquals(0, table.length);
|
|
|
|
|
|
|
|
table = new WebAssembly.Table({element: "anyfunc", initial: 0, maximum: undefined});
|
|
|
|
assertTableIsValid(table);
|
|
|
|
assertEquals(0, table.length);
|
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);
|
|
|
|
assertTableIsValid(table);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(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);
|
|
|
|
assertTableIsValid(table);
|
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)));
|
|
|
|
}
|
2016-10-17 11:39:04 +00:00
|
|
|
for (let key of [0.4, "", NaN, {}, [], () => {}]) {
|
|
|
|
assertEquals(null, table.get(key));
|
|
|
|
}
|
|
|
|
for (let key of [-1, table.length, table.length * 10]) {
|
|
|
|
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;
|
|
|
|
builder.addExport("wasm", builder.addFunction("", kSig_v_v));
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addExport("host", builder.addImport("test", "f", kSig_v_v));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for (let key of [0.4, "", NaN, {}, [], () => {}]) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for (let key of [-1, table.length, table.length * 10]) {
|
|
|
|
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;
|
|
|
|
builder.addExport("wasm", builder.addFunction("", kSig_v_v));
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addExport("host", builder.addImport("test", "f", kSig_v_v));
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let key of [0.4, "", NaN, {}, [], () => {}]) {
|
|
|
|
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;
|
|
|
|
builder.addExport("wasm", builder.addFunction("", kSig_v_v));
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addExport("host", builder.addImport("test", "f", kSig_v_v));
|
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);
|
|
|
|
assertThrows(() => table.grow(-10), RangeError);
|
|
|
|
|
|
|
|
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);
|
|
|
|
assertThrows(() => table.grow(-10), RangeError);
|
|
|
|
|
|
|
|
assertThrows(() => WebAssembly.Table.prototype.grow.call([], 0), TypeError);
|
|
|
|
})();
|