From b86bfc907f9ca14fdbfd0cdbca5ba88cfc660fc6 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 1 Feb 2020 18:10:07 +0100 Subject: lua: set package path if loaded from a script directory And document the shit. This uses code from commit bc1c024ae032e5b5c. --- player/lua.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'player') diff --git a/player/lua.c b/player/lua.c index 3d23a307fb..5934492bdf 100644 --- a/player/lua.c +++ b/player/lua.c @@ -83,6 +83,7 @@ static const char * const builtin_lua_scripts[][2] = { struct script_ctx { const char *name; const char *filename; + const char *path; // NULL if single file lua_State *state; struct mp_log *log; struct mpv_handle *client; @@ -273,6 +274,30 @@ static int load_scripts(lua_State *L) return 0; } +static void set_path(lua_State *L) +{ + struct script_ctx *ctx = get_ctx(L); + + if (!ctx->path) + return; + + void *tmp = talloc_new(NULL); + + lua_getglobal(L, "package"); // package + lua_getfield(L, -1, "path"); // package path + const char *path = lua_tostring(L, -1); + + char *newpath = talloc_asprintf(tmp, "%s;%s", + mp_path_join(tmp, ctx->path, "?.lua"), + path ? path : ""); + + lua_pushstring(L, newpath); // package path newpath + lua_setfield(L, -3, "path"); // package path + lua_pop(L, 2); // - + + talloc_free(tmp); +} + static int run_lua(lua_State *L) { struct script_ctx *ctx = lua_touserdata(L, -1); @@ -326,6 +351,9 @@ static int run_lua(lua_State *L) assert(lua_gettop(L) == 0); + set_path(L); + assert(lua_gettop(L) == 0); + // run this under an error handler that can do backtraces lua_pushcfunction(L, error_handler); // errf lua_pushcfunction(L, load_scripts); // errf fn @@ -348,6 +376,7 @@ static int load_lua(struct mp_script_args *args) .name = mpv_client_name(args->client), .log = args->log, .filename = args->filename, + .path = args->path, }; if (LUA_VERSION_NUM != 501 && LUA_VERSION_NUM != 502) { -- cgit v1.2.3