-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwasmtime.cpp
More file actions
214 lines (183 loc) · 8.44 KB
/
Copy pathwasmtime.cpp
File metadata and controls
214 lines (183 loc) · 8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "context.hpp"
#include "lower.hpp"
#include "lift.hpp"
#include "val.hpp"
#include "util.hpp"
#include "flatten.hpp"
#include <doctest/doctest.h>
#include <iostream>
#include <string>
#include <fstream>
#include <functional>
#include <wasmtime.hh>
std::vector<uint8_t> readWasmBinaryToBuffer(const char *filename)
{
std::ifstream file(filename, std::ios::binary);
std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(file), {});
return buffer;
}
wasmtime::Val val2WasmtimeVal(const cmcpp::WasmVal &val)
{
if (std::holds_alternative<int32_t>(val))
return wasmtime::Val(std::get<int32_t>(val));
if (std::holds_alternative<int64_t>(val))
return wasmtime::Val(std::get<int64_t>(val));
if (std::holds_alternative<cmcpp::float32_t>(val))
return wasmtime::Val(std::get<cmcpp::float32_t>(val));
if (std::holds_alternative<cmcpp::float64_t>(val))
return wasmtime::Val(std::get<cmcpp::float64_t>(val));
throw std::runtime_error("Invalid WasmValType");
}
std::vector<wasmtime::Val> vals2WasmtimeVals(const std::vector<cmcpp::WasmVal> &vals)
{
std::vector<wasmtime::Val> retVal;
for (const auto &val : vals)
retVal.push_back(val2WasmtimeVal(val));
return retVal;
}
cmcpp::WasmVal wasmtimeVal2Val(const wasmtime::Val &val)
{
switch (val.kind())
{
case wasmtime::ValKind::I32:
return val.i32();
case wasmtime::ValKind::I64:
return val.i64();
case wasmtime::ValKind::F32:
return val.f32();
case wasmtime::ValKind::F64:
return val.f64();
default:
throw std::runtime_error("Invalid WasmValType");
}
}
std::vector<cmcpp::WasmVal> wasmtimeVals2WasmVals(const std::vector<wasmtime::Val> &vals)
{
std::vector<cmcpp::WasmVal> retVal;
for (const auto &val : vals)
retVal.push_back(wasmtimeVal2Val(val));
return retVal;
}
std::ostream &operator<<(std::ostream &os, const cmcpp::ValType &valType)
{
os << static_cast<int>(valType);
return os;
}
TEST_CASE("generic Val")
{
// auto a = cmcpp::HostVal(std::vector<int>{1, 2, 3});
// auto b = cmcpp::HostVal(std::map<std::string, std::vector<int>>{std::make_pair("a", std::vector<int>{1, 2, 3})});
// // auto b = std::vector<int>{1, 2, 3};
// std::cout << b.type() << std::endl;
}
cmcpp::ListPtr createList()
{
auto list = std::make_shared<cmcpp::List>(cmcpp::ValType::Char);
list->vs.push_back('a');
list->vs.push_back('b');
list->vs.push_back('c');
return list;
}
TEST_CASE("wasmtime")
{
std::cout << "wasmtime" << std::endl;
wasmtime::Engine engine;
wasmtime::Store store(engine);
std::vector<uint8_t> _contents = readWasmBinaryToBuffer("test/wasm/build/wasmtests.wasm");
const wasmtime::Span<uint8_t> &contents = _contents;
auto module = wasmtime::Module::compile(engine, contents).unwrap();
wasmtime::WasiConfig wasi;
wasi.inherit_argv();
wasi.inherit_env();
wasi.inherit_stdin();
wasi.inherit_stdout();
wasi.inherit_stderr();
store.context().set_wasi(std::move(wasi)).unwrap();
wasmtime::Linker linker(engine);
linker.define_wasi().unwrap();
auto dbglog = [&store](wasmtime::Caller caller, uint32_t msg, uint32_t msg_len)
{
auto memory = std::get<wasmtime::Memory>(*caller.get_export("memory"));
auto data = memory.data(store.context());
const char *msg_ptr = reinterpret_cast<const char *>(&data[msg]);
std::string str(msg_ptr, msg_len);
std::cout << str << std::endl;
};
linker.func_wrap("$root", "dbglog", dbglog).unwrap();
auto instance = linker.instantiate(store, module).unwrap();
linker.define_instance(store, "linking2", instance).unwrap();
auto cabi_realloc = std::get<wasmtime::Func>(*instance.get(store, "cabi_realloc"));
std::function<int(int, int, int, int)> realloc = [&store, cabi_realloc](int a, int b, int c, int d) -> int
{
return cabi_realloc.call(store, {a, b, c, d}).unwrap()[0].i32();
};
auto memory = std::get<wasmtime::Memory>(*instance.get(store, "memory"));
store.context().set_data(memory);
wasmtime::Span<uint8_t> data = memory.data(store.context());
auto bool_and = std::get<wasmtime::Func>(*instance.get(store, "bool-and"));
auto list_char_append = std::get<wasmtime::Func>(*instance.get(store, "list-char-append"));
auto list_list_string_append = std::get<wasmtime::Func>(*instance.get(store, "list-list-string-append"));
auto string_append = std::get<wasmtime::Func>(*instance.get(store, "string-append"));
// Actual ABI Test Code --------------------------------------------
cmcpp::ListPtr list = createList();
CHECK(list->vs.size() == 3);
cmcpp::Val v = list;
auto list3 = std::get<cmcpp::ListPtr>(v);
CHECK(list3->vs.size() == 3);
cmcpp::CallContextPtr cx = cmcpp::createCallContext(data, realloc, cmcpp::encodeTo, cmcpp::decodeFrom);
std::vector<wasmtime::Val> wasmtimeVals;
std::vector<cmcpp::Val> cmcppVals;
std::vector<cmcpp::WasmVal> cmcppWasmVals;
wasmtimeVals = vals2WasmtimeVals(cmcpp::lower_values(*cx, {false, false}));
wasmtimeVals = bool_and.call(store, wasmtimeVals).unwrap();
cmcppVals = cmcpp::lift_values(*cx, wasmtimeVals2WasmVals(wasmtimeVals), {cmcpp::ValType::Bool});
CHECK(std::get<bool>(cmcppVals[0]) == false);
wasmtimeVals = vals2WasmtimeVals(cmcpp::lower_values(*cx, {false, true}));
wasmtimeVals = bool_and.call(store, wasmtimeVals).unwrap();
cmcppVals = cmcpp::lift_values(*cx, wasmtimeVals2WasmVals(wasmtimeVals), {cmcpp::ValType::Bool});
CHECK(std::get<bool>(cmcppVals[0]) == false);
wasmtimeVals = vals2WasmtimeVals(cmcpp::lower_values(*cx, {true, false}));
wasmtimeVals = bool_and.call(store, wasmtimeVals).unwrap();
cmcppVals = cmcpp::lift_values(*cx, wasmtimeVals2WasmVals(wasmtimeVals), {cmcpp::ValType::Bool});
CHECK(std::get<bool>(cmcppVals[0]) == false);
wasmtimeVals = vals2WasmtimeVals(cmcpp::lower_values(*cx, {true, true}));
wasmtimeVals = bool_and.call(store, wasmtimeVals).unwrap();
cmcppVals = cmcpp::lift_values(*cx, wasmtimeVals2WasmVals(wasmtimeVals), {cmcpp::ValType::Bool});
CHECK(std::get<bool>(cmcppVals[0]) == true);
auto myVec = std::vector<char>{'a', 'b', 'c'};
auto charList1 = cmcpp::lower_hostVal(*cx, myVec);
auto myVec2 = std::vector<char>{'d', 'e', 'f'};
auto charList2 = cmcpp::lower_hostVal(*cx, myVec2);
// auto charList2 = cmcpp::lower_hostVal(std::vector<char>{'d', 'e', 'f'});
// wasmtimeVals = vals2WasmtimeVals({charList1, charList2});
// wasmtimeVals = list_char_append.call(store, wasmtimeVals).unwrap();
// cmcppWasmVals = wasmtimeVals2WasmVals(wasmtimeVals);
// cmcppVals = cmcpp::lift_values(*cx, cmcppWasmVals, {std::make_pair(cmcpp::ValType::List, cmcpp::ValType::Char)});
// CHECK(std::get<cmcpp::ListPtr>(cmcppVals[0])->vs.size() == 6);
// CHECK(std::get<char>(std::get<cmcpp::ListPtr>(cmcppVals[0])->vs[0]) == 'a');
// CHECK(std::get<char>(std::get<cmcpp::ListPtr>(cmcppVals[0])->vs[3]) == 'd');
// CHECK(std::get<char>(std::get<cmcpp::ListPtr>(cmcppVals[0])->vs[5]) == 'f');
auto lvs = cmcpp::lower_values(*cx, cmcpp::MAX_FLAT_PARAMS, {"1234", "5678"});
auto ret = string_append.call(store, vals2WasmtimeVals(lvs)).unwrap();
auto wret = wasmtimeVals2WasmVals(ret);
cmcppVals = cmcpp::lift_values(*cx, cmcpp::MAX_FLAT_PARAMS, wret, {cmcpp::ValType::String});
auto str = std::get<cmcpp::StringPtr>(cmcppVals[0]);
CHECK(str->to_string().compare("12345678") == 0);
// auto myVec4 = std::vector<const std::string &>{{"xxx1234", "xxxx5678", "xxxxxabcd", "xxxxxxefgh"}};
// auto charListList4 = cmcpp::lower_hostVal(*cx, myVec4);
// lvs = cmcpp::lower_values(*cx, {charListList4});
auto myVec3 = std::vector<std::vector<std::string>>{{"1234", "5678"}, {"abcd", "efgh"}};
auto charListList = cmcpp::lower_hostVal(*cx, myVec3);
lvs = cmcpp::lower_values(*cx, {charListList, charListList});
wasmtimeVals = vals2WasmtimeVals(lvs);
wasmtimeVals = list_list_string_append.call(store, wasmtimeVals).unwrap();
wret = wasmtimeVals2WasmVals(wasmtimeVals);
cmcppVals = cmcpp::lift_values(*cx, wret, {cmcpp::ValType::String});
CHECK(std::get<cmcpp::StringPtr>(cmcppVals[0])->len == 32);
// Actual ABI Test Code --------------------------------------------
}
TEST_CASE("component-model-cpp version")
{
// static_assert(std::string(GREETER_VERSION) == std::string("1.0"));
// CHECK(std::string(GREETER_VERSION) == std::string("1.0"));
}