diff --git a/builtin.go b/builtin.go index 6b33d10..025cc81 100644 --- a/builtin.go +++ b/builtin.go @@ -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() diff --git a/lexer_test.go b/lexer_test.go index e90b362..4c7bfd6 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -164,5 +164,7 @@ Second line \ test("/", "/", - "EOF") + "EOF", + ) + } diff --git a/otto_test.go b/otto_test.go index cf511b0..0b8f4e0 100644 --- a/otto_test.go +++ b/otto_test.go @@ -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) { diff --git a/runtime.go b/runtime.go index 9dfb78f..a015588 100644 --- a/runtime.go +++ b/runtime.go @@ -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) }