Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: move example to README #5

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 80 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,93 @@ cmake ..
make
```

or just:
or:

```bash
make
```

## Example

See [extism-wamr.c](bin/extism-wamr.c)

## Notes
## 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 <stdio.h>
#include <stdlib.h>
#include <extism-wamr.h>

// 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;
}

```

3 changes: 2 additions & 1 deletion bin/extism-wamr.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ int main(int argc, char *argv[]) {
fputs(errbuf, stderr);
fputs("\n", stderr);
free(data);
extism_runtime_cleanup();
return 1;
}

const char *input = argc > 3 ? argv[3] : "";
size_t input_len = argc > 3 ? strlen(argv[3]) : 0;

for (int i = 0; i < loop; i++) {
// Call `count_vowels` function
// Call a function
if ((status = extism_plugin_call(plugin, argv[2], (const void *)input,
input_len)) != ExtismStatusOk) {
// Print error if it fails
Expand Down
Loading