Skip to content

Commit

Permalink
Implement CallbackScope class
Browse files Browse the repository at this point in the history
This is a wrapper class to support the following N-APIs.
  - napi_open_callback_scope()
  - napi_close_callback_scope()

Refs: https://nodejs.org/api/n-api.html#n_api_napi_open_callback_scope
  • Loading branch information
romandev committed Oct 2, 2018
1 parent dfcb939 commit cdca219
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
25 changes: 25 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3288,6 +3288,31 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
return Value(_env, result);
}

////////////////////////////////////////////////////////////////////////////////
// CallbackScope class
////////////////////////////////////////////////////////////////////////////////

inline CallbackScope::CallbackScope(
napi_env env, napi_callback_scope scope) : _env(env), _scope(scope) {
}

inline CallbackScope::CallbackScope(napi_env env) : _env(env) {
napi_status status = napi_open_callback_scope(_env, nullptr, nullptr, &_scope);
NAPI_THROW_IF_FAILED_VOID(_env, status);
}

inline CallbackScope::~CallbackScope() {
napi_close_callback_scope(_env, _scope);
}

inline CallbackScope::operator napi_callback_scope() const {
return _scope;
}

inline Napi::Env CallbackScope::Env() const {
return Napi::Env(_env);
}

////////////////////////////////////////////////////////////////////////////////
// AsyncContext class
////////////////////////////////////////////////////////////////////////////////
Expand Down
15 changes: 15 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,21 @@ namespace Napi {
napi_escapable_handle_scope _scope;
};

class CallbackScope {
public:
CallbackScope(napi_env env, napi_callback_scope scope);
explicit CallbackScope(napi_env env);
virtual ~CallbackScope();

operator napi_callback_scope() const;

Napi::Env Env() const;

private:
napi_env _env;
napi_callback_scope _scope;
};

class AsyncContext {
public:
explicit AsyncContext(napi_env env, const char* resource_name);
Expand Down

0 comments on commit cdca219

Please sign in to comment.