From 0060e90f70cd50069f217b080fd918147ed4ccb2 Mon Sep 17 00:00:00 2001 From: jakobkummerow Date: Tue, 15 Aug 2023 13:02:54 +0200 Subject: [PATCH] Fix construction from [Symbol.toPrimitive] (#102) Fixes #101. --- lib/jsbi.ts | 5 ++--- tests/tests.mjs | 10 ++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/jsbi.ts b/lib/jsbi.ts index 8f8878d..182e0d7 100644 --- a/lib/jsbi.ts +++ b/lib/jsbi.ts @@ -1819,9 +1819,8 @@ class JSBI extends Array { if (obj.constructor === JSBI) return obj; if (typeof Symbol !== 'undefined' && typeof Symbol.toPrimitive === 'symbol') { - const exoticToPrim = obj[Symbol.toPrimitive]; - if (exoticToPrim) { - const primitive = exoticToPrim(hint); + if (obj[Symbol.toPrimitive]) { + const primitive = obj[Symbol.toPrimitive](hint); if (typeof primitive !== 'object') return primitive; throw new TypeError('Cannot convert object to primitive value'); } diff --git a/tests/tests.mjs b/tests/tests.mjs index 62d664c..b20572a 100644 --- a/tests/tests.mjs +++ b/tests/tests.mjs @@ -131,6 +131,16 @@ const TESTS = [ } })(); +// https://github.com/GoogleChromeLabs/jsbi/issues/101 +(function() { + const o = { + num: 123, + [Symbol.toPrimitive]: function() { return this.num; } + }; + const result = JSBI.BigInt(o); + assertTrue(JSBI.equal(result, JSBI.BigInt(123))); +})(); + function parse(string) { if (string.charCodeAt(0) === 0x2D) { // '-' return JSBI.unaryMinus(JSBI.BigInt(string.slice(1)));