-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow options in
v8::Exception
ctors (#202)
... since V8 version 11.9. Use single `v8pp::throw_ex()` function with optional arguments for exception constructor and options
- Loading branch information
Showing
3 changed files
with
41 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,45 @@ | ||
#include "v8pp/throw_ex.hpp" | ||
#include "v8pp/json.hpp" | ||
#include "test.hpp" | ||
|
||
namespace { | ||
|
||
void test(v8pp::context& context, std::string const& type, | ||
v8::Local<v8::Value> (*exception_ctor)(v8::Local<v8::String>)) | ||
void test(v8pp::context& context, std::string type, v8pp::exception_ctor ctor = {}, v8::Local<v8::Value> opts = {}) | ||
{ | ||
v8::Isolate* isolate = context.isolate(); | ||
|
||
v8::HandleScope scope(isolate); | ||
|
||
v8::TryCatch try_catch(isolate); | ||
v8::Local<v8::Value> ex = v8pp::throw_ex(isolate, | ||
"exception message", exception_ctor); | ||
v8::Local<v8::Value> ex = v8pp::throw_ex(isolate, "exception message", ctor, opts); | ||
check(" has caught", try_catch.HasCaught()); | ||
check("the same stack trace", try_catch.Message()->GetStackTrace() == v8::Exception::GetStackTrace(ex)); | ||
v8::String::Utf8Value const err_msg(isolate, try_catch.Message()->Get()); | ||
check_eq("message", *err_msg, "Uncaught " + type + ": exception message"); | ||
|
||
if (!type.empty()) | ||
{ | ||
type += ": "; | ||
} | ||
check_eq("message", *err_msg, "Uncaught " + type + "exception message"); | ||
} | ||
|
||
} // unnamed namespace | ||
|
||
void test_throw_ex() | ||
{ | ||
v8pp::context context; | ||
v8::Isolate* isolate = context.isolate(); | ||
|
||
test(context, ""); | ||
test(context, "Error", v8::Exception::Error); | ||
test(context, "RangeError", v8::Exception::RangeError); | ||
test(context, "ReferenceError", v8::Exception::ReferenceError); | ||
test(context, "SyntaxError", v8::Exception::SyntaxError); | ||
test(context, "TypeError", v8::Exception::TypeError); | ||
|
||
if constexpr (v8pp::exception_ctor_with_options) | ||
{ | ||
v8::HandleScope scope(isolate); | ||
test(context, "Error", v8::Exception::Error, v8pp::json_parse(isolate, "{ 'opt1': true, 'opt2': 'str' }")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters