diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-02-27 12:59:22 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-02-27 12:59:22 -0300 |
commit | 6eb53b752617fae9e1329bfe2cfecdcbb593c398 (patch) | |
tree | c392ef632bbcfbf7b3716f5c6c17b06617bca8da | |
parent | 9b7987a9d1471ba94764286b28e0998f73deb46a (diff) | |
download | lua-6eb53b752617fae9e1329bfe2cfecdcbb593c398.tar.gz lua-6eb53b752617fae9e1329bfe2cfecdcbb593c398.tar.bz2 lua-6eb53b752617fae9e1329bfe2cfecdcbb593c398.zip |
Details
Several details in code (e.g., moving a variable to the most inner
scope that encloses its uses), comments, parameter names, extra tests.
-rw-r--r-- | lauxlib.c | 2 | ||||
-rw-r--r-- | lcode.c | 15 | ||||
-rw-r--r-- | lcode.h | 2 | ||||
-rw-r--r-- | ldblib.c | 4 | ||||
-rw-r--r-- | ldebug.c | 6 | ||||
-rw-r--r-- | lmathlib.c | 28 | ||||
-rw-r--r-- | ltests.c | 12 | ||||
-rw-r--r-- | ltests.h | 3 | ||||
-rw-r--r-- | lvm.c | 13 | ||||
-rw-r--r-- | testes/db.lua | 5 | ||||
-rw-r--r-- | testes/events.lua | 1 | ||||
-rw-r--r-- | testes/math.lua | 4 | ||||
-rw-r--r-- | testes/nextvar.lua | 2 |
13 files changed, 52 insertions, 45 deletions
@@ -902,10 +902,10 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { | |||
902 | LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { | 902 | LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { |
903 | luaL_checkstack(L, nup, "too many upvalues"); | 903 | luaL_checkstack(L, nup, "too many upvalues"); |
904 | for (; l->name != NULL; l++) { /* fill the table with given functions */ | 904 | for (; l->name != NULL; l++) { /* fill the table with given functions */ |
905 | int i; | ||
906 | if (l->func == NULL) /* place holder? */ | 905 | if (l->func == NULL) /* place holder? */ |
907 | lua_pushboolean(L, 0); | 906 | lua_pushboolean(L, 0); |
908 | else { | 907 | else { |
908 | int i; | ||
909 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ | 909 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ |
910 | lua_pushvalue(L, -nup); | 910 | lua_pushvalue(L, -nup); |
911 | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ | 911 | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ |
@@ -1730,17 +1730,12 @@ void luaK_fixline (FuncState *fs, int line) { | |||
1730 | } | 1730 | } |
1731 | 1731 | ||
1732 | 1732 | ||
1733 | void luaK_settablesize (FuncState *fs, int pc, int ra, int rc, int rb) { | 1733 | void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { |
1734 | Instruction *inst = &fs->f->code[pc]; | 1734 | Instruction *inst = &fs->f->code[pc]; |
1735 | int extra = 0; | 1735 | int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */ |
1736 | int k = 0; | 1736 | int extra = asize / (MAXARG_C + 1); /* higher bits of array size */ |
1737 | if (rb != 0) | 1737 | int rc = asize % (MAXARG_C + 1); /* lower bits of array size */ |
1738 | rb = luaO_ceillog2(rb) + 1; /* hash size */ | 1738 | int k = (extra > 0); /* true iff needs extra argument */ |
1739 | if (rc > MAXARG_C) { /* does it need the extra argument? */ | ||
1740 | extra = rc / (MAXARG_C + 1); | ||
1741 | rc %= (MAXARG_C + 1); | ||
1742 | k = 1; | ||
1743 | } | ||
1744 | *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k); | 1739 | *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k); |
1745 | *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); | 1740 | *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); |
1746 | } | 1741 | } |
@@ -95,7 +95,7 @@ LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); | |||
95 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, | 95 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, |
96 | expdesc *v2, int line); | 96 | expdesc *v2, int line); |
97 | LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, | 97 | LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, |
98 | int ra, int rb, int rc); | 98 | int ra, int asize, int hsize); |
99 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); | 99 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); |
100 | LUAI_FUNC void luaK_finish (FuncState *fs); | 100 | LUAI_FUNC void luaK_finish (FuncState *fs); |
101 | LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); | 101 | LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); |
@@ -202,8 +202,6 @@ static int db_getinfo (lua_State *L) { | |||
202 | static int db_getlocal (lua_State *L) { | 202 | static int db_getlocal (lua_State *L) { |
203 | int arg; | 203 | int arg; |
204 | lua_State *L1 = getthread(L, &arg); | 204 | lua_State *L1 = getthread(L, &arg); |
205 | lua_Debug ar; | ||
206 | const char *name; | ||
207 | int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ | 205 | int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ |
208 | if (lua_isfunction(L, arg + 1)) { /* function argument? */ | 206 | if (lua_isfunction(L, arg + 1)) { /* function argument? */ |
209 | lua_pushvalue(L, arg + 1); /* push function */ | 207 | lua_pushvalue(L, arg + 1); /* push function */ |
@@ -211,6 +209,8 @@ static int db_getlocal (lua_State *L) { | |||
211 | return 1; /* return only name (there is no value) */ | 209 | return 1; /* return only name (there is no value) */ |
212 | } | 210 | } |
213 | else { /* stack-level argument */ | 211 | else { /* stack-level argument */ |
212 | lua_Debug ar; | ||
213 | const char *name; | ||
214 | int level = (int)luaL_checkinteger(L, arg + 1); | 214 | int level = (int)luaL_checkinteger(L, arg + 1); |
215 | if (!lua_getstack(L1, level, &ar)) /* out of range? */ | 215 | if (!lua_getstack(L1, level, &ar)) /* out of range? */ |
216 | return luaL_argerror(L, arg+1, "level out of range"); | 216 | return luaL_argerror(L, arg+1, "level out of range"); |
@@ -101,7 +101,7 @@ int luaG_getfuncline (const Proto *f, int pc) { | |||
101 | } | 101 | } |
102 | 102 | ||
103 | 103 | ||
104 | static int currentline (CallInfo *ci) { | 104 | static int getcurrentline (CallInfo *ci) { |
105 | return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); | 105 | return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); |
106 | } | 106 | } |
107 | 107 | ||
@@ -339,7 +339,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, | |||
339 | break; | 339 | break; |
340 | } | 340 | } |
341 | case 'l': { | 341 | case 'l': { |
342 | ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; | 342 | ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1; |
343 | break; | 343 | break; |
344 | } | 344 | } |
345 | case 'u': { | 345 | case 'u': { |
@@ -775,7 +775,7 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { | |||
775 | msg = luaO_pushvfstring(L, fmt, argp); /* format message */ | 775 | msg = luaO_pushvfstring(L, fmt, argp); /* format message */ |
776 | va_end(argp); | 776 | va_end(argp); |
777 | if (isLua(ci)) /* if Lua function, add source:line information */ | 777 | if (isLua(ci)) /* if Lua function, add source:line information */ |
778 | luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); | 778 | luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); |
779 | luaG_errormsg(L); | 779 | luaG_errormsg(L); |
780 | } | 780 | } |
781 | 781 | ||
@@ -522,16 +522,18 @@ typedef struct { | |||
522 | ** Project the random integer 'ran' into the interval [0, n]. | 522 | ** Project the random integer 'ran' into the interval [0, n]. |
523 | ** Because 'ran' has 2^B possible values, the projection can only be | 523 | ** Because 'ran' has 2^B possible values, the projection can only be |
524 | ** uniform when the size of the interval is a power of 2 (exact | 524 | ** uniform when the size of the interval is a power of 2 (exact |
525 | ** division). To get a uniform projection into [0, n], we first compute | 525 | ** division). Otherwise, to get a uniform projection into [0, n], we |
526 | ** 'lim', the smallest Mersenne number not smaller than 'n'. We then | 526 | ** first compute 'lim', the smallest Mersenne number not smaller than |
527 | ** project 'ran' into the interval [0, lim]. If the result is inside | 527 | ** 'n'. We then project 'ran' into the interval [0, lim]. If the result |
528 | ** [0, n], we are done. Otherwise, we try with another 'ran', until we | 528 | ** is inside [0, n], we are done. Otherwise, we try with another 'ran', |
529 | ** have a result inside the interval. | 529 | ** until we have a result inside the interval. |
530 | */ | 530 | */ |
531 | static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, | 531 | static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, |
532 | RanState *state) { | 532 | RanState *state) { |
533 | lua_Unsigned lim = n; | 533 | if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */ |
534 | if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */ | 534 | return ran & n; /* no bias */ |
535 | else { | ||
536 | lua_Unsigned lim = n; | ||
535 | /* compute the smallest (2^b - 1) not smaller than 'n' */ | 537 | /* compute the smallest (2^b - 1) not smaller than 'n' */ |
536 | lim |= (lim >> 1); | 538 | lim |= (lim >> 1); |
537 | lim |= (lim >> 2); | 539 | lim |= (lim >> 2); |
@@ -541,13 +543,13 @@ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, | |||
541 | #if (LUA_MAXUNSIGNED >> 31) >= 3 | 543 | #if (LUA_MAXUNSIGNED >> 31) >= 3 |
542 | lim |= (lim >> 32); /* integer type has more than 32 bits */ | 544 | lim |= (lim >> 32); /* integer type has more than 32 bits */ |
543 | #endif | 545 | #endif |
546 | lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */ | ||
547 | && lim >= n /* not smaller than 'n', */ | ||
548 | && (lim >> 1) < n); /* and it is the smallest one */ | ||
549 | while ((ran &= lim) > n) /* project 'ran' into [0..lim] */ | ||
550 | ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ | ||
551 | return ran; | ||
544 | } | 552 | } |
545 | lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */ | ||
546 | && lim >= n /* not smaller than 'n', */ | ||
547 | && (lim == 0 || (lim >> 1) < n)); /* and it is the smallest one */ | ||
548 | while ((ran &= lim) > n) /* project 'ran' into [0..lim] */ | ||
549 | ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ | ||
550 | return ran; | ||
551 | } | 553 | } |
552 | 554 | ||
553 | 555 | ||
@@ -419,17 +419,19 @@ static void checkstack (global_State *g, lua_State *L1) { | |||
419 | CallInfo *ci; | 419 | CallInfo *ci; |
420 | UpVal *uv; | 420 | UpVal *uv; |
421 | lua_assert(!isdead(g, L1)); | 421 | lua_assert(!isdead(g, L1)); |
422 | if (L1->stack == NULL) { /* incomplete thread? */ | ||
423 | lua_assert(L1->stacksize == 0 && L1->openupval == NULL && | ||
424 | L1->ci == NULL); | ||
425 | return; | ||
426 | } | ||
422 | for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) | 427 | for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) |
423 | lua_assert(upisopen(uv)); /* must be open */ | 428 | lua_assert(upisopen(uv)); /* must be open */ |
424 | for (ci = L1->ci; ci != NULL; ci = ci->previous) { | 429 | for (ci = L1->ci; ci != NULL; ci = ci->previous) { |
425 | lua_assert(ci->top <= L1->stack_last); | 430 | lua_assert(ci->top <= L1->stack_last); |
426 | lua_assert(lua_checkpc(ci)); | 431 | lua_assert(lua_checkpc(ci)); |
427 | } | 432 | } |
428 | if (L1->stack) { /* complete thread? */ | 433 | for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) |
429 | for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) | 434 | checkliveness(L1, s2v(o)); /* entire stack must have valid values */ |
430 | checkliveness(L1, s2v(o)); /* entire stack must have valid values */ | ||
431 | } | ||
432 | else lua_assert(L1->stacksize == 0); | ||
433 | } | 435 | } |
434 | 436 | ||
435 | 437 | ||
@@ -25,9 +25,6 @@ | |||
25 | #define lua_assert(c) assert(c) | 25 | #define lua_assert(c) assert(c) |
26 | 26 | ||
27 | 27 | ||
28 | /* include opcode names */ | ||
29 | #define LUAI_DEFOPNAMES | ||
30 | |||
31 | 28 | ||
32 | /* compiled with -O0, Lua uses a lot of C stack space... */ | 29 | /* compiled with -O0, Lua uses a lot of C stack space... */ |
33 | #undef LUAI_MAXCSTACK | 30 | #undef LUAI_MAXCSTACK |
@@ -980,11 +980,11 @@ void luaV_finishOp (lua_State *L) { | |||
980 | 980 | ||
981 | 981 | ||
982 | /* | 982 | /* |
983 | ** Order operations with register operands. 'opf' actually works | 983 | ** Order operations with register operands. 'opn' actually works |
984 | ** for all numbers, but the fast track improves performance for | 984 | ** for all numbers, but the fast track improves performance for |
985 | ** integers. | 985 | ** integers. |
986 | */ | 986 | */ |
987 | #define op_order(L,opi,opf,other) { \ | 987 | #define op_order(L,opi,opn,other) { \ |
988 | int cond; \ | 988 | int cond; \ |
989 | TValue *rb = vRB(i); \ | 989 | TValue *rb = vRB(i); \ |
990 | if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ | 990 | if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ |
@@ -993,7 +993,7 @@ void luaV_finishOp (lua_State *L) { | |||
993 | cond = opi(ia, ib); \ | 993 | cond = opi(ia, ib); \ |
994 | } \ | 994 | } \ |
995 | else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ | 995 | else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ |
996 | cond = opf(s2v(ra), rb); \ | 996 | cond = opn(s2v(ra), rb); \ |
997 | else \ | 997 | else \ |
998 | Protect(cond = other(L, s2v(ra), rb)); \ | 998 | Protect(cond = other(L, s2v(ra), rb)); \ |
999 | docondjump(); } | 999 | docondjump(); } |
@@ -1323,8 +1323,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { | |||
1323 | Table *t; | 1323 | Table *t; |
1324 | if (b > 0) | 1324 | if (b > 0) |
1325 | b = 1 << (b - 1); /* size is 2^(b - 1) */ | 1325 | b = 1 << (b - 1); /* size is 2^(b - 1) */ |
1326 | if (TESTARG_k(i)) | 1326 | lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0)); |
1327 | c += GETARG_Ax(*pc) * (MAXARG_C + 1); | 1327 | if (TESTARG_k(i)) /* non-zero extra argument? */ |
1328 | c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */ | ||
1328 | pc++; /* skip extra argument */ | 1329 | pc++; /* skip extra argument */ |
1329 | L->top = ra + 1; /* correct top in case of emergency GC */ | 1330 | L->top = ra + 1; /* correct top in case of emergency GC */ |
1330 | t = luaH_new(L); /* memory allocation */ | 1331 | t = luaH_new(L); /* memory allocation */ |
@@ -1558,7 +1559,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { | |||
1558 | vmcase(OP_EQK) { | 1559 | vmcase(OP_EQK) { |
1559 | TValue *rb = KB(i); | 1560 | TValue *rb = KB(i); |
1560 | /* basic types do not use '__eq'; we can use raw equality */ | 1561 | /* basic types do not use '__eq'; we can use raw equality */ |
1561 | int cond = luaV_equalobj(NULL, s2v(ra), rb); | 1562 | int cond = luaV_rawequalobj(s2v(ra), rb); |
1562 | docondjump(); | 1563 | docondjump(); |
1563 | vmbreak; | 1564 | vmbreak; |
1564 | } | 1565 | } |
diff --git a/testes/db.lua b/testes/db.lua index 074a6d0b..941283f7 100644 --- a/testes/db.lua +++ b/testes/db.lua | |||
@@ -649,6 +649,11 @@ t = debug.getinfo(1) -- main | |||
649 | assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and | 649 | assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and |
650 | debug.getupvalue(t.func, 1) == "_ENV") | 650 | debug.getupvalue(t.func, 1) == "_ENV") |
651 | 651 | ||
652 | t = debug.getinfo(math.sin) -- C function | ||
653 | assert(t.isvararg == true and t.nparams == 0 and t.nups == 0) | ||
654 | |||
655 | t = debug.getinfo(string.gmatch("abc", "a")) -- C closure | ||
656 | assert(t.isvararg == true and t.nparams == 0 and t.nups > 0) | ||
652 | 657 | ||
653 | 658 | ||
654 | 659 | ||
diff --git a/testes/events.lua b/testes/events.lua index d0abe1d4..8a01330e 100644 --- a/testes/events.lua +++ b/testes/events.lua | |||
@@ -325,6 +325,7 @@ else | |||
325 | assert(u1 == u3 and u3 == u1 and u1 ~= u2) | 325 | assert(u1 == u3 and u3 == u1 and u1 ~= u2) |
326 | assert(u2 == u1 and u2 == u3 and u3 == u2) | 326 | assert(u2 == u1 and u2 == u3 and u3 == u2) |
327 | assert(u2 ~= {}) -- different types cannot be equal | 327 | assert(u2 ~= {}) -- different types cannot be equal |
328 | assert(rawequal(u1, u1) and not rawequal(u1, u3)) | ||
328 | 329 | ||
329 | local mirror = {} | 330 | local mirror = {} |
330 | debug.setmetatable(u3, {__index = mirror, __newindex = mirror}) | 331 | debug.setmetatable(u3, {__index = mirror, __newindex = mirror}) |
diff --git a/testes/math.lua b/testes/math.lua index 7248787b..930221e3 100644 --- a/testes/math.lua +++ b/testes/math.lua | |||
@@ -960,7 +960,10 @@ do | |||
960 | aux(-10,0) | 960 | aux(-10,0) |
961 | aux(1, 6) | 961 | aux(1, 6) |
962 | aux(1, 2) | 962 | aux(1, 2) |
963 | aux(1, 13) | ||
964 | aux(1, 31) | ||
963 | aux(1, 32) | 965 | aux(1, 32) |
966 | aux(1, 33) | ||
964 | aux(-10, 10) | 967 | aux(-10, 10) |
965 | aux(-10,-10) -- unit set | 968 | aux(-10,-10) -- unit set |
966 | aux(minint, minint) -- unit set | 969 | aux(minint, minint) -- unit set |
@@ -998,6 +1001,7 @@ do | |||
998 | end | 1001 | end |
999 | aux(0, maxint) | 1002 | aux(0, maxint) |
1000 | aux(1, maxint) | 1003 | aux(1, maxint) |
1004 | aux(3, maxint // 3) | ||
1001 | aux(minint, -1) | 1005 | aux(minint, -1) |
1002 | aux(minint // 2, maxint // 2) | 1006 | aux(minint // 2, maxint // 2) |
1003 | aux(minint, maxint) | 1007 | aux(minint, maxint) |
diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 9d919631..73af77dd 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua | |||
@@ -76,7 +76,7 @@ end | |||
76 | 76 | ||
77 | -- testing constructor sizes | 77 | -- testing constructor sizes |
78 | local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, | 78 | local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, |
79 | 30, 31, 32, 33, 34, 500, 1000} | 79 | 30, 31, 32, 33, 34, 254, 255, 256, 500, 1000} |
80 | 80 | ||
81 | for _, sa in ipairs(sizes) do -- 'sa' is size of the array part | 81 | for _, sa in ipairs(sizes) do -- 'sa' is size of the array part |
82 | local arr = {"return {"} | 82 | local arr = {"return {"} |