From 0cb659d78cdc5549d77ce01d540f89ec8498674d Mon Sep 17 00:00:00 2001 From: SquidDev Date: Thu, 30 May 2019 19:52:55 +0100 Subject: [PATCH] Ensure require is usable within the REPL As 'require' operates relative to the current program's directory, rather than the current directory, it meant we were trying to load files from /rom/programs. This is never a good idea, so we add the current directory to the package path, allowing you to use require as one'd expect. --- .../computercraft/lua/rom/programs/lua.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/lua.lua b/src/main/resources/assets/computercraft/lua/rom/programs/lua.lua index f48906b39..e1a4cb0f2 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/lua.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/lua.lua @@ -18,6 +18,23 @@ local tEnv = { } setmetatable( tEnv, { __index = _ENV } ) +-- Replace our package.path, so that it loads from the current directory, rather +-- than from /rom/programs. This makes it a little more friendly to use and +-- closer to what you'd expect. +do + local dir = shell.dir() + if dir:sub(1, 1) ~= "/" then dir = "/" .. dir end + if dir:sub(-1) ~= "/" then dir = dir .. "/" end + + local strip_path = "?;?.lua;?/init.lua;" + local path = package.path + if path:sub(1, #strip_path) == strip_path then + path = path:sub(#strip_path + 1) + end + + package.path = dir .. "?;" .. dir .. "?.lua;" .. dir .. "?/init.lua;" .. path +end + if term.isColour() then term.setTextColour( colours.yellow ) end