From 8dadaa652eb2be2949d91ff3a88d9605334f2f5f Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Tue, 1 Dec 2020 05:41:41 +0100 Subject: [PATCH] src: remove some duplication in DeserializeProps This commit introduces a new macro to reduce som code duplication in Environment::DeserializeProperties. PR-URL: https://github.com/nodejs/node/pull/36336 Reviewed-By: Zeyu Yang Reviewed-By: Rich Trott Reviewed-By: Richard Lau --- src/env.cc | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/env.cc b/src/env.cc index 71ad278d6a3878..544bb4b54daa0f 100644 --- a/src/env.cc +++ b/src/env.cc @@ -1356,17 +1356,17 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) { const std::vector& templates = info->persistent_templates; size_t i = 0; // index to the array size_t id = 0; -#define V(PropertyName, TypeName) \ +#define SetProperty(PropertyName, TypeName, vector, type, from) \ do { \ - if (templates.size() > i && id == templates[i].id) { \ - const PropInfo& d = templates[i]; \ + if (vector.size() > i && id == vector[i].id) { \ + const PropInfo& d = vector[i]; \ DCHECK_EQ(d.name, #PropertyName); \ MaybeLocal maybe_field = \ - isolate_->GetDataFromSnapshotOnce(d.index); \ + from->GetDataFromSnapshotOnce(d.index); \ Local field; \ if (!maybe_field.ToLocal(&field)) { \ fprintf(stderr, \ - "Failed to deserialize environment template " #PropertyName \ + "Failed to deserialize environment " #type " " #PropertyName \ "\n"); \ } \ set_##PropertyName(field); \ @@ -1374,32 +1374,19 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) { } \ } while (0); \ id++; +#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \ + templates, template, isolate_) ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V); #undef V i = 0; // index to the array id = 0; const std::vector& values = info->persistent_values; -#define V(PropertyName, TypeName) \ - do { \ - if (values.size() > i && id == values[i].id) { \ - const PropInfo& d = values[i]; \ - DCHECK_EQ(d.name, #PropertyName); \ - MaybeLocal maybe_field = \ - ctx->GetDataFromSnapshotOnce(d.index); \ - Local field; \ - if (!maybe_field.ToLocal(&field)) { \ - fprintf(stderr, \ - "Failed to deserialize environment value " #PropertyName \ - "\n"); \ - } \ - set_##PropertyName(field); \ - i++; \ - } \ - } while (0); \ - id++; +#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \ + values, value, ctx) ENVIRONMENT_STRONG_PERSISTENT_VALUES(V); #undef V +#undef SetProperty MaybeLocal maybe_ctx_from_snapshot = ctx->GetDataFromSnapshotOnce(info->context);