Skip to content

Commit d47399f

Browse files
committed
src: guard CallbackScope with N-API v3
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: #395 Fixes: #387 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
1 parent 29a0262 commit d47399f

File tree

5 files changed

+17
-0
lines changed

5 files changed

+17
-0
lines changed

napi-inl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,6 +3404,8 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
34043404
return Value(_env, result);
34053405
}
34063406

3407+
3408+
#if (NAPI_VERSION > 2)
34073409
////////////////////////////////////////////////////////////////////////////////
34083410
// CallbackScope class
34093411
////////////////////////////////////////////////////////////////////////////////
@@ -3431,6 +3433,7 @@ inline CallbackScope::operator napi_callback_scope() const {
34313433
inline Napi::Env CallbackScope::Env() const {
34323434
return Napi::Env(_env);
34333435
}
3436+
#endif
34343437

34353438
////////////////////////////////////////////////////////////////////////////////
34363439
// AsyncContext class

napi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,7 @@ namespace Napi {
16711671
napi_escapable_handle_scope _scope;
16721672
};
16731673

1674+
#if (NAPI_VERSION > 2)
16741675
class CallbackScope {
16751676
public:
16761677
CallbackScope(napi_env env, napi_callback_scope scope);
@@ -1686,6 +1687,7 @@ namespace Napi {
16861687
napi_async_context _async_context;
16871688
napi_callback_scope _scope;
16881689
};
1690+
#endif
16891691

16901692
class AsyncContext {
16911693
public:

test/binding.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ Object InitBasicTypesValue(Env env);
1616
Object InitBigInt(Env env);
1717
#endif
1818
Object InitBuffer(Env env);
19+
#if (NAPI_VERSION > 2)
1920
Object InitCallbackScope(Env env);
21+
#endif
2022
Object InitDataView(Env env);
2123
Object InitDataViewReadWrite(Env env);
2224
Object InitError(Env env);
@@ -50,7 +52,9 @@ Object Init(Env env, Object exports) {
5052
exports.Set("bigint", InitBigInt(env));
5153
#endif
5254
exports.Set("buffer", InitBuffer(env));
55+
#if (NAPI_VERSION > 2)
5356
exports.Set("callbackscope", InitCallbackScope(env));
57+
#endif
5458
exports.Set("dataview", InitDataView(env));
5559
exports.Set("dataview_read_write", InitDataView(env));
5660
exports.Set("dataview_read_write", InitDataViewReadWrite(env));

test/callbackscope.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using namespace Napi;
44

5+
#if (NAPI_VERSION > 2)
56
namespace {
67

78
static void RunInCallbackScope(const CallbackInfo& info) {
@@ -18,3 +19,4 @@ Object InitCallbackScope(Env env) {
1819
exports["runInCallbackScope"] = Function::New(env, RunInCallbackScope);
1920
return exports;
2021
}
22+
#endif

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
5353
testModules.splice(testModules.indexOf('typedarray-bigint'), 1);
5454
}
5555

56+
if ((process.env.npm_config_NAPI_VERSION !== undefined) &&
57+
(process.env.npm_config_NAPI_VERSION < 3)) {
58+
testModules.splice(testModules.indexOf('callbackscope'), 1);
59+
testModules.splice(testModules.indexOf('version_management'), 1);
60+
}
61+
5662
if (typeof global.gc === 'function') {
5763
console.log('Starting test suite\n');
5864

0 commit comments

Comments
 (0)