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