Lua beginner's document: A Brief Introduction to Lua

Source: Internet
Author: User

LuaIs a smallScriptLanguage, which is designed to embed in an application and provide flexible scalability and customization for the application.LuaThe most famous application is in Blizzard's online game WOW.

Lua scriptIt can be easily called by C/C ++ code, or call C/C ++ functions in turn, which makes Lua widely used in applications. Not only as an extension script, but also as a common configuration file, instead of XML, Ini and other file formats, and easier to understand and maintain.

LuaCompiled by standard C, the code is concise and elegant, and can be compiled and run on almost all operating systems and platforms. A complete Lua interpreter is only 200 k. Currently, Lua is the fastest in all script engines. All of this determinesLuaIs embeddedScript.

LuaThere is a simultaneous JIT project that provides real-time compilation on a specific platform, which will bring Lua better performance. Please visit http://luajit.luaforge.net/to learn about this project.

Unlike scripts such as Python, Lua does not provide a powerful library, which is determined by its positioning. Therefore, Lua is not suitable for developing independent applications. However, Lua still has basic functions such as mathematical operations and string processing.

Lua is currently in the latest version 5.1.

Lua has only one data type. table. is actually a hash table. It uses this to simulate arrays, linked lists, and so on. In terms of syntax, Lua supports the following forms:

 

This makes Lua similar to struct of C, which makes it easy to design parameters of C functions. You can use a table to input complicated parameters.

2. Introduction to data exchange

The Lua and C Programs exchange data through a stack: struct lua_State

The stack sequence number can be counted from the top and bottom of the stack, from the bottom of the stack, then the bottom of the stack is 1, increasing toward the top of the stack. From the top of the stack, the top of the stack is-1, and the direction toward the bottom of the stack is decreasing. It is generally counted from the top of the stack. The default stack size is 20. You can use lua_checkstack to modify the stack. Use lua_gettop to obtain the number of elements in the stack. It does not mean that there is an integer element at the top of the stack. Instead, it calculates the positive index of the top element of the stack in the stack, which is equivalent to the number of elements.

The stack used by Lua to call the C function is temporary and destroyed after the call is complete.

How to obtain parameters from the Lua script from the stack

If you know the name of a global variable in the Lua script, you can use void lua_getglobal (lua_State * L, const char * name ). This function places the value of the Lua variable referred to by name on the top of the stack.

To obtain the parameters used by Lua to call a function in Function C:

First, use lua_gettop to check the number of parameters.

Use the lua_is... class function to detect the parameter type and handle errors.

Use the lua_to... class function to convert the parameter to number or string. (For Lua, only these two simple types are supported)

Lua_ Tonumber returns double

Lua_tostring returns char *

Use lua_remove to delete elements from the stack

Continue to obtain the next element. Because lua_remove is called every time, the index used for each call of lua_tonumber is fixed to-1, that is, the top of the stack.

If lua_istable is set up, it indicates that the top of the stack is a table. Note that the table cannot be retrieved, and only the elements in the table can be retrieved one by one.

First, press the element name to the top of the stack: lua_pushstring (L, "I"); then you can call it with lua_gettable, and the value will be placed on the top of the stack. At the same time, the name of the pressed element is displayed. In the above method, we can obtain this value. Remember to use lua_remove. If one element of a table is also a table, repeat it. When all the elements of a table are finished, remember that the table is still in the stack and delete it with lua_remove.

If you want to obtain an array (the so-called array is actually the table where the key is a numerical sequence starting from 1 and the value type is the same), you can use lua_next to traverse this array:

First, press lua_pushnil into a null value, and then

 

How to return data from C to Lua script

Use the lua_push... class function to push data into the stack and return n; To tell Lua to return several values. Lua supports multiple return values, such as x, y = Test (). Lua extracts data from the stack based on n.

If you want to return a table:

 

If you want to return an array, use the following code: (pay attention to the trick comment. I am waiting for the official explanation. After verification, this problem only occurs when the dll method is called in windows. Normal WinCE)

 

The resulting array can be traversed in Lua as follows:

 

Or

 

Only the array can do this. The Record consisting of name and value cannot, and table. getn is only valid for the array.

Because of the high similarity of the above Code, it is easy to automatically generate the code. For example, according to a struct definition in C:

 

The following code is automatically generated:

 

LuaToData is similar.

If you use the object-oriented method to encapsulate the flag, it is more convenient to convert the DataToLua into a flag class method.

3. Examples of mutual calls between C and Lua scripts

First, the main program of C initializes the Lua Script Engine and registers some functions for the script to call:

 

ScriptThe Code is as follows:

 

Summary:LuaBeginner's document: AboutLuaAfter a brief introduction of the language, I hope this article will help you!

Related Article

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.