- 论坛徽章:
- 5
|
本帖最后由 starwing83 于 2012-09-29 16:27 编辑
回复 216# wwwsq
这样吧,我找段代码给你看:- void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
- int loop;
- for (loop = 0; loop < MAXTAGLOOP; loop++) {
- const TValue *tm;
- if (ttistable(t)) { /* `t' is a table? */
- Table *h = hvalue(t);
- TValue *oldval = cast(TValue *, luaH_get(h, key));
- /* if previous value is not nil, there must be a previous entry
- in the table; moreover, a metamethod has no relevance */
- if (!ttisnil(oldval) ||
- /* previous value is nil; must check the metamethod */
- ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
- /* no metamethod; is there a previous entry in the table? */
- (oldval != luaO_nilobject ||
- /* no previous entry; must create one. (The next test is
- always true; we only need the assignment.) */
- (oldval = luaH_newkey(L, h, key), 1)))) {
- /* no metamethod and (now) there is an entry with given key */
- setobj2t(L, oldval, val); /* assign new value to that entry */
- invalidateTMcache(h);
- luaC_barrierback(L, obj2gco(h), val);
- return;
- }
- /* else will try the metamethod */
- }
- else /* not a table; check metamethod */
- if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
- luaG_typeerror(L, t, "index");
- /* there is a metamethod */
- if (ttisfunction(tm)) {
- callTM(L, tm, t, key, val, 0);
- return;
- }
- t = tm; /* else repeat with 'tm' */
- }
- luaG_runerror(L, "loop in settable");
- }
复制代码 这段代码来自著名的Lua5.2.1,Lua编程语言是游戏界公认的游戏脚本编程语言,也是目前世界上最小最快的脚本语言之一,是《魔兽世界》的官方插件编程语言。Lua的C实现也公认是十分优美的项目,被公认为“艺术性的实现”。这个函数是Lua语言中唯一的一种数据结构“表”的设置函数,可以认为是Lua的核心函数之一。请看看他在if里面用了多少赋值、短路特性吧。
|
|