Skip to content

Commit fdc4b03

Browse files
danbevcjihrig
authored andcommitted
src: remove some duplication in DeserializeProps
This commit introduces a new macro to reduce som code duplication in Environment::DeserializeProperties. PR-URL: nodejs#36336 Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 4dcb65e commit fdc4b03

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

src/env.cc

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,50 +1346,37 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
13461346
const std::vector<PropInfo>& templates = info->persistent_templates;
13471347
size_t i = 0; // index to the array
13481348
size_t id = 0;
1349-
#define V(PropertyName, TypeName) \
1349+
#define SetProperty(PropertyName, TypeName, vector, type, from) \
13501350
do { \
1351-
if (templates.size() > i && id == templates[i].id) { \
1352-
const PropInfo& d = templates[i]; \
1351+
if (vector.size() > i && id == vector[i].id) { \
1352+
const PropInfo& d = vector[i]; \
13531353
DCHECK_EQ(d.name, #PropertyName); \
13541354
MaybeLocal<TypeName> maybe_field = \
1355-
isolate_->GetDataFromSnapshotOnce<TypeName>(d.index); \
1355+
from->GetDataFromSnapshotOnce<TypeName>(d.index); \
13561356
Local<TypeName> field; \
13571357
if (!maybe_field.ToLocal(&field)) { \
13581358
fprintf(stderr, \
1359-
"Failed to deserialize environment template " #PropertyName \
1359+
"Failed to deserialize environment " #type " " #PropertyName \
13601360
"\n"); \
13611361
} \
13621362
set_##PropertyName(field); \
13631363
i++; \
13641364
} \
13651365
} while (0); \
13661366
id++;
1367+
#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \
1368+
templates, template, isolate_)
13671369
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V);
13681370
#undef V
13691371

13701372
i = 0; // index to the array
13711373
id = 0;
13721374
const std::vector<PropInfo>& values = info->persistent_values;
1373-
#define V(PropertyName, TypeName) \
1374-
do { \
1375-
if (values.size() > i && id == values[i].id) { \
1376-
const PropInfo& d = values[i]; \
1377-
DCHECK_EQ(d.name, #PropertyName); \
1378-
MaybeLocal<TypeName> maybe_field = \
1379-
ctx->GetDataFromSnapshotOnce<TypeName>(d.index); \
1380-
Local<TypeName> field; \
1381-
if (!maybe_field.ToLocal(&field)) { \
1382-
fprintf(stderr, \
1383-
"Failed to deserialize environment value " #PropertyName \
1384-
"\n"); \
1385-
} \
1386-
set_##PropertyName(field); \
1387-
i++; \
1388-
} \
1389-
} while (0); \
1390-
id++;
1375+
#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \
1376+
values, value, ctx)
13911377
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V);
13921378
#undef V
1379+
#undef SetProperty
13931380

13941381
MaybeLocal<Context> maybe_ctx_from_snapshot =
13951382
ctx->GetDataFromSnapshotOnce<Context>(info->context);

0 commit comments

Comments
 (0)