@@ -129,12 +129,8 @@ class Reference {
129
129
_finalizeData(finalizeData),
130
130
_finalizeHint(finalizeHint) {
131
131
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 );
138
134
_persistent.MarkIndependent ();
139
135
}
140
136
}
@@ -159,12 +155,8 @@ class Reference {
159
155
160
156
int Release () {
161
157
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 );
168
160
_persistent.MarkIndependent ();
169
161
}
170
162
@@ -175,7 +167,7 @@ class Reference {
175
167
if (_persistent.IsEmpty ()) {
176
168
return v8::Local<v8::Value>();
177
169
} else {
178
- return _persistent. Get (_isolate);
170
+ return v8::Local<v8::Value>:: New (_isolate, _persistent );
179
171
}
180
172
}
181
173
@@ -293,7 +285,8 @@ class CallbackWrapperBase : public CallbackWrapper {
293
285
cb (v8impl::JsEnvFromV8Isolate (isolate), cbinfo_wrapper);
294
286
295
287
if (!TryCatch::lastException ().IsEmpty ()) {
296
- isolate->ThrowException (TryCatch::lastException ().Get (isolate));
288
+ isolate->ThrowException (
289
+ v8::Local<v8::Value>::New (isolate, TryCatch::lastException ()));
297
290
TryCatch::lastException ().Reset ();
298
291
}
299
292
}
@@ -491,21 +484,25 @@ void napi_module_register_cb(v8::Local<v8::Object> exports,
491
484
mod->nm_priv );
492
485
}
493
486
487
+ #ifndef EXTERNAL_NAPI
494
488
namespace node {
495
489
// Indicates whether NAPI was enabled/disabled via the node command-line.
496
490
extern bool load_napi_modules;
497
491
}
492
+ #endif // EXTERNAL_NAPI
498
493
499
494
// Registers a NAPI module.
500
495
void napi_module_register (napi_module* mod) {
501
496
// NAPI modules always work with the current node version.
502
497
int moduleVersion = NODE_MODULE_VERSION;
503
498
499
+ #ifndef EXTERNAL_NAPI
504
500
if (!node::load_napi_modules) {
505
501
// NAPI is disabled, so set the module version to -1 to cause the module
506
502
// to be unloaded.
507
503
moduleVersion = -1 ;
508
504
}
505
+ #endif // EXTERNAL_NAPI
509
506
510
507
node::node_module* nm = new node::node_module {
511
508
moduleVersion,
@@ -1802,28 +1799,19 @@ napi_status napi_wrap(napi_env e,
1802
1799
1803
1800
obj->SetAlignedPointerInInternalField (0 , nativeObj);
1804
1801
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
-
1818
1802
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 );
1826
1810
*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);
1827
1815
}
1828
1816
1829
1817
return GET_RETURN_STATUS ();
@@ -2164,8 +2152,8 @@ napi_status napi_get_and_clear_last_exception(napi_env e, napi_value* result) {
2164
2152
if (v8impl::TryCatch::lastException ().IsEmpty ()) {
2165
2153
return napi_get_undefined (e, result);
2166
2154
} 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 ( )));
2169
2157
v8impl::TryCatch::lastException ().Reset ();
2170
2158
}
2171
2159
0 commit comments