Add some more functions, simplify code

This commit is contained in:
King_DuckZ 2020-04-28 21:05:53 +02:00
parent 2122cb628d
commit ee587d0b9e
2 changed files with 22 additions and 19 deletions

View file

@ -4,6 +4,7 @@
#include <wren.hpp> #include <wren.hpp>
#include <cassert> #include <cassert>
#include <stdexcept> #include <stdexcept>
#include <vector>
namespace wren { namespace wren {
namespace { namespace {
@ -68,17 +69,6 @@ namespace wren {
retval.finalize = funcs.finalize; retval.finalize = funcs.finalize;
return retval; return retval;
} }
template <typename T> T slot_specialized (WrenVM* vm, int slot_num);
template <> bool slot_specialized (WrenVM* vm, int slot_num) {
return wrenGetSlotBool(vm, slot_num);
}
template <> std::string slot_specialized (WrenVM* vm, int slot_num) {
return wrenGetSlotString(vm, slot_num);
}
template <> double slot_specialized (WrenVM* vm, int slot_num) {
return wrenGetSlotDouble(vm, slot_num);
}
} //unnamed namespace } //unnamed namespace
struct VM::LocalData { struct VM::LocalData {
@ -169,9 +159,22 @@ namespace wren {
wrenReleaseHandle(m_local->wvm, handle); wrenReleaseHandle(m_local->wvm, handle);
} }
template <typename T> template <> bool VM::slot (int slot_num) {
T VM::slot (int slot_num) { return wrenGetSlotBool(m_local->wvm, slot_num);
return slot_specialized<T>(m_local->wvm, slot_num); }
template <> std::string VM::slot (int slot_num) {
return wrenGetSlotString(m_local->wvm, slot_num);
}
template <> double VM::slot (int slot_num) {
return wrenGetSlotDouble(m_local->wvm, slot_num);
}
template <> std::vector<char> VM::slot (int slot_num) {
int length;
const char* const data = wrenGetSlotBytes(m_local->wvm, slot_num, &length);
return std::vector<char>{data, data + length};
} }
template <> template <>
@ -183,6 +186,10 @@ namespace wren {
wrenEnsureSlots(m_local->wvm, num_slots); wrenEnsureSlots(m_local->wvm, num_slots);
} }
int VM::slot_count() {
return wrenGetSlotCount(m_local->wvm);
}
void VM::variable(const char* module, const char* name, int slot) { void VM::variable(const char* module, const char* name, int slot) {
wrenGetVariable(m_local->wvm, module, name, slot); wrenGetVariable(m_local->wvm, module, name, slot);
} }
@ -194,9 +201,4 @@ namespace wren {
void VM::variable (const Handle& handle, int slot) { void VM::variable (const Handle& handle, int slot) {
wrenSetSlotHandle(m_local->wvm, slot, handle); wrenSetSlotHandle(m_local->wvm, slot, handle);
} }
template bool VM::slot<bool>(int);
template std::string VM::slot<std::string>(int);
template double VM::slot<double>(int);
template int VM::slot<int>(int);
} //namespace wren } //namespace wren

View file

@ -63,6 +63,7 @@ namespace wren {
void release (Handle& handle) noexcept; void release (Handle& handle) noexcept;
template <typename T> T slot (int slot_num); template <typename T> T slot (int slot_num);
void ensure_slots(int num_slots); void ensure_slots(int num_slots);
int slot_count();
void variable(const char* module, const char* name, int slot); void variable(const char* module, const char* name, int slot);
void variable(const ModuleAndName& mod_and_name, int slot); void variable(const ModuleAndName& mod_and_name, int slot);
void variable(const Handle& handle, int slot); void variable(const Handle& handle, int slot);