summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/wasm/jsapi/instance/exports.any.js
blob: f7244923d83c51037cfe6186da49ee909b11498f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// META: global=window,dedicatedworker,jsshell,shadowrealm
// META: script=/wasm/jsapi/wasm-module-builder.js

let emptyModuleBinary;
setup(() => {
  emptyModuleBinary = new WasmModuleBuilder().toBuffer();
});

test(() => {
  const thisValues = [
    undefined,
    null,
    true,
    "",
    Symbol(),
    1,
    {},
    WebAssembly.Instance,
    WebAssembly.Instance.prototype,
  ];

  const desc = Object.getOwnPropertyDescriptor(WebAssembly.Instance.prototype, "exports");
  assert_equals(typeof desc, "object");

  const getter = desc.get;
  assert_equals(typeof getter, "function");

  assert_equals(typeof desc.set, "undefined");

  for (const thisValue of thisValues) {
    assert_throws_js(TypeError, () => getter.call(thisValue), `this=${format_value(thisValue)}`);
  }
}, "Branding");

test(() => {
  const module = new WebAssembly.Module(emptyModuleBinary);
  const instance = new WebAssembly.Instance(module);
  const exports = instance.exports;

  const desc = Object.getOwnPropertyDescriptor(WebAssembly.Instance.prototype, "exports");
  assert_equals(typeof desc, "object");

  const getter = desc.get;
  assert_equals(typeof getter, "function");

  assert_equals(getter.call(instance, {}), exports);
}, "Stray argument");

test(() => {
  const module = new WebAssembly.Module(emptyModuleBinary);
  const instance = new WebAssembly.Instance(module);
  const exports = instance.exports;
  instance.exports = {};
  assert_equals(instance.exports, exports, "Should not change the exports");
}, "Setting (sloppy mode)");

test(() => {
  const module = new WebAssembly.Module(emptyModuleBinary);
  const instance = new WebAssembly.Instance(module);
  const exports = instance.exports;
  assert_throws_js(TypeError, () => {
    "use strict";
    instance.exports = {};
  });
  assert_equals(instance.exports, exports, "Should not change the exports");
}, "Setting (strict mode)");