Lua C API

Source: Internet
Author: User

1. LUA Interpreter

What is the LUA interpreter?

The LUA interpreter is a standalone interpreter implemented using the LUA standard library and is a very small LUA application (no more than 500 lines of code in total). The interpreter is responsible for the interface between the program and the user: Get the file or string from the user and pass it to the LUA standard library, which is responsible for the final code run.

When Lua runs as a standalone program, LUA and the LUA interpreter deal with it.

2. Lua Extender

(1) Lua extension C program: C as an application language, LUA uses as a library

(2) C program extension Lua:lua as a program language, c as a library to use

In practical applications, such as game development in the daily activities, levels, battlefield and other major logic is the implementation of the Lua script extension. The game main program calls the active script, is the LUA extension C program (c program loading LUA script), and in the script must obtain some player information, such as the player location map number, player position and so on, is the C program open API to LUA call, C program extension LUA (LUA program call C).

So the concept of extension is mutual, you provide information to me, and then I help you to finish the work. Of course, the other side of the service can be provided solely by the other party, as well as the actual needs. Here are just a few examples of the use of Lua in the game: C Open API to LUA virtual machines, LUA scripts complete delegated tasks around these APIs, and C invokes the Lua script to complete the logic.

3. Lua C API

The Lua C API is a set of APIs for LUA and C communication, which has these features:

(1) functions to read and write LUA global variables

(2) functions that call LUA functions

(3) functions to run LUA code snippets

(4) A function that registers a C function and can then be called in Lua

Sacrificing robustness: Most of the functions in the API do not check the correctness of their parameters, and if you pass the wrong arguments, you may get "segmentation fault" or similar error messages, without a very clear error message to obtain.

Gain flexibility and simplicity

4. Important header file and function description

(1) lua.h: Defines the underlying functions that LUA provides, all of which are defined in lua.h with a lua_ prefix

A function to create a new LUA environment:Lua_open

Functions that call LUA functions:lua_pcall

Functions that read/write global variables for the LUA environment:

void Lua_pushnil (lua_state *l);

void Lua_pushboolean (lua_state *l, int bool);

void lua_pushnumber (lua_state *l, double n);

void lua_pushlstring (lua_state *l, const char *s, size_t length);

void lua_pushstring (lua_state *l, const char *s);

int lua_is ... (lua_state *l, int index);

int lua_gettop (lua_state *l);

void lua_settop (lua_state *l, int index);

void lua_pushvalue (lua_state *l, int index);

void lua_remove (lua_state *l, int index);

void Lua_insert (lua_state *l, int index);

void lua_replace (lua_state *l, int index);

Lua_settop (L,-1); /* Set top to their current value */

Lua_insert (L,-1); /* Move top element to the top */

......

A function that registers a new function that can be called by LUA code

(2) lauxlib.h:lauxlib.h defines the functions provided by the Auxiliary Library (AUXLIB), all functions defined therein are Lual_ -headed, The auxiliary library uses the underlying functions provided in LUA.H to provide a higher level of abstraction; All LUA standard libraries use Auxlib.

5. Lua_state structure

The LUA library does not define any global variables. All its states are stored in the dynamic structure lua_state, and pointers to this structure are used as a parameter to all LUA functions. This implementation allows LUA to re-enter (reentrant) and prepare for use in multi-threading.

Lua_state can be seen as a LUA runtime environment and is a closed environment. Each time Lua_open () gets a new LUA runtime environment, with no cross-cutting factor in the previous environment

6. The secrets of Lua and C communication

The secret of LUA and C communication is: virtual stacks. the use of stacks solves two uncoordinated problems between C and Lua: First, Lua automatically collects garbage, and C requires the allocation of storage units to be displayed, the contradiction caused by the two, and the confusion caused by the dynamic type in Lua and the static type inconsistency in C.

The stack is managed by LUA, and the garbage collector knows that the value is being used by C, and Lua operates the stack with a strict LIFO rule . When you call Lua, it only changes the top part of the stack. The C code has more freedom, and more specifically, you can query any element on the stack, or even insert and delete elements at any one location.

The C API follows the syntax of the C language, which is different from Lua. when using C for programming, we must note that there are many problems with type checking, error handling, and memory allocation. Most of the functions in the API do not check the correctness of their parameters; you need to be responsible for ensuring that the parameters are valid before calling the function. If you pass the wrong argument, you may get the error message "segmentation fault\" or something like this, and there is no clear error message available. In addition, the API focuses on flexibility and simplicity, sometimes at the expense of convenience and practicality. A general task might involve many API calls, which can be annoying, but he gives you the ability to control all details, such as error handling, buffer size, and similar problems. As the title of this chapter shows, the goal of this chapter is to preview what will be involved when you call Lua from C. If you do not understand some details do not worry, we will explain in detail in the following. However, there is a detailed description of the specified function in the LUA reference manual. Also, in the LUA release you can see examples of API applications, and the LUA standalone Interpreter (LUA.C) provides examples of application code, while standard libraries (LMATHLIB.C, LSTRLIB.C, and so on) provide examples of library code.


From now on, you put on the C Programmer's hat, when we talk about "you/You", we mean when you use C programming. The key content of communication between C and Lua lies in a virtual stack. almost all API calls operate on the value on the stack, and all data exchanges between C and Lua are done through this stack. Alternatively, you can use the stack to save temporary variables. The use of stacks solves two uncoordinated problems between C and Lua: First, Lua automatically collects garbage, and C requires the allocation of storage units to be displayed, both of which cause contradictions. Second, the dynamic types in Lua and the static types in C cause confusion. We'll cover the stack in detail in section 24.2.

Then look at: http://www.cnblogs.com/youxin/p/3797094.html

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.