summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/BigInt/wrapper-object-ordinary-toprimitive.js
blob: 99cd836ef2c3c80dab8473d6e9fa0a02e191a18d (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
// Copyright (C) 2021 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-toprimitive
description: >
    BigInt wrapper object is converted to primitive via OrdinaryToPrimitive.
info: |
    ToPrimitive ( input [ , preferredType ] )

    [...]
    2. If Type(input) is Object, then
        a. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
        b. If exoticToPrim is not undefined, then
            [...]
        c. If preferredType is not present, let preferredType be number.
        d. Return ? OrdinaryToPrimitive(input, preferredType).
features: [BigInt]
---*/

const BigIntToString = BigInt.prototype.toString;
let toStringGets = 0;
let toStringCalls = 0;
let toStringFunction = function() { ++toStringCalls; return `${BigIntToString.call(this)}foo`; };
Object.defineProperty(BigInt.prototype, "toString", {
    get: () => { ++toStringGets; return toStringFunction; },
});

assert.sameValue("" + Object(1n), "1", "hint: default");
assert.throws(TypeError, () => { +Object(1n); }, "hint: number");
assert.sameValue(`${Object(1n)}`, "1foo", "hint: string");

assert.sameValue(toStringGets, 1);
assert.sameValue(toStringCalls, 1);

const BigIntValueOf = BigInt.prototype.valueOf;
let valueOfGets = 0;
let valueOfCalls = 0;
let valueOfFunction = function() { ++valueOfCalls; return BigIntValueOf.call(this) * 2n; };
Object.defineProperty(BigInt.prototype, "valueOf", {
    get: () => { ++valueOfGets; return valueOfFunction; },
});

assert(Object(1n) == 2n, "hint: default");
assert.sameValue(Object(1n) + 1n, 3n, "hint: number");
assert.sameValue({ "1foo": 1, "2": 2 }[Object(1n)], 1, "hint: string");

assert.sameValue(toStringGets, 2);
assert.sameValue(toStringCalls, 2);
assert.sameValue(valueOfGets, 2);
assert.sameValue(valueOfCalls, 2);

toStringFunction = undefined;

assert.throws(TypeError, () => { 1 + Object(1n); }, "hint: default");
assert.sameValue(Object(1n) * 1n, 2n, "hint: number");
assert.sameValue("".concat(Object(1n)), "2", "hint: string");

assert.sameValue(toStringGets, 3);
assert.sameValue(toStringCalls, 2);
assert.sameValue(valueOfGets, 5);
assert.sameValue(valueOfCalls, 5);

valueOfFunction = null;

assert.throws(TypeError, () => { new Date(Object(1n)); }, "hint: default");
assert.throws(TypeError, () => { Number(Object(1n)); }, "hint: number");
assert.throws(TypeError, () => { String(Object(1n)); }, "hint: string");

assert.sameValue(toStringGets, 6);
assert.sameValue(toStringCalls, 2);
assert.sameValue(valueOfGets, 8);
assert.sameValue(valueOfCalls, 5);

reportCompare(0, 0);