Skip to content

Commit 30dadfc

Browse files
pquernary
authored andcommitted
Register builtin extensions via a macro, rather than a manual strcmp
Set the stage for making the builtin modules more dynamic. Note: this only converts crypto and net, I will add more extensions in a later commit. * node.h: Add utility macro for converting macro values to strings. * node.h: Include the actual module name inside the module structure, not just the file it was built from. * node.h: New Macro, NODE_MODULE_DECL, for declaring an external reference to a module structure. * node_extensions.cc: New File, implements get_builtin_module, which iterates over the module structures that are compiled into node. * node.cc(node::Binding): Use the new module lookup function to find modules. * node_{net,crypto}.c: Add NODE_MODULEs to generate the module structure.
1 parent 781d512 commit 30dadfc

7 files changed

Lines changed: 83 additions & 27 deletions

File tree

src/node.cc

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,16 +1571,23 @@ static Handle<Value> Binding(const Arguments& args) {
15711571

15721572
Local<String> module = args[0]->ToString();
15731573
String::Utf8Value module_v(module);
1574+
node_module_struct* modp;
15741575

15751576
if (binding_cache.IsEmpty()) {
15761577
binding_cache = Persistent<Object>::New(Object::New());
15771578
}
15781579

15791580
Local<Object> exports;
15801581

1581-
// TODO DRY THIS UP!
1582-
1583-
if (!strcmp(*module_v, "stdio")) {
1582+
if (binding_cache->Has(module)) {
1583+
exports = binding_cache->Get(module)->ToObject();
1584+
}
1585+
else if ((modp = get_builtin_module(*module_v)) != NULL) {
1586+
exports = Object::New();
1587+
modp->register_func(exports);
1588+
binding_cache->Set(module, exports);
1589+
}
1590+
else if (!strcmp(*module_v, "stdio")) {
15841591
if (binding_cache->Has(module)) {
15851592
exports = binding_cache->Get(module)->ToObject();
15861593
} else {
@@ -1623,15 +1630,6 @@ static Handle<Value> Binding(const Arguments& args) {
16231630
binding_cache->Set(module, exports);
16241631
}
16251632

1626-
} else if (!strcmp(*module_v, "net")) {
1627-
if (binding_cache->Has(module)) {
1628-
exports = binding_cache->Get(module)->ToObject();
1629-
} else {
1630-
exports = Object::New();
1631-
InitNet(exports);
1632-
binding_cache->Set(module, exports);
1633-
}
1634-
16351633
} else if (!strcmp(*module_v, "http_parser")) {
16361634
if (binding_cache->Has(module)) {
16371635
exports = binding_cache->Get(module)->ToObject();
@@ -1658,16 +1656,6 @@ static Handle<Value> Binding(const Arguments& args) {
16581656
Buffer::Initialize(exports);
16591657
binding_cache->Set(module, exports);
16601658
}
1661-
#ifdef HAVE_OPENSSL
1662-
} else if (!strcmp(*module_v, "crypto")) {
1663-
if (binding_cache->Has(module)) {
1664-
exports = binding_cache->Get(module)->ToObject();
1665-
} else {
1666-
exports = Object::New();
1667-
InitCrypto(exports);
1668-
binding_cache->Set(module, exports);
1669-
}
1670-
#endif
16711659
} else if (!strcmp(*module_v, "evals")) {
16721660
if (binding_cache->Has(module)) {
16731661
exports = binding_cache->Get(module)->ToObject();

src/node.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
#include <node_object_wrap.h>
1212

13+
#ifndef NODE_STRINGIFY
14+
#define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
15+
#define NODE_STRINGIFY_HELPER(n) #n
16+
#endif
17+
1318
namespace node {
1419

1520
#define NODE_PSYMBOL(s) Persistent<String>::New(String::NewSymbol(s))
@@ -83,14 +88,16 @@ v8::Local<v8::Value> ErrnoException(int errorno,
8388

8489
const char *signo_string(int errorno);
8590

86-
8791
struct node_module_struct {
8892
int version;
8993
void *dso_handle;
90-
const char *name;
94+
const char *filename;
9195
void (*register_func) (v8::Handle<v8::Object> target);
96+
const char *modname;
9297
};
9398

99+
node_module_struct* get_builtin_module(const char *name);
100+
94101
/**
95102
* When this version number is changed, node.js will refuse
96103
* to load older modules. This should be done whenever
@@ -108,9 +115,13 @@ struct node_module_struct {
108115
node::node_module_struct modname ## _module = \
109116
{ \
110117
NODE_STANDARD_MODULE_STUFF, \
111-
regfunc \
118+
regfunc, \
119+
NODE_STRINGIFY(modname) \
112120
};
113121

122+
#define NODE_MODULE_DECL(modname) \
123+
extern node::node_module_struct modname ## _module;
124+
114125

115126
} // namespace node
116127
#endif // SRC_NODE_H_

src/node_crypto.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,7 +2264,7 @@ void InitCrypto(Handle<Object> target) {
22642264
version_symbol = NODE_PSYMBOL("version");
22652265
}
22662266

2267-
2268-
22692267
} // namespace node
22702268

2269+
NODE_MODULE(node_crypto, node::InitCrypto);
2270+

src/node_extensions.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
#include "node.h"
3+
#include "node_version.h"
4+
#include <string.h>
5+
6+
#undef NODE_EXT_LIST_START
7+
#undef NODE_EXT_LIST_ITEM
8+
#undef NODE_EXT_LIST_END
9+
10+
#define NODE_EXT_LIST_START
11+
#define NODE_EXT_LIST_ITEM NODE_MODULE_DECL
12+
#define NODE_EXT_LIST_END
13+
14+
#include "node_extensions.h"
15+
16+
#undef NODE_EXT_LIST_START
17+
#undef NODE_EXT_LIST_ITEM
18+
#undef NODE_EXT_LIST_END
19+
20+
#define NODE_EXT_STRING(x) &x ## _module,
21+
#define NODE_EXT_LIST_START node::node_module_struct *node_module_list[] = {
22+
#define NODE_EXT_LIST_ITEM NODE_EXT_STRING
23+
#define NODE_EXT_LIST_END NULL};
24+
25+
#include "node_extensions.h"
26+
27+
namespace node {
28+
29+
node_module_struct* get_builtin_module(const char *name)
30+
{
31+
char buf[128];
32+
node_module_struct *cur = NULL;
33+
snprintf(buf, sizeof(buf), "node_%s", name);
34+
/* TODO: you could look these up in a hash, but there are only
35+
* a few, and once loaded they are cached. */
36+
for (int i = 0; node_module_list[i] != NULL; i++) {
37+
cur = node_module_list[i];
38+
if (strcmp(cur->modname, buf) == 0) {
39+
return cur;
40+
}
41+
}
42+
43+
return NULL;
44+
}
45+
46+
}; // namespace node

src/node_extensions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
NODE_EXT_LIST_START
3+
NODE_EXT_LIST_ITEM(node_net)
4+
#ifdef HAVE_OPENSSL
5+
NODE_EXT_LIST_ITEM(node_crypto)
6+
#endif
7+
NODE_EXT_LIST_END
8+

src/node_net.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,3 +1266,5 @@ void InitNet(Handle<Object> target) {
12661266
}
12671267

12681268
} // namespace node
1269+
1270+
NODE_MODULE(node_net, node::InitNet);

wscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ def build(bld):
459459
node.source = """
460460
src/node.cc
461461
src/node_buffer.cc
462+
src/node_extensions.cc
462463
src/node_http_parser.cc
463464
src/node_net.cc
464465
src/node_io_watcher.cc

0 commit comments

Comments
 (0)