Skip to content

Commit d3040a8

Browse files
committedJul 18, 2018
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
1 parent 8944b49 commit d3040a8

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed
 

‎test/test_context.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ void test_context()
1717
v8pp::context context;
1818

1919
v8::HandleScope scope(context.isolate());
20-
int const r = context.run_script("42")->Int32Value(context.isolate()->GetCurrentContext()).FromJust();
20+
v8::Local<v8::Value> result = context.run_script("42");
21+
int const r = result->Int32Value(context.isolate()->GetCurrentContext()).FromJust();
2122
check_eq("run_script", r, 42);
23+
24+
v8::TryCatch try_catch(context.isolate());
25+
result = context.run_script("syntax error");
26+
check("script with syntax error", result.IsEmpty());
27+
check(" has caught", try_catch.HasCaught());
2228
}
2329

2430
{

‎v8pp/context.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ v8::Local<v8::Value> context::run_script(std::string const& source,
259259
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
260260

261261
v8::ScriptOrigin origin(to_v8(isolate_, filename));
262-
v8::Local<v8::Script> script = v8::Script::Compile(context,
263-
to_v8(isolate_, source), &origin).ToLocalChecked();
262+
v8::Local<v8::Script> script;
263+
bool const is_valid = v8::Script::Compile(context,
264+
to_v8(isolate_, source), &origin).ToLocal(&script);
264265

265266
v8::Local<v8::Value> result;
266267
if (!script.IsEmpty())

0 commit comments

Comments
 (0)