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, limits: 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.

  • limits (Hash) (defaults to: nil)

    See the StoreLimitsBuilder‘s Rust doc for detailed description of the different options and the defaults.

Options Hash (limits:):

  • memory_size (Integer)

    The maximum number of bytes a linear memory can grow to.

  • table_elements (Integer)

    The maximum number of elements in a table.

  • instances (Integer)

    The maximum number of instances that can be created for a Store.

  • tables (Integer)

    The maximum number of tables that can be created for a Store.

  • memories (Integer)

    The maximum number of linear memories that can be created for a Store.

Returns:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'ext/src/ruby_api/store.rs', line 137

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<&WasiCtx>, Option<RHash>), ()>(
        args.keywords,
        &[],
        &[*WASI_CTX, *LIMITS],
    )?;

    let (engine,) = args.required;
    let (user_data,) = args.optional;
    let user_data = user_data.unwrap_or_else(|| ().into_value());
    let wasi = kw.optional.0.map(|wasi_ctx| wasi_ctx.get_inner());

    let limiter = match kw.optional.1 {
        None => StoreLimitsBuilder::new(),
        Some(limits) => hash_to_store_limits_builder(limits)?,
    }
    .build();
    let limiter = TrackingResourceLimiter::new(limiter);

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

    unsafe { &mut *store.inner.get() }.limiter(|data| &mut data.store_limits);

    Ok(store)
}

Instance Method Details

#dataObject

Returns The passed in value in new.

Returns:

  • (Object)

    The passed in value in new



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

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

#get_fuelInteger

Returns the amount of fuel in the Wasmtime::Store.

Returns:

  • (Integer)

Raises:

  • (Error)

    if fuel consumption is not enabled via Engine#new



185
186
187
# File 'ext/src/ruby_api/store.rs', line 185

pub fn get_fuel(&self) -> Result<u64, Error> {
    self.inner_ref().get_fuel().map_err(|e| error!("{}", e))
}

#linear_memory_limit_hit?Boolean

Returns whether the linear memory limit has been hit.

Returns:

  • (Boolean)


221
222
223
# File 'ext/src/ruby_api/store.rs', line 221

pub fn linear_memory_limit_hit(&self) -> bool {
    self.context().data().store_limits.linear_memory_limit_hit()
}

#max_linear_memory_consumedInteger

Returns the maximum linear memory consumed.

Returns:

  • (Integer)


229
230
231
232
233
234
# File 'ext/src/ruby_api/store.rs', line 229

pub fn max_linear_memory_consumed(&self) -> usize {
    self.context()
        .data()
        .store_limits
        .max_linear_memory_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:



212
213
214
# File 'ext/src/ruby_api/store.rs', line 212

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

#set_fuel(fuel) ⇒ Object

Sets fuel to the Wasmtime::Store.

Parameters:

  • fuel (Integer)

    The new fuel amount.

Raises:

  • (Error)

    if fuel consumption is not enabled via Engine#new



194
195
196
197
198
199
200
# File 'ext/src/ruby_api/store.rs', line 194

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

    Ok(())
}