Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pquerna/473638 to your computer and use it in GitHub Desktop.
Save pquerna/473638 to your computer and use it in GitHub Desktop.
From c14bdfbb0582fa2da347aeed4a97e3eb76a8e0c8 Mon Sep 17 00:00:00 2001
From: Paul Querna <pquerna@apache.org>
Date: Tue, 13 Jul 2010 00:22:33 -0700
Subject: [PATCH 1/3] Add basic structure and macros for node modules.
---
src/node.h | 30 ++++++++++++++++++++++++++++++
1 files changed, 30 insertions(+), 0 deletions(-)
diff --git a/src/node.h b/src/node.h
index 065bf2c..c11290b 100644
--- a/src/node.h
+++ b/src/node.h
@@ -82,5 +82,35 @@ v8::Local<v8::Value> ErrnoException(int errorno,
const char *path = NULL);
const char *signo_string(int errorno);
+
+
+struct node_module_struct {
+ int version;
+ void *dso_handle;
+ const char *name;
+ void (*register_func) (Handle<Object> target);
+};
+
+/**
+ * When this version number is changed, node.js will refuse
+ * to load older modules. This should be done whenever
+ * an API is broken in the C++ side, including in v8 or
+ * other dependencies
+ */
+#define NODE_MODULE_VERSION (1)
+
+#define NODE_STANDARD_MODULE_STUFF \
+ NODE_MODULE_VERSION, \
+ NULL, \
+ __FILE__
+
+#define NODE_MODULE(modname, regfunc) \
+ node_module_t modname ## _module = \
+ { \
+ NODE_STANDARD_MODULE_STUFF, \
+ regfunc \
+ };
+
+
} // namespace node
#endif // SRC_NODE_H_
--
1.7.0
From c3bea980de56e819386d5cd2f8b971dc438693dc Mon Sep 17 00:00:00 2001
From: Paul Querna <pquerna@apache.org>
Date: Tue, 13 Jul 2010 01:24:41 -0700
Subject: [PATCH 2/3] Fixes to the module structure for living in the node:: namespace,
---
src/node.h | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/node.h b/src/node.h
index c11290b..aec0955 100644
--- a/src/node.h
+++ b/src/node.h
@@ -88,7 +88,7 @@ struct node_module_struct {
int version;
void *dso_handle;
const char *name;
- void (*register_func) (Handle<Object> target);
+ void (*register_func) (v8::Handle<v8::Object> target);
};
/**
@@ -105,7 +105,7 @@ struct node_module_struct {
__FILE__
#define NODE_MODULE(modname, regfunc) \
- node_module_t modname ## _module = \
+ node::node_module_struct modname ## _module = \
{ \
NODE_STANDARD_MODULE_STUFF, \
regfunc \
--
1.7.0
From a62185e37ef726bc6984d548b73db1c7f4b9944a Mon Sep 17 00:00:00 2001
From: Paul Querna <pquerna@apache.org>
Date: Tue, 13 Jul 2010 01:33:51 -0700
Subject: [PATCH 3/3] Add support for the module structure to process.dlopen.
---
src/node.cc | 47 ++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 42 insertions(+), 5 deletions(-)
diff --git a/src/node.cc b/src/node.cc
index 5aa3712..79d41aa 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -1354,18 +1354,55 @@ Handle<Value> DLOpen(const v8::Arguments& args) {
return ThrowException(exception);
}
+ String::Utf8Value symbol(args[0]->ToString());
+ char *symstr = NULL;
+ {
+ char *sym = *symbol;
+ char *p = strrchr(sym, '/');
+ if (p != NULL) {
+ sym = p+1;
+ }
+
+ p = strrchr(sym, '.');
+ if (p != NULL) {
+ *p = NULL;
+ }
+
+ size_t slen = strlen(sym);
+ symstr = static_cast<char*>(calloc(1, slen + sizeof("_module") + 1));
+ memcpy(symstr, sym, slen);
+ memcpy(symstr+slen, "_module", sizeof("_module") + 1);
+ }
+
// Get the init() function from the dynamically shared object.
- void *init_handle = dlsym(handle, "init");
+ node_module_struct *mod = static_cast<node_module_struct *>(dlsym(handle, symstr));
+ free(symstr);
// Error out if not found.
- if (init_handle == NULL) {
+ if (mod == NULL) {
+
+ /* Start Compatibility hack: Remove once everyone is using NODE_MODULE macro */
+ node_module_struct compat_mod;
+ mod = &compat_mod;
+ mod->version = NODE_MODULE_VERSION;
+
+ void *init_handle = dlsym(handle, "init");
+ if (init_handle == NULL) {
+ Local<Value> exception =
+ Exception::Error(String::New("No module symbol found in module."));
+ return ThrowException(exception);
+ }
+ mod->register_func = (extInit)(init_handle);
+ /* End Compatibility hack */
+ }
+
+ if (mod->version != NODE_MODULE_VERSION) {
Local<Value> exception =
- Exception::Error(String::New("No 'init' symbol found in module."));
+ Exception::Error(String::New("Module version mismatch, refusing to load."));
return ThrowException(exception);
}
- extInit init = (extInit)(init_handle); // Cast
// Execute the C++ module
- init(target);
+ mod->register_func(target);
return Undefined();
}
--
1.7.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment