lua的函数与闭包

     文献[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值