Refactor so that make_method_bindable also accepts free functions now

This commit is contained in:
King_DuckZ 2022-05-19 01:06:09 +02:00
parent c6d01f745a
commit addb48822f
7 changed files with 96 additions and 37 deletions

View file

@ -37,6 +37,8 @@ namespace {
foreign x=(value)
foreign y=(value)
foreign z=(value)
foreign static base_x()
}
)script"};
@ -53,6 +55,10 @@ vec3.x = 3.5
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 = MathVector.base_x()
System.print("vec_base_x modified by scripting: <%(vec_base_x.x), %(vec_base_x.y), %(vec_base_x.z)>")
)script";
template <typename T>
@ -74,6 +80,8 @@ System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>")
void set_y(T y) { m_y = y; }
void set_z(T z) { m_z = z; }
static Vector base_x() { return {1.0, 0.0, 0.0}; }
private:
T m_x{}, m_y{}, m_z{};
};
@ -98,18 +106,19 @@ System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>")
} //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", "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>,