1#ifndef SOURCEMETA_BLAZE_EVALUATOR_DISPATCH_H_
2#define SOURCEMETA_BLAZE_EVALUATOR_DISPATCH_H_
5#include <sourcemeta/blaze/evaluator.h>
7#include <sourcemeta/core/crypto.h>
8#include <sourcemeta/core/css.h>
9#include <sourcemeta/core/dns.h>
10#include <sourcemeta/core/email.h>
11#include <sourcemeta/core/ip.h>
12#include <sourcemeta/core/regex.h>
13#include <sourcemeta/core/time.h>
14#include <sourcemeta/core/uritemplate.h>
21#define SOURCEMETA_ASSUME(x) \
25#define SOURCEMETA_ASSUME(x) assert(x)
28#define SOURCEMETA_STRINGIFY(x) #x
30#define EVALUATE_PUSH() \
31 if constexpr (HasCallback) { \
32 context.evaluator->evaluate_path.push_back( \
33 context.schema->extra[instruction.extra_index] \
34 .relative_schema_location); \
36 if constexpr (HasCallback) { \
37 context.evaluator->instance_location.push_back( \
38 instruction.relative_instance_location); \
40 if constexpr (HasCallback) { \
41 (*context.callback)(EvaluationType::Pre, true, instruction, \
42 context.schema->extra[instruction.extra_index], \
43 context.evaluator->evaluate_path, \
44 context.evaluator->instance_location, \
45 Evaluator::NULL_VALUE); \
48#define EVALUATE_POP() \
49 if constexpr (HasCallback) { \
50 (*context.callback)(EvaluationType::Post, result, instruction, \
51 context.schema->extra[instruction.extra_index], \
52 context.evaluator->evaluate_path, \
53 context.evaluator->instance_location, \
54 Evaluator::NULL_VALUE); \
56 if constexpr (HasCallback) { \
57 context.evaluator->evaluate_path.pop_back( \
58 context.schema->extra[instruction.extra_index] \
59 .relative_schema_location.size()); \
61 if constexpr (HasCallback) { \
62 context.evaluator->instance_location.pop_back( \
63 instruction.relative_instance_location.size()); \
66#define EVALUATE_BEGIN(instruction_type, precondition) \
67 assert(instruction.type == InstructionIndex::instruction_type); \
68 const auto &target{resolve_target( \
69 context.property_target, \
70 resolve_instance(instance, instruction.relative_instance_location))}; \
71 if (!(precondition)) [[unlikely]] { \
77#define EVALUATE_BEGIN_NON_STRING(instruction_type, precondition) \
78 assert(instruction.type == InstructionIndex::instruction_type); \
80 resolve_instance(instance, instruction.relative_instance_location)}; \
81 if (!(precondition)) [[unlikely]] { \
87#define EVALUATE_BEGIN_IF_STRING(instruction_type) \
88 assert(instruction.type == InstructionIndex::instruction_type); \
89 const auto *maybe_target{ \
90 resolve_string_target(context.property_target, instance, \
91 instruction.relative_instance_location)}; \
92 if (!maybe_target) [[unlikely]] { \
96 const auto &target{*maybe_target}; \
99#define EVALUATE_BEGIN_TRY_TARGET(instruction_type) \
100 assert(instruction.type == InstructionIndex::instruction_type); \
101 const auto &target{instance}; \
102 if (!target.is_object()) [[unlikely]] { \
105 assert(!instruction.relative_instance_location.empty()); \
106 const auto *target_check{ \
107 instruction.relative_instance_location.size() == 1 \
109 instruction.relative_instance_location.at(0).to_property(), \
110 instruction.relative_instance_location.at(0).property_hash()) \
111 : try_get(target, instruction.relative_instance_location)}; \
112 if (!target_check) [[unlikely]] { \
118#define EVALUATE_BEGIN_NO_PRECONDITION(instruction_type) \
119 assert(instruction.type == InstructionIndex::instruction_type); \
123#define EVALUATE_BEGIN_NO_PRECONDITION_AND_NO_PUSH(instruction_type) \
124 assert(instruction.type == InstructionIndex::instruction_type); \
125 if constexpr (HasCallback) { \
126 (*context.callback)(EvaluationType::Pre, true, instruction, \
127 context.schema->extra[instruction.extra_index], \
128 context.evaluator->evaluate_path, \
129 context.evaluator->instance_location, \
130 Evaluator::NULL_VALUE); \
134#define EVALUATE_BEGIN_PASS_THROUGH(instruction_type) \
135 assert(instruction.type == InstructionIndex::instruction_type); \
138#define EVALUATE_END(instruction_type) \
142#define EVALUATE_END_NO_POP(instruction_type) \
143 if constexpr (HasCallback) { \
144 (*context.callback)(EvaluationType::Post, result, instruction, \
145 context.schema->extra[instruction.extra_index], \
146 context.evaluator->evaluate_path, \
147 context.evaluator->instance_location, \
148 Evaluator::NULL_VALUE); \
152#define EVALUATE_END_PASS_THROUGH(instruction_type) return result;
154#define EVALUATE_ANNOTATION(instruction_type, destination, annotation_value) \
155 if constexpr (HasCallback) { \
156 context.evaluator->evaluate_path.push_back( \
157 context.schema->extra[instruction.extra_index] \
158 .relative_schema_location); \
159 context.evaluator->instance_location.push_back( \
160 instruction.relative_instance_location); \
161 const auto &reported_location{destination}; \
162 (*context.callback)(EvaluationType::Pre, true, instruction, \
163 context.schema->extra[instruction.extra_index], \
164 context.evaluator->evaluate_path, reported_location, \
165 Evaluator::NULL_VALUE); \
166 (*context.callback)(EvaluationType::Post, true, instruction, \
167 context.schema->extra[instruction.extra_index], \
168 context.evaluator->evaluate_path, reported_location, \
170 context.evaluator->evaluate_path.pop_back( \
171 context.schema->extra[instruction.extra_index] \
172 .relative_schema_location.size()); \
173 context.evaluator->instance_location.pop_back( \
174 instruction.relative_instance_location.size()); \
178#define EVALUATE_RECURSE(child, target) \
179 evaluate_instruction(child, target, depth + 1, context)
180#define EVALUATE_RECURSE_ON_PROPERTY_NAME(child, target, name) \
181 evaluate_instruction_with_property(child, target, depth + 1, context, name)
183namespace sourcemeta::blaze::dispatch {
184using namespace sourcemeta::core;
187constexpr auto DEPTH_LIMIT{300};
189inline auto resolve_target(
const JSON::String *property_target,
190 const JSON &instance)
noexcept ->
const JSON & {
191 if (property_target !=
nullptr) [[unlikely]] {
192 return Evaluator::EMPTY_STRING;
199inline auto resolve_instance(
const JSON &instance,
200 const Pointer &relative_instance_location)
202 if (relative_instance_location.empty()) {
210 if (relative_instance_location.size() == 1 &&
211 relative_instance_location.at(0).is_property()) {
212 return instance.at(relative_instance_location.at(0).to_property(),
213 relative_instance_location.at(0).property_hash());
216 return get(instance, relative_instance_location);
220resolve_string_target(
const JSON::String *property_target,
const JSON &instance,
221 const Pointer &relative_instance_location)
noexcept
222 ->
const JSON::String * {
223 if (property_target !=
nullptr) [[unlikely]] {
224 return property_target;
227 const auto &target{resolve_instance(instance, relative_instance_location)};
228 if (!target.is_string()) [[unlikely]] {
231 return &target.to_string();
239template <
typename Indexes>
240inline auto named_index(
const Indexes &indexes,
const JSON::String &name,
241 const JSON::Object::hash_type &name_hash)
noexcept
242 ->
const Indexes::first_type::value_type * {
245 std::size_t first{1};
246 std::size_t last{indexes.first.size()};
247 if (!indexes.second.empty()) {
250 if (name.size() >= indexes.second.size()) {
254 const auto &bounds{indexes.second[name.size()]};
255 if (bounds.second == 0) {
259 first = bounds.first;
260 last = bounds.second;
267 constexpr sourcemeta::core::PropertyHashJSON<JSON::String> HASHER{};
268 if (HASHER.is_perfect(name_hash)) {
269 for (
auto position = first; position <= last; position++) {
270 const auto &entry{indexes.first[position - 1]};
271 if (std::get<0>(entry) == name_hash) {
276 for (
auto position = first; position <= last; position++) {
277 const auto &entry{indexes.first[position - 1]};
278 if (std::get<0>(entry) == name_hash && std::get<2>(entry) == name) {
291inline auto type_set_test(
const ValueTypes &types,
292 const JSON::Type type)
noexcept ->
bool {
293 assert(std::to_underlying(type) < types.size());
294 return types[std::to_underlying(type)];
297inline auto effective_type_strict_real(
const JSON &instance)
noexcept
299 const auto real_type{instance.type()};
301 case JSON::Type::Decimal:
302 return instance.to_decimal().is_integer() ? JSON::Type::Integer
320inline auto type_set_spans_numbers(
const ValueTypes &types)
noexcept ->
bool {
321 const auto integer{types[std::to_underlying(JSON::Type::Integer)]};
322 return types[std::to_underlying(JSON::Type::Real)] == integer &&
323 types[std::to_underlying(JSON::Type::Decimal)] == integer;
326template <
bool Normalise>
327inline auto type_in_set(
const JSON &instance,
const ValueTypes &types)
noexcept
329 if constexpr (Normalise) {
330 return type_set_test(types, effective_type_strict_real(instance));
332 return type_set_test(types, instance.type());
336template <
bool Normalise>
337inline auto array_items_in_type_set(
const JSON &target,
339 for (
const auto &entry : target.as_array()) {
340 if (!type_in_set<Normalise>(entry, types)) [[unlikely]] {
348template <
bool Normalise>
349inline auto object_values_in_type_set(
const JSON &target,
352 for (
const auto &entry : target.as_object()) {
353 if (!type_in_set<Normalise>(entry.second, types)) [[unlikely]] {
361template <
bool Normalise>
362inline auto items_are_bounded_arrays_in_type_set(
363 const JSON &target,
const ValueTypes &types,
const std::size_t minimum,
364 const std::optional<std::size_t> &maximum)
noexcept ->
bool {
365 for (
const auto &item : target.as_array()) {
366 if (item.type() != JSON::Type::Array || item.array_size() < minimum ||
367 (maximum.has_value() && item.array_size() > maximum.value()))
372 if (!array_items_in_type_set<Normalise>(item, types)) [[unlikely]] {
380inline auto as_decimal(
const JSON &instance) -> Decimal {
381 assert(instance.is_integral());
382 return instance.is_decimal() ? instance.to_decimal()
383 : Decimal::exact_from(instance.to_real());
386inline auto integer_within_bounds(
const JSON &target,
388 if (target.is_integer()) {
389 const auto integer{target.to_integer()};
390 return integer >= value.first && integer <= value.second;
393 if (target.is_integral()) {
394 const auto decimal{as_decimal(target)};
395 return decimal >= Decimal{value.first} && decimal <= Decimal{value.second};
402inline auto assume_value(
const Value &variant)
noexcept ->
const T & {
403 const auto *pointer{std::get_if<T>(&variant)};
404 SOURCEMETA_ASSUME(pointer !=
nullptr);
409inline auto assume_value_copy(
const Value &variant)
noexcept -> T {
410 const auto *pointer{std::get_if<T>(&variant)};
411 SOURCEMETA_ASSUME(pointer !=
nullptr);
419 const sourcemeta::core::JSON::String *property_target;
425template <
bool HasCallback>
428 const sourcemeta::core::JSON &instance,
429 const std::uint64_t depth,
432template <
bool HasCallback>
433inline auto evaluate_instruction_with_property(
435 const sourcemeta::core::JSON &instance,
const std::uint64_t depth,
437 const sourcemeta::core::JSON::String &name) -> bool;
439#define INSTRUCTION_HANDLER(name) \
440 template <bool HasCallback> \
441 static inline auto name( \
442 [[maybe_unused]] const sourcemeta::blaze::Instruction &instruction, \
443 [[maybe_unused]] const sourcemeta::core::JSON &instance, \
444 [[maybe_unused]] const std::uint64_t depth, \
445 [[maybe_unused]] DispatchContext<HasCallback> &context) -> bool
447#define INSTRUCTION_DIRECT(name, value_type) \
448 SOURCEMETA_FORCEINLINE inline auto DIRECT_##name( \
449 const sourcemeta::core::JSON &target, const value_type &value) \
452#define INSTRUCTION_DIRECT_COPY(name, value_type) \
453 SOURCEMETA_FORCEINLINE inline auto DIRECT_##name( \
454 const sourcemeta::core::JSON &target, const value_type value) \
457#define DIRECT(name, target, value) DIRECT_##name(target, value)
465INSTRUCTION_HANDLER(AssertionFail) {
466 EVALUATE_BEGIN_NO_PRECONDITION(AssertionFail);
467 EVALUATE_END(AssertionFail);
470INSTRUCTION_HANDLER(AssertionDefines) {
471 EVALUATE_BEGIN_NON_STRING(AssertionDefines, target.is_object());
472 const auto &value{assume_value<ValueProperty>(instruction.value)};
473 result = target.defines(value.first, value.second);
474 EVALUATE_END(AssertionDefines);
477INSTRUCTION_HANDLER(AssertionDefinesStrict) {
478 EVALUATE_BEGIN_NO_PRECONDITION(AssertionDefinesStrict);
480 resolve_instance(instance, instruction.relative_instance_location)};
481 const auto &value{assume_value<ValueProperty>(instruction.value)};
482 result = target.is_object() && target.defines(value.first, value.second);
483 EVALUATE_END(AssertionDefinesStrict);
486INSTRUCTION_HANDLER(AssertionDefinesAll) {
487 EVALUATE_BEGIN_NON_STRING(AssertionDefinesAll, target.is_object());
488 const auto &value{assume_value<ValueStringSet>(instruction.value)};
490 assert(value.size() > 1);
493 if (value.size() <= target.object_size()) [[likely]] {
495 const auto &
object{target.as_object()};
496 for (
const auto &property : value) {
497 if (!
object.defines(property.first, property.second)) [[unlikely]] {
504 EVALUATE_END(AssertionDefinesAll);
507INSTRUCTION_HANDLER(AssertionDefinesAllStrict) {
508 EVALUATE_BEGIN_NO_PRECONDITION(AssertionDefinesAllStrict);
510 resolve_instance(instance, instruction.relative_instance_location)};
511 const auto &value{assume_value<ValueStringSet>(instruction.value)};
513 assert(value.size() > 1);
516 if (target.is_object() && value.size() <= target.object_size()) [[likely]] {
518 const auto &
object{target.as_object()};
519 for (
const auto &property : value) {
520 if (!
object.defines(property.first, property.second)) [[unlikely]] {
527 EVALUATE_END(AssertionDefinesAllStrict);
530INSTRUCTION_HANDLER(AssertionDefinesExactly) {
531 EVALUATE_BEGIN_NON_STRING(AssertionDefinesExactly, target.is_object());
532 const auto &value{assume_value<ValueStringSet>(instruction.value)};
534 assert(value.size() > 1);
535 const auto &
object{target.as_object()};
537 if (value.size() ==
object.size()) [[likely]] {
539 for (
const auto &property : value) {
540 if (!
object.defines(property.first, property.second)) [[unlikely]] {
547 EVALUATE_END(AssertionDefinesExactly);
550INSTRUCTION_HANDLER(AssertionDefinesExactlyStrict) {
551 EVALUATE_BEGIN_NO_PRECONDITION(AssertionDefinesExactlyStrict);
553 resolve_instance(instance, instruction.relative_instance_location)};
554 if (target.is_object()) [[likely]] {
555 const auto &value{assume_value<ValueStringSet>(instruction.value)};
557 assert(value.size() > 1);
558 const auto &
object{target.as_object()};
560 if (value.size() ==
object.size()) [[likely]] {
562 for (
const auto &property : value) {
563 if (!
object.defines(property.first, property.second)) [[unlikely]] {
571 EVALUATE_END(AssertionDefinesExactlyStrict);
574INSTRUCTION_HANDLER(AssertionDefinesExactlyStrictHash3) {
575 EVALUATE_BEGIN_NO_PRECONDITION(AssertionDefinesExactlyStrictHash3);
577 resolve_instance(instance, instruction.relative_instance_location)};
578 if (target.is_object()) [[likely]] {
581 const auto &value{assume_value<ValueStringHashes>(instruction.value)};
582 assert(value.first.size() == 3);
583 const auto &
object{target.as_object()};
589 object.size() == 3 && ((value.first.at(0).first ==
object.at(0).hash &&
590 value.first.at(1).first ==
object.at(1).hash &&
591 value.first.at(2).first ==
object.at(2).hash) ||
592 (value.first.at(0).first ==
object.at(0).hash &&
593 value.first.at(1).first ==
object.at(2).hash &&
594 value.first.at(2).first ==
object.at(1).hash) ||
595 (value.first.at(0).first ==
object.at(1).hash &&
596 value.first.at(1).first ==
object.at(0).hash &&
597 value.first.at(2).first ==
object.at(2).hash) ||
598 (value.first.at(0).first ==
object.at(1).hash &&
599 value.first.at(1).first ==
object.at(2).hash &&
600 value.first.at(2).first ==
object.at(0).hash) ||
601 (value.first.at(0).first ==
object.at(2).hash &&
602 value.first.at(1).first ==
object.at(0).hash &&
603 value.first.at(2).first ==
object.at(1).hash) ||
604 (value.first.at(0).first ==
object.at(2).hash &&
605 value.first.at(1).first ==
object.at(1).hash &&
606 value.first.at(2).first ==
object.at(0).hash));
609 EVALUATE_END(AssertionDefinesExactlyStrictHash3);
612INSTRUCTION_HANDLER(AssertionPropertyDependencies) {
613 EVALUATE_BEGIN_NON_STRING(AssertionPropertyDependencies, target.is_object());
614 const auto &value{assume_value<ValueStringMap>(instruction.value)};
616 assert(!value.empty());
618 const auto &
object{target.as_object()};
619 for (
const auto &entry : value) {
620 if (!
object.defines(entry.first, entry.hash)) {
624 assert(!entry.second.empty());
625 for (
const auto &dependency : entry.second) {
626 if (!
object.defines(dependency.first, dependency.second)) [[unlikely]] {
628 EVALUATE_END(AssertionPropertyDependencies);
633 EVALUATE_END(AssertionPropertyDependencies);
636INSTRUCTION_DIRECT_COPY(AssertionType,
ValueType) {
639 return target.type() == value ||
640 (value == JSON::Type::Integer && target.is_integral());
643INSTRUCTION_HANDLER(AssertionType) {
644 EVALUATE_BEGIN_NO_PRECONDITION(AssertionType);
646 resolve_instance(instance, instruction.relative_instance_location)};
647 const auto value{assume_value_copy<ValueType>(instruction.value)};
650 assert(value == JSON::Type::Integer);
651 SOURCEMETA_ASSUME(value == JSON::Type::Integer);
652 result = DIRECT(AssertionType, target, value);
653 EVALUATE_END(AssertionType);
656INSTRUCTION_DIRECT(AssertionTypeAny,
ValueTypes) {
659 return type_set_test(value, target.type()) ||
660 (type_set_test(value, JSON::Type::Integer) && target.is_integral());
663INSTRUCTION_HANDLER(AssertionTypeAny) {
664 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeAny);
665 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
668 resolve_instance(instance, instruction.relative_instance_location)};
669 result = DIRECT(AssertionTypeAny, target, value);
670 EVALUATE_END(AssertionTypeAny);
673INSTRUCTION_DIRECT_COPY(AssertionTypeStrict,
ValueType) {
674 return effective_type_strict_real(target) == value;
677INSTRUCTION_HANDLER(AssertionTypeStrict) {
678 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeStrict);
680 resolve_instance(instance, instruction.relative_instance_location)};
681 const auto value{assume_value_copy<ValueType>(instruction.value)};
682 result = DIRECT(AssertionTypeStrict, target, value);
683 EVALUATE_END(AssertionTypeStrict);
686INSTRUCTION_DIRECT(AssertionTypeStrictAny,
ValueTypes) {
687 return type_set_test(value, effective_type_strict_real(target));
690INSTRUCTION_HANDLER(AssertionTypeStrictAny) {
691 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeStrictAny);
692 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
695 resolve_instance(instance, instruction.relative_instance_location)};
696 result = DIRECT(AssertionTypeStrictAny, target, value);
697 EVALUATE_END(AssertionTypeStrictAny);
700INSTRUCTION_DIRECT(AssertionNotTypeStrictAny,
ValueTypes) {
701 return !type_set_test(value, effective_type_strict_real(target));
704INSTRUCTION_HANDLER(AssertionNotTypeStrictAny) {
705 EVALUATE_BEGIN_NO_PRECONDITION(AssertionNotTypeStrictAny);
706 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
709 resolve_instance(instance, instruction.relative_instance_location)};
710 result = DIRECT(AssertionNotTypeStrictAny, target, value);
711 EVALUATE_END(AssertionNotTypeStrictAny);
714INSTRUCTION_DIRECT(AssertionTypeStringBounded,
ValueRange) {
715 const auto &[minimum, maximum, exhaustive] = value;
716 return target.type() == JSON::Type::String &&
717 target.string_size() >= minimum &&
718 (!maximum.has_value() || target.string_size() <= maximum.value());
721INSTRUCTION_HANDLER(AssertionTypeStringBounded) {
722 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeStringBounded);
724 resolve_instance(instance, instruction.relative_instance_location)};
725 const auto &value{assume_value<ValueRange>(instruction.value)};
726 assert(!std::get<1>(value).has_value() ||
727 std::get<1>(value).value() >= std::get<0>(value));
728 assert(!std::get<2>(value));
729 result = DIRECT(AssertionTypeStringBounded, target, value);
730 EVALUATE_END(AssertionTypeStringBounded);
733INSTRUCTION_HANDLER(AssertionTypeStringUpper) {
734 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeStringUpper);
736 resolve_instance(instance, instruction.relative_instance_location)};
737 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
738 result = target.is_string() && target.string_size() <= value;
739 EVALUATE_END(AssertionTypeStringUpper);
742INSTRUCTION_DIRECT(AssertionTypeArrayBounded,
ValueRange) {
743 const auto &[minimum, maximum, exhaustive] = value;
746 SOURCEMETA_ASSUME(!exhaustive);
747 return target.type() == JSON::Type::Array && target.array_size() >= minimum &&
748 (!maximum.has_value() || target.array_size() <= maximum.value());
751INSTRUCTION_HANDLER(AssertionTypeArrayBounded) {
752 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeArrayBounded);
754 resolve_instance(instance, instruction.relative_instance_location)};
755 const auto &value{assume_value<ValueRange>(instruction.value)};
756 assert(!std::get<1>(value).has_value() ||
757 std::get<1>(value).value() >= std::get<0>(value));
758 result = DIRECT(AssertionTypeArrayBounded, target, value);
759 EVALUATE_END(AssertionTypeArrayBounded);
762INSTRUCTION_HANDLER(AssertionTypeArrayUpper) {
763 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeArrayUpper);
765 resolve_instance(instance, instruction.relative_instance_location)};
766 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
767 result = target.is_array() && target.array_size() <= value;
768 EVALUATE_END(AssertionTypeArrayUpper);
771INSTRUCTION_HANDLER(AssertionTypeObjectBounded) {
772 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeObjectBounded);
774 resolve_instance(instance, instruction.relative_instance_location)};
775 const auto &value{assume_value<ValueRange>(instruction.value)};
776 const auto &[minimum, maximum, exhaustive] = value;
777 assert(!maximum.has_value() || maximum.value() >= minimum);
780 SOURCEMETA_ASSUME(!exhaustive);
781 result = target.type() == JSON::Type::Object &&
782 target.object_size() >= minimum &&
783 (!maximum.has_value() || target.object_size() <= maximum.value());
784 EVALUATE_END(AssertionTypeObjectBounded);
787INSTRUCTION_HANDLER(AssertionTypeObjectUpper) {
788 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeObjectUpper);
790 resolve_instance(instance, instruction.relative_instance_location)};
791 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
792 result = target.is_object() && target.object_size() <= value;
793 EVALUATE_END(AssertionTypeObjectUpper);
796INSTRUCTION_HANDLER(AssertionRegex) {
797 EVALUATE_BEGIN_IF_STRING(AssertionRegex);
798 const auto &value{assume_value<ValueRegex>(instruction.value)};
799 result = matches(value.first, target);
800 EVALUATE_END(AssertionRegex);
803INSTRUCTION_HANDLER(AssertionStringSizeLess) {
804 EVALUATE_BEGIN_IF_STRING(AssertionStringSizeLess);
805 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
806 result = (JSON::size(target) < value);
807 EVALUATE_END(AssertionStringSizeLess);
810INSTRUCTION_HANDLER(AssertionStringSizeGreater) {
811 EVALUATE_BEGIN_IF_STRING(AssertionStringSizeGreater);
812 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
813 result = (JSON::size(target) > value);
814 EVALUATE_END(AssertionStringSizeGreater);
817INSTRUCTION_HANDLER(AssertionArraySizeLess) {
818 EVALUATE_BEGIN_NON_STRING(AssertionArraySizeLess, target.is_array());
819 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
820 result = (target.array_size() < value);
821 EVALUATE_END(AssertionArraySizeLess);
824INSTRUCTION_HANDLER(AssertionArraySizeGreater) {
825 EVALUATE_BEGIN_NON_STRING(AssertionArraySizeGreater, target.is_array());
826 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
827 result = (target.array_size() > value);
828 EVALUATE_END(AssertionArraySizeGreater);
831INSTRUCTION_HANDLER(AssertionObjectSizeLess) {
832 EVALUATE_BEGIN_NON_STRING(AssertionObjectSizeLess, target.is_object());
833 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
834 result = (target.object_size() < value);
835 EVALUATE_END(AssertionObjectSizeLess);
838INSTRUCTION_HANDLER(AssertionObjectSizeGreater) {
839 EVALUATE_BEGIN_NON_STRING(AssertionObjectSizeGreater, target.is_object());
840 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
841 result = (target.object_size() > value);
842 EVALUATE_END(AssertionObjectSizeGreater);
845INSTRUCTION_DIRECT(AssertionEqual,
ValueJSON) {
return target == value; }
847INSTRUCTION_HANDLER(AssertionEqual) {
848 EVALUATE_BEGIN_NO_PRECONDITION(AssertionEqual);
849 const auto &value{assume_value<ValueJSON>(instruction.value)};
851 if (context.property_target) [[unlikely]] {
852 result = value.is_string() && value.to_string() == *context.property_target;
855 resolve_instance(instance, instruction.relative_instance_location)};
856 result = DIRECT(AssertionEqual, target, value);
859 EVALUATE_END(AssertionEqual);
862INSTRUCTION_DIRECT(AssertionEqualsAny,
ValueSet) {
863 return value.contains(target);
866INSTRUCTION_HANDLER(AssertionEqualsAny) {
867 EVALUATE_BEGIN_NO_PRECONDITION(AssertionEqualsAny);
868 const auto &value{assume_value<ValueSet>(instruction.value)};
870 if (context.property_target) [[unlikely]] {
872 result = value.contains(JSON{*context.property_target});
875 resolve_instance(instance, instruction.relative_instance_location)};
876 result = DIRECT(AssertionEqualsAny, target, value);
879 EVALUATE_END(AssertionEqualsAny);
883 if (!target.is_string()) [[unlikely]] {
886 const auto &target_string{target.to_string()};
887 const auto string_size{target_string.size()};
888 static constexpr sourcemeta::core::PropertyHashJSON<ValueString> HASHER;
889 const auto value_hash{HASHER(target_string)};
890 if (string_size < value.second.size()) [[likely]] {
891 const auto &hint{value.second[string_size]};
892 if (hint.second != 0) [[likely]] {
893 for (std::size_t index = hint.first - 1; index < hint.second; index++) {
894 if (value.first[index].first == value_hash) {
903INSTRUCTION_HANDLER(AssertionEqualsAnyStringHash) {
904 EVALUATE_BEGIN_NO_PRECONDITION(AssertionEqualsAnyStringHash);
905 const auto &value{assume_value<ValueStringHashes>(instruction.value)};
907 if (context.property_target) [[unlikely]] {
908 static constexpr sourcemeta::core::PropertyHashJSON<ValueString> HASHER;
909 const auto value_hash{HASHER(*context.property_target)};
910 const auto string_size{context.property_target->size()};
911 if (string_size < value.second.size()) [[likely]] {
912 const auto &hint{value.second[string_size]};
913 assert(hint.first <= hint.second);
914 SOURCEMETA_ASSUME(hint.first <= hint.second);
915 if (hint.second != 0) [[likely]] {
916 for (std::size_t index = hint.first - 1; index < hint.second; index++) {
917 assert(HASHER.is_perfect(value.first[index].first));
918 if (value.first[index].first == value_hash) {
927 resolve_instance(instance, instruction.relative_instance_location)};
928 result = DIRECT(AssertionEqualsAnyStringHash, target, value);
931 EVALUATE_END(AssertionEqualsAnyStringHash);
934INSTRUCTION_HANDLER(AssertionGreaterEqual) {
935 EVALUATE_BEGIN_NON_STRING(AssertionGreaterEqual, target.is_number());
936 const auto &value{assume_value<ValueJSON>(instruction.value)};
937 result = target >= value;
938 EVALUATE_END(AssertionGreaterEqual);
941INSTRUCTION_HANDLER(AssertionLessEqual) {
942 EVALUATE_BEGIN_NON_STRING(AssertionLessEqual, target.is_number());
943 const auto &value{assume_value<ValueJSON>(instruction.value)};
944 result = target <= value;
945 EVALUATE_END(AssertionLessEqual);
948INSTRUCTION_HANDLER(AssertionGreater) {
949 EVALUATE_BEGIN_NON_STRING(AssertionGreater, target.is_number());
950 const auto &value{assume_value<ValueJSON>(instruction.value)};
951 result = target > value;
952 EVALUATE_END(AssertionGreater);
955INSTRUCTION_HANDLER(AssertionLess) {
956 EVALUATE_BEGIN_NON_STRING(AssertionLess, target.is_number());
957 const auto &value{assume_value<ValueJSON>(instruction.value)};
958 result = target < value;
959 EVALUATE_END(AssertionLess);
962INSTRUCTION_HANDLER(AssertionUnique) {
963 EVALUATE_BEGIN_NON_STRING(AssertionUnique, target.is_array());
964 result = target.unique();
965 EVALUATE_END(AssertionUnique);
968INSTRUCTION_HANDLER(AssertionDivisible) {
969 EVALUATE_BEGIN_NON_STRING(AssertionDivisible, target.is_number());
970 const auto &value{assume_value<ValueJSON>(instruction.value)};
971 assert(value.is_number());
972 result = target.divisible_by(value);
973 EVALUATE_END(AssertionDivisible);
977 return integer_within_bounds(target, value);
980INSTRUCTION_HANDLER(AssertionTypeIntegerBounded) {
981 EVALUATE_BEGIN_NON_STRING(AssertionTypeIntegerBounded,
true);
982 result = DIRECT(AssertionTypeIntegerBounded, target,
983 assume_value_copy<ValueIntegerBounds>(instruction.value));
984 EVALUATE_END(AssertionTypeIntegerBounded);
988 if (target.is_integer()) {
989 const auto integer{target.to_integer()};
990 return integer >= value.first && integer <= value.second;
995INSTRUCTION_HANDLER(AssertionTypeIntegerBoundedStrict) {
996 EVALUATE_BEGIN_NON_STRING(AssertionTypeIntegerBoundedStrict,
true);
997 result = DIRECT(AssertionTypeIntegerBoundedStrict, target,
998 assume_value_copy<ValueIntegerBounds>(instruction.value));
999 EVALUATE_END(AssertionTypeIntegerBoundedStrict);
1003 if (target.is_integer()) {
1004 return target.to_integer() >= value.first;
1006 if (target.is_integral()) {
1007 return as_decimal(target) >= Decimal{value.first};
1012INSTRUCTION_HANDLER(AssertionTypeIntegerLowerBound) {
1013 EVALUATE_BEGIN_NON_STRING(AssertionTypeIntegerLowerBound,
true);
1014 result = DIRECT(AssertionTypeIntegerLowerBound, target,
1015 assume_value_copy<ValueIntegerBounds>(instruction.value));
1016 EVALUATE_END(AssertionTypeIntegerLowerBound);
1020 if (target.is_integer()) {
1021 return target.to_integer() >= value.first;
1027 if (effective_type_strict_real(target) == JSON::Type::Integer) {
1028 return target.to_decimal() >= Decimal{value.first};
1033INSTRUCTION_HANDLER(AssertionTypeIntegerLowerBoundStrict) {
1034 EVALUATE_BEGIN_NON_STRING(AssertionTypeIntegerLowerBoundStrict,
true);
1035 result = DIRECT(AssertionTypeIntegerLowerBoundStrict, target,
1036 assume_value_copy<ValueIntegerBounds>(instruction.value));
1037 EVALUATE_END(AssertionTypeIntegerLowerBoundStrict);
1041 return integer_within_bounds(target, value);
1044INSTRUCTION_HANDLER(AssertionNumberIntegerBounded) {
1045 EVALUATE_BEGIN_NON_STRING(AssertionNumberIntegerBounded, target.is_number());
1046 result = DIRECT(AssertionNumberIntegerBounded, target,
1047 assume_value_copy<ValueIntegerBounds>(instruction.value));
1048 EVALUATE_END(AssertionNumberIntegerBounded);
1051INSTRUCTION_HANDLER(AssertionNumberType) {
1052 EVALUATE_BEGIN_NON_STRING(AssertionNumberType, target.is_number());
1053 switch (assume_value_copy<ValueNumberType>(instruction.value)) {
1054 case ValueNumberType::Float:
1055 result = target.is_float();
1057 case ValueNumberType::Double:
1058 result = target.is_double();
1061 EVALUATE_END(AssertionNumberType);
1064INSTRUCTION_HANDLER(AssertionStringType) {
1065 EVALUATE_BEGIN_IF_STRING(AssertionStringType);
1066 const auto value{assume_value_copy<ValueStringType>(instruction.value)};
1068 case ValueStringType::URI:
1069 result = URI::is_uri(target);
1071 case ValueStringType::URIReference:
1072 result = URI::is_uri_reference(target);
1074 case ValueStringType::URITemplate:
1075 result = URITemplate::is_uritemplate(target);
1077 case ValueStringType::IRI:
1078 result = URI::is_iri(target);
1080 case ValueStringType::IRIReference:
1081 result = URI::is_iri_reference(target);
1083 case ValueStringType::Email:
1084 result = is_email(target);
1086 case ValueStringType::IDNEmail:
1087 result = is_idn_email_uts46(target);
1089 case ValueStringType::IPv4:
1090 result = is_ipv4(target);
1092 case ValueStringType::IPv6:
1093 result = is_ipv6(target);
1095 case ValueStringType::Hostname:
1096 result = is_hostname(target);
1098 case ValueStringType::IDNHostname:
1099 result = is_idn_hostname_uts46(target);
1101 case ValueStringType::DateTime:
1102 result = is_rfc3339_datetime(target);
1104 case ValueStringType::Date:
1105 result = is_rfc3339_fulldate(target);
1107 case ValueStringType::Time:
1108 result = is_rfc3339_fulltime(target);
1110 case ValueStringType::PartialTime:
1111 result = is_rfc3339_partialtime_no_secfrac(target);
1113 case ValueStringType::Duration:
1114 result = is_rfc3339_duration(target);
1116 case ValueStringType::JSONPointer:
1117 result = is_pointer(target);
1119 case ValueStringType::RelativeJSONPointer:
1120 result = is_relative_pointer(target);
1122 case ValueStringType::UUID:
1123 result = is_uuid_like(target);
1125 case ValueStringType::Regex:
1126 result = is_regex_ecma(target);
1128 case ValueStringType::Color:
1129 result = is_css2_color(target);
1131 case ValueStringType::Base64:
1132 result = sourcemeta::core::is_base64(target);
1138 EVALUATE_END(AssertionStringType);
1141INSTRUCTION_HANDLER(AssertionPropertyType) {
1142 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyType);
1144 const auto value{assume_value_copy<ValueType>(instruction.value)};
1147 result = target_check->type() == value ||
1148 (value == JSON::Type::Integer && target_check->is_integral());
1149 EVALUATE_END(AssertionPropertyType);
1152INSTRUCTION_HANDLER(AssertionPropertyTypeEvaluate) {
1153 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeEvaluate);
1155 const auto value{assume_value_copy<ValueType>(instruction.value)};
1158 result = target_check->type() == value ||
1159 (value == JSON::Type::Integer && target_check->is_integral());
1162 context.evaluator->evaluate(target_check);
1165 EVALUATE_END(AssertionPropertyTypeEvaluate);
1168INSTRUCTION_HANDLER(AssertionPropertyTypeStrict) {
1169 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeStrict);
1171 const auto value{assume_value_copy<ValueType>(instruction.value)};
1172 result = effective_type_strict_real(*target_check) == value;
1173 EVALUATE_END(AssertionPropertyTypeStrict);
1176INSTRUCTION_HANDLER(AssertionPropertyTypeStrictEvaluate) {
1177 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeStrictEvaluate);
1179 const auto value{assume_value_copy<ValueType>(instruction.value)};
1180 result = effective_type_strict_real(*target_check) == value;
1183 context.evaluator->evaluate(target_check);
1186 EVALUATE_END(AssertionPropertyTypeStrictEvaluate);
1189INSTRUCTION_HANDLER(AssertionPropertyEqual) {
1190 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyEqual);
1191 const auto &value{assume_value<ValueJSON>(instruction.value)};
1193 result = DIRECT(AssertionEqual, *target_check, value);
1194 EVALUATE_END(AssertionPropertyEqual);
1197INSTRUCTION_HANDLER(AssertionPropertyTypeStrictAny) {
1198 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeStrictAny);
1199 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
1200 assert(value.any());
1202 result = type_set_test(value, effective_type_strict_real(*target_check));
1203 EVALUATE_END(AssertionPropertyTypeStrictAny);
1206INSTRUCTION_HANDLER(AssertionPropertyTypeStrictAnyEvaluate) {
1207 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeStrictAnyEvaluate);
1208 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
1209 assert(value.any());
1211 result = type_set_test(value, effective_type_strict_real(*target_check));
1214 context.evaluator->evaluate(target_check);
1217 EVALUATE_END(AssertionPropertyTypeStrictAnyEvaluate);
1220INSTRUCTION_HANDLER(AssertionObjectPropertiesSimple) {
1221 EVALUATE_BEGIN_NON_STRING(AssertionObjectPropertiesSimple,
true);
1222 if (!target.is_object()) {
1223 EVALUATE_END(AssertionObjectPropertiesSimple);
1226 const auto &value{assume_value<ValueObjectProperties>(instruction.value)};
1227 assert(value.size() >= instruction.children.size());
1228 assert(value.size() <= 32);
1229 static constexpr sourcemeta::core::PropertyHashJSON<ValueString>
1231 const auto &
object{target.as_object()};
1232 const auto schema_size{value.size()};
1233 std::uint32_t seen{0};
1236 for (
const auto &instance_entry :
object) {
1237 const auto &instance_hash{instance_entry.hash};
1238 for (std::size_t schema_index = 0; schema_index < schema_size;
1240 const auto &schema_hash{std::get<1>(value[schema_index])};
1243 if (schema_hash == instance_hash &&
1244 (PROPERTY_HASHER.is_perfect(instance_hash) ||
1245 instance_entry.first == std::get<0>(value[schema_index]))) {
1246 seen |= (
static_cast<std::uint32_t
>(1) << schema_index);
1247 if (schema_index < instruction.children.size()) {
1248 const auto &child{instruction.children[schema_index]};
1249 const auto &property_value{instance_entry.second};
1250 bool child_ok{
false};
1253#define CHILD_DIRECT(name, accessor, value_type) \
1256 DIRECT(name, property_value, accessor<value_type>(child.value)); \
1259 switch (child.type) {
1260 using enum InstructionIndex;
1261 CHILD_DIRECT(AssertionTypeStrict, assume_value_copy,
ValueType);
1262 CHILD_DIRECT(AssertionType, assume_value_copy,
ValueType);
1263 CHILD_DIRECT(AssertionTypeStrictAny, assume_value,
ValueTypes);
1264 CHILD_DIRECT(AssertionTypeAny, assume_value,
ValueTypes);
1265 CHILD_DIRECT(AssertionTypeStringBounded, assume_value,
ValueRange);
1266 CHILD_DIRECT(AssertionEqual, assume_value,
ValueJSON);
1267 CHILD_DIRECT(AssertionEqualsAny, assume_value,
ValueSet);
1268 CHILD_DIRECT(AssertionEqualsAnyStringHash, assume_value,
1270 CHILD_DIRECT(AssertionTypeIntegerBounded, assume_value_copy,
1272 CHILD_DIRECT(AssertionTypeIntegerBoundedStrict, assume_value_copy,
1274 CHILD_DIRECT(AssertionTypeIntegerLowerBound, assume_value_copy,
1276 CHILD_DIRECT(AssertionTypeIntegerLowerBoundStrict,
1278 CHILD_DIRECT(LoopItemsIntegerBounded, assume_value_copy,
1281 child_ok = evaluate_instruction_without_callback(
1282 child, property_value, depth + 1, context);
1289 if (!child_ok) [[unlikely]] {
1291 EVALUATE_END(AssertionObjectPropertiesSimple);
1301 const auto all_seen{schema_size == 32
1303 : (std::uint32_t{1} << schema_size) - 1};
1304 if (seen != all_seen) [[unlikely]] {
1305 for (std::size_t schema_index = 0; schema_index < schema_size;
1307 if (std::get<2>(value[schema_index]) &&
1308 !(seen & (
static_cast<std::uint32_t
>(1) << schema_index))) {
1310 EVALUATE_END(AssertionObjectPropertiesSimple);
1315 EVALUATE_END(AssertionObjectPropertiesSimple);
1318INSTRUCTION_HANDLER(AssertionPropertyEqualsAnyStringHash) {
1319 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyEqualsAnyStringHash);
1320 const auto &value{assume_value<ValueStringHashes>(instruction.value)};
1322 result = DIRECT(AssertionEqualsAnyStringHash, *target_check, value);
1323 EVALUATE_END(AssertionPropertyEqualsAnyStringHash);
1326INSTRUCTION_HANDLER(AssertionArrayPrefix) {
1327 EVALUATE_BEGIN_NON_STRING(AssertionArrayPrefix, target.is_array());
1329 assert(!instruction.children.empty());
1330 result = target.empty();
1331 const auto prefixes{instruction.children.size() - 1};
1332 const auto array_size{target.array_size()};
1333 if (!result) [[likely]] {
1335 array_size == prefixes ? prefixes : std::min(array_size, prefixes) - 1};
1336 const auto &entry{instruction.children[pointer]};
1338 assert(entry.type == sourcemeta::blaze::InstructionIndex::ControlGroup);
1339 SOURCEMETA_ASSUME(entry.type ==
1340 sourcemeta::blaze::InstructionIndex::ControlGroup);
1341 for (
const auto &child : entry.children) {
1342 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1349 EVALUATE_END(AssertionArrayPrefix);
1352INSTRUCTION_HANDLER(AssertionArrayPrefixEvaluate) {
1353 EVALUATE_BEGIN_NON_STRING(AssertionArrayPrefixEvaluate, target.is_array());
1355 assert(!instruction.children.empty());
1356 result = target.empty();
1357 const auto prefixes{instruction.children.size() - 1};
1358 const auto array_size{target.array_size()};
1359 if (!result) [[likely]] {
1361 array_size == prefixes ? prefixes : std::min(array_size, prefixes) - 1};
1362 const auto &entry{instruction.children[pointer]};
1364 assert(entry.type == sourcemeta::blaze::InstructionIndex::ControlGroup);
1365 SOURCEMETA_ASSUME(entry.type ==
1366 sourcemeta::blaze::InstructionIndex::ControlGroup);
1367 for (
const auto &child : entry.children) {
1368 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1370 EVALUATE_END(AssertionArrayPrefixEvaluate);
1375 SOURCEMETA_ASSUME(result);
1376 if (array_size == prefixes) {
1377 context.evaluator->evaluate(&target);
1379 for (std::size_t cursor = 0; cursor <= pointer; cursor++) {
1380 context.evaluator->evaluate(&target.at(cursor));
1385 EVALUATE_END(AssertionArrayPrefixEvaluate);
1395template <
bool HasCallback>
1396SOURCEMETA_FORCEINLINE
inline auto
1397evaluate_branch(
const sourcemeta::blaze::Instruction &branch,
1398 const sourcemeta::core::JSON &target,
const std::uint64_t depth,
1400 if (branch.type != InstructionIndex::ControlGroup) [[unlikely]] {
1401 return evaluate_instruction(branch, target, depth, context);
1404 auto step{branch.children.cbegin()};
1405 const auto last{branch.children.cend()};
1407 if constexpr (!HasCallback) {
1408 if (step != last && target.is_object() &&
1409 step->relative_instance_location.size() == 1 &&
1410 step->relative_instance_location.back().is_property()) {
1411 const auto &token{step->relative_instance_location.back()};
1412 if (step->type == InstructionIndex::AssertionPropertyEqual) {
1414 target.try_at(token.to_property(), token.property_hash())};
1415 if (tag !=
nullptr && !DIRECT(AssertionEqual, *tag,
1416 assume_value<ValueJSON>(step->value))) {
1421 }
else if (step->type ==
1422 InstructionIndex::AssertionPropertyEqualsAnyStringHash) {
1424 target.try_at(token.to_property(), token.property_hash())};
1425 if (tag !=
nullptr &&
1426 !DIRECT(AssertionEqualsAnyStringHash, *tag,
1427 assume_value<ValueStringHashes>(step->value))) {
1436 for (; step != last; ++step) {
1437 if (!evaluate_instruction(*step, target, depth + 1, context)) [[unlikely]] {
1445INSTRUCTION_HANDLER(LogicalOr) {
1446 EVALUATE_BEGIN_NO_PRECONDITION(LogicalOr);
1447 result = instruction.children.empty();
1449 resolve_instance(instance, instruction.relative_instance_location)};
1450 const auto value{assume_value_copy<ValueBoolean>(instruction.value)};
1454 for (
const auto &child : instruction.children) {
1455 if (evaluate_branch(child, target, depth + 1, context)) {
1460 for (
const auto &child : instruction.children) {
1461 if (evaluate_branch(child, target, depth + 1, context)) {
1468 EVALUATE_END(LogicalOr);
1471INSTRUCTION_HANDLER(LogicalOrEvaluate) {
1472 EVALUATE_BEGIN_NO_PRECONDITION(LogicalOrEvaluate);
1473 result = instruction.children.empty();
1475 resolve_instance(instance, instruction.relative_instance_location)};
1476 const auto value{assume_value_copy<ValueBoolean>(instruction.value)};
1480 for (
const auto &child : instruction.children) {
1483 const auto checkpoint{context.evaluator->checkpoint()};
1484 if (evaluate_branch(child, target, depth + 1, context)) {
1487 context.evaluator->rewind(checkpoint);
1491 for (
const auto &child : instruction.children) {
1492 const auto checkpoint{context.evaluator->checkpoint()};
1493 if (evaluate_branch(child, target, depth + 1, context)) {
1498 context.evaluator->rewind(checkpoint);
1502 EVALUATE_END(LogicalOrEvaluate);
1505INSTRUCTION_HANDLER(LogicalAnd) {
1506 EVALUATE_BEGIN_NO_PRECONDITION(LogicalAnd);
1509 resolve_instance(instance, instruction.relative_instance_location)};
1510 for (
const auto &child : instruction.children) {
1511 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1517 EVALUATE_END(LogicalAnd);
1520INSTRUCTION_HANDLER(LogicalWhenType) {
1521 const auto value{assume_value_copy<ValueType>(instruction.value)};
1525 assert(value != JSON::Type::Integer);
1526 SOURCEMETA_ASSUME(value != JSON::Type::Integer);
1527 assert(value != JSON::Type::Real);
1528 SOURCEMETA_ASSUME(value != JSON::Type::Real);
1529 assert(value != JSON::Type::Decimal);
1530 SOURCEMETA_ASSUME(value != JSON::Type::Decimal);
1532 EVALUATE_BEGIN(LogicalWhenType, target.type() == value);
1534 for (
const auto &child : instruction.children) {
1535 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1541 EVALUATE_END(LogicalWhenType);
1544INSTRUCTION_HANDLER(LogicalWhenDefines) {
1545 const auto &value{assume_value<ValueProperty>(instruction.value)};
1546 EVALUATE_BEGIN_NON_STRING(LogicalWhenDefines,
1547 target.is_object() &&
1548 target.defines(value.first, value.second));
1550 for (
const auto &child : instruction.children) {
1551 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1557 EVALUATE_END(LogicalWhenDefines);
1560INSTRUCTION_HANDLER(LogicalWhenArraySizeGreater) {
1561 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
1562 EVALUATE_BEGIN_NON_STRING(LogicalWhenArraySizeGreater,
1563 target.is_array() && target.array_size() > value);
1565 for (
const auto &child : instruction.children) {
1566 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1572 EVALUATE_END(LogicalWhenArraySizeGreater);
1575INSTRUCTION_HANDLER(LogicalXor) {
1576 EVALUATE_BEGIN_NO_PRECONDITION(LogicalXor);
1578 bool has_matched{
false};
1580 resolve_instance(instance, instruction.relative_instance_location)};
1581 const auto value{assume_value_copy<ValueBoolean>(instruction.value)};
1582 for (
const auto &child : instruction.children) {
1583 if (evaluate_branch(child, target, depth + 1, context)) {
1584 if (has_matched) [[unlikely]] {
1596 result = result && has_matched;
1597 EVALUATE_END(LogicalXor);
1600INSTRUCTION_HANDLER(LogicalXorEvaluate) {
1601 EVALUATE_BEGIN_NO_PRECONDITION(LogicalXorEvaluate);
1603 bool has_matched{
false};
1605 resolve_instance(instance, instruction.relative_instance_location)};
1606 const auto value{assume_value_copy<ValueBoolean>(instruction.value)};
1607 for (
const auto &child : instruction.children) {
1610 const auto checkpoint{context.evaluator->checkpoint()};
1611 if (evaluate_branch(child, target, depth + 1, context)) {
1612 if (has_matched) [[unlikely]] {
1622 context.evaluator->rewind(checkpoint);
1626 result = result && has_matched;
1627 EVALUATE_END(LogicalXorEvaluate);
1630INSTRUCTION_HANDLER(LogicalCondition) {
1631 EVALUATE_BEGIN_NO_PRECONDITION(LogicalCondition);
1633 const auto value{assume_value_copy<ValueIndexPair>(instruction.value)};
1634 const auto children_size{instruction.children.size()};
1635 assert(children_size >= value.first);
1636 SOURCEMETA_ASSUME(children_size >= value.first);
1637 assert(children_size >= value.second);
1638 SOURCEMETA_ASSUME(children_size >= value.second);
1644 resolve_instance(instance, instruction.relative_instance_location)};
1646 for (std::size_t cursor = 0; cursor < value.first; cursor++) {
1647 if (!EVALUATE_RECURSE(instruction.children[cursor], target)) {
1655 std::size_t consequence_start;
1656 std::size_t consequence_end;
1658 consequence_start = value.first;
1659 consequence_end = value.second > 0 ? value.second : children_size;
1661 consequence_start = value.second;
1662 consequence_end = value.second > 0 ? children_size : value.second;
1666 if (consequence_start < consequence_end) {
1667 if constexpr (HasCallback) {
1668 context.evaluator->evaluate_path.pop_back(
1669 context.schema->extra[instruction.extra_index]
1670 .relative_schema_location.size());
1673 for (
auto cursor = consequence_start; cursor < consequence_end; cursor++) {
1674 if (!EVALUATE_RECURSE(instruction.children[cursor], target)) {
1680 if constexpr (HasCallback) {
1681 context.evaluator->evaluate_path.push_back(
1682 context.schema->extra[instruction.extra_index]
1683 .relative_schema_location);
1687 EVALUATE_END(LogicalCondition);
1690INSTRUCTION_HANDLER(LogicalConditionEvaluate) {
1691 EVALUATE_BEGIN_NO_PRECONDITION(LogicalConditionEvaluate);
1693 const auto value{assume_value_copy<ValueIndexPair>(instruction.value)};
1694 const auto children_size{instruction.children.size()};
1695 assert(children_size >= value.first);
1696 SOURCEMETA_ASSUME(children_size >= value.first);
1697 assert(children_size >= value.second);
1698 SOURCEMETA_ASSUME(children_size >= value.second);
1704 resolve_instance(instance, instruction.relative_instance_location)};
1708 const auto checkpoint{context.evaluator->checkpoint()};
1710 for (std::size_t cursor = 0; cursor < value.first; cursor++) {
1711 if (!EVALUATE_RECURSE(instruction.children[cursor], target)) {
1718 context.evaluator->rewind(checkpoint);
1723 std::size_t consequence_start;
1724 std::size_t consequence_end;
1726 consequence_start = value.first;
1727 consequence_end = value.second > 0 ? value.second : children_size;
1729 consequence_start = value.second;
1730 consequence_end = value.second > 0 ? children_size : value.second;
1734 if (consequence_start < consequence_end) {
1735 if constexpr (HasCallback) {
1736 context.evaluator->evaluate_path.pop_back(
1737 context.schema->extra[instruction.extra_index]
1738 .relative_schema_location.size());
1741 for (
auto cursor = consequence_start; cursor < consequence_end; cursor++) {
1742 if (!EVALUATE_RECURSE(instruction.children[cursor], target)) {
1748 if constexpr (HasCallback) {
1749 context.evaluator->evaluate_path.push_back(
1750 context.schema->extra[instruction.extra_index]
1751 .relative_schema_location);
1755 EVALUATE_END(LogicalConditionEvaluate);
1758INSTRUCTION_HANDLER(ControlGroup) {
1759 EVALUATE_BEGIN_PASS_THROUGH(ControlGroup);
1760 for (
const auto &child : instruction.children) {
1761 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1767 EVALUATE_END_PASS_THROUGH(ControlGroup);
1770INSTRUCTION_HANDLER(ControlGroupWhenDefines) {
1771 EVALUATE_BEGIN_PASS_THROUGH(ControlGroupWhenDefines);
1772 assert(!instruction.children.empty());
1774 assert(!instruction.relative_instance_location.empty());
1776 resolve_instance(instance, instruction.relative_instance_location)};
1777 const auto &value{assume_value<ValueProperty>(instruction.value)};
1778 if (target.is_object() && target.defines(value.first, value.second)) {
1779 for (
const auto &child : instruction.children) {
1782 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1789 EVALUATE_END_PASS_THROUGH(ControlGroupWhenDefines);
1792INSTRUCTION_HANDLER(ControlGroupWhenDefinesDirect) {
1793 EVALUATE_BEGIN_PASS_THROUGH(ControlGroupWhenDefinesDirect);
1794 assert(!instruction.children.empty());
1795 assert(instruction.relative_instance_location.empty());
1796 const auto &value{assume_value<ValueProperty>(instruction.value)};
1798 if (instance.is_object() && instance.defines(value.first, value.second)) {
1799 for (
const auto &child : instruction.children) {
1800 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1807 EVALUATE_END_PASS_THROUGH(ControlGroupWhenDefinesDirect);
1810INSTRUCTION_HANDLER(ControlGroupWhenType) {
1811 EVALUATE_BEGIN_PASS_THROUGH(ControlGroupWhenType);
1812 assert(!instruction.children.empty());
1813 assert(instruction.relative_instance_location.empty());
1814 const auto value{assume_value_copy<ValueType>(instruction.value)};
1818 assert(value != JSON::Type::Integer);
1819 SOURCEMETA_ASSUME(value != JSON::Type::Integer);
1820 assert(value != JSON::Type::Real);
1821 SOURCEMETA_ASSUME(value != JSON::Type::Real);
1822 assert(value != JSON::Type::Decimal);
1823 SOURCEMETA_ASSUME(value != JSON::Type::Decimal);
1825 if (instance.type() == value) [[likely]] {
1826 for (
const auto &child : instruction.children) {
1827 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1834 EVALUATE_END_PASS_THROUGH(ControlGroupWhenType);
1837INSTRUCTION_HANDLER(ControlEvaluate) {
1838 EVALUATE_BEGIN_PASS_THROUGH(ControlEvaluate);
1839 const auto &value{assume_value<ValuePointer>(instruction.value)};
1840 context.evaluator->evaluate(&get(instance, value));
1841 EVALUATE_END_PASS_THROUGH(ControlEvaluate);
1844INSTRUCTION_HANDLER(ControlFrame) {
1845 EVALUATE_BEGIN_PASS_THROUGH(ControlFrame);
1846 assert(!instruction.children.empty());
1847 const auto parent_frame{context.frame};
1848 context.frame = context.evaluator->checkpoint();
1850 for (
const auto &child : instruction.children) {
1851 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1857 context.frame = parent_frame;
1859 EVALUATE_END_PASS_THROUGH(ControlFrame);
1862INSTRUCTION_HANDLER(ControlResource) {
1863 EVALUATE_BEGIN_PASS_THROUGH(ControlResource);
1864 assert(!instruction.children.empty());
1865 const auto index{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
1867 assert(index < context.schema->bindings.size());
1868 const auto &bindings{context.schema->bindings[index]};
1869 context.evaluator->enter_resource(bindings);
1870 for (
const auto &child : instruction.children) {
1871 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1877 context.evaluator->leave_resource(bindings);
1878 EVALUATE_END_PASS_THROUGH(ControlResource);
1881INSTRUCTION_HANDLER(ControlDynamicAnchorJump) {
1882 EVALUATE_BEGIN_NO_PRECONDITION(ControlDynamicAnchorJump);
1885 resolve_instance(instance, instruction.relative_instance_location)};
1886 const auto &value{assume_value<ValueIndexPair>(instruction.value)};
1887 const auto destination{
1888 context.evaluator->dynamic_target(value.first, value.second)};
1889 assert(destination != 0);
1890 assert(destination - 1 < context.schema->targets.size());
1891 const auto &bindings{
1893 ->bindings[context.schema->target_bindings[destination - 1]]};
1894 context.evaluator->enter_resource(bindings);
1895 for (
const auto &child : context.schema->targets[destination - 1]) {
1896 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1902 context.evaluator->leave_resource(bindings);
1903 EVALUATE_END(ControlDynamicAnchorJump);
1906INSTRUCTION_HANDLER(ControlJump) {
1907 EVALUATE_BEGIN_NO_PRECONDITION(ControlJump);
1909 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
1910 assert(context.schema->targets.size() > value);
1911 const auto &target{resolve_target(
1912 context.property_target,
1913 resolve_instance(instance, instruction.relative_instance_location))};
1914 for (
const auto &child : context.schema->targets[value]) {
1915 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1921 EVALUATE_END(ControlJump);
1924INSTRUCTION_HANDLER(ControlResourceJump) {
1925 EVALUATE_BEGIN_NO_PRECONDITION(ControlResourceJump);
1927 const auto &value{assume_value<ValueIndexPair>(instruction.value)};
1928 assert(context.schema->targets.size() > value.first);
1929 assert(value.second != 0);
1930 assert(value.second < context.schema->bindings.size());
1931 const auto &target{resolve_target(
1932 context.property_target,
1933 resolve_instance(instance, instruction.relative_instance_location))};
1934 const auto &bindings{context.schema->bindings[value.second]};
1935 context.evaluator->enter_resource(bindings);
1936 for (
const auto &child : context.schema->targets[value.first]) {
1937 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1943 context.evaluator->leave_resource(bindings);
1944 EVALUATE_END(ControlResourceJump);
1947INSTRUCTION_HANDLER(AnnotationEmit) {
1948 const auto &value{assume_value<ValueJSON>(instruction.value)};
1949 EVALUATE_ANNOTATION(AnnotationEmit, context.evaluator->instance_location,
1953INSTRUCTION_HANDLER(AnnotationEmitWrapped) {
1954 const auto &value{assume_value<ValueJSON>(instruction.value)};
1955 EVALUATE_ANNOTATION(AnnotationEmitWrapped,
1956 context.evaluator->instance_location, value);
1959INSTRUCTION_HANDLER(AnnotationToParent) {
1960 const auto &value{assume_value<ValueJSON>(instruction.value)};
1961 EVALUATE_ANNOTATION(
1964 context.evaluator->instance_location.initial(), value);
1967INSTRUCTION_HANDLER(AnnotationBasenameToParent) {
1968 EVALUATE_ANNOTATION(
1969 AnnotationBasenameToParent,
1971 context.evaluator->instance_location.initial(),
1972 context.evaluator->instance_location.back().to_json());
1975INSTRUCTION_HANDLER(Evaluate) {
1976 EVALUATE_BEGIN_NO_PRECONDITION(Evaluate);
1978 resolve_instance(instance, instruction.relative_instance_location)};
1979 context.evaluator->evaluate(&target);
1981 EVALUATE_END(Evaluate);
1984INSTRUCTION_HANDLER(LogicalNot) {
1985 EVALUATE_BEGIN_NO_PRECONDITION(LogicalNot);
1988 resolve_instance(instance, instruction.relative_instance_location)};
1989 for (
const auto &child : instruction.children) {
1990 if (!EVALUATE_RECURSE(child, target)) [[likely]] {
1996 EVALUATE_END(LogicalNot);
1999INSTRUCTION_HANDLER(LogicalNotEvaluate) {
2000 EVALUATE_BEGIN_NO_PRECONDITION(LogicalNotEvaluate);
2002 const auto checkpoint{context.evaluator->checkpoint()};
2005 resolve_instance(instance, instruction.relative_instance_location)};
2006 for (
const auto &child : instruction.children) {
2007 if (!EVALUATE_RECURSE(child, target)) [[likely]] {
2013 context.evaluator->rewind(checkpoint);
2015 EVALUATE_END(LogicalNotEvaluate);
2018INSTRUCTION_HANDLER(LoopPropertiesUnevaluated) {
2019 EVALUATE_BEGIN_NON_STRING(LoopPropertiesUnevaluated, target.is_object());
2020 assert(!instruction.children.empty());
2023 if (!context.evaluator->is_evaluated(&target, context.frame)) {
2024 for (
const auto &entry : target.as_object()) {
2025 if (context.evaluator->is_evaluated(&entry.second, context.frame)) {
2029 if constexpr (HasCallback) {
2030 context.evaluator->instance_location.push_back(entry.first);
2032 for (
const auto &child : instruction.children) {
2033 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2035 if constexpr (HasCallback) {
2036 context.evaluator->instance_location.pop_back();
2038 EVALUATE_END(LoopPropertiesUnevaluated);
2042 if constexpr (HasCallback) {
2043 context.evaluator->instance_location.pop_back();
2047 context.evaluator->evaluate(&target);
2050 EVALUATE_END(LoopPropertiesUnevaluated);
2053INSTRUCTION_HANDLER(LoopPropertiesUnevaluatedExcept) {
2054 EVALUATE_BEGIN_NON_STRING(LoopPropertiesUnevaluatedExcept,
2055 target.is_object());
2056 assert(!instruction.children.empty());
2057 const auto &value{assume_value<ValuePropertyFilter>(instruction.value)};
2058 const auto &[filter_strings, filter_prefixes, filter_regexes] = value;
2061 assert(!filter_strings.empty() || !filter_prefixes.empty() ||
2062 !filter_regexes.empty());
2064 if (!context.evaluator->is_evaluated(&target, context.frame)) {
2065 for (
const auto &entry : target.as_object()) {
2066 if (filter_strings.contains(entry.first, entry.hash)) {
2070 if (std::ranges::any_of(filter_prefixes,
2071 [&entry](
const auto &prefix) ->
bool {
2072 return entry.first.starts_with(prefix);
2077 if (std::ranges::any_of(filter_regexes,
2078 [&entry](
const auto &pattern) ->
bool {
2079 return matches(pattern.first, entry.first);
2084 if (context.evaluator->is_evaluated(&entry.second, context.frame)) {
2088 if constexpr (HasCallback) {
2089 context.evaluator->instance_location.push_back(entry.first);
2091 for (
const auto &child : instruction.children) {
2092 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2094 if constexpr (HasCallback) {
2095 context.evaluator->instance_location.pop_back();
2097 EVALUATE_END(LoopPropertiesUnevaluatedExcept);
2101 if constexpr (HasCallback) {
2102 context.evaluator->instance_location.pop_back();
2106 context.evaluator->evaluate(&target);
2109 EVALUATE_END(LoopPropertiesUnevaluatedExcept);
2112INSTRUCTION_HANDLER(LoopPropertiesMatch) {
2113 EVALUATE_BEGIN_NON_STRING(LoopPropertiesMatch, target.is_object());
2114 const auto &value{assume_value<ValueNamedIndexes>(instruction.value)};
2115 assert(!value.first.empty());
2127 const auto direct_children{!HasCallback && depth < DEPTH_LIMIT};
2128 for (
const auto &entry : target.as_object()) {
2129 const auto *match{named_index(value, entry.first, entry.hash)};
2134 const auto &subinstruction{instruction.children[std::get<1>(*match)]};
2135 assert(subinstruction.type ==
2136 sourcemeta::blaze::InstructionIndex::ControlGroup);
2137 for (
const auto &child : subinstruction.children) {
2142 if (direct_children && child.relative_instance_location.size() == 1)
2144 switch (child.type) {
2145 case InstructionIndex::AssertionPropertyTypeStrict:
2146 child_ok = DIRECT(AssertionTypeStrict, entry.second,
2147 assume_value_copy<ValueType>(child.value));
2149 case InstructionIndex::AssertionPropertyType:
2150 child_ok = DIRECT(AssertionType, entry.second,
2151 assume_value_copy<ValueType>(child.value));
2153 case InstructionIndex::AssertionPropertyTypeStrictAny:
2154 child_ok = DIRECT(AssertionTypeStrictAny, entry.second,
2155 assume_value<ValueTypes>(child.value));
2157 case InstructionIndex::AssertionTypeAny:
2158 child_ok = DIRECT(AssertionTypeAny, entry.second,
2159 assume_value<ValueTypes>(child.value));
2161 case InstructionIndex::AssertionTypeStringBounded:
2162 child_ok = DIRECT(AssertionTypeStringBounded, entry.second,
2163 assume_value<ValueRange>(child.value));
2165 case InstructionIndex::AssertionTypeArrayBounded:
2166 child_ok = DIRECT(AssertionTypeArrayBounded, entry.second,
2167 assume_value<ValueRange>(child.value));
2169 case InstructionIndex::AssertionEqual:
2170 child_ok = DIRECT(AssertionEqual, entry.second,
2171 assume_value<ValueJSON>(child.value));
2173 case InstructionIndex::AssertionEqualsAny:
2174 child_ok = DIRECT(AssertionEqualsAny, entry.second,
2175 assume_value<ValueSet>(child.value));
2177 case InstructionIndex::AssertionEqualsAnyStringHash:
2178 child_ok = DIRECT(AssertionEqualsAnyStringHash, entry.second,
2179 assume_value<ValueStringHashes>(child.value));
2182 child_ok = EVALUATE_RECURSE(child, target);
2186 child_ok = EVALUATE_RECURSE(child, target);
2189 if (!child_ok) [[unlikely]] {
2191 EVALUATE_END(LoopPropertiesMatch);
2196 EVALUATE_END(LoopPropertiesMatch);
2199INSTRUCTION_HANDLER(LoopPropertiesMatchRequired) {
2200 EVALUATE_BEGIN_NO_PRECONDITION(LoopPropertiesMatchRequired);
2202 resolve_instance(instance, instruction.relative_instance_location)};
2203 const auto &value{assume_value<ValueNamedIndexesRequired>(instruction.value)};
2204 assert(!value.first.first.empty());
2205 assert(value.second > 0);
2211 if (!target.is_object()) [[unlikely]] {
2212 EVALUATE_END(LoopPropertiesMatchRequired);
2223 const auto direct_children{!HasCallback && depth < DEPTH_LIMIT};
2224 for (
const auto &entry : target.as_object()) {
2225 const auto *match{named_index(value.first, entry.first, entry.hash)};
2230 if (std::get<3>(*match)) {
2234 const auto &subinstruction{instruction.children[std::get<1>(*match)]};
2235 assert(subinstruction.type ==
2236 sourcemeta::blaze::InstructionIndex::ControlGroup);
2237 for (
const auto &child : subinstruction.children) {
2242 if (direct_children && child.relative_instance_location.size() == 1)
2244 switch (child.type) {
2245 case InstructionIndex::AssertionPropertyTypeStrict:
2246 child_ok = DIRECT(AssertionTypeStrict, entry.second,
2247 assume_value_copy<ValueType>(child.value));
2249 case InstructionIndex::AssertionPropertyType:
2250 child_ok = DIRECT(AssertionType, entry.second,
2251 assume_value_copy<ValueType>(child.value));
2253 case InstructionIndex::AssertionPropertyTypeStrictAny:
2254 child_ok = DIRECT(AssertionTypeStrictAny, entry.second,
2255 assume_value<ValueTypes>(child.value));
2257 case InstructionIndex::AssertionTypeAny:
2258 child_ok = DIRECT(AssertionTypeAny, entry.second,
2259 assume_value<ValueTypes>(child.value));
2261 case InstructionIndex::AssertionTypeStringBounded:
2262 child_ok = DIRECT(AssertionTypeStringBounded, entry.second,
2263 assume_value<ValueRange>(child.value));
2265 case InstructionIndex::AssertionTypeArrayBounded:
2266 child_ok = DIRECT(AssertionTypeArrayBounded, entry.second,
2267 assume_value<ValueRange>(child.value));
2269 case InstructionIndex::AssertionEqual:
2270 child_ok = DIRECT(AssertionEqual, entry.second,
2271 assume_value<ValueJSON>(child.value));
2273 case InstructionIndex::AssertionEqualsAny:
2274 child_ok = DIRECT(AssertionEqualsAny, entry.second,
2275 assume_value<ValueSet>(child.value));
2277 case InstructionIndex::AssertionEqualsAnyStringHash:
2278 child_ok = DIRECT(AssertionEqualsAnyStringHash, entry.second,
2279 assume_value<ValueStringHashes>(child.value));
2282 child_ok = EVALUATE_RECURSE(child, target);
2286 child_ok = EVALUATE_RECURSE(child, target);
2289 if (!child_ok) [[unlikely]] {
2291 EVALUATE_END(LoopPropertiesMatchRequired);
2296 result = matched == value.second;
2298 EVALUATE_END(LoopPropertiesMatchRequired);
2301INSTRUCTION_HANDLER(LoopPropertiesMatchClosed) {
2302 EVALUATE_BEGIN_NON_STRING(LoopPropertiesMatchClosed, target.is_object());
2303 const auto &value{assume_value<ValueNamedIndexes>(instruction.value)};
2304 assert(!value.first.empty());
2316 const auto direct_children{!HasCallback && depth < DEPTH_LIMIT};
2317 for (
const auto &entry : target.as_object()) {
2318 const auto *match{named_index(value, entry.first, entry.hash)};
2319 if (!match) [[unlikely]] {
2324 const auto &subinstruction{instruction.children[std::get<1>(*match)]};
2325 assert(subinstruction.type ==
2326 sourcemeta::blaze::InstructionIndex::ControlGroup);
2327 for (
const auto &child : subinstruction.children) {
2332 if (direct_children && child.relative_instance_location.size() == 1)
2334 switch (child.type) {
2335 case InstructionIndex::AssertionPropertyTypeStrict:
2336 child_ok = DIRECT(AssertionTypeStrict, entry.second,
2337 assume_value_copy<ValueType>(child.value));
2339 case InstructionIndex::AssertionPropertyType:
2340 child_ok = DIRECT(AssertionType, entry.second,
2341 assume_value_copy<ValueType>(child.value));
2343 case InstructionIndex::AssertionPropertyTypeStrictAny:
2344 child_ok = DIRECT(AssertionTypeStrictAny, entry.second,
2345 assume_value<ValueTypes>(child.value));
2347 case InstructionIndex::AssertionTypeAny:
2348 child_ok = DIRECT(AssertionTypeAny, entry.second,
2349 assume_value<ValueTypes>(child.value));
2351 case InstructionIndex::AssertionTypeStringBounded:
2352 child_ok = DIRECT(AssertionTypeStringBounded, entry.second,
2353 assume_value<ValueRange>(child.value));
2355 case InstructionIndex::AssertionTypeArrayBounded:
2356 child_ok = DIRECT(AssertionTypeArrayBounded, entry.second,
2357 assume_value<ValueRange>(child.value));
2359 case InstructionIndex::AssertionEqual:
2360 child_ok = DIRECT(AssertionEqual, entry.second,
2361 assume_value<ValueJSON>(child.value));
2363 case InstructionIndex::AssertionEqualsAny:
2364 child_ok = DIRECT(AssertionEqualsAny, entry.second,
2365 assume_value<ValueSet>(child.value));
2367 case InstructionIndex::AssertionEqualsAnyStringHash:
2368 child_ok = DIRECT(AssertionEqualsAnyStringHash, entry.second,
2369 assume_value<ValueStringHashes>(child.value));
2372 child_ok = EVALUATE_RECURSE(child, target);
2376 child_ok = EVALUATE_RECURSE(child, target);
2379 if (!child_ok) [[unlikely]] {
2381 EVALUATE_END(LoopPropertiesMatchClosed);
2386 EVALUATE_END(LoopPropertiesMatchClosed);
2389INSTRUCTION_HANDLER(LoopProperties) {
2390 EVALUATE_BEGIN_NON_STRING(LoopProperties, target.is_object());
2391 assert(!instruction.children.empty());
2393 for (
const auto &entry : target.as_object()) {
2394 if constexpr (HasCallback) {
2395 context.evaluator->instance_location.push_back(entry.first);
2398 for (
const auto &child : instruction.children) {
2399 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2402 if constexpr (HasCallback) {
2403 context.evaluator->instance_location.pop_back();
2406 EVALUATE_END(LoopProperties);
2410 if constexpr (HasCallback) {
2411 context.evaluator->instance_location.pop_back();
2415 EVALUATE_END(LoopProperties);
2418INSTRUCTION_HANDLER(LoopPropertiesEvaluate) {
2419 EVALUATE_BEGIN_NON_STRING(LoopPropertiesEvaluate, target.is_object());
2423 for (
const auto &entry : target.as_object()) {
2424 if constexpr (HasCallback) {
2425 context.evaluator->instance_location.push_back(entry.first);
2428 for (
const auto &child : instruction.children) {
2429 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2432 if constexpr (HasCallback) {
2433 context.evaluator->instance_location.pop_back();
2436 EVALUATE_END(LoopPropertiesEvaluate);
2440 if constexpr (HasCallback) {
2441 context.evaluator->instance_location.pop_back();
2445 context.evaluator->evaluate(&target);
2446 EVALUATE_END(LoopPropertiesEvaluate);
2449INSTRUCTION_HANDLER(LoopPropertiesRegex) {
2450 EVALUATE_BEGIN_NON_STRING(LoopPropertiesRegex, target.is_object());
2451 assert(!instruction.children.empty());
2452 const auto &value{assume_value<ValueRegex>(instruction.value)};
2454 for (
const auto &entry : target.as_object()) {
2455 if (!matches(value.first, entry.first)) {
2459 if constexpr (HasCallback) {
2460 context.evaluator->instance_location.push_back(entry.first);
2463 for (
const auto &child : instruction.children) {
2464 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2467 if constexpr (HasCallback) {
2468 context.evaluator->instance_location.pop_back();
2471 EVALUATE_END(LoopPropertiesRegex);
2475 if constexpr (HasCallback) {
2476 context.evaluator->instance_location.pop_back();
2480 EVALUATE_END(LoopPropertiesRegex);
2483INSTRUCTION_HANDLER(LoopPropertiesRegexClosed) {
2484 EVALUATE_BEGIN_NON_STRING(LoopPropertiesRegexClosed, target.is_object());
2486 const auto &value{assume_value<ValueRegex>(instruction.value)};
2487 for (
const auto &entry : target.as_object()) {
2488 if (!matches(value.first, entry.first)) [[unlikely]] {
2493 if (instruction.children.empty()) {
2497 if constexpr (HasCallback) {
2498 context.evaluator->instance_location.push_back(entry.first);
2501 for (
const auto &child : instruction.children) {
2502 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2505 if constexpr (HasCallback) {
2506 context.evaluator->instance_location.pop_back();
2509 EVALUATE_END(LoopPropertiesRegexClosed);
2513 if constexpr (HasCallback) {
2514 context.evaluator->instance_location.pop_back();
2518 EVALUATE_END(LoopPropertiesRegexClosed);
2521INSTRUCTION_HANDLER(LoopPropertiesStartsWith) {
2522 EVALUATE_BEGIN_NON_STRING(LoopPropertiesStartsWith, target.is_object());
2523 assert(!instruction.children.empty());
2525 const auto &value{assume_value<ValueString>(instruction.value)};
2526 for (
const auto &entry : target.as_object()) {
2527 if (!entry.first.starts_with(value)) {
2531 if constexpr (HasCallback) {
2532 context.evaluator->instance_location.push_back(entry.first);
2535 for (
const auto &child : instruction.children) {
2536 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2539 if constexpr (HasCallback) {
2540 context.evaluator->instance_location.pop_back();
2543 EVALUATE_END(LoopPropertiesStartsWith);
2547 if constexpr (HasCallback) {
2548 context.evaluator->instance_location.pop_back();
2552 EVALUATE_END(LoopPropertiesStartsWith);
2555INSTRUCTION_HANDLER(LoopPropertiesExcept) {
2556 EVALUATE_BEGIN_NON_STRING(LoopPropertiesExcept, target.is_object());
2557 assert(!instruction.children.empty());
2559 const auto &value{assume_value<ValuePropertyFilter>(instruction.value)};
2560 const auto &[filter_strings, filter_prefixes, filter_regexes] = value;
2562 assert(!filter_strings.empty() || !filter_prefixes.empty() ||
2563 !filter_regexes.empty());
2565 for (
const auto &entry : target.as_object()) {
2566 if (filter_strings.contains(entry.first, entry.hash)) {
2570 if (std::ranges::any_of(filter_prefixes,
2571 [&entry](
const auto &prefix) ->
bool {
2572 return entry.first.starts_with(prefix);
2577 if (std::ranges::any_of(filter_regexes,
2578 [&entry](
const auto &pattern) ->
bool {
2579 return matches(pattern.first, entry.first);
2584 if constexpr (HasCallback) {
2585 context.evaluator->instance_location.push_back(entry.first);
2588 for (
const auto &child : instruction.children) {
2589 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2592 if constexpr (HasCallback) {
2593 context.evaluator->instance_location.pop_back();
2596 EVALUATE_END(LoopPropertiesExcept);
2600 if constexpr (HasCallback) {
2601 context.evaluator->instance_location.pop_back();
2605 EVALUATE_END(LoopPropertiesExcept);
2608INSTRUCTION_HANDLER(LoopPropertiesType) {
2609 EVALUATE_BEGIN_NON_STRING(LoopPropertiesType, target.is_object());
2611 const auto value{assume_value_copy<ValueType>(instruction.value)};
2612 for (
const auto &entry : target.as_object()) {
2613 if (entry.second.type() != value &&
2616 (value != JSON::Type::Integer || !entry.second.is_integral()))
2623 EVALUATE_END(LoopPropertiesType);
2626INSTRUCTION_HANDLER(LoopPropertiesTypeEvaluate) {
2627 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeEvaluate, target.is_object());
2629 const auto value{assume_value_copy<ValueType>(instruction.value)};
2630 for (
const auto &entry : target.as_object()) {
2631 if (entry.second.type() != value &&
2634 (value != JSON::Type::Integer || !entry.second.is_integral()))
2637 EVALUATE_END(LoopPropertiesTypeEvaluate);
2641 context.evaluator->evaluate(&target);
2642 EVALUATE_END(LoopPropertiesTypeEvaluate);
2645INSTRUCTION_HANDLER(LoopPropertiesExactlyTypeStrict) {
2646 EVALUATE_BEGIN_NO_PRECONDITION(LoopPropertiesExactlyTypeStrict);
2648 resolve_instance(instance, instruction.relative_instance_location)};
2649 const auto &value{assume_value<ValueTypedProperties>(instruction.value)};
2650 if (!target.is_object()) [[unlikely]] {
2651 EVALUATE_END(LoopPropertiesExactlyTypeStrict);
2654 const auto &
object{target.as_object()};
2655 if (
object.size() == value.second.size()) [[likely]] {
2657 assert(!value.second.empty());
2659 for (
const auto &entry :
object) {
2663 if (!value.second.contains(entry.first, entry.hash) ||
2664 effective_type_strict_real(entry.second) != value.first)
2672 EVALUATE_END(LoopPropertiesExactlyTypeStrict);
2675INSTRUCTION_HANDLER(LoopPropertiesExactlyTypeStrictHash) {
2676 EVALUATE_BEGIN_NO_PRECONDITION(LoopPropertiesExactlyTypeStrictHash);
2678 resolve_instance(instance, instruction.relative_instance_location)};
2680 const auto &value{assume_value<ValueTypedHashes>(instruction.value)};
2682 if (!target.is_object()) [[unlikely]] {
2683 EVALUATE_END(LoopPropertiesExactlyTypeStrictHash);
2686 const auto &
object{target.as_object()};
2687 const auto size{
object.size()};
2688 if (size == value.second.first.size()) [[likely]] {
2690 assert(!value.second.first.empty());
2696 std::size_t index{0};
2697 for (
const auto &entry :
object) {
2698 if (effective_type_strict_real(entry.second) != value.first) {
2699 EVALUATE_END(LoopPropertiesExactlyTypeStrictHash);
2704 if (entry.hash != value.second.first[index].first ||
2705 entry.first != value.second.first[index].second) {
2714 auto iterator =
object.cbegin();
2716 std::advance(iterator, index);
2717 for (; iterator !=
object.cend(); ++iterator) {
2718 if (effective_type_strict_real(iterator->second) != value.first) {
2724 if (std::ranges::none_of(value.second.first,
2725 [&iterator](
const auto &entry) ->
bool {
2726 return entry.first == iterator->hash &&
2727 entry.second == iterator->first;
2736 EVALUATE_END(LoopPropertiesExactlyTypeStrictHash);
2739INSTRUCTION_HANDLER(LoopPropertiesTypeStrict) {
2740 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeStrict, target.is_object());
2742 const auto value{assume_value_copy<ValueType>(instruction.value)};
2743 for (
const auto &entry : target.as_object()) {
2744 if (effective_type_strict_real(entry.second) != value) [[unlikely]] {
2750 EVALUATE_END(LoopPropertiesTypeStrict);
2753INSTRUCTION_HANDLER(LoopPropertiesTypeStrictEvaluate) {
2754 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeStrictEvaluate,
2755 target.is_object());
2757 const auto value{assume_value_copy<ValueType>(instruction.value)};
2758 for (
const auto &entry : target.as_object()) {
2759 if (effective_type_strict_real(entry.second) != value) [[unlikely]] {
2761 EVALUATE_END(LoopPropertiesTypeStrictEvaluate);
2765 context.evaluator->evaluate(&target);
2766 EVALUATE_END(LoopPropertiesTypeStrictEvaluate);
2769INSTRUCTION_HANDLER(LoopPropertiesTypeStrictAny) {
2770 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeStrictAny, target.is_object());
2772 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
2773 assert(value.any());
2774 result = type_set_spans_numbers(value)
2775 ? object_values_in_type_set<false>(target, value)
2776 : object_values_in_type_set<true>(target, value);
2778 EVALUATE_END(LoopPropertiesTypeStrictAny);
2781INSTRUCTION_HANDLER(LoopPropertiesTypeStrictAnyEvaluate) {
2782 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeStrictAnyEvaluate,
2783 target.is_object());
2785 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
2786 assert(value.any());
2787 result = type_set_spans_numbers(value)
2788 ? object_values_in_type_set<false>(target, value)
2789 : object_values_in_type_set<true>(target, value);
2790 if (!result) [[unlikely]] {
2791 EVALUATE_END(LoopPropertiesTypeStrictAnyEvaluate);
2794 context.evaluator->evaluate(&target);
2795 EVALUATE_END(LoopPropertiesTypeStrictAnyEvaluate);
2798INSTRUCTION_HANDLER(LoopKeys) {
2799 EVALUATE_BEGIN_NON_STRING(LoopKeys, target.is_object());
2800 assert(!instruction.children.empty());
2802 for (
const auto &entry : target.as_object()) {
2803 if constexpr (HasCallback) {
2804 context.evaluator->instance_location.push_back(entry.first);
2807 for (
const auto &child : instruction.children) {
2808 if (!EVALUATE_RECURSE_ON_PROPERTY_NAME(child, Evaluator::NULL_VALUE,
2809 entry.first)) [[unlikely]] {
2812 if constexpr (HasCallback) {
2813 context.evaluator->instance_location.pop_back();
2816 EVALUATE_END(LoopKeys);
2820 if constexpr (HasCallback) {
2821 context.evaluator->instance_location.pop_back();
2825 EVALUATE_END(LoopKeys);
2828INSTRUCTION_HANDLER(LoopItems) {
2829 EVALUATE_BEGIN_NON_STRING(LoopItems, target.is_array());
2830 assert(!instruction.children.empty());
2834 if constexpr (!HasCallback) {
2838 const auto direct_children{depth < DEPTH_LIMIT};
2839 for (
const auto &new_instance : target.as_array()) {
2840 for (
const auto &child : instruction.children) {
2842 if (direct_children) [[likely]] {
2843 switch (child.type) {
2844 case InstructionIndex::AssertionTypeArrayBounded:
2846 DIRECT(AssertionTypeArrayBounded,
2847 resolve_instance(new_instance,
2848 child.relative_instance_location),
2849 assume_value<ValueRange>(child.value));
2852 child_ok = EVALUATE_RECURSE(child, new_instance);
2856 child_ok = EVALUATE_RECURSE(child, new_instance);
2859 if (!child_ok) [[unlikely]] {
2861 EVALUATE_END(LoopItems);
2866 for (std::size_t index = 0; index < target.array_size(); index++) {
2867 if constexpr (HasCallback) {
2868 context.evaluator->instance_location.push_back(index);
2871 const auto &new_instance{target.at(index)};
2872 for (
const auto &child : instruction.children) {
2873 if (!EVALUATE_RECURSE(child, new_instance)) [[unlikely]] {
2876 if constexpr (HasCallback) {
2877 context.evaluator->instance_location.pop_back();
2880 EVALUATE_END(LoopItems);
2884 if constexpr (HasCallback) {
2885 context.evaluator->instance_location.pop_back();
2890 EVALUATE_END(LoopItems);
2893INSTRUCTION_HANDLER(LoopItemsFrom) {
2894 const auto value{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
2895 EVALUATE_BEGIN_NON_STRING(LoopItemsFrom,
2896 target.is_array() && value < target.array_size());
2897 assert(!instruction.children.empty());
2899 for (std::size_t index = value; index < target.array_size(); index++) {
2900 if constexpr (HasCallback) {
2901 context.evaluator->instance_location.push_back(index);
2904 const auto &new_instance{target.at(index)};
2905 for (
const auto &child : instruction.children) {
2906 if (!EVALUATE_RECURSE(child, new_instance)) [[unlikely]] {
2909 if constexpr (HasCallback) {
2910 context.evaluator->instance_location.pop_back();
2913 EVALUATE_END(LoopItemsFrom);
2917 if constexpr (HasCallback) {
2918 context.evaluator->instance_location.pop_back();
2922 EVALUATE_END(LoopItemsFrom);
2925INSTRUCTION_HANDLER(LoopItemsUnevaluated) {
2926 EVALUATE_BEGIN_NON_STRING(LoopItemsUnevaluated, target.is_array());
2931 if (!context.evaluator->is_evaluated(&target, context.frame)) {
2932 for (std::size_t index = 0; index < target.array_size(); index++) {
2933 const auto &new_instance{target.at(index)};
2934 if (context.evaluator->is_evaluated(&new_instance, context.frame)) {
2938 if constexpr (HasCallback) {
2939 context.evaluator->instance_location.push_back(index);
2941 for (
const auto &child : instruction.children) {
2942 if (!EVALUATE_RECURSE(child, new_instance)) [[unlikely]] {
2944 if constexpr (HasCallback) {
2945 context.evaluator->instance_location.pop_back();
2947 EVALUATE_END(LoopItemsUnevaluated);
2951 if constexpr (HasCallback) {
2952 context.evaluator->instance_location.pop_back();
2956 context.evaluator->evaluate(&target);
2959 EVALUATE_END(LoopItemsUnevaluated);
2962INSTRUCTION_HANDLER(LoopItemsType) {
2963 EVALUATE_BEGIN_NON_STRING(LoopItemsType, target.is_array());
2965 const auto value{assume_value_copy<ValueType>(instruction.value)};
2966 for (
const auto &entry : target.as_array()) {
2967 if (entry.type() != value &&
2970 (value != JSON::Type::Integer || !entry.is_integral())) [[unlikely]] {
2976 EVALUATE_END(LoopItemsType);
2979INSTRUCTION_HANDLER(LoopItemsTypeStrict) {
2980 EVALUATE_BEGIN_NON_STRING(LoopItemsTypeStrict, target.is_array());
2982 const auto value{assume_value_copy<ValueType>(instruction.value)};
2983 for (
const auto &entry : target.as_array()) {
2984 if (effective_type_strict_real(entry) != value) [[unlikely]] {
2990 EVALUATE_END(LoopItemsTypeStrict);
2993INSTRUCTION_HANDLER(LoopItemsTypeStrictAny) {
2994 EVALUATE_BEGIN_NON_STRING(LoopItemsTypeStrictAny, target.is_array());
2995 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
2996 assert(value.any());
2998 result = type_set_spans_numbers(value)
2999 ? array_items_in_type_set<false>(target, value)
3000 : array_items_in_type_set<true>(target, value);
3002 EVALUATE_END(LoopItemsTypeStrictAny);
3005INSTRUCTION_HANDLER(LoopItemsPropertiesExactlyTypeStrictHash) {
3006 EVALUATE_BEGIN_NO_PRECONDITION(LoopItemsPropertiesExactlyTypeStrictHash);
3008 resolve_instance(instance, instruction.relative_instance_location)};
3010 const auto &value{assume_value<ValueTypedHashes>(instruction.value)};
3012 assert(!value.second.first.empty());
3016 if (!target.is_array()) [[unlikely]] {
3017 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3020 const auto hashes_size{value.second.first.size()};
3021 for (
const auto &item : target.as_array()) {
3022 if (!item.is_object()) [[unlikely]] {
3024 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3027 const auto &
object{item.as_object()};
3028 const auto size{
object.size()};
3029 if (size != hashes_size) [[unlikely]] {
3031 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3037 if (hashes_size == 3) {
3038 for (
const auto &entry :
object) {
3039 if (effective_type_strict_real(entry.second) != value.first)
3043 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3044 }
else if (!(entry.hash == value.second.first[0].first ||
3045 entry.hash == value.second.first[1].first ||
3046 entry.hash == value.second.first[2].first)) {
3048 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3051 }
else if (hashes_size == 2) {
3052 for (
const auto &entry :
object) {
3053 if (effective_type_strict_real(entry.second) != value.first)
3057 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3058 }
else if (!(entry.hash == value.second.first[0].first ||
3059 entry.hash == value.second.first[1].first)) {
3061 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3064 }
else if (hashes_size == 1) {
3065 const auto &entry{*
object.cbegin()};
3066 if (effective_type_strict_real(entry.second) != value.first ||
3067 entry.hash != value.second.first[0].first) [[unlikely]] {
3069 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3072 std::size_t index{0};
3073 for (
const auto &entry :
object) {
3074 if (effective_type_strict_real(entry.second) != value.first)
3078 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3079 }
else if (entry.hash == value.second.first[index].first) {
3082 }
else if (!std::ranges::any_of(
3084 [&entry](
const auto &hash_entry) ->
bool {
3085 return hash_entry.first == entry.hash;
3088 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3096 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3099INSTRUCTION_HANDLER(LoopItemsPropertiesExactlyTypeStrictHash3) {
3100 EVALUATE_BEGIN_NO_PRECONDITION(LoopItemsPropertiesExactlyTypeStrictHash3);
3102 resolve_instance(instance, instruction.relative_instance_location)};
3104 const auto &value{assume_value<ValueTypedHashes>(instruction.value)};
3105 assert(value.second.first.size() == 3);
3107 assert(!value.second.first.empty());
3109 if (!target.is_array()) [[unlikely]] {
3111 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3114 for (
const auto &item : target.as_array()) {
3115 if (!item.is_object()) [[unlikely]] {
3116 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3119 const auto &
object{item.as_object()};
3120 if (
object.size() != 3) [[unlikely]] {
3121 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3124 const auto &value_1{
object.at(0)};
3125 const auto &value_2{
object.at(1)};
3126 const auto &value_3{
object.at(2)};
3128 if (effective_type_strict_real(value_1.second) != value.first ||
3129 effective_type_strict_real(value_2.second) != value.first ||
3130 effective_type_strict_real(value_3.second) != value.first)
3132 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3138 if ((value_1.hash == value.second.first[0].first &&
3139 value_2.hash == value.second.first[1].first &&
3140 value_3.hash == value.second.first[2].first) ||
3141 (value_1.hash == value.second.first[0].first &&
3142 value_2.hash == value.second.first[2].first &&
3143 value_3.hash == value.second.first[1].first) ||
3144 (value_1.hash == value.second.first[1].first &&
3145 value_2.hash == value.second.first[0].first &&
3146 value_3.hash == value.second.first[2].first) ||
3147 (value_1.hash == value.second.first[1].first &&
3148 value_2.hash == value.second.first[2].first &&
3149 value_3.hash == value.second.first[0].first) ||
3150 (value_1.hash == value.second.first[2].first &&
3151 value_2.hash == value.second.first[0].first &&
3152 value_3.hash == value.second.first[1].first) ||
3153 (value_1.hash == value.second.first[2].first &&
3154 value_2.hash == value.second.first[1].first &&
3155 value_3.hash == value.second.first[0].first)) {
3158 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3162 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3166 if (!target.is_array() || target.empty()) {
3169 for (
const auto &element : target.as_array()) {
3170 if (!element.is_number()) [[unlikely]] {
3174 if (element.is_integer()) {
3175 const auto integer{element.to_integer()};
3176 if (integer < value.first || integer > value.second) [[unlikely]] {
3179 }
else if (element.is_real()) {
3180 const auto real{element.to_real()};
3181 if (real <
static_cast<double>(value.first) ||
3182 real >
static_cast<double>(value.second)) [[unlikely]] {
3186 const auto real{element.to_decimal().to_double()};
3187 if (real <
static_cast<double>(value.first) ||
3188 real >
static_cast<double>(value.second)) [[unlikely]] {
3196INSTRUCTION_HANDLER(LoopItemsIntegerBounded) {
3197 EVALUATE_BEGIN_NON_STRING(LoopItemsIntegerBounded,
3198 target.is_array() && !target.empty());
3199 result = DIRECT(LoopItemsIntegerBounded, target,
3200 assume_value_copy<ValueIntegerBounds>(instruction.value));
3201 EVALUATE_END(LoopItemsIntegerBounded);
3204INSTRUCTION_HANDLER(LoopItemsIntegerBoundedSized) {
3206 assume_value<ValueIntegerBoundsWithSize>(instruction.value)};
3207 const auto &integer_bounds{value.first};
3208 const auto minimum_size{std::get<0>(value.second)};
3209 const auto &maximum_size{std::get<1>(value.second)};
3210 EVALUATE_BEGIN_NON_STRING(LoopItemsIntegerBoundedSized,
true);
3211 if (!target.is_array() || target.array_size() < minimum_size ||
3212 (maximum_size.has_value() &&
3213 target.array_size() > maximum_size.value())) {
3214 EVALUATE_END(LoopItemsIntegerBoundedSized);
3218 for (
const auto &element : target.as_array()) {
3219 if (!element.is_number()) [[unlikely]] {
3224 if (element.is_integer()) {
3225 const auto integer{element.to_integer()};
3226 if (integer < integer_bounds.first || integer > integer_bounds.second)
3231 }
else if (element.is_real()) {
3232 const auto real{element.to_real()};
3233 if (real <
static_cast<double>(integer_bounds.first) ||
3234 real >
static_cast<double>(integer_bounds.second)) [[unlikely]] {
3239 const auto real{element.to_decimal().to_double()};
3240 if (real <
static_cast<double>(integer_bounds.first) ||
3241 real >
static_cast<double>(integer_bounds.second)) [[unlikely]] {
3248 EVALUATE_END(LoopItemsIntegerBoundedSized);
3255INSTRUCTION_HANDLER(LoopItemsArrayBoundedTypeStrictAny) {
3256 EVALUATE_BEGIN_NON_STRING(LoopItemsArrayBoundedTypeStrictAny,
3258 const auto &value{assume_value<ValueTypesWithSize>(instruction.value)};
3259 const auto types{value.first};
3260 const auto minimum{std::get<0>(value.second)};
3261 const auto &maximum{std::get<1>(value.second)};
3262 assert(types.any());
3264 assert(!std::get<2>(value.second));
3265 result = type_set_spans_numbers(types)
3266 ? items_are_bounded_arrays_in_type_set<false>(target, types,
3268 : items_are_bounded_arrays_in_type_set<true>(target, types,
3271 EVALUATE_END(LoopItemsArrayBoundedTypeStrictAny);
3274INSTRUCTION_HANDLER(LoopContains) {
3275 EVALUATE_BEGIN_NON_STRING(LoopContains, target.is_array());
3278 const auto &value{assume_value<ValueRange>(instruction.value)};
3279 const auto &[minimum, maximum, is_exhaustive] = value;
3282 result = minimum == 0;
3284 std::numeric_limits<std::remove_cvref_t<
decltype(minimum)>>::min()};
3286 for (std::size_t index = 0; index < target.array_size(); index++) {
3287 if constexpr (HasCallback) {
3288 context.evaluator->instance_location.push_back(index);
3291 const auto &new_instance{target.at(index)};
3292 bool subresult{
true};
3293 for (
const auto &child : instruction.children) {
3294 if (!EVALUATE_RECURSE(child, new_instance)) {
3300 if constexpr (HasCallback) {
3301 context.evaluator->instance_location.pop_back();
3308 if (maximum.has_value() && match_count > maximum.value()) {
3317 if (match_count >= minimum) {
3322 if (!maximum.has_value() && !is_exhaustive) {
3329 EVALUATE_END(LoopContains);
3333#undef INSTRUCTION_HANDLER
3335template <
bool HasCallback>
3336using DispatchHandler = bool (*)(
const sourcemeta::blaze::Instruction &,
3337 const sourcemeta::core::JSON &, std::uint64_t,
3340template <
bool HasCallback>
3343static constexpr DispatchHandler<HasCallback> HANDLERS[113] = {
3346 AssertionDefinesStrict,
3347 AssertionDefinesAll,
3348 AssertionDefinesAllStrict,
3349 AssertionDefinesExactly,
3350 AssertionDefinesExactlyStrict,
3351 AssertionDefinesExactlyStrictHash3,
3352 AssertionPropertyDependencies,
3355 AssertionTypeStrict,
3356 AssertionTypeStrictAny,
3357 AssertionNotTypeStrictAny,
3358 AssertionTypeStringBounded,
3359 AssertionTypeStringUpper,
3360 AssertionTypeArrayBounded,
3361 AssertionTypeArrayUpper,
3362 AssertionTypeObjectBounded,
3363 AssertionTypeObjectUpper,
3365 AssertionStringSizeLess,
3366 AssertionStringSizeGreater,
3367 AssertionArraySizeLess,
3368 AssertionArraySizeGreater,
3369 AssertionObjectSizeLess,
3370 AssertionObjectSizeGreater,
3373 AssertionEqualsAnyStringHash,
3374 AssertionGreaterEqual,
3380 AssertionTypeIntegerBounded,
3381 AssertionTypeIntegerBoundedStrict,
3382 AssertionTypeIntegerLowerBound,
3383 AssertionTypeIntegerLowerBoundStrict,
3384 AssertionNumberIntegerBounded,
3385 AssertionNumberType,
3386 AssertionStringType,
3387 AssertionPropertyType,
3388 AssertionPropertyTypeEvaluate,
3389 AssertionPropertyTypeStrict,
3390 AssertionPropertyTypeStrictEvaluate,
3391 AssertionPropertyTypeStrictAny,
3392 AssertionPropertyTypeStrictAnyEvaluate,
3393 AssertionPropertyEqual,
3394 AssertionPropertyEqualsAnyStringHash,
3395 AssertionArrayPrefix,
3396 AssertionArrayPrefixEvaluate,
3397 AssertionObjectPropertiesSimple,
3399 AnnotationEmitWrapped,
3401 AnnotationBasenameToParent,
3411 LogicalConditionEvaluate,
3414 LogicalWhenArraySizeGreater,
3415 LoopPropertiesUnevaluated,
3416 LoopPropertiesUnevaluatedExcept,
3417 LoopPropertiesMatch,
3418 LoopPropertiesMatchClosed,
3419 LoopPropertiesMatchRequired,
3421 LoopPropertiesEvaluate,
3422 LoopPropertiesRegex,
3423 LoopPropertiesRegexClosed,
3424 LoopPropertiesStartsWith,
3425 LoopPropertiesExcept,
3427 LoopPropertiesTypeEvaluate,
3428 LoopPropertiesExactlyTypeStrict,
3429 LoopPropertiesExactlyTypeStrictHash,
3430 LoopPropertiesTypeStrict,
3431 LoopPropertiesTypeStrictEvaluate,
3432 LoopPropertiesTypeStrictAny,
3433 LoopPropertiesTypeStrictAnyEvaluate,
3437 LoopItemsUnevaluated,
3439 LoopItemsTypeStrict,
3440 LoopItemsTypeStrictAny,
3441 LoopItemsPropertiesExactlyTypeStrictHash,
3442 LoopItemsPropertiesExactlyTypeStrictHash3,
3443 LoopItemsIntegerBounded,
3444 LoopItemsIntegerBoundedSized,
3445 LoopItemsArrayBoundedTypeStrictAny,
3448 ControlGroupWhenDefines,
3449 ControlGroupWhenDefinesDirect,
3450 ControlGroupWhenType,
3454 ControlDynamicAnchorJump,
3456 ControlResourceJump};
3458template <
bool HasCallback>
3460evaluate_instruction(
const sourcemeta::blaze::Instruction &instruction,
3461 const sourcemeta::core::JSON &instance,
3462 const std::uint64_t depth,
3464 if (depth > DEPTH_LIMIT) [[unlikely]] {
3465 throw EvaluationError(
"The evaluation path depth limit was reached "
3466 "likely due to infinite recursion");
3469 return HANDLERS<HasCallback>[std::to_underlying(instruction.type)](
3470 instruction, instance, depth, context);
3473template <
bool HasCallback>
3474inline auto evaluate_instruction_without_callback(
3475 const sourcemeta::blaze::Instruction &instruction,
3476 const sourcemeta::core::JSON &instance,
const std::uint64_t depth,
3478 if (depth > DEPTH_LIMIT) [[unlikely]] {
3479 throw EvaluationError(
"The evaluation path depth limit was reached "
3480 "likely due to infinite recursion");
3485 context.property_target, context.frame};
3486 return HANDLERS<false>[std::to_underlying(instruction.type)](
3487 instruction, instance, depth, plain_context);
3490template <
bool HasCallback>
3491inline auto evaluate_instruction_with_property(
3492 const sourcemeta::blaze::Instruction &instruction,
3493 const sourcemeta::core::JSON &instance,
const std::uint64_t depth,
3495 const sourcemeta::core::JSON::String &name) ->
bool {
3496 const auto *previous = context.property_target;
3497 context.property_target = &name;
3499 evaluate_instruction(instruction, instance, depth, context);
3500 context.property_target = previous;
3508#undef EVALUATE_BEGIN
3509#undef EVALUATE_BEGIN_NON_STRING
3510#undef EVALUATE_BEGIN_IF_STRING
3511#undef EVALUATE_BEGIN_TRY_TARGET
3512#undef EVALUATE_BEGIN_NO_PRECONDITION
3513#undef EVALUATE_BEGIN_NO_PRECONDITION_AND_NO_PUSH
3514#undef EVALUATE_BEGIN_PASS_THROUGH
3516#undef EVALUATE_END_NO_POP
3517#undef EVALUATE_END_PASS_THROUGH
3518#undef EVALUATE_ANNOTATION
3519#undef EVALUATE_RECURSE
3520#undef EVALUATE_RECURSE_ON_PROPERTY_NAME
3521#undef SOURCEMETA_ASSUME
3522#undef SOURCEMETA_STRINGIFY
std::pair< std::vector< std::pair< ValueStringSet::hash_type, ValueString > >, std::vector< ValueIndexPair > > ValueStringHashes
Definition evaluator_value.h:199
sourcemeta::core::JSON::Type ValueType
Definition evaluator_value.h:64
std::function< void( const EvaluationType, bool, const Instruction &, const InstructionExtra &, const sourcemeta::core::WeakPointer &, const sourcemeta::core::WeakPointer &, const sourcemeta::core::JSON &)> Callback
Definition evaluator.h:82
sourcemeta::core::JSON ValueJSON
Definition evaluator_value.h:33
sourcemeta::core::JSON::TypeSet ValueTypes
Definition evaluator_value.h:60
std::size_t ValueUnsignedInteger
Definition evaluator_value.h:101
std::tuple< std::size_t, std::optional< std::size_t >, bool > ValueRange
Definition evaluator_value.h:107
std::pair< std::int64_t, std::int64_t > ValueIntegerBounds
Definition evaluator_value.h:209
std::unordered_set< sourcemeta::core::JSON, sourcemeta::core::HashJSON< sourcemeta::core::JSON > > ValueSet
Definition evaluator_value.h:37
Definition evaluator_dispatch.h:415