summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-01-26 15:44:39 +0000
committerubq323 <ubq323@ubq323.website>2023-01-26 15:44:39 +0000
commitb638ef3b070464f8a7c93e260d244d1516829368 (patch)
tree3f5af541b6578182611cd963758e4dcc25d40fe4
parent1a83a31a6012996afa5f5c62cab3aa5ec5d249f4 (diff)
reloade
-rw-r--r--repl.lua22
1 files changed, 17 insertions, 5 deletions
diff --git a/repl.lua b/repl.lua
index 911e7c7..274d17b 100644
--- a/repl.lua
+++ b/repl.lua
@@ -3,6 +3,18 @@ pprint.setup{show_all=true,use_tostring=true}
local repl_env = {}
+
+local function reload(modname)
+ local oldmod = require(modname)
+ assert(type(oldmod) == "table","reloading only works on modules that return tables")
+ package.loaded[modname] = nil
+ local newmod = require(modname)
+ package.loaded[modname]=oldmod
+ assert(type(newmod) == "table","new module doesn't seem to be a table, cancelling")
+ for k in pairs(oldmod) do oldmod[k] = nil end
+ for k,v in pairs(newmod) do oldmod[k] = v end
+end
+
-- should use loadstring on 5.1
-- works on luajit though
local function load_chunk(s)
@@ -20,6 +32,10 @@ local function read_chunk()
-- if error ends in <eof> try multiline
local function try(text)
+ if text:sub(1,3) == ",r " then
+ return function() reload(text:sub(4)) print("reloaded "..text:sub(4)) end
+ end
+
local f, err = load_chunk("return "..text..";")
if f then return f end
@@ -44,16 +60,12 @@ local function result(ok,...)
end
end
-local function msgh(e)
- return debug.traceback(e,1)
-end
-
while true do
local f,err = read_chunk()
if not f then
print("load error: "..err)
else
- result(xpcall(f,msgh))
+ result(xpcall(f,function(e) return debug.traceback(e,1) end))
end
end