Skip to content

Commit cc339b4

Browse files
committed
wasm test with c++
1 parent a93ee71 commit cc339b4

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

externals/wamr/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ set(WAMR_BUILD_LIB_WASI_THREADS 0)
2828
set(WAMR_BUILD_MINI_LOADER 0)
2929
set(WAMR_BUILD_MODULE_INST_CONTEXT 0)
3030
set(WAMR_BUILD_MULTI_MODULE 0)
31-
set(WAMR_BUILD_REF_TYPES 0)
31+
set(WAMR_BUILD_REF_TYPES 1)
3232
set(WAMR_BUILD_SHARED_MEMORY 0)
3333
set(WAMR_BUILD_SIMD 1)
3434
set(WAMR_BUILD_TAIL_CALL 1)

sources/libcore/wasm.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ extern "C"
2727

2828
void cage_wamr_log(uint32 log_level, const char *file, int line, const char *format, ...)
2929
{
30+
String str;
3031
std::va_list va;
3132
va_start(va, format);
32-
String str;
3333
std::vsnprintf(str.rawData(), str.MaxLength - 100, format, va);
34+
va_end(va);
3435
str.rawLength() = std::strlen(str.rawData());
3536
str = trim(str);
3637
SeverityEnum sev = SeverityEnum::Info;
@@ -256,6 +257,15 @@ namespace cage
256257
data.resize(std::max(sz, countOfWasmVal(returns)));
257258
}
258259
};
260+
261+
template<uint32 A>
262+
void strCopyTruncate(detail::StringBase<A> &dst, const char *src)
263+
{
264+
uint32 len = std::strlen(src);
265+
if (len > A)
266+
len = A;
267+
dst = detail::StringBase<A>(PointerRange<const char>(src, src + len));
268+
}
259269
}
260270

261271
namespace privat
@@ -332,7 +342,7 @@ namespace cage
332342
wasm_runtime_get_import_type(impl->module, i, &t);
333343
WasmSymbol w;
334344
w.moduleName = t.module_name;
335-
w.name = t.name;
345+
strCopyTruncate(w.name, t.name);
336346
w.kind = toString(t.kind);
337347
w.linked = t.linked;
338348
arr.push_back(w);
@@ -351,7 +361,7 @@ namespace cage
351361
wasm_export_t t = {};
352362
wasm_runtime_get_export_type(impl->module, i, &t);
353363
WasmSymbol w;
354-
w.name = t.name;
364+
strCopyTruncate(w.name, t.name);
355365
w.kind = toString(t.kind);
356366
w.linked = true;
357367
arr.push_back(w);

sources/test-core/wasm.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
PointerRange<const uint8> sums_wasm();
88
PointerRange<const uint8> strings_wasm();
99
PointerRange<const uint8> natives_wasm();
10+
PointerRange<const uint8> foo_wasm();
1011

1112
namespace
1213
{
@@ -149,6 +150,30 @@ void testWasm()
149150
}
150151
}
151152

153+
{
154+
CAGE_TESTCASE("foo (c++ objects lifetime)");
155+
Holder<WasmModule> mod = newWasmModule(foo_wasm().cast<const char>());
156+
printModuleDetails(+mod);
157+
Holder<WasmInstance> inst = wasmInstantiate(mod.share());
158+
WasmFunction get_foo = inst->function<sint32()>("get_foo");
159+
WasmFunction set_foo = inst->function<void(sint32)>("set_foo");
160+
WasmFunction get_map = inst->function<sint32(sint32)>("get_map");
161+
WasmFunction set_map = inst->function<void(sint32, sint32)>("set_map");
162+
CAGE_TEST(get_foo() == 2);
163+
set_foo(123);
164+
CAGE_TEST(get_foo() == 123);
165+
CAGE_TEST(get_map(10) == 2); // previously non-existent key
166+
set_map(10, 100);
167+
WasmBuffer tmp = inst->allocate(12'000);
168+
set_map(20, 400);
169+
CAGE_TEST(get_map(10) == 100);
170+
CAGE_TEST(get_map(20) == 400);
171+
set_map(30, 900);
172+
CAGE_TEST(get_map(10) == 100);
173+
CAGE_TEST(get_map(20) == 400);
174+
CAGE_TEST(get_map(30) == 900);
175+
}
176+
152177
{
153178
CAGE_TESTCASE("multiple instantiations of same module");
154179
Holder<WasmModule> mod = newWasmModule(strings_wasm().cast<const char>());

sources/test-core/wasm/compile.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ PATH="$PATH:/c/Program Files/LLVM/bin"
33
clang --target=wasm32 -Wl,--export-all -Wl,--no-entry -nostdlib -o sums.wasm sums.c
44
clang --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostdlib -o strings.wasm strings.c
55
clang --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostdlib -o natives.wasm natives.c
6+
#clang++ --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostdlib -nostartfiles -o foo.wasm foo.cpp
7+
em++ --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostartfiles -O2 -o foo.wasm foo.cpp

sources/test-core/wasm/foo.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <map>
2+
3+
struct Foo
4+
{
5+
int data = 1;
6+
Foo() { data = 2; }
7+
~Foo() { data = 3; }
8+
};
9+
10+
Foo foo;
11+
12+
extern "C"
13+
{
14+
int get_foo(void)
15+
{
16+
return foo.data;
17+
}
18+
19+
void set_foo(int v)
20+
{
21+
foo.data = v;
22+
}
23+
} // extern C
24+
25+
std::map<int, Foo> map;
26+
27+
extern "C"
28+
{
29+
int get_map(int key)
30+
{
31+
return map[key].data;
32+
}
33+
34+
void set_map(int key, int val)
35+
{
36+
map[key].data = val;
37+
}
38+
} // extern C

sources/test-core/wasm/foo.wasm

22.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)