Bugfixes and improvements, simplify examples

This commit is contained in:
King_DuckZ 2022-04-29 18:35:46 +02:00
parent fa0183a3bf
commit c2089568f3
4 changed files with 18 additions and 53 deletions

View file

@ -17,6 +17,7 @@
#include "wrenpp/def_configuration.hpp" #include "wrenpp/def_configuration.hpp"
#include "wrenpp/vm_fun.hpp" #include "wrenpp/vm_fun.hpp"
#include "wrenpp/callback_manager.hpp"
#include <string_view> #include <string_view>
#include <algorithm> #include <algorithm>
#include <ctime> #include <ctime>
@ -107,24 +108,6 @@ System.print("You have %(cale.appointment_count()) appointment(s)")
return nullptr; return nullptr;
} }
wren::foreign_method_t foreign_method_fn(
wren::VM* vm,
std::string_view module,
std::string_view class_name,
bool is_static,
std::string_view signature
) {
if (module == "calendar" and class_name == "Calendar") {
if (is_static and signature == "today()")
return &Calendar::today;
else if (not is_static and signature == "add_appointment(_)")
return wren::make_method_bindable<&Calendar::add_appointment>();
else if (not is_static and signature == "appointment_count()")
return wren::make_method_bindable<&Calendar::appointment_count>();
}
return nullptr;
}
wren::foreign_class_t foreign_class_fn( wren::foreign_class_t foreign_class_fn(
wren::VM* vm, wren::VM* vm,
std::string_view module, std::string_view module,
@ -145,6 +128,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)
.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>());
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"}));

View file

@ -17,6 +17,7 @@
#include "wrenpp/vm_fun.hpp" #include "wrenpp/vm_fun.hpp"
#include "wrenpp/def_configuration.hpp" #include "wrenpp/def_configuration.hpp"
#include "wrenpp/callback_manager.hpp"
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <fstream> #include <fstream>
@ -47,36 +48,6 @@ namespace {
set(vm, 0, cust_data->distrib(cust_data->generator)); set(vm, 0, cust_data->distrib(cust_data->generator));
} }
class MyConfig : public wren::DefConfiguration {
public:
wren::foreign_method_t foreign_method_fn (
wren::VM* vm,
std::string_view module,
std::string_view class_name,
bool is_static,
std::string_view signature
) {
//std::cout << "requested: \"" << module << "\" \"" <<
// class_name << "\" \"" << signature << "\" static: " <<
// std::boolalpha << is_static << '\n';
if ("main" == module) {
if ("Game" == class_name) {
if ("user_input()" == signature and is_static)
return &user_input;
else if ("random()" == signature and is_static)
return &random_num;
}
}
return nullptr;
}
//char* load_module_fn (wren::VM* vm, const char* name) {
// std::cout << "asked to load module \"" << name << "\"\n";
// return nullptr;
//}
};
std::string load_file (const std::string& path) { std::string load_file (const std::string& path) {
std::ifstream ifs(path); std::ifstream ifs(path);
ifs >> std::noskipws; ifs >> std::noskipws;
@ -92,8 +63,11 @@ int main() {
std::random_device rand_dev; std::random_device rand_dev;
CustomData custom_data(rand_dev()); CustomData custom_data(rand_dev());
MyConfig 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)
.add_callback(true, "main", "Game", "random()", &random_num);
interpret(vm, "main", load_file("main.wren")); interpret(vm, "main", load_file("main.wren"));
wren::call<void>(vm, {"main", "the_game"}, "play_game"); wren::call<void>(vm, {"main", "the_game"}, "play_game");

View file

@ -116,7 +116,7 @@ namespace wren {
CallbackManager(); CallbackManager();
~CallbackManager() noexcept; ~CallbackManager() noexcept;
void 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, foreign_method_t 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

@ -60,7 +60,8 @@ namespace wren::detail {
TempFullSignature::TempFullSignature (const FullSignatureOwning& full_sig) : TempFullSignature::TempFullSignature (const FullSignatureOwning& full_sig) :
FullSignatureBase(full_sig), FullSignatureBase(full_sig),
m_module_name(full_sig.module_name()), m_module_name(full_sig.module_name()),
m_class_name(full_sig.class_name()) m_class_name(full_sig.class_name()),
m_signature(full_sig.signature())
{ } { }
TempFullSignature::TempFullSignature (std::string_view module_name, std::string_view class_name, std::string_view signature) : TempFullSignature::TempFullSignature (std::string_view module_name, std::string_view class_name, std::string_view signature) :
@ -72,7 +73,8 @@ namespace wren::detail {
bool FullSignatureEqual::operator()(TempFullSignature left, TempFullSignature right) const { bool FullSignatureEqual::operator()(TempFullSignature left, TempFullSignature right) const {
return left.class_name() == right.class_name() and return left.class_name() == right.class_name() and
left.module_name() == right.module_name(); left.module_name() == right.module_name() and
left.signature() == right.signature();
} }
std::size_t FullSignatureHash::operator() (TempFullSignature key) const { std::size_t FullSignatureHash::operator() (TempFullSignature key) const {
@ -88,7 +90,7 @@ namespace wren {
CallbackManager::CallbackManager() = default; CallbackManager::CallbackManager() = default;
CallbackManager::~CallbackManager() noexcept = default; CallbackManager::~CallbackManager() noexcept = default;
void 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, foreign_method_t cb) {
using detail::FullSignatureOwning; using detail::FullSignatureOwning;
using detail::TempFullSignature; using detail::TempFullSignature;
@ -98,6 +100,8 @@ namespace wren {
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}, cb));
else else
it_found->second = cb; it_found->second = cb;
return *this;
} }
foreign_method_t CallbackManager::callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature) const { foreign_method_t CallbackManager::callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature) const {