From f749b7fb18d4662c86bfb23a82cb4d31a23ff7c7 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 22 Apr 2022 22:04:09 +0200 Subject: [PATCH] call() will fail earlier when object for call doesn't exist --- include/wrenpp/vm.hpp | 7 +++++-- src/vm.cpp | 24 ++++++++++++++++++++++-- src/vm_fun.cpp | 2 +- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/include/wrenpp/vm.hpp b/include/wrenpp/vm.hpp index ea0b742..88a3208 100644 --- a/include/wrenpp/vm.hpp +++ b/include/wrenpp/vm.hpp @@ -79,7 +79,8 @@ namespace wren { void release_handle (Handle& handle) noexcept; void ensure_slots(int num_slots); int slot_count(); - void variable(const char* module, const char* name, int slot); + void variable(const char* module, const char* name, int slot) noexcept; + void variable_or_throw(const char* module, const char* name, int slot); void set_slot_handle(const Handle& handle, int slot); SlotType slot_type(int slot_num); Handle slot_handle(int slot_num); @@ -103,7 +104,9 @@ namespace wren { template U* user_data(); template void set_user_data (U* user_data); void set_user_data (std::nullptr_t); - bool has_user_data() const; + bool has_user_data() const noexcept; + bool has_module (const char* module) const noexcept; + bool has_variable (const char* module, const char* name) const noexcept; std::size_t dynafunc_byte_size() const; void reset (Configuration* conf); diff --git a/src/vm.cpp b/src/vm.cpp index 7985ff0..977f960 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -266,10 +266,18 @@ namespace wren { m_local->user_data_type = detail::type_id(); } - bool VM::has_user_data() const { + bool VM::has_user_data() const noexcept { return nullptr != m_local->user_data; } + bool VM::has_module(const char* module) const noexcept { + return wrenHasModule(m_local->wvm, module); + } + + bool VM::has_variable(const char* module, const char* name) const noexcept { + return wrenHasVariable(m_local->wvm, module, name); + } + std::size_t VM::dynafunc_byte_size() const { return m_local->dynafunc.total_memory(); } @@ -322,11 +330,23 @@ namespace wren { return wrenGetSlotCount(m_local->wvm); } - void VM::variable(const char* module, const char* name, int slot) { + void VM::variable(const char* module, const char* name, int slot) noexcept { assert(slot_count() >= 1); wrenGetVariable(m_local->wvm, module, name, slot); } + void VM::variable_or_throw(const char* module, const char* name, int slot) { + using std::string; + using std::runtime_error; + + if (not has_module(module)) + throw runtime_error(string{"Module "} + module + " is unknown"); + if (not has_variable(module, name)) + throw runtime_error(string{"Variable "} + module + "::" + name + " not found"); + + variable(module, name, slot); + } + void VM::set_slot_handle (const Handle& handle, int slot) { wrenSetSlotHandle(m_local->wvm, slot, handle); } diff --git a/src/vm_fun.cpp b/src/vm_fun.cpp index 57fb118..3a9797b 100644 --- a/src/vm_fun.cpp +++ b/src/vm_fun.cpp @@ -73,7 +73,7 @@ namespace wren { } void variable(VM& vm, const ModuleAndName& mod_and_name, int slot) { - vm.variable(std::get<0>(mod_and_name), std::get<1>(mod_and_name), slot); + vm.variable_or_throw(std::get<0>(mod_and_name), std::get<1>(mod_and_name), slot); } void variable (VM& vm, const Handle& handle, int slot) {