wasmtime

Struct CodeBuilder

Source
pub struct CodeBuilder<'a> { /* private fields */ }
Available on crate features cranelift or winch only.
Expand description

Builder-style structure used to create a Module or pre-compile a module to a serialized list of bytes.

This structure can be used for more advanced configuration when compiling a WebAssembly module. Most configuration can use simpler constructors such as:

Note that a CodeBuilder always involves compiling WebAssembly bytes to machine code. To deserialize a list of bytes use Module::deserialize instead.

A CodeBuilder requires a source of WebAssembly bytes to be configured before calling compile_module_serialized or compile_module. This can be provided with either the wasm_binary or wasm_binary_file method. Note that only a single source of bytes can be provided.

§WebAssembly Text Format

This builder supports the WebAssembly Text Format (*.wat files) through the CodeBuilder::wasm_binary_or_text and CodeBuilder::wasm_binary_or_text_file methods. These methods automatically convert WebAssembly text files to binary. Note though that this behavior is disabled if the wat crate feature is not enabled.

Implementations§

Source§

impl<'a> CodeBuilder<'a>

Source

pub fn new(engine: &'a Engine) -> CodeBuilder<'a>

Creates a new builder which will insert modules into the specified Engine.

Source

pub fn wasm_binary( &mut self, wasm_bytes: impl Into<Cow<'a, [u8]>>, wasm_path: Option<&'a Path>, ) -> Result<&mut Self>

Configures the WebAssembly binary that is being compiled.

The wasm_bytes parameter must be a binary WebAssembly file. This will be stored within the CodeBuilder for processing later when compilation is finalized.

The optional wasm_path parameter is the path to the wasm_bytes on disk, if any. This may be used for diagnostics and other debugging-related purposes, but this method will not read the path specified.

§Errors

This method will return an error if WebAssembly bytes have already been configured.

Source

pub fn wasm_binary_or_text( &mut self, wasm_bytes: &'a [u8], wasm_path: Option<&'a Path>, ) -> Result<&mut Self>

Equivalent of CodeBuilder::wasm_binary that also accepts the WebAssembly text format.

This method will configure the WebAssembly binary to be compiled. The input wasm_bytes may either be the wasm text format or the binary format. If the wat crate feature is enabled, which is enabled by default, then the text format will automatically be converted to the binary format.

§Errors

This method will return an error if WebAssembly bytes have already been configured. This method will also return an error if wasm_bytes is the wasm text format and the text syntax is not valid.

Source

pub fn wasm_binary_file(&mut self, file: &'a Path) -> Result<&mut Self>

Reads the file specified for the WebAssembly bytes that are going to be compiled.

This method will read file from the filesystem and interpret it as a WebAssembly binary.

A DWARF package file will be probed using the root of file and with a .dwp extension. If found, it will be loaded and DWARF fusion performed.

§Errors

This method will return an error if WebAssembly bytes have already been configured.

If file can’t be read or an error happens reading it then that will also be returned.

If DWARF fusion is performed and the DWARF packaged file cannot be read then an error will be returned.

Source

pub fn wasm_binary_or_text_file(&mut self, file: &'a Path) -> Result<&mut Self>

Equivalent of CodeBuilder::wasm_binary_file that also accepts the WebAssembly text format.

This method is will read the file at path and interpret the contents to determine if it’s the wasm text format or binary format. The file extension of file is not consulted. The text format is automatically converted to the binary format if the crate feature wat is active.

§Errors

In addition to the errors returned by CodeBuilder::wasm_binary_file this may also fail if the text format is read and the syntax is invalid.

Source

pub fn dwarf_package_file(&mut self, file: &Path) -> Result<&mut Self>

Explicitly specify DWARF .dwp path.

§Errors

This method will return an error if the .dwp file has already been set through CodeBuilder::dwarf_package or auto-detection in CodeBuilder::wasm_binary_file.

This method will also return an error if file cannot be read.

Source

pub fn dwarf_package(&mut self, dwp_bytes: &'a [u8]) -> Result<&mut Self>

Set the DWARF package binary.

Initializes dwarf_package from dwp_bytes in preparation for DWARF fusion. Allows the DWARF package to be supplied as a byte array when the file probing performed in wasm_file is not appropriate.

§Errors

Returns an error if the *.dwp file is already set via auto-probing in CodeBuilder::wasm_binary_file or explicitly via CodeBuilder::dwarf_package_file.

Source

pub fn hint(&self) -> Option<CodeHint>

Returns a hint, if possible, of what the provided bytes are.

This method can be use to detect what the previously supplied bytes to methods such as CodeBuilder::wasm_binary_or_text are. This will return whether a module or a component was found in the provided bytes.

This method will return None if wasm bytes have not been configured or if the provided bytes don’t look like either a component or a module.

Source

pub fn compile_module_serialized(&self) -> Result<Vec<u8>>

Finishes this compilation and produces a serialized list of bytes.

This method requires that either CodeBuilder::wasm_binary or related methods were invoked prior to indicate what is being compiled.

This method will block the current thread until compilation has finished, and when done the serialized artifact will be returned.

Note that this method will never cache compilations, even if the cache feature is enabled.

§Errors

This can fail if the input wasm module was not valid or if another compilation-related error is encountered.

Source

pub fn compile_component_serialized(&self) -> Result<Vec<u8>>

Available on crate feature component-model only.

Same as CodeBuilder::compile_module_serialized except that it compiles a serialized Component instead of a module.

Source§

impl<'a> CodeBuilder<'a>

Source

pub fn compile_module(&self) -> Result<Module>

Available on crate feature runtime only.

Same as CodeBuilder::compile_module_serialized except that a Module is produced instead.

Note that this method will cache compilations if the cache feature is enabled and turned on in Config.

Source

pub fn compile_component(&self) -> Result<Component>

Available on crate features runtime and component-model only.

Same as CodeBuilder::compile_module except that it compiles a Component instead of a module.

Auto Trait Implementations§

§

impl<'a> Freeze for CodeBuilder<'a>

§

impl<'a> !RefUnwindSafe for CodeBuilder<'a>

§

impl<'a> Send for CodeBuilder<'a>

§

impl<'a> Sync for CodeBuilder<'a>

§

impl<'a> Unpin for CodeBuilder<'a>

§

impl<'a> !UnwindSafe for CodeBuilder<'a>

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.