Skip to content

Commit 31f3184

Browse files
authored
wavm: simplify get/register templates. (#87)
Signed-off-by: Piotr Sikora <[email protected]>
1 parent d981c85 commit 31f3184

File tree

1 file changed

+10
-185
lines changed

1 file changed

+10
-185
lines changed

src/wavm/wavm.cc

Lines changed: 10 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ template <> constexpr ValueType inferValueType<proxy_wasm::Word>() { return Valu
7070
namespace proxy_wasm {
7171

7272
// Forward declarations.
73+
template <typename... Args>
74+
void getFunctionWavm(WasmVm *vm, std::string_view function_name,
75+
std::function<void(ContextBase *, Args...)> *function);
7376
template <typename R, typename... Args>
7477
void getFunctionWavm(WasmVm *vm, std::string_view function_name,
7578
std::function<R(ContextBase *, Args...)> *function);
7679
template <typename R, typename... Args>
7780
void registerCallbackWavm(WasmVm *vm, std::string_view module_name, std::string_view function_name,
78-
R (*)(Args...));
79-
template <typename F, typename R, typename... Args>
80-
void registerCallbackWavm(WasmVm *vm, std::string_view module_name, std::string_view function_name,
81-
F, R (*)(Args...));
81+
R (*function)(Args...));
8282

8383
namespace Wavm {
8484

@@ -431,86 +431,6 @@ void registerCallbackWavm(WasmVm *vm, std::string_view module_name, std::string_
431431
reinterpret_cast<void *>(f), inferHostFunctionType(f)));
432432
}
433433

434-
template void registerCallbackWavm<void, void *>(WasmVm *vm, std::string_view module_name,
435-
std::string_view function_name, void (*f)(void *));
436-
template void registerCallbackWavm<void, void *, U32>(WasmVm *vm, std::string_view module_name,
437-
std::string_view function_name,
438-
void (*f)(void *, U32));
439-
template void registerCallbackWavm<void, void *, U32, U32>(WasmVm *vm, std::string_view module_name,
440-
std::string_view function_name,
441-
void (*f)(void *, U32, U32));
442-
template void registerCallbackWavm<void, void *, U32, U32, U32>(WasmVm *vm,
443-
std::string_view module_name,
444-
std::string_view function_name,
445-
void (*f)(void *, U32, U32, U32));
446-
template void
447-
registerCallbackWavm<void, void *, U32, U32, U32, U32>(WasmVm *vm, std::string_view module_name,
448-
std::string_view function_name,
449-
void (*f)(void *, U32, U32, U32, U32));
450-
template void registerCallbackWavm<void, void *, U32, U32, U32, U32, U32>(
451-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
452-
void (*f)(void *, U32, U32, U32, U32, U32));
453-
template void registerCallbackWavm<void, void *, U32, U32, U32, U32, U32, U32>(
454-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
455-
void (*f)(void *, U32, U32, U32, U32, U32, U32));
456-
template void registerCallbackWavm<void, void *, U32, U32, U32, U32, U32, U32, U32>(
457-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
458-
void (*f)(void *, U32, U32, U32, U32, U32, U32, U32));
459-
template void registerCallbackWavm<void, void *, U32, U32, U32, U32, U32, U32, U32, U32>(
460-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
461-
void (*f)(void *, U32, U32, U32, U32, U32, U32, U32, U32));
462-
template void registerCallbackWavm<void, void *, U32, U32, U32, U32, U32, U32, U32, U32, U32>(
463-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
464-
void (*f)(void *, U32, U32, U32, U32, U32, U32, U32, U32, U32));
465-
template void registerCallbackWavm<void, void *, U32, U32, U32, U32, U32, U32, U32, U32, U32, U32>(
466-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
467-
void (*f)(void *, U32, U32, U32, U32, U32, U32, U32, U32, U32, U32));
468-
469-
template void registerCallbackWavm<U32, void *>(WasmVm *vm, std::string_view module_name,
470-
std::string_view function_name, U32 (*f)(void *));
471-
template void registerCallbackWavm<U32, void *, U32>(WasmVm *vm, std::string_view module_name,
472-
std::string_view function_name,
473-
U32 (*f)(void *, U32));
474-
template void registerCallbackWavm<U32, void *, U32, U32>(WasmVm *vm, std::string_view module_name,
475-
std::string_view function_name,
476-
U32 (*f)(void *, U32, U32));
477-
template void registerCallbackWavm<U32, void *, U32, U32, U32>(WasmVm *vm,
478-
std::string_view module_name,
479-
std::string_view function_name,
480-
U32 (*f)(void *, U32, U32, U32));
481-
template void
482-
registerCallbackWavm<U32, void *, U32, U32, U32, U32>(WasmVm *vm, std::string_view module_name,
483-
std::string_view function_name,
484-
U32 (*f)(void *, U32, U32, U32, U32));
485-
template void registerCallbackWavm<U32, void *, U32, U32, U32, U32, U32>(
486-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
487-
U32 (*f)(void *, U32, U32, U32, U32, U32));
488-
template void registerCallbackWavm<U32, void *, U32, U32, U32, U32, U32, U32>(
489-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
490-
U32 (*f)(void *, U32, U32, U32, U32, U32, U32));
491-
template void registerCallbackWavm<U32, void *, U32, U32, U32, U32, U32, U32, U32>(
492-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
493-
U32 (*f)(void *, U32, U32, U32, U32, U32, U32, U32));
494-
template void registerCallbackWavm<U32, void *, U32, U32, U32, U32, U32, U32, U32, U32>(
495-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
496-
U32 (*f)(void *, U32, U32, U32, U32, U32, U32, U32, U32));
497-
template void registerCallbackWavm<U32, void *, U32, U32, U32, U32, U32, U32, U32, U32, U32>(
498-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
499-
U32 (*f)(void *, U32, U32, U32, U32, U32, U32, U32, U32, U32));
500-
template void registerCallbackWavm<U32, void *, U32, U32, U32, U32, U32, U32, U32, U32, U32, U32>(
501-
WasmVm *vm, std::string_view module_name, std::string_view function_name,
502-
U32 (*f)(void *, U32, U32, U32, U32, U32, U32, U32, U32, U32, U32));
503-
504-
template void registerCallbackWavm<U64, void *, U32>(WasmVm *vm, std::string_view module_name,
505-
std::string_view function_name,
506-
U64 (*f)(void *, U32));
507-
template void registerCallbackWavm<void, void *, U32, I64>(WasmVm *vm, std::string_view module_name,
508-
std::string_view function_name,
509-
void (*f)(void *, U32, I64));
510-
template void registerCallbackWavm<void, void *, U32, U64>(WasmVm *vm, std::string_view module_name,
511-
std::string_view function_name,
512-
void (*f)(void *, U32, U64));
513-
514434
template <typename R, typename... Args>
515435
IR::FunctionType inferStdFunctionType(std::function<R(ContextBase *, Args...)> *) {
516436
return IR::FunctionType(IR::inferResultType<R>(), IR::TypeTuple({IR::inferValueType<Args>()...}));
@@ -521,8 +441,8 @@ static bool checkFunctionType(WAVM::Runtime::Function *f, IR::FunctionType t) {
521441
}
522442

523443
template <typename R, typename... Args>
524-
void getFunctionWavmReturn(WasmVm *vm, std::string_view function_name,
525-
std::function<R(ContextBase *, Args...)> *function, uint32_t) {
444+
void getFunctionWavm(WasmVm *vm, std::string_view function_name,
445+
std::function<R(ContextBase *, Args...)> *function) {
526446
auto wavm = static_cast<proxy_wasm::Wavm::Wavm *>(vm);
527447
auto f =
528448
asFunctionNullable(getInstanceExport(wavm->module_instance_, std::string(function_name)));
@@ -550,11 +470,9 @@ void getFunctionWavmReturn(WasmVm *vm, std::string_view function_name,
550470
};
551471
}
552472

553-
struct Void {};
554-
555-
template <typename R, typename... Args>
556-
void getFunctionWavmReturn(WasmVm *vm, std::string_view function_name,
557-
std::function<R(ContextBase *, Args...)> *function, Void) {
473+
template <typename... Args>
474+
void getFunctionWavm(WasmVm *vm, std::string_view function_name,
475+
std::function<void(ContextBase *, Args...)> *function) {
558476
auto wavm = static_cast<proxy_wasm::Wavm::Wavm *>(vm);
559477
auto f =
560478
asFunctionNullable(getInstanceExport(wavm->module_instance_, std::string(function_name)));
@@ -568,104 +486,11 @@ void getFunctionWavmReturn(WasmVm *vm, std::string_view function_name,
568486
wavm->fail(FailState::UnableToInitializeCode,
569487
"Bad function signature for: " + std::string(function_name));
570488
}
571-
*function = [wavm, f, function_name](ContextBase *context, Args... args) -> R {
489+
*function = [wavm, f, function_name](ContextBase *context, Args... args) {
572490
WasmUntaggedValue values[] = {args...};
573491
CALL_WITH_CONTEXT(invokeFunction(wavm->context_, f, getFunctionType(f), &values[0]), context,
574492
wavm);
575493
};
576494
}
577495

578-
// NB: Unfortunately 'void' is not treated like every other function type in C++. In
579-
// particular it is not possible to specialize a template based on 'void'. Instead
580-
// we use 'Void' for template matching. Note that the template implementation above
581-
// which matchers on 'bool' does not use 'Void' in the implemenation.
582-
template <typename R, typename... Args>
583-
void getFunctionWavm(WasmVm *vm, std::string_view function_name,
584-
std::function<R(ContextBase *, Args...)> *function) {
585-
typename std::conditional<std::is_void<R>::value, Void, uint32_t>::type x{};
586-
getFunctionWavmReturn(vm, function_name, function, x);
587-
}
588-
589-
template void getFunctionWavm<void>(WasmVm *, std::string_view,
590-
std::function<void(ContextBase *)> *);
591-
template void getFunctionWavm<void, uint32_t>(WasmVm *, std::string_view,
592-
std::function<void(ContextBase *, uint32_t)> *);
593-
template void
594-
getFunctionWavm<void, uint32_t, uint32_t>(WasmVm *, std::string_view,
595-
std::function<void(ContextBase *, uint32_t, uint32_t)> *);
596-
template void getFunctionWavm<void, uint32_t, uint32_t, uint32_t>(
597-
WasmVm *, std::string_view, std::function<void(ContextBase *, uint32_t, uint32_t, uint32_t)> *);
598-
template void getFunctionWavm<void, uint32_t, uint32_t, uint32_t, uint32_t>(
599-
WasmVm *, std::string_view,
600-
std::function<void(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t)> *);
601-
template void getFunctionWavm<void, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
602-
WasmVm *, std::string_view,
603-
std::function<void(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)> *);
604-
template void getFunctionWavm<void, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
605-
WasmVm *, std::string_view,
606-
std::function<void(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)>
607-
*);
608-
template void getFunctionWavm<void, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
609-
WasmVm *, std::string_view,
610-
std::function<void(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
611-
uint32_t)> *);
612-
template void getFunctionWavm<void, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
613-
WasmVm *, std::string_view,
614-
std::function<void(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
615-
uint32_t, uint32_t)> *);
616-
template void getFunctionWavm<void, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
617-
WasmVm *, std::string_view,
618-
std::function<void(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
619-
uint32_t, uint32_t, uint32_t)> *);
620-
template void getFunctionWavm<void, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
621-
WasmVm *, std::string_view,
622-
std::function<void(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
623-
uint32_t, uint32_t, uint32_t, uint32_t)> *);
624-
625-
template void getFunctionWavm<uint32_t>(WasmVm *, std::string_view,
626-
std::function<uint32_t(ContextBase *)> *);
627-
template void
628-
getFunctionWavm<uint32_t, uint32_t>(WasmVm *, std::string_view,
629-
std::function<uint32_t(ContextBase *, uint32_t)> *);
630-
template void getFunctionWavm<uint32_t, uint32_t, uint32_t>(
631-
WasmVm *, std::string_view, std::function<uint32_t(ContextBase *, uint32_t, uint32_t)> *);
632-
template void getFunctionWavm<uint32_t, uint32_t, uint32_t, uint32_t>(
633-
WasmVm *, std::string_view,
634-
std::function<uint32_t(ContextBase *, uint32_t, uint32_t, uint32_t)> *);
635-
template void getFunctionWavm<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
636-
WasmVm *, std::string_view,
637-
std::function<uint32_t(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t)> *);
638-
template void getFunctionWavm<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
639-
WasmVm *, std::string_view,
640-
std::function<uint32_t(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)> *);
641-
template void getFunctionWavm<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
642-
WasmVm *, std::string_view,
643-
std::function<uint32_t(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
644-
uint32_t)> *);
645-
template void getFunctionWavm<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
646-
WasmVm *, std::string_view,
647-
std::function<uint32_t(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
648-
uint32_t, uint32_t)> *);
649-
template void getFunctionWavm<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
650-
WasmVm *, std::string_view,
651-
std::function<uint32_t(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
652-
uint32_t, uint32_t, uint32_t)> *);
653-
template void getFunctionWavm<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
654-
WasmVm *, std::string_view,
655-
std::function<uint32_t(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
656-
uint32_t, uint32_t, uint32_t, uint32_t)> *);
657-
template void getFunctionWavm<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>(
658-
WasmVm *, std::string_view,
659-
std::function<uint32_t(ContextBase *, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
660-
uint32_t, uint32_t, uint32_t, uint32_t, uint32_t)> *);
661-
662-
template <typename T> T getValue(IR::Value) {}
663-
template <> Word getValue(IR::Value v) { return v.u32; }
664-
template <> int32_t getValue(IR::Value v) { return v.i32; }
665-
template <> uint32_t getValue(IR::Value v) { return v.u32; }
666-
template <> int64_t getValue(IR::Value v) { return v.i64; }
667-
template <> uint64_t getValue(IR::Value v) { return v.u64; }
668-
template <> float getValue(IR::Value v) { return v.f32; }
669-
template <> double getValue(IR::Value v) { return v.f64; }
670-
671496
} // namespace proxy_wasm

0 commit comments

Comments
 (0)