lua-users home
lua-l archive

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


2008/12/9 Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>:
>> If the DLL you want to package are Lua binary modules, you can also
>> statically link them in your executable, and with the help of the
>> preload searcher it will be transparent to your application and other
>> Lua modules (ie. you will still load them through require).
>
> Just for the record: this works transparently if luaopen_xxx in your C module
> calls luaL_register with a non-null name, because luaL_register then sets
> the corresponding field in package.loaded.

This is also the case if the luaopen_xxx loader do nothing, require is
filling package.loaded (with the return value of the loader or true).

On that point, I just noticed a mistake in the manual. The manual states:

Lua manual <http://www.lua.org/manual/5.1/manual.html#pdf-require>
> If the loader returns any value, require assigns the returned value to
> package.loaded[modname]. If the loader returns no value and has not
> assigned any value to package.loaded[modname], then require assigns
> true to this entry.

If the loader explicitly returns nil (which afaik is a value), as
opposed to no value, that value is not assigned to package.loaded.
Here is an example (showing the problematic behaviour in module b):

-- a.lua
package.loaded[...] = nil
-- b.lua
return nil
-- program.lua
require 'a'
require 'b'
assert(package.loaded.a==nil)
assert(package.loaded.b==true)

Given that when the module assigns nil to package.loaded[modname],
'require' is letting the nil value there, I think 'require' should be
fixed to also take into account a nil return value from the module
loader. Otherwise the manual could (and should in the meantime) be
changed to something like this:

> If the loader returns a non-nil value, require assigns the returned
> value to package.loaded[modname]. If the loader returns nil or no
> value and has not assigned any value to package.loaded[modname],
> then require assigns true to this entry.