Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/extism/wamr-sdk
A lightweight WAMR backend for Extism
https://github.com/extism/wamr-sdk
extism iot plugins runtime wamr wasm webassembly
Last synced: about 21 hours ago
JSON representation
A lightweight WAMR backend for Extism
- Host: GitHub
- URL: https://github.com/extism/wamr-sdk
- Owner: extism
- License: bsd-3-clause
- Created: 2024-05-06T21:43:48.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-08-09T02:02:35.000Z (3 months ago)
- Last Synced: 2024-08-10T01:16:50.853Z (3 months ago)
- Topics: extism, iot, plugins, runtime, wamr, wasm, webassembly
- Language: C
- Homepage: https://extism.org
- Size: 188 KB
- Stars: 18
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WAMR SDK
A lightweight [WAMR](https://github.com/bytecodealliance/wasm-micro-runtime) backend for Extism
Supports a limited set of features compared to [extism/extism](https://github.com/extism/extism):
- Host functions
- Memory limitsIn progress:
- Linking multiple Wasm modules
## Building
Requires:
- CMake
- C compiler```bash
mkdir build
cd build
cmake ..
make
```or:
```bash
make
```## Getting started
- `extism_runtime_init` should always be called before creating any plugins, and there
is only a single global runtime that host functions can be loaded into
- The plugins listed in `ExtismManifest` that depend on other Wasm modules must have all
dependencies listed first in the manifest with module names specified.### Creating and calling a plugin
```c
#include
#include
#include// Read a file from disk - an implementation of `read_file` can be found
// in `bin/extism-wamr.c`
uint8_t *read_file(const char *, size_t *);// Return the input as-is
uint64_t host_reflect(ExtismExecEnv *env, uint64_t x) { return x; }// Run an Extism plugin and print the output
ExtismStatus run_wasm(const char *wasm_file, const char *func_name, const char *input, size_t input_len){
char errbuf[1024];
size_t datalen = 0, len = 0;
uint8_t *data = read_file(wasm_file, &datalen);
if (data == NULL) {
return ExtismStatusErr;
}// Initialize the runtime, this must be done before anything else
extism_runtime_init();// Specify the modules to be loaded, setting `name` to `NULL` marks a module
// at the main module
ExtismWasm wasm = {
.data = data,
.length = datalen,
.name = NULL,
};
ExtismManifest manifest;
extism_manifest_init(&manifest, &wasm, 1, NULL, 0, NULL);// Define a host function
extism_host_function("extism:host/user", "host_reflect", "(I)I", host_reflect,
NULL);// Create the plugin instance
ExtismPlugin *plugin = extism_plugin_new(&manifest, errbuf, 1024);
if (plugin == NULL) {
fputs("ERROR: ", stderr);
fputs(errbuf, stderr);
fputs("\n", stderr);
free(data);
extism_runtime_cleanup();
return ExtismStatusErr;
}// Call `func_name`
if ((status = extism_plugin_call(plugin, func_name, (const void *)input,
input_len)) != ExtismStatusOk) {
// Print error if it fails
const char *s = extism_plugin_error(plugin, &len);
fprintf(stderr, "ERROR(%d): ", status);
fwrite(s, len, 1, stderr);
fputc('\n', stderr);
} else {
// Otherwise print the output
uint8_t *output = extism_plugin_output(plugin, &len);
if (len > 0) {
fwrite(output, len, 1, stdout);
fputc('\n', stdout);
}
}// Cleanup
extism_plugin_free(plugin);
extism_runtime_cleanup();
free(data);
return ExtismStatusOk;
}```