#include "node_crypto.h"
#endif
-#if defined HAVE_DTRACE || defined HAVE_ETW || defined HAVE_SYSTEMTAP
+#if defined HAVE_DTRACE || defined HAVE_ETW
#include "node_dtrace.h"
#endif
-#if HAVE_SYSTEMTAP
-#include "node_provider.h"
-#endif
-
#include "ares.h"
+#include "async-wrap.h"
+#include "async-wrap-inl.h"
#include "env.h"
#include "env-inl.h"
#include "handle_wrap.h"
#include "string_bytes.h"
#include "uv.h"
#include "v8-debug.h"
+#include "v8-profiler.h"
#include "zlib.h"
#include <assert.h>
#define umask _umask
typedef int mode_t;
#else
+#include <sys/resource.h> // getrlimit, setrlimit
#include <unistd.h> // setuid, getuid
#endif
static bool use_debug_agent = false;
static bool debug_wait_connect = false;
static int debug_port = 5858;
+static bool v8_is_profiling = false;
+static node_module* modpending;
+static node_module* modlist_builtin;
+static node_module* modlist_addon;
// used by C++ modules as well
bool no_deprecation = false;
// process-relative uptime base, initialized at start-up
static double prog_start_time;
-
-static volatile bool debugger_running = false;
+static bool debugger_running;
static uv_async_t dispatch_debug_messages_async;
-static uv_async_t emit_debug_enabled_async;
// Declared in node_internals.h
Isolate* node_isolate = NULL;
static ArrayBufferAllocator the_singleton;
virtual ~ArrayBufferAllocator() {}
virtual void* Allocate(size_t length);
- virtual void Free(void* data);
+ virtual void* AllocateUninitialized(size_t length);
+ virtual void Free(void* data, size_t length);
private:
ArrayBufferAllocator() {}
ArrayBufferAllocator(const ArrayBufferAllocator&);
void* ArrayBufferAllocator::Allocate(size_t length) {
- if (length > kMaxLength) return NULL;
+ if (length > kMaxLength)
+ return NULL;
+ char* data = new char[length];
+ memset(data, 0, length);
+ return data;
+}
+
+
+void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
+ if (length > kMaxLength)
+ return NULL;
return new char[length];
}
-void ArrayBufferAllocator::Free(void* data) {
+void ArrayBufferAllocator::Free(void* data, size_t length) {
delete[] static_cast<char*>(data);
}
static void CheckImmediate(uv_check_t* handle, int status) {
+ HandleScope scope(node_isolate);
Environment* env = Environment::from_immediate_check_handle(handle);
Context::Scope context_scope(env->context());
MakeCallback(env, env->process_object(), env->immediate_callback_string());
#endif
+void SetupAsyncListener(const FunctionCallbackInfo<Value>& args) {
+ HandleScope handle_scope(args.GetIsolate());
+ Environment* env = Environment::GetCurrent(args.GetIsolate());
+
+ assert(args[0]->IsObject());
+ assert(args[1]->IsFunction());
+ assert(args[2]->IsFunction());
+ assert(args[3]->IsFunction());
+
+ env->set_async_listener_run_function(args[1].As<Function>());
+ env->set_async_listener_load_function(args[2].As<Function>());
+ env->set_async_listener_unload_function(args[3].As<Function>());
+
+ Local<Object> async_listener_flag_obj = args[0].As<Object>();
+ Environment::AsyncListener* async_listener = env->async_listener();
+ async_listener_flag_obj->SetIndexedPropertiesToExternalArrayData(
+ async_listener->fields(),
+ kExternalUnsignedIntArray,
+ async_listener->fields_count());
+
+ // Do a little housekeeping.
+ env->process_object()->Delete(
+ FIXED_ONE_BYTE_STRING(args.GetIsolate(), "_setupAsyncListener"));
+}
+
+
void SetupDomainUse(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
- if (env->using_domains()) return;
+ if (env->using_domains())
+ return;
env->set_using_domains(true);
HandleScope scope(node_isolate);
tick_callback_function);
env->set_tick_callback_function(tick_callback_function);
- if (!args[0]->IsArray()) {
- fprintf(stderr, "_setupDomainUse first argument must be an array\n");
- abort();
- }
- env->set_domain_array(args[0].As<Array>());
+ assert(args[0]->IsArray());
+ assert(args[1]->IsObject());
- if (!args[1]->IsObject()) {
- fprintf(stderr, "_setupDomainUse second argument must be an object\n");
- abort();
- }
+ env->set_domain_array(args[0].As<Array>());
Local<Object> domain_flag_obj = args[1].As<Object>();
Environment::DomainFlag* domain_flag = env->domain_flag();
domain_flag->fields(),
kExternalUnsignedIntArray,
domain_flag->fields_count());
+
+ // Do a little housekeeping.
+ env->process_object()->Delete(
+ FIXED_ONE_BYTE_STRING(args.GetIsolate(), "_setupDomainUse"));
+}
+
+
+void SetupNextTick(const FunctionCallbackInfo<Value>& args) {
+ HandleScope handle_scope(args.GetIsolate());
+ Environment* env = Environment::GetCurrent(args.GetIsolate());
+
+ assert(args[0]->IsObject());
+ assert(args[1]->IsFunction());
+
+ // Values use to cross communicate with processNextTick.
+ Local<Object> tick_info_obj = args[0].As<Object>();
+ tick_info_obj->SetIndexedPropertiesToExternalArrayData(
+ env->tick_info()->fields(),
+ kExternalUnsignedIntArray,
+ env->tick_info()->fields_count());
+
+ env->set_tick_callback_function(args[1].As<Function>());
+
+ // Do a little housekeeping.
+ env->process_object()->Delete(
+ FIXED_ONE_BYTE_STRING(args.GetIsolate(), "_setupNextTick"));
}
Handle<Value> MakeDomainCallback(Environment* env,
- const Handle<Object> object,
+ Handle<Value> recv,
const Handle<Function> callback,
int argc,
Handle<Value> argv[]) {
// If you hit this assertion, you forgot to enter the v8::Context first.
assert(env->context() == env->isolate()->GetCurrentContext());
- // TODO(trevnorris) Hook for long stack traces to be made here.
-
- Local<Value> domain_v = object->Get(env->domain_string());
- Local<Object> domain;
+ Local<Object> process = env->process_object();
+ Local<Object> object, domain;
+ Local<Value> domain_v;
TryCatch try_catch;
try_catch.SetVerbose(true);
- bool has_domain = domain_v->IsObject();
- if (has_domain) {
- domain = domain_v.As<Object>();
+ bool has_async_queue = false;
- if (domain->Get(env->disposed_string())->IsTrue()) {
- // domain has been disposed of.
- return Undefined(node_isolate);
+ if (recv->IsObject()) {
+ object = recv.As<Object>();
+ // TODO(trevnorris): This is sucky for performance. Fix it.
+ has_async_queue = object->Has(env->async_queue_string());
+ if (has_async_queue) {
+ env->async_listener_load_function()->Call(process, 1, &recv);
+
+ if (try_catch.HasCaught())
+ return Undefined(node_isolate);
}
+ }
- Local<Function> enter =
- domain->Get(env->enter_string()).As<Function>();
- assert(enter->IsFunction());
- enter->Call(domain, 0, NULL);
+ bool has_domain = false;
- if (try_catch.HasCaught()) {
- return Undefined(node_isolate);
+ if (!object.IsEmpty()) {
+ domain_v = object->Get(env->domain_string());
+ has_domain = domain_v->IsObject();
+ if (has_domain) {
+ domain = domain_v.As<Object>();
+
+ if (domain->Get(env->disposed_string())->IsTrue()) {
+ // domain has been disposed of.
+ return Undefined(node_isolate);
+ }
+
+ Local<Function> enter =
+ domain->Get(env->enter_string()).As<Function>();
+ assert(enter->IsFunction());
+ enter->Call(domain, 0, NULL);
+
+ if (try_catch.HasCaught()) {
+ return Undefined(node_isolate);
+ }
}
}
- Local<Value> ret = callback->Call(object, argc, argv);
+ Local<Value> ret = callback->Call(recv, argc, argv);
if (try_catch.HasCaught()) {
return Undefined(node_isolate);
}
}
+ if (has_async_queue) {
+ env->async_listener_unload_function()->Call(process, 1, &recv);
+
+ if (try_catch.HasCaught())
+ return Undefined(node_isolate);
+ }
+
Environment::TickInfo* tick_info = env->tick_info();
if (tick_info->last_threw() == 1) {
return ret;
}
- if (tick_info->in_tick() == 1) {
+ if (tick_info->in_tick()) {
return ret;
}
return ret;
}
- // process nextTicks after call
- Local<Object> process_object = env->process_object();
- Local<Function> tick_callback_function = env->tick_callback_function();
- tick_callback_function->Call(process_object, 0, NULL);
+ tick_info->set_in_tick(true);
+
+ env->tick_callback_function()->Call(process, 0, NULL);
+
+ tick_info->set_in_tick(false);
if (try_catch.HasCaught()) {
+ tick_info->set_last_threw(true);
return Undefined(node_isolate);
}
Handle<Value> MakeCallback(Environment* env,
- const Handle<Object> object,
+ Handle<Value> recv,
const Handle<Function> callback,
int argc,
Handle<Value> argv[]) {
+ if (env->using_domains())
+ return MakeDomainCallback(env, recv, callback, argc, argv);
+
// If you hit this assertion, you forgot to enter the v8::Context first.
assert(env->context() == env->isolate()->GetCurrentContext());
- // TODO(trevnorris) Hook for long stack traces to be made here.
- Local<Object> process_object = env->process_object();
-
- if (env->using_domains())
- return MakeDomainCallback(env, object, callback, argc, argv);
+ Local<Object> process = env->process_object();
TryCatch try_catch;
try_catch.SetVerbose(true);
- Local<Value> ret = callback->Call(object, argc, argv);
+ // TODO(trevnorris): This is sucky for performance. Fix it.
+ bool has_async_queue =
+ recv->IsObject() && recv.As<Object>()->Has(env->async_queue_string());
+ if (has_async_queue) {
+ env->async_listener_load_function()->Call(process, 1, &recv);
+ if (try_catch.HasCaught())
+ return Undefined(node_isolate);
+ }
+
+ Local<Value> ret = callback->Call(recv, argc, argv);
if (try_catch.HasCaught()) {
return Undefined(node_isolate);
}
+ if (has_async_queue) {
+ env->async_listener_unload_function()->Call(process, 1, &recv);
+
+ if (try_catch.HasCaught())
+ return Undefined(node_isolate);
+ }
+
Environment::TickInfo* tick_info = env->tick_info();
- if (tick_info->in_tick() == 1) {
+ if (tick_info->in_tick()) {
return ret;
}
return ret;
}
- // lazy load no domain next tick callbacks
- Local<Function> tick_callback_function = env->tick_callback_function();
- if (tick_callback_function.IsEmpty()) {
- Local<String> tick_callback_function_key =
- FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback");
- tick_callback_function =
- process_object->Get(tick_callback_function_key).As<Function>();
- if (!tick_callback_function->IsFunction()) {
- fprintf(stderr, "process._tickCallback assigned to non-function\n");
- abort();
- }
- env->set_tick_callback_function(tick_callback_function);
- }
+ tick_info->set_in_tick(true);
// process nextTicks after call
- tick_callback_function->Call(process_object, 0, NULL);
+ env->tick_callback_function()->Call(process, 0, NULL);
+
+ tick_info->set_in_tick(false);
if (try_catch.HasCaught()) {
+ tick_info->set_last_threw(true);
return Undefined(node_isolate);
}
// Internal only.
Handle<Value> MakeCallback(Environment* env,
- const Handle<Object> object,
+ Handle<Object> recv,
uint32_t index,
int argc,
Handle<Value> argv[]) {
- // If you hit this assertion, you forgot to enter the v8::Context first.
- assert(env->context() == env->isolate()->GetCurrentContext());
-
- Local<Function> callback = object->Get(index).As<Function>();
+ Local<Function> callback = recv->Get(index).As<Function>();
assert(callback->IsFunction());
- if (env->using_domains()) {
- return MakeDomainCallback(env, object, callback, argc, argv);
- }
-
- return MakeCallback(env, object, callback, argc, argv);
+ return MakeCallback(env, recv.As<Value>(), callback, argc, argv);
}
Handle<Value> MakeCallback(Environment* env,
- const Handle<Object> object,
- const Handle<String> symbol,
+ Handle<Object> recv,
+ Handle<String> symbol,
int argc,
Handle<Value> argv[]) {
- // If you hit this assertion, you forgot to enter the v8::Context first.
- assert(env->context() == env->isolate()->GetCurrentContext());
-
- Local<Function> callback = object->Get(symbol).As<Function>();
+ Local<Function> callback = recv->Get(symbol).As<Function>();
assert(callback->IsFunction());
-
- if (env->using_domains()) {
- return MakeDomainCallback(env, object, callback, argc, argv);
- }
-
- return MakeCallback(env, object, callback, argc, argv);
+ return MakeCallback(env, recv.As<Value>(), callback, argc, argv);
}
Handle<Value> MakeCallback(Environment* env,
- const Handle<Object> object,
+ Handle<Object> recv,
const char* method,
int argc,
Handle<Value> argv[]) {
- // If you hit this assertion, you forgot to enter the v8::Context first.
- assert(env->context() == env->isolate()->GetCurrentContext());
Local<String> method_string = OneByteString(node_isolate, method);
- return MakeCallback(env, object, method_string, argc, argv);
+ return MakeCallback(env, recv, method_string, argc, argv);
}
-Handle<Value> MakeCallback(const Handle<Object> object,
+Handle<Value> MakeCallback(Handle<Object> recv,
const char* method,
int argc,
Handle<Value> argv[]) {
- Local<Context> context = object->CreationContext();
+ HandleScope handle_scope(node_isolate); // FIXME(bnoordhuis) Isolate-ify.
+ Local<Context> context = recv->CreationContext();
Environment* env = Environment::GetCurrent(context);
Context::Scope context_scope(context);
- HandleScope handle_scope(env->isolate());
- return handle_scope.Close(MakeCallback(env, object, method, argc, argv));
+ return handle_scope.Close(MakeCallback(env, recv, method, argc, argv));
}
-Handle<Value> MakeCallback(const Handle<Object> object,
- const Handle<String> symbol,
+Handle<Value> MakeCallback(Handle<Object> recv,
+ Handle<String> symbol,
int argc,
Handle<Value> argv[]) {
- Local<Context> context = object->CreationContext();
+ HandleScope handle_scope(node_isolate); // FIXME(bnoordhuis) Isolate-ify.
+ Local<Context> context = recv->CreationContext();
Environment* env = Environment::GetCurrent(context);
Context::Scope context_scope(context);
- HandleScope handle_scope(env->isolate());
- return handle_scope.Close(MakeCallback(env, object, symbol, argc, argv));
+ return handle_scope.Close(MakeCallback(env, recv, symbol, argc, argv));
}
-Handle<Value> MakeCallback(const Handle<Object> object,
- const Handle<Function> callback,
+Handle<Value> MakeCallback(Handle<Object> recv,
+ Handle<Function> callback,
int argc,
Handle<Value> argv[]) {
- Local<Context> context = object->CreationContext();
+ HandleScope handle_scope(node_isolate); // FIXME(bnoordhuis) Isolate-ify.
+ Local<Context> context = recv->CreationContext();
Environment* env = Environment::GetCurrent(context);
Context::Scope context_scope(context);
- HandleScope handle_scope(env->isolate());
- return handle_scope.Close(MakeCallback(env, object, callback, argc, argv));
+ return handle_scope.Close(
+ MakeCallback(env, recv.As<Value>(), callback, argc, argv));
}
-Handle<Value> MakeDomainCallback(const Handle<Object> object,
- const Handle<Function> callback,
+Handle<Value> MakeDomainCallback(Handle<Object> recv,
+ Handle<Function> callback,
int argc,
Handle<Value> argv[]) {
- Local<Context> context = object->CreationContext();
+ Local<Context> context = recv->CreationContext();
Environment* env = Environment::GetCurrent(context);
Context::Scope context_scope(context);
HandleScope handle_scope(env->isolate());
return handle_scope.Close(
- MakeDomainCallback(env, object, callback, argc, argv));
+ MakeDomainCallback(env, recv, callback, argc, argv));
}
enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
HandleScope scope(node_isolate);
- if (!encoding_v->IsString()) return _default;
+ if (!encoding_v->IsString())
+ return _default;
String::Utf8Value encoding(encoding_v);
return StringBytes::Write(buf, buflen, val, encoding, NULL);
}
-void DisplayExceptionLine(Handle<Message> message) {
- // Prevent re-entry into this function. For example, if there is
- // a throw from a program in vm.runInThisContext(code, filename, true),
- // then we want to show the original failure, not the secondary one.
- static bool displayed_error = false;
-
- if (displayed_error) return;
- displayed_error = true;
+void AppendExceptionLine(Environment* env,
+ Handle<Value> er,
+ Handle<Message> message) {
+ if (message.IsEmpty())
+ return;
- uv_tty_reset_mode();
+ HandleScope scope(env->isolate());
+ Local<Object> err_obj;
+ if (!er.IsEmpty() && er->IsObject()) {
+ err_obj = er.As<Object>();
- fprintf(stderr, "\n");
-
- if (!message.IsEmpty()) {
- // Print (filename):(line number): (message).
- String::Utf8Value filename(message->GetScriptResourceName());
- const char* filename_string = *filename;
- int linenum = message->GetLineNumber();
- fprintf(stderr, "%s:%i\n", filename_string, linenum);
- // Print line of source code.
- String::Utf8Value sourceline(message->GetSourceLine());
- const char* sourceline_string = *sourceline;
-
- // Because of how node modules work, all scripts are wrapped with a
- // "function (module, exports, __filename, ...) {"
- // to provide script local variables.
- //
- // When reporting errors on the first line of a script, this wrapper
- // function is leaked to the user. There used to be a hack here to
- // truncate off the first 62 characters, but it caused numerous other
- // problems when vm.runIn*Context() methods were used for non-module
- // code.
- //
- // If we ever decide to re-instate such a hack, the following steps
- // must be taken:
- //
- // 1. Pass a flag around to say "this code was wrapped"
- // 2. Update the stack frame output so that it is also correct.
- //
- // It would probably be simpler to add a line rather than add some
- // number of characters to the first line, since V8 truncates the
- // sourceline to 78 characters, and we end up not providing very much
- // useful debugging info to the user if we remove 62 characters.
-
- int start = message->GetStartColumn();
- int end = message->GetEndColumn();
-
- fprintf(stderr, "%s\n", sourceline_string);
- // Print wavy underline (GetUnderline is deprecated).
- for (int i = 0; i < start; i++) {
- fputc((sourceline_string[i] == '\t') ? '\t' : ' ', stderr);
- }
- for (int i = start; i < end; i++) {
- fputc('^', stderr);
- }
- fputc('\n', stderr);
+ // Do it only once per message
+ if (!err_obj->GetHiddenValue(env->processed_string()).IsEmpty())
+ return;
+ err_obj->SetHiddenValue(env->processed_string(), True(env->isolate()));
}
+
+ static char arrow[1024];
+
+ // Print (filename):(line number): (message).
+ String::Utf8Value filename(message->GetScriptResourceName());
+ const char* filename_string = *filename;
+ int linenum = message->GetLineNumber();
+ // Print line of source code.
+ String::Utf8Value sourceline(message->GetSourceLine());
+ const char* sourceline_string = *sourceline;
+
+ // Because of how node modules work, all scripts are wrapped with a
+ // "function (module, exports, __filename, ...) {"
+ // to provide script local variables.
+ //
+ // When reporting errors on the first line of a script, this wrapper
+ // function is leaked to the user. There used to be a hack here to
+ // truncate off the first 62 characters, but it caused numerous other
+ // problems when vm.runIn*Context() methods were used for non-module
+ // code.
+ //
+ // If we ever decide to re-instate such a hack, the following steps
+ // must be taken:
+ //
+ // 1. Pass a flag around to say "this code was wrapped"
+ // 2. Update the stack frame output so that it is also correct.
+ //
+ // It would probably be simpler to add a line rather than add some
+ // number of characters to the first line, since V8 truncates the
+ // sourceline to 78 characters, and we end up not providing very much
+ // useful debugging info to the user if we remove 62 characters.
+
+ int start = message->GetStartColumn();
+ int end = message->GetEndColumn();
+
+ int off = snprintf(arrow,
+ sizeof(arrow),
+ "%s:%i\n%s\n",
+ filename_string,
+ linenum,
+ sourceline_string);
+ assert(off >= 0);
+
+ // Print wavy underline (GetUnderline is deprecated).
+ for (int i = 0; i < start; i++) {
+ assert(static_cast<size_t>(off) < sizeof(arrow));
+ arrow[off++] = (sourceline_string[i] == '\t') ? '\t' : ' ';
+ }
+ for (int i = start; i < end; i++) {
+ assert(static_cast<size_t>(off) < sizeof(arrow));
+ arrow[off++] = '^';
+ }
+ assert(static_cast<size_t>(off) < sizeof(arrow) - 1);
+ arrow[off++] = '\n';
+ arrow[off] = '\0';
+
+ Local<String> arrow_str = String::NewFromUtf8(env->isolate(), arrow);
+ Local<Value> msg;
+ Local<Value> stack;
+
+ // Allocation failed, just print it out
+ if (arrow_str.IsEmpty() || err_obj.IsEmpty() || !err_obj->IsNativeError())
+ goto print;
+
+ msg = err_obj->Get(env->message_string());
+ stack = err_obj->Get(env->stack_string());
+
+ if (msg.IsEmpty() || stack.IsEmpty())
+ goto print;
+
+ err_obj->Set(env->message_string(),
+ String::Concat(arrow_str, msg->ToString()));
+ err_obj->Set(env->stack_string(),
+ String::Concat(arrow_str, stack->ToString()));
+ return;
+
+ print:
+ if (env->printed_error())
+ return;
+ env->set_printed_error(true);
+ uv_tty_reset_mode();
+ fprintf(stderr, "\n%s", arrow);
}
-static void ReportException(Handle<Value> er, Handle<Message> message) {
- HandleScope scope(node_isolate);
+static void ReportException(Environment* env,
+ Handle<Value> er,
+ Handle<Message> message) {
+ HandleScope scope(env->isolate());
+
+ AppendExceptionLine(env, er, message);
- DisplayExceptionLine(message);
+ Local<Value> trace_value;
+
+ if (er->IsUndefined() || er->IsNull())
+ trace_value = Undefined(env->isolate());
+ else
+ trace_value = er->ToObject()->Get(env->stack_string());
- Local<Value> trace_value(
- er->ToObject()->Get(FIXED_ONE_BYTE_STRING(node_isolate, "stack")));
String::Utf8Value trace(trace_value);
// range errors have a trace member set to undefined
if (er->IsObject()) {
Local<Object> err_obj = er.As<Object>();
- message = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "message"));
- name = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "name"));
+ message = err_obj->Get(env->message_string());
+ name = err_obj->Get(FIXED_ONE_BYTE_STRING(env->isolate(), "name"));
}
if (message.IsEmpty() ||
}
-static void ReportException(const TryCatch& try_catch) {
- ReportException(try_catch.Exception(), try_catch.Message());
+static void ReportException(Environment* env, const TryCatch& try_catch) {
+ ReportException(env, try_catch.Exception(), try_catch.Message());
}
// Executes a str within the current v8 context.
-Local<Value> ExecuteString(Handle<String> source, Handle<Value> filename) {
- HandleScope scope(node_isolate);
+static Local<Value> ExecuteString(Environment* env,
+ Handle<String> source,
+ Handle<Value> filename) {
+ HandleScope scope(env->isolate());
TryCatch try_catch;
// try_catch must be nonverbose to disable FatalException() handler,
Local<v8::Script> script = v8::Script::Compile(source, filename);
if (script.IsEmpty()) {
- ReportException(try_catch);
+ ReportException(env, try_catch);
exit(3);
}
Local<Value> result = script->Run();
if (result.IsEmpty()) {
- ReportException(try_catch);
+ ReportException(env, try_catch);
exit(4);
}
int i = 0;
QUEUE_FOREACH(q, &req_wrap_queue) {
- ReqWrap<uv_req_t>* w = container_of(q, ReqWrap<uv_req_t>, req_wrap_queue_);
- if (w->persistent().IsEmpty()) continue;
+ ReqWrap<uv_req_t>* w = CONTAINER_OF(q, ReqWrap<uv_req_t>, req_wrap_queue_);
+ if (w->persistent().IsEmpty())
+ continue;
ary->Set(i++, w->object());
}
Local<String> owner_sym = FIXED_ONE_BYTE_STRING(node_isolate, "owner");
QUEUE_FOREACH(q, &handle_wrap_queue) {
- HandleWrap* w = container_of(q, HandleWrap, handle_wrap_queue_);
- if (w->persistent().IsEmpty() || (w->flags_ & HandleWrap::kUnref)) continue;
+ HandleWrap* w = CONTAINER_OF(q, HandleWrap, handle_wrap_queue_);
+ if (w->persistent().IsEmpty() || (w->flags_ & HandleWrap::kUnref))
+ continue;
Local<Object> object = w->object();
Local<Value> owner = object->Get(owner_sym);
- if (owner->IsUndefined()) owner = object;
+ if (owner->IsUndefined())
+ owner = object;
ary->Set(i++, owner);
}
for (int i = 0; i < ngroups; i++) {
groups_list->Set(i, Integer::New(groups[i], node_isolate));
- if (groups[i] == egid) seen_egid = true;
+ if (groups[i] == egid)
+ seen_egid = true;
}
delete[] groups;
extra_group = gid_by_name(args[1]);
if (extra_group == gid_not_found) {
- if (must_free) free(user);
+ if (must_free)
+ free(user);
return ThrowError("initgroups extra group not found");
}
static void Uptime(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);
double uptime;
- if (uv_uptime(&uptime)) return;
+ if (uv_uptime(&uptime))
+ return;
args.GetReturnValue().Set(uptime - prog_start_time);
}
void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
- Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope handle_scope(args.GetIsolate());
+ Environment* env = Environment::GetCurrent(args.GetIsolate());
size_t rss;
int err = uv_resident_set_memory(&rss);
args.GetReturnValue().Set(tuple);
}
+extern "C" void node_module_register(void* m) {
+ struct node_module* mp = reinterpret_cast<struct node_module*>(m);
+
+ if (mp->nm_flags & NM_F_BUILTIN) {
+ mp->nm_link = modlist_builtin;
+ modlist_builtin = mp;
+ } else {
+ assert(modpending == NULL);
+ modpending = mp;
+ }
+}
+
+struct node_module* get_builtin_module(const char* name) {
+ struct node_module* mp;
+
+ for (mp = modlist_builtin; mp != NULL; mp = mp->nm_link) {
+ if (strcmp(mp->nm_modname, name) == 0)
+ break;
+ }
+
+ assert(mp == NULL || (mp->nm_flags & NM_F_BUILTIN) != 0);
+ return (mp);
+}
typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);
// when two contexts try to load the same shared object. Maybe have a shadow
// cache that's a plain C list or hash table that's shared across contexts?
void DLOpen(const FunctionCallbackInfo<Value>& args) {
- Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope handle_scope(args.GetIsolate());
- char symbol[1024], *base, *pos;
+ Environment* env = Environment::GetCurrent(args.GetIsolate());
+ struct node_module* mp;
uv_lib_t lib;
- int r;
if (args.Length() < 2) {
- return ThrowError("process.dlopen takes exactly 2 arguments.");
+ ThrowError("process.dlopen takes exactly 2 arguments.");
+ return;
}
Local<Object> module = args[0]->ToObject(); // Cast
return;
}
- String::Utf8Value path(args[1]);
- base = *path;
-
- /* Find the shared library filename within the full path. */
-#ifdef __POSIX__
- pos = strrchr(base, '/');
- if (pos != NULL) {
- base = pos + 1;
- }
-#else // Windows
- for (;;) {
- pos = strpbrk(base, "\\/:");
- if (pos == NULL) {
- break;
- }
- base = pos + 1;
- }
-#endif // __POSIX__
-
- /* Strip the .node extension. */
- pos = strrchr(base, '.');
- if (pos != NULL) {
- *pos = '\0';
- }
-
- /* Add the `_module` suffix to the extension name. */
- r = snprintf(symbol, sizeof symbol, "%s_module", base);
- if (r <= 0 || static_cast<size_t>(r) >= sizeof symbol) {
- return ThrowError("Out of memory.");
- }
-
- /* Replace dashes with underscores. When loading foo-bar.node,
- * look for foo_bar_module, not foo-bar_module.
+ /*
+ * Objects containing v14 or later modules will have registered themselves
+ * on the pending list. Activate all of them now. At present, only one
+ * module per object is supported.
*/
- for (pos = symbol; *pos != '\0'; ++pos) {
- if (*pos == '-') *pos = '_';
- }
+ mp = modpending;
+ modpending = NULL;
- node_module_struct *mod;
- if (uv_dlsym(&lib, symbol, reinterpret_cast<void**>(&mod))) {
- char errmsg[1024];
- snprintf(errmsg, sizeof(errmsg), "Symbol %s not found.", symbol);
- return ThrowError(errmsg);
+ if (mp == NULL) {
+ ThrowError("Module did not self-register.");
+ return;
}
-
- if (mod->version != NODE_MODULE_VERSION) {
+ if (mp->nm_version != NODE_MODULE_VERSION) {
char errmsg[1024];
snprintf(errmsg,
sizeof(errmsg),
"Module version mismatch. Expected %d, got %d.",
- NODE_MODULE_VERSION, mod->version);
- return ThrowError(errmsg);
+ NODE_MODULE_VERSION, mp->nm_version);
+ ThrowError(errmsg);
+ return;
}
+ if (mp->nm_flags & NM_F_BUILTIN) {
+ ThrowError("Built-in module self-registered.");
+ return;
+ }
+
+ mp->nm_dso_handle = lib.handle;
+ mp->nm_link = modlist_addon;
+ modlist_addon = mp;
- // Execute the C++ module
- if (mod->register_context_func != NULL) {
- mod->register_context_func(exports, module, env->context());
- } else if (mod->register_func != NULL) {
- mod->register_func(exports, module);
+ if (mp->nm_context_register_func != NULL) {
+ mp->nm_context_register_func(exports, module, env->context(), mp->nm_priv);
+ } else if (mp->nm_register_func != NULL) {
+ mp->nm_register_func(exports, module, mp->nm_priv);
} else {
- return ThrowError("Module has no declared entry point.");
+ ThrowError("Module has no declared entry point.");
+ return;
}
// Tell coverity that 'handle' should not be freed when we return.
fprintf(stderr, "FATAL ERROR: %s\n", message);
}
fflush(stderr);
-#if defined(DEBUG)
abort();
-#endif
- exit(5);
}
if (!fatal_exception_function->IsFunction()) {
// failed before the process._fatalException function was added!
// this is probably pretty bad. Nothing to do but report and exit.
- ReportException(error, message);
+ ReportException(env, error, message);
exit(6);
}
if (fatal_try_catch.HasCaught()) {
// the fatal exception function threw, so we must exit
- ReportException(fatal_try_catch);
+ ReportException(env, fatal_try_catch);
exit(7);
}
if (false == caught->BooleanValue()) {
- ReportException(error, message);
- exit(8);
+ ReportException(env, error, message);
+ exit(1);
}
}
static void Binding(const FunctionCallbackInfo<Value>& args) {
- Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope handle_scope(args.GetIsolate());
+ Environment* env = Environment::GetCurrent(args.GetIsolate());
Local<String> module = args[0]->ToString();
String::Utf8Value module_v(module);
uint32_t l = modules->Length();
modules->Set(l, OneByteString(node_isolate, buf));
- node_module_struct* mod = get_builtin_module(*module_v);
+ node_module* mod = get_builtin_module(*module_v);
if (mod != NULL) {
exports = Object::New();
// Internal bindings don't have a "module" object, only exports.
- assert(mod->register_func == NULL);
- assert(mod->register_context_func != NULL);
+ assert(mod->nm_register_func == NULL);
+ assert(mod->nm_context_register_func != NULL);
Local<Value> unused = Undefined(env->isolate());
- mod->register_context_func(exports, unused, env->context());
+ mod->nm_context_register_func(exports, unused,
+ env->context(), mod->nm_priv);
cache->Set(module, exports);
} else if (!strcmp(*module_v, "constants")) {
exports = Object::New();
int32_t rc = -1; // Not found unless proven otherwise.
#ifdef __POSIX__
String::Utf8Value key(property);
- if (getenv(*key)) rc = 0;
+ if (getenv(*key))
+ rc = 0;
#else // _WIN32
String::Value key(property);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
}
}
#endif
- if (rc != -1) info.GetReturnValue().Set(rc);
+ if (rc != -1)
+ info.GetReturnValue().Set(rc);
}
#ifdef __POSIX__
String::Utf8Value key(property);
rc = getenv(*key) != NULL;
- if (rc) unsetenv(*key);
+ if (rc)
+ unsetenv(*key);
#else
String::Value key(property);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
HandleScope scope(node_isolate);
#ifdef __POSIX__
int size = 0;
- while (environ[size]) size++;
+ while (environ[size])
+ size++;
Local<Array> env = Array::New(size);
}
#else // _WIN32
WCHAR* environment = GetEnvironmentStringsW();
- if (environment == NULL) return; // This should not happen.
+ if (environment == NULL)
+ return; // This should not happen.
Local<Array> env = Array::New();
WCHAR* p = environment;
int i = 0;
void NeedImmediateCallbackGetter(Local<String> property,
const PropertyCallbackInfo<Value>& info) {
+ HandleScope handle_scope(info.GetIsolate());
Environment* env = Environment::GetCurrent(info.GetIsolate());
const uv_check_t* immediate_check_handle = env->immediate_check_handle();
bool active = uv_is_active(
Local<String> property,
Local<Value> value,
const PropertyCallbackInfo<void>& info) {
- Environment* env = Environment::GetCurrent(info.GetIsolate());
HandleScope handle_scope(info.GetIsolate());
+ Environment* env = Environment::GetCurrent(info.GetIsolate());
uv_check_t* immediate_check_handle = env->immediate_check_handle();
bool active = uv_is_active(
}
+void SetIdle(uv_prepare_t* handle, int) {
+ Environment* env = Environment::from_idle_prepare_handle(handle);
+ env->isolate()->GetCpuProfiler()->SetIdle(true);
+}
+
+
+void ClearIdle(uv_check_t* handle, int) {
+ Environment* env = Environment::from_idle_check_handle(handle);
+ env->isolate()->GetCpuProfiler()->SetIdle(false);
+}
+
+
+void StartProfilerIdleNotifier(Environment* env) {
+ uv_prepare_start(env->idle_prepare_handle(), SetIdle);
+ uv_check_start(env->idle_check_handle(), ClearIdle);
+}
+
+
+void StopProfilerIdleNotifier(Environment* env) {
+ uv_prepare_stop(env->idle_prepare_handle());
+ uv_check_stop(env->idle_check_handle());
+}
+
+
+void StartProfilerIdleNotifier(const FunctionCallbackInfo<Value>& args) {
+ HandleScope handle_scope(args.GetIsolate());
+ Environment* env = Environment::GetCurrent(args.GetIsolate());
+ StartProfilerIdleNotifier(env);
+}
+
+
+void StopProfilerIdleNotifier(const FunctionCallbackInfo<Value>& args) {
+ HandleScope handle_scope(args.GetIsolate());
+ Environment* env = Environment::GetCurrent(args.GetIsolate());
+ StopProfilerIdleNotifier(env);
+}
+
+
#define READONLY_PROPERTY(obj, str, var) \
do { \
obj->Set(OneByteString(node_isolate, str), var, v8::ReadOnly); \
int exec_argc,
const char* const* exec_argv) {
HandleScope scope(node_isolate);
- int i, j;
Local<Object> process = env->process_object();
#if HAVE_OPENSSL
// Stupid code to slice out the version string.
- int c, l = strlen(OPENSSL_VERSION_TEXT);
- for (i = j = 0; i < l; i++) {
- c = OPENSSL_VERSION_TEXT[i];
- if ('0' <= c && c <= '9') {
- for (j = i + 1; j < l; j++) {
- c = OPENSSL_VERSION_TEXT[j];
- if (c == ' ') break;
+ { // NOLINT(whitespace/braces)
+ size_t i, j, k;
+ int c;
+ for (i = j = 0, k = sizeof(OPENSSL_VERSION_TEXT) - 1; i < k; ++i) {
+ c = OPENSSL_VERSION_TEXT[i];
+ if ('0' <= c && c <= '9') {
+ for (j = i + 1; j < k; ++j) {
+ c = OPENSSL_VERSION_TEXT[j];
+ if (c == ' ')
+ break;
+ }
+ break;
}
- break;
}
+ READONLY_PROPERTY(
+ versions,
+ "openssl",
+ OneByteString(node_isolate, &OPENSSL_VERSION_TEXT[i], j - i));
}
- READONLY_PROPERTY(
- versions,
- "openssl",
- OneByteString(node_isolate, &OPENSSL_VERSION_TEXT[i], j - i));
#endif
// process.arch
DebugPortSetter);
// define various internal methods
+ NODE_SET_METHOD(process,
+ "_startProfilerIdleNotifier",
+ StartProfilerIdleNotifier);
+ NODE_SET_METHOD(process,
+ "_stopProfilerIdleNotifier",
+ StopProfilerIdleNotifier);
NODE_SET_METHOD(process, "_getActiveRequests", GetActiveRequests);
NODE_SET_METHOD(process, "_getActiveHandles", GetActiveHandles);
NODE_SET_METHOD(process, "reallyExit", Exit);
NODE_SET_METHOD(process, "binding", Binding);
+ NODE_SET_METHOD(process, "_setupAsyncListener", SetupAsyncListener);
+ NODE_SET_METHOD(process, "_setupNextTick", SetupNextTick);
NODE_SET_METHOD(process, "_setupDomainUse", SetupDomainUse);
// values use to cross communicate with processNextTick
// are not safe to ignore.
try_catch.SetVerbose(false);
- Local<String> script_name = FIXED_ONE_BYTE_STRING(node_isolate, "node.js");
- Local<Value> f_value = ExecuteString(MainSource(), script_name);
+ Local<String> script_name = FIXED_ONE_BYTE_STRING(env->isolate(), "node.js");
+ Local<Value> f_value = ExecuteString(env, MainSource(), script_name);
if (try_catch.HasCaught()) {
- ReportException(try_catch);
+ ReportException(env, try_catch);
exit(10);
}
assert(f_value->IsFunction());
// Add a reference to the global object
Local<Object> global = env->context()->Global();
-#if defined HAVE_DTRACE || defined HAVE_ETW || defined HAVE_SYSTEMTAP
+#if defined HAVE_DTRACE || defined HAVE_ETW
InitDTrace(global);
#endif
static void PrintHelp();
-static void ParseDebugOpt(const char* arg) {
- const char *p = 0;
+static bool ParseDebugOpt(const char* arg) {
+ const char* port = NULL;
- if (strstr(arg, "--debug-port=") == arg) {
- p = 1 + strchr(arg, '=');
- debug_port = atoi(p);
- } else {
+ if (!strcmp(arg, "--debug")) {
use_debug_agent = true;
- if (!strcmp(arg, "--debug-brk")) {
- debug_wait_connect = true;
- return;
- } else if (!strcmp(arg, "--debug")) {
- return;
- } else if (strstr(arg, "--debug-brk=") == arg) {
- debug_wait_connect = true;
- p = 1 + strchr(arg, '=');
- debug_port = atoi(p);
- } else if (strstr(arg, "--debug=") == arg) {
- p = 1 + strchr(arg, '=');
- debug_port = atoi(p);
- }
+ } else if (!strncmp(arg, "--debug=", sizeof("--debug=") - 1)) {
+ use_debug_agent = true;
+ port = arg + sizeof("--debug=") - 1;
+ } else if (!strcmp(arg, "--debug-brk")) {
+ use_debug_agent = true;
+ debug_wait_connect = true;
+ } else if (!strncmp(arg, "--debug-brk=", sizeof("--debug-brk=") - 1)) {
+ use_debug_agent = true;
+ debug_wait_connect = true;
+ port = arg + sizeof("--debug-brk=") - 1;
+ } else if (!strncmp(arg, "--debug-port=", sizeof("--debug-port=") - 1)) {
+ port = arg + sizeof("--debug-port=") - 1;
+ } else {
+ return false;
}
- if (p && debug_port > 1024 && debug_port < 65536)
- return;
- fprintf(stderr, "Bad debug option.\n");
- if (p) fprintf(stderr, "Debug port must be in range 1025 to 65535.\n");
+ if (port != NULL) {
+ debug_port = atoi(port);
+ if (debug_port < 1024 || debug_port > 65535) {
+ fprintf(stderr, "Debug port must be in range 1024 to 65535.\n");
+ PrintHelp();
+ exit(12);
+ }
+ }
- PrintHelp();
- exit(12);
+ return true;
}
static void PrintHelp() {
const char* const arg = argv[index];
unsigned int args_consumed = 1;
- if (strstr(arg, "--debug") == arg) {
- ParseDebugOpt(arg);
+ if (ParseDebugOpt(arg)) {
+ // Done, consumed by ParseDebugOpt().
} else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
printf("%s\n", NODE_VERSION);
exit(0);
eval_string = argv[index + 1];
if (eval_string == NULL) {
fprintf(stderr, "%s: %s requires an argument\n", argv[0], arg);
- exit(1);
+ exit(9);
}
- } else if (argv[index + 1] != NULL && argv[index + 1][0] != '-') {
+ } else if ((index + 1 < nargs) &&
+ argv[index + 1] != NULL &&
+ argv[index + 1][0] != '-') {
args_consumed += 1;
eval_string = argv[index + 1];
if (strncmp(eval_string, "\\-", 2) == 0) {
}
-// Called from the main thread.
-static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle, int status) {
- v8::Debug::ProcessDebugMessages();
-}
-
-
// Called from V8 Debug Agent TCP thread.
static void DispatchMessagesDebugAgentCallback() {
uv_async_send(&dispatch_debug_messages_async);
}
-// Called from the main thread
-static void EmitDebugEnabledAsyncCallback(uv_async_t* handle, int status) {
- Environment* env = Environment::GetCurrent(node_isolate);
+// Called from the main thread.
+static void EnableDebug(bool wait_connect) {
+ assert(debugger_running == false);
+ Isolate* isolate = node_isolate; // TODO(bnoordhuis) Multi-isolate support.
+ Isolate::Scope isolate_scope(isolate);
+ HandleScope handle_scope(isolate);
+ v8::Debug::SetDebugMessageDispatchHandler(DispatchMessagesDebugAgentCallback,
+ false);
+ debugger_running = v8::Debug::EnableAgent("node " NODE_VERSION,
+ debug_port,
+ wait_connect);
+ if (debugger_running == false) {
+ fprintf(stderr, "Starting debugger on port %d failed\n", debug_port);
+ fflush(stderr);
+ return;
+ }
+ fprintf(stderr, "Debugger listening on port %d\n", debug_port);
+ fflush(stderr);
+
+ Environment* env = Environment::GetCurrentChecked(isolate);
+ if (env == NULL)
+ return; // Still starting up.
+
Context::Scope context_scope(env->context());
- HandleScope handle_scope(env->isolate());
Local<Object> message = Object::New();
- message->Set(FIXED_ONE_BYTE_STRING(node_isolate, "cmd"),
- FIXED_ONE_BYTE_STRING(node_isolate, "NODE_DEBUG_ENABLED"));
- Local<Value> args[] = {
- FIXED_ONE_BYTE_STRING(node_isolate, "internalMessage"),
+ message->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "cmd"),
+ FIXED_ONE_BYTE_STRING(env->isolate(), "NODE_DEBUG_ENABLED"));
+ Local<Value> argv[] = {
+ FIXED_ONE_BYTE_STRING(env->isolate(), "internalMessage"),
message
};
- MakeCallback(env, env->process_object(), "emit", ARRAY_SIZE(args), args);
+ MakeCallback(env, env->process_object(), "emit", ARRAY_SIZE(argv), argv);
}
-// Called from the signal watcher callback
-static void EmitDebugEnabled() {
- uv_async_send(&emit_debug_enabled_async);
+// Called from the main thread.
+static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle, int status) {
+ if (debugger_running == false) {
+ fprintf(stderr, "Starting debugger agent.\n");
+ EnableDebug(false);
+ }
+ Isolate::Scope isolate_scope(node_isolate);
+ v8::Debug::ProcessDebugMessages();
}
-static void EnableDebug(bool wait_connect) {
- // If we're called from another thread, make sure to enter the right
- // v8 isolate.
- node_isolate->Enter();
-
- v8::Debug::SetDebugMessageDispatchHandler(DispatchMessagesDebugAgentCallback,
- false);
-
- // Start the debug thread and it's associated TCP server on port 5858.
- bool r = v8::Debug::EnableAgent("node " NODE_VERSION,
- debug_port,
- wait_connect);
-
- // Crappy check that everything went well. FIXME
- assert(r);
+#ifdef __POSIX__
+static volatile sig_atomic_t caught_early_debug_signal;
- // Print out some information.
- fprintf(stderr, "debugger listening on port %d\n", debug_port);
- fflush(stderr);
- debugger_running = true;
+static void EarlyDebugSignalHandler(int signo) {
+ caught_early_debug_signal = 1;
+}
- if (Environment::GetCurrentChecked(node_isolate) != NULL) {
- EmitDebugEnabled();
- }
- node_isolate->Exit();
+static void InstallEarlyDebugSignalHandler() {
+ struct sigaction sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = EarlyDebugSignalHandler;
+ sigaction(SIGUSR1, &sa, NULL);
}
-#ifdef __POSIX__
-static void EnableDebugSignalHandler(uv_signal_t* handle, int) {
- // Break once process will return execution to v8
- v8::Debug::DebugBreak(node_isolate);
-
- if (!debugger_running) {
- fprintf(stderr, "Hit SIGUSR1 - starting debugger agent.\n");
- EnableDebug(false);
- }
+static void EnableDebugSignalHandler(int signo) {
+ // Call only async signal-safe functions here!
+ v8::Debug::DebugBreak(*static_cast<Isolate* volatile*>(&node_isolate));
+ uv_async_send(&dispatch_debug_messages_async);
}
static void RegisterSignalHandler(int signal, void (*handler)(int signal)) {
struct sigaction sa;
-
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
sigfillset(&sa.sa_mask);
return ThrowErrnoException(errno, "kill");
}
}
+
+
+static int RegisterDebugSignalHandler() {
+ // FIXME(bnoordhuis) Should be per-isolate or per-context, not global.
+ RegisterSignalHandler(SIGUSR1, EnableDebugSignalHandler);
+ // If we caught a SIGUSR1 during the bootstrap process, re-raise it
+ // now that the debugger infrastructure is in place.
+ if (caught_early_debug_signal)
+ raise(SIGUSR1);
+ return 0;
+}
#endif // __POSIX__
#ifdef _WIN32
DWORD WINAPI EnableDebugThreadProc(void* arg) {
- // Break once process will return execution to v8
- if (!debugger_running) {
- for (int i = 0; i < 1; i++) {
- fprintf(stderr, "Starting debugger agent.\r\n");
- fflush(stderr);
- EnableDebug(false);
- }
- }
-
- v8::Debug::DebugBreak(node_isolate);
-
+ v8::Debug::DebugBreak(*static_cast<Isolate* volatile*>(&node_isolate));
+ uv_async_send(&dispatch_debug_messages_async);
return 0;
}
DispatchDebugMessagesAsyncCallback);
uv_unref(reinterpret_cast<uv_handle_t*>(&dispatch_debug_messages_async));
- // init async NODE_DEBUG_ENABLED emitter
- // FIXME(bnoordhuis) Should be per-isolate or per-context, not global.
- uv_async_init(uv_default_loop(),
- &emit_debug_enabled_async,
- EmitDebugEnabledAsyncCallback);
- uv_unref(reinterpret_cast<uv_handle_t*>(&emit_debug_enabled_async));
-
// Parse a few arguments which are specific to Node.
int v8_argc;
const char** v8_argv;
ParseArgs(argc, argv, exec_argc, exec_argv, &v8_argc, &v8_argv);
+ // TODO(bnoordhuis) Intercept --prof arguments and start the CPU profiler
+ // manually? That would give us a little more control over its runtime
+ // behavior but it could also interfere with the user's intentions in ways
+ // we fail to anticipate. Dillema.
+ for (int i = 1; i < v8_argc; ++i) {
+ if (strncmp(v8_argv[i], "--prof", sizeof("--prof") - 1) == 0) {
+ v8_is_profiling = true;
+ break;
+ }
+ }
+
// The const_cast doesn't violate conceptual const-ness. V8 doesn't modify
// the argv array or the elements it points to.
V8::SetFlagsFromCommandLine(&v8_argc, const_cast<char**>(v8_argv), true);
v8_argv = NULL;
if (v8_argc > 1) {
- exit(1);
+ exit(9);
}
if (debug_wait_connect) {
V8::SetFlagsFromString(expose_debug_as, sizeof(expose_debug_as) - 1);
}
- const char typed_arrays_flag[] = "--harmony_typed_arrays";
- V8::SetFlagsFromString(typed_arrays_flag, sizeof(typed_arrays_flag) - 1);
V8::SetArrayBufferAllocator(&ArrayBufferAllocator::the_singleton);
// Fetch a reference to the main isolate, so we have a reference to it
node_isolate = Isolate::GetCurrent();
#ifdef __POSIX__
+ // Raise the open file descriptor limit.
+ { // NOLINT (whitespace/braces)
+ struct rlimit lim;
+ if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
+ // Do a binary search for the limit.
+ rlim_t min = lim.rlim_cur;
+ rlim_t max = 1 << 20;
+ // But if there's a defined upper bound, don't search, just set it.
+ if (lim.rlim_max != RLIM_INFINITY) {
+ min = lim.rlim_max;
+ max = lim.rlim_max;
+ }
+ do {
+ lim.rlim_cur = min + (max - min) / 2;
+ if (setrlimit(RLIMIT_NOFILE, &lim)) {
+ max = lim.rlim_cur;
+ } else {
+ min = lim.rlim_cur;
+ }
+ } while (min + 1 < max);
+ }
+ }
// Ignore SIGPIPE
RegisterSignalHandler(SIGPIPE, SIG_IGN);
RegisterSignalHandler(SIGINT, SignalExit);
if (use_debug_agent) {
EnableDebug(debug_wait_connect);
} else {
-#ifdef _WIN32
RegisterDebugSignalHandler();
-#else // Posix
- // FIXME(bnoordhuis) Should be per-isolate or per-context, not global.
- static uv_signal_t signal_watcher;
- uv_signal_init(uv_default_loop(), &signal_watcher);
- uv_signal_start(&signal_watcher, EnableDebugSignalHandler, SIGUSR1);
- uv_unref(reinterpret_cast<uv_handle_t*>(&signal_watcher));
-#endif // __POSIX__
}
}
}
-void EmitExit(Environment* env) {
+int EmitExit(Environment* env) {
// process.emit('exit')
- Context::Scope context_scope(env->context());
HandleScope handle_scope(env->isolate());
+ Context::Scope context_scope(env->context());
Local<Object> process_object = env->process_object();
process_object->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_exiting"),
True(node_isolate));
+
+ Handle<String> exitCode = FIXED_ONE_BYTE_STRING(node_isolate, "exitCode");
+ int code = process_object->Get(exitCode)->IntegerValue();
+
Local<Value> args[] = {
FIXED_ONE_BYTE_STRING(node_isolate, "exit"),
- Integer::New(0, node_isolate)
+ Integer::New(code, node_isolate)
};
+
MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args);
+
+ // Reload exit code, it may be changed by `emit('exit')`
+ return process_object->Get(exitCode)->IntegerValue();
}
reinterpret_cast<uv_handle_t*>(env->immediate_check_handle()));
uv_idle_init(env->event_loop(), env->immediate_idle_handle());
+ // Inform V8's CPU profiler when we're idle. The profiler is sampling-based
+ // but not all samples are created equal; mark the wall clock time spent in
+ // epoll_wait() and friends so profiling tools can filter it out. The samples
+ // still end up in v8.log but with state=IDLE rather than state=EXTERNAL.
+ // TODO(bnoordhuis) Depends on a libuv implementation detail that we should
+ // probably fortify in the API contract, namely that the last started prepare
+ // or check watcher runs first. It's not 100% foolproof; if an add-on starts
+ // a prepare or check watcher after us, any samples attributed to its callback
+ // will be recorded with state=IDLE.
+ uv_prepare_init(env->event_loop(), env->idle_prepare_handle());
+ uv_check_init(env->event_loop(), env->idle_check_handle());
+ uv_unref(reinterpret_cast<uv_handle_t*>(env->idle_prepare_handle()));
+ uv_unref(reinterpret_cast<uv_handle_t*>(env->idle_check_handle()));
+
+ if (v8_is_profiling) {
+ StartProfilerIdleNotifier(env);
+ }
+
Local<FunctionTemplate> process_template = FunctionTemplate::New();
process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process"));
int Start(int argc, char** argv) {
+#if !defined(_WIN32)
+ // Try hard not to lose SIGUSR1 signals during the bootstrap process.
+ InstallEarlyDebugSignalHandler();
+#endif
+
assert(argc > 0);
// Hack around with the argv pointer. Used for process.title = "blah".
const char** exec_argv;
Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
+#if HAVE_OPENSSL
+ // V8 on Windows doesn't have a good source of entropy. Seed it from
+ // OpenSSL's pool.
+ V8::SetEntropySource(crypto::EntropySource);
+#endif
+
+ int code;
V8::Initialize();
{
Locker locker(node_isolate);
Environment* env =
CreateEnvironment(node_isolate, argc, argv, exec_argc, exec_argv);
+ // This Context::Scope is here so EnableDebug() can look up the current
+ // environment with Environment::GetCurrentChecked().
+ // TODO(bnoordhuis) Reorder the debugger initialization logic so it can
+ // be removed.
Context::Scope context_scope(env->context());
- HandleScope handle_scope(env->isolate());
uv_run(env->event_loop(), UV_RUN_DEFAULT);
- EmitExit(env);
+ code = EmitExit(env);
RunAtExit(env);
env->Dispose();
env = NULL;
delete[] exec_argv;
exec_argv = NULL;
- return 0;
+ return code;
}