@@ -198,7 +198,6 @@ ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
198
198
ssize_t DecodeWrite (char *buf, size_t buflen,
199
199
v8::Handle <v8::Value> val,
200
200
enum encoding encoding) {
201
- size_t i;
202
201
HandleScope scope;
203
202
204
203
// XXX
@@ -366,18 +365,17 @@ static void ReportException(TryCatch *try_catch, bool show_line = false) {
366
365
}
367
366
368
367
// 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) {
371
369
HandleScope scope;
372
370
TryCatch try_catch;
373
371
374
- Handle <Script> script = Script::Compile (source, filename);
372
+ Local <Script> script = Script::Compile (source, filename);
375
373
if (script.IsEmpty ()) {
376
374
ReportException (&try_catch);
377
375
exit (1 );
378
376
}
379
377
380
- Handle <Value> result = script->Run ();
378
+ Local <Value> result = script->Run ();
381
379
if (result.IsEmpty ()) {
382
380
ReportException (&try_catch);
383
381
exit (1 );
@@ -877,8 +875,7 @@ static void DebugMessageCallback(EV_P_ ev_async *watcher, int revents) {
877
875
HandleScope scope;
878
876
assert (watcher == &debug_watcher);
879
877
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" ));
882
879
}
883
880
884
881
static void DebugMessageDispatch (void ) {
@@ -893,33 +890,19 @@ static void DebugMessageDispatch(void) {
893
890
894
891
static void ExecuteNativeJS (const char *filename, const char *data) {
895
892
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
- }
906
893
}
907
894
908
- static Local<Object> Load (int argc, char *argv[]) {
895
+ static void Load (int argc, char *argv[]) {
909
896
HandleScope scope;
910
897
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
-
916
898
Local<FunctionTemplate> process_template = FunctionTemplate::New ();
917
899
node::EventEmitter::Initialize (process_template);
918
900
919
901
process = Persistent<Object>::New (process_template->GetFunction ()->NewInstance ());
920
902
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);
923
906
924
907
// process.version
925
908
process->Set (String::NewSymbol (" version" ), String::New (NODE_VERSION));
@@ -1012,11 +995,45 @@ static Local<Object> Load(int argc, char *argv[]) {
1012
995
HTTPServer::Initialize (http); // http.cc
1013
996
HTTPConnection::Initialize (http); // http.cc
1014
997
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
1020
1037
}
1021
1038
1022
1039
static void PrintHelp () {
0 commit comments