In Lua, a function is a first-class value. Defining a function is like creating a common type value (except that the data of a function type value is mainly a command ), therefore, functions can still be defined in the function body. Assume that the function F2 is defined in the function F1, then it is called the embedded (inner) function of F2, and F1 is the outsourcing (enclosing) function of F2. Both outsourcing and embedding are passed, that is, F2 embedding must be F1 embedding, while F1 outsourcing must be F2 outsourcing. Embedded functions can access all local variables created by external functions. This feature is called lexical scoping. These local variables are called external local variables of the embedded function (external ).
Local variable) or upvalue (this word may be misleading, because upvalue actually refers to a variable rather than a value ). Compile the following code:
Function F1 (n) -- The function parameter is also the local function F2 () print (n) -- reference the local variable of the outsourcing function end return F2 end G1 = F1 (1979) G1 () -- print 1979 g2 = F1 (500) G2 () -- print 500
After G1 = F1 (1979) is executed, the life of the local variable N should have ended, but it has become the upvalue of the embedded function F2 (which is assigned to the variable G1, so it can still "survive" in some form, so that G1 () can print the correct value.
But why does G2 have the same function body as G1 (both are the built-in function bodies of F1 F2), but the print value is different? This involves a very important concept-closure ). In fact, when Lua compiles a function, it generates a prototype for it, which contains the VM commands corresponding to the function body and the constant values (numbers, text strings, etc.) used by the function) and some debugging information. At runtime, Every time Lua executes a function, such as function... when an expression such as end is used, it creates a new data object, which contains the reference and environment of the corresponding function prototype (environment, used to find the table with global variables) and an array composed of all upvalue references. This data object is called a closure. It can be seen that the function is the concept of compile-time, static, and closure is the concept of runtime and dynamic. The values of G1 and G2 are not functions but closures, and they are two different closures. Each closure can have its own upvalue value, so the results printed by G1 and G2 are certainly different. Although closures and functions are essentially different concepts, we do not differentiate them for convenience and without confusion.
Upvalue is very convenient to use, but their semantics is also very subtle and should be noted. For example, change the F1 function:
Function F1 (n) Local function F2 () print (n) end n = N + 10 return F2 end G1 = F1 (1979) G1 () -- print 1989
The nested function is defined before n = N + 10. Why does G1 () print 1989? Upvalue is actually a local variable, while a local variable is saved on the function stack framework (stack frame). As long as upvalue has not left its own scope, it always lives on the function stack. In this case, the closure will access them by pointing to the upvalue reference on the stack, and once upvalue is about to leave its own scope (which also means it will disappear from the stack immediately ), the closure will allocate space for it and save the current value. Then, you can access the upvalue by pointing to the reference of the newly allocated space. When executed to n of F1 (1979)
When N + 10, the closure has been created, but N has not left the scope, so the closure still references N on the stack. When return F2 is complete, n is about to end, in this case, the closure copies N (1989 already) to the space you manage for future access. After figuring out the internal secrets, it is not difficult to explain the running results.
Upvalue can also provide a data sharing mechanism between closures. Example:
Function create (n) Local function foo1 () print (n) end local function foo2 () n = N + 10 end return foo1, foo2 end F1, F2 = create (1979) f1 () -- print 1979 F2 () F1 () -- print 1989 F2 () F1 () -- print 1999
The prototypes of the closures F1 and F2 are the built-in functions foo1 and foo2 in create, and the upvalue referenced by foo1 and foo2 are the same, that is, the local variable n of create. As mentioned above, after executing the create call, the closure will copy the N value on the stack. Does F1 and F2 have an n Copy respectively? Otherwise, when Lua finds that the upvalue of the two closures points to the same variables on the current stack, it will intelligently generate only one copy, and then let the two closures share the copy, in this way, any closure that modifies the upvalue will be noticed by another. The above example clearly demonstrates this: each call to F2 increases the value of upvalue by 10, and then F1 prints the updated value. This Semantics of upvalue is very valuable. It enables the closure to communicate with each other without relying on global variables, thus greatly improving the code reliability.
When a closure is created, its upvalue is no longer on the stack. This is because the nested function can reference the local variables of the outer outsourcing function:
Function Test (n) Local function Foo () Local function inner1 () print (n) end local function inner2 () n = N + 10 end return inner1, inner2 end return Foo end t = test (1979) F1, F2 = T () F1 () -- print 1979 F2 () F1 () -- print 1989 G1, G2 = T () g1 () -- print 1989 G2 () G1 () -- print 1999 F1 () -- print 1999
After T = test (1979) is executed, the local variable n of test is "dead", so when F1, when the two closure F2 are created, n is not found on the stack. How do they obtain N values? Don't forget that the n of the test function is not only the upvalue of inner1 and inner2, but also the upvalue of foo. After T = test (1979), the closure T must have properly stored n, if n cannot be found on the current stack, F1 and F2 will be automatically located in the upvalue reference array of their outsourcing closure (which is called this, copy the reference value to your upvalue reference array. Observe the above Code carefully to determine that G1 and G2 share the same upvalue with F1 and F2. Why? Actually, G1 and G2 are created with the same closure (T) as F1 and F2, so the upvalue (n) They reference is actually the same variable, the search mechanism described earlier ensures that all upvalue references in the end point to the same place.
Lua uses functions as basic type values and supports lexical demarcation to make the language have powerful abstraction capabilities. A thorough understanding of functions, closures, and upvalue will help programmers make good use of this capability.