1 #include "utils.h" 2 3 #include <gtest/gtest.h> 4 #include <wasmtime.h> 5 #include <wasmtime/component/component.hh> 6 #include <wasmtime/module.hh> 7 #include <wasmtime/store.hh> 8 9 using namespace wasmtime; 10 using namespace wasmtime::component; 11 12 TEST(component, define_module) { 13 static constexpr auto module_wat = std::string_view{ 14 R"END( 15 (module 16 (func $function (param $x i32) (result i32) 17 local.get $x) 18 (export "function" (func $function)) 19 ) 20 )END", 21 }; 22 23 static constexpr auto component_text = std::string_view{ 24 R"END( 25 (component 26 (import "x:y/z" (instance 27 (export "mod" (core module 28 (export "function" (func (param i32) (result i32))) 29 )) 30 )) 31 ) 32 )END", 33 }; 34 35 Engine engine; 36 Module module = Module::compile(engine, module_wat).unwrap(); 37 Store store(engine); 38 auto context = store.context(); 39 40 Component component = Component::compile(engine, component_text).unwrap(); 41 42 const auto linker = wasmtime_component_linker_new(engine.capi()); 43 44 const auto root = wasmtime_component_linker_root(linker); 45 46 wasmtime_component_linker_instance_t *x_y_z = nullptr; 47 auto err = wasmtime_component_linker_instance_add_instance( 48 root, "x:y/z", strlen("x:y/z"), &x_y_z); 49 CHECK_ERR(err); 50 51 err = wasmtime_component_linker_instance_add_module( 52 x_y_z, "mod", strlen("mod"), module.capi()); 53 CHECK_ERR(err); 54 55 wasmtime_component_linker_instance_delete(x_y_z); 56 wasmtime_component_linker_instance_delete(root); 57 58 wasmtime_component_instance_t instance = {}; 59 err = wasmtime_component_linker_instantiate(linker, context.capi(), 60 component.capi(), &instance); 61 CHECK_ERR(err); 62 63 wasmtime_component_linker_delete(linker); 64 } 65