Path: blob/main/crates/c-api/tests/component/call_func.cc
1692 views
#include "utils.h"12#include <array>3#include <gtest/gtest.h>4#include <wasmtime.h>56TEST(component, call_func) {7static constexpr auto component_text = std::string_view{8R"END(9(component10(core module $m11(func (export "f") (param $x i32) (param $y i32) (result i32)12(local.get $x)13(local.get $y)14(i32.add)15)16)17(core instance $i (instantiate $m))18(func $f (param "x" u32) (param "y" u32) (result u32) (canon lift (core func $i "f")))19(export "f" (func $f))20)21)END",22};23const auto engine = wasm_engine_new();24EXPECT_NE(engine, nullptr);2526const auto store = wasmtime_store_new(engine, nullptr, nullptr);27const auto context = wasmtime_store_context(store);2829wasmtime_component_t *component = nullptr;3031auto err = wasmtime_component_new(32engine, reinterpret_cast<const uint8_t *>(component_text.data()),33component_text.size(), &component);3435CHECK_ERR(err);3637const auto f =38wasmtime_component_get_export_index(component, nullptr, "f", 1);3940EXPECT_NE(f, nullptr);4142const auto linker = wasmtime_component_linker_new(engine);4344wasmtime_component_instance_t instance = {};45err = wasmtime_component_linker_instantiate(linker, context, component,46&instance);47CHECK_ERR(err);4849wasmtime_component_func_t func = {};50const auto found =51wasmtime_component_instance_get_func(&instance, context, f, &func);52EXPECT_TRUE(found);5354auto params = std::array<wasmtime_component_val_t, 2>{55wasmtime_component_val_t{56.kind = WASMTIME_COMPONENT_U32,57.of = {.u32 = 34},58},59wasmtime_component_val_t{60.kind = WASMTIME_COMPONENT_U32,61.of = {.u32 = 35},62},63};6465auto results = std::array<wasmtime_component_val_t, 1>{};6667err =68wasmtime_component_func_call(&func, context, params.data(), params.size(),69results.data(), results.size());70CHECK_ERR(err);7172err = wasmtime_component_func_post_return(&func, context);73CHECK_ERR(err);7475EXPECT_EQ(results[0].kind, WASMTIME_COMPONENT_U32);76EXPECT_EQ(results[0].of.u32, 69);7778wasmtime_component_export_index_delete(f);79wasmtime_component_linker_delete(linker);8081wasmtime_store_delete(store);82wasm_engine_delete(engine);83}848586