lua-users home
lua-l archive

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

I haven't looked at the 5.0 code yet, maybe this is in there but I
haven't heard anyone mention it. It would be very nice if Lua would
accept callback functions for performing file I/O, printing strings,
and allocating memory. It was very easy to modify the 4.0 code, but
ideally I'd like to embed Lua without any modification, which makes
upgrades and maintenance easier.

As an example, the 4.x releases used fputs in lbaselib.c. Instead
have something like:

   typedef void lua_WriteStringFunc(const char* msg);
   void lua_setWriteStringFunc(lua_WriteStringFunc wsf);

If no function is specified, default to using fputs(). Same for the
file I/O and memory routines. A good example implementation is zlib
or libpng. This would get rid of all of the modifications that I
make to the Lua code, which currently include:

lua_dofile() - completely replaced, use my own file I/O routines

luaD_error() - completely replaced, use my own logging/reporting

lmem.c - replaced all memory macros with my own allocators

lstate.h - #define LUA_USERSTATE void* myData;

luaB_print() - replace all fputs() with my own string printer

passresults() - removed the static modifier so my handlers for
                the above could call it.

In addition, I ignore some of the Lua functions and use my own
replacements entirely.