Lua Module Mechanism Analysis

Source: Internet
Author: User

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:

 
 
  1. view plaincopy to clipboardprint?require “os”    
  2. os.clock()    
  3. require “os”  
  4. 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:

 
 
  1. view plaincopy to clipboardprint?function walkTable(t)    
  2.        for k,v in pairs(t) do    
  3.               print("+++",k,v,"+++")    
  4.        end    
  5. end    
  6. function walkTable(t)  
  7.        for k,v in pairs(t) do  
  8.               print("+++",k,v,"+++")  
  9.        end  
  10. end  

The content in package. loaded is as follows, including some default loaded libraries and global table _ G

 
 
  1. view plaincopy to clipboardprint?+++ string      ,      table: 003B82B0     +++    
  2. +++ debug      ,      table: 003B9300      +++    
  3. +++ package   ,      table: 003B5C30     +++    
  4. +++ _G   ,      table: 003B2E10     +++    
  5. +++ io     ,      table: 003B7320      +++    
  6. +++ os    ,      table: 003B7988      +++    
  7. +++ table ,      table: 003B51E0     +++    
  8. +++ math       ,      table: 003B8728      +++    
  9. +++ coroutine ,      table: 003B59D8     +++    
  10. +++ string      ,      table: 003B82B0     +++  
  11. +++ debug      ,      table: 003B9300      +++  
  12. +++ package   ,      table: 003B5C30     +++  
  13. +++ _G   ,      table: 003B2E10     +++  
  14. +++ io     ,      table: 003B7320      +++  
  15. +++ os    ,      table: 003B7988      +++  
  16. +++ table ,      table: 003B51E0     +++  
  17. +++ math       ,      table: 003B8728      +++  
  18. +++ 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;

 
 
  1. view plaincopy to clipboardprint?module “mymodule”    
  2. module “mymodule” 

Actually, it is equivalent to the following statement:

 
 
  1. View plaincopy to clipboardprint? Local modname = "mymodule"-define the module name
  2. Local M = {} -- Define the module table to be returned
  3. _ G [modname] = M -- add the module table to the global variable
  4. Package. loaded [modname] = M -- add the module table to package. loaded to prevent multiple loads.
  5. 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
  6. Local modname = "mymodule"-define the module name
  7. Local M = {} -- Define the module table to be returned
  8. _ G [modname] = M -- add the module table to the global variable
  9. Package. loaded [modname] = M -- add the module table to package. loaded to prevent multiple loads.
  10. 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

 
 
  1. lua for windows IDE 

Summary: AboutLua ModuleThe content of the mechanism analysis is complete. I hope this article will help you!

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.