diff options
author | Felix Lange <fjl@twurst.com> | 2016-04-13 00:52:40 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2016-04-13 00:52:40 +0800 |
commit | 9d81f4fdd142de698d0129c75d11be8c66ab3f83 (patch) | |
tree | 81234b558c398430a6c3f820eacbd83e9677c7cc | |
parent | f460b0217fd8de6de90814e53e7ba4e2e86aff15 (diff) | |
parent | 4e85be0717d298e2cd406c84cf321cd856f1e94f (diff) | |
download | dexon-9d81f4fdd142de698d0129c75d11be8c66ab3f83.tar dexon-9d81f4fdd142de698d0129c75d11be8c66ab3f83.tar.gz dexon-9d81f4fdd142de698d0129c75d11be8c66ab3f83.tar.bz2 dexon-9d81f4fdd142de698d0129c75d11be8c66ab3f83.tar.lz dexon-9d81f4fdd142de698d0129c75d11be8c66ab3f83.tar.xz dexon-9d81f4fdd142de698d0129c75d11be8c66ab3f83.tar.zst dexon-9d81f4fdd142de698d0129c75d11be8c66ab3f83.zip |
Merge pull request #2448 from fjl/jsre-bignum
jsre: print BigNumber objects with custom constructor as number
-rw-r--r-- | jsre/pretty.go | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/jsre/pretty.go b/jsre/pretty.go index d0b42cd01..8b2b35e8e 100644 --- a/jsre/pretty.go +++ b/jsre/pretty.go @@ -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 { |