Skip to content

Commit

Permalink
src: guard CallbackScope with N-API v3
Browse files Browse the repository at this point in the history
CallbackScope support needs to be guarded
with N-API version 3, otherwise olders versions
of N-API that did not have CallbackScope
support will have compile failures.

PR-URL: nodejs/node-addon-api#395
Fixes: nodejs/node-addon-api#387
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Nicola Del Gobbo <[email protected]>
  • Loading branch information
John French committed Nov 13, 2018
1 parent 3a9cab6 commit 27e5e73
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3404,6 +3404,8 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
return Value(_env, result);
}


#if (NAPI_VERSION > 2)
////////////////////////////////////////////////////////////////////////////////
// CallbackScope class
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3431,6 +3433,7 @@ inline CallbackScope::operator napi_callback_scope() const {
inline Napi::Env CallbackScope::Env() const {
return Napi::Env(_env);
}
#endif

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

#if (NAPI_VERSION > 2)
class CallbackScope {
public:
CallbackScope(napi_env env, napi_callback_scope scope);
Expand All @@ -1686,6 +1687,7 @@ namespace Napi {
napi_async_context _async_context;
napi_callback_scope _scope;
};
#endif

class AsyncContext {
public:
Expand Down
4 changes: 4 additions & 0 deletions test/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Object InitBasicTypesValue(Env env);
Object InitBigInt(Env env);
#endif
Object InitBuffer(Env env);
#if (NAPI_VERSION > 2)
Object InitCallbackScope(Env env);
#endif
Object InitDataView(Env env);
Object InitDataViewReadWrite(Env env);
Object InitError(Env env);
Expand Down Expand Up @@ -50,7 +52,9 @@ Object Init(Env env, Object exports) {
exports.Set("bigint", InitBigInt(env));
#endif
exports.Set("buffer", InitBuffer(env));
#if (NAPI_VERSION > 2)
exports.Set("callbackscope", InitCallbackScope(env));
#endif
exports.Set("dataview", InitDataView(env));
exports.Set("dataview_read_write", InitDataView(env));
exports.Set("dataview_read_write", InitDataViewReadWrite(env));
Expand Down
2 changes: 2 additions & 0 deletions test/callbackscope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using namespace Napi;

#if (NAPI_VERSION > 2)
namespace {

static void RunInCallbackScope(const CallbackInfo& info) {
Expand All @@ -18,3 +19,4 @@ Object InitCallbackScope(Env env) {
exports["runInCallbackScope"] = Function::New(env, RunInCallbackScope);
return exports;
}
#endif
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
testModules.splice(testModules.indexOf('typedarray-bigint'), 1);
}

if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
(process.env.npm_config_NAPI_VERSION < 3)) {
testModules.splice(testModules.indexOf('callbackscope'), 1);
testModules.splice(testModules.indexOf('version_management'), 1);
}

if (typeof global.gc === 'function') {
console.log('Starting test suite\n');

Expand Down

0 comments on commit 27e5e73

Please sign in to comment.