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 disable_memory64(&mut self) { self.inner.memory64_enabled = false; }
Disable the Wasm `memory64` proposal. This is required by some supported Wasm runtime oracles.
disable_memory64
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_simd(&mut self) { self.inner.simd_enabled = false; }
Disable the Wasm `simd` proposal. This is required by some supported Wasm runtime oracles.
disable_simd
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_relaxed_simd(&mut self) { self.inner.relaxed_simd_enabled = false; }
Disable the Wasm `relaxed-simd` proposal. This is required by some supported Wasm runtime oracles.
disable_relaxed_simd
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 generate_crash_inputs(target: &str, wasm: &[u8]) -> Result<String, anyhow::Error> { let mut sha2 = Sha256::default(); sha2.update(wasm); let hash: [u8; 32] = sha2.finalize().into(); let hash_str = hash_str(hash)?; let wat = wasmprinter::print_bytes(wasm)?; let dir_path = format!("fuzz/crash-inputs/{target}"); let file_path = format!("crash-{hash_str}"); fs::create_dir_all(&dir_path)?; fs::write(format!("{dir_path}/{file_path}.wasm"), wasm)?; fs::write(format!("{dir_path}/{file_path}.wat"), wat)?; Ok(hash_str) }
Writes `.wasm` and `.wat` files for `target` with `wasm` contents. Returns the `hex` encoded string of the SHA-2 of the `wasm` input upon success. # Errors - If hex encoding fails. - If converting `.wasm` to `.wat` fails. - If writing the `.wasm` or `.wat` files fails.
generate_crash_inputs
rust
wasmi-labs/wasmi
crates/fuzz/src/crash_inputs.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/crash_inputs.rs
Apache-2.0
fn hash_str(hash: [u8; 32]) -> Result<String, anyhow::Error> { let mut s = String::new(); for byte in hash { write!(s, "{byte:02X}")?; } Ok(s) }
Returns the `hex` string of the `[u8; 32]` hash.
hash_str
rust
wasmi-labs/wasmi
crates/fuzz/src/crash_inputs.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/crash_inputs.rs
Apache-2.0
pub fn is_non_deterministic(&self) -> bool { matches!(self, Self::Trap(TrapCode::StackOverflow) | Self::Other) }
Returns `true` if `self` may be of non-deterministic origin.
is_non_deterministic
rust
wasmi-labs/wasmi
crates/fuzz/src/error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/error.rs
Apache-2.0
pub fn new( config: impl Into<wasm_smith::Config>, u: &mut Unstructured, ) -> arbitrary::Result<Self> { let config = config.into(); let module = wasm_smith::Module::new(config, u)?; Ok(Self { module }) }
Creates a new [`FuzzModule`] from the given `config` and fuzz input bytes, `u`.
new
rust
wasmi-labs/wasmi
crates/fuzz/src/module.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/module.rs
Apache-2.0
pub fn with_type(ty: FuzzValType, u: &mut Unstructured) -> Self { match ty { FuzzValType::I32 => Self::I32(i32::arbitrary(u).unwrap_or_default()), FuzzValType::I64 => Self::I64(i64::arbitrary(u).unwrap_or_default()), FuzzValType::F32 => Self::F32(f32::arbitrary(u).unwrap_or_default()), FuzzValType::F64 => Self::F64(f64::arbitrary(u).unwrap_or_default()), FuzzValType::V128 => Self::V128(u128::arbitrary(u).unwrap_or_default()), FuzzValType::FuncRef => Self::FuncRef { is_null: true }, FuzzValType::ExternRef => Self::ExternRef { is_null: true }, } }
Creates a new [`FuzzVal`] of the given `ty` initialized by `u`.
with_type
rust
wasmi-labs/wasmi
crates/fuzz/src/value.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/value.rs
Apache-2.0
pub(crate) fn push_func(&mut self, name: &str, ty: FuncType) { self.funcs.push(name); self.func_types.push(ty); }
Pushes an exported function `name` to `self`.
push_func
rust
wasmi-labs/wasmi
crates/fuzz/src/oracle/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/oracle/exports.rs
Apache-2.0
pub fn funcs(&self) -> ExportedFuncsIter { ExportedFuncsIter { names: self.funcs.iter(), types: self.func_types.iter(), } }
Returns an iterator yielding the names of the exported Wasm functions.
funcs
rust
wasmi-labs/wasmi
crates/fuzz/src/oracle/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/oracle/exports.rs
Apache-2.0
pub fn iter(&self) -> StringSequenceIter { StringSequenceIter { iter: self.strings.iter(), } }
Returns an iterator over the strings in `self`. The iterator yields the strings in order of their insertion.
iter
rust
wasmi-labs/wasmi
crates/fuzz/src/oracle/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/oracle/exports.rs
Apache-2.0
pub fn configure(&self, fuzz_config: &mut FuzzSmithConfig) { match self { ChosenOracle::WasmiStack => WasmiStackOracle::configure(fuzz_config), ChosenOracle::Wasmtime => WasmtimeOracle::configure(fuzz_config), } }
Configures `fuzz_config` for the chosen differential fuzzing oracle.
configure
rust
wasmi-labs/wasmi
crates/fuzz/src/oracle/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/oracle/mod.rs
Apache-2.0
pub fn setup(&self, wasm: &[u8]) -> Option<Box<dyn DifferentialOracle>> { let oracle: Box<dyn DifferentialOracle> = match self { ChosenOracle::WasmiStack => Box::new(WasmiStackOracle::setup(wasm)?), ChosenOracle::Wasmtime => Box::new(WasmtimeOracle::setup(wasm)?), }; Some(oracle) }
Sets up the chosen differential fuzzing oracle.
setup
rust
wasmi-labs/wasmi
crates/fuzz/src/oracle/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/oracle/mod.rs
Apache-2.0
pub fn exports(&self) -> ModuleExports { let mut exports = ModuleExports::default(); for export in self.instance.exports(&self.store) { let name = export.name(); match export.ty(&self.store) { wasmi::ExternType::Func(ty) => exports.push_func(name, ty), wasmi::ExternType::Global(_) => exports.push_global(name), wasmi::ExternType::Memory(_) => exports.push_memory(name), wasmi::ExternType::Table(_) => exports.push_table(name), }; } exports }
Returns the Wasm module export names.
exports
rust
wasmi-labs/wasmi
crates/fuzz/src/oracle/wasmi.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/fuzz/src/oracle/wasmi.rs
Apache-2.0
pub fn return_reg2_ext(reg0: impl Into<Reg>, reg1: impl Into<Reg>) -> Self { Self::return_reg2([reg0.into(), reg1.into()]) }
Creates a new [`Instruction::ReturnReg2`] for the given [`Reg`] indices.
return_reg2_ext
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn return_reg3_ext( reg0: impl Into<Reg>, reg1: impl Into<Reg>, reg2: impl Into<Reg>, ) -> Self { Self::return_reg3([reg0.into(), reg1.into(), reg2.into()]) }
Creates a new [`Instruction::ReturnReg3`] for the given [`Reg`] indices.
return_reg3_ext
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn return_many_ext( reg0: impl Into<Reg>, reg1: impl Into<Reg>, reg2: impl Into<Reg>, ) -> Self { Self::return_many([reg0.into(), reg1.into(), reg2.into()]) }
Creates a new [`Instruction::ReturnMany`] for the given [`Reg`] indices.
return_many_ext
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn register_list_ext( reg0: impl Into<Reg>, reg1: impl Into<Reg>, reg2: impl Into<Reg>, ) -> Self { Self::register_list([reg0.into(), reg1.into(), reg2.into()]) }
Creates a new [`Instruction::RegisterList`] instruction parameter.
register_list_ext
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn register_and_offset_hi(reg: impl Into<Reg>, offset_hi: Offset64Hi) -> Self { Self::register_and_imm32(reg, offset_hi.0) }
Creates a new [`Instruction::RegisterAndImm32`] from the given `reg` and `offset_hi`.
register_and_offset_hi
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn filter_register_and_offset_hi(self) -> Result<(Reg, Offset64Hi), Self> { if let Instruction::RegisterAndImm32 { reg, imm } = self { return Ok((reg, Offset64Hi(u32::from(imm)))); } Err(self) }
Returns `Some` [`Reg`] and [`Offset64Hi`] if encoded properly. # Errors Returns back `self` if it was an incorrect [`Instruction`]. This allows for a better error message to inform the user.
filter_register_and_offset_hi
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn register_and_lane<LaneType>(reg: impl Into<Reg>, lane: LaneType) -> Self where LaneType: Into<u8>, { Self::register_and_imm32(reg, u32::from(lane.into())) }
Creates a new [`Instruction::RegisterAndImm32`] from the given `reg` and `offset_hi`.
register_and_lane
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn filter_register_and_lane<LaneType>(self) -> Result<(Reg, LaneType), Self> where LaneType: TryFrom<u8>, { if let Instruction::RegisterAndImm32 { reg, imm } = self { let lane_index = u32::from(imm) as u8; let Ok(lane) = LaneType::try_from(lane_index) else { panic!("encountered out of bounds lane index: {}", lane_index) }; return Ok((reg, lane)); } Err(self) }
Returns `Some` [`Reg`] and a `lane` index if encoded properly. # Errors Returns back `self` if it was an incorrect [`Instruction`]. This allows for a better error message to inform the user.
filter_register_and_lane
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn imm16_and_offset_hi(value: impl Into<AnyConst16>, offset_hi: Offset64Hi) -> Self { Self::imm16_and_imm32(value, offset_hi.0) }
Creates a new [`Instruction::Imm16AndImm32`] from the given `value` and `offset_hi`.
imm16_and_offset_hi
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn filter_imm16_and_offset_hi<T>(self) -> Result<(T, Offset64Hi), Self> where T: From<AnyConst16>, { if let Instruction::Imm16AndImm32 { imm16, imm32 } = self { return Ok((T::from(imm16), Offset64Hi(u32::from(imm32)))); } Err(self) }
Returns `Some` [`Reg`] and [`Offset64Hi`] if encoded properly. # Errors Returns back `self` if it was an incorrect [`Instruction`]. This allows for a better error message to inform the user.
filter_imm16_and_offset_hi
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn lane_and_memory_index(value: impl Into<u8>, memory: Memory) -> Self { Self::imm16_and_imm32(u16::from(value.into()), u32::from(memory)) }
Creates a new [`Instruction::Imm16AndImm32`] from the given `lane` and `memory` index.
lane_and_memory_index
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn filter_lane_and_memory<LaneType>(self) -> Result<(LaneType, index::Memory), Self> where LaneType: TryFrom<u8>, { if let Instruction::Imm16AndImm32 { imm16, imm32 } = self { let Ok(lane) = LaneType::try_from(i16::from(imm16) as u16 as u8) else { return Err(self); }; return Ok((lane, index::Memory::from(u32::from(imm32)))); } Err(self) }
Returns `Some` lane and [`index::Memory`] if encoded properly. # Errors Returns back `self` if it was an incorrect [`Instruction`]. This allows for a better error message to inform the user.
filter_lane_and_memory
rust
wasmi-labs/wasmi
crates/ir/src/enum.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/enum.rs
Apache-2.0
pub fn is_zero(&self) -> bool { self.inner == AnyConst16::from(0_i16) }
Returns `true` if the [`Const16`] is equal to zero.
is_zero
rust
wasmi-labs/wasmi
crates/ir/src/immeditate.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/immeditate.rs
Apache-2.0
pub fn is_default(&self) -> bool { self.0 == 0 }
Returns `true` if `self` refers to the default linear memory which always is at index 0.
is_default
rust
wasmi-labs/wasmi
crates/ir/src/index.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/index.rs
Apache-2.0
fn new(is_positive: bool) -> Self { Self { is_positive, marker: PhantomData, } }
Create a new typed [`Sign`] with the given value.
new
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn is_init(self) -> bool { self.to_i16() != 0 }
Returns `true` if the [`BranchOffset16`] has been initialized.
is_init
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn init(&mut self, valid_offset: BranchOffset) -> Result<(), Error> { assert!(valid_offset.is_init()); assert!(!self.is_init()); let valid_offset16 = Self::try_from(valid_offset)?; *self = valid_offset16; Ok(()) }
Initializes the [`BranchOffset`] with a proper value. # Panics - If the [`BranchOffset`] have already been initialized. - If the given [`BranchOffset`] is not properly initialized. # Errors If `valid_offset` cannot be encoded as 16-bit [`BranchOffset16`].
init
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn from_src_to_dst(src: u32, dst: u32) -> Result<Self, Error> { let src = i64::from(src); let dst = i64::from(dst); let Some(offset) = dst.checked_sub(src) else { // Note: This never needs to be called on backwards branches since they are immediated resolved. unreachable!( "offset for forward branches must have `src` be smaller than or equal to `dst`" ); }; let Ok(offset) = i32::try_from(offset) else { return Err(Error::BranchOffsetOutOfBounds); }; Ok(Self(offset)) }
Creates an initialized [`BranchOffset`] from `src` to `dst`. # Errors If the resulting [`BranchOffset`] is out of bounds.
from_src_to_dst
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn is_init(self) -> bool { self.to_i32() != 0 }
Returns `true` if the [`BranchOffset`] has been initialized.
is_init
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn init(&mut self, valid_offset: BranchOffset) { assert!(valid_offset.is_init()); assert!(!self.is_init()); *self = valid_offset; }
Initializes the [`BranchOffset`] with a proper value. # Panics - If the [`BranchOffset`] have already been initialized. - If the given [`BranchOffset`] is not properly initialized.
init
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn bump_by(&mut self, amount: u64) -> Result<(), Error> { let new_amount = self .to_u64() .checked_add(amount) .ok_or(Error::BlockFuelOutOfBounds)?; self.0 = u32::try_from(new_amount).map_err(|_| Error::BlockFuelOutOfBounds)?; Ok(()) }
Bump the fuel by `amount` if possible. # Errors If the new fuel amount after this operation is out of bounds.
bump_by
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn from_u64(value: u64) -> Option<Self> { let hi = (value >> 32) as u32; let lo = (value & 0xFFFF_FFFF) as u32; let cmp = Comparator::try_from(hi).ok()?; let offset = BranchOffset::from(lo as i32); Some(Self { cmp, offset }) }
Creates a new [`ComparatorAndOffset`] from the given `u64` value. Returns `None` if the `u64` has an invalid encoding.
from_u64
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn as_u64(&self) -> u64 { let hi = self.cmp as u64; let lo = self.offset.to_i32() as u64; (hi << 32) | lo }
Converts the [`ComparatorAndOffset`] into an `u64` value.
as_u64
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn split(offset: u64) -> (Offset64Hi, Offset64Lo) { let offset_lo = (offset & 0xFFFF_FFFF) as u32; let offset_hi = (offset >> 32) as u32; (Offset64Hi(offset_hi), Offset64Lo(offset_lo)) }
Creates a new [`Offset64`] lo-hi pair from the given `offset`.
split
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn combine(hi: Offset64Hi, lo: Offset64Lo) -> Self { let hi = hi.0 as u64; let lo = lo.0 as u64; Self((hi << 32) | lo) }
Combines the given [`Offset64`] lo-hi pair into an [`Offset64`].
combine
rust
wasmi-labs/wasmi
crates/ir/src/primitive.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/primitive.rs
Apache-2.0
pub fn head_mut(&mut self) -> &mut Reg { &mut self.0 }
Returns an exclusive reference to the head [`Reg`] of the [`RegSpan`].
head_mut
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn has_overlapping_copies(results: Self, values: Self, len: u16) -> bool { RegSpanIter::has_overlapping_copies(results.iter(len), values.iter(len)) }
Returns `true` if `copy_span results <- values` has overlapping copies. # Examples - `[ ]`: empty never overlaps - `[ 1 <- 0 ]`: single element never overlaps - `[ 0 <- 1, 1 <- 2, 2 <- 3 ]`: no overlap - `[ 1 <- 0, 2 <- 1 ]`: overlaps!
has_overlapping_copies
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn to_array(self) -> [Reg; 2] { let span = self.span(); let fst = span.head(); let snd = fst.next(); [fst, snd] }
Returns an array of the results represented by `self`.
to_array
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn new(span: RegSpan) -> Result<Self, Error> { let head = span.head(); if head >= head.next_n(N) { return Err(Error::RegisterOutOfBounds); } Ok(Self { span }) }
Creates a new [`RegSpan`] starting with the given `start` [`Reg`].
new
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn span_mut(&mut self) -> &mut RegSpan { &mut self.span }
Returns an exclusive reference to the underlying [`RegSpan`] of `self`.
span_mut
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn contains(self, reg: Reg) -> bool { if self.is_empty() { return false; } let min = self.span.head(); let max = min.next_n(N); min <= reg && reg < max }
Returns `true` if the [`Reg`] is contained in `self`.
contains
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn is_empty(self) -> bool { N == 0 }
Returns `true` if `self` is empty.
is_empty
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn new(span: RegSpan, len: u16) -> Self { Self { span, len } }
Creates a new [`BoundedRegSpan`] from the given `span` and `len`.
new
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn span_mut(&mut self) -> &mut RegSpan { &mut self.span }
Returns a mutable reference to the underlying [`RegSpan`].
span_mut
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn contains(self, reg: Reg) -> bool { if self.is_empty() { return false; } let min = self.span.head(); let max = min.next_n(self.len); min <= reg && reg < max }
Returns `true` if the [`Reg`] is contained in `self`.
contains
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn is_empty(&self) -> bool { self.len() == 0 }
Returns `true` if `self` is empty.
is_empty
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn from_raw_parts(start: Reg, end: Reg) -> Self { debug_assert!(i16::from(start) <= i16::from(end)); Self { next: start, last: end, } }
Creates a [`RegSpanIter`] from then given raw `start` and `end` [`Reg`].
from_raw_parts
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
fn new(start: Reg, len: usize) -> Self { let len = u16::try_from(len) .unwrap_or_else(|_| panic!("out of bounds length for register span: {len}")); Self::new_u16(start, len) }
Creates a new [`RegSpanIter`] for the given `start` [`Reg`] and length `len`. # Panics If the `start..end` [`Reg`] span indices are out of bounds.
new
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
fn new_u16(start: Reg, len: u16) -> Self { let next = start; let last = start .0 .checked_add_unsigned(len) .map(Reg) .expect("overflowing register index for register span"); Self::from_raw_parts(next, last) }
Creates a new [`RegSpanIter`] for the given `start` [`Reg`] and length `len`. # Panics If the `start..end` [`Reg`] span indices are out of bounds.
new_u16
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn is_empty(&self) -> bool { self.len_as_u16() == 0 }
Returns `true` if `self` yields no more [`Reg`]s.
is_empty
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn has_overlapping_copies(results: Self, values: Self) -> bool { assert_eq!( results.len(), values.len(), "cannot copy between different sized register spans" ); let len = results.len(); if len <= 1 { // Empty spans or single-element spans can never overlap. return false; } let first_value = values.span().head(); let first_result = results.span().head(); if first_value >= first_result { // This case can never result in overlapping copies. return false; } let mut values = values; let last_value = values .next_back() .expect("span is non empty and thus must return"); last_value >= first_result }
Returns `true` if `copy_span results <- values` has overlapping copies. # Examples - `[ ]`: empty never overlaps - `[ 1 <- 0 ]`: single element never overlaps - `[ 0 <- 1, 1 <- 2, 2 <- 3 ]`: no overlap - `[ 1 <- 0, 2 <- 1 ]`: overlaps!
has_overlapping_copies
rust
wasmi-labs/wasmi
crates/ir/src/span.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/span.rs
Apache-2.0
pub fn visit_regs<V: VisitRegs>(&mut self, visitor: &mut V) { HostVisitor::host_visitor(self, visitor) }
Visit [`Reg`]s of `self` via the `visitor`.
visit_regs
rust
wasmi-labs/wasmi
crates/ir/src/visit_regs.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/ir/src/visit_regs.rs
Apache-2.0
pub fn add_wasi_snapshot_preview1_to_linker<T, U>( linker: &mut Linker<T>, wasi_ctx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, ) -> Result<(), Error> where U: WasiSnapshotPreview1, { <Linker<T> as AddWasi<T>>::add_wasi(linker, wasi_ctx) }
Adds the entire WASI API to the Wasmi [`Linker`]. Once linked, users can make use of all the low-level functionalities that `WASI` provides. You could call them `syscall`s and you'd be correct, because they mirror what a non-os-dependent set of syscalls would look like. You now have access to resources such as files, directories, random number generators, and certain parts of the networking stack. # Note `WASI` is versioned in snapshots. It's still a WIP. Currently, this crate supports `preview_1` Look [here](https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md) for more details.
add_wasi_snapshot_preview1_to_linker
rust
wasmi-labs/wasmi
crates/wasi/src/sync/snapshots/preview_1.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasi/src/sync/snapshots/preview_1.rs
Apache-2.0
pub fn add_wasi_snapshot_preview1_to_linker_builder<T, U>( linker: &mut LinkerBuilder<Constructing, T>, wasi_ctx: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, ) -> Result<(), Error> where U: WasiSnapshotPreview1, { <LinkerBuilder<Constructing, T> as AddWasi<T>>::add_wasi(linker, wasi_ctx) }
Adds the entire WASI API to the Wasmi [`LinkerBuilder`]. For more information view [`add_wasi_snapshot_preview1_to_linker`].
add_wasi_snapshot_preview1_to_linker_builder
rust
wasmi-labs/wasmi
crates/wasi/src/sync/snapshots/preview_1.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasi/src/sync/snapshots/preview_1.rs
Apache-2.0
fn generate_unique_host_functions(count: usize) -> Vec<(String, FuncType)> { let types = [ ValType::I32, ValType::I64, ValType::F32, ValType::F64, ValType::FuncRef, ValType::ExternRef, ]; (0..count) .map(|i| { let func_name = format!("{i}"); let (len_params, len_results) = if i % 2 == 0 { ((i / (types.len() * 2)) + 1, 0) } else { (0, (i / (types.len() * 2)) + 1) }; let chosen_type = types[i % 4]; let func_type = FuncType::new( vec![chosen_type; len_params], vec![chosen_type; len_results], ); (func_name, func_type) }) .collect() }
Generates `count` host functions with different signatures.
generate_unique_host_functions
rust
wasmi-labs/wasmi
crates/wasmi/benches/benches.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/benches/benches.rs
Apache-2.0
pub fn load_module_from_file(file_name: &str) -> wasmi::Module { let wasm = load_wasm_from_file(file_name); let engine = wasmi::Engine::new(&bench_config()); wasmi::Module::new(&engine, wasm).unwrap_or_else(|error| { panic!("could not parse Wasm module from file {file_name}: {error}",) }) }
Parses the Wasm binary at the given `file_name` into a Wasmi module. # Note This includes validation and compilation to Wasmi bytecode. # Panics If the benchmark Wasm file could not be opened, read or parsed.
load_module_from_file
rust
wasmi-labs/wasmi
crates/wasmi/benches/bench/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/benches/bench/mod.rs
Apache-2.0
pub fn load_instance_from_file(file_name: &str) -> (wasmi::Store<()>, wasmi::Instance) { let module = load_module_from_file(file_name); let linker = <wasmi::Linker<()>>::new(module.engine()); let mut store = wasmi::Store::new(module.engine(), ()); let instance = linker .instantiate(&mut store, &module) .unwrap() .start(&mut store) .unwrap(); (store, instance) }
Parses the Wasm binary from the given `file_name` into a Wasmi module. # Note This includes validation and compilation to Wasmi bytecode. # Panics If the benchmark Wasm file could not be opened, read or parsed.
load_instance_from_file
rust
wasmi-labs/wasmi
crates/wasmi/benches/bench/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/benches/bench/mod.rs
Apache-2.0
pub fn load_instance_from_wat(wasm: &[u8]) -> (wasmi::Store<()>, wasmi::Instance) { let engine = wasmi::Engine::new(&bench_config()); let module = wasmi::Module::new(&engine, wasm).unwrap(); let linker = <wasmi::Linker<()>>::new(&engine); let mut store = wasmi::Store::new(&engine, ()); let instance = linker .instantiate(&mut store, &module) .unwrap() .start(&mut store) .unwrap(); (store, instance) }
Parses the Wasm source from the given `.wat` bytes into a Wasmi module. # Note This includes validation and compilation to Wasmi bytecode. # Panics If the benchmark Wasm file could not be opened, read or parsed.
load_instance_from_wat
rust
wasmi-labs/wasmi
crates/wasmi/benches/bench/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/benches/bench/mod.rs
Apache-2.0
fn from_kind(kind: ErrorKind) -> Self { Self { kind: Box::new(kind), } }
Creates a new [`Error`] from the [`ErrorKind`].
from_kind
rust
wasmi-labs/wasmi
crates/wasmi/src/error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/error.rs
Apache-2.0
pub(crate) fn is_out_of_fuel(&self) -> bool { matches!( self.kind(), ErrorKind::TrapCode(TrapCode::OutOfFuel) | ErrorKind::ResumableOutOfFuel(_) | ErrorKind::Memory(MemoryError::OutOfFuel { .. }) | ErrorKind::Table(TableError::OutOfFuel { .. }) | ErrorKind::Fuel(FuelError::OutOfFuel { .. }) ) }
Returns `true` if the [`Error`] represents an out-of-fuel error.
is_out_of_fuel
rust
wasmi-labs/wasmi
crates/wasmi/src/error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/error.rs
Apache-2.0
pub fn as_trap_code(&self) -> Option<TrapCode> { let trap_code = match self { | Self::TrapCode(trap_code) => *trap_code, | Self::Fuel(FuelError::OutOfFuel { .. }) | Self::Table(TableError::OutOfFuel { .. }) | Self::Memory(MemoryError::OutOfFuel { .. }) => TrapCode::OutOfFuel, | Self::Memory(MemoryError::OutOfBoundsAccess) | Self::Memory(MemoryError::OutOfBoundsGrowth) => TrapCode::MemoryOutOfBounds, | Self::Table(TableError::ElementTypeMismatch) => TrapCode::BadSignature, | Self::Table(TableError::SetOutOfBounds) | Self::Table(TableError::FillOutOfBounds) | Self::Table(TableError::GrowOutOfBounds) | Self::Table(TableError::InitOutOfBounds) => TrapCode::TableOutOfBounds, | Self::ResumableOutOfFuel(_) => TrapCode::OutOfFuel, _ => return None, }; Some(trap_code) }
Returns a reference to [`TrapCode`] if [`ErrorKind`] is a [`TrapCode`].
as_trap_code
rust
wasmi-labs/wasmi
crates/wasmi/src/error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/error.rs
Apache-2.0
pub fn as_i32_exit_status(&self) -> Option<i32> { match self { Self::I32ExitStatus(exit_status) => Some(*exit_status), _ => None, } }
Returns a [`i32`] if [`ErrorKind`] is an [`ErrorKind::I32ExitStatus`].
as_i32_exit_status
rust
wasmi-labs/wasmi
crates/wasmi/src/error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/error.rs
Apache-2.0
pub fn as_host(&self) -> Option<&dyn HostError> { match self { Self::Host(error) => Some(error.as_ref()), _ => None, } }
Returns a dynamic reference to [`HostError`] if [`ErrorKind`] is a [`HostError`].
as_host
rust
wasmi-labs/wasmi
crates/wasmi/src/error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/error.rs
Apache-2.0
pub fn as_host_mut(&mut self) -> Option<&mut dyn HostError> { match self { Self::Host(error) => Some(error.as_mut()), _ => None, } }
Returns a dynamic reference to [`HostError`] if [`ErrorKind`] is a [`HostError`].
as_host_mut
rust
wasmi-labs/wasmi
crates/wasmi/src/error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/error.rs
Apache-2.0
pub fn into_host(self) -> Option<Box<dyn HostError>> { match self { Self::Host(error) => Some(error), _ => None, } }
Returns the [`HostError`] if [`ErrorKind`] is a [`HostError`].
into_host
rust
wasmi-labs/wasmi
crates/wasmi/src/error.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/error.rs
Apache-2.0
pub fn new<T>(object: T) -> Self where T: 'static + Any + Send + Sync, { Self { inner: Box::new(object), } }
Creates a new instance of `ExternRef` wrapping the given value.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/externref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/externref.rs
Apache-2.0
pub fn new<T>(mut ctx: impl AsContextMut, object: T) -> Self where T: 'static + Any + Send + Sync, { ctx.as_context_mut() .store .inner .alloc_extern_object(ExternObjectEntity::new(object)) }
Creates a new instance of `ExternRef` wrapping the given value.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/externref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/externref.rs
Apache-2.0
pub fn new<T>(ctx: impl AsContextMut, object: impl Into<Option<T>>) -> Self where T: 'static + Any + Send + Sync, { object .into() .map(|object| ExternObject::new(ctx, object)) .map(Self::from_object) .unwrap_or_else(Self::null) }
Creates a new [`ExternRef`] wrapping the given value.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/externref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/externref.rs
Apache-2.0
fn from_object(object: ExternObject) -> Self { Self { inner: Some(object), } }
Creates a new [`ExternRef`] to the given [`ExternObject`].
from_object
rust
wasmi-labs/wasmi
crates/wasmi/src/externref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/externref.rs
Apache-2.0
pub fn null() -> Self { Self { inner: None } }
Creates a new [`ExternRef`] which is `null`.
null
rust
wasmi-labs/wasmi
crates/wasmi/src/externref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/externref.rs
Apache-2.0
pub fn data<'a, T: 'a>(&self, ctx: impl Into<StoreContext<'a, T>>) -> Option<&'a dyn Any> { self.inner.map(|object| object.data(ctx)) }
Returns a shared reference to the underlying data for this [`ExternRef`]. # Panics Panics if `ctx` does not own this [`ExternRef`].
data
rust
wasmi-labs/wasmi
crates/wasmi/src/externref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/externref.rs
Apache-2.0
pub fn new(mut ctx: impl AsContextMut, initial_value: Val, mutability: Mutability) -> Self { ctx.as_context_mut() .store .inner .alloc_global(CoreGlobal::new(initial_value.into(), mutability)) }
Creates a new global variable to the store.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/global.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/global.rs
Apache-2.0
pub fn set(&self, mut ctx: impl AsContextMut, new_value: Val) -> Result<(), GlobalError> { ctx.as_context_mut() .store .inner .resolve_global_mut(self) .set(new_value.into()) }
Sets a new value to the global variable. # Errors - If the global variable is immutable. - If there is a type mismatch between the global variable and the new value. # Panics Panics if `ctx` does not own this [`Global`].
set
rust
wasmi-labs/wasmi
crates/wasmi/src/global.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/global.rs
Apache-2.0
pub fn get(&self, ctx: impl AsContext) -> Val { ctx.as_context() .store .inner .resolve_global(self) .get() .into() }
Returns the current value of the global variable. # Panics Panics if `ctx` does not own this [`Global`].
get
rust
wasmi-labs/wasmi
crates/wasmi/src/global.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/global.rs
Apache-2.0
pub fn trap_on_grow_failure(mut self, trap: bool) -> Self { self.0.trap_on_grow_failure = trap; self }
Indicates that a trap should be raised whenever a growth operation would fail. This operation will force `memory.grow` and `table.grow` instructions to raise a trap on failure instead of returning -1. This is not necessarily spec-compliant, but it can be quite handy when debugging a module that fails to allocate memory and might behave oddly as a result. This value defaults to `false`.
trap_on_grow_failure
rust
wasmi-labs/wasmi
crates/wasmi/src/limits.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/limits.rs
Apache-2.0
fn missing_definition(import: &ImportType) -> Self { Self::MissingDefinition { name: import.import_name().clone(), ty: import.ty().clone(), } }
Creates a new [`LinkerError`] for when an imported definition was not found.
missing_definition
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
fn invalid_type_definition(import: &ImportType, found: &ExternType) -> Self { Self::InvalidTypeDefinition { name: import.import_name().clone(), expected: import.ty().clone(), found: found.clone(), } }
Creates a new [`LinkerError`] for when an imported definition has an invalid type.
invalid_type_definition
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
fn as_extern(&self) -> Option<&Extern> { match self { Definition::Extern(item) => Some(item), Definition::HostFunc(_) => None, } }
Returns the [`Extern`] item if this [`Definition`] is [`Definition::Extern`]. Otherwise returns `None`.
as_extern
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn ty(&self, ctx: impl AsContext) -> ExternType { match self { Definition::Extern(item) => item.ty(ctx), Definition::HostFunc(host_func) => ExternType::Func(host_func.func_type().clone()), } }
Returns the [`ExternType`] of the [`Definition`].
ty
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn as_func(&self, mut ctx: impl AsContextMut<Data = T>) -> Option<Func> { match self { Definition::Extern(Extern::Func(func)) => Some(*func), Definition::HostFunc(host_func) => { let trampoline = ctx .as_context_mut() .store .alloc_trampoline(host_func.trampoline().clone()); let ty = host_func.func_type(); let entity = HostFuncEntity::new(ctx.as_context().engine(), ty, trampoline); let func = ctx .as_context_mut() .store .inner .alloc_func(FuncEntity::Host(entity)); Some(func) } _ => None, } }
Returns the [`Func`] of the [`Definition`] if it is a function. Returns `None` otherwise. # Note - This allocates a new [`Func`] on the `ctx` if it is a [`Linker`] defined host function. - This unifies handling of [`Definition::Extern(Extern::Func)`] and [`Definition::HostFunc`].
as_func
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn build() -> LinkerBuilder<state::Constructing, T> { LinkerBuilder { inner: Arc::new(LinkerInner::default()), marker: PhantomData, } }
Creates a new [`LinkerBuilder`] to construct a [`Linker`].
build
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn allow_shadowing(&mut self, allow: bool) -> &mut Self { self.inner.allow_shadowing(allow); self }
Configures whether this [`Linker`] allows to shadow previous definitions with the same name. Disabled by default.
allow_shadowing
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
fn ensure_undefined(&self, module: &str, name: &str) -> Result<(), LinkerError> { if let Some(shared) = &self.shared { if shared.has_definition(module, name) { return Err(LinkerError::DuplicateDefinition { import_name: ImportName::new(module, name), }); } } Ok(()) }
Ensures that the `name` in `module` is undefined in the shared definitions. Returns `Ok` if no shared definition exists. # Errors If there exists a shared definition for `name` in `module`.
ensure_undefined
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn define( &mut self, module: &str, name: &str, item: impl Into<Extern>, ) -> Result<&mut Self, LinkerError> { self.ensure_undefined(module, name)?; let key = self.inner.new_import_key(module, name); self.inner.insert(key, Definition::Extern(item.into()))?; Ok(self) }
Define a new item in this [`Linker`]. # Errors If there already is a definition under the same name for this [`Linker`].
define
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn func_new( &mut self, module: &str, name: &str, ty: FuncType, func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<(), Error> + Send + Sync + 'static, ) -> Result<&mut Self, LinkerError> { self.ensure_undefined(module, name)?; let func = HostFuncTrampolineEntity::new(ty, func); let key = self.inner.new_import_key(module, name); self.inner.insert(key, Definition::HostFunc(func))?; Ok(self) }
Creates a new named [`Func::new`]-style host [`Func`] for this [`Linker`]. For more information see [`Linker::func_wrap`]. # Errors If there already is a definition under the same name for this [`Linker`].
func_new
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn func_wrap<Params, Args>( &mut self, module: &str, name: &str, func: impl IntoFunc<T, Params, Args>, ) -> Result<&mut Self, LinkerError> { self.ensure_undefined(module, name)?; let func = HostFuncTrampolineEntity::wrap(func); let key = self.inner.new_import_key(module, name); self.inner.insert(key, Definition::HostFunc(func))?; Ok(self) }
Creates a new named [`Func::new`]-style host [`Func`] for this [`Linker`]. For information how to use this API see [`Func::wrap`]. This method creates a host function for this [`Linker`] under the given name. It is distinct in its ability to create a [`Store`] independent host function. Host functions defined this way can be used to instantiate instances in multiple different [`Store`] entities. The same applies to other [`Linker`] methods to define new [`Func`] instances such as [`Linker::func_new`]. In a concurrently running program, this means that these host functions could be called concurrently if different [`Store`] entities are executing on different threads. # Errors If there already is a definition under the same name for this [`Linker`]. [`Store`]: crate::Store
func_wrap
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn get( &self, context: impl AsContext<Data = T>, module: &str, name: &str, ) -> Option<Extern> { match self.get_definition(context, module, name) { Some(Definition::Extern(item)) => Some(*item), _ => None, } }
Looks up a defined [`Extern`] by name in this [`Linker`]. - Returns `None` if this name was not previously defined in this [`Linker`]. - Returns `None` if the definition is a [`Linker`] defined host function. # Panics If the [`Engine`] of this [`Linker`] and the [`Engine`] of `context` are not the same.
get
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
fn get_definition( &self, context: impl AsContext<Data = T>, module: &str, name: &str, ) -> Option<&Definition<T>> { assert!(Engine::same( context.as_context().store.engine(), self.engine() )); if let Some(shared) = &self.shared { if let Some(item) = shared.get_definition(module, name) { return Some(item); } } self.inner.get_definition(module, name) }
Looks up a [`Definition`] by name in this [`Linker`]. Returns `None` if this name was not previously defined in this [`Linker`]. # Panics If the [`Engine`] of this [`Linker`] and the [`Engine`] of `context` are not the same.
get_definition
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn instance( &mut self, mut store: impl AsContextMut<Data = T>, module_name: &str, instance: Instance, ) -> Result<&mut Self, Error> { assert!(Engine::same( store.as_context().store.engine(), self.engine() )); let mut store = store.as_context_mut(); for export in instance.exports(&mut store) { let key = self.inner.new_import_key(module_name, export.name()); let def = Definition::Extern(export.into_extern()); self.inner.insert(key, def)?; } Ok(self) }
Convenience wrapper to define an entire [`Instance`]` in this [`Linker`]. This is a convenience wrapper around [`Linker::define`] which defines all exports of the `instance` for `self`. The module name for each export is `module_name` and the field name for each export is the name in the `instance` itself. # Errors - If any item is re-defined in `self` (for example the same `module_name` was already defined). - If `instance` comes from a different [`Store`](crate::Store) than this [`Linker`] originally was created with. # Panics If the [`Engine`] of this [`Linker`] and the [`Engine`] of `store` are not the same.
instance
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn alias_module(&mut self, module: &str, as_module: &str) -> Result<(), Error> { self.inner.alias_module(module, as_module) }
Aliases one module's name as another. This method will alias all currently defined under `module` to also be defined under the name `as_module` too. # Errors Returns an error if any shadowing violations happen while defining new items.
alias_module
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn instantiate( &self, mut context: impl AsContextMut<Data = T>, module: &Module, ) -> Result<InstancePre, Error> { assert!(Engine::same(self.engine(), context.as_context().engine())); // TODO: possibly add further resource limitation here on number of externals. // Not clear that user can't import the same external lots of times to inflate this. let externals = module .imports() .map(|import| self.process_import(&mut context, import)) .collect::<Result<Vec<Extern>, Error>>()?; module.instantiate(context, externals) }
Instantiates the given [`Module`] using the definitions in the [`Linker`]. # Panics If the [`Engine`] of the [`Linker`] and `context` are not the same. # Errors - If the linker does not define imports of the instantiated [`Module`]. - If any imported item does not satisfy its type requirements.
instantiate
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
fn process_import( &self, mut context: impl AsContextMut<Data = T>, import: ImportType, ) -> Result<Extern, Error> { assert!(Engine::same(self.engine(), context.as_context().engine())); let module_name = import.module(); let field_name = import.name(); let resolved = self .get_definition(context.as_context(), module_name, field_name) .ok_or_else(|| LinkerError::missing_definition(&import))?; let invalid_type = |context| LinkerError::invalid_type_definition(&import, &resolved.ty(context)); match import.ty() { ExternType::Func(_expected) => { let func = resolved .as_func(&mut context) .ok_or_else(|| invalid_type(context))?; Ok(Extern::Func(func)) } ExternType::Table(_expected) => { let table = resolved .as_extern() .copied() .and_then(Extern::into_table) .ok_or_else(|| invalid_type(context))?; Ok(Extern::Table(table)) } ExternType::Memory(_expected) => { let memory = resolved .as_extern() .copied() .and_then(Extern::into_memory) .ok_or_else(|| invalid_type(context))?; Ok(Extern::Memory(memory)) } ExternType::Global(_expected) => { let global = resolved .as_extern() .copied() .and_then(Extern::into_global) .ok_or_else(|| invalid_type(context))?; Ok(Extern::Global(global)) } } }
Processes a single [`Module`] import. # Panics If the [`Engine`] of the [`Linker`] and `context` are not the same. # Errors If the imported item does not satisfy constraints set by the [`Module`].
process_import
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn create(&self, engine: &Engine) -> Linker<T> { Linker { engine: engine.clone(), shared: self.inner.clone().into(), inner: <LinkerInner<T>>::default(), } }
Finishes construction of the [`Linker`] by attaching an [`Engine`].
create
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn finish(self) -> LinkerBuilder<state::Ready, T> { LinkerBuilder { inner: self.inner, marker: PhantomData, } }
Signals that the [`LinkerBuilder`] is now ready to create new [`Linker`] instances.
finish
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
fn inner_mut(&mut self) -> &mut LinkerInner<T> { Arc::get_mut(&mut self.inner).unwrap_or_else(|| { unreachable!("tried to define host function in LinkerBuilder after Linker creation") }) }
Returns an exclusive reference to the underlying [`Linker`] internals if no [`Linker`] has been built, yet. # Panics If the [`LinkerBuilder`] has already created a [`Linker`] using [`LinkerBuilder::finish`].
inner_mut
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0
pub fn func_new( &mut self, module: &str, name: &str, ty: FuncType, func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<(), Error> + Send + Sync + 'static, ) -> Result<&mut Self, LinkerError> { self.inner_mut().func_new(module, name, ty, func)?; Ok(self) }
Creates a new named [`Func::new`]-style host [`Func`] for this [`Linker`]. For more information see [`Linker::func_wrap`]. # Errors If there already is a definition under the same name for this [`Linker`]. # Panics If the [`LinkerBuilder`] has already created a [`Linker`] using [`LinkerBuilder::finish`].
func_new
rust
wasmi-labs/wasmi
crates/wasmi/src/linker.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/linker.rs
Apache-2.0