From 109c7014ce805da2bdfbf3ae9fac50c08aad1e1c Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:04:25 -0400 Subject: [PATCH 01/24] Replace NULL with nullptr Signed-off-by: Minsoo Choo --- debug.cpp | 9 ++++----- state.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/debug.cpp b/debug.cpp index e0a0b5e..ad87102 100644 --- a/debug.cpp +++ b/debug.cpp @@ -102,7 +102,7 @@ lutok::debug::event(void) const std::string lutok::debug::name(void) const { - assert(_pimpl->lua_debug.name != NULL); + assert(_pimpl->lua_debug.name != nullptr); return _pimpl->lua_debug.name; } @@ -113,7 +113,7 @@ lutok::debug::name(void) const std::string lutok::debug::name_what(void) const { - assert(_pimpl->lua_debug.namewhat != NULL); + assert(_pimpl->lua_debug.namewhat != nullptr); return _pimpl->lua_debug.namewhat; } @@ -124,7 +124,7 @@ lutok::debug::name_what(void) const std::string lutok::debug::what(void) const { - assert(_pimpl->lua_debug.what != NULL); + assert(_pimpl->lua_debug.what != nullptr); return _pimpl->lua_debug.what; } @@ -135,7 +135,7 @@ lutok::debug::what(void) const std::string lutok::debug::source(void) const { - assert(_pimpl->lua_debug.source != NULL); + assert(_pimpl->lua_debug.source != nullptr); return _pimpl->lua_debug.source; } @@ -187,6 +187,5 @@ lutok::debug::last_line_defined(void) const std::string lutok::debug::short_src(void) const { - assert(_pimpl->lua_debug.short_src != NULL); return _pimpl->lua_debug.short_src; } diff --git a/state.cpp b/state.cpp index 231f6e8..7092d44 100644 --- a/state.cpp +++ b/state.cpp @@ -242,7 +242,7 @@ struct lutok::state::impl { lutok::state::state(void) { lua_State* lua = luaL_newstate(); - if (lua == NULL) + if (lua == nullptr) throw lutok::error("lua open failed"); _pimpl.reset(new impl(lua, true)); } @@ -267,7 +267,7 @@ lutok::state::state(void* raw_state_) : /// code. lutok::state::~state(void) { - if (_pimpl->owned && _pimpl->lua_state != NULL) + if (_pimpl->owned && _pimpl->lua_state != nullptr) close(); } @@ -283,10 +283,10 @@ lutok::state::~state(void) void lutok::state::close(void) { - assert(_pimpl->lua_state != NULL); + assert(_pimpl->lua_state != nullptr); assert(lua_gettop(_pimpl->lua_state) == 0); lua_close(_pimpl->lua_state); - _pimpl->lua_state = NULL; + _pimpl->lua_state = nullptr; } From 2ff507cbf4b4dbf36f7e3b22c58cba4e2116b055 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:17:57 -0400 Subject: [PATCH 02/24] Use default destructors instead of empty ones --- c_gate.cpp | 4 +--- debug.cpp | 4 +--- exceptions.cpp | 12 +++--------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/c_gate.cpp b/c_gate.cpp index dde366e..b616f02 100644 --- a/c_gate.cpp +++ b/c_gate.cpp @@ -45,9 +45,7 @@ lutok::state_c_gate::state_c_gate(state& state_) : /// Destroying this object has no implications on the life cycle of the Lua /// state. Only the corresponding state object controls when the Lua state is /// closed. -lutok::state_c_gate::~state_c_gate(void) -{ -} +lutok::state_c_gate::~state_c_gate(void) = default; /// Creates a C++ state for a C Lua state. diff --git a/debug.cpp b/debug.cpp index ad87102..4a6304f 100644 --- a/debug.cpp +++ b/debug.cpp @@ -51,9 +51,7 @@ lutok::debug::debug(void) : /// Destructor. -lutok::debug::~debug(void) -{ -} +lutok::debug::~debug(void) = default; /// Wrapper around lua_getinfo. diff --git a/exceptions.cpp b/exceptions.cpp index 2b1c968..a9c593b 100644 --- a/exceptions.cpp +++ b/exceptions.cpp @@ -45,9 +45,7 @@ lutok::error::error(const std::string& message) : /// Destructor for the error. -lutok::error::~error(void) throw() -{ -} +lutok::error::~error(void) throw() = default; /// Constructs a new error. @@ -63,9 +61,7 @@ lutok::api_error::api_error(const std::string& api_function_, /// Destructor for the error. -lutok::api_error::~api_error(void) throw() -{ -} +lutok::api_error::~api_error(void) throw() = default; /// Constructs a new api_error with the message on the top of the Lua stack. @@ -111,9 +107,7 @@ lutok::file_not_found_error::file_not_found_error( /// Destructor for the error. -lutok::file_not_found_error::~file_not_found_error(void) throw() -{ -} +lutok::file_not_found_error::~file_not_found_error(void) throw() = default; /// Gets the name of the file that could not be found. From 88a23e6de4fc8f0b076b7e81224aeec8f8e83267 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:25:20 -0400 Subject: [PATCH 03/24] Add include guard --- include/lutok/c_gate.hpp | 5 +++++ include/lutok/debug.hpp | 5 +++++ include/lutok/exceptions.hpp | 5 +++++ include/lutok/state.hpp | 5 +++++ include/lutok/state.ipp | 5 +++++ 5 files changed, 25 insertions(+) diff --git a/include/lutok/c_gate.hpp b/include/lutok/c_gate.hpp index 4fabe16..14a359a 100644 --- a/include/lutok/c_gate.hpp +++ b/include/lutok/c_gate.hpp @@ -1 +1,6 @@ +#ifndef C_GATE_HPP +#define C_GATE_HPP + #include "../../c_gate.hpp" + +#endif // C_GATE_HPP \ No newline at end of file diff --git a/include/lutok/debug.hpp b/include/lutok/debug.hpp index b942bbd..8031eeb 100644 --- a/include/lutok/debug.hpp +++ b/include/lutok/debug.hpp @@ -1 +1,6 @@ +#ifndef DEBUG_HPP +#define DEBUG_HPP + #include "../../debug.hpp" + +#endif // DEBUG_HPP \ No newline at end of file diff --git a/include/lutok/exceptions.hpp b/include/lutok/exceptions.hpp index 97f49a1..0e4e3ab 100644 --- a/include/lutok/exceptions.hpp +++ b/include/lutok/exceptions.hpp @@ -1 +1,6 @@ +#ifndef EXCEPTIONS_HPP +#define EXCEPTIONS_HPP + #include "../../exceptions.hpp" + +#endif // EXCEPTIONS_HPP \ No newline at end of file diff --git a/include/lutok/state.hpp b/include/lutok/state.hpp index 48ac65c..f7c85cd 100644 --- a/include/lutok/state.hpp +++ b/include/lutok/state.hpp @@ -1 +1,6 @@ +#ifndef STATE_HPP +#define STATE_HPP + #include "../../state.hpp" + +#endif // STATE_HPP \ No newline at end of file diff --git a/include/lutok/state.ipp b/include/lutok/state.ipp index 531d9c1..8d4cc19 100644 --- a/include/lutok/state.ipp +++ b/include/lutok/state.ipp @@ -1 +1,6 @@ +#ifndef STATE_IPP +#define STATE_IPP + #include "../../state.ipp" + +#endif // STATE_IPP \ No newline at end of file From ee54feb8da56cc42e3aa30967f44abe4104fffc8 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:31:23 -0400 Subject: [PATCH 04/24] Make member function const if possible --- c_gate.cpp | 2 +- c_gate.hpp | 2 +- debug.cpp | 4 +-- debug.hpp | 4 +-- stack_cleaner.cpp | 2 +- stack_cleaner.hpp | 2 +- state.cpp | 68 ++++++++++++++++++++++----------------------- state.hpp | 70 +++++++++++++++++++++++------------------------ 8 files changed, 77 insertions(+), 77 deletions(-) diff --git a/c_gate.cpp b/c_gate.cpp index b616f02..7de7ad3 100644 --- a/c_gate.cpp +++ b/c_gate.cpp @@ -68,7 +68,7 @@ lutok::state_c_gate::connect(lua_State* raw_state) /// /// \return A native lua_State object holding the Lua C API state. lua_State* -lutok::state_c_gate::c_state(void) +lutok::state_c_gate::c_state(void) const { return static_cast< lua_State* >(_state.raw_state()); } diff --git a/c_gate.hpp b/c_gate.hpp index 36bc9c2..6415bcd 100644 --- a/c_gate.hpp +++ b/c_gate.hpp @@ -62,7 +62,7 @@ class state_c_gate { static state connect(lua_State*); - lua_State* c_state(void); + lua_State* c_state(void) const; }; diff --git a/debug.cpp b/debug.cpp index 4a6304f..ff7ec04 100644 --- a/debug.cpp +++ b/debug.cpp @@ -62,7 +62,7 @@ lutok::debug::~debug(void) = default; /// \warning Terminates execution if there is not enough memory to manipulate /// the Lua stack. void -lutok::debug::get_info(state& s, const std::string& what_) +lutok::debug::get_info(state& s, const std::string& what_) const { lua_State* raw_state = state_c_gate(s).c_state(); @@ -76,7 +76,7 @@ lutok::debug::get_info(state& s, const std::string& what_) /// \param s The Lua state. /// \param level The second parameter to lua_getstack. void -lutok::debug::get_stack(state& s, const int level) +lutok::debug::get_stack(state& s, const int level) const { lua_State* raw_state = state_c_gate(s).c_state(); diff --git a/debug.hpp b/debug.hpp index 6ada1fc..0ec6236 100644 --- a/debug.hpp +++ b/debug.hpp @@ -61,8 +61,8 @@ class debug { debug(void); ~debug(void); - void get_info(state&, const std::string&); - void get_stack(state&, const int); + void get_info(state&, const std::string&) const; + void get_stack(state&, const int) const; int event(void) const; std::string name(void) const; diff --git a/stack_cleaner.cpp b/stack_cleaner.cpp index 419e55a..ccbeb1e 100644 --- a/stack_cleaner.cpp +++ b/stack_cleaner.cpp @@ -85,7 +85,7 @@ lutok::stack_cleaner::~stack_cleaner(void) /// elements that are currently in the stack will not be touched during /// destruction when the function is called. void -lutok::stack_cleaner::forget(void) +lutok::stack_cleaner::forget(void) const { _pimpl->original_depth = _pimpl->state_ref.get_top(); } diff --git a/stack_cleaner.hpp b/stack_cleaner.hpp index 3e3a8ed..e0b62fa 100644 --- a/stack_cleaner.hpp +++ b/stack_cleaner.hpp @@ -84,7 +84,7 @@ class stack_cleaner { stack_cleaner(state&); ~stack_cleaner(void); - void forget(void); + void forget(void) const; }; diff --git a/state.cpp b/state.cpp index 7092d44..46ad32f 100644 --- a/state.cpp +++ b/state.cpp @@ -281,7 +281,7 @@ lutok::state::~state(void) /// \pre The Lua stack is empty. This is not truly necessary but ensures that /// our code is consistent and clears the stack explicitly. void -lutok::state::close(void) +lutok::state::close(void) const { assert(_pimpl->lua_state != nullptr); assert(lua_gettop(_pimpl->lua_state) == 0); @@ -315,7 +315,7 @@ lutok::state::get_global(const std::string& name) /// /// \post state(-1) Contains the reference to the globals table. void -lutok::state::get_global_table(void) +lutok::state::get_global_table(void) const { #if LUA_VERSION_NUM >= 502 lua_pushvalue(_pimpl->lua_state, registry_index); @@ -338,7 +338,7 @@ lutok::state::get_global_table(void) /// \warning Terminates execution if there is not enough memory to manipulate /// the Lua stack. bool -lutok::state::get_metafield(const int index, const std::string& name) +lutok::state::get_metafield(const int index, const std::string& name) const { return luaL_getmetafield(_pimpl->lua_state, index, name.c_str()) != 0; } @@ -350,7 +350,7 @@ lutok::state::get_metafield(const int index, const std::string& name) /// /// \return The return value of lua_getmetatable. bool -lutok::state::get_metatable(const int index) +lutok::state::get_metatable(const int index) const { return lua_getmetatable(_pimpl->lua_state, index) != 0; } @@ -381,7 +381,7 @@ lutok::state::get_table(const int index) /// /// \return The return value of lua_gettop. int -lutok::state::get_top(void) +lutok::state::get_top(void) const { return lua_gettop(_pimpl->lua_state); } @@ -391,7 +391,7 @@ lutok::state::get_top(void) /// /// \param index The second parameter to lua_insert. void -lutok::state::insert(const int index) +lutok::state::insert(const int index) const { lua_insert(_pimpl->lua_state, index); } @@ -403,7 +403,7 @@ lutok::state::insert(const int index) /// /// \return The return value of lua_isboolean. bool -lutok::state::is_boolean(const int index) +lutok::state::is_boolean(const int index) const { return lua_isboolean(_pimpl->lua_state, index); } @@ -415,7 +415,7 @@ lutok::state::is_boolean(const int index) /// /// \return The return value of lua_isfunction. bool -lutok::state::is_function(const int index) +lutok::state::is_function(const int index) const { return lua_isfunction(_pimpl->lua_state, index); } @@ -427,7 +427,7 @@ lutok::state::is_function(const int index) /// /// \return The return value of lua_isnil. bool -lutok::state::is_nil(const int index) +lutok::state::is_nil(const int index) const { return lua_isnil(_pimpl->lua_state, index); } @@ -439,7 +439,7 @@ lutok::state::is_nil(const int index) /// /// \return The return value of lua_isnumber. bool -lutok::state::is_number(const int index) +lutok::state::is_number(const int index) const { return lua_isnumber(_pimpl->lua_state, index); } @@ -451,7 +451,7 @@ lutok::state::is_number(const int index) /// /// \return The return value of lua_isstring. bool -lutok::state::is_string(const int index) +lutok::state::is_string(const int index) const { return lua_isstring(_pimpl->lua_state, index); } @@ -463,7 +463,7 @@ lutok::state::is_string(const int index) /// /// \return The return value of lua_istable. bool -lutok::state::is_table(const int index) +lutok::state::is_table(const int index) const { return lua_istable(_pimpl->lua_state, index); } @@ -475,7 +475,7 @@ lutok::state::is_table(const int index) /// /// \return The return value of lua_isuserdata. bool -lutok::state::is_userdata(const int index) +lutok::state::is_userdata(const int index) const { return lua_isuserdata(_pimpl->lua_state, index); } @@ -518,7 +518,7 @@ lutok::state::load_string(const std::string& str) /// /// \warning Terminates execution if there is not enough memory. void -lutok::state::new_table(void) +lutok::state::new_table(void) const { lua_newtable(_pimpl->lua_state); } @@ -535,7 +535,7 @@ lutok::state::new_table(void) /// /// \warning Terminates execution if there is not enough memory. void* -lutok::state::new_userdata_voidp(const size_t size) +lutok::state::new_userdata_voidp(const size_t size) const { return lua_newuserdata(_pimpl->lua_state, size); } @@ -574,7 +574,7 @@ lutok::state::next(const int index) /// /// \warning Terminates execution if there is not enough memory. void -lutok::state::open_all(void) +lutok::state::open_all(void) const { luaL_openlibs(_pimpl->lua_state); } @@ -600,7 +600,7 @@ lutok::state::open_base(void) /// /// \warning Terminates execution if there is not enough memory. void -lutok::state::open_string(void) +lutok::state::open_string(void) const { #if LUA_VERSION_NUM >= 502 luaL_requiref(_pimpl->lua_state, LUA_STRLIBNAME, luaopen_string, 1); @@ -619,7 +619,7 @@ lutok::state::open_string(void) /// /// \warning Terminates execution if there is not enough memory. void -lutok::state::open_table(void) +lutok::state::open_table(void) const { #if LUA_VERSION_NUM >= 502 luaL_requiref(_pimpl->lua_state, LUA_TABLIBNAME, luaopen_table, 1); @@ -651,7 +651,7 @@ lutok::state::pcall(const int nargs, const int nresults, const int errfunc) /// /// \param count The second parameter to lua_pop. void -lutok::state::pop(const int count) +lutok::state::pop(const int count) const { assert(count <= lua_gettop(_pimpl->lua_state)); lua_pop(_pimpl->lua_state, count); @@ -663,7 +663,7 @@ lutok::state::pop(const int count) /// /// \param value The second parameter to lua_pushboolean. void -lutok::state::push_boolean(const bool value) +lutok::state::push_boolean(const bool value) const { lua_pushboolean(_pimpl->lua_state, value ? 1 : 0); } @@ -677,7 +677,7 @@ lutok::state::push_boolean(const bool value) /// \param function The C++ function to be pushed as a closure. /// \param nvalues The number of upvalues that the function receives. void -lutok::state::push_cxx_closure(cxx_function function, const int nvalues) +lutok::state::push_cxx_closure(cxx_function function, const int nvalues) const { cxx_function *data = static_cast< cxx_function* >( lua_newuserdata(_pimpl->lua_state, sizeof(cxx_function))); @@ -693,7 +693,7 @@ lutok::state::push_cxx_closure(cxx_function function, const int nvalues) /// /// \param function The C++ function to be pushed. void -lutok::state::push_cxx_function(cxx_function function) +lutok::state::push_cxx_function(cxx_function function) const { cxx_function *data = static_cast< cxx_function* >( lua_newuserdata(_pimpl->lua_state, sizeof(cxx_function))); @@ -706,7 +706,7 @@ lutok::state::push_cxx_function(cxx_function function) /// /// \param value The second parameter to lua_pushinteger. void -lutok::state::push_integer(const int value) +lutok::state::push_integer(const int value) const { lua_pushinteger(_pimpl->lua_state, value); } @@ -714,7 +714,7 @@ lutok::state::push_integer(const int value) /// Wrapper around lua_pushnil. void -lutok::state::push_nil(void) +lutok::state::push_nil(void) const { lua_pushnil(_pimpl->lua_state); } @@ -726,7 +726,7 @@ lutok::state::push_nil(void) /// /// \warning Terminates execution if there is not enough memory. void -lutok::state::push_string(const std::string& str) +lutok::state::push_string(const std::string& str) const { lua_pushstring(_pimpl->lua_state, str.c_str()); } @@ -736,7 +736,7 @@ lutok::state::push_string(const std::string& str) /// /// \param index The second parameter to lua_pushvalue. void -lutok::state::push_value(const int index) +lutok::state::push_value(const int index) const { lua_pushvalue(_pimpl->lua_state, index); } @@ -746,7 +746,7 @@ lutok::state::push_value(const int index) /// /// \param index The second parameter to lua_rawget. void -lutok::state::raw_get(const int index) +lutok::state::raw_get(const int index) const { lua_rawget(_pimpl->lua_state, index); } @@ -759,7 +759,7 @@ lutok::state::raw_get(const int index) /// \warning Terminates execution if there is not enough memory to manipulate /// the Lua stack. void -lutok::state::raw_set(const int index) +lutok::state::raw_set(const int index) const { lua_rawset(_pimpl->lua_state, index); } @@ -789,7 +789,7 @@ lutok::state::set_global(const std::string& name) /// /// \param index The second parameter to lua_setmetatable. void -lutok::state::set_metatable(const int index) +lutok::state::set_metatable(const int index) const { lua_setmetatable(_pimpl->lua_state, index); } @@ -822,7 +822,7 @@ lutok::state::set_table(const int index) /// /// \return The return value of lua_toboolean. bool -lutok::state::to_boolean(const int index) +lutok::state::to_boolean(const int index) const { assert(is_boolean(index)); return lua_toboolean(_pimpl->lua_state, index); @@ -835,7 +835,7 @@ lutok::state::to_boolean(const int index) /// /// \return The return value of lua_tointeger. long -lutok::state::to_integer(const int index) +lutok::state::to_integer(const int index) const { assert(is_number(index)); return lua_tointeger(_pimpl->lua_state, index); @@ -853,7 +853,7 @@ lutok::state::to_integer(const int index) /// /// \warning Terminates execution if there is not enough memory. void* -lutok::state::to_userdata_voidp(const int index) +lutok::state::to_userdata_voidp(const int index) const { return lua_touserdata(_pimpl->lua_state, index); } @@ -868,7 +868,7 @@ lutok::state::to_userdata_voidp(const int index) /// /// \warning Terminates execution if there is not enough memory. std::string -lutok::state::to_string(const int index) +lutok::state::to_string(const int index) const { assert(is_string(index)); const char *raw_string = lua_tostring(_pimpl->lua_state, index); @@ -898,7 +898,7 @@ lutok::state::upvalue_index(const int index) /// to call this method is by using the c_gate module, and c_gate takes care of /// casting this object to the appropriate type. void* -lutok::state::raw_state(void) +lutok::state::raw_state(void) const { return _pimpl->lua_state; } diff --git a/state.hpp b/state.hpp index 3b088f1..dc933b5 100644 --- a/state.hpp +++ b/state.hpp @@ -74,60 +74,60 @@ class state { /// Pointer to the shared internal implementation. std::shared_ptr< impl > _pimpl; - void* new_userdata_voidp(const size_t); - void* to_userdata_voidp(const int); + void* new_userdata_voidp(const size_t) const; + void* to_userdata_voidp(const int) const; friend class state_c_gate; explicit state(void*); - void* raw_state(void); + void* raw_state(void) const; public: state(void); ~state(void); - void close(void); + void close(void) const; void get_global(const std::string&); - void get_global_table(void); - bool get_metafield(const int, const std::string&); - bool get_metatable(const int); + void get_global_table(void) const; + bool get_metafield(const int, const std::string&) const; + bool get_metatable(const int) const; void get_table(const int); - int get_top(void); - void insert(const int); - bool is_boolean(const int); - bool is_function(const int); - bool is_nil(const int); - bool is_number(const int); - bool is_string(const int); - bool is_table(const int); - bool is_userdata(const int); + int get_top(void) const; + void insert(const int) const; + bool is_boolean(const int) const; + bool is_function(const int) const; + bool is_nil(const int) const; + bool is_number(const int) const; + bool is_string(const int) const; + bool is_table(const int) const; + bool is_userdata(const int) const; void load_file(const std::string&); void load_string(const std::string&); - void new_table(void); + void new_table(void) const; template< typename Type > Type* new_userdata(void); bool next(const int); - void open_all(void); + void open_all(void) const; void open_base(void); - void open_string(void); - void open_table(void); + void open_string(void) const; + void open_table(void) const; void pcall(const int, const int, const int); - void pop(const int); - void push_boolean(const bool); - void push_cxx_closure(cxx_function, const int); - void push_cxx_function(cxx_function); - void push_integer(const int); - void push_nil(void); - void push_string(const std::string&); - void push_value(const int); - void raw_get(const int); - void raw_set(const int); + void pop(const int) const; + void push_boolean(const bool) const; + void push_cxx_closure(cxx_function, const int) const; + void push_cxx_function(cxx_function) const; + void push_integer(const int) const; + void push_nil(void) const; + void push_string(const std::string&) const; + void push_value(const int) const; + void raw_get(const int) const; + void raw_set(const int) const; void set_global(const std::string&); - void set_metatable(const int); + void set_metatable(const int) const; void set_table(const int); - bool to_boolean(const int); - long to_integer(const int); + bool to_boolean(const int) const; + long to_integer(const int) const; template< typename Type > Type* to_userdata(const int); - std::string to_string(const int); - int upvalue_index(const int); + std::string to_string(const int) const; + static int upvalue_index(const int); }; From 03146c81383f3d68d9281b1156d5bb27151f87e0 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:34:41 -0400 Subject: [PATCH 05/24] Replace throw with noexcept --- exceptions.cpp | 6 +++--- exceptions.hpp | 6 +++--- state.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/exceptions.cpp b/exceptions.cpp index a9c593b..f18aa39 100644 --- a/exceptions.cpp +++ b/exceptions.cpp @@ -45,7 +45,7 @@ lutok::error::error(const std::string& message) : /// Destructor for the error. -lutok::error::~error(void) throw() = default; +lutok::error::~error(void) noexcept = default; /// Constructs a new error. @@ -61,7 +61,7 @@ lutok::api_error::api_error(const std::string& api_function_, /// Destructor for the error. -lutok::api_error::~api_error(void) throw() = default; +lutok::api_error::~api_error(void) noexcept = default; /// Constructs a new api_error with the message on the top of the Lua stack. @@ -107,7 +107,7 @@ lutok::file_not_found_error::file_not_found_error( /// Destructor for the error. -lutok::file_not_found_error::~file_not_found_error(void) throw() = default; +lutok::file_not_found_error::~file_not_found_error(void) noexcept = default; /// Gets the name of the file that could not be found. diff --git a/exceptions.hpp b/exceptions.hpp index 93a7948..91fc0df 100644 --- a/exceptions.hpp +++ b/exceptions.hpp @@ -45,7 +45,7 @@ class state; class error : public std::runtime_error { public: explicit error(const std::string&); - virtual ~error(void) throw(); + virtual ~error(void) noexcept; }; @@ -56,7 +56,7 @@ class api_error : public error { public: explicit api_error(const std::string&, const std::string&); - virtual ~api_error(void) throw(); + virtual ~api_error(void) noexcept; static api_error from_stack(state&, const std::string&); @@ -71,7 +71,7 @@ class file_not_found_error : public error { public: explicit file_not_found_error(const std::string&); - virtual ~file_not_found_error(void) throw(); + virtual ~file_not_found_error(void) noexcept; const std::string& filename(void) const; }; diff --git a/state.cpp b/state.cpp index 46ad32f..44e56b1 100644 --- a/state.cpp +++ b/state.cpp @@ -137,7 +137,7 @@ protected_settable(lua_State* state) /// function. static int call_cxx_function_from_c(lutok::cxx_function function, - lua_State* raw_state) throw() + lua_State* raw_state) noexcept { char error_buf[1024]; From cbf4e05d5d35f907997a179f28c42fe072136624 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:41:14 -0400 Subject: [PATCH 06/24] Mark single-argument constructors explicit This avoids unintentional implicit conversions. --- c_gate.hpp | 2 +- stack_cleaner.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/c_gate.hpp b/c_gate.hpp index 6415bcd..f98e276 100644 --- a/c_gate.hpp +++ b/c_gate.hpp @@ -57,7 +57,7 @@ class state_c_gate { state& _state; public: - state_c_gate(state&); + explicit state_c_gate(state&); ~state_c_gate(void); static state connect(lua_State*); diff --git a/stack_cleaner.hpp b/stack_cleaner.hpp index e0b62fa..bb0ea49 100644 --- a/stack_cleaner.hpp +++ b/stack_cleaner.hpp @@ -81,7 +81,7 @@ class stack_cleaner { stack_cleaner& operator=(const stack_cleaner&); public: - stack_cleaner(state&); + explicit stack_cleaner(state&); ~stack_cleaner(void); void forget(void) const; From 7a5b4afffb4eb0f6c4b093bc01b7559997802fb2 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:44:57 -0400 Subject: [PATCH 07/24] Remove const-qualified parameters in the function declaration Const-qualification of parameters only has an effect in function definitions. --- debug.hpp | 2 +- operations.hpp | 10 ++++----- state.hpp | 58 +++++++++++++++++++++++++------------------------- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/debug.hpp b/debug.hpp index 0ec6236..a810ae5 100644 --- a/debug.hpp +++ b/debug.hpp @@ -62,7 +62,7 @@ class debug { ~debug(void); void get_info(state&, const std::string&) const; - void get_stack(state&, const int) const; + void get_stack(state&, int) const; int event(void) const; std::string name(void) const; diff --git a/operations.hpp b/operations.hpp index ead7c77..c6a5296 100644 --- a/operations.hpp +++ b/operations.hpp @@ -43,11 +43,11 @@ namespace lutok { void create_module(state&, const std::string&, const std::map< std::string, cxx_function >&); -unsigned int do_file(state&, const std::string&, const int, const int, - const int); -unsigned int do_string(state&, const std::string&, const int, const int, - const int); -void eval(state&, const std::string&, const int); +unsigned int do_file(state&, const std::string&, int, int, + int); +unsigned int do_string(state&, const std::string&, int, int, + int); +void eval(state&, const std::string&, int); } // namespace lutok diff --git a/state.hpp b/state.hpp index dc933b5..a9d8776 100644 --- a/state.hpp +++ b/state.hpp @@ -74,8 +74,8 @@ class state { /// Pointer to the shared internal implementation. std::shared_ptr< impl > _pimpl; - void* new_userdata_voidp(const size_t) const; - void* to_userdata_voidp(const int) const; + void* new_userdata_voidp(size_t) const; + void* to_userdata_voidp(int) const; friend class state_c_gate; explicit state(void*); @@ -88,46 +88,46 @@ class state { void close(void) const; void get_global(const std::string&); void get_global_table(void) const; - bool get_metafield(const int, const std::string&) const; - bool get_metatable(const int) const; - void get_table(const int); + bool get_metafield(int, const std::string&) const; + bool get_metatable(int) const; + void get_table(int); int get_top(void) const; - void insert(const int) const; - bool is_boolean(const int) const; - bool is_function(const int) const; - bool is_nil(const int) const; - bool is_number(const int) const; - bool is_string(const int) const; - bool is_table(const int) const; - bool is_userdata(const int) const; + void insert(int) const; + bool is_boolean(int) const; + bool is_function(int) const; + bool is_nil(int) const; + bool is_number(int) const; + bool is_string(int) const; + bool is_table(int) const; + bool is_userdata(int) const; void load_file(const std::string&); void load_string(const std::string&); void new_table(void) const; template< typename Type > Type* new_userdata(void); - bool next(const int); + bool next(int); void open_all(void) const; void open_base(void); void open_string(void) const; void open_table(void) const; - void pcall(const int, const int, const int); - void pop(const int) const; - void push_boolean(const bool) const; - void push_cxx_closure(cxx_function, const int) const; + void pcall(int, int, int); + void pop(int) const; + void push_boolean(bool) const; + void push_cxx_closure(cxx_function, int) const; void push_cxx_function(cxx_function) const; - void push_integer(const int) const; + void push_integer(int) const; void push_nil(void) const; void push_string(const std::string&) const; - void push_value(const int) const; - void raw_get(const int) const; - void raw_set(const int) const; + void push_value(int) const; + void raw_get(int) const; + void raw_set(int) const; void set_global(const std::string&); - void set_metatable(const int) const; - void set_table(const int); - bool to_boolean(const int) const; - long to_integer(const int) const; - template< typename Type > Type* to_userdata(const int); - std::string to_string(const int) const; - static int upvalue_index(const int); + void set_metatable(int) const; + void set_table(int); + bool to_boolean(int) const; + long to_integer(int) const; + template< typename Type > Type* to_userdata(int); + std::string to_string(int) const; + static int upvalue_index(int); }; From 38f24885cd261964af3487ec4a54e7075801a109 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:47:23 -0400 Subject: [PATCH 08/24] Prefer using 'override' instead of 'virtual' --- exceptions.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exceptions.hpp b/exceptions.hpp index 91fc0df..ab90514 100644 --- a/exceptions.hpp +++ b/exceptions.hpp @@ -45,7 +45,7 @@ class state; class error : public std::runtime_error { public: explicit error(const std::string&); - virtual ~error(void) noexcept; + ~error(void) noexcept override; }; @@ -56,7 +56,7 @@ class api_error : public error { public: explicit api_error(const std::string&, const std::string&); - virtual ~api_error(void) noexcept; + ~api_error(void) noexcept override; static api_error from_stack(state&, const std::string&); @@ -71,7 +71,7 @@ class file_not_found_error : public error { public: explicit file_not_found_error(const std::string&); - virtual ~file_not_found_error(void) noexcept; + ~file_not_found_error(void) noexcept override; const std::string& filename(void) const; }; From d15b98ad97fff6611b4cc3eada1d0afa800da946 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:51:13 -0400 Subject: [PATCH 09/24] Use range-based loops --- operations.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/operations.cpp b/operations.cpp index 1b70dc4..12493ae 100644 --- a/operations.cpp +++ b/operations.cpp @@ -47,10 +47,9 @@ lutok::create_module(state& s, const std::string& name, { stack_cleaner cleaner(s); s.new_table(); - for (std::map< std::string, cxx_function >::const_iterator - iter = members.begin(); iter != members.end(); iter++) { - s.push_string((*iter).first); - s.push_cxx_function((*iter).second); + for (const auto & member : members) { + s.push_string(member.first); + s.push_cxx_function(member.second); s.set_table(-3); } s.set_global(name); From c582111941b3e7d4462ff8f924a457b16ad651aa Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:54:53 -0400 Subject: [PATCH 10/24] Use static_cast to avoid implicit conversion --- stack_cleaner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stack_cleaner.cpp b/stack_cleaner.cpp index ccbeb1e..ef546d2 100644 --- a/stack_cleaner.cpp +++ b/stack_cleaner.cpp @@ -75,7 +75,7 @@ lutok::stack_cleaner::~stack_cleaner(void) assert(current_depth >= _pimpl->original_depth); const unsigned int diff = current_depth - _pimpl->original_depth; if (diff > 0) - _pimpl->state_ref.pop(diff); + _pimpl->state_ref.pop(static_cast(diff)); } From 664a031d4904e9664178be974c81b8cb58084bf2 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:56:11 -0400 Subject: [PATCH 11/24] Use a braced initializer list for return type Repeating the return type from the declaration should be avoided. --- state.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/state.cpp b/state.cpp index 44e56b1..9ad4e6c 100644 --- a/state.cpp +++ b/state.cpp @@ -875,7 +875,7 @@ lutok::state::to_string(const int index) const // Note that the creation of a string object below (explicit for clarity) // implies that the raw string is duplicated and, henceforth, the string is // safe even if the corresponding element is popped from the Lua stack. - return std::string(raw_string); + return {raw_string}; } From 412bbe474d6bd08dd3b3cf3adf75aad8bb43af7d Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 20:57:43 -0400 Subject: [PATCH 12/24] Use auto when initializing with a cast This helps avoiding duplicating the type name. --- state.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/state.cpp b/state.cpp index 9ad4e6c..1ea8309 100644 --- a/state.cpp +++ b/state.cpp @@ -183,7 +183,7 @@ cxx_closure_trampoline(lua_State* raw_state) nupvalues = debug.nups; } - lutok::cxx_function* function = state.to_userdata< lutok::cxx_function >( + auto* function = state.to_userdata< lutok::cxx_function >( state.upvalue_index(nupvalues)); return call_cxx_function_from_c(*function, raw_state); } @@ -203,7 +203,7 @@ static int cxx_function_trampoline(lua_State* raw_state) { lutok::state state = lutok::state_c_gate::connect(raw_state); - lutok::cxx_function* function = state.to_userdata< lutok::cxx_function >( + auto* function = state.to_userdata< lutok::cxx_function >( state.upvalue_index(1)); return call_cxx_function_from_c(*function, raw_state); } @@ -679,7 +679,7 @@ lutok::state::push_boolean(const bool value) const void lutok::state::push_cxx_closure(cxx_function function, const int nvalues) const { - cxx_function *data = static_cast< cxx_function* >( + auto *data = static_cast< cxx_function* >( lua_newuserdata(_pimpl->lua_state, sizeof(cxx_function))); *data = function; lua_pushcclosure(_pimpl->lua_state, cxx_closure_trampoline, nvalues + 1); @@ -695,7 +695,7 @@ lutok::state::push_cxx_closure(cxx_function function, const int nvalues) const void lutok::state::push_cxx_function(cxx_function function) const { - cxx_function *data = static_cast< cxx_function* >( + auto *data = static_cast< cxx_function* >( lua_newuserdata(_pimpl->lua_state, sizeof(cxx_function))); *data = function; lua_pushcclosure(_pimpl->lua_state, cxx_function_trampoline, 1); From 410b14c6e4b89d5087f03f6abcaf575b48db628f Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 21:03:22 -0400 Subject: [PATCH 13/24] Don't access static member through instance --- state.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/state.cpp b/state.cpp index 1ea8309..1f2d90f 100644 --- a/state.cpp +++ b/state.cpp @@ -184,7 +184,7 @@ cxx_closure_trampoline(lua_State* raw_state) } auto* function = state.to_userdata< lutok::cxx_function >( - state.upvalue_index(nupvalues)); + lutok::state::upvalue_index(nupvalues)); return call_cxx_function_from_c(*function, raw_state); } @@ -204,7 +204,7 @@ cxx_function_trampoline(lua_State* raw_state) { lutok::state state = lutok::state_c_gate::connect(raw_state); auto* function = state.to_userdata< lutok::cxx_function >( - state.upvalue_index(1)); + lutok::state::upvalue_index(1)); return call_cxx_function_from_c(*function, raw_state); } From 8414b69e3d44aec5e54577073cdd9e2ccf872612 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 21:04:50 -0400 Subject: [PATCH 14/24] Use make_shared() --- state.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/state.cpp b/state.cpp index 1f2d90f..27c0a91 100644 --- a/state.cpp +++ b/state.cpp @@ -32,6 +32,7 @@ extern "C" { #include #include +#include #include "c_gate.hpp" #include "exceptions.hpp" @@ -244,7 +245,7 @@ lutok::state::state(void) lua_State* lua = luaL_newstate(); if (lua == nullptr) throw lutok::error("lua open failed"); - _pimpl.reset(new impl(lua, true)); + _pimpl = std::make_shared(lua, true); } From ccd96c53b2a35e7517062d02ebf644d73ef62fb0 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 21:35:09 -0400 Subject: [PATCH 15/24] Do not include unused headers --- debug.cpp | 1 - stack_cleaner.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/debug.cpp b/debug.cpp index ff7ec04..c6e35a4 100644 --- a/debug.cpp +++ b/debug.cpp @@ -33,7 +33,6 @@ #include #include #include -#include /// Internal implementation for lutok::debug. diff --git a/stack_cleaner.cpp b/stack_cleaner.cpp index ef546d2..dd5075a 100644 --- a/stack_cleaner.cpp +++ b/stack_cleaner.cpp @@ -27,7 +27,6 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include #include "stack_cleaner.hpp" #include "state.ipp" From 74466f9b092af5293a8f9c74b401a0cc408eb9be Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 21:38:15 -0400 Subject: [PATCH 16/24] Remove redundant static keyword Static definition in anonymous namspace is redundant --- state.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/state.cpp b/state.cpp index 27c0a91..3e7f8c7 100644 --- a/state.cpp +++ b/state.cpp @@ -50,7 +50,7 @@ namespace { /// \param state The Lua C API state. /// /// \return The number of return values pushed onto the stack. -static int +int protected_getglobal(lua_State* state) { lua_getglobal(state, lua_tostring(state, -1)); @@ -67,7 +67,7 @@ protected_getglobal(lua_State* state) /// \param state The Lua C API state. /// /// \return The number of return values pushed onto the stack. -static int +int protected_gettable(lua_State* state) { lua_gettable(state, -2); @@ -84,7 +84,7 @@ protected_gettable(lua_State* state) /// \param state The Lua C API state. /// /// \return The number of return values pushed onto the stack. -static int +int protected_next(lua_State* state) { const int more = lua_next(state, -2) != 0; @@ -101,7 +101,7 @@ protected_next(lua_State* state) /// \param state The Lua C API state. /// /// \return The number of return values pushed onto the stack. -static int +int protected_setglobal(lua_State* state) { lua_setglobal(state, lua_tostring(state, -2)); @@ -118,7 +118,7 @@ protected_setglobal(lua_State* state) /// \param state The Lua C API state. /// /// \return The number of return values pushed onto the stack. -static int +int protected_settable(lua_State* state) { lua_settable(state, -3); @@ -136,7 +136,7 @@ protected_settable(lua_State* state) /// /// \return The number of return values pushed onto the Lua stack by the /// function. -static int +int call_cxx_function_from_c(lutok::cxx_function function, lua_State* raw_state) noexcept { @@ -171,7 +171,7 @@ call_cxx_function_from_c(lutok::cxx_function function, /// \param raw_state The Lua C API state. /// /// \return The number of return values of the called closure. -static int +int cxx_closure_trampoline(lua_State* raw_state) { lutok::state state = lutok::state_c_gate::connect(raw_state); @@ -200,7 +200,7 @@ cxx_closure_trampoline(lua_State* raw_state) /// \param raw_state The Lua C API state. /// /// \return The number of return values of the called function. -static int +int cxx_function_trampoline(lua_State* raw_state) { lutok::state state = lutok::state_c_gate::connect(raw_state); From d790993f82c20d799192c6ea17c84a5aa004e334 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 21:58:27 -0400 Subject: [PATCH 17/24] Use final when possible --- exceptions.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exceptions.hpp b/exceptions.hpp index ab90514..52bfa30 100644 --- a/exceptions.hpp +++ b/exceptions.hpp @@ -50,7 +50,7 @@ class error : public std::runtime_error { /// Exception for errors raised by the Lua API library. -class api_error : public error { +class api_error final : public error { /// Name of the Lua C API function that caused the error. std::string _api_function; @@ -65,7 +65,7 @@ class api_error : public error { /// File not found error. -class file_not_found_error : public error { +class file_not_found_error final : public error { /// Name of the not-found file. std::string _filename; From 366b7a85f477ffe24b93f7d13c971d4ac6aa9213 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 22:40:28 -0400 Subject: [PATCH 18/24] Make function parameters const --- state.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/state.cpp b/state.cpp index 3e7f8c7..c95b3c7 100644 --- a/state.cpp +++ b/state.cpp @@ -137,7 +137,7 @@ protected_settable(lua_State* state) /// \return The number of return values pushed onto the Lua stack by the /// function. int -call_cxx_function_from_c(lutok::cxx_function function, +call_cxx_function_from_c(const lutok::cxx_function function, lua_State* raw_state) noexcept { char error_buf[1024]; @@ -228,7 +228,7 @@ struct lutok::state::impl { /// /// \param lua_ The Lua internal state. /// \param owned_ Whether we own the state or not. - impl(lua_State* lua_, bool owned_) : + impl(lua_State* lua_, const bool owned_) : lua_state(lua_), owned(owned_) { @@ -678,7 +678,7 @@ lutok::state::push_boolean(const bool value) const /// \param function The C++ function to be pushed as a closure. /// \param nvalues The number of upvalues that the function receives. void -lutok::state::push_cxx_closure(cxx_function function, const int nvalues) const +lutok::state::push_cxx_closure(const cxx_function function, const int nvalues) const { auto *data = static_cast< cxx_function* >( lua_newuserdata(_pimpl->lua_state, sizeof(cxx_function))); @@ -694,7 +694,7 @@ lutok::state::push_cxx_closure(cxx_function function, const int nvalues) const /// /// \param function The C++ function to be pushed. void -lutok::state::push_cxx_function(cxx_function function) const +lutok::state::push_cxx_function(const cxx_function function) const { auto *data = static_cast< cxx_function* >( lua_newuserdata(_pimpl->lua_state, sizeof(cxx_function))); From ba2c74a19636a1dcc08b3c1186dea1a4d28a6e5c Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 22:40:54 -0400 Subject: [PATCH 19/24] Remove redundant static_cast --- c_gate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c_gate.cpp b/c_gate.cpp index 7de7ad3..deddd26 100644 --- a/c_gate.cpp +++ b/c_gate.cpp @@ -60,7 +60,7 @@ lutok::state_c_gate::~state_c_gate(void) = default; lutok::state lutok::state_c_gate::connect(lua_State* raw_state) { - return state(static_cast< void* >(raw_state)); + return state(raw_state); } From 5494f7ae035fbdc415f7b499bec1f2d78a4fa3f3 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 22:58:43 -0400 Subject: [PATCH 20/24] Use explicit constructor --- test_utils.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_utils.hpp b/test_utils.hpp index 9cbb8ed..38f3a92 100644 --- a/test_utils.hpp +++ b/test_utils.hpp @@ -107,7 +107,7 @@ class stack_balance_checker { /// validate upon exit that the item is still there. This is an attempt /// to ensure that already-existing items are not removed from the stack /// by the code under test. - stack_balance_checker(lutok::state& state_, + explicit stack_balance_checker(lutok::state& state_, const bool with_sentinel_ = true) : _state(state_), _with_sentinel(with_sentinel_), From 72f2a93ae435cf1b1751faee8942a83b07b5ebc0 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 23:00:35 -0400 Subject: [PATCH 21/24] Don't access static member through instance --- state_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/state_test.cpp b/state_test.cpp index 40c70a6..b9e5af3 100644 --- a/state_test.cpp +++ b/state_test.cpp @@ -109,8 +109,8 @@ static int c_get_upvalues(lua_State* raw_state) { lutok::state state = lutok::state_c_gate::connect(raw_state); - const int i1 = lua_tointeger(raw_state, state.upvalue_index(1)); - const int i2 = lua_tointeger(raw_state, state.upvalue_index(2)); + const int i1 = lua_tointeger(raw_state, lutok::state::upvalue_index(1)); + const int i2 = lua_tointeger(raw_state, lutok::state::upvalue_index(2)); lua_pushinteger(raw_state, i1); lua_pushinteger(raw_state, i2); return 2; From e4c499fb1f569ddf6ae6598238691e8b786444a9 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Sun, 10 Mar 2024 23:04:51 -0400 Subject: [PATCH 22/24] Add [[nodiscard]] to some functions --- c_gate.hpp | 2 +- debug.hpp | 20 ++++++++++---------- exceptions.hpp | 4 ++-- state.hpp | 32 ++++++++++++++++---------------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/c_gate.hpp b/c_gate.hpp index f98e276..eceb846 100644 --- a/c_gate.hpp +++ b/c_gate.hpp @@ -62,7 +62,7 @@ class state_c_gate { static state connect(lua_State*); - lua_State* c_state(void) const; + [[nodiscard]] lua_State* c_state(void) const; }; diff --git a/debug.hpp b/debug.hpp index a810ae5..1a125bc 100644 --- a/debug.hpp +++ b/debug.hpp @@ -64,16 +64,16 @@ class debug { void get_info(state&, const std::string&) const; void get_stack(state&, int) const; - int event(void) const; - std::string name(void) const; - std::string name_what(void) const; - std::string what(void) const; - std::string source(void) const; - int current_line(void) const; - int n_ups(void) const; - int line_defined(void) const; - int last_line_defined(void) const; - std::string short_src(void) const; + [[nodiscard]] int event(void) const; + [[nodiscard]] std::string name(void) const; + [[nodiscard]] std::string name_what(void) const; + [[nodiscard]] std::string what(void) const; + [[nodiscard]] std::string source(void) const; + [[nodiscard]] int current_line(void) const; + [[nodiscard]] int n_ups(void) const; + [[nodiscard]] int line_defined(void) const; + [[nodiscard]] int last_line_defined(void) const; + [[nodiscard]] std::string short_src(void) const; }; diff --git a/exceptions.hpp b/exceptions.hpp index 52bfa30..6465b50 100644 --- a/exceptions.hpp +++ b/exceptions.hpp @@ -60,7 +60,7 @@ class api_error final : public error { static api_error from_stack(state&, const std::string&); - const std::string& api_function(void) const; + [[nodiscard]] const std::string& api_function(void) const; }; @@ -73,7 +73,7 @@ class file_not_found_error final : public error { explicit file_not_found_error(const std::string&); ~file_not_found_error(void) noexcept override; - const std::string& filename(void) const; + [[nodiscard]] const std::string& filename(void) const; }; diff --git a/state.hpp b/state.hpp index a9d8776..314804e 100644 --- a/state.hpp +++ b/state.hpp @@ -74,12 +74,12 @@ class state { /// Pointer to the shared internal implementation. std::shared_ptr< impl > _pimpl; - void* new_userdata_voidp(size_t) const; - void* to_userdata_voidp(int) const; + [[nodiscard]] void* new_userdata_voidp(size_t) const; + [[nodiscard]] void* to_userdata_voidp(int) const; friend class state_c_gate; explicit state(void*); - void* raw_state(void) const; + [[nodiscard]] void* raw_state(void) const; public: state(void); @@ -88,18 +88,18 @@ class state { void close(void) const; void get_global(const std::string&); void get_global_table(void) const; - bool get_metafield(int, const std::string&) const; - bool get_metatable(int) const; + [[nodiscard]] bool get_metafield(int, const std::string&) const; + [[nodiscard]] bool get_metatable(int) const; void get_table(int); - int get_top(void) const; + [[nodiscard]] int get_top(void) const; void insert(int) const; - bool is_boolean(int) const; - bool is_function(int) const; - bool is_nil(int) const; - bool is_number(int) const; - bool is_string(int) const; - bool is_table(int) const; - bool is_userdata(int) const; + [[nodiscard]] bool is_boolean(int) const; + [[nodiscard]] bool is_function(int) const; + [[nodiscard]] bool is_nil(int) const; + [[nodiscard]] bool is_number(int) const; + [[nodiscard]] bool is_string(int) const; + [[nodiscard]] bool is_table(int) const; + [[nodiscard]] bool is_userdata(int) const; void load_file(const std::string&); void load_string(const std::string&); void new_table(void) const; @@ -123,10 +123,10 @@ class state { void set_global(const std::string&); void set_metatable(int) const; void set_table(int); - bool to_boolean(int) const; - long to_integer(int) const; + [[nodiscard]] bool to_boolean(int) const; + [[nodiscard]] long to_integer(int) const; template< typename Type > Type* to_userdata(int); - std::string to_string(int) const; + [[nodiscard]] std::string to_string(int) const; static int upvalue_index(int); }; From 1cd22fc076b80389a6fcb4a9bad03b3302602c5b Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Mon, 11 Mar 2024 10:35:51 -0400 Subject: [PATCH 23/24] Throw with runtime_err --- state_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/state_test.cpp b/state_test.cpp index b9e5af3..a69f6d9 100644 --- a/state_test.cpp +++ b/state_test.cpp @@ -158,7 +158,7 @@ cxx_divide(lutok::state& state) if (divisor == 0) throw std::runtime_error("Divisor is 0"); if (dividend < 0 || divisor < 0) - throw std::string("Cannot divide negative numbers"); + throw std::runtime_error("Cannot divide negative numbers"); state.push_integer(dividend / divisor); state.push_integer(dividend % divisor); return 2; From c2419931862b1c0d62d801fd86096610aa5a8135 Mon Sep 17 00:00:00 2001 From: Minsoo Choo Date: Mon, 11 Mar 2024 11:06:42 -0400 Subject: [PATCH 24/24] Correct return type --- state.cpp | 2 +- state.hpp | 2 +- state_test.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/state.cpp b/state.cpp index c95b3c7..811bf49 100644 --- a/state.cpp +++ b/state.cpp @@ -835,7 +835,7 @@ lutok::state::to_boolean(const int index) const /// \param index The second parameter to lua_tointeger. /// /// \return The return value of lua_tointeger. -long +int lutok::state::to_integer(const int index) const { assert(is_number(index)); diff --git a/state.hpp b/state.hpp index 314804e..65d4abc 100644 --- a/state.hpp +++ b/state.hpp @@ -124,7 +124,7 @@ class state { void set_metatable(int) const; void set_table(int); [[nodiscard]] bool to_boolean(int) const; - [[nodiscard]] long to_integer(int) const; + [[nodiscard]] int to_integer(int) const; template< typename Type > Type* to_userdata(int); [[nodiscard]] std::string to_string(int) const; static int upvalue_index(int); diff --git a/state_test.cpp b/state_test.cpp index a69f6d9..aa15cca 100644 --- a/state_test.cpp +++ b/state_test.cpp @@ -802,7 +802,7 @@ ATF_TEST_CASE_BODY(push_cxx_function__fail_anything) lua_setglobal(raw(state), "cxx_divide"); ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(-3, -1)") != 0); - ATF_REQUIRE_MATCH("Unhandled exception", lua_tostring(raw(state), -1)); + ATF_REQUIRE_MATCH("Cannot divide negative numbers", lua_tostring(raw(state), -1)); lua_pop(raw(state), 1); }