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

121
src/callback_manager.cpp Normal file
View file

@ -0,0 +1,121 @@
/* Copyright 2020-2022, Michele Santullo
* This file is part of wrenpp.
*
* Wrenpp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wrenpp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wrenpp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wrenpp/callback_manager.hpp"
#include <algorithm>
#include <cassert>
namespace wren::detail {
FullSignatureOwning::FullSignatureOwning (std::string_view module_name, std::string_view class_name, std::string_view signature) :
FullSignatureBase(std::hash<std::string_view>{}(module_name), std::hash<std::string_view>{}(class_name), std::hash<std::string_view>{}(signature))
{
m_full_name.resize(module_name.size() + 1 + class_name.size() + 1 + signature.size() + 1);
char* buff = m_full_name.data();
std::size_t buff_used = 0;
std::copy(module_name.cbegin(), module_name.cend(), buff + buff_used);
m_module_range.start = buff_used;
m_module_range.length = module_name.size();
buff[buff_used + module_name.size()] = '\0';
buff_used += module_name.size() + 1;
std::copy(class_name.cbegin(), class_name.cend(), buff + buff_used);
m_class_range.start = buff_used;
m_class_range.length = class_name.size();
buff[buff_used + class_name.size()] = '\0';
buff_used += class_name.size() + 1;
std::copy(signature.cbegin(), signature.cend(), buff + buff_used);
m_signature_range.start = buff_used;
m_signature_range.length = signature.size();
buff[buff_used + signature.size()] = '\0';
buff_used += signature.size() + 1;
assert(m_full_name[m_module_range.start + m_module_range.length] == '\0');
assert(m_full_name[m_class_range.start + m_module_range.length] == '\0');
assert(m_full_name[m_signature_range.start + m_signature_range.length] == '\0');
assert(this->module_name() == module_name);
assert(this->class_name() == class_name);
assert(this->signature() == signature);
}
std::string_view FullSignatureOwning::make_string_view(WordRange range) const {
return {m_full_name.data() + range.start, range.length};
}
TempFullSignature::TempFullSignature (const FullSignatureOwning& full_sig) :
FullSignatureBase(full_sig),
m_module_name(full_sig.module_name()),
m_class_name(full_sig.class_name())
{ }
TempFullSignature::TempFullSignature (std::string_view module_name, std::string_view class_name, std::string_view signature) :
FullSignatureBase(std::hash<std::string_view>{}(module_name), std::hash<std::string_view>{}(class_name), std::hash<std::string_view>{}(signature)),
m_module_name(module_name),
m_class_name(class_name),
m_signature(signature)
{ }
bool FullSignatureEqual::operator()(TempFullSignature left, TempFullSignature right) const {
return left.class_name() == right.class_name() and
left.module_name() == right.module_name();
}
std::size_t FullSignatureHash::operator() (TempFullSignature key) const {
//see example at https://en.cppreference.com/w/cpp/utility/hash
const std::size_t h1 = key.module_hash();
const std::size_t h2 = key.class_hash();
const std::size_t h3 = key.signature_hash();
return h1 ^ ((h2 ^ (h3 << 1)) << 1);
}
} //namespace wren::detail
namespace wren {
CallbackManager::CallbackManager() = 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) {
using detail::FullSignatureOwning;
using detail::TempFullSignature;
MethodMap& map = *method_map(is_static);
auto it_found = map.find(TempFullSignature{module_name, class_name, signature});
if (map.cend() == it_found)
map.insert(it_found, std::make_pair(FullSignatureOwning{module_name, class_name, signature}, cb));
else
it_found->second = cb;
}
foreign_method_t CallbackManager::callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature) const {
using detail::TempFullSignature;
const MethodMap* const map = method_map(is_static);
const auto it_found = map->find(TempFullSignature{module_name, class_name, signature});
if (map->cend() != it_found)
return it_found->second;
else
return nullptr;
}
auto CallbackManager::method_map(bool is_static) noexcept -> MethodMap* {
return (is_static ? &m_static_methods : &m_normal_methods);
}
auto CallbackManager::method_map(bool is_static) const noexcept -> const MethodMap* {
return const_cast<CallbackManager*>(this)->method_map(is_static);
}
} //namespace wren

View file

@ -16,6 +16,8 @@
*/
#include "wrenpp/def_configuration.hpp"
#include "wrenpp/callback_manager.hpp"
#include "wrenpp/vm.hpp"
#include <iostream>
#include <algorithm>
@ -65,4 +67,8 @@ namespace wren {
}
return nullptr;
}
foreign_method_t DefConfiguration::foreign_method_fn (VM* vm, wren_string_t module, wren_string_t class_name, bool is_static, wren_string_t signature) {
return vm->callback_manager().callback(is_static, module, class_name, signature);
}
} //namespace wren

View file

@ -17,6 +17,7 @@
#include "wrenpp/vm.hpp"
#include "wrenpp/configuration.hpp"
#include "wrenpp/callback_manager.hpp"
#include "dynafunc_maker.hpp"
#include <wren.hpp>
#include <cassert>
@ -152,6 +153,7 @@ namespace wren {
#endif
}
CallbackManager callback_manager;
detail::Callbacks callbacks;
DynafuncMaker dynafunc;
WrenVM* wvm;
@ -316,6 +318,10 @@ namespace wren {
m_local->dynafunc.clear();
}
CallbackManager& VM::callback_manager() {
return m_local->callback_manager;
}
void VM::ensure_slots (int num_slots) {
wrenEnsureSlots(m_local->wvm, num_slots);
}