The close contact between Lua and C

Source: Internet
Author: User

Describes the close contact between Lua and C, and relies on a virtual stack. LUA uses this virtual stack to transmit the value between C. Each element on the stack is a LUA value (nil,number,string ... )。 When Lua calls the C function, the function gets a new stack, independent of the stack of the C function itself, and also independent of Lua's own stack. It contains all the parameters that Lua will pass to C, and then the C function will return the returned results to the caller in the stack. For the query operation of the stack, if you follow the rules of the stack, you can only get the elements at the top of the stack. But there are some differences with conventional stacks. is to use a single pointer to point to any element on the stack. A positive index (1...N) points from the bottom to the top of the stack, 1 is the first element to stack, n is the element at the top of the stack, the index of negative numbers ( -1...-n) points from the stack top to the bottom of the stack, -1 is the top element of the stack, and-n is the first element in the stack. These two indexing methods make it easy to get the elements in the stack. The basic operation of the interaction between Lua and C is a virtual stack, the virtual stack in Lua's C API for Lua_state, the following code shows from the creation of stacks, elements into the stack, according to the index to get the value of the elements in the stack, which is the most basic operation of Lua_state.
Lua_state *l = Lual_newstate ();//Create a new stacklua_pushstring (L,"muzixiaoxin");//Press a string into the stackLua_pushnumber (L,875);//Push an integral type into the stack//now there are two elements in the stack, the bottom of the stack is the string "Muzixiaoxin", the top of the stack is an integral type 875//The index of "Muzixiaoxin" is 1, or-2//the 855 index is 2, or-1.if(Lua_isstring (L,1)){//determine if the element at the bottom of the stack is a stringprintf"%s\n", Lua_tostring (L,1));//if it's a string, it's converted to string output .}if(Lua_isnumber (L,-1)){//determine if the top element of the stack is number typeprintf"%d", Lua_tonumber (L,2));//if it is, convert to number type output}lua_close (L); //Remember to release it when you don't need it.

For more related functions please refer to http://cloudwu.github.io/lua53doc/manual.html

C calling Lua to call Lua I see less, usually using LUA virtual machines to run scripts directly. Some have used Lua as a configuration file for C. As an example, create a new Lua file Test.lua
" muzixiaoxin "  1003
c need to load this file through the LUA C API, then execute, the result of execution exists in a stack, go to the stack to get the value of the variable. Look at the C code below:
Lua_state *l =lual_newstate ();intErr = Lual_loadfile (L,"Test.lua");//load the Lua file into a code block and load it without runningif(err) {return;} Err= Lua_pcall (L,0,0,0);//run the loaded code blockif(err) {return;} Lua_getglobal (L,"name");//Press the value of the global variable name into the top of the stackprintf"%s\n", Lua_tostring (L,-1));//Remove the top of the stack to print the result: MuzixiaoxinLua_close (L);//Remember to release it when you don't need it.
Lua calls C method called C, which is a bit cumbersome to write a fixed-format method for Lua to invoke. Let's simply write a summation of the C method:
// methods for calculating sums Static int sum (intint  b) {    return a + b;}

This method is to ask for two integral type and. To get Lua to use this method, we need to register this method with Lua's state machine, but the method that registers to the LUA state machine requires a fixed parameter and a fixed return value, and if the parameter is a LUA virtual stack, the virtual stack holds the parameters that Lua passes over. The return value of the LUA call is also returned to LUA through this virtual stack, and the final return value requires an int value, with the number returned to the LUA variable. Let's look at a good way to write:

//methods called by LuaStatic intLsum (lua_state*L) {    intA = (int) Lua_tonumber (L,-1);//one of the parameters that Lua invokes    intB = (int) Lua_tonumber (L,-2);//one of the parameters that Lua invokesLua_pushnumber (L, sum (A, b));//put the calculated add over pressure stack    return 1;//returns the number of returned values}

Next step, Lsum. This method is registered to the LUA state machine:

Lua_state *l = lual_newstate (); Lual_openlibs (L); // Open all the standard libraries in l so that you can use the Print method  "sum", lsum); // Register the C function lsum as a global variable sum of LUA int " Test.lua " // load the Lua file into a code block and run if (Err) {    return;} Lua_close (L);

The contents of Test.lua are:

Print ("" .. SUM (1,2))
The final output: To summarize, you have to implement Lua's method of calling C through an intermediate function (like lsum) to manipulate the LUA virtual stack. For more Lua C APIs please refer to http://cloudwu.github.io/lua53doc/manual.html

The close contact between Lua and C

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.