instantiate method

WasmInstance instantiate(
  1. WasmStore store,
  2. WasmModule module
)

Instantiates module in store.

Throws a WasmtimeException for a link error (a missing or mistyped import) and a WasmTrapException when a start function traps.

Implementation

WasmInstance instantiate(WasmStore store, WasmModule module) {
  // The host functions are called for as long as the store lives, so their
  // trampolines move into the store.
  _instantiated = true;
  for (final trampoline in _trampolines) {
    store.retain(trampoline.close);
  }
  return scoped((arena) {
    final out = arena<c.wasmtime_instance_t>();
    final trapOut = arena<ffi.Pointer<c.wasm_trap_t>>();
    clearHostException();
    final error = c.wasmtime_linker_instantiate(
      pointer,
      store.context,
      module.pointer,
      out,
      trapOut,
    );
    if (error != ffi.nullptr || trapOut.value != ffi.nullptr) {
      // A start function that threw is more informative than the trap.
      rethrowHostException();
      throwExecutionFailure(error, trapOut.value);
    }
    return WasmInstance.fromHandle(store, readInstanceHandle(out.ref));
  });
}