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

@ -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>,