Module wasmer_c_api::wasm_c_api::instance

source ·
Expand description

A WebAssembly instance is a stateful, executable instance of a WebAssembly module.

Instance objects contain all the exported WebAssembly functions, memories, tables and globals that allow interacting with WebAssembly.

Entry points: A WebAssembly instance is created with wasm_instance_new and freed with wasm_instance_delete.

§Example

The simplest way to instantiate a Wasm module is the following:

int main() {
    // Create the engine and the store.
    wasm_engine_t* engine = wasm_engine_new();
    wasm_store_t* store = wasm_store_new(engine);

    // Create a WebAssembly module from a WAT definition.
    wasm_byte_vec_t wat;
    wasmer_byte_vec_new_from_string(&wat, "(module)");
    wasm_byte_vec_t wasm;
    wat2wasm(&wat, &wasm);

    // Create the module.
    wasm_module_t* module = wasm_module_new(store, &wasm);
    assert(module);

    // Instantiate the module.
    wasm_extern_vec_t imports = WASM_EMPTY_VEC;
    wasm_trap_t* trap = NULL;

    wasm_instance_t* instance = wasm_instance_new(store, module, &imports, &trap);
    assert(instance);

    // Now do something with the instance, like calling the
    // exports with `wasm_instance_exports`.

    // Free everything.
    wasm_instance_delete(instance);
    wasm_module_delete(module);
    wasm_byte_vec_delete(&wasm);
    wasm_byte_vec_delete(&wat);
    wasm_store_delete(store);
    wasm_engine_delete(engine);

    return 0;
}

cbindgen:ignore

Structs§

Functions§