pub struct CodeBuilder<'a> { /* private fields */ }
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
or wasm_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).
WebAssembly text files are automatically converted to a WebAssembly binary
and then the binary is compiled. This requires the wat
feature of the
wasmtime
crate to be enabled, and the feature is enabled by default.
If the text format is not desired then the CodeBuilder::wat
method
can be used to disable this conversion.
Implementations§
Source§impl<'a> CodeBuilder<'a>
impl<'a> CodeBuilder<'a>
Sourcepub fn new(engine: &'a Engine) -> CodeBuilder<'a>
pub fn new(engine: &'a Engine) -> CodeBuilder<'a>
Creates a new builder which will insert modules into the specified
Engine
.
Sourcepub fn wasm(
&mut self,
wasm_bytes: &'a [u8],
wasm_path: Option<&'a Path>,
) -> Result<&mut Self>
pub fn wasm( &mut self, wasm_bytes: &'a [u8], wasm_path: Option<&'a Path>, ) -> Result<&mut Self>
Configures the WebAssembly binary or text that is being compiled.
The wasm_bytes
parameter is either a binary WebAssembly file or a
WebAssembly module in its text format. 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
If wasm bytes have already been configured via a call to this method or
CodeBuilder::wasm_file
then an error will be returned.
Sourcepub fn wat(&mut self, enable: bool) -> Result<&mut Self>
pub fn wat(&mut self, enable: bool) -> Result<&mut Self>
Configures whether the WebAssembly text format is supported in this builder.
This support is enabled by default if the wat
crate feature is also
enabled.
§Errors
If this feature is explicitly enabled here via this method and the
wat
crate feature is disabled then an error will be returned.
Sourcepub fn wasm_file(&mut self, file: &'a Path) -> Result<&mut Self>
pub fn wasm_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
either as a WebAssembly binary or as a WebAssembly text file. The
contents are inspected to do this, the file extension is not consulted.
§Errors
If wasm bytes have already been configured via a call to this method or
CodeBuilder::wasm
then an error will be returned.
If file
can’t be read or an error happens reading it then that will
also be returned.
Sourcepub fn compile_module_serialized(&self) -> Result<Vec<u8>>
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
or
CodeBuilder::wasm_file
was 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.
Sourcepub fn compile_component_serialized(&self) -> Result<Vec<u8>>
Available on crate feature component-model
only.
pub fn compile_component_serialized(&self) -> Result<Vec<u8>>
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>
impl<'a> CodeBuilder<'a>
Sourcepub fn compile_module(&self) -> Result<Module>
Available on crate feature runtime
only.
pub fn compile_module(&self) -> Result<Module>
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
.
Sourcepub fn compile_component(&self) -> Result<Component>
Available on crate features runtime
and component-model
only.
pub fn compile_component(&self) -> Result<Component>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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