Make various arguments push based on the Lua state passed, not on the Lua state they have (avoids a small class of errors and makes the panic function call tiself more regularly).

This commit is contained in:
ThePhD 2016-12-06 10:51:14 -05:00
parent 7628c635bd
commit 16152c7faf
8 changed files with 69 additions and 15 deletions

View File

@ -102,13 +102,32 @@ Get either the global table or the Lua registry as a :doc:`sol::table<table>`, w
.. code-block:: cpp
:caption: function: Lua set_panic
:caption: function: set_panic
:name: set-panic
void set_panic(lua_CFunction panic);
Overrides the panic function Lua calls when something unrecoverable or unexpected happens in the Lua VM. Must be a function of the that matches the ``int(lua_State*)`` function signature.
.. code-block:: cpp
:caption: function: memory_used
:name: memory-used
std::size_t memory_used() const;
Returns the amount of memory used *in bytes* by the Lua State.
.. code-block:: cpp
:caption: function: collect_garbage
:name: collect-garbage
void collect_garbage();
Attempts to run the garbage collector. Note that this is subject to the same rules as the Lua API's collect_garbage function: memory may or may not be freed, depending on dangling references or other things, so make sure you don't have tables or other stack-referencing items currently alive or referenced that you want to be collected.
.. code-block:: cpp
:caption: function: make a table

View File

@ -61,7 +61,7 @@ author = 'ThePhD'
# The short X.Y version.
version = '2.15'
# The full version, including alpha/beta/rc tags.
release = '2.15.3'
release = '2.15.5'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.

View File

@ -168,9 +168,9 @@ namespace sol {
namespace stack {
template <typename Table, typename Key>
struct pusher<proxy<Table, Key>> {
static int push(lua_State*, const proxy<Table, Key>& p) {
static int push(lua_State* L, const proxy<Table, Key>& p) {
sol::reference r = p;
return r.push();
return r.push(L);
}
};
} // stack

View File

@ -134,12 +134,20 @@ namespace sol {
}
int push() const noexcept {
lua_rawgeti(lua_state(), LUA_REGISTRYINDEX, ref);
return push(lua_state());
}
int push(lua_State* Ls) const noexcept {
lua_rawgeti(Ls, LUA_REGISTRYINDEX, ref);
return 1;
}
void pop(int n = 1) const noexcept {
lua_pop(lua_state(), n);
void pop() const noexcept {
pop(lua_state());
}
void pop(lua_State* Ls, int n = 1) const noexcept {
lua_pop(Ls, n);
}
int registry_index() const noexcept {

View File

@ -43,7 +43,11 @@ namespace sol {
}
int push() const {
lua_pushvalue(L, index);
return push(L);
}
int push(lua_State* Ls) const {
lua_pushvalue(Ls, index);
return 1;
}

View File

@ -243,12 +243,12 @@ namespace sol {
template<typename T>
struct pusher<T, std::enable_if_t<std::is_base_of<reference, T>::value || std::is_base_of<stack_reference, T>::value>> {
static int push(lua_State*, const T& ref) {
return ref.push();
static int push(lua_State* L, const T& ref) {
return ref.push(L);
}
static int push(lua_State*, T&& ref) {
return ref.push();
static int push(lua_State* L, T&& ref) {
return ref.push(L);
}
};

View File

@ -45,12 +45,20 @@ namespace sol {
stack_reference& operator=(const stack_reference&) noexcept = default;
int push() const noexcept {
lua_pushvalue(L, index);
return push(lua_state());
}
int push(lua_State* Ls) const noexcept {
lua_pushvalue(Ls, index);
return 1;
}
void pop(int n = 1) const noexcept {
lua_pop(lua_state(), n);
void pop() const noexcept {
pop(lua_state());
}
void pop(lua_State* Ls, int n = 1) const noexcept {
lua_pop(Ls, n);
}
int stack_index() const noexcept {

View File

@ -45,6 +45,13 @@ namespace sol {
count
};
inline std::size_t total_memory_used(lua_State* L) {
std::size_t kb = lua_gc(L, LUA_GCCOUNT, 0);
kb *= 1024;
kb += lua_gc(L, LUA_GCCOUNTB, 0);
return kb;
}
class state_view {
private:
lua_State* L;
@ -291,6 +298,14 @@ namespace sol {
return reg;
}
std::size_t memory_used() const {
return total_memory_used(lua_state());
}
void collect_garbage() {
lua_gc(lua_state(), LUA_GCCOLLECT, 0);
}
operator lua_State* () const {
return lua_state();
}