Refactor so that make_method_bindable also accepts free functions now

This includes pointers to static member functions too of course.
`make_method_bindable` got renamed into the more generic
`make_function_bindable` because it deals with all functions
now, not only member functions.
This commit is contained in:
King_DuckZ 2022-05-19 15:05:18 +02:00
parent c993f8a38e
commit 8a721494b1
3 changed files with 64 additions and 31 deletions

View file

@ -121,17 +121,15 @@ int main() {
MyConf conf;
wren::VM vm(&conf, nullptr);
vm.callback_manager()
//.add_callback(true, "calendar", "Calendar", "today()", [](){return &Calendar::today;})
//.add_callback(false, "calendar", "Calendar", "add_appointment(_)", wren::make_method_bindable<&Calendar::add_appointment>)
.add_callback(false, "calendar", "Calendar", "appointment_count()", wren::make_method_bindable<&Calendar::appointment_count>);
//.add_callback(false, "calendar", "Calendar", "appointment_count()", wren::detail::MakeMethodBindable<&Calendar::appointment_count>());
.add_callback(true, "calendar", "Calendar", "today()", [](){return &Calendar::today;})
.add_callback(false, "calendar", "Calendar", "add_appointment(_)", wren::make_function_bindable<&Calendar::add_appointment>)
.add_callback(false, "calendar", "Calendar", "appointment_count()", wren::make_function_bindable<&Calendar::appointment_count>);
vm.class_manager().add_class_maker("calendar", "Calendar", wren::make_foreign_class<Calendar>);
vm.interpret("main", g_test_script);
Calendar* const cale = std::get<0>(wren::variables<Calendar>(vm, MN<"main", "cale">));
cale->print_appointments();
wren::make_wren_object<Calendar, "calendar">(vm);
return 0;
}

View file

@ -38,7 +38,7 @@ namespace {
foreign y=(value)
foreign z=(value)
foreign base_x()
foreign static base_x()
}
)script"};
@ -56,7 +56,7 @@ vec3.y = vec2.x / vec2.z
vec3.z = vec1.x + vec2.y / 4.0
System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>")
var vec_base_x = vec2.base_x()
var vec_base_x = MathVector.base_x()
System.print("vec_base_x modified by scripting: <%(vec_base_x.x), %(vec_base_x.y), %(vec_base_x.z)>")
)script";
@ -80,7 +80,7 @@ System.print("vec_base_x modified by scripting: <%(vec_base_x.x), %(vec_base_x.y
void set_y(T y) { m_y = y; }
void set_z(T z) { m_z = z; }
Vector base_x() { return {1.0, 0.0, 0.0}; }
static Vector base_x() { return {1.0, 0.0, 0.0}; }
private:
T m_x{}, m_y{}, m_z{};
@ -106,19 +106,19 @@ System.print("vec_base_x modified by scripting: <%(vec_base_x.x), %(vec_base_x.y
} //unnamed namespace
int main() {
using wren::make_method_bindable;
using wren::make_function_bindable;
using wren::make_foreign_class;
MyConf conf;
wren::VM vm(&conf, nullptr);
vm.callback_manager()
.add_callback(false, "math_vector", "MathVector", "x=(_)", make_method_bindable<&Vector<double>::set_x>)
.add_callback(false, "math_vector", "MathVector", "y=(_)", make_method_bindable<&Vector<double>::set_y>)
.add_callback(false, "math_vector", "MathVector", "z=(_)", make_method_bindable<&Vector<double>::set_z>)
.add_callback(false, "math_vector", "MathVector", "x", make_method_bindable<&Vector<double>::x>)
.add_callback(false, "math_vector", "MathVector", "y", make_method_bindable<&Vector<double>::y>)
.add_callback(false, "math_vector", "MathVector", "z", make_method_bindable<&Vector<double>::z>)
.add_callback(false, "math_vector", "MathVector", "base_x()", make_method_bindable<&Vector<double>::base_x>);
.add_callback(false, "math_vector", "MathVector", "x=(_)", make_function_bindable<&Vector<double>::set_x>)
.add_callback(false, "math_vector", "MathVector", "y=(_)", make_function_bindable<&Vector<double>::set_y>)
.add_callback(false, "math_vector", "MathVector", "z=(_)", make_function_bindable<&Vector<double>::set_z>)
.add_callback(false, "math_vector", "MathVector", "x", make_function_bindable<&Vector<double>::x>)
.add_callback(false, "math_vector", "MathVector", "y", make_function_bindable<&Vector<double>::y>)
.add_callback(false, "math_vector", "MathVector", "z", make_function_bindable<&Vector<double>::z>)
.add_callback(true, "math_vector", "MathVector", "base_x()", make_function_bindable<&Vector<double>::base_x>);
vm.class_manager()
.add_class_maker("math_vector", "MathVector", make_foreign_class<Vector<double>,

View file

@ -84,25 +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:
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 {
std::cout << "foreign_method make for " << mn.module_name() <<
"::" << mn.class_name() << '\n';
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) {
@ -118,8 +152,7 @@ namespace wren {
}
};
template <auto V> struct MakeMethodBindable;
template <typename T, void(T::*Method)(VM&,ModuleAndName)> struct MakeMethodBindable<Method> {
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);
@ -128,8 +161,13 @@ namespace wren {
}
};
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,8 +236,5 @@ namespace wren {
#endif
template <auto V>
inline constexpr detail::MakeMethodBindable<V> make_method_bindable;
//template <auto V>
//inline detail::MakeFunctionBindable<V> make_function_bindable;
constexpr detail::MakeFunctionBindable<V> make_function_bindable;
} //namespace wren