From efae7270bb0ff9289625cc472f40c474319a7592 Mon Sep 17 00:00:00 2001 From: Vanya Sergeev Date: Sat, 9 Jul 2016 10:48:01 -0700 Subject: [PATCH] core/async: inherit parent's package.path in callback environment --- radio/core/async.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/radio/core/async.lua b/radio/core/async.lua index a3f5fa108..48142d265 100644 --- a/radio/core/async.lua +++ b/radio/core/async.lua @@ -18,6 +18,10 @@ ffi.cdef[[ int luaL_loadbuffer(lua_State *L, const char *buff, size_t sz, const char *name); const char *lua_tolstring(lua_State *L, int index, size_t *len); + enum { LUA_GLOBALSINDEX = -10002 }; + void lua_getfield (lua_State *L, int index, const char *k); + void lua_setfield (lua_State *L, int index, const char *k); + void lua_pushnumber(lua_State *L, lua_Number n); void lua_pushlstring(lua_State *L, const char *s, size_t len); void lua_pushboolean(lua_State *L, int b); @@ -77,6 +81,12 @@ function callback(callback_factory, ...) -- Open standard libraries in the Lua state ffi.C.luaL_openlibs(L) + -- Inherit parent environment's package.path + ffi.C.lua_getfield(L, ffi.C.LUA_GLOBALSINDEX, "package") + ffi.C.lua_pushlstring(L, package.path, #package.path) + ffi.C.lua_setfield(L, -2, "path") + ffi.C.lua_settop(L, -2) + -- Load the callback factory bytecode into the Lua state local bytecode = string.dump(callback_factory) if ffi.C.luaL_loadbuffer(L, bytecode, #bytecode, nil) ~= 0 then