Skip to content

Instantly share code, notes, and snippets.

@Eonblast
Created January 5, 2011 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eonblast/766137 to your computer and use it in GitHub Desktop.
Save Eonblast/766137 to your computer and use it in GitHub Desktop.
Diff Lua 5.2.2.0 alpha to Lua + len
$ diff -r lua-5.2.0-alpha lua-len/
diff -r lua-5.2.0-alpha/src/lapi.c lua-straight/src/lapi.c
725c725
< setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
---
> setobj2t(L, hvalue(t), luaH_set(L, hvalue(t), L->top-2), L->top-1); /*+ get & set +*/
738c738
< setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
---
> setobj2t(L, hvalue(o), luaH_setint(L, hvalue(o), n), L->top-1); /*+ get & set +*/
1091a1092,1100
> LUA_API void lua_count (lua_State *L, int idx) {
> StkId t;
> lua_lock(L);
> t = index2addr(L, idx);
> luaV_objcount(L, L->top, t);
> api_incr_top(L);
> lua_unlock(L);
> }
>
diff -r lua-5.2.0-alpha/src/lcode.c lua-straight/src/lcode.c
275c275
< TValue *idx = luaH_set(L, fs->h, key);
---
> TValue *idx = luaH_set(L, fs->h, key); /*+ get (unchanged) +*/
289c289
< setnvalue(idx, cast_num(k));
---
> setnvaluetbl(fs->h, idx, cast_num(k)); /*+ set +*/
724c724
< int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
---
> int o2 = (op != OP_UNM && op != OP_LEN && op != OP_COUNT) ? luaK_exp2RK(fs, e2) : 0;
775a776,780
> case OPR_COUNT: {
> luaK_exp2anyreg(fs, e); /* cannot operate on constants */
> codearith(fs, OP_COUNT, e, &e2, line);
> break;
> }
diff -r lua-5.2.0-alpha/src/lcode.h lua-straight/src/lcode.h
36c36
< typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
---
> typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_COUNT, OPR_NOUNOPR } UnOpr;
diff -r lua-5.2.0-alpha/src/ldebug.c lua-straight/src/ldebug.c
185c185
< setbvalue(luaH_setint(L, t, lineinfo[i]), 1);
---
> setbvaluetbl(t, luaH_setint(L, t, lineinfo[i]), 1); /*+ get & set +*/
413a414
> case OP_COUNT: tm = TM_COUNT; break;
diff -r lua-5.2.0-alpha/src/llex.c lua-straight/src/llex.c
130c130
< o = luaH_setstr(L, ls->fs->h, ts);
---
> o = luaH_setstr(L, ls->fs->h, ts); /*+ get (unchanged) +*/
132c132
< setbvalue(o, 1); /* t[string] = true */
---
> setbvalue(o, 1); /* t[string] = true */ /*+ set (unchanged) +*/
diff -r lua-5.2.0-alpha/src/lobject.h lua-straight/src/lobject.h
144a145,151
> /*+ set numeric value with live table element counter - used once in lcode.c +*/
> #define setnvaluetbl(T,objT,x) \
> { TValue *oT=(objT); Table *tbl = (T); \
> if(ttisnil(oT)) tbl->count = tbl->count +1; \
> oT->value_.n=(x); oT->tt_=LUA_TNUMBER; \
> } /*+*/
>
155a163,169
> /*+ set bool with live table element counr - used once in ldebug.c +*/
> #define setbvaluetbl(T,objT,x) /*+*/\
> { TValue *oT=(objT); Table *tbl = (T); /*+*/\
> if(ttisnil(oT)) tbl->count = tbl->count +1; /*+*/\
> oT->value_.b=(x); oT->tt_=LUA_TBOOLEAN; /*+*/\
> } /*+*/
>
194a209,223
> /*+ generic table set with live element count +*/
> #define setobjtbl(L,T,objT,objV) \
> { const TValue *oV=(objV); TValue *oT=(objT); Table *tbl = (T); \
> if(ttisnil(oT) && !ttisnil(oV)) \
> tbl->count = tbl->count +1; /*+!+*/\
> else if(!ttisnil(oT) && ttisnil(oV)) { \
> tbl->count = tbl->count -1; /*+!+*/\
> if(tbl->count < 0) \
> luaG_runerror(L, "table count underrun"); \
> }\
> oT->value_ = oV->value_; oT->tt_=oV->tt_; \
> checkliveness(G(L),oT); \
> }
>
>
207c236
< /* from table to same table */
---
> /* from table to same table: no change of element count */
209,210c238,241
< /* to table */
< #define setobj2t setobj
---
> /*+ to table: potential change of element count +*/
> #define setobj2t setobjtbl
> /* to table: set key */
> #define setobjk2t setobj
382a414
> int count; /*+ element count +*/
diff -r lua-5.2.0-alpha/src/lopcodes.c lua-straight/src/lopcodes.c
37a38
> "COUNT",
85a87
> ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_COUNT */
diff -r lua-5.2.0-alpha/src/lopcodes.h lua-straight/src/lopcodes.h
194a195
> OP_COUNT,/* A B R(A) := count of R(B) */
diff -r lua-5.2.0-alpha/src/lparser.c lua-straight/src/lparser.c
849a850
> case '%': return OPR_COUNT; /*+ set your preferred, non-standard sign here! +*/
diff -r lua-5.2.0-alpha/src/lstate.c lua-straight/src/lstate.c
136c136
< setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt);
---
> setobj2t(L, registry, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt); /*+ get & set +*/
139c139
< setobj2t(L, luaH_setint(L, registry, LUA_RIDX_GLOBALS), &mt);
---
> setobj2t(L, registry, luaH_setint(L, registry, LUA_RIDX_GLOBALS), &mt); /*+ get & set +*/
diff -r lua-5.2.0-alpha/src/ltable.c lua-straight/src/ltable.c
315c315
< setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]);
---
> setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]); /*+ no change +*/
324c324
< setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
---
> setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); /*+ no change +*/
367a368
> t->count = 0; /*+ main element count +*/
406c407
< return luaH_set(L, t, key); /* re-insert key into grown table */
---
> return luaH_set(L, t, key); /* re-insert key into grown table */ /*+ internal +*/
425c426
< setobj2t(L, gkey(mp), key);
---
> setobjk2t(L, gkey(mp), key); /*+ keys only, no change but name +*/
diff -r lua-5.2.0-alpha/src/ltm.c lua-straight/src/ltm.c
35c35
< "__gc", "__mode", "__len", "__eq",
---
> "__gc", "__mode", "__len", "__count", "__eq",
diff -r lua-5.2.0-alpha/src/ltm.h lua-straight/src/ltm.h
23a24
> TM_COUNT,
diff -r lua-5.2.0-alpha/src/lvm.c lua-straight/src/lvm.c
135c135
< TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
---
> TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ /*+ get +*/
138c138
< setobj2t(L, oldval, val);
---
> setobj2t(L, h, oldval, val); /*+ set +*/
169d168
<
333a333,363
> void luaV_objcount (lua_State *L, StkId ra, const TValue *rb) {
> const TValue *tm;
> switch (ttype(rb)) {
> /*+ get actual count of elements in table O(1) +*/
> case LUA_TTABLE: {
> Table *h = hvalue(rb);
> tm = fasttm(L, h->metatable, TM_COUNT);
> if (tm) break; /* metamethod? break switch to call it */
> setnvalue(ra, cast_num(h->count)); /*+ real element count +*/
> return;
> }
> /* get UTF-8 estimate (http://lua-users.org/wiki/LuaUnicode) */
> case LUA_TSTRING: {
> unsigned char *p = (unsigned char *)getstr(rawtsvalue(rb));
> unsigned char *q = p + tsvalue(rb)->len;
> size_t count = 0;
> while(p < q) { if(*p <= 127 || (*p >= 194 && *p <= 244)) count++; p++; }
> setnvalue(ra, cast_num(count)); /*+ utf-8 length estimate +*/
> return;
> }
> default: { /* try metamethod */
> tm = luaT_gettmbyobj(L, rb, TM_COUNT);
> if (ttisnil(tm)) /* no metamethod? */
> luaG_typeerror(L, rb, "get count of");
> break;
> }
> }
> callTM(L, tm, rb, luaO_nilobject, ra, 1);
> }
>
>
403c433
< case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN:
---
> case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN: case OP_COUNT:
602a633,635
> vmcase(OP_COUNT,
> Protect(luaV_objcount(L, ra, RB(i)));
> )
768c801
< setobj2t(L, luaH_setint(L, h, last--), val);
---
> setobj2t(L, h, luaH_setint(L, h, last--), val); /*+ get & set+*/
diff -r lua-5.2.0-alpha/src/lvm.h lua-straight/src/lvm.h
40a41
> LUAI_FUNC void luaV_objcount (lua_State *L, StkId ra, const TValue *rb);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment