1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-05 19:19:10 +08:00

support and test Eval() without an existing scope

This commit is contained in:
deoxxa 2015-12-05 20:24:12 +11:00
parent 9afbdc1417
commit 44a4b3115d
2 changed files with 21 additions and 0 deletions

View File

@ -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{}

View File

@ -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) {