lua-users home
lua-l archive

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


>From: Stephen Dekorte <steve@egroups.net>
>I guess I wasn't clear - I need to write out the precompiled chunk to
>a file from within lua. 
>
>In other words, with luac I can do this:
>
>luac -o MyFile.luac -c MyFile.lua
>
>and produce a MyFile.luac file that contains a precompiled version of
>MyFile.lua. But could I do the same thing in lua without using a
>system() call?

Not officially.
But you may want to try the following function, adapted from luac.c.

int luac_dofile(FILE* D, char* filename)
{
 FILE* f= (filename==NULL) ? stdin : fopen(filename,"r");
 ZIO z;
 char source[255+2];
 if (f==NULL) return 2;
 luaL_filesource(source,filename,sizeof(source));
 luaZ_Fopen(&z,f,source);
 luaU_dumpchunk(luaY_parser(&z),D);
 if (f!=stdin) fclose(f);
 return 0;
}

I think if there's a parse error, then this code will exit the host program,
which is ok for luac but not in general.
For robust code, see lua_dofile in ldo.c.

Apart from this, the modules used by luac are written so that they can be used
by themselves -- they only need a C API such as the one above, which prepares
things and calls luaU_dumpchunk.
So, if I can come up with a clean solution for the error problem, I will add
such an API in the luac modules.

Similar code can be written for loading binary files; see luac.c.
--lhf