Class: Wasmtime::Engine

Inherits:
Object
  • Object
show all
Defined in:
ext/src/ruby_api/engine.rs

Overview

Represents a Wasmtime execution engine.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(config = {}) ⇒ Object

Parameters:

  • config (Hash) (defaults to: {})

    The engine’s config. See the Config‘s Rust doc for detailed description of the different options and the defaults.

Options Hash (config):

  • :debug_info (Boolean)
  • :wasm_backtrace_details (Boolean)
  • :native_unwind_info (Boolean)
  • :consume_fuel (Boolean)
  • :epoch_interruption (Boolean)
  • :max_wasm_stack (Integer)
  • :wasm_threads (Boolean)
  • :wasm_multi_memory (Boolean)
  • :wasm_memory64 (Boolean)
  • :parallel_compilation (Boolean)
  • :cranelift_opt_level (Symbol)

    One of none, speed, speed_and_size.

  • :profiler (Symbol)

    One of none, jitdump, vtune.

See Also:



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_epochnil

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.

Returns:

  • (nil)


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.

Parameters:

  • wat_or_wasm (String)

    The String of WAT or Wasm.

Returns:

  • (String)

    Binary String of the compiled module.

See Also:



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.

Parameters:

  • milliseconds (Integer)

Returns:

  • (nil)


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_intervalnil

Stops a previously started timer with #start_epoch_interval. Does nothing if there is no running timer.

Returns:

  • (nil)


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();
    }
}