1 May 2000 04:43
experience implementing a module system
John Belmonte <jvb <at> prairienet.org>
2000-05-01 02:43:49 GMT
2000-05-01 02:43:49 GMT
Recently I had a go at implementing a module system in Lua. I'll describe
how it works and mention the limitations with Lua I ran into.
My motivation was to make a better module system than what is presented in
the Lua faq. The problem with that system is name clashes. If you are
using a global table to represent a module called "string", for example, you
have to be careful that no code in your project uses the same global name
for another purpose. Furthermore the module name must be hard-coded and
repeated throughout the module definition, which does not allow for easy
renaming.
As for the new design I created, I'll start with an example:
--
-- test.lua - this is a file using a module
--
dofile( "ModuleSys.lua" )
local modz = import( "modx.lua" ) -- note how module can be "renamed"
modz.func1() -- in global space must use plain module name
function test()
%modz.func1() -- when not in global space must use upvalue
end
--
-- modx.lua - this is a file defining a module
--
dofile( "ModuleSys.lua" )
(Continue reading)
In my machine, you can get the bug with
the following little chunk:
a = "abc "
print(strlen(a), strfind(a, "\0", strlen(a)-2, 1))
To fix it, you have to change one single line in lstrlib.c:
408c408
< const char *s2 = memfind(s+init, l1, p, l2);
---
> const char *s2 = memfind(s+init, l1-init, p, l2);
(In words, when you start the search with an offset, you must subtract the
offset from the total length of the subject.)
It will be fixed in next version.
-- Roberto
RSS Feed