Skip to content

Commit

Permalink
Enable back-compat with pre-N-API node (nodejs#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin authored Mar 20, 2017
1 parent 7b49c89 commit ada8aa8
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 173 deletions.
16 changes: 15 additions & 1 deletion src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@
#ifndef SRC_NODE_H_
#define SRC_NODE_H_

#include "node_macros.h"
#ifdef _WIN32
# ifndef BUILDING_NODE_EXTENSION
# define NODE_EXTERN __declspec(dllexport)
# else
# define NODE_EXTERN __declspec(dllimport)
# endif
#else
# define NODE_EXTERN /* nothing */
#endif

#ifdef BUILDING_NODE_EXTENSION
# undef BUILDING_V8_SHARED
Expand Down Expand Up @@ -421,6 +429,12 @@ node_module* get_linked_module(const char *name);

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

#ifdef _WIN32
# define NODE_MODULE_EXPORT __declspec(dllexport)
#else
# define NODE_MODULE_EXPORT __attribute__((visibility("default")))
#endif

#ifdef NODE_SHARED_MODE
# define NODE_CTOR_PREFIX
#else
Expand Down
60 changes: 24 additions & 36 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,8 @@ class Reference {
_finalizeData(finalizeData),
_finalizeHint(finalizeHint) {
if (initialRefcount == 0) {
if (_finalizeCallback != nullptr || _deleteSelf) {
_persistent.SetWeak(
this, FinalizeCallback, v8::WeakCallbackType::kParameter);
} else {
_persistent.SetWeak();
}
_persistent.SetWeak(
this, FinalizeCallback, v8::WeakCallbackType::kParameter);
_persistent.MarkIndependent();
}
}
Expand All @@ -159,12 +155,8 @@ class Reference {

int Release() {
if (--_refcount == 0) {
if (_finalizeCallback != nullptr || _deleteSelf) {
_persistent.SetWeak(
this, FinalizeCallback, v8::WeakCallbackType::kParameter);
} else {
_persistent.SetWeak();
}
_persistent.SetWeak(
this, FinalizeCallback, v8::WeakCallbackType::kParameter);
_persistent.MarkIndependent();
}

Expand All @@ -175,7 +167,7 @@ class Reference {
if (_persistent.IsEmpty()) {
return v8::Local<v8::Value>();
} else {
return _persistent.Get(_isolate);
return v8::Local<v8::Value>::New(_isolate, _persistent);
}
}

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

if (!TryCatch::lastException().IsEmpty()) {
isolate->ThrowException(TryCatch::lastException().Get(isolate));
isolate->ThrowException(
v8::Local<v8::Value>::New(isolate, TryCatch::lastException()));
TryCatch::lastException().Reset();
}
}
Expand Down Expand Up @@ -491,21 +484,25 @@ void napi_module_register_cb(v8::Local<v8::Object> exports,
mod->nm_priv);
}

#ifndef EXTERNAL_NAPI
namespace node {
// Indicates whether NAPI was enabled/disabled via the node command-line.
extern bool load_napi_modules;
}
#endif // EXTERNAL_NAPI

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

#ifndef EXTERNAL_NAPI
if (!node::load_napi_modules) {
// NAPI is disabled, so set the module version to -1 to cause the module
// to be unloaded.
moduleVersion = -1;
}
#endif // EXTERNAL_NAPI

node::node_module* nm = new node::node_module {
moduleVersion,
Expand Down Expand Up @@ -1802,28 +1799,19 @@ napi_status napi_wrap(napi_env e,

obj->SetAlignedPointerInInternalField(0, nativeObj);

if (finalize_cb != nullptr) {
// Create a separate self-deleting reference for the finalizer callback.
// This ensures the finalizer is not dependent on the lifetime of the
// returned reference.
new v8impl::Reference(isolate,
obj,
0,
true,
finalize_cb,
nativeObj,
finalize_hint);
}

if (result != nullptr) {
// If a reference result was requested, create one that is separate from the
// reference
// that holds the finalizer callback. The returned reference should be
// deleted via
// napi_delete_reference() when it is no longer needed.
v8impl::Reference* reference =
new v8impl::Reference(isolate, obj, 0, false);
// The returned reference should be deleted via napi_delete_reference()
// ONLY in response to the finalize callback invocation. (If it is deleted
// before then, then the finalize callback will never be invoked.)
// Therefore a finalize callback is required when returning a reference.
CHECK_ARG(finalize_cb);
v8impl::Reference* reference = new v8impl::Reference(
isolate, obj, 0, false, finalize_cb, nativeObj, finalize_hint);
*result = reinterpret_cast<napi_ref>(reference);
} else if (finalize_cb != nullptr) {
// Create a self-deleting reference just for the finalize callback.
new v8impl::Reference(
isolate, obj, 0, true, finalize_cb, nativeObj, finalize_hint);
}

return GET_RETURN_STATUS();
Expand Down Expand Up @@ -2164,8 +2152,8 @@ napi_status napi_get_and_clear_last_exception(napi_env e, napi_value* result) {
if (v8impl::TryCatch::lastException().IsEmpty()) {
return napi_get_undefined(e, result);
} else {
*result = v8impl::JsValueFromV8LocalValue(
v8impl::TryCatch::lastException().Get(v8impl::V8IsolateFromJsEnv(e)));
*result = v8impl::JsValueFromV8LocalValue(v8::Local<v8::Value>::New(
v8impl::V8IsolateFromJsEnv(e), v8impl::TryCatch::lastException()));
v8impl::TryCatch::lastException().Reset();
}

Expand Down
Loading

0 comments on commit ada8aa8

Please sign in to comment.