diff --git a/internal/jsre/jsre.go b/internal/jsre/jsre.go index 72a39be36c..7a8b44278a 100644 --- a/internal/jsre/jsre.go +++ b/internal/jsre/jsre.go @@ -106,9 +106,9 @@ func randomSource() *rand.Rand { func (re *JSRE) runEventLoop() { defer close(re.closed) - vm := goja.New() + re.vm = goja.New() r := randomSource() - vm.SetRandSource(r.Float64) + re.vm.SetRandSource(r.Float64) registry := map[*jsTimer]*jsTimer{} ready := make(chan *jsTimer) @@ -150,22 +150,22 @@ func (re *JSRE) runEventLoop() { } return goja.Undefined() } - vm.Set("_setTimeout", setTimeout) - vm.Set("_setInterval", setInterval) - vm.RunString(`var setTimeout = function(args) { + re.vm.Set("_setTimeout", setTimeout) + re.vm.Set("_setInterval", setInterval) + re.vm.RunString(`var setTimeout = function(args) { if (arguments.length < 1) { throw TypeError("Failed to execute 'setTimeout': 1 argument required, but only 0 present."); } return _setTimeout.apply(this, arguments); }`) - vm.RunString(`var setInterval = function(args) { + re.vm.RunString(`var setInterval = function(args) { if (arguments.length < 1) { throw TypeError("Failed to execute 'setInterval': 1 argument required, but only 0 present."); } return _setInterval.apply(this, arguments); }`) - vm.Set("clearTimeout", clearTimeout) - vm.Set("clearInterval", clearTimeout) + re.vm.Set("clearTimeout", clearTimeout) + re.vm.Set("clearInterval", clearTimeout) var waitForCallbacks bool @@ -185,9 +185,9 @@ loop: arguments = make([]interface{}, 1) } arguments[0] = timer.call.Arguments[0] - call, isFunc := goja.AssertFunction(vm.Get(`Function.call.call`)) + call, isFunc := goja.AssertFunction(timer.call.Arguments[0]) if !isFunc { - panic(vm.ToValue("js error: Function.call.call is not a function")) + panic(re.vm.ToValue("js error: timer/timeout callback is not a function")) } call(goja.Null(), timer.call.Arguments...) @@ -202,7 +202,7 @@ loop: } case req := <-re.evalQueue: // run the code, send the result back - req.fn(vm) + req.fn(re.vm) close(req.done) if waitForCallbacks && (len(registry) == 0) { break loop diff --git a/internal/jsre/jsre_test.go b/internal/jsre/jsre_test.go index bcb6e0dd23..65bda1ff7d 100644 --- a/internal/jsre/jsre_test.go +++ b/internal/jsre/jsre_test.go @@ -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.Bind("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) } diff --git a/vendor/github.com/dop251/goja/func.go b/vendor/github.com/dop251/goja/func.go index a4927b0097..5755c61169 100644 --- a/vendor/github.com/dop251/goja/func.go +++ b/vendor/github.com/dop251/goja/func.go @@ -1,6 +1,8 @@ package goja -import "reflect" +import ( + "reflect" +) type baseFuncObject struct { baseObject diff --git a/vendor/github.com/dop251/goja/value.go b/vendor/github.com/dop251/goja/value.go index 39225dbd0d..8bf103afc9 100644 --- a/vendor/github.com/dop251/goja/value.go +++ b/vendor/github.com/dop251/goja/value.go @@ -436,7 +436,7 @@ func (p *valueProperty) get(this Value) Value { } return _undefined } - call, _ := p.getterFunc.self.assertCallable() + call, r := p.getterFunc.self.assertCallable() return call(FunctionCall{ This: this, })