1 #include "utils.h" 2 3 #include <gtest/gtest.h> 4 #include <wasmtime.h> 5 6 TEST(component, define_module) { 7 static constexpr auto module_wat = std::string_view{ 8 R"END( 9 (module 10 (func $function (param $x i32) (result i32) 11 local.get $x) 12 (export "function" (func $function)) 13 ) 14 )END", 15 }; 16 17 static constexpr auto component_text = std::string_view{ 18 R"END( 19 (component 20 (import "x:y/z" (instance 21 (export "mod" (core module 22 (export "function" (func (param i32) (result i32))) 23 )) 24 )) 25 ) 26 )END", 27 }; 28 const auto engine = wasm_engine_new(); 29 EXPECT_NE(engine, nullptr); 30 31 wasm_byte_vec_t wasm; 32 auto err = wasmtime_wat2wasm(module_wat.data(), module_wat.size(), &wasm); 33 CHECK_ERR(err); 34 35 wasmtime_module_t *module = nullptr; 36 err = wasmtime_module_new( 37 engine, reinterpret_cast<const uint8_t *>(wasm.data), wasm.size, &module); 38 CHECK_ERR(err); 39 wasm_byte_vec_delete(&wasm); 40 41 const auto store = wasmtime_store_new(engine, nullptr, nullptr); 42 const auto context = wasmtime_store_context(store); 43 44 wasmtime_component_t *component = nullptr; 45 46 err = wasmtime_component_new( 47 engine, reinterpret_cast<const uint8_t *>(component_text.data()), 48 component_text.size(), &component); 49 50 CHECK_ERR(err); 51 52 const auto linker = wasmtime_component_linker_new(engine); 53 54 const auto root = wasmtime_component_linker_root(linker); 55 56 wasmtime_component_linker_instance_t *x_y_z = nullptr; 57 err = wasmtime_component_linker_instance_add_instance( 58 root, "x:y/z", strlen("x:y/z"), &x_y_z); 59 CHECK_ERR(err); 60 61 err = wasmtime_component_linker_instance_add_module(x_y_z, "mod", 62 strlen("mod"), module); 63 CHECK_ERR(err); 64 65 wasmtime_component_linker_instance_delete(x_y_z); 66 wasmtime_component_linker_instance_delete(root); 67 68 wasmtime_component_instance_t instance = {}; 69 err = wasmtime_component_linker_instantiate(linker, context, component, 70 &instance); 71 CHECK_ERR(err); 72 73 wasmtime_component_linker_delete(linker); 74 wasmtime_component_delete(component); 75 wasmtime_module_delete(module); 76 77 wasmtime_store_delete(store); 78 wasm_engine_delete(engine); 79 } 80