using System;
using Wasmtime.Exports;
namespace Wasmtime.Externs
{
///
/// Represents an external (instantiated) WebAssembly global.
///
public class ExternGlobal
{
internal ExternGlobal(GlobalExport export, IntPtr global)
{
_export = export;
_global = global;
}
///
/// The name of the WebAssembly global.
///
public string Name => _export.Name;
///
/// The kind of value for the global variable.
///
public ValueKind Kind => _export.Kind;
///
/// Determines whether or not the global variable is mutable.
///
public bool IsMutable => _export.IsMutable;
public object Value
{
get
{
unsafe
{
var v = stackalloc Interop.wasm_val_t[1];
Interop.wasm_global_get(_global, v);
return Interop.ToObject(v);
}
}
set
{
if (!IsMutable)
{
throw new InvalidOperationException($"The value of global '{Name}' cannot be modified.");
}
var v = Interop.ToValue(value, Kind);
unsafe
{
Interop.wasm_global_set(_global, &v);
}
}
}
private GlobalExport _export;
private IntPtr _global;
}
}