summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/wasm/jsapi/proto-from-ctor-realm.html
blob: 45405b52900bf95554635293502338b07026e62f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<meta charset="utf-8">
<title>WebAssembly JS API: Default [[Prototype]] value is from NewTarget's Realm</title>
<link rel="help" href="https://webidl.spec.whatwg.org/#internally-create-a-new-object-implementing-the-interface">
<link rel="help" href="https://tc39.es/ecma262/#sec-nativeerror">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="wasm-module-builder.js"></script>
<body>
<iframe id="constructor-iframe" hidden></iframe>
<iframe id="new-target-iframe" hidden></iframe>
<iframe id="other-iframe" hidden></iframe>
<script>
"use strict";

const constructorRealm = document.querySelector("#constructor-iframe").contentWindow;
const newTargetRealm = document.querySelector("#new-target-iframe").contentWindow;
const otherRealm = document.querySelector("#other-iframe").contentWindow;

const emptyModuleBinary = new WasmModuleBuilder().toBuffer();
const interfaces = [
  ["Module", emptyModuleBinary],
  ["Instance", new WebAssembly.Module(emptyModuleBinary)],
  ["Memory", {initial: 0}],
  ["Table", {element: "anyfunc", initial: 0}],
  ["Global", {value: "i32"}],

  // See step 2 of https://tc39.es/ecma262/#sec-nativeerror
  ["CompileError"],
  ["LinkError"],
  ["RuntimeError"],
];

const primitives = [
  undefined,
  null,
  false,
  true,
  0,
  -1,
  "",
  "str",
  Symbol(),
];

const getNewTargets = function* (realm) {
  for (const primitive of primitives) {
    const newTarget = new realm.Function();
    newTarget.prototype = primitive;
    yield [newTarget, "cross-realm NewTarget with `" + format_value(primitive) + "` prototype"];
  }

  // GetFunctionRealm (https://tc39.es/ecma262/#sec-getfunctionrealm) coverage:
  const bindOther = otherRealm.Function.prototype.bind;
  const ProxyOther = otherRealm.Proxy;

  const bound = new realm.Function();
  bound.prototype = undefined;
  yield [bindOther.call(bound), "bound cross-realm NewTarget with `undefined` prototype"];

  const boundBound = new realm.Function();
  boundBound.prototype = null;
  yield [bindOther.call(bindOther.call(boundBound)), "bound bound cross-realm NewTarget with `null` prototype"];

  const boundProxy = new realm.Function();
  boundProxy.prototype = false;
  yield [bindOther.call(new ProxyOther(boundProxy, {})), "bound Proxy of cross-realm NewTarget with `false` prototype"];

  const proxy = new realm.Function();
  proxy.prototype = true;
  yield [new ProxyOther(proxy, {}), "Proxy of cross-realm NewTarget with `true` prototype"];

  const proxyProxy = new realm.Function();
  proxyProxy.prototype = -0;
  yield [new ProxyOther(new ProxyOther(proxyProxy, {}), {}), "Proxy of Proxy of cross-realm NewTarget with `-0` prototype"];

  const proxyBound = new realm.Function();
  proxyBound.prototype = NaN;
  yield [new ProxyOther(bindOther.call(proxyBound), {}), "Proxy of bound cross-realm NewTarget with `NaN` prototype"];
};

for (const [interfaceName, constructorArg] of interfaces) {
  for (const [newTarget, testDescription] of getNewTargets(newTargetRealm)) {
    test(() => {
      const Constructor = constructorRealm.WebAssembly[interfaceName];
      const object = Reflect.construct(Constructor, [constructorArg], newTarget);

      const NewTargetConstructor = newTargetRealm.WebAssembly[interfaceName];
      assert_true(object instanceof NewTargetConstructor);
      assert_equals(Object.getPrototypeOf(object), NewTargetConstructor.prototype);
    }, `WebAssembly.${interfaceName}: ${testDescription}`);
  }
}
</script>
</body>