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:
parent
9afbdc1417
commit
44a4b3115d
5
otto.go
5
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{}
|
||||
|
|
16
otto_test.go
16
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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user