Skip to content

Commit 6a77233

Browse files
committed
Fixed required modules not getting their name as an argument.
Fixed protected mode not working properly when loading modules.
1 parent b1c6835 commit 6a77233

File tree

2 files changed

+46
-48
lines changed

2 files changed

+46
-48
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
<!-- ![https://img.shields.io/github/release/ReFreezed/LuaPreprocess.svg](https://github.com/ReFreezed/LuaPreprocess/releases/latest) -->
66
<!-- ![https://img.shields.io/github/license/ReFreezed/LuaPreprocess.svg](LICENSE.txt) -->
77

8-
Hot-load any file, including Lua files. Works with *LuaFileSystem* or [*LÖVE*](https://love2d.org/) (including 11.0 and 0.10).
8+
Hot-load any file, including Lua modules.
9+
Works with *LuaFileSystem* or [*LÖVE*](https://love2d.org/) (including 11.0 and 0.10).
910

1011
- [Usage with LuaFileSystem](#usage-with-luafilesystem)
1112
- [Usage in LÖVE](#usage-in-lÖve)
@@ -101,6 +102,7 @@ hotLoader.hasLoaded()
101102
hotLoader.hasRequired()
102103
hotLoader.isAllowingExternalPaths()
103104
hotLoader.load()
105+
hotLoader.log()
104106
hotLoader.preload()
105107
hotLoader.prerequire()
106108
hotLoader.removeAllCustomLoaders()

hotLoader.lua

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
getCheckingInterval
9292
setCheckingInterval
9393
94+
log
9495
getLogFormat
9596
setLogFormat
9697
@@ -180,11 +181,12 @@ SUBSTITUTION_POINT = SUBSTITUTION_POINT or "?"
180181

181182

182183

184+
local function incLevel(level)
185+
return (level == 0) and 0 or 1+level
186+
end
187+
183188
local function errorf(level, s, ...)
184-
error(
185-
("[HotLoader] "..s):format(...),
186-
level == 0 and 0 or 1+level
187-
)
189+
error(("[HotLoader] "..s):format(...), incLevel(level))
188190
end
189191

190192
-- logError( message )
@@ -298,17 +300,17 @@ end
298300

299301

300302

301-
-- path = getModuleFilePath( moduleName )
303+
-- path = getModuleFilePath( level, moduleName )
302304
local getModuleFilePath
303305
do
304306
local TEMPLATE_PATTERN = "[^" .. escapePattern(TEMPLATE_SEPARATOR) .. "]+"
305307
local SUBSTITUTION_POINT_PATTERN = escapePattern(SUBSTITUTION_POINT)
306308

307-
local modulePaths = {--[[ [moduleName1]=path, ... ]]}
309+
local modulePathCache = {--[[ [moduleName1]=path, ... ]]}
308310
local moduleNameModifications = {["."]="/", ["%"]="%%%%"}
309311

310-
--[[local]] function getModuleFilePath(moduleName)
311-
local path = modulePaths[moduleName]
312+
--[[local]] function getModuleFilePath(level, moduleName)
313+
local path = modulePathCache[moduleName]
312314
if path then return path end
313315

314316
local moduleNameModified = moduleName:gsub("[.%%]", moduleNameModifications) -- Change e.g. "foo.bar%1" into "foo/bar%%1".
@@ -322,7 +324,7 @@ do
322324
end
323325
end
324326

325-
modulePaths[moduleName] = path or errorf(1, "Cannot find module '%s'.", moduleName)
327+
modulePathCache[moduleName] = path or errorf(incLevel(level), "Cannot find module '%s'.", moduleName)
326328
return path
327329
end
328330
end
@@ -384,40 +386,38 @@ local getLastModifiedTime
384386
return require"lfs".attributes(path, "modification")
385387
end
386388

387-
-- time = getModuleLastModifiedTime( moduleName )
389+
-- time = getModuleLastModifiedTime( level, moduleName )
388390
-- Returns nil and a message on error.
389-
local function getModuleLastModifiedTime(moduleName)
390-
return getLastModifiedTime(getModuleFilePath(moduleName))
391+
local function getModuleLastModifiedTime(level, moduleName)
392+
return getLastModifiedTime(getModuleFilePath(incLevel(level), moduleName))
391393
end
392394

393395

394396

395-
-- module = loadModule( moduleName, protected )
396-
-- protected: If true then nil is returned on error, otherwise errors are raised.
397-
local function loadModule(moduleName, protected)
397+
-- module|nil = loadModule( level, moduleName, protectedLoad )
398+
local function loadModule(level, moduleName, protected)
399+
local main_chunk, err = loadLuaFile(getModuleFilePath(incLevel(level), moduleName))
398400
local module
399401

400402
if protected then
401-
local ok, chunkOrErr = pcall(loadLuaFile, getModuleFilePath(moduleName))
402-
if not ok then
403-
logError(chunkOrErr)
404-
return nil
405-
end
406-
module = chunkOrErr()
403+
if not main_chunk then logError(err) ; return nil end
404+
405+
local ok, moduleOrErr = pcall(main_chunk, moduleName)
406+
if not ok then logError(tostring(moduleOrErr)) ; return nil end
407+
module = moduleOrErr
407408

408409
else
409-
module = loadLuaFile(getModuleFilePath(moduleName))()
410+
if not main_chunk then error(err, incLevel(level)) end
411+
module = main_chunk(moduleName)
410412
end
411413

412414
if module == nil then module = true end
413415
return module
414416
end
415417

416-
417-
418-
-- resource = loadResource( path, protected )
419-
-- Returns nil and a message on error (if protected is true, otherwise errors are raised).
420-
local function loadResource(path, protected)
418+
-- resource = loadResource( level, path, protectedLoad )
419+
-- Returns nil and a message on error (if protectedLoad is true, otherwise errors are raised).
420+
local function loadResource(level, path, protected)
421421
local loader
422422
= customLoaders[path]
423423
or loaders[(path:match"%.([^.]+)$" or ""):lower()]
@@ -444,7 +444,7 @@ local function loadResource(path, protected)
444444
else
445445
res = loader(path)
446446
if not res then
447-
errorf(1, "Loader returned nothing for '%s'.", path)
447+
errorf(incLevel(level), "Loader returned nothing for '%s'.", path)
448448
end
449449
end
450450

@@ -744,7 +744,7 @@ local function createAndRegisterWatcher(level, watchers, id, path, value)
744744
if isLovePath(path) then
745745
local baseDir, err = love.filesystem.getRealDirectory(path) -- Fails when allowPathsOutsideLove is false.
746746
if not baseDir then
747-
errorf(1+level, "Could not get base directory for file '%s'. (%s)", path, err)
747+
errorf(incLevel(level), "Could not get base directory for file '%s'. (%s)", path, err)
748748
end
749749
watcher.fullPath = baseDir .. "/" .. path
750750

@@ -816,7 +816,7 @@ local function reloadModuleIfModTimeChanged(watcher)
816816
if modTime == watcher.modified then return end
817817

818818
hotLoader.log("Reloading module: %s", watcher.id)
819-
local module = loadModule(watcher.id, true)
819+
local module = loadModule(1, watcher.id, true)
820820

821821
if module == nil
822822
then hotLoader.log("Failed reloading module: %s", watcher.id)
@@ -825,12 +825,12 @@ local function reloadModuleIfModTimeChanged(watcher)
825825
watcher.modified = modTime -- Set this even if loading failed. We don't want to keep loading a corrupt file, for example.
826826
end
827827

828-
local function reloadResourceIfModTimeChanged(watcher)
828+
local function reloadResourceIfModTimeChanged(level, watcher)
829829
local modTime = getLastModifiedTime(watcher.path)
830830
if modTime == watcher.modified then return end
831831

832832
hotLoader.log("Reloading resource: %s", watcher.id)
833-
local res = loadResource(watcher.id, true)
833+
local res = loadResource(incLevel(level), watcher.id, true)
834834

835835
if not res
836836
then hotLoader.log("Failed reloading resource: %s", watcher.id)
@@ -938,7 +938,7 @@ function hotLoader.update(dt)
938938
for _, watcher in ipairs(watchedResources) do
939939
if watcher.watchedDirectoryChanged and ffiWindows_isWritable(watcher.fullPath) then
940940
watcher.watchedDirectoryChanged = false
941-
reloadResourceIfModTimeChanged(watcher)
941+
reloadResourceIfModTimeChanged(0, watcher)
942942
end
943943
end
944944
end
@@ -956,7 +956,7 @@ function hotLoader.update(dt)
956956
else
957957
local watcher = watchedResources[lastCheckedIndex - moduleCount]
958958
if not (watcher.watchedDirectory or watcher.watchedDirectoryChanged) then
959-
reloadResourceIfModTimeChanged(watcher)
959+
reloadResourceIfModTimeChanged(0, watcher)
960960
end
961961
end
962962
end
@@ -1066,7 +1066,7 @@ end
10661066

10671067

10681068
-- resource = load( path [, protectedLoad=false ] [, customLoader ] )
1069-
-- Returns nil and a message on error (if protected is true, otherwise errors are raised).
1069+
-- Returns nil and a message on error (if protectedLoad is true, otherwise errors are raised).
10701070
-- customLoader: If set, replaces the previous custom loader for path.
10711071
function hotLoader.load(path, protected, loader)
10721072
if type(protected) == "function" then
@@ -1082,7 +1082,7 @@ function hotLoader.load(path, protected, loader)
10821082
local watcher = watchedResources[path]
10831083

10841084
if not watcher then
1085-
local res, err = loadResource(path, protected)
1085+
local res, err = loadResource(2, path, protected)
10861086
if not res then return nil, err end
10871087

10881088
watcher = createAndRegisterWatcher(2, watchedResources, path, path, res)
@@ -1133,7 +1133,7 @@ end
11331133
function hotLoader.require(moduleName)
11341134
local watcher = (
11351135
watchedModules[moduleName]
1136-
or createAndRegisterWatcher(2, watchedModules, moduleName, getModuleFilePath(moduleName), loadModule(moduleName, false))
1136+
or createAndRegisterWatcher(2, watchedModules, moduleName, getModuleFilePath(2, moduleName), loadModule(2, moduleName, false))
11371137
)
11381138
return watcher.value
11391139
end
@@ -1155,9 +1155,9 @@ function hotLoader.prerequire(moduleName, module)
11551155

11561156
if watcher then
11571157
watcher.value = module
1158-
watcher.modified = getModuleLastModifiedTime(moduleName) -- May be unnecessary in most cases, but we should seldom reach this line anyway.
1158+
watcher.modified = getModuleLastModifiedTime(2, moduleName) -- May be unnecessary in most cases, but we should seldom reach this line anyway.
11591159
else
1160-
createAndRegisterWatcher(2, watchedModules, moduleName, getModuleFilePath(moduleName), module)
1160+
createAndRegisterWatcher(2, watchedModules, moduleName, getModuleFilePath(2, moduleName), module)
11611161
end
11621162
end
11631163

@@ -1183,7 +1183,7 @@ end
11831183

11841184

11851185

1186-
-- Make hotLoader start over and check the first monitored file next time hotLoader.update() is called.
1186+
-- Make the library start over and check the first monitored file next time hotLoader.update() is called.
11871187
-- The current update is aborted if this is called from within a loader.
11881188
-- resetCheckingState( )
11891189
function hotLoader.resetCheckingState()
@@ -1194,7 +1194,7 @@ end
11941194

11951195

11961196

1197-
function hotLoader.getLogFormat(s)
1197+
function hotLoader.getLogFormat()
11981198
return logFormat
11991199
end
12001200

@@ -1235,12 +1235,8 @@ end
12351235

12361236

12371237

1238-
--==============================================================
1239-
1240-
1241-
1242-
-- To silence hotLoader you can do hotLoader.log=function()end
1243-
-- log( formatString, value1, ... )
1238+
-- Internal function for printing messages.
1239+
-- To silence the library you can do hotLoader.log=function()end
12441240
function hotLoader.log(s, ...)
12451241
s = s:format(...)
12461242
local dateStr = logFormatHasD and os.date"%Y-%m-%d"

0 commit comments

Comments
 (0)