lua 中的assert 与loadstring 问题
loadstring 通过加载一个符合lua语言规范的字符串 返回一个lua function,
例如:
local str = "print 'dushiwei.cn'"
local str = loadstring(str)
assert(str)()
local str = "print 'dushiwei.cn'"
local str = loadstring(str)
等价于 local script = function()
print 'dushiwei.cn'
end
标签: lua loadstring assert
Lua编译与运行
Lua是解释性语言,但Lua会首先把代码预编译成中间码然后再执行。不要以为需要编译就不是解释型语言,Lua的编译器是语言运行时的一部分,所以,执行编译产生中间码速度会更快。
dofile/dostring和loadfile/loadstring的区别:
(1)do*会编译并执行;load*只编译代码生成中间码并且返回编译后的chunk作为一个函数,但不执行代码。
(2)load*较为灵活,发生错误时load*会返回nil和错误信息(可以打印出来)。
(3)如果要运行一个文件多次,load*只需要编译一次,但可以多次运行,do*每次都需要编译。
(4)dostring(str)等价于loadstring(str)()
热门日志
分类
- Django(4)
- ssdb(1)
- Mac(7)
- C(1)
- memcache(1)
- Python(32)
- Vim(8)
- sed(2)
- ansible(3)
- awk(4)
- shell(3)
- about(1)
- git(9)
- bat(4)
- svn(0)
- docker(1)
- Tornado(1)
- go(2)
- 架构(18)
- Vue(1)
- game(2)
- Html(6)
- Java(8)
- Mysql(37)
- Ajax(2)
- Jsp(1)
- Struts(8)
- Linux(72)
- JavaScript(39)
- Staruml(0)
- Mouth(1)
- Php(102)
- Windows(8)
- Message(48)
- Lua(10)
- Compute(1)
- Redis(7)
- Nginx(12)
- Jquery(1)
- Apache(1)
- cocos2d-x(8)