Skip to content

Commit

Permalink
napi: implement exception handling
Browse files Browse the repository at this point in the history
Re gh-27
Re gh-49
  • Loading branch information
Gabriel Schulhof committed Jan 20, 2017
1 parent b2f08e6 commit 5cd6adc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 53 deletions.
55 changes: 12 additions & 43 deletions src/node_jsrtapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -918,49 +918,6 @@ napi_value napi_make_callback(napi_env e, napi_value recv,
return reinterpret_cast<napi_value>(result);
}

bool napi_try_catch(napi_env e, napi_try_callback cbtry,
napi_catch_callback cbcatch, void* data) {
JsValueRef error = JS_INVALID_REFERENCE;
bool hasException;
cbtry(e, data);
if (error = JS_INVALID_REFERENCE) {
JsErrorCode errorCode = JsHasException(&hasException);
if (errorCode != JsNoError) {
// If script is in disabled state, no need to fail here.
// We will fail subsequent calls to Jsrt anyway.
assert(errorCode == JsErrorInDisabledState);
} else if (hasException) {
JsValueRef exceptionRef;
errorCode = JsGetAndClearException(&exceptionRef);
// We came here through JsHasException, so script shouldn't be in disabled
// state.
assert(errorCode != JsErrorInDisabledState);
if (errorCode == JsNoError) {
error = exceptionRef;
}
}
}
if(error != JS_INVALID_REFERENCE) {
cbcatch(e, data);
return true;
}
JsErrorCode errorCode = JsHasException(&hasException);
if (errorCode != JsNoError) {
if (errorCode == JsErrorInDisabledState) {
cbcatch(e, data);
return true;
}
// Should never get errorCode other than JsNoError/JsErrorInDisabledState
assert(false);
return false;
}
if (hasException) {
cbcatch(e, data);
return true;
}
return false;
}

struct ArrayBufferFinalizeInfo {
void *data;

Expand Down Expand Up @@ -1020,3 +977,15 @@ size_t napi_buffer_length(napi_env e, napi_value v) {
return 0;
return len;
}

bool napi_is_exception_pending(napi_env) {
bool returnValue = false;
JsErrorCode error = JsHasException(&returnValue);
return returnValue;
}

napi_value napi_get_and_clear_last_exception(napi_env e) {
JsValueRef lastException = napi_get_undefined_(e);
JsErrorCode error = JsGetAndClearException(&lastException);
return reinterpret_cast<napi_value>(lastException);
}
13 changes: 3 additions & 10 deletions src/node_jsvmapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,9 @@ NODE_EXTERN void napi_throw(napi_env e, napi_value error);
NODE_EXTERN void napi_throw_error(napi_env e, char* msg);
NODE_EXTERN void napi_throw_type_error(napi_env e, char* msg);

// TODO(ianhall): APIs for handling try catch semantics need serious
// design analysis, this is just a quick hack
// One thing to note is that v8::TryCatch relies on the current
// stack pointer position, so it cannot be used
// with a wrapper class hack like we're doing for handle scopes.
typedef void (*napi_try_callback)(napi_env e, void* data);
typedef void (*napi_catch_callback)(napi_env e, void* data);
NODE_EXTERN bool napi_try_catch(napi_env e, napi_try_callback t,
napi_catch_callback c, void* data);

// Methods to support catching exceptions
NODE_EXTERN bool napi_is_exception_pending(napi_env e);
NODE_EXTERN napi_value napi_get_and_clear_last_exception(napi_env e);

// Methods to provide node::Buffer functionality with napi types
NODE_EXTERN napi_value napi_buffer_new(napi_env e, char* data, uint32_t size);
Expand Down

0 comments on commit 5cd6adc

Please sign in to comment.