From 5fd5af314551c610ea08bad46925e0944bb4e092 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 11 Dec 2014 10:57:49 -0800 Subject: [PATCH] Ensure compile errors are wrapped --- lib/execjs/external_runtime.rb | 3 +++ lib/execjs/ruby_racer_runtime.rb | 11 ++++++++++- test/test_execjs.rb | 20 +++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index 04de55a..f123cfe 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -9,6 +9,9 @@ module ExecJS @runtime = runtime @source = source + + # Test compile context source + exec("") end def eval(source, options = {}) diff --git a/lib/execjs/ruby_racer_runtime.rb b/lib/execjs/ruby_racer_runtime.rb index f5bca9c..d7c36b7 100644 --- a/lib/execjs/ruby_racer_runtime.rb +++ b/lib/execjs/ruby_racer_runtime.rb @@ -8,7 +8,16 @@ module ExecJS lock do @v8_context = ::V8::Context.new - @v8_context.eval(source) + + begin + @v8_context.eval(source) + rescue ::V8::JSError => e + if e.value["name"] == "SyntaxError" + raise RuntimeError, e.value.to_s + else + raise ProgramError, e.value.to_s + end + end end end diff --git a/test/test_execjs.rb b/test/test_execjs.rb index 957b7b2..d900ce9 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -206,7 +206,25 @@ class TestExecJS < Test end end - def test_thrown_exception + def test_compile_syntax_error + assert_raises ExecJS::RuntimeError do + ExecJS.compile(")") + end + end + + def test_exec_thrown_exception + assert_raises ExecJS::ProgramError do + ExecJS.exec("throw 'hello'") + end + end + + def test_eval_thrown_exception + assert_raises ExecJS::ProgramError do + ExecJS.exec("throw 'hello'") + end + end + + def test_compile_thrown_exception assert_raises ExecJS::ProgramError do ExecJS.exec("throw 'hello'") end