[−][src]Crate wasmer_runtime_c_api
Wasmer Runtime C API
Wasmer is a standalone JIT WebAssembly runtime, aiming to be fully compatible with Emscripten, Rust and Go. Learn more.
This crate exposes a C and C++ API for the Wasmer runtime.
Usage
The C and C++ header files can be found in the source tree of this
crate, respectively wasmer.h
and
wasmer.hh
. They are automatically generated, and always
up-to-date in this repository.
Here is a simple example to use the C API:
#include <stdio.h>
#include "wasmer.h"
#include <assert.h>
#include <stdint.h>
int main()
{
// Read the Wasm file bytes.
FILE *file = fopen("sum.wasm", "r");
fseek(file, 0, SEEK_END);
long len = ftell(file);
uint8_t *bytes = malloc(len);
fseek(file, 0, SEEK_SET);
fread(bytes, 1, len, file);
fclose(file);
// Prepare the imports.
wasmer_import_t imports[] = {};
// Instantiate!
wasmer_instance_t *instance = NULL;
wasmer_result_t instantiation_result = wasmer_instantiate(&instance, bytes, len, imports, 0);
assert(instantiation_result == WASMER_OK);
// Let's call a function.
// Start by preparing the arguments.
// Value of argument #1 is `7i32`.
wasmer_value_t argument_one;
argument_one.tag = WASM_I32;
argument_one.value.I32 = 7;
// Value of argument #2 is `8i32`.
wasmer_value_t argument_two;
argument_two.tag = WASM_I32;
argument_two.value.I32 = 8;
// Prepare the arguments.
wasmer_value_t arguments[] = {argument_one, argument_two};
// Prepare the return value.
wasmer_value_t result_one;
wasmer_value_t results[] = {result_one};
// Call the `sum` function with the prepared arguments and the return value.
wasmer_result_t call_result = wasmer_instance_call(instance, "sum", arguments, 2, results, 1);
// Let's display the result.
printf("Call result: %d\n", call_result);
printf("Result: %d\n", results[0].value.I32);
// `sum(7, 8) == 15`.
assert(results[0].value.I32 == 15);
assert(call_result == WASMER_OK);
wasmer_instance_destroy(instance);
return 0;
}
Modules
error | Read runtime errors. |
export | Create, read, destroy export definitions (function, global, memory and table) on an instance. |
global | Create, set, get and destroy global variables of an instance. |
import | Create, read, destroy import definitions (function, global, memory and table) on an instance. |
instance | Instantiate a module, call functions, and read exports. |
memory | Create, read, write, grow, destroy memory of an instance. |
module | Compile, validate, instantiate, serialize, and destroy modules. |
table | Create, grow, destroy tables of an instance. |
trampoline | Trampoline emitter for transforming function calls. |
value | Create and map Rust to WebAssembly values. |
Structs
wasmer_byte_array | |
wasmer_limit_option_t | The |
wasmer_limits_t | The |
Enums
wasmer_result_t | The |