Skip to content

Commit 9f5643f

Browse files
committed
Reorganize the start-up process
- assign 'GLOBAL' (and now 'global') inside src/node.js - position for eventually allowing replacements to src/node.js for people like Kris Kowal who want the nice libev and libeio bindings but not necessarily node's choices of modules or promises.
1 parent 7a755e0 commit 9f5643f

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

src/node.cc

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
198198
ssize_t DecodeWrite(char *buf, size_t buflen,
199199
v8::Handle<v8::Value> val,
200200
enum encoding encoding) {
201-
size_t i;
202201
HandleScope scope;
203202

204203
// XXX
@@ -366,18 +365,17 @@ static void ReportException(TryCatch *try_catch, bool show_line = false) {
366365
}
367366

368367
// Executes a str within the current v8 context.
369-
Handle<Value> ExecuteString(v8::Handle<v8::String> source,
370-
v8::Handle<v8::Value> filename) {
368+
Local<Value> ExecuteString(Local<String> source, Local<Value> filename) {
371369
HandleScope scope;
372370
TryCatch try_catch;
373371

374-
Handle<Script> script = Script::Compile(source, filename);
372+
Local<Script> script = Script::Compile(source, filename);
375373
if (script.IsEmpty()) {
376374
ReportException(&try_catch);
377375
exit(1);
378376
}
379377

380-
Handle<Value> result = script->Run();
378+
Local<Value> result = script->Run();
381379
if (result.IsEmpty()) {
382380
ReportException(&try_catch);
383381
exit(1);
@@ -877,8 +875,7 @@ static void DebugMessageCallback(EV_P_ ev_async *watcher, int revents) {
877875
HandleScope scope;
878876
assert(watcher == &debug_watcher);
879877
assert(revents == EV_ASYNC);
880-
ExecuteString(String::New("1+1;"),
881-
String::New("debug_poll"));
878+
ExecuteString(String::New("1+1;"), String::New("debug_poll"));
882879
}
883880

884881
static void DebugMessageDispatch(void) {
@@ -893,33 +890,19 @@ static void DebugMessageDispatch(void) {
893890

894891
static void ExecuteNativeJS(const char *filename, const char *data) {
895892
HandleScope scope;
896-
TryCatch try_catch;
897-
ExecuteString(String::New(data), String::New(filename));
898-
// There should not be any syntax errors in these file!
899-
// If there are exit the process.
900-
if (try_catch.HasCaught()) {
901-
puts("There is an error in Node's built-in javascript");
902-
puts("This should be reported as a bug!");
903-
ReportException(&try_catch);
904-
exit(1);
905-
}
906893
}
907894

908-
static Local<Object> Load(int argc, char *argv[]) {
895+
static void Load(int argc, char *argv[]) {
909896
HandleScope scope;
910897

911-
Local<Object> global = Context::GetCurrent()->Global();
912-
913-
// Assign the global object to it's place as 'GLOBAL'
914-
global->Set(String::NewSymbol("GLOBAL"), global);
915-
916898
Local<FunctionTemplate> process_template = FunctionTemplate::New();
917899
node::EventEmitter::Initialize(process_template);
918900

919901
process = Persistent<Object>::New(process_template->GetFunction()->NewInstance());
920902

921-
// Assign the process object to its place.
922-
global->Set(String::NewSymbol("process"), process);
903+
// Add a reference to the global object
904+
Local<Object> global = Context::GetCurrent()->Global();
905+
process->Set(String::NewSymbol("global"), global);
923906

924907
// process.version
925908
process->Set(String::NewSymbol("version"), String::New(NODE_VERSION));
@@ -1012,11 +995,45 @@ static Local<Object> Load(int argc, char *argv[]) {
1012995
HTTPServer::Initialize(http); // http.cc
1013996
HTTPConnection::Initialize(http); // http.cc
1014997

1015-
// Compile, execute the src/*.js files. (Which were included a static C
1016-
// strings in node_natives.h)
1017-
// In node.js we actually load the file specified in ARGV[1]
1018-
// so your next reading stop should be node.js!
1019-
ExecuteNativeJS("node.js", native_node);
998+
999+
1000+
// Compile, execute the src/node.js file. (Which was included as static C
1001+
// string in node_natives.h. 'natve_node' is the string containing that
1002+
// source code.)
1003+
1004+
// The node.js file returns a function 'f'
1005+
1006+
#ifndef NDEBUG
1007+
TryCatch try_catch;
1008+
#endif
1009+
1010+
Local<Value> f_value = ExecuteString(String::New(native_node),
1011+
String::New("node.js"));
1012+
#ifndef NDEBUG
1013+
if (try_catch.HasCaught()) {
1014+
ReportException(&try_catch);
1015+
exit(10);
1016+
}
1017+
#endif
1018+
assert(f_value->IsFunction());
1019+
Local<Function> f = Local<Function>::Cast(f_value);
1020+
1021+
// Now we call 'f' with the 'process' variable that we've built up with
1022+
// all our bindings. Inside node.js we'll take care of assigning things to
1023+
// their places.
1024+
1025+
// We start the process this way in order to be more modular. Developers
1026+
// who do not like how 'src/node.js' setups the module system but do like
1027+
// Node's I/O bindings may want to replace 'f' with their own function.
1028+
1029+
f->Call(global, 1, &Local<Value>::New(process));
1030+
1031+
#ifndef NDEBUG
1032+
if (try_catch.HasCaught()) {
1033+
ReportException(&try_catch);
1034+
exit(11);
1035+
}
1036+
#endif
10201037
}
10211038

10221039
static void PrintHelp() {

src/node.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
(function () { // anonymous namespace
1+
__wrap__ = function (process) {
2+
3+
process.global.process = process;
4+
process.global.global = process.global;
5+
global.GLOBAL = global;
26

37
/** deprecation errors ************************************************/
48

@@ -986,4 +990,5 @@ process.loop();
986990

987991
process.emit("exit");
988992

989-
}()); // end anonymous namespace
993+
};
994+
__wrap__;

0 commit comments

Comments
 (0)