AboutLua ModuleMechanism Analysis is the content to be introduced in this article, mainly to understand and learnLua ModuleMechanism. For details, refer to this article.LuaModule (ModuleThe Mechanism is similar to the dynamic library of C language. Some public code can be put in the module and providedLuaUse the application script. The module may also be used for imaging Android Application Frameworks, creating a new Application programming model, just conjecture.
The simplest way to use a module:
- view plaincopy to clipboardprint?require “os”
- os.clock()
- require “os”
- os.clock()
Table
LuaThere are eight data types in the language. The table (table) type implements the association array associative array )". Table can save various data types. The retriable associated with the table, which defines some actions of table operations. For example, when the + operation is used, the _ add attribute of the retriable will be searched. If yes, the operation is executed.
Function
Function isLuaMedium 8 is one of the basic types, which can be passed as values like other types. The table associated with the function is the environment table environment), which can be considered as the execution environment of the function. You can use setfenv () and getfenv () to set and obtain the function environment table. Global Operations in functions, such as defining global variables, are performed in this table. Multiple Functions can share a table.
Require
When using require to load a module, we actually do the following:
Check whether the module exists in the package. loaded table. If yes, this value is returned. You can use the following function to output the table content:
- view plaincopy to clipboardprint?function walkTable(t)
- for k,v in pairs(t) do
- print("+++",k,v,"+++")
- end
- end
- function walkTable(t)
- for k,v in pairs(t) do
- print("+++",k,v,"+++")
- end
- end
The content in package. loaded is as follows, including some default loaded libraries and global table _ G
- view plaincopy to clipboardprint?+++ string , table: 003B82B0 +++
- +++ debug , table: 003B9300 +++
- +++ package , table: 003B5C30 +++
- +++ _G , table: 003B2E10 +++
- +++ io , table: 003B7320 +++
- +++ os , table: 003B7988 +++
- +++ table , table: 003B51E0 +++
- +++ math , table: 003B8728 +++
- +++ coroutine , table: 003B59D8 +++
- +++ string , table: 003B82B0 +++
- +++ debug , table: 003B9300 +++
- +++ package , table: 003B5C30 +++
- +++ _G , table: 003B2E10 +++
- +++ io , table: 003B7320 +++
- +++ os , table: 003B7988 +++
- +++ table , table: 003B51E0 +++
- +++ math , table: 003B8728 +++
- +++ coroutine , table: 003B59D8 +++
Find the loader for the module), query package. preload, usually empty
Use the loadfile function to load the Lua file. Loadfile loads the file as a function, and require transmits the module name as a parameter to the function. If a return value exists, the return value is placed in Table package. loaded. If not, the value in package. loaded is returned.
Module
When the module function is used in the module File, It is shown as follows;
- view plaincopy to clipboardprint?module “mymodule”
- module “mymodule”
Actually, it is equivalent to the following statement:
- View plaincopy to clipboardprint? Local modname = "mymodule"-define the module name
- Local M = {} -- Define the module table to be returned
- _ G [modname] = M -- add the module table to the global variable
- Package. loaded [modname] = M -- add the module table to package. loaded to prevent multiple loads.
- Setfenv (1, M) -- set the module table as the function environment table, so that all operations in the module are in the module table, so that the defined function is directly defined in the module table
- Local modname = "mymodule"-define the module name
- Local M = {} -- Define the module table to be returned
- _ G [modname] = M -- add the module table to the global variable
- Package. loaded [modname] = M -- add the module table to package. loaded to prevent multiple loads.
- Setfenv (1, M) -- set the module table as the function environment table, so that all operations in the module are in the module table, so that the defined function is directly defined in the module table
PassModule(), You can easily write the content in the module.
Test Environment
- lua for windows IDE
Summary: AboutLua ModuleThe content of the mechanism analysis is complete. I hope this article will help you!