schemurai 3.0.2 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -810,6 +810,35 @@ mod tests {
810
810
  }
811
811
 
812
812
  impl Engine {
813
+ fn clear_memo_guards(&mut self) {
814
+ for flags in [
815
+ &mut self.memo_guarded,
816
+ &mut self.memo_guard_sources,
817
+ &mut self.memo_guard_same_sources,
818
+ &mut self.memo_guard_scalar_sources,
819
+ &mut self.memo_guard_scalar_amplification,
820
+ ] {
821
+ *flags = vec![false; self.nodes.len()];
822
+ }
823
+ }
824
+
825
+ fn clear_memo_analysis(&mut self) {
826
+ self.memoizable = vec![false; self.nodes.len()];
827
+ self.memo_contextual = vec![false; self.nodes.len()];
828
+ self.clear_memo_guards();
829
+ self.memo_scope_projection = memo_scope::Projection::default();
830
+ }
831
+
832
+ fn compile_memo_projection(&mut self, edges: &[Vec<usize>], parents: &[Vec<usize>]) {
833
+ self.memo_scope_projection = if self.memo_guarded.iter().any(|&guarded| guarded)
834
+ || self.anchor_tables.len() > 1 && self.memo_contextual.iter().any(|&scoped| scoped)
835
+ {
836
+ memo_scope::Projection::compile(edges, parents, self.nodes.len())
837
+ } else {
838
+ memo_scope::Projection::default()
839
+ };
840
+ }
841
+
813
842
  pub(super) fn compile_memoization(&mut self) {
814
843
  let mut incoming = vec![0_usize; self.nodes.len()];
815
844
  let mut repeated_position = vec![false; self.nodes.len()];
@@ -909,6 +938,27 @@ impl Engine {
909
938
  }
910
939
  }
911
940
  }
941
+ // Every admission requires multiple incoming references or repeated
942
+ // positions. If neither exists, skip guard/cycle analysis and retain
943
+ // indexed false flags for the runtime's ordinary uncached path.
944
+ if incoming.iter().all(|&count| count <= 1)
945
+ && !repeated_position.iter().any(|&repeated| repeated)
946
+ {
947
+ self.clear_memo_analysis();
948
+ return;
949
+ }
950
+ let parents = graph::parents(&edges);
951
+ // Compute iteration reachability once, before either admission policy.
952
+ graph::propagate(&mut repeated_subtree, &parents);
953
+ // A position-only candidate needs nested iteration to amplify sharing.
954
+ // Share this necessary condition with both admission policies below.
955
+ let candidates: Vec<_> = (0..count)
956
+ .map(|node| incoming[node] > 1 || repeated_position[node] && repeated_subtree[node])
957
+ .collect();
958
+ if !candidates.iter().any(|&candidate| candidate) {
959
+ self.clear_memo_analysis();
960
+ return;
961
+ }
912
962
  // Reject cycles through any possible binding, plus recursive refs.
913
963
  // States 2 and 4 distinguish scope-independent and scoped successes.
914
964
  // Bound analysis depth just as evaluation is bounded; conservatively
@@ -920,7 +970,6 @@ impl Engine {
920
970
  eligible: &[bool],
921
971
  states: &mut [u8],
922
972
  count: usize,
923
- repeated_subtree: &mut [bool],
924
973
  ) -> bool {
925
974
  match states[node] {
926
975
  1 | 3 => return false,
@@ -934,16 +983,7 @@ impl Engine {
934
983
  states[node] = 1;
935
984
  let mut scoped = node >= count;
936
985
  let safe = edges[node].iter().all(|&child| {
937
- let safe = reusable(
938
- child,
939
- depth + 1,
940
- edges,
941
- eligible,
942
- states,
943
- count,
944
- repeated_subtree,
945
- );
946
- repeated_subtree[node] |= repeated_subtree[child];
986
+ let safe = reusable(child, depth + 1, edges, eligible, states, count);
947
987
  scoped |= states[child] == 4;
948
988
  safe
949
989
  });
@@ -959,20 +999,21 @@ impl Engine {
959
999
  let mut states = vec![0; edges.len()];
960
1000
  self.memoizable = (0..self.nodes.len())
961
1001
  .map(|node| {
962
- (incoming[node] > 1 || repeated_position[node])
963
- && reusable(node, 0, &edges, &eligible, &mut states, count, &mut repeated_subtree)
964
- // Position-only candidates need nested iteration to amplify
965
- // sharing. Simple records/scalars keep their cheap scans.
966
- && (incoming[node] > 1 || repeated_subtree[node])
1002
+ candidates[node] && reusable(node, 0, &edges, &eligible, &mut states, count)
967
1003
  })
968
1004
  .collect();
969
1005
  self.memo_contextual = states[..count].iter().map(|&state| state == 4).collect();
1006
+ // Proven acyclic candidates do not need any guard signatures, even if
1007
+ // unrelated non-candidates lie on cycles elsewhere in the program.
1008
+ if (0..count).all(|node| !candidates[node] || self.memoizable[node]) {
1009
+ self.clear_memo_guards();
1010
+ self.compile_memo_projection(&edges, &parents);
1011
+ return;
1012
+ }
970
1013
  // Cyclic candidates can reuse results only under an exact guard/scope
971
1014
  // context. Propagate unsupported instructions and nested iteration
972
1015
  // through reverse edges without a per-candidate graph walk.
973
- let parents = graph::parents(&edges);
974
1016
  graph::propagate(&mut unsupported, &parents);
975
- graph::propagate(&mut repeated_subtree, &parents);
976
1017
  // Iterative Kosaraju analysis identifies the only active sources
977
1018
  // whose guards can affect descendants: sources lying on a schema cycle.
978
1019
  fn cycle_nodes(edges: &[Vec<usize>], parents: &[Vec<usize>]) -> Vec<bool> {
@@ -1083,22 +1124,27 @@ impl Engine {
1083
1124
  self.memo_guard_scalar_amplification = scalar_amplification[..count].to_vec();
1084
1125
  // Object dependencies keep the same value; all other container child
1085
1126
  // edges change positions. Scalar/array inputs cannot take dependencies.
1127
+ let mut has_dependencies = false;
1086
1128
  for (node, ops) in self.nodes.iter().enumerate() {
1087
1129
  for op in ops {
1088
1130
  if let Op::Object(_, rules) = op {
1131
+ has_dependencies |= !rules.dependent_schemas.is_empty();
1089
1132
  same_edges[node]
1090
1133
  .extend(rules.dependent_schemas.iter().map(|&(_, child)| child));
1091
1134
  }
1092
1135
  }
1093
1136
  }
1094
- self.memo_guard_same_sources =
1095
- cycle_nodes(&same_edges, &graph::parents(&same_edges))[..count].to_vec();
1137
+ self.memo_guard_same_sources = if has_dependencies {
1138
+ cycle_nodes(&same_edges, &graph::parents(&same_edges))[..count].to_vec()
1139
+ } else {
1140
+ self.memo_guard_scalar_sources.clone()
1141
+ };
1096
1142
  self.memo_guard_sources = cyclic[..count].to_vec();
1097
1143
  self.memo_guarded = vec![false; count];
1098
1144
  for node in 0..count {
1099
1145
  let amplified_cycle = incoming[node] > 2 || cyclic_incoming[node] > 1;
1100
1146
  if !self.memoizable[node] && !unsupported[node]
1101
- && (incoming[node] > 1 || repeated_position[node] && repeated_subtree[node])
1147
+ && candidates[node]
1102
1148
  // A linear recursive property chain has no amplification.
1103
1149
  // Avoid signatures unless a cycle has repeated input positions
1104
1150
  // or multiple recursive edges, with or without an entry ref.
@@ -1111,12 +1157,6 @@ impl Engine {
1111
1157
  self.memo_guarded[node] = true;
1112
1158
  }
1113
1159
  }
1114
- self.memo_scope_projection = if self.memo_guarded.iter().any(|&guarded| guarded)
1115
- || self.anchor_tables.len() > 1 && self.memo_contextual.iter().any(|&scoped| scoped)
1116
- {
1117
- memo_scope::Projection::compile(&edges, &parents, count)
1118
- } else {
1119
- memo_scope::Projection::default()
1120
- };
1160
+ self.compile_memo_projection(&edges, &parents);
1121
1161
  }
1122
1162
  }
@@ -18,6 +18,14 @@ impl Projection {
18
18
  Self::bounded(edges, parents, count, MAX_NAMES)
19
19
  }
20
20
  fn bounded(edges: &[Vec<usize>], parents: &[Vec<usize>], count: usize, limit: usize) -> Self {
21
+ // With no incoming edges to any dispatch vertex, every real node's
22
+ // reachable name set is empty, including nodes on schema cycles.
23
+ if parents[count..].iter().all(Vec::is_empty) {
24
+ return Self {
25
+ names: vec![Some(Arc::from([])); count],
26
+ graph: None,
27
+ };
28
+ }
21
29
  let mut seen = vec![false; edges.len()];
22
30
  let order = graph::postorder(edges, &mut seen);
23
31
  let mut component = vec![usize::MAX; edges.len()];
@@ -179,6 +187,28 @@ mod tests {
179
187
  parents
180
188
  }
181
189
  #[test]
190
+ fn static_cycles_share_empty_projections_without_default_scope_names() {
191
+ // The unused dispatch vertex points into a real cycle, but no real
192
+ // node can reach it. Even a zero closure budget needs no graph fallback.
193
+ let edges = vec![vec![1], vec![0], vec![], vec![0]];
194
+ for limit in [0, 1, MAX_NAMES] {
195
+ let projection = Projection::bounded(&edges, &parents(&edges), 3, limit);
196
+ assert!(projection.graph.is_none());
197
+ for node in 0..3 {
198
+ assert!(
199
+ projection
200
+ .names(node, 3, 0, true, &budget::Budget::default())
201
+ .unwrap()
202
+ .is_empty()
203
+ );
204
+ assert!(Arc::ptr_eq(
205
+ projection.names[0].as_ref().unwrap(),
206
+ projection.names[node].as_ref().unwrap()
207
+ ));
208
+ }
209
+ }
210
+ }
211
+ #[test]
182
212
  fn projects_cycles_and_transitive_dynamic_dispatch_targets() {
183
213
  // Four nodes, two dynamic names and the recursive dispatch vertex.
184
214
  let edges = vec![
@@ -3,8 +3,28 @@
3
3
  //! method dispatch can invalidate the evaluator's borrowed instance storage.
4
4
  use super::*;
5
5
  use num_bigint::{BigInt, Sign};
6
+ use num_traits::float::FloatCore;
6
7
  use std::borrow::Cow;
7
8
 
9
+ // Use the application's BigInt version rather than num-rational's optional one.
10
+ pub(super) type BigRational = num_rational::Ratio<BigInt>;
11
+
12
+ fn binary_float(value: f64) -> Option<BigRational> {
13
+ if !value.is_finite() {
14
+ return None;
15
+ }
16
+ let (mantissa, exponent, sign) = value.integer_decode();
17
+ let numerator = BigInt::from(mantissa) * BigInt::from(sign);
18
+ Some(if exponent < 0 {
19
+ BigRational::new(
20
+ numerator,
21
+ BigInt::from(1) << exponent.unsigned_abs() as usize,
22
+ )
23
+ } else {
24
+ BigRational::from_integer(numerator << exponent as usize)
25
+ })
26
+ }
27
+
8
28
  fn integer(value: Value) -> BigInt {
9
29
  if unsafe { sn_fixnum(value) } != 0 {
10
30
  return BigInt::from(unsafe { sn_integer(value) });
@@ -98,7 +118,7 @@ impl Rules {
98
118
  // in binary, but rounds Rational bounds to Float. With multipleOf,
99
119
  // Ruby first converts the instance's decimal spelling to a Rational.
100
120
  let binary = if actual.is_none() && self.limits.iter().any(|(_, limit)| limit.integer) {
101
- BigRational::from_float(n)
121
+ binary_float(n)
102
122
  } else {
103
123
  None
104
124
  };
@@ -152,3 +172,39 @@ impl Rules {
152
172
  Ok(bits)
153
173
  }
154
174
  }
175
+
176
+ #[cfg(test)]
177
+ mod tests {
178
+ use super::*;
179
+
180
+ #[test]
181
+ fn binary_float_preserves_exact_values() {
182
+ let cases = [
183
+ (0.0, BigRational::from_integer(BigInt::from(0))),
184
+ (-0.0, BigRational::from_integer(BigInt::from(0))),
185
+ (
186
+ 0.1,
187
+ BigRational::new(BigInt::from(3602879701896397_u64), BigInt::from(1) << 55),
188
+ ),
189
+ (
190
+ f64::from_bits(1),
191
+ BigRational::new(BigInt::from(1), BigInt::from(1) << 1074),
192
+ ),
193
+ (
194
+ f64::MIN_POSITIVE,
195
+ BigRational::new(BigInt::from(1), BigInt::from(1) << 1022),
196
+ ),
197
+ (
198
+ f64::MAX,
199
+ BigRational::from_integer(BigInt::from((1_u64 << 53) - 1) << 971),
200
+ ),
201
+ ];
202
+ for (value, expected) in cases {
203
+ assert_eq!(binary_float(value), Some(expected.clone()), "{value}");
204
+ assert_eq!(binary_float(-value), Some(-expected), "{}", -value);
205
+ }
206
+ for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
207
+ assert_eq!(binary_float(value), None);
208
+ }
209
+ }
210
+ }
@@ -2,7 +2,7 @@
2
2
  //! multipleOf semantics. All evaluation helpers operate under the GVL and
3
3
  //! allocate no Ruby objects; unsupported magnitudes retain VM compatibility.
4
4
  use super::*;
5
- use num_rational::BigRational;
5
+ use arbitrary::BigRational;
6
6
  use std::cmp::Ordering;
7
7
  mod arbitrary;
8
8
 
@@ -440,14 +440,14 @@ impl FastMultiple {
440
440
 
441
441
  impl Engine {
442
442
  pub(super) fn compile_number(&mut self, operand: Value) -> Result<Option<Rules>, Value> {
443
- let mask = self.field(operand, b"mask\0")?;
443
+ let mask = self.field(operand, FIELD_MASK)?;
444
444
  let mask = unsafe { sn_integer(mask) } as i32;
445
445
  let mut limits = vec![];
446
446
  for (bit, name) in [
447
- (MAXIMUM, b"maximum\0".as_slice()),
448
- (MINIMUM, b"minimum\0"),
449
- (EXCLUSIVE_MAXIMUM, b"exclusive_maximum\0"),
450
- (EXCLUSIVE_MINIMUM, b"exclusive_minimum\0"),
447
+ (MAXIMUM, FIELD_MAXIMUM),
448
+ (MINIMUM, FIELD_MINIMUM),
449
+ (EXCLUSIVE_MAXIMUM, FIELD_EXCLUSIVE_MAXIMUM),
450
+ (EXCLUSIVE_MINIMUM, FIELD_EXCLUSIVE_MINIMUM),
451
451
  ] {
452
452
  if mask & bit != 0 {
453
453
  let value = self.field(operand, name)?;
@@ -460,7 +460,7 @@ impl Engine {
460
460
  let multiple = if mask & MULTIPLE_OF == 0 {
461
461
  None
462
462
  } else {
463
- let value = self.field(operand, b"multiple_of\0")?;
463
+ let value = self.field(operand, FIELD_MULTIPLE_OF)?;
464
464
  let Some(limit) = self.numeric_limit(value)? else {
465
465
  return Ok(None);
466
466
  };
@@ -537,8 +537,8 @@ impl Engine {
537
537
  let (numerator, denominator) = if integer {
538
538
  (value, None)
539
539
  } else {
540
- let numerator = self.field(value, b"numerator\0")?;
541
- let denominator = self.field(value, b"denominator\0")?;
540
+ let numerator = self.field(value, FIELD_NUMERATOR)?;
541
+ let denominator = self.field(value, FIELD_DENOMINATOR)?;
542
542
  (numerator, Some(denominator))
543
543
  };
544
544
  let exact = read_integer(numerator)
@@ -550,7 +550,7 @@ impl Engine {
550
550
  let arbitrary = exact
551
551
  .is_none()
552
552
  .then(|| Box::new(arbitrary::fraction(numerator, denominator)));
553
- let rounded = self.field(value, b"to_f\0")?;
553
+ let rounded = self.field(value, FIELD_TO_F)?;
554
554
  let rounded = unsafe { sn_float(rounded) };
555
555
  let wide_rounded_order = if integer && exact.is_none() {
556
556
  wide_float_tie(value, rounded)
@@ -32,14 +32,27 @@ impl Path {
32
32
  let bytes = unsafe { byte_view(data, len) };
33
33
  if matches!(encoding, ENCODING_UTF8 | ENCODING_ASCII)
34
34
  && (encoding != ENCODING_ASCII || bytes.is_ascii())
35
- && let Ok(name) = std::str::from_utf8(bytes)
35
+ && let Some(name) = if bytes.len() < 64 {
36
+ std::str::from_utf8(bytes).ok()
37
+ } else {
38
+ simdutf8::compat::from_utf8(bytes).ok()
39
+ }
36
40
  {
37
- for character in name.chars() {
38
- match character {
39
- '~' => self.text.push_str("~0"),
40
- '/' => self.text.push_str("~1"),
41
- _ => self.text.push(character),
41
+ // ASCII escape bytes are UTF-8 boundaries. Copy unchanged ends
42
+ // together; retain the scalar loop for densely escaped names.
43
+ if let Some(first) = memchr::memchr2(b'~', b'/', bytes) {
44
+ let end = memchr::memrchr2(b'~', b'/', bytes).unwrap() + 1;
45
+ self.text.push_str(&name[..first]);
46
+ for character in name[first..end].chars() {
47
+ match character {
48
+ '~' => self.text.push_str("~0"),
49
+ '/' => self.text.push_str("~1"),
50
+ _ => self.text.push(character),
51
+ }
42
52
  }
53
+ self.text.push_str(&name[end..]);
54
+ } else {
55
+ self.text.push_str(name);
43
56
  }
44
57
  } else {
45
58
  self.names.push((self.text.len(), value));
@@ -37,6 +37,7 @@ pub struct Caches {
37
37
  reports: reports::Reports,
38
38
  properties: properties::Properties,
39
39
  report_continuations_allowed: std::cell::Cell<bool>,
40
+ pub(super) content: content::Scratch,
40
41
  }
41
42
  #[repr(C)]
42
43
  #[derive(Clone, Copy)]
@@ -95,6 +96,15 @@ unsafe extern "C" {
95
96
  ) -> i32;
96
97
  }
97
98
  impl Caches {
99
+ // FFI callers normally provide runner-owned state. Only null-cache calls
100
+ // need a temporary owner, whose lifetime stays in the adapter's scope.
101
+ pub(super) fn supplied_or_default<'a>(
102
+ supplied: Option<&'a Self>,
103
+ fallback: &'a mut Option<Self>,
104
+ ) -> &'a Self {
105
+ supplied.unwrap_or_else(|| fallback.get_or_insert_with(Self::default))
106
+ }
107
+
98
108
  fn reset_pass(&self) {
99
109
  self.scopes.reset();
100
110
  self.reports.reset();
@@ -692,6 +702,7 @@ pub unsafe extern "C" fn sn_caches_size(caches_ptr: *const Caches) -> usize {
692
702
  + messages
693
703
  + unsafe { &*caches_ptr }.reports.memory_usage()
694
704
  + unsafe { &*caches_ptr }.properties.memory_usage()
705
+ + unsafe { &*caches_ptr }.content.memory_usage()
695
706
  + pointers
696
707
  + caches.capacity() * std::mem::size_of::<Option<Box<Cache>>>()
697
708
  + caches
@@ -978,6 +989,22 @@ pub unsafe extern "C" fn sn_scalar_arguments(caches: *const Caches, output: *mut
978
989
  #[cfg(test)]
979
990
  mod tests {
980
991
  use super::*;
992
+ #[test]
993
+ fn ffi_fallback_is_created_only_without_supplied_state() {
994
+ let supplied = Caches::default();
995
+ supplied.dynamic_calls.set(true);
996
+ let mut fallback = None;
997
+ let selected = Caches::supplied_or_default(Some(&supplied), &mut fallback);
998
+ assert!(std::ptr::eq(selected, &supplied));
999
+ assert!(selected.dynamic_calls());
1000
+ assert!(fallback.is_none());
1001
+
1002
+ let selected = Caches::supplied_or_default(None, &mut fallback);
1003
+ assert!(!selected.dynamic_calls());
1004
+ selected.dynamic_calls.set(true);
1005
+ assert!(fallback.as_ref().unwrap().dynamic_calls());
1006
+ }
1007
+
981
1008
  #[test]
982
1009
  fn child_modes_inherit_deadlines_without_adding_normal_scalar_checkpoints() {
983
1010
  let parent = Caches::default();
@@ -15,6 +15,53 @@ pub(super) struct Metadata {
15
15
  mod tests {
16
16
  use super::*;
17
17
 
18
+ #[test]
19
+ fn acyclic_candidates_do_not_require_guards_for_unrelated_cycles() {
20
+ let reference = |target| {
21
+ Op::Ref(Reference {
22
+ target,
23
+ kind: ReferenceKind::Static,
24
+ })
25
+ };
26
+ let mut engine = Engine {
27
+ nodes: vec![
28
+ vec![Op::Boolean(true)],
29
+ vec![reference(1)],
30
+ vec![reference(0)],
31
+ vec![reference(0)],
32
+ ],
33
+ ..Engine::default()
34
+ };
35
+ engine.compile_memoization();
36
+ assert_eq!(engine.memoizable, [true, false, false, false]);
37
+ assert_eq!(engine.memo_guarded, [false; 4]);
38
+ assert_eq!(engine.memo_guard_sources, [false; 4]);
39
+ }
40
+
41
+ #[test]
42
+ fn clears_prior_guard_analysis_when_no_reuse_positions_remain() {
43
+ let mut engine = Engine {
44
+ nodes: (0..4)
45
+ .map(|_| {
46
+ vec![Op::Ref(Reference {
47
+ target: 0,
48
+ kind: ReferenceKind::Static,
49
+ })]
50
+ })
51
+ .collect(),
52
+ ..Engine::default()
53
+ };
54
+ engine.compile_memoization();
55
+ assert!(engine.memo_guarded[0]);
56
+ assert!(engine.memo_scope_projection.memory_usage() > 0);
57
+ engine.nodes = vec![vec![Op::Boolean(true)], vec![Op::Boolean(false)]];
58
+ engine.compile_memoization();
59
+ assert_eq!(engine.memoizable, [false, false]);
60
+ assert_eq!(engine.memo_guarded, [false, false]);
61
+ assert_eq!(engine.memo_guard_sources, [false, false]);
62
+ assert_eq!(engine.memo_scope_projection.memory_usage(), 0);
63
+ }
64
+
18
65
  #[test]
19
66
  fn requires_guard_histories_for_cycles_reached_through_dynamic_overrides() {
20
67
  let reference = |target, kind| Op::Ref(Reference { target, kind });
@@ -173,10 +220,10 @@ impl Engine {
173
220
  index: usize,
174
221
  flags: i64,
175
222
  ) -> Result<(), Value> {
176
- let node = self.field(program, b"node\0")?;
177
- let resource = self.field(node, b"resource\0")?;
178
- let recursive = self.field(program, b"recursive_anchor?\0")?;
179
- let dynamic = self.field(program, b"dynamic_anchor\0")?;
223
+ let node = self.field(program, FIELD_NODE)?;
224
+ let resource = self.field(node, FIELD_RESOURCE)?;
225
+ let recursive = self.field(program, FIELD_RECURSIVE_ANCHOR_P)?;
226
+ let dynamic = self.field(program, FIELD_DYNAMIC_ANCHOR)?;
180
227
  self.metadata[index] = Metadata {
181
228
  resource,
182
229
  enters_scope: flags & i64::from(TRACKS_DYNAMIC_SCOPE) != 0,
@@ -204,7 +251,7 @@ impl Engine {
204
251
  opcode: i32,
205
252
  target: usize,
206
253
  ) -> Result<Op, Value> {
207
- let fragment = self.field(operand, b"fragment\0")?;
254
+ let fragment = self.field(operand, FIELD_FRAGMENT)?;
208
255
  let kind = if opcode == OP_RECURSIVE_REF
209
256
  && unsafe { sn_same(fragment, sn_nil()) } == 0
210
257
  && unsafe { sn_string_bytes(fragment) } == 0
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "limit_budget"
4
+
5
+ module Schemurai
6
+ module Internal
7
+ # Limit policy shared by evaluators; error storage and scalar state stay local.
8
+ module EvaluationLimits
9
+ def validate(instance, max_errors: nil, timeout: nil, budget: nil)
10
+ with_budget(max_errors, timeout, budget) { super(instance) }
11
+ end
12
+
13
+ def valid?(instance, max_errors: nil, timeout: nil, budget: nil)
14
+ with_budget(max_errors, timeout, budget) { super(instance) }
15
+ end
16
+
17
+ private def with_budget(max_errors, timeout, budget)
18
+ @budget = budget || LimitBudget.new(max_errors: max_errors, timeout: timeout)
19
+ result = yield
20
+ @budget.check_deadline!
21
+ result
22
+ ensure
23
+ @budget = nil
24
+ end
25
+
26
+ private def evaluate(node, instance)
27
+ @budget.checkpoint
28
+ super
29
+ end
30
+
31
+ private def evaluate_valid(node, instance)
32
+ @budget.checkpoint
33
+ super
34
+ end
35
+
36
+ private def valid_string?(node, value)
37
+ @budget.check_deadline!
38
+ result = super
39
+ @budget.check_deadline!
40
+ result
41
+ end
42
+
43
+ private def check_string(node, value)
44
+ @budget.check_deadline!
45
+ result = super
46
+ @budget.check_deadline!
47
+ result
48
+ end
49
+
50
+ private def valid_content?(schema, value)
51
+ @budget.check_deadline!
52
+ result = super
53
+ @budget.check_deadline!
54
+ result
55
+ end
56
+
57
+ private def check_content(schema, value)
58
+ @budget.check_deadline!
59
+ result = super
60
+ @budget.check_deadline!
61
+ result
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,63 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "limit_exceeded"
4
- require_relative "limit_budget"
4
+ require_relative "evaluation_limits"
5
5
  require_relative "evaluator"
6
6
 
7
7
  module Schemurai
8
8
  module Internal
9
9
  # Ruby evaluation with invocation-local limits.
10
10
  class LimitedEvaluator < Evaluator
11
- def validate(instance, max_errors: nil, timeout: nil, budget: nil)
12
- @budget = budget || LimitBudget.new(max_errors: max_errors, timeout: timeout)
13
- errors = []
14
- each_error(instance) do |error|
15
- errors << error
16
- end
17
- @budget.check_deadline!
18
- Result.new(errors)
19
- ensure
20
- @budget = nil
21
- end
22
-
23
- def valid?(instance, max_errors: nil, timeout: nil, budget: nil)
24
- @budget = budget || LimitBudget.new(max_errors: max_errors, timeout: timeout)
25
- result = super(instance)
26
- @budget.check_deadline!
27
- result
28
- ensure
29
- @budget = nil
30
- end
31
-
32
- private def evaluate(node, instance)
33
- @budget.checkpoint
34
- super
35
- end
36
-
37
- private def evaluate_valid(node, instance)
38
- @budget.checkpoint
39
- super
40
- end
11
+ include EvaluationLimits
41
12
 
42
13
  private def add_error(keyword, message = nil, append_keyword: true, &block)
43
14
  @budget.reserve_error if @error_callback
44
15
  super
45
16
  end
46
17
 
47
- private def valid_string?(node, value)
48
- @budget.check_deadline!
49
- result = super
50
- @budget.check_deadline!
51
- result
52
- end
53
-
54
- private def check_string(node, value)
55
- @budget.check_deadline!
56
- result = super
57
- @budget.check_deadline!
58
- result
59
- end
60
-
61
18
  private def decimal(value)
62
19
  @budget.check_deadline!
63
20
  result = super
@@ -72,20 +29,6 @@ module Schemurai
72
29
  result
73
30
  end
74
31
 
75
- private def valid_content?(schema, value)
76
- @budget.check_deadline!
77
- result = super
78
- @budget.check_deadline!
79
- result
80
- end
81
-
82
- private def check_content(schema, value)
83
- @budget.check_deadline!
84
- result = super
85
- @budget.check_deadline!
86
- result
87
- end
88
-
89
32
  private def json_equal?(left, right)
90
33
  @budget.checkpoint
91
34
  super
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Schemurai
4
4
  # The installed Schemurai version.
5
- VERSION = "3.0.2"
5
+ VERSION = "3.1.1"
6
6
  end