Skip to content

Commit

Permalink
scripting/scripting_lua: Crash instead of throwing stdlib exceptions
Browse files Browse the repository at this point in the history
In all cases these should only be thrown due to an issue in
C++/Rust code (rather than in a script), so it makes sense to just
handle them like failed assertions and crash the engine.
  • Loading branch information
caseif committed Jun 16, 2024
1 parent cb372da commit 6e26408
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
6 changes: 4 additions & 2 deletions engine/static/scripting/include/argus/scripting/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#pragma once

#include "argus/core/engine.hpp"

#include <functional>
#include <map>
#include <memory>
Expand Down Expand Up @@ -251,7 +253,7 @@ namespace argus {
template<typename T>
[[nodiscard]] const T &at(size_t index) const {
if (sizeof(T) != m_element_size) {
throw std::invalid_argument("Template parameter size does not match element size");
crash("Template parameter size does not match element size");
}
return *reinterpret_cast<const T *>(this->operator[](index));
}
Expand Down Expand Up @@ -341,7 +343,7 @@ namespace argus {
template<typename E>
void set(size_t index, const E &val) {
if (sizeof(E) != m_element_size) {
throw std::invalid_argument("Template type size does not match element size of VectorWrapper");
crash("Template type size does not match element size of VectorWrapper");
}
set(index, reinterpret_cast<void *>(&val));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ namespace argus {
VectorWrapper *wrapper = reinterpret_cast<VectorWrapper *>(obj);
return wrapper->get_underlying_vector<E>();
} else {
throw std::runtime_error("Invalid vector object type magic");
crash("Invalid vector object type magic");
}
} else if constexpr (is_reference_wrapper_v<T>) {
assert(param.type.type == IntegralType::Pointer);
Expand Down
4 changes: 4 additions & 0 deletions engine/static/scripting/src/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ namespace argus {
CopyCtorProxy copy_ctor,
MoveCtorProxy move_ctor,
DtorProxy dtor) {
if (size == 0) {
crash("Bound types cannot be zero-sized");
}

BoundTypeDef def {
name,
size,
Expand Down
10 changes: 5 additions & 5 deletions engine/static/scripting/src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ namespace argus {
m_count(count),
m_element_dtor(element_dtor) {
if (element_size == 0) {
throw std::invalid_argument("Element size must be greater than zero");
crash("Element size must be greater than zero");
}
}

Expand All @@ -274,7 +274,7 @@ namespace argus {

void *ArrayBlob::operator[](size_t index) {
if (index >= m_count) {
throw std::invalid_argument("Index is out of bounds");
crash("ArrayBlob index is out of bounds");
}

return reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(m_blob) + (m_element_size * index));
Expand All @@ -283,7 +283,7 @@ namespace argus {
const void *ArrayBlob::operator[](size_t index) const {
//return const_cast<const void *>(const_cast<ArrayBlob *>(this)->operator[](index));
if (index >= m_count) {
throw std::invalid_argument("Index is out of bounds");
crash("ArrayBlob index is out of bounds");
}

return reinterpret_cast<const void *>(reinterpret_cast<uintptr_t>(m_blob) + (m_element_size * index));
Expand Down Expand Up @@ -336,8 +336,8 @@ namespace argus {
const void *VectorWrapper::at(size_t index) const {
auto size = (*m_get_size_fn)(m_underlying_vec);
if (index >= size) {
throw std::out_of_range("Index " + std::to_string(index)
+ " is out of range in vector of size " + std::to_string(size));
crash("Index " + std::to_string(index)
+ " is out of range in VectorWrapper of size " + std::to_string(size));
}

return reinterpret_cast<void *>(reinterpret_cast<uintptr_t>((*m_get_element_fn)(m_underlying_vec, index)));
Expand Down
16 changes: 8 additions & 8 deletions engine/static/scripting/src/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,18 @@ namespace argus {
ObjectType &el_type = *vec_type.element_type.value();

if (el_type.type == IntegralType::Void) {
throw std::invalid_argument("Vectors of void are not supported");
crash("Vectors of void are not supported");
} else if (el_type.type == IntegralType::Callback) {
throw std::invalid_argument("Vectors of callbacks are not supported");
crash("Vectors of callbacks are not supported");
} else if (el_type.type == IntegralType::Type) {
throw std::invalid_argument("Vectors of types are not supported");
crash("Vectors of types are not supported");
} else if (el_type.type == IntegralType::Vector) {
throw std::invalid_argument("Vectors of vectors are not supported");
crash("Vectors of vectors are not supported");
} else if (el_type.type == IntegralType::Boolean) {
// C++ is stupid and specializes std::vector<bool> as a bitfield,
// which fucks with our assumptions about how we can tinker with
// the vector. Much easier to just not support it.
throw std::invalid_argument("Vectors of booleans are not supported");
// C++ is stupid and specializes std::vector<bool> as a bitfield
// which messes up our assumptions about how we can tinker with the
// vector. Much easier to just not support it.
crash("Vectors of booleans are not supported");
}
}

Expand Down
2 changes: 1 addition & 1 deletion engine/static/scripting_lua/src/lua_language_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ namespace argus {
case 8:
return *reinterpret_cast<const double *>(wrapper.value);
default:
throw std::invalid_argument("Bad floating-point width " + std::to_string(wrapper.type.size)
crash("Bad floating-point width " + std::to_string(wrapper.type.size)
+ " (must be 4, or 8)");
}
}
Expand Down

0 comments on commit 6e26408

Please sign in to comment.