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
39 #include "node_dtrace.h"
43 #include "async-wrap.h"
44 #include "async-wrap-inl.h"
47 #include "handle_wrap.h"
49 #include "string_bytes.h"
52 #include "v8-profiler.h"
57 #include <limits.h> // PATH_MAX
63 #include <sys/types.h>
69 #define strcasecmp _stricmp
70 #define getpid _getpid
74 #include <sys/resource.h> // getrlimit, setrlimit
75 #include <unistd.h> // setuid, getuid
78 #if defined(__POSIX__) && !defined(__ANDROID__)
79 #include <pwd.h> // getpwnam()
80 #include <grp.h> // getgrnam()
84 #include <crt_externs.h>
85 #define environ (*_NSGetEnviron())
86 #elif !defined(_MSC_VER)
87 extern char **environ;
93 using v8::ArrayBuffer;
98 using v8::FunctionCallbackInfo;
99 using v8::FunctionTemplate;
101 using v8::HandleScope;
102 using v8::HeapStatistics;
110 using v8::ObjectTemplate;
111 using v8::PropertyCallbackInfo;
113 using v8::ThrowException;
118 using v8::kExternalUnsignedIntArray;
120 // FIXME(bnoordhuis) Make these per-context?
121 QUEUE handle_wrap_queue = { &handle_wrap_queue, &handle_wrap_queue };
122 QUEUE req_wrap_queue = { &req_wrap_queue, &req_wrap_queue };
124 static bool print_eval = false;
125 static bool force_repl = false;
126 static bool trace_deprecation = false;
127 static bool throw_deprecation = false;
128 static const char* eval_string = NULL;
129 static bool use_debug_agent = false;
130 static bool debug_wait_connect = false;
131 static int debug_port = 5858;
132 static bool v8_is_profiling = false;
134 // used by C++ modules as well
135 bool no_deprecation = false;
137 // process-relative uptime base, initialized at start-up
138 static double prog_start_time;
139 static bool debugger_running;
140 static uv_async_t dispatch_debug_messages_async;
142 // Declared in node_internals.h
143 Isolate* node_isolate = NULL;
146 class ArrayBufferAllocator : public ArrayBuffer::Allocator {
148 // Impose an upper limit to avoid out of memory errors that bring down
150 static const size_t kMaxLength = 0x3fffffff;
151 static ArrayBufferAllocator the_singleton;
152 virtual ~ArrayBufferAllocator() {}
153 virtual void* Allocate(size_t length);
154 virtual void* AllocateUninitialized(size_t length);
155 virtual void Free(void* data, size_t length);
157 ArrayBufferAllocator() {}
158 ArrayBufferAllocator(const ArrayBufferAllocator&);
159 void operator=(const ArrayBufferAllocator&);
162 ArrayBufferAllocator ArrayBufferAllocator::the_singleton;
165 void* ArrayBufferAllocator::Allocate(size_t length) {
166 if (length > kMaxLength)
168 return new char[length];
172 void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
173 if (length > kMaxLength)
175 return new char[length];
179 void ArrayBufferAllocator::Free(void* data, size_t length) {
180 delete[] static_cast<char*>(data);
184 static void CheckImmediate(uv_check_t* handle, int status) {
185 Environment* env = Environment::from_immediate_check_handle(handle);
186 Context::Scope context_scope(env->context());
187 MakeCallback(env, env->process_object(), env->immediate_callback_string());
191 static void IdleImmediateDummy(uv_idle_t*, int) {
192 // Do nothing. Only for maintaining event loop.
193 // TODO(bnoordhuis) Maybe make libuv accept NULL idle callbacks.
197 static inline const char *errno_string(int errorno) {
198 #define ERRNO_CASE(e) case e: return #e;
205 ERRNO_CASE(EADDRINUSE);
209 ERRNO_CASE(EADDRNOTAVAIL);
213 ERRNO_CASE(EAFNOSUPPORT);
221 # if EAGAIN != EWOULDBLOCK
222 ERRNO_CASE(EWOULDBLOCK);
227 ERRNO_CASE(EALREADY);
243 ERRNO_CASE(ECANCELED);
251 ERRNO_CASE(ECONNABORTED);
255 ERRNO_CASE(ECONNREFUSED);
259 ERRNO_CASE(ECONNRESET);
267 ERRNO_CASE(EDESTADDRREQ);
291 ERRNO_CASE(EHOSTUNREACH);
303 ERRNO_CASE(EINPROGRESS);
339 ERRNO_CASE(EMSGSIZE);
343 ERRNO_CASE(EMULTIHOP);
347 ERRNO_CASE(ENAMETOOLONG);
351 ERRNO_CASE(ENETDOWN);
355 ERRNO_CASE(ENETRESET);
359 ERRNO_CASE(ENETUNREACH);
391 # if ENOLINK != ENOLCK
405 ERRNO_CASE(ENOPROTOOPT);
425 ERRNO_CASE(ENOTCONN);
433 ERRNO_CASE(ENOTEMPTY);
437 ERRNO_CASE(ENOTSOCK);
444 ERRNO_CASE(EOPNOTSUPP);
458 ERRNO_CASE(EOVERFLOW);
473 #ifdef EPROTONOSUPPORT
474 ERRNO_CASE(EPROTONOSUPPORT);
478 ERRNO_CASE(EPROTOTYPE);
506 ERRNO_CASE(ETIMEDOUT);
521 const char *signo_string(int signo) {
522 #define SIGNO_CASE(e) case e: return #e;
549 # if SIGABRT != SIGIOT
593 SIGNO_CASE(SIGSTKFLT);
610 SIGNO_CASE(SIGBREAK);
634 SIGNO_CASE(SIGVTALRM);
642 SIGNO_CASE(SIGWINCH);
650 # if SIGPOLL != SIGIO
660 # if SIGPWR != SIGLOST
674 Local<Value> ErrnoException(int errorno,
678 Environment* env = Environment::GetCurrent(node_isolate);
681 Local<String> estring = OneByteString(node_isolate, errno_string(errorno));
682 if (msg == NULL || msg[0] == '\0') {
683 msg = strerror(errorno);
685 Local<String> message = OneByteString(node_isolate, msg);
687 Local<String> cons1 =
688 String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", "));
689 Local<String> cons2 = String::Concat(cons1, message);
692 Local<String> cons3 =
693 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
694 Local<String> cons4 =
695 String::Concat(cons3, String::NewFromUtf8(node_isolate, path));
696 Local<String> cons5 =
697 String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
698 e = Exception::Error(cons5);
700 e = Exception::Error(cons2);
703 Local<Object> obj = e->ToObject();
704 obj->Set(env->errno_string(), Integer::New(errorno, node_isolate));
705 obj->Set(env->code_string(), estring);
708 obj->Set(env->path_string(), String::NewFromUtf8(node_isolate, path));
711 if (syscall != NULL) {
712 obj->Set(env->syscall_string(), OneByteString(node_isolate, syscall));
719 // hack alert! copy of ErrnoException, tuned for uv errors
720 Local<Value> UVException(int errorno,
724 Environment* env = Environment::GetCurrent(node_isolate);
727 msg = uv_strerror(errorno);
729 Local<String> estring = OneByteString(node_isolate, uv_err_name(errorno));
730 Local<String> message = OneByteString(node_isolate, msg);
731 Local<String> cons1 =
732 String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", "));
733 Local<String> cons2 = String::Concat(cons1, message);
737 Local<String> path_str;
741 if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) {
742 path_str = String::Concat(FIXED_ONE_BYTE_STRING(node_isolate, "\\\\"),
743 String::NewFromUtf8(node_isolate, path + 8));
744 } else if (strncmp(path, "\\\\?\\", 4) == 0) {
745 path_str = String::NewFromUtf8(node_isolate, path + 4);
747 path_str = String::NewFromUtf8(node_isolate, path);
750 path_str = String::NewFromUtf8(node_isolate, path);
753 Local<String> cons3 =
754 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
755 Local<String> cons4 =
756 String::Concat(cons3, path_str);
757 Local<String> cons5 =
758 String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
759 e = Exception::Error(cons5);
761 e = Exception::Error(cons2);
764 Local<Object> obj = e->ToObject();
765 // TODO(piscisaureus) errno should probably go
766 obj->Set(env->errno_string(), Integer::New(errorno, node_isolate));
767 obj->Set(env->code_string(), estring);
770 obj->Set(env->path_string(), path_str);
773 if (syscall != NULL) {
774 obj->Set(env->syscall_string(), OneByteString(node_isolate, syscall));
782 // Does about the same as strerror(),
783 // but supports all windows error messages
784 static const char *winapi_strerror(const int errorno) {
787 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
788 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
789 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
792 // Remove trailing newlines
793 for (int i = strlen(errmsg) - 1;
794 i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) {
800 // FormatMessage failed
801 return "Unknown error";
806 Local<Value> WinapiErrnoException(int errorno,
810 Environment* env = Environment::GetCurrent(node_isolate);
813 if (!msg || !msg[0]) {
814 msg = winapi_strerror(errorno);
816 Local<String> message = OneByteString(node_isolate, msg);
819 Local<String> cons1 =
820 String::Concat(message, FIXED_ONE_BYTE_STRING(node_isolate, " '"));
821 Local<String> cons2 =
822 String::Concat(cons1, String::NewFromUtf8(node_isolate, path));
823 Local<String> cons3 =
824 String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, "'"));
825 e = Exception::Error(cons3);
827 e = Exception::Error(message);
830 Local<Object> obj = e->ToObject();
831 obj->Set(env->errno_string(), Integer::New(errorno, node_isolate));
834 obj->Set(env->path_string(), String::NewFromUtf8(node_isolate, path));
837 if (syscall != NULL) {
838 obj->Set(env->syscall_string(), OneByteString(node_isolate, syscall));
846 void SetupAsyncListener(const FunctionCallbackInfo<Value>& args) {
847 Environment* env = Environment::GetCurrent(args.GetIsolate());
848 HandleScope handle_scope(args.GetIsolate());
850 assert(args[0]->IsObject());
851 assert(args[1]->IsFunction());
852 assert(args[2]->IsFunction());
853 assert(args[3]->IsFunction());
854 assert(args[4]->IsFunction());
855 assert(args[5]->IsFunction());
857 env->set_async_listener_run_function(args[1].As<Function>());
858 env->set_async_listener_load_function(args[2].As<Function>());
859 env->set_async_listener_unload_function(args[3].As<Function>());
860 env->set_async_listener_push_function(args[4].As<Function>());
861 env->set_async_listener_strip_function(args[5].As<Function>());
863 Local<Object> async_listener_flag_obj = args[0].As<Object>();
864 Environment::AsyncListener* async_listener = env->async_listener();
865 async_listener_flag_obj->SetIndexedPropertiesToExternalArrayData(
866 async_listener->fields(),
867 kExternalUnsignedIntArray,
868 async_listener->fields_count());
870 // Do a little housekeeping.
871 env->process_object()->Delete(
872 FIXED_ONE_BYTE_STRING(args.GetIsolate(), "_setupAsyncListener"));
876 void SetupNextTick(const FunctionCallbackInfo<Value>& args) {
877 Environment* env = Environment::GetCurrent(args.GetIsolate());
878 HandleScope handle_scope(args.GetIsolate());
880 assert(args[0]->IsObject() && args[1]->IsFunction());
882 // Values use to cross communicate with processNextTick.
883 Local<Object> tick_info_obj = args[0].As<Object>();
884 tick_info_obj->SetIndexedPropertiesToExternalArrayData(
885 env->tick_info()->fields(),
886 kExternalUnsignedIntArray,
887 env->tick_info()->fields_count());
889 env->set_tick_callback_function(args[1].As<Function>());
891 // Do a little housekeeping.
892 env->process_object()->Delete(
893 FIXED_ONE_BYTE_STRING(args.GetIsolate(), "_setupNextTick"));
897 Handle<Value> MakeCallback(Environment* env,
898 Handle<Object> object,
899 const Handle<Function> callback,
901 Handle<Value> argv[]) {
902 // If you hit this assertion, you forgot to enter the v8::Context first.
903 assert(env->context() == env->isolate()->GetCurrentContext());
905 Local<Object> process = env->process_object();
908 try_catch.SetVerbose(true);
910 // TODO(trevnorris): This is sucky for performance. Fix it.
911 bool has_async_queue = object->Has(env->async_queue_string());
912 if (has_async_queue) {
913 Local<Value> argv[] = { object };
914 env->async_listener_load_function()->Call(process, ARRAY_SIZE(argv), argv);
916 if (try_catch.HasCaught())
917 return Undefined(node_isolate);
920 Local<Value> ret = callback->Call(object, argc, argv);
922 if (try_catch.HasCaught()) {
923 return Undefined(node_isolate);
926 if (has_async_queue) {
927 Local<Value> val = object.As<Value>();
928 env->async_listener_unload_function()->Call(process, 1, &val);
930 if (try_catch.HasCaught())
931 return Undefined(node_isolate);
934 Environment::TickInfo* tick_info = env->tick_info();
936 if (tick_info->in_tick()) {
940 if (tick_info->length() == 0) {
941 tick_info->set_index(0);
945 tick_info->set_in_tick(true);
947 // process nextTicks after call
948 env->tick_callback_function()->Call(process, 0, NULL);
950 tick_info->set_in_tick(false);
952 if (try_catch.HasCaught()) {
953 tick_info->set_last_threw(true);
954 return Undefined(node_isolate);
962 Handle<Value> MakeCallback(Environment* env,
963 const Handle<Object> object,
966 Handle<Value> argv[]) {
967 Local<Function> callback = object->Get(index).As<Function>();
968 assert(callback->IsFunction());
970 return MakeCallback(env, object, callback, argc, argv);
974 Handle<Value> MakeCallback(Environment* env,
975 const Handle<Object> object,
976 const Handle<String> symbol,
978 Handle<Value> argv[]) {
979 Local<Function> callback = object->Get(symbol).As<Function>();
980 assert(callback->IsFunction());
981 return MakeCallback(env, object, callback, argc, argv);
985 Handle<Value> MakeCallback(Environment* env,
986 const Handle<Object> object,
989 Handle<Value> argv[]) {
990 Local<String> method_string = OneByteString(node_isolate, method);
991 return MakeCallback(env, object, method_string, argc, argv);
995 Handle<Value> MakeCallback(const Handle<Object> object,
998 Handle<Value> argv[]) {
999 Local<Context> context = object->CreationContext();
1000 Environment* env = Environment::GetCurrent(context);
1001 Context::Scope context_scope(context);
1002 HandleScope handle_scope(env->isolate());
1003 return handle_scope.Close(MakeCallback(env, object, method, argc, argv));
1007 Handle<Value> MakeCallback(const Handle<Object> object,
1008 const Handle<String> symbol,
1010 Handle<Value> argv[]) {
1011 Local<Context> context = object->CreationContext();
1012 Environment* env = Environment::GetCurrent(context);
1013 Context::Scope context_scope(context);
1014 HandleScope handle_scope(env->isolate());
1015 return handle_scope.Close(MakeCallback(env, object, symbol, argc, argv));
1019 Handle<Value> MakeCallback(const Handle<Object> object,
1020 const Handle<Function> callback,
1022 Handle<Value> argv[]) {
1023 Local<Context> context = object->CreationContext();
1024 Environment* env = Environment::GetCurrent(context);
1025 Context::Scope context_scope(context);
1026 HandleScope handle_scope(env->isolate());
1027 return handle_scope.Close(MakeCallback(env, object, callback, argc, argv));
1031 enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
1032 HandleScope scope(node_isolate);
1034 if (!encoding_v->IsString())
1037 String::Utf8Value encoding(encoding_v);
1039 if (strcasecmp(*encoding, "utf8") == 0) {
1041 } else if (strcasecmp(*encoding, "utf-8") == 0) {
1043 } else if (strcasecmp(*encoding, "ascii") == 0) {
1045 } else if (strcasecmp(*encoding, "base64") == 0) {
1047 } else if (strcasecmp(*encoding, "ucs2") == 0) {
1049 } else if (strcasecmp(*encoding, "ucs-2") == 0) {
1051 } else if (strcasecmp(*encoding, "utf16le") == 0) {
1053 } else if (strcasecmp(*encoding, "utf-16le") == 0) {
1055 } else if (strcasecmp(*encoding, "binary") == 0) {
1057 } else if (strcasecmp(*encoding, "buffer") == 0) {
1059 } else if (strcasecmp(*encoding, "hex") == 0) {
1061 } else if (strcasecmp(*encoding, "raw") == 0) {
1062 if (!no_deprecation) {
1063 fprintf(stderr, "'raw' (array of integers) has been removed. "
1067 } else if (strcasecmp(*encoding, "raws") == 0) {
1068 if (!no_deprecation) {
1069 fprintf(stderr, "'raws' encoding has been renamed to 'binary'. "
1070 "Please update your code.\n");
1078 Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
1079 return StringBytes::Encode(static_cast<const char*>(buf),
1084 // Returns -1 if the handle was not valid for decoding
1085 ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
1086 HandleScope scope(node_isolate);
1088 if (val->IsArray()) {
1089 fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
1095 return StringBytes::Size(val, encoding);
1099 # define MIN(a, b) ((a) < (b) ? (a) : (b))
1102 // Returns number of bytes written.
1103 ssize_t DecodeWrite(char *buf,
1105 v8::Handle<v8::Value> val,
1106 enum encoding encoding) {
1107 return StringBytes::Write(buf, buflen, val, encoding, NULL);
1110 void DisplayExceptionLine(Handle<Message> message) {
1111 // Prevent re-entry into this function. For example, if there is
1112 // a throw from a program in vm.runInThisContext(code, filename, true),
1113 // then we want to show the original failure, not the secondary one.
1114 static bool displayed_error = false;
1116 if (displayed_error)
1118 displayed_error = true;
1120 uv_tty_reset_mode();
1122 fprintf(stderr, "\n");
1124 if (!message.IsEmpty()) {
1125 // Print (filename):(line number): (message).
1126 String::Utf8Value filename(message->GetScriptResourceName());
1127 const char* filename_string = *filename;
1128 int linenum = message->GetLineNumber();
1129 fprintf(stderr, "%s:%i\n", filename_string, linenum);
1130 // Print line of source code.
1131 String::Utf8Value sourceline(message->GetSourceLine());
1132 const char* sourceline_string = *sourceline;
1134 // Because of how node modules work, all scripts are wrapped with a
1135 // "function (module, exports, __filename, ...) {"
1136 // to provide script local variables.
1138 // When reporting errors on the first line of a script, this wrapper
1139 // function is leaked to the user. There used to be a hack here to
1140 // truncate off the first 62 characters, but it caused numerous other
1141 // problems when vm.runIn*Context() methods were used for non-module
1144 // If we ever decide to re-instate such a hack, the following steps
1147 // 1. Pass a flag around to say "this code was wrapped"
1148 // 2. Update the stack frame output so that it is also correct.
1150 // It would probably be simpler to add a line rather than add some
1151 // number of characters to the first line, since V8 truncates the
1152 // sourceline to 78 characters, and we end up not providing very much
1153 // useful debugging info to the user if we remove 62 characters.
1155 int start = message->GetStartColumn();
1156 int end = message->GetEndColumn();
1158 fprintf(stderr, "%s\n", sourceline_string);
1159 // Print wavy underline (GetUnderline is deprecated).
1160 for (int i = 0; i < start; i++) {
1161 fputc((sourceline_string[i] == '\t') ? '\t' : ' ', stderr);
1163 for (int i = start; i < end; i++) {
1166 fputc('\n', stderr);
1171 static void ReportException(Handle<Value> er, Handle<Message> message) {
1172 HandleScope scope(node_isolate);
1174 DisplayExceptionLine(message);
1176 Local<Value> trace_value(
1177 er->ToObject()->Get(FIXED_ONE_BYTE_STRING(node_isolate, "stack")));
1178 String::Utf8Value trace(trace_value);
1180 // range errors have a trace member set to undefined
1181 if (trace.length() > 0 && !trace_value->IsUndefined()) {
1182 fprintf(stderr, "%s\n", *trace);
1184 // this really only happens for RangeErrors, since they're the only
1185 // kind that won't have all this info in the trace, or when non-Error
1186 // objects are thrown manually.
1187 Local<Value> message;
1190 if (er->IsObject()) {
1191 Local<Object> err_obj = er.As<Object>();
1192 message = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "message"));
1193 name = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "name"));
1196 if (message.IsEmpty() ||
1197 message->IsUndefined() ||
1199 name->IsUndefined()) {
1200 // Not an error object. Just print as-is.
1201 String::Utf8Value message(er);
1202 fprintf(stderr, "%s\n", *message);
1204 String::Utf8Value name_string(name);
1205 String::Utf8Value message_string(message);
1206 fprintf(stderr, "%s: %s\n", *name_string, *message_string);
1214 static void ReportException(const TryCatch& try_catch) {
1215 ReportException(try_catch.Exception(), try_catch.Message());
1219 // Executes a str within the current v8 context.
1220 Local<Value> ExecuteString(Handle<String> source, Handle<Value> filename) {
1221 HandleScope scope(node_isolate);
1224 // try_catch must be nonverbose to disable FatalException() handler,
1225 // we will handle exceptions ourself.
1226 try_catch.SetVerbose(false);
1228 Local<v8::Script> script = v8::Script::Compile(source, filename);
1229 if (script.IsEmpty()) {
1230 ReportException(try_catch);
1234 Local<Value> result = script->Run();
1235 if (result.IsEmpty()) {
1236 ReportException(try_catch);
1240 return scope.Close(result);
1244 static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
1245 HandleScope scope(node_isolate);
1247 Local<Array> ary = Array::New();
1251 QUEUE_FOREACH(q, &req_wrap_queue) {
1252 ReqWrap<uv_req_t>* w = CONTAINER_OF(q, ReqWrap<uv_req_t>, req_wrap_queue_);
1253 if (w->persistent().IsEmpty())
1255 ary->Set(i++, w->object());
1258 args.GetReturnValue().Set(ary);
1262 // Non-static, friend of HandleWrap. Could have been a HandleWrap method but
1263 // implemented here for consistency with GetActiveRequests().
1264 void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
1265 HandleScope scope(node_isolate);
1267 Local<Array> ary = Array::New();
1271 Local<String> owner_sym = FIXED_ONE_BYTE_STRING(node_isolate, "owner");
1273 QUEUE_FOREACH(q, &handle_wrap_queue) {
1274 HandleWrap* w = CONTAINER_OF(q, HandleWrap, handle_wrap_queue_);
1275 if (w->persistent().IsEmpty() || (w->flags_ & HandleWrap::kUnref))
1277 Local<Object> object = w->object();
1278 Local<Value> owner = object->Get(owner_sym);
1279 if (owner->IsUndefined())
1281 ary->Set(i++, owner);
1284 args.GetReturnValue().Set(ary);
1288 static void Abort(const FunctionCallbackInfo<Value>& args) {
1293 static void Chdir(const FunctionCallbackInfo<Value>& args) {
1294 HandleScope scope(node_isolate);
1296 if (args.Length() != 1 || !args[0]->IsString()) {
1297 return ThrowError("Bad argument."); // FIXME(bnoordhuis) ThrowTypeError?
1300 String::Utf8Value path(args[0]);
1301 int err = uv_chdir(*path);
1303 return ThrowUVException(err, "uv_chdir");
1308 static void Cwd(const FunctionCallbackInfo<Value>& args) {
1309 HandleScope scope(node_isolate);
1311 /* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
1312 char buf[MAX_PATH * 4 + 1];
1314 char buf[PATH_MAX + 1];
1317 int err = uv_cwd(buf, ARRAY_SIZE(buf) - 1);
1319 return ThrowUVException(err, "uv_cwd");
1322 buf[ARRAY_SIZE(buf) - 1] = '\0';
1323 Local<String> cwd = String::NewFromUtf8(node_isolate, buf);
1325 args.GetReturnValue().Set(cwd);
1329 static void Umask(const FunctionCallbackInfo<Value>& args) {
1330 HandleScope scope(node_isolate);
1333 if (args.Length() < 1 || args[0]->IsUndefined()) {
1335 umask(static_cast<mode_t>(old));
1336 } else if (!args[0]->IsInt32() && !args[0]->IsString()) {
1337 return ThrowTypeError("argument must be an integer or octal string.");
1340 if (args[0]->IsInt32()) {
1341 oct = args[0]->Uint32Value();
1344 String::Utf8Value str(args[0]);
1346 // Parse the octal string.
1347 for (int i = 0; i < str.length(); i++) {
1349 if (c > '7' || c < '0') {
1350 return ThrowTypeError("invalid octal string");
1356 old = umask(static_cast<mode_t>(oct));
1359 args.GetReturnValue().Set(old);
1363 #if defined(__POSIX__) && !defined(__ANDROID__)
1365 static const uid_t uid_not_found = static_cast<uid_t>(-1);
1366 static const gid_t gid_not_found = static_cast<gid_t>(-1);
1369 static uid_t uid_by_name(const char* name) {
1377 if (getpwnam_r(name, &pwd, buf, sizeof(buf), &pp) == 0 && pp != NULL) {
1381 return uid_not_found;
1385 static char* name_by_uid(uid_t uid) {
1394 if ((rc = getpwuid_r(uid, &pwd, buf, sizeof(buf), &pp)) == 0 && pp != NULL) {
1395 return strdup(pp->pw_name);
1406 static gid_t gid_by_name(const char* name) {
1414 if (getgrnam_r(name, &pwd, buf, sizeof(buf), &pp) == 0 && pp != NULL) {
1418 return gid_not_found;
1422 #if 0 // For future use.
1423 static const char* name_by_gid(gid_t gid) {
1432 if ((rc = getgrgid_r(gid, &pwd, buf, sizeof(buf), &pp)) == 0 && pp != NULL) {
1433 return strdup(pp->gr_name);
1445 static uid_t uid_by_name(Handle<Value> value) {
1446 if (value->IsUint32()) {
1447 return static_cast<uid_t>(value->Uint32Value());
1449 String::Utf8Value name(value);
1450 return uid_by_name(*name);
1455 static gid_t gid_by_name(Handle<Value> value) {
1456 if (value->IsUint32()) {
1457 return static_cast<gid_t>(value->Uint32Value());
1459 String::Utf8Value name(value);
1460 return gid_by_name(*name);
1465 static void GetUid(const FunctionCallbackInfo<Value>& args) {
1466 // uid_t is an uint32_t on all supported platforms.
1467 args.GetReturnValue().Set(static_cast<uint32_t>(getuid()));
1471 static void GetGid(const FunctionCallbackInfo<Value>& args) {
1472 // gid_t is an uint32_t on all supported platforms.
1473 args.GetReturnValue().Set(static_cast<uint32_t>(getgid()));
1477 static void SetGid(const FunctionCallbackInfo<Value>& args) {
1478 HandleScope scope(node_isolate);
1480 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1481 return ThrowTypeError("setgid argument must be a number or a string");
1484 gid_t gid = gid_by_name(args[0]);
1486 if (gid == gid_not_found) {
1487 return ThrowError("setgid group id does not exist");
1491 return ThrowErrnoException(errno, "setgid");
1496 static void SetUid(const FunctionCallbackInfo<Value>& args) {
1497 HandleScope scope(node_isolate);
1499 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1500 return ThrowTypeError("setuid argument must be a number or a string");
1503 uid_t uid = uid_by_name(args[0]);
1505 if (uid == uid_not_found) {
1506 return ThrowError("setuid user id does not exist");
1510 return ThrowErrnoException(errno, "setuid");
1515 static void GetGroups(const FunctionCallbackInfo<Value>& args) {
1516 HandleScope scope(node_isolate);
1518 int ngroups = getgroups(0, NULL);
1520 if (ngroups == -1) {
1521 return ThrowErrnoException(errno, "getgroups");
1524 gid_t* groups = new gid_t[ngroups];
1526 ngroups = getgroups(ngroups, groups);
1528 if (ngroups == -1) {
1530 return ThrowErrnoException(errno, "getgroups");
1533 Local<Array> groups_list = Array::New(ngroups);
1534 bool seen_egid = false;
1535 gid_t egid = getegid();
1537 for (int i = 0; i < ngroups; i++) {
1538 groups_list->Set(i, Integer::New(groups[i], node_isolate));
1539 if (groups[i] == egid)
1545 if (seen_egid == false) {
1546 groups_list->Set(ngroups, Integer::New(egid, node_isolate));
1549 args.GetReturnValue().Set(groups_list);
1553 static void SetGroups(const FunctionCallbackInfo<Value>& args) {
1554 HandleScope scope(node_isolate);
1556 if (!args[0]->IsArray()) {
1557 return ThrowTypeError("argument 1 must be an array");
1560 Local<Array> groups_list = args[0].As<Array>();
1561 size_t size = groups_list->Length();
1562 gid_t* groups = new gid_t[size];
1564 for (size_t i = 0; i < size; i++) {
1565 gid_t gid = gid_by_name(groups_list->Get(i));
1567 if (gid == gid_not_found) {
1569 return ThrowError("group name not found");
1575 int rc = setgroups(size, groups);
1579 return ThrowErrnoException(errno, "setgroups");
1584 static void InitGroups(const FunctionCallbackInfo<Value>& args) {
1585 HandleScope scope(node_isolate);
1587 if (!args[0]->IsUint32() && !args[0]->IsString()) {
1588 return ThrowTypeError("argument 1 must be a number or a string");
1591 if (!args[1]->IsUint32() && !args[1]->IsString()) {
1592 return ThrowTypeError("argument 2 must be a number or a string");
1595 String::Utf8Value arg0(args[0]);
1600 if (args[0]->IsUint32()) {
1601 user = name_by_uid(args[0]->Uint32Value());
1609 return ThrowError("initgroups user not found");
1612 extra_group = gid_by_name(args[1]);
1614 if (extra_group == gid_not_found) {
1617 return ThrowError("initgroups extra group not found");
1620 int rc = initgroups(user, extra_group);
1627 return ThrowErrnoException(errno, "initgroups");
1631 #endif // __POSIX__ && !defined(__ANDROID__)
1634 void Exit(const FunctionCallbackInfo<Value>& args) {
1635 HandleScope scope(node_isolate);
1636 exit(args[0]->IntegerValue());
1640 static void Uptime(const FunctionCallbackInfo<Value>& args) {
1641 HandleScope scope(node_isolate);
1643 if (uv_uptime(&uptime))
1645 args.GetReturnValue().Set(uptime - prog_start_time);
1649 void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
1650 Environment* env = Environment::GetCurrent(args.GetIsolate());
1651 HandleScope handle_scope(args.GetIsolate());
1654 int err = uv_resident_set_memory(&rss);
1656 return ThrowUVException(err, "uv_resident_set_memory");
1660 HeapStatistics v8_heap_stats;
1661 node_isolate->GetHeapStatistics(&v8_heap_stats);
1663 Local<Integer> heap_total =
1664 Integer::NewFromUnsigned(v8_heap_stats.total_heap_size(), node_isolate);
1665 Local<Integer> heap_used =
1666 Integer::NewFromUnsigned(v8_heap_stats.used_heap_size(), node_isolate);
1668 Local<Object> info = Object::New();
1669 info->Set(env->rss_string(), Number::New(node_isolate, rss));
1670 info->Set(env->heap_total_string(), heap_total);
1671 info->Set(env->heap_used_string(), heap_used);
1673 args.GetReturnValue().Set(info);
1677 void Kill(const FunctionCallbackInfo<Value>& args) {
1678 HandleScope scope(node_isolate);
1680 if (args.Length() != 2) {
1681 return ThrowError("Bad argument.");
1684 int pid = args[0]->IntegerValue();
1685 int sig = args[1]->Int32Value();
1686 int err = uv_kill(pid, sig);
1687 args.GetReturnValue().Set(err);
1690 // used in Hrtime() below
1691 #define NANOS_PER_SEC 1000000000
1693 // Hrtime exposes libuv's uv_hrtime() high-resolution timer.
1694 // The value returned by uv_hrtime() is a 64-bit int representing nanoseconds,
1695 // so this function instead returns an Array with 2 entries representing seconds
1696 // and nanoseconds, to avoid any integer overflow possibility.
1697 // Pass in an Array from a previous hrtime() call to instead get a time diff.
1698 void Hrtime(const FunctionCallbackInfo<Value>& args) {
1699 HandleScope scope(node_isolate);
1701 uint64_t t = uv_hrtime();
1703 if (args.Length() > 0) {
1704 // return a time diff tuple
1705 if (!args[0]->IsArray()) {
1706 return ThrowTypeError("process.hrtime() only accepts an Array tuple.");
1708 Local<Array> inArray = Local<Array>::Cast(args[0]);
1709 uint64_t seconds = inArray->Get(0)->Uint32Value();
1710 uint64_t nanos = inArray->Get(1)->Uint32Value();
1711 t -= (seconds * NANOS_PER_SEC) + nanos;
1714 Local<Array> tuple = Array::New(2);
1715 tuple->Set(0, Integer::NewFromUnsigned(t / NANOS_PER_SEC, node_isolate));
1716 tuple->Set(1, Integer::NewFromUnsigned(t % NANOS_PER_SEC, node_isolate));
1717 args.GetReturnValue().Set(tuple);
1721 typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);
1723 // DLOpen is process.dlopen(module, filename).
1724 // Used to load 'module.node' dynamically shared objects.
1726 // FIXME(bnoordhuis) Not multi-context ready. TBD how to resolve the conflict
1727 // when two contexts try to load the same shared object. Maybe have a shadow
1728 // cache that's a plain C list or hash table that's shared across contexts?
1729 void DLOpen(const FunctionCallbackInfo<Value>& args) {
1730 Environment* env = Environment::GetCurrent(args.GetIsolate());
1731 HandleScope handle_scope(args.GetIsolate());
1732 char symbol[1024], *base, *pos;
1736 if (args.Length() < 2) {
1737 return ThrowError("process.dlopen takes exactly 2 arguments.");
1740 Local<Object> module = args[0]->ToObject(); // Cast
1741 String::Utf8Value filename(args[1]); // Cast
1743 Local<String> exports_string = env->exports_string();
1744 Local<Object> exports = module->Get(exports_string)->ToObject();
1746 if (uv_dlopen(*filename, &lib)) {
1747 Local<String> errmsg = OneByteString(env->isolate(), uv_dlerror(&lib));
1749 // Windows needs to add the filename into the error message
1750 errmsg = String::Concat(errmsg, args[1]->ToString());
1752 ThrowException(Exception::Error(errmsg));
1756 String::Utf8Value path(args[1]);
1759 /* Find the shared library filename within the full path. */
1761 pos = strrchr(base, '/');
1767 pos = strpbrk(base, "\\/:");
1775 /* Strip the .node extension. */
1776 pos = strrchr(base, '.');
1781 /* Add the `_module` suffix to the extension name. */
1782 r = snprintf(symbol, sizeof symbol, "%s_module", base);
1783 if (r <= 0 || static_cast<size_t>(r) >= sizeof symbol) {
1784 return ThrowError("Out of memory.");
1787 /* Replace dashes with underscores. When loading foo-bar.node,
1788 * look for foo_bar_module, not foo-bar_module.
1790 for (pos = symbol; *pos != '\0'; ++pos) {
1795 node_module_struct *mod;
1796 if (uv_dlsym(&lib, symbol, reinterpret_cast<void**>(&mod))) {
1798 snprintf(errmsg, sizeof(errmsg), "Symbol %s not found.", symbol);
1799 return ThrowError(errmsg);
1802 if (mod->version != NODE_MODULE_VERSION) {
1806 "Module version mismatch. Expected %d, got %d.",
1807 NODE_MODULE_VERSION, mod->version);
1808 return ThrowError(errmsg);
1811 // Execute the C++ module
1812 if (mod->register_context_func != NULL) {
1813 mod->register_context_func(exports, module, env->context());
1814 } else if (mod->register_func != NULL) {
1815 mod->register_func(exports, module);
1817 return ThrowError("Module has no declared entry point.");
1820 // Tell coverity that 'handle' should not be freed when we return.
1821 // coverity[leaked_storage]
1825 static void OnFatalError(const char* location, const char* message) {
1827 fprintf(stderr, "FATAL ERROR: %s %s\n", location, message);
1829 fprintf(stderr, "FATAL ERROR: %s\n", message);
1839 NO_RETURN void FatalError(const char* location, const char* message) {
1840 OnFatalError(location, message);
1841 // to supress compiler warning
1846 void FatalException(Handle<Value> error, Handle<Message> message) {
1847 HandleScope scope(node_isolate);
1849 Environment* env = Environment::GetCurrent(node_isolate);
1850 Local<Object> process_object = env->process_object();
1851 Local<String> fatal_exception_string = env->fatal_exception_string();
1852 Local<Function> fatal_exception_function =
1853 process_object->Get(fatal_exception_string).As<Function>();
1855 if (!fatal_exception_function->IsFunction()) {
1856 // failed before the process._fatalException function was added!
1857 // this is probably pretty bad. Nothing to do but report and exit.
1858 ReportException(error, message);
1862 TryCatch fatal_try_catch;
1864 // Do not call FatalException when _fatalException handler throws
1865 fatal_try_catch.SetVerbose(false);
1867 // this will return true if the JS layer handled it, false otherwise
1868 Local<Value> caught =
1869 fatal_exception_function->Call(process_object, 1, &error);
1871 if (fatal_try_catch.HasCaught()) {
1872 // the fatal exception function threw, so we must exit
1873 ReportException(fatal_try_catch);
1877 if (false == caught->BooleanValue()) {
1878 ReportException(error, message);
1884 void FatalException(const TryCatch& try_catch) {
1885 HandleScope scope(node_isolate);
1886 // TODO(bajtos) do not call FatalException if try_catch is verbose
1887 // (requires V8 API to expose getter for try_catch.is_verbose_)
1888 FatalException(try_catch.Exception(), try_catch.Message());
1892 void OnMessage(Handle<Message> message, Handle<Value> error) {
1893 // The current version of V8 sends messages for errors only
1894 // (thus `error` is always set).
1895 FatalException(error, message);
1899 static void Binding(const FunctionCallbackInfo<Value>& args) {
1900 Environment* env = Environment::GetCurrent(args.GetIsolate());
1901 HandleScope handle_scope(args.GetIsolate());
1903 Local<String> module = args[0]->ToString();
1904 String::Utf8Value module_v(module);
1906 Local<Object> cache = env->binding_cache_object();
1907 Local<Object> exports;
1909 if (cache->Has(module)) {
1910 exports = cache->Get(module)->ToObject();
1911 args.GetReturnValue().Set(exports);
1915 // Append a string to process.moduleLoadList
1917 snprintf(buf, sizeof(buf), "Binding %s", *module_v);
1919 Local<Array> modules = env->module_load_list_array();
1920 uint32_t l = modules->Length();
1921 modules->Set(l, OneByteString(node_isolate, buf));
1923 node_module_struct* mod = get_builtin_module(*module_v);
1925 exports = Object::New();
1926 // Internal bindings don't have a "module" object, only exports.
1927 assert(mod->register_func == NULL);
1928 assert(mod->register_context_func != NULL);
1929 Local<Value> unused = Undefined(env->isolate());
1930 mod->register_context_func(exports, unused, env->context());
1931 cache->Set(module, exports);
1932 } else if (!strcmp(*module_v, "constants")) {
1933 exports = Object::New();
1934 DefineConstants(exports);
1935 cache->Set(module, exports);
1936 } else if (!strcmp(*module_v, "natives")) {
1937 exports = Object::New();
1938 DefineJavaScript(exports);
1939 cache->Set(module, exports);
1941 return ThrowError("No such module");
1944 args.GetReturnValue().Set(exports);
1948 static void ProcessTitleGetter(Local<String> property,
1949 const PropertyCallbackInfo<Value>& info) {
1950 HandleScope scope(node_isolate);
1952 uv_get_process_title(buffer, sizeof(buffer));
1953 info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, buffer));
1957 static void ProcessTitleSetter(Local<String> property,
1959 const PropertyCallbackInfo<void>& info) {
1960 HandleScope scope(node_isolate);
1961 String::Utf8Value title(value);
1962 // TODO(piscisaureus): protect with a lock
1963 uv_set_process_title(*title);
1967 static void EnvGetter(Local<String> property,
1968 const PropertyCallbackInfo<Value>& info) {
1969 HandleScope scope(node_isolate);
1971 String::Utf8Value key(property);
1972 const char* val = getenv(*key);
1974 return info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, val));
1977 String::Value key(property);
1978 WCHAR buffer[32767]; // The maximum size allowed for environment variables.
1979 DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
1981 ARRAY_SIZE(buffer));
1982 // If result >= sizeof buffer the buffer was too small. That should never
1983 // happen. If result == 0 and result != ERROR_SUCCESS the variable was not
1985 if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
1986 result < ARRAY_SIZE(buffer)) {
1987 const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
1988 Local<String> rc = String::NewFromTwoByte(node_isolate, two_byte_buffer);
1989 return info.GetReturnValue().Set(rc);
1992 // Not found. Fetch from prototype.
1993 info.GetReturnValue().Set(
1994 info.Data().As<Object>()->Get(property));
1998 static void EnvSetter(Local<String> property,
2000 const PropertyCallbackInfo<Value>& info) {
2001 HandleScope scope(node_isolate);
2003 String::Utf8Value key(property);
2004 String::Utf8Value val(value);
2005 setenv(*key, *val, 1);
2007 String::Value key(property);
2008 String::Value val(value);
2009 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2010 // Environment variables that start with '=' are read-only.
2011 if (key_ptr[0] != L'=') {
2012 SetEnvironmentVariableW(key_ptr, reinterpret_cast<WCHAR*>(*val));
2015 // Whether it worked or not, always return rval.
2016 info.GetReturnValue().Set(value);
2020 static void EnvQuery(Local<String> property,
2021 const PropertyCallbackInfo<Integer>& info) {
2022 HandleScope scope(node_isolate);
2023 int32_t rc = -1; // Not found unless proven otherwise.
2025 String::Utf8Value key(property);
2029 String::Value key(property);
2030 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2031 if (GetEnvironmentVariableW(key_ptr, NULL, 0) > 0 ||
2032 GetLastError() == ERROR_SUCCESS) {
2034 if (key_ptr[0] == L'=') {
2035 // Environment variables that start with '=' are hidden and read-only.
2036 rc = static_cast<int32_t>(v8::ReadOnly) |
2037 static_cast<int32_t>(v8::DontDelete) |
2038 static_cast<int32_t>(v8::DontEnum);
2043 info.GetReturnValue().Set(rc);
2047 static void EnvDeleter(Local<String> property,
2048 const PropertyCallbackInfo<Boolean>& info) {
2049 HandleScope scope(node_isolate);
2052 String::Utf8Value key(property);
2053 rc = getenv(*key) != NULL;
2057 String::Value key(property);
2058 WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2059 if (key_ptr[0] == L'=' || !SetEnvironmentVariableW(key_ptr, NULL)) {
2060 // Deletion failed. Return true if the key wasn't there in the first place,
2061 // false if it is still there.
2062 rc = GetEnvironmentVariableW(key_ptr, NULL, NULL) == 0 &&
2063 GetLastError() != ERROR_SUCCESS;
2066 info.GetReturnValue().Set(rc);
2070 static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
2071 HandleScope scope(node_isolate);
2074 while (environ[size])
2077 Local<Array> env = Array::New(size);
2079 for (int i = 0; i < size; ++i) {
2080 const char* var = environ[i];
2081 const char* s = strchr(var, '=');
2082 const int length = s ? s - var : strlen(var);
2083 Local<String> name = String::NewFromUtf8(node_isolate,
2085 String::kNormalString,
2090 WCHAR* environment = GetEnvironmentStringsW();
2091 if (environment == NULL)
2092 return; // This should not happen.
2093 Local<Array> env = Array::New();
2094 WCHAR* p = environment;
2096 while (*p != NULL) {
2099 // If the key starts with '=' it is a hidden environment variable.
2103 s = wcschr(p, L'=');
2108 const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
2109 const size_t two_byte_buffer_len = s - p;
2110 Local<String> value = String::NewFromTwoByte(node_isolate,
2112 String::kNormalString,
2113 two_byte_buffer_len);
2114 env->Set(i++, value);
2115 p = s + wcslen(s) + 1;
2117 FreeEnvironmentStringsW(environment);
2120 info.GetReturnValue().Set(env);
2124 static Handle<Object> GetFeatures() {
2125 HandleScope scope(node_isolate);
2127 Local<Object> obj = Object::New();
2128 #if defined(DEBUG) && DEBUG
2129 Local<Value> debug = True(node_isolate);
2131 Local<Value> debug = False(node_isolate);
2132 #endif // defined(DEBUG) && DEBUG
2134 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "debug"), debug);
2136 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "uv"), True(node_isolate));
2137 // TODO(bnoordhuis) ping libuv
2138 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "ipv6"), True(node_isolate));
2140 #ifdef OPENSSL_NPN_NEGOTIATED
2141 Local<Boolean> tls_npn = True(node_isolate);
2143 Local<Boolean> tls_npn = False(node_isolate);
2145 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_npn"), tls_npn);
2147 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
2148 Local<Boolean> tls_sni = True(node_isolate);
2150 Local<Boolean> tls_sni = False(node_isolate);
2152 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_sni"), tls_sni);
2154 obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls"),
2155 Boolean::New(get_builtin_module("crypto") != NULL));
2157 return scope.Close(obj);
2161 static void DebugPortGetter(Local<String> property,
2162 const PropertyCallbackInfo<Value>& info) {
2163 HandleScope scope(node_isolate);
2164 info.GetReturnValue().Set(debug_port);
2168 static void DebugPortSetter(Local<String> property,
2170 const PropertyCallbackInfo<void>& info) {
2171 HandleScope scope(node_isolate);
2172 debug_port = value->NumberValue();
2176 static void DebugProcess(const FunctionCallbackInfo<Value>& args);
2177 static void DebugPause(const FunctionCallbackInfo<Value>& args);
2178 static void DebugEnd(const FunctionCallbackInfo<Value>& args);
2181 void NeedImmediateCallbackGetter(Local<String> property,
2182 const PropertyCallbackInfo<Value>& info) {
2183 Environment* env = Environment::GetCurrent(info.GetIsolate());
2184 const uv_check_t* immediate_check_handle = env->immediate_check_handle();
2185 bool active = uv_is_active(
2186 reinterpret_cast<const uv_handle_t*>(immediate_check_handle));
2187 info.GetReturnValue().Set(active);
2191 static void NeedImmediateCallbackSetter(
2192 Local<String> property,
2194 const PropertyCallbackInfo<void>& info) {
2195 Environment* env = Environment::GetCurrent(info.GetIsolate());
2196 HandleScope handle_scope(info.GetIsolate());
2198 uv_check_t* immediate_check_handle = env->immediate_check_handle();
2199 bool active = uv_is_active(
2200 reinterpret_cast<const uv_handle_t*>(immediate_check_handle));
2202 if (active == value->BooleanValue())
2205 uv_idle_t* immediate_idle_handle = env->immediate_idle_handle();
2208 uv_check_stop(immediate_check_handle);
2209 uv_idle_stop(immediate_idle_handle);
2211 uv_check_start(immediate_check_handle, CheckImmediate);
2212 // Idle handle is needed only to stop the event loop from blocking in poll.
2213 uv_idle_start(immediate_idle_handle, IdleImmediateDummy);
2218 void SetIdle(uv_prepare_t* handle, int) {
2219 Environment* env = Environment::from_idle_prepare_handle(handle);
2220 env->isolate()->GetCpuProfiler()->SetIdle(true);
2224 void ClearIdle(uv_check_t* handle, int) {
2225 Environment* env = Environment::from_idle_check_handle(handle);
2226 env->isolate()->GetCpuProfiler()->SetIdle(false);
2230 void StartProfilerIdleNotifier(Environment* env) {
2231 uv_prepare_start(env->idle_prepare_handle(), SetIdle);
2232 uv_check_start(env->idle_check_handle(), ClearIdle);
2236 void StopProfilerIdleNotifier(Environment* env) {
2237 uv_prepare_stop(env->idle_prepare_handle());
2238 uv_check_stop(env->idle_check_handle());
2242 void StartProfilerIdleNotifier(const FunctionCallbackInfo<Value>& args) {
2243 StartProfilerIdleNotifier(Environment::GetCurrent(args.GetIsolate()));
2247 void StopProfilerIdleNotifier(const FunctionCallbackInfo<Value>& args) {
2248 StopProfilerIdleNotifier(Environment::GetCurrent(args.GetIsolate()));
2252 #define READONLY_PROPERTY(obj, str, var) \
2254 obj->Set(OneByteString(node_isolate, str), var, v8::ReadOnly); \
2258 void SetupProcessObject(Environment* env,
2260 const char* const* argv,
2262 const char* const* exec_argv) {
2263 HandleScope scope(node_isolate);
2265 Local<Object> process = env->process_object();
2267 process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "title"),
2269 ProcessTitleSetter);
2272 READONLY_PROPERTY(process,
2274 FIXED_ONE_BYTE_STRING(node_isolate, NODE_VERSION));
2276 // process.moduleLoadList
2277 READONLY_PROPERTY(process,
2279 env->module_load_list_array());
2282 Local<Object> versions = Object::New();
2283 READONLY_PROPERTY(process, "versions", versions);
2285 const char http_parser_version[] = NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR)
2287 NODE_STRINGIFY(HTTP_PARSER_VERSION_MINOR);
2288 READONLY_PROPERTY(versions,
2290 FIXED_ONE_BYTE_STRING(node_isolate, http_parser_version));
2292 // +1 to get rid of the leading 'v'
2293 READONLY_PROPERTY(versions,
2295 OneByteString(node_isolate, NODE_VERSION + 1));
2296 READONLY_PROPERTY(versions,
2298 OneByteString(node_isolate, V8::GetVersion()));
2299 READONLY_PROPERTY(versions,
2301 OneByteString(node_isolate, uv_version_string()));
2302 READONLY_PROPERTY(versions,
2304 FIXED_ONE_BYTE_STRING(node_isolate, ZLIB_VERSION));
2306 const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);
2307 READONLY_PROPERTY(versions,
2309 FIXED_ONE_BYTE_STRING(node_isolate, node_modules_version));
2312 // Stupid code to slice out the version string.
2313 { // NOLINT(whitespace/braces)
2316 for (i = j = 0, k = sizeof(OPENSSL_VERSION_TEXT) - 1; i < k; ++i) {
2317 c = OPENSSL_VERSION_TEXT[i];
2318 if ('0' <= c && c <= '9') {
2319 for (j = i + 1; j < k; ++j) {
2320 c = OPENSSL_VERSION_TEXT[j];
2330 OneByteString(node_isolate, &OPENSSL_VERSION_TEXT[i], j - i));
2335 READONLY_PROPERTY(process, "arch", OneByteString(node_isolate, ARCH));
2338 READONLY_PROPERTY(process,
2340 OneByteString(node_isolate, PLATFORM));
2343 Local<Array> arguments = Array::New(argc);
2344 for (int i = 0; i < argc; ++i) {
2345 arguments->Set(i, String::NewFromUtf8(node_isolate, argv[i]));
2347 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "argv"), arguments);
2350 Local<Array> exec_arguments = Array::New(exec_argc);
2351 for (int i = 0; i < exec_argc; ++i) {
2352 exec_arguments->Set(i, String::NewFromUtf8(node_isolate, exec_argv[i]));
2354 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execArgv"), exec_arguments);
2356 // create process.env
2357 Local<ObjectTemplate> process_env_template = ObjectTemplate::New();
2358 process_env_template->SetNamedPropertyHandler(EnvGetter,
2364 Local<Object> process_env = process_env_template->NewInstance();
2365 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "env"), process_env);
2367 READONLY_PROPERTY(process, "pid", Integer::New(getpid(), node_isolate));
2368 READONLY_PROPERTY(process, "features", GetFeatures());
2369 process->SetAccessor(
2370 FIXED_ONE_BYTE_STRING(node_isolate, "_needImmediateCallback"),
2371 NeedImmediateCallbackGetter,
2372 NeedImmediateCallbackSetter);
2376 READONLY_PROPERTY(process,
2378 String::NewFromUtf8(node_isolate, eval_string));
2383 READONLY_PROPERTY(process, "_print_eval", True(node_isolate));
2386 // -i, --interactive
2388 READONLY_PROPERTY(process, "_forceRepl", True(node_isolate));
2392 if (no_deprecation) {
2393 READONLY_PROPERTY(process, "noDeprecation", True(node_isolate));
2396 // --throw-deprecation
2397 if (throw_deprecation) {
2398 READONLY_PROPERTY(process, "throwDeprecation", True(node_isolate));
2401 // --trace-deprecation
2402 if (trace_deprecation) {
2403 READONLY_PROPERTY(process, "traceDeprecation", True(node_isolate));
2406 size_t exec_path_len = 2 * PATH_MAX;
2407 char* exec_path = new char[exec_path_len];
2408 Local<String> exec_path_value;
2409 if (uv_exepath(exec_path, &exec_path_len) == 0) {
2410 exec_path_value = String::NewFromUtf8(node_isolate,
2412 String::kNormalString,
2415 exec_path_value = String::NewFromUtf8(node_isolate, argv[0]);
2417 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execPath"),
2421 process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "debugPort"),
2425 // define various internal methods
2426 NODE_SET_METHOD(process,
2427 "_startProfilerIdleNotifier",
2428 StartProfilerIdleNotifier);
2429 NODE_SET_METHOD(process,
2430 "_stopProfilerIdleNotifier",
2431 StopProfilerIdleNotifier);
2432 NODE_SET_METHOD(process, "_getActiveRequests", GetActiveRequests);
2433 NODE_SET_METHOD(process, "_getActiveHandles", GetActiveHandles);
2434 NODE_SET_METHOD(process, "reallyExit", Exit);
2435 NODE_SET_METHOD(process, "abort", Abort);
2436 NODE_SET_METHOD(process, "chdir", Chdir);
2437 NODE_SET_METHOD(process, "cwd", Cwd);
2439 NODE_SET_METHOD(process, "umask", Umask);
2441 #if defined(__POSIX__) && !defined(__ANDROID__)
2442 NODE_SET_METHOD(process, "getuid", GetUid);
2443 NODE_SET_METHOD(process, "setuid", SetUid);
2445 NODE_SET_METHOD(process, "setgid", SetGid);
2446 NODE_SET_METHOD(process, "getgid", GetGid);
2448 NODE_SET_METHOD(process, "getgroups", GetGroups);
2449 NODE_SET_METHOD(process, "setgroups", SetGroups);
2450 NODE_SET_METHOD(process, "initgroups", InitGroups);
2451 #endif // __POSIX__ && !defined(__ANDROID__)
2453 NODE_SET_METHOD(process, "_kill", Kill);
2455 NODE_SET_METHOD(process, "_debugProcess", DebugProcess);
2456 NODE_SET_METHOD(process, "_debugPause", DebugPause);
2457 NODE_SET_METHOD(process, "_debugEnd", DebugEnd);
2459 NODE_SET_METHOD(process, "hrtime", Hrtime);
2461 NODE_SET_METHOD(process, "dlopen", DLOpen);
2463 NODE_SET_METHOD(process, "uptime", Uptime);
2464 NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
2466 NODE_SET_METHOD(process, "binding", Binding);
2468 NODE_SET_METHOD(process, "_setupAsyncListener", SetupAsyncListener);
2469 NODE_SET_METHOD(process, "_setupNextTick", SetupNextTick);
2471 // values use to cross communicate with processNextTick
2472 Local<Object> tick_info_obj = Object::New();
2473 tick_info_obj->SetIndexedPropertiesToExternalArrayData(
2474 env->tick_info()->fields(),
2475 kExternalUnsignedIntArray,
2476 env->tick_info()->fields_count());
2477 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickInfo"), tick_info_obj);
2479 // pre-set _events object for faster emit checks
2480 process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_events"), Object::New());
2484 #undef READONLY_PROPERTY
2487 static void AtExit() {
2488 uv_tty_reset_mode();
2492 static void SignalExit(int signal) {
2493 uv_tty_reset_mode();
2494 _exit(128 + signal);
2498 // Most of the time, it's best to use `console.error` to write
2499 // to the process.stderr stream. However, in some cases, such as
2500 // when debugging the stream.Writable class or the process.nextTick
2501 // function, it is useful to bypass JavaScript entirely.
2502 static void RawDebug(const FunctionCallbackInfo<Value>& args) {
2503 HandleScope scope(node_isolate);
2505 assert(args.Length() == 1 && args[0]->IsString() &&
2506 "must be called with a single string");
2508 String::Utf8Value message(args[0]);
2509 fprintf(stderr, "%s\n", *message);
2514 void Load(Environment* env) {
2515 HandleScope handle_scope(node_isolate);
2517 // Compile, execute the src/node.js file. (Which was included as static C
2518 // string in node_natives.h. 'natve_node' is the string containing that
2521 // The node.js file returns a function 'f'
2526 // Disable verbose mode to stop FatalException() handler from trying
2527 // to handle the exception. Errors this early in the start-up phase
2528 // are not safe to ignore.
2529 try_catch.SetVerbose(false);
2531 Local<String> script_name = FIXED_ONE_BYTE_STRING(node_isolate, "node.js");
2532 Local<Value> f_value = ExecuteString(MainSource(), script_name);
2533 if (try_catch.HasCaught()) {
2534 ReportException(try_catch);
2537 assert(f_value->IsFunction());
2538 Local<Function> f = Local<Function>::Cast(f_value);
2540 // Now we call 'f' with the 'process' variable that we've built up with
2541 // all our bindings. Inside node.js we'll take care of assigning things to
2544 // We start the process this way in order to be more modular. Developers
2545 // who do not like how 'src/node.js' setups the module system but do like
2546 // Node's I/O bindings may want to replace 'f' with their own function.
2548 // Add a reference to the global object
2549 Local<Object> global = env->context()->Global();
2551 #if defined HAVE_DTRACE || defined HAVE_ETW
2555 #if defined HAVE_PERFCTR
2556 InitPerfCounters(global);
2559 // Enable handling of uncaught exceptions
2560 // (FatalException(), break on uncaught exception in debugger)
2562 // This is not strictly necessary since it's almost impossible
2563 // to attach the debugger fast enought to break on exception
2564 // thrown during process startup.
2565 try_catch.SetVerbose(true);
2567 NODE_SET_METHOD(env->process_object(), "_rawDebug", RawDebug);
2569 Local<Value> arg = env->process_object();
2570 f->Call(global, 1, &arg);
2573 static void PrintHelp();
2575 static void ParseDebugOpt(const char* arg) {
2578 if (strstr(arg, "--debug-port=") == arg) {
2579 p = 1 + strchr(arg, '=');
2580 debug_port = atoi(p);
2582 use_debug_agent = true;
2583 if (!strcmp(arg, "--debug-brk")) {
2584 debug_wait_connect = true;
2586 } else if (!strcmp(arg, "--debug")) {
2588 } else if (strstr(arg, "--debug-brk=") == arg) {
2589 debug_wait_connect = true;
2590 p = 1 + strchr(arg, '=');
2591 debug_port = atoi(p);
2592 } else if (strstr(arg, "--debug=") == arg) {
2593 p = 1 + strchr(arg, '=');
2594 debug_port = atoi(p);
2597 if (p && debug_port > 1024 && debug_port < 65536)
2600 fprintf(stderr, "Bad debug option.\n");
2602 fprintf(stderr, "Debug port must be in range 1025 to 65535.\n");
2608 static void PrintHelp() {
2609 printf("Usage: node [options] [ -e script | script.js ] [arguments] \n"
2610 " node debug script.js [arguments] \n"
2613 " -v, --version print node's version\n"
2614 " -e, --eval script evaluate script\n"
2615 " -p, --print evaluate script and print result\n"
2616 " -i, --interactive always enter the REPL even if stdin\n"
2617 " does not appear to be a terminal\n"
2618 " --no-deprecation silence deprecation warnings\n"
2619 " --trace-deprecation show stack traces on deprecations\n"
2620 " --v8-options print v8 command line options\n"
2621 " --max-stack-size=val set max v8 stack size (bytes)\n"
2623 "Environment variables:\n"
2625 "NODE_PATH ';'-separated list of directories\n"
2627 "NODE_PATH ':'-separated list of directories\n"
2629 " prefixed to the module search path.\n"
2630 "NODE_MODULE_CONTEXTS Set to 1 to load modules in their own\n"
2631 " global contexts.\n"
2632 "NODE_DISABLE_COLORS Set to 1 to disable colors in the REPL\n"
2634 "Documentation can be found at http://nodejs.org/\n");
2638 // Parse command line arguments.
2640 // argv is modified in place. exec_argv and v8_argv are out arguments that
2641 // ParseArgs() allocates memory for and stores a pointer to the output
2642 // vector in. The caller should free them with delete[].
2646 // * argv contains the arguments with node and V8 options filtered out.
2647 // * exec_argv contains both node and V8 options and nothing else.
2648 // * v8_argv contains argv[0] plus any V8 options
2649 static void ParseArgs(int* argc,
2652 const char*** exec_argv,
2654 const char*** v8_argv) {
2655 const unsigned int nargs = static_cast<unsigned int>(*argc);
2656 const char** new_exec_argv = new const char*[nargs];
2657 const char** new_v8_argv = new const char*[nargs];
2658 const char** new_argv = new const char*[nargs];
2660 for (unsigned int i = 0; i < nargs; ++i) {
2661 new_exec_argv[i] = NULL;
2662 new_v8_argv[i] = NULL;
2666 // exec_argv starts with the first option, the other two start with argv[0].
2667 unsigned int new_exec_argc = 0;
2668 unsigned int new_v8_argc = 1;
2669 unsigned int new_argc = 1;
2670 new_v8_argv[0] = argv[0];
2671 new_argv[0] = argv[0];
2673 unsigned int index = 1;
2674 while (index < nargs && argv[index][0] == '-') {
2675 const char* const arg = argv[index];
2676 unsigned int args_consumed = 1;
2678 if (strstr(arg, "--debug") == arg) {
2680 } else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
2681 printf("%s\n", NODE_VERSION);
2683 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
2686 } else if (strcmp(arg, "--eval") == 0 ||
2687 strcmp(arg, "-e") == 0 ||
2688 strcmp(arg, "--print") == 0 ||
2689 strcmp(arg, "-pe") == 0 ||
2690 strcmp(arg, "-p") == 0) {
2691 bool is_eval = strchr(arg, 'e') != NULL;
2692 bool is_print = strchr(arg, 'p') != NULL;
2693 print_eval = print_eval || is_print;
2694 // --eval, -e and -pe always require an argument.
2695 if (is_eval == true) {
2697 eval_string = argv[index + 1];
2698 if (eval_string == NULL) {
2699 fprintf(stderr, "%s: %s requires an argument\n", argv[0], arg);
2702 } else if (argv[index + 1] != NULL && argv[index + 1][0] != '-') {
2704 eval_string = argv[index + 1];
2705 if (strncmp(eval_string, "\\-", 2) == 0) {
2706 // Starts with "\\-": escaped expression, drop the backslash.
2710 } else if (strcmp(arg, "--interactive") == 0 || strcmp(arg, "-i") == 0) {
2712 } else if (strcmp(arg, "--no-deprecation") == 0) {
2713 no_deprecation = true;
2714 } else if (strcmp(arg, "--trace-deprecation") == 0) {
2715 trace_deprecation = true;
2716 } else if (strcmp(arg, "--throw-deprecation") == 0) {
2717 throw_deprecation = true;
2718 } else if (strcmp(arg, "--v8-options") == 0) {
2719 new_v8_argv[new_v8_argc] = "--help";
2722 // V8 option. Pass through as-is.
2723 new_v8_argv[new_v8_argc] = arg;
2727 memcpy(new_exec_argv + new_exec_argc,
2729 args_consumed * sizeof(*argv));
2731 new_exec_argc += args_consumed;
2732 index += args_consumed;
2735 // Copy remaining arguments.
2736 const unsigned int args_left = nargs - index;
2737 memcpy(new_argv + new_argc, argv + index, args_left * sizeof(*argv));
2738 new_argc += args_left;
2740 *exec_argc = new_exec_argc;
2741 *exec_argv = new_exec_argv;
2742 *v8_argc = new_v8_argc;
2743 *v8_argv = new_v8_argv;
2745 // Copy new_argv over argv and update argc.
2746 memcpy(argv, new_argv, new_argc * sizeof(*argv));
2748 *argc = static_cast<int>(new_argc);
2752 // Called from V8 Debug Agent TCP thread.
2753 static void DispatchMessagesDebugAgentCallback() {
2754 uv_async_send(&dispatch_debug_messages_async);
2758 // Called from the main thread.
2759 static void EnableDebug(bool wait_connect) {
2760 assert(debugger_running == false);
2761 Isolate* isolate = node_isolate; // TODO(bnoordhuis) Multi-isolate support.
2762 Isolate::Scope isolate_scope(isolate);
2763 v8::Debug::SetDebugMessageDispatchHandler(DispatchMessagesDebugAgentCallback,
2765 debugger_running = v8::Debug::EnableAgent("node " NODE_VERSION,
2768 if (debugger_running == false) {
2769 fprintf(stderr, "Starting debugger on port %d failed\n", debug_port);
2773 fprintf(stderr, "Debugger listening on port %d\n", debug_port);
2776 Environment* env = Environment::GetCurrentChecked(isolate);
2778 return; // Still starting up.
2780 Context::Scope context_scope(env->context());
2781 HandleScope handle_scope(env->isolate());
2782 Local<Object> message = Object::New();
2783 message->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "cmd"),
2784 FIXED_ONE_BYTE_STRING(env->isolate(), "NODE_DEBUG_ENABLED"));
2785 Local<Value> argv[] = {
2786 FIXED_ONE_BYTE_STRING(env->isolate(), "internalMessage"),
2789 MakeCallback(env, env->process_object(), "emit", ARRAY_SIZE(argv), argv);
2793 // Called from the main thread.
2794 static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle, int status) {
2795 if (debugger_running == false) {
2796 fprintf(stderr, "Starting debugger agent.\n");
2799 Isolate::Scope isolate_scope(node_isolate);
2800 v8::Debug::ProcessDebugMessages();
2805 static volatile sig_atomic_t caught_early_debug_signal;
2808 static void EarlyDebugSignalHandler(int signo) {
2809 caught_early_debug_signal = 1;
2813 static void InstallEarlyDebugSignalHandler() {
2814 struct sigaction sa;
2815 memset(&sa, 0, sizeof(sa));
2816 sa.sa_handler = EarlyDebugSignalHandler;
2817 sigaction(SIGUSR1, &sa, NULL);
2821 static void EnableDebugSignalHandler(int signo) {
2822 // Call only async signal-safe functions here!
2823 v8::Debug::DebugBreak(*static_cast<Isolate* volatile*>(&node_isolate));
2824 uv_async_send(&dispatch_debug_messages_async);
2828 static void RegisterSignalHandler(int signal, void (*handler)(int signal)) {
2829 struct sigaction sa;
2830 memset(&sa, 0, sizeof(sa));
2831 sa.sa_handler = handler;
2832 sigfillset(&sa.sa_mask);
2833 sigaction(signal, &sa, NULL);
2837 void DebugProcess(const FunctionCallbackInfo<Value>& args) {
2838 HandleScope scope(node_isolate);
2840 if (args.Length() != 1) {
2841 return ThrowError("Invalid number of arguments.");
2847 pid = args[0]->IntegerValue();
2848 r = kill(pid, SIGUSR1);
2850 return ThrowErrnoException(errno, "kill");
2855 static int RegisterDebugSignalHandler() {
2856 // FIXME(bnoordhuis) Should be per-isolate or per-context, not global.
2857 RegisterSignalHandler(SIGUSR1, EnableDebugSignalHandler);
2858 // If we caught a SIGUSR1 during the bootstrap process, re-raise it
2859 // now that the debugger infrastructure is in place.
2860 if (caught_early_debug_signal)
2868 DWORD WINAPI EnableDebugThreadProc(void* arg) {
2869 v8::Debug::DebugBreak(*static_cast<Isolate* volatile*>(&node_isolate));
2870 uv_async_send(&dispatch_debug_messages_async);
2875 static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
2877 return _snwprintf(buf, buf_len, L"node-debug-handler-%u", pid);
2881 static int RegisterDebugSignalHandler() {
2882 wchar_t mapping_name[32];
2883 HANDLE mapping_handle;
2885 LPTHREAD_START_ROUTINE* handler;
2887 pid = GetCurrentProcessId();
2889 if (GetDebugSignalHandlerMappingName(pid,
2891 ARRAY_SIZE(mapping_name)) < 0) {
2895 mapping_handle = CreateFileMappingW(INVALID_HANDLE_VALUE,
2901 if (mapping_handle == NULL) {
2905 handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
2906 MapViewOfFile(mapping_handle,
2907 FILE_MAP_ALL_ACCESS,
2911 if (handler == NULL) {
2912 CloseHandle(mapping_handle);
2916 *handler = EnableDebugThreadProc;
2918 UnmapViewOfFile(static_cast<void*>(handler));
2924 static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
2925 HandleScope scope(node_isolate);
2927 HANDLE process = NULL;
2928 HANDLE thread = NULL;
2929 HANDLE mapping = NULL;
2930 wchar_t mapping_name[32];
2931 LPTHREAD_START_ROUTINE* handler = NULL;
2933 if (args.Length() != 1) {
2934 ThrowError("Invalid number of arguments.");
2938 pid = (DWORD) args[0]->IntegerValue();
2940 process = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION |
2941 PROCESS_VM_OPERATION | PROCESS_VM_WRITE |
2945 if (process == NULL) {
2946 ThrowException(WinapiErrnoException(GetLastError(), "OpenProcess"));
2950 if (GetDebugSignalHandlerMappingName(pid,
2952 ARRAY_SIZE(mapping_name)) < 0) {
2953 ThrowErrnoException(errno, "sprintf");
2957 mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name);
2958 if (mapping == NULL) {
2959 ThrowException(WinapiErrnoException(GetLastError(), "OpenFileMappingW"));
2963 handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
2964 MapViewOfFile(mapping,
2969 if (handler == NULL || *handler == NULL) {
2970 ThrowException(WinapiErrnoException(GetLastError(), "MapViewOfFile"));
2974 thread = CreateRemoteThread(process,
2981 if (thread == NULL) {
2982 ThrowException(WinapiErrnoException(GetLastError(), "CreateRemoteThread"));
2986 // Wait for the thread to terminate
2987 if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) {
2988 ThrowException(WinapiErrnoException(GetLastError(), "WaitForSingleObject"));
2993 if (process != NULL)
2994 CloseHandle(process);
2996 CloseHandle(thread);
2997 if (handler != NULL)
2998 UnmapViewOfFile(handler);
2999 if (mapping != NULL)
3000 CloseHandle(mapping);
3005 static void DebugPause(const FunctionCallbackInfo<Value>& args) {
3006 v8::Debug::DebugBreak(node_isolate);
3010 static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
3011 if (debugger_running) {
3012 v8::Debug::DisableAgent();
3013 debugger_running = false;
3018 void Init(int* argc,
3021 const char*** exec_argv) {
3022 // Initialize prog_start_time to get relative uptime.
3023 uv_uptime(&prog_start_time);
3025 // Make inherited handles noninheritable.
3026 uv_disable_stdio_inheritance();
3028 // init async debug messages dispatching
3029 // FIXME(bnoordhuis) Should be per-isolate or per-context, not global.
3030 uv_async_init(uv_default_loop(),
3031 &dispatch_debug_messages_async,
3032 DispatchDebugMessagesAsyncCallback);
3033 uv_unref(reinterpret_cast<uv_handle_t*>(&dispatch_debug_messages_async));
3035 // Parse a few arguments which are specific to Node.
3037 const char** v8_argv;
3038 ParseArgs(argc, argv, exec_argc, exec_argv, &v8_argc, &v8_argv);
3040 // TODO(bnoordhuis) Intercept --prof arguments and start the CPU profiler
3041 // manually? That would give us a little more control over its runtime
3042 // behavior but it could also interfere with the user's intentions in ways
3043 // we fail to anticipate. Dillema.
3044 for (int i = 1; i < v8_argc; ++i) {
3045 if (strncmp(v8_argv[i], "--prof", sizeof("--prof") - 1) == 0) {
3046 v8_is_profiling = true;
3051 // The const_cast doesn't violate conceptual const-ness. V8 doesn't modify
3052 // the argv array or the elements it points to.
3053 V8::SetFlagsFromCommandLine(&v8_argc, const_cast<char**>(v8_argv), true);
3055 // Anything that's still in v8_argv is not a V8 or a node option.
3056 for (int i = 1; i < v8_argc; i++) {
3057 fprintf(stderr, "%s: bad option: %s\n", argv[0], v8_argv[i]);
3066 if (debug_wait_connect) {
3067 const char expose_debug_as[] = "--expose_debug_as=v8debug";
3068 V8::SetFlagsFromString(expose_debug_as, sizeof(expose_debug_as) - 1);
3071 V8::SetArrayBufferAllocator(&ArrayBufferAllocator::the_singleton);
3073 // Fetch a reference to the main isolate, so we have a reference to it
3074 // even when we need it to access it from another (debugger) thread.
3075 node_isolate = Isolate::GetCurrent();
3078 // Raise the open file descriptor limit.
3079 { // NOLINT (whitespace/braces)
3081 if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
3082 // Do a binary search for the limit.
3083 rlim_t min = lim.rlim_cur;
3084 rlim_t max = 1 << 20;
3085 // But if there's a defined upper bound, don't search, just set it.
3086 if (lim.rlim_max != RLIM_INFINITY) {
3091 lim.rlim_cur = min + (max - min) / 2;
3092 if (setrlimit(RLIMIT_NOFILE, &lim)) {
3097 } while (min + 1 < max);
3101 RegisterSignalHandler(SIGPIPE, SIG_IGN);
3102 RegisterSignalHandler(SIGINT, SignalExit);
3103 RegisterSignalHandler(SIGTERM, SignalExit);
3106 V8::SetFatalErrorHandler(node::OnFatalError);
3107 V8::AddMessageListener(OnMessage);
3109 // If the --debug flag was specified then initialize the debug thread.
3110 if (use_debug_agent) {
3111 EnableDebug(debug_wait_connect);
3113 RegisterDebugSignalHandler();
3118 struct AtExitCallback {
3119 AtExitCallback* next_;
3120 void (*cb_)(void* arg);
3124 static AtExitCallback* at_exit_functions_;
3127 // TODO(bnoordhuis) Turn into per-context event.
3128 void RunAtExit(Environment* env) {
3129 AtExitCallback* p = at_exit_functions_;
3130 at_exit_functions_ = NULL;
3133 AtExitCallback* q = p->next_;
3141 void AtExit(void (*cb)(void* arg), void* arg) {
3142 AtExitCallback* p = new AtExitCallback;
3145 p->next_ = at_exit_functions_;
3146 at_exit_functions_ = p;
3150 void EmitExit(Environment* env) {
3151 // process.emit('exit')
3152 Context::Scope context_scope(env->context());
3153 HandleScope handle_scope(env->isolate());
3154 Local<Object> process_object = env->process_object();
3155 process_object->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_exiting"),
3156 True(node_isolate));
3158 Handle<String> exitCode = FIXED_ONE_BYTE_STRING(node_isolate, "exitCode");
3159 int code = process_object->Get(exitCode)->IntegerValue();
3161 Local<Value> args[] = {
3162 FIXED_ONE_BYTE_STRING(node_isolate, "exit"),
3163 Integer::New(code, node_isolate)
3166 MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
3171 Environment* CreateEnvironment(Isolate* isolate,
3173 const char* const* argv,
3175 const char* const* exec_argv) {
3176 HandleScope handle_scope(isolate);
3178 Local<Context> context = Context::New(isolate);
3179 Context::Scope context_scope(context);
3180 Environment* env = Environment::New(context);
3182 uv_check_init(env->event_loop(), env->immediate_check_handle());
3184 reinterpret_cast<uv_handle_t*>(env->immediate_check_handle()));
3185 uv_idle_init(env->event_loop(), env->immediate_idle_handle());
3187 // Inform V8's CPU profiler when we're idle. The profiler is sampling-based
3188 // but not all samples are created equal; mark the wall clock time spent in
3189 // epoll_wait() and friends so profiling tools can filter it out. The samples
3190 // still end up in v8.log but with state=IDLE rather than state=EXTERNAL.
3191 // TODO(bnoordhuis) Depends on a libuv implementation detail that we should
3192 // probably fortify in the API contract, namely that the last started prepare
3193 // or check watcher runs first. It's not 100% foolproof; if an add-on starts
3194 // a prepare or check watcher after us, any samples attributed to its callback
3195 // will be recorded with state=IDLE.
3196 uv_prepare_init(env->event_loop(), env->idle_prepare_handle());
3197 uv_check_init(env->event_loop(), env->idle_check_handle());
3198 uv_unref(reinterpret_cast<uv_handle_t*>(env->idle_prepare_handle()));
3199 uv_unref(reinterpret_cast<uv_handle_t*>(env->idle_check_handle()));
3201 if (v8_is_profiling) {
3202 StartProfilerIdleNotifier(env);
3205 Local<FunctionTemplate> process_template = FunctionTemplate::New();
3206 process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process"));
3208 Local<Object> process_object = process_template->GetFunction()->NewInstance();
3209 env->set_process_object(process_object);
3211 SetupProcessObject(env, argc, argv, exec_argc, exec_argv);
3218 int Start(int argc, char** argv) {
3219 #if !defined(_WIN32)
3220 // Try hard not to lose SIGUSR1 signals during the bootstrap process.
3221 InstallEarlyDebugSignalHandler();
3226 // Hack around with the argv pointer. Used for process.title = "blah".
3227 argv = uv_setup_args(argc, argv);
3229 // This needs to run *before* V8::Initialize(). The const_cast is not
3230 // optional, in case you're wondering.
3232 const char** exec_argv;
3233 Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
3236 // V8 on Windows doesn't have a good source of entropy. Seed it from
3238 V8::SetEntropySource(crypto::EntropySource);
3243 Locker locker(node_isolate);
3245 CreateEnvironment(node_isolate, argc, argv, exec_argc, exec_argv);
3246 Context::Scope context_scope(env->context());
3247 HandleScope handle_scope(env->isolate());
3248 uv_run(env->event_loop(), UV_RUN_DEFAULT);
3256 // Clean up. Not strictly necessary.