Class: Wasmtime::Engine
- Inherits:
-
Object
- Object
- Wasmtime::Engine
- Defined in:
- ext/src/ruby_api/engine.rs
Overview
Represents a Wasmtime execution engine.
Class Method Summary collapse
Instance Method Summary collapse
-
#increment_epoch ⇒ nil
Manually increment the engine’s epoch.
-
#precompile_module(wat_or_wasm) ⇒ String
AoT compile a WebAssembly text or WebAssembly binary module for later use.
-
#start_epoch_interval(milliseconds) ⇒ nil
Starts a timer that will increment the engine’s epoch every
milliseconds
. -
#stop_epoch_interval ⇒ nil
Stops a previously started timer with #start_epoch_interval.
Class Method Details
.new(config = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'ext/src/ruby_api/engine.rs', line 55
pub fn new(args: &[Value]) -> Result<Self, Error> {
let args = scan_args::scan_args::<(), (Option<Value>,), (), (), (), ()>(args)?;
let (config,) = args.optional;
let config = config.and_then(|v| if v.is_nil() { None } else { Some(v) });
let inner = match config {
Some(config) => {
let config = config.try_convert::<RHash>().and_then(hash_to_config)?;
EngineImpl::new(&config).map_err(|e| error!("{}", e))?
}
None => EngineImpl::default(),
};
Ok(Self {
inner,
#[cfg(feature = "tokio")]
timer_task: Default::default(),
})
}
|
Instance Method Details
#increment_epoch ⇒ nil
Manually increment the engine’s epoch. Note: this cannot be used from a different thread while WebAssembly is running because the Global VM lock (GVL) is not released. Using #start_epoch_interval is recommended because it sidesteps the GVL.
118 119 120 |
# File 'ext/src/ruby_api/engine.rs', line 118 pub fn increment_epoch(&self) { self.inner.increment_epoch(); } |
#precompile_module(wat_or_wasm) ⇒ String
AoT compile a WebAssembly text or WebAssembly binary module for later use.
The compiled module can be instantiated using Module.deserialize.
135 136 137 138 139 140 |
# File 'ext/src/ruby_api/engine.rs', line 135
pub fn precompile_module(&self, wat_or_wasm: RString) -> Result<RString, Error> {
self.inner
.precompile_module(unsafe { wat_or_wasm.as_slice() })
.map(|bytes| RString::from_slice(&bytes))
.map_err(|e| error!("{}", e.to_string()))
}
|
#start_epoch_interval(milliseconds) ⇒ nil
Starts a timer that will increment the engine’s epoch every milliseconds
. Waits milliseconds
before incrementing for the first time.
If a prior timer was started, it will be stopped.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'ext/src/ruby_api/engine.rs', line 84
pub fn start_epoch_interval(&self, milliseconds: u64) {
self.stop_epoch_interval();
let engine = self.inner.clone();
let handle = TOKIO_RT.spawn(async move {
let tick_every = tokio::time::Duration::from_millis(milliseconds);
let mut interval = async_timer::Interval::platform_new(tick_every);
loop {
interval.wait().await;
engine.increment_epoch();
}
});
*self.timer_task.borrow_mut() = Some(handle);
}
|
#stop_epoch_interval ⇒ nil
Stops a previously started timer with #start_epoch_interval. Does nothing if there is no running timer.
106 107 108 109 110 |
# File 'ext/src/ruby_api/engine.rs', line 106
pub fn stop_epoch_interval(&self) {
if let Some(handle) = self.timer_task.take() {
handle.abort();
}
}
|