summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kolesa <d.kolesa@samsung.com>2020-07-31 18:54:04 +0200
committerDaniel Kolesa <d.kolesa@samsung.com>2020-07-31 19:01:59 +0200
commit5ed0161564729388c970889518c903cdef5ff22e (patch)
treeade8458c4ac7e4d574f667d6671e59a4081f15b7
parent794ca20078164908b75c2108ee11b483da73cbb4 (diff)
elua: add searchpath impl for 5.1 compat
-rw-r--r--src/lib/elua/elua.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/lib/elua/elua.c b/src/lib/elua/elua.c
index 3834df1504..6fa856372e 100644
--- a/src/lib/elua/elua.c
+++ b/src/lib/elua/elua.c
@@ -76,6 +76,53 @@ _ffi_loader(lua_State *L)
76 lua_call(L, 2, 1); 76 lua_call(L, 2, 1);
77 return 1; 77 return 1;
78} 78}
79
80#if LUA_VERSION_NUM < 502
81/* adapted from lua 5.2 source */
82static const char *
83_push_next_template(lua_State *L, const char *path)
84{
85 while (*path == *LUA_PATHSEP) ++path;
86 if (!*path)
87 return NULL;
88 const char *l = strchr(path, *LUA_PATHSEP);
89 if (!l)
90 l = path + strlen(path);
91 lua_pushlstring(L, path, l - path);
92 return l;
93}
94
95static int
96_elua_searchpath(lua_State *L)
97{
98 const char *name = luaL_checkstring(L, 1);
99 const char *path = luaL_checkstring(L, 2);
100 const char *sep = luaL_optstring(L, 3, ".");
101 const char *dsep = luaL_optstring(L, 4, LUA_DIRSEP);
102 luaL_Buffer msg;
103 luaL_buffinit(L, &msg);
104 if (*sep)
105 name = luaL_gsub(L, name, sep, dsep);
106 while ((path = _push_next_template(L, path)))
107 {
108 const char *fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
109 lua_remove(L, -2);
110 FILE *rf = fopen(fname, "r");
111 if (rf)
112 {
113 fclose(rf);
114 return 1; /* found */
115 }
116 lua_pushfstring(L, "\n\tno file " LUA_QS, fname);
117 lua_remove(L, -2);
118 luaL_addvalue(&msg);
119 }
120 luaL_pushresult(&msg);
121 lua_pushnil(L);
122 lua_insert(L, -2);
123 return 2; /* nil plus error message */
124}
125#endif
79#endif 126#endif
80 127
81EAPI Elua_State * 128EAPI Elua_State *
@@ -92,6 +139,16 @@ elua_state_new(const char *progname)
92#ifdef ENABLE_LUA_OLD 139#ifdef ENABLE_LUA_OLD
93 /* search for cffi-lua early, and pass it through as ffi */ 140 /* search for cffi-lua early, and pass it through as ffi */
94 lua_getglobal(L, "package"); 141 lua_getglobal(L, "package");
142#if LUA_VERSION_NUM < 502
143 /* lua 5.1 does not have package.searchpath, we rely on having that */
144 lua_getfield(L, -1, "searchpath");
145 if (lua_isnil(L, -1))
146 {
147 lua_pushcfunction(L, _elua_searchpath);
148 lua_setfield(L, -3, "searchpath");
149 }
150 lua_pop(L, 1);
151#endif
95 lua_getfield(L, -1, "preload"); 152 lua_getfield(L, -1, "preload");
96 lua_getfield(L, -2, "searchers"); 153 lua_getfield(L, -2, "searchers");
97 if (lua_isnil(L, -1)) 154 if (lua_isnil(L, -1))