// Copyright 2009 Ryan Dahl #include #include #include #include #include #include /* PATH_MAX */ #include #include #include #include /* dlopen(), dlsym() */ #include #include /* setuid, getuid */ #include #include #include #include #include #include #include #if 0 // not in use # include #endif #include #include #include #include #include #include #include #include #include #include #include using namespace v8; extern char **environ; namespace node { static Persistent process; static Persistent dev_symbol; static Persistent ino_symbol; static Persistent mode_symbol; static Persistent nlink_symbol; static Persistent uid_symbol; static Persistent gid_symbol; static Persistent rdev_symbol; static Persistent size_symbol; static Persistent blksize_symbol; static Persistent blocks_symbol; static Persistent atime_symbol; static Persistent mtime_symbol; static Persistent ctime_symbol; static Persistent rss_symbol; static Persistent vsize_symbol; static Persistent heap_total_symbol; static Persistent heap_used_symbol; static Persistent listeners_symbol; static Persistent uncaught_exception_symbol; static Persistent emit_symbol; static int option_end_index = 0; static bool use_debug_agent = false; static bool debug_wait_connect = false; static int debug_port=5858; static ev_prepare next_tick_watcher; static ev_idle tick_spinner; static bool need_tick_cb; static Persistent tick_callback_sym; static ev_async eio_want_poll_notifier; static ev_async eio_done_poll_notifier; static ev_idle eio_poller; // We need to notify V8 when we're idle so that it can run the garbage // collector. The interface to this is V8::IdleNotification(). It returns // true if the heap hasn't be fully compacted, and needs to be run again. // Returning false means that it doesn't have anymore work to do. // // We try to wait for a period of GC_INTERVAL (2 seconds) of idleness, where // idleness means that no libev watchers have been executed. Since // everything in node uses libev watchers, this is a pretty good measure of // idleness. This is done with gc_check, which records the timestamp // last_active on every tick of the event loop, and with gc_timer which // executes every few seconds to measure if // last_active + GC_INTERVAL < ev_now() // If we do find a period of idleness, then we start the gc_idle timer which // will very repaidly call IdleNotification until the heap is fully // compacted. static ev_tstamp last_active; static ev_timer gc_timer; static ev_check gc_check; static ev_idle gc_idle; static bool needs_gc; #define GC_INTERVAL 2.0 static void CheckIdleness(EV_P_ ev_timer *watcher, int revents) { assert(watcher == &gc_timer); assert(revents == EV_TIMER); //fprintf(stderr, "check idle\n"); ev_tstamp idle_time = ev_now(EV_DEFAULT_UC) - last_active; if (idle_time > GC_INTERVAL) { if (needs_gc) { needs_gc = false; if (!V8::IdleNotification()) { ev_idle_start(EV_DEFAULT_UC_ &gc_idle); } } // reset the timer gc_timer.repeat = GC_INTERVAL; ev_timer_again(EV_DEFAULT_UC_ watcher); } } static void NotifyIdleness(EV_P_ ev_idle *watcher, int revents) { assert(watcher == &gc_idle); assert(revents == EV_IDLE); //fprintf(stderr, "notify idle\n"); if (V8::IdleNotification()) { ev_idle_stop(EV_A_ watcher); } needs_gc = false; } static void Activity(EV_P_ ev_check *watcher, int revents) { assert(watcher == &gc_check); assert(revents == EV_CHECK); int pending = ev_pending_count(EV_DEFAULT_UC); // Don't count GC watchers as activity. pending -= ev_is_pending(&gc_timer); pending -= ev_is_pending(&gc_idle); pending -= ev_is_pending(&next_tick_watcher); //if (ev_is_pending(&gc_check)) pending--; // This probably never happens? //fprintf(stderr, "activity, pending: %d\n", pending); if (pending) { last_active = ev_now(EV_DEFAULT_UC); ev_idle_stop(EV_DEFAULT_UC_ &gc_idle); if (!needs_gc) { gc_timer.repeat = GC_INTERVAL; ev_timer_again(EV_DEFAULT_UC_ &gc_timer); } needs_gc = true; } } static Handle NeedTickCallback(const Arguments& args) { HandleScope scope; need_tick_cb = true; ev_idle_start(EV_DEFAULT_UC_ &tick_spinner); return Undefined(); } static void Spin(EV_P_ ev_idle *watcher, int revents) { assert(watcher == &tick_spinner); assert(revents == EV_IDLE); } static void Tick(EV_P_ ev_prepare *watcher, int revents) { assert(watcher == &next_tick_watcher); assert(revents == EV_PREPARE); // Avoid entering a V8 scope. if (!need_tick_cb) return; need_tick_cb = false; ev_idle_stop(EV_DEFAULT_UC_ &tick_spinner); HandleScope scope; if (tick_callback_sym.IsEmpty()) { // Lazily set the symbol tick_callback_sym = Persistent::New(String::NewSymbol("_tickCallback")); } Local cb_v = process->Get(tick_callback_sym); if (!cb_v->IsFunction()) return; Local cb = Local::Cast(cb_v); TryCatch try_catch; cb->Call(process, 0, NULL); if (try_catch.HasCaught()) { FatalException(try_catch); } } static void DoPoll(EV_P_ ev_idle *watcher, int revents) { assert(watcher == &eio_poller); assert(revents == EV_IDLE); //printf("eio_poller\n"); if (eio_poll() != -1) { //printf("eio_poller stop\n"); ev_idle_stop(EV_DEFAULT_UC_ watcher); } } // Called from the main thread. static void WantPollNotifier(EV_P_ ev_async *watcher, int revents) { assert(watcher == &eio_want_poll_notifier); assert(revents == EV_ASYNC); //printf("want poll notifier\n"); if (eio_poll() == -1) { //printf("eio_poller start\n"); ev_idle_start(EV_DEFAULT_UC_ &eio_poller); } } static void DonePollNotifier(EV_P_ ev_async *watcher, int revents) { assert(watcher == &eio_done_poll_notifier); assert(revents == EV_ASYNC); //printf("done poll notifier\n"); if (eio_poll() != -1) { //printf("eio_poller stop\n"); ev_idle_stop(EV_DEFAULT_UC_ &eio_poller); } } // EIOWantPoll() is called from the EIO thread pool each time an EIO // request (that is, one of the node.fs.* functions) has completed. static void EIOWantPoll(void) { // Signal the main thread that eio_poll need to be processed. ev_async_send(EV_DEFAULT_UC_ &eio_want_poll_notifier); } static void EIODonePoll(void) { // Signal the main thread that we should stop calling eio_poll(). // from the idle watcher. ev_async_send(EV_DEFAULT_UC_ &eio_done_poll_notifier); } enum encoding ParseEncoding(Handle encoding_v, enum encoding _default) { HandleScope scope; if (!encoding_v->IsString()) return _default; String::Utf8Value encoding(encoding_v->ToString()); if (strcasecmp(*encoding, "utf8") == 0) { return UTF8; } else if (strcasecmp(*encoding, "utf-8") == 0) { return UTF8; } else if (strcasecmp(*encoding, "ascii") == 0) { return ASCII; } else if (strcasecmp(*encoding, "binary") == 0) { return BINARY; } else if (strcasecmp(*encoding, "raw") == 0) { fprintf(stderr, "'raw' (array of integers) has been removed. " "Use 'binary'.\n"); return BINARY; } else if (strcasecmp(*encoding, "raws") == 0) { fprintf(stderr, "'raws' encoding has been renamed to 'binary'. " "Please update your code.\n"); return BINARY; } else { return _default; } } Local Encode(const void *buf, size_t len, enum encoding encoding) { HandleScope scope; if (!len) return scope.Close(String::Empty()); if (encoding == BINARY) { const unsigned char *cbuf = static_cast(buf); uint16_t * twobytebuf = new uint16_t[len]; for (size_t i = 0; i < len; i++) { // XXX is the following line platform independent? twobytebuf[i] = cbuf[i]; } Local chunk = String::New(twobytebuf, len); delete [] twobytebuf; // TODO use ExternalTwoByteString? return scope.Close(chunk); } // utf8 or ascii encoding Local chunk = String::New((const char*)buf, len); return scope.Close(chunk); } // Returns -1 if the handle was not valid for decoding ssize_t DecodeBytes(v8::Handle val, enum encoding encoding) { HandleScope scope; if (val->IsArray()) { fprintf(stderr, "'raw' encoding (array of integers) has been removed. " "Use 'binary'.\n"); assert(0); return -1; } Local str = val->ToString(); if (encoding == UTF8) return str->Utf8Length(); return str->Length(); } #ifndef MIN # define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif // Returns number of bytes written. ssize_t DecodeWrite(char *buf, size_t buflen, v8::Handle val, enum encoding encoding) { HandleScope scope; // XXX // A lot of improvement can be made here. See: // http://code.google.com/p/v8/issues/detail?id=270 // http://groups.google.com/group/v8-dev/browse_thread/thread/dba28a81d9215291/ece2b50a3b4022c // http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611 if (val->IsArray()) { fprintf(stderr, "'raw' encoding (array of integers) has been removed. " "Use 'binary'.\n"); assert(0); return -1; } Local str = val->ToString(); if (encoding == UTF8) { str->WriteUtf8(buf, buflen, NULL, String::HINT_MANY_WRITES_EXPECTED); return buflen; } if (encoding == ASCII) { str->WriteAscii(buf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED); return buflen; } // THIS IS AWFUL!!! FIXME assert(encoding == BINARY); uint16_t * twobytebuf = new uint16_t[buflen]; str->Write(twobytebuf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED); for (size_t i = 0; i < buflen; i++) { unsigned char *b = reinterpret_cast(&twobytebuf[i]); assert(b[1] == 0); buf[i] = b[0]; } delete [] twobytebuf; return buflen; } static Persistent stats_constructor_template; Local BuildStatsObject(struct stat * s) { HandleScope scope; if (dev_symbol.IsEmpty()) { dev_symbol = NODE_PSYMBOL("dev"); ino_symbol = NODE_PSYMBOL("ino"); mode_symbol = NODE_PSYMBOL("mode"); nlink_symbol = NODE_PSYMBOL("nlink"); uid_symbol = NODE_PSYMBOL("uid"); gid_symbol = NODE_PSYMBOL("gid"); rdev_symbol = NODE_PSYMBOL("rdev"); size_symbol = NODE_PSYMBOL("size"); blksize_symbol = NODE_PSYMBOL("blksize"); blocks_symbol = NODE_PSYMBOL("blocks"); atime_symbol = NODE_PSYMBOL("atime"); mtime_symbol = NODE_PSYMBOL("mtime"); ctime_symbol = NODE_PSYMBOL("ctime"); } Local stats = stats_constructor_template->GetFunction()->NewInstance(); /* ID of device containing file */ stats->Set(dev_symbol, Integer::New(s->st_dev)); /* inode number */ stats->Set(ino_symbol, Integer::New(s->st_ino)); /* protection */ stats->Set(mode_symbol, Integer::New(s->st_mode)); /* number of hard links */ stats->Set(nlink_symbol, Integer::New(s->st_nlink)); /* user ID of owner */ stats->Set(uid_symbol, Integer::New(s->st_uid)); /* group ID of owner */ stats->Set(gid_symbol, Integer::New(s->st_gid)); /* device ID (if special file) */ stats->Set(rdev_symbol, Integer::New(s->st_rdev)); /* total size, in bytes */ stats->Set(size_symbol, Integer::New(s->st_size)); /* blocksize for filesystem I/O */ stats->Set(blksize_symbol, Integer::New(s->st_blksize)); /* number of blocks allocated */ stats->Set(blocks_symbol, Integer::New(s->st_blocks)); /* time of last access */ stats->Set(atime_symbol, NODE_UNIXTIME_V8(s->st_atime)); /* time of last modification */ stats->Set(mtime_symbol, NODE_UNIXTIME_V8(s->st_mtime)); /* time of last status change */ stats->Set(ctime_symbol, NODE_UNIXTIME_V8(s->st_ctime)); return scope.Close(stats); } // Extracts a C str from a V8 Utf8Value. const char* ToCString(const v8::String::Utf8Value& value) { return *value ? *value : ""; } static void ReportException(TryCatch &try_catch, bool show_line = false) { Handle message = try_catch.Message(); Handle error = try_catch.Exception(); Handle stack; if (error->IsObject()) { Handle obj = Handle::Cast(error); Handle raw_stack = obj->Get(String::New("stack")); if (raw_stack->IsString()) stack = Handle::Cast(raw_stack); } if (show_line && !message.IsEmpty()) { // Print (filename):(line number): (message). String::Utf8Value filename(message->GetScriptResourceName()); const char* filename_string = ToCString(filename); int linenum = message->GetLineNumber(); fprintf(stderr, "%s:%i\n", filename_string, linenum); // Print line of source code. String::Utf8Value sourceline(message->GetSourceLine()); const char* sourceline_string = ToCString(sourceline); fprintf(stderr, "%s\n", sourceline_string); // Print wavy underline (GetUnderline is deprecated). int start = message->GetStartColumn(); for (int i = 0; i < start; i++) { fprintf(stderr, " "); } int end = message->GetEndColumn(); for (int i = start; i < end; i++) { fprintf(stderr, "^"); } fprintf(stderr, "\n"); } if (stack.IsEmpty()) { message->PrintCurrentStackTrace(stderr); } else { String::Utf8Value trace(stack); fprintf(stderr, "%s\n", *trace); } fflush(stderr); } // Executes a str within the current v8 context. Local ExecuteString(Local source, Local filename) { HandleScope scope; TryCatch try_catch; Local