|
Start the interpreter.
Interpreter entry file lua.c, find the main C language in the document.
int main (int argc, char * argv []) {
struct Options opt;
int status;
opt.toclose = 0;
getstacksize (argc, argv, & opt); / * handle option `-s' * /
L = lua_open (opt.stacksize); / * create state * /
userinit (); / * open libraries * /
register_getargs (argv); / * create `getargs' function * /
status = handle_argv (argv + 1, & opt);
if (opt.toclose)
lua_close (L);
return status;
}
Setting structure struct Options stack size stacksize.
Struct Options structure defined as follows:
/ *
** Global options
* /
struct Options {
int toclose;
int stacksize;
};
In the getstacksize to stacksize assignment. You need to pass command line "-s" parameter.
lua_open New lua_State L.
userinit registry.
register_getargs Sign up for command line input parameter function getargs.
handle_argv program execution.
toclose If 1, then, before the program exits to call lua
Return status of program execution.
Wherein each facie call:
static void getstacksize (int argc, char * argv [], struct Options * opt) {
if (argc> = 2 && argv [1] [0] == '-' && argv [1] [1] == 's') {
int stacksize = atoi (& argv [1] [2]);
if (stacksize <= 0) {
fprintf (stderr, "lua: invalid stack size ( '% .20s') \ n", & argv [1] [2]);
exit (EXIT_FAILURE);
}
opt-> stacksize = stacksize;
}
else
opt-> stacksize = 0; / * no stack size * /
}
If the command line parameter "-s", take it back as a value struct Options of stacksize.
static void userinit (void) {
lua_baselibopen (L);
lua_iolibopen (L);
lua_strlibopen (L);
lua_mathlibopen (L);
lua_dblibopen (L);
/ * Add your libraries here * /
}
This is a call to all kinds of library registration function.
Next is registered getargs function
static void register_getargs (char * argv []) {
lua_pushuserdata (L, argv);
lua_pushcclosure (L, l_getargs, 1);
lua_setglobal (L, "getargs");
}
The command line parameters push, push the function l_getargs set to Lua variables getargs.
Then call getargs in Lua code will call a C function l_getargs this.
Implementation l_getargs this time a function call is as follows:
static void getargs (char * argv []) {
int i;
lua_newtable (L);
for (i = 0; argv [i]; i ++) {
/ * Arg [i] = argv [i] * /
lua_pushnumber (L, i);
lua_pushstring (L, argv [i]);
lua_settable (L, -3);
}
/ * Arg.n = maximum index in table `arg '* /
lua_pushstring (L, "n");
lua_pushnumber (L, i-1);
lua_settable (L, -3);
}
static int l_getargs (lua_State * l) {
char ** argv = (char **) lua_touserdata (l, -1);
getargs (argv);
return 1;
}
Get a userdata from the stack, which made all the command line parameters getargs.
Command-line arguments are made in the presence of a Lua table.
handle_argv parse the command line arguments, the main process execution.
The main part of the program is a big loop judge to resolve the various parameters.
manual_input used for interactive processing modes, other files and string handling performed by the ldo.
manual_input program is a loop, to perform user input by calling ldo (lua_dostring, buffer).
ldo follows:
static int ldo (int (* f) (lua_State * l, const char *), const char * name) {
int res;
handler h = lreset ();
int top = lua_gettop (L);
res = f (L, name); / * dostring | dofile * /
lua_settop (L, top); / * remove eventual results * /
signal (SIGINT, h); / * restore old action * /
/ * Lua gives no message in such cases, so lua.c provides one * /
if (res == LUA_ERRMEM) {
fprintf (stderr, "lua: memory allocation error \ n");
}
else if (res == LUA_ERRERR)
fprintf (stderr, "lua: error in error message \ n");
return res;
}
The first parameter is a function pointer can be passed lua_dofile, lua_dostring respectively correspond to the input file and string input.
The second parameter depending on the first function pointer and can mean different things: a file name or a string.
By calling lreset () sets the interrupt signal handler.
At the end of the call signal (SIGINT, h) to restore the interrupt signal processing.
Interpreter inlet stop here.
----------------------------------------
So far the problem:
> Lua_dofile, lua_dostring
---------------------------------------- |
|
|
|