Consolidate librpmbuild Lua code to one place

Not much changes here in practise, although this does patch_nums and
source_nums "leaking" after a spec parse as we forgot to update
*that* code when adding them. More visible when consolidated...

Also store the Lua context in the spec struct. This doesn't make much
of a difference as it is, but it'll be needed someday when we create
a new Lua environment for each spec parse, and at any rate this is
gives us a single, easy place to check whether it was initialized or not.
This commit is contained in:
Panu Matilainen 2020-10-15 16:13:49 +03:00
parent 6de7374bb8
commit 2579d3e5ad
5 changed files with 75 additions and 39 deletions

View File

@ -19,6 +19,10 @@ librpmbuild_la_SOURCES = \
parsePolicies.c policies.c \
rpmbuild_internal.h rpmbuild_misc.h
if WITH_LUA
librpmbuild_la_SOURCES += speclua.c
endif
librpmbuild_la_LDFLAGS = -version-info $(rpm_version_info)
librpmbuild_la_LIBADD = \
$(top_builddir)/lib/librpm.la \

View File

@ -12,7 +12,6 @@
#include <rpm/rpmlog.h>
#include <rpm/rpmurl.h>
#include <rpm/rpmfileutil.h>
#include "rpmio/rpmlua.h"
#include "build/rpmbuild_internal.h"
#include "build/rpmbuild_misc.h"
#include "debug.h"
@ -197,30 +196,6 @@ static int parseNoSource(rpmSpec spec, const char * field, rpmTagVal tag)
return 0;
}
static void addLuaSource(const struct Source *p)
{
#ifdef WITH_LUA
rpmlua lua = NULL; /* global state */
const char * what = (p->flags & RPMBUILD_ISPATCH) ? "patches" : "sources";
rpmluaPushTable(lua, what);
rpmluav var = rpmluavNew();
rpmluavSetListMode(var, 1);
rpmluavSetValue(var, RPMLUAV_STRING, p->path);
rpmluaSetVar(lua, var);
rpmluavFree(var);
rpmluaPop(lua);
what = (p->flags & RPMBUILD_ISPATCH) ? "patch_nums" : "source_nums";
rpmluaPushTable(lua, what);
var = rpmluavNew();
rpmluavSetListMode(var, 1);
rpmluavSetValueNum(var, p->num);
rpmluaSetVar(lua, var);
rpmluavFree(var);
rpmluaPop(lua);
#endif
}
static int tryDownload(const struct Source *p)
{
int rc = 0;
@ -346,7 +321,9 @@ int addSource(rpmSpec spec, int specline, const char *srcname, rpmTagVal tag)
rpmPushMacro(spec->macros, buf, NULL, p->fullSource, RMIL_SPEC);
free(buf);
addLuaSource(p);
#ifdef WITH_LUA
addLuaSource(spec->lua, p);
#endif
if (!nofetch && tryDownload(p))
return RPMRC_FAIL;

View File

@ -5,6 +5,7 @@
#include <rpm/rpmutil.h>
#include <rpm/rpmstrpool.h>
#include "build/rpmbuild_misc.h"
#include "rpmio/rpmlua.h"
#undef HASHTYPE
#undef HTKEYTYPE
@ -137,6 +138,7 @@ struct rpmSpec_s {
Package sourcePackage;
rpmMacroContext macros;
rpmlua lua;
rpmstrPool pool;
StringBuf prep; /*!< %prep scriptlet. */
@ -594,6 +596,15 @@ int specExpand(rpmSpec spec, int lineno, const char *sbuf,
RPM_GNUC_INTERNAL
int readManifest(rpmSpec spec, const char *path, const char *descr, int flags,
ARGV_t *avp, StringBuf *sbp);
RPM_GNUC_INTERNAL
void * specLuaInit(rpmSpec spec);
RPM_GNUC_INTERNAL
void * specLuaFini(rpmSpec spec);
RPM_GNUC_INTERNAL
void addLuaSource(rpmlua lua, const struct Source *p);
#ifdef __cplusplus
}
#endif

View File

@ -13,7 +13,6 @@
#include <rpm/rpmlog.h>
#include <rpm/rpmfileutil.h>
#include "rpmio/rpmlua.h"
#include "lib/rpmfi_internal.h" /* rpmfiles stuff */
#include "build/rpmbuild_internal.h"
@ -251,15 +250,7 @@ rpmSpec newSpec(void)
spec->pool = rpmstrPoolCreate();
#ifdef WITH_LUA
/* make sure patches and sources tables always exist */
rpmlua lua = NULL; /* global state */
const char * luavars[] = { "patches", "sources",
"patch_nums", "source_nums", NULL, };
for (const char **vp = luavars; vp && *vp; vp++) {
rpmluaDelVar(lua, *vp);
rpmluaPushTable(lua, *vp);
rpmluaPop(lua);
}
spec->lua = specLuaInit(spec);
#endif
return spec;
}
@ -310,9 +301,7 @@ rpmSpec rpmSpecFree(rpmSpec spec)
#ifdef WITH_LUA
// only destroy lua tables if there are no BASpecs left
if (spec->recursing || spec->BACount == 0) {
rpmlua lua = NULL; /* global state */
rpmluaDelVar(lua, "patches");
rpmluaDelVar(lua, "sources");
spec->lua = specLuaFini(spec);
}
#endif

55
build/speclua.c Normal file
View File

@ -0,0 +1,55 @@
#include "system.h"
#include <lua.h>
#include "rpmio/rpmlua.h"
#include "build/rpmbuild_internal.h"
#include "debug.h"
static const char * luavars[] = { "patches", "sources",
"patch_nums", "source_nums", NULL, };
void * specLuaInit(rpmSpec spec)
{
rpmlua lua = rpmluaGetGlobalState();
for (const char **vp = luavars; vp && *vp; vp++) {
rpmluaDelVar(lua, *vp);
rpmluaPushTable(lua, *vp);
rpmluaPop(lua);
}
return lua;
}
void * specLuaFini(rpmSpec spec)
{
rpmlua lua = spec->lua;
for (const char **vp = luavars; vp && *vp; vp++) {
rpmluaDelVar(lua, *vp);
}
return NULL;
}
void addLuaSource(rpmlua lua, const struct Source *p)
{
const char * what = (p->flags & RPMBUILD_ISPATCH) ? "patches" : "sources";
rpmluaPushTable(lua, what);
rpmluav var = rpmluavNew();
rpmluavSetListMode(var, 1);
rpmluavSetValue(var, RPMLUAV_STRING, p->path);
rpmluaSetVar(lua, var);
rpmluavFree(var);
rpmluaPop(lua);
what = (p->flags & RPMBUILD_ISPATCH) ? "patch_nums" : "source_nums";
rpmluaPushTable(lua, what);
var = rpmluavNew();
rpmluavSetListMode(var, 1);
rpmluavSetValueNum(var, p->num);
rpmluaSetVar(lua, var);
rpmluavFree(var);
rpmluaPop(lua);
}