wasm_encoder

Struct Function

Source
pub struct Function { /* private fields */ }
Expand description

An encoder for a function body within the code section.

§Example

use wasm_encoder::{CodeSection, Function, Instruction};

// Define the function body for:
//
//     (func (param i32 i32) (result i32)
//       local.get 0
//       local.get 1
//       i32.add)
let locals = vec![];
let mut func = Function::new(locals);
func.instruction(&Instruction::LocalGet(0));
func.instruction(&Instruction::LocalGet(1));
func.instruction(&Instruction::I32Add);

// Add our function to the code section.
let mut code = CodeSection::new();
code.function(&func);

Implementations§

Source§

impl Function

Source

pub fn new<L>(locals: L) -> Self

Create a new function body with the given locals.

The argument is an iterator over (N, Ty), which defines that the next N locals will be of type Ty.

For example, a function with locals 0 and 1 of type I32 and local 2 of type F32 would be created as:

let f = Function::new([(2, ValType::I32), (1, ValType::F32)]);

For more information about the code section (and function definition) in the WASM binary format see the WebAssembly spec

Source

pub fn new_with_locals_types<L>(locals: L) -> Self
where L: IntoIterator<Item = ValType>,

Create a function from a list of locals’ types.

Unlike Function::new, this constructor simply takes a list of types which are in order associated with locals.

For example:

let f = Function::new([(2, ValType::I32), (1, ValType::F32)]);
let g = Function::new_with_locals_types([
   ValType::I32, ValType::I32, ValType::F32
]);

assert_eq!(f, g)
Source

pub fn instruction(&mut self, instruction: &Instruction<'_>) -> &mut Self

Write an instruction into this function body.

Source

pub fn raw<B>(&mut self, bytes: B) -> &mut Self
where B: IntoIterator<Item = u8>,

Add raw bytes to this function’s body.

Source

pub fn byte_len(&self) -> usize

The number of bytes already added to this function.

This number doesn’t include the variable-width size field that encode will write before the added bytes, since the size of that field isn’t known until all the instructions are added to this function.

Source

pub fn into_raw_body(self) -> Vec<u8>

Unwraps and returns the raw byte encoding of this function.

This encoding doesn’t include the variable-width size field that encode will write before the added bytes. As such, its length will match the return value of [byte_len].

§Use Case

This raw byte form is suitable for later using with CodeSection::raw. Note that it differs from what results from Function::encode precisely due to the lack of the length prefix; CodeSection::raw will use this. Using Function::encode instead produces bytes that cannot be fed into other wasm-encoder types without stripping off the length prefix, which is awkward and error-prone.

This method combined with CodeSection::raw may be useful together if one wants to save the result of function encoding and use it later: for example, caching the result of some code generation process.

For example:

let mut f = Function::new([]);
f.instruction(&Instruction::End);
let bytes = f.into_raw_body();
// (save `bytes` somewhere for later use)
let mut code = CodeSection::new();
code.raw(&bytes[..]);

assert_eq!(2, bytes.len());  // Locals count, then `end`
assert_eq!(3, code.byte_len()); // Function length byte, function body

Trait Implementations§

Source§

impl Clone for Function

Source§

fn clone(&self) -> Function

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Function

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Encode for Function

Source§

fn encode(&self, sink: &mut Vec<u8>)

Encode the type into the given byte sink.
Source§

impl PartialEq for Function

Source§

fn eq(&self, other: &Function) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Function

Source§

impl StructuralPartialEq for Function

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.