文献[1]中,在第5章function and closure中提到:
When Lua compiles a function it generates a prototype containing the virtual machine instructions for the function, its constant values (numbers, literal strings, etc.), and some debug information. At run time, whenever Lua executes a function...end expression, it creates a new closure. Each closure has a reference to its corresponding prototype, a reference to its environment (a table wherein it looks for global variables), and an array of references to upvalues, which are used to access outer local variables.
函数是编译时的概念,是静态的;而闭包是运行时的概念,是动态的。函数会被编译成一种原型(prototype)包含虚拟机的指令,常量和debug信息。在运行的时候,虚拟机执行这段代码,会创建一个闭包,除了会引用上述的原型外,还会应用环境变量表(全局变量)和一组upvalues的引用。upvalue可以访问外层局部变量。
参考文献
[1] Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes.The Implementation of Lua 5.0