diff --git a/examples/calendar/main.cpp b/examples/calendar/main.cpp index 6fea8f8..1b33e56 100644 --- a/examples/calendar/main.cpp +++ b/examples/calendar/main.cpp @@ -18,6 +18,7 @@ #include "wrenpp/def_configuration.hpp" #include "wrenpp/vm_fun.hpp" #include "wrenpp/callback_manager.hpp" +#include "wrenpp/class_manager.hpp" #include #include #include @@ -107,19 +108,6 @@ System.print("You have %(cale.appointment_count()) appointment(s)") } return nullptr; } - - wren::foreign_class_t foreign_class_fn( - wren::VM* vm, - std::string_view module, - std::string_view class_name - ) { - if (module == "calendar" and class_name == "Calendar") { - return wren::make_foreign_class(); - } - else { - return {nullptr, nullptr}; - } - } }; } //unnamed namespace @@ -131,6 +119,7 @@ int main() { 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.class_manager().add_class_maker("calendar", "Calendar", &wren::make_foreign_class); vm.interpret("main", g_test_script); diff --git a/include/wrenpp/callback_manager.hpp b/include/wrenpp/callback_manager.hpp index 65bf237..a3e5408 100644 --- a/include/wrenpp/callback_manager.hpp +++ b/include/wrenpp/callback_manager.hpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace wren::detail { class FullSignatureOwning; diff --git a/include/wrenpp/class_manager.hpp b/include/wrenpp/class_manager.hpp new file mode 100644 index 0000000..d3345fb --- /dev/null +++ b/include/wrenpp/class_manager.hpp @@ -0,0 +1,88 @@ +/* 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 . + */ + +#pragma once + +#include "wren_types.hpp" +#include "strings_in_vector.hpp" +#include +#include +#include + +namespace wren { + namespace detail { + class ClassNameBase { + public: + ClassNameBase (const std::string_view& module_name, const std::string_view& class_name); + ClassNameBase (const ClassNameBase& other) = default; + + std::size_t module_hash() const noexcept { return m_module_hash; } + std::size_t class_hash() const noexcept { return m_class_name_hash; } + + bool operator== (const ClassNameBase& other) const; + + private: + std::size_t m_module_hash; + std::size_t m_class_name_hash; + }; + + class ClassNameOwning : StringsInVector<2>, public ClassNameBase { + public: + ClassNameOwning (const std::string_view& module_name, const std::string_view& class_name); + + std::string_view module_name() const { return this->as_string_view<0>(); } + std::string_view class_name() const { return this->as_string_view<1>(); } + + using StringsInVector<2>::operator<; + bool operator== (const ClassNameOwning& other) const; + }; + + class TempClassName : public ClassNameBase { + public: + TempClassName (std::string_view module_name, std::string_view class_name); + TempClassName (const ClassNameOwning& model); + bool operator== (const TempClassName& other) const; + + private: + std::string_view m_module_name, m_class_name; + }; + + struct ClassNameEqual { + using is_transparent = std::true_type; + bool operator()(TempClassName left, TempClassName right) const; + }; + + struct ClassNameHash { + using is_transparent = std::true_type; + std::size_t operator()(TempClassName value) const; + }; + } //namespace detail + + class ClassManager { + typedef foreign_class_t(*make_foreign_class_t)(); + typedef std::unordered_map ClassMap; + public: + ClassManager(); + ~ClassManager() noexcept; + + void add_class_maker (std::string_view module_name, std::string_view class_name, make_foreign_class_t); + foreign_class_t make_class(std::string_view module_name, std::string_view class_name); + + private: + ClassMap m_classes; + }; +} //namespace wren diff --git a/include/wrenpp/def_configuration.hpp b/include/wrenpp/def_configuration.hpp index a9614d7..8bdd38c 100644 --- a/include/wrenpp/def_configuration.hpp +++ b/include/wrenpp/def_configuration.hpp @@ -25,8 +25,9 @@ namespace wren { class DefConfiguration : public Configuration { public: static void write_fn (VM*, wren_string_t text); - static void error_fn (VM*, ErrorType, wren_string_t module, int line, wren_string_t msg); + static void error_fn (VM*, ErrorType, wren_string_t module_name, int line, wren_string_t msg); static void* reallocate_fn(void* ptr, std::size_t size); - static foreign_method_t foreign_method_fn (VM* vm, wren_string_t module, wren_string_t class_name, bool is_static, wren_string_t signature); + static foreign_method_t foreign_method_fn (VM* vm, wren_string_t module_name, wren_string_t class_name, bool is_static, wren_string_t signature); + static foreign_class_t foreign_class_fn (wren::VM* vm, std::string_view module_name, std::string_view class_name); }; } //namespace wren diff --git a/include/wrenpp/vm.hpp b/include/wrenpp/vm.hpp index 7891731..0cfef0b 100644 --- a/include/wrenpp/vm.hpp +++ b/include/wrenpp/vm.hpp @@ -36,14 +36,7 @@ namespace wren { class VM; class DynafuncMaker; class CallbackManager; - - typedef void(*foreign_method_t)(VM&); - typedef void(*finalizer_t)(void*); - - struct foreign_class_t { - foreign_method_t allocate; - finalizer_t finalize; - }; + class ClassManager; enum class SlotType { Bool, Num, Foreign, List, Null, String, Unknown, Map @@ -114,6 +107,7 @@ namespace wren { void reset (Configuration* conf); CallbackManager& callback_manager(); + ClassManager& class_manager(); private: struct LocalData; diff --git a/include/wrenpp/wren_types.hpp b/include/wrenpp/wren_types.hpp index db4540a..95c1029 100644 --- a/include/wrenpp/wren_types.hpp +++ b/include/wrenpp/wren_types.hpp @@ -15,13 +15,21 @@ * along with wrenpp. If not, see . */ +#pragma once + #include #include namespace wren { class VM; - typedef std::string_view wren_string_t; - typedef void(*foreign_method_t)(VM&); typedef std::tuple ModuleAndName; + typedef std::string_view wren_string_t; + typedef void(*finalizer_t)(void*); + typedef void(*foreign_method_t)(VM&); + + struct foreign_class_t { + foreign_method_t allocate; + finalizer_t finalize; + }; } //namespace wren diff --git a/meson.build b/meson.build index 3ced8a8..62f099e 100644 --- a/meson.build +++ b/meson.build @@ -62,6 +62,7 @@ wrenpp = library(meson.project_name(), 'src/handle.cpp', 'src/vm_fun.cpp', 'src/callback_manager.cpp', + 'src/class_manager.cpp', dependencies: [wren_dep], include_directories: public_incl, install: true, diff --git a/src/class_manager.cpp b/src/class_manager.cpp new file mode 100644 index 0000000..f0499c3 --- /dev/null +++ b/src/class_manager.cpp @@ -0,0 +1,97 @@ +/* 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 . + */ + +#include "wrenpp/class_manager.hpp" + +namespace wren { + namespace detail { + ClassNameBase::ClassNameBase (const std::string_view& module_name, const std::string_view& class_name) : + m_module_hash(std::hash{}(module_name)), + m_class_name_hash(std::hash{}(class_name)) + { + } + + bool ClassNameBase::operator==(const ClassNameBase& other) const { + return m_module_hash == other.m_module_hash and + m_class_name_hash == other.m_class_name_hash; + } + + ClassNameOwning::ClassNameOwning(const std::string_view& module_name, const std::string_view& class_name) : + StringsInVector<2>(module_name, class_name), + ClassNameBase(module_name, class_name) + { + } + + bool ClassNameOwning::operator== (const ClassNameOwning& other) const { + return + *static_cast(this) == static_cast(other) or + *static_cast*>(this) == static_cast&>(other); + } + + TempClassName::TempClassName(std::string_view module_name, std::string_view class_name) : + ClassNameBase(module_name, class_name), + m_module_name(module_name), + m_class_name(class_name) + { + } + + TempClassName::TempClassName (const ClassNameOwning& other) : + ClassNameBase(other), + m_module_name(other.module_name()), + m_class_name(other.class_name()) + { + } + + bool TempClassName::operator== (const TempClassName& other) const { + return *static_cast(this) == static_cast(other) and + m_module_name == other.m_module_name and + m_class_name == other.m_class_name; + } + + bool ClassNameEqual::operator()(TempClassName left, TempClassName right) const { + return left == right; + } + + std::size_t ClassNameHash::operator()(TempClassName value) const { + return value.module_hash() ^ (value.class_hash() << 1); + } + } //namespace detail + + ClassManager::ClassManager() = default; + ClassManager::~ClassManager() noexcept = default; + + void ClassManager::add_class_maker (std::string_view module_name, std::string_view class_name, make_foreign_class_t maker) { + using detail::TempClassName; + using detail::ClassNameOwning; + + auto it_found = m_classes.find(TempClassName{module_name, class_name}); + if (m_classes.cend() == it_found) + m_classes.insert(it_found, std::make_pair(ClassNameOwning{module_name, class_name}, maker)); + else + it_found->second = maker; + } + + foreign_class_t ClassManager::make_class (std::string_view module_name, std::string_view class_name) { + using detail::TempClassName; + + auto it_found = m_classes.find(TempClassName{module_name, class_name}); + if (m_classes.cend() != it_found) + return (*it_found->second)(); + else + return {nullptr, nullptr}; + } +} //namespace wren diff --git a/src/def_configuration.cpp b/src/def_configuration.cpp index a050f82..2763549 100644 --- a/src/def_configuration.cpp +++ b/src/def_configuration.cpp @@ -17,6 +17,7 @@ #include "wrenpp/def_configuration.hpp" #include "wrenpp/callback_manager.hpp" +#include "wrenpp/class_manager.hpp" #include "wrenpp/vm.hpp" #include #include @@ -68,7 +69,11 @@ 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); + foreign_method_t DefConfiguration::foreign_method_fn (VM* vm, wren_string_t module_name, wren_string_t class_name, bool is_static, wren_string_t signature) { + return vm->callback_manager().callback(is_static, module_name, class_name, signature); + } + + foreign_class_t DefConfiguration::foreign_class_fn (wren::VM* vm, std::string_view module_name, std::string_view class_name) { + return vm->class_manager().make_class(module_name, class_name); } } //namespace wren diff --git a/src/dynafunc_maker.hpp b/src/dynafunc_maker.hpp index 48a2e54..ffd1123 100644 --- a/src/dynafunc_maker.hpp +++ b/src/dynafunc_maker.hpp @@ -17,13 +17,11 @@ #pragma once +#include "wrenpp/wren_types.hpp" #include #include namespace wren { - class VM; - typedef void(*foreign_method_t)(VM&); - class DynafuncMaker { public: DynafuncMaker(); diff --git a/src/vm.cpp b/src/vm.cpp index 3b3a942..ecc1d1b 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -18,6 +18,7 @@ #include "wrenpp/vm.hpp" #include "wrenpp/configuration.hpp" #include "wrenpp/callback_manager.hpp" +#include "wrenpp/class_manager.hpp" #include "dynafunc_maker.hpp" #include #include @@ -154,6 +155,7 @@ namespace wren { } CallbackManager callback_manager; + ClassManager class_manager; detail::Callbacks callbacks; DynafuncMaker dynafunc; WrenVM* wvm; @@ -322,6 +324,10 @@ namespace wren { return m_local->callback_manager; } + ClassManager& VM::class_manager() { + return m_local->class_manager; + } + void VM::ensure_slots (int num_slots) { wrenEnsureSlots(m_local->wvm, num_slots); }