Lua 5.1 的变化

时隔多年,Lua终于又发布新版本了,这个版本改动主要集中在garbage collection上面,我匆匆看了一下新版本的Lua,写下了一些评论。

我原来的评论是用英文写的,锻炼一下自己的Chinglish,中文是随手加上的,写得不好,请见谅。

Operator changes
Adds % and #. % has the same meaning as % in C. # is the length operator.
增加了%和#运算符,%和C中的一样意思,#是用来得到字符串长度的

The Length Operator
The operator is used to get the length of a string. Example: n=#t; Just as what strlen() in C does. But if there are several nil in a string, the return value is undefined. The undefined value results from the O(log n) complexity algorithm Lua used.
用来得到字符串长度,例如n=#t。就像C语言中的strlen()一样。但是如果有多个nil在字符串中,结果将是不可预知的。这时由于Lua使用了一个时间复杂度为O(log n)的算法。

Environments
Environments represent the variables for the current function or the global scope. An environment is actually a table. After you get an environment instance, you can access its values using table functions. The pseudo-index together with lua_getfield() and lua_setfield() help to access environments in C.You can use getfenv() or setfenv() to access the function environment or the global environment in Lua. The function environment helps a function to pass some variables to the functions it calls, because the function call stack shares the same function environment. See the Lua reference for more details.
In fact, this new feature has ready existed in practice for a long time. Many Lua-based programs already used global tables or function tables. Integrating this feature will make Lua easier to used, of course.
environment表示函数或者全局的变量。一个environment事实上是一个table。当你得到一个environment的实例后,你就可以用一般操作table方法操作。pesudo-index和lua_getfield()以及 lua_setfield()是C中访问environment的函数,getfenv()和 setfenv()是Lua中的对应函数。函数environment帮助一个函数传递参数到它调用的函数,因为函数调用栈共享一个相同的environment。详见lua的帮助

Garbage Collection
The garbage collection algorithm changes, in my view, nearly completely. The 5.0 collector focuses on static statistics, while the 5.1 collector focuses more on dynamic statistics. That is: 5.0 collector counts bytes allocated; 5.1 collector counts bytes change per second, (it also counts bytes allocated). The 5.1 collector is an incremental collector so that the garbage-colecting operation is not a burst. This can avoid the sudden lag happens in Lua 5.0 when the Lua starts to collect a bunch of garbages.
The new garbage collection functions lua_gc() in C or collectgarbage() in Lua provide some more controllable garbage collection features. See the Lua reference for more details.
垃圾回收算法几乎完全改变了。5.0的主要关注静态的数据,而5.1的更关注动态变化。5.0的计算分配了的大小,5.1的计算每秒钟分配量的变化(虽然它也计算分配了的大小)。5.1的是一个渐进式的回收器,因此回收操作不会是一个突发的事件。这可以避免由于进行一次大规模垃圾回收而造成突然的停顿。新的垃圾回收函数:C语言中是lua_gc(),LUA中是collectgarbage(),提供了更细致的垃圾回收过程的控制。详见lua的帮助

Module
Lua 5.1 has a better encapsulation for modules. It puts several module functions into the "package" object, which makes them incompatible with Lua 5.0. It also provides user-defined loaders for loading a module. One loader for a module. If the loader for a module does not exists, Lua will call the all-in-one loader (the default loader). The require() function now provides not only loading Lua file but also C functions in C library. But the C function name should be of the pattern "luaopen_" plus the module name with all dots replaced by "_". It also supports "comment" (not the usual comment) in the module name in Lua. For example, "a.v1-b.c" in Lua means "luaopen_b_c" in C. See the Lua reference for how to use this "comment".
Submodule support is also added to Lua 5.1. But it seems to me just a recursive table in Lua or the "luaopen_a_b_c" like function in C (this function will be loaded if you call "require("a.b.c")" in Lua).
Lua5.1 对module的包装更好。它将多个module函数放入"package"对象中。这使到部分5.0代码在5.1中需要重写。它提供了用户定义的loader来加载module。每一个module有一个对应的loader。如果一个module对应的loader不存在,那么Lua将使用缺省的loader。require()函数现在提供了加载Lua文件以及C函数库中函数的功能。但是C函数的名称必须符合规范:"luaopen_"加上module 名字,名字中所有的"."要用"_"替换。它同时支持在module名字和C函数名字中“注释”(不是通常所说的注释)。例如Lua中"a.v1-b.c"表示C函数 "luaopen_b_c"。详见Lua的帮助

Trival Changes
There're other changes, which I think is not so important to be mentioned. See Lua reference for detailed informations.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值