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:
parent
61a011e711
commit
04ea4a2729
46
bug_test.go
46
bug_test.go
|
|
@ -111,3 +111,49 @@ func Test_issue16(t *testing.T) {
|
||||||
pqr.concat(ghi, def.abc, def, def.xyz).length;
|
pqr.concat(ghi, def.abc, def, def.xyz).length;
|
||||||
`, "9")
|
`, "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")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -356,6 +356,8 @@ func (self *_runtime) ToValue(value interface{}) (Value, error) {
|
||||||
|
|
||||||
func (self *_runtime) toValue(value interface{}) Value {
|
func (self *_runtime) toValue(value interface{}) Value {
|
||||||
switch value := value.(type) {
|
switch value := value.(type) {
|
||||||
|
case Value:
|
||||||
|
return value
|
||||||
case func(FunctionCall) Value:
|
case func(FunctionCall) Value:
|
||||||
return toValue(self.newNativeFunction(value, 0, "nativeFunction"))
|
return toValue(self.newNativeFunction(value, 0, "nativeFunction"))
|
||||||
case _nativeFunction:
|
case _nativeFunction:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user