call() will fail earlier when object for call doesn't exist

This commit is contained in:
King_DuckZ 2022-04-22 22:04:09 +02:00
commit f749b7fb18
3 changed files with 28 additions and 5 deletions

View file

@ -79,7 +79,8 @@ namespace wren {
void release_handle (Handle& handle) noexcept; void release_handle (Handle& handle) noexcept;
void ensure_slots(int num_slots); void ensure_slots(int num_slots);
int slot_count(); 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); void set_slot_handle(const Handle& handle, int slot);
SlotType slot_type(int slot_num); SlotType slot_type(int slot_num);
Handle slot_handle(int slot_num); Handle slot_handle(int slot_num);
@ -103,7 +104,9 @@ namespace wren {
template <typename U> U* user_data(); template <typename U> U* user_data();
template <typename U> void set_user_data (U* user_data); template <typename U> void set_user_data (U* user_data);
void set_user_data (std::nullptr_t); 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; std::size_t dynafunc_byte_size() const;
void reset (Configuration* conf); void reset (Configuration* conf);

View file

@ -266,10 +266,18 @@ namespace wren {
m_local->user_data_type = detail::type_id<std::nullptr_t>(); m_local->user_data_type = detail::type_id<std::nullptr_t>();
} }
bool VM::has_user_data() const { bool VM::has_user_data() const noexcept {
return nullptr != m_local->user_data; 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 { std::size_t VM::dynafunc_byte_size() const {
return m_local->dynafunc.total_memory(); return m_local->dynafunc.total_memory();
} }
@ -322,11 +330,23 @@ namespace wren {
return wrenGetSlotCount(m_local->wvm); 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); assert(slot_count() >= 1);
wrenGetVariable(m_local->wvm, module, name, slot); 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) { void VM::set_slot_handle (const Handle& handle, int slot) {
wrenSetSlotHandle(m_local->wvm, slot, handle); wrenSetSlotHandle(m_local->wvm, slot, handle);
} }

View file

@ -73,7 +73,7 @@ namespace wren {
} }
void variable(VM& vm, const ModuleAndName& mod_and_name, int slot) { 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) { void variable (VM& vm, const Handle& handle, int slot) {