From 09bbc860490a4cb635b615f20faef546a407d8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20S=C3=A1nchez=20Mu=C3=B1oz?= Date: Wed, 6 Nov 2024 18:29:38 +0100 Subject: [PATCH] refactor(eval): move more eval states to `State::Fn*` --- rsjsonnet-lang/src/program/eval/call.rs | 9 +++++--- rsjsonnet-lang/src/program/eval/mod.rs | 8 ------- rsjsonnet-lang/src/program/eval/state.rs | 8 ------- rsjsonnet-lang/src/program/eval/stdlib.rs | 27 ++++++++++++++--------- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/rsjsonnet-lang/src/program/eval/call.rs b/rsjsonnet-lang/src/program/eval/call.rs index 3d3da26..e4496f2 100644 --- a/rsjsonnet-lang/src/program/eval/call.rs +++ b/rsjsonnet-lang/src/program/eval/call.rs @@ -431,7 +431,8 @@ impl<'p> Evaluator<'_, 'p> { } BuiltInFunc::AssertEqual => { let [arg0, arg1] = check_num_args(args); - self.state_stack.push(State::StdAssertEqual); + self.state_stack + .push(State::FnInfallible(Self::do_std_assert_equal)); self.state_stack.push(State::DoThunk(arg1.view())); self.state_stack.push(State::DoThunk(arg0.view())); } @@ -621,12 +622,14 @@ impl<'p> Evaluator<'_, 'p> { self.state_stack.push(State::DoThunk(arg.view())); } BuiltInFunc::EncodeUtf8 => { - self.state_stack.push(State::StdEncodeUtf8); + self.state_stack + .push(State::FnFallible(Self::do_std_encode_utf8)); self.state_stack.push(State::DoThunk(args[0].view())); } BuiltInFunc::DecodeUtf8 => { let [arg] = check_num_args(args); - self.state_stack.push(State::StdDecodeUtf8); + self.state_stack + .push(State::FnFallible(Self::do_std_decode_utf8)); self.state_stack.push(State::DoThunk(arg.view())); } BuiltInFunc::ManifestIni => { diff --git a/rsjsonnet-lang/src/program/eval/mod.rs b/rsjsonnet-lang/src/program/eval/mod.rs index 5b9e241..35a2ea7 100644 --- a/rsjsonnet-lang/src/program/eval/mod.rs +++ b/rsjsonnet-lang/src/program/eval/mod.rs @@ -1306,10 +1306,6 @@ impl<'p, 'a> Evaluator<'a, 'p> { State::StdPruneValue => self.do_std_prune_value(), State::StdPruneArrayItem => self.do_std_prune_array_item(), State::StdPruneObjectField { name } => self.do_std_prune_object_field(name), - State::StdAssertEqual => self.do_std_assert_equal(), - State::StdAssertEqualCheck => self.do_std_assert_equal_check(), - State::StdAssertEqualFail1 => self.do_std_assert_equal_fail_1(), - State::StdAssertEqualFail2 => self.do_std_assert_equal_fail_2()?, State::StdFormat => self.do_std_format()?, State::StdFormatCodesArray1 { parts, @@ -1347,10 +1343,6 @@ impl<'p, 'a> Evaluator<'a, 'p> { fw, prec, } => self.do_std_format_code(&parts, part_i, fw, prec)?, - State::StdEncodeUtf8 => self.do_std_encode_utf8()?, - State::StdDecodeUtf8 => self.do_std_decode_utf8()?, - State::StdDecodeUtf8CheckItem => self.do_std_decode_utf8_check_item()?, - State::StdDecodeUtf8Finish => self.do_std_decode_utf8_finish(), State::StdManifestIni => self.do_std_manifest_ini()?, State::StdManifestIniSections => self.do_std_manifest_ini_sections()?, State::StdManifestPython => self.do_std_manifest_python(), diff --git a/rsjsonnet-lang/src/program/eval/state.rs b/rsjsonnet-lang/src/program/eval/state.rs index f42bf13..8b6a6b1 100644 --- a/rsjsonnet-lang/src/program/eval/state.rs +++ b/rsjsonnet-lang/src/program/eval/state.rs @@ -220,10 +220,6 @@ pub(super) enum State<'a, 'p> { StdPruneObjectField { name: InternedStr<'p>, }, - StdAssertEqual, - StdAssertEqualCheck, - StdAssertEqualFail1, - StdAssertEqualFail2, StdFormat, StdFormatCodesArray1 { parts: Rc>, @@ -261,10 +257,6 @@ pub(super) enum State<'a, 'p> { fw: u32, prec: u32, }, - StdEncodeUtf8, - StdDecodeUtf8, - StdDecodeUtf8CheckItem, - StdDecodeUtf8Finish, StdManifestIni, StdManifestIniSections, StdManifestPython, diff --git a/rsjsonnet-lang/src/program/eval/stdlib.rs b/rsjsonnet-lang/src/program/eval/stdlib.rs index 73b39c1..f9d5152 100644 --- a/rsjsonnet-lang/src/program/eval/stdlib.rs +++ b/rsjsonnet-lang/src/program/eval/stdlib.rs @@ -491,11 +491,12 @@ impl<'p> Evaluator<'_, 'p> { self.value_stack.push(lhs); self.value_stack.push(rhs); - self.state_stack.push(State::StdAssertEqualCheck); + self.state_stack + .push(State::FnInfallible(Self::do_std_assert_equal_check)); self.state_stack.push(State::EqualsValue); } - pub(super) fn do_std_assert_equal_check(&mut self) { + fn do_std_assert_equal_check(&mut self) { let equal = self.bool_stack.pop().unwrap(); if equal { self.value_stack.pop().unwrap(); @@ -506,7 +507,8 @@ impl<'p> Evaluator<'_, 'p> { self.value_stack .swap(value_stack_len - 1, value_stack_len - 2); self.string_stack.push(String::new()); - self.state_stack.push(State::StdAssertEqualFail1); + self.state_stack + .push(State::FnInfallible(Self::do_std_assert_equal_fail_1)); self.state_stack.push(State::ManifestJson { format: ManifestJsonFormat::default_to_string(), depth: 0, @@ -514,16 +516,17 @@ impl<'p> Evaluator<'_, 'p> { } } - pub(super) fn do_std_assert_equal_fail_1(&mut self) { + fn do_std_assert_equal_fail_1(&mut self) { self.string_stack.push(String::new()); - self.state_stack.push(State::StdAssertEqualFail2); + self.state_stack + .push(State::FnFallible(Self::do_std_assert_equal_fail_2)); self.state_stack.push(State::ManifestJson { format: ManifestJsonFormat::default_to_string(), depth: 0, }); } - pub(super) fn do_std_assert_equal_fail_2(&mut self) -> EvalResult<()> { + fn do_std_assert_equal_fail_2(&mut self) -> EvalResult<()> { let rhs = self.string_stack.pop().unwrap(); let lhs = self.string_stack.pop().unwrap(); Err(self.report_error(EvalErrorKind::AssertEqualFailed { lhs, rhs })) @@ -1060,16 +1063,18 @@ impl<'p> Evaluator<'_, 'p> { let array = self.expect_std_func_arg_array(arg, "decodeUTF8", 0)?; self.byte_array_stack.push(Vec::with_capacity(array.len())); - self.state_stack.push(State::StdDecodeUtf8Finish); + self.state_stack + .push(State::FnInfallible(Self::do_std_decode_utf8_finish)); for item in array.iter().rev() { - self.state_stack.push(State::StdDecodeUtf8CheckItem); + self.state_stack + .push(State::FnFallible(Self::do_std_decode_utf8_check_item)); self.state_stack.push(State::DoThunk(item.view())); } Ok(()) } - pub(super) fn do_std_decode_utf8_check_item(&mut self) -> EvalResult<()> { + fn do_std_decode_utf8_check_item(&mut self) -> EvalResult<()> { let item = self.value_stack.pop().unwrap(); let ValueData::Number(value) = item else { return Err(self.report_error(EvalErrorKind::Other { @@ -1090,7 +1095,7 @@ impl<'p> Evaluator<'_, 'p> { Ok(()) } - pub(super) fn do_std_decode_utf8_finish(&mut self) { + fn do_std_decode_utf8_finish(&mut self) { let bytes = self.byte_array_stack.pop().unwrap(); let s = String::from_utf8_lossy(&bytes); self.value_stack.push(ValueData::String(s.into())); @@ -2143,7 +2148,7 @@ impl<'p> Evaluator<'_, 'p> { Ok(()) } - pub(super) fn do_std_flatten_arrays_item(&mut self) -> EvalResult<()> { + fn do_std_flatten_arrays_item(&mut self) -> EvalResult<()> { let item = self.value_stack.pop().unwrap(); let ValueData::Array(item) = item else { return Err(self.report_error(EvalErrorKind::Other {