2022-05-15 14:03:29 +00:00
|
|
|
/* Copyright 2020-2022, Michele Santullo
|
|
|
|
* This file is part of wrenpp.
|
|
|
|
*
|
|
|
|
* Wrenpp is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Wrenpp is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with wrenpp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "wrenpp/def_configuration.hpp"
|
|
|
|
#include "wrenpp/vm_fun.hpp"
|
|
|
|
#include "wrenpp/callback_manager.hpp"
|
|
|
|
#include "wrenpp/class_manager.hpp"
|
|
|
|
#include <string_view>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
constexpr std::string_view g_math_vector_src{
|
|
|
|
"foreign class MathVector {" R"script(
|
2022-05-15 15:01:57 +00:00
|
|
|
construct new() {}
|
2022-05-15 14:03:29 +00:00
|
|
|
construct new(value) {}
|
|
|
|
construct new(x, y, z) {}
|
|
|
|
|
|
|
|
foreign x
|
|
|
|
foreign y
|
|
|
|
foreign z
|
2022-05-15 15:01:57 +00:00
|
|
|
|
|
|
|
foreign x=(value)
|
|
|
|
foreign y=(value)
|
|
|
|
foreign z=(value)
|
2022-05-18 23:06:09 +00:00
|
|
|
|
|
|
|
foreign static base_x()
|
2022-05-15 14:03:29 +00:00
|
|
|
}
|
|
|
|
)script"};
|
|
|
|
|
|
|
|
constexpr const char g_test_script[] =
|
|
|
|
R"script(import "math_vector" for MathVector
|
2022-05-15 15:01:57 +00:00
|
|
|
var vec1 = MathVector.new(15.0)
|
2022-05-15 14:03:29 +00:00
|
|
|
var vec2 = MathVector.new(10.0, 20.0, 30.0)
|
2022-05-15 15:01:57 +00:00
|
|
|
var vec3 = MathVector.new()
|
|
|
|
System.print("vec1 constructed from single value: <%(vec1.x), %(vec1.y), %(vec1.z)>")
|
|
|
|
System.print("vec2 constructed from three values: <%(vec2.x), %(vec2.y), %(vec2.z)>")
|
|
|
|
System.print("vec3 default constructed: <%(vec3.x), %(vec3.y), %(vec3.z)>")
|
|
|
|
|
|
|
|
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)>")
|
2022-05-18 23:06:09 +00:00
|
|
|
|
|
|
|
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)>")
|
|
|
|
|
2022-05-15 14:03:29 +00:00
|
|
|
)script";
|
|
|
|
|
2022-05-15 18:17:42 +00:00
|
|
|
template <typename T>
|
|
|
|
class Vector {
|
2022-05-15 14:03:29 +00:00
|
|
|
public:
|
2022-05-15 18:17:42 +00:00
|
|
|
Vector() = default;
|
|
|
|
explicit Vector (T value) :
|
2022-05-15 14:03:29 +00:00
|
|
|
m_x(value), m_y(value), m_z(value)
|
|
|
|
{}
|
2022-05-15 18:17:42 +00:00
|
|
|
Vector (T x, T y, T z) :
|
2022-05-15 14:03:29 +00:00
|
|
|
m_x(x), m_y(y), m_z(z)
|
|
|
|
{}
|
2022-05-15 18:17:42 +00:00
|
|
|
~Vector() noexcept = default;
|
2022-05-15 14:03:29 +00:00
|
|
|
|
2022-05-15 18:17:42 +00:00
|
|
|
T x() const { return m_x; }
|
|
|
|
T y() const { return m_y; }
|
|
|
|
T z() const { return m_z; }
|
|
|
|
void set_x(T x) { m_x = x; }
|
|
|
|
void set_y(T y) { m_y = y; }
|
|
|
|
void set_z(T z) { m_z = z; }
|
2022-05-15 14:03:29 +00:00
|
|
|
|
2022-05-18 23:06:09 +00:00
|
|
|
static Vector base_x() { return {1.0, 0.0, 0.0}; }
|
|
|
|
|
2022-05-15 14:03:29 +00:00
|
|
|
private:
|
2022-05-15 18:17:42 +00:00
|
|
|
T m_x{}, m_y{}, m_z{};
|
2022-05-15 14:03:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MyConf : public wren::DefConfiguration {
|
|
|
|
public:
|
|
|
|
char* load_module_fn(wren::VM* vm, std::string_view module_name) {
|
|
|
|
if (module_name == "math_vector") {
|
2022-05-15 17:19:56 +00:00
|
|
|
constexpr const std::size_t buff_sz = g_math_vector_src.size() + 1;
|
2022-05-15 18:17:42 +00:00
|
|
|
char* const buff = new char[buff_sz];
|
2022-05-15 14:03:29 +00:00
|
|
|
std::copy(g_math_vector_src.cbegin(), g_math_vector_src.cend(), buff);
|
2022-05-15 17:19:56 +00:00
|
|
|
buff[buff_sz - 1] = '\0';
|
2022-05-15 14:03:29 +00:00
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-05-15 18:17:42 +00:00
|
|
|
|
|
|
|
void load_module_complete_fn(wren::VM*, std::string_view, char* buffer) {
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
2022-05-15 14:03:29 +00:00
|
|
|
};
|
|
|
|
} //unnamed namespace
|
|
|
|
|
|
|
|
int main() {
|
2022-05-18 23:06:09 +00:00
|
|
|
using wren::make_function_bindable;
|
2022-05-15 15:01:57 +00:00
|
|
|
using wren::make_foreign_class;
|
2022-05-15 14:03:29 +00:00
|
|
|
|
|
|
|
MyConf conf;
|
|
|
|
wren::VM vm(&conf, nullptr);
|
|
|
|
vm.callback_manager()
|
2022-05-18 23:06:09 +00:00
|
|
|
.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>);
|
2022-05-15 15:01:57 +00:00
|
|
|
|
|
|
|
vm.class_manager()
|
2022-05-18 16:48:18 +00:00
|
|
|
.add_class_maker("math_vector", "MathVector", make_foreign_class<Vector<double>,
|
2022-05-15 15:01:57 +00:00
|
|
|
double, //single value constructor
|
|
|
|
std::tuple<double, double, double>, //x,y,z constructor
|
|
|
|
std::tuple<> //default constructor
|
|
|
|
>);
|
2022-05-15 14:03:29 +00:00
|
|
|
|
|
|
|
vm.interpret("main", g_test_script);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|