Skip to content

Commit

Permalink
Fix construction from [Symbol.toPrimitive] (#102)
Browse files Browse the repository at this point in the history
Fixes #101.
  • Loading branch information
jakobkummerow authored Aug 15, 2023
1 parent c40371e commit 0060e90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/jsbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
10 changes: 10 additions & 0 deletions tests/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down

0 comments on commit 0060e90

Please sign in to comment.