Refactor so that make_method_bindable also accepts free functions now
This commit is contained in:
parent
c6d01f745a
commit
addb48822f
7 changed files with 96 additions and 37 deletions
|
@ -23,6 +23,7 @@
|
|||
#include <vector>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <functional>
|
||||
|
||||
namespace wren::detail {
|
||||
class FullSignatureOwning;
|
||||
|
@ -106,7 +107,13 @@ namespace wren {
|
|||
CallbackManager();
|
||||
~CallbackManager() noexcept;
|
||||
|
||||
CallbackManager& add_callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature, foreign_method_t cb);
|
||||
CallbackManager& add_callback (
|
||||
bool is_static,
|
||||
std::string_view module_name,
|
||||
std::string_view class_name,
|
||||
std::string_view signature,
|
||||
std::function<foreign_method_t()> make_cb
|
||||
);
|
||||
foreign_method_t callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature) const;
|
||||
|
||||
private:
|
||||
|
|
|
@ -53,9 +53,6 @@ namespace wren {
|
|||
T* make_wren_object(VM& vm, Args&&... args);
|
||||
#endif
|
||||
|
||||
template <auto V>
|
||||
foreign_method_t make_method_bindable();
|
||||
|
||||
void interpret (VM& vm, const std::string& module_name, const std::string& script);
|
||||
void variable(VM& vm, ModuleAndName mod_and_name, int slot);
|
||||
void variable(VM& vm, const Handle& handle, int slot);
|
||||
|
@ -87,23 +84,59 @@ namespace wren {
|
|||
(set_single_for_call(vm, Indices + 1, args), ...);
|
||||
}
|
||||
|
||||
template <typename T, typename R, auto Method, typename... Args> struct MakeMethodBindableConstNonConst {
|
||||
private:
|
||||
template <auto V>
|
||||
class CallImplemProvider;
|
||||
template <typename T, typename R, typename... Args, R(T::*Method)(Args...)>
|
||||
class CallImplemProvider<Method> {
|
||||
protected:
|
||||
typedef R return_type;
|
||||
static constexpr std::size_t argument_count = sizeof...(Args);
|
||||
|
||||
template <int... Indices>
|
||||
static R call_implem (std::integer_sequence<int, Indices...>, VM& vm) {
|
||||
static_assert(sizeof...(Indices) == sizeof...(Args), "Mismatching argument count");
|
||||
T* obj = foreign<T>(vm, 0);
|
||||
return (obj->*Method)(get<std::decay_t<Args>>(vm, Indices + 1)...);
|
||||
}
|
||||
};
|
||||
template <typename T, typename R, typename... Args, R(T::*ConstMethod)(Args...)const>
|
||||
class CallImplemProvider<ConstMethod> {
|
||||
protected:
|
||||
typedef R return_type;
|
||||
static constexpr std::size_t argument_count = sizeof...(Args);
|
||||
|
||||
public:
|
||||
static foreign_method_t make() {
|
||||
return [](VM& vm, ModuleAndName) {
|
||||
template <int... Indices>
|
||||
static R call_implem (std::integer_sequence<int, Indices...>, VM& vm) {
|
||||
static_assert(sizeof...(Indices) == sizeof...(Args), "Mismatching argument count");
|
||||
T* obj = foreign<T>(vm, 0);
|
||||
return (obj->*ConstMethod)(get<std::decay_t<Args>>(vm, Indices + 1)...);
|
||||
}
|
||||
};
|
||||
template <typename R, typename... Args, R(*Function)(Args...)>
|
||||
class CallImplemProvider<Function> {
|
||||
protected:
|
||||
typedef R return_type;
|
||||
static constexpr std::size_t argument_count = sizeof...(Args);
|
||||
|
||||
template <int... Indices>
|
||||
static R call_implem (std::integer_sequence<int, Indices...>, VM& vm) {
|
||||
static_assert(sizeof...(Indices) == sizeof...(Args), "Mismatching argument count");
|
||||
return (*Function)(get<std::decay_t<Args>>(vm, Indices + 1)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <auto V>
|
||||
struct MakeFunctionBindable : private CallImplemProvider<V> {
|
||||
foreign_method_t operator()() const {
|
||||
using R = typename CallImplemProvider<V>::return_type;
|
||||
constexpr int argument_count = static_cast<int>(CallImplemProvider<V>::argument_count);
|
||||
|
||||
return [](VM& vm, ModuleAndName mn) {
|
||||
if constexpr (std::is_same_v<R, void>) {
|
||||
call_implem(std::make_integer_sequence<int, sizeof...(Args)>(), vm);
|
||||
CallImplemProvider<V>::call_implem(std::make_integer_sequence<int, argument_count>(), vm);
|
||||
}
|
||||
else {
|
||||
auto ret = call_implem(std::make_integer_sequence<int, sizeof...(Args)>(), vm);
|
||||
auto ret = CallImplemProvider<V>::call_implem(std::make_integer_sequence<int, argument_count>(), vm);
|
||||
//TODO: check for errors
|
||||
vm.ensure_slots(1);
|
||||
if constexpr (std::is_fundamental<R>::value) {
|
||||
|
@ -112,24 +145,29 @@ namespace wren {
|
|||
else {
|
||||
ModuleAndName wren_name = wren_class_name_from_type<R>(vm);
|
||||
R* const new_object = make_wren_object<R>(vm, wren_name, std::move(ret));
|
||||
static_cast<void>(new_object);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
template <auto V> struct MakeMethodBindable;
|
||||
template <typename T, void(T::*Method)(VM&)> struct MakeMethodBindable<Method> {
|
||||
static foreign_method_t make() {
|
||||
return [](VM& vm, ModuleAndName) {
|
||||
template <typename T, void(T::*Method)(VM&,ModuleAndName)> struct MakeFunctionBindable<Method> {
|
||||
foreign_method_t operator()() const {
|
||||
return [](VM& vm, ModuleAndName mn) {
|
||||
T* obj = foreign<T>(vm, 0);
|
||||
(obj->*Method)(vm);
|
||||
(obj->*Method)(vm, mn);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename R, typename... Args, R(T::*Method)(Args...)> struct MakeMethodBindable<Method> : MakeMethodBindableConstNonConst<T, R, Method, Args...> {};
|
||||
template <typename T, typename R, typename... Args, R(T::*Method)(Args...)const> struct MakeMethodBindable<Method> : MakeMethodBindableConstNonConst<T, R, Method, Args...> {};
|
||||
template <void(*Function)(VM&,ModuleAndName)> struct MakeFunctionBindable<Function> {
|
||||
foreign_method_t operator()() const {
|
||||
return [](VM& vm, ModuleAndName mn) {
|
||||
(*Function)(vm, mn);
|
||||
};
|
||||
}
|
||||
};
|
||||
} //namespace detail
|
||||
|
||||
inline void interpret (VM& vm, const std::string& module_name, const std::string& script) {
|
||||
|
@ -198,7 +236,5 @@ namespace wren {
|
|||
#endif
|
||||
|
||||
template <auto V>
|
||||
inline foreign_method_t make_method_bindable() {
|
||||
return detail::MakeMethodBindable<V>::make();
|
||||
}
|
||||
constexpr detail::MakeFunctionBindable<V> make_function_bindable;
|
||||
} //namespace wren
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue