code
stringlengths
40
729k
docstring
stringlengths
22
46.3k
func_name
stringlengths
1
97
language
stringclasses
1 value
repo
stringlengths
6
48
path
stringlengths
8
176
url
stringlengths
47
228
license
stringclasses
7 values
pub fn get_untyped_ptr(&mut self) -> NonNull<UntypedVal> { NonNull::from(&mut self.value) }
Returns a pointer to the [`UntypedVal`] of the [`Global`].
get_untyped_ptr
rust
wasmi-labs/wasmi
crates/core/src/global.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/global.rs
Apache-2.0
pub fn is<T: HostError>(&self) -> bool { (self as &dyn Any).is::<T>() }
Returns `true` if `self` is of type `T`.
is
rust
wasmi-labs/wasmi
crates/core/src/host_error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/host_error.rs
Apache-2.0
pub fn is_64(&self) -> bool { matches!(self, Self::I64) }
Returns `true` if `self` is [`IndexType::I64`].
is_64
rust
wasmi-labs/wasmi
crates/core/src/index_ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/index_ty.rs
Apache-2.0
pub fn max_size(&self) -> u128 { const WASM32_MAX_SIZE: u128 = 1 << 32; const WASM64_MAX_SIZE: u128 = 1 << 64; match self { Self::I32 => WASM32_MAX_SIZE, Self::I64 => WASM64_MAX_SIZE, } }
Returns the maximum size for Wasm memories and tables for `self`.
max_size
rust
wasmi-labs/wasmi
crates/core/src/index_ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/index_ty.rs
Apache-2.0
pub fn min(&self, other: &Self) -> Self { match (self, other) { (IndexType::I64, IndexType::I64) => IndexType::I64, _ => IndexType::I32, } }
Returns the minimum [`IndexType`] between `self` and `other`.
min
rust
wasmi-labs/wasmi
crates/core/src/index_ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/index_ty.rs
Apache-2.0
pub fn i32_exit_status(&self) -> Option<i32> { if let Self::I32Exit(status) = self { return Some(*status); } None }
Returns the classic `i32` exit program code of a `Trap` if any. Otherwise returns `None`.
i32_exit_status
rust
wasmi-labs/wasmi
crates/core/src/trap.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/trap.rs
Apache-2.0
fn with_reason(reason: TrapReason) -> Self { Self { reason: Box::new(reason), } }
Create a new [`Trap`] from the [`TrapReason`].
with_reason
rust
wasmi-labs/wasmi
crates/core/src/trap.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/trap.rs
Apache-2.0
pub fn trap_message(&self) -> &'static str { match self { Self::UnreachableCodeReached => "wasm `unreachable` instruction executed", Self::MemoryOutOfBounds => "out of bounds memory access", Self::TableOutOfBounds => "undefined element: out of bounds table access", Self::IndirectCallToNull => "uninitialized element 2", // TODO: fixme, remove the trailing " 2" again Self::IntegerDivisionByZero => "integer divide by zero", Self::IntegerOverflow => "integer overflow", Self::BadConversionToInteger => "invalid conversion to integer", Self::StackOverflow => "call stack exhausted", Self::BadSignature => "indirect call type mismatch", Self::OutOfFuel => "all fuel consumed by WebAssembly", Self::GrowthOperationLimited => "growth operation limited", } }
Returns the trap message as specified by the WebAssembly specification. # Note This API is primarily useful for the Wasm spec testsuite but might have other uses since it avoid heap memory allocation in certain cases.
trap_message
rust
wasmi-labs/wasmi
crates/core/src/trap.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/trap.rs
Apache-2.0
pub fn reinterpret(self, ty: ValType) -> Self { Self { ty, ..self } }
Changes the [`ValType`] of `self` to `ty`. # Note This acts similar to a Wasm reinterpret cast and the underlying `value` bits are unchanged.
reinterpret
rust
wasmi-labs/wasmi
crates/core/src/typed.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/typed.rs
Apache-2.0
fn write_lo64(&mut self, bits: u64) { self.lo64 = bits; }
Writes the low 64-bit of the [`UntypedVal`].
write_lo64
rust
wasmi-labs/wasmi
crates/core/src/untyped.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/untyped.rs
Apache-2.0
pub const fn from_bits64(lo64: u64) -> Self { Self { lo64, #[cfg(feature = "simd")] hi64: 0, } }
Creates an [`UntypedVal`] from the given lower 64-bit bits. This sets the high 64-bits to zero if any.
from_bits64
rust
wasmi-labs/wasmi
crates/core/src/untyped.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/untyped.rs
Apache-2.0
pub fn decode_slice<T>(slice: &[Self]) -> Result<T, UntypedError> where T: DecodeUntypedSlice, { <T as DecodeUntypedSlice>::decode_untyped_slice(slice) }
Decodes the slice of [`UntypedVal`] as a value of type `T`. # Note `T` can either be a single type or a tuple of types depending on the length of the `slice`. # Errors If the tuple length of `T` and the length of `slice` does not match.
decode_slice
rust
wasmi-labs/wasmi
crates/core/src/untyped.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/untyped.rs
Apache-2.0
pub fn encode_slice<T>(slice: &mut [Self], input: T) -> Result<(), UntypedError> where T: EncodeUntypedSlice, { <T as EncodeUntypedSlice>::encode_untyped_slice(input, slice) }
Encodes the slice of [`UntypedVal`] from the given value of type `T`. # Note `T` can either be a single type or a tuple of types depending on the length of the `slice`. # Errors If the tuple length of `T` and the length of `slice` does not match.
encode_slice
rust
wasmi-labs/wasmi
crates/core/src/untyped.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/untyped.rs
Apache-2.0
pub fn is_num(&self) -> bool { matches!(self, Self::I32 | Self::I64 | Self::F32 | Self::F64) }
Returns `true` if [`ValType`] is a Wasm numeric type. This is `true` for [`ValType::I32`], [`ValType::I64`], [`ValType::F32`] and [`ValType::F64`].
is_num
rust
wasmi-labs/wasmi
crates/core/src/value.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/value.rs
Apache-2.0
pub fn is_ref(&self) -> bool { matches!(self, Self::ExternRef | Self::FuncRef) }
Returns `true` if [`ValType`] is a Wasm reference type. This is `true` for [`ValType::FuncRef`] and [`ValType::ExternRef`].
is_ref
rust
wasmi-labs/wasmi
crates/core/src/value.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/value.rs
Apache-2.0
fn combine128(lo: i64, hi: i64) -> i128 { let lo = i128::from(lo as u64); let hi = i128::from(hi as u64); (hi << 64) | lo }
Combines the two 64-bit `lo` and `hi` into a single `i128` value.
combine128
rust
wasmi-labs/wasmi
crates/core/src/wasm.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/wasm.rs
Apache-2.0
fn split128(value: i128) -> (i64, i64) { let hi = (value >> 64) as i64; let lo = value as i64; (lo, hi) }
Splits the single `i128` value into a 64-bit `lo` and `hi` part.
split128
rust
wasmi-labs/wasmi
crates/core/src/wasm.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/wasm.rs
Apache-2.0
pub fn i64_add128(lhs_lo: i64, lhs_hi: i64, rhs_lo: i64, rhs_hi: i64) -> (i64, i64) { let lhs = combine128(lhs_lo, lhs_hi); let rhs = combine128(rhs_lo, rhs_hi); let result = lhs.wrapping_add(rhs); split128(result) }
Execute an `i64.add128` Wasm instruction. Returns a pair of `(lo, hi)` 64-bit values representing the 128-bit result. # Note This instruction is part of the Wasm `wide-arithmetic` proposal.
i64_add128
rust
wasmi-labs/wasmi
crates/core/src/wasm.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/wasm.rs
Apache-2.0
pub fn i64_sub128(lhs_lo: i64, lhs_hi: i64, rhs_lo: i64, rhs_hi: i64) -> (i64, i64) { let lhs = combine128(lhs_lo, lhs_hi); let rhs = combine128(rhs_lo, rhs_hi); let result = lhs.wrapping_sub(rhs); split128(result) }
Execute an `i64.sub128` Wasm instruction. Returns a pair of `(lo, hi)` 64-bit values representing the 128-bit result. # Note This instruction is part of the Wasm `wide-arithmetic` proposal.
i64_sub128
rust
wasmi-labs/wasmi
crates/core/src/wasm.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/wasm.rs
Apache-2.0
pub fn i64_mul_wide_s(lhs: i64, rhs: i64) -> (i64, i64) { let lhs = i128::from(lhs); let rhs = i128::from(rhs); let result = lhs.wrapping_mul(rhs); split128(result) }
Execute an `i64.mul_wide_s` Wasm instruction. Returns a pair of `(lo, hi)` 64-bit values representing the 128-bit result. # Note This instruction is part of the Wasm `wide-arithmetic` proposal.
i64_mul_wide_s
rust
wasmi-labs/wasmi
crates/core/src/wasm.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/wasm.rs
Apache-2.0
pub fn i64_mul_wide_u(lhs: i64, rhs: i64) -> (i64, i64) { let lhs = u128::from(lhs as u64); let rhs = u128::from(rhs as u64); let result = lhs.wrapping_mul(rhs); split128(result as i128) }
Execute an `i64.mul_wide_s` Wasm instruction. Returns a pair of `(lo, hi)` 64-bit values representing the 128-bit result. # Note This instruction is part of the Wasm `wide-arithmetic` proposal.
i64_mul_wide_u
rust
wasmi-labs/wasmi
crates/core/src/wasm.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/wasm.rs
Apache-2.0
fn effective_address(ptr: u64, offset: u64) -> Result<usize, TrapCode> { let Some(address) = ptr.checked_add(offset) else { return Err(TrapCode::MemoryOutOfBounds); }; usize::try_from(address).map_err(|_| TrapCode::MemoryOutOfBounds) }
Calculates the effective address of a linear memory access. # Errors If the resulting effective address overflows.
effective_address
rust
wasmi-labs/wasmi
crates/core/src/memory/access.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/access.rs
Apache-2.0
pub fn load<T>(memory: &[u8], ptr: u64, offset: u64) -> Result<T, TrapCode> where T: LittleEndianConvert, { let address = effective_address(ptr, offset)?; load_at::<T>(memory, address) }
Executes a generic `T.load` Wasm operation. # Errors - If `ptr + offset` overflows. - If `ptr + offset` loads out of bounds from `memory`.
load
rust
wasmi-labs/wasmi
crates/core/src/memory/access.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/access.rs
Apache-2.0
pub fn load_at<T>(memory: &[u8], address: usize) -> Result<T, TrapCode> where T: LittleEndianConvert, { let mut buffer = <<T as LittleEndianConvert>::Bytes as Default>::default(); buffer.load_into(memory, address)?; let value: T = <T as LittleEndianConvert>::from_le_bytes(buffer); Ok(value) }
Executes a generic `T.load` Wasm operation. # Errors If `address` loads out of bounds from `memory`.
load_at
rust
wasmi-labs/wasmi
crates/core/src/memory/access.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/access.rs
Apache-2.0
pub fn load_extend<T, U>(memory: &[u8], ptr: u64, offset: u64) -> Result<T, TrapCode> where U: LittleEndianConvert + ExtendInto<T>, { let address = effective_address(ptr, offset)?; load_extend_at::<T, U>(memory, address) }
Executes a generic `T.loadN_[s|u]` Wasm operation. # Errors - If `ptr + offset` overflows. - If `ptr + offset` loads out of bounds from `memory`.
load_extend
rust
wasmi-labs/wasmi
crates/core/src/memory/access.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/access.rs
Apache-2.0
pub fn load_extend_at<T, U>(memory: &[u8], address: usize) -> Result<T, TrapCode> where U: LittleEndianConvert + ExtendInto<T>, { load_at::<U>(memory, address).map(ExtendInto::extend_into) }
Executes a generic `T.loadN_[s|u]` Wasm operation. # Errors If `address` loads out of bounds from `memory`.
load_extend_at
rust
wasmi-labs/wasmi
crates/core/src/memory/access.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/access.rs
Apache-2.0
pub fn store<T>(memory: &mut [u8], ptr: u64, offset: u64, value: T) -> Result<(), TrapCode> where T: LittleEndianConvert, { let address = effective_address(ptr, offset)?; store_at::<T>(memory, address, value) }
Executes a generic `T.store` Wasm operation. # Errors - If `ptr + offset` overflows. - If `ptr + offset` stores out of bounds from `memory`.
store
rust
wasmi-labs/wasmi
crates/core/src/memory/access.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/access.rs
Apache-2.0
pub fn store_at<T>(memory: &mut [u8], address: usize, value: T) -> Result<(), TrapCode> where T: LittleEndianConvert, { let buffer = <T as LittleEndianConvert>::into_le_bytes(value); buffer.store_from(memory, address)?; Ok(()) }
Executes a generic `T.load` Wasm operation. # Errors If `address` loads out of bounds from `memory`.
store_at
rust
wasmi-labs/wasmi
crates/core/src/memory/access.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/access.rs
Apache-2.0
pub fn store_wrap<T, U>(memory: &mut [u8], ptr: u64, offset: u64, value: T) -> Result<(), TrapCode> where T: WrapInto<U>, U: LittleEndianConvert, { let address = effective_address(ptr, offset)?; store_wrap_at::<T, U>(memory, address, value) }
Executes a generic `T.store[N]` Wasm operation. # Errors - If `ptr + offset` overflows. - If `ptr + offset` stores out of bounds from `memory`.
store_wrap
rust
wasmi-labs/wasmi
crates/core/src/memory/access.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/access.rs
Apache-2.0
pub fn store_wrap_at<T, U>(memory: &mut [u8], address: usize, value: T) -> Result<(), TrapCode> where T: WrapInto<U>, U: LittleEndianConvert, { store_at::<U>(memory, address, value.wrap_into()) }
Executes a generic `T.store[N]` Wasm operation. # Errors - If `address` stores out of bounds from `memory`.
store_wrap_at
rust
wasmi-labs/wasmi
crates/core/src/memory/access.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/access.rs
Apache-2.0
fn vec_into_raw_parts(vec: Vec<u8>) -> (*mut u8, usize, usize) { let mut vec = ManuallyDrop::new(vec); (vec.as_mut_ptr(), vec.len(), vec.capacity()) }
Decomposes the `Vec<u8>` into its raw components. Returns the raw pointer to the underlying data, the length of the vector (in bytes), and the allocated capacity of the data (in bytes). These are the same arguments in the same order as the arguments to [`Vec::from_raw_parts`]. # Safety After calling this function, the caller is responsible for the memory previously managed by the `Vec`. The only way to do this is to convert the raw pointer, length, and capacity back into a `Vec` with the [`Vec::from_raw_parts`] function, allowing the destructor to perform the cleanup. # Note This utility method is required since [`Vec::into_raw_parts`] is not yet stable unfortunately. (Date: 2024-03-14)
vec_into_raw_parts
rust
wasmi-labs/wasmi
crates/core/src/memory/buffer.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/buffer.rs
Apache-2.0
pub fn new(size: usize) -> Result<Self, MemoryError> { let mut vec = Vec::new(); if vec.try_reserve(size).is_err() { return Err(MemoryError::OutOfSystemMemory); }; vec.extend(iter::repeat_n(0x00_u8, size)); let (ptr, len, capacity) = vec_into_raw_parts(vec); Ok(Self { ptr, len, capacity, is_static: false, }) }
Creates a new byte buffer with the given initial `size` in bytes. # Errors If the requested amount of heap bytes could not be allocated.
new
rust
wasmi-labs/wasmi
crates/core/src/memory/buffer.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/buffer.rs
Apache-2.0
pub fn new_static(buffer: &'static mut [u8], size: usize) -> Result<Self, MemoryError> { let Some(bytes) = buffer.get_mut(..size) else { return Err(MemoryError::InvalidStaticBufferSize); }; bytes.fill(0x00_u8); Ok(Self { ptr: buffer.as_mut_ptr(), len: size, capacity: buffer.len(), is_static: true, }) }
Creates a new static byte buffer with the given `size` in bytes. This will zero all the bytes in `buffer[0..initial_len`]. # Errors If `size` is greater than the length of `buffer`.
new_static
rust
wasmi-labs/wasmi
crates/core/src/memory/buffer.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/buffer.rs
Apache-2.0
pub fn grow(&mut self, new_size: usize) -> Result<(), MemoryError> { assert!(self.len() <= new_size); match self.get_vec() { Some(vec) => self.grow_vec(vec, new_size), None => self.grow_static(new_size), } }
Grows the byte buffer to the given `new_size`. The newly added bytes will be zero initialized. # Panics - If the current size of the [`ByteBuffer`] is larger than `new_size`. # Errors - If it is not possible to grow the [`ByteBuffer`] to `new_size`. - `vec`: If the system allocator ran out of memory to allocate. - `static`: If `new_size` is larger than it's the static buffer capacity.
grow
rust
wasmi-labs/wasmi
crates/core/src/memory/buffer.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/buffer.rs
Apache-2.0
fn grow_vec(&mut self, mut vec: Vec<u8>, new_size: usize) -> Result<(), MemoryError> { debug_assert!(vec.len() <= new_size); let additional = new_size - vec.len(); if vec.try_reserve(additional).is_err() { return Err(MemoryError::OutOfSystemMemory); }; vec.resize(new_size, 0x00_u8); (self.ptr, self.len, self.capacity) = vec_into_raw_parts(vec); Ok(()) }
Grow the byte buffer to the given `new_size` when backed by a [`Vec`].
grow_vec
rust
wasmi-labs/wasmi
crates/core/src/memory/buffer.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/buffer.rs
Apache-2.0
fn grow_static(&mut self, new_size: usize) -> Result<(), MemoryError> { if self.capacity < new_size { return Err(MemoryError::InvalidStaticBufferSize); } let len = self.len(); self.len = new_size; self.data_mut()[len..new_size].fill(0x00_u8); Ok(()) }
Grow the byte buffer to the given `new_size` when backed by a `&'static [u8]`.
grow_static
rust
wasmi-labs/wasmi
crates/core/src/memory/buffer.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/buffer.rs
Apache-2.0
pub fn data(&self) -> &[u8] { // # Safety // // The byte buffer is either backed by a `Vec<u8>` or a &'static [u8]` // which are both valid byte slices in the range `self.ptr[0..self.len]`. unsafe { slice::from_raw_parts(self.ptr, self.len) } }
Returns a shared slice to the bytes underlying to the byte buffer.
data
rust
wasmi-labs/wasmi
crates/core/src/memory/buffer.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/buffer.rs
Apache-2.0
pub fn data_mut(&mut self) -> &mut [u8] { // # Safety // // The byte buffer is either backed by a `Vec<u8>` or a &'static [u8]` // which are both valid byte slices in the range `self.ptr[0..self.len]`. unsafe { slice::from_raw_parts_mut(self.ptr, self.len) } }
Returns an exclusive slice to the bytes underlying to the byte buffer.
data_mut
rust
wasmi-labs/wasmi
crates/core/src/memory/buffer.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/buffer.rs
Apache-2.0
fn get_vec(&mut self) -> Option<Vec<u8>> { if self.is_static { return None; } // Safety // // - At this point we are guaranteed that the byte buffer is backed by a `Vec` // so it is safe to reconstruct the `Vec` by its raw parts. Some(unsafe { Vec::from_raw_parts(self.ptr, self.len, self.capacity) }) }
Returns the underlying `Vec<u8>` if the byte buffer is not backed by a static buffer. Otherwise returns `None`. # Note The returned `Vec` will free its memory and thus the memory of the [`ByteBuffer`] if dropped.
get_vec
rust
wasmi-labs/wasmi
crates/core/src/memory/buffer.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/buffer.rs
Apache-2.0
pub fn dynamic_ty(&self) -> MemoryType { let current_pages = self.size(); let maximum_pages = self.ty().maximum(); let page_size_log2 = self.ty().page_size_log2(); let is_64 = self.ty().is_64(); let mut b = MemoryType::builder(); b.min(current_pages); b.max(maximum_pages); b.page_size_log2(page_size_log2); b.memory64(is_64); b.build() .expect("must result in valid memory type due to invariants") }
Returns the dynamic [`MemoryType`] of the [`Memory`]. # Note This respects the current size of the [`Memory`] as its minimum size and is useful for import subtyping checks.
dynamic_ty
rust
wasmi-labs/wasmi
crates/core/src/memory/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/mod.rs
Apache-2.0
pub fn size(&self) -> u64 { (self.bytes.len() as u64) >> self.memory_type.page_size_log2() }
Returns the size, in WebAssembly pages, of this Wasm linear memory.
size
rust
wasmi-labs/wasmi
crates/core/src/memory/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/mod.rs
Apache-2.0
fn size_in_bytes(&self) -> u64 { let pages = self.size(); let bytes_per_page = u64::from(self.memory_type.page_size()); let Some(bytes) = pages.checked_mul(bytes_per_page) else { panic!( "unexpected out of bounds linear memory size: \ (pages = {pages}, bytes_per_page = {bytes_per_page})" ) }; bytes }
Returns the size of this Wasm linear memory in bytes.
size_in_bytes
rust
wasmi-labs/wasmi
crates/core/src/memory/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/mod.rs
Apache-2.0
fn max_size_in_bytes(&self) -> Option<u64> { let max_pages = self.memory_type.maximum()?; let bytes_per_page = u64::from(self.memory_type.page_size()); let Some(max_bytes) = max_pages.checked_mul(bytes_per_page) else { panic!( "unexpected out of bounds linear memory maximum size: \ (max_pages = {max_pages}, bytes_per_page = {bytes_per_page})" ) }; Some(max_bytes) }
Returns the maximum size of this Wasm linear memory in bytes if any.
max_size_in_bytes
rust
wasmi-labs/wasmi
crates/core/src/memory/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/mod.rs
Apache-2.0
fn minimum_byte_size(&self) -> Result<u128, SizeOverflow> { let min = u128::from(self.minimum); if min > self.absolute_max() { return Err(SizeOverflow); } Ok(min << self.page_size_log2) }
Returns the minimum size, in bytes, that the linear memory must have. # Errors If the calculation of the minimum size overflows the maximum size. This means that the linear memory can't be allocated. The caller is responsible to deal with that situation.
minimum_byte_size
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
fn maximum_byte_size(&self) -> Result<u128, SizeOverflow> { match self.maximum { Some(max) => { let max = u128::from(max); if max > self.absolute_max() { return Err(SizeOverflow); } Ok(max << self.page_size_log2) } None => Ok(self.max_size_based_on_index_type()), } }
Returns the maximum size, in bytes, that the linear memory must have. # Note If the maximum size of a memory type is not specified a concrete maximum value is returned dependent on the index type of the memory type. # Errors If the calculation of the maximum size overflows the index type. This means that the linear memory can't be allocated. The caller is responsible to deal with that situation.
maximum_byte_size
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
fn page_size(&self) -> u32 { debug_assert!( self.page_size_log2 == 16 || self.page_size_log2 == 0, "invalid `page_size_log2`: {}; must be 16 or 0", self.page_size_log2 ); 1 << self.page_size_log2 }
Returns the size of the linear memory pages in bytes.
page_size
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
fn absolute_max(&self) -> u128 { self.max_size_based_on_index_type() >> self.page_size_log2 }
Returns the absolute maximum size in pages that a linear memory is allowed to have.
absolute_max
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
pub fn memory64(&mut self, memory64: bool) -> &mut Self { self.inner.index_type = match memory64 { true => IndexType::I64, false => IndexType::I32, }; self }
Set whether this is a 64-bit memory type or not. By default a memory is a 32-bit, a.k.a. `false`. 64-bit memories are part of the [Wasm `memory64` proposal]. [Wasm `memory64` proposal]: https://github.com/WebAssembly/memory64
memory64
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
pub fn min(&mut self, minimum: u64) -> &mut Self { self.inner.minimum = minimum; self }
Sets the minimum number of pages the built [`MemoryType`] supports. The default minimum is `0`.
min
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
pub fn max(&mut self, maximum: Option<u64>) -> &mut Self { self.inner.maximum = maximum; self }
Sets the optional maximum number of pages the built [`MemoryType`] supports. A value of `None` means that there is no maximum number of pages. The default maximum is `None`.
max
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
pub fn build(self) -> Result<MemoryType, MemoryError> { self.validate()?; Ok(MemoryType { inner: self.inner }) }
Finalize the construction of the [`MemoryType`]. # Errors If the chosen configuration for the constructed [`MemoryType`] is invalid.
build
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
fn validate(&self) -> Result<(), MemoryError> { match self.inner.page_size_log2 { 0 | MemoryType::DEFAULT_PAGE_SIZE_LOG2 => {} _ => { // Case: currently, pages sizes log2 can only be 0 or 16. // Note: Future Wasm extensions might allow more values. return Err(MemoryError::InvalidMemoryType); } } if self.inner.minimum_byte_size().is_err() { // Case: the minimum size overflows a `absolute_max` return Err(MemoryError::InvalidMemoryType); } if let Some(max) = self.inner.maximum { if self.inner.maximum_byte_size().is_err() { // Case: the maximum size overflows a `absolute_max` return Err(MemoryError::InvalidMemoryType); } if self.inner.minimum > max { // Case: maximum size must be at least as large as minimum size return Err(MemoryError::InvalidMemoryType); } } Ok(()) }
Validates the configured [`MemoryType`] of the [`MemoryTypeBuilder`]. # Errors If the chosen configuration for the constructed [`MemoryType`] is invalid.
validate
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
pub fn is_subtype_of(&self, other: &Self) -> bool { if self.is_64() != other.is_64() { return false; } if self.page_size() != other.page_size() { return false; } if self.minimum() < other.minimum() { return false; } match (self.maximum(), other.maximum()) { (_, None) => true, (Some(max), Some(other_max)) => max <= other_max, _ => false, } }
Returns `true` if the [`MemoryType`] is a subtype of the `other` [`MemoryType`]. # Note This implements the [subtyping rules] according to the WebAssembly spec. [import subtyping]: https://webassembly.github.io/spec/core/valid/types.html#import-subtyping
is_subtype_of
rust
wasmi-labs/wasmi
crates/core/src/memory/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/memory/ty.rs
Apache-2.0
pub fn new<I>(ty: ValType, items: I) -> Self where I: IntoIterator<Item = UntypedVal>, { let items: Box<[UntypedVal]> = items.into_iter().collect(); assert!( u32::try_from(items.len()).is_ok(), "element segment has too many items: {}", items.len() ); Self { ty, items } }
Creates a new [`ElementSegment`]. # Panics If the length of `items` exceeds `u32`.
new
rust
wasmi-labs/wasmi
crates/core/src/table/element.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/element.rs
Apache-2.0
pub fn drop_items(&mut self) { self.items = [].into(); }
Drops the items of the [`ElementSegment`].
drop_items
rust
wasmi-labs/wasmi
crates/core/src/table/element.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/element.rs
Apache-2.0
pub fn size(self) -> u32 { let len = self.items().len(); u32::try_from(len).unwrap_or_else(|_| { panic!("element segments are ensured to have at most 2^32 items but found: {len}") }) }
Returns the number of items in the [`ElementSegment`].
size
rust
wasmi-labs/wasmi
crates/core/src/table/element.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/element.rs
Apache-2.0
pub fn dynamic_ty(&self) -> TableType { TableType::new_impl( self.ty().element(), self.ty().index_ty(), self.size(), self.ty().maximum(), ) }
Returns the dynamic [`TableType`] of the [`Table`]. # Note This respects the current size of the [`Table`] as its minimum size and is useful for import subtyping checks.
dynamic_ty
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn size(&self) -> u64 { let len = self.elements.len(); let Ok(len) = u64::try_from(len) else { panic!("`table.size` is out of system bounds: {len}"); }; len }
Returns the current size of the [`Table`].
size
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn get(&self, index: u64) -> Option<TypedVal> { let untyped = self.get_untyped(index)?; let value = TypedVal::new(self.ty().element(), untyped); Some(value) }
Returns the [`Table`] element value at `index`. Returns `None` if `index` is out of bounds.
get
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn get_untyped(&self, index: u64) -> Option<UntypedVal> { let index = usize::try_from(index).ok()?; self.elements.get(index).copied() }
Returns the untyped [`Table`] element value at `index`. Returns `None` if `index` is out of bounds. # Note This is a more efficient version of [`Table::get`] for internal use only.
get_untyped
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn set(&mut self, index: u64, value: TypedVal) -> Result<(), TableError> { self.ty().ensure_element_type_matches(value.ty())?; self.set_untyped(index, value.into()) }
Sets the [`TypedVal`] of this [`Table`] at `index`. # Errors - If `index` is out of bounds. - If `value` does not match the [`Table`] element type.
set
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn set_untyped(&mut self, index: u64, value: UntypedVal) -> Result<(), TableError> { let Some(untyped) = self.elements.get_mut(index as usize) else { return Err(TableError::SetOutOfBounds); }; *untyped = value; Ok(()) }
Returns the [`UntypedVal`] of the [`Table`] at `index`. # Errors If `index` is out of bounds.
set_untyped
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn init( &mut self, element: ElementSegmentRef, dst_index: u64, src_index: u32, len: u32, fuel: Option<&mut Fuel>, ) -> Result<(), TableError> { let table_type = self.ty(); assert!( table_type.element().is_ref(), "table.init currently only works on reftypes" ); self.ty().ensure_element_type_matches(element.ty())?; // Convert parameters to indices. let Ok(dst_index) = usize::try_from(dst_index) else { return Err(TableError::InitOutOfBounds); }; let Ok(src_index) = usize::try_from(src_index) else { return Err(TableError::InitOutOfBounds); }; let Ok(len_size) = usize::try_from(len) else { return Err(TableError::InitOutOfBounds); }; // Perform bounds check before anything else. let dst_items = self .elements .get_mut(dst_index..) .and_then(|items| items.get_mut(..len_size)) .ok_or(TableError::InitOutOfBounds)?; let src_items = element .items() .get(src_index..) .and_then(|items| items.get(..len_size)) .ok_or(TableError::InitOutOfBounds)?; if len == 0 { // Bail out early if nothing needs to be initialized. // The Wasm spec demands to still perform the bounds check // so we cannot bail out earlier. return Ok(()); } if let Some(fuel) = fuel { fuel.consume_fuel_if(|costs| costs.fuel_for_copying_values(u64::from(len)))?; } // Perform the actual table initialization. dst_items.copy_from_slice(src_items); Ok(()) }
Initialize `len` elements from `src_element[src_index..]` into `self[dst_index..]`. # Errors Returns an error if the range is out of bounds of either the source or destination tables. # Panics If the [`ElementSegment`] element type does not match the [`Table`] element type. Note: This is a panic instead of an error since it is asserted at Wasm validation time.
init
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn copy( dst_table: &mut Self, dst_index: u64, src_table: &Self, src_index: u64, len: u64, fuel: Option<&mut Fuel>, ) -> Result<(), TableError> { dst_table .ty() .ensure_element_type_matches(src_table.ty().element())?; // Turn parameters into proper slice indices. let Ok(src_index) = usize::try_from(src_index) else { return Err(TableError::CopyOutOfBounds); }; let Ok(dst_index) = usize::try_from(dst_index) else { return Err(TableError::CopyOutOfBounds); }; let Ok(len_size) = usize::try_from(len) else { return Err(TableError::CopyOutOfBounds); }; // Perform bounds check before anything else. let dst_items = dst_table .elements .get_mut(dst_index..) .and_then(|items| items.get_mut(..len_size)) .ok_or(TableError::CopyOutOfBounds)?; let src_items = src_table .elements .get(src_index..) .and_then(|items| items.get(..len_size)) .ok_or(TableError::CopyOutOfBounds)?; if let Some(fuel) = fuel { fuel.consume_fuel_if(|costs| costs.fuel_for_copying_values(len))?; } // Finally, copy elements in-place for the table. dst_items.copy_from_slice(src_items); Ok(()) }
Copy `len` elements from `src_table[src_index..]` into `dst_table[dst_index..]`. # Errors Returns an error if the range is out of bounds of either the source or destination tables.
copy
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn copy_within( &mut self, dst_index: u64, src_index: u64, len: u64, fuel: Option<&mut Fuel>, ) -> Result<(), TableError> { // These accesses just perform the bounds checks required by the Wasm spec. let max_offset = cmp::max(dst_index, src_index); max_offset .checked_add(len) .filter(|&offset| offset <= self.size()) .ok_or(TableError::CopyOutOfBounds)?; // Turn parameters into proper indices. let Ok(src_index) = usize::try_from(src_index) else { return Err(TableError::CopyOutOfBounds); }; let Ok(dst_index) = usize::try_from(dst_index) else { return Err(TableError::CopyOutOfBounds); }; let Ok(len_size) = usize::try_from(len) else { return Err(TableError::CopyOutOfBounds); }; if let Some(fuel) = fuel { fuel.consume_fuel_if(|costs| costs.fuel_for_copying_values(len))?; } // Finally, copy elements in-place for the table. self.elements .copy_within(src_index..src_index.wrapping_add(len_size), dst_index); Ok(()) }
Copy `len` elements from `self[src_index..]` into `self[dst_index..]`. # Errors Returns an error if the range is out of bounds of the table.
copy_within
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn fill( &mut self, dst: u64, val: TypedVal, len: u64, fuel: Option<&mut Fuel>, ) -> Result<(), TableError> { self.ty().ensure_element_type_matches(val.ty())?; self.fill_untyped(dst, val.into(), len, fuel) }
Fill `table[dst..(dst + len)]` with the given value. # Errors - If `val` has a type mismatch with the element type of the [`Table`]. - If the region to be filled is out of bounds for the [`Table`]. - If `val` originates from a different [`Store`] than the [`Table`]. # Panics If `ctx` does not own `dst_table` or `src_table`. [`Store`]: [`crate::Store`]
fill
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn fill_untyped( &mut self, dst: u64, val: UntypedVal, len: u64, fuel: Option<&mut Fuel>, ) -> Result<(), TableError> { let Ok(dst_index) = usize::try_from(dst) else { return Err(TableError::FillOutOfBounds); }; let Ok(len_size) = usize::try_from(len) else { return Err(TableError::FillOutOfBounds); }; let dst = self .elements .get_mut(dst_index..) .and_then(|elements| elements.get_mut(..len_size)) .ok_or(TableError::FillOutOfBounds)?; if let Some(fuel) = fuel { fuel.consume_fuel_if(|costs| costs.fuel_for_copying_values(len))?; } dst.fill(val); Ok(()) }
Fill `table[dst..(dst + len)]` with the given value. # Note This is an API for internal use only and exists for efficiency reasons. # Errors - If the region to be filled is out of bounds for the [`Table`]. # Panics If `ctx` does not own `dst_table` or `src_table`. [`Store`]: [`crate::Store`]
fill_untyped
rust
wasmi-labs/wasmi
crates/core/src/table/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/mod.rs
Apache-2.0
pub fn new(element: ValType, min: u32, max: Option<u32>) -> Self { Self::new_impl(element, IndexType::I32, u64::from(min), max.map(u64::from)) }
Creates a new [`TableType`]. # Panics If `min` is greater than `max`.
new
rust
wasmi-labs/wasmi
crates/core/src/table/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/ty.rs
Apache-2.0
pub fn new64(element: ValType, min: u64, max: Option<u64>) -> Self { Self::new_impl(element, IndexType::I64, min, max) }
Creates a new [`TableType`] with a 64-bit index type. # Note 64-bit tables are part of the [Wasm `memory64` proposal]. [Wasm `memory64` proposal]: https://github.com/WebAssembly/memory64 # Panics If `min` is greater than `max`.
new64
rust
wasmi-labs/wasmi
crates/core/src/table/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/ty.rs
Apache-2.0
pub(crate) fn new_impl( element: ValType, index_ty: IndexType, min: u64, max: Option<u64>, ) -> Self { let absolute_max = index_ty.max_size(); assert!(u128::from(min) <= absolute_max); max.inspect(|&max| { assert!(min <= max && u128::from(max) <= absolute_max); }); Self { element, min, max, index_ty, } }
Convenience constructor to create a new [`TableType`].
new_impl
rust
wasmi-labs/wasmi
crates/core/src/table/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/ty.rs
Apache-2.0
pub(crate) fn ensure_element_type_matches(&self, ty: ValType) -> Result<(), TableError> { if self.element() != ty { return Err(TableError::ElementTypeMismatch); } Ok(()) }
Returns `Ok` if the element type of `self` matches `ty`. # Errors Returns a [`TableError::ElementTypeMismatch`] otherwise.
ensure_element_type_matches
rust
wasmi-labs/wasmi
crates/core/src/table/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/ty.rs
Apache-2.0
pub fn is_subtype_of(&self, other: &Self) -> bool { if self.is_64() != other.is_64() { return false; } if self.element() != other.element() { return false; } if self.minimum() < other.minimum() { return false; } match (self.maximum(), other.maximum()) { (_, None) => true, (Some(max), Some(other_max)) => max <= other_max, _ => false, } }
Returns `true` if the [`TableType`] is a subtype of the `other` [`TableType`]. # Note This implements the [subtyping rules] according to the WebAssembly spec. [import subtyping]: https://webassembly.github.io/spec/core/valid/types.html#import-subtyping
is_subtype_of
rust
wasmi-labs/wasmi
crates/core/src/table/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/core/src/table/ty.rs
Apache-2.0
fn to_compile_error(&self) -> proc_macro::TokenStream { let message = &self.0; quote! { ::core::compile_error!(#message) }.into() }
Converts the [`Error`] into a `compile_error!` token stream.
to_compile_error
rust
wasmi-labs/wasmi
crates/c_api/macro/lib.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/macro/lib.rs
Apache-2.0
pub(crate) fn handle_result<T>( result: Result<T>, ok_then: impl FnOnce(T), ) -> Option<Box<wasmi_error_t>> { match result { Ok(value) => { ok_then(value); None } Err(error) => Some(Box::new(wasmi_error_t::from(error))), } }
Convenience method, applies `ok_then(T)` if `result` is `Ok` and otherwise returns a [`wasmi_error_t`].
handle_result
rust
wasmi-labs/wasmi
crates/c_api/src/error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/error.rs
Apache-2.0
pub(crate) fn func(&self) -> Func { match self.inner.which { Extern::Func(f) => f, _ => unsafe { hint::unreachable_unchecked() }, } }
Returns the underlying [`Func`] of the [`wasm_func_t`].
func
rust
wasmi-labs/wasmi
crates/c_api/src/func.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/func.rs
Apache-2.0
unsafe fn create_function( store: &mut wasm_store_t, ty: &wasm_functype_t, func: impl Fn(*const wasm_val_vec_t, *mut wasm_val_vec_t) -> Option<Box<wasm_trap_t>> + Send + Sync + 'static, ) -> Box<wasm_func_t> { let ty = ty.ty().ty.clone(); let func = Func::new( store.inner.context_mut(), ty, move |_caller, params, results| { let params: wasm_val_vec_t = params .iter() .cloned() .map(wasm_val_t::from) .collect::<Box<[_]>>() .into(); let mut out_results: wasm_val_vec_t = vec![wasm_val_t::default(); results.len()].into(); if let Some(trap) = func(&params, &mut out_results) { return Err(trap.error); } results .iter_mut() .zip(out_results.as_slice()) .for_each(|(result, out_results)| { *result = out_results.to_val(); }); Ok(()) }, ); Box::new(wasm_func_t { inner: wasm_extern_t { store: store.inner.clone(), which: func.into(), }, }) }
Creates a [`wasm_func_t`] from the [`wasm_functype_t`] and C-like closure for the [`wasm_store_t`]. # Note This is a convenience method that internally creates a trampoline Rust-like closure around that C-like closure to propagate the Wasm function call and do all marshalling that is required. # Safety It is the caller's responsibility not to alias the [`wasm_functype_t`] with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
create_function
rust
wasmi-labs/wasmi
crates/c_api/src/func.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/func.rs
Apache-2.0
fn prepare_params_and_results( dst: &mut Vec<Val>, params: impl ExactSizeIterator<Item = Val>, len_results: usize, ) -> (&[Val], &mut [Val]) { debug_assert!(dst.is_empty()); let len_params = params.len(); dst.reserve(len_params + len_results); dst.extend(params); dst.extend(iter::repeat_n(Val::FuncRef(FuncRef::null()), len_results)); let (params, results) = dst.split_at_mut(len_params); (params, results) }
Prepares `dst` to be populated with `params` and reserve space for `len_results`. The parameters and results are returned as separate slices.
prepare_params_and_results
rust
wasmi-labs/wasmi
crates/c_api/src/func.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/func.rs
Apache-2.0
fn error_from_panic(panic: Box<dyn Any + Send>) -> Error { if let Some(msg) = panic.downcast_ref::<String>() { Error::new(msg.clone()) } else if let Some(msg) = panic.downcast_ref::<&'static str>() { Error::new(*msg) } else { Error::new("panic happened on the Rust side") } }
Converts the panic data to a Wasmi [`Error`] as a best-effort basis.
error_from_panic
rust
wasmi-labs/wasmi
crates/c_api/src/func.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/func.rs
Apache-2.0
fn global(&self) -> Global { match self.inner.which { Extern::Global(g) => g, _ => unsafe { hint::unreachable_unchecked() }, } }
Returns the underlying [`Global`] of the [`wasm_global_t`].
global
rust
wasmi-labs/wasmi
crates/c_api/src/global.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/global.rs
Apache-2.0
pub(crate) fn new(store: WasmStoreRef, instance: Instance) -> wasm_instance_t { wasm_instance_t { store, inner: instance, } }
Creates a new [`wasm_instance_t`] with the `store` wrapping `instance`.
new
rust
wasmi-labs/wasmi
crates/c_api/src/instance.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/instance.rs
Apache-2.0
fn memory(&self) -> Memory { match self.inner.which { Extern::Memory(m) => m, _ => unsafe { hint::unreachable_unchecked() }, } }
Returns the underlying [`Memory`] of the [`wasm_memory_t`].
memory
rust
wasmi-labs/wasmi
crates/c_api/src/memory.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/memory.rs
Apache-2.0
fn fill_exports(module: &Module, out: &mut wasm_exporttype_vec_t) { let exports = module .exports() .map(|e| { Some(Box::new(wasm_exporttype_t::new( String::from(e.name()), CExternType::new(e.ty().clone()), ))) }) .collect::<Box<[_]>>(); out.set_buffer(exports); }
Fills `out` with the exports of the [`Module`].
fill_exports
rust
wasmi-labs/wasmi
crates/c_api/src/module.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/module.rs
Apache-2.0
fn fill_imports(module: &Module, out: &mut wasm_importtype_vec_t) { let imports = module .imports() .map(|i| { Some(Box::new(wasm_importtype_t::new( String::from(i.module()), String::from(i.name()), CExternType::new(i.ty().clone()), ))) }) .collect::<Box<[_]>>(); out.set_buffer(imports); }
Fills `out` with the imports of the [`Module`].
fill_imports
rust
wasmi-labs/wasmi
crates/c_api/src/module.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/module.rs
Apache-2.0
pub fn is_func(&self) -> bool { matches!(self, Self::Func(_)) }
Returns `true` if `self` is a Wasm function reference.
is_func
rust
wasmi-labs/wasmi
crates/c_api/src/ref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/ref.rs
Apache-2.0
pub fn is_null(&self) -> bool { match self { WasmRef::Func(r) => r.is_null(), WasmRef::Extern(r) => r.is_null(), } }
Returns `true` if `self` is a `null` reference.
is_null
rust
wasmi-labs/wasmi
crates/c_api/src/ref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/ref.rs
Apache-2.0
pub(crate) fn new(r: WasmRef) -> Option<Box<wasm_ref_t>> { if r.is_null() || !r.is_func() { None } else { Some(Box::new(wasm_ref_t { inner: r })) } }
Creates a new boxed [`wasm_ref_t`] from the given [`WasmRef`].
new
rust
wasmi-labs/wasmi
crates/c_api/src/ref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/ref.rs
Apache-2.0
pub(crate) fn ref_to_val(r: &wasm_ref_t) -> Val { match r.inner { WasmRef::Func(r) => Val::FuncRef(r), WasmRef::Extern(r) => Val::ExternRef(r), } }
Converts the [`wasm_ref_t`] into a Wasmi [`Val`]. # Note This clones the [`wasm_ref_t`] if necessary and does not consume it.
ref_to_val
rust
wasmi-labs/wasmi
crates/c_api/src/ref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/ref.rs
Apache-2.0
fn table(&self) -> Table { match self.inner.which { Extern::Table(t) => t, _ => unsafe { hint::unreachable_unchecked() }, } }
Returns the underlying [`Table`] of the [`wasm_table_t`].
table
rust
wasmi-labs/wasmi
crates/c_api/src/table.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/table.rs
Apache-2.0
fn option_wasm_ref_t_to_ref(r: Option<&wasm_ref_t>, table_ty: &TableType) -> WasmRef { r.map(|r| r.inner.clone()) .unwrap_or_else(|| match table_ty.element() { wasmi::core::ValType::FuncRef => WasmRef::Func(FuncRef::null()), wasmi::core::ValType::ExternRef => WasmRef::Extern(ExternRef::null()), invalid => panic!("encountered invalid table type: {invalid:?}"), }) }
Returns the [`WasmRef`] respective to the optional [`wasm_ref_t`]. Returns a `null` [`WasmRef`] if [`wasm_ref_t`] is `None`.
option_wasm_ref_t_to_ref
rust
wasmi-labs/wasmi
crates/c_api/src/table.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/table.rs
Apache-2.0
pub unsafe fn slice_from_raw_parts<'a, T>(ptr: *const T, len: usize) -> &'a [T] { match len { 0 => &[], _ => slice::from_raw_parts(ptr, len), } }
Convenience method for creating a shared Rust slice from C inputs. # Note Returns an empty Rust slice if `len` is 0 disregarding `ptr`.
slice_from_raw_parts
rust
wasmi-labs/wasmi
crates/c_api/src/utils.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/utils.rs
Apache-2.0
pub unsafe fn slice_from_raw_parts_mut<'a, T>(ptr: *mut T, len: usize) -> &'a mut [T] { match len { 0 => &mut [], _ => slice::from_raw_parts_mut(ptr, len), } }
Convenience method for creating a mutable Rust slice from C inputs. # Note Returns an empty Rust slice if `len` is 0 disregarding `ptr`.
slice_from_raw_parts_mut
rust
wasmi-labs/wasmi
crates/c_api/src/utils.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/utils.rs
Apache-2.0
pub(crate) fn initialize<T>(dst: &mut MaybeUninit<T>, value: T) { unsafe { ptr::write(dst.as_mut_ptr(), value); } }
Initialize a `MaybeUninit<T>` with `value`. # ToDo Replace calls to this function with [`MaybeUninit::write`] once it is stable. [`MaybeUninit::write`]: https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.write
initialize
rust
wasmi-labs/wasmi
crates/c_api/src/utils.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/utils.rs
Apache-2.0
pub fn to_val(&self) -> Val { match into_valtype(self.kind) { ValType::I32 => Val::from(unsafe { self.of.i32 }), ValType::I64 => Val::from(unsafe { self.of.i64 }), ValType::F32 => Val::from(F32::from(unsafe { self.of.f32 })), ValType::F64 => Val::from(F64::from(unsafe { self.of.f64 })), ValType::V128 => Val::from(V128::from(unsafe { self.of.v128 })), ValType::FuncRef => match unsafe { self.of.ref_ }.is_null() { true => Val::FuncRef(FuncRef::null()), false => ref_to_val(unsafe { &*self.of.ref_ }), }, ValType::ExternRef => { core::unreachable!("`wasm_val_t`: cannot contain non-function reference values") } } }
Creates a new [`Val`] from the [`wasm_val_t`]. # Note This effectively clones the [`wasm_val_t`] if necessary.
to_val
rust
wasmi-labs/wasmi
crates/c_api/src/val.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/val.rs
Apache-2.0
pub(crate) fn into_valtype(kind: wasm_valkind_t) -> ValType { match kind { wasm_valkind_t::WASM_I32 => ValType::I32, wasm_valkind_t::WASM_I64 => ValType::I64, wasm_valkind_t::WASM_F32 => ValType::F32, wasm_valkind_t::WASM_F64 => ValType::F64, wasm_valkind_t::WASM_V128 => ValType::V128, wasm_valkind_t::WASM_EXTERNREF => ValType::ExternRef, wasm_valkind_t::WASM_FUNCREF => ValType::FuncRef, } }
Converts the [`wasm_valkind_t`] into the respective [`ValType`].
into_valtype
rust
wasmi-labs/wasmi
crates/c_api/src/types/val.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/types/val.rs
Apache-2.0
pub(crate) fn from_valtype(ty: &ValType) -> wasm_valkind_t { match ty { ValType::I32 => wasm_valkind_t::WASM_I32, ValType::I64 => wasm_valkind_t::WASM_I64, ValType::F32 => wasm_valkind_t::WASM_F32, ValType::F64 => wasm_valkind_t::WASM_F64, ValType::V128 => wasm_valkind_t::WASM_V128, ValType::ExternRef => wasm_valkind_t::WASM_EXTERNREF, ValType::FuncRef => wasm_valkind_t::WASM_FUNCREF, } }
Converts the [`ValType`] into the respective [`wasm_valkind_t`].
from_valtype
rust
wasmi-labs/wasmi
crates/c_api/src/types/val.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/c_api/src/types/val.rs
Apache-2.0
pub fn enable_nan_canonicalization(&mut self) { self.inner.canonicalize_nans = true; }
Enable NaN canonicalization. # Note Enable NaN canonicalization to avoid non-determinism between Wasm runtimes for differential fuzzing.
enable_nan_canonicalization
rust
wasmi-labs/wasmi
crates/fuzz/src/config.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/config.rs
Apache-2.0
pub fn export_everything(&mut self) { self.inner.export_everything = true; }
Export everything. This is required to query state and call Wasm functions.
export_everything
rust
wasmi-labs/wasmi
crates/fuzz/src/config.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/config.rs
Apache-2.0
pub fn disable_multi_memory(&mut self) { self.inner.multi_value_enabled = false; self.inner.max_memories = cmp::min(self.inner.max_memories, 1); }
Disable the Wasm `multi-memory` proposal. This is required by some supported Wasm runtime oracles.
disable_multi_memory
rust
wasmi-labs/wasmi
crates/fuzz/src/config.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/config.rs
Apache-2.0
pub fn disable_custom_page_sizes(&mut self) { self.inner.custom_page_sizes_enabled = false; }
Disable the Wasm `custom-page-sizes` proposal. This is required by some supported Wasm runtime oracles.
disable_custom_page_sizes
rust
wasmi-labs/wasmi
crates/fuzz/src/config.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/config.rs
Apache-2.0
pub fn disable_wide_arithmetic(&mut self) { self.inner.wide_arithmetic_enabled = false; }
Disable the Wasm `wide-arithmetic` proposal. This is required by some supported Wasm runtime oracles.
disable_wide_arithmetic
rust
wasmi-labs/wasmi
crates/fuzz/src/config.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/config.rs
Apache-2.0