You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.7 KiB

/*
Example of compiling, instantiating, and linking two WebAssembly modules
together.
You can compile and run this example on Linux with:
cargo build --release -p wasmtime-c-api
cc examples/linking.c \
-I crates/c-api/include \
target/release/libwasmtime.a \
-lpthread -ldl -lm \
-o linking
./linking
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations.
You can also build using cmake:
mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-linking
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <wasi.h>
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
#include <wasm.h>
#include <wasmtime.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
static void exit_with_error(const char *message, wasmtime_error_t *error,
wasm_trap_t *trap);
static void read_wat_file(wasm_engine_t *engine, wasm_byte_vec_t *bytes,
const char *file);
int main() {
// Set up our context
wasm_engine_t *engine = wasm_engine_new();
assert(engine != NULL);
wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
assert(store != NULL);
wasmtime_context_t *context = wasmtime_store_context(store);
wasm_byte_vec_t linking1_wasm, linking2_wasm;
read_wat_file(engine, &linking1_wasm, "examples/linking1.wat");
read_wat_file(engine, &linking2_wasm, "examples/linking2.wat");
// Compile our two modules
wasmtime_error_t *error;
wasmtime_module_t *linking1_module = NULL;
wasmtime_module_t *linking2_module = NULL;
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
error = wasmtime_module_new(engine, (uint8_t *)linking1_wasm.data,
linking1_wasm.size, &linking1_module);
if (error != NULL)
exit_with_error("failed to compile linking1", error, NULL);
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
error = wasmtime_module_new(engine, (uint8_t *)linking2_wasm.data,
linking2_wasm.size, &linking2_module);
if (error != NULL)
exit_with_error("failed to compile linking2", error, NULL);
wasm_byte_vec_delete(&linking1_wasm);
wasm_byte_vec_delete(&linking2_wasm);
// Configure WASI and store it within our `wasmtime_store_t`
wasi_config_t *wasi_config = wasi_config_new();
assert(wasi_config);
wasi_config_inherit_argv(wasi_config);
wasi_config_inherit_env(wasi_config);
wasi_config_inherit_stdin(wasi_config);
wasi_config_inherit_stdout(wasi_config);
wasi_config_inherit_stderr(wasi_config);
wasm_trap_t *trap = NULL;
error = wasmtime_context_set_wasi(context, wasi_config);
if (error != NULL)
exit_with_error("failed to instantiate wasi", NULL, trap);
// Create our linker which will be linking our modules together, and then add
// our WASI instance to it.
wasmtime_linker_t *linker = wasmtime_linker_new(engine);
error = wasmtime_linker_define_wasi(linker);
if (error != NULL)
exit_with_error("failed to link wasi", error, NULL);
// Instantiate `linking2` with our linker.
wasmtime_instance_t linking2;
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
error = wasmtime_linker_instantiate(linker, context, linking2_module,
&linking2, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to instantiate linking2", error, trap);
// Register our new `linking2` instance with the linker
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
error = wasmtime_linker_define_instance(linker, context, "linking2",
strlen("linking2"), &linking2);
if (error != NULL)
exit_with_error("failed to link linking2", error, NULL);
// Instantiate `linking1` with the linker now that `linking2` is defined
wasmtime_instance_t linking1;
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
error = wasmtime_linker_instantiate(linker, context, linking1_module,
&linking1, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to instantiate linking1", error, trap);
// Lookup our `run` export function
wasmtime_extern_t run;
bool ok = wasmtime_instance_export_get(context, &linking1, "run", 3, &run);
assert(ok);
assert(run.kind == WASMTIME_EXTERN_FUNC);
error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call run", error, trap);
// Clean up after ourselves at this point
wasmtime_linker_delete(linker);
wasmtime_module_delete(linking1_module);
wasmtime_module_delete(linking2_module);
wasmtime_store_delete(store);
wasm_engine_delete(engine);
return 0;
}
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
static void read_wat_file(wasm_engine_t *engine, wasm_byte_vec_t *bytes,
const char *filename) {
wasm_byte_vec_t wat;
// Load our input file to parse it next
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
FILE *file = fopen(filename, "r");
if (!file) {
printf("> Error loading file!\n");
exit(1);
}
fseek(file, 0L, SEEK_END);
size_t file_size = ftell(file);
wasm_byte_vec_new_uninitialized(&wat, file_size);
fseek(file, 0L, SEEK_SET);
if (fread(wat.data, file_size, 1, file) != 1) {
printf("> Error loading module!\n");
exit(1);
}
fclose(file);
// Parse the wat into the binary wasm format
wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, bytes);
if (error != NULL)
exit_with_error("failed to parse wat", error, NULL);
wasm_byte_vec_delete(&wat);
}
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
static void exit_with_error(const char *message, wasmtime_error_t *error,
wasm_trap_t *trap) {
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
} else {
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
add clang format (#7601) * add clang-format We chose WebKit style because out of all the builtin styles it seems the closest to what already exists in wasmtime. Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: don&#39;t reorder headers The order here matters Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * c-api: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fiber: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * runtime: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: apply clang format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * tests: apply clang-format Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * ci: add clang-format checks Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * clang-format: keep braces on the same line This is more the existing style Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * remove clang-format Just use the tool defaults (LLVM) Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * Fix ci name Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat a couple of comments prtest:full Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting for doc-wasm.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * manually reformat wasmtime.h Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * disable formatting To prevent a link from being broken Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * examples: fixing build commands Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; * fix parameter comment Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt; --------- Signed-off-by: Tyler Rockwood &lt;rockwood@redpanda.com&gt;
11 months ago
fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}