| 23 | } |
| 24 | |
| 25 | int main(int argc, char *argv[]) { |
| 26 | if (argc != 4) { |
| 27 | fprintf(stderr, "Usage: %s <x> <y> <component.wasm>\n", argv[0]); |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | uint32_t x = (uint32_t)atoi(argv[1]); |
| 32 | uint32_t y = (uint32_t)atoi(argv[2]); |
| 33 | const char *path = argv[3]; |
| 34 | |
| 35 | // 1. Create engine with component model enabled |
| 36 | wasm_config_t *config = wasm_config_new(); |
| 37 | wasmtime_config_wasm_component_model_set(config, true); |
| 38 | wasm_engine_t *engine = wasm_engine_new_with_config(config); |
| 39 | |
| 40 | // 2. Read the component file |
| 41 | FILE *f = fopen(path, "rb"); |
| 42 | if (!f) { |
| 43 | fprintf(stderr, "error: could not open %s\n", path); |
| 44 | return 1; |
| 45 | } |
| 46 | fseek(f, 0, SEEK_END); |
| 47 | long fsize = ftell(f); |
| 48 | fseek(f, 0, SEEK_SET); |
| 49 | uint8_t *wasm_bytes = malloc(fsize); |
| 50 | fread(wasm_bytes, 1, fsize, f); |
| 51 | fclose(f); |
| 52 | |
| 53 | // 3. Compile the component |
| 54 | wasmtime_component_t *component = NULL; |
| 55 | exit_if_error("compile component", |
| 56 | wasmtime_component_new(engine, wasm_bytes, fsize, &component)); |
| 57 | free(wasm_bytes); |
| 58 | |
| 59 | // 4. Create linker and add WASI P2 |
| 60 | wasmtime_component_linker_t *linker = |
| 61 | wasmtime_component_linker_new(engine); |
| 62 | exit_if_error("add WASI to linker", |
| 63 | wasmtime_component_linker_add_wasip2(linker)); |
| 64 | |
| 65 | // 5. Create store with WASI config |
| 66 | wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL); |
| 67 | wasmtime_context_t *context = wasmtime_store_context(store); |
| 68 | exit_if_error("set WASI config", |
| 69 | wasmtime_context_set_wasi(context, wasi_config_new())); |
| 70 | |
| 71 | // 6. Instantiate |
| 72 | wasmtime_component_instance_t instance; |
| 73 | exit_if_error("instantiate component", |
| 74 | wasmtime_component_linker_instantiate(linker, context, component, &instance)); |
| 75 | |
| 76 | // 7. Look up the exported "add" function. |
| 77 | // The export is nested: first find the "docs:adder/add@0.1.0" instance, |
| 78 | // then the "add" function within it. |
| 79 | wasmtime_component_export_index_t *iface_idx = |
| 80 | wasmtime_component_instance_get_export_index( |
| 81 | &instance, context, NULL, |
| 82 | "docs:adder/add@0.1.0", strlen("docs:adder/add@0.1.0")); |
nothing calls this directly
no test coverage detected
searching dependent graphs…