From 4a14d75ddbe7245911c590c9a992ae3c9f636ec7 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 30 Apr 2020 16:45:10 +0200 Subject: [PATCH] Add more methods. --- src/wren/handle.hpp | 2 +- src/wren/vm.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/wren/vm.hpp | 12 +++++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/wren/handle.hpp b/src/wren/handle.hpp index f4fcf7e..e29d799 100644 --- a/src/wren/handle.hpp +++ b/src/wren/handle.hpp @@ -10,7 +10,7 @@ namespace wren { Handle() noexcept; Handle (const Handle&) = delete; Handle (Handle&& other) noexcept; - explicit Handle (VM* vm, WrenHandle* handle) noexcept : + Handle (VM* vm, WrenHandle* handle) noexcept : m_handle(handle), m_vm(vm) { diff --git a/src/wren/vm.cpp b/src/wren/vm.cpp index 64ab275..03f3db8 100644 --- a/src/wren/vm.cpp +++ b/src/wren/vm.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace wren { namespace { @@ -159,18 +160,22 @@ namespace wren { } template <> bool VM::slot (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) { + assert(SlotType::String == slot_type(slot_num)); return wrenGetSlotString(m_local->wvm, slot_num); } template <> double VM::slot (int slot_num) { + assert(SlotType::Num == slot_type(slot_num)); return wrenGetSlotDouble(m_local->wvm, slot_num); } template <> std::vector VM::slot (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{data, data + length}; @@ -178,9 +183,17 @@ namespace wren { template <> int VM::slot (int slot_num) { + assert(SlotType::Num == slot_type(slot_num)); return static_cast(slot(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(length)}; + } + void VM::set (int slot_num, const std::string& value) { wrenSetSlotString(m_local->wvm, slot_num, value.c_str()); } @@ -189,6 +202,14 @@ namespace wren { 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) { wrenSetSlotDouble(m_local->wvm, slot_num, value); } @@ -224,4 +245,27 @@ namespace wren { void VM::variable (const Handle& handle, int slot) { wrenSetSlotHandle(m_local->wvm, slot, handle); } + + SlotType VM::slot_type(int slot_num) { + const WrenType tp = wrenGetSlotType(m_local->wvm, slot_num); + switch (tp) { + case WREN_TYPE_NUM: return SlotType::Num; + case WREN_TYPE_BOOL: return SlotType::Bool; + case WREN_TYPE_LIST: return SlotType::List; + case WREN_TYPE_NULL: return SlotType::Null; + case WREN_TYPE_STRING: return SlotType::String; + case WREN_TYPE_FOREIGN: return SlotType::Foreign; + case WREN_TYPE_UNKNOWN: return SlotType::Unknown; + }; + assert(false); + return SlotType::Unknown; + } + + Handle VM::slot_handle(int slot_num) { + return {this, wrenGetSlotHandle(m_local->wvm, slot_num)}; + } + + Handle VM::make_call_handle(const char* signature) { + return {this, wrenMakeCallHandle(m_local->wvm, signature)}; + } } //namespace wren diff --git a/src/wren/vm.hpp b/src/wren/vm.hpp index 758afe8..970dd09 100644 --- a/src/wren/vm.hpp +++ b/src/wren/vm.hpp @@ -11,12 +11,12 @@ # include #endif #include +#include namespace wren { class Configuration; class VM; class DynafuncMaker; - class Handle; typedef void(*foreign_method_t)(VM*); typedef void(*finalizer_t)(void*); @@ -27,6 +27,10 @@ namespace wren { finalizer_t finalize; }; + enum class SlotType { + Bool, Num, Foreign, List, Null, String, Unknown + }; + namespace detail { struct Callbacks { void (*write_fn)(Configuration&, VM*, const char*) {nullptr}; @@ -63,8 +67,11 @@ namespace wren { void interpret (const std::string& module_name, const std::string& script); void release (Handle& handle) noexcept; template T slot (int slot_num); + std::string_view slot_string_view (int slot_num); void set (int slot_num, const std::string& value); void set (int slot_num, const char* value); + void set (int slot_num, std::string_view value); + void set (int slot_num, const char* beg, const char* end); void set (int slot_num, double value); void set (int slot_num, bool value); void set (int slot_num, const std::vector& value); @@ -74,6 +81,9 @@ namespace wren { 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); + SlotType slot_type(int slot_num); + Handle slot_handle(int slot_num); + Handle make_call_handle(const char* signature); private: struct LocalData;