I was wondering how difficult it would be to change the compiled Lua in such a way that it won't need to be loaded into memory, but can be mapped into memory using mmap (or similar), and used in situ. I figure the compiled bytecode representation would have to be position independent for that to work.
I am motivated to ask because I can see potential memory savings in embedded systems, for example through bytecode in read-only memory.
if (stat(path, &sb))
errx(1, "can't stat %s", path);
if ((fd = open(path, O_RDONLY)) == -1)
errx(1, "can't open %s", path);
if ((bytecode = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd,
(off_t)0L)) == MAP_FAILED)
errx(1, "can't map application file into memory");
errx(1, "failed to load %s: %s", path, lua_tostring(L, -1));
munmap(bytecode, sb.st_size);
close(fd);
if (lua_pcall(L, 0, LUA_MULTRET, 0))
errx(1, "failed to execute %s: %s", path, lua_tostring(L, -1));
That is not for "V6", it is for 5.5, and it is already implemented
(see it in github). Note that only the bytecode, a few structures, and
strings are used directly from the "static" memory. (As Sainan pointed
out, several structures contain pointers, which make them difficult to
use unmodified.)
-- Roberto
Maybe a little off-topic, but regarding the 5.5 bytecode
- What will be the major differences between 5.4 and 5.5 bytecode (aside from alignment)
- What is the "variant" in the instruction format?
Cheers,
~b