Struct walrus::FunctionBuilder

source ·
pub struct FunctionBuilder { /* private fields */ }
Expand description

Build instances of LocalFunction.

Example

Implementations§

source§

impl FunctionBuilder

source

pub fn new( types: &mut ModuleTypes, params: &[ValType], results: &[ValType] ) -> FunctionBuilder

Creates a new, empty function builder.

source

pub fn name(&mut self, function_name: String) -> &mut FunctionBuilder

Set function name.

source

pub fn func_body_id(&self) -> InstrSeqId

Get the id of this function’s body’s instruction sequence.

source

pub fn func_body(&mut self) -> InstrSeqBuilder<'_>

Get a InstrSeqBuilder for building and mutating this function’s body.

source

pub fn instr_seq(&mut self, id: InstrSeqId) -> InstrSeqBuilder<'_>

Continue building and mutating an existing instruction sequence.

Example
let mut module = walrus::Module::default();
let mut builder = walrus::FunctionBuilder::new(&mut module.types, &[], &[]);

let mut block = builder.dangling_instr_seq(None);
let id = block.id();
// Build up the block some.
block
    .f64_const(1337.0)
    .drop();

// Do some other stuff...
drop(block);

// Use `instr_seq` to get the builder for the block again, and build
// some more things onto it.
let mut block = builder.instr_seq(id);
block
    .i32_const(42)
    .drop();
source

pub fn dangling_instr_seq( &mut self, ty: impl Into<InstrSeqType> ) -> InstrSeqBuilder<'_>

Create a new instruction sequence that is unreachable.

It is your responsibility to

  • make a Instr::Block, Instr::Loop, or Instr::IfElse that uses this instruction sequence, and

  • append that Instr into a parent instruction sequence via InstrSeqBuilder::instr or InstrSeqBuilder::instr_at

or else this built up instruction sequence will never be used.

Example
use walrus::ir::*;

let mut module = walrus::Module::default();
let mut builder = walrus::FunctionBuilder::new(&mut module.types, &[], &[]);

// Create an empty, dangling instruction sequemce.
let mut seq = builder.dangling_instr_seq(None);
let seq_id = seq.id();

// Do stuff with the sequence...
drop(seq);

// Finally, make our instruction sequence reachable by adding an
// block/loop/if-else instruction that uses it to a reachable instruction
// sequence.
builder
    .func_body()
    .instr(Block { seq: seq_id });
source

pub fn finish( self, args: Vec<LocalId>, funcs: &mut ModuleFunctions ) -> FunctionId

Finishes this builder, wrapping it all up and inserting it into the specified Module.

Example
let mut module = walrus::Module::default();
let mut builder = walrus::FunctionBuilder::new(&mut module.types, &[], &[]);

builder
    .func_body()
    .i32_const(1234)
    .drop();

let function_id = builder.finish(vec![], &mut module.funcs);
source

pub fn local_func(self, args: Vec<LocalId>) -> LocalFunction

Returns the crate::LocalFunction built by this builder.

Trait Implementations§

source§

impl Debug for FunctionBuilder

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.