Showing with 2,187 additions and 25 deletions.
  1. +1 −0 .gitignore
  2. +126 −1 README
  3. +11 −2 src/lapi.c
  4. +8 −3 src/lcode.c
  5. +1 −1 src/lcode.h
  6. +2 −1 src/ldebug.c
  7. BIN src/liblua.a
  8. +2 −2 src/llex.c
  9. +39 −3 src/lobject.h
  10. +2 −0 src/lopcodes.c
  11. +1 −0 src/lopcodes.h
  12. +1 −0 src/lparser.c
  13. +2 −2 src/lstate.c
  14. +5 −4 src/ltable.c
  15. +1 −1 src/ltm.c
  16. +1 −0 src/ltm.h
  17. +1 −1 src/lua.h
  18. +43 −4 src/lvm.c
  19. +1 −0 src/lvm.h
  20. BIN { → test}/.DS_Store
  21. +32 −0 test/README
  22. +465 −0 test/bad-utf-8.txt
  23. +1 −0 test/bench.lua
  24. +67 −0 test/benchobj.lua
  25. +27 −0 test/bisect.lua
  26. +16 −0 test/cf.lua
  27. +5 −0 test/echo.lua
  28. +7 −0 test/env.lua
  29. +32 −0 test/factorial.lua
  30. +40 −0 test/fib.lua
  31. +13 −0 test/fibfor.lua
  32. +13 −0 test/globals.lua
  33. +3 −0 test/hello.lua
  34. +446 −0 test/len.lua
  35. +111 −0 test/life.lua
  36. +7 −0 test/luac.lua
  37. +7 −0 test/printf.lua
  38. +12 −0 test/readonly.lua
  39. +29 −0 test/sieve.lua
  40. +66 −0 test/sort.lua
  41. +28 −0 test/strlen.lua
  42. +12 −0 test/table.lua
  43. +32 −0 test/trace-calls.lua
  44. +38 −0 test/trace-globals.lua
  45. +416 −0 test/utf-8.txt
  46. +14 −0 test/xd.lua
@@ -2,3 +2,4 @@
*.log
lua
luac
*/.DS_Store
127 README
@@ -1,5 +1,130 @@


This is Lua 5.2 (alpha), released on 23 Nov 2010
with a hack for true count of table and string sizes.
as of 06 Jan 2011.

THIS IS NOT A STANDARD LUA INSTALLATION.
Please go to http://www.lua.org/download.html for that.

For installation instructions, license details, and
further information about Lua, see doc/readme.html.


Use:

Use the % operator to retrieve the actual number of
elemets in a table that are not 'nil'.

t = {'foo', nil, 'bar'}
print(%t)

Or to get an estimate about how many printable
characters a UTF-8 string has.

Compare this with #t. Of course, instead of '%' we
might want to have a function.


Basic direction:

git clone git@github.com:Eonblast/Lua.git lua52
cd lua52
sudo make <your os> install
src/lua test/len.lua
src/lua test/strlen.lua


Implementation:

The count is updated with every table update and
therefore takes no time when called.

The diff to Lua 5.2.2.0 alpha can be seen here
https://github.com/Eonblast/Lua/compare/Lua_5.2.2.0_alpha_original...master


Contact:

hd2010@eonblast.com



Samples:

See test/len.lua and test/strlen.lua

All these samples work fine, they are taken from the
above files, there are many more.



-- # # # # # # # # # # # # # # # # # # # # # # # # #
Sparce amd Complete Lists
-- # # # # # # # # # # # # # # # # # # # # # # # # #


t = { }
assert(t, 0)

t = { nil, nil }
assert(t, 0)

t = { nil, nil, nil }
assert(t, 0)

t = { nil, 'foo', nil }
assert(t, 1)

t = { nil, nil, 'foo', nil , nil }
assert(t, 1)


t = { nil, nil, 'too', nil, nil, 'bar', nil, nil }
assert(t, 2)


-- # # # # # # # # # # # # # # # # # # # # # # # # #

t = {}
t[1] = 'foo'
t[2] = nil
t[3] = 'bar'
assert(t, 2)


t = {}
t[1] = 'foo'
t[2] = 'too'
t[3] = 'bar'
assert(t, 3)


t = {}
t[1] = nil
t[2] = nil
t[3] = 'bar'
assert(t, 1)


t = {}
t[0] = 'foo'
t[1] = 'too'
t[2] = 'bar'
t[-1] = nil
assert(t, 3)


-- # # # # # # # # # # # # # # # # # # # # # # # # #
UTF-8 String Test Samples
-- # # # # # # # # # # # # # # # # # # # # # # # # #

assert("", 0)
assert(" ", 1)
assert("Q", 1)
assert("Room", 4)
assert("Tür", 3)
assert("Dûnedaín", 8)
assert("Stößel", 6)
assert("Fu∂ark", 6)
assert("«∑€®†Ω", 6)
assert("黃耀賢 (Yau-Hsien Huang)", 21)
@@ -722,7 +722,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) {
api_checknelems(L, 2);
t = index2addr(L, idx);
api_check(L, ttistable(t), "table expected");
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 +*/
luaC_barrierback(L, gcvalue(t), L->top-1);
L->top -= 2;
lua_unlock(L);
@@ -735,7 +735,7 @@ LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
api_checknelems(L, 1);
o = index2addr(L, idx);
api_check(L, ttistable(o), "table expected");
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 +*/
luaC_barrierback(L, gcvalue(o), L->top-1);
L->top--;
lua_unlock(L);
@@ -1089,6 +1089,15 @@ LUA_API void lua_len (lua_State *L, int idx) {
lua_unlock(L);
}

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);
}


LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
lua_Alloc f;
@@ -272,7 +272,7 @@ static void freeexp (FuncState *fs, expdesc *e) {

static int addk (FuncState *fs, TValue *key, TValue *v) {
lua_State *L = fs->L;
TValue *idx = luaH_set(L, fs->h, key);
TValue *idx = luaH_set(L, fs->h, key); /*+ get (unchanged) +*/
Proto *f = fs->f;
int k, oldsize;
if (ttisnumber(idx)) {
@@ -286,7 +286,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
/* constant not found; create a new entry */
oldsize = f->sizek;
k = fs->nk;
setnvalue(idx, cast_num(k));
setnvaluetbl(fs->h, idx, cast_num(k)); /*+ set +*/
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
setobj(L, &f->k[k], v);
@@ -721,7 +721,7 @@ static void codearith (FuncState *fs, OpCode op,
if (constfolding(op, e1, e2))
return;
else {
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;
int o1 = luaK_exp2RK(fs, e1);
if (o1 > o2) {
freeexp(fs, e1);
@@ -773,6 +773,11 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
codearith(fs, OP_LEN, e, &e2, line);
break;
}
case OPR_COUNT: {
luaK_exp2anyreg(fs, e); /* cannot operate on constants */
codearith(fs, OP_COUNT, e, &e2, line);
break;
}
default: lua_assert(0);
}
}
@@ -33,7 +33,7 @@ typedef enum BinOpr {
} BinOpr;


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;


#define getcode(fs,e) ((fs)->f->code[(e)->u.info])
@@ -182,7 +182,7 @@ static void collectvalidlines (lua_State *L, Closure *f) {
sethvalue(L, L->top, t);
incr_top(L);
for (i=0; i<f->l.p->sizelineinfo; i++)
setbvalue(luaH_setint(L, t, lineinfo[i]), 1);
setbvaluetbl(t, luaH_setint(L, t, lineinfo[i]), 1); /*+ get & set +*/
}
}

@@ -411,6 +411,7 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
case OP_POW: tm = TM_POW; break;
case OP_UNM: tm = TM_UNM; break;
case OP_LEN: tm = TM_LEN; break;
case OP_COUNT: tm = TM_COUNT; break;
case OP_LT: tm = TM_LT; break;
case OP_LE: tm = TM_LE; break;
case OP_CONCAT: tm = TM_CONCAT; break;
BIN +512 Bytes (100%) src/liblua.a
Binary file not shown.
@@ -127,9 +127,9 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
TValue *o; /* entry for `str' */
TString *ts = luaS_newlstr(L, str, l); /* create new string */
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
o = luaH_setstr(L, ls->fs->h, ts);
o = luaH_setstr(L, ls->fs->h, ts); /*+ get (unchanged) +*/
if (ttisnil(o)) {
setbvalue(o, 1); /* t[string] = true */
setbvalue(o, 1); /* t[string] = true */ /*+ set (unchanged) +*/
luaC_checkGC(L);
}
L->top--; /* remove string from stack */
@@ -142,6 +142,15 @@ typedef struct lua_TValue {
#define setnvalue(obj,x) \
{ TValue *i_o=(obj); i_o->value_.n=(x); i_o->tt_=LUA_TNUMBER; }

/*+ 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)) \
if(tbl->count++ == (size_t)-1) \
luaG_runerror(L, "table count overrun"); \
oT->value_.n=(x); oT->tt_=LUA_TNUMBER; \
}

#define setfvalue(obj,x) \
{ TValue *i_o=(obj); i_o->value_.f=(x); i_o->tt_=LUA_TLCF; }

@@ -153,6 +162,15 @@ typedef struct lua_TValue {
#define setbvalue(obj,x) \
{ TValue *i_o=(obj); i_o->value_.b=(x); i_o->tt_=LUA_TBOOLEAN; }

/*+ 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)) \
if(tbl->count++ == (size_t)-1) \
luaG_runerror(L, "table count overrun"); \
oT->value_.b=(x); oT->tt_=LUA_TBOOLEAN; \
}

#define setsvalue(L,obj,x) \
{ TValue *i_o=(obj); \
i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TSTRING; \
@@ -192,6 +210,21 @@ typedef struct lua_TValue {
o1->value_ = o2->value_; o1->tt_=o2->tt_; \
checkliveness(G(L),o1); }

/*+ 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)) { \
if(tbl->count++ == (size_t)-1) \
luaG_runerror(L, "table count overrun"); } \
else if(!ttisnil(oT) && ttisnil(oV)) { \
tbl->count--; \
if(tbl->count < 0) \
luaG_runerror(L, "table count underrun"); }\
oT->value_ = oV->value_; oT->tt_=oV->tt_; \
checkliveness(G(L),oT); \
}



/*
** different types of assignments, according to destination
@@ -204,10 +237,12 @@ typedef struct lua_TValue {
#define setsvalue2s setsvalue
#define sethvalue2s sethvalue
#define setptvalue2s setptvalue
/* from table to same table */
/* from table to same table: no change of element count */
#define setobjt2t setobj
/* to table */
#define setobj2t setobj
/*+ to table: potential change of element count +*/
#define setobj2t setobjtbl
/* to table: set key */
#define setobjk2t setobj
/* to new object */
#define setobj2n setobj
#define setsvalue2n setsvalue
@@ -380,6 +415,7 @@ typedef struct Table {
Node *lastfree; /* any free position is before this position */
GCObject *gclist;
int sizearray; /* size of `array' array */
size_t count; /*+ element count +*/
} Table;


@@ -35,6 +35,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
"UNM",
"NOT",
"LEN",
"COUNT",
"CONCAT",
"JMP",
"EQ",
@@ -83,6 +84,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_COUNT */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */
,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
@@ -192,6 +192,7 @@ OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */
OP_UNM,/* A B R(A) := -R(B) */
OP_NOT,/* A B R(A) := not R(B) */
OP_LEN,/* A B R(A) := length of R(B) */
OP_COUNT,/* A B R(A) := count of R(B) */

OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */

@@ -847,6 +847,7 @@ static UnOpr getunopr (int op) {
case TK_NOT: return OPR_NOT;
case '-': return OPR_MINUS;
case '#': return OPR_LEN;
case '%': return OPR_COUNT; /*+ set your preferred, non-standard sign here! +*/
default: return OPR_NOUNOPR;
}
}
@@ -133,10 +133,10 @@ static void init_registry (lua_State *L, global_State *g) {
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
/* registry[LUA_RIDX_MAINTHREAD] = L */
setthvalue(L, &mt, L);
setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt);
setobj2t(L, registry, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt); /*+ get & set +*/
/* registry[LUA_RIDX_GLOBALS] = table of globals */
sethvalue(L, &mt, luaH_new(L));
setobj2t(L, luaH_setint(L, registry, LUA_RIDX_GLOBALS), &mt);
setobj2t(L, registry, luaH_setint(L, registry, LUA_RIDX_GLOBALS), &mt); /*+ get & set +*/
}