Add more methods.
This commit is contained in:
parent
dafce95c8f
commit
4a14d75ddb
3 changed files with 56 additions and 2 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -11,12 +11,12 @@
|
|||
# include <concepts>
|
||||
#endif
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
|
||||
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 <typename T> 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<char>& 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;
|
||||
|
|
Loading…
Reference in a new issue