11cb0ef41Sopenharmony_ci// META: global=window,dedicatedworker,jsshell
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_citest(() => {
41cb0ef41Sopenharmony_ci  const thisValues = [
51cb0ef41Sopenharmony_ci    undefined,
61cb0ef41Sopenharmony_ci    null,
71cb0ef41Sopenharmony_ci    true,
81cb0ef41Sopenharmony_ci    "",
91cb0ef41Sopenharmony_ci    Symbol(),
101cb0ef41Sopenharmony_ci    1,
111cb0ef41Sopenharmony_ci    {},
121cb0ef41Sopenharmony_ci    WebAssembly.Global,
131cb0ef41Sopenharmony_ci    WebAssembly.Global.prototype,
141cb0ef41Sopenharmony_ci  ];
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  const desc = Object.getOwnPropertyDescriptor(WebAssembly.Global.prototype, "value");
171cb0ef41Sopenharmony_ci  assert_equals(typeof desc, "object");
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  const getter = desc.get;
201cb0ef41Sopenharmony_ci  assert_equals(typeof getter, "function");
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  const setter = desc.set;
231cb0ef41Sopenharmony_ci  assert_equals(typeof setter, "function");
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  for (const thisValue of thisValues) {
261cb0ef41Sopenharmony_ci    assert_throws_js(TypeError, () => getter.call(thisValue), `getter with this=${format_value(thisValue)}`);
271cb0ef41Sopenharmony_ci    assert_throws_js(TypeError, () => setter.call(thisValue, 1), `setter with this=${format_value(thisValue)}`);
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci}, "Branding");
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cifor (const type of ["i32", "i64", "f32", "f64"]) {
321cb0ef41Sopenharmony_ci  const [initial, value, invalid] = type === "i64" ? [0n, 1n, 2] : [0, 1, 2n];
331cb0ef41Sopenharmony_ci  const immutableOptions = [
341cb0ef41Sopenharmony_ci    [{}, "missing"],
351cb0ef41Sopenharmony_ci    [{ "mutable": undefined }, "undefined"],
361cb0ef41Sopenharmony_ci    [{ "mutable": null }, "null"],
371cb0ef41Sopenharmony_ci    [{ "mutable": false }, "false"],
381cb0ef41Sopenharmony_ci    [{ "mutable": "" }, "empty string"],
391cb0ef41Sopenharmony_ci    [{ "mutable": 0 }, "zero"],
401cb0ef41Sopenharmony_ci  ];
411cb0ef41Sopenharmony_ci  for (const [opts, name] of immutableOptions) {
421cb0ef41Sopenharmony_ci    test(() => {
431cb0ef41Sopenharmony_ci      opts.value = type;
441cb0ef41Sopenharmony_ci      const global = new WebAssembly.Global(opts);
451cb0ef41Sopenharmony_ci      assert_equals(global.value, initial, "initial value");
461cb0ef41Sopenharmony_ci      assert_equals(global.valueOf(), initial, "initial valueOf");
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci      assert_throws_js(TypeError, () => global.value = value);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci      assert_equals(global.value, initial, "post-set value");
511cb0ef41Sopenharmony_ci      assert_equals(global.valueOf(), initial, "post-set valueOf");
521cb0ef41Sopenharmony_ci    }, `Immutable ${type} (${name})`);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    test(t => {
551cb0ef41Sopenharmony_ci      opts.value = type;
561cb0ef41Sopenharmony_ci      const global = new WebAssembly.Global(opts);
571cb0ef41Sopenharmony_ci      assert_equals(global.value, initial, "initial value");
581cb0ef41Sopenharmony_ci      assert_equals(global.valueOf(), initial, "initial valueOf");
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci      const value = {
611cb0ef41Sopenharmony_ci        valueOf: t.unreached_func("should not call valueOf"),
621cb0ef41Sopenharmony_ci        toString: t.unreached_func("should not call toString"),
631cb0ef41Sopenharmony_ci      };
641cb0ef41Sopenharmony_ci      assert_throws_js(TypeError, () => global.value = value);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci      assert_equals(global.value, initial, "post-set value");
671cb0ef41Sopenharmony_ci      assert_equals(global.valueOf(), initial, "post-set valueOf");
681cb0ef41Sopenharmony_ci    }, `Immutable ${type} with ToNumber side-effects (${name})`);
691cb0ef41Sopenharmony_ci  }
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  const mutableOptions = [
721cb0ef41Sopenharmony_ci    [{ "mutable": true }, "true"],
731cb0ef41Sopenharmony_ci    [{ "mutable": 1 }, "one"],
741cb0ef41Sopenharmony_ci    [{ "mutable": "x" }, "string"],
751cb0ef41Sopenharmony_ci    [Object.create({ "mutable": true }), "true on prototype"],
761cb0ef41Sopenharmony_ci  ];
771cb0ef41Sopenharmony_ci  for (const [opts, name] of mutableOptions) {
781cb0ef41Sopenharmony_ci    test(() => {
791cb0ef41Sopenharmony_ci      opts.value = type;
801cb0ef41Sopenharmony_ci      const global = new WebAssembly.Global(opts);
811cb0ef41Sopenharmony_ci      assert_equals(global.value, initial, "initial value");
821cb0ef41Sopenharmony_ci      assert_equals(global.valueOf(), initial, "initial valueOf");
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci      global.value = value;
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci      assert_throws_js(TypeError, () => global.value = invalid);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci      assert_equals(global.value, value, "post-set value");
891cb0ef41Sopenharmony_ci      assert_equals(global.valueOf(), value, "post-set valueOf");
901cb0ef41Sopenharmony_ci    }, `Mutable ${type} (${name})`);
911cb0ef41Sopenharmony_ci  }
921cb0ef41Sopenharmony_ci}
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_citest(() => {
951cb0ef41Sopenharmony_ci  const argument = { "value": "i64", "mutable": true };
961cb0ef41Sopenharmony_ci  const global = new WebAssembly.Global(argument);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  assert_equals(global.value, 0n, "initial value using ToJSValue");
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  const valid = [
1011cb0ef41Sopenharmony_ci    [123n, 123n],
1021cb0ef41Sopenharmony_ci    [2n ** 63n, - (2n ** 63n)],
1031cb0ef41Sopenharmony_ci    [true, 1n],
1041cb0ef41Sopenharmony_ci    [false, 0n],
1051cb0ef41Sopenharmony_ci    ["456", 456n],
1061cb0ef41Sopenharmony_ci  ];
1071cb0ef41Sopenharmony_ci  for (const [input, output] of valid) {
1081cb0ef41Sopenharmony_ci    global.value = input;
1091cb0ef41Sopenharmony_ci    assert_equals(global.valueOf(), output, "post-set valueOf");
1101cb0ef41Sopenharmony_ci  }
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci  const invalid = [
1131cb0ef41Sopenharmony_ci    undefined,
1141cb0ef41Sopenharmony_ci    null,
1151cb0ef41Sopenharmony_ci    0,
1161cb0ef41Sopenharmony_ci    1,
1171cb0ef41Sopenharmony_ci    4.2,
1181cb0ef41Sopenharmony_ci    Symbol(),
1191cb0ef41Sopenharmony_ci  ];
1201cb0ef41Sopenharmony_ci  for (const input of invalid) {
1211cb0ef41Sopenharmony_ci    assert_throws_js(TypeError, () => global.value = input);
1221cb0ef41Sopenharmony_ci  }
1231cb0ef41Sopenharmony_ci}, "i64 mutability");
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_citest(() => {
1261cb0ef41Sopenharmony_ci  const argument = { "value": "i32", "mutable": true };
1271cb0ef41Sopenharmony_ci  const global = new WebAssembly.Global(argument);
1281cb0ef41Sopenharmony_ci  const desc = Object.getOwnPropertyDescriptor(WebAssembly.Global.prototype, "value");
1291cb0ef41Sopenharmony_ci  assert_equals(typeof desc, "object");
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci  const setter = desc.set;
1321cb0ef41Sopenharmony_ci  assert_equals(typeof setter, "function");
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => setter.call(global));
1351cb0ef41Sopenharmony_ci}, "Calling setter without argument");
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_citest(() => {
1381cb0ef41Sopenharmony_ci  const argument = { "value": "i32", "mutable": true };
1391cb0ef41Sopenharmony_ci  const global = new WebAssembly.Global(argument);
1401cb0ef41Sopenharmony_ci  const desc = Object.getOwnPropertyDescriptor(WebAssembly.Global.prototype, "value");
1411cb0ef41Sopenharmony_ci  assert_equals(typeof desc, "object");
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  const getter = desc.get;
1441cb0ef41Sopenharmony_ci  assert_equals(typeof getter, "function");
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci  const setter = desc.set;
1471cb0ef41Sopenharmony_ci  assert_equals(typeof setter, "function");
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci  assert_equals(getter.call(global, {}), 0);
1501cb0ef41Sopenharmony_ci  assert_equals(setter.call(global, 1, {}), undefined);
1511cb0ef41Sopenharmony_ci  assert_equals(global.value, 1);
1521cb0ef41Sopenharmony_ci}, "Stray argument");
153