cmd/geth, jsre, rpc: run all JS code on the event loop

Some JSRE methods (PrettyPrint, ToVal) bypassed the event loop. All
calls to the JS VM are now wrapped. In order to make this somewhat more
foolproof, the otto VM is now a local variable inside the event loop.
This commit is contained in:
Felix Lange
2015-05-25 02:27:37 +02:00
parent 394826f520
commit e221a449e0
5 changed files with 126 additions and 178 deletions

View File

@ -1,16 +1,15 @@
package jsre
import (
"github.com/robertkrimen/otto"
"io/ioutil"
"os"
"testing"
"time"
"github.com/robertkrimen/otto"
)
type testNativeObjectBinding struct {
toVal func(interface{}) otto.Value
}
type testNativeObjectBinding struct{}
type msg struct {
Msg string
@ -21,7 +20,8 @@ func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value
if err != nil {
return otto.UndefinedValue()
}
return no.toVal(&msg{m})
v, _ := call.Otto.ToValue(&msg{m})
return v
}
func TestExec(t *testing.T) {
@ -74,7 +74,7 @@ func TestNatto(t *testing.T) {
func TestBind(t *testing.T) {
jsre := New("/tmp")
jsre.Bind("no", &testNativeObjectBinding{jsre.ToVal})
jsre.Bind("no", &testNativeObjectBinding{})
val, err := jsre.Run(`no.TestMethod("testMsg")`)
if err != nil {