Skip to content

Commit

Permalink
Lua: introduce LuaJIT/Lua 5.1 compatibility
Browse files Browse the repository at this point in the history
Introduce LuaJIT/Lua 5.1 compatibility by integrating
backports of currently used 5.2+ functionality from
https://github.com/keplerproject/lua-compat-5.2

Future functionality backports should be implemented
in corelib/lua_compat.c as well, serving as a central
place for this purpose.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
Reviewed-by: Stefano Babic <sbabic@denx.de>
Tested-by: Jörg Krause <joerg.krause@embedded.rocks>
  • Loading branch information
stormc authored and sbabic committed Oct 12, 2017
1 parent c7fb7ab commit 7b49b8d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion corelib/Makefile
Expand Up @@ -7,7 +7,7 @@ lib-y += installer.o \
swupdate_dict.o
lib-$(CONFIG_DOWNLOAD) += downloader.o
lib-$(CONFIG_MTD) += mtd-interface.o
lib-$(CONFIG_LUA) += lua_interface.o
lib-$(CONFIG_LUA) += lua_interface.o lua_compat.o
lib-$(CONFIG_HASH_VERIFY) += verify_signature.o
lib-$(CONFIG_ENCRYPTED_IMAGES) += swupdate_decrypt.o
lib-$(CONFIG_LIBCONFIG) += swupdate_settings.o \
Expand Down
59 changes: 59 additions & 0 deletions corelib/lua_compat.c
@@ -0,0 +1,59 @@
/*
* Author: Christian Storm
* Copyright (C) 2017, Siemens AG
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc.
*/

#include <lua.h>
#include <lua_util.h>

/*
* These LuaJIT/Lua 5.1 compatibility functions are taken from
* https://github.com/keplerproject/lua-compat-5.2
*/
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
{
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup + 1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
}
lua_pop(L, nup); /* remove upvalues */
}

void luaL_requiref(lua_State *L, char const* modname, lua_CFunction openf, int glb)
{
luaL_checkstack(L, 3, "not enough stack slots");
lua_pushcfunction(L, openf);
lua_pushstring(L, modname);
lua_call(L, 1, 1);
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_replace(L, -2);
lua_pushvalue(L, -2);
lua_setfield(L, -2, modname);
lua_pop(L, 1);
if (glb) {
lua_pushvalue(L, -1);
lua_setglobal(L, modname);
}
}
#endif
7 changes: 7 additions & 0 deletions include/lua_util.h
Expand Up @@ -36,6 +36,13 @@ int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img);
int lua_handlers_init(void);
#define lua_parser_exit(L) lua_close((lua_State *)L)

#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
#define LUA_OK 0
#define luaL_newlib(L, l) (lua_newtable((L)),luaL_setfuncs((L), (l), 0))
void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
void luaL_requiref(lua_State *L, char const* modname, lua_CFunction openf, int glb);
#endif

#else

#define lua_State void
Expand Down

0 comments on commit 7b49b8d

Please sign in to comment.