* \brief Helper class for linking modules together with name-based resolution. * * This class is used for easily instantiating `Module`s by defining names into * the linker and performing name-based resolution during instantiation. A * `Linker` can also be used to link in WASI functions to instantiate a module. */
| 26 | * `Linker` can also be used to link in WASI functions to instantiate a module. |
| 27 | */ |
| 28 | class LinkerInstance { |
| 29 | WASMTIME_OWN_WRAPPER(LinkerInstance, wasmtime_component_linker_instance); |
| 30 | |
| 31 | /** |
| 32 | * \brief Adds a module to this linker instance under the specified name. |
| 33 | */ |
| 34 | Result<std::monostate> add_module(std::string_view name, |
| 35 | const Module &module) { |
| 36 | wasmtime_error_t *error = wasmtime_component_linker_instance_add_module( |
| 37 | ptr.get(), name.data(), name.size(), module.capi()); |
| 38 | if (error != nullptr) { |
| 39 | return Error(error); |
| 40 | } |
| 41 | return std::monostate(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * \brief Adds an new instance to this linker instance under the specified |
| 46 | * name. |
| 47 | * |
| 48 | * Note that this `LinkerInstance` can no longer be used until the returned |
| 49 | * `LinkerInstance` is dropped. |
| 50 | */ |
| 51 | Result<LinkerInstance> add_instance(std::string_view name) { |
| 52 | wasmtime_component_linker_instance_t *ret = nullptr; |
| 53 | wasmtime_error_t *error = wasmtime_component_linker_instance_add_instance( |
| 54 | ptr.get(), name.data(), name.size(), &ret); |
| 55 | if (error != nullptr) { |
| 56 | return Error(error); |
| 57 | } |
| 58 | return LinkerInstance(ret); |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | template <typename F> |
| 63 | static wasmtime_error_t * |
| 64 | raw_callback(void *env, wasmtime_context_t *store, |
| 65 | const wasmtime_component_func_type_t *ty_const, |
| 66 | wasmtime_component_val_t *args, size_t nargs, |
| 67 | wasmtime_component_val_t *results, size_t nresults) { |
| 68 | static_assert(alignof(Val) == alignof(wasmtime_component_val_t)); |
| 69 | static_assert(sizeof(Val) == sizeof(wasmtime_component_val_t)); |
| 70 | wasmtime_component_func_type_t *ty = |
| 71 | const_cast<wasmtime_component_func_type_t *>(ty_const); |
| 72 | F *func = reinterpret_cast<F *>(env); |
| 73 | Span<Val> args_span(Val::from_capi(args), nargs); |
| 74 | Span<Val> results_span(Val::from_capi(results), nresults); |
| 75 | Result<std::monostate> result = |
| 76 | (*func)(Store::Context(store), *FuncType::from_capi(&ty), args_span, |
| 77 | results_span); |
| 78 | |
| 79 | if (!result) { |
| 80 | return result.err().capi_release(); |
| 81 | } |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | template <typename F> static void raw_finalize(void *env) { |
no outgoing calls
no test coverage detected