lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 21/01/2020 16.05, Hongyi Zhao wrote:
What do mean by saying that ``You don't even need a file system for running Lua.''? Could you please give a more concrete example for this case?

You can add loader functions to `package.preload`.  On a match,
`require` will just get & run that function instead of searching the
file system.  (So if you know what modules your code needs, you can
preload all of them.  And then require doesn't need to hit the file system.)

(IIRC, for C libraries, you'd add `package.loaded["foo"] = luaopen_foo`
and for Lua files, you can just say `package.loaded["foo"] =
loadfile("/path/to/foo.lua")`, or alternatively `load [[(contents of
that file)]]`. From C, you'd put the file's contents in a string /
header so that it's linked into the program and then use lua_loadstring.)

When creating a Lua state from your program, you just do the preload
stuff before running code in the state, and then your code can still
look like normal Lua code that's fetching stuff from the file system
with require, only it doesn't involve the file system.  (And then you
can shim / remove the actual file system access functions and it still
works when there's no file system / maybe even no operating system.)

-- nobody