Lua Introduction
1993
At the Pontifical Catholic University of Rio de Janeiro in
Brazil) was born with a programming language. The inventor was the school's three researchers who gave the language a romantic name-Lua, representing the beautiful moon in Portuguese. It turns out that she didn't
The Lua language has grown into a concise, elegant, and fun language as its name implies.
From the very beginning, Lua was used as an easy way to embed data.
(Other applications) and a scalable lightweight scripting language, so she has been following simple, compact, portable, and fast principles, the official implementation fully adopts ANSI
C Programming, can be embedded into the host Program in the form of a C library. Each Lua version maintains the open source code tradition, but the license protocols used by different versions are different, from version 5.0 (the latest version is 5.1)
At first, she adopted the famous mit license agreement. Due to the above features, Lua has been widely applied in various fields such as game development, Robot Control, distributed application, image processing, bioinformatics, etc.
More widely used. Many famous games, such as escape from Monkey Island and world
Warcraft and westward journey all use Lua to work with the engine to complete data descriptions, configuration management, logic control, and other tasks.
As a process-based Dynamic Language, Lua has
Features: 1. The variable name has no type and the value has a type. The variable name can be bound to any type at runtime. 2. The language provides only one data structure, A table 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.
(Dynamic
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;
Lua
Syntax is similar to Pascal and modula, but more concise. All syntax generation rules (ebnf) are only 60. Programmers familiar with C and Pascal generally only need to spend half an hour
Master it completely. In terms of semantics, Lua is very similar to scheme. They fully share the above 1, 3, 4, and 6 features,
The continuation and coroutine of scheme are basically the same, but the degree of freedom is higher. Most notably, both languages provide only one data structure: Lua tables and Scheme
). Because of this, some people even call Lua "only use table Scheme ".
Libraries and tools
Compared with Java, Python, and Perl, Lua may not have many open-source tools and libraries, but many of them are excellent. The following resources can be found in.
I. Kepler
Kepler
Is a simple and lightweight web development platform (but this does not mean that you can only use it to develop simple applications). It supports writing web programs with Lua, so it is easy to learn and use, and can be easily applied in
In systems with limited resources. Because ansi c and Lua are used for development, it can be transplanted to any platform that supports ansi c.
Kepler is expanded by multiple Lua
Library, including cgilua, luasocket, luafilesystem, copas, luasql, lualdap, luaexpat,
Luaxmlrpc, luasoap, luazip, and Xavante can be roughly divided into the core library and Function Support library. Among them, the core is cgilua and
Luasocket, which is responsible for TCP/UDP
Sockets operations. The former allows you to create dynamic pages and process input data on web forms. Kepler uses the cgilua starter (launcher) to enable Web Servers
Can execute and communicate with cgilua and web programs. The current version is applicable to CGI, FastCGI, Apache, IIS, tomcat, and Zope.
Cgilua starters, so web programs developed with Lua can be freely migrated on these types of servers, as long as the corresponding cgilua starters are installed at the same time.
Luafilesystem
Is a supplement to the file read/write function in the standard Lua library. It provides a portable method to access the system's directory structure and file attributes. Copas is a coroutine-based service scheduler.
Xavante is a web server developed with Lua that supports HTTP 1.1. It directly supports cgilua without a starter.
Other components provide functions such as SQL database access, XML parsing, LDAP, soap, XMLRPC, and zip file operations. If you only need some of these functions, you can extract related components (and the components on which they depend) for use.
Ii. wxlua
Gui
It is a field in which developers spend a lot of effort. Therefore, simplifying GUI programming has always been the direction of the majority of programmers. With the rise of scripting languages, dynamic, flexible, and easy-to-use scripting languages are introduced
GUI development is a very valuable attempt. Because the complex GUI layout requires a large amount of description information, compared with other scripts, Lua language is suitable for programming and data description in building a GUI
Has a unique advantage.
WxWidgets is a famous cross-platform C ++
In the GUI library, wxlua builds a bridge between Lua and wxWidgets. Through its Lua code, almost all features of wxWidgets can be called. Wxlua
The wxWidgets class system is mapped to the Lua (prototype-based) object model, which enables programmers to develop wxlua programs in an object-based or object-oriented style. A Lua script
Writing, running, testing, and modification can be very fast, which undoubtedly greatly improves the development efficiency of GUI programs. Therefore, wxlua is very suitable for the construction of rapid prototypes. In addition, Lua itself and
The good portability of wxWidgets enables the Lua GUI program to run smoothly on many platforms.
Iii. Pluto
Although
The tables in can be saved to files through the table constructor in the form of Lua code to achieve persistence. However, when there is a complex reference relationship between data, in special cases such as cyclic reference and shared reference
It becomes quite difficult and cumbersome. Pluto persistence library can solve this problem for users. With its help, programmers can save any complex table data to binary files in special formats for future use.
Recovery, the database automatically handles situations such as cyclic references.
In addition to tables, Pluto also supports persistence of functions (closure) and threads.
. As we all know, a basic action in program debugging is to reproduce bugs. However, in many cases, the conditions for generating bugs are very complex. Relying on many factors, it is difficult for developers to accurately build completely consistent operations.
Environment. With Pluto's persistence capabilities for functions and threads, we can save the complete runtime environment of the program when a bug occurs, so that we can easily reproduce the bug in the future. Another
An important application is the preservation of game progress. The running status of the Lua script that implements game logic can be written to the file at any time for future recovery, which makes it very easy to save the game at any time point.
Iv. luacom
Luacom
Is an extension library that allows Lua to interact with component objects that comply with COM specifications (automation objects. The interaction involves two aspects: first, allow the Lua program to use the com
Image. Luacom supports dynamic instantiation of COM objects registered in the system registry and dynamic access to running objects. With the help of luacom, calling the COM Object method is like calling
Like the Lua function, the access attribute is similar to the field in the access table. It is also responsible for automatic conversion of the automation data type and Lua data type. With these features, the Lua program operates
COM objects become much easier, and Lua's inherent dynamic nature makes it a very flexible component assembly language.
Another aspect of interaction is support for the use of Lua
To implement (automate) component objects and provide them to external customers. Luacom supports both in-process and out-of-process components. It provides some auxiliary functions to handle registration and Object Instantiation.
Related work. Because luacom is actually constructing a COM Object Based on the Lua table, we can do something very interesting: In the userdata data type (representative is not
With the support of the Lua World Data Structure and dynamic meta mechanism, Lua can access a variety of external data through tables, including C ++ objects, C structures, or CORBA objects; luacom
It is easy to package tables that represent the data into a COM Object for external use, so that those old applications and libraries can enter the com world without too much effort.
V. tolua
Straight
It is common to use C to implement some functions and then import the corresponding functions to Lua. However, although Lua provides APIs for interaction with C language, you must manually perform the tedious Lua stack (
Exchange data with C) operation, but also pay attention to the correct conversion of the Data Types in the two languages. No wonder someone says that Lua's c
The API is similar to the assembly language. To reduce the burden on programmers, some C/C ++ wrapper came into being.
Tolua is not
Wrapper, but it is an Automatic Wrapper code generator. It uses a package file to describe the constants, variables, functions, and classes to be imported into the Lua environment.
And method. This file is written in simplified C ++ header file format. First, let tolua parse the package file to generate the C/C ++ source file containing the corresponding glue code. Then compile the generated source file
Translate and link it with the target modules that implement specific functions.
Although tolua automatically generates the glue code, it is not convenient to write a description file. Some other Wrapper libraries use the C ++ template metaprogramming technology to automatically generate appropriate connection code, thus avoiding additional description files, such as the boost library luabind.
6. luajit
Lua
It is very efficient and runs faster than many other scripts (such as Perl, Python, and Ruby), which has been proven in a third-party independent evaluation. Even so, there will still be people not satisfied, they
I always think "Well, it's not fast enough !". Luajit is an attempt to squeeze out a little more speed. It uses JIT compilation technology to compile Lua code to the local machine code and then directly execute it by the CPU.
The luajit evaluation report shows that it has a significant acceleration effect in floating point operations, loops, and collaborative process switching. However, if the program relies heavily on functions compiled by C, the running speed will not be improved.
Currently, luajit only supports x86 CPUs.
Luajit contains a library named Coco, which can be used independently. Coco provides real coordination for c Functions
You can suspend the coroutine at any point in the C function, and then return it to that point using the coroutine recovery operation in the future. In standard Lua, the suspension and restoration of coroutine are not allowed to cross the C function call boundary.
. Coco uses features that depend on specific systems, so pay special attention when Porting Programs.
VII. chunkspy
Lua's Virtual Machine bytecode instruction set is not part of the language definition, so no documentation is provided officially. You can certainly obtain information by viewing the source code, but this is inconvenient after all.
Chunkspy
It is a Lua Virtual Machine bytecode reassembler that can output a binary Lua code block to a bytecode Assembly file in a variety of formats that are very easy to read (detailed or simple, with or without the source program. It also supports
With interactive disassembly, you can immediately see the corresponding bytecode command after you have typed a line of code. The author of chunkspy wrote a detailed article about lua5 VM commands, named
No-frills introduction to Lua 5 VM
Instructions, which can be found on the project homepage. This article has now corrected the latest lua5.1. In addition, he is also the developer of the Yueliang project.
Projects use the Lua language itself to implement Lua. From the project name, the author should be a Chinese.
8. Others
Other libraries and tools include
Luaedit, luaeclipse, vs '05lualangpack (they are all ide or IDE plug-ins), luawrapper, calua,
Cpplua (wrapper Library), luaprofiler (performance measurement tool), etc., readers can find on the lua-user.org and luaforge.net website
To them and other useful resources.