diff --git a/bug_test.go b/bug_test.go index b9bf3519..b262cb89 100644 --- a/bug_test.go +++ b/bug_test.go @@ -111,3 +111,49 @@ func Test_issue16(t *testing.T) { pqr.concat(ghi, def.abc, def, def.xyz).length; `, "9") } + +func Test_issue21(t *testing.T) { + Terst(t) + + otto1 := New() + otto1.Run(` + abc = {} + abc.ghi = "Nothing happens."; + var jkl = 0; + abc.def = function() { + jkl += 1; + return 1; + } + `) + abc, err := otto1.Get("abc") + Is(err, nil) + + otto2 := New() + otto2.Set("cba", abc) + _, err = otto2.Run(` + var pqr = 0; + cba.mno = function() { + pqr -= 1; + return 1; + } + cba.def(); + cba.def(); + cba.def(); + `) + Is(err, nil) + + jkl, err := otto1.Get("jkl") + Is(err, nil) + Is(jkl, "3") + + _, err = otto1.Run(` + abc.mno(); + abc.mno(); + abc.mno(); + `) + Is(err, nil) + + pqr, err := otto2.Get("pqr") + Is(err, nil) + Is(pqr, "-3") +} diff --git a/runtime.go b/runtime.go index aca8cb83..1d9758e3 100644 --- a/runtime.go +++ b/runtime.go @@ -356,6 +356,8 @@ func (self *_runtime) ToValue(value interface{}) (Value, error) { func (self *_runtime) toValue(value interface{}) Value { switch value := value.(type) { + case Value: + return value case func(FunctionCall) Value: return toValue(self.newNativeFunction(value, 0, "nativeFunction")) case _nativeFunction: