本书已经由图灵出版社正式出版,书名《Lua设计与实现》。
喜欢的朋友,可以购买一本支持作者,购买链接:
-
第一章 概论
-
第二章 基础数据结构
-
第三章 Lua虚拟机
-
第四章 Lua虚拟机指令
-
第五章 表
-
第六章 环境与模块
- 环境相关的变量
- 模块
- Lua模块热更新原理
- [闭包]
-
第七章 Lua调试器(100%)
-
第八章 Lua GC
-
第九章 杂项
Lua source internal
本书已经由图灵出版社正式出版,书名《Lua设计与实现》。
喜欢的朋友,可以购买一本支持作者,购买链接:
第一章 概论
第二章 基础数据结构
第三章 Lua虚拟机
第四章 Lua虚拟机指令
第五章 表
第六章 环境与模块
第七章 Lua调试器(100%)
第八章 Lua GC
第九章 杂项
static void setnodevector (lua_State *L, Table *t, int size) {
int lsize;
if (size == 0) { /* no elements to hash part? */
t->node = cast(Node *, dummynode); /* use common `dummynode' */
lsize = 0;
}
else {
int i;
lsize = ceillog2(size);
if (lsize > MAXBITS)
luaG_runerror(L, "table overflow");
size = twoto(lsize);
t->node = luaM_newvector(L, size, Node);
for (i=0; i<size; i++) {
Node *n = gnode(t, i);
gnext(n) = NULL;
setnilvalue(gkey(n));
setnilvalue(gval(n));
}
}
t->lsizenode = cast_byte(lsize);
t->lastfree = gnode(t, size); /* all positions are free */
}
t->lastfree = gnode(t, size); /* all positions are free */
从这行代码可知,lastfree指针指向的那个节点是不可用的;
这个图里的lastfree指向的有点不明确
结贴!!!自己看错了,最后说明一下。
“然而,即使是这样,却仍然有另一个没有解决的问题.从上面的算法可以看出,没有被引用的对象其颜色在一个扫描过程中始终保持不变为白色,那么假如一个对象在一个GC过程的标记阶段之后被创建,根据前面对颜色的描述,它应该是白色的,这样在紧跟着的回收阶段,这个对象就会在没有被扫描标记的情况下被认为是没有被引用的对象而删除.因此,Lua除了前面的三色概念之外,又细分出来一个"双白色"的概念.简单的说,Lua中的白色分为"当前白色(currentwhite)"和"非当前白色(otherwhite)",这两种白色的状态交替使用,第N次GC使用的是第一种白色,那么下一次就是另外一种,以此类推,代码在回收时候会做判断,如果某个对象的白色不是此次GC回收使用的白色状态将不会被认为是没有被引用的对象而回收,这样的白色对象将留在下一次GC中进行扫描,因为在下一次GC中上一次幸免的白色将成为这次的回收颜色.”
我觉得您的说法有点问题:
sweep的代码(如果是deadmask(otherwhite)就会进入else逻辑,从而freeobj):
int deadmask = otherwhite(g);
// 循环省略...
if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */
lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT));
makewhite(g, curr); /* make it white (for next cycle) */
p = &curr->gch.next;
}
else { /* must erase `curr' */
lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT));
*p = curr->gch.next;
if (curr == g->rootgc) /* is the first element of the list? */
g->rootgc = curr->gch.next; /* adjust first */
freeobj(L, curr);
}
}
补充说明:
在atomic最后,会切换白色,也就是currentwhite变成了otherwhite,而otherwhite变成了currentwhite。说明了从在这个时间点,全部扫描完之后,如果仍然是白色的节点在接下来的sweep会被清理掉,而之后的白色不会被清理掉,说明如下:
第四章-Lua词法
列举的是 Lua 的 EBNF,是否标题叫 “Lua 的语法” 来得比较合理?当然叫 “Lua 的词法” 丝毫不影响阅读。
rt
给的图上base位置应该是错的
我是个初学者
关于第二章 基础数据结构中的Lua表中的:以上代码创建了一个Lua表,向其中添加了key分别为1和100的两条记录,最后通过ipairs打印出其数组部分的key分别是什么,但是最后的输出只有1这一个key,可见100在这里并没有落入数组中.,这个似乎是错误的,我测试过我能够输出t[100]为0,查了下ipairs当得出数据为nil时,则停止。
希望能早日完成整本书。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.