From 85c45027dabaf46e81d56414bd60ec4d727a4edd Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 30 Mar 2023 09:20:55 +0800 Subject: fix package.path setting. --- src/yuescript/yue_compiler.cpp | 18 ++++++++++++++++++ src/yuescript/yue_compiler.h | 1 + src/yuescript/yuescript.h | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 6b2b27d..a98d1c2 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -3852,10 +3852,21 @@ private: _useModule = true; if (!L) { L = luaL_newstate(); + int top = lua_gettop(L); + DEFER(lua_settop(L, top)); if (_luaOpen) { _luaOpen(static_cast(L)); } passOptions(); + auto it = _config.options.find("path"s); + if (it != _config.options.end()) { + lua_getglobal(L, "package"); + auto path = it->second + ';'; + lua_pushlstring(L, path.c_str(), path.size()); + lua_getfield(L, -2, "path"); + lua_concat(L, 2); + lua_setfield(L, -2, "path"); + } _stateOwner = true; } lua_pushliteral(L, YUE_MODULE); // YUE_MODULE @@ -8498,4 +8509,11 @@ CompileInfo YueCompiler::compile(std::string_view codes, const YueConfig& config return _compiler->compile(codes, config); } +void YueCompiler::clear(void* luaState) { + auto L = static_cast(luaState); + lua_pushliteral(L, YUE_MODULE); // YUE_MODULE + lua_pushnil(L); + lua_rawset(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE] = nil +} + } // namespace yue diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index 860ddfb..bdffe08 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h @@ -82,6 +82,7 @@ public: bool sameModule = false); virtual ~YueCompiler(); CompileInfo compile(std::string_view codes, const YueConfig& config = {}); + static void clear(void* luaState); private: std::unique_ptr _compiler; diff --git a/src/yuescript/yuescript.h b/src/yuescript/yuescript.h index 271a982..19e636c 100644 --- a/src/yuescript/yuescript.h +++ b/src/yuescript/yuescript.h @@ -64,7 +64,9 @@ local function find_modulepath(name) end local file_exist, file_path local tried = {} - local paths = {package.path, yue.options.path} + local paths = {} + paths[#paths + 1] = yue.options.path + paths[#paths + 1] = package.path for i = 1, #paths do local yue_path = paths[i] for path in yue_path:gmatch("[^;]+") do -- cgit v1.2.3-55-g6feb