Lua1.0 Environment Preparation

Source: Internet
Author: User

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.*/intlua_pushcfunction (lua_CFunction fn){ if((top-stack) >= MAXSTACK-1) {  lua_error ("stack overflow");  return1; } tag(top) = T_CFUNCTION; fvalue(top++) = fn; return0;}

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.*/intlua_storeglobal (char*name){ intn = lua_findsymbol (name); if(n < 0) return1; if(tag(top-1) == T_MARK) return 1; s_object(n) = *(--top); return0;}

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.*/intlua_findsymbol (char*s){ inti; for(i=0; i<lua_ntable; i++)  if (streq(s,s_name(i)))   returni; 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 staticSymbol 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 typedefstruct{ 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:

?
1 lua_dofile (argv[1]);

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.