"Programming in Lua 3" reading notes (XXII)

Date: 2014.8.6
PartⅣ The C API

26 Extending Your Application
     It is important that the use of Lua is used to make the configuration language. With the main language to do some configuration functions.

 
26.1 The Basics
     Sometimes programs need to configure some features information, a lot of time there may be many other ways to make configuration easier than using lua: such as using environment variables or read a file, read the file related to parse the file. If Lua configuration then corresponds to the file with lua replaced as csv, txt files, etc. to be read.
     Using Lua configured when you need to use to control Lua Lua API to parse the file, and then get the value you want to configure from Lua global variables. Load function assumes the function of loading a file, the function call luaL_loadfile fast read statements from the name given to the file, and then call lua_pcall to run fast statement. If encountered an error (for example: in the configuration file syntax error), the error function at this time to push the stack and returns a non-zero error code; lua_tostring time we use the program arguments from the stack obtained index -1 information.
eg
lua_tostring (L, -1) - get the information from the stack
     After the file has been loaded, you need to get the variable values ​​from Lua global variables. At this point calls lua_getglobal get the desired value, the parameter is the name of a global variable. Each call will find the value of the global variable value push the stack, then the corresponding main program gets the corresponding value from the stack. Lua_is * is then used to determine the type of the value, and then calls lua_to * get the value of the corresponding type.
     The benefits of using Lua configuration is done, Lua help you do all the grammar detection process (the configuration file may even have a comment?); Lua users can do more complex configuration process, such as configuration files may prompt some users information, or to detect an environment variable to select the appropriate values:
e.g.
--configuration file
if getenv("DISPLAY") == ":0.0" then
     width = 300;height = 300
else
     width = 200;height = 200
     Another reason is that: for now the program is easy to use Lua to add new configuration information, this feature makes the program more flexible.

 
26.2 Table Manipulation
     Such a scenario is assumed: configure the background color of the window. In this case it requires three values: RGB value. C, typically in the range of each value is [0, 255], and Lua, because all are real number, so the use of the [0,1] range.
     Usually a variable will bring a lot of trouble for each value statement, such as: If a program needs a variety of configurations, such as foreground color, button color, etc., and that this will require a lot of variables; and this method is not suitable for use predefined color , or default. In this case, Lua in the table on such a configuration suitable for the job:
e.g.
background = {r = 0.30,g = 0.10,b = 0}
     Use table will bring great convenience to the script; this time also can easily use some of the predetermined value, such as:
e.g.
BLUE = { r = 0, g = 0, b = 1.0}
background = BLUE
     While the C code To obtain these values, the following can be implemented:
e.g.
lua_getglobal(L,"background")
if (! lua_istable(L,-1))
     error(L,"'background' is not a table");
red = getcolorfield(L,"r");
green = getcolorfield(L,"g");
blue = getcolorfield(L,"b");

#define MAX_COLOR     255
int getcolorfield(lua_State *L,const char* key)
{
      int result;
     //key 推出,而把相应的value推进栈
    lua_pushstring(L,key);
      lua_gettable(L,-2);
     //前面两段可以用 lua_getfield(L,-1,key);来替代
     if(!lua_isnumber(L,-1))
          error(L,"invalid component in background color");
     result = (int)(lua_tonumber(L,-1)) * MAX_COLOR);
     lua_pop(L,1);
     return result;
}
 

26.3 Calling Lua Function
     Profiles can be defined function calls to the program, which is a big strong Lua lies.
     API calls a function is relatively simple: First, the function push will be called to the stack; then the parameter push will be called to the stack; may then perform the actual function call lua_pcall function; finally obtained results of the call from the stack.
     The second parameter is the number lua_pcall representation and the number of results returned transmission parameters; fourth parameter refers error handling function, 0 indicates no error handling function, a function representing the position of the rest of the index value in the stack, thus these functions should first push to the stack; the Lua will adjust the parameters and returns the number of the operation result actual function, you will be required if the extra push Ni or discard the value. Before the results push, lua_pcall functions and function parameters will be removed away from the stack. When the function returns a number of results, a result of first push.
     If you encounter any errors during operation lua_pcall, lua_pcall will return an error code; Not only that, but also will push to stack in an error message (and will still launch function parameters and functions). Prior to push the error message, if there is an error handler, lua_pcall still will first perform error handling function.
     The normal error conditions, lua_pcall returns LUA_ERRUN has two types of errors will return different error codes: The first is a memory allocation error will be returned LUA_ERRMEM; the second case is when running lua error handling when the function itself, because at this time certainly will not call the error function, so this time will return an error code LUA_ERROR immediately. lua5.2 also defined in the third case, when the finalizer (interpreter?) throw wrong time, this time will be returned LUA_ERRGCMM (error in a GC metamethod). This code indicates that the error is not related to the calling function itself.


 
26.4 A Generic Call Function
     Here, will create a wrapper (wrapper?) Used to call lua functions used in C vararg (variable parameter?) Characteristics. If we said wrapper is call_va, parameters: 1, the name of the function to be called; 2, and a return value of the result parameter string representation; 3, the parameter list; 4, stores the return value of the result list pointer. Use the API to handle all operations:
e.g.
call_va(L,"f","dd>d",x,y,&z);
     String "dd> d" means "double and two types of parameters, a type of return value of a double", here "d" means "double", "I" represents the "integer", "s" represents "string . " Use ">" delimiters. If the function does not return a value, ">" is optional.

Reproduced in: https: //www.cnblogs.com/zhong-dev/p/4044562.html

Guess you like

Origin blog.csdn.net/weixin_34014277/article/details/94600984