Class: Wasmtime::Global

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

Overview

Represents a WebAssembly global.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.const(store, type, default) ⇒ Global

Returns A constant global.

Parameters:

  • store (Store)
  • type (Symbol)

    The WebAssembly type of the value held by this global.

  • default (Object)

    The default value of this global.

Returns:

  • (Global)

    A constant global.



85
86
87
# File 'ext/src/ruby_api/global.rs', line 85

pub fn const_(store: Obj<Store>, value_type: Symbol, default: Value) -> Result<Self, Error> {
    Self::new(store, value_type, default, Mutability::Const)
}

.var(store, type, default:) ⇒ Global

Returns A variable global.

Parameters:

  • store (Store)
  • type (Symbol)

    The WebAssembly type of the value held by this global.

  • default (Object)

    The default value of this global.

Returns:

  • (Global)

    A variable global.



95
96
97
# File 'ext/src/ruby_api/global.rs', line 95

pub fn var(store: Obj<Store>, value_type: Symbol, default: Value) -> Result<Self, Error> {
    Self::new(store, value_type, default, Mutability::Var)
}

Instance Method Details

#const?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'ext/src/ruby_api/global.rs', line 131

pub fn is_const(&self) -> Result<bool, Error> {
    self.ty().map(|ty| ty.mutability() == Mutability::Const)
}

#getObject

Returns The current value of the global.

Returns:

  • (Object)

    The current value of the global.



151
152
153
154
155
# File 'ext/src/ruby_api/global.rs', line 151

pub fn get(&self) -> Result<Value, Error> {
    self.inner
        .get(self.store.context_mut()?)
        .to_ruby_value(&self.store)
}

#set(value) ⇒ nil

Sets the value of the global. Raises if the global is a const.

Parameters:

  • value (Object)

    An object that can be converted to the global’s type.

Returns:

  • (nil)


162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/src/ruby_api/global.rs', line 162

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

#typeSymbol

Returns The Wasm type of the global‘s content.

Returns:

  • (Symbol)

    The Wasm type of the global‘s content.



145
146
147
# File 'ext/src/ruby_api/global.rs', line 145

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

#var?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'ext/src/ruby_api/global.rs', line 138

pub fn is_var(&self) -> Result<bool, Error> {
    self.ty().map(|ty| ty.mutability() == Mutability::Var)
}