Skip to content

Commit 4e85be0

Browse files
committed
jsre: print BigNumber objects with custom constructor as number
1 parent f460b02 commit 4e85be0

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

jsre/pretty.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,20 @@ func iterOwnKeys(vm *otto.Otto, obj *otto.Object, f func(string)) {
227227
}
228228

229229
func (ctx ppctx) isBigNumber(v *otto.Object) bool {
230-
BigNumber, err := ctx.vm.Run("BigNumber.prototype")
231-
if err != nil {
232-
panic(err)
230+
// Handle numbers with custom constructor.
231+
if v, _ := v.Get("constructor"); v.Object() != nil {
232+
if strings.HasPrefix(toString(v.Object()), "function BigNumber") {
233+
return true
234+
}
235+
}
236+
// Handle default constructor.
237+
BigNumber, _ := ctx.vm.Object("BigNumber.prototype")
238+
if BigNumber == nil {
239+
return false
233240
}
234-
cp := constructorPrototype(v)
235-
return cp != nil && cp.Value() == BigNumber
241+
bv, _ := BigNumber.Call("isPrototypeOf", v)
242+
b, _ := bv.ToBoolean()
243+
return b
236244
}
237245

238246
func toString(obj *otto.Object) string {

0 commit comments

Comments
 (0)