@@ -238,9 +238,43 @@ AsyncHooks::DefaultTriggerAsyncIdScope::DefaultTriggerAsyncIdScope(
238238 : DefaultTriggerAsyncIdScope(async_wrap->env (),
239239 async_wrap->get_async_id()) {}
240240
241- std::vector<size_t > IsolateData::Serialize (SnapshotCreator* creator) {
241+ std::ostream& operator <<(std::ostream& output,
242+ const std::vector<SnapshotIndex>& v) {
243+ output << " { " ;
244+ for (const SnapshotIndex i : v) {
245+ output << i << " , " ;
246+ }
247+ output << " }" ;
248+ return output;
249+ }
250+
251+ std::ostream& operator <<(std::ostream& output,
252+ const std::vector<PropInfo>& vec) {
253+ output << " {\n " ;
254+ for (const auto & info : vec) {
255+ output << " { \" " << info.name << " \" , " << std::to_string (info.id ) << " , "
256+ << std::to_string (info.index ) << " },\n " ;
257+ }
258+ output << " }" ;
259+ return output;
260+ }
261+
262+ std::ostream& operator <<(std::ostream& output,
263+ const IsolateDataSerializeInfo& i) {
264+ output << " {\n "
265+ << " // -- primitive begins --\n "
266+ << i.primitive_values << " ,\n "
267+ << " // -- primitive ends --\n "
268+ << " // -- template_values begins --\n "
269+ << i.template_values << " ,\n "
270+ << " // -- template_values ends --\n "
271+ << " }" ;
272+ return output;
273+ }
274+
275+ IsolateDataSerializeInfo IsolateData::Serialize (SnapshotCreator* creator) {
242276 Isolate* isolate = creator->GetIsolate ();
243- std::vector< size_t > indexes ;
277+ IsolateDataSerializeInfo info ;
244278 HandleScope handle_scope (isolate);
245279 // XXX(joyeecheung): technically speaking, the indexes here should be
246280 // consecutive and we could just return a range instead of an array,
@@ -251,21 +285,36 @@ std::vector<size_t> IsolateData::Serialize(SnapshotCreator* creator) {
251285#define VY (PropertyName, StringValue ) V(Symbol, PropertyName)
252286#define VS (PropertyName, StringValue ) V(String, PropertyName)
253287#define V (TypeName, PropertyName ) \
254- indexes.push_back (creator->AddData (PropertyName##_.Get (isolate)));
288+ info.primitive_values .push_back ( \
289+ creator->AddData (PropertyName##_.Get (isolate)));
255290 PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES (VP)
256291 PER_ISOLATE_SYMBOL_PROPERTIES (VY)
257292 PER_ISOLATE_STRING_PROPERTIES (VS)
258293#undef V
259294#undef VY
260295#undef VS
261296#undef VP
297+
262298 for (size_t i = 0 ; i < AsyncWrap::PROVIDERS_LENGTH; i++)
263- indexes.push_back (creator->AddData (async_wrap_provider (i)));
299+ info.primitive_values .push_back (creator->AddData (async_wrap_provider (i)));
300+
301+ size_t id = 0 ;
302+ #define V (PropertyName, TypeName ) \
303+ do { \
304+ Local<TypeName> field = PropertyName (); \
305+ if (!field.IsEmpty ()) { \
306+ size_t index = creator->AddData (field); \
307+ info.template_values .push_back ({#PropertyName, id, index}); \
308+ } \
309+ id++; \
310+ } while (0 );
311+ PER_ISOLATE_TEMPLATE_PROPERTIES (V)
312+ #undef V
264313
265- return indexes ;
314+ return info ;
266315}
267316
268- void IsolateData::DeserializeProperties (const std::vector< size_t >* indexes ) {
317+ void IsolateData::DeserializeProperties (const IsolateDataSerializeInfo* info ) {
269318 size_t i = 0 ;
270319 HandleScope handle_scope (isolate_);
271320
@@ -275,7 +324,8 @@ void IsolateData::DeserializeProperties(const std::vector<size_t>* indexes) {
275324#define V (TypeName, PropertyName ) \
276325 do { \
277326 MaybeLocal<TypeName> maybe_field = \
278- isolate_->GetDataFromSnapshotOnce <TypeName>((*indexes)[i++]); \
327+ isolate_->GetDataFromSnapshotOnce <TypeName>( \
328+ info->primitive_values [i++]); \
279329 Local<TypeName> field; \
280330 if (!maybe_field.ToLocal (&field)) { \
281331 fprintf (stderr, " Failed to deserialize " #PropertyName " \n " ); \
@@ -292,13 +342,38 @@ void IsolateData::DeserializeProperties(const std::vector<size_t>* indexes) {
292342
293343 for (size_t j = 0 ; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
294344 MaybeLocal<String> maybe_field =
295- isolate_->GetDataFromSnapshotOnce <String>((*indexes) [i++]);
345+ isolate_->GetDataFromSnapshotOnce <String>(info-> primitive_values [i++]);
296346 Local<String> field;
297347 if (!maybe_field.ToLocal (&field)) {
298348 fprintf (stderr, " Failed to deserialize AsyncWrap provider %zu\n " , j);
299349 }
300350 async_wrap_providers_[j].Set (isolate_, field);
301351 }
352+
353+ const std::vector<PropInfo>& values = info->template_values ;
354+ i = 0 ; // index to the array
355+ size_t id = 0 ;
356+ #define V (PropertyName, TypeName ) \
357+ do { \
358+ if (values.size () > i && id == values[i].id ) { \
359+ const PropInfo& d = values[i]; \
360+ DCHECK_EQ (d.name , #PropertyName); \
361+ MaybeLocal<TypeName> maybe_field = \
362+ isolate_->GetDataFromSnapshotOnce <TypeName>(d.index ); \
363+ Local<TypeName> field; \
364+ if (!maybe_field.ToLocal (&field)) { \
365+ fprintf (stderr, \
366+ " Failed to deserialize isolate data template " #PropertyName \
367+ " \n " ); \
368+ } \
369+ set_##PropertyName (field); \
370+ i++; \
371+ } \
372+ id++; \
373+ } while (0 );
374+
375+ PER_ISOLATE_TEMPLATE_PROPERTIES (V);
376+ #undef V
302377}
303378
304379void IsolateData::CreateProperties () {
@@ -363,13 +438,15 @@ void IsolateData::CreateProperties() {
363438 sizeof (#Provider) - 1 ).ToLocalChecked ());
364439 NODE_ASYNC_PROVIDER_TYPES (V)
365440#undef V
441+
442+ // TODO(legendecas): eagerly create per isolate templates.
366443}
367444
368445IsolateData::IsolateData (Isolate* isolate,
369446 uv_loop_t * event_loop,
370447 MultiIsolatePlatform* platform,
371448 ArrayBufferAllocator* node_allocator,
372- const std::vector< size_t >* indexes )
449+ const IsolateDataSerializeInfo* isolate_data_info )
373450 : isolate_(isolate),
374451 event_loop_ (event_loop),
375452 node_allocator_(node_allocator == nullptr ? nullptr
@@ -378,10 +455,10 @@ IsolateData::IsolateData(Isolate* isolate,
378455 options_.reset (
379456 new PerIsolateOptions (*(per_process::cli_options->per_isolate )));
380457
381- if (indexes == nullptr ) {
458+ if (isolate_data_info == nullptr ) {
382459 CreateProperties ();
383460 } else {
384- DeserializeProperties (indexes );
461+ DeserializeProperties (isolate_data_info );
385462 }
386463}
387464
@@ -1528,16 +1605,6 @@ void AsyncHooks::Deserialize(Local<Context> context) {
15281605 info_ = nullptr ;
15291606}
15301607
1531- std::ostream& operator <<(std::ostream& output,
1532- const std::vector<SnapshotIndex>& v) {
1533- output << " { " ;
1534- for (const SnapshotIndex i : v) {
1535- output << i << " , " ;
1536- }
1537- output << " }" ;
1538- return output;
1539- }
1540-
15411608std::ostream& operator <<(std::ostream& output,
15421609 const AsyncHooks::SerializeInfo& i) {
15431610 output << " {\n "
@@ -1749,19 +1816,6 @@ EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) {
17491816 should_abort_on_uncaught_toggle_.Serialize (ctx, creator);
17501817
17511818 size_t id = 0 ;
1752- #define V (PropertyName, TypeName ) \
1753- do { \
1754- Local<TypeName> field = PropertyName (); \
1755- if (!field.IsEmpty ()) { \
1756- size_t index = creator->AddData (field); \
1757- info.persistent_templates .push_back ({#PropertyName, id, index}); \
1758- } \
1759- id++; \
1760- } while (0 );
1761- ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES (V)
1762- #undef V
1763-
1764- id = 0 ;
17651819#define V (PropertyName, TypeName ) \
17661820 do { \
17671821 Local<TypeName> field = PropertyName (); \
@@ -1778,17 +1832,6 @@ EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) {
17781832 return info;
17791833}
17801834
1781- std::ostream& operator <<(std::ostream& output,
1782- const std::vector<PropInfo>& vec) {
1783- output << " {\n " ;
1784- for (const auto & info : vec) {
1785- output << " { \" " << info.name << " \" , " << std::to_string (info.id ) << " , "
1786- << std::to_string (info.index ) << " },\n " ;
1787- }
1788- output << " }" ;
1789- return output;
1790- }
1791-
17921835std::ostream& operator <<(std::ostream& output,
17931836 const std::vector<std::string>& vec) {
17941837 output << " {\n " ;
@@ -1818,9 +1861,6 @@ std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& i) {
18181861 << i.stream_base_state << " , // stream_base_state\n "
18191862 << i.should_abort_on_uncaught_toggle
18201863 << " , // should_abort_on_uncaught_toggle\n "
1821- << " // -- persistent_templates begins --\n "
1822- << i.persistent_templates << " ,\n "
1823- << " // persistent_templates ends --\n "
18241864 << " // -- persistent_values begins --\n "
18251865 << i.persistent_values << " ,\n "
18261866 << " // -- persistent_values ends --\n "
@@ -1869,40 +1909,30 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
18691909 std::cerr << *info << " \n " ;
18701910 }
18711911
1872- const std::vector<PropInfo>& templates = info->persistent_templates ;
1912+ const std::vector<PropInfo>& values = info->persistent_values ;
18731913 size_t i = 0 ; // index to the array
18741914 size_t id = 0 ;
1875- #define SetProperty (PropertyName, TypeName, vector, type, from ) \
1915+ #define V (PropertyName, TypeName ) \
18761916 do { \
1877- if (vector .size () > i && id == vector [i].id ) { \
1878- const PropInfo& d = vector [i]; \
1917+ if (values .size () > i && id == values [i].id ) { \
1918+ const PropInfo& d = values [i]; \
18791919 DCHECK_EQ (d.name , #PropertyName); \
18801920 MaybeLocal<TypeName> maybe_field = \
1881- from ->GetDataFromSnapshotOnce <TypeName>(d.index ); \
1921+ ctx ->GetDataFromSnapshotOnce <TypeName>(d.index ); \
18821922 Local<TypeName> field; \
18831923 if (!maybe_field.ToLocal (&field)) { \
18841924 fprintf (stderr, \
1885- " Failed to deserialize environment " #type " " #PropertyName \
1925+ " Failed to deserialize environment value " #PropertyName \
18861926 " \n " ); \
18871927 } \
18881928 set_##PropertyName (field); \
18891929 i++; \
18901930 } \
1891- } while (0 ); \
1892- id++;
1893- #define V (PropertyName, TypeName ) SetProperty(PropertyName, TypeName, \
1894- templates, template , isolate_)
1895- ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES (V);
1896- #undef V
1931+ id++; \
1932+ } while (0 );
18971933
1898- i = 0 ; // index to the array
1899- id = 0 ;
1900- const std::vector<PropInfo>& values = info->persistent_values ;
1901- #define V (PropertyName, TypeName ) SetProperty(PropertyName, TypeName, \
1902- values, value, ctx)
19031934 ENVIRONMENT_STRONG_PERSISTENT_VALUES (V);
19041935#undef V
1905- #undef SetProperty
19061936
19071937 MaybeLocal<Context> maybe_ctx_from_snapshot =
19081938 ctx->GetDataFromSnapshotOnce <Context>(info->context );
0 commit comments