lua-users home
lua-l archive

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


Luiz Henrique de Figueiredo wrote:
>> could someone please make it easier to open an arbitrarily large set
>> of libraries right after luaL_openlibs(L)
> 
> Perhaps I'm missing something but the usual solution for that is to copy
> linit.c and edit it at will: linit.c is meant to be modified and it's a
> simple matter of adding or deleting enties in the lualibs list.

This is exactly what I do for my boot code, where lots of things are
different (like the malloc() interface).

But for my system code (ppc LynxOS, statically linked) I created an
interface that has the same name Norman used: lua_interp.h/.c. The
difference is that I don't do any argc/argv processing there. Here's the
interface I chose:

  extern lua_interp_t* lua_interp_create(
    const lua_interp_lib_t libs[],
    lua_interp_get_t       get,
    void*                  get_cookie,
    lua_interp_put_t       put,
    void*                  put_cookie);

  extern void lua_interp_destroy(lua_interp_t** lp_ptr);

  extern lua_interp_t* lua_interp_set_prompt(
    lua_interp_t* lp,
    const char*   prompt,
    const char*   continuation_prompt);

  extern int lua_interp_dostring(
    lua_interp_t* lp,
    const char *s);

  extern int lua_interp_run(lua_interp_t* lp);

Note that I pass a list of libraries to lua_interp_create(). The
'get'/'put' are for reading/writing a line from/to the user. This allows
me to use readline when available but to use other methods when
necessary. Our application accepts console input from a TCP connection,
so in that case the 'get' function differs.

For test programs on my target (programs other than the main application
mentioned above), I have a simple main() that has a list of libraries
(name-function pairs), a usage printer, and an argument parser. main()
calls lua_interp_create(libs, readline_get, NULL, fputs, stdout). If a
command line option indicates code to run, main() calls
lua_interp_dostring() on each appropriate argument, otherwise it calls
lua_interp_run().

Like Norman I think it would be nice to encapsulate that behavior so
that main() could be simpler. But I can also see multiple ways of doing
so. I've been very happy with the way my division worked out, but that's
because I created the interface to contain exactly those things which I
need to vary across instances of the interpreter. Others would probably
like to see readline used by default and perhaps might like to have
lua_interp_create() parse Lua-specific command line arguments before
program-specific ones.

Doug

-- 
Innovative Concepts, Inc. www.innocon.com 703-893-2007 x220