Lua Study Notes (continuously updated)

Source: Internet
Author: User

Lua notes:

* This variable exists only when one variable is not equal to nil.

* Comment: single line comment: -- Multi-line comment: -- [[--]

* Except false and nil are false in the conditions of the control structure, all other values are true. Therefore, Lua thinks that both 0 and null strings are true.

* Numberss indicates a real number, and Lua does not contain integers.
* Lua compares numbers according to the size of traditional numbers and compares strings in alphabetical order, but the alphabetic order depends on the local environment.
* Logical operators consider false and nil as false. Pay special attention to nil.

* A and B -- if A is false, A is returned; otherwise, B is returned.
A or B -- if A is true, A is returned; otherwise, B is returned.

* When a string uses an arithmetic operator, the string is converted to a number.

*~ = Not equal

*. It is a string connector in Lua. When writing a number after a number, spaces must be added to prevent interpretation errors.
* Note: To assign values to multiple variables, you must assign values to each variable in sequence.

The three expressions are calculated only once before the start of the loop.
For I = 1, F (x) Do
Print (I)

* Functions can be stored in variables and can be used as function parameters or return values.

* Userdata can store c Data in Lua variables.


* Lua has the following features: 1. The variable name has no type, and the value has a type. The variable name can be bound to any type of value at runtime; 2. The language only provides a unique data structure, called a table, which is similar to a key.
-Value is associated with an array and any type of value can be used as the key and value. Provides consistent and expressive table construction syntax, making Lua suitable for describing complex data. 3. The function is
Supports anonymous functions and regular tail recursion (proper tail recursion). 4. Supports lexical
Scoping and closure; 5. Provides thread-type and structured coroutine mechanisms to facilitate collaborative multitasking;
6. program text in the string format can be compiled during runtime and loaded to the Virtual Machine for execution. 7. The dynamic metadata mechanism is provided through the metamethod.
Meta-mechanic), which allows you to change or expand the internal semantics of the syntax facility as needed when running the program. 8. It is convenient to use the table and dynamic meta mechanism to implement prototype-based
(Prototype-based) object-oriented model; 9. A complete module mechanism has been provided since version 5.1 to better support the development of large-scale applications;

* Closure has two features:
1. As a reference to a function variable-when a function is returned, it is activated.
2. A closure is a stack that does not release resources when a function returns.

* C calls the Lua script
Void load (char * filename, int * width, int * Height ){
Lua_state * l = lua_open ();
Luaopen_base (L );
Luaopen_io (L );
Luaopen_string (L );
Luaopen_math (L );
If (lual_loadfile (L, filename) | lua_pcall (L, 0, 0, 0 ))
Error (L, "cannot run configuration file: % s ",
Lua_tostring (L,-1 ));
Lua_getglobal (L, "width ");
Lua_getglobal (L, "height ");
If (! Lua_isnumber (L,-2 ))
Error (L, "'width' shocould be a number/N ");
If (! Lua_isnumber (L,-1 ))
Programing in Lua 189
Copyright? 2005, translation team,
Error (L, "'height' shocould be a number/N ");
* Width = (INT) lua_tonumber (L,-2 );
* Height = (INT) lua_tonumber (L,-1 );
Lua_close (L );

* Lua calls the c Function
When Lua calls the C function, the first parameter is always at the Index = 1 of the private stack. Even when a C function calls the Lua code (The Lua code calls the same C function or other C functions), each C function has its own independent private stack, and the first parameter is at Index = 1.

* There are three statuses of collaboration: Pending, running, and stopped. When we create a collaboration program, the initial state is suspended.

* The package Library provides Lua with a simple method for loading and creating modules. It consists of the require, module, and package tables.
1. Module (name [,...])
Function: Create a module.
When package. Loaded [name] exists, the table in the package is used as the module;
When a table specified by name exists in the global table, this table serves as the module;
When no table name is saved in the previous two cases, a new table is created and used as the value of the global name and the package is created. loaded [name], and set T. _ name is name, T. _ m is module, T. _ package is the full name of the package (Module name-component. b. c). Finally, set the module T as the new environment table and package of the current function. the new value of loaded [name] (that is, the old environment table cannot be accessed, except for the package. seeall parameter) to be used by require

The optional parameter after module (name) is the function that receives the module name, such as package. seeall

* Require (modname)
Function: load the specified module.
This function first checks whether a modname exists in the package. Loaded table. If a modname exists, the value is directly returned. If no modname exists, the modname is loaded using the loader defined by the handler.
Find the loader sequence:
(1) check whether the package. preload table has a modname. If yes, load
(2) load by Lua loader and by searching the path stored in package. Path. If yes, load
(3) load data through C loader and find the path stored in package. cpath. If yes, load data.
(4) load through all-in-one loader:
Find modname. dll and find the luaopen _ <XXXX>
Where XXXX is the character after the block name-replaced with _. Character: For example: A. v1-b.c when the function name is luaopen_ B _c
When require does not search for a Lua or C library, it will call all-in-one loader. This loader uses the C path as the directory of the block to be loaded,
When a suitable loader is found, require loads the modules. When the loader returns a value, it is stored in the package. Loaded [modname] table. Finally, the package. Loaded [modname] Table is returned.
When loading fails, require triggers an error

* Add, sub, Mul, Div, Mod, pow, UNM, Concat, Len, EQ, LT, le, tostring, GC, index, newindex, call...
In the functions provided by Lua itself, you are not allowed to change the retriable of any other types of values except the table type value, unless you use C extension or other libraries. setretriable and getretriable
Is the only method to operate the table Type of retriable.

Retriable and object-oriented
Lua is a process-oriented language, but it can simulate the appearance of an object through the retriable. the key lies in the _ index field. it provides the index value entry for the table. this is similar to rewriting the indexer in C #. When the table wants to index A value such as table [Key], Lua will first look for the key value in the table itself, if the table does not have a retriable with the _ index attribute, Lua searches for the table based on the function logic defined by _ index. if you think about it, isn't this an implementation method for the core idea inheritance in Object-Oriented Systems. there are many ways to implement object-oriented in Lua, But no matter which one can do without _ index.

* The userdata type is used to store any C data in the Lua variable.

* Luavs. bat is not applicable to vs2008.
Read install and find the following:
If all you want is to build the Lua interpreter, you may put all. c files in a single project, Login T for luac. C and print. C.

Then, it is very easy to create a new empty project and import all the C files except luac. C and print. c Under SRC (meaning the makefile in does not need to be imported =. =)
Then compile all the files directly.



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: 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.