11cb0ef41Sopenharmony_ci// META: global=window,dedicatedworker,jsshell
21cb0ef41Sopenharmony_ci// META: script=/wasm/jsapi/wasm-module-builder.js
31cb0ef41Sopenharmony_ci// META: script=assertions.js
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cifunction nulls(n) {
61cb0ef41Sopenharmony_ci  return Array(n).fill(null);
71cb0ef41Sopenharmony_ci}
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_citest(() => {
101cb0ef41Sopenharmony_ci  const argument = { "element": "anyfunc", "initial": 5 };
111cb0ef41Sopenharmony_ci  const table = new WebAssembly.Table(argument);
121cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => table.grow());
131cb0ef41Sopenharmony_ci}, "Missing arguments");
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_citest(t => {
161cb0ef41Sopenharmony_ci  const thisValues = [
171cb0ef41Sopenharmony_ci    undefined,
181cb0ef41Sopenharmony_ci    null,
191cb0ef41Sopenharmony_ci    true,
201cb0ef41Sopenharmony_ci    "",
211cb0ef41Sopenharmony_ci    Symbol(),
221cb0ef41Sopenharmony_ci    1,
231cb0ef41Sopenharmony_ci    {},
241cb0ef41Sopenharmony_ci    WebAssembly.Table,
251cb0ef41Sopenharmony_ci    WebAssembly.Table.prototype,
261cb0ef41Sopenharmony_ci  ];
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const argument = {
291cb0ef41Sopenharmony_ci    valueOf: t.unreached_func("Should not touch the argument (valueOf)"),
301cb0ef41Sopenharmony_ci    toString: t.unreached_func("Should not touch the argument (toString)"),
311cb0ef41Sopenharmony_ci  };
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  const fn = WebAssembly.Table.prototype.grow;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  for (const thisValue of thisValues) {
361cb0ef41Sopenharmony_ci    assert_throws_js(TypeError, () => fn.call(thisValue, argument), `this=${format_value(thisValue)}`);
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci}, "Branding");
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_citest(() => {
411cb0ef41Sopenharmony_ci  const argument = { "element": "anyfunc", "initial": 5 };
421cb0ef41Sopenharmony_ci  const table = new WebAssembly.Table(argument);
431cb0ef41Sopenharmony_ci  assert_equal_to_array(table, nulls(5), "before");
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  const result = table.grow(3);
461cb0ef41Sopenharmony_ci  assert_equals(result, 5);
471cb0ef41Sopenharmony_ci  assert_equal_to_array(table, nulls(8), "after");
481cb0ef41Sopenharmony_ci}, "Basic");
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_citest(() => {
511cb0ef41Sopenharmony_ci  const argument = { "element": "anyfunc", "initial": 3, "maximum": 5 };
521cb0ef41Sopenharmony_ci  const table = new WebAssembly.Table(argument);
531cb0ef41Sopenharmony_ci  assert_equal_to_array(table, nulls(3), "before");
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  const result = table.grow(2);
561cb0ef41Sopenharmony_ci  assert_equals(result, 3);
571cb0ef41Sopenharmony_ci  assert_equal_to_array(table, nulls(5), "after");
581cb0ef41Sopenharmony_ci}, "Reached maximum");
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_citest(() => {
611cb0ef41Sopenharmony_ci  const argument = { "element": "anyfunc", "initial": 2, "maximum": 5 };
621cb0ef41Sopenharmony_ci  const table = new WebAssembly.Table(argument);
631cb0ef41Sopenharmony_ci  assert_equal_to_array(table, nulls(2), "before");
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  assert_throws_js(RangeError, () => table.grow(4));
661cb0ef41Sopenharmony_ci  assert_equal_to_array(table, nulls(2), "after");
671cb0ef41Sopenharmony_ci}, "Exceeded maximum");
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ciconst outOfRangeValues = [
701cb0ef41Sopenharmony_ci  undefined,
711cb0ef41Sopenharmony_ci  NaN,
721cb0ef41Sopenharmony_ci  Infinity,
731cb0ef41Sopenharmony_ci  -Infinity,
741cb0ef41Sopenharmony_ci  -1,
751cb0ef41Sopenharmony_ci  0x100000000,
761cb0ef41Sopenharmony_ci  0x1000000000,
771cb0ef41Sopenharmony_ci  "0x100000000",
781cb0ef41Sopenharmony_ci  { valueOf() { return 0x100000000; } },
791cb0ef41Sopenharmony_ci];
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_cifor (const value of outOfRangeValues) {
821cb0ef41Sopenharmony_ci  test(() => {
831cb0ef41Sopenharmony_ci    const argument = { "element": "anyfunc", "initial": 1 };
841cb0ef41Sopenharmony_ci    const table = new WebAssembly.Table(argument);
851cb0ef41Sopenharmony_ci    assert_throws_js(TypeError, () => table.grow(value));
861cb0ef41Sopenharmony_ci  }, `Out-of-range argument: ${format_value(value)}`);
871cb0ef41Sopenharmony_ci}
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_citest(() => {
901cb0ef41Sopenharmony_ci  const argument = { "element": "anyfunc", "initial": 5 };
911cb0ef41Sopenharmony_ci  const table = new WebAssembly.Table(argument);
921cb0ef41Sopenharmony_ci  assert_equal_to_array(table, nulls(5), "before");
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  const result = table.grow(3, null, {});
951cb0ef41Sopenharmony_ci  assert_equals(result, 5);
961cb0ef41Sopenharmony_ci  assert_equal_to_array(table, nulls(8), "after");
971cb0ef41Sopenharmony_ci}, "Stray argument");
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_citest(() => {
1001cb0ef41Sopenharmony_ci  const builder = new WasmModuleBuilder();
1011cb0ef41Sopenharmony_ci  builder
1021cb0ef41Sopenharmony_ci    .addFunction("fn", kSig_v_v)
1031cb0ef41Sopenharmony_ci    .addBody([])
1041cb0ef41Sopenharmony_ci    .exportFunc();
1051cb0ef41Sopenharmony_ci  const bin = builder.toBuffer()
1061cb0ef41Sopenharmony_ci  const argument = { "element": "anyfunc", "initial": 1 };
1071cb0ef41Sopenharmony_ci  const table = new WebAssembly.Table(argument);
1081cb0ef41Sopenharmony_ci  const fn = new WebAssembly.Instance(new WebAssembly.Module(bin)).exports.fn;
1091cb0ef41Sopenharmony_ci  const result = table.grow(2, fn);
1101cb0ef41Sopenharmony_ci  assert_equals(result, 1);
1111cb0ef41Sopenharmony_ci  assert_equals(table.get(0), null);
1121cb0ef41Sopenharmony_ci  assert_equals(table.get(1), fn);
1131cb0ef41Sopenharmony_ci  assert_equals(table.get(2), fn);
1141cb0ef41Sopenharmony_ci}, "Grow with exported-function argument");
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_citest(() => {
1171cb0ef41Sopenharmony_ci  const argument = { "element": "anyfunc", "initial": 1 };
1181cb0ef41Sopenharmony_ci  const table = new WebAssembly.Table(argument);
1191cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => table.grow(2, {}));
1201cb0ef41Sopenharmony_ci}, "Grow with non-function argument");
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_citest(() => {
1231cb0ef41Sopenharmony_ci  const argument = { "element": "anyfunc", "initial": 1 };
1241cb0ef41Sopenharmony_ci  const table = new WebAssembly.Table(argument);
1251cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => table.grow(2, () => true));
1261cb0ef41Sopenharmony_ci}, "Grow with JS-function argument");
127