#include "node.h" #include "node_tcp.h" #include "node_http.h" #include "node_timer.h" #include #include #include #include #include using namespace v8; using namespace std; static int exit_code = 0; void node_fatal_exception (TryCatch &try_catch) { HandleScope handle_scope; Local message = try_catch.Message(); String::Utf8Value error(try_catch.Exception()); fprintf( stderr , "Uncaught Exception. line %d '%s'\n\n" , try_catch.Message()->GetLineNumber() , *error ); ev_unloop(node_loop(), EVUNLOOP_ALL); exit_code = 1; } // Reads a file into a v8 string. static Handle ReadFile (const string& name) { FILE* file = fopen(name.c_str(), "rb"); if (file == NULL) return Handle(); fseek(file, 0, SEEK_END); int size = ftell(file); rewind(file); char* chars = new char[size + 1]; chars[size] = '\0'; for (int i = 0; i < size;) { int read = fread(&chars[i], 1, size - i, file); i += read; } fclose(file); Handle result = String::New(chars, size); delete[] chars; return result; } static void ParseOptions (int argc, char* argv[], map& options, string* file) { for (int i = 1; i < argc; i++) { string arg = argv[i]; int index = arg.find('=', 0); if (index == string::npos) { *file = arg; } else { string key = arg.substr(0, index); string value = arg.substr(index+1); options[key] = value; } } } static bool compile (Handle script) { HandleScope handle_scope; // We're just about to compile the script; set up an error handler to // catch any exceptions the script might throw. TryCatch try_catch; // Compile the script and check for errors. Handle