Implement CallbackManager

With it users don't have to provide a foreign_method_fn()
function anymore, it's become optional.
This commit is contained in:
King_DuckZ 2022-04-29 18:16:57 +02:00
commit fa0183a3bf
8 changed files with 272 additions and 22 deletions

View file

@ -17,6 +17,7 @@
#include "wrenpp/vm_fun.hpp"
#include "wrenpp/def_configuration.hpp"
#include "wrenpp/callback_manager.hpp"
#include <chrono>
#include <thread>
#include <unistd.h>
@ -58,33 +59,15 @@ const char* raw_fetch_env (const char* name) noexcept {
void user_get_env (wren::VM& vm) {
set(vm, 0, raw_fetch_env(wren::get<const char*>(vm, 1)));
}
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 << "VM " << vm << " requested foreign method " <<
// class_name << "::" << signature <<
// " in module " << module << "\n";
if ("User" == class_name and "main" == module and "get_env(_)" == signature)
return &user_get_env;
return nullptr;
}
};
} //unnamed namespace
int main() {
//typedef wren::ModuleAndName MN;
MyConfig config;
wren::DefConfiguration config;
wren::VM vm(&config, nullptr);
vm.callback_manager().add_callback(true, "main", "User", "get_env(_)", &user_get_env);
vm.interpret("main", g_script);
wren::call<void>(vm, {"main", "the_user"}, "greet");