wasmer-c-api
This crate exposes a C and a C++ API for the Wasmer runtime. It also fully supports the wasm-c-api common API.
Once you install Wasmer in your system, the shared object files and the headers will be automatically available inside the Wasmer installed path.
The C (wasmer.h
) and C++ (wasmer.hh
) header
files can be found in the Wasmer include
directory:
bash
wasmer config --includedir
The runtime shared libraries (.so
, .dylib
, .dll
) can be found in the Wasmer
lib
directory:
bash
wasmer config --libdir
Note: You can also download the libraries or header files directly from [Wasmer release page].
The full C API documentation can be found here: https://wasmerio.github.io/wasmer/c-api/
Here is a simple example to use the C API:
```c
int main()
{
// Read the Wasm file bytes.
FILE *file = fopen("sum.wasm", "r");
fseek(file, 0, SEEKEND);
long len = ftell(file);
uint8t *bytes = malloc(len);
fseek(file, 0, SEEKSET);
fread(bytes, 1, len, file);
fclose(file);
// Prepare the imports.
wasmerimportt imports[] = {};
// Instantiate!
wasmerinstancet *instance = NULL;
wasmerresultt instantiationresult = wasmerinstantiate(&instance, bytes, len, imports, 0);
assert(instantiationresult == WASMEROK);
// Let's call a function.
// Start by preparing the arguments.
// Value of argument #1 is 7i32
.
wasmervaluet argumentone;
argumentone.tag = WASMI32;
argumentone.value.I32 = 7;
// Value of argument #2 is 8i32
.
wasmervaluet argumenttwo;
argumenttwo.tag = WASMI32;
argumenttwo.value.I32 = 8;
// Prepare the arguments.
wasmervaluet arguments[] = {argumentone, argumenttwo};
// Prepare the return value.
wasmervaluet resultone;
wasmervaluet results[] = {resultone};
// Call the sum
function with the prepared arguments and the return value.
wasmerresultt callresult = wasmerinstancecall(instance, "sum", arguments, 2, results, 1);
// Let's display the result.
printf("Call result: %d\n", callresult);
printf("Result: %d\n", results[0].value.I32);
// sum(7, 8) == 15
.
assert(results[0].value.I32 == 15);
assert(callresult == WASMEROK);
wasmerinstance_destroy(instance);
return 0;
}
```
Tests are run using the release build of the library. If you make changes or compile with non-default features, please ensure you rebuild in release mode for the tests to see the changes.
The tests can be run via cargo test
, such as:
sh
$ cargo test --release -- --nocapture
To run tests manually, enter the lib/c-api/tests
directory
and run the following commands:
sh
$ cmake .
$ make
$ make test
The Wasmer binary ships with an utility tool that outputs config
in the pkg-config
format.
You can use it like:
bash
wasmer config --pkg-config > $PKG_CONFIG_PATH/wasmer.pc
Wasmer is primarily distributed under the terms of the MIT license (LICENSE).