diff --git a/otto.go b/otto.go index ba98abf..1800510 100644 --- a/otto.go +++ b/otto.go @@ -302,6 +302,11 @@ func (self Otto) Run(src interface{}) (Value, error) { // already defined in the current stack frame. This is most useful in, for // example, a debugger call. func (self Otto) Eval(src interface{}) (Value, error) { + if self.runtime.scope == nil { + self.runtime.enterGlobalScope() + defer self.runtime.leaveScope() + } + value, err := self.runtime.cmpl_eval(src) if !value.safe() { value = Value{} diff --git a/otto_test.go b/otto_test.go index 0319889..7354aac 100644 --- a/otto_test.go +++ b/otto_test.go @@ -1441,6 +1441,22 @@ func TestOttoEval(t *testing.T) { is(err, nil) }) + + tt(t, func() { + vm := New() + + _, err := vm.Eval("null") + is(err, nil) + + vm.Set("a", 1) + vm.Set("b", 2) + + v, err := vm.Eval("a + b") + is(err, nil) + r, err := v.Export() + is(err, nil) + is(r, 3) + }) } func Test_objectLength(t *testing.T) {