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 const fn binary(ty: WasmType, op: &'static str) -> Self { Self::Binary { ty, op } }
Create a new binary [`WasmOp`] for the given [`ValType`]: `fn(T, T) -> T`
binary
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
pub const fn cmp(ty: WasmType, op: &'static str) -> Self { Self::Cmp { ty, op } }
Create a new compare [`WasmOp`] for the given [`ValType`]: `fn(T, T) -> i32`
cmp
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
pub const fn load(ty: WasmType, op: &'static str) -> Self { Self::Load { ty, op } }
Create a new `load` [`WasmOp`] for the given [`ValType`].
load
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
pub const fn store(ty: WasmType, op: &'static str) -> Self { Self::Store { ty, op } }
Create a new `store` [`WasmOp`] for the given [`ValType`].
store
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
pub fn param_ty(&self) -> WasmType { match self { Self::Binary { ty, op: _ } => *ty, Self::Cmp { ty, op: _ } => *ty, Self::Load { .. } => panic!("load instructions have no parameters"), Self::Store { ty, op: _ } => *ty, } }
Returns the parameter [`ValType`] of the [`WasmOp`].
param_ty
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
pub fn result_ty(&self) -> WasmType { match self { Self::Binary { ty, op: _ } => *ty, Self::Cmp { ty: _, op: _ } => WasmType::I32, Self::Load { ty, op: _ } => *ty, Self::Store { .. } => panic!("store instructions have no results"), } }
Returns the result [`ValType`] of the [`WasmOp`].
result_ty
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
pub fn display_ty(&self) -> WasmType { match self { Self::Binary { .. } => self.param_ty(), Self::Cmp { .. } => self.param_ty(), Self::Load { .. } => self.result_ty(), Self::Store { .. } => self.param_ty(), } }
Returns the display [`ValType`] of the [`WasmOp`].
display_ty
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
pub fn op(&self) -> &'static str { match self { WasmOp::Binary { ty: _, op } => op, WasmOp::Cmp { ty: _, op } => op, WasmOp::Load { ty: _, op } => op, WasmOp::Store { ty: _, op } => op, } }
Returns the operator identifier of the [`WasmOp`].
op
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
fn test_binary_reg_imm16_rhs<T>( wasm_op: WasmOp, value: T, make_instr: fn(result: Reg, lhs: Reg, rhs: Const16<T>) -> Instruction, ) where T: Copy + TryInto<Const16<T>>, DisplayWasm<T>: Display, { let immediate: Const16<T> = value .try_into() .unwrap_or_else(|_| panic!("failed to convert {} to Const16", DisplayWasm::from(value))); let expected = [ make_instr(Reg::from(1), Reg::from(0), immediate), Instruction::return_reg(1), ]; test_binary_reg_imm_with(wasm_op, value, expected).run() }
Variant of [`test_binary_reg_imm16`] where the `rhs` operand is an immediate value.
test_binary_reg_imm16_rhs
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
fn test_binary_reg_imm16_lhs<T>( wasm_op: WasmOp, value: T, make_instr: fn(result: Reg, lhs: Const16<T>, rhs: Reg) -> Instruction, ) where T: Copy + TryInto<Const16<T>>, DisplayWasm<T>: Display, { let immediate: Const16<T> = value .try_into() .unwrap_or_else(|_| panic!("failed to convert {} to Const16", DisplayWasm::from(value))); let expected = [ make_instr(Reg::from(1), immediate, Reg::from(0)), Instruction::return_reg(1), ]; test_binary_reg_imm_lhs_with(wasm_op, value, expected).run() }
Variant of [`test_binary_reg_imm16`] where the `lhs` operand is an immediate value.
test_binary_reg_imm16_lhs
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
fn test_binary_reg_imm32_lhs<T>( wasm_op: WasmOp, value: T, make_instr: fn(result: Reg, lhs: Reg, rhs: Reg) -> Instruction, ) where T: Copy + Into<UntypedVal>, DisplayWasm<T>: Display, { let expected = [ make_instr(Reg::from(1), Reg::from(-1), Reg::from(0)), Instruction::return_reg(1), ]; let mut testcase = testcase_binary_imm_reg(wasm_op, value); testcase.expect_func(ExpectedFunc::new(expected).consts([value.into()])); testcase.run() }
Variant of [`test_binary_reg_imm32`] where both operands are swapped.
test_binary_reg_imm32_lhs
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
fn test_binary_reg_imm32_lhs_commutative<T>( wasm_op: WasmOp, value: T, make_instr: fn(result: Reg, lhs: Reg, rhs: Reg) -> Instruction, ) where T: Copy + Into<UntypedVal>, DisplayWasm<T>: Display, { let expected = [ make_instr(Reg::from(1), Reg::from(0), Reg::from(-1)), Instruction::return_reg(1), ]; let mut testcase = testcase_binary_imm_reg(wasm_op, value); testcase.expect_func(ExpectedFunc::new(expected).consts([value.into()])); testcase.run() }
Variant of [`test_binary_reg_imm32`] where both operands are swapped.
test_binary_reg_imm32_lhs_commutative
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/mod.rs
Apache-2.0
fn test_for_both<T>(if_true: T, if_false: T) where T: WasmTy, DisplayWasm<T>: Display, { test_for::<T>(true, if_true, if_false); test_for::<T>(false, if_true, if_false); }
Run the test for both sign polarities of the `br_if` condition.
test_for_both
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/br_if.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/br_if.rs
Apache-2.0
fn test_for_both<T>(if_true: T, if_false: T) where T: WasmTy + Into<AnyConst32>, DisplayWasm<T>: Display, { test_for::<T>(true, if_true, if_false); test_for::<T>(false, if_true, if_false); }
Run the test for both sign polarities of the `br_if` condition.
test_for_both
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/br_if.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/br_if.rs
Apache-2.0
fn test_for_both(if_true: i64, if_false: i64) { test_for(true, if_true, if_false); test_for(false, if_true, if_false); }
Run the test for both sign polarities of the `br_if` condition.
test_for_both
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/br_if.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/br_if.rs
Apache-2.0
fn test_for_both(if_true: f64, if_false: f64) { test_for(true, if_true, if_false); test_for(false, if_true, if_false); }
Run the test for both sign polarities of the `br_if` condition.
test_for_both
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/br_if.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/br_if.rs
Apache-2.0
fn test_mutable<T>(value: T) where T: WasmTy, DisplayWasm<T>: Display, { let ty = T::NAME; let display_value = DisplayWasm::from(value); let wasm = format!( r#" (module (global $g (mut {ty}) ({ty}.const {display_value})) (func (result {ty}) global.get $g ) ) "# ); TranslationTest::new(&wasm) .expect_func_instrs([ Instruction::global_get(Reg::from(0), Global::from(0)), Instruction::return_reg(Reg::from(0)), ]) .run() }
Test for `global.get` of internally defined mutable global variables. # Note Optimization to replace `global.get` with the underlying initial value of the global variable cannot be done since the value might change during program execution.
test_mutable
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/global_get.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/global_get.rs
Apache-2.0
fn test_immutable<T>(value: T) where T: WasmTy, DisplayWasm<T>: Display, { let ty = T::NAME; let display_value = DisplayWasm::from(value); let wasm = format!( r#" (module (global $g {ty} ({ty}.const {display_value})) (func (result {ty}) global.get $g ) ) "#, ); let mut testcase = TranslationTest::new(&wasm); let instr = <T as WasmTy>::return_imm_instr(&value); match instr { Instruction::ReturnReg { value: register } => { assert!(register.is_const()); testcase.expect_func(ExpectedFunc::new([instr]).consts([value])); } instr => { testcase.expect_func_instrs([instr]); } } testcase.run(); }
Test for `global.get` of internally defined immutable global variables. # Note In this case optimization to replace the `global.get` with the constant value of the global variable can be applied always.
test_immutable
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/global_get.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/global_get.rs
Apache-2.0
fn test_imported<T>() where T: WasmTy, { let ty = T::NAME; let wasm = format!( r#" (module (import "host" "g" (global $g {ty})) (func (result {ty}) global.get $g ) ) "#, ); TranslationTest::new(&wasm) .expect_func_instrs([ Instruction::global_get(Reg::from(0), Global::from(0)), Instruction::return_reg(Reg::from(0)), ]) .run() }
Test for `global.get` of immutable imported global variables. # Note Even though the accessed global variable is immutable no optimization can be applied since its value is unknown due to being imported.
test_imported
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/global_get.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/global_get.rs
Apache-2.0
fn wat(&self) -> &'static str { match self { Self::Memory32 => "i32", Self::Memory64 => "i64", } }
Returns the `.wat` string reprensetation for the [`IndexType`] of a `memory` declaration.
wat
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/mod.rs
Apache-2.0
fn is_default(&self) -> bool { self.0 == 0 }
Returns `true` if [`MemIdx`] refers to the default Wasm memory index.
is_default
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/mod.rs
Apache-2.0
fn instr(&self) -> Option<Instruction> { match self.0 { 0 => None, n => Some(Instruction::memory_index(n)), } }
Returns the `$mem{n}` memory index used by some Wasm memory instructions.
instr
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/mod.rs
Apache-2.0
fn assert_overflowing_ptr_offset(index_ty: IndexType, ptr: u64, offset: u64) { match index_ty { IndexType::Memory32 => { let Ok(ptr32) = u32::try_from(ptr) else { panic!("ptr must be a 32-bit value but found: {ptr}"); }; let Ok(offset32) = u32::try_from(offset) else { panic!("offset must be a 32-bit value but found: {offset}"); }; assert!( ptr32.checked_add(offset32).is_none(), "ptr+offset must overflow in this testcase (32-bit)" ); } IndexType::Memory64 => { assert!( ptr.checked_add(offset).is_none(), "ptr+offset must overflow in this testcase (64-bit)" ); } } }
Asserts that `ptr+offset` overflow either `i32` or `i64` depending on `index_ty`.
assert_overflowing_ptr_offset
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/mod.rs
Apache-2.0
fn effective_address32(ptr: u64, offset: u64) -> Address32 { let Some(addr) = ptr.checked_add(offset) else { panic!("ptr+offset must not overflow in this testcase") }; let Ok(addr) = Address::try_from(addr) else { panic!("ptr+offset must fit in a `usize` for this testcase") }; let Ok(addr32) = Address32::try_from(addr) else { panic!("ptr+offset must fit in a `u32` for this testcase") }; addr32 }
Returns the effective 32-bit address for `ptr+offset`.
effective_address32
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/mod.rs
Apache-2.0
pub fn param_ty(self) -> ValType { match self { CmpOp::I32And | CmpOp::I32Or | CmpOp::I32Xor | CmpOp::I32Eq | CmpOp::I32Ne | CmpOp::I32LtS | CmpOp::I32LtU | CmpOp::I32LeS | CmpOp::I32LeU | CmpOp::I32GtS | CmpOp::I32GtU | CmpOp::I32GeS | CmpOp::I32GeU => ValType::I32, CmpOp::I64And | CmpOp::I64Or | CmpOp::I64Xor | CmpOp::I64Eq | CmpOp::I64Ne | CmpOp::I64LtS | CmpOp::I64LtU | CmpOp::I64LeS | CmpOp::I64LeU | CmpOp::I64GtS | CmpOp::I64GtU | CmpOp::I64GeS | CmpOp::I64GeU => ValType::I64, CmpOp::F32Eq | CmpOp::F32Ne | CmpOp::F32Lt | CmpOp::F32Le | CmpOp::F32Gt | CmpOp::F32Ge => ValType::F32, CmpOp::F64Eq | CmpOp::F64Ne | CmpOp::F64Lt | CmpOp::F64Le | CmpOp::F64Gt | CmpOp::F64Ge => ValType::F64, } }
Returns the Wasm parameter type of the [`CmpOp`].
param_ty
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/mod.rs
Apache-2.0
pub fn result_ty(self) -> ValType { match self { CmpOp::I64And | CmpOp::I64Or | CmpOp::I64Xor => ValType::I64, _ => ValType::I32, } }
Returns the Wasm result type of the [`CmpOp`].
result_ty
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/mod.rs
Apache-2.0
pub fn op_str(self) -> &'static str { match self { CmpOp::I32And => "and", CmpOp::I32Or => "or", CmpOp::I32Xor => "xor", CmpOp::I32Eq => "eq", CmpOp::I32Ne => "ne", CmpOp::I32LtS => "lt_s", CmpOp::I32LtU => "lt_u", CmpOp::I32LeS => "le_s", CmpOp::I32LeU => "le_u", CmpOp::I32GtS => "gt_s", CmpOp::I32GtU => "gt_u", CmpOp::I32GeS => "ge_s", CmpOp::I32GeU => "ge_u", CmpOp::I64And => "and", CmpOp::I64Or => "or", CmpOp::I64Xor => "xor", CmpOp::I64Eq => "eq", CmpOp::I64Ne => "ne", CmpOp::I64LtS => "lt_s", CmpOp::I64LtU => "lt_u", CmpOp::I64LeS => "le_s", CmpOp::I64LeU => "le_u", CmpOp::I64GtS => "gt_s", CmpOp::I64GtU => "gt_u", CmpOp::I64GeS => "ge_s", CmpOp::I64GeU => "ge_u", CmpOp::F32Eq => "eq", CmpOp::F32Ne => "ne", CmpOp::F32Lt => "lt", CmpOp::F32Le => "le", CmpOp::F32Gt => "gt", CmpOp::F32Ge => "ge", CmpOp::F64Eq => "eq", CmpOp::F64Ne => "ne", CmpOp::F64Lt => "lt", CmpOp::F64Le => "le", CmpOp::F64Gt => "gt", CmpOp::F64Ge => "ge", } }
Returns a string representation of the Wasm operator without type annotation.
op_str
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/mod.rs
Apache-2.0
pub fn wat(self) -> &'static str { match self { MulWideOp::MulWideS => "i64.mul_wide_s", MulWideOp::MulWideU => "i64.mul_wide_u", } }
Returns the `.wat` formatted instruction name.
wat
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/wide_arithmetic.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/wide_arithmetic.rs
Apache-2.0
pub fn eval(self, lhs: i64, rhs: i64) -> (i64, i64) { match self { MulWideOp::MulWideS => wasm::i64_mul_wide_s(lhs, rhs), MulWideOp::MulWideU => wasm::i64_mul_wide_u(lhs, rhs), } }
Evaluates the inputs for the selected instruction.
eval
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/wide_arithmetic.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/wide_arithmetic.rs
Apache-2.0
fn return_f64imm32_instr(value: f64) -> Instruction { let const32 = <Const32<f64>>::try_from(value).expect("value must be 32-bit encodable"); Instruction::return_f64imm32(const32) }
Creates an [`Instruction::ReturnF64Imm32`] from the given `f64` value. # Panics If the `value` cannot be converted into `f32` losslessly.
return_f64imm32_instr
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/binary/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/binary/mod.rs
Apache-2.0
fn conversion_reg_with<I, O, E>(wasm_op: &str, expected: E) where I: WasmTy, O: WasmTy, E: IntoIterator<Item = Instruction>, { let param_ty = <I as WasmTy>::NAME; let result_ty = <O as WasmTy>::NAME; let wasm = format!( r#" (module (func (param {param_ty}) (result {result_ty}) local.get 0 {result_ty}.{wasm_op} ) ) "#, ); TranslationTest::new(&wasm) .expect_func_instrs(expected) .run(); }
Asserts that the unary Wasm operator `wasm_op` translates properly to a unary Wasmi instruction.
conversion_reg_with
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
Apache-2.0
fn conversion_reg<I, O>(wasm_op: &str, make_instr: fn(result: Reg, input: Reg) -> Instruction) where I: WasmTy, O: WasmTy, { let expected = [ make_instr(Reg::from(1), Reg::from(0)), Instruction::return_reg(1), ]; conversion_reg_with::<I, O, _>(wasm_op, expected) }
Asserts that the unary Wasm operator `wasm_op` translates properly to a unary Wasmi instruction.
conversion_reg
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
Apache-2.0
fn unary_reg<T>(wasm_op: &str, make_instr: fn(result: Reg, input: Reg) -> Instruction) where T: WasmTy, { conversion_reg::<T, T>(wasm_op, make_instr) }
Asserts that the unary Wasm operator `wasm_op` translates properly to a unary Wasmi instruction.
unary_reg
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
Apache-2.0
fn conversion_imm<I, O>(wasm_op: &str, input: I, eval: fn(input: I) -> O) where I: WasmTy, O: WasmTy, DisplayWasm<I>: Display, { let param_ty = <I as WasmTy>::NAME; let result_ty = <O as WasmTy>::NAME; let wasm_input = DisplayWasm::from(input); let wasm = format!( r#" (module (func (result {result_ty}) {param_ty}.const {wasm_input} {result_ty}.{wasm_op} ) ) "#, ); let result = eval(input); let instr = <O as WasmTy>::return_imm_instr(&result); let mut testcase = TranslationTest::new(&wasm); if let Instruction::ReturnReg { value } = &instr { assert!(value.is_const()); testcase.expect_func(ExpectedFunc::new([instr]).consts([result])); } else { testcase.expect_func_instrs([instr]); } testcase.run(); }
Asserts that the unary Wasm operator `wasm_op` translates properly to a unary Wasmi instruction.
conversion_imm
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
Apache-2.0
fn unary_imm<T>(wasm_op: &str, input: T, eval: fn(input: T) -> T) where T: WasmTy, DisplayWasm<T>: Display, { conversion_imm::<T, T>(wasm_op, input, eval) }
Asserts that the unary Wasm operator `wasm_op` translates properly to a unary Wasmi instruction.
unary_imm
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
Apache-2.0
fn fallible_conversion_imm_err<I, O>(wasm_op: &str, input: I, eval: fn(input: I) -> TrapCode) where I: WasmTy, O: WasmTy, DisplayWasm<I>: Display, { let param_ty = <I as WasmTy>::NAME; let result_ty = <O as WasmTy>::NAME; let wasm_input = DisplayWasm::from(input); let wasm = format!( r#" (module (func (result {result_ty}) {param_ty}.const {wasm_input} {result_ty}.{wasm_op} ) ) "#, ); let trap_code = eval(input); TranslationTest::new(&wasm) .expect_func_instrs([Instruction::trap(trap_code)]) .run(); }
Asserts that the unary Wasm operator `wasm_op` translates properly to a unary Wasmi instruction.
fallible_conversion_imm_err
rust
wasmi-labs/wasmi
crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/engine/translator/tests/op/unary/mod.rs
Apache-2.0
pub fn new(nullable_func: impl Into<Option<Func>>) -> Self { Self { inner: nullable_func.into(), } }
Creates a new [`FuncRef`]. # Examples ```rust # use wasmi::{Func, FuncRef, Store, Engine}; # let engine = Engine::default(); # let mut store = <Store<()>>::new(&engine, ()); assert!(FuncRef::new(None).is_null()); assert!(FuncRef::new(Func::wrap(&mut store, |x: i32| x)).func().is_some()); ```
new
rust
wasmi-labs/wasmi
crates/wasmi/src/func/funcref.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/funcref.rs
Apache-2.0
fn new(results: &'a mut [UntypedVal]) -> Self { Self { results } }
Create new [`FuncResults`] from the given `results` slice.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/func/func_inout.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/func_inout.rs
Apache-2.0
pub fn encode_results<T>(self, values: T) -> FuncFinished where T: EncodeUntypedSlice, { UntypedVal::encode_slice::<T>(self.results, values) .unwrap_or_else(|error| panic!("encountered unexpected invalid tuple length: {error}")); FuncFinished {} }
Encodes the results of the host function invocation as `T`. # Panics If the number of results dictated by `T` does not match the expected amount.
encode_results
rust
wasmi-labs/wasmi
crates/wasmi/src/func/func_inout.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/func_inout.rs
Apache-2.0
pub fn encode_results_from_slice(self, values: &[Val]) -> Result<FuncFinished, UntypedError> { assert_eq!(self.results.len(), values.len()); self.results.iter_mut().zip(values).for_each(|(dst, src)| { *dst = src.clone().into(); }); Ok(FuncFinished {}) }
Encodes the results of the host function invocation given the `values` slice. # Panics If the number of expected results does not match the length of `values`.
encode_results_from_slice
rust
wasmi-labs/wasmi
crates/wasmi/src/func/func_inout.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/func_inout.rs
Apache-2.0
pub fn new( params_results: &'a mut [UntypedVal], len_params: usize, len_results: usize, ) -> Self { assert_eq!(params_results.len(), cmp::max(len_params, len_results)); Self { params_results, len_params, len_results, } }
Create new [`FuncInOut`]. # Panics If the length of hte `params_results` slice does not match the maximum of the `len_params` and `Len_results`.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/func/func_inout.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/func_inout.rs
Apache-2.0
pub fn decode_params<T>(self) -> (T, FuncResults<'a>) where T: DecodeUntypedSlice, { let decoded = UntypedVal::decode_slice::<T>(self.params()) .unwrap_or_else(|error| panic!("encountered unexpected invalid tuple length: {error}")); let results = self.into_func_results(); (decoded, results) }
Decodes and returns the executed host function parameters as `T`. # Panics If the number of function parameters dictated by `T` does not match.
decode_params
rust
wasmi-labs/wasmi
crates/wasmi/src/func/func_inout.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/func_inout.rs
Apache-2.0
pub fn decode_params_into_slice( self, values: &mut [Val], ) -> Result<FuncResults<'a>, UntypedError> { assert_eq!(self.params().len(), values.len()); self.params().iter().zip(values).for_each(|(src, dst)| { *dst = src.with_type(dst.ty()); }); let results = self.into_func_results(); Ok(results) }
Decodes and stores the executed host functions parameters into `values`. # Panics If the number of host function parameters and items in `values` does not match.
decode_params_into_slice
rust
wasmi-labs/wasmi
crates/wasmi/src/func/func_inout.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/func_inout.rs
Apache-2.0
fn into_func_results(self) -> FuncResults<'a> { FuncResults::new(&mut self.params_results[..self.len_results]) }
Consumes `self` to return the [`FuncResults`] out of it.
into_func_results
rust
wasmi-labs/wasmi
crates/wasmi/src/func/func_inout.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/func_inout.rs
Apache-2.0
pub fn ty_dedup(&self) -> &DedupFuncType { match self { Self::Wasm(func) => func.ty_dedup(), Self::Host(func) => func.ty_dedup(), } }
Returns the signature of the Wasm function.
ty_dedup
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn new(signature: DedupFuncType, body: EngineFunc, instance: Instance) -> Self { Self { ty: signature, body, instance, } }
Creates a new Wasm function from the given raw parts.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn new( // engine: &Engine, ty: FuncType, func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<(), Error> + Send + Sync + 'static, ) -> Self { // Preprocess parameters and results buffers so that we can reuse those // computations within the closure implementation. We put both parameters // and results into a single buffer which we can split to minimize the // amount of allocations per trampoline invocation. let params_iter = ty.params().iter().copied().map(Val::default); let results_iter = ty.results().iter().copied().map(Val::default); let len_params = ty.params().len(); let params_results: Box<[Val]> = params_iter.chain(results_iter).collect(); let trampoline = <TrampolineEntity<T>>::new(move |caller, args| { // We are required to clone the buffer because we are operating within a `Fn`. // This way the trampoline closure only has to own a single slice buffer. // Note: An alternative solution is to use interior mutability but that solution // comes with its own downsides. let mut params_results = params_results.clone(); let (params, results) = params_results.split_at_mut(len_params); let func_results = args.decode_params_into_slice(params).unwrap(); func(caller, params, results)?; Ok(func_results.encode_results_from_slice(results).unwrap()) }); Self { ty, trampoline } }
Creates a new host function trampoline from the given dynamically typed closure.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn wrap<Params, Results>(func: impl IntoFunc<T, Params, Results>) -> Self { let (ty, trampoline) = func.into_func(); // let ty = engine.alloc_func_type(signature); Self { ty, trampoline } }
Creates a new host function trampoline from the given statically typed closure.
wrap
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn new<F>(trampoline: F) -> Self where F: Fn(Caller<T>, FuncInOut) -> Result<FuncFinished, Error> + Send + Sync + 'static, { Self { closure: Arc::new(trampoline), } }
Creates a new [`TrampolineEntity`] from the given host function.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn call( &self, mut ctx: impl AsContextMut<Data = T>, instance: Option<&Instance>, params: FuncInOut, ) -> Result<FuncFinished, Error> { let caller = <Caller<T>>::new(&mut ctx, instance); (self.closure)(caller, params) }
Calls the host function trampoline with the given inputs. The result is written back into the `outputs` buffer.
call
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn new<T>( mut ctx: impl AsContextMut<Data = T>, ty: FuncType, func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<(), Error> + Send + Sync + 'static, ) -> Self { let host_func = HostFuncTrampolineEntity::new(ty.clone(), func); let trampoline = host_func.trampoline().clone(); let func = ctx.as_context_mut().store.alloc_trampoline(trampoline); let host_func = HostFuncEntity::new(ctx.as_context().engine(), &ty, func); ctx.as_context_mut() .store .inner .alloc_func(host_func.into()) }
Creates a new [`Func`] with the given arguments. This is typically used to create a host-defined function to pass as an import to a Wasm module. - `ty`: The signature that the given closure adheres to, used to indicate what the inputs and outputs are. - `func`: The native code invoked whenever this Func will be called. The closure is provided a [`Caller`] as its first argument which allows it to query information about the [`Instance`] that is associated to the call. # Note - The given [`FuncType`] `ty` must match the parameters and results otherwise the resulting host [`Func`] might trap during execution. - It is the responsibility of the caller of [`Func::new`] to guarantee that the correct amount and types of results are written into the results buffer from the `func` closure. If an incorrect amount of results or types of results is written into the buffer then the remaining computation may fail in unexpected ways. This footgun can be avoided by using the typed [`Func::wrap`] method instead. - Prefer using [`Func::wrap`] over this method if possible since [`Func`] instances created using this constructor have runtime overhead for every invocation that can be avoided by using [`Func::wrap`].
new
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn wrap<T, Params, Results>( mut ctx: impl AsContextMut<Data = T>, func: impl IntoFunc<T, Params, Results>, ) -> Self { let host_func = HostFuncTrampolineEntity::wrap(func); let ty = host_func.func_type(); let trampoline = host_func.trampoline().clone(); let func = ctx.as_context_mut().store.alloc_trampoline(trampoline); let host_func = HostFuncEntity::new(ctx.as_context().engine(), ty, func); ctx.as_context_mut() .store .inner .alloc_func(host_func.into()) }
Creates a new host function from the given closure.
wrap
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub(crate) fn ty_dedup<'a, T: 'a>( &self, ctx: impl Into<StoreContext<'a, T>>, ) -> &'a DedupFuncType { ctx.into().store.inner.resolve_func(self).ty_dedup() }
Returns the signature of the function.
ty_dedup
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn ty(&self, ctx: impl AsContext) -> FuncType { ctx.as_context() .store .inner .resolve_func_type(self.ty_dedup(&ctx)) }
Returns the function type of the [`Func`].
ty
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn call<T>( &self, mut ctx: impl AsContextMut<Data = T>, inputs: &[Val], outputs: &mut [Val], ) -> Result<(), Error> { self.verify_and_prepare_inputs_outputs(ctx.as_context(), inputs, outputs)?; // Note: Cloning an [`Engine`] is intentionally a cheap operation. ctx.as_context().store.engine().clone().execute_func( ctx.as_context_mut(), self, inputs, outputs, )?; Ok(()) }
Calls the Wasm or host function with the given inputs. The result is written back into the `outputs` buffer. # Errors - If the function returned a [`Error`]. - If the types of the `inputs` do not match the expected types for the function signature of `self`. - If the number of input values does not match the expected number of inputs required by the function signature of `self`. - If the number of output values does not match the expected number of outputs required by the function signature of `self`.
call
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn call_resumable<T>( &self, mut ctx: impl AsContextMut<Data = T>, inputs: &[Val], outputs: &mut [Val], ) -> Result<ResumableCall, Error> { self.verify_and_prepare_inputs_outputs(ctx.as_context(), inputs, outputs)?; // Note: Cloning an [`Engine`] is intentionally a cheap operation. ctx.as_context() .store .engine() .clone() .execute_func_resumable(ctx.as_context_mut(), self, inputs, outputs) .map(ResumableCall::new) }
Calls the Wasm or host function with the given inputs. The result is written back into the `outputs` buffer. Returns a resumable handle to the function invocation upon encountering host errors with which it is possible to handle the error and continue the execution as if no error occurred. # Note This is a non-standard WebAssembly API and might not be available at other WebAssembly engines. Please be aware that depending on this feature might mean a lock-in to Wasmi for users. # Errors - If the function returned a Wasm [`Error`]. - If the types of the `inputs` do not match the expected types for the function signature of `self`. - If the number of input values does not match the expected number of inputs required by the function signature of `self`. - If the number of output values does not match the expected number of outputs required by the function signature of `self`.
call_resumable
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
fn verify_and_prepare_inputs_outputs( &self, ctx: impl AsContext, inputs: &[Val], outputs: &mut [Val], ) -> Result<(), FuncError> { let fn_type = self.ty_dedup(ctx.as_context()); ctx.as_context() .store .inner .resolve_func_type_with(fn_type, |func_type| { func_type.match_params(inputs)?; func_type.prepare_outputs(outputs)?; Ok(()) }) }
Verify that the `inputs` and `outputs` value types match the function signature. Since [`Func`] is a dynamically typed function instance there is a need to verify that the given input parameters match the required types and that the given output slice matches the expected length. These checks can be avoided using the [`TypedFunc`] API. # Errors - If the `inputs` value types do not match the function input types. - If the number of `inputs` do not match the function input types. - If the number of `outputs` do not match the function output types.
verify_and_prepare_inputs_outputs
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn typed<Params, Results>( &self, ctx: impl AsContext, ) -> Result<TypedFunc<Params, Results>, Error> where Params: WasmParams, Results: WasmResults, { TypedFunc::new(ctx, *self) }
Creates a new [`TypedFunc`] from this [`Func`]. # Note This performs static type checks given `Params` as parameter types to [`Func`] and `Results` as result types of [`Func`] so that those type checks can be avoided when calling the created [`TypedFunc`]. # Errors If the function signature of `self` does not match `Params` and `Results` as parameter types and result types respectively.
typed
rust
wasmi-labs/wasmi
crates/wasmi/src/func/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/mod.rs
Apache-2.0
pub fn new<P, R>(params: P, results: R) -> Self where P: IntoIterator, R: IntoIterator, <P as IntoIterator>::IntoIter: Iterator<Item = ValType> + ExactSizeIterator, <R as IntoIterator>::IntoIter: Iterator<Item = ValType> + ExactSizeIterator, { let core = match CoreFuncType::new(params, results) { Ok(func_type) => func_type, Err(error) => panic!("failed to create function type: {error}"), }; Self { core } }
Creates a new [`FuncType`]. # Errors If an out of bounds number of parameters or results are given.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/func/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/ty.rs
Apache-2.0
pub(crate) fn match_params<T>(&self, params: &[T]) -> Result<(), FuncError> where T: DynamicallyTyped, { if self.params().len() != params.len() { return Err(FuncError::MismatchingParameterLen); } if self .params() .iter() .copied() .ne(params.iter().map(<T as DynamicallyTyped>::ty)) { return Err(FuncError::MismatchingParameterType); } Ok(()) }
Returns `Ok` if the number and types of items in `params` matches as expected by the [`FuncType`]. # Errors - If the number of items in `params` does not match the number of parameters of the function type. - If any type of an item in `params` does not match the expected type of the function type.
match_params
rust
wasmi-labs/wasmi
crates/wasmi/src/func/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/ty.rs
Apache-2.0
pub(crate) fn match_results<T>(&self, results: &[T]) -> Result<(), FuncError> where T: DynamicallyTyped, { if self.results().len() != results.len() { return Err(FuncError::MismatchingResultLen); } if self .results() .iter() .copied() .ne(results.iter().map(<T as DynamicallyTyped>::ty)) { return Err(FuncError::MismatchingResultType); } Ok(()) }
Returns `Ok` if the number and types of items in `results` matches as expected by the [`FuncType`]. # Note Only checks types if `check_type` is set to `true`. # Errors - If the number of items in `results` does not match the number of results of the function type. - If any type of an item in `results` does not match the expected type of the function type.
match_results
rust
wasmi-labs/wasmi
crates/wasmi/src/func/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/ty.rs
Apache-2.0
pub(crate) fn prepare_outputs(&self, outputs: &mut [Val]) -> Result<(), FuncError> { if self.results().len() != outputs.len() { return Err(FuncError::MismatchingResultLen); } for (output, result_ty) in outputs.iter_mut().zip(self.results()) { *output = Val::default(*result_ty); } Ok(()) }
Initializes the values in `outputs` to match the types expected by the [`FuncType`]. # Note This is required by an implementation detail of how function result passing is current implemented in the Wasmi execution engine and might change in the future. # Errors If the number of items in `outputs` does not match the number of results of the [`FuncType`].
prepare_outputs
rust
wasmi-labs/wasmi
crates/wasmi/src/func/ty.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/ty.rs
Apache-2.0
pub(crate) fn new(ctx: impl AsContext, func: Func) -> Result<Self, Error> { let func_type = func.ty(&ctx); let (actual_params, actual_results) = ( <Params as WasmTyList>::types(), <Results as WasmTyList>::types(), ); func_type.match_params(actual_params.as_ref())?; func_type.match_results(actual_results.as_ref())?; Ok(Self { signature: PhantomData, func, }) }
Creates a new [`TypedFunc`] for the given [`Func`] using the static typing. # Errors If the provided static types `Params` and `Results` for the parameters and result types of `func` mismatch the signature of `func`.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/func/typed_func.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/typed_func.rs
Apache-2.0
pub fn call(&self, mut ctx: impl AsContextMut, params: Params) -> Result<Results, Error> { // Note: Cloning an [`Engine`] is intentionally a cheap operation. ctx.as_context().store.engine().clone().execute_func( ctx.as_context_mut(), &self.func, params, <CallResultsTuple<Results>>::default(), ) }
Calls this Wasm or host function with the specified parameters. Returns either the results of the call, or a [`Error`] if one happened. For more information, see the [`Func::typed`] and [`Func::call`] documentation. # Panics Panics if `ctx` does not own this [`TypedFunc`]. # Errors If the execution of the called Wasm function traps.
call
rust
wasmi-labs/wasmi
crates/wasmi/src/func/typed_func.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/typed_func.rs
Apache-2.0
pub fn call_resumable( &self, mut ctx: impl AsContextMut, params: Params, ) -> Result<TypedResumableCall<Results>, Error> { // Note: Cloning an [`Engine`] is intentionally a cheap operation. ctx.as_context() .store .engine() .clone() .execute_func_resumable( ctx.as_context_mut(), &self.func, params, <CallResultsTuple<Results>>::default(), ) .map(TypedResumableCall::new) }
Calls this Wasm or host function with the specified parameters. Returns a resumable handle to the function invocation upon encountering host errors with which it is possible to handle the error and continue the execution as if no error occurred. # Note This is a non-standard WebAssembly API and might not be available at other WebAssembly engines. Please be aware that depending on this feature might mean a lock-in to Wasmi for users. # Errors If the function returned a [`Error`] originating from WebAssembly.
call_resumable
rust
wasmi-labs/wasmi
crates/wasmi/src/func/typed_func.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/func/typed_func.rs
Apache-2.0
pub fn new(module: &Module) -> Self { fn vec_with_capacity_exact<T>(capacity: usize) -> Vec<T> { let mut v = Vec::new(); v.reserve_exact(capacity); v } let mut len_funcs = module.len_funcs(); let mut len_globals = module.len_globals(); let mut len_tables = module.len_tables(); let mut len_memories = module.len_memories(); for import in module.imports() { match import.ty() { ExternType::Func(_) => { len_funcs += 1; } ExternType::Table(_) => { len_tables += 1; } ExternType::Memory(_) => { len_memories += 1; } ExternType::Global(_) => { len_globals += 1; } } } Self { func_types: module.func_types_cloned(), tables: vec_with_capacity_exact(len_tables), funcs: vec_with_capacity_exact(len_funcs), memories: vec_with_capacity_exact(len_memories), globals: vec_with_capacity_exact(len_globals), start_fn: None, exports: Map::default(), data_segments: Vec::new(), elem_segments: Vec::new(), } }
Creates a new [`InstanceEntityBuilder`] optimized for the [`Module`].
new
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/builder.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/builder.rs
Apache-2.0
pub fn set_start(&mut self, start_fn: FuncIdx) { match &mut self.start_fn { Some(index) => panic!("already set start function {index:?}"), None => { self.start_fn = Some(start_fn); } } }
Sets the start function of the built instance. # Panics If the start function has already been set.
set_start
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/builder.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/builder.rs
Apache-2.0
pub fn get_memory(&self, index: u32) -> Memory { self.memories .get(index as usize) .copied() .unwrap_or_else(|| panic!("missing `Memory` at index: {index}")) }
Returns the [`Memory`] at the `index`. # Panics If there is no [`Memory`] at the given `index.
get_memory
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/builder.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/builder.rs
Apache-2.0
pub fn get_table(&self, index: u32) -> Table { self.tables .get(index as usize) .copied() .unwrap_or_else(|| panic!("missing `Table` at index: {index}")) }
Returns the [`Table`] at the `index`. # Panics If there is no [`Table`] at the given `index.
get_table
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/builder.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/builder.rs
Apache-2.0
pub fn get_global(&self, index: u32) -> Global { self.globals .get(index as usize) .copied() .unwrap_or_else(|| panic!("missing `Global` at index: {index}")) }
Returns the [`Global`] at the `index`. # Panics If there is no [`Global`] at the given `index.
get_global
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/builder.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/builder.rs
Apache-2.0
pub fn get_func(&self, index: u32) -> Func { self.funcs .get(index as usize) .copied() .unwrap_or_else(|| panic!("missing `Func` at index: {index}")) }
Returns the function at the `index`. # Panics If there is no function at the given `index.
get_func
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/builder.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/builder.rs
Apache-2.0
pub fn push_export(&mut self, name: &str, new_value: Extern) { if let Some(old_value) = self.exports.get(name) { panic!( "tried to register {new_value:?} for name {name} \ but name is already used by {old_value:?}", ) } self.exports.insert(name.into(), new_value); }
Pushes a new [`Extern`] under the given `name` to the [`InstanceEntity`] under construction. # Panics If the name has already been used by an already pushed [`Extern`].
push_export
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/builder.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/builder.rs
Apache-2.0
pub fn into_global(self) -> Option<Global> { if let Self::Global(global) = self { return Some(global); } None }
Returns the underlying global variable if `self` is a global variable. Returns `None` otherwise.
into_global
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub fn into_table(self) -> Option<Table> { if let Self::Table(table) = self { return Some(table); } None }
Returns the underlying table if `self` is a table. Returns `None` otherwise.
into_table
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub fn into_memory(self) -> Option<Memory> { if let Self::Memory(memory) = self { return Some(memory); } None }
Returns the underlying linear memory if `self` is a linear memory. Returns `None` otherwise.
into_memory
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub fn into_func(self) -> Option<Func> { if let Self::Func(func) = self { return Some(func); } None }
Returns the underlying function if `self` is a function. Returns `None` otherwise.
into_func
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub fn ty(&self, ctx: impl AsContext) -> ExternType { match self { Extern::Global(global) => global.ty(ctx).into(), Extern::Table(table) => table.ty(ctx).into(), Extern::Memory(memory) => memory.ty(ctx).into(), Extern::Func(func) => func.ty(ctx).into(), } }
Returns the type associated with this [`Extern`]. # Panics If this item does not belong to the `store` provided.
ty
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub fn global(&self) -> Option<&GlobalType> { match self { Self::Global(ty) => Some(ty), _ => None, } }
Returns the underlying [`GlobalType`] or `None` if it is of a different type.
global
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub fn table(&self) -> Option<&TableType> { match self { Self::Table(ty) => Some(ty), _ => None, } }
Returns the underlying [`TableType`] or `None` if it is of a different type.
table
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub fn memory(&self) -> Option<&MemoryType> { match self { Self::Memory(ty) => Some(ty), _ => None, } }
Returns the underlying [`MemoryType`] or `None` if it is of a different type.
memory
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub fn func(&self) -> Option<&FuncType> { match self { Self::Func(ty) => Some(ty), _ => None, } }
Returns the underlying [`FuncType`] or `None` if it is of a different type.
func
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub(crate) fn new(name: &'instance str, definition: Extern) -> Export<'instance> { Self { name, definition } }
Creates a new [`Export`] with the given `name` and `definition`.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/exports.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/exports.rs
Apache-2.0
pub fn get_memory(&self, index: u32) -> Option<Memory> { self.memories.get(index as usize).copied() }
Returns the linear memory at the `index` if any.
get_memory
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/mod.rs
Apache-2.0
pub fn get_table(&self, index: u32) -> Option<Table> { self.tables.get(index as usize).copied() }
Returns the table at the `index` if any.
get_table
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/mod.rs
Apache-2.0
pub fn get_global(&self, index: u32) -> Option<Global> { self.globals.get(index as usize).copied() }
Returns the global variable at the `index` if any.
get_global
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/mod.rs
Apache-2.0
pub fn get_func(&self, index: u32) -> Option<Func> { self.funcs.get(index as usize).copied() }
Returns the function at the `index` if any.
get_func
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/mod.rs
Apache-2.0
pub fn get_signature(&self, index: u32) -> Option<&DedupFuncType> { self.func_types.get(index as usize) }
Returns the signature at the `index` if any.
get_signature
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/mod.rs
Apache-2.0
pub fn get_data_segment(&self, index: u32) -> Option<DataSegment> { self.data_segments.get(index as usize).copied() }
Returns the [`DataSegment`] at the `index` if any.
get_data_segment
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/mod.rs
Apache-2.0
pub fn get_element_segment(&self, index: u32) -> Option<ElementSegment> { self.elem_segments.get(index as usize).copied() }
Returns the [`ElementSegment`] at the `index` if any.
get_element_segment
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/mod.rs
Apache-2.0
pub(crate) fn get_func_by_index(&self, store: impl AsContext, index: u32) -> Option<Func> { store .as_context() .store .inner .resolve_instance(self) .get_func(index) }
Returns the function at the `index` if any. # Panics Panics if `store` does not own this [`Instance`].
get_func_by_index
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/mod.rs
Apache-2.0
pub fn get_export(&self, store: impl AsContext, name: &str) -> Option<Extern> { store .as_context() .store .inner .resolve_instance(self) .get_export(name) }
Returns the value exported to the given `name` if any. # Panics Panics if `store` does not own this [`Instance`].
get_export
rust
wasmi-labs/wasmi
crates/wasmi/src/instance/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/instance/mod.rs
Apache-2.0
pub fn new_active(mut ctx: impl AsContextMut) -> Self { ctx.as_context_mut() .store .inner .alloc_data_segment(DataSegmentEntity::active()) }
Allocates a new active [`DataSegment`] on the store. # Errors If more than [`u32::MAX`] much linear memory is allocated.
new_active
rust
wasmi-labs/wasmi
crates/wasmi/src/memory/data.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/memory/data.rs
Apache-2.0
pub fn new_passive(mut ctx: impl AsContextMut, bytes: PassiveDataSegmentBytes) -> Self { ctx.as_context_mut() .store .inner .alloc_data_segment(DataSegmentEntity::passive(bytes)) }
Allocates a new passive [`DataSegment`] on the store. # Errors If more than [`u32::MAX`] much linear memory is allocated.
new_passive
rust
wasmi-labs/wasmi
crates/wasmi/src/memory/data.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/memory/data.rs
Apache-2.0
pub fn passive(bytes: PassiveDataSegmentBytes) -> Self { Self { bytes: Some(bytes) } }
Creates a new passive [`DataSegmentEntity`] with its `bytes`.
passive
rust
wasmi-labs/wasmi
crates/wasmi/src/memory/data.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/memory/data.rs
Apache-2.0
pub fn bytes(&self) -> &[u8] { self.bytes .as_ref() .map(AsRef::as_ref) .unwrap_or_else(|| &[]) }
Returns the bytes of the [`DataSegmentEntity`].
bytes
rust
wasmi-labs/wasmi
crates/wasmi/src/memory/data.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/memory/data.rs
Apache-2.0
pub fn drop_bytes(&mut self) { self.bytes = None; }
Drops the bytes of the [`DataSegmentEntity`].
drop_bytes
rust
wasmi-labs/wasmi
crates/wasmi/src/memory/data.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/memory/data.rs
Apache-2.0
pub fn new(mut ctx: impl AsContextMut, ty: MemoryType) -> Result<Self, Error> { let (inner, mut resource_limiter) = ctx .as_context_mut() .store .store_inner_and_resource_limiter_ref(); let entity = CoreMemory::new(ty.core, &mut resource_limiter)?; let memory = inner.alloc_memory(entity); Ok(memory) }
Creates a new linear memory to the store. # Errors If more than [`u32::MAX`] much linear memory is allocated.
new
rust
wasmi-labs/wasmi
crates/wasmi/src/memory/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/memory/mod.rs
Apache-2.0
pub fn new_static( mut ctx: impl AsContextMut, ty: MemoryType, buf: &'static mut [u8], ) -> Result<Self, Error> { let (inner, mut resource_limiter) = ctx .as_context_mut() .store .store_inner_and_resource_limiter_ref(); let entity = CoreMemory::new_static(ty.core, &mut resource_limiter, buf)?; let memory = inner.alloc_memory(entity); Ok(memory) }
Creates a new linear memory to the store. # Errors If more than [`u32::MAX`] much linear memory is allocated. - If static buffer is invalid
new_static
rust
wasmi-labs/wasmi
crates/wasmi/src/memory/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/memory/mod.rs
Apache-2.0
pub fn ty(&self, ctx: impl AsContext) -> MemoryType { let core = ctx.as_context().store.inner.resolve_memory(self).ty(); MemoryType { core } }
Returns the memory type of the linear memory. # Panics Panics if `ctx` does not own this [`Memory`].
ty
rust
wasmi-labs/wasmi
crates/wasmi/src/memory/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/memory/mod.rs
Apache-2.0
pub(crate) fn dynamic_ty(&self, ctx: impl AsContext) -> MemoryType { let core = ctx .as_context() .store .inner .resolve_memory(self) .dynamic_ty(); MemoryType { core } }
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. # Panics Panics if `ctx` does not own this [`Memory`].
dynamic_ty
rust
wasmi-labs/wasmi
crates/wasmi/src/memory/mod.rs
https://github.com/wasmi-labs/wasmi/blob/master/crates/wasmi/src/memory/mod.rs
Apache-2.0