Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Internals #41

Merged
merged 4 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 39 additions & 41 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5000,10 +5000,8 @@ struct CmdArgs {
ArrayBufferAllocator* allocator;
Isolate::CreateParams params;
Locker* locker;
Isolate* isolate;
IsolateData* isolate_data;
Isolate::Scope* isolate_scope;
Environment* env;
Local<Context> context;
Context::Scope* context_scope;
bool request_stop = false;
Expand All @@ -5020,23 +5018,23 @@ void deleteCmdArgs() {
}

int _StopEnv() {
env->set_trace_sync_io(false);
_environment->set_trace_sync_io(false);

int exit_code = EmitExit(env);
RunAtExit(env);
int exit_code = EmitExit(_environment);
RunAtExit(_environment);
uv_key_delete(&thread_local_env);

v8_platform.DrainVMTasks();
WaitForInspectorDisconnect(env);
WaitForInspectorDisconnect(_environment);

return exit_code;
}

void deleteIsolate() {
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
CHECK_EQ(node_isolate, isolate);
CHECK_EQ(node_isolate, _isolate);
node_isolate = nullptr;
isolate->Dispose();
_isolate->Dispose();
}

void deinitV8() {
Expand Down Expand Up @@ -5089,45 +5087,45 @@ void createIsolate() {
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
#endif

isolate = Isolate::New(params);
if (isolate == nullptr) {
_isolate = Isolate::New(params);
if (_isolate == nullptr) {
fprintf(stderr, "Could not create isolate.");
fflush(stderr);
return; // TODO: Handle error
//return 12; // Signal internal error.
}

isolate->AddMessageListener(OnMessage);
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
isolate->SetAutorunMicrotasks(false);
isolate->SetFatalErrorHandler(OnFatalError);
_isolate->AddMessageListener(OnMessage);
_isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
_isolate->SetAutorunMicrotasks(false);
_isolate->SetFatalErrorHandler(OnFatalError);

if (track_heap_objects) {
isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
_isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
}

{
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
CHECK_EQ(node_isolate, nullptr);
node_isolate = isolate;
node_isolate = _isolate;
}
}

void createInitialEnvironment() {
locker = new Locker(isolate);
isolate_scope = new Isolate::Scope(isolate);
static HandleScope handle_scope(isolate); // TODO (jh): Once we write a Deinit(), we need to put this on the heap to call the deconstructor.
isolate_data = new IsolateData(isolate, uv_default_loop(), allocator->zero_fill_field());
locker = new Locker(_isolate);
isolate_scope = new Isolate::Scope(_isolate);
static HandleScope handle_scope(_isolate); // TODO (jh): Once we write a Deinit(), we need to put this on the heap to call the deconstructor.
isolate_data = new IsolateData(_isolate, uv_default_loop(), allocator->zero_fill_field());

//////////
// Start 3
//////////
//HandleScope handle_scope(isolate); // (jh) in the initial Start functions, two handle scopes were created (one in Start() 2 and one in Start() 3). Currently, we have no idea why.
context = NewContext(isolate);
context = NewContext(_isolate);
context_scope = new Context::Scope(context);
env = new Environment(isolate_data, context);
_environment = new node::Environment(isolate_data, context);
CHECK_EQ(0, uv_key_create(&thread_local_env));
uv_key_set(&thread_local_env, env);
uv_key_set(&thread_local_env, _environment);
}

void configureOpenSsl() {
Expand All @@ -5154,30 +5152,30 @@ void _StartEnv(int argc,

int v8_argc = 0;
const char* const* v8_argv = nullptr;
env->Start(argc, argv, v8_argc, v8_argv, v8_is_profiling);
_environment->Start(argc, argv, v8_argc, v8_argv, v8_is_profiling);

const char* path = argc > 1 ? argv[1] : nullptr;
StartInspector(env, path, debug_options);
StartInspector(_environment, path, debug_options);

if (debug_options.inspector_enabled() && !v8_platform.InspectorStarted(env)) {
if (debug_options.inspector_enabled() && !v8_platform.InspectorStarted(_environment)) {
return; // TODO (jh): Handle error
//return 12; // Signal internal error.
}

env->set_abort_on_uncaught_exception(abort_on_uncaught_exception);
_environment->set_abort_on_uncaught_exception(abort_on_uncaught_exception);

if (no_force_async_hooks_checks) {
env->async_hooks()->no_force_checks();
_environment->async_hooks()->no_force_checks();
}

{
Environment::AsyncCallbackScope callback_scope(env);
env->async_hooks()->push_async_ids(1, 0);
LoadEnvironment(env);
env->async_hooks()->pop_async_id(1);
Environment::AsyncCallbackScope callback_scope(_environment);
_environment->async_hooks()->push_async_ids(1, 0);
LoadEnvironment(_environment);
_environment->async_hooks()->pop_async_id(1);
}

env->set_trace_sync_io(trace_sync_io);
_environment->set_trace_sync_io(trace_sync_io);
}

} // namespace initialize
Expand Down Expand Up @@ -5255,23 +5253,23 @@ v8::Local<v8::Value> Run(const std::string& path) {
}

v8::Local<v8::Value> Evaluate(const std::string& java_script_code) {
EscapableHandleScope scope(env->isolate());
TryCatch try_catch(env->isolate());
EscapableHandleScope scope(_environment->isolate());
TryCatch try_catch(_environment->isolate());

// try_catch must be nonverbose to disable FatalException() handler,
// we will handle exceptions ourself.
try_catch.SetVerbose(false);

//ScriptOrigin origin(filename); // TODO jh: set reasonable ScriptOrigin. This is used for debugging
MaybeLocal<v8::Script> script = v8::Script::Compile(env->context(), v8::String::NewFromUtf8(isolate, java_script_code.c_str())/*, origin*/);
MaybeLocal<v8::Script> script = v8::Script::Compile(_environment->context(), v8::String::NewFromUtf8(_isolate, java_script_code.c_str())/*, origin*/);
if (script.IsEmpty()) {
ReportException(env, try_catch);
ReportException(_environment, try_catch);
exit(3); //TODO jh: don't exit process when function breaks. Handle error differently.
}

Local<Value> result = script.ToLocalChecked()->Run();
if (result.IsEmpty()) {
ReportException(env, try_catch);
ReportException(_environment, try_catch);
exit(4); //TODO jh: don't exit process when function breaks. Handle error differently.
}

Expand Down Expand Up @@ -5309,7 +5307,7 @@ v8::Local<v8::Value> Call(v8::Local<v8::Object> receiver, v8::Local<v8::Function

// TODO: Error handling: Node.js has exceptions disabled.
v8::Local<v8::Value> Call(v8::Local<v8::Object> object, const std::string& function_name, const std::vector<v8::Local<v8::Value>>& args) {
Local<v8::String> v8_function_name = v8::String::NewFromUtf8(isolate, function_name.c_str());
Local<v8::String> v8_function_name = v8::String::NewFromUtf8(_isolate, function_name.c_str());

Local<v8::Value> value = object->Get(v8_function_name);
if (!value->IsFunction()) {
Expand All @@ -5326,7 +5324,7 @@ v8::Local<v8::Value> Call(v8::Local<v8::Object> object, const std::string & func

// TODO: Node.js has exceptions disabled.
v8::Local<v8::Object> IncludeModule(const std::string& module_name) {
std::vector<v8::Local<v8::Value>> args = {v8::String::NewFromUtf8(isolate, module_name.c_str())};
std::vector<v8::Local<v8::Value>> args = {v8::String::NewFromUtf8(_isolate, module_name.c_str())};

auto module = Call(GetRootObject(), "require", args);
if (module->IsUndefined()) {
Expand Down Expand Up @@ -5381,7 +5379,7 @@ void StopEventLoop() {
}

bool ProcessEvents() {
return TickEventLoop(*env);
return TickEventLoop(*_environment);
}

} // namespace node::lib
Expand Down
8 changes: 7 additions & 1 deletion src/node_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ namespace node { namespace lib {

namespace { // private variables
bool _event_loop_running = false;
v8::Isolate* _isolate = nullptr;
Environment* _environment = nullptr;
}

bool EventLoopIsRunning() { return _event_loop_running; }
const bool EventLoopIsRunning() { return _event_loop_running; }

const v8::Isolate* Isolate() { return _isolate; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, is there a reason you did not name this getIsolate()?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const Environment* Environment() { return _environment; }

/*********************************************************
* Function types
Expand Down