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

Remove throw behavior from Otto.Call

This commit is contained in:
Robert Krimen 2013-05-19 21:08:32 -07:00
parent 83c56dd73d
commit 18630616eb
2 changed files with 19 additions and 42 deletions

27
otto.go
View File

@ -216,18 +216,11 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
thisValue := UndefinedValue() thisValue := UndefinedValue()
new_, throw := false, false new_ := false
switch { switch {
case strings.HasPrefix(source, "throw new "):
source = source[10:]
throw = true
new_ = true
case strings.HasPrefix(source, "new "): case strings.HasPrefix(source, "new "):
source = source[4:] source = source[4:]
new_ = true new_ = true
case strings.HasPrefix(source, "throw "):
source = source[6:]
throw = true
} }
if !new_ && this == nil { if !new_ && this == nil {
@ -242,17 +235,11 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
} }
}) })
if !fallback && err == nil { if !fallback && err == nil {
if throw {
panic(value)
}
return value, nil return value, nil
} }
} else { } else {
value, err := self.ToValue(this) value, err := self.ToValue(this)
if err != nil { if err != nil {
if throw {
panic(UndefinedValue())
}
return UndefinedValue(), err return UndefinedValue(), err
} }
thisValue = value thisValue = value
@ -260,9 +247,6 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
fnValue, err := self.Run(source) fnValue, err := self.Run(source)
if err != nil { if err != nil {
if throw {
panic(UndefinedValue())
}
return UndefinedValue(), err return UndefinedValue(), err
} }
@ -270,24 +254,15 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
if new_ { if new_ {
value, err = fnValue.constructSafe(thisValue, argumentList...) value, err = fnValue.constructSafe(thisValue, argumentList...)
if err != nil { if err != nil {
if throw {
panic(UndefinedValue())
}
return UndefinedValue(), err return UndefinedValue(), err
} }
} else { } else {
value, err = fnValue.Call(thisValue, argumentList...) value, err = fnValue.Call(thisValue, argumentList...)
if err != nil { if err != nil {
if throw {
panic(UndefinedValue())
}
return UndefinedValue(), err return UndefinedValue(), err
} }
} }
if throw {
panic(value)
}
return value, nil return value, nil
} }

View File

@ -451,9 +451,27 @@ func TestOttoCall(t *testing.T) {
Is(value, "1,2,3,,4,5,6,7,abc") Is(value, "1,2,3,,4,5,6,7,abc")
} }
func TestOttoCall_new(t *testing.T) {
Terst(t)
_, test := runTestWithOtto()
failSet("abc", func(call FunctionCall) Value {
value, err := call.Otto.Call(`new Object`, nil, "Nothing happens.")
Is(err, nil)
return value
})
test(`
def = abc();
[ def, def instanceof String ];
`, "Nothing happens.,true")
}
func TestOttoCall_throw(t *testing.T) { func TestOttoCall_throw(t *testing.T) {
Terst(t) Terst(t)
return
_, test := runTestWithOtto() _, test := runTestWithOtto()
failSet("abc", func(call FunctionCall) Value { failSet("abc", func(call FunctionCall) Value {
@ -492,19 +510,3 @@ func TestOttoCall_throw(t *testing.T) {
[ error instanceof Error, error.message, error.def, typeof error, error, error instanceof Number ]; [ error instanceof Error, error.message, error.def, typeof error, error, error instanceof Number ];
`, "false,,,object,3.14159,true") `, "false,,,object,3.14159,true")
} }
func TestOttoCall_new(t *testing.T) {
Terst(t)
_, test := runTestWithOtto()
failSet("abc", func(call FunctionCall) Value {
value, err := call.Otto.Call(`new Object`, nil, "Nothing happens.")
Is(err, nil)
return value
})
test(`
def = abc();
[ def, def instanceof String ];
`, "Nothing happens.,true")
}