1 // Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
9 #include <limits.h> /* PATH_MAX */
13 #include <dlfcn.h> /* dlopen(), dlsym() */
14 #include <sys/types.h>
15 #include <unistd.h> /* setuid, getuid */
17 #include <node_buffer.h>
18 #include <node_io_watcher.h>
19 #include <node_net2.h>
20 #include <node_events.h>
21 #include <node_cares.h>
22 #include <node_file.h>
25 # include <node_idle_watcher.h>
27 #include <node_http_parser.h>
28 #include <node_signal_watcher.h>
29 #include <node_stat_watcher.h>
30 #include <node_timer.h>
31 #include <node_child_process.h>
32 #include <node_constants.h>
33 #include <node_stdio.h>
34 #include <node_natives.h>
35 #include <node_version.h>
37 #include <node_crypto.h>
39 #include <node_script.h>
45 extern char **environ;
49 static Persistent<Object> process;
51 static Persistent<String> errno_symbol;
52 static Persistent<String> syscall_symbol;
54 static Persistent<String> dev_symbol;
55 static Persistent<String> ino_symbol;
56 static Persistent<String> mode_symbol;
57 static Persistent<String> nlink_symbol;
58 static Persistent<String> uid_symbol;
59 static Persistent<String> gid_symbol;
60 static Persistent<String> rdev_symbol;
61 static Persistent<String> size_symbol;
62 static Persistent<String> blksize_symbol;
63 static Persistent<String> blocks_symbol;
64 static Persistent<String> atime_symbol;
65 static Persistent<String> mtime_symbol;
66 static Persistent<String> ctime_symbol;
68 static Persistent<String> rss_symbol;
69 static Persistent<String> vsize_symbol;
70 static Persistent<String> heap_total_symbol;
71 static Persistent<String> heap_used_symbol;
73 static Persistent<String> listeners_symbol;
74 static Persistent<String> uncaught_exception_symbol;
75 static Persistent<String> emit_symbol;
77 static int option_end_index = 0;
78 static bool use_debug_agent = false;
79 static bool debug_wait_connect = false;
80 static int debug_port=5858;
82 static ev_prepare next_tick_watcher;
83 static ev_idle tick_spinner;
84 static bool need_tick_cb;
85 static Persistent<String> tick_callback_sym;
87 static ev_async eio_want_poll_notifier;
88 static ev_async eio_done_poll_notifier;
89 static ev_idle eio_poller;
91 // We need to notify V8 when we're idle so that it can run the garbage
92 // collector. The interface to this is V8::IdleNotification(). It returns
93 // true if the heap hasn't be fully compacted, and needs to be run again.
94 // Returning false means that it doesn't have anymore work to do.
96 // A rather convoluted algorithm has been devised to determine when Node is
97 // idle. You'll have to figure it out for yourself.
98 static ev_check gc_check;
99 static ev_idle gc_idle;
100 static ev_timer gc_timer;
104 #define FAST_TICK 0.7
105 #define GC_WAIT_TIME 5.
106 #define RPM_SAMPLES 100
107 #define TICK_TIME(n) tick_times[(tick_time_head - (n)) % RPM_SAMPLES]
108 static ev_tstamp tick_times[RPM_SAMPLES];
109 static int tick_time_head;
111 static void StartGCTimer () {
112 if (!ev_is_active(&gc_timer)) {
113 ev_timer_start(EV_DEFAULT_UC_ &gc_timer);
114 ev_unref(EV_DEFAULT_UC);
118 static void StopGCTimer () {
119 if (ev_is_active(&gc_timer)) {
120 ev_ref(EV_DEFAULT_UC);
121 ev_timer_stop(&gc_timer);
125 static void Idle(EV_P_ ev_idle *watcher, int revents) {
126 assert(watcher == &gc_idle);
127 assert(revents == EV_IDLE);
129 //fprintf(stderr, "idle\n");
131 if (V8::IdleNotification()) {
132 ev_idle_stop(EV_A_ watcher);
138 // Called directly after every call to select() (or epoll, or whatever)
139 static void Check(EV_P_ ev_check *watcher, int revents) {
140 assert(watcher == &gc_check);
141 assert(revents == EV_CHECK);
143 tick_times[tick_time_head] = ev_now();
144 tick_time_head = (tick_time_head + 1) % RPM_SAMPLES;
148 for (int i = 0; i < (int)(GC_WAIT_TIME/FAST_TICK); i++) {
149 double d = TICK_TIME(i+1) - TICK_TIME(i+2);
150 //printf("d = %f\n", d);
151 // If in the last 5 ticks the difference between
152 // ticks was less than 0.7 seconds, then continue.
159 // Otherwise start the gc!
161 //fprintf(stderr, "start idle 2\n");
162 ev_idle_start(EV_A_ &gc_idle);
166 static Handle<Value> NeedTickCallback(const Arguments& args) {
169 ev_idle_start(EV_DEFAULT_UC_ &tick_spinner);
174 static void Spin(EV_P_ ev_idle *watcher, int revents) {
175 assert(watcher == &tick_spinner);
176 assert(revents == EV_IDLE);
180 static void Tick(EV_P_ ev_prepare *watcher, int revents) {
181 assert(watcher == &next_tick_watcher);
182 assert(revents == EV_PREPARE);
184 // Avoid entering a V8 scope.
185 if (!need_tick_cb) return;
187 need_tick_cb = false;
188 ev_idle_stop(EV_DEFAULT_UC_ &tick_spinner);
192 if (tick_callback_sym.IsEmpty()) {
193 // Lazily set the symbol
195 Persistent<String>::New(String::NewSymbol("_tickCallback"));
198 Local<Value> cb_v = process->Get(tick_callback_sym);
199 if (!cb_v->IsFunction()) return;
200 Local<Function> cb = Local<Function>::Cast(cb_v);
204 cb->Call(process, 0, NULL);
206 if (try_catch.HasCaught()) {
207 FatalException(try_catch);
212 static void DoPoll(EV_P_ ev_idle *watcher, int revents) {
213 assert(watcher == &eio_poller);
214 assert(revents == EV_IDLE);
216 //printf("eio_poller\n");
218 if (eio_poll() != -1) {
219 //printf("eio_poller stop\n");
220 ev_idle_stop(EV_DEFAULT_UC_ watcher);
225 // Called from the main thread.
226 static void WantPollNotifier(EV_P_ ev_async *watcher, int revents) {
227 assert(watcher == &eio_want_poll_notifier);
228 assert(revents == EV_ASYNC);
230 //printf("want poll notifier\n");
232 if (eio_poll() == -1) {
233 //printf("eio_poller start\n");
234 ev_idle_start(EV_DEFAULT_UC_ &eio_poller);
239 static void DonePollNotifier(EV_P_ ev_async *watcher, int revents) {
240 assert(watcher == &eio_done_poll_notifier);
241 assert(revents == EV_ASYNC);
243 //printf("done poll notifier\n");
245 if (eio_poll() != -1) {
246 //printf("eio_poller stop\n");
247 ev_idle_stop(EV_DEFAULT_UC_ &eio_poller);
252 // EIOWantPoll() is called from the EIO thread pool each time an EIO
253 // request (that is, one of the node.fs.* functions) has completed.
254 static void EIOWantPoll(void) {
255 // Signal the main thread that eio_poll need to be processed.
256 ev_async_send(EV_DEFAULT_UC_ &eio_want_poll_notifier);
260 static void EIODonePoll(void) {
261 // Signal the main thread that we should stop calling eio_poll().
262 // from the idle watcher.
263 ev_async_send(EV_DEFAULT_UC_ &eio_done_poll_notifier);
267 static inline const char *errno_string(int errorno) {
268 #define ERRNO_CASE(e) case e: return #e;
276 ERRNO_CASE(EADDRINUSE);
280 ERRNO_CASE(EADDRNOTAVAIL);
284 ERRNO_CASE(EAFNOSUPPORT);
292 # if EAGAIN != EWOULDBLOCK
293 ERRNO_CASE(EWOULDBLOCK);
298 ERRNO_CASE(EALREADY);
314 ERRNO_CASE(ECANCELED);
322 ERRNO_CASE(ECONNABORTED);
326 ERRNO_CASE(ECONNREFUSED);
330 ERRNO_CASE(ECONNRESET);
338 ERRNO_CASE(EDESTADDRREQ);
362 ERRNO_CASE(EHOSTUNREACH);
374 ERRNO_CASE(EINPROGRESS);
410 ERRNO_CASE(EMSGSIZE);
414 ERRNO_CASE(EMULTIHOP);
418 ERRNO_CASE(ENAMETOOLONG);
422 ERRNO_CASE(ENETDOWN);
426 ERRNO_CASE(ENETRESET);
430 ERRNO_CASE(ENETUNREACH);
462 # if ENOLINK != ENOLCK
476 ERRNO_CASE(ENOPROTOOPT);
496 ERRNO_CASE(ENOTCONN);
504 ERRNO_CASE(ENOTEMPTY);
508 ERRNO_CASE(ENOTSOCK);
515 ERRNO_CASE(EOPNOTSUPP);
529 ERRNO_CASE(EOVERFLOW);
544 #ifdef EPROTONOSUPPORT
545 ERRNO_CASE(EPROTONOSUPPORT);
549 ERRNO_CASE(EPROTOTYPE);
577 ERRNO_CASE(ETIMEDOUT);
592 const char *signo_string(int signo) {
593 #define SIGNO_CASE(e) case e: return #e;
621 # if SIGABRT != SIGIOT
662 SIGNO_CASE(SIGSTKFLT);
699 SIGNO_CASE(SIGVTALRM);
707 SIGNO_CASE(SIGWINCH);
715 # if SIGPOLL != SIGIO
737 Local<Value> ErrnoException(int errorno,
740 Local<String> estring = String::NewSymbol(errno_string(errorno));
741 if (!msg[0]) msg = strerror(errorno);
742 Local<String> message = String::NewSymbol(msg);
744 Local<String> cons1 = String::Concat(estring, String::NewSymbol(", "));
745 Local<String> cons2 = String::Concat(cons1, message);
747 Local<Value> e = Exception::Error(cons2);
749 Local<Object> obj = e->ToObject();
751 if (errno_symbol.IsEmpty()) {
752 syscall_symbol = NODE_PSYMBOL("syscall");
753 errno_symbol = NODE_PSYMBOL("errno");
756 obj->Set(errno_symbol, Integer::New(errorno));
757 if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall));
762 enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
765 if (!encoding_v->IsString()) return _default;
767 String::Utf8Value encoding(encoding_v->ToString());
769 if (strcasecmp(*encoding, "utf8") == 0) {
771 } else if (strcasecmp(*encoding, "utf-8") == 0) {
773 } else if (strcasecmp(*encoding, "ascii") == 0) {
775 } else if (strcasecmp(*encoding, "binary") == 0) {
777 } else if (strcasecmp(*encoding, "raw") == 0) {
778 fprintf(stderr, "'raw' (array of integers) has been removed. "
781 } else if (strcasecmp(*encoding, "raws") == 0) {
782 fprintf(stderr, "'raws' encoding has been renamed to 'binary'. "
783 "Please update your code.\n");
790 Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
793 if (!len) return scope.Close(String::Empty());
795 if (encoding == BINARY) {
796 const unsigned char *cbuf = static_cast<const unsigned char*>(buf);
797 uint16_t * twobytebuf = new uint16_t[len];
798 for (size_t i = 0; i < len; i++) {
799 // XXX is the following line platform independent?
800 twobytebuf[i] = cbuf[i];
802 Local<String> chunk = String::New(twobytebuf, len);
803 delete [] twobytebuf; // TODO use ExternalTwoByteString?
804 return scope.Close(chunk);
807 // utf8 or ascii encoding
808 Local<String> chunk = String::New((const char*)buf, len);
809 return scope.Close(chunk);
812 // Returns -1 if the handle was not valid for decoding
813 ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
816 if (val->IsArray()) {
817 fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
823 Local<String> str = val->ToString();
825 if (encoding == UTF8) return str->Utf8Length();
827 return str->Length();
831 # define MIN(a, b) ((a) < (b) ? (a) : (b))
834 // Returns number of bytes written.
835 ssize_t DecodeWrite(char *buf,
837 v8::Handle<v8::Value> val,
838 enum encoding encoding) {
842 // A lot of improvement can be made here. See:
843 // http://code.google.com/p/v8/issues/detail?id=270
844 // http://groups.google.com/group/v8-dev/browse_thread/thread/dba28a81d9215291/ece2b50a3b4022c
845 // http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611
847 if (val->IsArray()) {
848 fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
854 Local<String> str = val->ToString();
856 if (encoding == UTF8) {
857 str->WriteUtf8(buf, buflen, NULL, String::HINT_MANY_WRITES_EXPECTED);
861 if (encoding == ASCII) {
862 str->WriteAscii(buf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED);
866 // THIS IS AWFUL!!! FIXME
868 assert(encoding == BINARY);
870 uint16_t * twobytebuf = new uint16_t[buflen];
872 str->Write(twobytebuf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED);
874 for (size_t i = 0; i < buflen; i++) {
875 unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]);
880 delete [] twobytebuf;
885 static Persistent<FunctionTemplate> stats_constructor_template;
887 Local<Object> BuildStatsObject(struct stat * s) {
890 if (dev_symbol.IsEmpty()) {
891 dev_symbol = NODE_PSYMBOL("dev");
892 ino_symbol = NODE_PSYMBOL("ino");
893 mode_symbol = NODE_PSYMBOL("mode");
894 nlink_symbol = NODE_PSYMBOL("nlink");
895 uid_symbol = NODE_PSYMBOL("uid");
896 gid_symbol = NODE_PSYMBOL("gid");
897 rdev_symbol = NODE_PSYMBOL("rdev");
898 size_symbol = NODE_PSYMBOL("size");
899 blksize_symbol = NODE_PSYMBOL("blksize");
900 blocks_symbol = NODE_PSYMBOL("blocks");
901 atime_symbol = NODE_PSYMBOL("atime");
902 mtime_symbol = NODE_PSYMBOL("mtime");
903 ctime_symbol = NODE_PSYMBOL("ctime");
906 Local<Object> stats =
907 stats_constructor_template->GetFunction()->NewInstance();
909 /* ID of device containing file */
910 stats->Set(dev_symbol, Integer::New(s->st_dev));
913 stats->Set(ino_symbol, Integer::New(s->st_ino));
916 stats->Set(mode_symbol, Integer::New(s->st_mode));
918 /* number of hard links */
919 stats->Set(nlink_symbol, Integer::New(s->st_nlink));
921 /* user ID of owner */
922 stats->Set(uid_symbol, Integer::New(s->st_uid));
924 /* group ID of owner */
925 stats->Set(gid_symbol, Integer::New(s->st_gid));
927 /* device ID (if special file) */
928 stats->Set(rdev_symbol, Integer::New(s->st_rdev));
930 /* total size, in bytes */
931 stats->Set(size_symbol, Integer::New(s->st_size));
933 /* blocksize for filesystem I/O */
934 stats->Set(blksize_symbol, Integer::New(s->st_blksize));
936 /* number of blocks allocated */
937 stats->Set(blocks_symbol, Integer::New(s->st_blocks));
939 /* time of last access */
940 stats->Set(atime_symbol, NODE_UNIXTIME_V8(s->st_atime));
942 /* time of last modification */
943 stats->Set(mtime_symbol, NODE_UNIXTIME_V8(s->st_mtime));
945 /* time of last status change */
946 stats->Set(ctime_symbol, NODE_UNIXTIME_V8(s->st_ctime));
948 return scope.Close(stats);
952 // Extracts a C str from a V8 Utf8Value.
953 const char* ToCString(const v8::String::Utf8Value& value) {
954 return *value ? *value : "<str conversion failed>";
957 static void ReportException(TryCatch &try_catch, bool show_line = false) {
958 Handle<Message> message = try_catch.Message();
960 Handle<Value> error = try_catch.Exception();
961 Handle<String> stack;
963 if (error->IsObject()) {
964 Handle<Object> obj = Handle<Object>::Cast(error);
965 Handle<Value> raw_stack = obj->Get(String::New("stack"));
966 if (raw_stack->IsString()) stack = Handle<String>::Cast(raw_stack);
969 if (show_line && !message.IsEmpty()) {
970 // Print (filename):(line number): (message).
971 String::Utf8Value filename(message->GetScriptResourceName());
972 const char* filename_string = ToCString(filename);
973 int linenum = message->GetLineNumber();
974 fprintf(stderr, "%s:%i\n", filename_string, linenum);
975 // Print line of source code.
976 String::Utf8Value sourceline(message->GetSourceLine());
977 const char* sourceline_string = ToCString(sourceline);
978 fprintf(stderr, "%s\n", sourceline_string);
979 // Print wavy underline (GetUnderline is deprecated).
980 int start = message->GetStartColumn();
981 for (int i = 0; i < start; i++) {
982 fprintf(stderr, " ");
984 int end = message->GetEndColumn();
985 for (int i = start; i < end; i++) {
986 fprintf(stderr, "^");
988 fprintf(stderr, "\n");
991 if (stack.IsEmpty()) {
992 message->PrintCurrentStackTrace(stderr);
994 String::Utf8Value trace(stack);
995 fprintf(stderr, "%s\n", *trace);
1000 // Executes a str within the current v8 context.
1001 Local<Value> ExecuteString(Local<String> source, Local<Value> filename) {
1005 Local<v8::Script> script = v8::Script::Compile(source, filename);
1006 if (script.IsEmpty()) {
1007 ReportException(try_catch);
1011 Local<Value> result = script->Run();
1012 if (result.IsEmpty()) {
1013 ReportException(try_catch);
1017 return scope.Close(result);
1020 static Handle<Value> ByteLength(const Arguments& args) {
1023 if (args.Length() < 1 || !args[0]->IsString()) {
1024 return ThrowException(Exception::Error(String::New("Bad argument.")));
1027 Local<Integer> length = Integer::New(DecodeBytes(args[0], ParseEncoding(args[1], UTF8)));
1029 return scope.Close(length);
1032 static Handle<Value> Loop(const Arguments& args) {
1034 assert(args.Length() == 0);
1036 // TODO Probably don't need to start this each time.
1037 // Avoids failing on test/mjsunit/test-eio-race3.js though
1038 ev_idle_start(EV_DEFAULT_UC_ &eio_poller);
1040 ev_loop(EV_DEFAULT_UC_ 0);
1044 static Handle<Value> Unloop(const Arguments& args) {
1045 fprintf(stderr, "Deprecation: Don't use process.unloop(). It will be removed soon.\n");
1047 int how = EVUNLOOP_ONE;
1048 if (args[0]->IsString()) {
1049 String::Utf8Value how_s(args[0]->ToString());
1050 if (0 == strcmp(*how_s, "all")) {
1054 ev_unloop(EV_DEFAULT_ how);
1058 static Handle<Value> Chdir(const Arguments& args) {
1061 if (args.Length() != 1 || !args[0]->IsString()) {
1062 return ThrowException(Exception::Error(String::New("Bad argument.")));
1065 String::Utf8Value path(args[0]->ToString());
1067 int r = chdir(*path);
1070 return ThrowException(Exception::Error(String::New(strerror(errno))));
1076 static Handle<Value> Cwd(const Arguments& args) {
1078 assert(args.Length() == 0);
1080 char output[PATH_MAX];
1081 char *r = getcwd(output, PATH_MAX);
1083 return ThrowException(Exception::Error(String::New(strerror(errno))));
1085 Local<String> cwd = String::New(output);
1087 return scope.Close(cwd);
1090 static Handle<Value> Umask(const Arguments& args){
1093 if(args.Length() < 1) {
1097 else if(!args[0]->IsInt32()) {
1098 return ThrowException(Exception::TypeError(
1099 String::New("argument must be an integer.")));
1102 old = umask((mode_t)args[0]->Uint32Value());
1104 return scope.Close(Uint32::New(old));
1108 static Handle<Value> GetUid(const Arguments& args) {
1110 assert(args.Length() == 0);
1112 return scope.Close(Integer::New(uid));
1115 static Handle<Value> GetGid(const Arguments& args) {
1117 assert(args.Length() == 0);
1119 return scope.Close(Integer::New(gid));
1123 static Handle<Value> SetGid(const Arguments& args) {
1126 if (args.Length() < 1) {
1127 return ThrowException(Exception::Error(
1128 String::New("setgid requires 1 argument")));
1131 Local<Integer> given_gid = args[0]->ToInteger();
1132 int gid = given_gid->Int32Value();
1134 if ((result = setgid(gid)) != 0) {
1135 return ThrowException(Exception::Error(String::New(strerror(errno))));
1140 static Handle<Value> SetUid(const Arguments& args) {
1143 if (args.Length() < 1) {
1144 return ThrowException(Exception::Error(
1145 String::New("setuid requires 1 argument")));
1148 Local<Integer> given_uid = args[0]->ToInteger();
1149 int uid = given_uid->Int32Value();
1151 if ((result = setuid(uid)) != 0) {
1152 return ThrowException(Exception::Error(String::New(strerror(errno))));
1158 v8::Handle<v8::Value> Exit(const v8::Arguments& args) {
1162 exit(args[0]->IntegerValue());
1167 #define HAVE_GETMEM 1
1168 #include <unistd.h> /* getpagesize() */
1170 #if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
1171 #define PROCFS_FILE_OFFSET_BITS_HACK 1
1172 #undef _FILE_OFFSET_BITS
1174 #define PROCFS_FILE_OFFSET_BITS_HACK 0
1179 #if (PROCFS_FILE_OFFSET_BITS_HACK - 0 == 1)
1180 #define _FILE_OFFSET_BITS 64
1183 int getmem(size_t *rss, size_t *vsize) {
1184 pid_t pid = getpid();
1186 size_t page_size = getpagesize();
1188 sprintf(pidpath, "/proc/%d/psinfo", pid);
1191 FILE *f = fopen(pidpath, "r");
1194 if (fread(&psinfo, sizeof(psinfo_t), 1, f) != 1) {
1201 *vsize = (size_t) psinfo.pr_size * page_size;
1202 *rss = (size_t) psinfo.pr_rssize * 1024;
1212 #define HAVE_GETMEM 1
1214 #include <sys/param.h>
1215 #include <sys/sysctl.h>
1216 #include <sys/user.h>
1221 int getmem(size_t *rss, size_t *vsize) {
1223 struct kinfo_proc *kinfo = NULL;
1226 size_t page_size = getpagesize();
1230 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
1231 if (kd == NULL) goto error;
1233 kinfo = kvm_getprocs(kd, KERN_PROC_PID, pid, &nprocs);
1234 if (kinfo == NULL) goto error;
1236 *rss = kinfo->ki_rssize * page_size;
1237 *vsize = kinfo->ki_size;
1244 if (kd) kvm_close(kd);
1247 #endif // __FreeBSD__
1251 #define HAVE_GETMEM 1
1252 /* Researched by Tim Becker and Michael Knight
1253 * http://blog.kuriositaet.de/?p=257
1256 #include <mach/task.h>
1257 #include <mach/mach_init.h>
1259 int getmem(size_t *rss, size_t *vsize) {
1260 struct task_basic_info t_info;
1261 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
1263 int r = task_info(mach_task_self(),
1265 (task_info_t)&t_info,
1268 if (r != KERN_SUCCESS) return -1;
1270 *rss = t_info.resident_size;
1271 *vsize = t_info.virtual_size;
1278 # define HAVE_GETMEM 1
1279 # include <sys/param.h> /* for MAXPATHLEN */
1281 int getmem(size_t *rss, size_t *vsize) {
1282 FILE *f = fopen("/proc/self/stat", "r");
1287 char buffer[MAXPATHLEN];
1288 size_t page_size = getpagesize();
1291 if (fscanf(f, "%d ", &itmp) == 0) goto error;
1293 if (fscanf (f, "%s ", &buffer[0]) == 0) goto error;
1295 if (fscanf (f, "%c ", &ctmp) == 0) goto error;
1296 /* Parent process */
1297 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1299 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1301 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1303 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1304 /* TTY owner process group */
1305 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1307 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1308 /* Minor faults (no memory page) */
1309 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1310 /* Minor faults, children */
1311 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1312 /* Major faults (memory page faults) */
1313 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1314 /* Major faults, children */
1315 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1317 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1319 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1320 /* utime, children */
1321 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1322 /* stime, children */
1323 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1324 /* jiffies remaining in current time slice */
1325 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1327 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1328 /* jiffies until next timeout */
1329 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1330 /* jiffies until next SIGALRM */
1331 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1332 /* start time (jiffies since system boot) */
1333 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1335 /* Virtual memory size */
1336 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1337 *vsize = (size_t) itmp;
1339 /* Resident set size */
1340 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1341 *rss = (size_t) itmp * page_size;
1344 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1346 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1348 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1349 /* Start of stack */
1350 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1363 static void CheckStatus(EV_P_ ev_timer *watcher, int revents) {
1364 assert(watcher == &gc_timer);
1365 assert(revents == EV_TIMER);
1370 if (!ev_is_active(&gc_idle) && getmem(&rss, &vsize) == 0) {
1371 if (rss > 1024*1024*128) {
1372 // larger than 128 megs, just start the idle watcher
1373 ev_idle_start(EV_A_ &gc_idle);
1377 #endif // HAVE_GETMEM
1379 double d = ev_now() - TICK_TIME(3);
1381 //printfb("timer d = %f\n", d);
1383 if (d >= GC_WAIT_TIME - 1.) {
1384 //fprintf(stderr, "start idle\n");
1385 ev_idle_start(EV_A_ &gc_idle);
1390 v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) {
1392 assert(args.Length() == 0);
1395 return ThrowException(Exception::Error(String::New("Not support on your platform. (Talk to Ryan.)")));
1399 int r = getmem(&rss, &vsize);
1402 return ThrowException(Exception::Error(String::New(strerror(errno))));
1405 Local<Object> info = Object::New();
1407 if (rss_symbol.IsEmpty()) {
1408 rss_symbol = NODE_PSYMBOL("rss");
1409 vsize_symbol = NODE_PSYMBOL("vsize");
1410 heap_total_symbol = NODE_PSYMBOL("heapTotal");
1411 heap_used_symbol = NODE_PSYMBOL("heapUsed");
1414 info->Set(rss_symbol, Integer::NewFromUnsigned(rss));
1415 info->Set(vsize_symbol, Integer::NewFromUnsigned(vsize));
1418 HeapStatistics v8_heap_stats;
1419 V8::GetHeapStatistics(&v8_heap_stats);
1420 info->Set(heap_total_symbol,
1421 Integer::NewFromUnsigned(v8_heap_stats.total_heap_size()));
1422 info->Set(heap_used_symbol,
1423 Integer::NewFromUnsigned(v8_heap_stats.used_heap_size()));
1425 return scope.Close(info);
1430 v8::Handle<v8::Value> Kill(const v8::Arguments& args) {
1433 if (args.Length() < 1 || !args[0]->IsNumber()) {
1434 return ThrowException(Exception::Error(String::New("Bad argument.")));
1437 pid_t pid = args[0]->IntegerValue();
1441 if (args.Length() >= 2) {
1442 if (args[1]->IsNumber()) {
1443 sig = args[1]->Int32Value();
1444 } else if (args[1]->IsString()) {
1445 Local<String> signame = args[1]->ToString();
1447 Local<Value> sig_v = process->Get(signame);
1448 if (!sig_v->IsNumber()) {
1449 return ThrowException(Exception::Error(String::New("Unknown signal")));
1451 sig = sig_v->Int32Value();
1455 int r = kill(pid, sig);
1458 return ThrowException(Exception::Error(String::New(strerror(errno))));
1464 typedef void (*extInit)(Handle<Object> exports);
1466 // DLOpen is node.dlopen(). Used to load 'module.node' dynamically shared
1468 Handle<Value> DLOpen(const v8::Arguments& args) {
1471 if (args.Length() < 2) return Undefined();
1473 String::Utf8Value filename(args[0]->ToString()); // Cast
1474 Local<Object> target = args[1]->ToObject(); // Cast
1476 // Actually call dlopen().
1477 // FIXME: This is a blocking function and should be called asynchronously!
1478 // This function should be moved to file.cc and use libeio to make this
1480 void *handle = dlopen(*filename, RTLD_LAZY);
1483 if (handle == NULL) {
1484 Local<Value> exception = Exception::Error(String::New(dlerror()));
1485 return ThrowException(exception);
1488 // Get the init() function from the dynamically shared object.
1489 void *init_handle = dlsym(handle, "init");
1490 // Error out if not found.
1491 if (init_handle == NULL) {
1492 Local<Value> exception =
1493 Exception::Error(String::New("No 'init' symbol found in module."));
1494 return ThrowException(exception);
1496 extInit init = (extInit)(init_handle); // Cast
1498 // Execute the C++ module
1505 Handle<Value> Compile(const Arguments& args) {
1508 if (args.Length() < 2) {
1509 return ThrowException(Exception::TypeError(
1510 String::New("needs two arguments.")));
1513 Local<String> source = args[0]->ToString();
1514 Local<String> filename = args[1]->ToString();
1518 Local<v8::Script> script = v8::Script::Compile(source, filename);
1519 if (try_catch.HasCaught()) {
1520 // Hack because I can't get a proper stacktrace on SyntaxError
1521 ReportException(try_catch, true);
1525 Local<Value> result = script->Run();
1526 if (try_catch.HasCaught()) return try_catch.ReThrow();
1528 return scope.Close(result);
1531 static void OnFatalError(const char* location, const char* message) {
1533 fprintf(stderr, "FATAL ERROR: %s %s\n", location, message);
1535 fprintf(stderr, "FATAL ERROR: %s\n", message);
1540 static int uncaught_exception_counter = 0;
1542 void FatalException(TryCatch &try_catch) {
1545 // Check if uncaught_exception_counter indicates a recursion
1546 if (uncaught_exception_counter > 0) {
1547 ReportException(try_catch);
1551 if (listeners_symbol.IsEmpty()) {
1552 listeners_symbol = NODE_PSYMBOL("listeners");
1553 uncaught_exception_symbol = NODE_PSYMBOL("uncaughtException");
1554 emit_symbol = NODE_PSYMBOL("emit");
1557 Local<Value> listeners_v = process->Get(listeners_symbol);
1558 assert(listeners_v->IsFunction());
1560 Local<Function> listeners = Local<Function>::Cast(listeners_v);
1562 Local<String> uncaught_exception_symbol_l = Local<String>::New(uncaught_exception_symbol);
1563 Local<Value> argv[1] = { uncaught_exception_symbol_l };
1564 Local<Value> ret = listeners->Call(process, 1, argv);
1566 assert(ret->IsArray());
1568 Local<Array> listener_array = Local<Array>::Cast(ret);
1570 uint32_t length = listener_array->Length();
1571 // Report and exit if process has no "uncaughtException" listener
1573 ReportException(try_catch);
1577 // Otherwise fire the process "uncaughtException" event
1578 Local<Value> emit_v = process->Get(emit_symbol);
1579 assert(emit_v->IsFunction());
1581 Local<Function> emit = Local<Function>::Cast(emit_v);
1583 Local<Value> error = try_catch.Exception();
1584 Local<Value> event_argv[2] = { uncaught_exception_symbol_l, error };
1586 uncaught_exception_counter++;
1587 emit->Call(process, 2, event_argv);
1588 // Decrement so we know if the next exception is a recursion or not
1589 uncaught_exception_counter--;
1593 static ev_async debug_watcher;
1594 volatile static bool debugger_msg_pending = false;
1596 static void DebugMessageCallback(EV_P_ ev_async *watcher, int revents) {
1598 assert(watcher == &debug_watcher);
1599 assert(revents == EV_ASYNC);
1600 Debug::ProcessDebugMessages();
1603 static void DebugMessageDispatch(void) {
1604 // This function is called from V8's debug thread when a debug TCP client
1605 // has sent a message.
1607 // Send a signal to our main thread saying that it should enter V8 to
1608 // handle the message.
1609 debugger_msg_pending = true;
1610 ev_async_send(EV_DEFAULT_UC_ &debug_watcher);
1613 static Handle<Value> CheckBreak(const Arguments& args) {
1615 assert(args.Length() == 0);
1617 // TODO FIXME This function is a hack to wait until V8 is ready to accept
1618 // commands. There seems to be a bug in EnableAgent( _ , _ , true) which
1619 // makes it unusable here. Ideally we'd be able to bind EnableAgent and
1620 // get it to halt until Eclipse connects.
1622 if (!debug_wait_connect)
1625 printf("Waiting for remote debugger connection...\n");
1627 const int halfSecond = 50;
1628 const int tenMs=10000;
1629 debugger_msg_pending = false;
1631 if (debugger_msg_pending) {
1632 Debug::DebugBreak();
1633 Debug::ProcessDebugMessages();
1634 debugger_msg_pending = false;
1636 // wait for 500 msec of silence from remote debugger
1637 int cnt = halfSecond;
1639 debugger_msg_pending = false;
1641 if (debugger_msg_pending) {
1642 debugger_msg_pending = false;
1653 Persistent<Object> binding_cache;
1655 static Handle<Value> Binding(const Arguments& args) {
1658 Local<String> module = args[0]->ToString();
1659 String::Utf8Value module_v(module);
1661 if (binding_cache.IsEmpty()) {
1662 binding_cache = Persistent<Object>::New(Object::New());
1665 Local<Object> exports;
1667 // TODO DRY THIS UP!
1669 if (!strcmp(*module_v, "stdio")) {
1670 if (binding_cache->Has(module)) {
1671 exports = binding_cache->Get(module)->ToObject();
1673 exports = Object::New();
1674 Stdio::Initialize(exports);
1675 binding_cache->Set(module, exports);
1678 } else if (!strcmp(*module_v, "cares")) {
1679 if (binding_cache->Has(module)) {
1680 exports = binding_cache->Get(module)->ToObject();
1682 exports = Object::New();
1683 Cares::Initialize(exports);
1684 binding_cache->Set(module, exports);
1687 } else if (!strcmp(*module_v, "fs")) {
1688 if (binding_cache->Has(module)) {
1689 exports = binding_cache->Get(module)->ToObject();
1691 exports = Object::New();
1693 // Initialize the stats object
1694 Local<FunctionTemplate> stat_templ = FunctionTemplate::New();
1695 stats_constructor_template = Persistent<FunctionTemplate>::New(stat_templ);
1696 exports->Set(String::NewSymbol("Stats"),
1697 stats_constructor_template->GetFunction());
1698 StatWatcher::Initialize(exports);
1699 File::Initialize(exports);
1700 binding_cache->Set(module, exports);
1703 } else if (!strcmp(*module_v, "signal_watcher")) {
1704 if (binding_cache->Has(module)) {
1705 exports = binding_cache->Get(module)->ToObject();
1707 exports = Object::New();
1708 SignalWatcher::Initialize(exports);
1709 binding_cache->Set(module, exports);
1712 } else if (!strcmp(*module_v, "net")) {
1713 if (binding_cache->Has(module)) {
1714 exports = binding_cache->Get(module)->ToObject();
1716 exports = Object::New();
1718 binding_cache->Set(module, exports);
1721 } else if (!strcmp(*module_v, "http_parser")) {
1722 if (binding_cache->Has(module)) {
1723 exports = binding_cache->Get(module)->ToObject();
1725 exports = Object::New();
1726 InitHttpParser(exports);
1727 binding_cache->Set(module, exports);
1730 } else if (!strcmp(*module_v, "child_process")) {
1731 if (binding_cache->Has(module)) {
1732 exports = binding_cache->Get(module)->ToObject();
1734 exports = Object::New();
1735 ChildProcess::Initialize(exports);
1736 binding_cache->Set(module, exports);
1739 } else if (!strcmp(*module_v, "buffer")) {
1740 if (binding_cache->Has(module)) {
1741 exports = binding_cache->Get(module)->ToObject();
1743 exports = Object::New();
1744 Buffer::Initialize(exports);
1745 binding_cache->Set(module, exports);
1748 } else if (!strcmp(*module_v, "crypto")) {
1749 if (binding_cache->Has(module)) {
1750 exports = binding_cache->Get(module)->ToObject();
1752 exports = Object::New();
1753 InitCrypto(exports);
1754 binding_cache->Set(module, exports);
1757 } else if (!strcmp(*module_v, "evals")) {
1758 if (binding_cache->Has(module)) {
1759 exports = binding_cache->Get(module)->ToObject();
1761 exports = Object::New();
1762 node::Script::Initialize(exports);
1763 binding_cache->Set(module, exports);
1766 } else if (!strcmp(*module_v, "natives")) {
1767 if (binding_cache->Has(module)) {
1768 exports = binding_cache->Get(module)->ToObject();
1770 exports = Object::New();
1771 // Explicitly define native sources.
1772 // TODO DRY/automate this?
1773 exports->Set(String::New("assert"), String::New(native_assert));
1774 exports->Set(String::New("buffer"), String::New(native_buffer));
1775 exports->Set(String::New("child_process"),String::New(native_child_process));
1776 exports->Set(String::New("dns"), String::New(native_dns));
1777 exports->Set(String::New("events"), String::New(native_events));
1778 exports->Set(String::New("file"), String::New(native_file));
1779 exports->Set(String::New("freelist"), String::New(native_freelist));
1780 exports->Set(String::New("fs"), String::New(native_fs));
1781 exports->Set(String::New("http"), String::New(native_http));
1782 exports->Set(String::New("crypto"), String::New(native_crypto));
1783 exports->Set(String::New("ini"), String::New(native_ini));
1784 exports->Set(String::New("mjsunit"), String::New(native_mjsunit));
1785 exports->Set(String::New("net"), String::New(native_net));
1786 exports->Set(String::New("posix"), String::New(native_posix));
1787 exports->Set(String::New("querystring"), String::New(native_querystring));
1788 exports->Set(String::New("repl"), String::New(native_repl));
1789 exports->Set(String::New("sys"), String::New(native_sys));
1790 exports->Set(String::New("tcp"), String::New(native_tcp));
1791 exports->Set(String::New("uri"), String::New(native_uri));
1792 exports->Set(String::New("url"), String::New(native_url));
1793 exports->Set(String::New("utils"), String::New(native_utils));
1794 exports->Set(String::New("path"), String::New(native_path));
1795 exports->Set(String::New("module"), String::New(native_module));
1796 binding_cache->Set(module, exports);
1800 return ThrowException(Exception::Error(String::New("No such module")));
1803 return scope.Close(exports);
1807 static void Load(int argc, char *argv[]) {
1810 Local<FunctionTemplate> process_template = FunctionTemplate::New();
1811 node::EventEmitter::Initialize(process_template);
1813 process = Persistent<Object>::New(process_template->GetFunction()->NewInstance());
1815 // Add a reference to the global object
1816 Local<Object> global = Context::GetCurrent()->Global();
1817 process->Set(String::NewSymbol("global"), global);
1820 process->Set(String::NewSymbol("version"), String::New(NODE_VERSION));
1821 // process.installPrefix
1822 process->Set(String::NewSymbol("installPrefix"), String::New(NODE_PREFIX));
1825 #define xstr(s) str(s)
1827 process->Set(String::NewSymbol("platform"), String::New(xstr(PLATFORM)));
1831 Local<Array> arguments = Array::New(argc - option_end_index + 1);
1832 arguments->Set(Integer::New(0), String::New(argv[0]));
1833 for (j = 1, i = option_end_index + 1; i < argc; j++, i++) {
1834 Local<String> arg = String::New(argv[i]);
1835 arguments->Set(Integer::New(j), arg);
1838 process->Set(String::NewSymbol("ARGV"), arguments);
1839 process->Set(String::NewSymbol("argv"), arguments);
1841 // create process.env
1842 Local<Object> env = Object::New();
1843 for (i = 0; environ[i]; i++) {
1844 // skip entries without a '=' character
1845 for (j = 0; environ[i][j] && environ[i][j] != '='; j++) { ; }
1846 // create the v8 objects
1847 Local<String> field = String::New(environ[i], j);
1848 Local<String> value = Local<String>();
1849 if (environ[i][j] == '=') {
1850 value = String::New(environ[i]+j+1);
1853 env->Set(field, value);
1855 // assign process.ENV
1856 process->Set(String::NewSymbol("ENV"), env);
1857 process->Set(String::NewSymbol("env"), env);
1859 process->Set(String::NewSymbol("pid"), Integer::New(getpid()));
1861 // define various internal methods
1862 NODE_SET_METHOD(process, "loop", Loop);
1863 NODE_SET_METHOD(process, "unloop", Unloop);
1864 NODE_SET_METHOD(process, "compile", Compile);
1865 NODE_SET_METHOD(process, "_byteLength", ByteLength);
1866 NODE_SET_METHOD(process, "_needTickCallback", NeedTickCallback);
1867 NODE_SET_METHOD(process, "reallyExit", Exit);
1868 NODE_SET_METHOD(process, "chdir", Chdir);
1869 NODE_SET_METHOD(process, "cwd", Cwd);
1870 NODE_SET_METHOD(process, "getuid", GetUid);
1871 NODE_SET_METHOD(process, "setuid", SetUid);
1873 NODE_SET_METHOD(process, "setgid", SetGid);
1874 NODE_SET_METHOD(process, "getgid", GetGid);
1876 NODE_SET_METHOD(process, "umask", Umask);
1877 NODE_SET_METHOD(process, "dlopen", DLOpen);
1878 NODE_SET_METHOD(process, "kill", Kill);
1879 NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
1880 NODE_SET_METHOD(process, "checkBreak", CheckBreak);
1882 NODE_SET_METHOD(process, "binding", Binding);
1884 // Assign the EventEmitter. It was created in main().
1885 process->Set(String::NewSymbol("EventEmitter"),
1886 EventEmitter::constructor_template->GetFunction());
1889 // Initialize the C++ modules..................filename of module
1890 IOWatcher::Initialize(process); // io_watcher.cc
1891 // Not in use at the moment.
1892 //IdleWatcher::Initialize(process); // idle_watcher.cc
1893 Timer::Initialize(process); // timer.cc
1894 DefineConstants(process); // constants.cc
1896 // Compile, execute the src/node.js file. (Which was included as static C
1897 // string in node_natives.h. 'natve_node' is the string containing that
1900 // The node.js file returns a function 'f'
1906 Local<Value> f_value = ExecuteString(String::New(native_node),
1907 String::New("node.js"));
1909 if (try_catch.HasCaught()) {
1910 ReportException(try_catch);
1914 assert(f_value->IsFunction());
1915 Local<Function> f = Local<Function>::Cast(f_value);
1917 // Now we call 'f' with the 'process' variable that we've built up with
1918 // all our bindings. Inside node.js we'll take care of assigning things to
1921 // We start the process this way in order to be more modular. Developers
1922 // who do not like how 'src/node.js' setups the module system but do like
1923 // Node's I/O bindings may want to replace 'f' with their own function.
1925 Local<Value> args[1] = { Local<Value>::New(process) };
1927 f->Call(global, 1, args);
1930 if (try_catch.HasCaught()) {
1931 ReportException(try_catch);
1937 static void PrintHelp();
1939 static void ParseDebugOpt(const char* arg) {
1942 use_debug_agent = true;
1943 if (!strcmp (arg, "--debug-brk")) {
1944 debug_wait_connect = true;
1946 } else if (!strcmp(arg, "--debug")) {
1948 } else if (strstr(arg, "--debug-brk=") == arg) {
1949 debug_wait_connect = true;
1950 p = 1 + strchr(arg, '=');
1951 debug_port = atoi(p);
1952 } else if (strstr(arg, "--debug=") == arg) {
1953 p = 1 + strchr(arg, '=');
1954 debug_port = atoi(p);
1956 if (p && debug_port > 1024 && debug_port < 65536)
1959 fprintf(stderr, "Bad debug option.\n");
1960 if (p) fprintf(stderr, "Debug port must be in range 1025 to 65535.\n");
1966 static void PrintHelp() {
1967 printf("Usage: node [options] script.js [arguments] \n"
1969 " -v, --version print node's version\n"
1970 " --debug[=port] enable remote debugging via given TCP port\n"
1971 " without stopping the execution\n"
1972 " --debug-brk[=port] as above, but break in script.js and\n"
1973 " wait for remote debugger to connect\n"
1974 " --v8-options print v8 command line options\n"
1975 " --vars print various compiled-in variables\n"
1977 "Enviromental variables:\n"
1978 "NODE_PATH ':'-separated list of directories\n"
1979 " prefixed to the module search path,\n"
1981 "NODE_DEBUG Print additional debugging output.\n"
1983 "Documentation can be found at http://nodejs.org/api.html"
1984 " or with 'man node'\n");
1987 // Parse node command line arguments.
1988 static void ParseArgs(int *argc, char **argv) {
1989 // TODO use parse opts
1990 for (int i = 1; i < *argc; i++) {
1991 const char *arg = argv[i];
1992 if (strstr(arg, "--debug") == arg) {
1994 argv[i] = const_cast<char*>("");
1995 option_end_index = i;
1996 } else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
1997 printf("%s\n", NODE_VERSION);
1999 } else if (strcmp(arg, "--vars") == 0) {
2000 printf("NODE_PREFIX: %s\n", NODE_PREFIX);
2001 printf("NODE_CFLAGS: %s\n", NODE_CFLAGS);
2003 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
2006 } else if (strcmp(arg, "--v8-options") == 0) {
2007 argv[i] = const_cast<char*>("--help");
2008 option_end_index = i+1;
2009 } else if (argv[i][0] != '-') {
2010 option_end_index = i-1;
2019 int main(int argc, char *argv[]) {
2020 // Parse a few arguments which are specific to Node.
2021 node::ParseArgs(&argc, argv);
2022 // Parse the rest of the args (up to the 'option_end_index' (where '--' was
2023 // in the command line))
2024 V8::SetFlagsFromCommandLine(&node::option_end_index, argv, false);
2026 // Error out if we don't have a script argument.
2028 fprintf(stderr, "No script was specified.\n");
2035 struct sigaction sa;
2036 bzero(&sa, sizeof(sa));
2037 sa.sa_handler = SIG_IGN;
2038 sigaction(SIGPIPE, &sa, NULL);
2041 // Initialize the default ev loop.
2043 // TODO(Ryan) I'm experiencing abnormally high load using Solaris's
2044 // EVBACKEND_PORT. Temporarally forcing select() until I debug.
2045 ev_default_loop(EVBACKEND_SELECT);
2047 ev_default_loop(EVFLAG_AUTO);
2050 ev_prepare_init(&node::next_tick_watcher, node::Tick);
2051 ev_prepare_start(EV_DEFAULT_UC_ &node::next_tick_watcher);
2052 ev_unref(EV_DEFAULT_UC);
2054 ev_idle_init(&node::tick_spinner, node::Spin);
2056 ev_check_init(&node::gc_check, node::Check);
2057 ev_check_start(EV_DEFAULT_UC_ &node::gc_check);
2058 ev_unref(EV_DEFAULT_UC);
2060 ev_idle_init(&node::gc_idle, node::Idle);
2061 ev_timer_init(&node::gc_timer, node::CheckStatus, 5., 5.);
2064 // Setup the EIO thread pool
2065 { // It requires 3, yes 3, watchers.
2066 ev_idle_init(&node::eio_poller, node::DoPoll);
2068 ev_async_init(&node::eio_want_poll_notifier, node::WantPollNotifier);
2069 ev_async_start(EV_DEFAULT_UC_ &node::eio_want_poll_notifier);
2070 ev_unref(EV_DEFAULT_UC);
2072 ev_async_init(&node::eio_done_poll_notifier, node::DonePollNotifier);
2073 ev_async_start(EV_DEFAULT_UC_ &node::eio_done_poll_notifier);
2074 ev_unref(EV_DEFAULT_UC);
2076 eio_init(node::EIOWantPoll, node::EIODonePoll);
2077 // Don't handle more than 10 reqs on each eio_poll(). This is to avoid
2078 // race conditions. See test/mjsunit/test-eio-race.js
2079 eio_set_max_poll_reqs(10);
2083 HandleScope handle_scope;
2085 V8::SetFatalErrorHandler(node::OnFatalError);
2087 // If the --debug flag was specified then initialize the debug thread.
2088 if (node::use_debug_agent) {
2089 // Initialize the async watcher for receiving messages from the debug
2090 // thread and marshal it into the main thread. DebugMessageCallback()
2091 // is called from the main thread to execute a random bit of javascript
2092 // - which will give V8 control so it can handle whatever new message
2093 // had been received on the debug thread.
2094 ev_async_init(&node::debug_watcher, node::DebugMessageCallback);
2095 ev_set_priority(&node::debug_watcher, EV_MAXPRI);
2096 // Set the callback DebugMessageDispatch which is called from the debug
2098 Debug::SetDebugMessageDispatchHandler(node::DebugMessageDispatch);
2099 // Start the async watcher.
2100 ev_async_start(EV_DEFAULT_UC_ &node::debug_watcher);
2101 // unref it so that we exit the event loop despite it being active.
2102 ev_unref(EV_DEFAULT_UC);
2104 // Start the debug thread and it's associated TCP server on port 5858.
2105 bool r = Debug::EnableAgent("node " NODE_VERSION, node::debug_port);
2107 // Crappy check that everything went well. FIXME
2109 // Print out some information.
2110 printf("debugger listening on port %d\n", node::debug_port);
2113 // Create the one and only Context.
2114 Persistent<Context> context = Context::New();
2115 Context::Scope context_scope(context);
2117 // Create all the objects, load modules, do everything.
2118 // so your next reading stop should be node::Load()!
2119 node::Load(argc, argv);
2121 node::Stdio::Flush();