Allow users to store a custom pointer into the VM object.

This commit is contained in:
King_DuckZ 2020-05-01 17:44:09 +02:00
commit f33900b351
4 changed files with 249 additions and 29 deletions

View file

@ -84,7 +84,7 @@ int main() {
//typedef wren::ModuleAndName MN;
MyConfig config;
wren::VM vm(&config);
wren::VM vm(&config, nullptr);
vm.interpret("main", g_script);
wren::call<void>(vm, {"main", "the_user"}, "greet");

View file

@ -120,9 +120,11 @@ namespace wren {
detail::Callbacks callbacks;
DynafuncMaker dynafunc;
WrenVM* wvm;
void* user_data;
std::uint32_t user_data_type;
};
VM::VM (Configuration* conf, const detail::Callbacks& cb) :
VM::VM (Configuration* conf, const detail::Callbacks& cb, void* user_data, std::uint32_t user_data_type) :
m_local(std::make_unique<LocalData>(cb))
{
WrenConfiguration wconf;
@ -157,6 +159,9 @@ namespace wren {
m_local->wvm = wrenNewVM(&wconf);
if (not m_local->wvm)
throw std::runtime_error("Failed to initialize Wren VM");
m_local->user_data = user_data;
m_local->user_data_type = user_data_type;
}
VM::~VM() noexcept = default;
@ -165,6 +170,19 @@ namespace wren {
return &m_local->dynafunc;
}
std::uint32_t VM::user_data_type() const {
return m_local->user_data_type;
}
void* VM::void_user_data() {
return m_local->user_data;
}
void VM::set_user_data (void* user_data, std::uint32_t user_data_type) {
m_local->user_data = user_data;
m_local->user_data_type = user_data_type;
}
void VM::interpret (const char* module_name, const char* script) {
using std::string;
using std::runtime_error;
@ -183,6 +201,30 @@ namespace wren {
wrenReleaseHandle(m_local->wvm, handle);
}
void VM::set_slot_string (int slot_num, const char* value) {
wrenSetSlotString(m_local->wvm, slot_num, value);
}
void VM::set_slot_double (int slot_num, double value) {
wrenSetSlotDouble(m_local->wvm, slot_num, value);
}
void VM::set_slot_bool (int slot_num, bool value) {
wrenSetSlotBool(m_local->wvm, slot_num, value);
}
void VM::set_slot_null (int slot_num) {
wrenSetSlotNull(m_local->wvm, slot_num);
}
void VM::set_slot_bytes (int slot_num, const char* bytes, std::size_t length) {
wrenSetSlotBytes(m_local->wvm, slot_num, bytes, length);
}
void* VM::set_slot_new_foreign (int slot_num, int class_slot, std::size_t size) {
return wrenSetSlotNewForeign(m_local->wvm, slot_num, class_slot, size);
}
bool VM::slot_bool (int slot_num) {
assert(SlotType::Bool == slot_type(slot_num));
return wrenGetSlotBool(m_local->wvm, slot_num);
@ -210,28 +252,13 @@ namespace wren {
return wrenGetSlotForeign(m_local->wvm, slot_num);
}
void VM::set_slot_string (int slot_num, const char* value) {
wrenSetSlotString(m_local->wvm, slot_num, value);
void VM::set_user_data (std::nullptr_t) {
m_local->user_data = nullptr;
m_local->user_data_type = detail::type_id<std::nullptr_t>();
}
void VM::set_slot_double (int slot_num, double value) {
wrenSetSlotDouble(m_local->wvm, slot_num, value);
}
void VM::set_slot_bool (int slot_num, bool value) {
wrenSetSlotBool(m_local->wvm, slot_num, value);
}
void VM::set_slot_null (int slot_num) {
wrenSetSlotNull(m_local->wvm, slot_num);
}
void VM::set_slot_bytes (int slot_num, const char* bytes, std::size_t length) {
wrenSetSlotBytes(m_local->wvm, slot_num, bytes, length);
}
void* VM::set_slot_new_foreign (int slot_num, int class_slot, std::size_t size) {
return wrenSetSlotNewForeign(m_local->wvm, slot_num, class_slot, size);
bool VM::has_user_data() const {
return nullptr != m_local->user_data;
}
void VM::ensure_slots (int num_slots) {