Skip to content

Commit

Permalink
Merge pull request #5946 from mwerle/bug/fix_lua_debug
Browse files Browse the repository at this point in the history
Fix builds which use "-DDEBUG"
  • Loading branch information
Webster Sheets authored Nov 4, 2024
2 parents 8cb90a2 + 3ccb261 commit 628f39c
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/GeoSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "BaseSphere.h"
#include "Camera.h"
#include "core/Log.h"
#include "vector3.h"

#include <deque>
Expand Down Expand Up @@ -37,7 +38,7 @@ class GeoSphere : public BaseSphere {
virtual double GetHeight(const vector3d &p) const override final
{
const double h = m_terrain->GetHeight(p);
#ifdef DEBUG
#ifndef NDEBUG
// XXX don't remove this. Fix your fractals instead
// Fractals absolutely MUST return heights >= 0.0 (one planet radius)
// otherwise atmosphere and other things break.
Expand All @@ -46,7 +47,7 @@ class GeoSphere : public BaseSphere {
m_terrain->DebugDump();
assert(h >= 0.0);
}
#endif /* DEBUG */
#endif /* NDEBUG */
return h;
}

Expand Down
7 changes: 6 additions & 1 deletion src/lua/LuaMetaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ void *LuaMetaTypeBase::CheckUserdata(lua_State *l, int index, const char *type)
return p;
}

void LuaMetaTypeBase::CreateMetaType(lua_State *l)
void LuaMetaTypeBase::CreateMetaType(lua_State *l, bool pushToStack)
{
luaL_getsubtable(l, LUA_REGISTRYINDEX, "LuaMetaTypes");
m_lua = l;
Expand Down Expand Up @@ -476,4 +476,9 @@ void LuaMetaTypeBase::CreateMetaType(lua_State *l)

// replace the LuaMetaTypes registry table, leaving the created metatype on the stack
lua_replace(l, -2);

// clean up the stack if required
if (!pushToStack) {
lua_pop(l, 1);
}
}
3 changes: 2 additions & 1 deletion src/lua/LuaMetaType.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class LuaMetaTypeBase {
{}

// Creates and registers the lua-side object for this type.
void CreateMetaType(lua_State *l);
// If pushToStack is true, leaves the created metatype object on the top of the stack
void CreateMetaType(lua_State *l, bool pushToStack = false);

const char *GetTypeName() const { return m_typeName.c_str(); }

Expand Down
5 changes: 3 additions & 2 deletions src/lua/LuaObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void LuaObjectBase::CreateObject(const luaL_Reg *methods, const luaL_Reg *attrs,

// create a throwaway, non-inheriting metatype
LuaMetaTypeBase metaType("");
metaType.CreateMetaType(l);
metaType.CreateMetaType(l, true);

// add methods
if (methods) {
Expand Down Expand Up @@ -350,7 +350,7 @@ void LuaObjectBase::CreateClass(const char *type, const char *parent, const luaL
LuaMetaTypeBase metaType(type);
if (parent)
metaType.SetParent(parent);
metaType.CreateMetaType(l);
metaType.CreateMetaType(l, true);

// add attributes
if (attrs) {
Expand Down Expand Up @@ -450,6 +450,7 @@ bool LuaObjectBase::PushRegistered(LuaWrappable *o)

if (!o) {
lua_pushnil(l);
LUA_DEBUG_END(l, 1);
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion src/lua/LuaSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ static int l_space_put_ship_on_route(lua_State *l)
// update velocity direction
ship->SetVelocity((targpos - ship->GetPosition()).Normalized() * pp.getVel() + targetbody->GetVelocityRelTo(ship->GetFrame()));
}
LUA_DEBUG_END(l, 1);

LUA_DEBUG_END(l, 0);
return 0;
}

Expand Down Expand Up @@ -827,6 +828,8 @@ static int l_space_spawn_cargo_near(lua_State *l)
} else {
c_body = new CargoBody(model, LuaRef(l, 1));
}
lua_pop(l, 1); // pop model_name

Body *nearbody = LuaObject<Body>::CheckFromLua(2);
float min_dist = luaL_checknumber(l, 3);
float max_dist = luaL_checknumber(l, 4);
Expand Down
3 changes: 2 additions & 1 deletion src/lua/LuaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define _LUAUTILS_H

// to mask __attribute on MSVC
#include "core/Log.h"
#include "core/macros.h"
#include "DateTime.h"

Expand Down Expand Up @@ -94,7 +95,7 @@ std::string pi_lua_dumpstack(lua_State *l, int top);
void pi_lua_printvalue(lua_State *l, int idx);
void pi_lua_stacktrace(lua_State *l);

#ifdef DEBUG
#ifndef NDEBUG
#define LUA_DEBUG_START(luaptr) const int __luaStartStackDepth = lua_gettop(luaptr)
#define LUA_DEBUG_END(luaptr, expectedStackDiff) \
do { \
Expand Down
8 changes: 5 additions & 3 deletions src/lua/core/Sandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ static const luaL_Reg STANDARD_LIBS[] = {

void pi_lua_open_standard_base(lua_State *L)
{
LUA_DEBUG_START(L);

for (const luaL_Reg *lib = STANDARD_LIBS; lib->func; ++lib) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1);
Expand Down Expand Up @@ -174,9 +176,7 @@ void pi_lua_open_standard_base(lua_State *L)
lua_getfield(L, -1, "open");
assert(lua_iscfunction(L, -1));
LuaFileSystem::register_raw_io_open_function(lua_tocfunction(L, -1));

lua_pop(L, 1); // pop the io table
lua_getglobal(L, LUA_IOLIBNAME);
lua_pop(L, 1); // pop the io.open function

// patch io.open so we can check the path
lua_pushcfunction(L, LuaFileSystem::l_patched_io_open);
Expand Down Expand Up @@ -245,6 +245,8 @@ void pi_lua_open_standard_base(lua_State *L)
lua_setfield(L, -2, "dumpstack");

lua_pop(L, 1); // pop the debug table

LUA_DEBUG_END(L, 0);
}

static int l_handle_error(lua_State *L)
Expand Down
6 changes: 5 additions & 1 deletion src/pigui/LuaModelSpinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ void LuaObject<PiGui::ModelSpinner>::RegisterClass()
using T = PiGui::ModelSpinner;

static LuaMetaType<T> s_metaType(s_type);
s_metaType.CreateMetaType(Lua::manager->GetLuaState());
auto l = Lua::manager->GetLuaState();

LUA_DEBUG_START(l);

s_metaType.CreateMetaType(l);

s_metaType.StartRecording()
.AddCallCtor(&l_model_new)
Expand Down
8 changes: 5 additions & 3 deletions src/pigui/LuaPiGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ namespace PiGui {

void Init()
{
LUA_DEBUG_START(l);

LuaObject<PiGui::Instance>::RegisterClass();

lua_State *l = ::Lua::manager->GetLuaState();

LUA_DEBUG_START(l);

lua_newtable(l);
m_handlers = LuaRef(l, -1);
lua_pop(l, 1);
Expand All @@ -63,6 +63,8 @@ namespace PiGui {
keys.Set(p.first, p.second);
}

lua_pop(l, 1);

pi_lua_import(l, "Event");

// Create a new event table and store it for UI use
Expand Down
2 changes: 1 addition & 1 deletion src/pigui/PiGuiSandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void PiGui::Lua::RegisterSandbox()
pi_lua_split_table_path(L, "PiGui");
lua_gettable(L, -2);
luaL_setfuncs(L, l_stack_functions, 0);
lua_pop(L, 1);
lua_pop(L, 2);

LUA_DEBUG_END(L, 0);
}
2 changes: 1 addition & 1 deletion src/scenegraph/Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace Serializer {
template <typename T>
void readObject(T &out)
{
#ifdef DEBUG
#ifndef NDEBUG
if (!Check(sizeof(T)))
throw std::out_of_range("Serializer::Reader encountered truncated stream.");
#endif
Expand Down

0 comments on commit 628f39c

Please sign in to comment.