From 44062ebd525ec3782668ddb16709bcd152903b1e Mon Sep 17 00:00:00 2001 From: Lupus590 Date: Fri, 8 May 2020 16:07:33 +0100 Subject: [PATCH] Allow lua REPL to warn about using local variables (#367) `local varname = value` results in `varname` being inaccessible in the next REPL input. This is often unintended and can lead to confusing behaviour. We produce a warning when this occurs. --- src/main/resources/assets/computercraft/lua/bios.lua | 5 +++++ .../assets/computercraft/lua/rom/programs/lua.lua | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/main/resources/assets/computercraft/lua/bios.lua b/src/main/resources/assets/computercraft/lua/bios.lua index 687c9c798..cef90b633 100644 --- a/src/main/resources/assets/computercraft/lua/bios.lua +++ b/src/main/resources/assets/computercraft/lua/bios.lua @@ -932,6 +932,11 @@ settings.define("motd.path", { description = [[The path to load random messages from. Should be a colon (":") separated string of file paths.]], type = "string", }) +settings.define("lua.warn_against_use_of_local", { + default = true, + description = [[Print a message when input in the Lua REPL starts with the word 'local'. Local variables defined in the Lua REPL are be inaccessable on the next input.]], + type = "boolean", +}) if term.isColour() then settings.define("bios.use_multishell", { default = true, 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 5eea4dd81..b746240b9 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/lua.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/lua.lua @@ -67,6 +67,13 @@ while bRunning do if s:match("%S") and tCommandHistory[#tCommandHistory] ~= s then table.insert(tCommandHistory, s) end + if settings.get("lua.warn_against_use_of_local") and s:match("^%s*local%s+") then + if term.isColour() then + term.setTextColour(colours.yellow) + end + print("local variables from the previous input are inaccessible afterwards. If you want to be able to use a variable across multiple inputs then remove the local keyword.") + term.setTextColour(colours.white) + end local nForcePrint = 0 local func, e = load(s, "=lua", "t", tEnv)