multiversx_sc_scenario/api/impl_vh/
vm_hooks_api.rsuse crate::debug_executor::StaticVarData;
use super::VMHooksApiBackend;
use std::marker::PhantomData;
use multiversx_chain_vm::executor::{MemPtr, VMHooks};
use multiversx_sc::api::{HandleTypeInfo, ManagedBufferApiImpl};
#[derive(Clone, Debug)]
pub struct VMHooksApi<S: VMHooksApiBackend> {
_phantom: PhantomData<S>,
}
impl<VHB: VMHooksApiBackend> VMHooksApi<VHB> {
pub fn api_impl() -> VMHooksApi<VHB> {
VMHooksApi {
_phantom: PhantomData,
}
}
pub fn with_vm_hooks<R, F>(&self, f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
{
VHB::with_vm_hooks(f)
}
pub fn with_vm_hooks_ctx_1<R, F>(&self, handle: &VHB::HandleType, f: F) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
{
VHB::with_vm_hooks_ctx_1(handle.clone(), f)
}
pub fn with_vm_hooks_ctx_2<R, F>(
&self,
handle1: &VHB::HandleType,
handle2: &VHB::HandleType,
f: F,
) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
{
VHB::with_vm_hooks_ctx_2(handle1.clone(), handle2.clone(), f)
}
pub fn with_vm_hooks_ctx_3<R, F>(
&self,
handle1: &VHB::HandleType,
handle2: &VHB::HandleType,
handle3: &VHB::HandleType,
f: F,
) -> R
where
F: FnOnce(&dyn VMHooks) -> R,
{
VHB::with_vm_hooks_ctx_3(handle1.clone(), handle2.clone(), handle3.clone(), f)
}
pub fn assert_live_handle(&self, handle: &VHB::HandleType) {
VHB::assert_live_handle(handle);
}
pub fn with_static_data<R, F>(&self, f: F) -> R
where
F: FnOnce(&StaticVarData) -> R,
{
VHB::with_static_data(f)
}
pub(crate) fn with_temp_buffer_ptr<R, F>(
&self,
handle: <Self as HandleTypeInfo>::ManagedBufferHandle,
length: usize,
f: F,
) -> R
where
F: FnOnce(MemPtr) -> R,
{
let mut temp_buffer = [0u8; 32];
self.mb_load_slice(handle, 0, &mut temp_buffer[..length])
.expect("error extracting address bytes");
f(temp_buffer.as_ptr() as MemPtr)
}
pub(crate) fn with_temp_address_ptr<R, F>(
&self,
handle: <Self as HandleTypeInfo>::ManagedBufferHandle,
f: F,
) -> R
where
F: FnOnce(MemPtr) -> R,
{
self.with_temp_buffer_ptr(handle, 32, f)
}
}
pub(crate) fn i32_to_bool(vm_hooks_result: i32) -> bool {
vm_hooks_result > 0
}
impl<VHB: VMHooksApiBackend> HandleTypeInfo for VMHooksApi<VHB> {
type ManagedBufferHandle = VHB::HandleType;
type BigIntHandle = VHB::HandleType;
type BigFloatHandle = VHB::HandleType;
type EllipticCurveHandle = VHB::HandleType;
type ManagedMapHandle = VHB::HandleType;
}
impl<VHB: VMHooksApiBackend> PartialEq for VMHooksApi<VHB> {
fn eq(&self, _: &Self) -> bool {
true
}
}
impl<VHB: VMHooksApiBackend> Eq for VMHooksApi<VHB> {}