Skip to content

Instantly share code, notes, and snippets.

@gabrielschulhof
Created February 23, 2017 19:15
Show Gist options
  • Save gabrielschulhof/763a8563deab4eb5f681df3817658fe0 to your computer and use it in GitHub Desktop.
Save gabrielschulhof/763a8563deab4eb5f681df3817658fe0 to your computer and use it in GitHub Desktop.
diff --git a/src/node.cc b/src/node.cc
index 90eee0f..c2ef453 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -29,6 +29,10 @@
#include "node_lttng.h"
#endif
+#if defined ENABLE_NAPI
+#include "node_jsvmapi_internal.h"
+#endif
+
#include "ares.h"
#include "async-wrap.h"
#include "async-wrap-inl.h"
@@ -159,6 +163,9 @@ static const char* trace_enabled_categories = nullptr;
static const char* icu_data_dir = nullptr;
#endif
+// By default we accept N-API addons
+bool no_napi_modules = false;
+
// used by C++ modules as well
bool no_deprecation = false;
@@ -2401,7 +2408,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
// Objects containing v14 or later modules will have registered themselves
// on the pending list. Activate all of them now. At present, only one
// module per object is supported.
- node_module* const mp = modpending;
+ node_module* mp = modpending;
modpending = nullptr;
if (is_dlopen_error) {
@@ -2420,7 +2427,14 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
env->ThrowError("Module did not self-register.");
return;
}
+
+#ifdef ENABLE_NAPI
+ bool isNapiModule = (!no_napi_modules && mp->nm_version == -1);
+
+ if (mp->nm_version != NODE_MODULE_VERSION && !isNapiModule) {
+#else /* !defined ENABLE_NAPI */
if (mp->nm_version != NODE_MODULE_VERSION) {
+#endif /* def ENABLE_NAPI */
char errmsg[1024];
snprintf(errmsg,
sizeof(errmsg),
@@ -2451,6 +2465,21 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
Local<String> exports_string = env->exports_string();
Local<Object> exports = module->Get(exports_string)->ToObject(env->isolate());
+#ifdef ENABLE_NAPI
+ if (isNapiModule) {
+ if (mp->nm_register_func != nullptr) {
+ reinterpret_cast<node::addon_abi_register_func>(mp->nm_register_func)(
+ v8impl::JsEnvFromV8Isolate(v8::Isolate::GetCurrent()),
+ v8impl::JsValueFromV8LocalValue(exports),
+ v8impl::JsValueFromV8LocalValue(module),
+ mp->nm_priv);
+ } else {
+ uv_dlclose(&lib);
+ env->ThrowError("Module has no declared entry point.");
+ }
+ return;
+ }
+#endif /* def ENABLE_NAPI */
if (mp->nm_context_register_func != nullptr) {
mp->nm_context_register_func(exports, module, env->context(), mp->nm_priv);
} else if (mp->nm_register_func != nullptr) {
@@ -2465,7 +2494,6 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
// coverity[leaked_storage]
}
-
static void OnFatalError(const char* location, const char* message) {
if (location) {
PrintErrorString("FATAL ERROR: %s %s\n", location, message);
@@ -2627,8 +2655,10 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(mod->nm_register_func, nullptr);
CHECK_NE(mod->nm_context_register_func, nullptr);
Local<Value> unused = Undefined(env->isolate());
- mod->nm_context_register_func(exports, unused,
- env->context(), mod->nm_priv);
+ if (mod->nm_context_register_func != nullptr) {
+ mod->nm_context_register_func(exports, unused,
+ env->context(), mod->nm_priv);
+ }
cache->Set(module, exports);
} else if (!strcmp(*module_v, "constants")) {
exports = Object::New(env->isolate());
@@ -2684,7 +2714,19 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
env->context(),
mod->nm_priv);
} else if (mod->nm_register_func != nullptr) {
+#ifdef ENABLE_NAPI
+ if (mod->nm_version != -1) {
+ mod->nm_register_func(exports, module, mod->nm_priv);
+ } else {
+ reinterpret_cast<node::addon_abi_register_func>(mod->nm_register_func)(
+ v8impl::JsEnvFromV8Isolate(v8::Isolate::GetCurrent()),
+ v8impl::JsValueFromV8LocalValue(exports),
+ v8impl::JsValueFromV8LocalValue(module),
+ mod->nm_priv);
+ }
+#else /* !defined ENABLE_NAPI */
mod->nm_register_func(exports, module, mod->nm_priv);
+#endif /* def ENABLE_NAPI */
} else {
return env->ThrowError("Linked module has no declared entry point.");
}
@@ -3668,6 +3710,8 @@ static void ParseArgs(int* argc,
force_repl = true;
} else if (strcmp(arg, "--no-deprecation") == 0) {
no_deprecation = true;
+ } else if (strcmp(arg, "--no-napi-modules") == 0) {
+ no_napi_modules = true;
} else if (strcmp(arg, "--no-warnings") == 0) {
no_process_warnings = true;
} else if (strcmp(arg, "--trace-warnings") == 0) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment