diff --git a/src/node.cc b/src/node.cc index 6b697d3f3e..e5ec939c41 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1571,6 +1571,7 @@ static Handle Binding(const Arguments& args) { Local module = args[0]->ToString(); String::Utf8Value module_v(module); + node_module_struct* modp; if (binding_cache.IsEmpty()) { binding_cache = Persistent::New(Object::New()); @@ -1578,9 +1579,15 @@ static Handle Binding(const Arguments& args) { Local exports; - // TODO DRY THIS UP! - - if (!strcmp(*module_v, "stdio")) { + if (binding_cache->Has(module)) { + exports = binding_cache->Get(module)->ToObject(); + } + else if ((modp = get_builtin_module(*module_v)) != NULL) { + exports = Object::New(); + modp->register_func(exports); + binding_cache->Set(module, exports); + } + else if (!strcmp(*module_v, "stdio")) { if (binding_cache->Has(module)) { exports = binding_cache->Get(module)->ToObject(); } else { @@ -1623,15 +1630,6 @@ static Handle Binding(const Arguments& args) { binding_cache->Set(module, exports); } - } else if (!strcmp(*module_v, "net")) { - if (binding_cache->Has(module)) { - exports = binding_cache->Get(module)->ToObject(); - } else { - exports = Object::New(); - InitNet(exports); - binding_cache->Set(module, exports); - } - } else if (!strcmp(*module_v, "http_parser")) { if (binding_cache->Has(module)) { exports = binding_cache->Get(module)->ToObject(); @@ -1658,16 +1656,6 @@ static Handle Binding(const Arguments& args) { Buffer::Initialize(exports); binding_cache->Set(module, exports); } - #ifdef HAVE_OPENSSL - } else if (!strcmp(*module_v, "crypto")) { - if (binding_cache->Has(module)) { - exports = binding_cache->Get(module)->ToObject(); - } else { - exports = Object::New(); - InitCrypto(exports); - binding_cache->Set(module, exports); - } - #endif } else if (!strcmp(*module_v, "evals")) { if (binding_cache->Has(module)) { exports = binding_cache->Get(module)->ToObject(); diff --git a/src/node.h b/src/node.h index aec0955786..d879e4b236 100644 --- a/src/node.h +++ b/src/node.h @@ -10,6 +10,11 @@ #include +#ifndef NODE_STRINGIFY +#define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n) +#define NODE_STRINGIFY_HELPER(n) #n +#endif + namespace node { #define NODE_PSYMBOL(s) Persistent::New(String::NewSymbol(s)) @@ -83,14 +88,16 @@ v8::Local ErrnoException(int errorno, const char *signo_string(int errorno); - struct node_module_struct { int version; void *dso_handle; - const char *name; + const char *filename; void (*register_func) (v8::Handle target); + const char *modname; }; +node_module_struct* get_builtin_module(const char *name); + /** * When this version number is changed, node.js will refuse * to load older modules. This should be done whenever @@ -108,9 +115,13 @@ struct node_module_struct { node::node_module_struct modname ## _module = \ { \ NODE_STANDARD_MODULE_STUFF, \ - regfunc \ + regfunc, \ + NODE_STRINGIFY(modname) \ }; +#define NODE_MODULE_DECL(modname) \ + extern node::node_module_struct modname ## _module; + } // namespace node #endif // SRC_NODE_H_ diff --git a/src/node_crypto.cc b/src/node_crypto.cc index ef314fa2ab..c16f660698 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -2264,7 +2264,7 @@ void InitCrypto(Handle target) { version_symbol = NODE_PSYMBOL("version"); } - - } // namespace node +NODE_MODULE(node_crypto, node::InitCrypto); + diff --git a/src/node_extensions.cc b/src/node_extensions.cc new file mode 100644 index 0000000000..776bb40af8 --- /dev/null +++ b/src/node_extensions.cc @@ -0,0 +1,46 @@ + +#include "node.h" +#include "node_version.h" +#include + +#undef NODE_EXT_LIST_START +#undef NODE_EXT_LIST_ITEM +#undef NODE_EXT_LIST_END + +#define NODE_EXT_LIST_START +#define NODE_EXT_LIST_ITEM NODE_MODULE_DECL +#define NODE_EXT_LIST_END + +#include "node_extensions.h" + +#undef NODE_EXT_LIST_START +#undef NODE_EXT_LIST_ITEM +#undef NODE_EXT_LIST_END + +#define NODE_EXT_STRING(x) &x ## _module, +#define NODE_EXT_LIST_START node::node_module_struct *node_module_list[] = { +#define NODE_EXT_LIST_ITEM NODE_EXT_STRING +#define NODE_EXT_LIST_END NULL}; + +#include "node_extensions.h" + +namespace node { + +node_module_struct* get_builtin_module(const char *name) +{ + char buf[128]; + node_module_struct *cur = NULL; + snprintf(buf, sizeof(buf), "node_%s", name); + /* TODO: you could look these up in a hash, but there are only + * a few, and once loaded they are cached. */ + for (int i = 0; node_module_list[i] != NULL; i++) { + cur = node_module_list[i]; + if (strcmp(cur->modname, buf) == 0) { + return cur; + } + } + + return NULL; +} + +}; // namespace node diff --git a/src/node_extensions.h b/src/node_extensions.h new file mode 100644 index 0000000000..f463a8cb7e --- /dev/null +++ b/src/node_extensions.h @@ -0,0 +1,8 @@ + +NODE_EXT_LIST_START +NODE_EXT_LIST_ITEM(node_net) +#ifdef HAVE_OPENSSL +NODE_EXT_LIST_ITEM(node_crypto) +#endif +NODE_EXT_LIST_END + diff --git a/src/node_net.cc b/src/node_net.cc index 0e8ad81971..1f2ae7ca44 100644 --- a/src/node_net.cc +++ b/src/node_net.cc @@ -1266,3 +1266,5 @@ void InitNet(Handle target) { } } // namespace node + +NODE_MODULE(node_net, node::InitNet); diff --git a/wscript b/wscript index d537fd1b5a..22152b0e05 100644 --- a/wscript +++ b/wscript @@ -459,6 +459,7 @@ def build(bld): node.source = """ src/node.cc src/node_buffer.cc + src/node_extensions.cc src/node_http_parser.cc src/node_net.cc src/node_io_watcher.cc