Skip to content
This repository was archived by the owner on Apr 16, 2025. It is now read-only.

Commit 40fc6f8

Browse files
authored
expose wasmtime_linker_define_func for Linker class (#16)
1 parent efdb2f0 commit 40fc6f8

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

include/wasmtime.hh

+20
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ public:
843843
*/
844844
class FuncType {
845845
friend class Func;
846+
friend class Linker;
846847

847848
struct deleter {
848849
void operator()(wasm_functype_t *p) const { wasm_functype_delete(p); }
@@ -2067,6 +2068,7 @@ inline Store::Context::Context(Caller *caller) : Context(*caller) {}
20672068
class Func {
20682069
friend class Val;
20692070
friend class Instance;
2071+
friend class Linker;
20702072

20712073
wasmtime_func_t func;
20722074

@@ -2684,6 +2686,24 @@ public:
26842686
return std::nullopt;
26852687
}
26862688

2689+
/// Defines a new function in this linker
2690+
template <typename F>
2691+
[[nodiscard]] Result<std::monostate> define_func(std::string_view module,
2692+
std::string_view name,
2693+
const FuncType &ty, F f) {
2694+
2695+
auto *error = wasmtime_linker_define_func(
2696+
ptr.get(), module.data(), module.length(), name.data(), name.length(),
2697+
ty.ptr.get(), Func::raw_callback<F>, std::make_unique<F>(f).release(),
2698+
Func::raw_finalize<F>);
2699+
2700+
if (error != nullptr) {
2701+
return Error(error);
2702+
}
2703+
2704+
return std::monostate();
2705+
}
2706+
26872707
/// Loads the "default" function, according to WASI commands and reactors, of
26882708
/// the module named `name` in this linker.
26892709
[[nodiscard]] Result<Func> get_default(Store::Context cx,

tests/simple.cc

+7-1
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,20 @@ TEST(Linker, Smoke) {
294294
Global g = unwrap(Global::create(store, GlobalType(ValKind::I32, false), 1));
295295
unwrap(linker.define("a", "g", g));
296296
unwrap(linker.define_wasi());
297-
297+
unwrap(linker.define_func(
298+
"a", "f", FuncType({}, {}),
299+
[](auto caller, auto params, auto results) -> auto {
300+
return std::monostate();
301+
}));
298302
Module mod = unwrap(Module::compile(engine, "(module)"));
299303
Instance i = unwrap(Instance::create(store, mod, {}));
300304
unwrap(linker.define_instance(store, "x", i));
301305
unwrap(linker.instantiate(store, mod));
302306
unwrap(linker.module(store, "y", mod));
303307
EXPECT_TRUE(linker.get(store, "a", "g"));
304308
unwrap(linker.get_default(store, "g"));
309+
EXPECT_TRUE(linker.get(store, "a", "f"));
310+
EXPECT_TRUE(std::holds_alternative<Func>(*linker.get(store, "a", "f")));
305311
}
306312

307313
TEST(Caller, Smoke) {

0 commit comments

Comments
 (0)