Skip to content

Commit c4636a5

Browse files
committed
Dynamically load native scripts
1 parent 901d5fd commit c4636a5

File tree

5 files changed

+67
-26
lines changed

5 files changed

+67
-26
lines changed

src/node.cc

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <node_child_process.h>
3737
#include <node_constants.h>
3838
#include <node_stdio.h>
39-
#include <node_natives.h>
39+
#include <node_javascript.h>
4040
#include <node_version.h>
4141
#ifdef HAVE_OPENSSL
4242
#include <node_crypto.h>
@@ -1499,30 +1499,9 @@ static Handle<Value> Binding(const Arguments& args) {
14991499

15001500
} else if (!strcmp(*module_v, "natives")) {
15011501
exports = Object::New();
1502-
// Explicitly define native sources.
1503-
// TODO DRY/automate this?
1504-
exports->Set(String::New("assert"), String::New(native_assert));
1505-
exports->Set(String::New("buffer"), String::New(native_buffer));
1506-
exports->Set(String::New("child_process"),String::New(native_child_process));
1507-
exports->Set(String::New("constants"), String::New(native_constants));
1508-
exports->Set(String::New("dgram"), String::New(native_dgram));
1509-
exports->Set(String::New("dns"), String::New(native_dns));
1510-
exports->Set(String::New("events"), String::New(native_events));
1511-
exports->Set(String::New("freelist"), String::New(native_freelist));
1512-
exports->Set(String::New("fs"), String::New(native_fs));
1513-
exports->Set(String::New("http"), String::New(native_http));
1514-
exports->Set(String::New("crypto"), String::New(native_crypto));
1515-
exports->Set(String::New("net"), String::New(native_net));
1516-
exports->Set(String::New("querystring"), String::New(native_querystring));
1517-
exports->Set(String::New("repl"), String::New(native_repl));
1518-
exports->Set(String::New("readline"), String::New(native_readline));
1519-
exports->Set(String::New("sys"), String::New(native_sys));
1520-
exports->Set(String::New("url"), String::New(native_url));
1521-
exports->Set(String::New("util"), String::New(native_util));
1522-
exports->Set(String::New("path"), String::New(native_path));
1523-
exports->Set(String::New("string_decoder"), String::New(native_string_decoder));
1524-
exports->Set(String::New("stream"), String::New(native_stream));
1502+
DefineJavaScript(exports);
15251503
binding_cache->Set(module, exports);
1504+
15261505
} else {
15271506

15281507
return ThrowException(Exception::Error(String::New("No such module")));
@@ -1663,7 +1642,7 @@ static void Load(int argc, char *argv[]) {
16631642

16641643
TryCatch try_catch;
16651644

1666-
Local<Value> f_value = ExecuteString(String::New(native_node),
1645+
Local<Value> f_value = ExecuteString(String::New(MainSource()),
16671646
String::New("node.js"));
16681647
if (try_catch.HasCaught()) {
16691648
ReportException(try_catch, true);

src/node_javascript.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <v8.h>
2+
#include "node.h"
3+
#include "node_natives.h"
4+
#include <string.h>
5+
#include <strings.h>
6+
7+
using namespace v8;
8+
9+
namespace node {
10+
11+
const char* MainSource() {
12+
return node_native;
13+
}
14+
15+
void DefineJavaScript(v8::Handle<v8::Object> target) {
16+
HandleScope scope;
17+
18+
for (int i = 0; natives[i].name; i++) {
19+
if (natives[i].source != node_native) {
20+
Local<String> name = String::New(natives[i].name);
21+
// TODO: Use ExternalAsciiStringResource for source
22+
// Might need to do some assertions in js2c about chars > 128
23+
Local<String> source = String::New(natives[i].source);
24+
target->Set(name, source);
25+
}
26+
}
27+
}
28+
29+
30+
31+
} // namespace node

src/node_javascript.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <v8.h>
2+
3+
namespace node {
4+
5+
void DefineJavaScript(v8::Handle<v8::Object> target);
6+
const char* MainSource();
7+
8+
} // namespace node

tools/js2c.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,30 @@ def ReadMacros(lines):
211211
212212
%(source_lines)s\
213213
214+
struct _native {
215+
const char* name;
216+
const char* source;
217+
};
218+
219+
static const struct _native natives[] = {
220+
221+
%(native_lines)s\
222+
223+
{ NULL, NULL } /* sentinel */
224+
225+
};
226+
214227
}
215228
#endif
216229
"""
217230

218231

232+
NATIVE_DECLARATION = """\
233+
{ "%(id)s", %(id)s_native },
234+
"""
235+
219236
SOURCE_DECLARATION = """\
220-
static const char native_%(id)s[] = { %(data)s };
237+
const char %(id)s_native[] = { %(data)s };
221238
"""
222239

223240

@@ -252,6 +269,9 @@ def JS2C(source, target):
252269
# Build source code lines
253270
source_lines = [ ]
254271
source_lines_empty = []
272+
273+
native_lines = []
274+
255275
for s in modules:
256276
delay = str(s).endswith('-delay.js')
257277
lines = ReadFile(str(s))
@@ -269,6 +289,7 @@ def JS2C(source, target):
269289
ids.append((id, len(lines)))
270290
source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data })
271291
source_lines_empty.append(SOURCE_DECLARATION % { 'id': id, 'data': 0 })
292+
native_lines.append(NATIVE_DECLARATION % { 'id': id })
272293

273294
# Build delay support functions
274295
get_index_cases = [ ]
@@ -312,6 +333,7 @@ def JS2C(source, target):
312333
'builtin_count': len(ids) + len(delay_ids),
313334
'delay_count': len(delay_ids),
314335
'source_lines': "\n".join(source_lines),
336+
'native_lines': "\n".join(native_lines),
315337
'get_index_cases': "".join(get_index_cases),
316338
'get_script_source_cases': "".join(get_script_source_cases),
317339
'get_script_name_cases': "".join(get_script_name_cases)

wscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ def build(bld):
477477
src/node_main.cc
478478
src/node.cc
479479
src/node_buffer.cc
480+
src/node_javascript.cc
480481
src/node_extensions.cc
481482
src/node_http_parser.cc
482483
src/node_net.cc

0 commit comments

Comments
 (0)