Temporary commit - work in progress

This commit is contained in:
King_DuckZ 2022-05-19 01:06:09 +02:00
parent 1fa6d62f17
commit 65fee858dd
7 changed files with 54 additions and 28 deletions

View file

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

View file

@ -65,8 +65,8 @@ int main() {
wren::DefConfiguration config; wren::DefConfiguration config;
wren::VM vm(&config, &custom_data); wren::VM vm(&config, &custom_data);
vm.callback_manager().add_callback(true, "main", "Game", "user_input()", &user_input) vm.callback_manager().add_callback(true, "main", "Game", "user_input()", [](){return &user_input;})
.add_callback(true, "main", "Game", "random()", &random_num); .add_callback(true, "main", "Game", "random()", [](){return &random_num;});
interpret(vm, "main", load_file("main.wren")); interpret(vm, "main", load_file("main.wren"));
wren::call<void>(vm, wren::MN<"main", "the_game">, "play_game"); wren::call<void>(vm, wren::MN<"main", "the_game">, "play_game");

View file

@ -66,7 +66,7 @@ int main() {
wren::DefConfiguration config; wren::DefConfiguration config;
wren::VM vm(&config, nullptr); wren::VM vm(&config, nullptr);
vm.callback_manager().add_callback(true, "main", "User", "get_env(_)", &user_get_env); vm.callback_manager().add_callback(true, "main", "User", "get_env(_)", [](){return &user_get_env;});
vm.interpret("main", g_script); vm.interpret("main", g_script);

View file

@ -37,6 +37,8 @@ namespace {
foreign x=(value) foreign x=(value)
foreign y=(value) foreign y=(value)
foreign z=(value) foreign z=(value)
foreign base_x()
} }
)script"}; )script"};
@ -53,6 +55,10 @@ vec3.x = 3.5
vec3.y = vec2.x / vec2.z vec3.y = vec2.x / vec2.z
vec3.z = vec1.x + vec2.y / 4.0 vec3.z = vec1.x + vec2.y / 4.0
System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>") System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>")
var vec_base_x = vec2.base_x()
System.print("vec_base_x modified by scripting: <%(vec_base_x.x), %(vec_base_x.y), %(vec_base_x.z)>")
)script"; )script";
template <typename T> 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_y(T y) { m_y = y; }
void set_z(T z) { m_z = z; } void set_z(T z) { m_z = z; }
Vector base_x() { return {1.0, 0.0, 0.0}; }
private: private:
T m_x{}, m_y{}, m_z{}; T m_x{}, m_y{}, m_z{};
}; };
@ -104,12 +112,13 @@ int main() {
MyConf conf; MyConf conf;
wren::VM vm(&conf, nullptr); wren::VM vm(&conf, nullptr);
vm.callback_manager() vm.callback_manager()
.add_callback(false, "math_vector", "MathVector", "x=(_)", make_method_bindable<&Vector<double>::set_x>()) .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", "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", "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", "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", "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", "z", make_method_bindable<&Vector<double>::z>)
.add_callback(false, "math_vector", "MathVector", "base_x()", make_method_bindable<&Vector<double>::base_x>);
vm.class_manager() vm.class_manager()
.add_class_maker("math_vector", "MathVector", make_foreign_class<Vector<double>, .add_class_maker("math_vector", "MathVector", make_foreign_class<Vector<double>,

View file

@ -23,6 +23,7 @@
#include <vector> #include <vector>
#include <string_view> #include <string_view>
#include <type_traits> #include <type_traits>
#include <functional>
namespace wren::detail { namespace wren::detail {
class FullSignatureOwning; class FullSignatureOwning;
@ -106,7 +107,13 @@ namespace wren {
CallbackManager(); CallbackManager();
~CallbackManager() noexcept; ~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; foreign_method_t callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature) const;
private: private:

View file

@ -53,9 +53,6 @@ namespace wren {
T* make_wren_object(VM& vm, Args&&... args); T* make_wren_object(VM& vm, Args&&... args);
#endif #endif
template <auto V>
foreign_method_t make_method_bindable();
void interpret (VM& vm, const std::string& module_name, const std::string& script); 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, ModuleAndName mod_and_name, int slot);
void variable(VM& vm, const Handle& handle, int slot); void variable(VM& vm, const Handle& handle, int slot);
@ -97,12 +94,14 @@ namespace wren {
} }
public: public:
static foreign_method_t make() { foreign_method_t operator()() const {
return [](VM& vm, ModuleAndName) { return [](VM& vm, ModuleAndName mn) {
if constexpr (std::is_same_v<R, void>) { if constexpr (std::is_same_v<R, void>) {
call_implem(std::make_integer_sequence<int, sizeof...(Args)>(), vm); call_implem(std::make_integer_sequence<int, sizeof...(Args)>(), vm);
} }
else { 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 = call_implem(std::make_integer_sequence<int, sizeof...(Args)>(), vm);
//TODO: check for errors //TODO: check for errors
vm.ensure_slots(1); vm.ensure_slots(1);
@ -112,6 +111,7 @@ namespace wren {
else { else {
ModuleAndName wren_name = wren_class_name_from_type<R>(vm); ModuleAndName wren_name = wren_class_name_from_type<R>(vm);
R* const new_object = make_wren_object<R>(vm, wren_name, std::move(ret)); R* const new_object = make_wren_object<R>(vm, wren_name, std::move(ret));
static_cast<void>(new_object);
} }
} }
}; };
@ -119,11 +119,11 @@ namespace wren {
}; };
template <auto V> struct MakeMethodBindable; template <auto V> struct MakeMethodBindable;
template <typename T, void(T::*Method)(VM&)> struct MakeMethodBindable<Method> { template <typename T, void(T::*Method)(VM&,ModuleAndName)> struct MakeMethodBindable<Method> {
static foreign_method_t make() { foreign_method_t operator()() const {
return [](VM& vm, ModuleAndName) { return [](VM& vm, ModuleAndName mn) {
T* obj = foreign<T>(vm, 0); T* obj = foreign<T>(vm, 0);
(obj->*Method)(vm); (obj->*Method)(vm, mn);
}; };
} }
}; };
@ -198,7 +198,8 @@ namespace wren {
#endif #endif
template <auto V> template <auto V>
inline foreign_method_t make_method_bindable() { inline constexpr detail::MakeMethodBindable<V> make_method_bindable;
return detail::MakeMethodBindable<V>::make();
} //template <auto V>
//inline detail::MakeFunctionBindable<V> make_function_bindable;
} //namespace wren } //namespace wren

View file

@ -59,16 +59,22 @@ namespace wren {
CallbackManager::CallbackManager() = default; CallbackManager::CallbackManager() = default;
CallbackManager::~CallbackManager() noexcept = default; CallbackManager::~CallbackManager() noexcept = default;
CallbackManager& 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& 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
) {
using detail::FullSignatureOwning; using detail::FullSignatureOwning;
using detail::TempFullSignature; using detail::TempFullSignature;
MethodMap& map = *method_map(is_static); MethodMap& map = *method_map(is_static);
auto it_found = map.find(TempFullSignature{module_name, class_name, signature}); auto it_found = map.find(TempFullSignature{module_name, class_name, signature});
if (map.cend() == it_found) if (map.cend() == it_found)
map.insert(it_found, std::make_pair(FullSignatureOwning{module_name, class_name, signature}, cb)); map.insert(it_found, std::make_pair(FullSignatureOwning{module_name, class_name, signature}, make_cb()));
else else
it_found->second = cb; it_found->second = make_cb();
return *this; return *this;
} }