From ee587d0b9e142c15316a4f16f0ec16e682089073 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 28 Apr 2020 21:05:53 +0200 Subject: [PATCH] Add some more functions, simplify code --- src/wren/vm.cpp | 40 +++++++++++++++++++++------------------- src/wren/vm.hpp | 1 + 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/wren/vm.cpp b/src/wren/vm.cpp index 6a4283b..706b569 100644 --- a/src/wren/vm.cpp +++ b/src/wren/vm.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace wren { namespace { @@ -68,17 +69,6 @@ namespace wren { retval.finalize = funcs.finalize; return retval; } - - template 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 struct VM::LocalData { @@ -169,9 +159,22 @@ namespace wren { wrenReleaseHandle(m_local->wvm, handle); } - template - T VM::slot (int slot_num) { - return slot_specialized(m_local->wvm, slot_num); + template <> bool VM::slot (int slot_num) { + return wrenGetSlotBool(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 VM::slot (int slot_num) { + int length; + const char* const data = wrenGetSlotBytes(m_local->wvm, slot_num, &length); + return std::vector{data, data + length}; } template <> @@ -183,6 +186,10 @@ namespace wren { 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) { wrenGetVariable(m_local->wvm, module, name, slot); } @@ -194,9 +201,4 @@ namespace wren { void VM::variable (const Handle& handle, int slot) { wrenSetSlotHandle(m_local->wvm, slot, handle); } - - template bool VM::slot(int); - template std::string VM::slot(int); - template double VM::slot(int); - template int VM::slot(int); } //namespace wren diff --git a/src/wren/vm.hpp b/src/wren/vm.hpp index 2cb6594..52f7863 100644 --- a/src/wren/vm.hpp +++ b/src/wren/vm.hpp @@ -63,6 +63,7 @@ namespace wren { void release (Handle& handle) noexcept; template T slot (int slot_num); void ensure_slots(int num_slots); + int slot_count(); void variable(const char* module, const char* name, int slot); void variable(const ModuleAndName& mod_and_name, int slot); void variable(const Handle& handle, int slot);