From d3040a85af358a2780d71c6b8e22449d39c809ba Mon Sep 17 00:00:00 2001 From: Pavel Medvedev Date: Wed, 18 Jul 2018 22:23:11 +0200 Subject: [PATCH] fix `context::run_script()` with syntax error Return empty result handle for a script with syntax error, do not catch JavaScript exception. Added a test case. See issue #80 --- test/test_context.cpp | 8 +++++++- v8pp/context.cpp | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/test/test_context.cpp b/test/test_context.cpp index 3444c497..f93bc295 100644 --- a/test/test_context.cpp +++ b/test/test_context.cpp @@ -17,8 +17,14 @@ void test_context() v8pp::context context; v8::HandleScope scope(context.isolate()); - int const r = context.run_script("42")->Int32Value(context.isolate()->GetCurrentContext()).FromJust(); + v8::Local result = context.run_script("42"); + int const r = result->Int32Value(context.isolate()->GetCurrentContext()).FromJust(); check_eq("run_script", r, 42); + + v8::TryCatch try_catch(context.isolate()); + result = context.run_script("syntax error"); + check("script with syntax error", result.IsEmpty()); + check(" has caught", try_catch.HasCaught()); } { diff --git a/v8pp/context.cpp b/v8pp/context.cpp index 0801197a..595c92ae 100644 --- a/v8pp/context.cpp +++ b/v8pp/context.cpp @@ -259,8 +259,9 @@ v8::Local context::run_script(std::string const& source, v8::Local context = isolate_->GetCurrentContext(); v8::ScriptOrigin origin(to_v8(isolate_, filename)); - v8::Local script = v8::Script::Compile(context, - to_v8(isolate_, source), &origin).ToLocalChecked(); + v8::Local script; + bool const is_valid = v8::Script::Compile(context, + to_v8(isolate_, source), &origin).ToLocal(&script); v8::Local result; if (!script.IsEmpty())