From b638ef3b070464f8a7c93e260d244d1516829368 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 26 Jan 2023 15:44:39 +0000 Subject: reloade --- repl.lua | 22 +++++++++++++++++----- 1 file 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 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 -- cgit v1.2.3