| 1 |
// Copyright (C) 2021 Alexey Shvayka. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
/*--- |
| 4 |
esid: sec-toprimitive |
| 5 |
description: > |
| 6 |
BigInt wrapper object is converted to primitive via OrdinaryToPrimitive. |
| 7 |
info: | |
| 8 |
ToPrimitive ( input [ , preferredType ] ) |
| 9 |
|
| 10 |
[...] |
| 11 |
2. If Type(input) is Object, then |
| 12 |
a. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). |
| 13 |
b. If exoticToPrim is not undefined, then |
| 14 |
[...] |
| 15 |
c. If preferredType is not present, let preferredType be number. |
| 16 |
d. Return ? OrdinaryToPrimitive(input, preferredType). |
| 17 |
features: [BigInt] |
| 18 |
---*/ |
| 19 |
|
| 20 |
const BigIntToString = BigInt.prototype.toString; |
| 21 |
let toStringGets = 0; |
| 22 |
let toStringCalls = 0; |
| 23 |
let toStringFunction = function() { ++toStringCalls; return `${BigIntToString.call(this)}foo`; }; |
| 24 |
Object.defineProperty(BigInt.prototype, "toString", { |
| 25 |
get: () => { ++toStringGets; return toStringFunction; }, |
| 26 |
}); |
| 27 |
|
| 28 |
assert.sameValue("" + Object(1n), "1", "hint: default"); |
| 29 |
assert.throws(TypeError, () => { +Object(1n); }, "hint: number"); |
| 30 |
assert.sameValue(`${Object(1n)}`, "1foo", "hint: string"); |
| 31 |
|
| 32 |
assert.sameValue(toStringGets, 1); |
| 33 |
assert.sameValue(toStringCalls, 1); |
| 34 |
|
| 35 |
const BigIntValueOf = BigInt.prototype.valueOf; |
| 36 |
let valueOfGets = 0; |
| 37 |
let valueOfCalls = 0; |
| 38 |
let valueOfFunction = function() { ++valueOfCalls; return BigIntValueOf.call(this) * 2n; }; |
| 39 |
Object.defineProperty(BigInt.prototype, "valueOf", { |
| 40 |
get: () => { ++valueOfGets; return valueOfFunction; }, |
| 41 |
}); |
| 42 |
|
| 43 |
assert(Object(1n) == 2n, "hint: default"); |
| 44 |
assert.sameValue(Object(1n) + 1n, 3n, "hint: number"); |
| 45 |
assert.sameValue({ "1foo": 1, "2": 2 }[Object(1n)], 1, "hint: string"); |
| 46 |
|
| 47 |
assert.sameValue(toStringGets, 2); |
| 48 |
assert.sameValue(toStringCalls, 2); |
| 49 |
assert.sameValue(valueOfGets, 2); |
| 50 |
assert.sameValue(valueOfCalls, 2); |
| 51 |
|
| 52 |
toStringFunction = undefined; |
| 53 |
|
| 54 |
assert.throws(TypeError, () => { 1 + Object(1n); }, "hint: default"); |
| 55 |
assert.sameValue(Object(1n) * 1n, 2n, "hint: number"); |
| 56 |
assert.sameValue("".concat(Object(1n)), "2", "hint: string"); |
| 57 |
|
| 58 |
assert.sameValue(toStringGets, 3); |
| 59 |
assert.sameValue(toStringCalls, 2); |
| 60 |
assert.sameValue(valueOfGets, 5); |
| 61 |
assert.sameValue(valueOfCalls, 5); |
| 62 |
|
| 63 |
valueOfFunction = null; |
| 64 |
|
| 65 |
assert.throws(TypeError, () => { new Date(Object(1n)); }, "hint: default"); |
| 66 |
assert.throws(TypeError, () => { Number(Object(1n)); }, "hint: number"); |
| 67 |
assert.throws(TypeError, () => { String(Object(1n)); }, "hint: string"); |
| 68 |
|
| 69 |
assert.sameValue(toStringGets, 6); |
| 70 |
assert.sameValue(toStringCalls, 2); |
| 71 |
assert.sameValue(valueOfGets, 8); |
| 72 |
assert.sameValue(valueOfCalls, 5); |
| 73 |
|
| 74 |
reportCompare(0, 0); |