1// META: global=window,dedicatedworker,jsshell 2// META: script=/wasm/jsapi/wasm-module-builder.js 3// META: script=assertions.js 4 5function nulls(n) { 6 return Array(n).fill(null); 7} 8 9test(() => { 10 const argument = { "element": "anyfunc", "initial": 5 }; 11 const table = new WebAssembly.Table(argument); 12 assert_throws_js(TypeError, () => table.grow()); 13}, "Missing arguments"); 14 15test(t => { 16 const thisValues = [ 17 undefined, 18 null, 19 true, 20 "", 21 Symbol(), 22 1, 23 {}, 24 WebAssembly.Table, 25 WebAssembly.Table.prototype, 26 ]; 27 28 const argument = { 29 valueOf: t.unreached_func("Should not touch the argument (valueOf)"), 30 toString: t.unreached_func("Should not touch the argument (toString)"), 31 }; 32 33 const fn = WebAssembly.Table.prototype.grow; 34 35 for (const thisValue of thisValues) { 36 assert_throws_js(TypeError, () => fn.call(thisValue, argument), `this=${format_value(thisValue)}`); 37 } 38}, "Branding"); 39 40test(() => { 41 const argument = { "element": "anyfunc", "initial": 5 }; 42 const table = new WebAssembly.Table(argument); 43 assert_equal_to_array(table, nulls(5), "before"); 44 45 const result = table.grow(3); 46 assert_equals(result, 5); 47 assert_equal_to_array(table, nulls(8), "after"); 48}, "Basic"); 49 50test(() => { 51 const argument = { "element": "anyfunc", "initial": 3, "maximum": 5 }; 52 const table = new WebAssembly.Table(argument); 53 assert_equal_to_array(table, nulls(3), "before"); 54 55 const result = table.grow(2); 56 assert_equals(result, 3); 57 assert_equal_to_array(table, nulls(5), "after"); 58}, "Reached maximum"); 59 60test(() => { 61 const argument = { "element": "anyfunc", "initial": 2, "maximum": 5 }; 62 const table = new WebAssembly.Table(argument); 63 assert_equal_to_array(table, nulls(2), "before"); 64 65 assert_throws_js(RangeError, () => table.grow(4)); 66 assert_equal_to_array(table, nulls(2), "after"); 67}, "Exceeded maximum"); 68 69const outOfRangeValues = [ 70 undefined, 71 NaN, 72 Infinity, 73 -Infinity, 74 -1, 75 0x100000000, 76 0x1000000000, 77 "0x100000000", 78 { valueOf() { return 0x100000000; } }, 79]; 80 81for (const value of outOfRangeValues) { 82 test(() => { 83 const argument = { "element": "anyfunc", "initial": 1 }; 84 const table = new WebAssembly.Table(argument); 85 assert_throws_js(TypeError, () => table.grow(value)); 86 }, `Out-of-range argument: ${format_value(value)}`); 87} 88 89test(() => { 90 const argument = { "element": "anyfunc", "initial": 5 }; 91 const table = new WebAssembly.Table(argument); 92 assert_equal_to_array(table, nulls(5), "before"); 93 94 const result = table.grow(3, null, {}); 95 assert_equals(result, 5); 96 assert_equal_to_array(table, nulls(8), "after"); 97}, "Stray argument"); 98 99test(() => { 100 const builder = new WasmModuleBuilder(); 101 builder 102 .addFunction("fn", kSig_v_v) 103 .addBody([]) 104 .exportFunc(); 105 const bin = builder.toBuffer() 106 const argument = { "element": "anyfunc", "initial": 1 }; 107 const table = new WebAssembly.Table(argument); 108 const fn = new WebAssembly.Instance(new WebAssembly.Module(bin)).exports.fn; 109 const result = table.grow(2, fn); 110 assert_equals(result, 1); 111 assert_equals(table.get(0), null); 112 assert_equals(table.get(1), fn); 113 assert_equals(table.get(2), fn); 114}, "Grow with exported-function argument"); 115 116test(() => { 117 const argument = { "element": "anyfunc", "initial": 1 }; 118 const table = new WebAssembly.Table(argument); 119 assert_throws_js(TypeError, () => table.grow(2, {})); 120}, "Grow with non-function argument"); 121 122test(() => { 123 const argument = { "element": "anyfunc", "initial": 1 }; 124 const table = new WebAssembly.Table(argument); 125 assert_throws_js(TypeError, () => table.grow(2, () => true)); 126}, "Grow with JS-function argument"); 127