Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
chakrashim,n-api: Add napi_fatal_exception
Browse files Browse the repository at this point in the history
Implemented `napi_fatal_exception` in terms of the shim because node
core still relies on V8 APIs. Stubbed out
`v8::Exception::CreateMessage` as it's not possible to get that data
from ChakraCore currently if the exception was not thrown.
  • Loading branch information
kfarnung committed Mar 24, 2018
1 parent cdaf0d4 commit bd169e3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions deps/chakrashim/include/v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ class Local {
friend class Context;
friend class Date;
friend class Debug;
friend class Exception;
friend class External;
friend class Function;
friend class FunctionCallbackData;
Expand Down Expand Up @@ -2520,6 +2521,7 @@ class V8_EXPORT Exception {
static Local<Value> SyntaxError(Handle<String> message);
static Local<Value> TypeError(Handle<String> message);
static Local<Value> Error(Handle<String> message);
static Local<Message> CreateMessage(Isolate* isolate, Local<Value> exception);
};

class V8_EXPORT MicrotasksScope {
Expand Down
9 changes: 9 additions & 0 deletions deps/chakrashim/src/v8exception.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ Local<Value> Exception::Error(Handle<String> message) {
return Utils::NewError(message, JsCreateError);
}

Local<Message> Exception::CreateMessage(Isolate* isolate,
Local<Value> exception) {
// ChakraCore doesn't have any way to get metadata about an error that was
// instantiated, but not thrown. Create an empty object to use instead.
JsValueRef metadata;
JsCreateObject(&metadata);
return v8::Local<v8::Message>::New(metadata);
}

} // namespace v8
31 changes: 23 additions & 8 deletions src/node_api_jsrt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ namespace v8impl {

//=== Conversion between V8 Isolate and napi_env ==========================

v8::Isolate* V8IsolateFromJsEnv(napi_env e) {
return reinterpret_cast<v8::Isolate*>(e);
}

class HandleScopeWrapper {
public:
explicit HandleScopeWrapper(
Expand Down Expand Up @@ -148,6 +144,15 @@ v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
memcpy(&local, &v, sizeof(v));
return local;
}

inline void TriggerFatalException(
napi_env env, v8::Local<v8::Value> local_err) {
// The API requires V8 types, so use the shim until there are engine-neutral
// core APIs.
v8::Local<v8::Message> local_msg =
v8::Exception::CreateMessage(env->isolate, local_err);
node::FatalException(env->isolate, local_err, local_msg);
}
} // end of namespace v8impl

namespace jsrtimpl {
Expand Down Expand Up @@ -619,6 +624,16 @@ napi_status napi_set_last_error(JsErrorCode jsError, void* engine_reserved) {
return status;
}

napi_status napi_fatal_exception(napi_env env, napi_value err) {
CHECK_ARG(err);

v8::Local<v8::Value> local_err = v8impl::V8LocalValueFromJsValue(err);
v8impl::TriggerFatalException(env, local_err);

napi_clear_last_error();
return napi_ok;
}

NAPI_NO_RETURN void napi_fatal_error(const char* location,
size_t location_len,
const char* message,
Expand Down Expand Up @@ -2139,7 +2154,7 @@ napi_status napi_make_callback(napi_env env,
size_t argc,
const napi_value* argv,
napi_value* result) {
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv(env);
v8::Isolate* isolate = env->isolate;
v8::Local<v8::Object> v8recv =
v8impl::V8LocalValueFromJsValue(recv).As<v8::Object>();
v8::Local<v8::Function> v8func =
Expand Down Expand Up @@ -2197,7 +2212,7 @@ napi_status napi_create_buffer(napi_env env,
// TODO(tawoll): Replace v8impl with jsrt-based version.

v8::MaybeLocal<v8::Object> maybe =
node::Buffer::New(v8impl::V8IsolateFromJsEnv(env), length);
node::Buffer::New(env->isolate, length);
if (maybe.IsEmpty()) {
return napi_generic_failure;
}
Expand All @@ -2223,7 +2238,7 @@ napi_status napi_create_external_buffer(napi_env env,
jsrtimpl::ExternalData* externalData = new jsrtimpl::ExternalData(
env, data, finalize_cb, finalize_hint);
v8::MaybeLocal<v8::Object> maybe = node::Buffer::New(
v8impl::V8IsolateFromJsEnv(env),
env->isolate,
static_cast<char*>(data),
length,
jsrtimpl::ExternalData::FinalizeBuffer,
Expand All @@ -2247,7 +2262,7 @@ napi_status napi_create_buffer_copy(napi_env env,
// chakra shim here.

v8::MaybeLocal<v8::Object> maybe = node::Buffer::Copy(
v8impl::V8IsolateFromJsEnv(env), static_cast<const char*>(data), length);
env->isolate, static_cast<const char*>(data), length);
if (maybe.IsEmpty()) {
return napi_generic_failure;
}
Expand Down

0 comments on commit bd169e3

Please sign in to comment.