Skip to content

Commit ada8aa8

Browse files
authored
Enable back-compat with pre-N-API node (nodejs#167)
1 parent 7b49c89 commit ada8aa8

File tree

5 files changed

+167
-173
lines changed

5 files changed

+167
-173
lines changed

src/node.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@
2222
#ifndef SRC_NODE_H_
2323
#define SRC_NODE_H_
2424

25-
#include "node_macros.h"
25+
#ifdef _WIN32
26+
# ifndef BUILDING_NODE_EXTENSION
27+
# define NODE_EXTERN __declspec(dllexport)
28+
# else
29+
# define NODE_EXTERN __declspec(dllimport)
30+
# endif
31+
#else
32+
# define NODE_EXTERN /* nothing */
33+
#endif
2634

2735
#ifdef BUILDING_NODE_EXTENSION
2836
# undef BUILDING_V8_SHARED
@@ -421,6 +429,12 @@ node_module* get_linked_module(const char *name);
421429

422430
extern "C" NODE_EXTERN void node_module_register(void* mod);
423431

432+
#ifdef _WIN32
433+
# define NODE_MODULE_EXPORT __declspec(dllexport)
434+
#else
435+
# define NODE_MODULE_EXPORT __attribute__((visibility("default")))
436+
#endif
437+
424438
#ifdef NODE_SHARED_MODE
425439
# define NODE_CTOR_PREFIX
426440
#else

src/node_api.cc

+24-36
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,8 @@ class Reference {
129129
_finalizeData(finalizeData),
130130
_finalizeHint(finalizeHint) {
131131
if (initialRefcount == 0) {
132-
if (_finalizeCallback != nullptr || _deleteSelf) {
133-
_persistent.SetWeak(
134-
this, FinalizeCallback, v8::WeakCallbackType::kParameter);
135-
} else {
136-
_persistent.SetWeak();
137-
}
132+
_persistent.SetWeak(
133+
this, FinalizeCallback, v8::WeakCallbackType::kParameter);
138134
_persistent.MarkIndependent();
139135
}
140136
}
@@ -159,12 +155,8 @@ class Reference {
159155

160156
int Release() {
161157
if (--_refcount == 0) {
162-
if (_finalizeCallback != nullptr || _deleteSelf) {
163-
_persistent.SetWeak(
164-
this, FinalizeCallback, v8::WeakCallbackType::kParameter);
165-
} else {
166-
_persistent.SetWeak();
167-
}
158+
_persistent.SetWeak(
159+
this, FinalizeCallback, v8::WeakCallbackType::kParameter);
168160
_persistent.MarkIndependent();
169161
}
170162

@@ -175,7 +167,7 @@ class Reference {
175167
if (_persistent.IsEmpty()) {
176168
return v8::Local<v8::Value>();
177169
} else {
178-
return _persistent.Get(_isolate);
170+
return v8::Local<v8::Value>::New(_isolate, _persistent);
179171
}
180172
}
181173

@@ -293,7 +285,8 @@ class CallbackWrapperBase : public CallbackWrapper {
293285
cb(v8impl::JsEnvFromV8Isolate(isolate), cbinfo_wrapper);
294286

295287
if (!TryCatch::lastException().IsEmpty()) {
296-
isolate->ThrowException(TryCatch::lastException().Get(isolate));
288+
isolate->ThrowException(
289+
v8::Local<v8::Value>::New(isolate, TryCatch::lastException()));
297290
TryCatch::lastException().Reset();
298291
}
299292
}
@@ -491,21 +484,25 @@ void napi_module_register_cb(v8::Local<v8::Object> exports,
491484
mod->nm_priv);
492485
}
493486

487+
#ifndef EXTERNAL_NAPI
494488
namespace node {
495489
// Indicates whether NAPI was enabled/disabled via the node command-line.
496490
extern bool load_napi_modules;
497491
}
492+
#endif // EXTERNAL_NAPI
498493

499494
// Registers a NAPI module.
500495
void napi_module_register(napi_module* mod) {
501496
// NAPI modules always work with the current node version.
502497
int moduleVersion = NODE_MODULE_VERSION;
503498

499+
#ifndef EXTERNAL_NAPI
504500
if (!node::load_napi_modules) {
505501
// NAPI is disabled, so set the module version to -1 to cause the module
506502
// to be unloaded.
507503
moduleVersion = -1;
508504
}
505+
#endif // EXTERNAL_NAPI
509506

510507
node::node_module* nm = new node::node_module {
511508
moduleVersion,
@@ -1802,28 +1799,19 @@ napi_status napi_wrap(napi_env e,
18021799

18031800
obj->SetAlignedPointerInInternalField(0, nativeObj);
18041801

1805-
if (finalize_cb != nullptr) {
1806-
// Create a separate self-deleting reference for the finalizer callback.
1807-
// This ensures the finalizer is not dependent on the lifetime of the
1808-
// returned reference.
1809-
new v8impl::Reference(isolate,
1810-
obj,
1811-
0,
1812-
true,
1813-
finalize_cb,
1814-
nativeObj,
1815-
finalize_hint);
1816-
}
1817-
18181802
if (result != nullptr) {
1819-
// If a reference result was requested, create one that is separate from the
1820-
// reference
1821-
// that holds the finalizer callback. The returned reference should be
1822-
// deleted via
1823-
// napi_delete_reference() when it is no longer needed.
1824-
v8impl::Reference* reference =
1825-
new v8impl::Reference(isolate, obj, 0, false);
1803+
// The returned reference should be deleted via napi_delete_reference()
1804+
// ONLY in response to the finalize callback invocation. (If it is deleted
1805+
// before then, then the finalize callback will never be invoked.)
1806+
// Therefore a finalize callback is required when returning a reference.
1807+
CHECK_ARG(finalize_cb);
1808+
v8impl::Reference* reference = new v8impl::Reference(
1809+
isolate, obj, 0, false, finalize_cb, nativeObj, finalize_hint);
18261810
*result = reinterpret_cast<napi_ref>(reference);
1811+
} else if (finalize_cb != nullptr) {
1812+
// Create a self-deleting reference just for the finalize callback.
1813+
new v8impl::Reference(
1814+
isolate, obj, 0, true, finalize_cb, nativeObj, finalize_hint);
18271815
}
18281816

18291817
return GET_RETURN_STATUS();
@@ -2164,8 +2152,8 @@ napi_status napi_get_and_clear_last_exception(napi_env e, napi_value* result) {
21642152
if (v8impl::TryCatch::lastException().IsEmpty()) {
21652153
return napi_get_undefined(e, result);
21662154
} else {
2167-
*result = v8impl::JsValueFromV8LocalValue(
2168-
v8impl::TryCatch::lastException().Get(v8impl::V8IsolateFromJsEnv(e)));
2155+
*result = v8impl::JsValueFromV8LocalValue(v8::Local<v8::Value>::New(
2156+
v8impl::V8IsolateFromJsEnv(e), v8impl::TryCatch::lastException()));
21692157
v8impl::TryCatch::lastException().Reset();
21702158
}
21712159

0 commit comments

Comments
 (0)