lua-users home
lua-l archive

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


2012/11/10 Drake Wilson <drake@dasyatidae.net>:
> Quoth Jerome Vuarand <jerome.vuarand@gmail.com>, on 2012-11-10 03:51:19 +0000:
>> Here I disagree with Drake Wilson. I think it's easier to directly
>> call your c_loader function:
>>
>>     package.preload[k:lower()] = c_loader(k:upper())
>>     package.preload[k:upper()] = package.preload[k:lower()]
>
> I respect your opinion, but I think this is not a good idea because it
> means that all scripts enumerated are immediately loaded into the Lua
> heap as chunk functions even if they are not executed yet.
>
> That is
> why the c_loader call should be deferred until the preload function is
> actually called.  (Renaming the c_loader function to something like
> load_script makes this easier to see.  "preload" is actually kind of
> an unfortnuate name, now that I think of it.)

I think that is precisely the point of package.preload, that is to
have modules ready to start, with as much errors already detected as
possible.

If you want to delay the load of the chunk into memory until the call
to require, you have another mechanism available which is the
package.loaders (5.1) / package.searchers (5.2) table. Simply call
table.insert(package.loaders, 1, c_loader) and you will have your
delayed load mechanism. You just need a slight adjustmet to c_loader
so that it deals with the uppercase/lowercase trick and assign to
package.loaded directly instead of letting require do it. And it
should be renamed c_searcher to follow the manual teminology.