Skip to content

Commit

Permalink
Prevent otto.Value from becoming a _goStructObject
Browse files Browse the repository at this point in the history
Fix #21, this would happen during .Set(...)
  • Loading branch information
robertkrimen committed Jun 6, 2013
1 parent 61a011e commit 04ea4a2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
46 changes: 46 additions & 0 deletions bug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 2 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 04ea4a2

Please sign in to comment.