1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "node_buffer.h"
24 #include "node_constants.h"
25 #include "node_file.h"
26 #include "node_http_parser.h"
27 #include "node_javascript.h"
28 #include "node_version.h"
30 #if defined HAVE_PERFCTR
31 #include "node_counters.h"
35 #include "node_crypto.h"
38 #if defined HAVE_DTRACE || defined HAVE_ETW || defined HAVE_SYSTEMTAP
39 #include "node_dtrace.h"
43 #include "node_provider.h"
47 #include "handle_wrap.h"
49 #include "string_bytes.h"
56 #include <limits.h> // PATH_MAX
62 #include <sys/types.h>
68 #define strcasecmp _stricmp
69 #define getpid _getpid
73 #include <unistd.h> // setuid, getuid
76 #if defined(__POSIX__) && !defined(__ANDROID__)
77 #include <pwd.h> // getpwnam()
78 #include <grp.h> // getgrnam()
82 #include <crt_externs.h>
83 #define environ (*_NSGetEnviron())
84 #elif !defined(_MSC_VER)
85 extern char **environ;
91 using v8::ArrayBuffer;
96 using v8::FunctionCallbackInfo;
97 using v8::FunctionTemplate;
99 using v8::HandleScope;
100 using v8::HeapStatistics;
108 using v8::ObjectTemplate;
109 using v8::Persistent;
110 using v8::PropertyCallbackInfo;
111 using v8::ResourceConstraints;
112 using v8::SetResourceConstraints;
114 using v8::ThrowException;
119 using v8::kExternalUnsignedIntArray;
121 QUEUE handle_wrap_queue = { &handle_wrap_queue, &handle_wrap_queue };
122 QUEUE req_wrap_queue = { &req_wrap_queue, &req_wrap_queue };
124 // declared in req_wrap.h
125 Cached<String> process_symbol;
126 Cached<String> domain_symbol;
128 // declared in node_internals.h
129 Persistent<Object> process_p;
131 static Persistent<Function> process_tickCallback;
132 static Persistent<Object> binding_cache;
133 static Persistent<Array> module_load_list;
134 static Persistent<Array> p_domain_box;
136 static Cached<String> exports_symbol;
138 static Cached<String> errno_symbol;
139 static Cached<String> syscall_symbol;
140 static Cached<String> errpath_symbol;
141 static Cached<String> code_symbol;
143 static Cached<String> rss_symbol;
144 static Cached<String> heap_total_symbol;
145 static Cached<String> heap_used_symbol;
147 static Cached<String> fatal_exception_symbol;
149 static Cached<String> enter_symbol;
150 static Cached<String> exit_symbol;
151 static Cached<String> disposed_symbol;
153 // Essential for node_wrap.h
154 Persistent<FunctionTemplate> pipeConstructorTmpl;
155 Persistent<FunctionTemplate> tcpConstructorTmpl;
156 Persistent<FunctionTemplate> ttyConstructorTmpl;
158 static bool print_eval = false;
159 static bool force_repl = false;
160 static bool trace_deprecation = false;
161 static bool throw_deprecation = false;
162 static char *eval_string = NULL;
163 static int option_end_index = 0;
164 static bool use_debug_agent = false;
165 static bool debug_wait_connect = false;
166 static int debug_port = 5858;
167 bool using_domains = false;
169 // used by C++ modules as well
170 bool no_deprecation = false;
172 static uv_check_t check_immediate_watcher;
173 static uv_idle_t idle_immediate_dummy;
174 static bool need_immediate_cb;
175 static Cached<String> immediate_callback_sym;
177 // for quick ref to tickCallback values
185 // easily communicate domain depth
190 // process-relative uptime base, initialized at start-up
191 static double prog_start_time;
193 static volatile bool debugger_running = false;
194 static uv_async_t dispatch_debug_messages_async;
195 static uv_async_t emit_debug_enabled_async;
197 // Declared in node_internals.h
198 Isolate* node_isolate = NULL;
201 class ArrayBufferAllocator : public ArrayBuffer::Allocator {
203 // Impose an upper limit to avoid out of memory errors that bring down
205 static const size_t kMaxLength = 0x3fffffff;
206 static ArrayBufferAllocator the_singleton;
207 virtual ~ArrayBufferAllocator() {}
208 virtual void* Allocate(size_t length);
209 virtual void Free(void* data);
211 ArrayBufferAllocator() {}
212 ArrayBufferAllocator(const ArrayBufferAllocator&);
213 void operator=(const ArrayBufferAllocator&);
216 ArrayBufferAllocator ArrayBufferAllocator::the_singleton;
219 void* ArrayBufferAllocator::Allocate(size_t length) {
220 if (length > kMaxLength) return NULL;
221 return new char[length];
225 void ArrayBufferAllocator::Free(void* data) {
226 delete[] static_cast<char*>(data);
230 static void CheckImmediate(uv_check_t* handle, int status) {
231 assert(handle == &check_immediate_watcher);
234 HandleScope scope(node_isolate);
236 if (immediate_callback_sym.IsEmpty()) {
237 immediate_callback_sym =
238 FIXED_ONE_BYTE_STRING(node_isolate, "_immediateCallback");
241 MakeCallback(process_p, immediate_callback_sym, 0, NULL);
245 static void IdleImmediateDummy(uv_idle_t* handle, int status) {
246 // Do nothing. Only for maintaining event loop
247 assert(handle == &idle_immediate_dummy);
252 static inline const char *errno_string(int errorno) {
253 #define ERRNO_CASE(e) case e: return #e;
260 ERRNO_CASE(EADDRINUSE);
264 ERRNO_CASE(EADDRNOTAVAIL);
268 ERRNO_CASE(EAFNOSUPPORT);
276 # if EAGAIN != EWOULDBLOCK
277 ERRNO_CASE(EWOULDBLOCK);
282 ERRNO_CASE(EALREADY);
298 ERRNO_CASE(ECANCELED);
306 ERRNO_CASE(ECONNABORTED);
310 ERRNO_CASE(ECONNREFUSED);
314 ERRNO_CASE(ECONNRESET);
322 ERRNO_CASE(EDESTADDRREQ);
346 ERRNO_CASE(EHOSTUNREACH);
358 ERRNO_CASE(EINPROGRESS);
394 ERRNO_CASE(EMSGSIZE);
398 ERRNO_CASE(EMULTIHOP);
402 ERRNO_CASE(ENAMETOOLONG);
406 ERRNO_CASE(ENETDOWN);
410 ERRNO_CASE(ENETRESET);
414 ERRNO_CASE(ENETUNREACH);
446 # if ENOLINK != ENOLCK
460 ERRNO_CASE(ENOPROTOOPT);
480 ERRNO_CASE(ENOTCONN);
488 ERRNO_CASE(ENOTEMPTY);
492 ERRNO_CASE(ENOTSOCK);
499 ERRNO_CASE(EOPNOTSUPP);
513 ERRNO_CASE(EOVERFLOW);
528 #ifdef EPROTONOSUPPORT
529 ERRNO_CASE(EPROTONOSUPPORT);
533 ERRNO_CASE(EPROTOTYPE);
561 ERRNO_CASE(ETIMEDOUT);
576 const char *signo_string(int signo) {
577 #define SIGNO_CASE(e) case e: return #e;
604 # if SIGABRT != SIGIOT
648 SIGNO_CASE(SIGSTKFLT);
665 SIGNO_CASE(SIGBREAK);
689 SIGNO_CASE(SIGVTALRM);
697 SIGNO_CASE(SIGWINCH);
705 # if SIGPOLL != SIGIO
715 # if SIGPWR != SIGLOST
729 Local<Value> ErrnoException(int errorno,
734 Local<String> estring = OneByteString(node_isolate, errno_string(errorno));
735 if (msg == NULL || msg[0] == '\0') {
736 msg = strerror(errorno);
738 Local<String> message = OneByteString(node_isolate, msg);
740 Local<String> cons1 =
741 String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", "));
742 Local<String> cons2 = String::Concat(cons1, message);
744 if (syscall_symbol.IsEmpty()) {
745 syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall");
746 errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno");
747 errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path");
748 code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code");
752 Local<String> cons3 =
753 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
754 Local<String> cons4 =
755 String::Concat(cons3, String::NewFromUtf8(node_isolate, path));
756 Local<String> cons5 =
757 String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
758 e = Exception::Error(cons5);
760 e = Exception::Error(cons2);
763 Local<Object> obj = e->ToObject();
765 obj->Set(errno_symbol, Integer::New(errorno, node_isolate));
766 obj->Set(code_symbol, estring);
767 if (path) obj->Set(errpath_symbol, String::NewFromUtf8(node_isolate, path));
768 if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall));
773 // hack alert! copy of ErrnoException, tuned for uv errors
774 Local<Value> UVException(int errorno,
778 if (syscall_symbol.IsEmpty()) {
779 syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall");
780 errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno");
781 errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path");
782 code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code");
786 msg = uv_strerror(errorno);
788 Local<String> estring = OneByteString(node_isolate, uv_err_name(errorno));
789 Local<String> message = OneByteString(node_isolate, msg);
790 Local<String> cons1 =
791 String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", "));
792 Local<String> cons2 = String::Concat(cons1, message);
796 Local<String> path_str;
800 if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) {
801 path_str = String::Concat(FIXED_ONE_BYTE_STRING(node_isolate, "\\\\"),
802 String::NewFromUtf8(node_isolate, path + 8));
803 } else if (strncmp(path, "\\\\?\\", 4) == 0) {
804 path_str = String::NewFromUtf8(node_isolate, path + 4);
806 path_str = String::NewFromUtf8(node_isolate, path);
809 path_str = String::NewFromUtf8(node_isolate, path);
812 Local<String> cons3 =
813 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
814 Local<String> cons4 =
815 String::Concat(cons3, path_str);
816 Local<String> cons5 =
817 String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
818 e = Exception::Error(cons5);
820 e = Exception::Error(cons2);
823 Local<Object> obj = e->ToObject();
825 // TODO(piscisaureus) errno should probably go
826 obj->Set(errno_symbol, Integer::New(errorno, node_isolate));
827 obj->Set(code_symbol, estring);
828 if (path) obj->Set(errpath_symbol, path_str);
829 if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall));
835 // Does about the same as strerror(),
836 // but supports all windows error messages
837 static const char *winapi_strerror(const int errorno) {
840 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
841 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
842 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
845 // Remove trailing newlines
846 for (int i = strlen(errmsg) - 1;
847 i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) {
853 // FormatMessage failed
854 return "Unknown error";
859 Local<Value> WinapiErrnoException(int errorno,
864 if (!msg || !msg[0]) {
865 msg = winapi_strerror(errorno);
867 Local<String> message = OneByteString(node_isolate, msg);
869 if (syscall_symbol.IsEmpty()) {
870 syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall");
871 errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno");
872 errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path");
873 code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code");
877 Local<String> cons1 =
878 String::Concat(message, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
879 Local<String> cons2 =
880 String::Concat(cons1, String::NewFromUtf8(node_isolate, path));
881 Local<String> cons3 =
882 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
883 e = Exception::Error(cons3);
885 e = Exception::Error(message);
888 Local<Object> obj = e->ToObject();
890 obj->Set(errno_symbol, Integer::New(errorno, node_isolate));
891 if (path) obj->Set(errpath_symbol, String::NewFromUtf8(node_isolate, path));
892 if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall));
898 void SetupDomainUse(const FunctionCallbackInfo<Value>& args) {
899 if (using_domains) return;
900 HandleScope scope(node_isolate);
901 using_domains = true;
902 Local<Object> process = PersistentToLocal(node_isolate, process_p);
904 process->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_tickDomainCallback"));
905 if (!tdc_v->IsFunction()) {
906 fprintf(stderr, "process._tickDomainCallback assigned to non-function\n");
909 Local<Function> tdc = tdc_v.As<Function>();
910 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback"), tdc);
911 process_tickCallback.Reset(node_isolate, tdc);
912 if (!args[0]->IsArray()) {
913 fprintf(stderr, "_setupDomainUse first argument must be an array\n");
916 p_domain_box.Reset(node_isolate, args[0].As<Array>());
917 if (!args[1]->IsObject()) {
918 fprintf(stderr, "_setupDomainUse second argument must be an object\n");
921 Local<Object> flag = args[1].As<Object>();
922 flag->SetIndexedPropertiesToExternalArrayData(&domain_flag,
923 kExternalUnsignedIntArray,
929 return using_domains && domain_flag.count > 0;
933 Handle<Value> GetDomain() {
934 // no domain can exist if no domain module has been loaded
935 if (!InDomain() || p_domain_box.IsEmpty())
936 return Null(node_isolate);
938 return PersistentToLocal(node_isolate, p_domain_box)->Get(0);
943 MakeDomainCallback(const Handle<Object> object,
944 const Handle<Function> callback,
946 Handle<Value> argv[]) {
947 // TODO(trevnorris) Hook for long stack traces to be made here.
949 // lazy load domain specific symbols
950 if (enter_symbol.IsEmpty()) {
951 enter_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "enter");
952 exit_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "exit");
953 disposed_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "_disposed");
956 Local<Value> domain_v = object->Get(domain_symbol);
957 Local<Object> domain;
958 Local<Function> enter;
959 Local<Function> exit;
962 try_catch.SetVerbose(true);
964 bool has_domain = domain_v->IsObject();
966 domain = domain_v->ToObject();
967 assert(!domain.IsEmpty());
968 if (domain->Get(disposed_symbol)->IsTrue()) {
969 // domain has been disposed of.
970 return Undefined(node_isolate);
972 enter = Local<Function>::Cast(domain->Get(enter_symbol));
973 assert(!enter.IsEmpty());
974 enter->Call(domain, 0, NULL);
976 if (try_catch.HasCaught()) {
977 return Undefined(node_isolate);
981 Local<Value> ret = callback->Call(object, argc, argv);
983 if (try_catch.HasCaught()) {
984 return Undefined(node_isolate);
988 exit = Local<Function>::Cast(domain->Get(exit_symbol));
989 assert(!exit.IsEmpty());
990 exit->Call(domain, 0, NULL);
992 if (try_catch.HasCaught()) {
993 return Undefined(node_isolate);
997 if (tick_infobox.last_threw == 1) {
998 tick_infobox.last_threw = 0;
1002 if (tick_infobox.in_tick == 1) {
1006 if (tick_infobox.length == 0) {
1007 tick_infobox.index = 0;
1011 // process nextTicks after call
1012 Local<Object> process = PersistentToLocal(node_isolate, process_p);
1013 Local<Function> fn = PersistentToLocal(node_isolate, process_tickCallback);
1014 fn->Call(process, 0, NULL);
1016 if (try_catch.HasCaught()) {
1017 return Undefined(node_isolate);
1025 MakeCallback(const Handle<Object> object,
1026 const Handle<Function> callback,
1028 Handle<Value> argv[]) {
1029 // TODO(trevnorris) Hook for long stack traces to be made here.
1030 Local<Object> process = PersistentToLocal(node_isolate, process_p);
1033 return MakeDomainCallback(object, callback, argc, argv);
1035 // lazy load no domain next tick callbacks
1036 if (process_tickCallback.IsEmpty()) {
1038 process->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback"));
1039 if (!cb_v->IsFunction()) {
1040 fprintf(stderr, "process._tickCallback assigned to non-function\n");
1043 process_tickCallback.Reset(node_isolate, cb_v.As<Function>());
1047 try_catch.SetVerbose(true);
1049 Local<Value> ret = callback->Call(object, argc, argv);
1051 if (try_catch.HasCaught()) {
1052 return Undefined(node_isolate);
1055 if (tick_infobox.in_tick == 1) {
1059 if (tick_infobox.length == 0) {
1060 tick_infobox.index = 0;
1064 // process nextTicks after call
1065 Local<Function> fn = PersistentToLocal(node_isolate, process_tickCallback);
1066 fn->Call(process, 0, NULL);
1068 if (try_catch.HasCaught()) {
1069 return Undefined(node_isolate);
1078 MakeCallback(const Handle<Object> object,
1081 Handle<Value> argv[]) {
1082 HandleScope scope(node_isolate);
1084 Local<Function> callback = object->Get(index).As<Function>();
1085 assert(callback->IsFunction());
1088 return scope.Close(MakeDomainCallback(object, callback, argc, argv));
1089 return scope.Close(MakeCallback(object, callback, argc, argv));
1094 MakeCallback(const Handle<Object> object,
1095 const Handle<String> symbol,
1097 Handle<Value> argv[]) {
1098 HandleScope scope(node_isolate);
1100 Local<Function> callback = object->Get(symbol).As<Function>();
1101 assert(callback->IsFunction());
1104 return scope.Close(MakeDomainCallback(object, callback, argc, argv));
1105 return scope.Close(MakeCallback(object, callback, argc, argv));
1110 MakeCallback(const Handle<Object> object,
1113 Handle<Value> argv[]) {
1114 HandleScope scope(node_isolate);
1116 Local<String> method_string = OneByteString(node_isolate, method);
1117 Handle<Value> ret = MakeCallback(object, method_string, argc, argv);
1119 return scope.Close(ret);
1123 enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
1124 HandleScope scope(node_isolate);
1126 if (!encoding_v->IsString()) return _default;
1128 String::Utf8Value encoding(encoding_v);
1130 if (strcasecmp(*encoding, "utf8") == 0) {
1132 } else if (strcasecmp(*encoding, "utf-8") == 0) {
1134 } else if (strcasecmp(*encoding, "ascii") == 0) {
1136 } else if (strcasecmp(*encoding, "base64") == 0) {
1138 } else if (strcasecmp(*encoding, "ucs2") == 0) {
1140 } else if (strcasecmp(*encoding, "ucs-2") == 0) {
1142 } else if (strcasecmp(*encoding, "utf16le") == 0) {
1144 } else if (strcasecmp(*encoding, "utf-16le") == 0) {
1146 } else if (strcasecmp(*encoding, "binary") == 0) {
1148 } else if (strcasecmp(*encoding, "buffer") == 0) {
1150 } else if (strcasecmp(*encoding, "hex") == 0) {
1152 } else if (strcasecmp(*encoding, "raw") == 0) {
1153 if (!no_deprecation) {
1154 fprintf(stderr, "'raw' (array of integers) has been removed. "
1158 } else if (strcasecmp(*encoding, "raws") == 0) {
1159 if (!no_deprecation) {
1160 fprintf(stderr, "'raws' encoding has been renamed to 'binary'. "
1161 "Please update your code.\n");
1169 Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
1170 return StringBytes::Encode(static_cast<const char*>(buf),
1175 // Returns -1 if the handle was not valid for decoding
1176 ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
1177 HandleScope scope(node_isolate);
1179 if (val->IsArray()) {
1180 fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
1186 return StringBytes::Size(val, encoding);
1190 # define MIN(a, b) ((a) < (b) ? (a) : (b))
1193 // Returns number of bytes written.
1194 ssize_t DecodeWrite(char *buf,
1196 v8::Handle<v8::Value> val,
1197 enum encoding encoding) {
1198 return StringBytes::Write(buf, buflen, val, encoding, NULL);
1201 void DisplayExceptionLine(Handle<Message> message) {
1202 // Prevent re-entry into this function. For example, if there is
1203 // a throw from a program in vm.runInThisContext(code, filename, true),
1204 // then we want to show the original failure, not the secondary one.
1205 static bool displayed_error = false;
1207 if (displayed_error) return;
1208 displayed_error = true;
1210 uv_tty_reset_mode();
1212 fprintf(stderr, "\n");
1214 if (!message.IsEmpty()) {
1215 // Print (filename):(line number): (message).
1216 String::Utf8Value filename(message->GetScriptResourceName());
1217 const char* filename_string = *filename;
1218 int linenum = message->GetLineNumber();
1219 fprintf(stderr, "%s:%i\n", filename_string, linenum);
1220 // Print line of source code.
1221 String::Utf8Value sourceline(message->GetSourceLine());
1222 const char* sourceline_string = *sourceline;
1224 // Because of how node modules work, all scripts are wrapped with a
1225 // "function (module, exports, __filename, ...) {"
1226 // to provide script local variables.
1228 // When reporting errors on the first line of a script, this wrapper
1229 // function is leaked to the user. There used to be a hack here to
1230 // truncate off the first 62 characters, but it caused numerous other
1231 // problems when vm.runIn*Context() methods were used for non-module
1234 // If we ever decide to re-instate such a hack, the following steps
1237 // 1. Pass a flag around to say "this code was wrapped"
1238 // 2. Update the stack frame output so that it is also correct.
1240 // It would probably be simpler to add a line rather than add some
1241 // number of characters to the first line, since V8 truncates the
1242 // sourceline to 78 characters, and we end up not providing very much
1243 // useful debugging info to the user if we remove 62 characters.
1245 int start = message->GetStartColumn();
1246 int end = message->GetEndColumn();
1248 fprintf(stderr, "%s\n", sourceline_string);
1249 // Print wavy underline (GetUnderline is deprecated).
1250 for (int i = 0; i < start; i++) {
1251 fputc((sourceline_string[i] == '\t') ? '\t' : ' ', stderr);
1253 for (int i = start; i < end; i++) {
1256 fputc('\n', stderr);
1261 static void ReportException(Handle<Value> er, Handle<Message> message) {
1262 HandleScope scope(node_isolate);
1264 DisplayExceptionLine(message);
1266 Local<Value> trace_value(
1267 er->ToObject()->Get(FIXED_ONE_BYTE_STRING(node_isolate, "stack")));
1268 String::Utf8Value trace(trace_value);
1270 // range errors have a trace member set to undefined
1271 if (trace.length() > 0 && !trace_value->IsUndefined()) {
1272 fprintf(stderr, "%s\n", *trace);
1274 // this really only happens for RangeErrors, since they're the only
1275 // kind that won't have all this info in the trace, or when non-Error
1276 // objects are thrown manually.
1277 Local<Value> message;
1280 if (er->IsObject()) {
1281 Local<Object> err_obj = er.As<Object>();
1282 message = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "message"));
1283 name = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "name"));
1286 if (message.IsEmpty() ||
1287 message->IsUndefined() ||
1289 name->IsUndefined()) {
1290 // Not an error object. Just print as-is.
1291 String::Utf8Value message(er);
1292 fprintf(stderr, "%s\n", *message);
1294 String::Utf8Value name_string(name);
1295 String::Utf8Value message_string(message);
1296 fprintf(stderr, "%s: %s\n", *name_string, *message_string);
1304 static void ReportException(const TryCatch& try_catch) {
1305 ReportException(try_catch.Exception(), try_catch.Message());
1309 // Executes a str within the current v8 context.
1310 Local<Value> ExecuteString(Handle<String> source, Handle<Value> filename) {
1311 HandleScope scope(node_isolate);
1314 // try_catch must be nonverbose to disable FatalException() handler,
1315 // we will handle exceptions ourself.
1316 try_catch.SetVerbose(false);
1318 Local<v8::Script> script = v8::Script::Compile(source, filename);
1319 if (script.IsEmpty()) {
1320 ReportException(try_catch);
1324 Local<Value> result = script->Run();
1325 if (result.IsEmpty()) {
1326 ReportException(try_catch);
1330 return scope.Close(result);
1334 static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
1335 HandleScope scope(node_isolate);
1337 Local<Array> ary = Array::New();
1341 QUEUE_FOREACH(q, &req_wrap_queue) {
1342 ReqWrap<uv_req_t>* w = container_of(q, ReqWrap<uv_req_t>, req_wrap_queue_);
1343 if (w->persistent().IsEmpty()) continue;
1344 ary->Set(i++, w->object());
1347 args.GetReturnValue().Set(ary);
1351 // Non-static, friend of HandleWrap. Could have been a HandleWrap method but
1352 // implemented here for consistency with GetActiveRequests().
1353 void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
1354 HandleScope scope(node_isolate);
1356 Local<Array> ary = Array::New();
1360 Local<String> owner_sym = FIXED_ONE_BYTE_STRING(node_isolate, "owner");
1362 QUEUE_FOREACH(q, &handle_wrap_queue) {
1363 HandleWrap* w = container_of(q, HandleWrap, handle_wrap_queue_);
1364 if (w->persistent().IsEmpty() || (w->flags_ & HandleWrap::kUnref)) continue;
1365 Local<Object> object = w->object();
1366 Local<Value> owner = object->Get(owner_sym);
1367 if (owner->IsUndefined()) owner = object;
1368 ary->Set(i++, owner);
1371 args.GetReturnValue().Set(ary);
1375 static void Abort(const FunctionCallbackInfo<Value>& args) {
1380 static void Chdir(const FunctionCallbackInfo<Value>& args) {
1381 HandleScope scope(node_isolate);
1383 if (args.Length() != 1 || !args[0]->IsString()) {
1384 return ThrowError("Bad argument."); // FIXME(bnoordhuis) ThrowTypeError?
1387 String::Utf8Value path(args[0]);
1388 int err = uv_chdir(*path);
1390 return ThrowUVException(err, "uv_chdir");
1395 static void Cwd(const FunctionCallbackInfo<Value>& args) {
1396 HandleScope scope(node_isolate);
1398 /* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
1399 char buf[MAX_PATH * 4 + 1];
1401 char buf[PATH_MAX + 1];
1404 int err = uv_cwd(buf, ARRAY_SIZE(buf) - 1);
1406 return ThrowUVException(err, "uv_cwd");
1409 buf[ARRAY_SIZE(buf) - 1] = '\0';
1410 Local<String> cwd = String::NewFromUtf8(node_isolate, buf);
1412 args.GetReturnValue().Set(cwd);
1416 static void Umask(const FunctionCallbackInfo<Value>& args) {
1417 HandleScope scope(node_isolate);
1420 if (args.Length() < 1 || args[0]->IsUndefined()) {
1422 umask(static_cast<mode_t>(old));
1423 } else if (!args[0]->IsInt32() && !args[0]->IsString()) {
1424 return ThrowTypeError("argument must be an integer or octal string.");
1427 if (args[0]->IsInt32()) {
1428 oct = args[0]->Uint32Value();
1431 String::Utf8Value str(args[0]);
1433 // Parse the octal string.
1434 for (int i = 0; i < str.length(); i++) {
1436 if (c > '7' || c < '0') {
1437 return ThrowTypeError("invalid octal string");
1443 old = umask(static_cast<mode_t>(oct));
1446 args.GetReturnValue().Set(old);
1450 #if defined(__POSIX__) && !defined(__ANDROID__)
1452 static const uid_t uid_not_found = static_cast<uid_t>(-1);
1453 static const gid_t gid_not_found = static_cast<gid_t>(-1);
1456 static uid_t uid_by_name(const char* name) {
1464 if (getpwnam_r(name, &pwd, buf, sizeof(buf), &pp) == 0 && pp != NULL) {
1468 return uid_not_found;
1472 static char* name_by_uid(uid_t uid) {
1481 if ((rc = getpwuid_r(uid, &pwd, buf, sizeof(buf), &pp)) == 0 && pp != NULL) {
1482 return strdup(pp->pw_name);
1493 static gid_t gid_by_name(const char* name) {
1501 if (getgrnam_r(name, &pwd, buf, sizeof(buf), &pp) == 0 && pp != NULL) {
1505 return gid_not_found;
1509 #if 0 // For future use.
1510 static const char* name_by_gid(gid_t gid) {
1519 if ((rc = getgrgid_r(gid, &pwd, buf, sizeof(buf), &pp)) == 0 && pp != NULL) {
1520 return strdup(pp->gr_name);
1532 static uid_t uid_by_name(Handle<Value> value) {
1533 if (value->IsUint32()) {
1534 return static_cast<uid_t>(value->Uint32Value());
1536 String::Utf8Value name(value);
1537 return uid_by_name(*name);
1542 static gid_t gid_by_name(Handle<Value> value) {
1543 if (value->IsUint32()) {
1544 return static_cast<gid_t>(value->Uint32Value());
1546 String::Utf8Value name(value);
1547 return gid_by_name(*name);
1552 static void GetUid(const FunctionCallbackInfo<Value>& args) {
1553 args.GetReturnValue().Set(getuid());
1557 static void GetGid(const FunctionCallbackInfo<Value>& args) {
1558 args.GetReturnValue().Set(getgid());
1562 static void SetGid(const FunctionCallbackInfo<Value>& args) {
1563 HandleScope scope(node_isolate);
1565 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1566 return ThrowTypeError("setgid argument must be a number or a string");
1569 gid_t gid = gid_by_name(args[0]);
1571 if (gid == gid_not_found) {
1572 return ThrowError("setgid group id does not exist");
1576 return ThrowErrnoException(errno, "setgid");
1581 static void SetUid(const FunctionCallbackInfo<Value>& args) {
1582 HandleScope scope(node_isolate);
1584 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1585 return ThrowTypeError("setuid argument must be a number or a string");
1588 uid_t uid = uid_by_name(args[0]);
1590 if (uid == uid_not_found) {
1591 return ThrowError("setuid user id does not exist");
1595 return ThrowErrnoException(errno, "setuid");
1600 static void GetGroups(const FunctionCallbackInfo<Value>& args) {
1601 HandleScope scope(node_isolate);
1603 int ngroups = getgroups(0, NULL);
1605 if (ngroups == -1) {
1606 return ThrowErrnoException(errno, "getgroups");
1609 gid_t* groups = new gid_t[ngroups];
1611 ngroups = getgroups(ngroups, groups);
1613 if (ngroups == -1) {
1615 return ThrowErrnoException(errno, "getgroups");
1618 Local<Array> groups_list = Array::New(ngroups);
1619 bool seen_egid = false;
1620 gid_t egid = getegid();
1622 for (int i = 0; i < ngroups; i++) {
1623 groups_list->Set(i, Integer::New(groups[i], node_isolate));
1624 if (groups[i] == egid) seen_egid = true;
1629 if (seen_egid == false) {
1630 groups_list->Set(ngroups, Integer::New(egid, node_isolate));
1633 args.GetReturnValue().Set(groups_list);
1637 static void SetGroups(const FunctionCallbackInfo<Value>& args) {
1638 HandleScope scope(node_isolate);
1640 if (!args[0]->IsArray()) {
1641 return ThrowTypeError("argument 1 must be an array");
1644 Local<Array> groups_list = args[0].As<Array>();
1645 size_t size = groups_list->Length();
1646 gid_t* groups = new gid_t[size];
1648 for (size_t i = 0; i < size; i++) {
1649 gid_t gid = gid_by_name(groups_list->Get(i));
1651 if (gid == gid_not_found) {
1653 return ThrowError("group name not found");
1659 int rc = setgroups(size, groups);
1663 return ThrowErrnoException(errno, "setgroups");
1668 static void InitGroups(const FunctionCallbackInfo<Value>& args) {
1669 HandleScope scope(node_isolate);
1671 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1672 return ThrowTypeError("argument 1 must be a number or a string");
1675 if (!args[1]->IsUint32() && !args[1]->IsString()) {
1676 return ThrowTypeError("argument 2 must be a number or a string");
1679 String::Utf8Value arg0(args[0]);
1684 if (args[0]->IsUint32()) {
1685 user = name_by_uid(args[0]->Uint32Value());
1693 return ThrowError("initgroups user not found");
1696 extra_group = gid_by_name(args[1]);
1698 if (extra_group == gid_not_found) {
1699 if (must_free) free(user);
1700 return ThrowError("initgroups extra group not found");
1703 int rc = initgroups(user, extra_group);
1710 return ThrowErrnoException(errno, "initgroups");
1714 #endif // __POSIX__ && !defined(__ANDROID__)
1717 void Exit(const FunctionCallbackInfo<Value>& args) {
1718 HandleScope scope(node_isolate);
1719 exit(args[0]->IntegerValue());
1723 static void Uptime(const FunctionCallbackInfo<Value>& args) {
1724 HandleScope scope(node_isolate);
1726 if (uv_uptime(&uptime)) return;
1727 args.GetReturnValue().Set(uptime - prog_start_time);
1731 void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
1732 HandleScope scope(node_isolate);
1736 int err = uv_resident_set_memory(&rss);
1738 return ThrowUVException(err, "uv_resident_set_memory");
1741 Local<Object> info = Object::New();
1743 if (rss_symbol.IsEmpty()) {
1744 rss_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "rss");
1745 heap_total_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "heapTotal");
1746 heap_used_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "heapUsed");
1749 info->Set(rss_symbol, Number::New(rss));
1752 HeapStatistics v8_heap_stats;
1753 node_isolate->GetHeapStatistics(&v8_heap_stats);
1754 info->Set(heap_total_symbol,
1755 Integer::NewFromUnsigned(v8_heap_stats.total_heap_size(),
1757 info->Set(heap_used_symbol,
1758 Integer::NewFromUnsigned(v8_heap_stats.used_heap_size(),
1761 args.GetReturnValue().Set(info);
1765 void Kill(const FunctionCallbackInfo<Value>& args) {
1766 HandleScope scope(node_isolate);
1768 if (args.Length() != 2) {
1769 return ThrowError("Bad argument.");
1772 int pid = args[0]->IntegerValue();
1773 int sig = args[1]->Int32Value();
1774 int err = uv_kill(pid, sig);
1775 args.GetReturnValue().Set(err);
1778 // used in Hrtime() below
1779 #define NANOS_PER_SEC 1000000000
1781 // Hrtime exposes libuv's uv_hrtime() high-resolution timer.
1782 // The value returned by uv_hrtime() is a 64-bit int representing nanoseconds,
1783 // so this function instead returns an Array with 2 entries representing seconds
1784 // and nanoseconds, to avoid any integer overflow possibility.
1785 // Pass in an Array from a previous hrtime() call to instead get a time diff.
1786 void Hrtime(const FunctionCallbackInfo<Value>& args) {
1787 HandleScope scope(node_isolate);
1789 uint64_t t = uv_hrtime();
1791 if (args.Length() > 0) {
1792 // return a time diff tuple
1793 if (!args[0]->IsArray()) {
1794 return ThrowTypeError("process.hrtime() only accepts an Array tuple.");
1796 Local<Array> inArray = Local<Array>::Cast(args[0]);
1797 uint64_t seconds = inArray->Get(0)->Uint32Value();
1798 uint64_t nanos = inArray->Get(1)->Uint32Value();
1799 t -= (seconds * NANOS_PER_SEC) + nanos;
1802 Local<Array> tuple = Array::New(2);
1803 tuple->Set(0, Integer::NewFromUnsigned(t / NANOS_PER_SEC, node_isolate));
1804 tuple->Set(1, Integer::NewFromUnsigned(t % NANOS_PER_SEC, node_isolate));
1805 args.GetReturnValue().Set(tuple);
1809 typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);
1811 // DLOpen is process.dlopen(module, filename).
1812 // Used to load 'module.node' dynamically shared objects.
1813 void DLOpen(const FunctionCallbackInfo<Value>& args) {
1814 HandleScope scope(node_isolate);
1815 char symbol[1024], *base, *pos;
1819 if (args.Length() < 2) {
1820 return ThrowError("process.dlopen takes exactly 2 arguments.");
1823 Local<Object> module = args[0]->ToObject(); // Cast
1824 String::Utf8Value filename(args[1]); // Cast
1826 if (exports_symbol.IsEmpty()) {
1827 exports_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "exports");
1829 Local<Object> exports = module->Get(exports_symbol)->ToObject();
1831 if (uv_dlopen(*filename, &lib)) {
1832 Local<String> errmsg = OneByteString(node_isolate, uv_dlerror(&lib));
1834 // Windows needs to add the filename into the error message
1835 errmsg = String::Concat(errmsg, args[1]->ToString());
1837 ThrowException(Exception::Error(errmsg));
1841 String::Utf8Value path(args[1]);
1844 /* Find the shared library filename within the full path. */
1846 pos = strrchr(base, '/');
1852 pos = strpbrk(base, "\\/:");
1860 /* Strip the .node extension. */
1861 pos = strrchr(base, '.');
1866 /* Add the `_module` suffix to the extension name. */
1867 r = snprintf(symbol, sizeof symbol, "%s_module", base);
1868 if (r <= 0 || static_cast<size_t>(r) >= sizeof symbol) {
1869 return ThrowError("Out of memory.");
1872 /* Replace dashes with underscores. When loading foo-bar.node,
1873 * look for foo_bar_module, not foo-bar_module.
1875 for (pos = symbol; *pos != '\0'; ++pos) {
1876 if (*pos == '-') *pos = '_';
1879 node_module_struct *mod;
1880 if (uv_dlsym(&lib, symbol, reinterpret_cast<void**>(&mod))) {
1882 snprintf(errmsg, sizeof(errmsg), "Symbol %s not found.", symbol);
1883 return ThrowError(errmsg);
1886 if (mod->version != NODE_MODULE_VERSION) {
1890 "Module version mismatch. Expected %d, got %d.",
1891 NODE_MODULE_VERSION, mod->version);
1892 return ThrowError(errmsg);
1895 // Execute the C++ module
1896 mod->register_func(exports, module);
1898 // Tell coverity that 'handle' should not be freed when we return.
1899 // coverity[leaked_storage]
1903 static void OnFatalError(const char* location, const char* message) {
1905 fprintf(stderr, "FATAL ERROR: %s %s\n", location, message);
1907 fprintf(stderr, "FATAL ERROR: %s\n", message);
1917 NO_RETURN void FatalError(const char* location, const char* message) {
1918 OnFatalError(location, message);
1919 // to supress compiler warning
1924 void FatalException(Handle<Value> error, Handle<Message> message) {
1925 HandleScope scope(node_isolate);
1927 if (fatal_exception_symbol.IsEmpty()) {
1928 fatal_exception_symbol =
1929 FIXED_ONE_BYTE_STRING(node_isolate, "_fatalException");
1932 Local<Object> process = PersistentToLocal(node_isolate, process_p);
1933 Local<Value> fatal_v = process->Get(fatal_exception_symbol);
1935 if (!fatal_v->IsFunction()) {
1936 // failed before the process._fatalException function was added!
1937 // this is probably pretty bad. Nothing to do but report and exit.
1938 ReportException(error, message);
1942 Local<Function> fatal_f = Local<Function>::Cast(fatal_v);
1944 TryCatch fatal_try_catch;
1946 // Do not call FatalException when _fatalException handler throws
1947 fatal_try_catch.SetVerbose(false);
1949 // this will return true if the JS layer handled it, false otherwise
1950 Local<Value> caught = fatal_f->Call(process, 1, &error);
1952 if (fatal_try_catch.HasCaught()) {
1953 // the fatal exception function threw, so we must exit
1954 ReportException(fatal_try_catch);
1958 if (false == caught->BooleanValue()) {
1959 ReportException(error, message);
1965 void FatalException(const TryCatch& try_catch) {
1966 HandleScope scope(node_isolate);
1967 // TODO(bajtos) do not call FatalException if try_catch is verbose
1968 // (requires V8 API to expose getter for try_catch.is_verbose_)
1969 FatalException(try_catch.Exception(), try_catch.Message());
1973 void OnMessage(Handle<Message> message, Handle<Value> error) {
1974 // The current version of V8 sends messages for errors only
1975 // (thus `error` is always set).
1976 FatalException(error, message);
1980 static void Binding(const FunctionCallbackInfo<Value>& args) {
1981 HandleScope scope(node_isolate);
1983 Local<String> module = args[0]->ToString();
1984 String::Utf8Value module_v(module);
1985 node_module_struct* modp;
1987 Local<Object> cache = PersistentToLocal(node_isolate, binding_cache);
1988 Local<Object> exports;
1990 if (cache->Has(module)) {
1991 exports = cache->Get(module)->ToObject();
1992 args.GetReturnValue().Set(exports);
1996 // Append a string to process.moduleLoadList
1998 snprintf(buf, sizeof(buf), "Binding %s", *module_v);
2000 Local<Array> modules = PersistentToLocal(node_isolate, module_load_list);
2001 uint32_t l = modules->Length();
2002 modules->Set(l, OneByteString(node_isolate, buf));
2004 if ((modp = get_builtin_module(*module_v)) != NULL) {
2005 exports = Object::New();
2006 // Internal bindings don't have a "module" object,
2008 modp->register_func(exports, Undefined(node_isolate));
2009 cache->Set(module, exports);
2010 } else if (!strcmp(*module_v, "constants")) {
2011 exports = Object::New();
2012 DefineConstants(exports);
2013 cache->Set(module, exports);
2014 } else if (!strcmp(*module_v, "natives")) {
2015 exports = Object::New();
2016 DefineJavaScript(exports);
2017 cache->Set(module, exports);
2019 return ThrowError("No such module");
2022 args.GetReturnValue().Set(exports);
2026 static void ProcessTitleGetter(Local<String> property,
2027 const PropertyCallbackInfo<Value>& info) {
2028 HandleScope scope(node_isolate);
2030 uv_get_process_title(buffer, sizeof(buffer));
2031 info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, buffer));
2035 static void ProcessTitleSetter(Local<String> property,
2037 const PropertyCallbackInfo<void>& info) {
2038 HandleScope scope(node_isolate);
2039 String::Utf8Value title(value);
2040 // TODO(piscisaureus): protect with a lock
2041 uv_set_process_title(*title);
2045 static void EnvGetter(Local<String> property,
2046 const PropertyCallbackInfo<Value>& info) {
2047 HandleScope scope(node_isolate);
2049 String::Utf8Value key(property);
2050 const char* val = getenv(*key);
2052 return info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, val));
2055 String::Value key(property);
2056 WCHAR buffer[32767]; // The maximum size allowed for environment variables.
2057 DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
2059 ARRAY_SIZE(buffer));
2060 // If result >= sizeof buffer the buffer was too small. That should never
2061 // happen. If result == 0 and result != ERROR_SUCCESS the variable was not
2063 if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
2064 result < ARRAY_SIZE(buffer)) {
2065 const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
2066 Local<String> rc = String::NewFromTwoByte(node_isolate, two_byte_buffer);
2067 return info.GetReturnValue().Set(rc);
2070 // Not found. Fetch from prototype.
2071 info.GetReturnValue().Set(
2072 info.Data().As<Object>()->Get(property));
2076 static void EnvSetter(Local<String> property,
2078 const PropertyCallbackInfo<Value>& info) {
2079 HandleScope scope(node_isolate);
2081 String::Utf8Value key(property);
2082 String::Utf8Value val(value);
2083 setenv(*key, *val, 1);
2085 String::Value key(property);
2086 String::Value val(value);
2087 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2088 // Environment variables that start with '=' are read-only.
2089 if (key_ptr[0] != L'=') {
2090 SetEnvironmentVariableW(key_ptr, reinterpret_cast<WCHAR*>(*val));
2093 // Whether it worked or not, always return rval.
2094 info.GetReturnValue().Set(value);
2098 static void EnvQuery(Local<String> property,
2099 const PropertyCallbackInfo<Integer>& info) {
2100 HandleScope scope(node_isolate);
2101 int32_t rc = -1; // Not found unless proven otherwise.
2103 String::Utf8Value key(property);
2104 if (getenv(*key)) rc = 0;
2106 String::Value key(property);
2107 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2108 if (GetEnvironmentVariableW(key_ptr, NULL, 0) > 0 ||
2109 GetLastError() == ERROR_SUCCESS) {
2111 if (key_ptr[0] == L'=') {
2112 // Environment variables that start with '=' are hidden and read-only.
2113 rc = static_cast<int32_t>(v8::ReadOnly) |
2114 static_cast<int32_t>(v8::DontDelete) |
2115 static_cast<int32_t>(v8::DontEnum);
2119 if (rc != -1) info.GetReturnValue().Set(rc);
2123 static void EnvDeleter(Local<String> property,
2124 const PropertyCallbackInfo<Boolean>& info) {
2125 HandleScope scope(node_isolate);
2128 String::Utf8Value key(property);
2129 rc = getenv(*key) != NULL;
2130 if (rc) unsetenv(*key);
2132 String::Value key(property);
2133 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2134 if (key_ptr[0] == L'=' || !SetEnvironmentVariableW(key_ptr, NULL)) {
2135 // Deletion failed. Return true if the key wasn't there in the first place,
2136 // false if it is still there.
2137 rc = GetEnvironmentVariableW(key_ptr, NULL, NULL) == 0 &&
2138 GetLastError() != ERROR_SUCCESS;
2141 info.GetReturnValue().Set(rc);
2145 static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
2146 HandleScope scope(node_isolate);
2149 while (environ[size]) size++;
2151 Local<Array> env = Array::New(size);
2153 for (int i = 0; i < size; ++i) {
2154 const char* var = environ[i];
2155 const char* s = strchr(var, '=');
2156 const int length = s ? s - var : strlen(var);
2157 Local<String> name = String::NewFromUtf8(node_isolate,
2159 String::kNormalString,
2164 WCHAR* environment = GetEnvironmentStringsW();
2165 if (environment == NULL) return; // This should not happen.
2166 Local<Array> env = Array::New();
2167 WCHAR* p = environment;
2169 while (*p != NULL) {
2172 // If the key starts with '=' it is a hidden environment variable.
2176 s = wcschr(p, L'=');
2181 const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
2182 const size_t two_byte_buffer_len = s - p;
2183 Local<String> value = String::NewFromTwoByte(node_isolate,
2185 String::kNormalString,
2186 two_byte_buffer_len);
2187 env->Set(i++, value);
2188 p = s + wcslen(s) + 1;
2190 FreeEnvironmentStringsW(environment);
2193 info.GetReturnValue().Set(env);
2197 static Handle<Object> GetFeatures() {
2198 HandleScope scope(node_isolate);
2200 Local<Object> obj = Object::New();
2201 #if defined(DEBUG) && DEBUG
2202 Local<Value> debug = True(node_isolate);
2204 Local<Value> debug = False(node_isolate);
2205 #endif // defined(DEBUG) && DEBUG
2207 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "debug"), debug);
2209 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "uv"), True(node_isolate));
2210 // TODO(bnoordhuis) ping libuv
2211 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "ipv6"), True(node_isolate));
2213 #ifdef OPENSSL_NPN_NEGOTIATED
2214 Local<Boolean> tls_npn = True(node_isolate);
2216 Local<Boolean> tls_npn = False(node_isolate);
2218 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_npn"), tls_npn);
2220 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
2221 Local<Boolean> tls_sni = True(node_isolate);
2223 Local<Boolean> tls_sni = False(node_isolate);
2225 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_sni"), tls_sni);
2227 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls"),
2228 Boolean::New(get_builtin_module("crypto") != NULL));
2230 return scope.Close(obj);
2234 static void DebugPortGetter(Local<String> property,
2235 const PropertyCallbackInfo<Value>& info) {
2236 HandleScope scope(node_isolate);
2237 info.GetReturnValue().Set(debug_port);
2241 static void DebugPortSetter(Local<String> property,
2243 const PropertyCallbackInfo<void>& info) {
2244 HandleScope scope(node_isolate);
2245 debug_port = value->NumberValue();
2249 static void DebugProcess(const FunctionCallbackInfo<Value>& args);
2250 static void DebugPause(const FunctionCallbackInfo<Value>& args);
2251 static void DebugEnd(const FunctionCallbackInfo<Value>& args);
2254 void NeedImmediateCallbackGetter(Local<String> property,
2255 const PropertyCallbackInfo<Value>& info) {
2256 info.GetReturnValue().Set(need_immediate_cb);
2260 static void NeedImmediateCallbackSetter(Local<String> property,
2262 const PropertyCallbackInfo<void>&) {
2263 HandleScope scope(node_isolate);
2265 bool bool_value = value->BooleanValue();
2267 if (need_immediate_cb == bool_value) return;
2269 need_immediate_cb = bool_value;
2271 if (need_immediate_cb) {
2272 uv_check_start(&check_immediate_watcher, node::CheckImmediate);
2273 // idle handle is needed only to maintain event loop
2274 uv_idle_start(&idle_immediate_dummy, node::IdleImmediateDummy);
2276 uv_check_stop(&check_immediate_watcher);
2277 uv_idle_stop(&idle_immediate_dummy);
2282 #define READONLY_PROPERTY(obj, str, var) \
2284 obj->Set(OneByteString(node_isolate, str), var, v8::ReadOnly); \
2288 Handle<Object> SetupProcessObject(int argc, char *argv[]) {
2289 HandleScope scope(node_isolate);
2293 Local<FunctionTemplate> process_template = FunctionTemplate::New();
2294 process_template->SetClassName(
2295 FIXED_ONE_BYTE_STRING(node_isolate, "process"));
2297 Local<Object> process = process_template->GetFunction()->NewInstance();
2298 assert(process.IsEmpty() == false);
2299 assert(process->IsObject() == true);
2301 process_p.Reset(node_isolate, process);
2303 process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "title"),
2305 ProcessTitleSetter);
2308 READONLY_PROPERTY(process,
2310 FIXED_ONE_BYTE_STRING(node_isolate, NODE_VERSION));
2312 // process.moduleLoadList
2313 Local<Array> modules = Array::New();
2314 module_load_list.Reset(node_isolate, modules);
2315 READONLY_PROPERTY(process, "moduleLoadList", modules);
2318 Local<Object> versions = Object::New();
2319 READONLY_PROPERTY(process, "versions", versions);
2321 const char http_parser_version[] = NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR)
2323 NODE_STRINGIFY(HTTP_PARSER_VERSION_MINOR);
2324 READONLY_PROPERTY(versions,
2326 FIXED_ONE_BYTE_STRING(node_isolate, http_parser_version));
2328 // +1 to get rid of the leading 'v'
2329 READONLY_PROPERTY(versions,
2331 OneByteString(node_isolate, NODE_VERSION + 1));
2332 READONLY_PROPERTY(versions,
2334 OneByteString(node_isolate, V8::GetVersion()));
2335 READONLY_PROPERTY(versions,
2337 OneByteString(node_isolate, uv_version_string()));
2338 READONLY_PROPERTY(versions,
2340 FIXED_ONE_BYTE_STRING(node_isolate, ZLIB_VERSION));
2342 const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);
2343 READONLY_PROPERTY(versions,
2345 FIXED_ONE_BYTE_STRING(node_isolate, node_modules_version));
2348 // Stupid code to slice out the version string.
2349 int c, l = strlen(OPENSSL_VERSION_TEXT);
2350 for (i = j = 0; i < l; i++) {
2351 c = OPENSSL_VERSION_TEXT[i];
2352 if ('0' <= c && c <= '9') {
2353 for (j = i + 1; j < l; j++) {
2354 c = OPENSSL_VERSION_TEXT[j];
2355 if (c == ' ') break;
2363 OneByteString(node_isolate, &OPENSSL_VERSION_TEXT[i], j - i));
2369 READONLY_PROPERTY(process, "arch", OneByteString(node_isolate, ARCH));
2372 READONLY_PROPERTY(process,
2374 OneByteString(node_isolate, PLATFORM));
2377 Local<Array> arguments = Array::New(argc - option_end_index + 1);
2378 arguments->Set(0, String::NewFromUtf8(node_isolate, argv[0]));
2379 for (j = 1, i = option_end_index; i < argc; j++, i++) {
2380 arguments->Set(j, String::NewFromUtf8(node_isolate, argv[i]));
2382 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "argv"), arguments);
2385 Local<Array> exec_argv = Array::New(option_end_index - 1);
2386 for (j = 1, i = 0; j < option_end_index; j++, i++) {
2387 exec_argv->Set(i, String::NewFromUtf8(node_isolate, argv[j]));
2389 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execArgv"), exec_argv);
2391 // create process.env
2392 Local<ObjectTemplate> envTemplate = ObjectTemplate::New();
2393 envTemplate->SetNamedPropertyHandler(EnvGetter,
2399 Local<Object> env = envTemplate->NewInstance();
2400 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "env"), env);
2402 READONLY_PROPERTY(process, "pid", Integer::New(getpid(), node_isolate));
2403 READONLY_PROPERTY(process, "features", GetFeatures());
2404 process->SetAccessor(
2405 FIXED_ONE_BYTE_STRING(node_isolate, "_needImmediateCallback"),
2406 NeedImmediateCallbackGetter,
2407 NeedImmediateCallbackSetter);
2411 READONLY_PROPERTY(process,
2413 String::NewFromUtf8(node_isolate, eval_string));
2418 READONLY_PROPERTY(process, "_print_eval", True(node_isolate));
2421 // -i, --interactive
2423 READONLY_PROPERTY(process, "_forceRepl", True(node_isolate));
2427 if (no_deprecation) {
2428 READONLY_PROPERTY(process, "noDeprecation", True(node_isolate));
2431 // --throw-deprecation
2432 if (throw_deprecation) {
2433 READONLY_PROPERTY(process, "throwDeprecation", True(node_isolate));
2436 // --trace-deprecation
2437 if (trace_deprecation) {
2438 READONLY_PROPERTY(process, "traceDeprecation", True(node_isolate));
2441 size_t exec_path_len = 2 * PATH_MAX;
2442 char* exec_path = new char[exec_path_len];
2443 Local<String> exec_path_value;
2444 if (uv_exepath(exec_path, &exec_path_len) == 0) {
2445 exec_path_value = String::NewFromUtf8(node_isolate,
2447 String::kNormalString,
2450 exec_path_value = String::NewFromUtf8(node_isolate, argv[0]);
2452 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execPath"),
2456 process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "debugPort"),
2460 // define various internal methods
2461 NODE_SET_METHOD(process, "_getActiveRequests", GetActiveRequests);
2462 NODE_SET_METHOD(process, "_getActiveHandles", GetActiveHandles);
2463 NODE_SET_METHOD(process, "reallyExit", Exit);
2464 NODE_SET_METHOD(process, "abort", Abort);
2465 NODE_SET_METHOD(process, "chdir", Chdir);
2466 NODE_SET_METHOD(process, "cwd", Cwd);
2468 NODE_SET_METHOD(process, "umask", Umask);
2470 #if defined(__POSIX__) && !defined(__ANDROID__)
2471 NODE_SET_METHOD(process, "getuid", GetUid);
2472 NODE_SET_METHOD(process, "setuid", SetUid);
2474 NODE_SET_METHOD(process, "setgid", SetGid);
2475 NODE_SET_METHOD(process, "getgid", GetGid);
2477 NODE_SET_METHOD(process, "getgroups", GetGroups);
2478 NODE_SET_METHOD(process, "setgroups", SetGroups);
2479 NODE_SET_METHOD(process, "initgroups", InitGroups);
2480 #endif // __POSIX__ && !defined(__ANDROID__)
2482 NODE_SET_METHOD(process, "_kill", Kill);
2484 NODE_SET_METHOD(process, "_debugProcess", DebugProcess);
2485 NODE_SET_METHOD(process, "_debugPause", DebugPause);
2486 NODE_SET_METHOD(process, "_debugEnd", DebugEnd);
2488 NODE_SET_METHOD(process, "hrtime", Hrtime);
2490 NODE_SET_METHOD(process, "dlopen", DLOpen);
2492 NODE_SET_METHOD(process, "uptime", Uptime);
2493 NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
2495 NODE_SET_METHOD(process, "binding", Binding);
2497 NODE_SET_METHOD(process, "_setupDomainUse", SetupDomainUse);
2499 // values use to cross communicate with processNextTick
2500 Local<Object> info_box = Object::New();
2501 info_box->SetIndexedPropertiesToExternalArrayData(&tick_infobox,
2502 kExternalUnsignedIntArray,
2504 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickInfoBox"), info_box);
2506 // pre-set _events object for faster emit checks
2507 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_events"), Object::New());
2509 return scope.Close(process);
2513 #undef READONLY_PROPERTY
2516 static void AtExit() {
2517 uv_tty_reset_mode();
2521 static void SignalExit(int signal) {
2522 uv_tty_reset_mode();
2523 _exit(128 + signal);
2527 void Load(Handle<Object> process_l) {
2528 HandleScope handle_scope(node_isolate);
2530 process_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "process");
2531 domain_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "domain");
2533 // Compile, execute the src/node.js file. (Which was included as static C
2534 // string in node_natives.h. 'natve_node' is the string containing that
2537 // The node.js file returns a function 'f'
2542 // Disable verbose mode to stop FatalException() handler from trying
2543 // to handle the exception. Errors this early in the start-up phase
2544 // are not safe to ignore.
2545 try_catch.SetVerbose(false);
2547 Local<String> script_name = FIXED_ONE_BYTE_STRING(node_isolate, "node.js");
2548 Local<Value> f_value = ExecuteString(MainSource(), script_name);
2549 if (try_catch.HasCaught()) {
2550 ReportException(try_catch);
2553 assert(f_value->IsFunction());
2554 Local<Function> f = Local<Function>::Cast(f_value);
2556 // Now we call 'f' with the 'process' variable that we've built up with
2557 // all our bindings. Inside node.js we'll take care of assigning things to
2560 // We start the process this way in order to be more modular. Developers
2561 // who do not like how 'src/node.js' setups the module system but do like
2562 // Node's I/O bindings may want to replace 'f' with their own function.
2564 // Add a reference to the global object
2565 Local<Object> global = v8::Context::GetCurrent()->Global();
2567 #if defined HAVE_DTRACE || defined HAVE_ETW || defined HAVE_SYSTEMTAP
2571 #if defined HAVE_PERFCTR
2572 InitPerfCounters(global);
2575 // Enable handling of uncaught exceptions
2576 // (FatalException(), break on uncaught exception in debugger)
2578 // This is not strictly necessary since it's almost impossible
2579 // to attach the debugger fast enought to break on exception
2580 // thrown during process startup.
2581 try_catch.SetVerbose(true);
2583 Local<Value> arg = process_l;
2584 f->Call(global, 1, &arg);
2587 static void PrintHelp();
2589 static void ParseDebugOpt(const char* arg) {
2592 if (strstr(arg, "--debug-port=") == arg) {
2593 p = 1 + strchr(arg, '=');
2594 debug_port = atoi(p);
2596 use_debug_agent = true;
2597 if (!strcmp(arg, "--debug-brk")) {
2598 debug_wait_connect = true;
2600 } else if (!strcmp(arg, "--debug")) {
2602 } else if (strstr(arg, "--debug-brk=") == arg) {
2603 debug_wait_connect = true;
2604 p = 1 + strchr(arg, '=');
2605 debug_port = atoi(p);
2606 } else if (strstr(arg, "--debug=") == arg) {
2607 p = 1 + strchr(arg, '=');
2608 debug_port = atoi(p);
2611 if (p && debug_port > 1024 && debug_port < 65536)
2614 fprintf(stderr, "Bad debug option.\n");
2615 if (p) fprintf(stderr, "Debug port must be in range 1025 to 65535.\n");
2621 static void PrintHelp() {
2622 printf("Usage: node [options] [ -e script | script.js ] [arguments] \n"
2623 " node debug script.js [arguments] \n"
2626 " -v, --version print node's version\n"
2627 " -e, --eval script evaluate script\n"
2628 " -p, --print evaluate script and print result\n"
2629 " -i, --interactive always enter the REPL even if stdin\n"
2630 " does not appear to be a terminal\n"
2631 " --no-deprecation silence deprecation warnings\n"
2632 " --trace-deprecation show stack traces on deprecations\n"
2633 " --v8-options print v8 command line options\n"
2634 " --max-stack-size=val set max v8 stack size (bytes)\n"
2636 "Environment variables:\n"
2638 "NODE_PATH ';'-separated list of directories\n"
2640 "NODE_PATH ':'-separated list of directories\n"
2642 " prefixed to the module search path.\n"
2643 "NODE_MODULE_CONTEXTS Set to 1 to load modules in their own\n"
2644 " global contexts.\n"
2645 "NODE_DISABLE_COLORS Set to 1 to disable colors in the REPL\n"
2647 "Documentation can be found at http://nodejs.org/\n");
2651 // Parse node command line arguments.
2652 static void ParseArgs(int argc, char **argv) {
2655 // TODO(bnoordhuis) use parse opts
2656 for (i = 1; i < argc; i++) {
2657 const char *arg = argv[i];
2658 if (strstr(arg, "--debug") == arg) {
2660 argv[i] = const_cast<char*>("");
2661 } else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
2662 printf("%s\n", NODE_VERSION);
2664 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
2667 } else if (strcmp(arg, "--eval") == 0 ||
2668 strcmp(arg, "-e") == 0 ||
2669 strcmp(arg, "--print") == 0 ||
2670 strcmp(arg, "-pe") == 0 ||
2671 strcmp(arg, "-p") == 0) {
2672 bool is_eval = strchr(arg, 'e') != NULL;
2673 bool is_print = strchr(arg, 'p') != NULL;
2675 // argument to -p and --print is optional
2676 if (is_eval == true && i + 1 >= argc) {
2677 fprintf(stderr, "Error: %s requires an argument\n", arg);
2681 print_eval = print_eval || is_print;
2682 argv[i] = const_cast<char*>("");
2684 // --eval, -e and -pe always require an argument
2685 if (is_eval == true) {
2686 eval_string = argv[++i];
2690 // next arg is the expression to evaluate unless it starts with:
2691 // - a dash, then it's another switch
2692 // - "\\-", then it's an escaped expression, drop the backslash
2693 if (argv[i + 1] == NULL) continue;
2694 if (argv[i + 1][0] == '-') continue;
2695 eval_string = argv[++i];
2696 if (strncmp(eval_string, "\\-", 2) == 0) ++eval_string;
2697 } else if (strcmp(arg, "--interactive") == 0 || strcmp(arg, "-i") == 0) {
2699 argv[i] = const_cast<char*>("");
2700 } else if (strcmp(arg, "--v8-options") == 0) {
2701 argv[i] = const_cast<char*>("--help");
2702 } else if (strcmp(arg, "--no-deprecation") == 0) {
2703 argv[i] = const_cast<char*>("");
2704 no_deprecation = true;
2705 } else if (strcmp(arg, "--trace-deprecation") == 0) {
2706 argv[i] = const_cast<char*>("");
2707 trace_deprecation = true;
2708 } else if (strcmp(arg, "--throw-deprecation") == 0) {
2709 argv[i] = const_cast<char*>("");
2710 throw_deprecation = true;
2711 } else if (argv[i][0] != '-') {
2716 option_end_index = i;
2720 // Called from the main thread.
2721 static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle, int status) {
2722 v8::Debug::ProcessDebugMessages();
2726 // Called from V8 Debug Agent TCP thread.
2727 static void DispatchMessagesDebugAgentCallback() {
2728 uv_async_send(&dispatch_debug_messages_async);
2732 // Called from the main thread
2733 static void EmitDebugEnabledAsyncCallback(uv_async_t* handle, int status) {
2734 HandleScope handle_scope(node_isolate);
2735 Local<Object> obj = Object::New();
2736 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "cmd"),
2737 FIXED_ONE_BYTE_STRING(node_isolate, "NODE_DEBUG_ENABLED"));
2738 Local<Value> args[] = {
2739 FIXED_ONE_BYTE_STRING(node_isolate, "internalMessage"),
2742 MakeCallback(process_p, "emit", ARRAY_SIZE(args), args);
2746 // Called from the signal watcher callback
2747 static void EmitDebugEnabled() {
2748 uv_async_send(&emit_debug_enabled_async);
2752 static void EnableDebug(bool wait_connect) {
2753 // If we're called from another thread, make sure to enter the right
2755 node_isolate->Enter();
2757 v8::Debug::SetDebugMessageDispatchHandler(DispatchMessagesDebugAgentCallback,
2760 // Start the debug thread and it's associated TCP server on port 5858.
2761 bool r = v8::Debug::EnableAgent("node " NODE_VERSION,
2765 // Crappy check that everything went well. FIXME
2768 // Print out some information.
2769 fprintf(stderr, "debugger listening on port %d\n", debug_port);
2772 debugger_running = true;
2774 // Do not emit NODE_DEBUG_ENABLED when debugger is enabled before starting
2775 // the main process (i.e. when called via `node --debug`)
2776 if (!process_p.IsEmpty())
2779 node_isolate->Exit();
2784 static void EnableDebugSignalHandler(uv_signal_t* handle, int) {
2785 // Break once process will return execution to v8
2786 v8::Debug::DebugBreak(node_isolate);
2788 if (!debugger_running) {
2789 fprintf(stderr, "Hit SIGUSR1 - starting debugger agent.\n");
2795 static void RegisterSignalHandler(int signal, void (*handler)(int signal)) {
2796 struct sigaction sa;
2798 memset(&sa, 0, sizeof(sa));
2799 sa.sa_handler = handler;
2800 sigfillset(&sa.sa_mask);
2801 sigaction(signal, &sa, NULL);
2805 void DebugProcess(const FunctionCallbackInfo<Value>& args) {
2806 HandleScope scope(node_isolate);
2808 if (args.Length() != 1) {
2809 return ThrowError("Invalid number of arguments.");
2815 pid = args[0]->IntegerValue();
2816 r = kill(pid, SIGUSR1);
2818 return ThrowErrnoException(errno, "kill");
2825 DWORD WINAPI EnableDebugThreadProc(void* arg) {
2826 // Break once process will return execution to v8
2827 if (!debugger_running) {
2828 for (int i = 0; i < 1; i++) {
2829 fprintf(stderr, "Starting debugger agent.\r\n");
2835 v8::Debug::DebugBreak(node_isolate);
2841 static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
2843 return _snwprintf(buf, buf_len, L"node-debug-handler-%u", pid);
2847 static int RegisterDebugSignalHandler() {
2848 wchar_t mapping_name[32];
2849 HANDLE mapping_handle;
2851 LPTHREAD_START_ROUTINE* handler;
2853 pid = GetCurrentProcessId();
2855 if (GetDebugSignalHandlerMappingName(pid,
2857 ARRAY_SIZE(mapping_name)) < 0) {
2861 mapping_handle = CreateFileMappingW(INVALID_HANDLE_VALUE,
2867 if (mapping_handle == NULL) {
2871 handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
2872 MapViewOfFile(mapping_handle,
2873 FILE_MAP_ALL_ACCESS,
2877 if (handler == NULL) {
2878 CloseHandle(mapping_handle);
2882 *handler = EnableDebugThreadProc;
2884 UnmapViewOfFile(static_cast<void*>(handler));
2890 static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
2891 HandleScope scope(node_isolate);
2893 HANDLE process = NULL;
2894 HANDLE thread = NULL;
2895 HANDLE mapping = NULL;
2896 wchar_t mapping_name[32];
2897 LPTHREAD_START_ROUTINE* handler = NULL;
2899 if (args.Length() != 1) {
2900 ThrowError("Invalid number of arguments.");
2904 pid = (DWORD) args[0]->IntegerValue();
2906 process = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION |
2907 PROCESS_VM_OPERATION | PROCESS_VM_WRITE |
2911 if (process == NULL) {
2912 ThrowException(WinapiErrnoException(GetLastError(), "OpenProcess"));
2916 if (GetDebugSignalHandlerMappingName(pid,
2918 ARRAY_SIZE(mapping_name)) < 0) {
2919 ThrowErrnoException(errno, "sprintf");
2923 mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name);
2924 if (mapping == NULL) {
2925 ThrowException(WinapiErrnoException(GetLastError(), "OpenFileMappingW"));
2929 handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
2930 MapViewOfFile(mapping,
2935 if (handler == NULL || *handler == NULL) {
2936 ThrowException(WinapiErrnoException(GetLastError(), "MapViewOfFile"));
2940 thread = CreateRemoteThread(process,
2947 if (thread == NULL) {
2948 ThrowException(WinapiErrnoException(GetLastError(), "CreateRemoteThread"));
2952 // Wait for the thread to terminate
2953 if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) {
2954 ThrowException(WinapiErrnoException(GetLastError(), "WaitForSingleObject"));
2959 if (process != NULL)
2960 CloseHandle(process);
2962 CloseHandle(thread);
2963 if (handler != NULL)
2964 UnmapViewOfFile(handler);
2965 if (mapping != NULL)
2966 CloseHandle(mapping);
2971 static void DebugPause(const FunctionCallbackInfo<Value>& args) {
2972 v8::Debug::DebugBreak(node_isolate);
2976 static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
2977 if (debugger_running) {
2978 v8::Debug::DisableAgent();
2979 debugger_running = false;
2984 char** Init(int argc, char *argv[]) {
2985 // Initialize prog_start_time to get relative uptime.
2986 uv_uptime(&prog_start_time);
2988 // Make inherited handles noninheritable.
2989 uv_disable_stdio_inheritance();
2991 // init async debug messages dispatching
2992 uv_async_init(uv_default_loop(),
2993 &dispatch_debug_messages_async,
2994 DispatchDebugMessagesAsyncCallback);
2995 uv_unref(reinterpret_cast<uv_handle_t*>(&dispatch_debug_messages_async));
2997 // init async NODE_DEBUG_ENABLED emitter
2998 uv_async_init(uv_default_loop(),
2999 &emit_debug_enabled_async,
3000 EmitDebugEnabledAsyncCallback);
3001 uv_unref(reinterpret_cast<uv_handle_t*>(&emit_debug_enabled_async));
3003 // Parse a few arguments which are specific to Node.
3004 node::ParseArgs(argc, argv);
3005 // Parse the rest of the args (up to the 'option_end_index' (where '--' was
3006 // in the command line))
3007 int v8argc = option_end_index;
3008 char **v8argv = argv;
3010 if (debug_wait_connect) {
3011 // v8argv is a copy of argv up to the script file argument +2 if --debug-brk
3012 // to expose the v8 debugger js object so that node.js can set
3013 // a breakpoint on the first line of the startup script
3015 v8argv = new char*[v8argc];
3016 memcpy(v8argv, argv, sizeof(*argv) * option_end_index);
3017 v8argv[option_end_index] = const_cast<char*>("--expose_debug_as");
3018 v8argv[option_end_index + 1] = const_cast<char*>("v8debug");
3021 V8::SetFlagsFromCommandLine(&v8argc, v8argv, false);
3023 const char typed_arrays_flag[] = "--harmony_typed_arrays";
3024 V8::SetFlagsFromString(typed_arrays_flag, sizeof(typed_arrays_flag) - 1);
3025 V8::SetArrayBufferAllocator(&ArrayBufferAllocator::the_singleton);
3027 // Fetch a reference to the main isolate, so we have a reference to it
3028 // even when we need it to access it from another (debugger) thread.
3029 node_isolate = Isolate::GetCurrent();
3033 RegisterSignalHandler(SIGPIPE, SIG_IGN);
3034 RegisterSignalHandler(SIGINT, SignalExit);
3035 RegisterSignalHandler(SIGTERM, SignalExit);
3038 uv_check_init(uv_default_loop(), &check_immediate_watcher);
3039 uv_unref(reinterpret_cast<uv_handle_t*>(&check_immediate_watcher));
3040 uv_idle_init(uv_default_loop(), &idle_immediate_dummy);
3042 V8::SetFatalErrorHandler(node::OnFatalError);
3043 V8::AddMessageListener(OnMessage);
3045 // If the --debug flag was specified then initialize the debug thread.
3046 if (use_debug_agent) {
3047 EnableDebug(debug_wait_connect);
3050 RegisterDebugSignalHandler();
3052 static uv_signal_t signal_watcher;
3053 uv_signal_init(uv_default_loop(), &signal_watcher);
3054 uv_signal_start(&signal_watcher, EnableDebugSignalHandler, SIGUSR1);
3055 uv_unref(reinterpret_cast<uv_handle_t*>(&signal_watcher));
3063 struct AtExitCallback {
3064 AtExitCallback* next_;
3065 void (*cb_)(void* arg);
3069 static AtExitCallback* at_exit_functions_;
3073 AtExitCallback* p = at_exit_functions_;
3074 at_exit_functions_ = NULL;
3077 AtExitCallback* q = p->next_;
3085 void AtExit(void (*cb)(void* arg), void* arg) {
3086 AtExitCallback* p = new AtExitCallback;
3089 p->next_ = at_exit_functions_;
3090 at_exit_functions_ = p;
3094 void EmitExit(v8::Handle<v8::Object> process_l) {
3095 // process.emit('exit')
3096 process_l->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_exiting"),
3097 True(node_isolate));
3098 Local<Value> args[] = {
3099 FIXED_ONE_BYTE_STRING(node_isolate, "exit"),
3100 Integer::New(0, node_isolate)
3102 MakeCallback(process_l, "emit", ARRAY_SIZE(args), args);
3105 static char **copy_argv(int argc, char **argv) {
3113 for (i = 0; i < argc; i++) {
3114 strlen_sum += strlen(argv[i]) + 1;
3117 argv_copy = static_cast<char**>(
3118 malloc(sizeof(*argv_copy) * (argc + 1) + strlen_sum));
3123 argv_data = reinterpret_cast<char*>(argv_copy) +
3124 sizeof(*argv_copy) * (argc + 1);
3126 for (i = 0; i < argc; i++) {
3127 argv_copy[i] = argv_data;
3128 len = strlen(argv[i]) + 1;
3129 memcpy(argv_data, argv[i], len);
3133 argv_copy[argc] = NULL;
3138 int Start(int argc, char *argv[]) {
3139 // Hack aroung with the argv pointer. Used for process.title = "blah".
3140 argv = uv_setup_args(argc, argv);
3142 // Logic to duplicate argv as Init() modifies arguments
3143 // that are passed into it.
3144 char **argv_copy = copy_argv(argc, argv);
3146 // This needs to run *before* V8::Initialize()
3147 // Use copy here as to not modify the original argv:
3148 Init(argc, argv_copy);
3152 Locker locker(node_isolate);
3153 HandleScope handle_scope(node_isolate);
3155 // Create the one and only Context.
3156 Local<Context> context = Context::New(node_isolate);
3157 Context::Scope context_scope(context);
3159 binding_cache.Reset(node_isolate, Object::New());
3161 // Use original argv, as we're just copying values out of it.
3162 Local<Object> process_l = SetupProcessObject(argc, argv);
3164 // Create all the objects, load modules, do everything.
3165 // so your next reading stop should be node::Load()!
3168 // All our arguments are loaded. We've evaluated all of the scripts. We
3169 // might even have created TCP servers. Now we enter the main eventloop. If
3170 // there are no watchers on the loop (except for the ones that were
3171 // uv_unref'd) then this function exits. As long as there are active
3172 // watchers, it blocks.
3173 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
3175 EmitExit(process_l);
3180 // Clean up. Not strictly necessary.
3184 // Clean up the copy: