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:
parent
83c56dd73d
commit
18630616eb
27
otto.go
27
otto.go
|
@ -216,18 +216,11 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
|
|||
|
||||
thisValue := UndefinedValue()
|
||||
|
||||
new_, throw := false, false
|
||||
new_ := false
|
||||
switch {
|
||||
case strings.HasPrefix(source, "throw new "):
|
||||
source = source[10:]
|
||||
throw = true
|
||||
new_ = true
|
||||
case strings.HasPrefix(source, "new "):
|
||||
source = source[4:]
|
||||
new_ = true
|
||||
case strings.HasPrefix(source, "throw "):
|
||||
source = source[6:]
|
||||
throw = true
|
||||
}
|
||||
|
||||
if !new_ && this == nil {
|
||||
|
@ -242,17 +235,11 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
|
|||
}
|
||||
})
|
||||
if !fallback && err == nil {
|
||||
if throw {
|
||||
panic(value)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
} else {
|
||||
value, err := self.ToValue(this)
|
||||
if err != nil {
|
||||
if throw {
|
||||
panic(UndefinedValue())
|
||||
}
|
||||
return UndefinedValue(), err
|
||||
}
|
||||
thisValue = value
|
||||
|
@ -260,9 +247,6 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
|
|||
|
||||
fnValue, err := self.Run(source)
|
||||
if err != nil {
|
||||
if throw {
|
||||
panic(UndefinedValue())
|
||||
}
|
||||
return UndefinedValue(), err
|
||||
}
|
||||
|
||||
|
@ -270,24 +254,15 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
|
|||
if new_ {
|
||||
value, err = fnValue.constructSafe(thisValue, argumentList...)
|
||||
if err != nil {
|
||||
if throw {
|
||||
panic(UndefinedValue())
|
||||
}
|
||||
return UndefinedValue(), err
|
||||
}
|
||||
} else {
|
||||
value, err = fnValue.Call(thisValue, argumentList...)
|
||||
if err != nil {
|
||||
if throw {
|
||||
panic(UndefinedValue())
|
||||
}
|
||||
return UndefinedValue(), err
|
||||
}
|
||||
}
|
||||
|
||||
if throw {
|
||||
panic(value)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
|
|
34
otto_test.go
34
otto_test.go
|
@ -451,9 +451,27 @@ func TestOttoCall(t *testing.T) {
|
|||
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) {
|
||||
Terst(t)
|
||||
|
||||
return
|
||||
|
||||
_, test := runTestWithOtto()
|
||||
|
||||
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 ];
|
||||
`, "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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user