2121#include < string>
2222
2323#include " include/proxy-wasm/limits.h"
24+ #include " include/proxy-wasm/word.h"
2425
2526#include " crates/c-api/include/wasmtime.hh" // IWYU pragma: keep
2627
@@ -69,6 +70,19 @@ template <typename Arg, typename... Args> std::string printValues(Arg arg, Args.
6970 return printValue (arg) + ((" , " + printValue (args)) + ... + " " );
7071}
7172
73+ template <typename ... Args> void InPlaceConvertWasmToHostEndianness (Args &...args) {
74+ (void (args = wasmtoh (convertWordToUint32 (args), true )), ...);
75+ }
76+ proxy_wasm::Word ConvertWasmToHostEndianness (const proxy_wasm::Word &arg) {
77+ return proxy_wasm::Word (wasmtoh (convertWordToUint32 (arg), true ));
78+ }
79+ template <typename ... Args> void InPlaceConvertHostToWasmEndianness (Args &...args) {
80+ (void (args = htowasm (convertWordToUint32 (args), true )), ...);
81+ }
82+ template <typename ... Args> auto ConvertHostToWasmEndianness (const Args &...args) {
83+ return std::make_tuple ((htowasm (convertWordToUint32 (args), true ))...);
84+ }
85+
7286} // namespace
7387
7488class Wasmtime : public WasmVm {
@@ -280,6 +294,7 @@ void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
280294 module_name, function_name,
281295 [this , function,
282296 function_name = std::string (module_name) + " ." + std::string (function_name)](Args... args) {
297+ InPlaceConvertWasmToHostEndianness (args...);
283298 const bool log = cmpLogLevel (LogLevel::trace);
284299 if (log) {
285300 integration ()->trace (" [vm->host] " + function_name + " (" + printValues (args...) + " )" );
@@ -301,6 +316,7 @@ void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
301316 module_name, function_name,
302317 [this , function,
303318 function_name = std::string (module_name) + " ." + std::string (function_name)](Args... args) {
319+ InPlaceConvertWasmToHostEndianness (args...);
304320 const bool log = cmpLogLevel (LogLevel::trace);
305321 if (log) {
306322 integration ()->trace (" [vm->host] " + function_name + " (" + printValues (args...) + " )" );
@@ -309,7 +325,7 @@ void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
309325 if (log) {
310326 integration ()->trace (" [vm<-host] " + function_name + " return: " + printValue (result));
311327 }
312- return result;
328+ return ConvertHostToWasmEndianness ( result) ;
313329 });
314330 if (!result) {
315331 fail (FailState::ConfigureFailed, " Failed to register host function: " + result.err ().message ());
@@ -337,6 +353,7 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
337353 integration ()->trace (" [host->vm] " + std::string (function_name) + " (" + printValues (args...) +
338354 " )" );
339355 }
356+ InPlaceConvertHostToWasmEndianness (args...);
340357 TrapResult<std::monostate> result = func.call (store_->context (), {args...});
341358 if (!result) {
342359 fail (FailState::RuntimeError,
@@ -369,17 +386,19 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
369386 integration ()->trace (" [host->vm] " + std::string (function_name) + " (" + printValues (args...) +
370387 " )" );
371388 }
389+ InPlaceConvertHostToWasmEndianness (args...);
372390 TrapResult<R> result = func.call (store_->context (), {args...});
373391 if (!result) {
374392 fail (FailState::RuntimeError,
375393 " Function: " + std::string (function_name) + " failed: " + result.err ().message ());
376394 return R{};
377395 }
396+ R result_host = ConvertWasmToHostEndianness (result.ok ());
378397 if (log) {
379398 integration ()->trace (" [host<-vm] " + std::string (function_name) +
380- " return: " + printValue (result. unwrap () ));
399+ " return: " + printValue (result_host ));
381400 }
382- return result. ok () ;
401+ return result_host ;
383402 };
384403};
385404
0 commit comments