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_script.h"
29 #include "node_version.h"
31 #if defined HAVE_PERFCTR
32 #include "node_counters.h"
36 #include "node_crypto.h"
39 #if defined HAVE_DTRACE || defined HAVE_ETW || defined HAVE_SYSTEMTAP
40 #include "node_dtrace.h"
44 #include "node_provider.h"
48 #include "handle_wrap.h"
50 #include "string_bytes.h"
57 #include <limits.h> // PATH_MAX
63 #include <sys/types.h>
69 #define strcasecmp _stricmp
70 #define getpid _getpid
74 #include <unistd.h> // setuid, getuid
77 #if defined(__POSIX__) && !defined(__ANDROID__)
78 #include <pwd.h> // getpwnam()
79 #include <grp.h> // getgrnam()
83 #include <crt_externs.h>
84 #define environ (*_NSGetEnviron())
85 #elif !defined(_MSC_VER)
86 extern char **environ;
92 using v8::ArrayBuffer;
97 using v8::FunctionCallbackInfo;
98 using v8::FunctionTemplate;
100 using v8::HandleScope;
101 using v8::HeapStatistics;
109 using v8::ObjectTemplate;
110 using v8::Persistent;
111 using v8::PropertyCallbackInfo;
112 using v8::ResourceConstraints;
113 using v8::SetResourceConstraints;
115 using v8::ThrowException;
120 using v8::kExternalUnsignedIntArray;
122 QUEUE handle_wrap_queue = { &handle_wrap_queue, &handle_wrap_queue };
123 QUEUE req_wrap_queue = { &req_wrap_queue, &req_wrap_queue };
125 // declared in req_wrap.h
126 Cached<String> process_symbol;
127 Cached<String> domain_symbol;
129 // declared in node_internals.h
130 Persistent<Object> process_p;
132 static Persistent<Function> process_tickCallback;
133 static Persistent<Object> binding_cache;
134 static Persistent<Array> module_load_list;
135 static Persistent<Array> p_domain_box;
137 static Cached<String> exports_symbol;
139 static Cached<String> errno_symbol;
140 static Cached<String> syscall_symbol;
141 static Cached<String> errpath_symbol;
142 static Cached<String> code_symbol;
144 static Cached<String> rss_symbol;
145 static Cached<String> heap_total_symbol;
146 static Cached<String> heap_used_symbol;
148 static Cached<String> fatal_exception_symbol;
150 static Cached<String> enter_symbol;
151 static Cached<String> exit_symbol;
152 static Cached<String> disposed_symbol;
154 // Essential for node_wrap.h
155 Persistent<FunctionTemplate> pipeConstructorTmpl;
156 Persistent<FunctionTemplate> tcpConstructorTmpl;
157 Persistent<FunctionTemplate> ttyConstructorTmpl;
159 static bool print_eval = false;
160 static bool force_repl = false;
161 static bool trace_deprecation = false;
162 static bool throw_deprecation = false;
163 static char *eval_string = NULL;
164 static int option_end_index = 0;
165 static bool use_debug_agent = false;
166 static bool debug_wait_connect = false;
167 static int debug_port = 5858;
168 static int max_stack_size = 0;
169 bool using_domains = false;
171 // used by C++ modules as well
172 bool no_deprecation = false;
174 static uv_check_t check_immediate_watcher;
175 static uv_idle_t idle_immediate_dummy;
176 static bool need_immediate_cb;
177 static Cached<String> immediate_callback_sym;
179 // for quick ref to tickCallback values
187 // easily communicate domain depth
192 #ifdef OPENSSL_NPN_NEGOTIATED
193 static bool use_npn = true;
195 static bool use_npn = false;
198 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
199 static bool use_sni = true;
201 static bool use_sni = false;
204 // process-relative uptime base, initialized at start-up
205 static double prog_start_time;
207 static volatile bool debugger_running = false;
208 static uv_async_t dispatch_debug_messages_async;
209 static uv_async_t emit_debug_enabled_async;
211 // Declared in node_internals.h
212 Isolate* node_isolate = NULL;
215 class ArrayBufferAllocator : public ArrayBuffer::Allocator {
217 // Impose an upper limit to avoid out of memory errors that bring down
219 static const size_t kMaxLength = 0x3fffffff;
220 static ArrayBufferAllocator the_singleton;
221 virtual ~ArrayBufferAllocator() {}
222 virtual void* Allocate(size_t length);
223 virtual void Free(void* data);
225 ArrayBufferAllocator() {}
226 ArrayBufferAllocator(const ArrayBufferAllocator&);
227 void operator=(const ArrayBufferAllocator&);
230 ArrayBufferAllocator ArrayBufferAllocator::the_singleton;
233 void* ArrayBufferAllocator::Allocate(size_t length) {
234 if (length > kMaxLength) return NULL;
235 return new char[length];
239 void ArrayBufferAllocator::Free(void* data) {
240 delete[] static_cast<char*>(data);
244 static void CheckImmediate(uv_check_t* handle, int status) {
245 assert(handle == &check_immediate_watcher);
248 HandleScope scope(node_isolate);
250 if (immediate_callback_sym.IsEmpty()) {
251 immediate_callback_sym =
252 FIXED_ONE_BYTE_STRING(node_isolate, "_immediateCallback");
255 MakeCallback(process_p, immediate_callback_sym, 0, NULL);
259 static void IdleImmediateDummy(uv_idle_t* handle, int status) {
260 // Do nothing. Only for maintaining event loop
261 assert(handle == &idle_immediate_dummy);
266 static inline const char *errno_string(int errorno) {
267 #define ERRNO_CASE(e) case e: return #e;
274 ERRNO_CASE(EADDRINUSE);
278 ERRNO_CASE(EADDRNOTAVAIL);
282 ERRNO_CASE(EAFNOSUPPORT);
290 # if EAGAIN != EWOULDBLOCK
291 ERRNO_CASE(EWOULDBLOCK);
296 ERRNO_CASE(EALREADY);
312 ERRNO_CASE(ECANCELED);
320 ERRNO_CASE(ECONNABORTED);
324 ERRNO_CASE(ECONNREFUSED);
328 ERRNO_CASE(ECONNRESET);
336 ERRNO_CASE(EDESTADDRREQ);
360 ERRNO_CASE(EHOSTUNREACH);
372 ERRNO_CASE(EINPROGRESS);
408 ERRNO_CASE(EMSGSIZE);
412 ERRNO_CASE(EMULTIHOP);
416 ERRNO_CASE(ENAMETOOLONG);
420 ERRNO_CASE(ENETDOWN);
424 ERRNO_CASE(ENETRESET);
428 ERRNO_CASE(ENETUNREACH);
460 # if ENOLINK != ENOLCK
474 ERRNO_CASE(ENOPROTOOPT);
494 ERRNO_CASE(ENOTCONN);
502 ERRNO_CASE(ENOTEMPTY);
506 ERRNO_CASE(ENOTSOCK);
513 ERRNO_CASE(EOPNOTSUPP);
527 ERRNO_CASE(EOVERFLOW);
542 #ifdef EPROTONOSUPPORT
543 ERRNO_CASE(EPROTONOSUPPORT);
547 ERRNO_CASE(EPROTOTYPE);
575 ERRNO_CASE(ETIMEDOUT);
590 const char *signo_string(int signo) {
591 #define SIGNO_CASE(e) case e: return #e;
618 # if SIGABRT != SIGIOT
662 SIGNO_CASE(SIGSTKFLT);
679 SIGNO_CASE(SIGBREAK);
703 SIGNO_CASE(SIGVTALRM);
711 SIGNO_CASE(SIGWINCH);
719 # if SIGPOLL != SIGIO
729 # if SIGPWR != SIGLOST
743 Local<Value> ErrnoException(int errorno,
748 Local<String> estring = OneByteString(node_isolate, errno_string(errorno));
749 if (msg == NULL || msg[0] == '\0') {
750 msg = strerror(errorno);
752 Local<String> message = OneByteString(node_isolate, msg);
754 Local<String> cons1 =
755 String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", "));
756 Local<String> cons2 = String::Concat(cons1, message);
758 if (syscall_symbol.IsEmpty()) {
759 syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall");
760 errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno");
761 errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path");
762 code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code");
766 Local<String> cons3 =
767 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
768 Local<String> cons4 =
769 String::Concat(cons3, String::NewFromUtf8(node_isolate, path));
770 Local<String> cons5 =
771 String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
772 e = Exception::Error(cons5);
774 e = Exception::Error(cons2);
777 Local<Object> obj = e->ToObject();
779 obj->Set(errno_symbol, Integer::New(errorno, node_isolate));
780 obj->Set(code_symbol, estring);
781 if (path) obj->Set(errpath_symbol, String::NewFromUtf8(node_isolate, path));
782 if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall));
787 // hack alert! copy of ErrnoException, tuned for uv errors
788 Local<Value> UVException(int errorno,
792 if (syscall_symbol.IsEmpty()) {
793 syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall");
794 errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno");
795 errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path");
796 code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code");
800 msg = uv_strerror(errorno);
802 Local<String> estring = OneByteString(node_isolate, uv_err_name(errorno));
803 Local<String> message = OneByteString(node_isolate, msg);
804 Local<String> cons1 =
805 String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", "));
806 Local<String> cons2 = String::Concat(cons1, message);
810 Local<String> path_str;
814 if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) {
815 path_str = String::Concat(FIXED_ONE_BYTE_STRING(node_isolate, "\\\\"),
816 String::NewFromUtf8(node_isolate, path + 8));
817 } else if (strncmp(path, "\\\\?\\", 4) == 0) {
818 path_str = String::NewFromUtf8(node_isolate, path + 4);
820 path_str = String::NewFromUtf8(node_isolate, path);
823 path_str = String::NewFromUtf8(node_isolate, path);
826 Local<String> cons3 =
827 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
828 Local<String> cons4 =
829 String::Concat(cons3, path_str);
830 Local<String> cons5 =
831 String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
832 e = Exception::Error(cons5);
834 e = Exception::Error(cons2);
837 Local<Object> obj = e->ToObject();
839 // TODO(piscisaureus) errno should probably go
840 obj->Set(errno_symbol, Integer::New(errorno, node_isolate));
841 obj->Set(code_symbol, estring);
842 if (path) obj->Set(errpath_symbol, path_str);
843 if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall));
849 // Does about the same as strerror(),
850 // but supports all windows error messages
851 static const char *winapi_strerror(const int errorno) {
854 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
855 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
856 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
859 // Remove trailing newlines
860 for (int i = strlen(errmsg) - 1;
861 i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) {
867 // FormatMessage failed
868 return "Unknown error";
873 Local<Value> WinapiErrnoException(int errorno,
878 if (!msg || !msg[0]) {
879 msg = winapi_strerror(errorno);
881 Local<String> message = OneByteString(node_isolate, msg);
883 if (syscall_symbol.IsEmpty()) {
884 syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall");
885 errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno");
886 errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path");
887 code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code");
891 Local<String> cons1 =
892 String::Concat(message, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
893 Local<String> cons2 =
894 String::Concat(cons1, String::NewFromUtf8(node_isolate, path));
895 Local<String> cons3 =
896 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
897 e = Exception::Error(cons3);
899 e = Exception::Error(message);
902 Local<Object> obj = e->ToObject();
904 obj->Set(errno_symbol, Integer::New(errorno, node_isolate));
905 if (path) obj->Set(errpath_symbol, String::NewFromUtf8(node_isolate, path));
906 if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall));
912 void SetupDomainUse(const FunctionCallbackInfo<Value>& args) {
913 if (using_domains) return;
914 HandleScope scope(node_isolate);
915 using_domains = true;
916 Local<Object> process = PersistentToLocal(node_isolate, process_p);
918 process->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_tickDomainCallback"));
919 if (!tdc_v->IsFunction()) {
920 fprintf(stderr, "process._tickDomainCallback assigned to non-function\n");
923 Local<Function> tdc = tdc_v.As<Function>();
924 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback"), tdc);
925 process_tickCallback.Reset(node_isolate, tdc);
926 if (!args[0]->IsArray()) {
927 fprintf(stderr, "_setupDomainUse first argument must be an array\n");
930 p_domain_box.Reset(node_isolate, args[0].As<Array>());
931 if (!args[1]->IsObject()) {
932 fprintf(stderr, "_setupDomainUse second argument must be an object\n");
935 Local<Object> flag = args[1].As<Object>();
936 flag->SetIndexedPropertiesToExternalArrayData(&domain_flag,
937 kExternalUnsignedIntArray,
943 return using_domains && domain_flag.count > 0;
947 Handle<Value> GetDomain() {
948 // no domain can exist if no domain module has been loaded
949 if (!InDomain() || p_domain_box.IsEmpty())
950 return Null(node_isolate);
952 return PersistentToLocal(node_isolate, p_domain_box)->Get(0);
957 MakeDomainCallback(const Handle<Object> object,
958 const Handle<Function> callback,
960 Handle<Value> argv[]) {
961 // TODO(trevnorris) Hook for long stack traces to be made here.
963 // lazy load domain specific symbols
964 if (enter_symbol.IsEmpty()) {
965 enter_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "enter");
966 exit_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "exit");
967 disposed_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "_disposed");
970 Local<Value> domain_v = object->Get(domain_symbol);
971 Local<Object> domain;
972 Local<Function> enter;
973 Local<Function> exit;
976 try_catch.SetVerbose(true);
978 bool has_domain = domain_v->IsObject();
980 domain = domain_v->ToObject();
981 assert(!domain.IsEmpty());
982 if (domain->Get(disposed_symbol)->IsTrue()) {
983 // domain has been disposed of.
984 return Undefined(node_isolate);
986 enter = Local<Function>::Cast(domain->Get(enter_symbol));
987 assert(!enter.IsEmpty());
988 enter->Call(domain, 0, NULL);
990 if (try_catch.HasCaught()) {
991 return Undefined(node_isolate);
995 Local<Value> ret = callback->Call(object, argc, argv);
997 if (try_catch.HasCaught()) {
998 return Undefined(node_isolate);
1002 exit = Local<Function>::Cast(domain->Get(exit_symbol));
1003 assert(!exit.IsEmpty());
1004 exit->Call(domain, 0, NULL);
1006 if (try_catch.HasCaught()) {
1007 return Undefined(node_isolate);
1011 if (tick_infobox.last_threw == 1) {
1012 tick_infobox.last_threw = 0;
1016 if (tick_infobox.in_tick == 1) {
1020 if (tick_infobox.length == 0) {
1021 tick_infobox.index = 0;
1025 // process nextTicks after call
1026 Local<Object> process = PersistentToLocal(node_isolate, process_p);
1027 Local<Function> fn = PersistentToLocal(node_isolate, process_tickCallback);
1028 fn->Call(process, 0, NULL);
1030 if (try_catch.HasCaught()) {
1031 return Undefined(node_isolate);
1039 MakeCallback(const Handle<Object> object,
1040 const Handle<Function> callback,
1042 Handle<Value> argv[]) {
1043 // TODO(trevnorris) Hook for long stack traces to be made here.
1044 Local<Object> process = PersistentToLocal(node_isolate, process_p);
1047 return MakeDomainCallback(object, callback, argc, argv);
1049 // lazy load no domain next tick callbacks
1050 if (process_tickCallback.IsEmpty()) {
1052 process->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback"));
1053 if (!cb_v->IsFunction()) {
1054 fprintf(stderr, "process._tickCallback assigned to non-function\n");
1057 process_tickCallback.Reset(node_isolate, cb_v.As<Function>());
1061 try_catch.SetVerbose(true);
1063 Local<Value> ret = callback->Call(object, argc, argv);
1065 if (try_catch.HasCaught()) {
1066 return Undefined(node_isolate);
1069 if (tick_infobox.in_tick == 1) {
1073 if (tick_infobox.length == 0) {
1074 tick_infobox.index = 0;
1078 // process nextTicks after call
1079 Local<Function> fn = PersistentToLocal(node_isolate, process_tickCallback);
1080 fn->Call(process, 0, NULL);
1082 if (try_catch.HasCaught()) {
1083 return Undefined(node_isolate);
1092 MakeCallback(const Handle<Object> object,
1095 Handle<Value> argv[]) {
1096 HandleScope scope(node_isolate);
1098 Local<Function> callback = object->Get(index).As<Function>();
1099 assert(callback->IsFunction());
1102 return scope.Close(MakeDomainCallback(object, callback, argc, argv));
1103 return scope.Close(MakeCallback(object, callback, argc, argv));
1108 MakeCallback(const Handle<Object> object,
1109 const Handle<String> symbol,
1111 Handle<Value> argv[]) {
1112 HandleScope scope(node_isolate);
1114 Local<Function> callback = object->Get(symbol).As<Function>();
1115 assert(callback->IsFunction());
1118 return scope.Close(MakeDomainCallback(object, callback, argc, argv));
1119 return scope.Close(MakeCallback(object, callback, argc, argv));
1124 MakeCallback(const Handle<Object> object,
1127 Handle<Value> argv[]) {
1128 HandleScope scope(node_isolate);
1130 Local<String> method_string = OneByteString(node_isolate, method);
1131 Handle<Value> ret = MakeCallback(object, method_string, argc, argv);
1133 return scope.Close(ret);
1137 enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
1138 HandleScope scope(node_isolate);
1140 if (!encoding_v->IsString()) return _default;
1142 String::Utf8Value encoding(encoding_v);
1144 if (strcasecmp(*encoding, "utf8") == 0) {
1146 } else if (strcasecmp(*encoding, "utf-8") == 0) {
1148 } else if (strcasecmp(*encoding, "ascii") == 0) {
1150 } else if (strcasecmp(*encoding, "base64") == 0) {
1152 } else if (strcasecmp(*encoding, "ucs2") == 0) {
1154 } else if (strcasecmp(*encoding, "ucs-2") == 0) {
1156 } else if (strcasecmp(*encoding, "utf16le") == 0) {
1158 } else if (strcasecmp(*encoding, "utf-16le") == 0) {
1160 } else if (strcasecmp(*encoding, "binary") == 0) {
1162 } else if (strcasecmp(*encoding, "buffer") == 0) {
1164 } else if (strcasecmp(*encoding, "hex") == 0) {
1166 } else if (strcasecmp(*encoding, "raw") == 0) {
1167 if (!no_deprecation) {
1168 fprintf(stderr, "'raw' (array of integers) has been removed. "
1172 } else if (strcasecmp(*encoding, "raws") == 0) {
1173 if (!no_deprecation) {
1174 fprintf(stderr, "'raws' encoding has been renamed to 'binary'. "
1175 "Please update your code.\n");
1183 Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
1184 return StringBytes::Encode(static_cast<const char*>(buf),
1189 // Returns -1 if the handle was not valid for decoding
1190 ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
1191 HandleScope scope(node_isolate);
1193 if (val->IsArray()) {
1194 fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
1200 return StringBytes::Size(val, encoding);
1204 # define MIN(a, b) ((a) < (b) ? (a) : (b))
1207 // Returns number of bytes written.
1208 ssize_t DecodeWrite(char *buf,
1210 v8::Handle<v8::Value> val,
1211 enum encoding encoding) {
1212 return StringBytes::Write(buf, buflen, val, encoding, NULL);
1215 void DisplayExceptionLine(Handle<Message> message) {
1216 // Prevent re-entry into this function. For example, if there is
1217 // a throw from a program in vm.runInThisContext(code, filename, true),
1218 // then we want to show the original failure, not the secondary one.
1219 static bool displayed_error = false;
1221 if (displayed_error) return;
1222 displayed_error = true;
1224 uv_tty_reset_mode();
1226 fprintf(stderr, "\n");
1228 if (!message.IsEmpty()) {
1229 // Print (filename):(line number): (message).
1230 String::Utf8Value filename(message->GetScriptResourceName());
1231 const char* filename_string = *filename;
1232 int linenum = message->GetLineNumber();
1233 fprintf(stderr, "%s:%i\n", filename_string, linenum);
1234 // Print line of source code.
1235 String::Utf8Value sourceline(message->GetSourceLine());
1236 const char* sourceline_string = *sourceline;
1238 // Because of how node modules work, all scripts are wrapped with a
1239 // "function (module, exports, __filename, ...) {"
1240 // to provide script local variables.
1242 // When reporting errors on the first line of a script, this wrapper
1243 // function is leaked to the user. There used to be a hack here to
1244 // truncate off the first 62 characters, but it caused numerous other
1245 // problems when vm.runIn*Context() methods were used for non-module
1248 // If we ever decide to re-instate such a hack, the following steps
1251 // 1. Pass a flag around to say "this code was wrapped"
1252 // 2. Update the stack frame output so that it is also correct.
1254 // It would probably be simpler to add a line rather than add some
1255 // number of characters to the first line, since V8 truncates the
1256 // sourceline to 78 characters, and we end up not providing very much
1257 // useful debugging info to the user if we remove 62 characters.
1259 int start = message->GetStartColumn();
1260 int end = message->GetEndColumn();
1262 fprintf(stderr, "%s\n", sourceline_string);
1263 // Print wavy underline (GetUnderline is deprecated).
1264 for (int i = 0; i < start; i++) {
1265 fputc((sourceline_string[i] == '\t') ? '\t' : ' ', stderr);
1267 for (int i = start; i < end; i++) {
1270 fputc('\n', stderr);
1275 static void ReportException(Handle<Value> er, Handle<Message> message) {
1276 HandleScope scope(node_isolate);
1278 DisplayExceptionLine(message);
1280 Local<Value> trace_value(
1281 er->ToObject()->Get(FIXED_ONE_BYTE_STRING(node_isolate, "stack")));
1282 String::Utf8Value trace(trace_value);
1284 // range errors have a trace member set to undefined
1285 if (trace.length() > 0 && !trace_value->IsUndefined()) {
1286 fprintf(stderr, "%s\n", *trace);
1288 // this really only happens for RangeErrors, since they're the only
1289 // kind that won't have all this info in the trace, or when non-Error
1290 // objects are thrown manually.
1291 Local<Value> message;
1294 if (er->IsObject()) {
1295 Local<Object> err_obj = er.As<Object>();
1296 message = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "message"));
1297 name = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "name"));
1300 if (message.IsEmpty() ||
1301 message->IsUndefined() ||
1303 name->IsUndefined()) {
1304 // Not an error object. Just print as-is.
1305 String::Utf8Value message(er);
1306 fprintf(stderr, "%s\n", *message);
1308 String::Utf8Value name_string(name);
1309 String::Utf8Value message_string(message);
1310 fprintf(stderr, "%s: %s\n", *name_string, *message_string);
1318 static void ReportException(const TryCatch& try_catch) {
1319 ReportException(try_catch.Exception(), try_catch.Message());
1323 // Executes a str within the current v8 context.
1324 Local<Value> ExecuteString(Handle<String> source, Handle<Value> filename) {
1325 HandleScope scope(node_isolate);
1328 // try_catch must be nonverbose to disable FatalException() handler,
1329 // we will handle exceptions ourself.
1330 try_catch.SetVerbose(false);
1332 Local<v8::Script> script = v8::Script::Compile(source, filename);
1333 if (script.IsEmpty()) {
1334 ReportException(try_catch);
1338 Local<Value> result = script->Run();
1339 if (result.IsEmpty()) {
1340 ReportException(try_catch);
1344 return scope.Close(result);
1348 static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
1349 HandleScope scope(node_isolate);
1351 Local<Array> ary = Array::New();
1355 QUEUE_FOREACH(q, &req_wrap_queue) {
1356 ReqWrap<uv_req_t>* w = container_of(q, ReqWrap<uv_req_t>, req_wrap_queue_);
1357 if (w->persistent().IsEmpty()) continue;
1358 ary->Set(i++, w->object());
1361 args.GetReturnValue().Set(ary);
1365 // Non-static, friend of HandleWrap. Could have been a HandleWrap method but
1366 // implemented here for consistency with GetActiveRequests().
1367 void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
1368 HandleScope scope(node_isolate);
1370 Local<Array> ary = Array::New();
1374 Local<String> owner_sym = FIXED_ONE_BYTE_STRING(node_isolate, "owner");
1376 QUEUE_FOREACH(q, &handle_wrap_queue) {
1377 HandleWrap* w = container_of(q, HandleWrap, handle_wrap_queue_);
1378 if (w->persistent().IsEmpty() || (w->flags_ & HandleWrap::kUnref)) continue;
1379 Local<Object> object = w->object();
1380 Local<Value> owner = object->Get(owner_sym);
1381 if (owner->IsUndefined()) owner = object;
1382 ary->Set(i++, owner);
1385 args.GetReturnValue().Set(ary);
1389 static void Abort(const FunctionCallbackInfo<Value>& args) {
1394 static void Chdir(const FunctionCallbackInfo<Value>& args) {
1395 HandleScope scope(node_isolate);
1397 if (args.Length() != 1 || !args[0]->IsString()) {
1398 return ThrowError("Bad argument."); // FIXME(bnoordhuis) ThrowTypeError?
1401 String::Utf8Value path(args[0]);
1402 int err = uv_chdir(*path);
1404 return ThrowUVException(err, "uv_chdir");
1409 static void Cwd(const FunctionCallbackInfo<Value>& args) {
1410 HandleScope scope(node_isolate);
1412 /* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
1413 char buf[MAX_PATH * 4 + 1];
1415 char buf[PATH_MAX + 1];
1418 int err = uv_cwd(buf, ARRAY_SIZE(buf) - 1);
1420 return ThrowUVException(err, "uv_cwd");
1423 buf[ARRAY_SIZE(buf) - 1] = '\0';
1424 Local<String> cwd = String::NewFromUtf8(node_isolate, buf);
1426 args.GetReturnValue().Set(cwd);
1430 static void Umask(const FunctionCallbackInfo<Value>& args) {
1431 HandleScope scope(node_isolate);
1434 if (args.Length() < 1 || args[0]->IsUndefined()) {
1436 umask(static_cast<mode_t>(old));
1437 } else if (!args[0]->IsInt32() && !args[0]->IsString()) {
1438 return ThrowTypeError("argument must be an integer or octal string.");
1441 if (args[0]->IsInt32()) {
1442 oct = args[0]->Uint32Value();
1445 String::Utf8Value str(args[0]);
1447 // Parse the octal string.
1448 for (int i = 0; i < str.length(); i++) {
1450 if (c > '7' || c < '0') {
1451 return ThrowTypeError("invalid octal string");
1457 old = umask(static_cast<mode_t>(oct));
1460 args.GetReturnValue().Set(old);
1464 #if defined(__POSIX__) && !defined(__ANDROID__)
1466 static const uid_t uid_not_found = static_cast<uid_t>(-1);
1467 static const gid_t gid_not_found = static_cast<gid_t>(-1);
1470 static uid_t uid_by_name(const char* name) {
1478 if (getpwnam_r(name, &pwd, buf, sizeof(buf), &pp) == 0 && pp != NULL) {
1482 return uid_not_found;
1486 static char* name_by_uid(uid_t uid) {
1495 if ((rc = getpwuid_r(uid, &pwd, buf, sizeof(buf), &pp)) == 0 && pp != NULL) {
1496 return strdup(pp->pw_name);
1507 static gid_t gid_by_name(const char* name) {
1515 if (getgrnam_r(name, &pwd, buf, sizeof(buf), &pp) == 0 && pp != NULL) {
1519 return gid_not_found;
1523 #if 0 // For future use.
1524 static const char* name_by_gid(gid_t gid) {
1533 if ((rc = getgrgid_r(gid, &pwd, buf, sizeof(buf), &pp)) == 0 && pp != NULL) {
1534 return strdup(pp->gr_name);
1546 static uid_t uid_by_name(Handle<Value> value) {
1547 if (value->IsUint32()) {
1548 return static_cast<uid_t>(value->Uint32Value());
1550 String::Utf8Value name(value);
1551 return uid_by_name(*name);
1556 static gid_t gid_by_name(Handle<Value> value) {
1557 if (value->IsUint32()) {
1558 return static_cast<gid_t>(value->Uint32Value());
1560 String::Utf8Value name(value);
1561 return gid_by_name(*name);
1566 static void GetUid(const FunctionCallbackInfo<Value>& args) {
1567 args.GetReturnValue().Set(getuid());
1571 static void GetGid(const FunctionCallbackInfo<Value>& args) {
1572 args.GetReturnValue().Set(getgid());
1576 static void SetGid(const FunctionCallbackInfo<Value>& args) {
1577 HandleScope scope(node_isolate);
1579 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1580 return ThrowTypeError("setgid argument must be a number or a string");
1583 gid_t gid = gid_by_name(args[0]);
1585 if (gid == gid_not_found) {
1586 return ThrowError("setgid group id does not exist");
1590 return ThrowErrnoException(errno, "setgid");
1595 static void SetUid(const FunctionCallbackInfo<Value>& args) {
1596 HandleScope scope(node_isolate);
1598 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1599 return ThrowTypeError("setuid argument must be a number or a string");
1602 uid_t uid = uid_by_name(args[0]);
1604 if (uid == uid_not_found) {
1605 return ThrowError("setuid user id does not exist");
1609 return ThrowErrnoException(errno, "setuid");
1614 static void GetGroups(const FunctionCallbackInfo<Value>& args) {
1615 HandleScope scope(node_isolate);
1617 int ngroups = getgroups(0, NULL);
1619 if (ngroups == -1) {
1620 return ThrowErrnoException(errno, "getgroups");
1623 gid_t* groups = new gid_t[ngroups];
1625 ngroups = getgroups(ngroups, groups);
1627 if (ngroups == -1) {
1629 return ThrowErrnoException(errno, "getgroups");
1632 Local<Array> groups_list = Array::New(ngroups);
1633 bool seen_egid = false;
1634 gid_t egid = getegid();
1636 for (int i = 0; i < ngroups; i++) {
1637 groups_list->Set(i, Integer::New(groups[i], node_isolate));
1638 if (groups[i] == egid) seen_egid = true;
1643 if (seen_egid == false) {
1644 groups_list->Set(ngroups, Integer::New(egid, node_isolate));
1647 args.GetReturnValue().Set(groups_list);
1651 static void SetGroups(const FunctionCallbackInfo<Value>& args) {
1652 HandleScope scope(node_isolate);
1654 if (!args[0]->IsArray()) {
1655 return ThrowTypeError("argument 1 must be an array");
1658 Local<Array> groups_list = args[0].As<Array>();
1659 size_t size = groups_list->Length();
1660 gid_t* groups = new gid_t[size];
1662 for (size_t i = 0; i < size; i++) {
1663 gid_t gid = gid_by_name(groups_list->Get(i));
1665 if (gid == gid_not_found) {
1667 return ThrowError("group name not found");
1673 int rc = setgroups(size, groups);
1677 return ThrowErrnoException(errno, "setgroups");
1682 static void InitGroups(const FunctionCallbackInfo<Value>& args) {
1683 HandleScope scope(node_isolate);
1685 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1686 return ThrowTypeError("argument 1 must be a number or a string");
1689 if (!args[1]->IsUint32() && !args[1]->IsString()) {
1690 return ThrowTypeError("argument 2 must be a number or a string");
1693 String::Utf8Value arg0(args[0]);
1698 if (args[0]->IsUint32()) {
1699 user = name_by_uid(args[0]->Uint32Value());
1707 return ThrowError("initgroups user not found");
1710 extra_group = gid_by_name(args[1]);
1712 if (extra_group == gid_not_found) {
1713 if (must_free) free(user);
1714 return ThrowError("initgroups extra group not found");
1717 int rc = initgroups(user, extra_group);
1724 return ThrowErrnoException(errno, "initgroups");
1728 #endif // __POSIX__ && !defined(__ANDROID__)
1731 void Exit(const FunctionCallbackInfo<Value>& args) {
1732 HandleScope scope(node_isolate);
1733 exit(args[0]->IntegerValue());
1737 static void Uptime(const FunctionCallbackInfo<Value>& args) {
1738 HandleScope scope(node_isolate);
1740 if (uv_uptime(&uptime)) return;
1741 args.GetReturnValue().Set(uptime - prog_start_time);
1745 void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
1746 HandleScope scope(node_isolate);
1750 int err = uv_resident_set_memory(&rss);
1752 return ThrowUVException(err, "uv_resident_set_memory");
1755 Local<Object> info = Object::New();
1757 if (rss_symbol.IsEmpty()) {
1758 rss_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "rss");
1759 heap_total_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "heapTotal");
1760 heap_used_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "heapUsed");
1763 info->Set(rss_symbol, Number::New(rss));
1766 HeapStatistics v8_heap_stats;
1767 node_isolate->GetHeapStatistics(&v8_heap_stats);
1768 info->Set(heap_total_symbol,
1769 Integer::NewFromUnsigned(v8_heap_stats.total_heap_size(),
1771 info->Set(heap_used_symbol,
1772 Integer::NewFromUnsigned(v8_heap_stats.used_heap_size(),
1775 args.GetReturnValue().Set(info);
1779 void Kill(const FunctionCallbackInfo<Value>& args) {
1780 HandleScope scope(node_isolate);
1782 if (args.Length() != 2) {
1783 return ThrowError("Bad argument.");
1786 int pid = args[0]->IntegerValue();
1787 int sig = args[1]->Int32Value();
1788 int err = uv_kill(pid, sig);
1789 args.GetReturnValue().Set(err);
1792 // used in Hrtime() below
1793 #define NANOS_PER_SEC 1000000000
1795 // Hrtime exposes libuv's uv_hrtime() high-resolution timer.
1796 // The value returned by uv_hrtime() is a 64-bit int representing nanoseconds,
1797 // so this function instead returns an Array with 2 entries representing seconds
1798 // and nanoseconds, to avoid any integer overflow possibility.
1799 // Pass in an Array from a previous hrtime() call to instead get a time diff.
1800 void Hrtime(const FunctionCallbackInfo<Value>& args) {
1801 HandleScope scope(node_isolate);
1803 uint64_t t = uv_hrtime();
1805 if (args.Length() > 0) {
1806 // return a time diff tuple
1807 if (!args[0]->IsArray()) {
1808 return ThrowTypeError("process.hrtime() only accepts an Array tuple.");
1810 Local<Array> inArray = Local<Array>::Cast(args[0]);
1811 uint64_t seconds = inArray->Get(0)->Uint32Value();
1812 uint64_t nanos = inArray->Get(1)->Uint32Value();
1813 t -= (seconds * NANOS_PER_SEC) + nanos;
1816 Local<Array> tuple = Array::New(2);
1817 tuple->Set(0, Integer::NewFromUnsigned(t / NANOS_PER_SEC, node_isolate));
1818 tuple->Set(1, Integer::NewFromUnsigned(t % NANOS_PER_SEC, node_isolate));
1819 args.GetReturnValue().Set(tuple);
1823 typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);
1825 // DLOpen is process.dlopen(module, filename).
1826 // Used to load 'module.node' dynamically shared objects.
1827 void DLOpen(const FunctionCallbackInfo<Value>& args) {
1828 HandleScope scope(node_isolate);
1829 char symbol[1024], *base, *pos;
1833 if (args.Length() < 2) {
1834 return ThrowError("process.dlopen takes exactly 2 arguments.");
1837 Local<Object> module = args[0]->ToObject(); // Cast
1838 String::Utf8Value filename(args[1]); // Cast
1840 if (exports_symbol.IsEmpty()) {
1841 exports_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "exports");
1843 Local<Object> exports = module->Get(exports_symbol)->ToObject();
1845 if (uv_dlopen(*filename, &lib)) {
1846 Local<String> errmsg = OneByteString(node_isolate, uv_dlerror(&lib));
1848 // Windows needs to add the filename into the error message
1849 errmsg = String::Concat(errmsg, args[1]->ToString());
1851 ThrowException(Exception::Error(errmsg));
1855 String::Utf8Value path(args[1]);
1858 /* Find the shared library filename within the full path. */
1860 pos = strrchr(base, '/');
1866 pos = strpbrk(base, "\\/:");
1874 /* Strip the .node extension. */
1875 pos = strrchr(base, '.');
1880 /* Add the `_module` suffix to the extension name. */
1881 r = snprintf(symbol, sizeof symbol, "%s_module", base);
1882 if (r <= 0 || static_cast<size_t>(r) >= sizeof symbol) {
1883 return ThrowError("Out of memory.");
1886 /* Replace dashes with underscores. When loading foo-bar.node,
1887 * look for foo_bar_module, not foo-bar_module.
1889 for (pos = symbol; *pos != '\0'; ++pos) {
1890 if (*pos == '-') *pos = '_';
1893 node_module_struct *mod;
1894 if (uv_dlsym(&lib, symbol, reinterpret_cast<void**>(&mod))) {
1896 snprintf(errmsg, sizeof(errmsg), "Symbol %s not found.", symbol);
1897 return ThrowError(errmsg);
1900 if (mod->version != NODE_MODULE_VERSION) {
1904 "Module version mismatch. Expected %d, got %d.",
1905 NODE_MODULE_VERSION, mod->version);
1906 return ThrowError(errmsg);
1909 // Execute the C++ module
1910 mod->register_func(exports, module);
1912 // Tell coverity that 'handle' should not be freed when we return.
1913 // coverity[leaked_storage]
1917 static void OnFatalError(const char* location, const char* message) {
1919 fprintf(stderr, "FATAL ERROR: %s %s\n", location, message);
1921 fprintf(stderr, "FATAL ERROR: %s\n", message);
1931 NO_RETURN void FatalError(const char* location, const char* message) {
1932 OnFatalError(location, message);
1933 // to supress compiler warning
1938 void FatalException(Handle<Value> error, Handle<Message> message) {
1939 HandleScope scope(node_isolate);
1941 if (fatal_exception_symbol.IsEmpty()) {
1942 fatal_exception_symbol =
1943 FIXED_ONE_BYTE_STRING(node_isolate, "_fatalException");
1946 Local<Object> process = PersistentToLocal(node_isolate, process_p);
1947 Local<Value> fatal_v = process->Get(fatal_exception_symbol);
1949 if (!fatal_v->IsFunction()) {
1950 // failed before the process._fatalException function was added!
1951 // this is probably pretty bad. Nothing to do but report and exit.
1952 ReportException(error, message);
1956 Local<Function> fatal_f = Local<Function>::Cast(fatal_v);
1958 TryCatch fatal_try_catch;
1960 // Do not call FatalException when _fatalException handler throws
1961 fatal_try_catch.SetVerbose(false);
1963 // this will return true if the JS layer handled it, false otherwise
1964 Local<Value> caught = fatal_f->Call(process, 1, &error);
1966 if (fatal_try_catch.HasCaught()) {
1967 // the fatal exception function threw, so we must exit
1968 ReportException(fatal_try_catch);
1972 if (false == caught->BooleanValue()) {
1973 ReportException(error, message);
1979 void FatalException(const TryCatch& try_catch) {
1980 HandleScope scope(node_isolate);
1981 // TODO(bajtos) do not call FatalException if try_catch is verbose
1982 // (requires V8 API to expose getter for try_catch.is_verbose_)
1983 FatalException(try_catch.Exception(), try_catch.Message());
1987 void OnMessage(Handle<Message> message, Handle<Value> error) {
1988 // The current version of V8 sends messages for errors only
1989 // (thus `error` is always set).
1990 FatalException(error, message);
1994 static void Binding(const FunctionCallbackInfo<Value>& args) {
1995 HandleScope scope(node_isolate);
1997 Local<String> module = args[0]->ToString();
1998 String::Utf8Value module_v(module);
1999 node_module_struct* modp;
2001 Local<Object> cache = PersistentToLocal(node_isolate, binding_cache);
2002 Local<Object> exports;
2004 if (cache->Has(module)) {
2005 exports = cache->Get(module)->ToObject();
2006 args.GetReturnValue().Set(exports);
2010 // Append a string to process.moduleLoadList
2012 snprintf(buf, sizeof(buf), "Binding %s", *module_v);
2014 Local<Array> modules = PersistentToLocal(node_isolate, module_load_list);
2015 uint32_t l = modules->Length();
2016 modules->Set(l, OneByteString(node_isolate, buf));
2018 if ((modp = get_builtin_module(*module_v)) != NULL) {
2019 exports = Object::New();
2020 // Internal bindings don't have a "module" object,
2022 modp->register_func(exports, Undefined(node_isolate));
2023 cache->Set(module, exports);
2024 } else if (!strcmp(*module_v, "constants")) {
2025 exports = Object::New();
2026 DefineConstants(exports);
2027 cache->Set(module, exports);
2028 } else if (!strcmp(*module_v, "natives")) {
2029 exports = Object::New();
2030 DefineJavaScript(exports);
2031 cache->Set(module, exports);
2033 return ThrowError("No such module");
2036 args.GetReturnValue().Set(exports);
2040 static void ProcessTitleGetter(Local<String> property,
2041 const PropertyCallbackInfo<Value>& info) {
2042 HandleScope scope(node_isolate);
2044 uv_get_process_title(buffer, sizeof(buffer));
2045 info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, buffer));
2049 static void ProcessTitleSetter(Local<String> property,
2051 const PropertyCallbackInfo<void>& info) {
2052 HandleScope scope(node_isolate);
2053 String::Utf8Value title(value);
2054 // TODO(piscisaureus): protect with a lock
2055 uv_set_process_title(*title);
2059 static void EnvGetter(Local<String> property,
2060 const PropertyCallbackInfo<Value>& info) {
2061 HandleScope scope(node_isolate);
2063 String::Utf8Value key(property);
2064 const char* val = getenv(*key);
2066 return info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, val));
2069 String::Value key(property);
2070 WCHAR buffer[32767]; // The maximum size allowed for environment variables.
2071 DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
2073 ARRAY_SIZE(buffer));
2074 // If result >= sizeof buffer the buffer was too small. That should never
2075 // happen. If result == 0 and result != ERROR_SUCCESS the variable was not
2077 if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
2078 result < ARRAY_SIZE(buffer)) {
2079 const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
2080 Local<String> rc = String::NewFromTwoByte(node_isolate, two_byte_buffer);
2081 return info.GetReturnValue().Set(rc);
2084 // Not found. Fetch from prototype.
2085 info.GetReturnValue().Set(
2086 info.Data().As<Object>()->Get(property));
2090 static void EnvSetter(Local<String> property,
2092 const PropertyCallbackInfo<Value>& info) {
2093 HandleScope scope(node_isolate);
2095 String::Utf8Value key(property);
2096 String::Utf8Value val(value);
2097 setenv(*key, *val, 1);
2099 String::Value key(property);
2100 String::Value val(value);
2101 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2102 // Environment variables that start with '=' are read-only.
2103 if (key_ptr[0] != L'=') {
2104 SetEnvironmentVariableW(key_ptr, reinterpret_cast<WCHAR*>(*val));
2107 // Whether it worked or not, always return rval.
2108 info.GetReturnValue().Set(value);
2112 static void EnvQuery(Local<String> property,
2113 const PropertyCallbackInfo<Integer>& info) {
2114 HandleScope scope(node_isolate);
2115 int32_t rc = -1; // Not found unless proven otherwise.
2117 String::Utf8Value key(property);
2118 if (getenv(*key)) rc = 0;
2120 String::Value key(property);
2121 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2122 if (GetEnvironmentVariableW(key_ptr, NULL, 0) > 0 ||
2123 GetLastError() == ERROR_SUCCESS) {
2125 if (key_ptr[0] == L'=') {
2126 // Environment variables that start with '=' are hidden and read-only.
2127 rc = static_cast<int32_t>(v8::ReadOnly) |
2128 static_cast<int32_t>(v8::DontDelete) |
2129 static_cast<int32_t>(v8::DontEnum);
2133 if (rc != -1) info.GetReturnValue().Set(rc);
2137 static void EnvDeleter(Local<String> property,
2138 const PropertyCallbackInfo<Boolean>& info) {
2139 HandleScope scope(node_isolate);
2142 String::Utf8Value key(property);
2143 rc = getenv(*key) != NULL;
2144 if (rc) unsetenv(*key);
2146 String::Value key(property);
2147 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2148 if (key_ptr[0] == L'=' || !SetEnvironmentVariableW(key_ptr, NULL)) {
2149 // Deletion failed. Return true if the key wasn't there in the first place,
2150 // false if it is still there.
2151 rc = GetEnvironmentVariableW(key_ptr, NULL, NULL) == 0 &&
2152 GetLastError() != ERROR_SUCCESS;
2155 info.GetReturnValue().Set(rc);
2159 static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
2160 HandleScope scope(node_isolate);
2163 while (environ[size]) size++;
2165 Local<Array> env = Array::New(size);
2167 for (int i = 0; i < size; ++i) {
2168 const char* var = environ[i];
2169 const char* s = strchr(var, '=');
2170 const int length = s ? s - var : strlen(var);
2171 Local<String> name = String::NewFromUtf8(node_isolate,
2173 String::kNormalString,
2178 WCHAR* environment = GetEnvironmentStringsW();
2179 if (environment == NULL) return; // This should not happen.
2180 Local<Array> env = Array::New();
2181 WCHAR* p = environment;
2183 while (*p != NULL) {
2186 // If the key starts with '=' it is a hidden environment variable.
2190 s = wcschr(p, L'=');
2195 const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
2196 const size_t two_byte_buffer_len = s - p;
2197 Local<String> value = String::NewFromTwoByte(node_isolate,
2199 String::kNormalString,
2200 two_byte_buffer_len);
2201 env->Set(i++, value);
2202 p = s + wcslen(s) + 1;
2204 FreeEnvironmentStringsW(environment);
2207 info.GetReturnValue().Set(env);
2211 static Handle<Object> GetFeatures() {
2212 HandleScope scope(node_isolate);
2214 Local<Object> obj = Object::New();
2215 #if defined(DEBUG) && DEBUG
2216 Local<Value> debug = True(node_isolate);
2218 Local<Value> debug = False(node_isolate);
2219 #endif // defined(DEBUG) && DEBUG
2221 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "debug"), debug);
2223 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "uv"), True(node_isolate));
2224 // TODO(bnoordhuis) ping libuv
2225 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "ipv6"), True(node_isolate));
2227 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_npn"),
2228 Boolean::New(use_npn));
2229 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_sni"),
2230 Boolean::New(use_sni));
2231 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls"),
2232 Boolean::New(get_builtin_module("crypto") != NULL));
2234 return scope.Close(obj);
2238 static void DebugPortGetter(Local<String> property,
2239 const PropertyCallbackInfo<Value>& info) {
2240 HandleScope scope(node_isolate);
2241 info.GetReturnValue().Set(debug_port);
2245 static void DebugPortSetter(Local<String> property,
2247 const PropertyCallbackInfo<void>& info) {
2248 HandleScope scope(node_isolate);
2249 debug_port = value->NumberValue();
2253 static void DebugProcess(const FunctionCallbackInfo<Value>& args);
2254 static void DebugPause(const FunctionCallbackInfo<Value>& args);
2255 static void DebugEnd(const FunctionCallbackInfo<Value>& args);
2258 void NeedImmediateCallbackGetter(Local<String> property,
2259 const PropertyCallbackInfo<Value>& info) {
2260 info.GetReturnValue().Set(need_immediate_cb);
2264 static void NeedImmediateCallbackSetter(Local<String> property,
2266 const PropertyCallbackInfo<void>&) {
2267 HandleScope scope(node_isolate);
2269 bool bool_value = value->BooleanValue();
2271 if (need_immediate_cb == bool_value) return;
2273 need_immediate_cb = bool_value;
2275 if (need_immediate_cb) {
2276 uv_check_start(&check_immediate_watcher, node::CheckImmediate);
2277 // idle handle is needed only to maintain event loop
2278 uv_idle_start(&idle_immediate_dummy, node::IdleImmediateDummy);
2280 uv_check_stop(&check_immediate_watcher);
2281 uv_idle_stop(&idle_immediate_dummy);
2286 #define READONLY_PROPERTY(obj, str, var) \
2288 obj->Set(OneByteString(node_isolate, str), var, v8::ReadOnly); \
2292 Handle<Object> SetupProcessObject(int argc, char *argv[]) {
2293 HandleScope scope(node_isolate);
2297 Local<FunctionTemplate> process_template = FunctionTemplate::New();
2298 process_template->SetClassName(
2299 FIXED_ONE_BYTE_STRING(node_isolate, "process"));
2301 Local<Object> process = process_template->GetFunction()->NewInstance();
2302 assert(process.IsEmpty() == false);
2303 assert(process->IsObject() == true);
2305 process_p.Reset(node_isolate, process);
2307 process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "title"),
2309 ProcessTitleSetter);
2312 READONLY_PROPERTY(process,
2314 FIXED_ONE_BYTE_STRING(node_isolate, NODE_VERSION));
2316 // process.moduleLoadList
2317 Local<Array> modules = Array::New();
2318 module_load_list.Reset(node_isolate, modules);
2319 READONLY_PROPERTY(process, "moduleLoadList", modules);
2322 Local<Object> versions = Object::New();
2323 READONLY_PROPERTY(process, "versions", versions);
2325 const char http_parser_version[] = NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR)
2327 NODE_STRINGIFY(HTTP_PARSER_VERSION_MINOR);
2328 READONLY_PROPERTY(versions,
2330 FIXED_ONE_BYTE_STRING(node_isolate, http_parser_version));
2332 // +1 to get rid of the leading 'v'
2333 READONLY_PROPERTY(versions,
2335 OneByteString(node_isolate, NODE_VERSION + 1));
2336 READONLY_PROPERTY(versions,
2338 OneByteString(node_isolate, V8::GetVersion()));
2339 READONLY_PROPERTY(versions,
2341 OneByteString(node_isolate, uv_version_string()));
2342 READONLY_PROPERTY(versions,
2344 FIXED_ONE_BYTE_STRING(node_isolate, ZLIB_VERSION));
2346 const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);
2347 READONLY_PROPERTY(versions,
2349 FIXED_ONE_BYTE_STRING(node_isolate, node_modules_version));
2352 // Stupid code to slice out the version string.
2353 int c, l = strlen(OPENSSL_VERSION_TEXT);
2354 for (i = j = 0; i < l; i++) {
2355 c = OPENSSL_VERSION_TEXT[i];
2356 if ('0' <= c && c <= '9') {
2357 for (j = i + 1; j < l; j++) {
2358 c = OPENSSL_VERSION_TEXT[j];
2359 if (c == ' ') break;
2367 OneByteString(node_isolate, &OPENSSL_VERSION_TEXT[i], j - i));
2373 READONLY_PROPERTY(process, "arch", OneByteString(node_isolate, ARCH));
2376 READONLY_PROPERTY(process,
2378 OneByteString(node_isolate, PLATFORM));
2381 Local<Array> arguments = Array::New(argc - option_end_index + 1);
2382 arguments->Set(0, String::NewFromUtf8(node_isolate, argv[0]));
2383 for (j = 1, i = option_end_index; i < argc; j++, i++) {
2384 arguments->Set(j, String::NewFromUtf8(node_isolate, argv[i]));
2386 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "argv"), arguments);
2389 Local<Array> exec_argv = Array::New(option_end_index - 1);
2390 for (j = 1, i = 0; j < option_end_index; j++, i++) {
2391 exec_argv->Set(i, String::NewFromUtf8(node_isolate, argv[j]));
2393 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execArgv"), exec_argv);
2395 // create process.env
2396 Local<ObjectTemplate> envTemplate = ObjectTemplate::New();
2397 envTemplate->SetNamedPropertyHandler(EnvGetter,
2403 Local<Object> env = envTemplate->NewInstance();
2404 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "env"), env);
2406 READONLY_PROPERTY(process, "pid", Integer::New(getpid(), node_isolate));
2407 READONLY_PROPERTY(process, "features", GetFeatures());
2408 process->SetAccessor(
2409 FIXED_ONE_BYTE_STRING(node_isolate, "_needImmediateCallback"),
2410 NeedImmediateCallbackGetter,
2411 NeedImmediateCallbackSetter);
2415 READONLY_PROPERTY(process,
2417 String::NewFromUtf8(node_isolate, eval_string));
2422 READONLY_PROPERTY(process, "_print_eval", True(node_isolate));
2425 // -i, --interactive
2427 READONLY_PROPERTY(process, "_forceRepl", True(node_isolate));
2431 if (no_deprecation) {
2432 READONLY_PROPERTY(process, "noDeprecation", True(node_isolate));
2435 // --throw-deprecation
2436 if (throw_deprecation) {
2437 READONLY_PROPERTY(process, "throwDeprecation", True(node_isolate));
2440 // --trace-deprecation
2441 if (trace_deprecation) {
2442 READONLY_PROPERTY(process, "traceDeprecation", True(node_isolate));
2445 size_t exec_path_len = 2 * PATH_MAX;
2446 char* exec_path = new char[exec_path_len];
2447 Local<String> exec_path_value;
2448 if (uv_exepath(exec_path, &exec_path_len) == 0) {
2449 exec_path_value = String::NewFromUtf8(node_isolate,
2451 String::kNormalString,
2454 exec_path_value = String::NewFromUtf8(node_isolate, argv[0]);
2456 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execPath"),
2460 process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "debugPort"),
2464 // define various internal methods
2465 NODE_SET_METHOD(process, "_getActiveRequests", GetActiveRequests);
2466 NODE_SET_METHOD(process, "_getActiveHandles", GetActiveHandles);
2467 NODE_SET_METHOD(process, "reallyExit", Exit);
2468 NODE_SET_METHOD(process, "abort", Abort);
2469 NODE_SET_METHOD(process, "chdir", Chdir);
2470 NODE_SET_METHOD(process, "cwd", Cwd);
2472 NODE_SET_METHOD(process, "umask", Umask);
2474 #if defined(__POSIX__) && !defined(__ANDROID__)
2475 NODE_SET_METHOD(process, "getuid", GetUid);
2476 NODE_SET_METHOD(process, "setuid", SetUid);
2478 NODE_SET_METHOD(process, "setgid", SetGid);
2479 NODE_SET_METHOD(process, "getgid", GetGid);
2481 NODE_SET_METHOD(process, "getgroups", GetGroups);
2482 NODE_SET_METHOD(process, "setgroups", SetGroups);
2483 NODE_SET_METHOD(process, "initgroups", InitGroups);
2484 #endif // __POSIX__ && !defined(__ANDROID__)
2486 NODE_SET_METHOD(process, "_kill", Kill);
2488 NODE_SET_METHOD(process, "_debugProcess", DebugProcess);
2489 NODE_SET_METHOD(process, "_debugPause", DebugPause);
2490 NODE_SET_METHOD(process, "_debugEnd", DebugEnd);
2492 NODE_SET_METHOD(process, "hrtime", Hrtime);
2494 NODE_SET_METHOD(process, "dlopen", DLOpen);
2496 NODE_SET_METHOD(process, "uptime", Uptime);
2497 NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
2499 NODE_SET_METHOD(process, "binding", Binding);
2501 NODE_SET_METHOD(process, "_setupDomainUse", SetupDomainUse);
2503 // values use to cross communicate with processNextTick
2504 Local<Object> info_box = Object::New();
2505 info_box->SetIndexedPropertiesToExternalArrayData(&tick_infobox,
2506 kExternalUnsignedIntArray,
2508 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickInfoBox"), info_box);
2510 // pre-set _events object for faster emit checks
2511 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_events"), Object::New());
2513 return scope.Close(process);
2517 #undef READONLY_PROPERTY
2520 static void AtExit() {
2521 uv_tty_reset_mode();
2525 static void SignalExit(int signal) {
2526 uv_tty_reset_mode();
2527 _exit(128 + signal);
2531 void Load(Handle<Object> process_l) {
2532 HandleScope handle_scope(node_isolate);
2534 process_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "process");
2535 domain_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "domain");
2537 // Compile, execute the src/node.js file. (Which was included as static C
2538 // string in node_natives.h. 'natve_node' is the string containing that
2541 // The node.js file returns a function 'f'
2546 // Disable verbose mode to stop FatalException() handler from trying
2547 // to handle the exception. Errors this early in the start-up phase
2548 // are not safe to ignore.
2549 try_catch.SetVerbose(false);
2551 Local<String> script_name = FIXED_ONE_BYTE_STRING(node_isolate, "node.js");
2552 Local<Value> f_value = ExecuteString(MainSource(), script_name);
2553 if (try_catch.HasCaught()) {
2554 ReportException(try_catch);
2557 assert(f_value->IsFunction());
2558 Local<Function> f = Local<Function>::Cast(f_value);
2560 // Now we call 'f' with the 'process' variable that we've built up with
2561 // all our bindings. Inside node.js we'll take care of assigning things to
2564 // We start the process this way in order to be more modular. Developers
2565 // who do not like how 'src/node.js' setups the module system but do like
2566 // Node's I/O bindings may want to replace 'f' with their own function.
2568 // Add a reference to the global object
2569 Local<Object> global = v8::Context::GetCurrent()->Global();
2571 #if defined HAVE_DTRACE || defined HAVE_ETW || defined HAVE_SYSTEMTAP
2575 #if defined HAVE_PERFCTR
2576 InitPerfCounters(global);
2579 // Enable handling of uncaught exceptions
2580 // (FatalException(), break on uncaught exception in debugger)
2582 // This is not strictly necessary since it's almost impossible
2583 // to attach the debugger fast enought to break on exception
2584 // thrown during process startup.
2585 try_catch.SetVerbose(true);
2587 Local<Value> arg = process_l;
2588 f->Call(global, 1, &arg);
2591 static void PrintHelp();
2593 static void ParseDebugOpt(const char* arg) {
2596 if (strstr(arg, "--debug-port=") == arg) {
2597 p = 1 + strchr(arg, '=');
2598 debug_port = atoi(p);
2600 use_debug_agent = true;
2601 if (!strcmp(arg, "--debug-brk")) {
2602 debug_wait_connect = true;
2604 } else if (!strcmp(arg, "--debug")) {
2606 } else if (strstr(arg, "--debug-brk=") == arg) {
2607 debug_wait_connect = true;
2608 p = 1 + strchr(arg, '=');
2609 debug_port = atoi(p);
2610 } else if (strstr(arg, "--debug=") == arg) {
2611 p = 1 + strchr(arg, '=');
2612 debug_port = atoi(p);
2615 if (p && debug_port > 1024 && debug_port < 65536)
2618 fprintf(stderr, "Bad debug option.\n");
2619 if (p) fprintf(stderr, "Debug port must be in range 1025 to 65535.\n");
2625 static void PrintHelp() {
2626 printf("Usage: node [options] [ -e script | script.js ] [arguments] \n"
2627 " node debug script.js [arguments] \n"
2630 " -v, --version print node's version\n"
2631 " -e, --eval script evaluate script\n"
2632 " -p, --print evaluate script and print result\n"
2633 " -i, --interactive always enter the REPL even if stdin\n"
2634 " does not appear to be a terminal\n"
2635 " --no-deprecation silence deprecation warnings\n"
2636 " --trace-deprecation show stack traces on deprecations\n"
2637 " --v8-options print v8 command line options\n"
2638 " --max-stack-size=val set max v8 stack size (bytes)\n"
2640 "Environment variables:\n"
2642 "NODE_PATH ';'-separated list of directories\n"
2644 "NODE_PATH ':'-separated list of directories\n"
2646 " prefixed to the module search path.\n"
2647 "NODE_MODULE_CONTEXTS Set to 1 to load modules in their own\n"
2648 " global contexts.\n"
2649 "NODE_DISABLE_COLORS Set to 1 to disable colors in the REPL\n"
2651 "Documentation can be found at http://nodejs.org/\n");
2655 // Parse node command line arguments.
2656 static void ParseArgs(int argc, char **argv) {
2659 // TODO(bnoordhuis) use parse opts
2660 for (i = 1; i < argc; i++) {
2661 const char *arg = argv[i];
2662 if (strstr(arg, "--debug") == arg) {
2664 argv[i] = const_cast<char*>("");
2665 } else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
2666 printf("%s\n", NODE_VERSION);
2668 } else if (strstr(arg, "--max-stack-size=") == arg) {
2670 p = 1 + strchr(arg, '=');
2671 max_stack_size = atoi(p);
2672 argv[i] = const_cast<char*>("");
2673 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
2676 } else if (strcmp(arg, "--eval") == 0 ||
2677 strcmp(arg, "-e") == 0 ||
2678 strcmp(arg, "--print") == 0 ||
2679 strcmp(arg, "-pe") == 0 ||
2680 strcmp(arg, "-p") == 0) {
2681 bool is_eval = strchr(arg, 'e') != NULL;
2682 bool is_print = strchr(arg, 'p') != NULL;
2684 // argument to -p and --print is optional
2685 if (is_eval == true && i + 1 >= argc) {
2686 fprintf(stderr, "Error: %s requires an argument\n", arg);
2690 print_eval = print_eval || is_print;
2691 argv[i] = const_cast<char*>("");
2693 // --eval, -e and -pe always require an argument
2694 if (is_eval == true) {
2695 eval_string = argv[++i];
2699 // next arg is the expression to evaluate unless it starts with:
2700 // - a dash, then it's another switch
2701 // - "\\-", then it's an escaped expression, drop the backslash
2702 if (argv[i + 1] == NULL) continue;
2703 if (argv[i + 1][0] == '-') continue;
2704 eval_string = argv[++i];
2705 if (strncmp(eval_string, "\\-", 2) == 0) ++eval_string;
2706 } else if (strcmp(arg, "--interactive") == 0 || strcmp(arg, "-i") == 0) {
2708 argv[i] = const_cast<char*>("");
2709 } else if (strcmp(arg, "--v8-options") == 0) {
2710 argv[i] = const_cast<char*>("--help");
2711 } else if (strcmp(arg, "--no-deprecation") == 0) {
2712 argv[i] = const_cast<char*>("");
2713 no_deprecation = true;
2714 } else if (strcmp(arg, "--trace-deprecation") == 0) {
2715 argv[i] = const_cast<char*>("");
2716 trace_deprecation = true;
2717 } else if (strcmp(arg, "--throw-deprecation") == 0) {
2718 argv[i] = const_cast<char*>("");
2719 throw_deprecation = true;
2720 } else if (argv[i][0] != '-') {
2725 option_end_index = i;
2729 // Called from the main thread.
2730 static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle, int status) {
2731 v8::Debug::ProcessDebugMessages();
2735 // Called from V8 Debug Agent TCP thread.
2736 static void DispatchMessagesDebugAgentCallback() {
2737 uv_async_send(&dispatch_debug_messages_async);
2741 // Called from the main thread
2742 static void EmitDebugEnabledAsyncCallback(uv_async_t* handle, int status) {
2743 HandleScope handle_scope(node_isolate);
2744 Local<Object> obj = Object::New();
2745 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "cmd"),
2746 FIXED_ONE_BYTE_STRING(node_isolate, "NODE_DEBUG_ENABLED"));
2747 Local<Value> args[] = {
2748 FIXED_ONE_BYTE_STRING(node_isolate, "internalMessage"),
2751 MakeCallback(process_p, "emit", ARRAY_SIZE(args), args);
2755 // Called from the signal watcher callback
2756 static void EmitDebugEnabled() {
2757 uv_async_send(&emit_debug_enabled_async);
2761 static void EnableDebug(bool wait_connect) {
2762 // If we're called from another thread, make sure to enter the right
2764 node_isolate->Enter();
2766 v8::Debug::SetDebugMessageDispatchHandler(DispatchMessagesDebugAgentCallback,
2769 // Start the debug thread and it's associated TCP server on port 5858.
2770 bool r = v8::Debug::EnableAgent("node " NODE_VERSION,
2774 // Crappy check that everything went well. FIXME
2777 // Print out some information.
2778 fprintf(stderr, "debugger listening on port %d\n", debug_port);
2781 debugger_running = true;
2783 // Do not emit NODE_DEBUG_ENABLED when debugger is enabled before starting
2784 // the main process (i.e. when called via `node --debug`)
2785 if (!process_p.IsEmpty())
2788 node_isolate->Exit();
2793 static void EnableDebugSignalHandler(uv_signal_t* handle, int) {
2794 // Break once process will return execution to v8
2795 v8::Debug::DebugBreak(node_isolate);
2797 if (!debugger_running) {
2798 fprintf(stderr, "Hit SIGUSR1 - starting debugger agent.\n");
2804 static void RegisterSignalHandler(int signal, void (*handler)(int signal)) {
2805 struct sigaction sa;
2807 memset(&sa, 0, sizeof(sa));
2808 sa.sa_handler = handler;
2809 sigfillset(&sa.sa_mask);
2810 sigaction(signal, &sa, NULL);
2814 void DebugProcess(const FunctionCallbackInfo<Value>& args) {
2815 HandleScope scope(node_isolate);
2817 if (args.Length() != 1) {
2818 return ThrowError("Invalid number of arguments.");
2824 pid = args[0]->IntegerValue();
2825 r = kill(pid, SIGUSR1);
2827 return ThrowErrnoException(errno, "kill");
2834 DWORD WINAPI EnableDebugThreadProc(void* arg) {
2835 // Break once process will return execution to v8
2836 if (!debugger_running) {
2837 for (int i = 0; i < 1; i++) {
2838 fprintf(stderr, "Starting debugger agent.\r\n");
2844 v8::Debug::DebugBreak(node_isolate);
2850 static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
2852 return _snwprintf(buf, buf_len, L"node-debug-handler-%u", pid);
2856 static int RegisterDebugSignalHandler() {
2857 wchar_t mapping_name[32];
2858 HANDLE mapping_handle;
2860 LPTHREAD_START_ROUTINE* handler;
2862 pid = GetCurrentProcessId();
2864 if (GetDebugSignalHandlerMappingName(pid,
2866 ARRAY_SIZE(mapping_name)) < 0) {
2870 mapping_handle = CreateFileMappingW(INVALID_HANDLE_VALUE,
2876 if (mapping_handle == NULL) {
2880 handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
2881 MapViewOfFile(mapping_handle,
2882 FILE_MAP_ALL_ACCESS,
2886 if (handler == NULL) {
2887 CloseHandle(mapping_handle);
2891 *handler = EnableDebugThreadProc;
2893 UnmapViewOfFile(static_cast<void*>(handler));
2899 static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
2900 HandleScope scope(node_isolate);
2902 HANDLE process = NULL;
2903 HANDLE thread = NULL;
2904 HANDLE mapping = NULL;
2905 wchar_t mapping_name[32];
2906 LPTHREAD_START_ROUTINE* handler = NULL;
2908 if (args.Length() != 1) {
2909 ThrowError("Invalid number of arguments.");
2913 pid = (DWORD) args[0]->IntegerValue();
2915 process = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION |
2916 PROCESS_VM_OPERATION | PROCESS_VM_WRITE |
2920 if (process == NULL) {
2921 ThrowException(WinapiErrnoException(GetLastError(), "OpenProcess"));
2925 if (GetDebugSignalHandlerMappingName(pid,
2927 ARRAY_SIZE(mapping_name)) < 0) {
2928 ThrowErrnoException(errno, "sprintf");
2932 mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name);
2933 if (mapping == NULL) {
2934 ThrowException(WinapiErrnoException(GetLastError(), "OpenFileMappingW"));
2938 handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
2939 MapViewOfFile(mapping,
2944 if (handler == NULL || *handler == NULL) {
2945 ThrowException(WinapiErrnoException(GetLastError(), "MapViewOfFile"));
2949 thread = CreateRemoteThread(process,
2956 if (thread == NULL) {
2957 ThrowException(WinapiErrnoException(GetLastError(), "CreateRemoteThread"));
2961 // Wait for the thread to terminate
2962 if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) {
2963 ThrowException(WinapiErrnoException(GetLastError(), "WaitForSingleObject"));
2968 if (process != NULL)
2969 CloseHandle(process);
2971 CloseHandle(thread);
2972 if (handler != NULL)
2973 UnmapViewOfFile(handler);
2974 if (mapping != NULL)
2975 CloseHandle(mapping);
2980 static void DebugPause(const FunctionCallbackInfo<Value>& args) {
2981 v8::Debug::DebugBreak(node_isolate);
2985 static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
2986 if (debugger_running) {
2987 v8::Debug::DisableAgent();
2988 debugger_running = false;
2993 char** Init(int argc, char *argv[]) {
2994 // Initialize prog_start_time to get relative uptime.
2995 uv_uptime(&prog_start_time);
2997 // Make inherited handles noninheritable.
2998 uv_disable_stdio_inheritance();
3000 // init async debug messages dispatching
3001 uv_async_init(uv_default_loop(),
3002 &dispatch_debug_messages_async,
3003 DispatchDebugMessagesAsyncCallback);
3004 uv_unref(reinterpret_cast<uv_handle_t*>(&dispatch_debug_messages_async));
3006 // init async NODE_DEBUG_ENABLED emitter
3007 uv_async_init(uv_default_loop(),
3008 &emit_debug_enabled_async,
3009 EmitDebugEnabledAsyncCallback);
3010 uv_unref(reinterpret_cast<uv_handle_t*>(&emit_debug_enabled_async));
3012 // Parse a few arguments which are specific to Node.
3013 node::ParseArgs(argc, argv);
3014 // Parse the rest of the args (up to the 'option_end_index' (where '--' was
3015 // in the command line))
3016 int v8argc = option_end_index;
3017 char **v8argv = argv;
3019 if (debug_wait_connect) {
3020 // v8argv is a copy of argv up to the script file argument +2 if --debug-brk
3021 // to expose the v8 debugger js object so that node.js can set
3022 // a breakpoint on the first line of the startup script
3024 v8argv = new char*[v8argc];
3025 memcpy(v8argv, argv, sizeof(*argv) * option_end_index);
3026 v8argv[option_end_index] = const_cast<char*>("--expose_debug_as");
3027 v8argv[option_end_index + 1] = const_cast<char*>("v8debug");
3030 // For the normal stack which moves from high to low addresses when frames
3031 // are pushed, we can compute the limit as stack_size bytes below the
3032 // the address of a stack variable (e.g. &stack_var) as an approximation
3033 // of the start of the stack (we're assuming that we haven't pushed a lot
3035 if (max_stack_size != 0) {
3037 ResourceConstraints constraints;
3039 uint32_t *stack_limit = &stack_var - (max_stack_size / sizeof(uint32_t));
3040 constraints.set_stack_limit(stack_limit);
3041 SetResourceConstraints(&constraints); // Must be done before V8::Initialize
3043 V8::SetFlagsFromCommandLine(&v8argc, v8argv, false);
3045 const char typed_arrays_flag[] = "--harmony_typed_arrays";
3046 V8::SetFlagsFromString(typed_arrays_flag, sizeof(typed_arrays_flag) - 1);
3047 V8::SetArrayBufferAllocator(&ArrayBufferAllocator::the_singleton);
3049 // Fetch a reference to the main isolate, so we have a reference to it
3050 // even when we need it to access it from another (debugger) thread.
3051 node_isolate = Isolate::GetCurrent();
3055 RegisterSignalHandler(SIGPIPE, SIG_IGN);
3056 RegisterSignalHandler(SIGINT, SignalExit);
3057 RegisterSignalHandler(SIGTERM, SignalExit);
3060 uv_check_init(uv_default_loop(), &check_immediate_watcher);
3061 uv_unref(reinterpret_cast<uv_handle_t*>(&check_immediate_watcher));
3062 uv_idle_init(uv_default_loop(), &idle_immediate_dummy);
3064 V8::SetFatalErrorHandler(node::OnFatalError);
3065 V8::AddMessageListener(OnMessage);
3067 // If the --debug flag was specified then initialize the debug thread.
3068 if (use_debug_agent) {
3069 EnableDebug(debug_wait_connect);
3072 RegisterDebugSignalHandler();
3074 static uv_signal_t signal_watcher;
3075 uv_signal_init(uv_default_loop(), &signal_watcher);
3076 uv_signal_start(&signal_watcher, EnableDebugSignalHandler, SIGUSR1);
3077 uv_unref(reinterpret_cast<uv_handle_t*>(&signal_watcher));
3085 struct AtExitCallback {
3086 AtExitCallback* next_;
3087 void (*cb_)(void* arg);
3091 static AtExitCallback* at_exit_functions_;
3095 AtExitCallback* p = at_exit_functions_;
3096 at_exit_functions_ = NULL;
3099 AtExitCallback* q = p->next_;
3107 void AtExit(void (*cb)(void* arg), void* arg) {
3108 AtExitCallback* p = new AtExitCallback;
3111 p->next_ = at_exit_functions_;
3112 at_exit_functions_ = p;
3116 void EmitExit(v8::Handle<v8::Object> process_l) {
3117 // process.emit('exit')
3118 process_l->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_exiting"),
3119 True(node_isolate));
3120 Local<Value> args[] = {
3121 FIXED_ONE_BYTE_STRING(node_isolate, "exit"),
3122 Integer::New(0, node_isolate)
3124 MakeCallback(process_l, "emit", ARRAY_SIZE(args), args);
3127 static char **copy_argv(int argc, char **argv) {
3135 for (i = 0; i < argc; i++) {
3136 strlen_sum += strlen(argv[i]) + 1;
3139 argv_copy = static_cast<char**>(
3140 malloc(sizeof(*argv_copy) * (argc + 1) + strlen_sum));
3145 argv_data = reinterpret_cast<char*>(argv_copy) +
3146 sizeof(*argv_copy) * (argc + 1);
3148 for (i = 0; i < argc; i++) {
3149 argv_copy[i] = argv_data;
3150 len = strlen(argv[i]) + 1;
3151 memcpy(argv_data, argv[i], len);
3155 argv_copy[argc] = NULL;
3160 int Start(int argc, char *argv[]) {
3161 // Hack aroung with the argv pointer. Used for process.title = "blah".
3162 argv = uv_setup_args(argc, argv);
3164 // Logic to duplicate argv as Init() modifies arguments
3165 // that are passed into it.
3166 char **argv_copy = copy_argv(argc, argv);
3168 // This needs to run *before* V8::Initialize()
3169 // Use copy here as to not modify the original argv:
3170 Init(argc, argv_copy);
3174 Locker locker(node_isolate);
3175 HandleScope handle_scope(node_isolate);
3177 // Create the one and only Context.
3178 Local<Context> context = Context::New(node_isolate);
3179 Context::Scope context_scope(context);
3181 binding_cache.Reset(node_isolate, Object::New());
3183 // Use original argv, as we're just copying values out of it.
3184 Local<Object> process_l = SetupProcessObject(argc, argv);
3186 // Create all the objects, load modules, do everything.
3187 // so your next reading stop should be node::Load()!
3190 // All our arguments are loaded. We've evaluated all of the scripts. We
3191 // might even have created TCP servers. Now we enter the main eventloop. If
3192 // there are no watchers on the loop (except for the ones that were
3193 // uv_unref'd) then this function exits. As long as there are active
3194 // watchers, it blocks.
3195 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
3197 EmitExit(process_l);
3202 // Clean up. Not strictly necessary.
3206 // Clean up the copy: