Bugfixes and improvements, simplify examples
This commit is contained in:
parent
fa0183a3bf
commit
c2089568f3
4 changed files with 18 additions and 53 deletions
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "wrenpp/def_configuration.hpp"
|
||||
#include "wrenpp/vm_fun.hpp"
|
||||
#include "wrenpp/callback_manager.hpp"
|
||||
#include <string_view>
|
||||
#include <algorithm>
|
||||
#include <ctime>
|
||||
|
@ -107,24 +108,6 @@ System.print("You have %(cale.appointment_count()) appointment(s)")
|
|||
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::VM* vm,
|
||||
std::string_view module,
|
||||
|
@ -145,6 +128,10 @@ int main() {
|
|||
|
||||
MyConf conf;
|
||||
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);
|
||||
|
||||
Calendar* const cale = std::get<0>(wren::variables<Calendar>(vm, MN{"main", "cale"}));
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "wrenpp/vm_fun.hpp"
|
||||
#include "wrenpp/def_configuration.hpp"
|
||||
#include "wrenpp/callback_manager.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
@ -47,36 +48,6 @@ namespace {
|
|||
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::ifstream ifs(path);
|
||||
ifs >> std::noskipws;
|
||||
|
@ -92,8 +63,11 @@ int main() {
|
|||
std::random_device rand_dev;
|
||||
CustomData custom_data(rand_dev());
|
||||
|
||||
MyConfig config;
|
||||
wren::DefConfiguration config;
|
||||
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"));
|
||||
wren::call<void>(vm, {"main", "the_game"}, "play_game");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue