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,25 +20,24 @@ import (
"io/ioutil"
"os"
"path"
"reflect"
"testing"
"time"
"github.com/robertkrimen/otto"
"github.com/dop251/goja"
)
type testNativeObjectBinding struct{}
type testNativeObjectBinding struct {
vm *goja.Runtime
}
type msg struct {
Msg string
}
func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value {
m, err := call.Argument(0).ToString()
if err != nil {
return otto.UndefinedValue()
}
v, _ := call.Otto.ToValue(&msg{m})
return v
func (no *testNativeObjectBinding) TestMethod(call goja.FunctionCall) goja.Value {
m := call.Argument(0).ToString().String()
return no.vm.ToValue(&msg{m})
}
func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
@ -51,7 +50,8 @@ func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
t.Fatal("cannot create test.js:", err)
}
}
return New(dir, os.Stdout), dir
jsre := New(dir, os.Stdout)
return jsre, dir
}
func TestExec(t *testing.T) {
@ -66,11 +66,11 @@ func TestExec(t *testing.T) {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if !val.IsString() {
if val.ExportType().Kind() != reflect.String {
t.Errorf("expected string value, got %v", val)
}
exp := "testMsg"
got, _ := val.ToString()
got := val.ToString().String()
if exp != got {
t.Errorf("expected '%v', got '%v'", exp, got)
}
@ -90,11 +90,11 @@ func TestNatto(t *testing.T) {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if !val.IsString() {
if val.ExportType().Kind() != reflect.String {
t.Errorf("expected string value, got %v", val)
}
exp := "testMsg"
got, _ := val.ToString()
got := val.ToString().String()
if exp != got {
t.Errorf("expected '%v', got '%v'", exp, got)
}
@ -105,7 +105,7 @@ func TestBind(t *testing.T) {
jsre := New("", os.Stdout)
defer jsre.Stop(false)
jsre.Bind("no", &testNativeObjectBinding{})
jsre.Set("no", &testNativeObjectBinding{vm: jsre.vm})
_, err := jsre.Run(`no.TestMethod("testMsg")`)
if err != nil {
@ -125,11 +125,11 @@ func TestLoadScript(t *testing.T) {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if !val.IsString() {
if val.ExportType().Kind() != reflect.String {
t.Errorf("expected string value, got %v", val)
}
exp := "testMsg"
got, _ := val.ToString()
got := val.ToString().String()
if exp != got {
t.Errorf("expected '%v', got '%v'", exp, got)
}