Reprint Source: http://my.oschina.net/xhan/blog/306719
Start with the main function of LUA.C and see what happens during the execution of the code.
12345 |
if (argc < 2) { puts ( "usage: lua filename [functionnames]" ); return ; } |
Lua1.0 must have at least one parameter to execute, or exit directly.
Then there are three lua_register.
?
123 |
lua_register ( "callfunc" , callfunc); lua_register ( "execstr" , execstr); lua_register ( "test" , test); |
To see what Lua_register is? You can see that Lua_register is a macro, defined in LUA.H:
?
1 |
#define lua_register(n,f) (lua_pushcfunction(f), lua_storeglobal(n)) |
And then look at Lua_pushcfunction, and Lua_storeglobal,
In the opcode.c file
?
12345678910111213 |
/*
** Push an object (tag=cfunction) to stack. Return 0 on success or 1 on error.
*/
int
lua_pushcfunction (lua_CFunction fn)
{
if
((top-stack) >= MAXSTACK-1)
{
lua_error (
"stack overflow"
);
return
1;
}
tag(top) = T_CFUNCTION; fvalue(top++) = fn;
return
0;
}
|
The lua_pushcfunction parameter is a function pointer, and at the end of the call, the Object type at the top of the stack is set to T_cfunction, and the value of the function pointer passed to the actual parameter fn. The stack here is a link between Lua and C calling, as a method implemented in C code that is converted to an Object of type t_cfunction.
OPCODE.C file
?
123456789101112 |
/*
** Store top of the stack at a global variable array field.
** Return 1 on error, 0 on success.
*/
int
lua_storeglobal (
char
*name)
{
int
n = lua_findsymbol (name);
if
(n < 0)
return
1;
if
(tag(top-1) == T_MARK)
return 1;
s_object(n) = *(--top);
return
0;
}
|
TABLE.C file
?
1234567891011121314151617181920212223242526 |
/*
** Given a name, search it at symbol table and return its index. If not
** found, allocate at end of table, checking oveflow and return its index.
** On error, return -1.
*/
int
lua_findsymbol (
char
*s)
{
int
i;
for
(i=0; i<lua_ntable; i++)
if (streq(s,s_name(i)))
return
i;
if
(lua_ntable >= MAXSYMBOL-1)
{
lua_error (
"symbol table overflow"
);
return
-1;
}
s_name(lua_ntable) = strdup(s);
if
(s_name(lua_ntable) == NULL)
{
lua_error (
"not enough memory"
);
return
-1;
}
s_tag(lua_ntable++) = T_NIL;
return
(lua_ntable-1);
}
|
The role of Lua_storeglobal is to store global variables in the symbol table. The argument is a string, which is the name of the global variable, Lua_findsymbol finds the string specified by the parameter in the symbol table, returns its index in the symbol table if found, otherwise, creates a new symbol at the end of the table, returning its index in the table.
Symbol table: In the table.c file
?
1234567 |
static
Symbol tablebuffer[MAXSYMBOL] = {
{
"type"
,{T_CFUNCTION,{lua_type}}},
{
"tonumber"
,{T_CFUNCTION,{lua_obj2number}}},
{
"next"
,{T_CFUNCTION,{lua_next}}},
{
"nextvar"
,{T_CFUNCTION,{lua_nextvar}}},
{
"print"
,{T_CFUNCTION,{lua_print}}}
};
|
Let's look at the definition of the symbol:
?
12345 |
typedef struct { char *name; Object object; } Symbol; |
It includes the name of a symbol and a corresponding Object type.
Lua_storeglobal after Lua_findsymbol returns, use the returned index to locate the corresponding symbol and set its obejct field. To this, the method registers Lua_register ("Callfunc", Callfunc); The call is complete. So, Lua_register ("Callfunc", Callfunc); is to register a function named "Callfunc" in the global symbol table, and when called in a Lua script, the callfunc is the C function. The other functions are registered with the same.
Registration of the function library:
?
123 |
iolib_open (); strlib_open (); mathlib_open (); |
As you can see, the registration of library functions is also using the Lua_register macro mentioned above. After the call is complete, all of the system's own functions have been registered.
Here, the environment is ready to start working:
?
The following code handles the arguments that start with the second argument of the command line as an parameterless function call.
Lua1.0 Environment Preparation