diff --git a/src/node.cc b/src/node.cc index f92270c8391..aea25706377 100644 --- a/src/node.cc +++ b/src/node.cc @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include #ifdef HAVE_OPENSSL #include @@ -1499,30 +1499,9 @@ static Handle Binding(const Arguments& args) { } else if (!strcmp(*module_v, "natives")) { exports = Object::New(); - // Explicitly define native sources. - // TODO DRY/automate this? - exports->Set(String::New("assert"), String::New(native_assert)); - exports->Set(String::New("buffer"), String::New(native_buffer)); - exports->Set(String::New("child_process"),String::New(native_child_process)); - exports->Set(String::New("constants"), String::New(native_constants)); - exports->Set(String::New("dgram"), String::New(native_dgram)); - exports->Set(String::New("dns"), String::New(native_dns)); - exports->Set(String::New("events"), String::New(native_events)); - exports->Set(String::New("freelist"), String::New(native_freelist)); - exports->Set(String::New("fs"), String::New(native_fs)); - exports->Set(String::New("http"), String::New(native_http)); - exports->Set(String::New("crypto"), String::New(native_crypto)); - exports->Set(String::New("net"), String::New(native_net)); - exports->Set(String::New("querystring"), String::New(native_querystring)); - exports->Set(String::New("repl"), String::New(native_repl)); - exports->Set(String::New("readline"), String::New(native_readline)); - exports->Set(String::New("sys"), String::New(native_sys)); - exports->Set(String::New("url"), String::New(native_url)); - exports->Set(String::New("util"), String::New(native_util)); - exports->Set(String::New("path"), String::New(native_path)); - exports->Set(String::New("string_decoder"), String::New(native_string_decoder)); - exports->Set(String::New("stream"), String::New(native_stream)); + DefineJavaScript(exports); binding_cache->Set(module, exports); + } else { return ThrowException(Exception::Error(String::New("No such module"))); @@ -1663,7 +1642,7 @@ static void Load(int argc, char *argv[]) { TryCatch try_catch; - Local f_value = ExecuteString(String::New(native_node), + Local f_value = ExecuteString(String::New(MainSource()), String::New("node.js")); if (try_catch.HasCaught()) { ReportException(try_catch, true); diff --git a/src/node_javascript.cc b/src/node_javascript.cc new file mode 100644 index 00000000000..95005eeb15c --- /dev/null +++ b/src/node_javascript.cc @@ -0,0 +1,31 @@ +#include +#include "node.h" +#include "node_natives.h" +#include +#include + +using namespace v8; + +namespace node { + +const char* MainSource() { + return node_native; +} + +void DefineJavaScript(v8::Handle target) { + HandleScope scope; + + for (int i = 0; natives[i].name; i++) { + if (natives[i].source != node_native) { + Local name = String::New(natives[i].name); + // TODO: Use ExternalAsciiStringResource for source + // Might need to do some assertions in js2c about chars > 128 + Local source = String::New(natives[i].source); + target->Set(name, source); + } + } +} + + + +} // namespace node diff --git a/src/node_javascript.h b/src/node_javascript.h new file mode 100644 index 00000000000..4df3bfa31ee --- /dev/null +++ b/src/node_javascript.h @@ -0,0 +1,8 @@ +#include + +namespace node { + +void DefineJavaScript(v8::Handle target); +const char* MainSource(); + +} // namespace node diff --git a/tools/js2c.py b/tools/js2c.py index 64e5c2b625e..db4885553bb 100755 --- a/tools/js2c.py +++ b/tools/js2c.py @@ -211,13 +211,30 @@ namespace node { %(source_lines)s\ +struct _native { + const char* name; + const char* source; +}; + +static const struct _native natives[] = { + +%(native_lines)s\ + + { NULL, NULL } /* sentinel */ + +}; + } #endif """ +NATIVE_DECLARATION = """\ + { "%(id)s", %(id)s_native }, +""" + SOURCE_DECLARATION = """\ - static const char native_%(id)s[] = { %(data)s }; + const char %(id)s_native[] = { %(data)s }; """ @@ -252,6 +269,9 @@ def JS2C(source, target): # Build source code lines source_lines = [ ] source_lines_empty = [] + + native_lines = [] + for s in modules: delay = str(s).endswith('-delay.js') lines = ReadFile(str(s)) @@ -269,6 +289,7 @@ def JS2C(source, target): ids.append((id, len(lines))) source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data }) source_lines_empty.append(SOURCE_DECLARATION % { 'id': id, 'data': 0 }) + native_lines.append(NATIVE_DECLARATION % { 'id': id }) # Build delay support functions get_index_cases = [ ] @@ -312,6 +333,7 @@ def JS2C(source, target): 'builtin_count': len(ids) + len(delay_ids), 'delay_count': len(delay_ids), 'source_lines': "\n".join(source_lines), + 'native_lines': "\n".join(native_lines), 'get_index_cases': "".join(get_index_cases), 'get_script_source_cases': "".join(get_script_source_cases), 'get_script_name_cases': "".join(get_script_name_cases) diff --git a/wscript b/wscript index 135a9a84673..a07fd2df06e 100644 --- a/wscript +++ b/wscript @@ -477,6 +477,7 @@ def build(bld): src/node_main.cc src/node.cc src/node_buffer.cc + src/node_javascript.cc src/node_extensions.cc src/node_http_parser.cc src/node_net.cc