LUA Practice Preparation
When I started practicing Lua, I needed a LUA interpreter to execute LUA code, I learned LUA on windows, and I needed to generate the latest version of the LUA interpreter myself.
Methods are seen in:
Http://www.lua.org/manual/5.3/readme.html Building Lua on other systems
Set up a Visual Studio Win32 Console Application project, place the C file and H file in the source files and header files directory, and delete the luac.c file. Start the project error, follow the prompts to increase the symbol _crt_secure_no_warnings, start again, you can successfully generate the LUA interpreter.
C API Overview
Lua is an embedded language. That is, it is not a separate program, but a library that can be linked to other programs. LUA runs from the main program, the main program through the C API and LUA interaction, C API is a set of C code and LUA to interact with the C function. The main program can register new functions written in the C language (or other languages) in Lua, and LUA can invoke these new functions as libraries.
There are two notable differences between C and Lua:
1. c requires explicit memory release, and Lua uses a garbage collection mechanism.
2. C uses static types, and LUA uses dynamic types.
The design of the C API solves both of these problems. C and LUA interact through stacks.
Both the data and the data generated during the interaction between C and Lua are in the stack, which prevents the data from being recycled by LUA during use. Use the data in the stack to be aware of the use of the post-removal, if it pops up and then use, it may be recycled during use.
C through lua_pushstring/lua_pushnumber/... To push the value of the specified type to the stack by lua_tostring/lua_tonumber/... This method gets the value of the specified type in the stack. This solves the static type problem of C.
A simple LUA interpreter
In this interpreter, we use LUA as a library, first generating a DLL file for Lua.
The methods for building and using DLLs on Visual Studio are seen in:
https://msdn.microsoft.com/en-us/library/vstudio/ms235636 (v=vs.110). aspx
Searching for __declspec (dllexport) in the LUA project, you will find that the DLL that generates LUA needs to add a symbol Lua_build_as_dll.
With the LUA library, we can write an interpreter. The function of the interpreter is to iterate through the LUA code entered by the user and execute it.
#include <stdio.h>#include <string.h>#include "lua.h"#include "lualib.h"#include "lauxlib.h"intMainvoid){Charbuf[ the]; Lua_state *l = Lual_newstate (); Lual_openlibs (L);intError =0; while(Fgets (BUF,sizeof(BUF), stdin)! = NULL) {error = Lual_loadbuffer (L, buf,strlen(BUF),"Input") || Lua_pcall (L,0,0,0);if(Error)//error handling, printing error messages{printf("%s\n", Lua_tostring (L,-1)); Lua_pop (L,1); }} lua_close (L);return 0;}
C calling the Lua method
C Call the Lua method, you need to put the Lua method, parameters into the stack, and then call Lua_pcall/lua_call ... and other methods.
We prepare a LUA file, which defines a method printname, loading LUA files through a C program, and invoking this printname method.
Lua files are as follows:
function printName(name) print("hello, my name is " .. name)end
The C code is as follows:
#include <stdio.h>#include "lua.h"#include "lauxlib.h"#include "lualib.h"intMainvoid) {Lua_state *l = Lual_newstate (); Lual_openlibs (L);intError = Lual_loadfile (L,"Test.lua") || Lua_pcall (L,0,0,0);if(Error)//error handling, printing error messages, exiting{printf("%s\n", Lua_tostring (L,-1)); Lua_pop (L,1);return 1; } lua_getglobal (L,"Printname");//Gets the Lua method to invokeLua_pushstring (L,"Xiaoming");parameters required by the//lua methodLua_pcall (L,1,0,0);return 0;}
Lua calls the C method
Lua calls the C method and needs to register the C method in the LUA register:
voidconstchar *name, lua_CFunction f);
The format of the C method is:
typedefint (*lua_CFunction) (lua_State *L);
Example: Modifying a LUA file, calling the C method.
function printName(name) ifthen --checkName为C方法 print("hello, my name is " .. name) endend
Defining the CheckName method in C
staticint checkName(lua_State *L){ char *name = lua_tostring(L, -1//获取参数name intstrcmp"GM"); 1//弹出参数name //返回结果 return1//结果个数}
C's main program, register the CheckName method in Lua, and invoke the Lua Printname method.
intMainvoid) {Lua_state *l = Lual_newstate (); Lual_openlibs (L); Lual_loadfile (L,"Test.lua");//Do not consider error conditionsLua_pcall (L,0,0,0); Lua_register (L,"CheckName", CheckName);//Register C functionLua_getglobal (L,"Printname"); Lua_pushstring (L,"Xiaoming");//lua_pushstring (L, "GM"); no output intError = Lua_pcall (L,1,0,0);if(Error) {printf("%s\n", Lua_tostring (L,-1)); Lua_pop (L,1);return 1; }return 0;}
C is a process-oriented language, and the C method and the Lua method are consistent and easy to register with Lua. Like c++/java/c# ... Object-oriented language, many methods exist on the object, how to register these methods?
The LUA-driven GUI describes a method for creating a manager for a variety of objects, using the type and ID to find any object, in the 11th chapter of the Lua Game development Practice Guide. For example, there is a method draw (...) on the object, the method is defined in the manager draw (type, ID, ...), the method inside gets the object, call Draw (...) on the object. Method. The last method to register in Lua is the manager's in-draw method.
Kopiluainterface takes a different approach by putting a C # object into Lua, which can be used just like in C #.
Put the C # object into the LUA container:
Lua LUA = new Lua ();
lua["DT"] = DateTime.Now;
Using C # objects in Lua
Print (dt. GetType, type (dt))
This C # object is put into the LUA container in a userdata way, and the reason it can be userdata on the GetType is because this userdata defines the meta-method __index.
Resources
The second edition of LUA programming
A practical guide to the development of LUA games
Lua 5.3 Reference Manual: HTTP://WWW.LUA.ORG/MANUAL/5.3/
Kopiluainterface:https://github.com/gfoot/kopiluainterface
Lua Summary Two