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 #ifdef OPENSSL_NPN_NEGOTIATED
191 static bool use_npn = true;
193 static bool use_npn = false;
196 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
197 static bool use_sni = true;
199 static bool use_sni = false;
202 // process-relative uptime base, initialized at start-up
203 static double prog_start_time;
205 static volatile bool debugger_running = false;
206 static uv_async_t dispatch_debug_messages_async;
207 static uv_async_t emit_debug_enabled_async;
209 // Declared in node_internals.h
210 Isolate* node_isolate = NULL;
213 class ArrayBufferAllocator : public ArrayBuffer::Allocator {
215 // Impose an upper limit to avoid out of memory errors that bring down
217 static const size_t kMaxLength = 0x3fffffff;
218 static ArrayBufferAllocator the_singleton;
219 virtual ~ArrayBufferAllocator() {}
220 virtual void* Allocate(size_t length);
221 virtual void Free(void* data);
223 ArrayBufferAllocator() {}
224 ArrayBufferAllocator(const ArrayBufferAllocator&);
225 void operator=(const ArrayBufferAllocator&);
228 ArrayBufferAllocator ArrayBufferAllocator::the_singleton;
231 void* ArrayBufferAllocator::Allocate(size_t length) {
232 if (length > kMaxLength) return NULL;
233 return new char[length];
237 void ArrayBufferAllocator::Free(void* data) {
238 delete[] static_cast<char*>(data);
242 static void CheckImmediate(uv_check_t* handle, int status) {
243 assert(handle == &check_immediate_watcher);
246 HandleScope scope(node_isolate);
248 if (immediate_callback_sym.IsEmpty()) {
249 immediate_callback_sym =
250 FIXED_ONE_BYTE_STRING(node_isolate, "_immediateCallback");
253 MakeCallback(process_p, immediate_callback_sym, 0, NULL);
257 static void IdleImmediateDummy(uv_idle_t* handle, int status) {
258 // Do nothing. Only for maintaining event loop
259 assert(handle == &idle_immediate_dummy);
264 static inline const char *errno_string(int errorno) {
265 #define ERRNO_CASE(e) case e: return #e;
272 ERRNO_CASE(EADDRINUSE);
276 ERRNO_CASE(EADDRNOTAVAIL);
280 ERRNO_CASE(EAFNOSUPPORT);
288 # if EAGAIN != EWOULDBLOCK
289 ERRNO_CASE(EWOULDBLOCK);
294 ERRNO_CASE(EALREADY);
310 ERRNO_CASE(ECANCELED);
318 ERRNO_CASE(ECONNABORTED);
322 ERRNO_CASE(ECONNREFUSED);
326 ERRNO_CASE(ECONNRESET);
334 ERRNO_CASE(EDESTADDRREQ);
358 ERRNO_CASE(EHOSTUNREACH);
370 ERRNO_CASE(EINPROGRESS);
406 ERRNO_CASE(EMSGSIZE);
410 ERRNO_CASE(EMULTIHOP);
414 ERRNO_CASE(ENAMETOOLONG);
418 ERRNO_CASE(ENETDOWN);
422 ERRNO_CASE(ENETRESET);
426 ERRNO_CASE(ENETUNREACH);
458 # if ENOLINK != ENOLCK
472 ERRNO_CASE(ENOPROTOOPT);
492 ERRNO_CASE(ENOTCONN);
500 ERRNO_CASE(ENOTEMPTY);
504 ERRNO_CASE(ENOTSOCK);
511 ERRNO_CASE(EOPNOTSUPP);
525 ERRNO_CASE(EOVERFLOW);
540 #ifdef EPROTONOSUPPORT
541 ERRNO_CASE(EPROTONOSUPPORT);
545 ERRNO_CASE(EPROTOTYPE);
573 ERRNO_CASE(ETIMEDOUT);
588 const char *signo_string(int signo) {
589 #define SIGNO_CASE(e) case e: return #e;
616 # if SIGABRT != SIGIOT
660 SIGNO_CASE(SIGSTKFLT);
677 SIGNO_CASE(SIGBREAK);
701 SIGNO_CASE(SIGVTALRM);
709 SIGNO_CASE(SIGWINCH);
717 # if SIGPOLL != SIGIO
727 # if SIGPWR != SIGLOST
741 Local<Value> ErrnoException(int errorno,
746 Local<String> estring = OneByteString(node_isolate, errno_string(errorno));
747 if (msg == NULL || msg[0] == '\0') {
748 msg = strerror(errorno);
750 Local<String> message = OneByteString(node_isolate, msg);
752 Local<String> cons1 =
753 String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", "));
754 Local<String> cons2 = String::Concat(cons1, message);
756 if (syscall_symbol.IsEmpty()) {
757 syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall");
758 errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno");
759 errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path");
760 code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code");
764 Local<String> cons3 =
765 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
766 Local<String> cons4 =
767 String::Concat(cons3, String::NewFromUtf8(node_isolate, path));
768 Local<String> cons5 =
769 String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
770 e = Exception::Error(cons5);
772 e = Exception::Error(cons2);
775 Local<Object> obj = e->ToObject();
777 obj->Set(errno_symbol, Integer::New(errorno, node_isolate));
778 obj->Set(code_symbol, estring);
779 if (path) obj->Set(errpath_symbol, String::NewFromUtf8(node_isolate, path));
780 if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall));
785 // hack alert! copy of ErrnoException, tuned for uv errors
786 Local<Value> UVException(int errorno,
790 if (syscall_symbol.IsEmpty()) {
791 syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall");
792 errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno");
793 errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path");
794 code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code");
798 msg = uv_strerror(errorno);
800 Local<String> estring = OneByteString(node_isolate, uv_err_name(errorno));
801 Local<String> message = OneByteString(node_isolate, msg);
802 Local<String> cons1 =
803 String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", "));
804 Local<String> cons2 = String::Concat(cons1, message);
808 Local<String> path_str;
812 if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) {
813 path_str = String::Concat(FIXED_ONE_BYTE_STRING(node_isolate, "\\\\"),
814 String::NewFromUtf8(node_isolate, path + 8));
815 } else if (strncmp(path, "\\\\?\\", 4) == 0) {
816 path_str = String::NewFromUtf8(node_isolate, path + 4);
818 path_str = String::NewFromUtf8(node_isolate, path);
821 path_str = String::NewFromUtf8(node_isolate, path);
824 Local<String> cons3 =
825 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
826 Local<String> cons4 =
827 String::Concat(cons3, path_str);
828 Local<String> cons5 =
829 String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
830 e = Exception::Error(cons5);
832 e = Exception::Error(cons2);
835 Local<Object> obj = e->ToObject();
837 // TODO(piscisaureus) errno should probably go
838 obj->Set(errno_symbol, Integer::New(errorno, node_isolate));
839 obj->Set(code_symbol, estring);
840 if (path) obj->Set(errpath_symbol, path_str);
841 if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall));
847 // Does about the same as strerror(),
848 // but supports all windows error messages
849 static const char *winapi_strerror(const int errorno) {
852 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
853 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
854 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
857 // Remove trailing newlines
858 for (int i = strlen(errmsg) - 1;
859 i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) {
865 // FormatMessage failed
866 return "Unknown error";
871 Local<Value> WinapiErrnoException(int errorno,
876 if (!msg || !msg[0]) {
877 msg = winapi_strerror(errorno);
879 Local<String> message = OneByteString(node_isolate, msg);
881 if (syscall_symbol.IsEmpty()) {
882 syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall");
883 errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno");
884 errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path");
885 code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code");
889 Local<String> cons1 =
890 String::Concat(message, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
891 Local<String> cons2 =
892 String::Concat(cons1, String::NewFromUtf8(node_isolate, path));
893 Local<String> cons3 =
894 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
895 e = Exception::Error(cons3);
897 e = Exception::Error(message);
900 Local<Object> obj = e->ToObject();
902 obj->Set(errno_symbol, Integer::New(errorno, node_isolate));
903 if (path) obj->Set(errpath_symbol, String::NewFromUtf8(node_isolate, path));
904 if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall));
910 void SetupDomainUse(const FunctionCallbackInfo<Value>& args) {
911 if (using_domains) return;
912 HandleScope scope(node_isolate);
913 using_domains = true;
914 Local<Object> process = PersistentToLocal(node_isolate, process_p);
916 process->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_tickDomainCallback"));
917 if (!tdc_v->IsFunction()) {
918 fprintf(stderr, "process._tickDomainCallback assigned to non-function\n");
921 Local<Function> tdc = tdc_v.As<Function>();
922 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback"), tdc);
923 process_tickCallback.Reset(node_isolate, tdc);
924 if (!args[0]->IsArray()) {
925 fprintf(stderr, "_setupDomainUse first argument must be an array\n");
928 p_domain_box.Reset(node_isolate, args[0].As<Array>());
929 if (!args[1]->IsObject()) {
930 fprintf(stderr, "_setupDomainUse second argument must be an object\n");
933 Local<Object> flag = args[1].As<Object>();
934 flag->SetIndexedPropertiesToExternalArrayData(&domain_flag,
935 kExternalUnsignedIntArray,
941 return using_domains && domain_flag.count > 0;
945 Handle<Value> GetDomain() {
946 // no domain can exist if no domain module has been loaded
947 if (!InDomain() || p_domain_box.IsEmpty())
948 return Null(node_isolate);
950 return PersistentToLocal(node_isolate, p_domain_box)->Get(0);
955 MakeDomainCallback(const Handle<Object> object,
956 const Handle<Function> callback,
958 Handle<Value> argv[]) {
959 // TODO(trevnorris) Hook for long stack traces to be made here.
961 // lazy load domain specific symbols
962 if (enter_symbol.IsEmpty()) {
963 enter_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "enter");
964 exit_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "exit");
965 disposed_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "_disposed");
968 Local<Value> domain_v = object->Get(domain_symbol);
969 Local<Object> domain;
970 Local<Function> enter;
971 Local<Function> exit;
974 try_catch.SetVerbose(true);
976 bool has_domain = domain_v->IsObject();
978 domain = domain_v->ToObject();
979 assert(!domain.IsEmpty());
980 if (domain->Get(disposed_symbol)->IsTrue()) {
981 // domain has been disposed of.
982 return Undefined(node_isolate);
984 enter = Local<Function>::Cast(domain->Get(enter_symbol));
985 assert(!enter.IsEmpty());
986 enter->Call(domain, 0, NULL);
988 if (try_catch.HasCaught()) {
989 return Undefined(node_isolate);
993 Local<Value> ret = callback->Call(object, argc, argv);
995 if (try_catch.HasCaught()) {
996 return Undefined(node_isolate);
1000 exit = Local<Function>::Cast(domain->Get(exit_symbol));
1001 assert(!exit.IsEmpty());
1002 exit->Call(domain, 0, NULL);
1004 if (try_catch.HasCaught()) {
1005 return Undefined(node_isolate);
1009 if (tick_infobox.last_threw == 1) {
1010 tick_infobox.last_threw = 0;
1014 if (tick_infobox.in_tick == 1) {
1018 if (tick_infobox.length == 0) {
1019 tick_infobox.index = 0;
1023 // process nextTicks after call
1024 Local<Object> process = PersistentToLocal(node_isolate, process_p);
1025 Local<Function> fn = PersistentToLocal(node_isolate, process_tickCallback);
1026 fn->Call(process, 0, NULL);
1028 if (try_catch.HasCaught()) {
1029 return Undefined(node_isolate);
1037 MakeCallback(const Handle<Object> object,
1038 const Handle<Function> callback,
1040 Handle<Value> argv[]) {
1041 // TODO(trevnorris) Hook for long stack traces to be made here.
1042 Local<Object> process = PersistentToLocal(node_isolate, process_p);
1045 return MakeDomainCallback(object, callback, argc, argv);
1047 // lazy load no domain next tick callbacks
1048 if (process_tickCallback.IsEmpty()) {
1050 process->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback"));
1051 if (!cb_v->IsFunction()) {
1052 fprintf(stderr, "process._tickCallback assigned to non-function\n");
1055 process_tickCallback.Reset(node_isolate, cb_v.As<Function>());
1059 try_catch.SetVerbose(true);
1061 Local<Value> ret = callback->Call(object, argc, argv);
1063 if (try_catch.HasCaught()) {
1064 return Undefined(node_isolate);
1067 if (tick_infobox.in_tick == 1) {
1071 if (tick_infobox.length == 0) {
1072 tick_infobox.index = 0;
1076 // process nextTicks after call
1077 Local<Function> fn = PersistentToLocal(node_isolate, process_tickCallback);
1078 fn->Call(process, 0, NULL);
1080 if (try_catch.HasCaught()) {
1081 return Undefined(node_isolate);
1090 MakeCallback(const Handle<Object> object,
1093 Handle<Value> argv[]) {
1094 HandleScope scope(node_isolate);
1096 Local<Function> callback = object->Get(index).As<Function>();
1097 assert(callback->IsFunction());
1100 return scope.Close(MakeDomainCallback(object, callback, argc, argv));
1101 return scope.Close(MakeCallback(object, callback, argc, argv));
1106 MakeCallback(const Handle<Object> object,
1107 const Handle<String> symbol,
1109 Handle<Value> argv[]) {
1110 HandleScope scope(node_isolate);
1112 Local<Function> callback = object->Get(symbol).As<Function>();
1113 assert(callback->IsFunction());
1116 return scope.Close(MakeDomainCallback(object, callback, argc, argv));
1117 return scope.Close(MakeCallback(object, callback, argc, argv));
1122 MakeCallback(const Handle<Object> object,
1125 Handle<Value> argv[]) {
1126 HandleScope scope(node_isolate);
1128 Local<String> method_string = OneByteString(node_isolate, method);
1129 Handle<Value> ret = MakeCallback(object, method_string, argc, argv);
1131 return scope.Close(ret);
1135 enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
1136 HandleScope scope(node_isolate);
1138 if (!encoding_v->IsString()) return _default;
1140 String::Utf8Value encoding(encoding_v);
1142 if (strcasecmp(*encoding, "utf8") == 0) {
1144 } else if (strcasecmp(*encoding, "utf-8") == 0) {
1146 } else if (strcasecmp(*encoding, "ascii") == 0) {
1148 } else if (strcasecmp(*encoding, "base64") == 0) {
1150 } else if (strcasecmp(*encoding, "ucs2") == 0) {
1152 } else if (strcasecmp(*encoding, "ucs-2") == 0) {
1154 } else if (strcasecmp(*encoding, "utf16le") == 0) {
1156 } else if (strcasecmp(*encoding, "utf-16le") == 0) {
1158 } else if (strcasecmp(*encoding, "binary") == 0) {
1160 } else if (strcasecmp(*encoding, "buffer") == 0) {
1162 } else if (strcasecmp(*encoding, "hex") == 0) {
1164 } else if (strcasecmp(*encoding, "raw") == 0) {
1165 if (!no_deprecation) {
1166 fprintf(stderr, "'raw' (array of integers) has been removed. "
1170 } else if (strcasecmp(*encoding, "raws") == 0) {
1171 if (!no_deprecation) {
1172 fprintf(stderr, "'raws' encoding has been renamed to 'binary'. "
1173 "Please update your code.\n");
1181 Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
1182 return StringBytes::Encode(static_cast<const char*>(buf),
1187 // Returns -1 if the handle was not valid for decoding
1188 ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
1189 HandleScope scope(node_isolate);
1191 if (val->IsArray()) {
1192 fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
1198 return StringBytes::Size(val, encoding);
1202 # define MIN(a, b) ((a) < (b) ? (a) : (b))
1205 // Returns number of bytes written.
1206 ssize_t DecodeWrite(char *buf,
1208 v8::Handle<v8::Value> val,
1209 enum encoding encoding) {
1210 return StringBytes::Write(buf, buflen, val, encoding, NULL);
1213 void DisplayExceptionLine(Handle<Message> message) {
1214 // Prevent re-entry into this function. For example, if there is
1215 // a throw from a program in vm.runInThisContext(code, filename, true),
1216 // then we want to show the original failure, not the secondary one.
1217 static bool displayed_error = false;
1219 if (displayed_error) return;
1220 displayed_error = true;
1222 uv_tty_reset_mode();
1224 fprintf(stderr, "\n");
1226 if (!message.IsEmpty()) {
1227 // Print (filename):(line number): (message).
1228 String::Utf8Value filename(message->GetScriptResourceName());
1229 const char* filename_string = *filename;
1230 int linenum = message->GetLineNumber();
1231 fprintf(stderr, "%s:%i\n", filename_string, linenum);
1232 // Print line of source code.
1233 String::Utf8Value sourceline(message->GetSourceLine());
1234 const char* sourceline_string = *sourceline;
1236 // Because of how node modules work, all scripts are wrapped with a
1237 // "function (module, exports, __filename, ...) {"
1238 // to provide script local variables.
1240 // When reporting errors on the first line of a script, this wrapper
1241 // function is leaked to the user. There used to be a hack here to
1242 // truncate off the first 62 characters, but it caused numerous other
1243 // problems when vm.runIn*Context() methods were used for non-module
1246 // If we ever decide to re-instate such a hack, the following steps
1249 // 1. Pass a flag around to say "this code was wrapped"
1250 // 2. Update the stack frame output so that it is also correct.
1252 // It would probably be simpler to add a line rather than add some
1253 // number of characters to the first line, since V8 truncates the
1254 // sourceline to 78 characters, and we end up not providing very much
1255 // useful debugging info to the user if we remove 62 characters.
1257 int start = message->GetStartColumn();
1258 int end = message->GetEndColumn();
1260 fprintf(stderr, "%s\n", sourceline_string);
1261 // Print wavy underline (GetUnderline is deprecated).
1262 for (int i = 0; i < start; i++) {
1263 fputc((sourceline_string[i] == '\t') ? '\t' : ' ', stderr);
1265 for (int i = start; i < end; i++) {
1268 fputc('\n', stderr);
1273 static void ReportException(Handle<Value> er, Handle<Message> message) {
1274 HandleScope scope(node_isolate);
1276 DisplayExceptionLine(message);
1278 Local<Value> trace_value(
1279 er->ToObject()->Get(FIXED_ONE_BYTE_STRING(node_isolate, "stack")));
1280 String::Utf8Value trace(trace_value);
1282 // range errors have a trace member set to undefined
1283 if (trace.length() > 0 && !trace_value->IsUndefined()) {
1284 fprintf(stderr, "%s\n", *trace);
1286 // this really only happens for RangeErrors, since they're the only
1287 // kind that won't have all this info in the trace, or when non-Error
1288 // objects are thrown manually.
1289 Local<Value> message;
1292 if (er->IsObject()) {
1293 Local<Object> err_obj = er.As<Object>();
1294 message = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "message"));
1295 name = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "name"));
1298 if (message.IsEmpty() ||
1299 message->IsUndefined() ||
1301 name->IsUndefined()) {
1302 // Not an error object. Just print as-is.
1303 String::Utf8Value message(er);
1304 fprintf(stderr, "%s\n", *message);
1306 String::Utf8Value name_string(name);
1307 String::Utf8Value message_string(message);
1308 fprintf(stderr, "%s: %s\n", *name_string, *message_string);
1316 static void ReportException(const TryCatch& try_catch) {
1317 ReportException(try_catch.Exception(), try_catch.Message());
1321 // Executes a str within the current v8 context.
1322 Local<Value> ExecuteString(Handle<String> source, Handle<Value> filename) {
1323 HandleScope scope(node_isolate);
1326 // try_catch must be nonverbose to disable FatalException() handler,
1327 // we will handle exceptions ourself.
1328 try_catch.SetVerbose(false);
1330 Local<v8::Script> script = v8::Script::Compile(source, filename);
1331 if (script.IsEmpty()) {
1332 ReportException(try_catch);
1336 Local<Value> result = script->Run();
1337 if (result.IsEmpty()) {
1338 ReportException(try_catch);
1342 return scope.Close(result);
1346 static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
1347 HandleScope scope(node_isolate);
1349 Local<Array> ary = Array::New();
1353 QUEUE_FOREACH(q, &req_wrap_queue) {
1354 ReqWrap<uv_req_t>* w = container_of(q, ReqWrap<uv_req_t>, req_wrap_queue_);
1355 if (w->persistent().IsEmpty()) continue;
1356 ary->Set(i++, w->object());
1359 args.GetReturnValue().Set(ary);
1363 // Non-static, friend of HandleWrap. Could have been a HandleWrap method but
1364 // implemented here for consistency with GetActiveRequests().
1365 void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
1366 HandleScope scope(node_isolate);
1368 Local<Array> ary = Array::New();
1372 Local<String> owner_sym = FIXED_ONE_BYTE_STRING(node_isolate, "owner");
1374 QUEUE_FOREACH(q, &handle_wrap_queue) {
1375 HandleWrap* w = container_of(q, HandleWrap, handle_wrap_queue_);
1376 if (w->persistent().IsEmpty() || (w->flags_ & HandleWrap::kUnref)) continue;
1377 Local<Object> object = w->object();
1378 Local<Value> owner = object->Get(owner_sym);
1379 if (owner->IsUndefined()) owner = object;
1380 ary->Set(i++, owner);
1383 args.GetReturnValue().Set(ary);
1387 static void Abort(const FunctionCallbackInfo<Value>& args) {
1392 static void Chdir(const FunctionCallbackInfo<Value>& args) {
1393 HandleScope scope(node_isolate);
1395 if (args.Length() != 1 || !args[0]->IsString()) {
1396 return ThrowError("Bad argument."); // FIXME(bnoordhuis) ThrowTypeError?
1399 String::Utf8Value path(args[0]);
1400 int err = uv_chdir(*path);
1402 return ThrowUVException(err, "uv_chdir");
1407 static void Cwd(const FunctionCallbackInfo<Value>& args) {
1408 HandleScope scope(node_isolate);
1410 /* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
1411 char buf[MAX_PATH * 4 + 1];
1413 char buf[PATH_MAX + 1];
1416 int err = uv_cwd(buf, ARRAY_SIZE(buf) - 1);
1418 return ThrowUVException(err, "uv_cwd");
1421 buf[ARRAY_SIZE(buf) - 1] = '\0';
1422 Local<String> cwd = String::NewFromUtf8(node_isolate, buf);
1424 args.GetReturnValue().Set(cwd);
1428 static void Umask(const FunctionCallbackInfo<Value>& args) {
1429 HandleScope scope(node_isolate);
1432 if (args.Length() < 1 || args[0]->IsUndefined()) {
1434 umask(static_cast<mode_t>(old));
1435 } else if (!args[0]->IsInt32() && !args[0]->IsString()) {
1436 return ThrowTypeError("argument must be an integer or octal string.");
1439 if (args[0]->IsInt32()) {
1440 oct = args[0]->Uint32Value();
1443 String::Utf8Value str(args[0]);
1445 // Parse the octal string.
1446 for (int i = 0; i < str.length(); i++) {
1448 if (c > '7' || c < '0') {
1449 return ThrowTypeError("invalid octal string");
1455 old = umask(static_cast<mode_t>(oct));
1458 args.GetReturnValue().Set(old);
1462 #if defined(__POSIX__) && !defined(__ANDROID__)
1464 static const uid_t uid_not_found = static_cast<uid_t>(-1);
1465 static const gid_t gid_not_found = static_cast<gid_t>(-1);
1468 static uid_t uid_by_name(const char* name) {
1476 if (getpwnam_r(name, &pwd, buf, sizeof(buf), &pp) == 0 && pp != NULL) {
1480 return uid_not_found;
1484 static char* name_by_uid(uid_t uid) {
1493 if ((rc = getpwuid_r(uid, &pwd, buf, sizeof(buf), &pp)) == 0 && pp != NULL) {
1494 return strdup(pp->pw_name);
1505 static gid_t gid_by_name(const char* name) {
1513 if (getgrnam_r(name, &pwd, buf, sizeof(buf), &pp) == 0 && pp != NULL) {
1517 return gid_not_found;
1521 #if 0 // For future use.
1522 static const char* name_by_gid(gid_t gid) {
1531 if ((rc = getgrgid_r(gid, &pwd, buf, sizeof(buf), &pp)) == 0 && pp != NULL) {
1532 return strdup(pp->gr_name);
1544 static uid_t uid_by_name(Handle<Value> value) {
1545 if (value->IsUint32()) {
1546 return static_cast<uid_t>(value->Uint32Value());
1548 String::Utf8Value name(value);
1549 return uid_by_name(*name);
1554 static gid_t gid_by_name(Handle<Value> value) {
1555 if (value->IsUint32()) {
1556 return static_cast<gid_t>(value->Uint32Value());
1558 String::Utf8Value name(value);
1559 return gid_by_name(*name);
1564 static void GetUid(const FunctionCallbackInfo<Value>& args) {
1565 args.GetReturnValue().Set(getuid());
1569 static void GetGid(const FunctionCallbackInfo<Value>& args) {
1570 args.GetReturnValue().Set(getgid());
1574 static void SetGid(const FunctionCallbackInfo<Value>& args) {
1575 HandleScope scope(node_isolate);
1577 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1578 return ThrowTypeError("setgid argument must be a number or a string");
1581 gid_t gid = gid_by_name(args[0]);
1583 if (gid == gid_not_found) {
1584 return ThrowError("setgid group id does not exist");
1588 return ThrowErrnoException(errno, "setgid");
1593 static void SetUid(const FunctionCallbackInfo<Value>& args) {
1594 HandleScope scope(node_isolate);
1596 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1597 return ThrowTypeError("setuid argument must be a number or a string");
1600 uid_t uid = uid_by_name(args[0]);
1602 if (uid == uid_not_found) {
1603 return ThrowError("setuid user id does not exist");
1607 return ThrowErrnoException(errno, "setuid");
1612 static void GetGroups(const FunctionCallbackInfo<Value>& args) {
1613 HandleScope scope(node_isolate);
1615 int ngroups = getgroups(0, NULL);
1617 if (ngroups == -1) {
1618 return ThrowErrnoException(errno, "getgroups");
1621 gid_t* groups = new gid_t[ngroups];
1623 ngroups = getgroups(ngroups, groups);
1625 if (ngroups == -1) {
1627 return ThrowErrnoException(errno, "getgroups");
1630 Local<Array> groups_list = Array::New(ngroups);
1631 bool seen_egid = false;
1632 gid_t egid = getegid();
1634 for (int i = 0; i < ngroups; i++) {
1635 groups_list->Set(i, Integer::New(groups[i], node_isolate));
1636 if (groups[i] == egid) seen_egid = true;
1641 if (seen_egid == false) {
1642 groups_list->Set(ngroups, Integer::New(egid, node_isolate));
1645 args.GetReturnValue().Set(groups_list);
1649 static void SetGroups(const FunctionCallbackInfo<Value>& args) {
1650 HandleScope scope(node_isolate);
1652 if (!args[0]->IsArray()) {
1653 return ThrowTypeError("argument 1 must be an array");
1656 Local<Array> groups_list = args[0].As<Array>();
1657 size_t size = groups_list->Length();
1658 gid_t* groups = new gid_t[size];
1660 for (size_t i = 0; i < size; i++) {
1661 gid_t gid = gid_by_name(groups_list->Get(i));
1663 if (gid == gid_not_found) {
1665 return ThrowError("group name not found");
1671 int rc = setgroups(size, groups);
1675 return ThrowErrnoException(errno, "setgroups");
1680 static void InitGroups(const FunctionCallbackInfo<Value>& args) {
1681 HandleScope scope(node_isolate);
1683 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1684 return ThrowTypeError("argument 1 must be a number or a string");
1687 if (!args[1]->IsUint32() && !args[1]->IsString()) {
1688 return ThrowTypeError("argument 2 must be a number or a string");
1691 String::Utf8Value arg0(args[0]);
1696 if (args[0]->IsUint32()) {
1697 user = name_by_uid(args[0]->Uint32Value());
1705 return ThrowError("initgroups user not found");
1708 extra_group = gid_by_name(args[1]);
1710 if (extra_group == gid_not_found) {
1711 if (must_free) free(user);
1712 return ThrowError("initgroups extra group not found");
1715 int rc = initgroups(user, extra_group);
1722 return ThrowErrnoException(errno, "initgroups");
1726 #endif // __POSIX__ && !defined(__ANDROID__)
1729 void Exit(const FunctionCallbackInfo<Value>& args) {
1730 HandleScope scope(node_isolate);
1731 exit(args[0]->IntegerValue());
1735 static void Uptime(const FunctionCallbackInfo<Value>& args) {
1736 HandleScope scope(node_isolate);
1738 if (uv_uptime(&uptime)) return;
1739 args.GetReturnValue().Set(uptime - prog_start_time);
1743 void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
1744 HandleScope scope(node_isolate);
1748 int err = uv_resident_set_memory(&rss);
1750 return ThrowUVException(err, "uv_resident_set_memory");
1753 Local<Object> info = Object::New();
1755 if (rss_symbol.IsEmpty()) {
1756 rss_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "rss");
1757 heap_total_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "heapTotal");
1758 heap_used_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "heapUsed");
1761 info->Set(rss_symbol, Number::New(rss));
1764 HeapStatistics v8_heap_stats;
1765 node_isolate->GetHeapStatistics(&v8_heap_stats);
1766 info->Set(heap_total_symbol,
1767 Integer::NewFromUnsigned(v8_heap_stats.total_heap_size(),
1769 info->Set(heap_used_symbol,
1770 Integer::NewFromUnsigned(v8_heap_stats.used_heap_size(),
1773 args.GetReturnValue().Set(info);
1777 void Kill(const FunctionCallbackInfo<Value>& args) {
1778 HandleScope scope(node_isolate);
1780 if (args.Length() != 2) {
1781 return ThrowError("Bad argument.");
1784 int pid = args[0]->IntegerValue();
1785 int sig = args[1]->Int32Value();
1786 int err = uv_kill(pid, sig);
1787 args.GetReturnValue().Set(err);
1790 // used in Hrtime() below
1791 #define NANOS_PER_SEC 1000000000
1793 // Hrtime exposes libuv's uv_hrtime() high-resolution timer.
1794 // The value returned by uv_hrtime() is a 64-bit int representing nanoseconds,
1795 // so this function instead returns an Array with 2 entries representing seconds
1796 // and nanoseconds, to avoid any integer overflow possibility.
1797 // Pass in an Array from a previous hrtime() call to instead get a time diff.
1798 void Hrtime(const FunctionCallbackInfo<Value>& args) {
1799 HandleScope scope(node_isolate);
1801 uint64_t t = uv_hrtime();
1803 if (args.Length() > 0) {
1804 // return a time diff tuple
1805 if (!args[0]->IsArray()) {
1806 return ThrowTypeError("process.hrtime() only accepts an Array tuple.");
1808 Local<Array> inArray = Local<Array>::Cast(args[0]);
1809 uint64_t seconds = inArray->Get(0)->Uint32Value();
1810 uint64_t nanos = inArray->Get(1)->Uint32Value();
1811 t -= (seconds * NANOS_PER_SEC) + nanos;
1814 Local<Array> tuple = Array::New(2);
1815 tuple->Set(0, Integer::NewFromUnsigned(t / NANOS_PER_SEC, node_isolate));
1816 tuple->Set(1, Integer::NewFromUnsigned(t % NANOS_PER_SEC, node_isolate));
1817 args.GetReturnValue().Set(tuple);
1821 typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);
1823 // DLOpen is process.dlopen(module, filename).
1824 // Used to load 'module.node' dynamically shared objects.
1825 void DLOpen(const FunctionCallbackInfo<Value>& args) {
1826 HandleScope scope(node_isolate);
1827 char symbol[1024], *base, *pos;
1831 if (args.Length() < 2) {
1832 return ThrowError("process.dlopen takes exactly 2 arguments.");
1835 Local<Object> module = args[0]->ToObject(); // Cast
1836 String::Utf8Value filename(args[1]); // Cast
1838 if (exports_symbol.IsEmpty()) {
1839 exports_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "exports");
1841 Local<Object> exports = module->Get(exports_symbol)->ToObject();
1843 if (uv_dlopen(*filename, &lib)) {
1844 Local<String> errmsg = OneByteString(node_isolate, uv_dlerror(&lib));
1846 // Windows needs to add the filename into the error message
1847 errmsg = String::Concat(errmsg, args[1]->ToString());
1849 ThrowException(Exception::Error(errmsg));
1853 String::Utf8Value path(args[1]);
1856 /* Find the shared library filename within the full path. */
1858 pos = strrchr(base, '/');
1864 pos = strpbrk(base, "\\/:");
1872 /* Strip the .node extension. */
1873 pos = strrchr(base, '.');
1878 /* Add the `_module` suffix to the extension name. */
1879 r = snprintf(symbol, sizeof symbol, "%s_module", base);
1880 if (r <= 0 || static_cast<size_t>(r) >= sizeof symbol) {
1881 return ThrowError("Out of memory.");
1884 /* Replace dashes with underscores. When loading foo-bar.node,
1885 * look for foo_bar_module, not foo-bar_module.
1887 for (pos = symbol; *pos != '\0'; ++pos) {
1888 if (*pos == '-') *pos = '_';
1891 node_module_struct *mod;
1892 if (uv_dlsym(&lib, symbol, reinterpret_cast<void**>(&mod))) {
1894 snprintf(errmsg, sizeof(errmsg), "Symbol %s not found.", symbol);
1895 return ThrowError(errmsg);
1898 if (mod->version != NODE_MODULE_VERSION) {
1902 "Module version mismatch. Expected %d, got %d.",
1903 NODE_MODULE_VERSION, mod->version);
1904 return ThrowError(errmsg);
1907 // Execute the C++ module
1908 mod->register_func(exports, module);
1910 // Tell coverity that 'handle' should not be freed when we return.
1911 // coverity[leaked_storage]
1915 static void OnFatalError(const char* location, const char* message) {
1917 fprintf(stderr, "FATAL ERROR: %s %s\n", location, message);
1919 fprintf(stderr, "FATAL ERROR: %s\n", message);
1929 NO_RETURN void FatalError(const char* location, const char* message) {
1930 OnFatalError(location, message);
1931 // to supress compiler warning
1936 void FatalException(Handle<Value> error, Handle<Message> message) {
1937 HandleScope scope(node_isolate);
1939 if (fatal_exception_symbol.IsEmpty()) {
1940 fatal_exception_symbol =
1941 FIXED_ONE_BYTE_STRING(node_isolate, "_fatalException");
1944 Local<Object> process = PersistentToLocal(node_isolate, process_p);
1945 Local<Value> fatal_v = process->Get(fatal_exception_symbol);
1947 if (!fatal_v->IsFunction()) {
1948 // failed before the process._fatalException function was added!
1949 // this is probably pretty bad. Nothing to do but report and exit.
1950 ReportException(error, message);
1954 Local<Function> fatal_f = Local<Function>::Cast(fatal_v);
1956 TryCatch fatal_try_catch;
1958 // Do not call FatalException when _fatalException handler throws
1959 fatal_try_catch.SetVerbose(false);
1961 // this will return true if the JS layer handled it, false otherwise
1962 Local<Value> caught = fatal_f->Call(process, 1, &error);
1964 if (fatal_try_catch.HasCaught()) {
1965 // the fatal exception function threw, so we must exit
1966 ReportException(fatal_try_catch);
1970 if (false == caught->BooleanValue()) {
1971 ReportException(error, message);
1977 void FatalException(const TryCatch& try_catch) {
1978 HandleScope scope(node_isolate);
1979 // TODO(bajtos) do not call FatalException if try_catch is verbose
1980 // (requires V8 API to expose getter for try_catch.is_verbose_)
1981 FatalException(try_catch.Exception(), try_catch.Message());
1985 void OnMessage(Handle<Message> message, Handle<Value> error) {
1986 // The current version of V8 sends messages for errors only
1987 // (thus `error` is always set).
1988 FatalException(error, message);
1992 static void Binding(const FunctionCallbackInfo<Value>& args) {
1993 HandleScope scope(node_isolate);
1995 Local<String> module = args[0]->ToString();
1996 String::Utf8Value module_v(module);
1997 node_module_struct* modp;
1999 Local<Object> cache = PersistentToLocal(node_isolate, binding_cache);
2000 Local<Object> exports;
2002 if (cache->Has(module)) {
2003 exports = cache->Get(module)->ToObject();
2004 args.GetReturnValue().Set(exports);
2008 // Append a string to process.moduleLoadList
2010 snprintf(buf, sizeof(buf), "Binding %s", *module_v);
2012 Local<Array> modules = PersistentToLocal(node_isolate, module_load_list);
2013 uint32_t l = modules->Length();
2014 modules->Set(l, OneByteString(node_isolate, buf));
2016 if ((modp = get_builtin_module(*module_v)) != NULL) {
2017 exports = Object::New();
2018 // Internal bindings don't have a "module" object,
2020 modp->register_func(exports, Undefined(node_isolate));
2021 cache->Set(module, exports);
2022 } else if (!strcmp(*module_v, "constants")) {
2023 exports = Object::New();
2024 DefineConstants(exports);
2025 cache->Set(module, exports);
2026 } else if (!strcmp(*module_v, "natives")) {
2027 exports = Object::New();
2028 DefineJavaScript(exports);
2029 cache->Set(module, exports);
2031 return ThrowError("No such module");
2034 args.GetReturnValue().Set(exports);
2038 static void ProcessTitleGetter(Local<String> property,
2039 const PropertyCallbackInfo<Value>& info) {
2040 HandleScope scope(node_isolate);
2042 uv_get_process_title(buffer, sizeof(buffer));
2043 info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, buffer));
2047 static void ProcessTitleSetter(Local<String> property,
2049 const PropertyCallbackInfo<void>& info) {
2050 HandleScope scope(node_isolate);
2051 String::Utf8Value title(value);
2052 // TODO(piscisaureus): protect with a lock
2053 uv_set_process_title(*title);
2057 static void EnvGetter(Local<String> property,
2058 const PropertyCallbackInfo<Value>& info) {
2059 HandleScope scope(node_isolate);
2061 String::Utf8Value key(property);
2062 const char* val = getenv(*key);
2064 return info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, val));
2067 String::Value key(property);
2068 WCHAR buffer[32767]; // The maximum size allowed for environment variables.
2069 DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
2071 ARRAY_SIZE(buffer));
2072 // If result >= sizeof buffer the buffer was too small. That should never
2073 // happen. If result == 0 and result != ERROR_SUCCESS the variable was not
2075 if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
2076 result < ARRAY_SIZE(buffer)) {
2077 const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
2078 Local<String> rc = String::NewFromTwoByte(node_isolate, two_byte_buffer);
2079 return info.GetReturnValue().Set(rc);
2082 // Not found. Fetch from prototype.
2083 info.GetReturnValue().Set(
2084 info.Data().As<Object>()->Get(property));
2088 static void EnvSetter(Local<String> property,
2090 const PropertyCallbackInfo<Value>& info) {
2091 HandleScope scope(node_isolate);
2093 String::Utf8Value key(property);
2094 String::Utf8Value val(value);
2095 setenv(*key, *val, 1);
2097 String::Value key(property);
2098 String::Value val(value);
2099 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2100 // Environment variables that start with '=' are read-only.
2101 if (key_ptr[0] != L'=') {
2102 SetEnvironmentVariableW(key_ptr, reinterpret_cast<WCHAR*>(*val));
2105 // Whether it worked or not, always return rval.
2106 info.GetReturnValue().Set(value);
2110 static void EnvQuery(Local<String> property,
2111 const PropertyCallbackInfo<Integer>& info) {
2112 HandleScope scope(node_isolate);
2113 int32_t rc = -1; // Not found unless proven otherwise.
2115 String::Utf8Value key(property);
2116 if (getenv(*key)) rc = 0;
2118 String::Value key(property);
2119 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2120 if (GetEnvironmentVariableW(key_ptr, NULL, 0) > 0 ||
2121 GetLastError() == ERROR_SUCCESS) {
2123 if (key_ptr[0] == L'=') {
2124 // Environment variables that start with '=' are hidden and read-only.
2125 rc = static_cast<int32_t>(v8::ReadOnly) |
2126 static_cast<int32_t>(v8::DontDelete) |
2127 static_cast<int32_t>(v8::DontEnum);
2131 if (rc != -1) info.GetReturnValue().Set(rc);
2135 static void EnvDeleter(Local<String> property,
2136 const PropertyCallbackInfo<Boolean>& info) {
2137 HandleScope scope(node_isolate);
2140 String::Utf8Value key(property);
2141 rc = getenv(*key) != NULL;
2142 if (rc) unsetenv(*key);
2144 String::Value key(property);
2145 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2146 if (key_ptr[0] == L'=' || !SetEnvironmentVariableW(key_ptr, NULL)) {
2147 // Deletion failed. Return true if the key wasn't there in the first place,
2148 // false if it is still there.
2149 rc = GetEnvironmentVariableW(key_ptr, NULL, NULL) == 0 &&
2150 GetLastError() != ERROR_SUCCESS;
2153 info.GetReturnValue().Set(rc);
2157 static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
2158 HandleScope scope(node_isolate);
2161 while (environ[size]) size++;
2163 Local<Array> env = Array::New(size);
2165 for (int i = 0; i < size; ++i) {
2166 const char* var = environ[i];
2167 const char* s = strchr(var, '=');
2168 const int length = s ? s - var : strlen(var);
2169 Local<String> name = String::NewFromUtf8(node_isolate,
2171 String::kNormalString,
2176 WCHAR* environment = GetEnvironmentStringsW();
2177 if (environment == NULL) return; // This should not happen.
2178 Local<Array> env = Array::New();
2179 WCHAR* p = environment;
2181 while (*p != NULL) {
2184 // If the key starts with '=' it is a hidden environment variable.
2188 s = wcschr(p, L'=');
2193 const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
2194 const size_t two_byte_buffer_len = s - p;
2195 Local<String> value = String::NewFromTwoByte(node_isolate,
2197 String::kNormalString,
2198 two_byte_buffer_len);
2199 env->Set(i++, value);
2200 p = s + wcslen(s) + 1;
2202 FreeEnvironmentStringsW(environment);
2205 info.GetReturnValue().Set(env);
2209 static Handle<Object> GetFeatures() {
2210 HandleScope scope(node_isolate);
2212 Local<Object> obj = Object::New();
2213 #if defined(DEBUG) && DEBUG
2214 Local<Value> debug = True(node_isolate);
2216 Local<Value> debug = False(node_isolate);
2217 #endif // defined(DEBUG) && DEBUG
2219 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "debug"), debug);
2221 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "uv"), True(node_isolate));
2222 // TODO(bnoordhuis) ping libuv
2223 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "ipv6"), True(node_isolate));
2225 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_npn"),
2226 Boolean::New(use_npn));
2227 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_sni"),
2228 Boolean::New(use_sni));
2229 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls"),
2230 Boolean::New(get_builtin_module("crypto") != NULL));
2232 return scope.Close(obj);
2236 static void DebugPortGetter(Local<String> property,
2237 const PropertyCallbackInfo<Value>& info) {
2238 HandleScope scope(node_isolate);
2239 info.GetReturnValue().Set(debug_port);
2243 static void DebugPortSetter(Local<String> property,
2245 const PropertyCallbackInfo<void>& info) {
2246 HandleScope scope(node_isolate);
2247 debug_port = value->NumberValue();
2251 static void DebugProcess(const FunctionCallbackInfo<Value>& args);
2252 static void DebugPause(const FunctionCallbackInfo<Value>& args);
2253 static void DebugEnd(const FunctionCallbackInfo<Value>& args);
2256 void NeedImmediateCallbackGetter(Local<String> property,
2257 const PropertyCallbackInfo<Value>& info) {
2258 info.GetReturnValue().Set(need_immediate_cb);
2262 static void NeedImmediateCallbackSetter(Local<String> property,
2264 const PropertyCallbackInfo<void>&) {
2265 HandleScope scope(node_isolate);
2267 bool bool_value = value->BooleanValue();
2269 if (need_immediate_cb == bool_value) return;
2271 need_immediate_cb = bool_value;
2273 if (need_immediate_cb) {
2274 uv_check_start(&check_immediate_watcher, node::CheckImmediate);
2275 // idle handle is needed only to maintain event loop
2276 uv_idle_start(&idle_immediate_dummy, node::IdleImmediateDummy);
2278 uv_check_stop(&check_immediate_watcher);
2279 uv_idle_stop(&idle_immediate_dummy);
2284 #define READONLY_PROPERTY(obj, str, var) \
2286 obj->Set(OneByteString(node_isolate, str), var, v8::ReadOnly); \
2290 Handle<Object> SetupProcessObject(int argc, char *argv[]) {
2291 HandleScope scope(node_isolate);
2295 Local<FunctionTemplate> process_template = FunctionTemplate::New();
2296 process_template->SetClassName(
2297 FIXED_ONE_BYTE_STRING(node_isolate, "process"));
2299 Local<Object> process = process_template->GetFunction()->NewInstance();
2300 assert(process.IsEmpty() == false);
2301 assert(process->IsObject() == true);
2303 process_p.Reset(node_isolate, process);
2305 process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "title"),
2307 ProcessTitleSetter);
2310 READONLY_PROPERTY(process,
2312 FIXED_ONE_BYTE_STRING(node_isolate, NODE_VERSION));
2314 // process.moduleLoadList
2315 Local<Array> modules = Array::New();
2316 module_load_list.Reset(node_isolate, modules);
2317 READONLY_PROPERTY(process, "moduleLoadList", modules);
2320 Local<Object> versions = Object::New();
2321 READONLY_PROPERTY(process, "versions", versions);
2323 const char http_parser_version[] = NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR)
2325 NODE_STRINGIFY(HTTP_PARSER_VERSION_MINOR);
2326 READONLY_PROPERTY(versions,
2328 FIXED_ONE_BYTE_STRING(node_isolate, http_parser_version));
2330 // +1 to get rid of the leading 'v'
2331 READONLY_PROPERTY(versions,
2333 OneByteString(node_isolate, NODE_VERSION + 1));
2334 READONLY_PROPERTY(versions,
2336 OneByteString(node_isolate, V8::GetVersion()));
2337 READONLY_PROPERTY(versions,
2339 OneByteString(node_isolate, uv_version_string()));
2340 READONLY_PROPERTY(versions,
2342 FIXED_ONE_BYTE_STRING(node_isolate, ZLIB_VERSION));
2344 const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);
2345 READONLY_PROPERTY(versions,
2347 FIXED_ONE_BYTE_STRING(node_isolate, node_modules_version));
2350 // Stupid code to slice out the version string.
2351 int c, l = strlen(OPENSSL_VERSION_TEXT);
2352 for (i = j = 0; i < l; i++) {
2353 c = OPENSSL_VERSION_TEXT[i];
2354 if ('0' <= c && c <= '9') {
2355 for (j = i + 1; j < l; j++) {
2356 c = OPENSSL_VERSION_TEXT[j];
2357 if (c == ' ') break;
2365 OneByteString(node_isolate, &OPENSSL_VERSION_TEXT[i], j - i));
2371 READONLY_PROPERTY(process, "arch", OneByteString(node_isolate, ARCH));
2374 READONLY_PROPERTY(process,
2376 OneByteString(node_isolate, PLATFORM));
2379 Local<Array> arguments = Array::New(argc - option_end_index + 1);
2380 arguments->Set(0, String::NewFromUtf8(node_isolate, argv[0]));
2381 for (j = 1, i = option_end_index; i < argc; j++, i++) {
2382 arguments->Set(j, String::NewFromUtf8(node_isolate, argv[i]));
2384 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "argv"), arguments);
2387 Local<Array> exec_argv = Array::New(option_end_index - 1);
2388 for (j = 1, i = 0; j < option_end_index; j++, i++) {
2389 exec_argv->Set(i, String::NewFromUtf8(node_isolate, argv[j]));
2391 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execArgv"), exec_argv);
2393 // create process.env
2394 Local<ObjectTemplate> envTemplate = ObjectTemplate::New();
2395 envTemplate->SetNamedPropertyHandler(EnvGetter,
2401 Local<Object> env = envTemplate->NewInstance();
2402 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "env"), env);
2404 READONLY_PROPERTY(process, "pid", Integer::New(getpid(), node_isolate));
2405 READONLY_PROPERTY(process, "features", GetFeatures());
2406 process->SetAccessor(
2407 FIXED_ONE_BYTE_STRING(node_isolate, "_needImmediateCallback"),
2408 NeedImmediateCallbackGetter,
2409 NeedImmediateCallbackSetter);
2413 READONLY_PROPERTY(process,
2415 String::NewFromUtf8(node_isolate, eval_string));
2420 READONLY_PROPERTY(process, "_print_eval", True(node_isolate));
2423 // -i, --interactive
2425 READONLY_PROPERTY(process, "_forceRepl", True(node_isolate));
2429 if (no_deprecation) {
2430 READONLY_PROPERTY(process, "noDeprecation", True(node_isolate));
2433 // --throw-deprecation
2434 if (throw_deprecation) {
2435 READONLY_PROPERTY(process, "throwDeprecation", True(node_isolate));
2438 // --trace-deprecation
2439 if (trace_deprecation) {
2440 READONLY_PROPERTY(process, "traceDeprecation", True(node_isolate));
2443 size_t exec_path_len = 2 * PATH_MAX;
2444 char* exec_path = new char[exec_path_len];
2445 Local<String> exec_path_value;
2446 if (uv_exepath(exec_path, &exec_path_len) == 0) {
2447 exec_path_value = String::NewFromUtf8(node_isolate,
2449 String::kNormalString,
2452 exec_path_value = String::NewFromUtf8(node_isolate, argv[0]);
2454 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execPath"),
2458 process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "debugPort"),
2462 // define various internal methods
2463 NODE_SET_METHOD(process, "_getActiveRequests", GetActiveRequests);
2464 NODE_SET_METHOD(process, "_getActiveHandles", GetActiveHandles);
2465 NODE_SET_METHOD(process, "reallyExit", Exit);
2466 NODE_SET_METHOD(process, "abort", Abort);
2467 NODE_SET_METHOD(process, "chdir", Chdir);
2468 NODE_SET_METHOD(process, "cwd", Cwd);
2470 NODE_SET_METHOD(process, "umask", Umask);
2472 #if defined(__POSIX__) && !defined(__ANDROID__)
2473 NODE_SET_METHOD(process, "getuid", GetUid);
2474 NODE_SET_METHOD(process, "setuid", SetUid);
2476 NODE_SET_METHOD(process, "setgid", SetGid);
2477 NODE_SET_METHOD(process, "getgid", GetGid);
2479 NODE_SET_METHOD(process, "getgroups", GetGroups);
2480 NODE_SET_METHOD(process, "setgroups", SetGroups);
2481 NODE_SET_METHOD(process, "initgroups", InitGroups);
2482 #endif // __POSIX__ && !defined(__ANDROID__)
2484 NODE_SET_METHOD(process, "_kill", Kill);
2486 NODE_SET_METHOD(process, "_debugProcess", DebugProcess);
2487 NODE_SET_METHOD(process, "_debugPause", DebugPause);
2488 NODE_SET_METHOD(process, "_debugEnd", DebugEnd);
2490 NODE_SET_METHOD(process, "hrtime", Hrtime);
2492 NODE_SET_METHOD(process, "dlopen", DLOpen);
2494 NODE_SET_METHOD(process, "uptime", Uptime);
2495 NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
2497 NODE_SET_METHOD(process, "binding", Binding);
2499 NODE_SET_METHOD(process, "_setupDomainUse", SetupDomainUse);
2501 // values use to cross communicate with processNextTick
2502 Local<Object> info_box = Object::New();
2503 info_box->SetIndexedPropertiesToExternalArrayData(&tick_infobox,
2504 kExternalUnsignedIntArray,
2506 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickInfoBox"), info_box);
2508 // pre-set _events object for faster emit checks
2509 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_events"), Object::New());
2511 return scope.Close(process);
2515 #undef READONLY_PROPERTY
2518 static void AtExit() {
2519 uv_tty_reset_mode();
2523 static void SignalExit(int signal) {
2524 uv_tty_reset_mode();
2525 _exit(128 + signal);
2529 void Load(Handle<Object> process_l) {
2530 HandleScope handle_scope(node_isolate);
2532 process_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "process");
2533 domain_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "domain");
2535 // Compile, execute the src/node.js file. (Which was included as static C
2536 // string in node_natives.h. 'natve_node' is the string containing that
2539 // The node.js file returns a function 'f'
2544 // Disable verbose mode to stop FatalException() handler from trying
2545 // to handle the exception. Errors this early in the start-up phase
2546 // are not safe to ignore.
2547 try_catch.SetVerbose(false);
2549 Local<String> script_name = FIXED_ONE_BYTE_STRING(node_isolate, "node.js");
2550 Local<Value> f_value = ExecuteString(MainSource(), script_name);
2551 if (try_catch.HasCaught()) {
2552 ReportException(try_catch);
2555 assert(f_value->IsFunction());
2556 Local<Function> f = Local<Function>::Cast(f_value);
2558 // Now we call 'f' with the 'process' variable that we've built up with
2559 // all our bindings. Inside node.js we'll take care of assigning things to
2562 // We start the process this way in order to be more modular. Developers
2563 // who do not like how 'src/node.js' setups the module system but do like
2564 // Node's I/O bindings may want to replace 'f' with their own function.
2566 // Add a reference to the global object
2567 Local<Object> global = v8::Context::GetCurrent()->Global();
2569 #if defined HAVE_DTRACE || defined HAVE_ETW || defined HAVE_SYSTEMTAP
2573 #if defined HAVE_PERFCTR
2574 InitPerfCounters(global);
2577 // Enable handling of uncaught exceptions
2578 // (FatalException(), break on uncaught exception in debugger)
2580 // This is not strictly necessary since it's almost impossible
2581 // to attach the debugger fast enought to break on exception
2582 // thrown during process startup.
2583 try_catch.SetVerbose(true);
2585 Local<Value> arg = process_l;
2586 f->Call(global, 1, &arg);
2589 static void PrintHelp();
2591 static void ParseDebugOpt(const char* arg) {
2594 if (strstr(arg, "--debug-port=") == arg) {
2595 p = 1 + strchr(arg, '=');
2596 debug_port = atoi(p);
2598 use_debug_agent = true;
2599 if (!strcmp(arg, "--debug-brk")) {
2600 debug_wait_connect = true;
2602 } else if (!strcmp(arg, "--debug")) {
2604 } else if (strstr(arg, "--debug-brk=") == arg) {
2605 debug_wait_connect = true;
2606 p = 1 + strchr(arg, '=');
2607 debug_port = atoi(p);
2608 } else if (strstr(arg, "--debug=") == arg) {
2609 p = 1 + strchr(arg, '=');
2610 debug_port = atoi(p);
2613 if (p && debug_port > 1024 && debug_port < 65536)
2616 fprintf(stderr, "Bad debug option.\n");
2617 if (p) fprintf(stderr, "Debug port must be in range 1025 to 65535.\n");
2623 static void PrintHelp() {
2624 printf("Usage: node [options] [ -e script | script.js ] [arguments] \n"
2625 " node debug script.js [arguments] \n"
2628 " -v, --version print node's version\n"
2629 " -e, --eval script evaluate script\n"
2630 " -p, --print evaluate script and print result\n"
2631 " -i, --interactive always enter the REPL even if stdin\n"
2632 " does not appear to be a terminal\n"
2633 " --no-deprecation silence deprecation warnings\n"
2634 " --trace-deprecation show stack traces on deprecations\n"
2635 " --v8-options print v8 command line options\n"
2636 " --max-stack-size=val set max v8 stack size (bytes)\n"
2638 "Environment variables:\n"
2640 "NODE_PATH ';'-separated list of directories\n"
2642 "NODE_PATH ':'-separated list of directories\n"
2644 " prefixed to the module search path.\n"
2645 "NODE_MODULE_CONTEXTS Set to 1 to load modules in their own\n"
2646 " global contexts.\n"
2647 "NODE_DISABLE_COLORS Set to 1 to disable colors in the REPL\n"
2649 "Documentation can be found at http://nodejs.org/\n");
2653 // Parse node command line arguments.
2654 static void ParseArgs(int argc, char **argv) {
2657 // TODO(bnoordhuis) use parse opts
2658 for (i = 1; i < argc; i++) {
2659 const char *arg = argv[i];
2660 if (strstr(arg, "--debug") == arg) {
2662 argv[i] = const_cast<char*>("");
2663 } else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
2664 printf("%s\n", NODE_VERSION);
2666 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
2669 } else if (strcmp(arg, "--eval") == 0 ||
2670 strcmp(arg, "-e") == 0 ||
2671 strcmp(arg, "--print") == 0 ||
2672 strcmp(arg, "-pe") == 0 ||
2673 strcmp(arg, "-p") == 0) {
2674 bool is_eval = strchr(arg, 'e') != NULL;
2675 bool is_print = strchr(arg, 'p') != NULL;
2677 // argument to -p and --print is optional
2678 if (is_eval == true && i + 1 >= argc) {
2679 fprintf(stderr, "Error: %s requires an argument\n", arg);
2683 print_eval = print_eval || is_print;
2684 argv[i] = const_cast<char*>("");
2686 // --eval, -e and -pe always require an argument
2687 if (is_eval == true) {
2688 eval_string = argv[++i];
2692 // next arg is the expression to evaluate unless it starts with:
2693 // - a dash, then it's another switch
2694 // - "\\-", then it's an escaped expression, drop the backslash
2695 if (argv[i + 1] == NULL) continue;
2696 if (argv[i + 1][0] == '-') continue;
2697 eval_string = argv[++i];
2698 if (strncmp(eval_string, "\\-", 2) == 0) ++eval_string;
2699 } else if (strcmp(arg, "--interactive") == 0 || strcmp(arg, "-i") == 0) {
2701 argv[i] = const_cast<char*>("");
2702 } else if (strcmp(arg, "--v8-options") == 0) {
2703 argv[i] = const_cast<char*>("--help");
2704 } else if (strcmp(arg, "--no-deprecation") == 0) {
2705 argv[i] = const_cast<char*>("");
2706 no_deprecation = true;
2707 } else if (strcmp(arg, "--trace-deprecation") == 0) {
2708 argv[i] = const_cast<char*>("");
2709 trace_deprecation = true;
2710 } else if (strcmp(arg, "--throw-deprecation") == 0) {
2711 argv[i] = const_cast<char*>("");
2712 throw_deprecation = true;
2713 } else if (argv[i][0] != '-') {
2718 option_end_index = i;
2722 // Called from the main thread.
2723 static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle, int status) {
2724 v8::Debug::ProcessDebugMessages();
2728 // Called from V8 Debug Agent TCP thread.
2729 static void DispatchMessagesDebugAgentCallback() {
2730 uv_async_send(&dispatch_debug_messages_async);
2734 // Called from the main thread
2735 static void EmitDebugEnabledAsyncCallback(uv_async_t* handle, int status) {
2736 HandleScope handle_scope(node_isolate);
2737 Local<Object> obj = Object::New();
2738 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "cmd"),
2739 FIXED_ONE_BYTE_STRING(node_isolate, "NODE_DEBUG_ENABLED"));
2740 Local<Value> args[] = {
2741 FIXED_ONE_BYTE_STRING(node_isolate, "internalMessage"),
2744 MakeCallback(process_p, "emit", ARRAY_SIZE(args), args);
2748 // Called from the signal watcher callback
2749 static void EmitDebugEnabled() {
2750 uv_async_send(&emit_debug_enabled_async);
2754 static void EnableDebug(bool wait_connect) {
2755 // If we're called from another thread, make sure to enter the right
2757 node_isolate->Enter();
2759 v8::Debug::SetDebugMessageDispatchHandler(DispatchMessagesDebugAgentCallback,
2762 // Start the debug thread and it's associated TCP server on port 5858.
2763 bool r = v8::Debug::EnableAgent("node " NODE_VERSION,
2767 // Crappy check that everything went well. FIXME
2770 // Print out some information.
2771 fprintf(stderr, "debugger listening on port %d\n", debug_port);
2774 debugger_running = true;
2776 // Do not emit NODE_DEBUG_ENABLED when debugger is enabled before starting
2777 // the main process (i.e. when called via `node --debug`)
2778 if (!process_p.IsEmpty())
2781 node_isolate->Exit();
2786 static void EnableDebugSignalHandler(uv_signal_t* handle, int) {
2787 // Break once process will return execution to v8
2788 v8::Debug::DebugBreak(node_isolate);
2790 if (!debugger_running) {
2791 fprintf(stderr, "Hit SIGUSR1 - starting debugger agent.\n");
2797 static void RegisterSignalHandler(int signal, void (*handler)(int signal)) {
2798 struct sigaction sa;
2800 memset(&sa, 0, sizeof(sa));
2801 sa.sa_handler = handler;
2802 sigfillset(&sa.sa_mask);
2803 sigaction(signal, &sa, NULL);
2807 void DebugProcess(const FunctionCallbackInfo<Value>& args) {
2808 HandleScope scope(node_isolate);
2810 if (args.Length() != 1) {
2811 return ThrowError("Invalid number of arguments.");
2817 pid = args[0]->IntegerValue();
2818 r = kill(pid, SIGUSR1);
2820 return ThrowErrnoException(errno, "kill");
2827 DWORD WINAPI EnableDebugThreadProc(void* arg) {
2828 // Break once process will return execution to v8
2829 if (!debugger_running) {
2830 for (int i = 0; i < 1; i++) {
2831 fprintf(stderr, "Starting debugger agent.\r\n");
2837 v8::Debug::DebugBreak(node_isolate);
2843 static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
2845 return _snwprintf(buf, buf_len, L"node-debug-handler-%u", pid);
2849 static int RegisterDebugSignalHandler() {
2850 wchar_t mapping_name[32];
2851 HANDLE mapping_handle;
2853 LPTHREAD_START_ROUTINE* handler;
2855 pid = GetCurrentProcessId();
2857 if (GetDebugSignalHandlerMappingName(pid,
2859 ARRAY_SIZE(mapping_name)) < 0) {
2863 mapping_handle = CreateFileMappingW(INVALID_HANDLE_VALUE,
2869 if (mapping_handle == NULL) {
2873 handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
2874 MapViewOfFile(mapping_handle,
2875 FILE_MAP_ALL_ACCESS,
2879 if (handler == NULL) {
2880 CloseHandle(mapping_handle);
2884 *handler = EnableDebugThreadProc;
2886 UnmapViewOfFile(static_cast<void*>(handler));
2892 static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
2893 HandleScope scope(node_isolate);
2895 HANDLE process = NULL;
2896 HANDLE thread = NULL;
2897 HANDLE mapping = NULL;
2898 wchar_t mapping_name[32];
2899 LPTHREAD_START_ROUTINE* handler = NULL;
2901 if (args.Length() != 1) {
2902 ThrowError("Invalid number of arguments.");
2906 pid = (DWORD) args[0]->IntegerValue();
2908 process = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION |
2909 PROCESS_VM_OPERATION | PROCESS_VM_WRITE |
2913 if (process == NULL) {
2914 ThrowException(WinapiErrnoException(GetLastError(), "OpenProcess"));
2918 if (GetDebugSignalHandlerMappingName(pid,
2920 ARRAY_SIZE(mapping_name)) < 0) {
2921 ThrowErrnoException(errno, "sprintf");
2925 mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name);
2926 if (mapping == NULL) {
2927 ThrowException(WinapiErrnoException(GetLastError(), "OpenFileMappingW"));
2931 handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
2932 MapViewOfFile(mapping,
2937 if (handler == NULL || *handler == NULL) {
2938 ThrowException(WinapiErrnoException(GetLastError(), "MapViewOfFile"));
2942 thread = CreateRemoteThread(process,
2949 if (thread == NULL) {
2950 ThrowException(WinapiErrnoException(GetLastError(), "CreateRemoteThread"));
2954 // Wait for the thread to terminate
2955 if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) {
2956 ThrowException(WinapiErrnoException(GetLastError(), "WaitForSingleObject"));
2961 if (process != NULL)
2962 CloseHandle(process);
2964 CloseHandle(thread);
2965 if (handler != NULL)
2966 UnmapViewOfFile(handler);
2967 if (mapping != NULL)
2968 CloseHandle(mapping);
2973 static void DebugPause(const FunctionCallbackInfo<Value>& args) {
2974 v8::Debug::DebugBreak(node_isolate);
2978 static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
2979 if (debugger_running) {
2980 v8::Debug::DisableAgent();
2981 debugger_running = false;
2986 char** Init(int argc, char *argv[]) {
2987 // Initialize prog_start_time to get relative uptime.
2988 uv_uptime(&prog_start_time);
2990 // Make inherited handles noninheritable.
2991 uv_disable_stdio_inheritance();
2993 // init async debug messages dispatching
2994 uv_async_init(uv_default_loop(),
2995 &dispatch_debug_messages_async,
2996 DispatchDebugMessagesAsyncCallback);
2997 uv_unref(reinterpret_cast<uv_handle_t*>(&dispatch_debug_messages_async));
2999 // init async NODE_DEBUG_ENABLED emitter
3000 uv_async_init(uv_default_loop(),
3001 &emit_debug_enabled_async,
3002 EmitDebugEnabledAsyncCallback);
3003 uv_unref(reinterpret_cast<uv_handle_t*>(&emit_debug_enabled_async));
3005 // Parse a few arguments which are specific to Node.
3006 node::ParseArgs(argc, argv);
3007 // Parse the rest of the args (up to the 'option_end_index' (where '--' was
3008 // in the command line))
3009 int v8argc = option_end_index;
3010 char **v8argv = argv;
3012 if (debug_wait_connect) {
3013 // v8argv is a copy of argv up to the script file argument +2 if --debug-brk
3014 // to expose the v8 debugger js object so that node.js can set
3015 // a breakpoint on the first line of the startup script
3017 v8argv = new char*[v8argc];
3018 memcpy(v8argv, argv, sizeof(*argv) * option_end_index);
3019 v8argv[option_end_index] = const_cast<char*>("--expose_debug_as");
3020 v8argv[option_end_index + 1] = const_cast<char*>("v8debug");
3023 V8::SetFlagsFromCommandLine(&v8argc, v8argv, false);
3025 const char typed_arrays_flag[] = "--harmony_typed_arrays";
3026 V8::SetFlagsFromString(typed_arrays_flag, sizeof(typed_arrays_flag) - 1);
3027 V8::SetArrayBufferAllocator(&ArrayBufferAllocator::the_singleton);
3029 // Fetch a reference to the main isolate, so we have a reference to it
3030 // even when we need it to access it from another (debugger) thread.
3031 node_isolate = Isolate::GetCurrent();
3035 RegisterSignalHandler(SIGPIPE, SIG_IGN);
3036 RegisterSignalHandler(SIGINT, SignalExit);
3037 RegisterSignalHandler(SIGTERM, SignalExit);
3040 uv_check_init(uv_default_loop(), &check_immediate_watcher);
3041 uv_unref(reinterpret_cast<uv_handle_t*>(&check_immediate_watcher));
3042 uv_idle_init(uv_default_loop(), &idle_immediate_dummy);
3044 V8::SetFatalErrorHandler(node::OnFatalError);
3045 V8::AddMessageListener(OnMessage);
3047 // If the --debug flag was specified then initialize the debug thread.
3048 if (use_debug_agent) {
3049 EnableDebug(debug_wait_connect);
3052 RegisterDebugSignalHandler();
3054 static uv_signal_t signal_watcher;
3055 uv_signal_init(uv_default_loop(), &signal_watcher);
3056 uv_signal_start(&signal_watcher, EnableDebugSignalHandler, SIGUSR1);
3057 uv_unref(reinterpret_cast<uv_handle_t*>(&signal_watcher));
3065 struct AtExitCallback {
3066 AtExitCallback* next_;
3067 void (*cb_)(void* arg);
3071 static AtExitCallback* at_exit_functions_;
3075 AtExitCallback* p = at_exit_functions_;
3076 at_exit_functions_ = NULL;
3079 AtExitCallback* q = p->next_;
3087 void AtExit(void (*cb)(void* arg), void* arg) {
3088 AtExitCallback* p = new AtExitCallback;
3091 p->next_ = at_exit_functions_;
3092 at_exit_functions_ = p;
3096 void EmitExit(v8::Handle<v8::Object> process_l) {
3097 // process.emit('exit')
3098 process_l->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_exiting"),
3099 True(node_isolate));
3100 Local<Value> args[] = {
3101 FIXED_ONE_BYTE_STRING(node_isolate, "exit"),
3102 Integer::New(0, node_isolate)
3104 MakeCallback(process_l, "emit", ARRAY_SIZE(args), args);
3107 static char **copy_argv(int argc, char **argv) {
3115 for (i = 0; i < argc; i++) {
3116 strlen_sum += strlen(argv[i]) + 1;
3119 argv_copy = static_cast<char**>(
3120 malloc(sizeof(*argv_copy) * (argc + 1) + strlen_sum));
3125 argv_data = reinterpret_cast<char*>(argv_copy) +
3126 sizeof(*argv_copy) * (argc + 1);
3128 for (i = 0; i < argc; i++) {
3129 argv_copy[i] = argv_data;
3130 len = strlen(argv[i]) + 1;
3131 memcpy(argv_data, argv[i], len);
3135 argv_copy[argc] = NULL;
3140 int Start(int argc, char *argv[]) {
3141 // Hack aroung with the argv pointer. Used for process.title = "blah".
3142 argv = uv_setup_args(argc, argv);
3144 // Logic to duplicate argv as Init() modifies arguments
3145 // that are passed into it.
3146 char **argv_copy = copy_argv(argc, argv);
3148 // This needs to run *before* V8::Initialize()
3149 // Use copy here as to not modify the original argv:
3150 Init(argc, argv_copy);
3154 Locker locker(node_isolate);
3155 HandleScope handle_scope(node_isolate);
3157 // Create the one and only Context.
3158 Local<Context> context = Context::New(node_isolate);
3159 Context::Scope context_scope(context);
3161 binding_cache.Reset(node_isolate, Object::New());
3163 // Use original argv, as we're just copying values out of it.
3164 Local<Object> process_l = SetupProcessObject(argc, argv);
3166 // Create all the objects, load modules, do everything.
3167 // so your next reading stop should be node::Load()!
3170 // All our arguments are loaded. We've evaluated all of the scripts. We
3171 // might even have created TCP servers. Now we enter the main eventloop. If
3172 // there are no watchers on the loop (except for the ones that were
3173 // uv_unref'd) then this function exits. As long as there are active
3174 // watchers, it blocks.
3175 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
3177 EmitExit(process_l);
3182 // Clean up. Not strictly necessary.
3186 // Clean up the copy: