1 #include <array>
2 #include <gtest/gtest.h>
3 #include <wasmtime/component.hh>
4 #include <wasmtime/store.hh>
5 
6 using namespace wasmtime::component;
7 
TEST(component,call_func)8 TEST(component, call_func) {
9   static constexpr auto component_text = std::string_view{
10       R"END(
11 (component
12     (core module $m
13         (func (export "f") (param $x i32) (param $y i32) (result i32)
14             (local.get $x)
15             (local.get $y)
16             (i32.add)
17         )
18     )
19     (core instance $i (instantiate $m))
20     (func $f (param "x" u32) (param "y" u32) (result u32) (canon lift (core func $i "f")))
21     (export "f" (func $f))
22 )
23       )END",
24   };
25 
26   wasmtime::Engine engine;
27   wasmtime::Store store(engine);
28   auto context = store.context();
29   auto component = Component::compile(engine, component_text).unwrap();
30   auto f = *component.export_index(nullptr, "f");
31 
32   Linker linker(engine);
33 
34   auto instance = linker.instantiate(context, component).unwrap();
35   auto func = *instance.get_func(context, f);
36 
37   auto params = std::array<Val, 2>{
38       uint32_t(34),
39       uint32_t(35),
40   };
41 
42   auto results = std::array<Val, 1>{false};
43 
44   func.call(context, params, results).unwrap();
45 
46   func.post_return(context).unwrap();
47 
48   EXPECT_TRUE(results[0].is_u32());
49   EXPECT_EQ(results[0].get_u32(), 69);
50 }
51