Skip to content

Commit

Permalink
wasm test with c++
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Jan 21, 2025
1 parent a93ee71 commit cc339b4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion externals/wamr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ set(WAMR_BUILD_LIB_WASI_THREADS 0)
set(WAMR_BUILD_MINI_LOADER 0)
set(WAMR_BUILD_MODULE_INST_CONTEXT 0)
set(WAMR_BUILD_MULTI_MODULE 0)
set(WAMR_BUILD_REF_TYPES 0)
set(WAMR_BUILD_REF_TYPES 1)
set(WAMR_BUILD_SHARED_MEMORY 0)
set(WAMR_BUILD_SIMD 1)
set(WAMR_BUILD_TAIL_CALL 1)
Expand Down
16 changes: 13 additions & 3 deletions sources/libcore/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ extern "C"

void cage_wamr_log(uint32 log_level, const char *file, int line, const char *format, ...)
{
String str;
std::va_list va;
va_start(va, format);
String str;
std::vsnprintf(str.rawData(), str.MaxLength - 100, format, va);
va_end(va);
str.rawLength() = std::strlen(str.rawData());
str = trim(str);
SeverityEnum sev = SeverityEnum::Info;
Expand Down Expand Up @@ -256,6 +257,15 @@ namespace cage
data.resize(std::max(sz, countOfWasmVal(returns)));
}
};

template<uint32 A>
void strCopyTruncate(detail::StringBase<A> &dst, const char *src)
{
uint32 len = std::strlen(src);
if (len > A)
len = A;
dst = detail::StringBase<A>(PointerRange<const char>(src, src + len));
}
}

namespace privat
Expand Down Expand Up @@ -332,7 +342,7 @@ namespace cage
wasm_runtime_get_import_type(impl->module, i, &t);
WasmSymbol w;
w.moduleName = t.module_name;
w.name = t.name;
strCopyTruncate(w.name, t.name);
w.kind = toString(t.kind);
w.linked = t.linked;
arr.push_back(w);
Expand All @@ -351,7 +361,7 @@ namespace cage
wasm_export_t t = {};
wasm_runtime_get_export_type(impl->module, i, &t);
WasmSymbol w;
w.name = t.name;
strCopyTruncate(w.name, t.name);
w.kind = toString(t.kind);
w.linked = true;
arr.push_back(w);
Expand Down
25 changes: 25 additions & 0 deletions sources/test-core/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
PointerRange<const uint8> sums_wasm();
PointerRange<const uint8> strings_wasm();
PointerRange<const uint8> natives_wasm();
PointerRange<const uint8> foo_wasm();

namespace
{
Expand Down Expand Up @@ -149,6 +150,30 @@ void testWasm()
}
}

{
CAGE_TESTCASE("foo (c++ objects lifetime)");
Holder<WasmModule> mod = newWasmModule(foo_wasm().cast<const char>());
printModuleDetails(+mod);
Holder<WasmInstance> inst = wasmInstantiate(mod.share());
WasmFunction get_foo = inst->function<sint32()>("get_foo");
WasmFunction set_foo = inst->function<void(sint32)>("set_foo");
WasmFunction get_map = inst->function<sint32(sint32)>("get_map");
WasmFunction set_map = inst->function<void(sint32, sint32)>("set_map");
CAGE_TEST(get_foo() == 2);
set_foo(123);
CAGE_TEST(get_foo() == 123);
CAGE_TEST(get_map(10) == 2); // previously non-existent key
set_map(10, 100);
WasmBuffer tmp = inst->allocate(12'000);
set_map(20, 400);
CAGE_TEST(get_map(10) == 100);
CAGE_TEST(get_map(20) == 400);
set_map(30, 900);
CAGE_TEST(get_map(10) == 100);
CAGE_TEST(get_map(20) == 400);
CAGE_TEST(get_map(30) == 900);
}

{
CAGE_TESTCASE("multiple instantiations of same module");
Holder<WasmModule> mod = newWasmModule(strings_wasm().cast<const char>());
Expand Down
2 changes: 2 additions & 0 deletions sources/test-core/wasm/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ PATH="$PATH:/c/Program Files/LLVM/bin"
clang --target=wasm32 -Wl,--export-all -Wl,--no-entry -nostdlib -o sums.wasm sums.c
clang --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostdlib -o strings.wasm strings.c
clang --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostdlib -o natives.wasm natives.c
#clang++ --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostdlib -nostartfiles -o foo.wasm foo.cpp
em++ --target=wasm32 -Wl,--allow-undefined -Wl,--export-all -Wl,--no-entry -nostartfiles -O2 -o foo.wasm foo.cpp
38 changes: 38 additions & 0 deletions sources/test-core/wasm/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <map>

struct Foo
{
int data = 1;
Foo() { data = 2; }
~Foo() { data = 3; }
};

Foo foo;

extern "C"
{
int get_foo(void)
{
return foo.data;
}

void set_foo(int v)
{
foo.data = v;
}
} // extern C

std::map<int, Foo> map;

extern "C"
{
int get_map(int key)
{
return map[key].data;
}

void set_map(int key, int val)
{
map[key].data = val;
}
} // extern C
Binary file added sources/test-core/wasm/foo.wasm
Binary file not shown.

0 comments on commit cc339b4

Please sign in to comment.