Skip to content

Commit

Permalink
Lua 5.4 fixes - replacements for openlib calls (without ruining the s…
Browse files Browse the repository at this point in the history
…tack this time)
  • Loading branch information
xyon authored and xyon committed Jan 11, 2025
1 parent e337b57 commit a011953
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 40 deletions.
38 changes: 14 additions & 24 deletions Src/Module/LuaScript/LuaInterpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,9 @@ void Interpreter::LoadAPI ()
{"unit", vec_unit},
{NULL, NULL}
};
lua_newtable(L);
luaL_setfuncs(L, vecLib, 0);
luaL_newlib(L, vecLib);
lua_setglobal(L, "vec");
lua_getglobal(L, "vec");

static const struct luaL_Reg matLib[] = {
{"identity", mat_identity},
Expand All @@ -784,18 +784,18 @@ void Interpreter::LoadAPI ()
{"rotm", mat_rotm},
{NULL, NULL}
};
lua_newtable(L);
luaL_setfuncs(L, matLib, 0);
luaL_newlib(L, matLib);
lua_setglobal(L, "mat");
lua_getglobal(L, "mat");

// Load the process library
static const struct luaL_Reg procLib[] = {
{"Frameskip", procFrameskip},
{NULL, NULL}
};
lua_newtable(L);
luaL_setfuncs(L, procLib, 0);
luaL_newlib(L, procLib);
lua_setglobal(L, "proc");
lua_getglobal(L, "proc");

// Load the oapi library
static const struct luaL_Reg oapiLib[] = {
Expand Down Expand Up @@ -1055,27 +1055,27 @@ void Interpreter::LoadAPI ()

{NULL, NULL}
};
lua_newtable(L);
luaL_setfuncs(L, oapiLib, 0);
luaL_newlib(L, oapiLib);
lua_setglobal(L, "oapi");
lua_getglobal(L, "oapi");

// Load the (dummy) term library
static const struct luaL_Reg termLib[] = {
{"out", termOut},
{NULL, NULL}
};
lua_newtable(L);
luaL_setfuncs(L, termLib, 0);
luaL_newlib(L, termLib);
lua_setglobal(L, "term");
lua_getglobal(L, "term");

// Load XRSound library
static const struct luaL_Reg XRSoundLib[] = {
{"create_instance", xrsound_create_instance},
{NULL, NULL}
};
lua_newtable(L);
luaL_setfuncs(L, XRSoundLib, 0);
luaL_newlib(L, XRSoundLib);
lua_setglobal(L, "xrsound");
lua_getglobal(L, "xrsound");

// Set up global tables of constants

Expand Down Expand Up @@ -1377,7 +1377,6 @@ void Interpreter::LoadMFDAPI ()
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, mfdLib, 0);
}

Expand All @@ -1403,7 +1402,6 @@ void Interpreter::LoadNTVERTEXAPI ()
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, ntvLib, 0);

/* now the stack has the metatable at index 1 and
Expand Down Expand Up @@ -1451,7 +1449,6 @@ void Interpreter::LoadNTVERTEXAPI ()
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, idxLib, 0);
}

Expand All @@ -1473,9 +1470,9 @@ void Interpreter::LoadBitAPI()
{"ror", bit_ror},
{NULL, NULL}
};
lua_newtable(L);
luaL_setfuncs(L, bitLib, 0);
luaL_newlib(L, bitLib);
lua_setglobal(L, "bit");
lua_getglobal(L, "bit");
}

void Interpreter::LoadLightEmitterMethods ()
Expand Down Expand Up @@ -1504,7 +1501,6 @@ void Interpreter::LoadLightEmitterMethods ()
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, methodLib, 0);

lua_createtable(L, 0, 3);
Expand All @@ -1527,7 +1523,6 @@ void Interpreter::LoadBeaconMethods ()
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, beaconLib, 0);

lua_createtable(L, 0, 3);
Expand All @@ -1549,7 +1544,6 @@ void Interpreter::LoadCustomCameraMethods ()
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, CustomCameraLib, 0);
}

Expand Down Expand Up @@ -1591,7 +1585,6 @@ void Interpreter::LoadSketchpadAPI ()
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, skpLib, 0);

lua_createtable (L, 0, 8);
Expand Down Expand Up @@ -1624,7 +1617,6 @@ void Interpreter::LoadAnnotationAPI ()
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, noteMtd, 0);
}

Expand All @@ -1639,7 +1631,6 @@ void Interpreter::LoadVesselStatusAPI()
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); // push metatable
lua_settable(L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, vs, 0);

static const struct luaL_Reg vs2[] = {
Expand All @@ -1651,7 +1642,6 @@ void Interpreter::LoadVesselStatusAPI()
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); // push metatable
lua_settable(L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, vs2, 0);
}

Expand Down
7 changes: 4 additions & 3 deletions Src/Module/LuaScript/LuaInterpreter/lua_vessel_mtd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,12 +571,13 @@ void Interpreter::LoadVesselAPI ()
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable

lua_newtable(L);
luaL_setfuncs(L, vesselLib, 0);
lua_newtable(L);
luaL_setfuncs(L, vesselAcc, 0);
luaL_newlib(L, vesselAcc);
lua_setglobal(L, "vessel");

// put vessel back on the stack after registering global (lua 5.4)
lua_getglobal(L, "vessel");

// create pseudo-instance "focus"
lua_pushlightuserdata (L, &vfocus);
luaL_getmetatable (L, "VESSEL.vtable"); // push metatable
Expand Down
1 change: 0 additions & 1 deletion Src/Module/LuaScript/LuaInterpreter/lua_xrsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void Interpreter::LoadXRSoundAPI ()
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable
lua_newtable(L);
luaL_setfuncs(L, xrsoundLib, 0);

lua_createtable (L, 0, 7);
Expand Down
4 changes: 2 additions & 2 deletions Src/Plugin/LuaConsole/ConsoleInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ void ConsoleInterpreter::LoadAPI ()
{"SetVerbosity", termSetVerbosity},
{NULL, NULL}
};
lua_newtable(L);
luaL_setfuncs(L, termLib, 0);
luaL_newlib(L, termLib);
lua_setglobal(L, "term");
lua_getglobal(L, "term");

// also replace built-in print so it works as expected in the console
static const struct luaL_Reg printlib[] = {
Expand Down
6 changes: 4 additions & 2 deletions Src/Plugin/LuaMFD/MfdInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ void MFDInterpreter::LoadAPI ()
{"SetVerbosity", termSetVerbosity},
{NULL, NULL}
};
lua_newtable(L);
luaL_setfuncs(L, termLib, 0);
luaL_newlib(L, termLib);
lua_setglobal(L, "term");

// Put term back on stack (lua 5.4)
lua_getglobal(L, "term");

// also replace built-in print so it works as expected in the MFD
static const struct luaL_Reg printlib[] = {
{"print", termOut},
Expand Down
4 changes: 2 additions & 2 deletions Src/Vessel/DeltaGlider/DGLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ int DeltaGlider::Lua_InitInstance (void *context)
luaL_newmetatable (L, "DG.vtable");

// create a table for the overloaded methods
lua_newtable(L);
luaL_setfuncs(L, dgLib, 0);
luaL_newlib(L, dgLib);
lua_setglobal(L, "DG.method");
lua_getglobal(L, "DG.method");

// create metatable for accessing inherited methods from VESSEL
luaL_newmetatable (L, "DG.base");
Expand Down
4 changes: 2 additions & 2 deletions Src/Vessel/HST/HST_Lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ int HST::Lua_InitInstance(void *context)
luaL_newmetatable (L, "HST.vtable");

// create a table for the overloaded methods
lua_newtable(L);
luaL_setfuncs(L, hstLib, 0);
luaL_newlib(L, hstLib);
lua_setglobal(L, "HST.method");
lua_getglobal(L, "HST.method");

// create metatable for accessing inherited methods from VESSEL
luaL_newmetatable (L, "HST.base");
Expand Down
6 changes: 2 additions & 4 deletions Src/Vessel/Quadcopter/QuadcopterLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ int LuaInterface::InitInstance(void *context)
// create metatable for vessel userdata
luaL_newmetatable(L, "QC.vtable");

// create a table for the overloaded methods
lua_newtable(L);
luaL_setfuncs(L, qcLib, 0);
lua_setglobal(L, "QC.method");
// create a new anonymous table for the overloaded methods
luaL_newlib(L, qcLib);

// create metatable for accessing inherited methods from VESSEL
luaL_newmetatable(L, "QC.base");
Expand Down

0 comments on commit a011953

Please sign in to comment.