Implement ClassManager

Very similar to CallbackManager in its functioning.
This commit is contained in:
King_DuckZ 2022-05-14 17:06:25 +02:00
parent 74670f4683
commit 258237cbf3
11 changed files with 217 additions and 29 deletions

View file

@ -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 <string_view>
#include <algorithm>
#include <ctime>
@ -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<Calendar>();
}
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<Calendar>);
vm.interpret("main", g_test_script);

View file

@ -22,6 +22,7 @@
#include <unordered_map>
#include <vector>
#include <string_view>
#include <type_traits>
namespace wren::detail {
class FullSignatureOwning;

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "wren_types.hpp"
#include "strings_in_vector.hpp"
#include <unordered_map>
#include <string>
#include <type_traits>
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<detail::ClassNameOwning, make_foreign_class_t, detail::ClassNameHash, detail::ClassNameEqual> 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

View file

@ -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

View file

@ -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;

View file

@ -15,13 +15,21 @@
* along with wrenpp. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string_view>
#include <tuple>
namespace wren {
class VM;
typedef std::tuple<const char*, const char*> ModuleAndName;
typedef std::string_view wren_string_t;
typedef void(*finalizer_t)(void*);
typedef void(*foreign_method_t)(VM&);
typedef std::tuple<const char*, const char*> ModuleAndName;
struct foreign_class_t {
foreign_method_t allocate;
finalizer_t finalize;
};
} //namespace wren

View file

@ -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,

97
src/class_manager.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<std::string_view>{}(module_name)),
m_class_name_hash(std::hash<std::string_view>{}(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<const ClassNameBase*>(this) == static_cast<const ClassNameBase&>(other) or
*static_cast<const StringsInVector<2>*>(this) == static_cast<const StringsInVector<2>&>(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<const ClassNameBase*>(this) == static_cast<const ClassNameBase&>(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

View file

@ -17,6 +17,7 @@
#include "wrenpp/def_configuration.hpp"
#include "wrenpp/callback_manager.hpp"
#include "wrenpp/class_manager.hpp"
#include "wrenpp/vm.hpp"
#include <iostream>
#include <algorithm>
@ -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

View file

@ -17,13 +17,11 @@
#pragma once
#include "wrenpp/wren_types.hpp"
#include <vector>
#include <cstddef>
namespace wren {
class VM;
typedef void(*foreign_method_t)(VM&);
class DynafuncMaker {
public:
DynafuncMaker();

View file

@ -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 <wren.hpp>
#include <cassert>
@ -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);
}