2016-11-08 19:01:56 -06:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2015-08-11 11:14:46 -05:00
|
|
|
// 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 04:40:23 -05:00
|
|
|
"io"
|
2020-01-27 04:50:48 -06:00
|
|
|
"reflect"
|
2015-08-11 11:14:46 -05:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
"github.com/dop251/goja"
|
2015-08-11 11:14:46 -05:00
|
|
|
"github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxPrettyPrintLevel = 3
|
|
|
|
indentString = " "
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-05-06 04:40:23 -05: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 09:28:29 -05:00
|
|
|
ErrorColor = color.New(color.FgHiRed).SprintfFunc()
|
2015-08-11 11:14:46 -05: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.
|
2020-01-27 04:50:48 -06:00
|
|
|
func prettyPrint(vm *goja.Runtime, value goja.Value, w io.Writer) {
|
2016-05-06 04:40:23 -05:00
|
|
|
ppctx{vm: vm, w: w}.printValue(value, 0, false)
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
|
|
|
|
2016-05-11 09:28:29 -05:00
|
|
|
// prettyError writes err to standard output.
|
2020-01-27 04:50:48 -06:00
|
|
|
func prettyError(vm *goja.Runtime, err error, w io.Writer) {
|
2016-05-11 09:28:29 -05:00
|
|
|
failure := err.Error()
|
2020-01-27 04:50:48 -06:00
|
|
|
if gojaErr, ok := err.(*goja.Exception); ok {
|
|
|
|
failure = gojaErr.String()
|
2016-05-11 09:28:29 -05:00
|
|
|
}
|
|
|
|
fmt.Fprint(w, ErrorColor("%s", failure))
|
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
func (re *JSRE) prettyPrintJS(call goja.FunctionCall) goja.Value {
|
|
|
|
for _, v := range call.Arguments {
|
|
|
|
prettyPrint(re.vm, v, re.output)
|
2016-12-10 18:53:34 -06:00
|
|
|
fmt.Fprintln(re.output)
|
2015-08-15 17:55:17 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
return goja.Undefined()
|
2015-08-15 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
2016-05-06 04:40:23 -05:00
|
|
|
type ppctx struct {
|
2020-01-27 04:50:48 -06:00
|
|
|
vm *goja.Runtime
|
2016-05-06 04:40:23 -05:00
|
|
|
w io.Writer
|
|
|
|
}
|
2015-08-11 11:14:46 -05:00
|
|
|
|
|
|
|
func (ctx ppctx) indent(level int) string {
|
|
|
|
return strings.Repeat(indentString, level)
|
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
func (ctx ppctx) printValue(v goja.Value, level int, inArray bool) {
|
|
|
|
if goja.IsNull(v) || goja.IsUndefined(v) {
|
|
|
|
fmt.Fprint(ctx.w, SpecialColor(v.String()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
kind := v.ExportType().Kind()
|
2015-08-11 11:14:46 -05:00
|
|
|
switch {
|
2020-01-27 04:50:48 -06:00
|
|
|
case kind == reflect.Bool:
|
|
|
|
fmt.Fprint(ctx.w, SpecialColor("%t", v.ToBoolean()))
|
|
|
|
case kind == reflect.String:
|
|
|
|
fmt.Fprint(ctx.w, StringColor("%q", v.String()))
|
|
|
|
case kind >= reflect.Int && kind <= reflect.Complex128:
|
|
|
|
fmt.Fprint(ctx.w, NumberColor("%s", v.String()))
|
2015-08-11 11:14:46 -05:00
|
|
|
default:
|
2020-01-27 04:50:48 -06:00
|
|
|
if obj, ok := v.(*goja.Object); ok {
|
|
|
|
ctx.printObject(obj, level, inArray)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(ctx.w, "<unprintable %T>", v)
|
|
|
|
}
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
// SafeGet attempt to get the value associated to `key`, and
|
|
|
|
// catches the panic that goja creates if an error occurs in
|
|
|
|
// key.
|
|
|
|
func SafeGet(obj *goja.Object, key string) (ret goja.Value) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
ret = goja.Undefined()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ret = obj.Get(key)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx ppctx) printObject(obj *goja.Object, level int, inArray bool) {
|
|
|
|
switch obj.ClassName() {
|
2016-07-12 10:47:15 -05:00
|
|
|
case "Array", "GoArray":
|
2020-01-27 04:50:48 -06:00
|
|
|
lv := obj.Get("length")
|
|
|
|
len := lv.ToInteger()
|
2015-08-11 11:14:46 -05:00
|
|
|
if len == 0 {
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprintf(ctx.w, "[]")
|
2015-08-11 11:14:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if level > maxPrettyPrintLevel {
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprint(ctx.w, "[...]")
|
2015-08-11 11:14:46 -05:00
|
|
|
return
|
|
|
|
}
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprint(ctx.w, "[")
|
2015-08-11 11:14:46 -05:00
|
|
|
for i := int64(0); i < len; i++ {
|
2020-01-27 04:50:48 -06:00
|
|
|
el := obj.Get(strconv.FormatInt(i, 10))
|
|
|
|
if el != nil {
|
2015-08-15 17:56:05 -05:00
|
|
|
ctx.printValue(el, level+1, true)
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
|
|
|
if i < len-1 {
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprintf(ctx.w, ", ")
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprint(ctx.w, "]")
|
2015-08-11 11:14:46 -05:00
|
|
|
|
|
|
|
case "Object":
|
|
|
|
// Print values from bignumber.js as regular numbers.
|
|
|
|
if ctx.isBigNumber(obj) {
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprint(ctx.w, NumberColor("%s", toString(obj)))
|
2015-08-11 11:14:46 -05: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 04:40:23 -05:00
|
|
|
fmt.Fprint(ctx.w, "{}")
|
2015-08-11 11:14:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if level > maxPrettyPrintLevel {
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprint(ctx.w, "{...}")
|
2015-08-11 11:14:46 -05:00
|
|
|
return
|
|
|
|
}
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprintln(ctx.w, "{")
|
2015-08-11 11:14:46 -05:00
|
|
|
for i, k := range keys {
|
2020-01-27 04:50:48 -06:00
|
|
|
v := SafeGet(obj, k)
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprintf(ctx.w, "%s%s: ", ctx.indent(level+1), k)
|
2015-08-15 17:56:05 -05:00
|
|
|
ctx.printValue(v, level+1, false)
|
2015-08-11 11:14:46 -05:00
|
|
|
if i < len(keys)-1 {
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprintf(ctx.w, ",")
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprintln(ctx.w)
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
2015-08-15 17:56:05 -05:00
|
|
|
if inArray {
|
|
|
|
level--
|
|
|
|
}
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprintf(ctx.w, "%s}", ctx.indent(level))
|
2015-08-11 11:14:46 -05:00
|
|
|
|
|
|
|
case "Function":
|
2020-01-27 04:50:48 -06:00
|
|
|
robj := obj.ToString()
|
|
|
|
desc := strings.Trim(strings.Split(robj.String(), "{")[0], " \t\n")
|
|
|
|
desc = strings.Replace(desc, " (", "(", 1)
|
|
|
|
fmt.Fprint(ctx.w, FunctionColor("%s", desc))
|
2015-08-11 11:14:46 -05:00
|
|
|
|
|
|
|
case "RegExp":
|
2016-05-06 04:40:23 -05:00
|
|
|
fmt.Fprint(ctx.w, StringColor("%s", toString(obj)))
|
2015-08-11 11:14:46 -05:00
|
|
|
|
|
|
|
default:
|
2020-01-27 04:50:48 -06:00
|
|
|
if level <= maxPrettyPrintLevel {
|
|
|
|
s := obj.ToString().String()
|
|
|
|
fmt.Fprintf(ctx.w, "<%s %s>", obj.ClassName(), s)
|
2015-08-11 11:14:46 -05:00
|
|
|
} else {
|
2020-01-27 04:50:48 -06:00
|
|
|
fmt.Fprintf(ctx.w, "<%s>", obj.ClassName())
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
func (ctx ppctx) fields(obj *goja.Object) []string {
|
2015-08-11 11:14:46 -05:00
|
|
|
var (
|
|
|
|
vals, methods []string
|
|
|
|
seen = make(map[string]bool)
|
|
|
|
)
|
|
|
|
add := func(k string) {
|
2016-02-20 08:47:25 -06:00
|
|
|
if seen[k] || boringKeys[k] || strings.HasPrefix(k, "_") {
|
2015-08-11 11:14:46 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
seen[k] = true
|
2020-01-27 04:50:48 -06:00
|
|
|
|
|
|
|
key := SafeGet(obj, k)
|
|
|
|
if key == nil {
|
|
|
|
// The value corresponding to that key could not be found
|
|
|
|
// (typically because it is backed by an RPC call that is
|
|
|
|
// not supported by this instance. Add it to the list of
|
|
|
|
// values so that it appears as `undefined` to the user.
|
2015-08-11 11:14:46 -05:00
|
|
|
vals = append(vals, k)
|
2020-01-27 04:50:48 -06:00
|
|
|
} else {
|
|
|
|
if _, callable := goja.AssertFunction(key); callable {
|
|
|
|
methods = append(methods, k)
|
|
|
|
} else {
|
|
|
|
vals = append(vals, k)
|
|
|
|
}
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
|
|
|
}
|
2016-02-15 09:42:39 -06:00
|
|
|
iterOwnAndConstructorKeys(ctx.vm, obj, add)
|
2015-08-11 11:14:46 -05:00
|
|
|
sort.Strings(vals)
|
|
|
|
sort.Strings(methods)
|
|
|
|
return append(vals, methods...)
|
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
func iterOwnAndConstructorKeys(vm *goja.Runtime, obj *goja.Object, f func(string)) {
|
2016-02-15 09:42:39 -06:00
|
|
|
seen := make(map[string]bool)
|
|
|
|
iterOwnKeys(vm, obj, func(prop string) {
|
|
|
|
seen[prop] = true
|
|
|
|
f(prop)
|
|
|
|
})
|
2020-01-27 04:50:48 -06:00
|
|
|
if cp := constructorPrototype(vm, obj); cp != nil {
|
2016-02-15 09:42:39 -06:00
|
|
|
iterOwnKeys(vm, cp, func(prop string) {
|
|
|
|
if !seen[prop] {
|
|
|
|
f(prop)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
func iterOwnKeys(vm *goja.Runtime, obj *goja.Object, f func(string)) {
|
|
|
|
Object := vm.Get("Object").ToObject(vm)
|
|
|
|
getOwnPropertyNames, isFunc := goja.AssertFunction(Object.Get("getOwnPropertyNames"))
|
|
|
|
if !isFunc {
|
|
|
|
panic(vm.ToValue("Object.getOwnPropertyNames isn't a function"))
|
|
|
|
}
|
|
|
|
rv, err := getOwnPropertyNames(goja.Null(), obj)
|
|
|
|
if err != nil {
|
|
|
|
panic(vm.ToValue(fmt.Sprintf("Error getting object properties: %v", err)))
|
|
|
|
}
|
|
|
|
gv := rv.Export()
|
2016-02-11 19:19:52 -06: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 11:14:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
func (ctx ppctx) isBigNumber(v *goja.Object) bool {
|
2016-04-12 10:42:14 -05:00
|
|
|
// Handle numbers with custom constructor.
|
2020-01-27 04:50:48 -06:00
|
|
|
if obj := v.Get("constructor").ToObject(ctx.vm); obj != nil {
|
|
|
|
if strings.HasPrefix(toString(obj), "function BigNumber") {
|
2016-04-12 10:42:14 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Handle default constructor.
|
2020-01-27 04:50:48 -06:00
|
|
|
BigNumber := ctx.vm.Get("BigNumber").ToObject(ctx.vm)
|
2016-04-12 10:42:14 -05:00
|
|
|
if BigNumber == nil {
|
|
|
|
return false
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
2020-01-27 04:50:48 -06:00
|
|
|
prototype := BigNumber.Get("prototype").ToObject(ctx.vm)
|
|
|
|
isPrototypeOf, callable := goja.AssertFunction(prototype.Get("isPrototypeOf"))
|
|
|
|
if !callable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
bv, _ := isPrototypeOf(prototype, v)
|
|
|
|
return bv.ToBoolean()
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
func toString(obj *goja.Object) string {
|
|
|
|
return obj.ToString().String()
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
|
|
|
|
2020-01-27 04:50:48 -06:00
|
|
|
func constructorPrototype(vm *goja.Runtime, obj *goja.Object) *goja.Object {
|
|
|
|
if v := obj.Get("constructor"); v != nil {
|
|
|
|
if v := v.ToObject(vm).Get("prototype"); v != nil {
|
|
|
|
return v.ToObject(vm)
|
2015-08-11 11:14:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|