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

Prevent otto.Value from becoming a _goStructObject

Fix #21, this would happen during .Set(...)
This commit is contained in:
Robert Krimen 2013-06-05 21:52:59 -07:00
parent 61a011e711
commit 04ea4a2729
2 changed files with 48 additions and 0 deletions

View File

@ -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")
}

View File

@ -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: