Add more methods.
This commit is contained in:
parent
dafce95c8f
commit
4a14d75ddb
3 changed files with 56 additions and 2 deletions
|
@ -4,6 +4,7 @@
|
|||
#include <wren.hpp>
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
#include <iterator>
|
||||
|
||||
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<char> 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<char>{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<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());
|
||||
}
|
||||
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue