On Wed, Dec 26, 2018 at 12:02 PM ARATA Mizuki <minorinoki@gmail.com> wrote:
Hi,

Even though LuajitTeX has lfs as a built-in library, trying to load it via `require` doesn't work.
(LuaTeX with Lua 5.2/5.3 doesn't have this problem)

This is because the field package.loaded["lfs"] is not set during initialization.

To fix this, change Luas_open to set the module (returned by the module loader) to the package.loaded table, which is available as _LOADED on the registry table.

Also, zlib suffers from a similar problem, both on LuaTeX and LuajitTeX.

Here is a proposed patch to fix these problems:

diff --git a/source/texk/web2c/luatexdir/lua/luastuff.c b/source/texk/web2c/luatexdir/lua/luastuff.c
index 0d9342223..a0590c400 100644
--- a/source/texk/web2c/luatexdir/lua/luastuff.c
+++ b/source/texk/web2c/luatexdir/lua/luastuff.c
@@ -36,9 +36,14 @@ int lua_active = 0;
 #define Luas_load(Luas,getS,ls,lua_id) \
     lua_load(Luas,getS,ls,lua_id);
 #define Luas_open(name,luaopen_lib) \
+    luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); \
     lua_pushcfunction(L, luaopen_lib); \
     lua_pushstring(L, name); \
-    lua_call(L, 1, 0);
+    lua_call(L, 1, 1); \
+    lua_pushvalue(L, -1); /* copy of module */ \
+    lua_setfield(L, -3, name); /* _LOADED[name] = module */ \
+    lua_setglobal(L, name); /* _G[name] = module */ \
+    lua_pop(L, 1); /* pop _LOADED table */
 #else
 #define Luas_load(Luas,getS,ls,lua_id) \
     lua_load(Luas,getS,ls,lua_id,NULL);
@@ -347,8 +352,7 @@ void luainterpreter(void)
         luatex_socketlua_open(L);
     }
     /*tex |zlib|'s slightly odd calling convention */
-    luaopen_zlib(L);
-    lua_setglobal(L, "zlib");
+    Luas_open("zlib", luaopen_zlib);
     luaopen_gzip(L);
     /*tex our own libraries register themselves */
     luaopen_fio(L);

--
Mizuki

_______________________________________________
dev-luatex mailing list
dev-luatex@ntg.nl
https://mailman.ntg.nl/mailman/listinfo/dev-luatex


require here is not strictly needed, given that the code below work.
We will check the code above, but lfs has some security implications 
and zlib is a bit weird to load.


 $ cat test-lfs.lua 
for k,v in pairs(lfs) do print(k,v) end 

$ luajittex --luaonly test-lfs.lua 
symlinkattributes function: 0x408fc618
isfile function: 0x416764c0
_VERSION LuaFileSystem 1.7.0
attributes function: 0x408fc2f0
_COPYRIGHT Copyright (C) 2003-2017 Kepler Project
shortname function: 0x41676520
chdir function: 0x408fc378
rmdir function: 0x408fc5d0
touch function: 0x408fc528
lock_dir function: 0x408fc818
link function: 0x408fc498
setmode function: 0x408fc4e0
currentdir function: 0x408fc3c0
readlink function: 0x41676540
isdir function: 0x41676500
mkdir function: 0x408fc5a8
dir function: 0x408fc340
lock function: 0x408fc410
unlock function: 0x408fc570
_DESCRIPTION LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution


$ cat test-zlib.lua 
for k,v in pairs(zlib) do print(k,v) end 

$ luajittex --luaonly test-zlib.lua 
compressobj function: 0x41ea6d00
version function: 0x40b7df90
decompressobj function: 0x40b81fa0
decompress function: 0x41ea6cc0
crc32 function: 0x41ea6c70
compress function: 0x41ea6c98
adler32 function: 0x40b7dc40

--
luigi