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.



51
52
53
# File 'ext/src/ruby_api/global.rs', line 51

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.



61
62
63
# File 'ext/src/ruby_api/global.rs', line 61

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)


98
99
100
# File 'ext/src/ruby_api/global.rs', line 98

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.



118
119
120
121
122
# File 'ext/src/ruby_api/global.rs', line 118

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)


129
130
131
132
133
134
135
136
137
138
139
140
# File 'ext/src/ruby_api/global.rs', line 129

pub fn set(&self, value: Value) -> Result<(), Error> {
    self.inner
        .set(
            self.store.context_mut()?,
            value.to_wasm_val(&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.



112
113
114
# File 'ext/src/ruby_api/global.rs', line 112

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

#var?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'ext/src/ruby_api/global.rs', line 105

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