Compare commits
5 commits
Author | SHA1 | Date | |
---|---|---|---|
8a721494b1 | |||
c993f8a38e | |||
e55390338a | |||
678624a75d | |||
65fee858dd |
11 changed files with 106 additions and 47 deletions
|
@ -120,9 +120,10 @@ 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_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.class_manager().add_class_maker("calendar", "Calendar", wren::make_foreign_class<Calendar>);
|
||||||
|
|
||||||
vm.interpret("main", g_test_script);
|
vm.interpret("main", g_test_script);
|
||||||
|
|
|
@ -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");
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,8 @@ namespace {
|
||||||
foreign x=(value)
|
foreign x=(value)
|
||||||
foreign y=(value)
|
foreign y=(value)
|
||||||
foreign z=(value)
|
foreign z=(value)
|
||||||
|
|
||||||
|
foreign static 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 = MathVector.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; }
|
||||||
|
|
||||||
|
static 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{};
|
||||||
};
|
};
|
||||||
|
@ -98,18 +106,19 @@ System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>")
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
using wren::make_method_bindable;
|
using wren::make_function_bindable;
|
||||||
using wren::make_foreign_class;
|
using wren::make_foreign_class;
|
||||||
|
|
||||||
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_function_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_function_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_function_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_function_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_function_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_function_bindable<&Vector<double>::z>)
|
||||||
|
.add_callback(true, "math_vector", "MathVector", "base_x()", make_function_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>,
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -62,13 +62,13 @@ namespace duckcore {
|
||||||
typedef BitArray<0, BitArray<1, BitArray<2, BitArray<4, BitArray<5,
|
typedef BitArray<0, BitArray<1, BitArray<2, BitArray<4, BitArray<5,
|
||||||
BitArray<7, BitArray<8, BitArray<10, BitArray<11, BitArray<12,
|
BitArray<7, BitArray<8, BitArray<10, BitArray<11, BitArray<12,
|
||||||
BitArray<16, BitArray<22, BitArray<23,
|
BitArray<16, BitArray<22, BitArray<23,
|
||||||
BitArray<26, nullptr_t> > > > > > > > > > > > > > PolynomialBits;
|
BitArray<26, std::nullptr_t> > > > > > > > > > > > > > PolynomialBits;
|
||||||
template <typename P, int M>
|
template <typename P, int M>
|
||||||
struct MakePolynomial {
|
struct MakePolynomial {
|
||||||
enum { value = (1 << (M - P::value)) bitor MakePolynomial<typename P::Next, M>::value };
|
enum { value = (1 << (M - P::value)) bitor MakePolynomial<typename P::Next, M>::value };
|
||||||
};
|
};
|
||||||
template <int M>
|
template <int M>
|
||||||
struct MakePolynomial<nullptr_t, M> {
|
struct MakePolynomial<std::nullptr_t, M> {
|
||||||
enum { value = 0 };
|
enum { value = 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,7 @@ namespace wren {
|
||||||
constexpr auto from = last_whatever + (last_whatever == fname_len ? 0 : 1);
|
constexpr auto from = last_whatever + (last_whatever == fname_len ? 0 : 1);
|
||||||
constexpr auto len = detail::find_token_end(fname + from, fname_len - from);
|
constexpr auto len = detail::find_token_end(fname + from, fname_len - from);
|
||||||
|
|
||||||
constexpr dhandy::bt::string<len> retval{fname + from, 0};
|
constexpr dhandy::bt::string<len+1> retval{fname + from, 0};
|
||||||
return retval;
|
return retval;
|
||||||
//#elif defined(_MSC_VER)
|
//#elif defined(_MSC_VER)
|
||||||
//void __cdecl guess_class_name<class MyTest>(void)
|
//void __cdecl guess_class_name<class MyTest>(void)
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../vm.hpp"
|
#include "../vm.hpp"
|
||||||
|
#include "module_and_name.hpp"
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
@ -46,10 +47,9 @@ namespace wren {
|
||||||
|
|
||||||
#if __cpp_concepts >= 201907
|
#if __cpp_concepts >= 201907
|
||||||
template <typename T>
|
template <typename T>
|
||||||
concept ConstCharTuple = requires {
|
concept ConstCharTuple =
|
||||||
std::same_as<std::remove_cv_t<T>, Handle> or
|
std::same_as<std::decay_t<T>, Handle> or
|
||||||
std::same_as<std::remove_cv_t<T>, ModuleAndName>;
|
std::same_as<std::decay_t<T>, ModuleAndName>;
|
||||||
};
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if __cpp_concepts >= 201907
|
#if __cpp_concepts >= 201907
|
||||||
|
|
|
@ -56,7 +56,7 @@ namespace dhandy {
|
||||||
template <std::size_t S2> constexpr bool operator== (const string<S2, Ch>&) const { return false; }
|
template <std::size_t S2> constexpr bool operator== (const string<S2, Ch>&) const { return false; }
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
requires (std::is_same_v<Args, typename string<S, Ch>::value_type> && ...)
|
requires (std::is_same_v<Args, Ch> && ...)
|
||||||
constexpr string ( Args... );
|
constexpr string ( Args... );
|
||||||
|
|
||||||
constexpr const value_type (&data_arr() const)[S] { return m_data; }
|
constexpr const value_type (&data_arr() const)[S] { return m_data; }
|
||||||
|
@ -110,8 +110,8 @@ namespace dhandy {
|
||||||
|
|
||||||
template <std::size_t S, typename Ch>
|
template <std::size_t S, typename Ch>
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
requires (std::is_same_v<Args, typename string<S, Ch>::value_type> && ...)
|
requires (std::is_same_v<Args, Ch> && ...)
|
||||||
inline constexpr string<S, Ch>::string (Args... parArgs) :
|
constexpr inline string<S, Ch>::string (Args... parArgs) :
|
||||||
m_data{parArgs...}
|
m_data{parArgs...}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
@ -87,23 +84,59 @@ namespace wren {
|
||||||
(set_single_for_call(vm, Indices + 1, args), ...);
|
(set_single_for_call(vm, Indices + 1, args), ...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename R, auto Method, typename... Args> struct MakeMethodBindableConstNonConst {
|
template <auto V>
|
||||||
private:
|
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>
|
template <int... Indices>
|
||||||
static R call_implem (std::integer_sequence<int, Indices...>, VM& vm) {
|
static R call_implem (std::integer_sequence<int, Indices...>, VM& vm) {
|
||||||
static_assert(sizeof...(Indices) == sizeof...(Args), "Mismatching argument count");
|
static_assert(sizeof...(Indices) == sizeof...(Args), "Mismatching argument count");
|
||||||
T* obj = foreign<T>(vm, 0);
|
T* obj = foreign<T>(vm, 0);
|
||||||
return (obj->*Method)(get<std::decay_t<Args>>(vm, Indices + 1)...);
|
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 foreign_method_t make() {
|
static R call_implem (std::integer_sequence<int, Indices...>, VM& vm) {
|
||||||
return [](VM& vm, ModuleAndName) {
|
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>) {
|
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 {
|
else {
|
||||||
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
|
//TODO: check for errors
|
||||||
vm.ensure_slots(1);
|
vm.ensure_slots(1);
|
||||||
if constexpr (std::is_fundamental<R>::value) {
|
if constexpr (std::is_fundamental<R>::value) {
|
||||||
|
@ -112,24 +145,29 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <auto V> struct MakeMethodBindable;
|
template <typename T, void(T::*Method)(VM&,ModuleAndName)> struct MakeFunctionBindable<Method> {
|
||||||
template <typename T, void(T::*Method)(VM&)> struct MakeMethodBindable<Method> {
|
foreign_method_t operator()() const {
|
||||||
static foreign_method_t make() {
|
return [](VM& vm, ModuleAndName mn) {
|
||||||
return [](VM& vm, ModuleAndName) {
|
|
||||||
T* obj = foreign<T>(vm, 0);
|
T* obj = foreign<T>(vm, 0);
|
||||||
(obj->*Method)(vm);
|
(obj->*Method)(vm, mn);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename R, typename... Args, R(T::*Method)(Args...)> struct MakeMethodBindable<Method> : MakeMethodBindableConstNonConst<T, R, Method, Args...> {};
|
template <void(*Function)(VM&,ModuleAndName)> struct MakeFunctionBindable<Function> {
|
||||||
template <typename T, typename R, typename... Args, R(T::*Method)(Args...)const> struct MakeMethodBindable<Method> : MakeMethodBindableConstNonConst<T, R, Method, Args...> {};
|
foreign_method_t operator()() const {
|
||||||
|
return [](VM& vm, ModuleAndName mn) {
|
||||||
|
(*Function)(vm, mn);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
} //namespace detail
|
} //namespace detail
|
||||||
|
|
||||||
inline void interpret (VM& vm, const std::string& module_name, const std::string& script) {
|
inline void interpret (VM& vm, const std::string& module_name, const std::string& script) {
|
||||||
|
@ -198,7 +236,5 @@ namespace wren {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <auto V>
|
template <auto V>
|
||||||
inline foreign_method_t make_method_bindable() {
|
constexpr detail::MakeFunctionBindable<V> make_function_bindable;
|
||||||
return detail::MakeMethodBindable<V>::make();
|
|
||||||
}
|
|
||||||
} //namespace wren
|
} //namespace wren
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue