using System;
using System.Runtime.InteropServices;
namespace Wasmtime
{
///
/// Represents a WebAssembly module.
///
public class Module : IDisposable
{
internal Module(Store store, string name, byte[] bytes)
{
if (store.Handle.IsInvalid)
{
throw new ArgumentNullException(nameof(store));
}
unsafe
{
fixed (byte *ptr = bytes)
{
Interop.wasm_byte_vec_t vec;
vec.size = (UIntPtr)bytes.Length;
vec.data = ptr;
Handle = Interop.wasm_module_new(store.Handle, ref vec);
}
if (Handle.IsInvalid)
{
throw new WasmtimeException($"WebAssembly module '{name}' is not valid.");
}
}
Store = store;
Name = name;
Imports = new Wasmtime.Imports.Imports(this);
Exports = new Wasmtime.Exports.Exports(this);
}
///
/// Instantiates a WebAssembly module for the given host.
///
/// The host to use for the WebAssembly module's instance.
/// Returns a new .
public Instance Instantiate(IHost host = null)
{
return Instantiate(null, host);
}
///
/// Instantiates a WebAssembly module for the given host.
///
/// The WASI instance to use for WASI imports.
/// The host to use for the WebAssembly module's instance.
/// Returns a new .
public Instance Instantiate(Wasi wasi, IHost host = null)
{
if (!(host?.Instance is null))
{
throw new InvalidOperationException("The host has already been associated with an instantiated module.");
}
var instance = new Instance(this, wasi, host);
if (!(host is null))
{
host.Instance = instance;
return instance;
}
return instance;
}
///
/// The associated with the module.
///
public Store Store { get; private set; }
///
/// The name of the module.
///
public string Name { get; private set; }
///
/// The imports of the module.
///
public Wasmtime.Imports.Imports Imports { get; private set; }
///
/// The exports of the module.
///
///
public Wasmtime.Exports.Exports Exports { get; private set; }
///
public void Dispose()
{
if (!Handle.IsInvalid)
{
Handle.Dispose();
Handle.SetHandleAsInvalid();
}
if (!(Imports is null))
{
Imports.Dispose();
Imports = null;
}
}
internal Interop.ModuleHandle Handle { get; private set; }
}
}