pub struct WasmtimeEngine { /* private fields */ }
Expand description
An Engine
which is a global context for compilation and management of wasm
modules.
An engine can be safely shared across threads and is a cheap cloneable handle to the actual engine. The engine itself will be deallocated once all references to it have gone away.
Engines store global configuration preferences such as compilation settings, enabled features, etc. You’ll likely only need at most one of these for a program.
§Engines and Clone
Using clone
on an Engine
is a cheap operation. It will not create an
entirely new engine, but rather just a new reference to the existing engine.
In other words it’s a shallow copy, not a deep copy.
§Engines and Default
You can create an engine with default configuration settings using
Engine::default()
. Be sure to consult the documentation of Config
for
default settings.
Implementations§
Source§impl Engine
impl Engine
Sourcepub fn new(config: &Config) -> Result<Engine, Error>
pub fn new(config: &Config) -> Result<Engine, Error>
Creates a new Engine
with the specified compilation and
configuration settings.
§Errors
This method can fail if the config
is invalid or some
configurations are incompatible.
For example, feature reference_types
will need to set
the compiler setting enable_safepoints
and unwind_info
to true
, but explicitly disable these two compiler settings
will cause errors.
Sourcepub fn weak(&self) -> EngineWeak
pub fn weak(&self) -> EngineWeak
Take a weak reference to this engine.
Sourcepub fn same(a: &Engine, b: &Engine) -> bool
pub fn same(a: &Engine, b: &Engine) -> bool
Returns whether the engine a
and b
refer to the same configuration.
Sourcepub fn is_async(&self) -> bool
pub fn is_async(&self) -> bool
Returns whether the engine is configured to support async functions.
Sourcepub fn detect_precompiled(&self, bytes: &[u8]) -> Option<Precompiled>
pub fn detect_precompiled(&self, bytes: &[u8]) -> Option<Precompiled>
Detects whether the bytes provided are a precompiled object produced by Wasmtime.
This function will inspect the header of bytes
to determine if it
looks like a precompiled core wasm module or a precompiled component.
This does not validate the full structure or guarantee that
deserialization will succeed, instead it helps higher-levels of the
stack make a decision about what to do next when presented with the
bytes
as an input module.
If the bytes
looks like a precompiled object previously produced by
Module::serialize
,
Component::serialize
,
Engine::precompile_module
, or Engine::precompile_component
, then
this will return Some(...)
indicating so. Otherwise None
is
returned.
Sourcepub fn detect_precompiled_file(
&self,
path: impl AsRef<Path>,
) -> Result<Option<Precompiled>, Error>
pub fn detect_precompiled_file( &self, path: impl AsRef<Path>, ) -> Result<Option<Precompiled>, Error>
Like Engine::detect_precompiled
, but performs the detection on a file.
Source§impl Engine
impl Engine
Sourcepub fn precompile_module(&self, bytes: &[u8]) -> Result<Vec<u8>, Error>
pub fn precompile_module(&self, bytes: &[u8]) -> Result<Vec<u8>, Error>
Ahead-of-time (AOT) compiles a WebAssembly module.
The bytes
provided must be in one of two formats:
- A binary-encoded WebAssembly module. This is always supported.
- A text-encoded instance of the WebAssembly text format.
This is only supported when the
wat
feature of this crate is enabled. If this is supplied then the text format will be parsed before validation. Note that thewat
feature is enabled by default.
This method may be used to compile a module for use with a different target
host. The output of this method may be used with
Module::deserialize
on hosts compatible
with the Config
associated with this Engine
.
The output of this method is safe to send to another host machine for later
execution. As the output is already a compiled module, translation and code
generation will be skipped and this will improve the performance of constructing
a Module
from the output of this method.
Sourcepub fn precompile_component(&self, bytes: &[u8]) -> Result<Vec<u8>, Error>
pub fn precompile_component(&self, bytes: &[u8]) -> Result<Vec<u8>, Error>
Same as Engine::precompile_module
except for a
Component
Source§impl Engine
impl Engine
Sourcepub fn tls_eager_initialize()
pub fn tls_eager_initialize()
Eagerly initialize thread-local functionality shared by all Engine
s.
Wasmtime’s implementation on some platforms may involve per-thread setup that needs to happen whenever WebAssembly is invoked. This setup can take on the order of a few hundred microseconds, whereas the overhead of calling WebAssembly is otherwise on the order of a few nanoseconds. This setup cost is paid once per-OS-thread. If your application is sensitive to the latencies of WebAssembly function calls, even those that happen first on a thread, then this function can be used to improve the consistency of each call into WebAssembly by explicitly frontloading the cost of the one-time setup per-thread.
Note that this function is not required to be called in any embedding. Wasmtime will automatically initialize thread-local-state as necessary on calls into WebAssembly. This is provided for use cases where the latency of WebAssembly calls are extra-important, which is not necessarily true of all embeddings.
Sourcepub fn increment_epoch(&self)
pub fn increment_epoch(&self)
Increments the epoch.
When using epoch-based interruption, currently-executing Wasm
code within this engine will trap or yield “soon” when the
epoch deadline is reached or exceeded. (The configuration, and
the deadline, are set on the Store
.) The intent of the
design is for this method to be called by the embedder at some
regular cadence, for example by a thread that wakes up at some
interval, or by a signal handler.
See Config::epoch_interruption
for an introduction to epoch-based interruption and pointers
to the other relevant methods.
When performing increment_epoch
in a separate thread, consider using
Engine::weak
to hold an EngineWeak
and
performing EngineWeak::upgrade
on each
tick, so that the epoch ticking thread does not keep an Engine
alive
longer than any of its consumers.
§Signal Safety
This method is signal-safe: it does not make any syscalls, and performs only an atomic increment to the epoch value in memory.
Sourcepub fn precompile_compatibility_hash(&self) -> impl Hash
pub fn precompile_compatibility_hash(&self) -> impl Hash
Returns a std::hash::Hash
that can be used to check precompiled WebAssembly compatibility.
The outputs of Engine::precompile_module
and Engine::precompile_component
are compatible with a different Engine
instance only if the two engines use
compatible Config
s. If this Hash matches between two Engine
s then binaries
from one are guaranteed to deserialize in the other.
Sourcepub unsafe fn unload_process_handlers(self)
pub unsafe fn unload_process_handlers(self)
Unload process-related trap/signal handlers and destroy this engine.
This method is not safe and is not widely applicable. It is not required to be called and is intended for use cases such as unloading a dynamic library from a process. It is difficult to invoke this method correctly and it requires careful coordination to do so.
§Panics
This method will panic if this Engine
handle is not the last remaining
engine handle.
§Aborts
This method will abort the process on some platforms in some situations where unloading the handler cannot be performed and an unrecoverable state is reached. For example on Unix platforms with signal handling the process will be aborted if the current signal handlers are not Wasmtime’s.
§Unsafety
This method is not generally safe to call and has a number of preconditions that must be met to even possibly be safe. Even with these known preconditions met there may be other unknown invariants to uphold as well.
-
There must be no other instances of
Engine
elsewhere in the process. Note that this isn’t just copies of thisEngine
but it’s any otherEngine
at all. This unloads global state that is used by allEngine
s so this instance must be the last. -
On Unix platforms no other signal handlers could have been installed for signals that Wasmtime catches. In this situation Wasmtime won’t know how to restore signal handlers that Wasmtime possibly overwrote when Wasmtime was initially loaded. If possible initialize other libraries first and then initialize Wasmtime last (e.g. defer creating an
Engine
). -
All existing threads which have used this DLL or copy of Wasmtime may no longer use this copy of Wasmtime. Per-thread state is not iterated and destroyed. Only future threads may use future instances of this Wasmtime itself.
If other crashes are seen from using this method please feel free to file an issue to update the documentation here with more preconditions that must be met.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Engine
impl !RefUnwindSafe for Engine
impl Send for Engine
impl Sync for Engine
impl Unpin for Engine
impl !UnwindSafe for Engine
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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