Refactoring.

VM now aims to only wrap Wren's c functions as closely as
possible. All the fun stuff should be put into vm_fun.hpp.
This commit is contained in:
King_DuckZ 2020-04-30 18:32:41 +02:00
commit 2755371792
7 changed files with 173 additions and 108 deletions

View file

@ -4,7 +4,6 @@
#include <wren.hpp>
#include <cassert>
#include <stdexcept>
#include <iterator>
namespace wren {
namespace {
@ -150,80 +149,51 @@ namespace wren {
}
}
void VM::interpret (const std::string& module_name, const std::string& script) {
return interpret(module_name.c_str(), script.c_str());
}
void VM::release (Handle& handle) noexcept {
void VM::release_handle (Handle& handle) noexcept {
if (handle)
wrenReleaseHandle(m_local->wvm, handle);
}
template <> bool VM::slot (int slot_num) {
bool VM::slot_bool (int slot_num) {
assert(SlotType::Bool == slot_type(slot_num));
return wrenGetSlotBool(m_local->wvm, slot_num);
}
template <> std::string VM::slot (int slot_num) {
const char* VM::slot_string (int slot_num) {
assert(SlotType::String == slot_type(slot_num));
return wrenGetSlotString(m_local->wvm, slot_num);
}
template <> double VM::slot (int slot_num) {
double VM::slot_double (int slot_num) {
assert(SlotType::Num == slot_type(slot_num));
return wrenGetSlotDouble(m_local->wvm, slot_num);
}
template <> std::vector<char> VM::slot (int slot_num) {
std::pair<const char*, int> VM::slot_bytes (int slot_num) {
assert(SlotType::String == slot_type(slot_num));
int length;
const char* const data = wrenGetSlotBytes(m_local->wvm, slot_num, &length);
return std::vector<char>{data, data + length};
return {data, length};
}
template <>
int VM::slot (int slot_num) {
assert(SlotType::Num == slot_type(slot_num));
return static_cast<int>(slot<double>(slot_num));
}
std::string_view VM::slot_string_view (int slot_num) {
assert(SlotType::String == slot_type(slot_num));
int length;
const char* const data = wrenGetSlotBytes(m_local->wvm, slot_num, &length);
return {data, static_cast<std::size_t>(length)};
}
void VM::set (int slot_num, const std::string& value) {
wrenSetSlotString(m_local->wvm, slot_num, value.c_str());
}
void VM::set (int slot_num, const char* value) {
void VM::set_slot_string (int slot_num, const char* value) {
wrenSetSlotString(m_local->wvm, slot_num, value);
}
void VM::set (int slot_num, std::string_view value) {
wrenSetSlotBytes(m_local->wvm, slot_num, value.data(), value.size());
}
void VM::set (int slot_num, const char* beg, const char* end) {
wrenSetSlotBytes(m_local->wvm, slot_num, beg, std::distance(beg, end));
}
void VM::set (int slot_num, double value) {
void VM::set_slot_double (int slot_num, double value) {
wrenSetSlotDouble(m_local->wvm, slot_num, value);
}
void VM::set (int slot_num, bool value) {
void VM::set_slot_bool (int slot_num, bool value) {
wrenSetSlotBool(m_local->wvm, slot_num, value);
}
void VM::set (int slot_num, const std::vector<char>& value) {
wrenSetSlotBytes(m_local->wvm, slot_num, value.data(), value.size());
void VM::set_slot_null (int slot_num) {
wrenSetSlotNull(m_local->wvm, slot_num);
}
void VM::set (int slot_num, std::nullptr_t) {
wrenSetSlotNull(m_local->wvm, slot_num);
void VM::set_slot_bytes (int slot_num, const char* bytes, std::size_t length) {
wrenSetSlotBytes(m_local->wvm, slot_num, bytes, length);
}
void VM::ensure_slots (int num_slots) {
@ -238,11 +208,7 @@ namespace wren {
wrenGetVariable(m_local->wvm, module, name, slot);
}
void VM::variable(const ModuleAndName& mod_and_name, int slot) {
this->variable(std::get<0>(mod_and_name), std::get<1>(mod_and_name), slot);
}
void VM::variable (const Handle& handle, int slot) {
void VM::set_slot_handle (const Handle& handle, int slot) {
wrenSetSlotHandle(m_local->wvm, slot, handle);
}