2015-08-11 16:14:46 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package jsre
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-06 09:40:23 +00:00
|
|
|
"io"
|
2015-08-11 16:14:46 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/robertkrimen/otto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxPrettyPrintLevel = 3
|
|
|
|
indentString = " "
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-05-06 09:40:23 +00:00
|
|
|
FunctionColor = color.New(color.FgMagenta).SprintfFunc()
|
|
|
|
SpecialColor = color.New(color.Bold).SprintfFunc()
|
|
|
|
NumberColor = color.New(color.FgRed).SprintfFunc()
|
|
|
|
StringColor = color.New(color.FgGreen).SprintfFunc()
|
2016-05-11 14:28:29 +00:00
|
|
|
ErrorColor = color.New(color.FgHiRed).SprintfFunc()
|
2015-08-11 16:14:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// these fields are hidden when printing objects.
|
|
|
|
var boringKeys = map[string]bool{
|
|
|
|
"valueOf": true,
|
|
|
|
"toString": true,
|
|
|
|
"toLocaleString": true,
|
|
|
|
"hasOwnProperty": true,
|
|
|
|
"isPrototypeOf": true,
|
|
|
|
"propertyIsEnumerable": true,
|
|
|
|
"constructor": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// prettyPrint writes value to standard output.
|
2016-05-06 09:40:23 +00:00
|
|
|
func prettyPrint(vm *otto.Otto, value otto.Value, w io.Writer) {
|
|
|
|
ppctx{vm: vm, w: w}.printValue(value, 0, false)
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 14:28:29 +00:00
|
|
|
// prettyError writes err to standard output.
|
|
|
|
func prettyError(vm *otto.Otto, err error, w io.Writer) {
|
|
|
|
failure := err.Error()
|
|
|
|
if ottoErr, ok := err.(*otto.Error); ok {
|
|
|
|
failure = ottoErr.String()
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, ErrorColor("%s", failure))
|
|
|
|
}
|
|
|
|
|
|
|
|
// jsErrorString adds a backtrace to errors generated by otto.
|
|
|
|
func jsErrorString(err error) string {
|
|
|
|
if ottoErr, ok := err.(*otto.Error); ok {
|
|
|
|
return ottoErr.String()
|
|
|
|
}
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
func prettyPrintJS(call otto.FunctionCall, w io.Writer) otto.Value {
|
2015-08-15 22:55:17 +00:00
|
|
|
for _, v := range call.ArgumentList {
|
2016-05-06 09:40:23 +00:00
|
|
|
prettyPrint(call.Otto, v, w)
|
|
|
|
fmt.Fprintln(w)
|
2015-08-15 22:55:17 +00:00
|
|
|
}
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
type ppctx struct {
|
|
|
|
vm *otto.Otto
|
|
|
|
w io.Writer
|
|
|
|
}
|
2015-08-11 16:14:46 +00:00
|
|
|
|
|
|
|
func (ctx ppctx) indent(level int) string {
|
|
|
|
return strings.Repeat(indentString, level)
|
|
|
|
}
|
|
|
|
|
2015-08-15 22:56:05 +00:00
|
|
|
func (ctx ppctx) printValue(v otto.Value, level int, inArray bool) {
|
2015-08-11 16:14:46 +00:00
|
|
|
switch {
|
|
|
|
case v.IsObject():
|
2015-08-15 22:56:05 +00:00
|
|
|
ctx.printObject(v.Object(), level, inArray)
|
2015-08-11 16:14:46 +00:00
|
|
|
case v.IsNull():
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, SpecialColor("null"))
|
2015-08-11 16:14:46 +00:00
|
|
|
case v.IsUndefined():
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, SpecialColor("undefined"))
|
2015-08-11 16:14:46 +00:00
|
|
|
case v.IsString():
|
|
|
|
s, _ := v.ToString()
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, StringColor("%q", s))
|
2015-08-11 16:14:46 +00:00
|
|
|
case v.IsBoolean():
|
|
|
|
b, _ := v.ToBoolean()
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, SpecialColor("%t", b))
|
2015-08-11 16:14:46 +00:00
|
|
|
case v.IsNaN():
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, NumberColor("NaN"))
|
2015-08-11 16:14:46 +00:00
|
|
|
case v.IsNumber():
|
|
|
|
s, _ := v.ToString()
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, NumberColor("%s", s))
|
2015-08-11 16:14:46 +00:00
|
|
|
default:
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, "<unprintable>")
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 22:56:05 +00:00
|
|
|
func (ctx ppctx) printObject(obj *otto.Object, level int, inArray bool) {
|
2015-08-11 16:14:46 +00:00
|
|
|
switch obj.Class() {
|
2016-07-12 15:47:15 +00:00
|
|
|
case "Array", "GoArray":
|
2015-08-11 16:14:46 +00:00
|
|
|
lv, _ := obj.Get("length")
|
|
|
|
len, _ := lv.ToInteger()
|
|
|
|
if len == 0 {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintf(ctx.w, "[]")
|
2015-08-11 16:14:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if level > maxPrettyPrintLevel {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, "[...]")
|
2015-08-11 16:14:46 +00:00
|
|
|
return
|
|
|
|
}
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, "[")
|
2015-08-11 16:14:46 +00:00
|
|
|
for i := int64(0); i < len; i++ {
|
|
|
|
el, err := obj.Get(strconv.FormatInt(i, 10))
|
|
|
|
if err == nil {
|
2015-08-15 22:56:05 +00:00
|
|
|
ctx.printValue(el, level+1, true)
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
|
|
|
if i < len-1 {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintf(ctx.w, ", ")
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, "]")
|
2015-08-11 16:14:46 +00:00
|
|
|
|
|
|
|
case "Object":
|
|
|
|
// Print values from bignumber.js as regular numbers.
|
|
|
|
if ctx.isBigNumber(obj) {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, NumberColor("%s", toString(obj)))
|
2015-08-11 16:14:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Otherwise, print all fields indented, but stop if we're too deep.
|
|
|
|
keys := ctx.fields(obj)
|
|
|
|
if len(keys) == 0 {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, "{}")
|
2015-08-11 16:14:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if level > maxPrettyPrintLevel {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, "{...}")
|
2015-08-11 16:14:46 +00:00
|
|
|
return
|
|
|
|
}
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintln(ctx.w, "{")
|
2015-08-11 16:14:46 +00:00
|
|
|
for i, k := range keys {
|
|
|
|
v, _ := obj.Get(k)
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintf(ctx.w, "%s%s: ", ctx.indent(level+1), k)
|
2015-08-15 22:56:05 +00:00
|
|
|
ctx.printValue(v, level+1, false)
|
2015-08-11 16:14:46 +00:00
|
|
|
if i < len(keys)-1 {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintf(ctx.w, ",")
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintln(ctx.w)
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
2015-08-15 22:56:05 +00:00
|
|
|
if inArray {
|
|
|
|
level--
|
|
|
|
}
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintf(ctx.w, "%s}", ctx.indent(level))
|
2015-08-11 16:14:46 +00:00
|
|
|
|
|
|
|
case "Function":
|
|
|
|
// Use toString() to display the argument list if possible.
|
|
|
|
if robj, err := obj.Call("toString"); err != nil {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, FunctionColor("function()"))
|
2015-08-11 16:14:46 +00:00
|
|
|
} else {
|
|
|
|
desc := strings.Trim(strings.Split(robj.String(), "{")[0], " \t\n")
|
|
|
|
desc = strings.Replace(desc, " (", "(", 1)
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, FunctionColor("%s", desc))
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "RegExp":
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprint(ctx.w, StringColor("%s", toString(obj)))
|
2015-08-11 16:14:46 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
if v, _ := obj.Get("toString"); v.IsFunction() && level <= maxPrettyPrintLevel {
|
|
|
|
s, _ := obj.Call("toString")
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintf(ctx.w, "<%s %s>", obj.Class(), s.String())
|
2015-08-11 16:14:46 +00:00
|
|
|
} else {
|
2016-05-06 09:40:23 +00:00
|
|
|
fmt.Fprintf(ctx.w, "<%s>", obj.Class())
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx ppctx) fields(obj *otto.Object) []string {
|
|
|
|
var (
|
|
|
|
vals, methods []string
|
|
|
|
seen = make(map[string]bool)
|
|
|
|
)
|
|
|
|
add := func(k string) {
|
2016-02-20 14:47:25 +00:00
|
|
|
if seen[k] || boringKeys[k] || strings.HasPrefix(k, "_") {
|
2015-08-11 16:14:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
seen[k] = true
|
|
|
|
if v, _ := obj.Get(k); v.IsFunction() {
|
|
|
|
methods = append(methods, k)
|
|
|
|
} else {
|
|
|
|
vals = append(vals, k)
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 15:42:39 +00:00
|
|
|
iterOwnAndConstructorKeys(ctx.vm, obj, add)
|
2015-08-11 16:14:46 +00:00
|
|
|
sort.Strings(vals)
|
|
|
|
sort.Strings(methods)
|
|
|
|
return append(vals, methods...)
|
|
|
|
}
|
|
|
|
|
2016-02-15 15:42:39 +00:00
|
|
|
func iterOwnAndConstructorKeys(vm *otto.Otto, obj *otto.Object, f func(string)) {
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
iterOwnKeys(vm, obj, func(prop string) {
|
|
|
|
seen[prop] = true
|
|
|
|
f(prop)
|
|
|
|
})
|
|
|
|
if cp := constructorPrototype(obj); cp != nil {
|
|
|
|
iterOwnKeys(vm, cp, func(prop string) {
|
|
|
|
if !seen[prop] {
|
|
|
|
f(prop)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func iterOwnKeys(vm *otto.Otto, obj *otto.Object, f func(string)) {
|
|
|
|
Object, _ := vm.Object("Object")
|
|
|
|
rv, _ := Object.Call("getOwnPropertyNames", obj.Value())
|
2015-08-11 16:14:46 +00:00
|
|
|
gv, _ := rv.Export()
|
2016-02-12 01:19:52 +00:00
|
|
|
switch gv := gv.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
for _, v := range gv {
|
|
|
|
f(v.(string))
|
|
|
|
}
|
|
|
|
case []string:
|
|
|
|
for _, v := range gv {
|
|
|
|
f(v)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("Object.getOwnPropertyNames returned unexpected type %T", gv))
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx ppctx) isBigNumber(v *otto.Object) bool {
|
2016-04-12 15:42:14 +00:00
|
|
|
// 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
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
2016-04-12 15:42:14 +00:00
|
|
|
bv, _ := BigNumber.Call("isPrototypeOf", v)
|
|
|
|
b, _ := bv.ToBoolean()
|
|
|
|
return b
|
2015-08-11 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toString(obj *otto.Object) string {
|
|
|
|
s, _ := obj.Call("toString")
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func constructorPrototype(obj *otto.Object) *otto.Object {
|
|
|
|
if v, _ := obj.Get("constructor"); v.Object() != nil {
|
|
|
|
if v, _ = v.Object().Get("prototype"); v.Object() != nil {
|
|
|
|
return v.Object()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|