* \brief Representation of a compiled WebAssembly component. */
| 37 | * \brief Representation of a compiled WebAssembly component. |
| 38 | */ |
| 39 | class Component { |
| 40 | WASMTIME_CLONE_WRAPPER(Component, wasmtime_component); |
| 41 | |
| 42 | #ifdef WASMTIME_FEATURE_COMPILER |
| 43 | |
| 44 | #ifdef WASMTIME_FEATURE_WAT |
| 45 | /** |
| 46 | * \brief Compiles a component from the WebAssembly text format. |
| 47 | * |
| 48 | * This function will automatically use `wat2wasm` on the input and then |
| 49 | * delegate to the #compile function. |
| 50 | */ |
| 51 | static Result<Component> compile(Engine &engine, std::string_view wat) { |
| 52 | auto wasm = wat2wasm(wat); |
| 53 | if (!wasm) { |
| 54 | return wasm.err(); |
| 55 | } |
| 56 | auto bytes = wasm.ok(); |
| 57 | return compile(engine, bytes); |
| 58 | } |
| 59 | #endif // WASMTIME_FEATURE_WAT |
| 60 | |
| 61 | /** |
| 62 | * \brief Compiles a component from the WebAssembly binary format. |
| 63 | * |
| 64 | * This function compiles the provided WebAssembly binary specified by `wasm` |
| 65 | * within the compilation settings configured by `engine`. This method is |
| 66 | * synchronous and will not return until the component has finished compiling. |
| 67 | * |
| 68 | * This function can fail if the WebAssembly binary is invalid or doesn't |
| 69 | * validate (or similar). Note that this API does not compile WebAssembly |
| 70 | * modules, which is done with `Module` instead of `Component`. |
| 71 | */ |
| 72 | static Result<Component> compile(Engine &engine, Span<uint8_t> wasm) { |
| 73 | wasmtime_component_t *ret = nullptr; |
| 74 | auto *error = |
| 75 | wasmtime_component_new(engine.capi(), wasm.data(), wasm.size(), &ret); |
| 76 | if (error != nullptr) { |
| 77 | return Error(error); |
| 78 | } |
| 79 | return Component(ret); |
| 80 | } |
| 81 | #endif // WASMTIME_FEATURE_COMPILER |
| 82 | |
| 83 | /** |
| 84 | * \brief Deserializes a previous list of bytes created with `serialize`. |
| 85 | * |
| 86 | * This function is intended to be much faster than `compile` where it uses |
| 87 | * the artifacts of a previous compilation to quickly create an in-memory |
| 88 | * component ready for instantiation. |
| 89 | * |
| 90 | * It is not safe to pass arbitrary input to this function, it is only safe to |
| 91 | * pass in output from previous calls to `serialize`. For more information see |
| 92 | * the Rust documentation - |
| 93 | * https://docs.wasmtime.dev/api/wasmtime/struct.Module.html#method.deserialize |
| 94 | */ |
| 95 | static Result<Component> deserialize(Engine &engine, Span<uint8_t> wasm) { |
| 96 | wasmtime_component_t *ret = nullptr; |
no outgoing calls
no test coverage detected