summaryrefslogtreecommitdiff
path: root/src/lua-apk.c
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2013-06-13 14:21:01 +0000
committerNatanael Copa <ncopa@alpinelinux.org>2013-06-13 14:21:01 +0000
commitf292a858677ae0e1af8910ffbd4b338f4b36c18b (patch)
tree3b8eed188655af869e95b5077f2ca5515e23fc91 /src/lua-apk.c
parent860aa642f0c2000e0e012d59c3cd7e1e985c00dd (diff)
downloadapk-tools-f292a858677ae0e1af8910ffbd4b338f4b36c18b.tar.gz
apk-tools-f292a858677ae0e1af8910ffbd4b338f4b36c18b.tar.bz2
apk-tools-f292a858677ae0e1af8910ffbd4b338f4b36c18b.tar.xz
apk-tools-f292a858677ae0e1af8910ffbd4b338f4b36c18b.zip
lua: support for lua 5.2
Diffstat (limited to 'src/lua-apk.c')
-rw-r--r--src/lua-apk.c47
1 files changed, 32 insertions, 15 deletions
diff --git a/src/lua-apk.c b/src/lua-apk.c
index 8e68c0f..16bd734 100644
--- a/src/lua-apk.c
+++ b/src/lua-apk.c
@@ -12,6 +12,10 @@
#define APK_DB_META "apk_database"
#define APK_IPKG_META "apk_installed_package"
+#if LUA_VERSION_NUM < 502
+# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
+#endif
+
struct flagmap {
const char *name;
int flag;
@@ -33,6 +37,15 @@ struct flagmap opendb_flagmap[] = {
{NULL, 0}
};
+/* implemented as luaL_typerror until lua 5.1, dropped in 5.2
+ * (C) 1994-2012 Lua.org, PUC-Rio. MIT license
+ */
+static int typerror (lua_State *L, int narg, const char *tname) {
+ const char *msg = lua_pushfstring(L, "%s expected, got %s",
+ tname, luaL_typename(L, narg));
+ return luaL_argerror(L, narg, msg);
+}
+
static apk_blob_t check_blob(lua_State *L, int index)
{
apk_blob_t blob;
@@ -137,7 +150,7 @@ static struct apk_database *checkdb(lua_State *L, int index)
luaL_checktype(L, index, LUA_TUSERDATA);
db = (struct apk_database *) luaL_checkudata(L, index, APK_DB_META);
if (db == NULL)
- luaL_typerror(L, index, APK_DB_META);
+ typerror(L, index, APK_DB_META);
return db;
}
@@ -258,7 +271,7 @@ static int Pinstalled(lua_State *L)
return 1;
}
-static const luaL_reg reg_apk_methods[] = {
+static const luaL_Reg reg_apk_methods[] = {
{"version_validate", Pversion_validate},
{"version_compare", Pversion_compare},
{"version_is_less", Pversion_is_less},
@@ -270,29 +283,33 @@ static const luaL_reg reg_apk_methods[] = {
{NULL, NULL}
};
-static const luaL_reg reg_apk_db_meta_methods[] = {
+static const luaL_Reg reg_apk_db_meta_methods[] = {
{"__gc", Papk_db_close},
{NULL, NULL}
};
+static int db_create_meta(lua_State *L)
+{
+ luaL_newmetatable(L, APK_DB_META);
+ lua_newtable(L);
+
+ lua_setfield(L, -2, "__index");
+ lua_pushcfunction(L, Papk_db_close);
+ lua_setfield(L, -2, "__gc");
+ return 1;
+}
+
LUALIB_API int luaopen_apk(lua_State *L)
{
- luaL_register(L, LIBNAME, reg_apk_methods);
+ db_create_meta(L);
+ luaL_newlib(L, reg_apk_methods);
+ lua_pushvalue(L, -1);
+ lua_setglobal(L, LIBNAME);
+
lua_pushliteral(L, "version");
lua_pushliteral(L, APK_VERSION);
lua_settable(L, -3);
- luaL_newmetatable(L, APK_DB_META);
- luaL_register(L, NULL, reg_apk_db_meta_methods);
- lua_pushliteral(L, "__index");
- lua_pushvalue(L, -3);
- lua_rawset(L, -3);
-
- lua_pushliteral(L, "__metatable");
- lua_pushvalue(L, -3);
- lua_rawset(L, -3);
- lua_pop(L, 1);
-
return 1;
}