Skip to content

Commit

Permalink
Remove "api" global
Browse files Browse the repository at this point in the history
Now that we support multiple threads in LUA we need to save the API
object on each state rather than as a global.
  • Loading branch information
blairsteven committed Jan 22, 2024
1 parent 74a770a commit 15caa66
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ static bool apteryx_xml_debug = false;
printf (fmt, ## args); \
}

/* Global pointer to the loaded schema */
static sch_instance *api = NULL;
/* A root user can write to read-only fields */
static bool is_root = true;

Expand Down Expand Up @@ -116,11 +114,31 @@ push_node (lua_State * L, sch_instance * api, const char *path, const char *key)
return true;
}

static sch_instance *
_get_api (lua_State *L)
{
lua_pushstring(L, "__apteryx_api");
lua_gettable(L, LUA_REGISTRYINDEX);
sch_instance *api = (sch_instance *)lua_topointer(L, -1);
lua_pop(L, 1);
return api;
}

static void
_set_api (lua_State *L, sch_instance *api)
{
lua_pushstring(L, "__apteryx_api");
lua_pushlightuserdata(L, api);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pop(L, 1);
}

static int
__index (lua_State * L)
{
const char *path;
const char *key;
sch_instance *api = _get_api(L);

/* If no API, this key does not exist! */
if (!api)
Expand Down Expand Up @@ -165,6 +183,7 @@ __newindex (lua_State * L)
const char *key;
const char *value;
char *name;
sch_instance *api = _get_api (L);

/* If no API, this key does not exist! */
if (!api)
Expand Down Expand Up @@ -227,6 +246,7 @@ __call (lua_State * L)
const char *path;
const char *key;
const char *value;
sch_instance *api = _get_api (L);

/* If no API, this key does not exist! */
if (!api)
Expand Down Expand Up @@ -331,7 +351,9 @@ lua_apteryx_api (lua_State * L)
{"__call", __call},
{NULL, NULL}
};
sch_instance *api = _get_api (L);
const char *path = ".";

if (lua_gettop (L) == 1 && lua_isstring (L, 1))
{
path = lua_tostring (L, 1);
Expand All @@ -345,6 +367,7 @@ lua_apteryx_api (lua_State * L)

/* Parse XML files in the specified directory */
api = sch_load (path);
_set_api (L, api);
if (!api)
{
/* No good */
Expand All @@ -371,6 +394,7 @@ lua_apteryx_valid (lua_State * L)
luaL_error (L, "Invalid arguments: requires path");
return 0;
}
sch_instance *api = _get_api (L);

/* If no API, this path does not exist! */
if (api && sch_lookup (api, lua_tostring (L, 1)))
Expand Down

0 comments on commit 15caa66

Please sign in to comment.