@@ -36,8 +36,7 @@ NodeMainInstance::NodeMainInstance(Isolate* isolate,
36
36
isolate_(isolate),
37
37
platform_(platform),
38
38
isolate_data_(nullptr ),
39
- owns_isolate_(false ),
40
- deserialize_mode_(false ) {
39
+ snapshot_data_(nullptr ) {
41
40
isolate_data_ =
42
41
std::make_unique<IsolateData>(isolate_, event_loop, platform, nullptr );
43
42
@@ -61,77 +60,81 @@ std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
61
60
new NodeMainInstance (isolate, event_loop, platform, args, exec_args));
62
61
}
63
62
64
- NodeMainInstance::NodeMainInstance (
65
- Isolate::CreateParams* params,
66
- uv_loop_t * event_loop,
67
- MultiIsolatePlatform* platform,
68
- const std::vector<std::string>& args,
69
- const std::vector<std::string>& exec_args,
70
- const std::vector<size_t >* per_isolate_data_indexes)
63
+ NodeMainInstance::NodeMainInstance (const SnapshotData* snapshot_data,
64
+ uv_loop_t * event_loop,
65
+ MultiIsolatePlatform* platform,
66
+ const std::vector<std::string>& args,
67
+ const std::vector<std::string>& exec_args)
71
68
: args_(args),
72
69
exec_args_(exec_args),
73
70
array_buffer_allocator_(ArrayBufferAllocator::Create()),
74
71
isolate_(nullptr ),
75
72
platform_(platform),
76
- isolate_data_(nullptr ),
77
- owns_isolate_( true ) {
78
- params-> array_buffer_allocator = array_buffer_allocator_. get ();
79
- deserialize_mode_ = per_isolate_data_indexes != nullptr ;
80
- if (deserialize_mode_ ) {
73
+ isolate_data_(),
74
+ isolate_params_(std::make_unique<Isolate::CreateParams>()),
75
+ snapshot_data_(snapshot_data) {
76
+ isolate_params_-> array_buffer_allocator = array_buffer_allocator_. get () ;
77
+ if (snapshot_data != nullptr ) {
81
78
// TODO(joyeecheung): collect external references and set it in
82
79
// params.external_references.
83
80
const std::vector<intptr_t >& external_references =
84
81
CollectExternalReferences ();
85
- params->external_references = external_references.data ();
82
+ isolate_params_->external_references = external_references.data ();
83
+ isolate_params_->snapshot_blob =
84
+ const_cast <v8::StartupData*>(&(snapshot_data->blob ));
86
85
}
87
86
88
87
isolate_ = Isolate::Allocate ();
89
88
CHECK_NOT_NULL (isolate_);
90
89
// Register the isolate on the platform before the isolate gets initialized,
91
90
// so that the isolate can access the platform during initialization.
92
91
platform->RegisterIsolate (isolate_, event_loop);
93
- SetIsolateCreateParamsForNode (params );
94
- Isolate::Initialize (isolate_, *params );
92
+ SetIsolateCreateParamsForNode (isolate_params_. get () );
93
+ Isolate::Initialize (isolate_, *isolate_params_ );
95
94
96
95
// If the indexes are not nullptr, we are not deserializing
97
- CHECK_IMPLIES (deserialize_mode_, params->external_references != nullptr );
98
- isolate_data_ = std::make_unique<IsolateData>(isolate_,
99
- event_loop,
100
- platform,
101
- array_buffer_allocator_.get (),
102
- per_isolate_data_indexes);
96
+ isolate_data_ = std::make_unique<IsolateData>(
97
+ isolate_,
98
+ event_loop,
99
+ platform,
100
+ array_buffer_allocator_.get (),
101
+ snapshot_data == nullptr ? nullptr
102
+ : &(snapshot_data->isolate_data_indices ));
103
103
IsolateSettings s;
104
104
SetIsolateMiscHandlers (isolate_, s);
105
- if (!deserialize_mode_ ) {
105
+ if (snapshot_data == nullptr ) {
106
106
// If in deserialize mode, delay until after the deserialization is
107
107
// complete.
108
108
SetIsolateErrorHandlers (isolate_, s);
109
109
}
110
110
isolate_data_->max_young_gen_size =
111
- params ->constraints .max_young_generation_size_in_bytes ();
111
+ isolate_params_ ->constraints .max_young_generation_size_in_bytes ();
112
112
}
113
113
114
114
void NodeMainInstance::Dispose () {
115
- CHECK (!owns_isolate_);
115
+ // This should only be called on a main instance that does not own its
116
+ // isolate.
117
+ CHECK_NULL (isolate_params_);
116
118
platform_->DrainTasks (isolate_);
117
119
}
118
120
119
121
NodeMainInstance::~NodeMainInstance () {
120
- if (!owns_isolate_ ) {
122
+ if (isolate_params_ == nullptr ) {
121
123
return ;
122
124
}
125
+ // This should only be done on a main instance that owns its isolate.
123
126
platform_->UnregisterIsolate (isolate_);
124
127
isolate_->Dispose ();
125
128
}
126
129
127
- int NodeMainInstance::Run (const EnvSerializeInfo* env_info ) {
130
+ int NodeMainInstance::Run () {
128
131
Locker locker (isolate_);
129
132
Isolate::Scope isolate_scope (isolate_);
130
133
HandleScope handle_scope (isolate_);
131
134
132
135
int exit_code = 0 ;
133
136
DeleteFnPtr<Environment, FreeEnvironment> env =
134
- CreateMainEnvironment (&exit_code, env_info );
137
+ CreateMainEnvironment (&exit_code);
135
138
CHECK_NOT_NULL (env);
136
139
137
140
Context::Scope context_scope (env->context ());
@@ -167,8 +170,7 @@ void NodeMainInstance::Run(int* exit_code, Environment* env) {
167
170
}
168
171
169
172
DeleteFnPtr<Environment, FreeEnvironment>
170
- NodeMainInstance::CreateMainEnvironment (int * exit_code,
171
- const EnvSerializeInfo* env_info) {
173
+ NodeMainInstance::CreateMainEnvironment (int * exit_code) {
172
174
*exit_code = 0 ; // Reset the exit code to 0
173
175
174
176
HandleScope handle_scope (isolate_);
@@ -179,16 +181,15 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code,
179
181
isolate_->GetHeapProfiler ()->StartTrackingHeapObjects (true );
180
182
}
181
183
182
- CHECK_IMPLIES (deserialize_mode_, env_info != nullptr );
183
184
Local<Context> context;
184
185
DeleteFnPtr<Environment, FreeEnvironment> env;
185
186
186
- if (deserialize_mode_ ) {
187
+ if (snapshot_data_ != nullptr ) {
187
188
env.reset (new Environment (isolate_data_.get (),
188
189
isolate_,
189
190
args_,
190
191
exec_args_,
191
- env_info,
192
+ &(snapshot_data_-> env_info ) ,
192
193
EnvironmentFlags::kDefaultFlags ,
193
194
{}));
194
195
context = Context::FromSnapshot (isolate_,
@@ -200,7 +201,7 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code,
200
201
Context::Scope context_scope (context);
201
202
CHECK (InitializeContextRuntime (context).IsJust ());
202
203
SetIsolateErrorHandlers (isolate_, {});
203
- env->InitializeMainContext (context, env_info);
204
+ env->InitializeMainContext (context, &(snapshot_data_-> env_info ) );
204
205
#if HAVE_INSPECTOR
205
206
env->InitializeInspector ({});
206
207
#endif
0 commit comments