@@ -4458,26 +4458,22 @@ class HandleScopeHeapWrapper {
4458
4458
};
4459
4459
4460
4460
ArrayBufferAllocator* allocator;
4461
- Isolate::CreateParams params;
4462
4461
Locker* locker;
4463
4462
IsolateData* isolate_data;
4464
- Isolate::Scope* isolate_scope ;
4463
+ HandleScopeHeapWrapper* _handle_scope_wrapper = nullptr ;
4465
4464
Local<Context> context;
4466
- Context::Scope* context_scope;
4467
4465
bool request_stop = false ;
4468
4466
CmdArgs* cmd_args = nullptr ;
4469
4467
bool _event_loop_running = false ;
4470
- v8::Isolate* _isolate = nullptr ;
4471
4468
Environment* _environment = nullptr ;
4472
- HandleScopeHeapWrapper* _handle_scope_wrapper = nullptr ;
4473
4469
4474
4470
bool eventLoopIsRunning () {
4475
4471
return _event_loop_running;
4476
4472
}
4477
4473
4478
4474
namespace internal {
4479
4475
v8::Isolate* isolate () {
4480
- return _isolate ;
4476
+ return node_isolate ;
4481
4477
}
4482
4478
4483
4479
Environment* environment () {
@@ -4531,33 +4527,27 @@ void _StopEnv() {
4531
4527
delete _environment;
4532
4528
_environment = nullptr ;
4533
4529
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
+ }
4542
4532
4533
+ void _DeleteIsolate () {
4543
4534
delete isolate_data;
4544
4535
isolate_data = nullptr ;
4545
4536
4546
4537
delete _handle_scope_wrapper;
4547
4538
_handle_scope_wrapper = nullptr ;
4548
4539
4549
- delete isolate_scope;
4550
- isolate_scope = nullptr ;
4540
+ node_isolate->Exit ();
4551
4541
4552
4542
delete locker;
4553
4543
locker = nullptr ;
4554
- }
4555
4544
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
+
4561
4551
4562
4552
delete allocator;
4563
4553
allocator = nullptr ;
@@ -4598,55 +4588,54 @@ void _InitV8() {
4598
4588
}
4599
4589
4600
4590
int _CreateIsolate () {
4591
+ Isolate::CreateParams params;
4601
4592
allocator = new ArrayBufferAllocator ();
4602
4593
params.array_buffer_allocator = allocator;
4603
4594
#ifdef NODE_ENABLE_VTUNE_PROFILING
4604
4595
params.code_event_handler = vTune::GetVtuneCodeEventHandler ();
4605
4596
#endif
4606
4597
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 ) {
4611
4600
return 12 ; // Signal internal error.
4612
4601
}
4613
4602
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);
4618
4607
4619
4608
{
4620
4609
Mutex::ScopedLock scoped_lock (node_isolate_mutex);
4621
4610
CHECK_EQ (node_isolate, nullptr );
4622
- node_isolate = _isolate ;
4611
+ node_isolate = isolate ;
4623
4612
}
4624
4613
4625
4614
return 0 ;
4626
4615
}
4627
4616
4628
4617
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 );
4632
4621
4633
4622
isolate_data = new IsolateData (
4634
- _isolate ,
4623
+ node_isolate ,
4635
4624
uv_default_loop (),
4636
4625
v8_platform.Platform (),
4637
4626
allocator->zero_fill_field ());
4638
4627
4639
4628
if (track_heap_objects) {
4640
- _isolate ->GetHeapProfiler ()->StartTrackingHeapObjects (true );
4629
+ node_isolate ->GetHeapProfiler ()->StartTrackingHeapObjects (true );
4641
4630
}
4642
4631
4643
4632
// TODO(justus-hildebrand): in the initial Start functions,
4644
4633
// two handle scopes were created
4645
4634
// (one in Start() 2 and one in Start() 3). Currently, we have no idea why.
4646
4635
// 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);
4650
4639
}
4651
4640
4652
4641
void _ConfigureOpenSsl () {
@@ -4716,13 +4705,14 @@ int Initialize(int argc, const char** argv, const bool evaluate_stdin) {
4716
4705
4717
4706
// Hack around with the argv pointer. Used for process.title = "blah --args".
4718
4707
// 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)));
4720
4710
4721
4711
// This needs to run *before* V8::Initialize().
4722
4712
// Init() puts the v8 specific cmd args in exec_argc and exec_argv.
4723
4713
int exec_argc = 0 ;
4724
4714
const char ** exec_argv = nullptr ;
4725
- Init (&argc, argv , &exec_argc, &exec_argv);
4715
+ Init (&argc, custom_argv , &exec_argc, &exec_argv);
4726
4716
4727
4717
initialize::_ConfigureOpenSsl ();
4728
4718
initialize::_InitV8 ();
@@ -4734,15 +4724,11 @@ int Initialize(int argc, const char** argv, const bool evaluate_stdin) {
4734
4724
4735
4725
initialize::_CreateInitialEnvironment ();
4736
4726
exit_code = initialize::_StartEnv (argc,
4737
- argv ,
4727
+ custom_argv ,
4738
4728
exec_argc,
4739
4729
exec_argv,
4740
4730
evaluate_stdin);
4741
- if (exit_code != 0 ) {
4742
- return exit_code;
4743
- }
4744
-
4745
- return 0 ;
4731
+ return exit_code;
4746
4732
}
4747
4733
4748
4734
int Deinitialize () {
@@ -4763,19 +4749,28 @@ int Deinitialize() {
4763
4749
}
4764
4750
4765
4751
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) {
4766
4756
// TODO(cmfcmf) Read entire file into string.
4767
4757
// There is most certainly a better way
4768
4758
// https://stackoverflow.com/a/2602258/2560557
4769
4759
std::ifstream t (path);
4770
4760
std::stringstream buffer;
4771
4761
buffer << t.rdbuf ();
4772
4762
4773
- return Evaluate (buffer.str ());
4763
+ return Evaluate (_environment, buffer.str ());
4774
4764
}
4775
4765
4776
4766
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 ());
4779
4774
4780
4775
// try_catch must be nonverbose to disable FatalException() handler,
4781
4776
// we will handle exceptions ourself.
@@ -4785,12 +4780,12 @@ v8::MaybeLocal<v8::Value> Evaluate(const std::string& js_code) {
4785
4780
// This is used for debugging
4786
4781
// ScriptOrigin origin(filename);
4787
4782
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 ())
4790
4785
/* removed param: origin*/ );
4791
4786
4792
4787
if (script.IsEmpty ()) {
4793
- ReportException (_environment , try_catch);
4788
+ ReportException (env , try_catch);
4794
4789
return MaybeLocal<v8::Value>();
4795
4790
}
4796
4791
@@ -4819,32 +4814,40 @@ void RunEventLoop(const std::function<void()>& callback,
4819
4814
}
4820
4815
4821
4816
v8::MaybeLocal<v8::Object> GetRootObject () {
4822
- if (context.IsEmpty ()) {
4823
- return MaybeLocal<v8::Object>();
4824
- }
4825
- return context->Global ();
4817
+ return GetRootObject (_environment);
4826
4818
}
4827
4819
4820
+ v8::MaybeLocal<v8::Object> GetRootObject (Environment* env) {
4821
+ return env->context ()->Global ();
4822
+ }
4828
4823
4829
4824
v8::MaybeLocal<v8::Value> Call (v8::Local<v8::Object> receiver,
4830
4825
v8::Local<v8::Function> function,
4831
4826
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) {
4832
4834
return function->Call (receiver,
4833
4835
args.size (),
4834
4836
const_cast <v8::Local<v8::Value>*>(&args[0 ]));
4835
4837
}
4836
4838
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);
4841
4843
}
4842
4844
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,
4844
4847
const std::string& function_name,
4845
4848
const std::vector<v8::Local<v8::Value>>& args) {
4846
4849
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 ());
4848
4851
4849
4852
Local<v8::String> v8_function_name;
4850
4853
@@ -4864,18 +4867,17 @@ v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> object,
4864
4867
return MaybeLocal<v8::Value>();
4865
4868
}
4866
4869
4867
- return Call (object, v8::Local<v8::Function>::Cast (value), args);
4870
+ return Call (env, object, v8::Local<v8::Function>::Cast (value), args);
4868
4871
}
4869
4872
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);
4874
4875
}
4875
4876
4876
- v8::MaybeLocal<v8::Object> IncludeModule (const std::string& name) {
4877
+ v8::MaybeLocal<v8::Object> IncludeModule (Environment* env,
4878
+ const std::string& name) {
4877
4879
MaybeLocal<v8::String> maybe_arg =
4878
- v8::String::NewFromUtf8 (_isolate , name.c_str ());
4880
+ v8::String::NewFromUtf8 (env-> isolate () , name.c_str ());
4879
4881
4880
4882
Local<v8::String> arg;
4881
4883
@@ -4886,14 +4888,14 @@ v8::MaybeLocal<v8::Object> IncludeModule(const std::string& name) {
4886
4888
4887
4889
Local<v8::Object> root_object;
4888
4890
4889
- if (!GetRootObject ().ToLocal (&root_object)) {
4891
+ if (!GetRootObject (env ).ToLocal (&root_object)) {
4890
4892
// cannot get root object
4891
4893
return MaybeLocal<v8::Object>();
4892
4894
}
4893
4895
4894
4896
std::vector<Local<v8::Value>> args = { arg };
4895
4897
4896
- MaybeLocal<v8::Value> maybe_module = Call (root_object, " require" , args);
4898
+ MaybeLocal<v8::Value> maybe_module = Call (env, root_object, " require" , args);
4897
4899
Local<v8::Value> module;
4898
4900
4899
4901
if (!maybe_module.ToLocal (&module)) {
@@ -4906,8 +4908,14 @@ v8::MaybeLocal<v8::Object> IncludeModule(const std::string& name) {
4906
4908
4907
4909
v8::MaybeLocal<v8::Value> GetValue (v8::Local<v8::Object> object,
4908
4910
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) {
4909
4917
MaybeLocal<v8::String> maybe_key =
4910
- v8::String::NewFromUtf8 (_isolate , value_name.c_str ());
4918
+ v8::String::NewFromUtf8 (env-> isolate () , value_name.c_str ());
4911
4919
4912
4920
Local<v8::String> key;
4913
4921
@@ -4923,6 +4931,14 @@ void RegisterModule(const std::string& name,
4923
4931
const addon_context_register_func& callback,
4924
4932
void * priv,
4925
4933
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) {
4926
4942
node::node_module* module = new node::node_module ();
4927
4943
4928
4944
module->nm_version = NODE_MODULE_VERSION;
@@ -4935,18 +4951,30 @@ void RegisterModule(const std::string& name,
4935
4951
node_module_register (module);
4936
4952
4937
4953
if (target != " " ) {
4938
- Evaluate (" const " + target + " = process.binding('" + name + " ')" );
4954
+ Evaluate (env, " const " + target + " = process.binding('" + name + " ')" );
4939
4955
}
4940
4956
}
4941
4957
4942
4958
void RegisterModule (const std::string& name,
4943
4959
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,
4945
4972
const std::string& target) {
4946
4973
auto map_on_heap = new const std::map<std::string,
4947
4974
v8::FunctionCallback>(module_functions);
4948
4975
4949
- RegisterModule (name,
4976
+ RegisterModule (env,
4977
+ name,
4950
4978
node::_RegisterModuleCallback,
4951
4979
const_cast <std::map<std::string,
4952
4980
v8::FunctionCallback>*>(map_on_heap),
0 commit comments