[][src]Struct wasmer::Global

pub struct Global { /* fields omitted */ }

A WebAssembly global instance.

A global instance is the runtime representation of a global variable. It consists of an individual value and a flag indicating whether it is mutable.

Spec: https://webassembly.github.io/spec/core/exec/runtime.html#global-instances

Implementations

impl Global[src]

pub fn new(store: &Store, val: Val) -> Self[src]

Create a new Global with the initial value Val.

Example

let g = Global::new(&store, Value::I32(1));

assert_eq!(g.get(), Value::I32(1));
assert_eq!(g.ty().mutability, Mutability::Const);

pub fn new_mut(store: &Store, val: Val) -> Self[src]

Create a mutable Global with the initial value Val.

Example

let g = Global::new_mut(&store, Value::I32(1));

assert_eq!(g.get(), Value::I32(1));
assert_eq!(g.ty().mutability, Mutability::Var);

pub fn ty(&self) -> &GlobalType[src]

Returns the GlobalType of the Global.

Example

let c = Global::new(&store, Value::I32(1));
let v = Global::new_mut(&store, Value::I64(1));

assert_eq!(c.ty(), &GlobalType::new(Type::I32, Mutability::Const));
assert_eq!(v.ty(), &GlobalType::new(Type::I64, Mutability::Var));

pub fn store(&self) -> &Store[src]

Returns the Store where the Global belongs.

Example

let g = Global::new(&store, Value::I32(1));

assert_eq!(g.store(), &store);

pub fn get(&self) -> Val[src]

Retrieves the current value Val that the Global has.

Example

let g = Global::new(&store, Value::I32(1));

assert_eq!(g.get(), Value::I32(1));

pub fn set(&self, val: Val) -> Result<(), RuntimeError>[src]

Sets a custom value Val to the runtime Global.

Example

let g = Global::new_mut(&store, Value::I32(1));

assert_eq!(g.get(), Value::I32(1));

g.set(Value::I32(2));

assert_eq!(g.get(), Value::I32(2));

Errors

Trying to mutate a immutable global will raise an error:

This example panics
let g = Global::new(&store, Value::I32(1));

g.set(Value::I32(2)).unwrap();

Trying to set a value of a incompatible type will raise an error:

This example panics
let g = Global::new(&store, Value::I32(1));

// This results in an error: `RuntimeError`.
g.set(Value::I64(2)).unwrap();

pub fn same(&self, other: &Self) -> bool[src]

Returns whether or not these two globals refer to the same data.

Example

let g = Global::new(&store, Value::I32(1));

assert!(g.same(&g));

Trait Implementations

impl Clone for Global[src]

impl Debug for Global[src]

impl<'a> Exportable<'a> for Global[src]

impl From<Global> for Extern[src]

Auto Trait Implementations

impl !RefUnwindSafe for Global

impl Send for Global

impl Sync for Global

impl Unpin for Global

impl !UnwindSafe for Global

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pointable for T

type Init = T

The type for initializers.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,