2424#include < variant>
2525
2626#include " include/proxy-wasm/limits.h"
27+ #include " include/proxy-wasm/word.h"
2728
2829#include " crates/c-api/include/wasmtime.hh" // IWYU pragma: keep
2930
@@ -72,6 +73,19 @@ template <typename Arg, typename... Args> std::string printValues(Arg arg, Args.
7273 return printValue (arg) + ((" , " + printValue (args)) + ... + " " );
7374}
7475
76+ template <typename ... Args> void InPlaceConvertWasmToHostEndianness (Args &...args) {
77+ (void (args = wasmtoh (convertWordToUint32 (args), true )), ...);
78+ }
79+ proxy_wasm::Word ConvertWasmToHostEndianness (const proxy_wasm::Word &arg) {
80+ return proxy_wasm::Word (wasmtoh (convertWordToUint32 (arg), true ));
81+ }
82+ template <typename ... Args> void InPlaceConvertHostToWasmEndianness (Args &...args) {
83+ (void (args = htowasm (convertWordToUint32 (args), true )), ...);
84+ }
85+ template <typename ... Args> auto ConvertHostToWasmEndianness (const Args &...args) {
86+ return std::make_tuple ((htowasm (convertWordToUint32 (args), true ))...);
87+ }
88+
7589} // namespace
7690
7791class Wasmtime : public WasmVm {
@@ -283,6 +297,7 @@ void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
283297 module_name, function_name,
284298 [this , function,
285299 function_name = std::string (module_name) + " ." + std::string (function_name)](Args... args) {
300+ InPlaceConvertWasmToHostEndianness (args...);
286301 const bool log = cmpLogLevel (LogLevel::trace);
287302 if (log) {
288303 integration ()->trace (" [vm->host] " + function_name + " (" + printValues (args...) + " )" );
@@ -304,6 +319,7 @@ void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
304319 module_name, function_name,
305320 [this , function,
306321 function_name = std::string (module_name) + " ." + std::string (function_name)](Args... args) {
322+ InPlaceConvertWasmToHostEndianness (args...);
307323 const bool log = cmpLogLevel (LogLevel::trace);
308324 if (log) {
309325 integration ()->trace (" [vm->host] " + function_name + " (" + printValues (args...) + " )" );
@@ -312,7 +328,7 @@ void Wasmtime::registerHostFunctionImpl(std::string_view module_name,
312328 if (log) {
313329 integration ()->trace (" [vm<-host] " + function_name + " return: " + printValue (result));
314330 }
315- return result;
331+ return ConvertHostToWasmEndianness ( result) ;
316332 });
317333 if (!result) {
318334 fail (FailState::ConfigureFailed, " Failed to register host function: " + result.err ().message ());
@@ -340,6 +356,7 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
340356 integration ()->trace (" [host->vm] " + std::string (function_name) + " (" + printValues (args...) +
341357 " )" );
342358 }
359+ InPlaceConvertHostToWasmEndianness (args...);
343360 TrapResult<std::monostate> result = func.call (store_->context (), {args...});
344361 if (!result) {
345362 fail (FailState::RuntimeError,
@@ -372,17 +389,19 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
372389 integration ()->trace (" [host->vm] " + std::string (function_name) + " (" + printValues (args...) +
373390 " )" );
374391 }
392+ InPlaceConvertHostToWasmEndianness (args...);
375393 TrapResult<R> result = func.call (store_->context (), {args...});
376394 if (!result) {
377395 fail (FailState::RuntimeError,
378396 " Function: " + std::string (function_name) + " failed: " + result.err ().message ());
379397 return R{};
380398 }
399+ R result_host = ConvertWasmToHostEndianness (result.ok ());
381400 if (log) {
382401 integration ()->trace (" [host<-vm] " + std::string (function_name) +
383- " return: " + printValue (result. unwrap () ));
402+ " return: " + printValue (result_host ));
384403 }
385- return result. ok () ;
404+ return result_host ;
386405 };
387406};
388407
0 commit comments