| 33 | } |
| 34 | |
| 35 | int serialize(wasm_byte_vec_t *buffer) { |
| 36 | // Set up our compilation context. Note that we could also work with a |
| 37 | // `wasm_config_t` here to configure what feature are enabled and various |
| 38 | // compilation settings. |
| 39 | printf("Initializing...\n"); |
| 40 | wasm_engine_t *engine = wasm_engine_new(); |
| 41 | assert(engine != NULL); |
| 42 | |
| 43 | // Read our input file, which in this case is a wasm text file. |
| 44 | FILE *file = fopen("examples/hello.wat", "r"); |
| 45 | assert(file != NULL); |
| 46 | fseek(file, 0L, SEEK_END); |
| 47 | size_t file_size = ftell(file); |
| 48 | fseek(file, 0L, SEEK_SET); |
| 49 | wasm_byte_vec_t wat; |
| 50 | wasm_byte_vec_new_uninitialized(&wat, file_size); |
| 51 | if (fread(wat.data, file_size, 1, file) != 1) { |
| 52 | printf("> Error loading module!\n"); |
| 53 | return 1; |
| 54 | } |
| 55 | fclose(file); |
| 56 | |
| 57 | // Parse the wat into the binary wasm format |
| 58 | wasm_byte_vec_t wasm; |
| 59 | wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm); |
| 60 | if (error != NULL) |
| 61 | exit_with_error("failed to parse wat", error, NULL); |
| 62 | wasm_byte_vec_delete(&wat); |
| 63 | |
| 64 | // Now that we've got our binary webassembly we can compile our module |
| 65 | // and serialize into buffer. |
| 66 | printf("Compiling and serializing module...\n"); |
| 67 | wasmtime_module_t *module = NULL; |
| 68 | error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module); |
| 69 | wasm_byte_vec_delete(&wasm); |
| 70 | if (error != NULL) |
| 71 | exit_with_error("failed to compile module", error, NULL); |
| 72 | error = wasmtime_module_serialize(module, buffer); |
| 73 | wasmtime_module_delete(module); |
| 74 | if (error != NULL) |
| 75 | exit_with_error("failed to serialize module", error, NULL); |
| 76 | |
| 77 | printf("Serialized.\n"); |
| 78 | |
| 79 | wasm_engine_delete(engine); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int deserialize(wasm_byte_vec_t *buffer) { |
| 84 | // Set up our compilation context. Note that we could also work with a |
no test coverage detected