From 8bc8946be888f8b0e1c08369a39ecbc99f914a59 Mon Sep 17 00:00:00 2001 From: James R Date: Sat, 10 Oct 2020 16:43:09 -0700 Subject: [PATCH 1/3] Turn the lua sector lines hack into a macro --- src/doomtype.h | 8 ++++++++ src/lua_maplib.c | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/doomtype.h b/src/doomtype.h index 0aa3e23e0..4e13ba96d 100644 --- a/src/doomtype.h +++ b/src/doomtype.h @@ -371,4 +371,12 @@ typedef UINT32 tic_t; #define WSTRING2(s) L ## s #define WSTRING(s) WSTRING2 (s) +/* +A hack by Monster Iestyn: Return a pointer to a field of +a struct from a pointer to another field in the struct. +Needed for some lua shenanigans. +*/ +#define FIELDFROM( type, field, have, want ) \ + (void *)((intptr_t)(field) - offsetof (type, have) + offsetof (type, want)) + #endif //__DOOMTYPE__ diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 12a0be27d..132c81e48 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -447,7 +447,7 @@ static int sectorlines_get(lua_State *L) // get the "linecount" by shifting our retrieved memory address of "lines" to where "linecount" is in the sector_t, then dereferencing the result // we need this to determine the array's actual size, and therefore also the maximum value allowed as an index // this only works if seclines is actually a pointer to a sector's lines member in memory, oh boy - numoflines = (size_t)(*(size_t *)(((size_t)seclines) - (offsetof(sector_t, lines) - offsetof(sector_t, linecount)))); + numoflines = *(size_t *)FIELDFROM (sector_t, seclines, lines,/* -> */linecount); /* OLD HACK // check first linedef to figure which of its sectors owns this sector->lines pointer From 7f8ec74c274e0bdd3b9f35d8c329ae7707456022 Mon Sep 17 00:00:00 2001 From: James R Date: Sat, 10 Oct 2020 17:40:01 -0700 Subject: [PATCH 2/3] Use the macro in sectorlines_num too --- src/lua_maplib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 132c81e48..855ec9482 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -481,7 +481,7 @@ static int sectorlines_num(lua_State *L) return luaL_error(L, "accessed sector_t.lines doesn't exist anymore."); // see comments in the _get function above - numoflines = (size_t)(*(size_t *)(((size_t)seclines) - (offsetof(sector_t, lines) - offsetof(sector_t, linecount)))); + numoflines = *(size_t *)FIELDFROM (sector_t, seclines, lines,/* -> */linecount); lua_pushinteger(L, numoflines); return 1; } From 4b0725f36f2f94af90206672638e6dd008de3502 Mon Sep 17 00:00:00 2001 From: James R Date: Thu, 15 Oct 2020 16:17:51 -0700 Subject: [PATCH 3/3] Use FIELDFROM in lua polyobject code --- src/lua_polyobjlib.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lua_polyobjlib.c b/src/lua_polyobjlib.c index c4dfa8ae4..365d97056 100644 --- a/src/lua_polyobjlib.c +++ b/src/lua_polyobjlib.c @@ -99,7 +99,7 @@ static int polyobjvertices_get(lua_State *L) } } - numofverts = (size_t)(*(size_t *)(((size_t)polyverts) - (offsetof(polyobj_t, vertices) - offsetof(polyobj_t, numVertices)))); + numofverts = *(size_t *)FIELDFROM (polyobj_t, polyverts, vertices,/* -> */numVertices); if (!numofverts) return luaL_error(L, "no vertices found!"); @@ -120,7 +120,7 @@ static int polyobjvertices_num(lua_State *L) if (!polyverts || !(*polyverts)) return luaL_error(L, "accessed polyobj_t.vertices doesn't exist anymore."); - numofverts = (size_t)(*(size_t *)(((size_t)polyverts) - (offsetof(polyobj_t, vertices) - offsetof(polyobj_t, numVertices)))); + numofverts = *(size_t *)FIELDFROM (polyobj_t, polyverts, vertices,/* -> */numVertices); lua_pushinteger(L, numofverts); return 1; } @@ -156,7 +156,7 @@ static int polyobjlines_get(lua_State *L) } } - numoflines = (size_t)(*(size_t *)(((size_t)polylines) - (offsetof(polyobj_t, lines) - offsetof(polyobj_t, numLines)))); + numoflines = *(size_t *)FIELDFROM (polyobj_t, polylines, lines,/* -> */numLines); if (!numoflines) return luaL_error(L, "no lines found!"); @@ -177,7 +177,7 @@ static int polyobjlines_num(lua_State *L) if (!polylines || !(*polylines)) return luaL_error(L, "accessed polyobj_t.lines doesn't exist anymore."); - numoflines = (size_t)(*(size_t *)(((size_t)polylines) - (offsetof(polyobj_t, lines) - offsetof(polyobj_t, numLines)))); + numoflines = *(size_t *)FIELDFROM (polyobj_t, polylines, lines,/* -> */numLines); lua_pushinteger(L, numoflines); return 1; }