rib/interpreter/
mod.rs

1// Copyright 2024-2025 Golem Cloud
2//
3// Licensed under the Golem Source License v1.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://license.golem.cloud/LICENSE
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub use env::*;
16pub use eval::*;
17pub use interpreter_input::*;
18pub use interpreter_result::*;
19pub use literal::*;
20pub use rib_function_invoke::*;
21pub use rib_interpreter::*;
22pub use rib_runtime_error::*;
23pub use stack::*;
24
25mod env;
26mod eval;
27mod instruction_cursor;
28mod interpreter_input;
29mod interpreter_result;
30mod interpreter_stack_value;
31mod literal;
32mod rib_function_invoke;
33mod rib_interpreter;
34mod rib_runtime_error;
35mod stack;
36
37use crate::{DefaultWorkerNameGenerator, GenerateWorkerName, RibByteCode};
38use std::sync::Arc;
39
40pub async fn interpret(
41    rib: RibByteCode,
42    rib_input: RibInput,
43    function_invoke: Arc<dyn RibComponentFunctionInvoke + Sync + Send>,
44    generate_worker_name: Option<Arc<dyn GenerateWorkerName + Sync + Send>>,
45) -> Result<RibResult, RibRuntimeError> {
46    let mut interpreter = Interpreter::new(
47        rib_input,
48        function_invoke,
49        generate_worker_name.unwrap_or_else(|| Arc::new(DefaultWorkerNameGenerator)),
50    );
51    interpreter.run(rib).await
52}
53
54// This function can be used for those the Rib Scripts
55// where there are no side effecting function calls.
56// It is recommended to use `interpret` over `interpret_pure` if you are unsure.
57pub async fn interpret_pure(
58    rib: RibByteCode,
59    rib_input: RibInput,
60    generate_worker_name: Option<Arc<dyn GenerateWorkerName + Sync + Send>>,
61) -> Result<RibResult, RibRuntimeError> {
62    let mut interpreter = Interpreter::pure(
63        rib_input,
64        generate_worker_name.unwrap_or_else(|| Arc::new(DefaultWorkerNameGenerator)),
65    );
66    interpreter.run(rib.clone()).await
67}
68
69#[macro_export]
70macro_rules! internal_corrupted_state {
71    ($fmt:expr) => {{
72        $crate::interpreter::rib_runtime_error::RibRuntimeError::InvariantViolation($crate::interpreter::rib_runtime_error::InvariantViolation::InternalCorruptedState($fmt.to_string()))
73    }};
74
75    ($fmt:expr, $($arg:tt)*) => {{
76        $crate::interpreter::rib_runtime_error::RibRuntimeError::InvariantViolation($crate::interpreter::rib_runtime_error::InvariantViolation::InternalCorruptedState(format!($fmt, $($arg)*)))
77    }};
78}
79
80#[macro_export]
81macro_rules! bail_corrupted_state {
82    ($fmt:expr) => {{
83        return Err($crate::interpreter::rib_runtime_error::RibRuntimeError::InvariantViolation($crate::interpreter::rib_runtime_error::InvariantViolation::InternalCorruptedState($fmt.to_string())));
84    }};
85
86    ($fmt:expr, $($arg:tt)*) => {{
87        return Err($crate::interpreter::rib_runtime_error::RibRuntimeError::InvariantViolation($crate::interpreter::rib_runtime_error::InvariantViolation::InternalCorruptedState(format!($fmt, $($arg)*))));
88    }};
89}