Class: Wasmtime::Store

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

Overview

Represents a WebAssembly store.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(engine, data = nil, wasi_ctx: nil) ⇒ Wasmtime::Store

Examples:

store = Wasmtime::Store.new(Wasmtime::Engine.new)
store = Wasmtime::Store.new(Wasmtime::Engine.new, {})

Parameters:

  • engine (Wasmtime::Engine)

    The engine for this store.

  • data (Object) (defaults to: nil)

    The data attached to the store. Can be retrieved through #data and Caller#data.

  • wasi_ctx (Wasmtime::WasiCtxBuilder) (defaults to: nil)

    The WASI context to use in this store.

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/src/ruby_api/store.rs', line 97

pub fn new(args: &[Value]) -> Result<Self, Error> {
    let args = scan_args::scan_args::<(&Engine,), (Option<Value>,), (), (), _, ()>(args)?;
    let kw = scan_args::get_kwargs::<_, (), (Option<&WasiCtxBuilder>,), ()>(
        args.keywords,
        &[],
        &[*WASI_CTX],
    )?;
    let (engine,) = args.required;
    let (user_data,) = args.optional;
    let user_data = user_data.unwrap_or_else(|| QNIL.into());
    let wasi = match kw.optional.0 {
        None => None,
        Some(wasi_ctx_builder) => Some(wasi_ctx_builder.build_context()?),
    };

    let eng = engine.get();
    let store_data = StoreData {
        user_data,
        wasi,
        refs: Default::default(),
    };
    let store = Self {
        inner: UnsafeCell::new(StoreImpl::new(eng, store_data)),
    };

    Ok(store)
}

Instance Method Details

#add_fuel(fuel) ⇒ Nil

Adds fuel to the Wasmtime::Store.

Parameters:

  • fuel (Integer)

    The fuel to add.

Returns:

  • (Nil)


144
145
146
147
148
149
150
# File 'ext/src/ruby_api/store.rs', line 144

pub fn add_fuel(&self, fuel: u64) -> Result<Value, Error> {
    unsafe { &mut *self.inner.get() }
        .add_fuel(fuel)
        .map_err(|e| error!("{}", e))?;

    Ok(*QNIL)
}

#consume_fuel(fuel) ⇒ Integer

Synthetically consumes fuel from this Wasmtime::Store. Raises if there isn’t enough fuel left in the Wasmtime::Store, or when the Engine’s config does not have fuel enabled.

Parameters:

  • fuel (Integer)

    The fuel to consume.

Returns:

  • (Integer)

    The remaining fuel.



160
161
162
163
164
# File 'ext/src/ruby_api/store.rs', line 160

pub fn consume_fuel(&self, fuel: u64) -> Result<u64, Error> {
    unsafe { &mut *self.inner.get() }
        .consume_fuel(fuel)
        .map_err(|e| error!("{}", e))
}

#dataObject

Returns The passed in value in new.

Returns:

  • (Object)

    The passed in value in new



127
128
129
# File 'ext/src/ruby_api/store.rs', line 127

pub fn data(&self) -> Value {
    self.context().data().user_data()
}

#fuel_consumedInteger, Nil

Returns the amount of fuel consumed by this Wasmtime::Store’s execution so far, or nil when the Engine’s config does not have fuel enabled.

Returns:

  • (Integer, Nil)


135
136
137
# File 'ext/src/ruby_api/store.rs', line 135

pub fn fuel_consumed(&self) -> Option<u64> {
    self.inner_ref().fuel_consumed()
}

#set_epoch_deadline(ticks_beyond_current) ⇒ nil

Sets the epoch deadline to a certain number of ticks in the future.

Raises if there isn’t enough fuel left in the Wasmtime::Store, or when the Engine’s config does not have fuel enabled.

Parameters:

  • ticks_beyond_current (Integer)

    The number of ticks before this store reaches the deadline.

Returns:

  • (nil)

See Also:



176
177
178
# File 'ext/src/ruby_api/store.rs', line 176

pub fn set_epoch_deadline(&self, ticks_beyond_current: u64) {
    unsafe { &mut *self.inner.get() }.set_epoch_deadline(ticks_beyond_current);
}