From acc801fec9ff98fc27e8bef32307e7ded901d76f Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 21 Nov 2024 17:11:09 -0400 Subject: [PATCH] Maintain common static JSON values in `EvaluationContext` (#203) Signed-off-by: Juan Cruz Viotti --- src/evaluator/context.cc | 3 +++ src/evaluator/dispatch.h | 4 ++-- src/evaluator/evaluator.cc | 19 +++++++++---------- .../sourcemeta/blaze/evaluator_context.h | 3 ++- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/evaluator/context.cc b/src/evaluator/context.cc index d6d7ea72..1e3d5fa3 100644 --- a/src/evaluator/context.cc +++ b/src/evaluator/context.cc @@ -4,6 +4,9 @@ namespace sourcemeta::blaze { +const sourcemeta::jsontoolkit::JSON EvaluationContext::null{nullptr}; +const sourcemeta::jsontoolkit::JSON EvaluationContext::empty_string{""}; + // TODO: Completely inline push/pop auto EvaluationContext::push( diff --git a/src/evaluator/dispatch.h b/src/evaluator/dispatch.h index 3f2404ab..0309b158 100644 --- a/src/evaluator/dispatch.h +++ b/src/evaluator/dispatch.h @@ -668,10 +668,10 @@ HANDLER_START { auto destination = context.instance_location; destination.push_back(control.value); callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, - destination, context.null); + destination, EvaluationContext::null); context.evaluate(control.value); callback.value()(EvaluationType::Post, true, step, context.evaluate_path, - destination, context.null); + destination, EvaluationContext::null); } else { context.evaluate(control.value); } diff --git a/src/evaluator/evaluator.cc b/src/evaluator/evaluator.cc index a55e74ef..3e59d606 100644 --- a/src/evaluator/evaluator.cc +++ b/src/evaluator/evaluator.cc @@ -24,8 +24,7 @@ inline auto resolve_target( // to cope with non-string keywords inside `propertyNames` // that need to fail validation. But then, the actual string // we return doesn't matter, so we can always return a dummy one. - static const sourcemeta::jsontoolkit::JSON empty_string{""}; - return empty_string; + return sourcemeta::blaze::EvaluationContext::empty_string; } return instance; @@ -101,7 +100,7 @@ auto evaluate_step( } \ if (callback.has_value()) { \ callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ - context.instance_location, context.null); \ + context.instance_location, EvaluationContext::null); \ } \ bool result{false}; @@ -127,7 +126,7 @@ auto evaluate_step( } \ if (callback.has_value()) { \ callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ - context.instance_location, context.null); \ + context.instance_location, EvaluationContext::null); \ } \ const auto &target{maybe_target.value().get()}; \ bool result{false}; @@ -159,7 +158,7 @@ auto evaluate_step( assert(!step_category.relative_instance_location.empty()); \ if (callback.has_value()) { \ callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ - context.instance_location, context.null); \ + context.instance_location, EvaluationContext::null); \ } \ bool result{false}; @@ -174,7 +173,7 @@ auto evaluate_step( } \ if (callback.has_value()) { \ callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ - context.instance_location, context.null); \ + context.instance_location, EvaluationContext::null); \ } \ bool result{false}; @@ -183,7 +182,7 @@ auto evaluate_step( const auto &step_category{std::get(step)}; \ if (callback.has_value()) { \ callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ - context.instance_location, context.null); \ + context.instance_location, EvaluationContext::null); \ } \ bool result{true}; @@ -196,7 +195,7 @@ auto evaluate_step( if (callback.has_value()) { \ callback.value()(EvaluationType::Post, result, step, \ context.evaluate_path, context.instance_location, \ - context.null); \ + EvaluationContext::null); \ } \ context.pop(step_category.relative_schema_location.size(), \ step_category.relative_instance_location.size(), track); \ @@ -210,7 +209,7 @@ auto evaluate_step( if (callback.has_value()) { \ callback.value()(EvaluationType::Post, result, step, \ context.evaluate_path, context.instance_location, \ - context.null); \ + EvaluationContext::null); \ } \ SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ return result; @@ -229,7 +228,7 @@ auto evaluate_step( step_category.relative_instance_location, track); \ if (callback.has_value()) { \ callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ - destination, context.null); \ + destination, EvaluationContext::null); \ callback.value()(EvaluationType::Post, true, step, context.evaluate_path, \ destination, annotation_value); \ } \ diff --git a/src/evaluator/include/sourcemeta/blaze/evaluator_context.h b/src/evaluator/include/sourcemeta/blaze/evaluator_context.h index 164795a4..ac6b5cda 100644 --- a/src/evaluator/include/sourcemeta/blaze/evaluator_context.h +++ b/src/evaluator/include/sourcemeta/blaze/evaluator_context.h @@ -60,7 +60,8 @@ class SOURCEMETA_BLAZE_EVALUATOR_EXPORT EvaluationContext { -> bool; auto unevaluate() -> void; - const sourcemeta::jsontoolkit::JSON null{nullptr}; + static const sourcemeta::jsontoolkit::JSON null; + static const sourcemeta::jsontoolkit::JSON empty_string; // Exporting symbols that depends on the standard C++ library is considered // safe.