summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/ShadowRealm/prototype/importValue/specifier-tostring.js
blob: ec3e17ccd5d17b0ae387e079f19c47f714838ca7 (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
// |reftest| shell-option(--enable-shadow-realms) skip-if(!xulRuntime.shell) -- requires shell-options
// Copyright (C) 2021 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-shadowrealm.prototype.importvalue
description: >
  ShadowRealm.prototype.importValue coerces specifier to string.
features: [ShadowRealm]
---*/

assert.sameValue(
  typeof ShadowRealm.prototype.importValue,
  'function',
  'This test must fail if ShadowRealm.prototype.importValue is not a function'
);

const r = new ShadowRealm();
let count = 0;

const specifier = Object.create(null);

// A - valueOF

specifier.valueOf = function() {
  count += 1;
  throw new Test262Error();
};

assert.throws(Test262Error, () => {
  r.importValue(specifier);
}, 'ToString(specifier) returns abrupt from valueOf');

assert.sameValue(count, 1, 'ToString calls the valueOf method');


// B - toString

count = 0;

specifier.valueOf = function() {
  count += 1000;
  throw new Error('valueOf is not reached if toString is present');
};

specifier.toString = function() {
  count += 1;
  throw new Test262Error();
};

assert.throws(Test262Error, () => {
  r.importValue(specifier);
}, 'ToString(specifier) returns abrupt from toString');

assert.sameValue(count, 1, 'ToString calls the toString method');

// C - @@toPrimitive

count = 0;

specifier[Symbol.toPrimitive] = function() {
  count += 1;
  throw new Test262Error();
};

specifier.toString = function() {
  count += 1000;
  throw new Error('toString is not reached if @@toPrimitive is present');
};

assert.throws(Test262Error, () => {
  r.importValue(specifier);
}, 'ToString(specifier) returns abrupt from @@toPrimitive');

assert.sameValue(count, 1, 'ToString calls the @@toPrimitive method');

reportCompare(0, 0);