Path: blob/main/crates/c-api/tests/component/define_module.cc
1692 views
#include "utils.h"12#include <gtest/gtest.h>3#include <wasmtime.h>45TEST(component, define_module) {6static constexpr auto module_wat = std::string_view{7R"END(8(module9(func $function (param $x i32) (result i32)10local.get $x)11(export "function" (func $function))12)13)END",14};1516static constexpr auto component_text = std::string_view{17R"END(18(component19(import "x:y/z" (instance20(export "mod" (core module21(export "function" (func (param i32) (result i32)))22))23))24)25)END",26};27const auto engine = wasm_engine_new();28EXPECT_NE(engine, nullptr);2930wasm_byte_vec_t wasm;31auto err = wasmtime_wat2wasm(module_wat.data(), module_wat.size(), &wasm);32CHECK_ERR(err);3334wasmtime_module_t *module = nullptr;35err = wasmtime_module_new(36engine, reinterpret_cast<const uint8_t *>(wasm.data), wasm.size, &module);37CHECK_ERR(err);3839const auto store = wasmtime_store_new(engine, nullptr, nullptr);40const auto context = wasmtime_store_context(store);4142wasmtime_component_t *component = nullptr;4344err = wasmtime_component_new(45engine, reinterpret_cast<const uint8_t *>(component_text.data()),46component_text.size(), &component);4748CHECK_ERR(err);4950const auto linker = wasmtime_component_linker_new(engine);5152const auto root = wasmtime_component_linker_root(linker);5354wasmtime_component_linker_instance_t *x_y_z = nullptr;55err = wasmtime_component_linker_instance_add_instance(56root, "x:y/z", strlen("x:y/z"), &x_y_z);57CHECK_ERR(err);5859err = wasmtime_component_linker_instance_add_module(x_y_z, "mod",60strlen("mod"), module);61CHECK_ERR(err);6263wasmtime_component_linker_instance_delete(x_y_z);64wasmtime_component_linker_instance_delete(root);6566wasmtime_component_instance_t instance = {};67err = wasmtime_component_linker_instantiate(linker, context, component,68&instance);69CHECK_ERR(err);7071wasmtime_component_linker_delete(linker);7273wasmtime_store_delete(store);74wasm_engine_delete(engine);75}767778