[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: module with c api
- From: Thijs Schreijer <thijs@...>
- Date: Fri, 23 Aug 2013 12:50:47 +0000
> > Try loaders, see [1]. Create a loader that checks your database for the
> > requested module and fetches it.
> > [1] http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders
>
> Do I understand it correct, that require uses also a table for the
> information of thre require data? So in my case
> the require call must check the data first in my database eg
> local mymodule = require "mymodule"
Require uses multiple tables; package.preload, package.loaded, package.loaders
They are all listed in the ref manual with their uses.
>
> In this case in my database table a row exists with the name "mymodule",
> so can I cange the require, that it load
> it from my database?
No need to do that of you create a loader function
>
> Thanks
>
> Phil
There are multiple ways to achieve what you want, imo the loader is the least intrusive. You could stuff all your modules in 'package.loaded' table upon starting a new lua state, but that would load all modules always. If you create a loader, there is no need to modify 'require' because it already uses the loaders. And the modules will only be loaded if requested by a call to 'require'.
>From the manual;
> Each entry in this table is a searcher function. When looking for a
> module, require calls each of these searchers in ascending order,
> with the module name (the argument given to require) as its sole
> parameter. The function can return another function (the module
> loader) or a string explaining why it did not find that module (or
> nil if it has nothing to say). Lua initializes this table with four
> functions.
So create a loader function, insert it in 'package.loaders'. Whenever your loader function is called it can check the existence of a module by that name in your database.
Insert the loader function at position 1, or 2 if you always want to check your database and return what is there, or insert it last if you want local files to overrule what is in the database.
Thijs