table.js 9.41 KB
Newer Older
1 2 3 4 5 6
// 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

7 8
'use strict';

9 10 11
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");

12 13 14
// Basic tests.

var outOfUint32RangeValue = 1e12;
15
var int32ButOob = 1073741824;
16 17
var kMaxUint32 = (4 * 1024 * 1024 * 1024) - 1;
var kV8MaxWasmTableSize = 10000000;
18

19 20 21 22 23 24 25
function assertTableIsValid(table) {
  assertSame(WebAssembly.Table.prototype, table.__proto__);
  assertSame(WebAssembly.Table, table.constructor);
  assertTrue(table instanceof Object);
  assertTrue(table instanceof WebAssembly.Table);
}

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
(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);

46 47 48 49
  assertThrows(() => new WebAssembly.Table(
    {element: "anyfunc", initial: -1}), RangeError);
  assertThrows(() => new WebAssembly.Table(
    {element: "anyfunc", initial: outOfUint32RangeValue}), RangeError);
50

51 52 53 54 55 56
  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);
57

58 59
  let table;
  table = new WebAssembly.Table({element: "anyfunc", initial: 1});
60
  assertTableIsValid(table);
61 62
  assertEquals(1, table.length);
  assertEquals(null, table.get(0));
63
  assertEquals(undefined, table[0]);
64

65
  table = new WebAssembly.Table({element: "anyfunc", initial: "2"});
66
  assertTableIsValid(table);
67 68 69
  assertEquals(2, table.length);
  assertEquals(null, table.get(0));
  assertEquals(null, table.get(1));
70 71
  assertEquals(undefined, table[0]);
  assertEquals(undefined, table[1]);
72

73
  table = new WebAssembly.Table({element: "anyfunc", initial: {valueOf() { return "1" }}});
74
  assertTableIsValid(table);
75 76
  assertEquals(1, table.length);
  assertEquals(null, table.get(0));
77
  assertEquals(undefined, table[0]);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

  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);
94

95
  table = new WebAssembly.Table({element: "anyfunc", maximum: {valueOf() { return "10" }}});
96
  assertTableIsValid(table);
97 98 99 100 101
  assertEquals(0, table.length);

  table = new WebAssembly.Table({element: "anyfunc", initial: 0, maximum: undefined});
  assertTableIsValid(table);
  assertEquals(0, table.length);
102 103 104 105 106 107 108 109

  table = new WebAssembly.Table({element: "anyfunc", maximum: kMaxUint32});
  assertTableIsValid(table);
  assertEquals(0, table.length);

  table = new WebAssembly.Table({element: "anyfunc", maximum: kV8MaxWasmTableSize + 1});
  assertTableIsValid(table);
  assertEquals(0, table.length);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
})();

(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);
138
})();
139 140 141 142 143 144 145 146 147 148 149 150 151

(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});

152
  for (let i = 0; i < table.length; ++i) {
153 154 155
    assertEquals(null, table.get(i));
    assertEquals(null, table.get(String(i)));
  }
156 157 158 159 160 161
  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);
  }
162 163 164
  assertThrows(() => table.get(Symbol()), TypeError);
  assertThrows(() => WebAssembly.Table.prototype.get.call([], 0), TypeError);
})();
165 166 167

(function TestSet() {
  let builder = new WasmModuleBuilder;
168
  builder.addExport("wasm", builder.addFunction("", kSig_v_v).addBody([]));
169
  builder.addExport("host", builder.addImport("test", "f", kSig_v_v));
170 171 172 173 174 175 176 177 178 179
  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));
180
      assertSame(undefined, table[i]);
181 182 183 184 185 186 187
    }

    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));
188
      assertSame(undefined, table[i]);
189 190 191 192 193 194
    }

    for (let key of [0.4, "", NaN, {}, [], () => {}]) {
      assertSame(undefined, table.set(0, null));
      assertSame(undefined, table.set(key, f));
      assertSame(f, table.get(0));
195
      assertSame(undefined, table[key]);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    }

    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);
  }
})();

213 214 215

(function TestIndexing() {
  let builder = new WasmModuleBuilder;
216
  builder.addExport("wasm", builder.addFunction("", kSig_v_v).addBody([]));
217
  builder.addExport("host", builder.addImport("test", "f", kSig_v_v));
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  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));
    }
  }
})();

237 238
(function TestGrow() {
  let builder = new WasmModuleBuilder;
239
  builder.addExport("wasm", builder.addFunction("", kSig_v_v).addBody([]));
240
  builder.addExport("host", builder.addImport("test", "f", kSig_v_v));
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  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);
276 277 278 279 280

  table = new WebAssembly.Table(
    {element: "anyfunc", initial: 0, maximum: kV8MaxWasmTableSize});
  table.grow(kV8MaxWasmTableSize);
  assertThrows(() => table.grow(1), RangeError);
281
})();