Skip to content

Commit

Permalink
update API
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoroz committed Sep 20, 2024
1 parent 0f5f9fc commit e53e166
Show file tree
Hide file tree
Showing 11 changed files with 1,654 additions and 1,059 deletions.
447 changes: 256 additions & 191 deletions doc/api/embedding.md

Large diffs are not rendered by default.

30 changes: 23 additions & 7 deletions src/js_native_api_v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,30 @@ struct napi_env__ {
// v8 uses a special exception to indicate termination, the
// `handle_exception` callback should identify such case using
// terminatedOrTerminating() before actually handle the exception
template <typename T, typename U = decltype(HandleThrow)>
inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) {
int open_handle_scopes_before = open_handle_scopes;
int open_callback_scopes_before = open_callback_scopes;
napi_clear_last_error(this);
template <typename Call, typename JSExceptionHandler = decltype(HandleThrow)>
inline void CallIntoModule(
Call&& call, JSExceptionHandler&& handle_exception = HandleThrow) {
CallModuleScope scope = OpenCallModuleScope();
call(this);
CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
CloseCallModuleScope(scope, handle_exception);
}

struct CallModuleScope {
int open_handle_scopes_before;
int open_callback_scopes_before;
};

inline CallModuleScope OpenCallModuleScope() {
napi_clear_last_error(this);
return {open_handle_scopes, open_callback_scopes};
}

template <typename JSExceptionHandler = decltype(HandleThrow)>
inline void CloseCallModuleScope(
const CallModuleScope& scope,
JSExceptionHandler&& handle_exception = HandleThrow) {
CHECK_EQ(open_handle_scopes, scope.open_handle_scopes_before);
CHECK_EQ(open_callback_scopes, scope.open_callback_scopes_before);
if (!last_exception.IsEmpty()) {
handle_exception(this, last_exception.Get(this->isolate));
last_exception.Reset();
Expand Down
Loading

0 comments on commit e53e166

Please sign in to comment.