Browse Source

Dynamically load native scripts

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
c4636a578c
  1. 29
      src/node.cc
  2. 31
      src/node_javascript.cc
  3. 8
      src/node_javascript.h
  4. 24
      tools/js2c.py
  5. 1
      wscript

29
src/node.cc

@ -36,7 +36,7 @@
#include <node_child_process.h>
#include <node_constants.h>
#include <node_stdio.h>
#include <node_natives.h>
#include <node_javascript.h>
#include <node_version.h>
#ifdef HAVE_OPENSSL
#include <node_crypto.h>
@ -1499,30 +1499,9 @@ static Handle<Value> 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<Value> f_value = ExecuteString(String::New(native_node),
Local<Value> f_value = ExecuteString(String::New(MainSource()),
String::New("node.js"));
if (try_catch.HasCaught()) {
ReportException(try_catch, true);

31
src/node_javascript.cc

@ -0,0 +1,31 @@
#include <v8.h>
#include "node.h"
#include "node_natives.h"
#include <string.h>
#include <strings.h>
using namespace v8;
namespace node {
const char* MainSource() {
return node_native;
}
void DefineJavaScript(v8::Handle<v8::Object> target) {
HandleScope scope;
for (int i = 0; natives[i].name; i++) {
if (natives[i].source != node_native) {
Local<String> name = String::New(natives[i].name);
// TODO: Use ExternalAsciiStringResource for source
// Might need to do some assertions in js2c about chars > 128
Local<String> source = String::New(natives[i].source);
target->Set(name, source);
}
}
}
} // namespace node

8
src/node_javascript.h

@ -0,0 +1,8 @@
#include <v8.h>
namespace node {
void DefineJavaScript(v8::Handle<v8::Object> target);
const char* MainSource();
} // namespace node

24
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)

1
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

Loading…
Cancel
Save