Class: Wasmtime::Table

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

Overview

Represents a WebAssembly table.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(store, type, initial, min_size:, max_size: nil) ⇒ Object

Parameters:

  • store (Store)
  • type (Symbol)

    The WebAssembly type of the value held by this table.

  • initial (Value)

    The initial value of values in the table.

  • min_size (Integer)

    The minimum number of elements in the table.

  • max_size (Integer, nil) (defaults to: nil)

    The maximum number of elements in the table.



85
86
87
88
89
90
91
92
93
94
95
96
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
# File 'ext/src/ruby_api/table.rs', line 85

pub fn new(args: &[Value]) -> Result<Self, Error> {
    let args = scan_args::scan_args::<(Obj<Store>, Symbol, Value), (), (), (), _, ()>(args)?;
    let kw = scan_args::get_kwargs::<_, (u32,), (Option<u32>,), ()>(
        args.keywords,
        &[*MIN_SIZE],
        &[*MAX_SIZE],
    )?;
    let (store, value_type, default) = args.required;
    let (min,) = kw.required;
    let (max,) = kw.optional;
    let wasm_type = value_type.to_val_type()?;
    let wasm_default = default.to_wasm_val(&store.into(), wasm_type.clone())?;
    let ref_ = wasm_default
        .ref_()
        .ok_or_else(|| error!("Expected Ref for table value"))?;

    let table_type = wasm_type
        .as_ref()
        .ok_or_else(|| error!("Expected RefType"))?
        .clone();

    let inner = TableImpl::new(
        store.context_mut(),
        wasmtime::TableType::new(table_type, min, max),
        ref_,
    )
    .map_err(|e| error!("{}", e))?;

    let table = Self {
        store: store.into(),
        inner,
    };

    table.retain_non_nil_extern_ref(default)?;

    Ok(table)
}

Instance Method Details

#get(index) ⇒ Object?

Returns the table element value at index, or nil if index is out of bound.

Parameters:

  • index (Integer)

Returns:

  • (Object, nil)


152
153
154
155
156
157
# File 'ext/src/ruby_api/table.rs', line 152

pub fn get(&self, index: u64) -> Result<Value, Error> {
    match self.inner.get(self.store.context_mut()?, index) {
        Some(wasm_val) => Val::from(wasm_val).to_ruby_value(&self.store),
        None => Ok(().into_value()),
    }
}

#grow(delta, initial) ⇒ void

This method returns an undefined value.

Grows the size of this table by delta. Raises if the table grows beyond its limit.

Parameters:

  • delta (Integer)

    The number of elements to add to the table.

  • initial (Object)

    The initial value for newly added table slots.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/src/ruby_api/table.rs', line 191

pub fn grow(&self, delta: u64, initial: Value) -> Result<u64, Error> {
    self.inner
        .grow(
            self.store.context_mut()?,
            delta,
            initial
                .to_wasm_val(&self.store, wasmtime::ValType::from(self.value_type()?))?
                .ref_()
                .ok_or_else(|| error!("Expected Ref"))?,
        )
        .map_err(|e| error!("{}", e))
        .and_then(|result| {
            self.retain_non_nil_extern_ref(initial)?;
            Ok(result)
        })
}

#max_sizeInteger?

Returns The maximum size of this table.

Returns:

  • (Integer, nil)

    The maximum size of this table.



142
143
144
# File 'ext/src/ruby_api/table.rs', line 142

pub fn max_size(&self) -> Result<Option<u64>, Error> {
    self.ty().map(|ty| ty.maximum())
}

#min_sizeInteger

Returns The minimum size of this table.

Returns:

  • (Integer)

    The minimum size of this table.



136
137
138
# File 'ext/src/ruby_api/table.rs', line 136

pub fn min_size(&self) -> Result<u64, Error> {
    self.ty().map(|ty| ty.minimum())
}

#set(index, value) ⇒ void

This method returns an undefined value.

Sets the table entry at index to value.

Parameters:

  • index (Integer)
  • value (Object)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'ext/src/ruby_api/table.rs', line 166

pub fn set(&self, index: u64, value: Value) -> Result<(), Error> {
    self.inner
        .set(
            self.store.context_mut()?,
            index,
            value
                .to_wasm_val(&self.store, wasmtime::ValType::from(self.value_type()?))?
                .ref_()
                .ok_or_else(|| error!("Expected Ref"))?,
        )
        .map_err(|e| error!("{}", e))
        .and_then(|result| {
            self.retain_non_nil_extern_ref(value)?;
            Ok(result)
        })
}

#sizeInteger

Returns The size of the table.

Returns:

  • (Integer)

    The size of the table.



210
211
212
# File 'ext/src/ruby_api/table.rs', line 210

pub fn size(&self) -> Result<u64, Error> {
    Ok(self.inner.size(self.store.context()?))
}

#typeSymbol

Returns The Wasm type of the elements of this table.

Returns:

  • (Symbol)

    The Wasm type of the elements of this table.



130
131
132
# File 'ext/src/ruby_api/table.rs', line 130

pub fn type_(&self) -> Result<Symbol, Error> {
    self.ty()?.element().to_sym()
}