Skip to content

Commit cfae579

Browse files
authored
Merge pull request #87 from hpicgs/dev
Merge dev into PR branch
2 parents e916467 + 9d6e944 commit cfae579

File tree

2 files changed

+248
-97
lines changed

2 files changed

+248
-97
lines changed

src/node.cc

+104-76
Original file line numberDiff line numberDiff line change
@@ -4458,26 +4458,22 @@ class HandleScopeHeapWrapper {
44584458
};
44594459

44604460
ArrayBufferAllocator* allocator;
4461-
Isolate::CreateParams params;
44624461
Locker* locker;
44634462
IsolateData* isolate_data;
4464-
Isolate::Scope* isolate_scope;
4463+
HandleScopeHeapWrapper* _handle_scope_wrapper = nullptr;
44654464
Local<Context> context;
4466-
Context::Scope* context_scope;
44674465
bool request_stop = false;
44684466
CmdArgs* cmd_args = nullptr;
44694467
bool _event_loop_running = false;
4470-
v8::Isolate* _isolate = nullptr;
44714468
Environment* _environment = nullptr;
4472-
HandleScopeHeapWrapper* _handle_scope_wrapper = nullptr;
44734469

44744470
bool eventLoopIsRunning() {
44754471
return _event_loop_running;
44764472
}
44774473

44784474
namespace internal {
44794475
v8::Isolate* isolate() {
4480-
return _isolate;
4476+
return node_isolate;
44814477
}
44824478

44834479
Environment* environment() {
@@ -4531,33 +4527,27 @@ void _StopEnv() {
45314527
delete _environment;
45324528
_environment = nullptr;
45334529

4534-
delete context_scope;
4535-
context_scope = nullptr;
4536-
4537-
if (!context.IsEmpty()) {
4538-
// No need to delete the context value (delete *context),
4539-
// this is already done by deleting the context_scope above.
4540-
context.Clear();
4541-
}
4530+
context->Exit();
4531+
}
45424532

4533+
void _DeleteIsolate() {
45434534
delete isolate_data;
45444535
isolate_data = nullptr;
45454536

45464537
delete _handle_scope_wrapper;
45474538
_handle_scope_wrapper = nullptr;
45484539

4549-
delete isolate_scope;
4550-
isolate_scope = nullptr;
4540+
node_isolate->Exit();
45514541

45524542
delete locker;
45534543
locker = nullptr;
4554-
}
45554544

4556-
void _DeleteIsolate() {
4557-
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
4558-
CHECK_EQ(node_isolate, _isolate);
4559-
node_isolate = nullptr;
4560-
_isolate->Dispose();
4545+
{
4546+
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
4547+
node_isolate->Dispose();
4548+
node_isolate = nullptr;
4549+
}
4550+
45614551

45624552
delete allocator;
45634553
allocator = nullptr;
@@ -4598,55 +4588,54 @@ void _InitV8() {
45984588
}
45994589

46004590
int _CreateIsolate() {
4591+
Isolate::CreateParams params;
46014592
allocator = new ArrayBufferAllocator();
46024593
params.array_buffer_allocator = allocator;
46034594
#ifdef NODE_ENABLE_VTUNE_PROFILING
46044595
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
46054596
#endif
46064597

4607-
_isolate = Isolate::New(params);
4608-
if (_isolate == nullptr) {
4609-
fprintf(stderr, "Could not create isolate.");
4610-
fflush(stderr);
4598+
Isolate* const isolate = Isolate::New(params);
4599+
if (isolate == nullptr) {
46114600
return 12; // Signal internal error.
46124601
}
46134602

4614-
_isolate->AddMessageListener(OnMessage);
4615-
_isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
4616-
_isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
4617-
_isolate->SetFatalErrorHandler(OnFatalError);
4603+
isolate->AddMessageListener(OnMessage);
4604+
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
4605+
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
4606+
isolate->SetFatalErrorHandler(OnFatalError);
46184607

46194608
{
46204609
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
46214610
CHECK_EQ(node_isolate, nullptr);
4622-
node_isolate = _isolate;
4611+
node_isolate = isolate;
46234612
}
46244613

46254614
return 0;
46264615
}
46274616

46284617
void _CreateInitialEnvironment() {
4629-
locker = new Locker(_isolate);
4630-
isolate_scope = new Isolate::Scope(_isolate);
4631-
_handle_scope_wrapper = new HandleScopeHeapWrapper(_isolate);
4618+
locker = new Locker(node_isolate);
4619+
node_isolate->Enter();
4620+
_handle_scope_wrapper = new HandleScopeHeapWrapper(node_isolate);
46324621

46334622
isolate_data = new IsolateData(
4634-
_isolate,
4623+
node_isolate,
46354624
uv_default_loop(),
46364625
v8_platform.Platform(),
46374626
allocator->zero_fill_field());
46384627

46394628
if (track_heap_objects) {
4640-
_isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
4629+
node_isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
46414630
}
46424631

46434632
// TODO(justus-hildebrand): in the initial Start functions,
46444633
// two handle scopes were created
46454634
// (one in Start() 2 and one in Start() 3). Currently, we have no idea why.
46464635
// HandleScope handle_scope(isolate);
4647-
context = NewContext(_isolate);
4648-
context_scope = new Context::Scope(context);
4649-
_environment = new node::Environment(isolate_data, context);
4636+
context = NewContext(node_isolate);
4637+
context->Enter();
4638+
_environment = new Environment(isolate_data, context);
46504639
}
46514640

46524641
void _ConfigureOpenSsl() {
@@ -4716,13 +4705,14 @@ int Initialize(int argc, const char** argv, const bool evaluate_stdin) {
47164705

47174706
// Hack around with the argv pointer. Used for process.title = "blah --args".
47184707
// argv won't be modified
4719-
uv_setup_args(argc, const_cast<char**>(argv));
4708+
const char** custom_argv = const_cast<const char**>(
4709+
uv_setup_args(argc, const_cast<char**>(argv)));
47204710

47214711
// This needs to run *before* V8::Initialize().
47224712
// Init() puts the v8 specific cmd args in exec_argc and exec_argv.
47234713
int exec_argc = 0;
47244714
const char** exec_argv = nullptr;
4725-
Init(&argc, argv, &exec_argc, &exec_argv);
4715+
Init(&argc, custom_argv, &exec_argc, &exec_argv);
47264716

47274717
initialize::_ConfigureOpenSsl();
47284718
initialize::_InitV8();
@@ -4734,15 +4724,11 @@ int Initialize(int argc, const char** argv, const bool evaluate_stdin) {
47344724

47354725
initialize::_CreateInitialEnvironment();
47364726
exit_code = initialize::_StartEnv(argc,
4737-
argv,
4727+
custom_argv,
47384728
exec_argc,
47394729
exec_argv,
47404730
evaluate_stdin);
4741-
if (exit_code != 0) {
4742-
return exit_code;
4743-
}
4744-
4745-
return 0;
4731+
return exit_code;
47464732
}
47474733

47484734
int Deinitialize() {
@@ -4763,19 +4749,28 @@ int Deinitialize() {
47634749
}
47644750

47654751
v8::MaybeLocal<v8::Value> Run(const std::string& path) {
4752+
return Run(_environment, path);
4753+
}
4754+
4755+
v8::MaybeLocal<v8::Value> Run(Environment* env, const std::string& path) {
47664756
// TODO(cmfcmf) Read entire file into string.
47674757
// There is most certainly a better way
47684758
// https://stackoverflow.com/a/2602258/2560557
47694759
std::ifstream t(path);
47704760
std::stringstream buffer;
47714761
buffer << t.rdbuf();
47724762

4773-
return Evaluate(buffer.str());
4763+
return Evaluate(_environment, buffer.str());
47744764
}
47754765

47764766
v8::MaybeLocal<v8::Value> Evaluate(const std::string& js_code) {
4777-
EscapableHandleScope scope(_environment->isolate());
4778-
TryCatch try_catch(_environment->isolate());
4767+
return Evaluate(_environment, js_code);
4768+
}
4769+
4770+
v8::MaybeLocal<v8::Value> Evaluate(Environment* env,
4771+
const std::string& js_code) {
4772+
EscapableHandleScope scope(env->isolate());
4773+
TryCatch try_catch(env->isolate());
47794774

47804775
// try_catch must be nonverbose to disable FatalException() handler,
47814776
// we will handle exceptions ourself.
@@ -4785,12 +4780,12 @@ v8::MaybeLocal<v8::Value> Evaluate(const std::string& js_code) {
47854780
// This is used for debugging
47864781
// ScriptOrigin origin(filename);
47874782
MaybeLocal<v8::Script> script = v8::Script::Compile(
4788-
_environment->context(),
4789-
v8::String::NewFromUtf8(_isolate, js_code.c_str())
4783+
env->context(),
4784+
v8::String::NewFromUtf8(env->isolate(), js_code.c_str())
47904785
/*removed param: origin*/);
47914786

47924787
if (script.IsEmpty()) {
4793-
ReportException(_environment, try_catch);
4788+
ReportException(env, try_catch);
47944789
return MaybeLocal<v8::Value>();
47954790
}
47964791

@@ -4819,32 +4814,40 @@ void RunEventLoop(const std::function<void()>& callback,
48194814
}
48204815

48214816
v8::MaybeLocal<v8::Object> GetRootObject() {
4822-
if (context.IsEmpty()) {
4823-
return MaybeLocal<v8::Object>();
4824-
}
4825-
return context->Global();
4817+
return GetRootObject(_environment);
48264818
}
48274819

4820+
v8::MaybeLocal<v8::Object> GetRootObject(Environment* env) {
4821+
return env->context()->Global();
4822+
}
48284823

48294824
v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> receiver,
48304825
v8::Local<v8::Function> function,
48314826
const std::vector<v8::Local<v8::Value>>& args) {
4827+
return Call(_environment, receiver, function, args);
4828+
}
4829+
4830+
v8::MaybeLocal<v8::Value> Call(Environment* env,
4831+
v8::Local<v8::Object> receiver,
4832+
v8::Local<v8::Function> function,
4833+
const std::vector<v8::Local<v8::Value>>& args) {
48324834
return function->Call(receiver,
48334835
args.size(),
48344836
const_cast<v8::Local<v8::Value>*>(&args[0]));
48354837
}
48364838

4837-
v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> receiver,
4838-
v8::Local<v8::Function> function,
4839-
std::initializer_list<v8::Local<Value>> args) {
4840-
return Call(receiver, function, std::vector<v8::Local<v8::Value>>(args));
4839+
v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> object,
4840+
const std::string& function_name,
4841+
const std::vector<v8::Local<v8::Value>>& args) {
4842+
return Call(_environment, object, function_name, args);
48414843
}
48424844

4843-
v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> object,
4845+
v8::MaybeLocal<v8::Value> Call(Environment* env,
4846+
v8::Local<v8::Object> object,
48444847
const std::string& function_name,
48454848
const std::vector<v8::Local<v8::Value>>& args) {
48464849
MaybeLocal<v8::String> maybe_function_name =
4847-
v8::String::NewFromUtf8(_isolate, function_name.c_str());
4850+
v8::String::NewFromUtf8(env->isolate(), function_name.c_str());
48484851

48494852
Local<v8::String> v8_function_name;
48504853

@@ -4864,18 +4867,17 @@ v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> object,
48644867
return MaybeLocal<v8::Value>();
48654868
}
48664869

4867-
return Call(object, v8::Local<v8::Function>::Cast(value), args);
4870+
return Call(env, object, v8::Local<v8::Function>::Cast(value), args);
48684871
}
48694872

4870-
v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> object,
4871-
const std::string& function_name,
4872-
std::initializer_list<v8::Local<Value>> args) {
4873-
return Call(object, function_name, std::vector<v8::Local<v8::Value>>(args));
4873+
v8::MaybeLocal<v8::Object> IncludeModule(const std::string& name) {
4874+
return IncludeModule(_environment, name);
48744875
}
48754876

4876-
v8::MaybeLocal<v8::Object> IncludeModule(const std::string& name) {
4877+
v8::MaybeLocal<v8::Object> IncludeModule(Environment* env,
4878+
const std::string& name) {
48774879
MaybeLocal<v8::String> maybe_arg =
4878-
v8::String::NewFromUtf8(_isolate, name.c_str());
4880+
v8::String::NewFromUtf8(env->isolate(), name.c_str());
48794881

48804882
Local<v8::String> arg;
48814883

@@ -4886,14 +4888,14 @@ v8::MaybeLocal<v8::Object> IncludeModule(const std::string& name) {
48864888

48874889
Local<v8::Object> root_object;
48884890

4889-
if (!GetRootObject().ToLocal(&root_object)) {
4891+
if (!GetRootObject(env).ToLocal(&root_object)) {
48904892
// cannot get root object
48914893
return MaybeLocal<v8::Object>();
48924894
}
48934895

48944896
std::vector<Local<v8::Value>> args = { arg };
48954897

4896-
MaybeLocal<v8::Value> maybe_module = Call(root_object, "require", args);
4898+
MaybeLocal<v8::Value> maybe_module = Call(env, root_object, "require", args);
48974899
Local<v8::Value> module;
48984900

48994901
if (!maybe_module.ToLocal(&module)) {
@@ -4906,8 +4908,14 @@ v8::MaybeLocal<v8::Object> IncludeModule(const std::string& name) {
49064908

49074909
v8::MaybeLocal<v8::Value> GetValue(v8::Local<v8::Object> object,
49084910
const std::string& value_name) {
4911+
return GetValue(_environment, object, value_name);
4912+
}
4913+
4914+
v8::MaybeLocal<v8::Value> GetValue(Environment* env,
4915+
v8::Local<v8::Object> object,
4916+
const std::string& value_name) {
49094917
MaybeLocal<v8::String> maybe_key =
4910-
v8::String::NewFromUtf8(_isolate, value_name.c_str());
4918+
v8::String::NewFromUtf8(env->isolate(), value_name.c_str());
49114919

49124920
Local<v8::String> key;
49134921

@@ -4923,6 +4931,14 @@ void RegisterModule(const std::string& name,
49234931
const addon_context_register_func& callback,
49244932
void* priv,
49254933
const std::string& target) {
4934+
RegisterModule(_environment, name, callback, priv, target);
4935+
}
4936+
4937+
void RegisterModule(Environment* env,
4938+
const std::string& name,
4939+
const addon_context_register_func& callback,
4940+
void* priv,
4941+
const std::string& target) {
49264942
node::node_module* module = new node::node_module();
49274943

49284944
module->nm_version = NODE_MODULE_VERSION;
@@ -4935,18 +4951,30 @@ void RegisterModule(const std::string& name,
49354951
node_module_register(module);
49364952

49374953
if (target != "") {
4938-
Evaluate("const " + target + " = process.binding('" + name + "')");
4954+
Evaluate(env, "const " + target + " = process.binding('" + name + "')");
49394955
}
49404956
}
49414957

49424958
void RegisterModule(const std::string& name,
49434959
const std::map<std::string,
4944-
v8::FunctionCallback>& module_functions,
4960+
v8::FunctionCallback>& module_functions,
4961+
const std::string& target) {
4962+
RegisterModule(_environment,
4963+
name,
4964+
module_functions,
4965+
target);
4966+
}
4967+
4968+
void RegisterModule(Environment* env,
4969+
const std::string& name,
4970+
const std::map<std::string,
4971+
v8::FunctionCallback>& module_functions,
49454972
const std::string& target) {
49464973
auto map_on_heap = new const std::map<std::string,
49474974
v8::FunctionCallback>(module_functions);
49484975

4949-
RegisterModule(name,
4976+
RegisterModule(env,
4977+
name,
49504978
node::_RegisterModuleCallback,
49514979
const_cast<std::map<std::string,
49524980
v8::FunctionCallback>*>(map_on_heap),

0 commit comments

Comments
 (0)