lua-users home
lua-l archive

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

  Assume a virgin Lua state:

	L = luaL_newstate();

  One can load a Lua script into this state and execute it:


  But what you can do is severely limited (no io, no math, etc).  I started
writing a long post starting from this point, and to summarize, I ended up
with the following:

	A function called _REQUIRE() [1] that pretty much does what
	require() does (finds modules and loads them), but also adds the
	identifier used to load the code into an internal table (unnamed,
	but think package.loaded[]) and returns whatever the module returns. 
	It does not, however, modify the global state.

	A function called _MODULE() [2] that just sets a new environment
	(like module()) but does not take a name (no need for it).  That's

  So now our virgin state would be created thusly:

	L = luaL_newstate();

  Code wanting to be a module would do:

	----[ sample.lua ]---------

	local string = _REQUIRE("string")
	function split(s,delim) ... end
	function wrap(s,margin) ... end
	function trim(s) ...end

	----[ end ]---------

  And code wanting to use this as a module would do:

	local foo = _REQUIRE("sample")

	x = foo.split(...)

  My intent was to start with as a restricted environment as possible and
build things up from there.  Unfortunately (or fortunately, depending upon
your point of view) I went on vacation before I could finish the post, and
thus, I'm posting a rather simplified version of it (it's probably better
for it).  

  Just what *is* the minimum required to support modules?  And how difficult
does it need to be?

  -spc (So, start from luaL_newstate() and go from there ...)

[1]	named thus to distinquish it from the standard require() function
	and two, it needed to be added to the global Lua state and three,
	variables starting with an underscore and upperletters is reserved
	for Lua.

[2]	Named for similar reasons