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