diff mbox series

Lua: introduce LuaJIT/Lua 5.1 compatibility

Message ID 20171010083123.3042-1-christian.storm@siemens.com
State Accepted
Headers show
Series Lua: introduce LuaJIT/Lua 5.1 compatibility | expand

Commit Message

Storm, Christian Oct. 10, 2017, 8:31 a.m. UTC
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>
---
 corelib/Makefile     |  2 +-
 corelib/lua_compat.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/lua_util.h   |  7 +++++++
 3 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 corelib/lua_compat.c

Comments

Stefano Babic Oct. 10, 2017, 4:09 p.m. UTC | #1
Hi Christian,

On 10/10/2017 10:31, Christian Storm wrote:
> 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>
> ---
>  corelib/Makefile     |  2 +-
>  corelib/lua_compat.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  include/lua_util.h   |  7 +++++++
>  3 files changed, 67 insertions(+), 1 deletion(-)
>  create mode 100644 corelib/lua_compat.c
> 
> diff --git a/corelib/Makefile b/corelib/Makefile
> index d99edf2..16767b2 100644
> --- a/corelib/Makefile
> +++ b/corelib/Makefile
> @@ -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 \
> diff --git a/corelib/lua_compat.c b/corelib/lua_compat.c
> new file mode 100644
> index 0000000..6204e8d
> --- /dev/null
> +++ b/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
> diff --git a/include/lua_util.h b/include/lua_util.h
> index 7842cc5..684eddf 100644
> --- a/include/lua_util.h
> +++ b/include/lua_util.h
> @@ -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
> 

Fine with me - CCed to Jörg who had originally raised this issue.

Reviewed-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
Jörg Krause Oct. 12, 2017, 7:22 a.m. UTC | #2
Hi,

On Tue, 2017-10-10 at 18:09 +0200, Stefano Babic wrote:
> Hi Christian,
> 
> On 10/10/2017 10:31, Christian Storm wrote:
> > 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>
> > ---
> >  corelib/Makefile     |  2 +-
> >  corelib/lua_compat.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  include/lua_util.h   |  7 +++++++
> >  3 files changed, 67 insertions(+), 1 deletion(-)
> >  create mode 100644 corelib/lua_compat.c
> > 
> > diff --git a/corelib/Makefile b/corelib/Makefile
> > index d99edf2..16767b2 100644
> > --- a/corelib/Makefile
> > +++ b/corelib/Makefile
> > @@ -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 \
> > diff --git a/corelib/lua_compat.c b/corelib/lua_compat.c
> > new file mode 100644
> > index 0000000..6204e8d
> > --- /dev/null
> > +++ b/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
> > diff --git a/include/lua_util.h b/include/lua_util.h
> > index 7842cc5..684eddf 100644
> > --- a/include/lua_util.h
> > +++ b/include/lua_util.h
> > @@ -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
> > 
> 
> Fine with me - CCed to Jörg who had originally raised this issue.
> 
> Reviewed-by: Stefano Babic <sbabic@denx.de>

Successfully tested in Buildroot with LuaJIT 2.0.5 and Lua 5.1.5.

Tested-by: Jörg Krause <joerg.krause@embedded.rocks>

Best regards,
Jörg Krause
Stefano Babic Oct. 12, 2017, 9:34 a.m. UTC | #3
On 12/10/2017 09:22, Jörg Krause wrote:
> Hi,
> 
> On Tue, 2017-10-10 at 18:09 +0200, Stefano Babic wrote:
>> Hi Christian,
>>
>> On 10/10/2017 10:31, Christian Storm wrote:
>>> 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>
>>> ---
>>>  corelib/Makefile     |  2 +-
>>>  corelib/lua_compat.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>>>  include/lua_util.h   |  7 +++++++
>>>  3 files changed, 67 insertions(+), 1 deletion(-)
>>>  create mode 100644 corelib/lua_compat.c
>>>
>>> diff --git a/corelib/Makefile b/corelib/Makefile
>>> index d99edf2..16767b2 100644
>>> --- a/corelib/Makefile
>>> +++ b/corelib/Makefile
>>> @@ -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 \
>>> diff --git a/corelib/lua_compat.c b/corelib/lua_compat.c
>>> new file mode 100644
>>> index 0000000..6204e8d
>>> --- /dev/null
>>> +++ b/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
>>> diff --git a/include/lua_util.h b/include/lua_util.h
>>> index 7842cc5..684eddf 100644
>>> --- a/include/lua_util.h
>>> +++ b/include/lua_util.h
>>> @@ -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
>>>
>>
>> Fine with me - CCed to Jörg who had originally raised this issue.
>>
>> Reviewed-by: Stefano Babic <sbabic@denx.de>
> 
> Successfully tested in Buildroot with LuaJIT 2.0.5 and Lua 5.1.5.
> 
> Tested-by: Jörg Krause <joerg.krause@embedded.rocks>
> 

Thanks - I will merge it.

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/Makefile b/corelib/Makefile
index d99edf2..16767b2 100644
--- a/corelib/Makefile
+++ b/corelib/Makefile
@@ -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 \
diff --git a/corelib/lua_compat.c b/corelib/lua_compat.c
new file mode 100644
index 0000000..6204e8d
--- /dev/null
+++ b/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
diff --git a/include/lua_util.h b/include/lua_util.h
index 7842cc5..684eddf 100644
--- a/include/lua_util.h
+++ b/include/lua_util.h
@@ -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