Skip to content

Commit

Permalink
lua: add polyfills for lua 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeOShannessy committed Jul 6, 2023
1 parent 27f9a09 commit 182f54f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
39 changes: 37 additions & 2 deletions Source/smokeview/lua_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -5663,7 +5663,9 @@ int yieldOrOkSSF = LUA_YIELD;
int runSSFScript() {
if (yieldOrOkSSF == LUA_YIELD) {
int nresults = 0;
#if LUA_VERSION_NUM < 504
#if LUA_VERSION_NUM < 502
yieldOrOkSSF = lua_resume(L, 0);
#elif LUA_VERSION_NUM < 504
yieldOrOkSSF = lua_resume(L, NULL, 0);
#else
yieldOrOkSSF = lua_resume(L, NULL, 0, &nresults);
Expand Down Expand Up @@ -5702,7 +5704,9 @@ int yieldOrOk = LUA_YIELD;
int runLuaScript() {
if (yieldOrOk == LUA_YIELD) {
int nresults = 0;
#if LUA_VERSION_NUM < 504
#if LUA_VERSION_NUM < 502
yieldOrOk = lua_resume(L, 0);
#elif LUA_VERSION_NUM < 504
yieldOrOk = lua_resume(L, NULL, 0);
#else
yieldOrOk = lua_resume(L, NULL, 0, &nresults);
Expand Down Expand Up @@ -5737,4 +5741,35 @@ int runLuaScript() {
GLUTPOSTREDISPLAY;
return yieldOrOk;
}
#if LUA_VERSION_NUM < 502
LUA_API lua_Number lua_version (lua_State *L) {
UNUSED(L);
return LUA_VERSION_NUM;
}

LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
lua_Number v = lua_version(L);
if (sz != LUAL_NUMSIZES) /* check numeric types */
luaL_error(L, "core and library have incompatible numeric types");
else if (v != ver)
luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
(LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v);
}

LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
if (l->func == NULL) /* place holder? */
lua_pushboolean(L, 0);
else {
int i;
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -nup);
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
}
lua_setfield(L, -(nup + 2), l->name);
}
lua_pop(L, nup); /* remove upvalues */
}
#endif
#endif
20 changes: 20 additions & 0 deletions Source/smokeview/lua_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,23 @@ int runScriptString(char *string);
int lua_get_sliceinfo(lua_State *L);
int lua_get_csvinfo(lua_State *L);
int lua_initsmvdata(lua_State *L);
#if LUA_VERSION_NUM < 502
/* macro to avoid warnings about unused variables */
#if !defined(UNUSED)
#define UNUSED(x) ((void)(x))
#endif
#include <lauxlib.h>
#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz);
#define luaL_checkversion(L) \
luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
#define LUA_OK 0
#define luaL_newlibtable(L,l) \
lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
#define luaL_newlib(L,l) \
(luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
#endif
#if LUA_VERSION_NUM < 504
#define LUA_OK 0
#endif

0 comments on commit 182f54f

Please sign in to comment.