From 4c0322569c1c7da651ada37935ba2ab98523e5b4 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Tue, 2 Aug 2022 16:25:11 -0700 Subject: [PATCH] add tests for "only coerce once in BigInt constructor" --- test/built-ins/BigInt/constructor-coercion.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/built-ins/BigInt/constructor-coercion.js diff --git a/test/built-ins/BigInt/constructor-coercion.js b/test/built-ins/BigInt/constructor-coercion.js new file mode 100644 index 0000000000..50068afae5 --- /dev/null +++ b/test/built-ins/BigInt/constructor-coercion.js @@ -0,0 +1,27 @@ +// Copyright (C) 2022 Kevin Gibbons. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: BigInt constructor only coerces its input once +esid: sec-bigint-constructor-number-value +info: | + BigInt ( value ) + 1. If NewTarget is not undefined, throw a TypeError exception. + 2. Let prim be ? ToPrimitive(value, number). + 3. If Type(prim) is Number, return ? NumberToBigInt(prim). + 4. Otherwise, return ? ToBigInt(prim). +features: [BigInt] +---*/ + +var first = true; +var v = { + [Symbol.toPrimitive]: function() { + if (first) { + first = false; + return "42"; + } + throw new Test262Error("Symbol.toPrimitive should only be invoked once"); + }, +}; + +assert.sameValue(BigInt(v), 42n, "BigInt constructor should use the post-ToPrimitive value as the argument to ToBigInt");