pretty.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package jsre
  17. import (
  18. "fmt"
  19. "io"
  20. "reflect"
  21. "sort"
  22. "strconv"
  23. "strings"
  24. "github.com/dop251/goja"
  25. "github.com/fatih/color"
  26. )
  27. const (
  28. maxPrettyPrintLevel = 3
  29. indentString = " "
  30. )
  31. var (
  32. FunctionColor = color.New(color.FgMagenta).SprintfFunc()
  33. SpecialColor = color.New(color.Bold).SprintfFunc()
  34. NumberColor = color.New(color.FgRed).SprintfFunc()
  35. StringColor = color.New(color.FgGreen).SprintfFunc()
  36. ErrorColor = color.New(color.FgHiRed).SprintfFunc()
  37. )
  38. // these fields are hidden when printing objects.
  39. var boringKeys = map[string]bool{
  40. "valueOf": true,
  41. "toString": true,
  42. "toLocaleString": true,
  43. "hasOwnProperty": true,
  44. "isPrototypeOf": true,
  45. "propertyIsEnumerable": true,
  46. "constructor": true,
  47. }
  48. // prettyPrint writes value to standard output.
  49. func prettyPrint(vm *goja.Runtime, value goja.Value, w io.Writer) {
  50. ppctx{vm: vm, w: w}.printValue(value, 0, false)
  51. }
  52. // prettyError writes err to standard output.
  53. func prettyError(vm *goja.Runtime, err error, w io.Writer) {
  54. failure := err.Error()
  55. if gojaErr, ok := err.(*goja.Exception); ok {
  56. failure = gojaErr.String()
  57. }
  58. fmt.Fprint(w, ErrorColor("%s", failure))
  59. }
  60. func (re *JSRE) prettyPrintJS(call goja.FunctionCall) goja.Value {
  61. for _, v := range call.Arguments {
  62. prettyPrint(re.vm, v, re.output)
  63. fmt.Fprintln(re.output)
  64. }
  65. return goja.Undefined()
  66. }
  67. type ppctx struct {
  68. vm *goja.Runtime
  69. w io.Writer
  70. }
  71. func (ctx ppctx) indent(level int) string {
  72. return strings.Repeat(indentString, level)
  73. }
  74. func (ctx ppctx) printValue(v goja.Value, level int, inArray bool) {
  75. if goja.IsNull(v) || goja.IsUndefined(v) {
  76. fmt.Fprint(ctx.w, SpecialColor(v.String()))
  77. return
  78. }
  79. kind := v.ExportType().Kind()
  80. switch {
  81. case kind == reflect.Bool:
  82. fmt.Fprint(ctx.w, SpecialColor("%t", v.ToBoolean()))
  83. case kind == reflect.String:
  84. fmt.Fprint(ctx.w, StringColor("%q", v.String()))
  85. case kind >= reflect.Int && kind <= reflect.Complex128:
  86. fmt.Fprint(ctx.w, NumberColor("%s", v.String()))
  87. default:
  88. if obj, ok := v.(*goja.Object); ok {
  89. ctx.printObject(obj, level, inArray)
  90. } else {
  91. fmt.Fprintf(ctx.w, "<unprintable %T>", v)
  92. }
  93. }
  94. }
  95. // SafeGet attempt to get the value associated to `key`, and
  96. // catches the panic that goja creates if an error occurs in
  97. // key.
  98. func SafeGet(obj *goja.Object, key string) (ret goja.Value) {
  99. defer func() {
  100. if r := recover(); r != nil {
  101. ret = goja.Undefined()
  102. }
  103. }()
  104. ret = obj.Get(key)
  105. return ret
  106. }
  107. func (ctx ppctx) printObject(obj *goja.Object, level int, inArray bool) {
  108. switch obj.ClassName() {
  109. case "Array", "GoArray":
  110. lv := obj.Get("length")
  111. len := lv.ToInteger()
  112. if len == 0 {
  113. fmt.Fprintf(ctx.w, "[]")
  114. return
  115. }
  116. if level > maxPrettyPrintLevel {
  117. fmt.Fprint(ctx.w, "[...]")
  118. return
  119. }
  120. fmt.Fprint(ctx.w, "[")
  121. for i := int64(0); i < len; i++ {
  122. el := obj.Get(strconv.FormatInt(i, 10))
  123. if el != nil {
  124. ctx.printValue(el, level+1, true)
  125. }
  126. if i < len-1 {
  127. fmt.Fprintf(ctx.w, ", ")
  128. }
  129. }
  130. fmt.Fprint(ctx.w, "]")
  131. case "Object":
  132. // Print values from bignumber.js as regular numbers.
  133. if ctx.isBigNumber(obj) {
  134. fmt.Fprint(ctx.w, NumberColor("%s", toString(obj)))
  135. return
  136. }
  137. // Otherwise, print all fields indented, but stop if we're too deep.
  138. keys := ctx.fields(obj)
  139. if len(keys) == 0 {
  140. fmt.Fprint(ctx.w, "{}")
  141. return
  142. }
  143. if level > maxPrettyPrintLevel {
  144. fmt.Fprint(ctx.w, "{...}")
  145. return
  146. }
  147. fmt.Fprintln(ctx.w, "{")
  148. for i, k := range keys {
  149. v := SafeGet(obj, k)
  150. fmt.Fprintf(ctx.w, "%s%s: ", ctx.indent(level+1), k)
  151. ctx.printValue(v, level+1, false)
  152. if i < len(keys)-1 {
  153. fmt.Fprintf(ctx.w, ",")
  154. }
  155. fmt.Fprintln(ctx.w)
  156. }
  157. if inArray {
  158. level--
  159. }
  160. fmt.Fprintf(ctx.w, "%s}", ctx.indent(level))
  161. case "Function":
  162. robj := obj.ToString()
  163. desc := strings.Trim(strings.Split(robj.String(), "{")[0], " \t\n")
  164. desc = strings.Replace(desc, " (", "(", 1)
  165. fmt.Fprint(ctx.w, FunctionColor("%s", desc))
  166. case "RegExp":
  167. fmt.Fprint(ctx.w, StringColor("%s", toString(obj)))
  168. default:
  169. if level <= maxPrettyPrintLevel {
  170. s := obj.ToString().String()
  171. fmt.Fprintf(ctx.w, "<%s %s>", obj.ClassName(), s)
  172. } else {
  173. fmt.Fprintf(ctx.w, "<%s>", obj.ClassName())
  174. }
  175. }
  176. }
  177. func (ctx ppctx) fields(obj *goja.Object) []string {
  178. var (
  179. vals, methods []string
  180. seen = make(map[string]bool)
  181. )
  182. add := func(k string) {
  183. if seen[k] || boringKeys[k] || strings.HasPrefix(k, "_") {
  184. return
  185. }
  186. seen[k] = true
  187. key := SafeGet(obj, k)
  188. if key == nil {
  189. // The value corresponding to that key could not be found
  190. // (typically because it is backed by an RPC call that is
  191. // not supported by this instance. Add it to the list of
  192. // values so that it appears as `undefined` to the user.
  193. vals = append(vals, k)
  194. } else {
  195. if _, callable := goja.AssertFunction(key); callable {
  196. methods = append(methods, k)
  197. } else {
  198. vals = append(vals, k)
  199. }
  200. }
  201. }
  202. iterOwnAndConstructorKeys(ctx.vm, obj, add)
  203. sort.Strings(vals)
  204. sort.Strings(methods)
  205. return append(vals, methods...)
  206. }
  207. func iterOwnAndConstructorKeys(vm *goja.Runtime, obj *goja.Object, f func(string)) {
  208. seen := make(map[string]bool)
  209. iterOwnKeys(vm, obj, func(prop string) {
  210. seen[prop] = true
  211. f(prop)
  212. })
  213. if cp := constructorPrototype(vm, obj); cp != nil {
  214. iterOwnKeys(vm, cp, func(prop string) {
  215. if !seen[prop] {
  216. f(prop)
  217. }
  218. })
  219. }
  220. }
  221. func iterOwnKeys(vm *goja.Runtime, obj *goja.Object, f func(string)) {
  222. Object := vm.Get("Object").ToObject(vm)
  223. getOwnPropertyNames, isFunc := goja.AssertFunction(Object.Get("getOwnPropertyNames"))
  224. if !isFunc {
  225. panic(vm.ToValue("Object.getOwnPropertyNames isn't a function"))
  226. }
  227. rv, err := getOwnPropertyNames(goja.Null(), obj)
  228. if err != nil {
  229. panic(vm.ToValue(fmt.Sprintf("Error getting object properties: %v", err)))
  230. }
  231. gv := rv.Export()
  232. switch gv := gv.(type) {
  233. case []interface{}:
  234. for _, v := range gv {
  235. f(v.(string))
  236. }
  237. case []string:
  238. for _, v := range gv {
  239. f(v)
  240. }
  241. default:
  242. panic(fmt.Errorf("Object.getOwnPropertyNames returned unexpected type %T", gv))
  243. }
  244. }
  245. func (ctx ppctx) isBigNumber(v *goja.Object) bool {
  246. // Handle numbers with custom constructor.
  247. if obj := v.Get("constructor").ToObject(ctx.vm); obj != nil {
  248. if strings.HasPrefix(toString(obj), "function BigNumber") {
  249. return true
  250. }
  251. }
  252. // Handle default constructor.
  253. BigNumber := ctx.vm.Get("BigNumber").ToObject(ctx.vm)
  254. if BigNumber == nil {
  255. return false
  256. }
  257. prototype := BigNumber.Get("prototype").ToObject(ctx.vm)
  258. isPrototypeOf, callable := goja.AssertFunction(prototype.Get("isPrototypeOf"))
  259. if !callable {
  260. return false
  261. }
  262. bv, _ := isPrototypeOf(prototype, v)
  263. return bv.ToBoolean()
  264. }
  265. func toString(obj *goja.Object) string {
  266. return obj.ToString().String()
  267. }
  268. func constructorPrototype(vm *goja.Runtime, obj *goja.Object) *goja.Object {
  269. if v := obj.Get("constructor"); v != nil {
  270. if v := v.ToObject(vm).Get("prototype"); v != nil {
  271. return v.ToObject(vm)
  272. }
  273. }
  274. return nil
  275. }