Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active December 11, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/4629455 to your computer and use it in GitHub Desktop.
Save isaacs/4629455 to your computer and use it in GitHub Desktop.
diff --git a/lib/module.js b/lib/module.js
index 67a95f0..a7c7115 100644
--- a/lib/module.js
+++ b/lib/module.js
@@ -488,9 +488,7 @@ Module._extensions['.json'] = function(module, filename) {
//Native extension for .node
-Module._extensions['.node'] = function(module, filename) {
- process.dlopen(filename, module.exports);
-};
+Module._extensions['.node'] = process.dlopen;
// bootstrap main module.
diff --git a/src/node.cc b/src/node.cc
index 32cff12..fe7d4bc 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -101,6 +101,8 @@ Persistent<String> domain_symbol;
static Persistent<Object> process;
+static Persistent<String> exports_symbol;
+
static Persistent<String> errno_symbol;
static Persistent<String> syscall_symbol;
static Persistent<String> errpath_symbol;
@@ -1786,8 +1788,8 @@ Handle<Value> Hrtime(const v8::Arguments& args) {
typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);
-// DLOpen is node.dlopen(). Used to load 'module.node' dynamically shared
-// objects.
+// DLOpen is process.dlopen(module, filename).
+// Used to load 'module.node' dynamically shared objects.
Handle<Value> DLOpen(const v8::Arguments& args) {
HandleScope scope;
char symbol[1024], *base, *pos;
@@ -1800,8 +1802,13 @@ Handle<Value> DLOpen(const v8::Arguments& args) {
return ThrowException(exception);
}
- String::Utf8Value filename(args[0]); // Cast
- Local<Object> target = args[1]->ToObject(); // Cast
+ Local<Object> module = args[0]->ToObject(); // Cast
+ String::Utf8Value filename(args[1]); // Cast
+
+ if (exports_symbol.IsEmpty()) {
+ exports_symbol = NODE_PSYMBOL("exports");
+ }
+ Local<Object> exports = module->Get(exports_symbol)->ToObject();
if (uv_dlopen(*filename, &lib)) {
Local<String> errmsg = String::New(uv_dlerror(&lib));
@@ -1812,7 +1819,7 @@ Handle<Value> DLOpen(const v8::Arguments& args) {
return ThrowException(Exception::Error(errmsg));
}
- String::Utf8Value path(args[0]);
+ String::Utf8Value path(args[1]);
base = *path;
/* Find the shared library filename within the full path. */
@@ -1869,7 +1876,7 @@ Handle<Value> DLOpen(const v8::Arguments& args) {
}
// Execute the C++ module
- mod->register_func(target);
+ mod->register_func(exports, module);
// Tell coverity that 'handle' should not be freed when we return.
// coverity[leaked_storage]
@@ -1953,7 +1960,9 @@ static Handle<Value> Binding(const Arguments& args) {
if ((modp = get_builtin_module(*module_v)) != NULL) {
exports = Object::New();
- modp->register_func(exports);
+ // Internal bindings don't have a "module" object,
+ // only exports.
+ modp->register_func(exports, Undefined());
binding_cache->Set(module, exports);
} else if (!strcmp(*module_v, "constants")) {
diff --git a/src/node.h b/src/node.h
index 51da0d9..682b505 100644
--- a/src/node.h
+++ b/src/node.h
@@ -83,6 +83,7 @@
# endif
#endif
+
namespace node {
NODE_EXTERN extern bool no_deprecation;
@@ -198,11 +199,15 @@ NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(int errorno,
const char *signo_string(int errorno);
+
+NODE_EXTERN typedef void (* addon_register_func)(
+ v8::Handle<v8::Object> exports, v8::Handle<v8::Value> module);
+
struct node_module_struct {
int version;
void *dso_handle;
const char *filename;
- void (*register_func) (v8::Handle<v8::Object> target);
+ node::addon_register_func register_func;
const char *modname;
};
@@ -214,7 +219,7 @@ node_module_struct* get_builtin_module(const char *name);
* an API is broken in the C++ side, including in v8 or
* other dependencies.
*/
-#define NODE_MODULE_VERSION 0x000A /* v0.10 */
+#define NODE_MODULE_VERSION 0x000B /* v0.11 */
#define NODE_STANDARD_MODULE_STUFF \
NODE_MODULE_VERSION, \
@@ -232,7 +237,7 @@ node_module_struct* get_builtin_module(const char *name);
NODE_MODULE_EXPORT node::node_module_struct modname ## _module = \
{ \
NODE_STANDARD_MODULE_STUFF, \
- regfunc, \
+ (node::addon_register_func)regfunc, \
NODE_STRINGIFY(modname) \
}; \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment