console, internal/jsre: use github.com/dop251/goja (#20470)

This replaces the JavaScript interpreter used by the console with goja,
which is actively maintained and a lot faster than otto. Clef still uses otto
and eth/tracers still uses duktape, so we are currently dependent on three
different JS interpreters. We're looking to replace the remaining uses of otto
soon though.
This commit is contained in:
Guillaume Ballet
2020-01-27 11:50:48 +01:00
committed by Felix Lange
parent 60deeb103e
commit 7b68975a00
11 changed files with 570 additions and 488 deletions

View File

@ -20,35 +20,43 @@ import (
"sort"
"strings"
"github.com/robertkrimen/otto"
"github.com/dop251/goja"
)
// CompleteKeywords returns potential continuations for the given line. Since line is
// evaluated, callers need to make sure that evaluating line does not have side effects.
func (jsre *JSRE) CompleteKeywords(line string) []string {
var results []string
jsre.Do(func(vm *otto.Otto) {
jsre.Do(func(vm *goja.Runtime) {
results = getCompletions(vm, line)
})
return results
}
func getCompletions(vm *otto.Otto, line string) (results []string) {
func getCompletions(vm *goja.Runtime, line string) (results []string) {
parts := strings.Split(line, ".")
objRef := "this"
prefix := line
if len(parts) > 1 {
objRef = strings.Join(parts[0:len(parts)-1], ".")
prefix = parts[len(parts)-1]
}
obj, _ := vm.Object(objRef)
if obj == nil {
if len(parts) == 0 {
return nil
}
// Find the right-most fully named object in the line. e.g. if line = "x.y.z"
// and "x.y" is an object, obj will reference "x.y".
obj := vm.GlobalObject()
for i := 0; i < len(parts)-1; i++ {
v := obj.Get(parts[i])
if v == nil {
return nil // No object was found
}
obj = v.ToObject(vm)
}
// Go over the keys of the object and retain the keys matching prefix.
// Example: if line = "x.y.z" and "x.y" exists and has keys "zebu", "zebra"
// and "platypus", then "x.y.zebu" and "x.y.zebra" will be added to results.
prefix := parts[len(parts)-1]
iterOwnAndConstructorKeys(vm, obj, func(k string) {
if strings.HasPrefix(k, prefix) {
if objRef == "this" {
if len(parts) == 1 {
results = append(results, k)
} else {
results = append(results, strings.Join(parts[:len(parts)-1], ".")+"."+k)
@ -59,9 +67,9 @@ func getCompletions(vm *otto.Otto, line string) (results []string) {
// Append opening parenthesis (for functions) or dot (for objects)
// if the line itself is the only completion.
if len(results) == 1 && results[0] == line {
obj, _ := vm.Object(line)
obj := obj.Get(parts[len(parts)-1])
if obj != nil {
if obj.Class() == "Function" {
if _, isfunc := goja.AssertFunction(obj); isfunc {
results[0] += "("
} else {
results[0] += "."