use lehit::{
runtime::{AbiCallError, AbiCallStatus, ExecutionMode, RuntimeBuilder},
semantic::{CanonicalSymbolPath, SemanticNamedType},
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut runtime = RuntimeBuilder::new()
.with_execution_mode(ExecutionMode::Checked)
.build()?;
runtime.load_script(
"app://checked-entry.lt",
"as fn(argument: usize) -> i32
let values: [i32; 2] = [11, 22] return values[argument]",
)?;
let function = runtime
.function("app://checked-entry.lt", "")?
.abi_function()?;
let mut call = function.abi_call_builder();
call.push_rust_value_argument(1usize)?;
assert_eq!(call.call()?.into_rust_value::<i32>()?, 22);
let mut call = function.abi_call_builder();
call.push_rust_value_argument(3usize)?;
assert!(matches!(
call.call(),
Err(AbiCallError::Execution(status))
if status == AbiCallStatus::BOUNDS_VIOLATION
));
let aggregate_path = "app://aggregate-entry.lt";
let pair_type = SemanticNamedType {
path: CanonicalSymbolPath {
module: aggregate_path.into(),
local: "Pair".into(),
},
generic_arguments: Vec::new(),
};
runtime.load_script(
aggregate_path,
"as fn() -> Pair
pub acs Pair: struct { pub fld values: [i32; 2] }
return Pair { values: [41, 42] }",
)?;
let old_function = runtime.function(aggregate_path, "")?.abi_function()?;
let old_descriptor = old_function.lehit_type_descriptor::<8>(pair_type.clone())?;
let old_value = old_function
.abi_call_builder()
.call()?
.into_leased_lehit_value(&old_descriptor)?;
assert_eq!(old_value.as_bytes(), &[41, 0, 0, 0, 42, 0, 0, 0]);
runtime.reload_script(
aggregate_path,
"as fn() -> Pair
pub acs Pair: struct { pub fld values: [i32; 2] }
return Pair { values: [43, 44] }",
)?;
let new_function = runtime.function(aggregate_path, "")?.abi_function()?;
let new_descriptor = new_function.lehit_type_descriptor::<8>(pair_type.clone())?;
let new_value = new_function
.abi_call_builder()
.call()?
.into_leased_lehit_value(&new_descriptor)?;
assert_eq!(new_value.as_bytes(), &[43, 0, 0, 0, 44, 0, 0, 0]);
assert_eq!(
old_function
.abi_call_builder()
.call()?
.into_leased_lehit_value(&old_descriptor)?
.as_bytes(),
&[41, 0, 0, 0, 42, 0, 0, 0]
);
runtime.unload_module(aggregate_path)?;
assert!(runtime.unit_for_module(aggregate_path).is_none());
assert_eq!(old_value.as_bytes(), &[41, 0, 0, 0, 42, 0, 0, 0]);
assert_eq!(new_value.as_bytes(), &[43, 0, 0, 0, 44, 0, 0, 0]);
println!("Lehit embedding example completed successfully");
Ok(())
}