Skip to content

Commit

Permalink
Merge pull request #2448 from fjl/jsre-bignum
Browse files Browse the repository at this point in the history
jsre: print BigNumber objects with custom constructor as number
  • Loading branch information
fjl committed Apr 12, 2016
2 parents f460b02 + 4e85be0 commit 9d81f4f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions jsre/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,20 @@ func iterOwnKeys(vm *otto.Otto, obj *otto.Object, f func(string)) {
}

func (ctx ppctx) isBigNumber(v *otto.Object) bool {
BigNumber, err := ctx.vm.Run("BigNumber.prototype")
if err != nil {
panic(err)
// Handle numbers with custom constructor.
if v, _ := v.Get("constructor"); v.Object() != nil {
if strings.HasPrefix(toString(v.Object()), "function BigNumber") {
return true
}
}
// Handle default constructor.
BigNumber, _ := ctx.vm.Object("BigNumber.prototype")
if BigNumber == nil {
return false
}
cp := constructorPrototype(v)
return cp != nil && cp.Value() == BigNumber
bv, _ := BigNumber.Call("isPrototypeOf", v)
b, _ := bv.ToBoolean()
return b
}

func toString(obj *otto.Object) string {
Expand Down

0 comments on commit 9d81f4f

Please sign in to comment.