using System; namespace Wasmtime { /// /// Represents a constant WebAssembly global value. /// public class Global { /// /// Creates a new with the given initial value. /// /// The initial value of the global. public Global(T initialValue) { InitialValue = initialValue; Kind = Interop.ToValueKind(typeof(T)); } /// /// The value of the global. /// public T Value { get { if (Handle is null) { throw new InvalidOperationException("The global cannot be used before it is bound to a module instance."); } unsafe { var v = stackalloc Interop.wasm_val_t[1]; Interop.wasm_global_get(Handle.DangerousGetHandle(), v); // TODO: figure out a way that doesn't box the value return (T)Interop.ToObject(v); } } } internal ValueKind Kind { get; private set; } internal Interop.GlobalHandle Handle { get; set; } internal T InitialValue { get; private set; } } }