Blaze 0.0.1
The ultra high-performance JSON Schema evaluator
Loading...
Searching...
No Matches
evaluator_dispatch.h
1#ifndef SOURCEMETA_BLAZE_EVALUATOR_DISPATCH_H_
2#define SOURCEMETA_BLAZE_EVALUATOR_DISPATCH_H_
3
4// NOLINTNEXTLINE(misc-header-include-cycle)
5#include <sourcemeta/blaze/evaluator.h>
6
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>
15
16#include <cassert> // assert
17
18// TODO(C++23): Replace SOURCEMETA_ASSUME with [[assume]] when available
19// across all compilers (Clang 19, GCC 13)
20#if defined(__clang__)
21#define SOURCEMETA_ASSUME(x) \
22 assert(x); \
23 __builtin_assume(x)
24#else
25#define SOURCEMETA_ASSUME(x) assert(x)
26#endif
27
28#define SOURCEMETA_STRINGIFY(x) #x
29
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); \
35 } \
36 if constexpr (HasCallback) { \
37 context.evaluator->instance_location.push_back( \
38 instruction.relative_instance_location); \
39 } \
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); \
46 }
47
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); \
55 } \
56 if constexpr (HasCallback) { \
57 context.evaluator->evaluate_path.pop_back( \
58 context.schema->extra[instruction.extra_index] \
59 .relative_schema_location.size()); \
60 } \
61 if constexpr (HasCallback) { \
62 context.evaluator->instance_location.pop_back( \
63 instruction.relative_instance_location.size()); \
64 }
65
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]] { \
72 return true; \
73 } \
74 EVALUATE_PUSH() \
75 bool result{false};
76
77#define EVALUATE_BEGIN_NON_STRING(instruction_type, precondition) \
78 assert(instruction.type == InstructionIndex::instruction_type); \
79 const auto &target{ \
80 resolve_instance(instance, instruction.relative_instance_location)}; \
81 if (!(precondition)) [[unlikely]] { \
82 return true; \
83 } \
84 EVALUATE_PUSH() \
85 bool result{false};
86
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]] { \
93 return true; \
94 } \
95 EVALUATE_PUSH() \
96 const auto &target{*maybe_target}; \
97 bool result{false};
98
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]] { \
103 return true; \
104 } \
105 assert(!instruction.relative_instance_location.empty()); \
106 const auto *target_check{ \
107 instruction.relative_instance_location.size() == 1 \
108 ? target.try_at( \
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]] { \
113 return true; \
114 } \
115 EVALUATE_PUSH() \
116 bool result{false};
117
118#define EVALUATE_BEGIN_NO_PRECONDITION(instruction_type) \
119 assert(instruction.type == InstructionIndex::instruction_type); \
120 EVALUATE_PUSH() \
121 bool result{false};
122
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); \
131 } \
132 bool result{true};
133
134#define EVALUATE_BEGIN_PASS_THROUGH(instruction_type) \
135 assert(instruction.type == InstructionIndex::instruction_type); \
136 bool result{true};
137
138#define EVALUATE_END(instruction_type) \
139 EVALUATE_POP() \
140 return result;
141
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); \
149 } \
150 return result;
151
152#define EVALUATE_END_PASS_THROUGH(instruction_type) return result;
153
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, \
169 annotation_value); \
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()); \
175 } \
176 return true;
177
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)
182
183namespace sourcemeta::blaze::dispatch {
184using namespace sourcemeta::core;
185
186// The recursion depth past which evaluation is assumed to be non-terminating
187constexpr auto DEPTH_LIMIT{300};
188
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;
193 }
194
195 // NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter)
196 return instance;
197}
198
199inline auto resolve_instance(const JSON &instance,
200 const Pointer &relative_instance_location)
201 -> const JSON & {
202 if (relative_instance_location.empty()) {
203 // NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter)
204 return instance;
205 }
206
207 // A single property is what almost every instruction that navigates at all
208 // carries, and resolving it here keeps the lookup inline, rather than
209 // calling out to the pointer walk that the general case needs
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());
214 }
215
216 return get(instance, relative_instance_location);
217}
218
219inline auto
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;
225 }
226
227 const auto &target{resolve_instance(instance, relative_instance_location)};
228 if (!target.is_string()) [[unlikely]] {
229 return nullptr;
230 } else {
231 return &target.to_string();
232 }
233}
234
235// The entries a loop matches on, and the entries a loop that counts what it
236// matched holds, differ only in what each entry records, so the lookup is
237// written once over whichever of them it is handed and answers with the entry
238// it matched rather than with any one of that entry's fields
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 * {
243 // Few enough names come without a table of contents, as reaching one costs
244 // more than scanning them, so the whole range is the candidate range
245 std::size_t first{1};
246 std::size_t last{indexes.first.size()};
247 if (!indexes.second.empty()) {
248 // A name whose size no entry has cannot match, which settles most of the
249 // misses without comparing against any entry at all
250 if (name.size() >= indexes.second.size()) {
251 return nullptr;
252 }
253
254 const auto &bounds{indexes.second[name.size()]};
255 if (bounds.second == 0) {
256 return nullptr;
257 }
258
259 first = bounds.first;
260 last = bounds.second;
261 }
262
263 // A perfect hash records the name's size, so matching one is proof the names
264 // are equal and only a longer name needs comparing. Whether the hash is
265 // perfect depends on the name alone, so it is decided once rather than per
266 // entry, as `JSONObject` does for the same reason
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) {
272 return &entry;
273 }
274 }
275 } else {
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) {
279 return &entry;
280 }
281 }
282 }
283
284 return nullptr;
285}
286
287// `std::bitset::test` bounds-checks its index and throws when it is out of
288// range, which every type check would pay on every value it looks at. A
289// `JSON::Type` is always a valid index into a set that holds one bit per type,
290// so the check can never fire and the subscript is used instead
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)];
295}
296
297inline auto effective_type_strict_real(const JSON &instance) noexcept
298 -> JSON::Type {
299 const auto real_type{instance.type()};
300 switch (real_type) {
301 case JSON::Type::Decimal:
302 return instance.to_decimal().is_integer() ? JSON::Type::Integer
303 : JSON::Type::Real;
304 default:
305 return real_type;
306 }
307}
308
309// An integral number that no 64-bit integer can hold cannot be narrowed to one
310// to be compared, and `as_integer` throws rather than saturating, so the
311// comparison happens in a domain wide enough to hold any of them instead.
312// A real converts through `exact_from` because the constructor spells a double
313// with 17 significant digits, which is too few to tell apart two integers that
314// a bound of this width can sit between
315// Normalising a decimal to the integer or real it represents cannot change
316// whether a type set holds it when the set treats all three number kinds
317// alike, which is what `"type": "number"` compiles to. Deciding this once per
318// instruction keeps the normalisation out of the loop, rather than trading its
319// call for a branch on every element
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;
324}
325
326template <bool Normalise>
327inline auto type_in_set(const JSON &instance, const ValueTypes &types) noexcept
328 -> bool {
329 if constexpr (Normalise) {
330 return type_set_test(types, effective_type_strict_real(instance));
331 } else {
332 return type_set_test(types, instance.type());
333 }
334}
335
336template <bool Normalise>
337inline auto array_items_in_type_set(const JSON &target,
338 const ValueTypes &types) noexcept -> bool {
339 for (const auto &entry : target.as_array()) {
340 if (!type_in_set<Normalise>(entry, types)) [[unlikely]] {
341 return false;
342 }
343 }
344
345 return true;
346}
347
348template <bool Normalise>
349inline auto object_values_in_type_set(const JSON &target,
350 const ValueTypes &types) noexcept
351 -> bool {
352 for (const auto &entry : target.as_object()) {
353 if (!type_in_set<Normalise>(entry.second, types)) [[unlikely]] {
354 return false;
355 }
356 }
357
358 return true;
359}
360
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()))
368 [[unlikely]] {
369 return false;
370 }
371
372 if (!array_items_in_type_set<Normalise>(item, types)) [[unlikely]] {
373 return false;
374 }
375 }
376
377 return true;
378}
379
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());
384}
385
386inline auto integer_within_bounds(const JSON &target,
387 const ValueIntegerBounds &value) -> bool {
388 if (target.is_integer()) {
389 const auto integer{target.to_integer()};
390 return integer >= value.first && integer <= value.second;
391 }
392
393 if (target.is_integral()) {
394 const auto decimal{as_decimal(target)};
395 return decimal >= Decimal{value.first} && decimal <= Decimal{value.second};
396 }
397
398 return false;
399}
400
401template <typename T>
402inline auto assume_value(const Value &variant) noexcept -> const T & {
403 const auto *pointer{std::get_if<T>(&variant)};
404 SOURCEMETA_ASSUME(pointer != nullptr);
405 return *pointer;
406}
407
408template <typename T>
409inline auto assume_value_copy(const Value &variant) noexcept -> T {
410 const auto *pointer{std::get_if<T>(&variant)};
411 SOURCEMETA_ASSUME(pointer != nullptr);
412 return *pointer;
413}
414
415template <bool HasCallback> struct DispatchContext {
416 const sourcemeta::blaze::Template *schema;
417 const sourcemeta::blaze::Callback *callback;
419 const sourcemeta::core::JSON::String *property_target;
420 // Marks below this point were made outside the subschema being evaluated,
421 // so an `unevaluated*` keyword within it must not see them
422 std::size_t frame;
423};
424
425template <bool HasCallback>
426inline auto
427evaluate_instruction(const sourcemeta::blaze::Instruction &instruction,
428 const sourcemeta::core::JSON &instance,
429 const std::uint64_t depth,
430 DispatchContext<HasCallback> &context) -> bool;
431
432template <bool HasCallback>
433inline auto evaluate_instruction_with_property(
434 const sourcemeta::blaze::Instruction &instruction,
435 const sourcemeta::core::JSON &instance, const std::uint64_t depth,
437 const sourcemeta::core::JSON::String &name) -> bool;
438
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
446
447#define INSTRUCTION_DIRECT(name, value_type) \
448 SOURCEMETA_FORCEINLINE inline auto DIRECT_##name( \
449 const sourcemeta::core::JSON &target, const value_type &value) \
450 ->bool
451
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) \
455 ->bool
456
457#define DIRECT(name, target, value) DIRECT_##name(target, value)
458
459// Forward declarations
460INSTRUCTION_DIRECT_COPY(LoopItemsIntegerBounded, ValueIntegerBounds);
461
462// These handlers take their names from the instructions they implement, which
463// `InstructionIndex` spells the same way and trace output emits verbatim
464// NOLINTBEGIN(readability-identifier-naming)
465INSTRUCTION_HANDLER(AssertionFail) {
466 EVALUATE_BEGIN_NO_PRECONDITION(AssertionFail);
467 EVALUATE_END(AssertionFail);
468}
469
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);
475}
476
477INSTRUCTION_HANDLER(AssertionDefinesStrict) {
478 EVALUATE_BEGIN_NO_PRECONDITION(AssertionDefinesStrict);
479 const auto &target{
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);
484}
485
486INSTRUCTION_HANDLER(AssertionDefinesAll) {
487 EVALUATE_BEGIN_NON_STRING(AssertionDefinesAll, target.is_object());
488 const auto &value{assume_value<ValueStringSet>(instruction.value)};
489 // Otherwise we are we even emitting this instruction?
490 assert(value.size() > 1);
491
492 // Otherwise there is no way the instance can satisfy it anyway
493 if (value.size() <= target.object_size()) [[likely]] {
494 result = true;
495 const auto &object{target.as_object()};
496 for (const auto &property : value) {
497 if (!object.defines(property.first, property.second)) [[unlikely]] {
498 result = false;
499 break;
500 }
501 }
502 }
503
504 EVALUATE_END(AssertionDefinesAll);
505}
506
507INSTRUCTION_HANDLER(AssertionDefinesAllStrict) {
508 EVALUATE_BEGIN_NO_PRECONDITION(AssertionDefinesAllStrict);
509 const auto &target{
510 resolve_instance(instance, instruction.relative_instance_location)};
511 const auto &value{assume_value<ValueStringSet>(instruction.value)};
512 // Otherwise we are we even emitting this instruction?
513 assert(value.size() > 1);
514
515 // Otherwise there is no way the instance can satisfy it anyway
516 if (target.is_object() && value.size() <= target.object_size()) [[likely]] {
517 result = true;
518 const auto &object{target.as_object()};
519 for (const auto &property : value) {
520 if (!object.defines(property.first, property.second)) [[unlikely]] {
521 result = false;
522 break;
523 }
524 }
525 }
526
527 EVALUATE_END(AssertionDefinesAllStrict);
528}
529
530INSTRUCTION_HANDLER(AssertionDefinesExactly) {
531 EVALUATE_BEGIN_NON_STRING(AssertionDefinesExactly, target.is_object());
532 const auto &value{assume_value<ValueStringSet>(instruction.value)};
533 // Otherwise we are we even emitting this instruction?
534 assert(value.size() > 1);
535 const auto &object{target.as_object()};
536
537 if (value.size() == object.size()) [[likely]] {
538 result = true;
539 for (const auto &property : value) {
540 if (!object.defines(property.first, property.second)) [[unlikely]] {
541 result = false;
542 break;
543 }
544 }
545 }
546
547 EVALUATE_END(AssertionDefinesExactly);
548}
549
550INSTRUCTION_HANDLER(AssertionDefinesExactlyStrict) {
551 EVALUATE_BEGIN_NO_PRECONDITION(AssertionDefinesExactlyStrict);
552 const auto &target{
553 resolve_instance(instance, instruction.relative_instance_location)};
554 if (target.is_object()) [[likely]] {
555 const auto &value{assume_value<ValueStringSet>(instruction.value)};
556 // Otherwise we are we even emitting this instruction?
557 assert(value.size() > 1);
558 const auto &object{target.as_object()};
559
560 if (value.size() == object.size()) [[likely]] {
561 result = true;
562 for (const auto &property : value) {
563 if (!object.defines(property.first, property.second)) [[unlikely]] {
564 result = false;
565 break;
566 }
567 }
568 }
569 }
570
571 EVALUATE_END(AssertionDefinesExactlyStrict);
572}
573
574INSTRUCTION_HANDLER(AssertionDefinesExactlyStrictHash3) {
575 EVALUATE_BEGIN_NO_PRECONDITION(AssertionDefinesExactlyStrictHash3);
576 const auto &target{
577 resolve_instance(instance, instruction.relative_instance_location)};
578 if (target.is_object()) [[likely]] {
579 // TODO: Take advantage of the table of contents structure to speed up
580 // checks
581 const auto &value{assume_value<ValueStringHashes>(instruction.value)};
582 assert(value.first.size() == 3);
583 const auto &object{target.as_object()};
584
585 // A perfect hash records the key's size, which the compiler requires of
586 // every key here, so matching one proves the keys equal and the unrolled
587 // permutation check compares nothing but hashes
588 result =
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));
607 }
608
609 EVALUATE_END(AssertionDefinesExactlyStrictHash3);
610}
611
612INSTRUCTION_HANDLER(AssertionPropertyDependencies) {
613 EVALUATE_BEGIN_NON_STRING(AssertionPropertyDependencies, target.is_object());
614 const auto &value{assume_value<ValueStringMap>(instruction.value)};
615 // Otherwise we are we even emitting this instruction?
616 assert(!value.empty());
617 result = true;
618 const auto &object{target.as_object()};
619 for (const auto &entry : value) {
620 if (!object.defines(entry.first, entry.hash)) {
621 continue;
622 }
623
624 assert(!entry.second.empty());
625 for (const auto &dependency : entry.second) {
626 if (!object.defines(dependency.first, dependency.second)) [[unlikely]] {
627 result = false;
628 EVALUATE_END(AssertionPropertyDependencies);
629 }
630 }
631 }
632
633 EVALUATE_END(AssertionPropertyDependencies);
634}
635
636INSTRUCTION_DIRECT_COPY(AssertionType, ValueType) {
637 // In non-strict mode, we consider a real number that represents an
638 // integer to be an integer
639 return target.type() == value ||
640 (value == JSON::Type::Integer && target.is_integral());
641}
642
643INSTRUCTION_HANDLER(AssertionType) {
644 EVALUATE_BEGIN_NO_PRECONDITION(AssertionType);
645 const auto &target{
646 resolve_instance(instance, instruction.relative_instance_location)};
647 const auto value{assume_value_copy<ValueType>(instruction.value)};
648 // TODO: Maybe make this instruction about integers, as it is the
649 // only where where it is actually useful?
650 assert(value == JSON::Type::Integer);
651 SOURCEMETA_ASSUME(value == JSON::Type::Integer);
652 result = DIRECT(AssertionType, target, value);
653 EVALUATE_END(AssertionType);
654}
655
656INSTRUCTION_DIRECT(AssertionTypeAny, ValueTypes) {
657 // In non-strict mode, we consider a real number that represents an
658 // integer to be an integer
659 return type_set_test(value, target.type()) ||
660 (type_set_test(value, JSON::Type::Integer) && target.is_integral());
661}
662
663INSTRUCTION_HANDLER(AssertionTypeAny) {
664 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeAny);
665 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
666 assert(value.any());
667 const auto &target{
668 resolve_instance(instance, instruction.relative_instance_location)};
669 result = DIRECT(AssertionTypeAny, target, value);
670 EVALUATE_END(AssertionTypeAny);
671}
672
673INSTRUCTION_DIRECT_COPY(AssertionTypeStrict, ValueType) {
674 return effective_type_strict_real(target) == value;
675}
676
677INSTRUCTION_HANDLER(AssertionTypeStrict) {
678 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeStrict);
679 const auto &target{
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);
684}
685
686INSTRUCTION_DIRECT(AssertionTypeStrictAny, ValueTypes) {
687 return type_set_test(value, effective_type_strict_real(target));
688}
689
690INSTRUCTION_HANDLER(AssertionTypeStrictAny) {
691 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeStrictAny);
692 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
693 assert(value.any());
694 const auto &target{
695 resolve_instance(instance, instruction.relative_instance_location)};
696 result = DIRECT(AssertionTypeStrictAny, target, value);
697 EVALUATE_END(AssertionTypeStrictAny);
698}
699
700INSTRUCTION_DIRECT(AssertionNotTypeStrictAny, ValueTypes) {
701 return !type_set_test(value, effective_type_strict_real(target));
702}
703
704INSTRUCTION_HANDLER(AssertionNotTypeStrictAny) {
705 EVALUATE_BEGIN_NO_PRECONDITION(AssertionNotTypeStrictAny);
706 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
707 assert(value.any());
708 const auto &target{
709 resolve_instance(instance, instruction.relative_instance_location)};
710 result = DIRECT(AssertionNotTypeStrictAny, target, value);
711 EVALUATE_END(AssertionNotTypeStrictAny);
712}
713
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());
719}
720
721INSTRUCTION_HANDLER(AssertionTypeStringBounded) {
722 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeStringBounded);
723 const auto &target{
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);
731}
732
733INSTRUCTION_HANDLER(AssertionTypeStringUpper) {
734 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeStringUpper);
735 const auto &target{
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);
740}
741
742INSTRUCTION_DIRECT(AssertionTypeArrayBounded, ValueRange) {
743 const auto &[minimum, maximum, exhaustive] = value;
744 // Require early breaking
745 assert(!exhaustive);
746 SOURCEMETA_ASSUME(!exhaustive);
747 return target.type() == JSON::Type::Array && target.array_size() >= minimum &&
748 (!maximum.has_value() || target.array_size() <= maximum.value());
749}
750
751INSTRUCTION_HANDLER(AssertionTypeArrayBounded) {
752 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeArrayBounded);
753 const auto &target{
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);
760}
761
762INSTRUCTION_HANDLER(AssertionTypeArrayUpper) {
763 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeArrayUpper);
764 const auto &target{
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);
769}
770
771INSTRUCTION_HANDLER(AssertionTypeObjectBounded) {
772 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeObjectBounded);
773 const auto &target{
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);
778 // Require early breaking
779 assert(!exhaustive);
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);
785}
786
787INSTRUCTION_HANDLER(AssertionTypeObjectUpper) {
788 EVALUATE_BEGIN_NO_PRECONDITION(AssertionTypeObjectUpper);
789 const auto &target{
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);
794}
795
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);
801}
802
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);
808}
809
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);
815}
816
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);
822}
823
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);
829}
830
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);
836}
837
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);
843}
844
845INSTRUCTION_DIRECT(AssertionEqual, ValueJSON) { return target == value; }
846
847INSTRUCTION_HANDLER(AssertionEqual) {
848 EVALUATE_BEGIN_NO_PRECONDITION(AssertionEqual);
849 const auto &value{assume_value<ValueJSON>(instruction.value)};
850
851 if (context.property_target) [[unlikely]] {
852 result = value.is_string() && value.to_string() == *context.property_target;
853 } else {
854 const auto &target{
855 resolve_instance(instance, instruction.relative_instance_location)};
856 result = DIRECT(AssertionEqual, target, value);
857 }
858
859 EVALUATE_END(AssertionEqual);
860}
861
862INSTRUCTION_DIRECT(AssertionEqualsAny, ValueSet) {
863 return value.contains(target);
864}
865
866INSTRUCTION_HANDLER(AssertionEqualsAny) {
867 EVALUATE_BEGIN_NO_PRECONDITION(AssertionEqualsAny);
868 const auto &value{assume_value<ValueSet>(instruction.value)};
869
870 if (context.property_target) [[unlikely]] {
871 // TODO: This involves a string copy
872 result = value.contains(JSON{*context.property_target});
873 } else {
874 const auto &target{
875 resolve_instance(instance, instruction.relative_instance_location)};
876 result = DIRECT(AssertionEqualsAny, target, value);
877 }
878
879 EVALUATE_END(AssertionEqualsAny);
880}
881
882INSTRUCTION_DIRECT(AssertionEqualsAnyStringHash, ValueStringHashes) {
883 if (!target.is_string()) [[unlikely]] {
884 return false;
885 }
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) {
895 return true;
896 }
897 }
898 }
899 }
900 return false;
901}
902
903INSTRUCTION_HANDLER(AssertionEqualsAnyStringHash) {
904 EVALUATE_BEGIN_NO_PRECONDITION(AssertionEqualsAnyStringHash);
905 const auto &value{assume_value<ValueStringHashes>(instruction.value)};
906
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) {
919 result = true;
920 break;
921 }
922 }
923 }
924 }
925 } else {
926 const auto &target{
927 resolve_instance(instance, instruction.relative_instance_location)};
928 result = DIRECT(AssertionEqualsAnyStringHash, target, value);
929 }
930
931 EVALUATE_END(AssertionEqualsAnyStringHash);
932}
933
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);
939}
940
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);
946}
947
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);
953}
954
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);
960}
961
962INSTRUCTION_HANDLER(AssertionUnique) {
963 EVALUATE_BEGIN_NON_STRING(AssertionUnique, target.is_array());
964 result = target.unique();
965 EVALUATE_END(AssertionUnique);
966}
967
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);
974}
975
976INSTRUCTION_DIRECT(AssertionTypeIntegerBounded, ValueIntegerBounds) {
977 return integer_within_bounds(target, value);
978}
979
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);
985}
986
987INSTRUCTION_DIRECT(AssertionTypeIntegerBoundedStrict, ValueIntegerBounds) {
988 if (target.is_integer()) {
989 const auto integer{target.to_integer()};
990 return integer >= value.first && integer <= value.second;
991 }
992 return false;
993}
994
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);
1000}
1001
1002INSTRUCTION_DIRECT(AssertionTypeIntegerLowerBound, ValueIntegerBounds) {
1003 if (target.is_integer()) {
1004 return target.to_integer() >= value.first;
1005 }
1006 if (target.is_integral()) {
1007 return as_decimal(target) >= Decimal{value.first};
1008 }
1009 return false;
1010}
1011
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);
1017}
1018
1019INSTRUCTION_DIRECT(AssertionTypeIntegerLowerBoundStrict, ValueIntegerBounds) {
1020 if (target.is_integer()) {
1021 return target.to_integer() >= value.first;
1022 }
1023 // An integer too large for 64 bits is held as a decimal, which the strict
1024 // type check still reads as an integer when it was written as digits alone.
1025 // The bounded counterpart needs no such branch, as a value out of that range
1026 // cannot sit within bounds of that width either way
1027 if (effective_type_strict_real(target) == JSON::Type::Integer) {
1028 return target.to_decimal() >= Decimal{value.first};
1029 }
1030 return false;
1031}
1032
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);
1038}
1039
1040INSTRUCTION_DIRECT(AssertionNumberIntegerBounded, ValueIntegerBounds) {
1041 return integer_within_bounds(target, value);
1042}
1043
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);
1049}
1050
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();
1056 break;
1057 case ValueNumberType::Double:
1058 result = target.is_double();
1059 break;
1060 }
1061 EVALUATE_END(AssertionNumberType);
1062}
1063
1064INSTRUCTION_HANDLER(AssertionStringType) {
1065 EVALUATE_BEGIN_IF_STRING(AssertionStringType);
1066 const auto value{assume_value_copy<ValueStringType>(instruction.value)};
1067 switch (value) {
1068 case ValueStringType::URI:
1069 result = URI::is_uri(target);
1070 break;
1071 case ValueStringType::URIReference:
1072 result = URI::is_uri_reference(target);
1073 break;
1074 case ValueStringType::URITemplate:
1075 result = URITemplate::is_uritemplate(target);
1076 break;
1077 case ValueStringType::IRI:
1078 result = URI::is_iri(target);
1079 break;
1080 case ValueStringType::IRIReference:
1081 result = URI::is_iri_reference(target);
1082 break;
1083 case ValueStringType::Email:
1084 result = is_email(target);
1085 break;
1086 case ValueStringType::IDNEmail:
1087 result = is_idn_email_uts46(target);
1088 break;
1089 case ValueStringType::IPv4:
1090 result = is_ipv4(target);
1091 break;
1092 case ValueStringType::IPv6:
1093 result = is_ipv6(target);
1094 break;
1095 case ValueStringType::Hostname:
1096 result = is_hostname(target);
1097 break;
1098 case ValueStringType::IDNHostname:
1099 result = is_idn_hostname_uts46(target);
1100 break;
1101 case ValueStringType::DateTime:
1102 result = is_rfc3339_datetime(target);
1103 break;
1104 case ValueStringType::Date:
1105 result = is_rfc3339_fulldate(target);
1106 break;
1107 case ValueStringType::Time:
1108 result = is_rfc3339_fulltime(target);
1109 break;
1110 case ValueStringType::PartialTime:
1111 result = is_rfc3339_partialtime_no_secfrac(target);
1112 break;
1113 case ValueStringType::Duration:
1114 result = is_rfc3339_duration(target);
1115 break;
1116 case ValueStringType::JSONPointer:
1117 result = is_pointer(target);
1118 break;
1119 case ValueStringType::RelativeJSONPointer:
1120 result = is_relative_pointer(target);
1121 break;
1122 case ValueStringType::UUID:
1123 result = is_uuid_like(target);
1124 break;
1125 case ValueStringType::Regex:
1126 result = is_regex_ecma(target);
1127 break;
1128 case ValueStringType::Color:
1129 result = is_css2_color(target);
1130 break;
1131 case ValueStringType::Base64:
1132 result = sourcemeta::core::is_base64(target);
1133 break;
1134 default:
1135 std::unreachable();
1136 }
1137
1138 EVALUATE_END(AssertionStringType);
1139}
1140
1141INSTRUCTION_HANDLER(AssertionPropertyType) {
1142 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyType);
1143 // Now here we refer to the actual property
1144 const auto value{assume_value_copy<ValueType>(instruction.value)};
1145 // In non-strict mode, we consider a real number that represents an
1146 // integer to be an integer
1147 result = target_check->type() == value ||
1148 (value == JSON::Type::Integer && target_check->is_integral());
1149 EVALUATE_END(AssertionPropertyType);
1150}
1151
1152INSTRUCTION_HANDLER(AssertionPropertyTypeEvaluate) {
1153 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeEvaluate);
1154 // Now here we refer to the actual property
1155 const auto value{assume_value_copy<ValueType>(instruction.value)};
1156 // In non-strict mode, we consider a real number that represents an
1157 // integer to be an integer
1158 result = target_check->type() == value ||
1159 (value == JSON::Type::Integer && target_check->is_integral());
1160
1161 if (result) {
1162 context.evaluator->evaluate(target_check);
1163 }
1164
1165 EVALUATE_END(AssertionPropertyTypeEvaluate);
1166}
1167
1168INSTRUCTION_HANDLER(AssertionPropertyTypeStrict) {
1169 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeStrict);
1170 // Now here we refer to the actual property
1171 const auto value{assume_value_copy<ValueType>(instruction.value)};
1172 result = effective_type_strict_real(*target_check) == value;
1173 EVALUATE_END(AssertionPropertyTypeStrict);
1174}
1175
1176INSTRUCTION_HANDLER(AssertionPropertyTypeStrictEvaluate) {
1177 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeStrictEvaluate);
1178 // Now here we refer to the actual property
1179 const auto value{assume_value_copy<ValueType>(instruction.value)};
1180 result = effective_type_strict_real(*target_check) == value;
1181
1182 if (result) {
1183 context.evaluator->evaluate(target_check);
1184 }
1185
1186 EVALUATE_END(AssertionPropertyTypeStrictEvaluate);
1187}
1188
1189INSTRUCTION_HANDLER(AssertionPropertyEqual) {
1190 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyEqual);
1191 const auto &value{assume_value<ValueJSON>(instruction.value)};
1192 // Now here we refer to the actual property
1193 result = DIRECT(AssertionEqual, *target_check, value);
1194 EVALUATE_END(AssertionPropertyEqual);
1195}
1196
1197INSTRUCTION_HANDLER(AssertionPropertyTypeStrictAny) {
1198 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeStrictAny);
1199 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
1200 assert(value.any());
1201 // Now here we refer to the actual property
1202 result = type_set_test(value, effective_type_strict_real(*target_check));
1203 EVALUATE_END(AssertionPropertyTypeStrictAny);
1204}
1205
1206INSTRUCTION_HANDLER(AssertionPropertyTypeStrictAnyEvaluate) {
1207 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyTypeStrictAnyEvaluate);
1208 const auto value{assume_value_copy<ValueTypes>(instruction.value)};
1209 assert(value.any());
1210 // Now here we refer to the actual property
1211 result = type_set_test(value, effective_type_strict_real(*target_check));
1212
1213 if (result) {
1214 context.evaluator->evaluate(target_check);
1215 }
1216
1217 EVALUATE_END(AssertionPropertyTypeStrictAnyEvaluate);
1218}
1219
1220INSTRUCTION_HANDLER(AssertionObjectPropertiesSimple) {
1221 EVALUATE_BEGIN_NON_STRING(AssertionObjectPropertiesSimple, true);
1222 if (!target.is_object()) {
1223 EVALUATE_END(AssertionObjectPropertiesSimple);
1224 }
1225
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>
1230 PROPERTY_HASHER;
1231 const auto &object{target.as_object()};
1232 const auto schema_size{value.size()};
1233 std::uint32_t seen{0};
1234 result = true;
1235
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;
1239 schema_index++) {
1240 const auto &schema_hash{std::get<1>(value[schema_index])};
1241 // A perfect hash records the key's size, so matching one proves the keys
1242 // equal and only a longer key needs comparing
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};
1251
1252// NOLINTBEGIN(bugprone-macro-parentheses)
1253#define CHILD_DIRECT(name, accessor, value_type) \
1254 case name: \
1255 child_ok = \
1256 DIRECT(name, property_value, accessor<value_type>(child.value)); \
1257 break
1258
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,
1277 assume_value_copy, ValueIntegerBounds);
1278 CHILD_DIRECT(LoopItemsIntegerBounded, assume_value_copy,
1280 default:
1281 child_ok = evaluate_instruction_without_callback(
1282 child, property_value, depth + 1, context);
1283 break;
1284 }
1285
1286// NOLINTEND(bugprone-macro-parentheses)
1287#undef CHILD_DIRECT
1288
1289 if (!child_ok) [[unlikely]] {
1290 result = false;
1291 EVALUATE_END(AssertionObjectPropertiesSimple);
1292 }
1293 }
1294
1295 break;
1296 }
1297 }
1298 }
1299
1300 // Check required properties that were not seen
1301 const auto all_seen{schema_size == 32
1302 ? ~std::uint32_t{0}
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;
1306 schema_index++) {
1307 if (std::get<2>(value[schema_index]) &&
1308 !(seen & (static_cast<std::uint32_t>(1) << schema_index))) {
1309 result = false;
1310 EVALUATE_END(AssertionObjectPropertiesSimple);
1311 }
1312 }
1313 }
1314
1315 EVALUATE_END(AssertionObjectPropertiesSimple);
1316}
1317
1318INSTRUCTION_HANDLER(AssertionPropertyEqualsAnyStringHash) {
1319 EVALUATE_BEGIN_TRY_TARGET(AssertionPropertyEqualsAnyStringHash);
1320 const auto &value{assume_value<ValueStringHashes>(instruction.value)};
1321 // Now here we refer to the actual property
1322 result = DIRECT(AssertionEqualsAnyStringHash, *target_check, value);
1323 EVALUATE_END(AssertionPropertyEqualsAnyStringHash);
1324}
1325
1326INSTRUCTION_HANDLER(AssertionArrayPrefix) {
1327 EVALUATE_BEGIN_NON_STRING(AssertionArrayPrefix, target.is_array());
1328 // Otherwise there is no point in emitting this instruction
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]] {
1334 const auto pointer{
1335 array_size == prefixes ? prefixes : std::min(array_size, prefixes) - 1};
1336 const auto &entry{instruction.children[pointer]};
1337 result = true;
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]] {
1343 result = false;
1344 break;
1345 }
1346 }
1347 }
1348
1349 EVALUATE_END(AssertionArrayPrefix);
1350}
1351
1352INSTRUCTION_HANDLER(AssertionArrayPrefixEvaluate) {
1353 EVALUATE_BEGIN_NON_STRING(AssertionArrayPrefixEvaluate, target.is_array());
1354 // Otherwise there is no point in emitting this instruction
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]] {
1360 const auto pointer{
1361 array_size == prefixes ? prefixes : std::min(array_size, prefixes) - 1};
1362 const auto &entry{instruction.children[pointer]};
1363 result = true;
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]] {
1369 result = false;
1370 EVALUATE_END(AssertionArrayPrefixEvaluate);
1371 }
1372 }
1373
1374 assert(result);
1375 SOURCEMETA_ASSUME(result);
1376 if (array_size == prefixes) {
1377 context.evaluator->evaluate(&target);
1378 } else {
1379 for (std::size_t cursor = 0; cursor <= pointer; cursor++) {
1380 context.evaluator->evaluate(&target.at(cursor));
1381 }
1382 }
1383 }
1384
1385 EVALUATE_END(AssertionArrayPrefixEvaluate);
1386}
1387
1388// Every branch of a disjunction is wrapped in a group that only runs the
1389// instructions it holds, and the tag that rules the branch out is the first of
1390// them. Doing both here spares a dispatch for the wrapper and, where the
1391// branch leads with its tag, another one for the tag. The wrapper reports
1392// nothing of its own, so running it here is indistinguishable from dispatching
1393// it, yet the tag does report, which is why it only steps aside when there is
1394// no callback to report to
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,
1399 DispatchContext<HasCallback> &context) -> bool {
1400 if (branch.type != InstructionIndex::ControlGroup) [[unlikely]] {
1401 return evaluate_instruction(branch, target, depth, context);
1402 }
1403
1404 auto step{branch.children.cbegin()};
1405 const auto last{branch.children.cend()};
1406
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) {
1413 const auto *tag{
1414 target.try_at(token.to_property(), token.property_hash())};
1415 if (tag != nullptr && !DIRECT(AssertionEqual, *tag,
1416 assume_value<ValueJSON>(step->value))) {
1417 return false;
1418 }
1419
1420 ++step;
1421 } else if (step->type ==
1422 InstructionIndex::AssertionPropertyEqualsAnyStringHash) {
1423 const auto *tag{
1424 target.try_at(token.to_property(), token.property_hash())};
1425 if (tag != nullptr &&
1426 !DIRECT(AssertionEqualsAnyStringHash, *tag,
1427 assume_value<ValueStringHashes>(step->value))) {
1428 return false;
1429 }
1430
1431 ++step;
1432 }
1433 }
1434 }
1435
1436 for (; step != last; ++step) {
1437 if (!evaluate_instruction(*step, target, depth + 1, context)) [[unlikely]] {
1438 return false;
1439 }
1440 }
1441
1442 return true;
1443}
1444
1445INSTRUCTION_HANDLER(LogicalOr) {
1446 EVALUATE_BEGIN_NO_PRECONDITION(LogicalOr);
1447 result = instruction.children.empty();
1448 const auto &target{
1449 resolve_instance(instance, instruction.relative_instance_location)};
1450 const auto value{assume_value_copy<ValueBoolean>(instruction.value)};
1451
1452 // This boolean value controls whether we should be exhaustive
1453 if (value) {
1454 for (const auto &child : instruction.children) {
1455 if (evaluate_branch(child, target, depth + 1, context)) {
1456 result = true;
1457 }
1458 }
1459 } else {
1460 for (const auto &child : instruction.children) {
1461 if (evaluate_branch(child, target, depth + 1, context)) {
1462 result = true;
1463 break;
1464 }
1465 }
1466 }
1467
1468 EVALUATE_END(LogicalOr);
1469}
1470
1471INSTRUCTION_HANDLER(LogicalOrEvaluate) {
1472 EVALUATE_BEGIN_NO_PRECONDITION(LogicalOrEvaluate);
1473 result = instruction.children.empty();
1474 const auto &target{
1475 resolve_instance(instance, instruction.relative_instance_location)};
1476 const auto value{assume_value_copy<ValueBoolean>(instruction.value)};
1477
1478 // This boolean value controls whether we should be exhaustive
1479 if (value) {
1480 for (const auto &child : instruction.children) {
1481 // A branch that does not hold contributes nothing, including any
1482 // evaluation it marked before failing
1483 const auto checkpoint{context.evaluator->checkpoint()};
1484 if (evaluate_branch(child, target, depth + 1, context)) {
1485 result = true;
1486 } else {
1487 context.evaluator->rewind(checkpoint);
1488 }
1489 }
1490 } else {
1491 for (const auto &child : instruction.children) {
1492 const auto checkpoint{context.evaluator->checkpoint()};
1493 if (evaluate_branch(child, target, depth + 1, context)) {
1494 result = true;
1495 break;
1496 }
1497
1498 context.evaluator->rewind(checkpoint);
1499 }
1500 }
1501
1502 EVALUATE_END(LogicalOrEvaluate);
1503}
1504
1505INSTRUCTION_HANDLER(LogicalAnd) {
1506 EVALUATE_BEGIN_NO_PRECONDITION(LogicalAnd);
1507 result = true;
1508 const auto &target{
1509 resolve_instance(instance, instruction.relative_instance_location)};
1510 for (const auto &child : instruction.children) {
1511 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1512 result = false;
1513 break;
1514 }
1515 }
1516
1517 EVALUATE_END(LogicalAnd);
1518}
1519
1520INSTRUCTION_HANDLER(LogicalWhenType) {
1521 const auto value{assume_value_copy<ValueType>(instruction.value)};
1522
1523 // Not having to worry about numbers in this instruction
1524 // makes things a lot simpler
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);
1531
1532 EVALUATE_BEGIN(LogicalWhenType, target.type() == value);
1533 result = true;
1534 for (const auto &child : instruction.children) {
1535 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1536 result = false;
1537 break;
1538 }
1539 }
1540
1541 EVALUATE_END(LogicalWhenType);
1542}
1543
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));
1549 result = true;
1550 for (const auto &child : instruction.children) {
1551 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1552 result = false;
1553 break;
1554 }
1555 }
1556
1557 EVALUATE_END(LogicalWhenDefines);
1558}
1559
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);
1564 result = true;
1565 for (const auto &child : instruction.children) {
1566 if (!EVALUATE_RECURSE(child, target)) [[unlikely]] {
1567 result = false;
1568 break;
1569 }
1570 }
1571
1572 EVALUATE_END(LogicalWhenArraySizeGreater);
1573}
1574
1575INSTRUCTION_HANDLER(LogicalXor) {
1576 EVALUATE_BEGIN_NO_PRECONDITION(LogicalXor);
1577 result = true;
1578 bool has_matched{false};
1579 const auto &target{
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]] {
1585 result = false;
1586 // This boolean value controls whether we should be exhaustive
1587 if (!value) {
1588 break;
1589 }
1590 } else {
1591 has_matched = true;
1592 }
1593 }
1594 }
1595
1596 result = result && has_matched;
1597 EVALUATE_END(LogicalXor);
1598}
1599
1600INSTRUCTION_HANDLER(LogicalXorEvaluate) {
1601 EVALUATE_BEGIN_NO_PRECONDITION(LogicalXorEvaluate);
1602 result = true;
1603 bool has_matched{false};
1604 const auto &target{
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) {
1608 // A branch that does not hold contributes nothing, including any
1609 // evaluation it marked before failing
1610 const auto checkpoint{context.evaluator->checkpoint()};
1611 if (evaluate_branch(child, target, depth + 1, context)) {
1612 if (has_matched) [[unlikely]] {
1613 result = false;
1614 // This boolean value controls whether we should be exhaustive
1615 if (!value) {
1616 break;
1617 }
1618 } else {
1619 has_matched = true;
1620 }
1621 } else {
1622 context.evaluator->rewind(checkpoint);
1623 }
1624 }
1625
1626 result = result && has_matched;
1627 EVALUATE_END(LogicalXorEvaluate);
1628}
1629
1630INSTRUCTION_HANDLER(LogicalCondition) {
1631 EVALUATE_BEGIN_NO_PRECONDITION(LogicalCondition);
1632 result = true;
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);
1639
1640 // The condition is the leading segment [0, then start). It may be empty,
1641 // for example when a `$ref` condition inlines and its jump is dropped, in
1642 // which case it trivially passes and the then branch applies
1643 const auto &target{
1644 resolve_instance(instance, instruction.relative_instance_location)};
1645
1646 for (std::size_t cursor = 0; cursor < value.first; cursor++) {
1647 if (!EVALUATE_RECURSE(instruction.children[cursor], target)) {
1648 result = false;
1649 break;
1650 }
1651 }
1652
1653 // On a passing condition the then branch runs, otherwise the else branch,
1654 // which is absent when it starts at zero
1655 std::size_t consequence_start;
1656 std::size_t consequence_end;
1657 if (result) {
1658 consequence_start = value.first;
1659 consequence_end = value.second > 0 ? value.second : children_size;
1660 } else {
1661 consequence_start = value.second;
1662 consequence_end = value.second > 0 ? children_size : value.second;
1663 }
1664
1665 result = true;
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());
1671 }
1672
1673 for (auto cursor = consequence_start; cursor < consequence_end; cursor++) {
1674 if (!EVALUATE_RECURSE(instruction.children[cursor], target)) {
1675 result = false;
1676 break;
1677 }
1678 }
1679
1680 if constexpr (HasCallback) {
1681 context.evaluator->evaluate_path.push_back(
1682 context.schema->extra[instruction.extra_index]
1683 .relative_schema_location);
1684 }
1685 }
1686
1687 EVALUATE_END(LogicalCondition);
1688}
1689
1690INSTRUCTION_HANDLER(LogicalConditionEvaluate) {
1691 EVALUATE_BEGIN_NO_PRECONDITION(LogicalConditionEvaluate);
1692 result = true;
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);
1699
1700 // The condition is the leading segment [0, then start). It may be empty,
1701 // for example when a `$ref` condition inlines and its jump is dropped, in
1702 // which case it trivially passes and the then branch applies
1703 const auto &target{
1704 resolve_instance(instance, instruction.relative_instance_location)};
1705
1706 // A condition that holds does contribute the evaluation it marked, but one
1707 // that does not hold contributes nothing at all
1708 const auto checkpoint{context.evaluator->checkpoint()};
1709
1710 for (std::size_t cursor = 0; cursor < value.first; cursor++) {
1711 if (!EVALUATE_RECURSE(instruction.children[cursor], target)) {
1712 result = false;
1713 break;
1714 }
1715 }
1716
1717 if (!result) {
1718 context.evaluator->rewind(checkpoint);
1719 }
1720
1721 // On a passing condition the then branch runs, otherwise the else branch,
1722 // which is absent when it starts at zero
1723 std::size_t consequence_start;
1724 std::size_t consequence_end;
1725 if (result) {
1726 consequence_start = value.first;
1727 consequence_end = value.second > 0 ? value.second : children_size;
1728 } else {
1729 consequence_start = value.second;
1730 consequence_end = value.second > 0 ? children_size : value.second;
1731 }
1732
1733 result = true;
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());
1739 }
1740
1741 for (auto cursor = consequence_start; cursor < consequence_end; cursor++) {
1742 if (!EVALUATE_RECURSE(instruction.children[cursor], target)) {
1743 result = false;
1744 break;
1745 }
1746 }
1747
1748 if constexpr (HasCallback) {
1749 context.evaluator->evaluate_path.push_back(
1750 context.schema->extra[instruction.extra_index]
1751 .relative_schema_location);
1752 }
1753 }
1754
1755 EVALUATE_END(LogicalConditionEvaluate);
1756}
1757
1758INSTRUCTION_HANDLER(ControlGroup) {
1759 EVALUATE_BEGIN_PASS_THROUGH(ControlGroup);
1760 for (const auto &child : instruction.children) {
1761 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1762 result = false;
1763 break;
1764 }
1765 }
1766
1767 EVALUATE_END_PASS_THROUGH(ControlGroup);
1768}
1769
1770INSTRUCTION_HANDLER(ControlGroupWhenDefines) {
1771 EVALUATE_BEGIN_PASS_THROUGH(ControlGroupWhenDefines);
1772 assert(!instruction.children.empty());
1773 // Otherwise why are we emitting this property?
1774 assert(!instruction.relative_instance_location.empty());
1775 const auto &target{
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) {
1780 // Note that in this control instruction, we purposely
1781 // don't navigate into the target
1782 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1783 result = false;
1784 break;
1785 }
1786 }
1787 }
1788
1789 EVALUATE_END_PASS_THROUGH(ControlGroupWhenDefines);
1790}
1791
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)};
1797
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]] {
1801 result = false;
1802 break;
1803 }
1804 }
1805 }
1806
1807 EVALUATE_END_PASS_THROUGH(ControlGroupWhenDefinesDirect);
1808}
1809
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)};
1815
1816 // Not having to worry about numbers in this instruction
1817 // makes things a lot simpler
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);
1824
1825 if (instance.type() == value) [[likely]] {
1826 for (const auto &child : instruction.children) {
1827 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1828 result = false;
1829 break;
1830 }
1831 }
1832 }
1833
1834 EVALUATE_END_PASS_THROUGH(ControlGroupWhenType);
1835}
1836
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);
1842}
1843
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();
1849
1850 for (const auto &child : instruction.children) {
1851 if (!EVALUATE_RECURSE(child, instance)) [[unlikely]] {
1852 result = false;
1853 break;
1854 }
1855 }
1856
1857 context.frame = parent_frame;
1858
1859 EVALUATE_END_PASS_THROUGH(ControlFrame);
1860}
1861
1862INSTRUCTION_HANDLER(ControlResource) {
1863 EVALUATE_BEGIN_PASS_THROUGH(ControlResource);
1864 assert(!instruction.children.empty());
1865 const auto index{assume_value_copy<ValueUnsignedInteger>(instruction.value)};
1866 assert(index != 0);
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]] {
1872 result = false;
1873 break;
1874 }
1875 }
1876
1877 context.evaluator->leave_resource(bindings);
1878 EVALUATE_END_PASS_THROUGH(ControlResource);
1879}
1880
1881INSTRUCTION_HANDLER(ControlDynamicAnchorJump) {
1882 EVALUATE_BEGIN_NO_PRECONDITION(ControlDynamicAnchorJump);
1883 result = true;
1884 const auto &target{
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{
1892 context.schema
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]] {
1897 result = false;
1898 break;
1899 }
1900 }
1901
1902 context.evaluator->leave_resource(bindings);
1903 EVALUATE_END(ControlDynamicAnchorJump);
1904}
1905
1906INSTRUCTION_HANDLER(ControlJump) {
1907 EVALUATE_BEGIN_NO_PRECONDITION(ControlJump);
1908 result = true;
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]] {
1916 result = false;
1917 break;
1918 }
1919 }
1920
1921 EVALUATE_END(ControlJump);
1922}
1923
1924INSTRUCTION_HANDLER(ControlResourceJump) {
1925 EVALUATE_BEGIN_NO_PRECONDITION(ControlResourceJump);
1926 result = true;
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]] {
1938 result = false;
1939 break;
1940 }
1941 }
1942
1943 context.evaluator->leave_resource(bindings);
1944 EVALUATE_END(ControlResourceJump);
1945}
1946
1947INSTRUCTION_HANDLER(AnnotationEmit) {
1948 const auto &value{assume_value<ValueJSON>(instruction.value)};
1949 EVALUATE_ANNOTATION(AnnotationEmit, context.evaluator->instance_location,
1950 value);
1951}
1952
1953INSTRUCTION_HANDLER(AnnotationEmitWrapped) {
1954 const auto &value{assume_value<ValueJSON>(instruction.value)};
1955 EVALUATE_ANNOTATION(AnnotationEmitWrapped,
1956 context.evaluator->instance_location, value);
1957}
1958
1959INSTRUCTION_HANDLER(AnnotationToParent) {
1960 const auto &value{assume_value<ValueJSON>(instruction.value)};
1961 EVALUATE_ANNOTATION(
1962 AnnotationToParent,
1963 // TODO: Can we avoid a copy of the instance location here?
1964 context.evaluator->instance_location.initial(), value);
1965}
1966
1967INSTRUCTION_HANDLER(AnnotationBasenameToParent) {
1968 EVALUATE_ANNOTATION(
1969 AnnotationBasenameToParent,
1970 // TODO: Can we avoid a copy of the instance location here?
1971 context.evaluator->instance_location.initial(),
1972 context.evaluator->instance_location.back().to_json());
1973}
1974
1975INSTRUCTION_HANDLER(Evaluate) {
1976 EVALUATE_BEGIN_NO_PRECONDITION(Evaluate);
1977 const auto &target{
1978 resolve_instance(instance, instruction.relative_instance_location)};
1979 context.evaluator->evaluate(&target);
1980 result = true;
1981 EVALUATE_END(Evaluate);
1982}
1983
1984INSTRUCTION_HANDLER(LogicalNot) {
1985 EVALUATE_BEGIN_NO_PRECONDITION(LogicalNot);
1986
1987 const auto &target{
1988 resolve_instance(instance, instruction.relative_instance_location)};
1989 for (const auto &child : instruction.children) {
1990 if (!EVALUATE_RECURSE(child, target)) [[likely]] {
1991 result = true;
1992 break;
1993 }
1994 }
1995
1996 EVALUATE_END(LogicalNot);
1997}
1998
1999INSTRUCTION_HANDLER(LogicalNotEvaluate) {
2000 EVALUATE_BEGIN_NO_PRECONDITION(LogicalNotEvaluate);
2001
2002 const auto checkpoint{context.evaluator->checkpoint()};
2003
2004 const auto &target{
2005 resolve_instance(instance, instruction.relative_instance_location)};
2006 for (const auto &child : instruction.children) {
2007 if (!EVALUATE_RECURSE(child, target)) [[likely]] {
2008 result = true;
2009 break;
2010 }
2011 }
2012
2013 context.evaluator->rewind(checkpoint);
2014
2015 EVALUATE_END(LogicalNotEvaluate);
2016}
2017
2018INSTRUCTION_HANDLER(LoopPropertiesUnevaluated) {
2019 EVALUATE_BEGIN_NON_STRING(LoopPropertiesUnevaluated, target.is_object());
2020 assert(!instruction.children.empty());
2021 result = true;
2022
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)) {
2026 continue;
2027 }
2028
2029 if constexpr (HasCallback) {
2030 context.evaluator->instance_location.push_back(entry.first);
2031 }
2032 for (const auto &child : instruction.children) {
2033 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2034 result = false;
2035 if constexpr (HasCallback) {
2036 context.evaluator->instance_location.pop_back();
2037 }
2038 EVALUATE_END(LoopPropertiesUnevaluated);
2039 }
2040 }
2041
2042 if constexpr (HasCallback) {
2043 context.evaluator->instance_location.pop_back();
2044 }
2045 }
2046
2047 context.evaluator->evaluate(&target);
2048 }
2049
2050 EVALUATE_END(LoopPropertiesUnevaluated);
2051}
2052
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;
2059 result = true;
2060 // Otherwise why emit this instruction?
2061 assert(!filter_strings.empty() || !filter_prefixes.empty() ||
2062 !filter_regexes.empty());
2063
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)) {
2067 continue;
2068 }
2069
2070 if (std::ranges::any_of(filter_prefixes,
2071 [&entry](const auto &prefix) -> bool {
2072 return entry.first.starts_with(prefix);
2073 })) {
2074 continue;
2075 }
2076
2077 if (std::ranges::any_of(filter_regexes,
2078 [&entry](const auto &pattern) -> bool {
2079 return matches(pattern.first, entry.first);
2080 })) {
2081 continue;
2082 }
2083
2084 if (context.evaluator->is_evaluated(&entry.second, context.frame)) {
2085 continue;
2086 }
2087
2088 if constexpr (HasCallback) {
2089 context.evaluator->instance_location.push_back(entry.first);
2090 }
2091 for (const auto &child : instruction.children) {
2092 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2093 result = false;
2094 if constexpr (HasCallback) {
2095 context.evaluator->instance_location.pop_back();
2096 }
2097 EVALUATE_END(LoopPropertiesUnevaluatedExcept);
2098 }
2099 }
2100
2101 if constexpr (HasCallback) {
2102 context.evaluator->instance_location.pop_back();
2103 }
2104 }
2105
2106 context.evaluator->evaluate(&target);
2107 }
2108
2109 EVALUATE_END(LoopPropertiesUnevaluatedExcept);
2110}
2111
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());
2116 result = true;
2117 // The loop already holds each property's value, so a child that only looks at
2118 // that value is decided here, rather than dispatched to and left to find the
2119 // property in the object a second time. The evaluation-marking variants of
2120 // those assertions are deliberately absent, as skipping them would skip the
2121 // marker. Some of them compare against the property name rather than the
2122 // value while `propertyNames` is being evaluated, which cannot reach here, as
2123 // that evaluation hands its children a null instance that this loop's
2124 // precondition rejects. Children are evaluated at `depth + 1`, which the
2125 // direct path does not route through the depth check. Every condition holds
2126 // for every child, so decide once rather than per property
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)};
2130 if (!match) {
2131 continue;
2132 }
2133
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) {
2138 bool child_ok;
2139 // A child whose instance location is not just the matched property is not
2140 // about the value this loop holds, which inlining a reference into the
2141 // group can produce, so it resolves its own target as usual
2142 if (direct_children && child.relative_instance_location.size() == 1)
2143 [[likely]] {
2144 switch (child.type) {
2145 case InstructionIndex::AssertionPropertyTypeStrict:
2146 child_ok = DIRECT(AssertionTypeStrict, entry.second,
2147 assume_value_copy<ValueType>(child.value));
2148 break;
2149 case InstructionIndex::AssertionPropertyType:
2150 child_ok = DIRECT(AssertionType, entry.second,
2151 assume_value_copy<ValueType>(child.value));
2152 break;
2153 case InstructionIndex::AssertionPropertyTypeStrictAny:
2154 child_ok = DIRECT(AssertionTypeStrictAny, entry.second,
2155 assume_value<ValueTypes>(child.value));
2156 break;
2157 case InstructionIndex::AssertionTypeAny:
2158 child_ok = DIRECT(AssertionTypeAny, entry.second,
2159 assume_value<ValueTypes>(child.value));
2160 break;
2161 case InstructionIndex::AssertionTypeStringBounded:
2162 child_ok = DIRECT(AssertionTypeStringBounded, entry.second,
2163 assume_value<ValueRange>(child.value));
2164 break;
2165 case InstructionIndex::AssertionTypeArrayBounded:
2166 child_ok = DIRECT(AssertionTypeArrayBounded, entry.second,
2167 assume_value<ValueRange>(child.value));
2168 break;
2169 case InstructionIndex::AssertionEqual:
2170 child_ok = DIRECT(AssertionEqual, entry.second,
2171 assume_value<ValueJSON>(child.value));
2172 break;
2173 case InstructionIndex::AssertionEqualsAny:
2174 child_ok = DIRECT(AssertionEqualsAny, entry.second,
2175 assume_value<ValueSet>(child.value));
2176 break;
2177 case InstructionIndex::AssertionEqualsAnyStringHash:
2178 child_ok = DIRECT(AssertionEqualsAnyStringHash, entry.second,
2179 assume_value<ValueStringHashes>(child.value));
2180 break;
2181 default:
2182 child_ok = EVALUATE_RECURSE(child, target);
2183 break;
2184 }
2185 } else {
2186 child_ok = EVALUATE_RECURSE(child, target);
2187 }
2188
2189 if (!child_ok) [[unlikely]] {
2190 result = false;
2191 EVALUATE_END(LoopPropertiesMatch);
2192 }
2193 }
2194 }
2195
2196 EVALUATE_END(LoopPropertiesMatch);
2197}
2198
2199INSTRUCTION_HANDLER(LoopPropertiesMatchRequired) {
2200 EVALUATE_BEGIN_NO_PRECONDITION(LoopPropertiesMatchRequired);
2201 const auto &target{
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);
2206
2207 // This stands for a presence assertion that rejected a non-object outright,
2208 // which is what let the compiler leave out the object type check beside it,
2209 // so the same refusal has to happen here rather than passing vacuously the
2210 // way a loop that only applies subschemas does
2211 if (!target.is_object()) [[unlikely]] {
2212 EVALUATE_END(LoopPropertiesMatchRequired);
2213 }
2214
2215 result = true;
2216 // The names a matching object has to define are proven by counting the
2217 // required entries this walk matched, as names are unique and an object
2218 // holds each of them once. Short-circuiting on a failing child leaves the
2219 // count short, which costs nothing, as the verdict is already false
2220 ValueUnsignedInteger matched{0};
2221 // Children are decided against the value this loop holds under exactly the
2222 // conditions `LoopPropertiesMatch` sets out, which this mirrors
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)};
2226 if (!match) {
2227 continue;
2228 }
2229
2230 if (std::get<3>(*match)) {
2231 matched += 1;
2232 }
2233
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) {
2238 bool child_ok;
2239 // A child whose instance location is not just the matched property is not
2240 // about the value this loop holds, which inlining a reference into the
2241 // group can produce, so it resolves its own target as usual
2242 if (direct_children && child.relative_instance_location.size() == 1)
2243 [[likely]] {
2244 switch (child.type) {
2245 case InstructionIndex::AssertionPropertyTypeStrict:
2246 child_ok = DIRECT(AssertionTypeStrict, entry.second,
2247 assume_value_copy<ValueType>(child.value));
2248 break;
2249 case InstructionIndex::AssertionPropertyType:
2250 child_ok = DIRECT(AssertionType, entry.second,
2251 assume_value_copy<ValueType>(child.value));
2252 break;
2253 case InstructionIndex::AssertionPropertyTypeStrictAny:
2254 child_ok = DIRECT(AssertionTypeStrictAny, entry.second,
2255 assume_value<ValueTypes>(child.value));
2256 break;
2257 case InstructionIndex::AssertionTypeAny:
2258 child_ok = DIRECT(AssertionTypeAny, entry.second,
2259 assume_value<ValueTypes>(child.value));
2260 break;
2261 case InstructionIndex::AssertionTypeStringBounded:
2262 child_ok = DIRECT(AssertionTypeStringBounded, entry.second,
2263 assume_value<ValueRange>(child.value));
2264 break;
2265 case InstructionIndex::AssertionTypeArrayBounded:
2266 child_ok = DIRECT(AssertionTypeArrayBounded, entry.second,
2267 assume_value<ValueRange>(child.value));
2268 break;
2269 case InstructionIndex::AssertionEqual:
2270 child_ok = DIRECT(AssertionEqual, entry.second,
2271 assume_value<ValueJSON>(child.value));
2272 break;
2273 case InstructionIndex::AssertionEqualsAny:
2274 child_ok = DIRECT(AssertionEqualsAny, entry.second,
2275 assume_value<ValueSet>(child.value));
2276 break;
2277 case InstructionIndex::AssertionEqualsAnyStringHash:
2278 child_ok = DIRECT(AssertionEqualsAnyStringHash, entry.second,
2279 assume_value<ValueStringHashes>(child.value));
2280 break;
2281 default:
2282 child_ok = EVALUATE_RECURSE(child, target);
2283 break;
2284 }
2285 } else {
2286 child_ok = EVALUATE_RECURSE(child, target);
2287 }
2288
2289 if (!child_ok) [[unlikely]] {
2290 result = false;
2291 EVALUATE_END(LoopPropertiesMatchRequired);
2292 }
2293 }
2294 }
2295
2296 result = matched == value.second;
2297
2298 EVALUATE_END(LoopPropertiesMatchRequired);
2299}
2300
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());
2305 result = true;
2306 // The loop already holds each property's value, so a child that only looks at
2307 // that value is decided here, rather than dispatched to and left to find the
2308 // property in the object a second time. The evaluation-marking variants of
2309 // those assertions are deliberately absent, as skipping them would skip the
2310 // marker. Some of them compare against the property name rather than the
2311 // value while `propertyNames` is being evaluated, which cannot reach here, as
2312 // that evaluation hands its children a null instance that this loop's
2313 // precondition rejects. Children are evaluated at `depth + 1`, which the
2314 // direct path does not route through the depth check. Every condition holds
2315 // for every child, so decide once rather than per property
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]] {
2320 result = false;
2321 break;
2322 }
2323
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) {
2328 bool child_ok;
2329 // A child whose instance location is not just the matched property is not
2330 // about the value this loop holds, which inlining a reference into the
2331 // group can produce, so it resolves its own target as usual
2332 if (direct_children && child.relative_instance_location.size() == 1)
2333 [[likely]] {
2334 switch (child.type) {
2335 case InstructionIndex::AssertionPropertyTypeStrict:
2336 child_ok = DIRECT(AssertionTypeStrict, entry.second,
2337 assume_value_copy<ValueType>(child.value));
2338 break;
2339 case InstructionIndex::AssertionPropertyType:
2340 child_ok = DIRECT(AssertionType, entry.second,
2341 assume_value_copy<ValueType>(child.value));
2342 break;
2343 case InstructionIndex::AssertionPropertyTypeStrictAny:
2344 child_ok = DIRECT(AssertionTypeStrictAny, entry.second,
2345 assume_value<ValueTypes>(child.value));
2346 break;
2347 case InstructionIndex::AssertionTypeAny:
2348 child_ok = DIRECT(AssertionTypeAny, entry.second,
2349 assume_value<ValueTypes>(child.value));
2350 break;
2351 case InstructionIndex::AssertionTypeStringBounded:
2352 child_ok = DIRECT(AssertionTypeStringBounded, entry.second,
2353 assume_value<ValueRange>(child.value));
2354 break;
2355 case InstructionIndex::AssertionTypeArrayBounded:
2356 child_ok = DIRECT(AssertionTypeArrayBounded, entry.second,
2357 assume_value<ValueRange>(child.value));
2358 break;
2359 case InstructionIndex::AssertionEqual:
2360 child_ok = DIRECT(AssertionEqual, entry.second,
2361 assume_value<ValueJSON>(child.value));
2362 break;
2363 case InstructionIndex::AssertionEqualsAny:
2364 child_ok = DIRECT(AssertionEqualsAny, entry.second,
2365 assume_value<ValueSet>(child.value));
2366 break;
2367 case InstructionIndex::AssertionEqualsAnyStringHash:
2368 child_ok = DIRECT(AssertionEqualsAnyStringHash, entry.second,
2369 assume_value<ValueStringHashes>(child.value));
2370 break;
2371 default:
2372 child_ok = EVALUATE_RECURSE(child, target);
2373 break;
2374 }
2375 } else {
2376 child_ok = EVALUATE_RECURSE(child, target);
2377 }
2378
2379 if (!child_ok) [[unlikely]] {
2380 result = false;
2381 EVALUATE_END(LoopPropertiesMatchClosed);
2382 }
2383 }
2384 }
2385
2386 EVALUATE_END(LoopPropertiesMatchClosed);
2387}
2388
2389INSTRUCTION_HANDLER(LoopProperties) {
2390 EVALUATE_BEGIN_NON_STRING(LoopProperties, target.is_object());
2391 assert(!instruction.children.empty());
2392 result = true;
2393 for (const auto &entry : target.as_object()) {
2394 if constexpr (HasCallback) {
2395 context.evaluator->instance_location.push_back(entry.first);
2396 }
2397
2398 for (const auto &child : instruction.children) {
2399 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2400 result = false;
2401
2402 if constexpr (HasCallback) {
2403 context.evaluator->instance_location.pop_back();
2404 }
2405
2406 EVALUATE_END(LoopProperties);
2407 }
2408 }
2409
2410 if constexpr (HasCallback) {
2411 context.evaluator->instance_location.pop_back();
2412 }
2413 }
2414
2415 EVALUATE_END(LoopProperties);
2416}
2417
2418INSTRUCTION_HANDLER(LoopPropertiesEvaluate) {
2419 EVALUATE_BEGIN_NON_STRING(LoopPropertiesEvaluate, target.is_object());
2420 // The subschema may compile to no children, in which case every property
2421 // trivially matches and only the evaluation marking below takes effect
2422 result = true;
2423 for (const auto &entry : target.as_object()) {
2424 if constexpr (HasCallback) {
2425 context.evaluator->instance_location.push_back(entry.first);
2426 }
2427
2428 for (const auto &child : instruction.children) {
2429 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2430 result = false;
2431
2432 if constexpr (HasCallback) {
2433 context.evaluator->instance_location.pop_back();
2434 }
2435
2436 EVALUATE_END(LoopPropertiesEvaluate);
2437 }
2438 }
2439
2440 if constexpr (HasCallback) {
2441 context.evaluator->instance_location.pop_back();
2442 }
2443 }
2444
2445 context.evaluator->evaluate(&target);
2446 EVALUATE_END(LoopPropertiesEvaluate);
2447}
2448
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)};
2453 result = true;
2454 for (const auto &entry : target.as_object()) {
2455 if (!matches(value.first, entry.first)) {
2456 continue;
2457 }
2458
2459 if constexpr (HasCallback) {
2460 context.evaluator->instance_location.push_back(entry.first);
2461 }
2462
2463 for (const auto &child : instruction.children) {
2464 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2465 result = false;
2466
2467 if constexpr (HasCallback) {
2468 context.evaluator->instance_location.pop_back();
2469 }
2470
2471 EVALUATE_END(LoopPropertiesRegex);
2472 }
2473 }
2474
2475 if constexpr (HasCallback) {
2476 context.evaluator->instance_location.pop_back();
2477 }
2478 }
2479
2480 EVALUATE_END(LoopPropertiesRegex);
2481}
2482
2483INSTRUCTION_HANDLER(LoopPropertiesRegexClosed) {
2484 EVALUATE_BEGIN_NON_STRING(LoopPropertiesRegexClosed, target.is_object());
2485 result = true;
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]] {
2489 result = false;
2490 break;
2491 }
2492
2493 if (instruction.children.empty()) {
2494 continue;
2495 }
2496
2497 if constexpr (HasCallback) {
2498 context.evaluator->instance_location.push_back(entry.first);
2499 }
2500
2501 for (const auto &child : instruction.children) {
2502 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2503 result = false;
2504
2505 if constexpr (HasCallback) {
2506 context.evaluator->instance_location.pop_back();
2507 }
2508
2509 EVALUATE_END(LoopPropertiesRegexClosed);
2510 }
2511 }
2512
2513 if constexpr (HasCallback) {
2514 context.evaluator->instance_location.pop_back();
2515 }
2516 }
2517
2518 EVALUATE_END(LoopPropertiesRegexClosed);
2519}
2520
2521INSTRUCTION_HANDLER(LoopPropertiesStartsWith) {
2522 EVALUATE_BEGIN_NON_STRING(LoopPropertiesStartsWith, target.is_object());
2523 assert(!instruction.children.empty());
2524 result = true;
2525 const auto &value{assume_value<ValueString>(instruction.value)};
2526 for (const auto &entry : target.as_object()) {
2527 if (!entry.first.starts_with(value)) {
2528 continue;
2529 }
2530
2531 if constexpr (HasCallback) {
2532 context.evaluator->instance_location.push_back(entry.first);
2533 }
2534
2535 for (const auto &child : instruction.children) {
2536 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2537 result = false;
2538
2539 if constexpr (HasCallback) {
2540 context.evaluator->instance_location.pop_back();
2541 }
2542
2543 EVALUATE_END(LoopPropertiesStartsWith);
2544 }
2545 }
2546
2547 if constexpr (HasCallback) {
2548 context.evaluator->instance_location.pop_back();
2549 }
2550 }
2551
2552 EVALUATE_END(LoopPropertiesStartsWith);
2553}
2554
2555INSTRUCTION_HANDLER(LoopPropertiesExcept) {
2556 EVALUATE_BEGIN_NON_STRING(LoopPropertiesExcept, target.is_object());
2557 assert(!instruction.children.empty());
2558 result = true;
2559 const auto &value{assume_value<ValuePropertyFilter>(instruction.value)};
2560 const auto &[filter_strings, filter_prefixes, filter_regexes] = value;
2561 // Otherwise why emit this instruction?
2562 assert(!filter_strings.empty() || !filter_prefixes.empty() ||
2563 !filter_regexes.empty());
2564
2565 for (const auto &entry : target.as_object()) {
2566 if (filter_strings.contains(entry.first, entry.hash)) {
2567 continue;
2568 }
2569
2570 if (std::ranges::any_of(filter_prefixes,
2571 [&entry](const auto &prefix) -> bool {
2572 return entry.first.starts_with(prefix);
2573 })) {
2574 continue;
2575 }
2576
2577 if (std::ranges::any_of(filter_regexes,
2578 [&entry](const auto &pattern) -> bool {
2579 return matches(pattern.first, entry.first);
2580 })) {
2581 continue;
2582 }
2583
2584 if constexpr (HasCallback) {
2585 context.evaluator->instance_location.push_back(entry.first);
2586 }
2587
2588 for (const auto &child : instruction.children) {
2589 if (!EVALUATE_RECURSE(child, entry.second)) [[unlikely]] {
2590 result = false;
2591
2592 if constexpr (HasCallback) {
2593 context.evaluator->instance_location.pop_back();
2594 }
2595
2596 EVALUATE_END(LoopPropertiesExcept);
2597 }
2598 }
2599
2600 if constexpr (HasCallback) {
2601 context.evaluator->instance_location.pop_back();
2602 }
2603 }
2604
2605 EVALUATE_END(LoopPropertiesExcept);
2606}
2607
2608INSTRUCTION_HANDLER(LoopPropertiesType) {
2609 EVALUATE_BEGIN_NON_STRING(LoopPropertiesType, target.is_object());
2610 result = true;
2611 const auto value{assume_value_copy<ValueType>(instruction.value)};
2612 for (const auto &entry : target.as_object()) {
2613 if (entry.second.type() != value &&
2614 // In non-strict mode, we consider a real number that represents an
2615 // integer to be an integer
2616 (value != JSON::Type::Integer || !entry.second.is_integral()))
2617 [[unlikely]] {
2618 result = false;
2619 break;
2620 }
2621 }
2622
2623 EVALUATE_END(LoopPropertiesType);
2624}
2625
2626INSTRUCTION_HANDLER(LoopPropertiesTypeEvaluate) {
2627 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeEvaluate, target.is_object());
2628 result = true;
2629 const auto value{assume_value_copy<ValueType>(instruction.value)};
2630 for (const auto &entry : target.as_object()) {
2631 if (entry.second.type() != value &&
2632 // In non-strict mode, we consider a real number that represents an
2633 // integer to be an integer
2634 (value != JSON::Type::Integer || !entry.second.is_integral()))
2635 [[unlikely]] {
2636 result = false;
2637 EVALUATE_END(LoopPropertiesTypeEvaluate);
2638 }
2639 }
2640
2641 context.evaluator->evaluate(&target);
2642 EVALUATE_END(LoopPropertiesTypeEvaluate);
2643}
2644
2645INSTRUCTION_HANDLER(LoopPropertiesExactlyTypeStrict) {
2646 EVALUATE_BEGIN_NO_PRECONDITION(LoopPropertiesExactlyTypeStrict);
2647 const auto &target{
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);
2652 }
2653
2654 const auto &object{target.as_object()};
2655 if (object.size() == value.second.size()) [[likely]] {
2656 // Otherwise why emit this instruction?
2657 assert(!value.second.empty());
2658 result = true;
2659 for (const auto &entry : object) {
2660 // The property count already matches the required set, so confirming
2661 // that every property is one of the required names is what makes the
2662 // object exactly the required set, and the value must be of the type
2663 if (!value.second.contains(entry.first, entry.hash) ||
2664 effective_type_strict_real(entry.second) != value.first)
2665 [[unlikely]] {
2666 result = false;
2667 break;
2668 }
2669 }
2670 }
2671
2672 EVALUATE_END(LoopPropertiesExactlyTypeStrict);
2673}
2674
2675INSTRUCTION_HANDLER(LoopPropertiesExactlyTypeStrictHash) {
2676 EVALUATE_BEGIN_NO_PRECONDITION(LoopPropertiesExactlyTypeStrictHash);
2677 const auto &target{
2678 resolve_instance(instance, instruction.relative_instance_location)};
2679 // TODO: Take advantage of the table of contents structure to speed up checks
2680 const auto &value{assume_value<ValueTypedHashes>(instruction.value)};
2681
2682 if (!target.is_object()) [[unlikely]] {
2683 EVALUATE_END(LoopPropertiesExactlyTypeStrictHash);
2684 }
2685
2686 const auto &object{target.as_object()};
2687 const auto size{object.size()};
2688 if (size == value.second.first.size()) [[likely]] {
2689 // Otherwise why emit this instruction?
2690 assert(!value.second.first.empty());
2691
2692 // The idea is to first assume the object property ordering and the
2693 // hashes collection aligns. If they don't we do a full comparison
2694 // from where we left of.
2695
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);
2700 }
2701
2702 // A hash match alone does not prove the property is required, as two
2703 // distinct names can share a hash, so the name itself is confirmed
2704 if (entry.hash != value.second.first[index].first ||
2705 entry.first != value.second.first[index].second) {
2706 break;
2707 }
2708
2709 index += 1;
2710 }
2711
2712 result = true;
2713 if (index < size) {
2714 auto iterator = object.cbegin();
2715 // Continue where we left
2716 std::advance(iterator, index);
2717 for (; iterator != object.cend(); ++iterator) {
2718 if (effective_type_strict_real(iterator->second) != value.first) {
2719 result = false;
2720 break;
2721 }
2722
2723 // NOLINTNEXTLINE(modernize-use-ranges)
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;
2728 })) {
2729 result = false;
2730 break;
2731 }
2732 }
2733 }
2734 }
2735
2736 EVALUATE_END(LoopPropertiesExactlyTypeStrictHash);
2737}
2738
2739INSTRUCTION_HANDLER(LoopPropertiesTypeStrict) {
2740 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeStrict, target.is_object());
2741 result = true;
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]] {
2745 result = false;
2746 break;
2747 }
2748 }
2749
2750 EVALUATE_END(LoopPropertiesTypeStrict);
2751}
2752
2753INSTRUCTION_HANDLER(LoopPropertiesTypeStrictEvaluate) {
2754 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeStrictEvaluate,
2755 target.is_object());
2756 result = true;
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]] {
2760 result = false;
2761 EVALUATE_END(LoopPropertiesTypeStrictEvaluate);
2762 }
2763 }
2764
2765 context.evaluator->evaluate(&target);
2766 EVALUATE_END(LoopPropertiesTypeStrictEvaluate);
2767}
2768
2769INSTRUCTION_HANDLER(LoopPropertiesTypeStrictAny) {
2770 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeStrictAny, target.is_object());
2771 result = true;
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);
2777
2778 EVALUATE_END(LoopPropertiesTypeStrictAny);
2779}
2780
2781INSTRUCTION_HANDLER(LoopPropertiesTypeStrictAnyEvaluate) {
2782 EVALUATE_BEGIN_NON_STRING(LoopPropertiesTypeStrictAnyEvaluate,
2783 target.is_object());
2784 result = true;
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);
2792 }
2793
2794 context.evaluator->evaluate(&target);
2795 EVALUATE_END(LoopPropertiesTypeStrictAnyEvaluate);
2796}
2797
2798INSTRUCTION_HANDLER(LoopKeys) {
2799 EVALUATE_BEGIN_NON_STRING(LoopKeys, target.is_object());
2800 assert(!instruction.children.empty());
2801 result = true;
2802 for (const auto &entry : target.as_object()) {
2803 if constexpr (HasCallback) {
2804 context.evaluator->instance_location.push_back(entry.first);
2805 }
2806
2807 for (const auto &child : instruction.children) {
2808 if (!EVALUATE_RECURSE_ON_PROPERTY_NAME(child, Evaluator::NULL_VALUE,
2809 entry.first)) [[unlikely]] {
2810 result = false;
2811
2812 if constexpr (HasCallback) {
2813 context.evaluator->instance_location.pop_back();
2814 }
2815
2816 EVALUATE_END(LoopKeys);
2817 }
2818 }
2819
2820 if constexpr (HasCallback) {
2821 context.evaluator->instance_location.pop_back();
2822 }
2823 }
2824
2825 EVALUATE_END(LoopKeys);
2826}
2827
2828INSTRUCTION_HANDLER(LoopItems) {
2829 EVALUATE_BEGIN_NON_STRING(LoopItems, target.is_array());
2830 assert(!instruction.children.empty());
2831 result = true;
2832
2833 // To avoid index lookups and unnecessary conditionals
2834 if constexpr (!HasCallback) {
2835 // Children are evaluated at `depth + 1`, which the direct path does not
2836 // route through the depth check, and that holds for every child, so decide
2837 // once rather than per element
2838 const auto direct_children{depth < DEPTH_LIMIT};
2839 for (const auto &new_instance : target.as_array()) {
2840 for (const auto &child : instruction.children) {
2841 bool child_ok;
2842 if (direct_children) [[likely]] {
2843 switch (child.type) {
2844 case InstructionIndex::AssertionTypeArrayBounded:
2845 child_ok =
2846 DIRECT(AssertionTypeArrayBounded,
2847 resolve_instance(new_instance,
2848 child.relative_instance_location),
2849 assume_value<ValueRange>(child.value));
2850 break;
2851 default:
2852 child_ok = EVALUATE_RECURSE(child, new_instance);
2853 break;
2854 }
2855 } else {
2856 child_ok = EVALUATE_RECURSE(child, new_instance);
2857 }
2858
2859 if (!child_ok) [[unlikely]] {
2860 result = false;
2861 EVALUATE_END(LoopItems);
2862 }
2863 }
2864 }
2865 } else {
2866 for (std::size_t index = 0; index < target.array_size(); index++) {
2867 if constexpr (HasCallback) {
2868 context.evaluator->instance_location.push_back(index);
2869 }
2870
2871 const auto &new_instance{target.at(index)};
2872 for (const auto &child : instruction.children) {
2873 if (!EVALUATE_RECURSE(child, new_instance)) [[unlikely]] {
2874 result = false;
2875
2876 if constexpr (HasCallback) {
2877 context.evaluator->instance_location.pop_back();
2878 }
2879
2880 EVALUATE_END(LoopItems);
2881 }
2882 }
2883
2884 if constexpr (HasCallback) {
2885 context.evaluator->instance_location.pop_back();
2886 }
2887 }
2888 }
2889
2890 EVALUATE_END(LoopItems);
2891}
2892
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());
2898 result = true;
2899 for (std::size_t index = value; index < target.array_size(); index++) {
2900 if constexpr (HasCallback) {
2901 context.evaluator->instance_location.push_back(index);
2902 }
2903
2904 const auto &new_instance{target.at(index)};
2905 for (const auto &child : instruction.children) {
2906 if (!EVALUATE_RECURSE(child, new_instance)) [[unlikely]] {
2907 result = false;
2908
2909 if constexpr (HasCallback) {
2910 context.evaluator->instance_location.pop_back();
2911 }
2912
2913 EVALUATE_END(LoopItemsFrom);
2914 }
2915 }
2916
2917 if constexpr (HasCallback) {
2918 context.evaluator->instance_location.pop_back();
2919 }
2920 }
2921
2922 EVALUATE_END(LoopItemsFrom);
2923}
2924
2925INSTRUCTION_HANDLER(LoopItemsUnevaluated) {
2926 EVALUATE_BEGIN_NON_STRING(LoopItemsUnevaluated, target.is_array());
2927 // The subschema may compile to no children, in which case every item
2928 // trivially matches and only the evaluation marking below takes effect
2929 result = true;
2930
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)) {
2935 continue;
2936 }
2937
2938 if constexpr (HasCallback) {
2939 context.evaluator->instance_location.push_back(index);
2940 }
2941 for (const auto &child : instruction.children) {
2942 if (!EVALUATE_RECURSE(child, new_instance)) [[unlikely]] {
2943 result = false;
2944 if constexpr (HasCallback) {
2945 context.evaluator->instance_location.pop_back();
2946 }
2947 EVALUATE_END(LoopItemsUnevaluated);
2948 }
2949 }
2950
2951 if constexpr (HasCallback) {
2952 context.evaluator->instance_location.pop_back();
2953 }
2954 }
2955
2956 context.evaluator->evaluate(&target);
2957 }
2958
2959 EVALUATE_END(LoopItemsUnevaluated);
2960}
2961
2962INSTRUCTION_HANDLER(LoopItemsType) {
2963 EVALUATE_BEGIN_NON_STRING(LoopItemsType, target.is_array());
2964 result = true;
2965 const auto value{assume_value_copy<ValueType>(instruction.value)};
2966 for (const auto &entry : target.as_array()) {
2967 if (entry.type() != value &&
2968 // In non-strict mode, we consider a real number that represents an
2969 // integer to be an integer
2970 (value != JSON::Type::Integer || !entry.is_integral())) [[unlikely]] {
2971 result = false;
2972 break;
2973 }
2974 }
2975
2976 EVALUATE_END(LoopItemsType);
2977}
2978
2979INSTRUCTION_HANDLER(LoopItemsTypeStrict) {
2980 EVALUATE_BEGIN_NON_STRING(LoopItemsTypeStrict, target.is_array());
2981 result = true;
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]] {
2985 result = false;
2986 break;
2987 }
2988 }
2989
2990 EVALUATE_END(LoopItemsTypeStrict);
2991}
2992
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());
2997
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);
3001
3002 EVALUATE_END(LoopItemsTypeStrictAny);
3003}
3004
3005INSTRUCTION_HANDLER(LoopItemsPropertiesExactlyTypeStrictHash) {
3006 EVALUATE_BEGIN_NO_PRECONDITION(LoopItemsPropertiesExactlyTypeStrictHash);
3007 const auto &target{
3008 resolve_instance(instance, instruction.relative_instance_location)};
3009 // TODO: Take advantage of the table of contents structure to speed up checks
3010 const auto &value{assume_value<ValueTypedHashes>(instruction.value)};
3011 // Otherwise why emit this instruction?
3012 assert(!value.second.first.empty());
3013
3014 result = true;
3015
3016 if (!target.is_array()) [[unlikely]] {
3017 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3018 }
3019
3020 const auto hashes_size{value.second.first.size()};
3021 for (const auto &item : target.as_array()) {
3022 if (!item.is_object()) [[unlikely]] {
3023 result = false;
3024 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3025 }
3026
3027 const auto &object{item.as_object()};
3028 const auto size{object.size()};
3029 if (size != hashes_size) [[unlikely]] {
3030 result = false;
3031 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3032 }
3033
3034 // Unroll, for performance reasons, for small collections. A perfect hash
3035 // records the key's size, which the compiler requires of every key here,
3036 // so matching one proves the keys equal
3037 if (hashes_size == 3) {
3038 for (const auto &entry : object) {
3039 if (effective_type_strict_real(entry.second) != value.first)
3040 // NOLINTNEXTLINE(bugprone-branch-clone)
3041 [[unlikely]] {
3042 result = false;
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)) {
3047 result = false;
3048 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3049 }
3050 }
3051 } else if (hashes_size == 2) {
3052 for (const auto &entry : object) {
3053 if (effective_type_strict_real(entry.second) != value.first)
3054 // NOLINTNEXTLINE(bugprone-branch-clone)
3055 [[unlikely]] {
3056 result = false;
3057 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3058 } else if (!(entry.hash == value.second.first[0].first ||
3059 entry.hash == value.second.first[1].first)) {
3060 result = false;
3061 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3062 }
3063 }
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]] {
3068 result = false;
3069 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3070 }
3071 } else {
3072 std::size_t index{0};
3073 for (const auto &entry : object) {
3074 if (effective_type_strict_real(entry.second) != value.first)
3075 // NOLINTNEXTLINE(bugprone-branch-clone)
3076 [[unlikely]] {
3077 result = false;
3078 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3079 } else if (entry.hash == value.second.first[index].first) {
3080 index += 1;
3081 continue;
3082 } else if (!std::ranges::any_of(
3083 value.second.first,
3084 [&entry](const auto &hash_entry) -> bool {
3085 return hash_entry.first == entry.hash;
3086 })) {
3087 result = false;
3088 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3089 } else {
3090 index += 1;
3091 }
3092 }
3093 }
3094 }
3095
3096 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash);
3097}
3098
3099INSTRUCTION_HANDLER(LoopItemsPropertiesExactlyTypeStrictHash3) {
3100 EVALUATE_BEGIN_NO_PRECONDITION(LoopItemsPropertiesExactlyTypeStrictHash3);
3101 const auto &target{
3102 resolve_instance(instance, instruction.relative_instance_location)};
3103 // TODO: Take advantage of the table of contents structure to speed up checks
3104 const auto &value{assume_value<ValueTypedHashes>(instruction.value)};
3105 assert(value.second.first.size() == 3);
3106 // Otherwise why emit this instruction?
3107 assert(!value.second.first.empty());
3108
3109 if (!target.is_array()) [[unlikely]] {
3110 result = true;
3111 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3112 }
3113
3114 for (const auto &item : target.as_array()) {
3115 if (!item.is_object()) [[unlikely]] {
3116 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3117 }
3118
3119 const auto &object{item.as_object()};
3120 if (object.size() != 3) [[unlikely]] {
3121 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3122 }
3123
3124 const auto &value_1{object.at(0)};
3125 const auto &value_2{object.at(1)};
3126 const auto &value_3{object.at(2)};
3127
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)
3131 [[unlikely]] {
3132 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3133 }
3134
3135 // A perfect hash records the key's size, which the compiler requires of
3136 // every key here, so matching one proves the keys equal and the unrolled
3137 // permutation check compares nothing but hashes
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)) {
3156 continue;
3157 }
3158 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3159 }
3160
3161 result = true;
3162 EVALUATE_END(LoopItemsPropertiesExactlyTypeStrictHash3);
3163}
3164
3165INSTRUCTION_DIRECT_COPY(LoopItemsIntegerBounded, ValueIntegerBounds) {
3166 if (!target.is_array() || target.empty()) {
3167 return true;
3168 }
3169 for (const auto &element : target.as_array()) {
3170 if (!element.is_number()) [[unlikely]] {
3171 return false;
3172 }
3173
3174 if (element.is_integer()) {
3175 const auto integer{element.to_integer()};
3176 if (integer < value.first || integer > value.second) [[unlikely]] {
3177 return false;
3178 }
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]] {
3183 return false;
3184 }
3185 } else {
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]] {
3189 return false;
3190 }
3191 }
3192 }
3193 return true;
3194}
3195
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);
3202}
3203
3204INSTRUCTION_HANDLER(LoopItemsIntegerBoundedSized) {
3205 const auto &value{
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);
3215 }
3216
3217 result = true;
3218 for (const auto &element : target.as_array()) {
3219 if (!element.is_number()) [[unlikely]] {
3220 result = false;
3221 break;
3222 }
3223
3224 if (element.is_integer()) {
3225 const auto integer{element.to_integer()};
3226 if (integer < integer_bounds.first || integer > integer_bounds.second)
3227 [[unlikely]] {
3228 result = false;
3229 break;
3230 }
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]] {
3235 result = false;
3236 break;
3237 }
3238 } else {
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]] {
3242 result = false;
3243 break;
3244 }
3245 }
3246 }
3247
3248 EVALUATE_END(LoopItemsIntegerBoundedSized);
3249}
3250
3251// `items` whose subschema is itself an `items` with a size bound compiles to a
3252// loop whose body is two instructions that both only look at the item, which
3253// means a dispatch per item of the outer array. Nesting the two loops here
3254// decides the whole shape in one instruction instead
3255INSTRUCTION_HANDLER(LoopItemsArrayBoundedTypeStrictAny) {
3256 EVALUATE_BEGIN_NON_STRING(LoopItemsArrayBoundedTypeStrictAny,
3257 target.is_array());
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());
3263 // Require early breaking
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,
3267 minimum, maximum)
3268 : items_are_bounded_arrays_in_type_set<true>(target, types,
3269 minimum, maximum);
3270
3271 EVALUATE_END(LoopItemsArrayBoundedTypeStrictAny);
3272}
3273
3274INSTRUCTION_HANDLER(LoopContains) {
3275 EVALUATE_BEGIN_NON_STRING(LoopContains, target.is_array());
3276 // The subschema may compile to no children, in which case every array
3277 // element trivially matches and the range check alone decides the result
3278 const auto &value{assume_value<ValueRange>(instruction.value)};
3279 const auto &[minimum, maximum, is_exhaustive] = value;
3280 // Note that the range may be unsatisfiable, as `minContains` and
3281 // `maxContains` are free to invert it, in which case no array matches
3282 result = minimum == 0;
3283 auto match_count{
3284 std::numeric_limits<std::remove_cvref_t<decltype(minimum)>>::min()};
3285
3286 for (std::size_t index = 0; index < target.array_size(); index++) {
3287 if constexpr (HasCallback) {
3288 context.evaluator->instance_location.push_back(index);
3289 }
3290
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)) {
3295 subresult = false;
3296 break;
3297 }
3298 }
3299
3300 if constexpr (HasCallback) {
3301 context.evaluator->instance_location.pop_back();
3302 }
3303
3304 if (subresult) {
3305 match_count += 1;
3306
3307 // Exceeding the upper bound is definitely a failure
3308 if (maximum.has_value() && match_count > maximum.value()) {
3309 result = false;
3310
3311 // Note that here we don't want to consider whether to run
3312 // exhaustively or not. At this point, its already a failure,
3313 // and anything that comes after would not run at all anyway
3314 break;
3315 }
3316
3317 if (match_count >= minimum) {
3318 result = true;
3319
3320 // Exceeding the lower bound when there is no upper bound
3321 // is definitely a success
3322 if (!maximum.has_value() && !is_exhaustive) {
3323 break;
3324 }
3325 }
3326 }
3327 }
3328
3329 EVALUATE_END(LoopContains);
3330}
3331// NOLINTEND(readability-identifier-naming)
3332
3333#undef INSTRUCTION_HANDLER
3334
3335template <bool HasCallback>
3336using DispatchHandler = bool (*)(const sourcemeta::blaze::Instruction &,
3337 const sourcemeta::core::JSON &, std::uint64_t,
3339
3340template <bool HasCallback>
3341// Must have same order as InstructionIndex
3342// NOLINTNEXTLINE(modernize-avoid-c-arrays)
3343static constexpr DispatchHandler<HasCallback> HANDLERS[113] = {
3344 AssertionFail,
3345 AssertionDefines,
3346 AssertionDefinesStrict,
3347 AssertionDefinesAll,
3348 AssertionDefinesAllStrict,
3349 AssertionDefinesExactly,
3350 AssertionDefinesExactlyStrict,
3351 AssertionDefinesExactlyStrictHash3,
3352 AssertionPropertyDependencies,
3353 AssertionType,
3354 AssertionTypeAny,
3355 AssertionTypeStrict,
3356 AssertionTypeStrictAny,
3357 AssertionNotTypeStrictAny,
3358 AssertionTypeStringBounded,
3359 AssertionTypeStringUpper,
3360 AssertionTypeArrayBounded,
3361 AssertionTypeArrayUpper,
3362 AssertionTypeObjectBounded,
3363 AssertionTypeObjectUpper,
3364 AssertionRegex,
3365 AssertionStringSizeLess,
3366 AssertionStringSizeGreater,
3367 AssertionArraySizeLess,
3368 AssertionArraySizeGreater,
3369 AssertionObjectSizeLess,
3370 AssertionObjectSizeGreater,
3371 AssertionEqual,
3372 AssertionEqualsAny,
3373 AssertionEqualsAnyStringHash,
3374 AssertionGreaterEqual,
3375 AssertionLessEqual,
3376 AssertionGreater,
3377 AssertionLess,
3378 AssertionUnique,
3379 AssertionDivisible,
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,
3398 AnnotationEmit,
3399 AnnotationEmitWrapped,
3400 AnnotationToParent,
3401 AnnotationBasenameToParent,
3402 Evaluate,
3403 LogicalNot,
3404 LogicalNotEvaluate,
3405 LogicalOr,
3406 LogicalOrEvaluate,
3407 LogicalAnd,
3408 LogicalXor,
3409 LogicalXorEvaluate,
3410 LogicalCondition,
3411 LogicalConditionEvaluate,
3412 LogicalWhenType,
3413 LogicalWhenDefines,
3414 LogicalWhenArraySizeGreater,
3415 LoopPropertiesUnevaluated,
3416 LoopPropertiesUnevaluatedExcept,
3417 LoopPropertiesMatch,
3418 LoopPropertiesMatchClosed,
3419 LoopPropertiesMatchRequired,
3420 LoopProperties,
3421 LoopPropertiesEvaluate,
3422 LoopPropertiesRegex,
3423 LoopPropertiesRegexClosed,
3424 LoopPropertiesStartsWith,
3425 LoopPropertiesExcept,
3426 LoopPropertiesType,
3427 LoopPropertiesTypeEvaluate,
3428 LoopPropertiesExactlyTypeStrict,
3429 LoopPropertiesExactlyTypeStrictHash,
3430 LoopPropertiesTypeStrict,
3431 LoopPropertiesTypeStrictEvaluate,
3432 LoopPropertiesTypeStrictAny,
3433 LoopPropertiesTypeStrictAnyEvaluate,
3434 LoopKeys,
3435 LoopItems,
3436 LoopItemsFrom,
3437 LoopItemsUnevaluated,
3438 LoopItemsType,
3439 LoopItemsTypeStrict,
3440 LoopItemsTypeStrictAny,
3441 LoopItemsPropertiesExactlyTypeStrictHash,
3442 LoopItemsPropertiesExactlyTypeStrictHash3,
3443 LoopItemsIntegerBounded,
3444 LoopItemsIntegerBoundedSized,
3445 LoopItemsArrayBoundedTypeStrictAny,
3446 LoopContains,
3447 ControlGroup,
3448 ControlGroupWhenDefines,
3449 ControlGroupWhenDefinesDirect,
3450 ControlGroupWhenType,
3451 ControlEvaluate,
3452 ControlFrame,
3453 ControlResource,
3454 ControlDynamicAnchorJump,
3455 ControlJump,
3456 ControlResourceJump};
3457
3458template <bool HasCallback>
3459inline auto
3460evaluate_instruction(const sourcemeta::blaze::Instruction &instruction,
3461 const sourcemeta::core::JSON &instance,
3462 const std::uint64_t depth,
3463 DispatchContext<HasCallback> &context) -> bool {
3464 if (depth > DEPTH_LIMIT) [[unlikely]] {
3465 throw EvaluationError("The evaluation path depth limit was reached "
3466 "likely due to infinite recursion");
3467 }
3468
3469 return HANDLERS<HasCallback>[std::to_underlying(instruction.type)](
3470 instruction, instance, depth, context);
3471}
3472
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,
3477 DispatchContext<HasCallback> &context) -> bool {
3478 if (depth > DEPTH_LIMIT) [[unlikely]] {
3479 throw EvaluationError("The evaluation path depth limit was reached "
3480 "likely due to infinite recursion");
3481 }
3482
3483 DispatchContext<false> plain_context{context.schema, context.callback,
3484 context.evaluator,
3485 context.property_target, context.frame};
3486 return HANDLERS<false>[std::to_underlying(instruction.type)](
3487 instruction, instance, depth, plain_context);
3488}
3489
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;
3498 const auto result =
3499 evaluate_instruction(instruction, instance, depth, context);
3500 context.property_target = previous;
3501 return result;
3502}
3503
3504} // namespace sourcemeta::blaze::dispatch
3505
3506#undef EVALUATE_PUSH
3507#undef EVALUATE_POP
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
3515#undef EVALUATE_END
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
3523
3524#endif
Definition evaluator.h:88
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_instruction.h:368
Definition evaluator.h:35
Definition evaluator_dispatch.h:415