1
0
mirror of https://github.com/robertkrimen/otto synced 2025-09-28 18:45:22 +08:00

Convert _syntaxError to SyntaxError (eval)

This commit is contained in:
Robert Krimen 2012-10-17 10:27:22 -07:00
parent 32cbffe7b8
commit 474a473e36
4 changed files with 27 additions and 2 deletions

View File

@ -20,12 +20,12 @@ func builtinGlobal_eval(call FunctionCall) Value {
}
program, err := parse(toString(source))
if err != nil {
//panic(call.runtime.newError("SyntaxError", UndefinedValue()))
panic(&_syntaxError{Message: fmt.Sprintf("%v", err)})
}
runtime := call.runtime
runtime.EnterEvalExecutionContext(call)
defer runtime.LeaveExecutionContext()
// TODO Catch syntax error and convert to... SyntaxError
returnValue := runtime.evaluate(program)
if returnValue.isEmpty() {
return UndefinedValue()

View File

@ -164,5 +164,7 @@ Second line \
test("/",
"/",
"EOF")
"EOF",
)
}

View File

@ -1797,12 +1797,15 @@ func Test_eval(t *testing.T) {
Terst(t)
test := runTest()
test(`
abc = 1
`)
test(`
eval("abc += 1")
`, "2")
test(`
(function(){
var abc = 11
@ -1811,6 +1814,22 @@ func Test_eval(t *testing.T) {
})()
`, "12")
test(`abc`, "2")
test(`
var ghi;
(function(){
try {
eval("var prop = \\u2029;");
return false;
} catch (abc) {
ghi = abc.toString()
return abc instanceof SyntaxError;
}
})()
`, "true")
// TODO Make this a sane result
// Lightning bolt, lightning bolt, lightning bolt, ...
test(`ghi`, "SyntaxError: SyntaxError: SyntaxError: Unexpected token ILLEGAL ()")
}
func Test_isNaN(t *testing.T) {

View File

@ -191,6 +191,10 @@ func (self *_runtime) tryEvaluate(inner func() Value) (tryValue Value, throw boo
throw = true
throwValue = toValue(self.newError(caught.Name, caught.MessageValue()))
return
case *_syntaxError:
throw = true
throwValue = toValue(self.newError("SyntaxError", toValue(caught.String())))
return
}
panic(caught)
}