3 @@ -25,7 +25,7 @@ PLATS= aix ansi bsd freebsd generic linu
5 CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
6 lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
7 - lundump.o lvm.o lzio.o
8 + lundump.o lvm.o lzio.o lnum.o
9 LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
10 lstrlib.o loadlib.o linit.o
12 @@ -148,6 +148,7 @@ llex.o: llex.c lua.h luaconf.h ldo.h lob
13 lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
14 lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
15 ltm.h lzio.h lmem.h ldo.h
16 +lnum.o: lnum.c lua.h llex.h lnum.h
17 loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
18 lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \
19 ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h
20 @@ -179,4 +180,18 @@ lzio.o: lzio.c lua.h luaconf.h llimits.h
21 print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \
22 ltm.h lzio.h lmem.h lopcodes.h lundump.h
24 +luaconf.h: lnum_config.h
27 +lbaselib.c: llimits.h lobject.h lapi.h
29 +liolib.c: lnum.h llex.h
32 +lobject.c: llex.h lnum.h
49 const char lua_ident[] =
50 @@ -241,12 +241,13 @@ LUA_API void lua_pushvalue (lua_State *L
52 LUA_API int lua_type (lua_State *L, int idx) {
53 StkId o = index2adr(L, idx);
54 - return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
55 + return (o == luaO_nilobject) ? LUA_TNONE : ttype_ext(o);
59 LUA_API const char *lua_typename (lua_State *L, int t) {
61 + lua_assert( t!= LUA_TINT );
62 return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
65 @@ -264,6 +265,14 @@ LUA_API int lua_isnumber (lua_State *L,
69 +LUA_API int lua_isinteger (lua_State *L, int idx) {
72 + const TValue *o = index2adr(L, idx);
73 + return tonumber(o,&tmp) && (ttisint(o) || tt_integer_valued(o,&dum));
77 LUA_API int lua_isstring (lua_State *L, int idx) {
78 int t = lua_type(L, idx);
79 return (t == LUA_TSTRING || t == LUA_TNUMBER);
80 @@ -309,31 +318,66 @@ LUA_API int lua_lessthan (lua_State *L,
85 LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
87 const TValue *o = index2adr(L, idx);
88 - if (tonumber(o, &n))
89 + if (tonumber(o, &n)) {
91 + if (nvalue_img(o) != 0)
92 + luaG_runerror(L, "expecting a real number");
102 LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
104 + /* Lua 5.1 documented behaviour is to return nonzero for non-integer:
105 + * "If the number is not an integer, it is truncated in some non-specified way."
106 + * I would suggest to change this, to return 0 for anything that would
107 + * not fit in 'lua_Integer'.
109 +#ifdef LUA_COMPAT_TOINTEGER
110 + /* Lua 5.1 compatible */
111 const TValue *o = index2adr(L, idx);
112 if (tonumber(o, &n)) {
114 - lua_Number num = nvalue(o);
115 - lua_number2integer(res, num);
119 + if (ttisint(o)) return ivalue(o);
121 +# ifdef LNUM_COMPLEX
122 + if (nvalue_img_fast(o) != 0)
123 + luaG_runerror(L, "expecting a real number");
125 + lua_number2integer(i, d);
131 + /* New suggestion */
132 + const TValue *o = index2adr(L, idx);
133 + if (tonumber(o, &n)) {
135 + if (ttisint(o)) return ivalue(o);
136 + if (tt_integer_valued(o,&i)) return i;
144 +LUA_API lua_Complex lua_tocomplex (lua_State *L, int idx) {
146 + const TValue *o = index2adr(L, idx);
147 + if (tonumber(o, &tmp))
148 + return nvalue_complex(o);
154 LUA_API int lua_toboolean (lua_State *L, int idx) {
155 const TValue *o = index2adr(L, idx);
156 return !l_isfalse(o);
157 @@ -364,6 +408,7 @@ LUA_API size_t lua_objlen (lua_State *L,
158 case LUA_TSTRING: return tsvalue(o)->len;
159 case LUA_TUSERDATA: return uvalue(o)->len;
160 case LUA_TTABLE: return luaH_getn(hvalue(o));
164 lua_lock(L); /* `luaV_tostring' may create a new string */
165 @@ -426,6 +471,8 @@ LUA_API void lua_pushnil (lua_State *L)
169 +/* 'lua_pushnumber()' may lose accuracy on integers, 'lua_pushinteger' will not.
171 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
173 setnvalue(L->top, n);
174 @@ -434,12 +481,22 @@ LUA_API void lua_pushnumber (lua_State *
178 -LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
179 +LUA_API void lua_pushinteger (lua_State *L, lua_Integer i) {
181 + setivalue(L->top, i);
188 +LUA_API void lua_pushcomplex (lua_State *L, lua_Complex v) {
190 - setnvalue(L->top, cast_num(n));
191 + setnvalue_complex( L->top, v );
198 LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
199 @@ -569,7 +626,7 @@ LUA_API void lua_rawgeti (lua_State *L,
201 o = index2adr(L, idx);
202 api_check(L, ttistable(o));
203 - setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
204 + setobj2s(L, L->top, luaH_getint(hvalue(o), n));
208 @@ -597,6 +654,9 @@ LUA_API int lua_getmetatable (lua_State
210 mt = uvalue(obj)->metatable;
213 + mt = G(L)->mt[LUA_TNUMBER];
216 mt = G(L)->mt[ttype(obj)];
218 @@ -687,7 +747,7 @@ LUA_API void lua_rawseti (lua_State *L,
219 api_checknelems(L, 1);
220 o = index2adr(L, idx);
221 api_check(L, ttistable(o));
222 - setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
223 + setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
224 luaC_barriert(L, hvalue(o), L->top-1);
227 @@ -721,7 +781,7 @@ LUA_API int lua_setmetatable (lua_State
231 - G(L)->mt[ttype(obj)] = mt;
232 + G(L)->mt[ttype_ext(obj)] = mt;
236 @@ -1085,3 +1145,32 @@ LUA_API const char *lua_setupvalue (lua_
241 +/* Help function for 'luaB_tonumber()', avoids multiple str->number
242 + * conversions for Lua "tonumber()".
244 + * Also pushes floating point numbers with integer value as integer, which
245 + * can be used by 'tonumber()' in scripts to bring values back to integer
248 + * Note: The 'back to integer realm' is _not_ to affect string conversions:
249 + * 'tonumber("4294967295.1")' should give a floating point value, although
250 + * the value would be 4294967296 (and storable in int64 realm).
252 +int lua_pushvalue_as_number (lua_State *L, int idx)
254 + const TValue *o = index2adr(L, idx);
257 + if (ttisnumber(o)) {
258 + if ( (!ttisint(o)) && tt_integer_valued(o,&i)) {
259 + lua_pushinteger( L, i );
262 + } else if (!tonumber(o, &tmp)) {
265 + if (ttisint(o)) lua_pushinteger( L, ivalue(o) );
266 + else lua_pushnumber( L, nvalue_fast(o) );
273 LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);
275 +int lua_pushvalue_as_number (lua_State *L, int idx);
285 +#include "llimits.h"
287 #define FREELIST_REF 0 /* free list of references */
289 @@ -66,7 +66,7 @@ LUALIB_API int luaL_typerror (lua_State
292 static void tag_error (lua_State *L, int narg, int tag) {
293 - luaL_typerror(L, narg, lua_typename(L, tag));
294 + luaL_typerror(L, narg, tag==LUA_TINT ? "integer" : lua_typename(L, tag));
298 @@ -188,8 +188,8 @@ LUALIB_API lua_Number luaL_optnumber (lu
300 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
301 lua_Integer d = lua_tointeger(L, narg);
302 - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
303 - tag_error(L, narg, LUA_TNUMBER);
304 + if (d == 0 && !lua_isinteger(L, narg)) /* avoid extra test when d is not 0 */
305 + tag_error(L, narg, LUA_TINT);
309 @@ -200,6 +200,16 @@ LUALIB_API lua_Integer luaL_optinteger (
314 +LUALIB_API lua_Complex luaL_checkcomplex (lua_State *L, int narg) {
315 + lua_Complex c = lua_tocomplex(L, narg);
316 + if (c == 0 && !lua_isnumber(L, narg)) /* avoid extra test when c is not 0 */
317 + tag_error(L, narg, LUA_TNUMBER);
323 LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
324 if (!lua_getmetatable(L, obj)) /* no metatable? */
328 @@ -57,6 +57,12 @@ LUALIB_API lua_Number (luaL_optnumber) (
329 LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
330 LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
332 +#define luaL_checkint32(L,narg) ((int)luaL_checkinteger(L,narg))
333 +#define luaL_optint32(L,narg,def) ((int)luaL_optinteger(L,narg,def))
336 + LUALIB_API lua_Complex (luaL_checkcomplex) (lua_State *L, int narg);
339 LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
340 LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
348 +#include "llimits.h"
349 +#include "lobject.h"
354 @@ -54,20 +56,25 @@ static int luaB_tonumber (lua_State *L)
355 int base = luaL_optint(L, 2, 10);
356 if (base == 10) { /* standard conversion */
358 - if (lua_isnumber(L, 1)) {
359 - lua_pushnumber(L, lua_tonumber(L, 1));
360 + if (lua_isnumber(L, 1)) { /* numeric string, or a number */
361 + lua_pushvalue_as_number(L,1); /* API extension (not to lose accuracy here) */
367 const char *s1 = luaL_checkstring(L, 1);
370 + unsigned LUA_INTEGER n;
371 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
372 - n = strtoul(s1, &s2, base);
373 + n = lua_str2ul(s1, &s2, base);
374 if (s1 != s2) { /* at least one valid digit? */
375 while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
376 if (*s2 == '\0') { /* no invalid trailing characters? */
378 + /* Push as number, there needs to be separate 'luaB_tointeger' for
379 + * when the caller wants to preserve the bits (matters if unsigned
380 + * values are used).
382 lua_pushnumber(L, (lua_Number)n);
385 @@ -144,7 +151,7 @@ static int luaB_setfenv (lua_State *L) {
386 luaL_checktype(L, 2, LUA_TTABLE);
389 - if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
390 + if (lua_isnumber(L, 1) && lua_tointeger(L, 1) == 0) {
391 /* change environment of current thread */
394 @@ -209,7 +216,7 @@ static int luaB_collectgarbage (lua_Stat
398 - lua_pushnumber(L, res);
399 + lua_pushinteger(L, res);
403 @@ -631,6 +638,8 @@ static void base_open (lua_State *L) {
404 luaL_register(L, "_G", base_funcs);
405 lua_pushliteral(L, LUA_VERSION);
406 lua_setglobal(L, "_VERSION"); /* set global _VERSION */
407 + lua_pushliteral(L, LUA_LNUM);
408 + lua_setglobal(L, "_LNUM"); /* "[complex] double|float|ldouble int32|int64" */
409 /* `ipairs' and `pairs' need auxliliary functions as upvalues */
410 auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
411 auxopen(L, "pairs", luaB_pairs, luaB_next);
415 #include "lopcodes.h"
421 #define hasjumps(e) ((e)->t != (e)->f)
424 static int isnumeral(expdesc *e) {
425 - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
428 + (e->k == VKNUM2) ||
430 + (e->k == VKINT) || (e->k == VKNUM);
431 + return (ek && e->t == NO_JUMP && e->f == NO_JUMP);
435 @@ -231,12 +236,16 @@ static int addk (FuncState *fs, TValue *
436 TValue *idx = luaH_set(L, fs->h, k);
438 int oldsize = f->sizek;
439 - if (ttisnumber(idx)) {
440 - lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
441 - return cast_int(nvalue(idx));
442 + if (ttype(idx)==LUA_TNUMBER) {
443 + luai_normalize(idx);
444 + lua_assert( ttype(idx)==LUA_TINT ); /* had no fraction */
446 + if (ttisint(idx)) {
447 + lua_assert(luaO_rawequalObj(&fs->f->k[ivalue(idx)], v));
448 + return cast_int(ivalue(idx));
450 else { /* constant not found; create a new entry */
451 - setnvalue(idx, cast_num(fs->nk));
452 + setivalue(idx, fs->nk);
453 luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
454 MAXARG_Bx, "constant table overflow");
455 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
456 @@ -261,6 +270,21 @@ int luaK_numberK (FuncState *fs, lua_Num
460 +int luaK_integerK (FuncState *fs, lua_Integer r) {
463 + return addk(fs, &o, &o);
468 +static int luaK_imagK (FuncState *fs, lua_Number r) {
470 + setnvalue_complex(&o, r*I);
471 + return addk(fs, &o, &o);
475 static int boolK (FuncState *fs, int b) {
478 @@ -359,6 +383,16 @@ static void discharge2reg (FuncState *fs
479 luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
483 + luaK_codeABx(fs, OP_LOADK, reg, luaK_integerK(fs, e->u.ival));
488 + luaK_codeABx(fs, OP_LOADK, reg, luaK_imagK(fs, e->u.nval));
493 Instruction *pc = &getcode(fs, e);
495 @@ -444,6 +478,10 @@ void luaK_exp2val (FuncState *fs, expdes
496 int luaK_exp2RK (FuncState *fs, expdesc *e) {
506 @@ -451,6 +489,10 @@ int luaK_exp2RK (FuncState *fs, expdesc
507 if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
508 e->u.s.info = (e->k == VNIL) ? nilK(fs) :
509 (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
510 + (e->k == VKINT) ? luaK_integerK(fs, e->u.ival) :
512 + (e->k == VKNUM2) ? luaK_imagK(fs, e->u.nval) :
514 boolK(fs, (e->k == VTRUE));
516 return RKASK(e->u.s.info);
517 @@ -540,7 +582,10 @@ void luaK_goiftrue (FuncState *fs, expde
518 int pc; /* pc of last jump */
519 luaK_dischargevars(fs, e);
521 - case VK: case VKNUM: case VTRUE: {
525 + case VKINT: case VK: case VKNUM: case VTRUE: {
526 pc = NO_JUMP; /* always true; do nothing */
529 @@ -598,7 +643,10 @@ static void codenot (FuncState *fs, expd
533 - case VK: case VKNUM: case VTRUE: {
537 + case VKINT: case VK: case VKNUM: case VTRUE: {
541 @@ -634,25 +682,70 @@ void luaK_indexed (FuncState *fs, expdes
543 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
544 lua_Number v1, v2, r;
546 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
550 + /* real and imaginary parts don't mix. */
552 + if (e1->k == VKNUM2) {
553 + if ((op != OP_UNM) && (e2->k != VKNUM2)) return 0;
555 + else if (e2->k == VKNUM2) { return 0; }
557 + if ((e1->k == VKINT) && (e2->k == VKINT)) {
558 + lua_Integer i1= e1->u.ival, i2= e2->u.ival;
561 + /* Integer/integer calculations (may end up producing floating point) */
563 + case OP_ADD: done= try_addint( &rr, i1, i2 ); break;
564 + case OP_SUB: done= try_subint( &rr, i1, i2 ); break;
565 + case OP_MUL: done= try_mulint( &rr, i1, i2 ); break;
566 + case OP_DIV: done= try_divint( &rr, i1, i2 ); break;
567 + case OP_MOD: done= try_modint( &rr, i1, i2 ); break;
568 + case OP_POW: done= try_powint( &rr, i1, i2 ); break;
569 + case OP_UNM: done= try_unmint( &rr, i1 ); break;
570 + default: done= 0; break;
573 + e1->u.ival = rr; /* remained within integer range */
577 + v1 = (e1->k == VKINT) ? ((lua_Number)e1->u.ival) : e1->u.nval;
578 + v2 = (e2->k == VKINT) ? ((lua_Number)e2->u.ival) : e2->u.nval;
581 case OP_ADD: r = luai_numadd(v1, v2); break;
582 case OP_SUB: r = luai_numsub(v1, v2); break;
583 - case OP_MUL: r = luai_nummul(v1, v2); break;
586 + if (vkres==VKNUM2) return 0; /* leave to runtime (could do here, but not worth it?) */
588 + r = luai_nummul(v1, v2); break;
590 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
591 - r = luai_numdiv(v1, v2); break;
593 + if (vkres==VKNUM2) return 0; /* leave to runtime */
595 + r = luai_numdiv(v1, v2); break;
597 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
599 + if (vkres==VKNUM2) return 0; /* leave to runtime */
601 r = luai_nummod(v1, v2); break;
602 - case OP_POW: r = luai_numpow(v1, v2); break;
605 + if (vkres==VKNUM2) return 0; /* leave to runtime */
607 + r = luai_numpow(v1, v2); break;
608 case OP_UNM: r = luai_numunm(v1); break;
609 case OP_LEN: return 0; /* no constant folding for 'len' */
610 default: lua_assert(0); r = 0; break;
612 if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
613 + e1->k = cast(expkind,vkres);
617 @@ -696,7 +789,8 @@ static void codecomp (FuncState *fs, OpC
619 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
621 - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
622 + e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
629 @@ -71,6 +71,6 @@ LUAI_FUNC void luaK_prefix (FuncState *f
630 LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
631 LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
632 LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
634 +LUAI_FUNC int luaK_integerK (FuncState *fs, lua_Integer r);
639 @@ -183,7 +183,7 @@ static void collectvalidlines (lua_State
640 int *lineinfo = f->l.p->lineinfo;
642 for (i=0; i<f->l.p->sizelineinfo; i++)
643 - setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
644 + setbvalue(luaH_setint(L, t, lineinfo[i]), 1);
645 sethvalue(L, L->top, t);
648 @@ -566,7 +566,7 @@ static int isinstack (CallInfo *ci, cons
650 void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
651 const char *name = NULL;
652 - const char *t = luaT_typenames[ttype(o)];
653 + const char *t = luaT_typenames[ttype_ext(o)];
654 const char *kind = (isinstack(L->ci, o)) ?
655 getobjname(L, L->ci, cast_int(o - L->base), &name) :
657 @@ -594,8 +594,8 @@ void luaG_aritherror (lua_State *L, cons
660 int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
661 - const char *t1 = luaT_typenames[ttype(p1)];
662 - const char *t2 = luaT_typenames[ttype(p2)];
663 + const char *t1 = luaT_typenames[ttype_ext(p1)];
664 + const char *t2 = luaT_typenames[ttype_ext(p2)];
666 luaG_runerror(L, "attempt to compare two %s values", t1);
670 @@ -219,9 +219,9 @@ static StkId adjust_varargs (lua_State *
672 htab = luaH_new(L, nvar, 1); /* create `arg' table */
673 for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
674 - setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
675 + setobj2n(L, luaH_setint(L, htab, i+1), L->top - nvar + i);
676 /* store counter in field `n' */
677 - setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
678 + setivalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), nvar);
681 /* move fixed parameters to final position */
684 @@ -52,6 +52,11 @@ static void DumpNumber(lua_Number x, Dum
688 +static void DumpInteger(lua_Integer x, DumpState* D)
693 static void DumpVector(const void* b, int n, size_t size, DumpState* D)
696 @@ -93,8 +98,11 @@ static void DumpConstants(const Proto* f
697 DumpChar(bvalue(o),D);
700 - DumpNumber(nvalue(o),D);
701 + DumpNumber(nvalue_fast(o),D);
704 + DumpInteger(ivalue(o),D);
707 DumpString(rawtsvalue(o),D);
729 @@ -269,6 +271,13 @@ static int io_lines (lua_State *L) {
730 ** =======================================================
734 +* Many problems if we intend the same 'n' format specifier (see 'file:read()')
735 +* to work for both FP and integer numbers, without losing their accuracy. So
736 +* we don't. 'n' reads numbers as floating points, 'i' as integers. Old code
737 +* remains valid, but won't provide full integer accuracy (this only matters
738 +* with float FP and/or 64-bit integers).
741 static int read_number (lua_State *L, FILE *f) {
743 @@ -279,6 +288,43 @@ static int read_number (lua_State *L, FI
744 else return 0; /* read fails */
747 +static int read_integer (lua_State *L, FILE *f) {
749 + if (fscanf(f, LUA_INTEGER_SCAN, &i) == 1) {
750 + lua_pushinteger(L, i);
753 + else return 0; /* read fails */
757 +static int read_complex (lua_State *L, FILE *f) {
758 + /* NNN / NNNi / NNN+MMMi / NNN-MMMi */
760 + if (fscanf(f, LUA_NUMBER_SCAN, &a) == 1) {
764 + lua_pushcomplex(L, a*I);
768 + /* "i" is consumed if at the end; just 'NNN+MMM' will most likely
769 + * behave as if "i" was there? (TBD: test)
771 + if (fscanf(f, LUA_NUMBER_SCAN "i", &b) == 1) {
772 + lua_pushcomplex(L, a+ (c=='+' ? b:-b)*I);
777 + lua_pushnumber(L,a); /*real part only*/
780 + return 0; /* read fails */
785 static int test_eof (lua_State *L, FILE *f) {
787 @@ -352,6 +398,14 @@ static int g_read (lua_State *L, FILE *f
788 case 'n': /* number */
789 success = read_number(L, f);
791 + case 'i': /* integer (full accuracy) */
792 + success = read_integer(L, f);
795 + case 'c': /* complex */
796 + success = read_complex(L, f);
800 success = read_line(L, f);
802 @@ -412,9 +466,10 @@ static int g_write (lua_State *L, FILE *
804 for (; nargs--; arg++) {
805 if (lua_type(L, arg) == LUA_TNUMBER) {
806 - /* optimization: could be done exactly as for strings */
808 - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
809 + if (lua_isinteger(L,arg))
810 + status = status && fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg)) > 0;
812 + status = status && fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
816 @@ -457,7 +512,7 @@ static int f_setvbuf (lua_State *L) {
817 static const char *const modenames[] = {"no", "full", "line", NULL};
819 int op = luaL_checkoption(L, 2, NULL, modenames);
820 - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
821 + size_t sz = luaL_optint32(L, 3, LUAL_BUFFERSIZE);
822 int res = setvbuf(f, NULL, mode[op], sz);
823 return pushresult(L, res == 0, NULL);
839 -const char *const luaX_tokens [] = {
840 +static const char *const luaX_tokens [] = {
841 "and", "break", "do", "else", "elseif",
842 "end", "false", "for", "function", "if",
843 "in", "local", "nil", "not", "or", "repeat",
844 "return", "then", "true", "until", "while",
845 "..", "...", "==", ">=", "<=", "~=",
846 "<number>", "<name>", "<string>", "<eof>",
854 @@ -90,7 +95,11 @@ static const char *txtToken (LexState *l
864 return luaZ_buffer(ls->buff);
866 @@ -173,23 +182,27 @@ static void buffreplace (LexState *ls, c
867 if (p[n] == from) p[n] = to;
871 -static void trydecpoint (LexState *ls, SemInfo *seminfo) {
872 +/* TK_NUMBER (/ TK_NUMBER2) */
873 +static int trydecpoint (LexState *ls, SemInfo *seminfo) {
874 /* format error: try to update decimal point separator */
875 struct lconv *cv = localeconv();
876 char old = ls->decpoint;
878 ls->decpoint = (cv ? cv->decimal_point[0] : '.');
879 buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
880 - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
881 + ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, NULL);
883 /* format error with correct decimal point: no more options */
884 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
885 luaX_lexerror(ls, "malformed number", TK_NUMBER);
892 -static void read_numeral (LexState *ls, SemInfo *seminfo) {
893 +/* TK_NUMBER / TK_INT (/TK_NUMBER2) */
894 +static int read_numeral (LexState *ls, SemInfo *seminfo) {
896 lua_assert(isdigit(ls->current));
899 @@ -200,8 +213,9 @@ static void read_numeral (LexState *ls,
902 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
903 - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
904 - trydecpoint(ls, seminfo); /* try to update decimal point separator */
905 + ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, &seminfo->i );
906 + if (!ret) return trydecpoint(ls, seminfo); /* try to update decimal point separator */
911 @@ -329,6 +343,7 @@ static void read_string (LexState *ls, i
916 static int llex (LexState *ls, SemInfo *seminfo) {
917 luaZ_resetbuffer(ls->buff);
919 @@ -400,8 +415,7 @@ static int llex (LexState *ls, SemInfo *
921 else if (!isdigit(ls->current)) return '.';
923 - read_numeral(ls, seminfo);
925 + return read_numeral(ls, seminfo);
929 @@ -414,8 +428,7 @@ static int llex (LexState *ls, SemInfo *
932 else if (isdigit(ls->current)) {
933 - read_numeral(ls, seminfo);
935 + return read_numeral(ls, seminfo);
937 else if (isalpha(ls->current) || ls->current == '_') {
938 /* identifier or reserved word */
941 @@ -29,19 +29,22 @@ enum RESERVED {
942 TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
943 /* other terminal symbols */
944 TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
945 - TK_NAME, TK_STRING, TK_EOS
946 + TK_NAME, TK_STRING, TK_EOS, TK_INT
948 + , TK_NUMBER2 /* imaginary constants: Ni */
952 /* number of reserved words */
953 #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
956 -/* array with token `names' */
957 -LUAI_DATA const char *const luaX_tokens [];
960 +/* SemInfo is a local data structure of 'llex.c', used for carrying a string
961 + * or a number. A separate token (TK_*) will tell, how to interpret the data.
967 } SemInfo; /* semantics information */
971 @@ -49,6 +49,7 @@ typedef LUAI_USER_ALIGNMENT_T L_Umaxalig
973 /* result of a `usual argument conversion' over lua_Number */
974 typedef LUAI_UACNUMBER l_uacNumber;
975 +typedef LUAI_UACINTEGER l_uacInteger;
978 /* internal assertions for in-house debugging */
979 @@ -80,7 +81,6 @@ typedef LUAI_UACNUMBER l_uacNumber;
980 #define cast_int(i) cast(int, (i))
985 ** type for virtual-machine instructions
986 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
990 ** See Copyright Notice in lua.h
997 @@ -16,113 +15,210 @@
1001 +/* 'luai_vectpow()' as a replacement for 'cpow()'. Defined in the header; we
1002 + * don't intrude the code libs internal functions.
1004 +#ifdef LNUM_COMPLEX
1009 -#define PI (3.14159265358979323846)
1010 -#define RADIANS_PER_DEGREE (PI/180.0)
1013 +# define PI (3.14159265358979323846F)
1014 +#elif defined(M_PI)
1017 +# define PI (3.14159265358979323846264338327950288)
1019 +#define RADIANS_PER_DEGREE (PI/180)
1023 +# define HUGE HUGE_VALF
1024 +#elif defined(LNUM_LDOUBLE)
1025 +# define HUGE HUGE_VALL
1027 +# define HUGE HUGE_VAL
1030 static int math_abs (lua_State *L) {
1031 - lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
1032 +#ifdef LNUM_COMPLEX
1033 + lua_pushnumber(L, _LF(cabs) (luaL_checkcomplex(L,1)));
1035 + lua_pushnumber(L, _LF(fabs) (luaL_checknumber(L, 1)));
1040 static int math_sin (lua_State *L) {
1041 - lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
1042 +#ifdef LNUM_COMPLEX
1043 + lua_pushcomplex(L, _LF(csin) (luaL_checkcomplex(L,1)));
1045 + lua_pushnumber(L, _LF(sin) (luaL_checknumber(L, 1)));
1050 static int math_sinh (lua_State *L) {
1051 - lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
1052 +#ifdef LNUM_COMPLEX
1053 + lua_pushcomplex(L, _LF(csinh) (luaL_checkcomplex(L,1)));
1055 + lua_pushnumber(L, _LF(sinh) (luaL_checknumber(L, 1)));
1060 static int math_cos (lua_State *L) {
1061 - lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
1062 +#ifdef LNUM_COMPLEX
1063 + lua_pushcomplex(L, _LF(ccos) (luaL_checkcomplex(L,1)));
1065 + lua_pushnumber(L, _LF(cos) (luaL_checknumber(L, 1)));
1070 static int math_cosh (lua_State *L) {
1071 - lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
1072 +#ifdef LNUM_COMPLEX
1073 + lua_pushcomplex(L, _LF(ccosh) (luaL_checkcomplex(L,1)));
1075 + lua_pushnumber(L, _LF(cosh) (luaL_checknumber(L, 1)));
1080 static int math_tan (lua_State *L) {
1081 - lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
1082 +#ifdef LNUM_COMPLEX
1083 + lua_pushcomplex(L, _LF(ctan) (luaL_checkcomplex(L,1)));
1085 + lua_pushnumber(L, _LF(tan) (luaL_checknumber(L, 1)));
1090 static int math_tanh (lua_State *L) {
1091 - lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
1092 +#ifdef LNUM_COMPLEX
1093 + lua_pushcomplex(L, _LF(ctanh) (luaL_checkcomplex(L,1)));
1095 + lua_pushnumber(L, _LF(tanh) (luaL_checknumber(L, 1)));
1100 static int math_asin (lua_State *L) {
1101 - lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
1102 +#ifdef LNUM_COMPLEX
1103 + lua_pushcomplex(L, _LF(casin) (luaL_checkcomplex(L,1)));
1105 + lua_pushnumber(L, _LF(asin) (luaL_checknumber(L, 1)));
1110 static int math_acos (lua_State *L) {
1111 - lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
1112 +#ifdef LNUM_COMPLEX
1113 + lua_pushcomplex(L, _LF(cacos) (luaL_checkcomplex(L,1)));
1115 + lua_pushnumber(L, _LF(acos) (luaL_checknumber(L, 1)));
1120 static int math_atan (lua_State *L) {
1121 - lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
1122 +#ifdef LNUM_COMPLEX
1123 + lua_pushcomplex(L, _LF(catan) (luaL_checkcomplex(L,1)));
1125 + lua_pushnumber(L, _LF(atan) (luaL_checknumber(L, 1)));
1130 static int math_atan2 (lua_State *L) {
1131 - lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1132 + /* scalars only */
1133 + lua_pushnumber(L, _LF(atan2) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1137 static int math_ceil (lua_State *L) {
1138 - lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
1139 +#ifdef LNUM_COMPLEX
1140 + lua_Complex v= luaL_checkcomplex(L, 1);
1141 + lua_pushcomplex(L, _LF(ceil) (_LF(creal)(v)) + _LF(ceil) (_LF(cimag)(v))*I);
1143 + lua_pushnumber(L, _LF(ceil) (luaL_checknumber(L, 1)));
1148 static int math_floor (lua_State *L) {
1149 - lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
1150 +#ifdef LNUM_COMPLEX
1151 + lua_Complex v= luaL_checkcomplex(L, 1);
1152 + lua_pushcomplex(L, _LF(floor) (_LF(creal)(v)) + _LF(floor) (_LF(cimag)(v))*I);
1154 + lua_pushnumber(L, _LF(floor) (luaL_checknumber(L, 1)));
1159 -static int math_fmod (lua_State *L) {
1160 - lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1161 +static int math_fmod (lua_State *L) {
1162 + /* scalars only */
1163 + lua_pushnumber(L, _LF(fmod) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1167 static int math_modf (lua_State *L) {
1169 - double fp = modf(luaL_checknumber(L, 1), &ip);
1170 + /* scalars only */
1172 + lua_Number fp = _LF(modf) (luaL_checknumber(L, 1), &ip);
1173 lua_pushnumber(L, ip);
1174 lua_pushnumber(L, fp);
1178 static int math_sqrt (lua_State *L) {
1179 - lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
1180 +#ifdef LNUM_COMPLEX
1181 + lua_pushcomplex(L, _LF(csqrt) (luaL_checkcomplex(L,1)));
1183 + lua_pushnumber(L, _LF(sqrt) (luaL_checknumber(L, 1)));
1188 static int math_pow (lua_State *L) {
1189 - lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1190 +#ifdef LNUM_COMPLEX
1191 + /* C99 'cpow' gives somewhat inaccurate results (i.e. (-1)^2 = -1+1.2246467991474e-16i).
1192 + * 'luai_vectpow' smoothens such, reusing it is the reason we need to #include "lnum.h".
1194 + lua_pushcomplex(L, luai_vectpow(luaL_checkcomplex(L,1), luaL_checkcomplex(L,2)));
1196 + lua_pushnumber(L, _LF(pow) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1201 static int math_log (lua_State *L) {
1202 - lua_pushnumber(L, log(luaL_checknumber(L, 1)));
1203 +#ifdef LNUM_COMPLEX
1204 + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)));
1206 + lua_pushnumber(L, _LF(log) (luaL_checknumber(L, 1)));
1211 static int math_log10 (lua_State *L) {
1212 - lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
1213 +#ifdef LNUM_COMPLEX
1214 + /* Not in standard <complex.h> , but easy to calculate: log_a(x) = log_b(x) / log_b(a)
1216 + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)) / _LF(log) (10));
1218 + lua_pushnumber(L, _LF(log10) (luaL_checknumber(L, 1)));
1223 static int math_exp (lua_State *L) {
1224 - lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
1225 +#ifdef LNUM_COMPLEX
1226 + lua_pushcomplex(L, _LF(cexp) (luaL_checkcomplex(L,1)));
1228 + lua_pushnumber(L, _LF(exp) (luaL_checknumber(L, 1)));
1233 @@ -138,19 +234,20 @@ static int math_rad (lua_State *L) {
1235 static int math_frexp (lua_State *L) {
1237 - lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
1238 + lua_pushnumber(L, _LF(frexp) (luaL_checknumber(L, 1), &e));
1239 lua_pushinteger(L, e);
1243 static int math_ldexp (lua_State *L) {
1244 - lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
1245 + lua_pushnumber(L, _LF(ldexp) (luaL_checknumber(L, 1), luaL_checkint(L, 2)));
1251 static int math_min (lua_State *L) {
1252 + /* scalars only */
1253 int n = lua_gettop(L); /* number of arguments */
1254 lua_Number dmin = luaL_checknumber(L, 1);
1256 @@ -165,6 +262,7 @@ static int math_min (lua_State *L) {
1259 static int math_max (lua_State *L) {
1260 + /* scalars only */
1261 int n = lua_gettop(L); /* number of arguments */
1262 lua_Number dmax = luaL_checknumber(L, 1);
1264 @@ -182,25 +280,20 @@ static int math_random (lua_State *L) {
1265 /* the `%' avoids the (rare) case of r==1, and is needed also because on
1266 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
1267 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
1268 - switch (lua_gettop(L)) { /* check number of arguments */
1269 - case 0: { /* no arguments */
1270 - lua_pushnumber(L, r); /* Number between 0 and 1 */
1273 - case 1: { /* only upper limit */
1274 - int u = luaL_checkint(L, 1);
1275 - luaL_argcheck(L, 1<=u, 1, "interval is empty");
1276 - lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
1279 - case 2: { /* lower and upper limits */
1280 - int l = luaL_checkint(L, 1);
1281 - int u = luaL_checkint(L, 2);
1282 - luaL_argcheck(L, l<=u, 2, "interval is empty");
1283 - lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
1286 - default: return luaL_error(L, "wrong number of arguments");
1287 + int n= lua_gettop(L); /* number of arguments */
1288 + if (n==0) { /* no arguments: range [0,1) */
1289 + lua_pushnumber(L, r);
1290 + } else if (n<=2) { /* int range [1,u] or [l,u] */
1291 + int l= n==1 ? 1 : luaL_checkint(L, 1);
1292 + int u = luaL_checkint(L, n);
1295 + luaL_argcheck(L, l<=u, n, "interval is empty");
1296 + d= _LF(floor)(r*(u-l+1));
1297 + lua_number2int(tmp,d);
1298 + lua_pushinteger(L, l+tmp);
1300 + return luaL_error(L, "wrong number of arguments");
1304 @@ -211,6 +304,66 @@ static int math_randomseed (lua_State *L
1309 +* Lua 5.1 does not have acosh, asinh, atanh for scalars (not ANSI C)
1311 +#if __STDC_VERSION__ >= 199901L
1312 +static int math_acosh (lua_State *L) {
1313 +# ifdef LNUM_COMPLEX
1314 + lua_pushcomplex(L, _LF(cacosh) (luaL_checkcomplex(L,1)));
1316 + lua_pushnumber(L, _LF(acosh) (luaL_checknumber(L,1)));
1320 +static int math_asinh (lua_State *L) {
1321 +# ifdef LNUM_COMPLEX
1322 + lua_pushcomplex(L, _LF(casinh) (luaL_checkcomplex(L,1)));
1324 + lua_pushnumber(L, _LF(asinh) (luaL_checknumber(L,1)));
1328 +static int math_atanh (lua_State *L) {
1329 +# ifdef LNUM_COMPLEX
1330 + lua_pushcomplex(L, _LF(catanh) (luaL_checkcomplex(L,1)));
1332 + lua_pushnumber(L, _LF(atanh) (luaL_checknumber(L,1)));
1339 + * C99 complex functions, not covered above.
1341 +#ifdef LNUM_COMPLEX
1342 +static int math_arg (lua_State *L) {
1343 + lua_pushnumber(L, _LF(carg) (luaL_checkcomplex(L,1)));
1347 +static int math_imag (lua_State *L) {
1348 + lua_pushnumber(L, _LF(cimag) (luaL_checkcomplex(L,1)));
1352 +static int math_real (lua_State *L) {
1353 + lua_pushnumber(L, _LF(creal) (luaL_checkcomplex(L,1)));
1357 +static int math_conj (lua_State *L) {
1358 + lua_pushcomplex(L, _LF(conj) (luaL_checkcomplex(L,1)));
1362 +static int math_proj (lua_State *L) {
1363 + lua_pushcomplex(L, _LF(cproj) (luaL_checkcomplex(L,1)));
1369 static const luaL_Reg mathlib[] = {
1371 @@ -241,6 +394,18 @@ static const luaL_Reg mathlib[] = {
1372 {"sqrt", math_sqrt},
1373 {"tanh", math_tanh},
1375 +#if __STDC_VERSION__ >= 199901L
1376 + {"acosh", math_acosh},
1377 + {"asinh", math_asinh},
1378 + {"atanh", math_atanh},
1380 +#ifdef LNUM_COMPLEX
1381 + {"arg", math_arg},
1382 + {"imag", math_imag},
1383 + {"real", math_real},
1384 + {"conj", math_conj},
1385 + {"proj", math_proj},
1390 @@ -252,8 +417,10 @@ LUALIB_API int luaopen_math (lua_State *
1391 luaL_register(L, LUA_MATHLIBNAME, mathlib);
1392 lua_pushnumber(L, PI);
1393 lua_setfield(L, -2, "pi");
1394 - lua_pushnumber(L, HUGE_VAL);
1395 + lua_pushnumber(L, HUGE);
1396 lua_setfield(L, -2, "huge");
1397 + lua_pushinteger(L, LUA_INTEGER_MAX );
1398 + lua_setfield(L, -2, "hugeint");
1399 #if defined(LUA_COMPAT_MOD)
1400 lua_getfield(L, -1, "fmod");
1401 lua_setfield(L, -2, "mod");
1406 +** $Id: lnum.c,v ... $
1407 +** Internal number model
1408 +** See Copyright Notice in lua.h
1411 +#include <stdlib.h>
1414 +#include <string.h>
1426 +** lua_real2str converts a (non-complex) number to a string.
1427 +** lua_str2real converts a string to a (non-complex) number.
1429 +#define lua_real2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
1432 +* Note: Only 'strtod()' is part of ANSI C; others are C99 and
1433 +* may need '--std=c99' compiler setting (at least on Ubuntu 7.10).
1435 +* Visual C++ 2008 Express does not have 'strtof()', nor 'strtold()'.
1436 +* References to '_strtold()' exist but don't compile. It seems best
1437 +* to leave Windows users with DOUBLE only (or compile with MinGW).
1439 +* In practise, using '(long double)strtod' is a risky thing, since
1440 +* it will cause accuracy loss in reading in numbers, and such losses
1441 +* will pile up in later processing. Get a real 'strtold()' or don't
1442 +* use that mode at all.
1445 +# define lua_str2real strtod
1446 +#elif defined(LNUM_FLOAT)
1447 +# define lua_str2real strtof
1448 +#elif defined(LNUM_LDOUBLE)
1449 +# define lua_str2real strtold
1452 +#define lua_integer2str(s,v) sprintf((s), LUA_INTEGER_FMT, (v))
1454 +/* 's' is expected to be LUAI_MAXNUMBER2STR long (enough for any number)
1456 +void luaO_num2buf( char *s, const TValue *o )
1459 + lua_assert( ttisnumber(o) );
1461 + /* Reason to handle integers differently is not only speed, but accuracy as
1462 + * well. We want to make any integer tostring() without roundings, at all.
1465 + lua_integer2str( s, ivalue(o) );
1468 + n= nvalue_fast(o);
1469 + lua_real2str(s, n);
1471 +#ifdef LNUM_COMPLEX
1472 + lua_Number n2= nvalue_img_fast(o);
1473 + if (n2!=0) { /* Postfix with +-Ni */
1474 + int re0= (n == 0);
1475 + char *s2= re0 ? s : strchr(s,'\0');
1476 + if ((!re0) && (n2>0)) *s2++= '+';
1477 + lua_real2str( s2, n2 );
1484 +* If a LUA_TNUMBER has integer value, give it.
1486 +int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref ) {
1490 + lua_assert( ttype(o)==LUA_TNUMBER );
1491 + lua_assert( ref );
1492 +#ifdef LNUM_COMPLEX
1493 + if (nvalue_img_fast(o)!=0) return 0;
1495 + d= nvalue_fast(o);
1496 + lua_number2integer(i, d);
1497 + if (cast_num(i) == d) {
1498 + *ref= i; return 1;
1504 + * Lua 5.1.3 (using 'strtod()') allows 0x+hex but not 0+octal. This is good,
1505 + * and we should NOT use 'autobase' 0 with 'strtoul[l]()' for this reason.
1507 + * Lua 5.1.3 allows '0x...' numbers to overflow and lose precision; this is not
1508 + * good. On Visual C++ 2008, 'strtod()' does not even take them in. Better to
1509 + * require hex values to fit 'lua_Integer' or give an error that they don't?
1511 + * Full hex range (0 .. 0xff..ff) is stored as integers, not to lose any bits.
1512 + * Numerical value of 0xff..ff will be -1, if used in calculations.
1514 + * Returns: TK_INT for a valid integer, '*endptr_ref' updated
1515 + * TK_NUMBER for seemingly numeric, to be parsed as floating point
1516 + * 0 for bad characters, not a number (or '0x' out of range)
1518 +static int luaO_str2i (const char *s, lua_Integer *res, char **endptr_ref) {
1520 + /* 'v' gets ULONG_MAX on possible overflow (which is > LUA_INTEGER_MAX);
1521 + * we don't have to check 'errno' here.
1523 + unsigned LUA_INTEGER v= lua_str2ul(s, &endptr, 10);
1524 + if (endptr == s) return 0; /* nothing numeric */
1525 + if (v==0 && *endptr=='x') {
1526 + errno= 0; /* needs to be set, 'strtoul[l]' does not clear it */
1527 + v= lua_str2ul(endptr+1, &endptr, 16); /* retry as hex, unsigned range */
1528 + if (errno==ERANGE) { /* clamped to 0xff..ff */
1529 +#if (defined(LNUM_INT32) && !defined(LNUM_FLOAT)) || defined(LNUM_LDOUBLE)
1530 + return TK_NUMBER; /* Allow to be read as floating point (has more integer range) */
1532 + return 0; /* Reject the number */
1535 + } else if ((v > LUA_INTEGER_MAX) || (*endptr && (!isspace(*endptr)))) {
1536 + return TK_NUMBER; /* not in signed range, or has '.', 'e' etc. trailing */
1538 + *res= (lua_Integer)v;
1539 + *endptr_ref= endptr;
1543 +/* 0 / TK_NUMBER / TK_INT (/ TK_NUMBER2) */
1544 +int luaO_str2d (const char *s, lua_Number *res_n, lua_Integer *res_i) {
1546 + int ret= TK_NUMBER;
1547 + /* Check integers first, if caller is allowing.
1548 + * If 'res2'==NULL, they're only looking for floating point.
1551 + ret= luaO_str2i(s,res_i,&endptr);
1552 + if (ret==0) return 0;
1554 + if (ret==TK_NUMBER) {
1555 + lua_assert(res_n);
1556 + /* Note: Visual C++ 2008 Express 'strtod()' does not read in "0x..."
1557 + * numbers; it will read '0' and spit 'x' as endptr.
1558 + * This means hex constants not fitting in 'lua_Integer' won't
1559 + * be read in at all. What to do?
1561 + *res_n = lua_str2real(s, &endptr);
1562 + if (endptr == s) return 0; /* conversion failed */
1563 + /* Visual C++ 2008 'strtod()' does not allow "0x..." input. */
1564 +#if defined(_MSC_VER) && !defined(LNUM_FLOAT) && !defined(LNUM_INT64)
1565 + if (*res_n==0 && *endptr=='x') {
1566 + /* Hex constant too big for 'lua_Integer' but that could fit in 'lua_Number'
1569 + unsigned __int64 v= _strtoui64( s, &endptr, 16 );
1570 + /* We just let > 64 bit values be clamped to _UI64_MAX (MSDN does not say 'errno'==ERANGE would be set) */
1571 + *res_n= cast_num(v);
1572 + if (*res_n != v) return 0; /* Would have lost accuracy */
1575 +#ifdef LNUM_COMPLEX
1576 + if (*endptr == 'i') { endptr++; ret= TK_NUMBER2; }
1580 + while (isspace(cast(unsigned char, *endptr))) endptr++;
1581 + if (*endptr) return 0; /* invalid trail */
1587 +/* Functions for finding out, when integer operations remain in range
1588 + * (and doing them).
1590 +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1591 + lua_Integer v= ib+ic; /* may overflow */
1592 + if (ib>0 && ic>0) { if (v < 0) return 0; /*overflow, use floats*/ }
1593 + else if (ib<0 && ic<0) { if (v >= 0) return 0; }
1598 +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1599 + lua_Integer v= ib-ic; /* may overflow */
1600 + if (ib>=0 && ic<0) { if (v < 0) return 0; /*overflow, use floats*/ }
1601 + else if (ib<0 && ic>0) { if (v >= 0) return 0; }
1606 +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1607 + if (ib!=LUA_INTEGER_MIN && ic!=LUA_INTEGER_MIN) {
1608 + lua_Integer b= luai_abs(ib), c= luai_abs(ic);
1609 + if ( (ib==0) || (LUA_INTEGER_MAX/b >= c) ) {
1610 + *r= ib*ic; /* no overflow */
1613 + } else if (ib==0 || ic==0) {
1617 + /* Result can be LUA_INTEGER_MIN; if it is, calculating it using floating
1618 + * point will not cause accuracy loss.
1620 + if ( luai_nummul( cast_num(ib), cast_num(ic) ) == LUA_INTEGER_MIN ) {
1621 + *r= LUA_INTEGER_MIN;
1627 +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1628 + /* N/0: leave to float side, to give an error
1630 + if (ic==0) return 0;
1632 + /* N/LUA_INTEGER_MIN: always non-integer results, or 0 or +1
1634 + if (ic==LUA_INTEGER_MIN) {
1635 + if (ib==LUA_INTEGER_MIN) { *r=1; return 1; }
1636 + if (ib==0) { *r=0; return 1; }
1638 + /* LUA_INTEGER_MIN (-2^31|63)/N: calculate using float side (either the division
1639 + * causes non-integer results, or there is no accuracy loss in int->fp->int
1640 + * conversions (N=2,4,8,..,256 and N=2^30,2^29,..2^23).
1642 + } else if (ib==LUA_INTEGER_MIN) {
1643 + lua_Number d= luai_numdiv( cast_num(LUA_INTEGER_MIN), cast_num(ic) );
1644 + lua_Integer i; lua_number2integer(i,d);
1645 + if (cast_num(i)==d) { *r= i; return 1; }
1648 + /* Note: We _can_ use ANSI C mod here, even on negative values, since
1649 + * we only test for == 0 (the sign would be implementation dependent).
1651 + if (ib%ic == 0) { *r= ib/ic; return 1; }
1657 +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1659 + /* ANSI C can be trusted when b%c==0, or when values are non-negative.
1660 + * b - (floor(b/c) * c)
1662 + * + +: b - (b/c) * c (b % c can be used)
1663 + * - -: b - (b/c) * c (b % c could work, but not defined by ANSI C)
1664 + * 0 -: b - (b/c) * c (=0, b % c could work, but not defined by ANSI C)
1665 + * - +: b - (b/c-1) * c (when b!=-c)
1666 + * + -: b - (b/c-1) * c (when b!=-c)
1668 + * o MIN%MIN ends up 0, via overflow in calcs but that does not matter.
1669 + * o MIN%MAX ends up MAX-1 (and other such numbers), also after overflow,
1670 + * but that does not matter, results do.
1672 + lua_Integer v= ib % ic;
1673 + if ( v!=0 && (ib<0 || ic<0) ) {
1674 + v= ib - ((ib/ic) - ((ib<=0 && ic<0) ? 0:1)) * ic;
1676 + /* Result should always have same sign as 2nd argument. (PIL2) */
1677 + lua_assert( (v<0) ? (ic<0) : (v>0) ? (ic>0) : 1 );
1681 + return 0; /* let float side return NaN */
1684 +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1686 + /* In FLOAT/INT32 or FLOAT|DOUBLE/INT64 modes, calculating integer powers
1687 + * via FP realm may lose accuracy (i.e. 7^11 = 1977326743, which fits int32
1688 + * but not 23-bit float mantissa).
1690 + * The current solution is dumb, but it works and uses little code. Use of
1691 + * integer powers is not anticipated to be very frequent (apart from 2^x,
1692 + * which is separately optimized).
1695 + else if (ic<0) return 0; /* FP realm */
1696 + else if (ib==2 && ic < (int)sizeof(lua_Integer)*8-1) *r= ((lua_Integer)1)<<ic; /* 1,2,4,...2^30 | 2^62 optimization */
1697 + else if (ic==0) *r=1;
1698 + else if (luai_abs(ib)==1) *r= (ic%2) ? ib:1;
1700 + lua_Integer x= ib;
1702 + if (!try_mulint( &x, x, ib ))
1703 + return 0; /* FP realm */
1710 +int try_unmint( lua_Integer *r, lua_Integer ib ) {
1711 + /* Negating LUA_INTEGER_MIN leaves the range. */
1712 + if ( ib != LUA_INTEGER_MIN )
1713 + { *r= -ib; return 1; }
1721 +** $Id: lnum.h,v ... $
1722 +** Internal Number model
1723 +** See Copyright Notice in lua.h
1731 +#include "lobject.h"
1734 +** The luai_num* macros define the primitive operations over 'lua_Number's
1735 +** (not 'lua_Integer's, not 'lua_Complex').
1737 +#define luai_numadd(a,b) ((a)+(b))
1738 +#define luai_numsub(a,b) ((a)-(b))
1739 +#define luai_nummul(a,b) ((a)*(b))
1740 +#define luai_numdiv(a,b) ((a)/(b))
1741 +#define luai_nummod(a,b) ((a) - _LF(floor)((a)/(b))*(b))
1742 +#define luai_numpow(a,b) (_LF(pow)(a,b))
1743 +#define luai_numunm(a) (-(a))
1744 +#define luai_numeq(a,b) ((a)==(b))
1745 +#define luai_numlt(a,b) ((a)<(b))
1746 +#define luai_numle(a,b) ((a)<=(b))
1747 +#define luai_numisnan(a) (!luai_numeq((a), (a)))
1749 +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1750 +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1751 +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1752 +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1753 +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1754 +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1755 +int try_unmint( lua_Integer *r, lua_Integer ib );
1757 +#ifdef LNUM_COMPLEX
1758 + static inline lua_Complex luai_vectunm( lua_Complex a ) { return -a; }
1759 + static inline lua_Complex luai_vectadd( lua_Complex a, lua_Complex b ) { return a+b; }
1760 + static inline lua_Complex luai_vectsub( lua_Complex a, lua_Complex b ) { return a-b; }
1761 + static inline lua_Complex luai_vectmul( lua_Complex a, lua_Complex b ) { return a*b; }
1762 + static inline lua_Complex luai_vectdiv( lua_Complex a, lua_Complex b ) { return a/b; }
1765 + * C99 does not provide modulus for complex numbers. It most likely is not
1766 + * meaningful at all.
1772 + * C99 'cpow' gives inaccurate results for many common cases s.a. (1i)^2 ->
1773 + * -1+1.2246467991474e-16i (OS X 10.4, gcc 4.0.1 build 5367)
1775 + * [(a+bi)^(c+di)] = (r^c) * exp(-d*t) * cos(c*t + d*ln(r)) +
1776 + * = (r^c) * exp(-d*t) * sin(c*t + d*ln(r)) *i
1777 + * r = sqrt(a^2+b^2), t = arctan( b/a )
1779 + * Reference: <http://home.att.net/~srschmitt/complexnumbers.html>
1780 + * Could also be calculated using: x^y = exp(ln(x)*y)
1782 + * Note: Defined here (and not in .c) so 'lmathlib.c' can share the
1786 + lua_Complex luai_vectpow( lua_Complex a, lua_Complex b )
1789 + lua_Number ar= _LF(creal)(a), ai= _LF(cimag)(a);
1790 + lua_Number br= _LF(creal)(b), bi= _LF(cimag)(b);
1792 + if (ai==0 && bi==0) { /* a^c (real) */
1793 + return luai_numpow( ar, br );
1796 + int br_int= (int)br;
1798 + if ( ai!=0 && bi==0 && br_int==br && br_int!=0 && br_int!=INT_MIN ) {
1799 + /* (a+bi)^N, N = { +-1,+-2, ... +-INT_MAX }
1801 + lua_Number k= luai_numpow( _LF(sqrt) (ar*ar + ai*ai), br );
1802 + lua_Number cos_z, sin_z;
1804 + /* Situation depends upon c (N) in the following manner:
1806 + * N%4==0 => cos(c*t)=1, sin(c*t)=0
1807 + * (N*sign(b))%4==1 or (N*sign(b))%4==-3 => cos(c*t)=0, sin(c*t)=1
1808 + * N%4==2 or N%4==-2 => cos(c*t)=-1, sin(c*t)=0
1809 + * (N*sign(b))%4==-1 or (N*sign(b))%4==3 => cos(c*t)=0, sin(c*t)=-1
1811 + int br_int_abs = br_int<0 ? -br_int:br_int;
1813 + switch( (br_int_abs%4) * (br_int<0 ? -1:1) * (ai<0 ? -1:1) ) {
1814 + case 0: cos_z=1, sin_z=0; break;
1815 + case 2: case -2: cos_z=-1, sin_z=0; break;
1816 + case 1: case -3: cos_z=0, sin_z=1; break;
1817 + case 3: case -1: cos_z=0, sin_z=-1; break;
1818 + default: lua_assert(0); return 0;
1820 + return k*cos_z + (k*sin_z)*I;
1823 + return _LF(cpow) ( a, b );
1827 +LUAI_FUNC int luaO_str2d (const char *s, lua_Number *res1, lua_Integer *res2);
1828 +LUAI_FUNC void luaO_num2buf( char *s, const TValue *o );
1830 +LUAI_FUNC int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref );
1832 +#define luai_normalize(o) \
1833 +{ lua_Integer _i; if (tt_integer_valued(o,&_i)) setivalue(o,_i); }
1837 +++ b/src/lnum_config.h
1840 +** $Id: lnum_config.h,v ... $
1841 +** Internal Number model
1842 +** See Copyright Notice in lua.h
1845 +#ifndef lnum_config_h
1846 +#define lnum_config_h
1849 +** Default number modes
1851 +#if (!defined LNUM_DOUBLE) && (!defined LNUM_FLOAT) && (!defined LNUM_LDOUBLE)
1852 +# define LNUM_FLOAT
1854 +#if (!defined LNUM_INT16) && (!defined LNUM_INT32) && (!defined LNUM_INT64)
1855 +# define LNUM_INT32
1859 +** Require C99 mode for COMPLEX, FLOAT and LDOUBLE (only DOUBLE is ANSI C).
1861 +#if defined(LNUM_COMPLEX) && (__STDC_VERSION__ < 199901L)
1862 +# error "Need C99 for complex (use '--std=c99' or similar)"
1863 +#elif defined(LNUM_LDOUBLE) && (__STDC_VERSION__ < 199901L) && !defined(_MSC_VER)
1864 +# error "Need C99 for 'long double' (use '--std=c99' or similar)"
1865 +#elif defined(LNUM_FLOAT) && (__STDC_VERSION__ < 199901L)
1866 +/* LNUM_FLOAT not supported on Windows */
1867 +# error "Need C99 for 'float' (use '--std=c99' or similar)"
1871 +** Number mode identifier to accompany the version string.
1873 +#ifdef LNUM_COMPLEX
1874 +# define _LNUM1 "complex "
1879 +# define _LNUM2 "double"
1880 +#elif defined(LNUM_FLOAT)
1881 +# define _LNUM2 "float"
1882 +#elif defined(LNUM_LDOUBLE)
1883 +# define _LNUM2 "ldouble"
1886 +# define _LNUM3 "int32"
1887 +#elif defined(LNUM_INT64)
1888 +# define _LNUM3 "int64"
1889 +#elif defined(LNUM_INT16)
1890 +# define _LNUM3 "int16"
1892 +#define LUA_LNUM _LNUM1 _LNUM2 " " _LNUM3
1895 +** LUA_NUMBER is the type of floating point number in Lua
1896 +** LUA_NUMBER_SCAN is the format for reading numbers.
1897 +** LUA_NUMBER_FMT is the format for writing numbers.
1900 +# define LUA_NUMBER float
1901 +# define LUA_NUMBER_SCAN "%f"
1902 +# define LUA_NUMBER_FMT "%g"
1903 +#elif (defined LNUM_DOUBLE)
1904 +# define LUA_NUMBER double
1905 +# define LUA_NUMBER_SCAN "%lf"
1906 +# define LUA_NUMBER_FMT "%.14g"
1907 +#elif (defined LNUM_LDOUBLE)
1908 +# define LUA_NUMBER long double
1909 +# define LUA_NUMBER_SCAN "%Lg"
1910 +# define LUA_NUMBER_FMT "%.20Lg"
1915 +** LUAI_MAXNUMBER2STR: size of a buffer fitting any number->string result.
1917 +** double: 24 (sign, x.xxxxxxxxxxxxxxe+nnnn, and \0)
1918 +** int64: 21 (19 digits, sign, and \0)
1919 +** long double: 43 for 128-bit (sign, x.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
1920 +** 30 for 80-bit (sign, x.xxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
1922 +#ifdef LNUM_LDOUBLE
1923 +# define _LUAI_MN2S 44
1925 +# define _LUAI_MN2S 24
1928 +#ifdef LNUM_COMPLEX
1929 +# define LUAI_MAXNUMBER2STR (2*_LUAI_MN2S)
1931 +# define LUAI_MAXNUMBER2STR _LUAI_MN2S
1935 +** LUA_INTEGER is the integer type used by lua_pushinteger/lua_tointeger/lua_isinteger.
1936 +** LUA_INTEGER_SCAN is the format for reading integers
1937 +** LUA_INTEGER_FMT is the format for writing integers
1939 +** Note: Visual C++ 2005 does not have 'strtoull()', use '_strtoui64()' instead.
1942 +# if LUAI_BITSINT > 16
1943 +# define LUA_INTEGER int
1944 +# define LUA_INTEGER_SCAN "%d"
1945 +# define LUA_INTEGER_FMT "%d"
1947 +/* Note: 'LUA_INTEGER' being 'ptrdiff_t' (as in Lua 5.1) causes problems with
1948 + * 'printf()' operations. Also 'unsigned ptrdiff_t' is invalid.
1950 +# define LUA_INTEGER long
1951 +# define LUA_INTEGER_SCAN "%ld"
1952 +# define LUA_INTEGER_FMT "%ld"
1954 +# define LUA_INTEGER_MAX 0x7FFFFFFF /* 2^31-1 */
1956 +#elif defined(LNUM_INT64)
1957 +# define LUA_INTEGER long long
1959 +# define lua_str2ul _strtoui64
1961 +# define lua_str2ul strtoull
1963 +# define LUA_INTEGER_SCAN "%lld"
1964 +# define LUA_INTEGER_FMT "%lld"
1965 +# define LUA_INTEGER_MAX 0x7fffffffffffffffLL /* 2^63-1 */
1966 +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX - 1LL) /* -2^63 */
1968 +#elif defined(LNUM_INT16)
1969 +# if LUAI_BITSINT > 16
1970 +# define LUA_INTEGER short
1971 +# define LUA_INTEGER_SCAN "%hd"
1972 +# define LUA_INTEGER_FMT "%hd"
1974 +# define LUA_INTEGER int
1975 +# define LUA_INTEGER_SCAN "%d"
1976 +# define LUA_INTEGER_FMT "%d"
1978 +# define LUA_INTEGER_MAX 0x7FFF /* 2^16-1 */
1982 +# define lua_str2ul (unsigned LUA_INTEGER)strtoul
1984 +#ifndef LUA_INTEGER_MIN
1985 +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX -1) /* -2^16|32 */
1989 +@@ lua_number2int is a macro to convert lua_Number to int.
1990 +@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
1991 +** CHANGE them if you know a faster way to convert a lua_Number to
1992 +** int (with any rounding method and without throwing errors) in your
1993 +** system. In Pentium machines, a naive typecast from double to int
1994 +** in C is extremely slow, so any alternative is worth trying.
1997 +/* On a Pentium, resort to a trick */
1998 +#if defined(LNUM_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
1999 + (defined(__i386) || defined (_M_IX86) || defined(__i386__))
2001 +/* On a Microsoft compiler, use assembler */
2002 +# if defined(_MSC_VER)
2003 +# define lua_number2int(i,d) __asm fld d __asm fistp i
2006 +/* the next trick should work on any Pentium, but sometimes clashes
2007 + with a DirectX idiosyncrasy */
2008 +union luai_Cast { double l_d; long l_l; };
2009 +# define lua_number2int(i,d) \
2010 + { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
2013 +# ifndef LNUM_INT64
2014 +# define lua_number2integer lua_number2int
2017 +/* this option always works, but may be slow */
2019 +# define lua_number2int(i,d) ((i)=(int)(d))
2022 +/* Note: Some compilers (OS X gcc 4.0?) may choke on double->long long conversion
2023 + * since it can lose precision. Others do require 'long long' there.
2025 +#ifndef lua_number2integer
2026 +# define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
2030 +** 'luai_abs()' to give absolute value of 'lua_Integer'
2033 +# define luai_abs abs
2034 +#elif defined(LNUM_INT64) && (__STDC_VERSION__ >= 199901L)
2035 +# define luai_abs llabs
2037 +# define luai_abs(v) ((v) >= 0 ? (v) : -(v))
2041 +** LUAI_UACNUMBER is the result of an 'usual argument conversion' over a number.
2042 +** LUAI_UACINTEGER the same, over an integer.
2044 +#define LUAI_UACNUMBER double
2045 +#define LUAI_UACINTEGER long
2047 +/* ANSI C only has math funcs for 'double. C99 required for float and long double
2051 +# define _LF(name) name
2052 +#elif defined(LNUM_FLOAT)
2053 +# define _LF(name) name ## f
2054 +#elif defined(LNUM_LDOUBLE)
2055 +# define _LF(name) name ## l
2064 #include "lstring.h"
2071 const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
2072 @@ -70,12 +71,31 @@ int luaO_log2 (unsigned int x) {
2075 int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
2076 - if (ttype(t1) != ttype(t2)) return 0;
2077 + if (!ttype_ext_same(t1,t2)) return 0;
2078 else switch (ttype(t1)) {
2082 + if (ttype(t2)==LUA_TINT)
2083 + return ivalue(t1) == ivalue(t2);
2084 + else { /* t1:int, t2:num */
2085 +#ifdef LNUM_COMPLEX
2086 + if (nvalue_img_fast(t2) != 0) return 0;
2088 + /* Avoid doing accuracy losing cast, if possible. */
2090 + if (tt_integer_valued(t2,&tmp))
2091 + return ivalue(t1) == tmp;
2093 + return luai_numeq( cast_num(ivalue(t1)), nvalue_fast(t2) );
2096 - return luai_numeq(nvalue(t1), nvalue(t2));
2097 + if (ttype(t2)==LUA_TINT)
2098 + return luaO_rawequalObj(t2, t1); /* swap LUA_TINT to left */
2099 +#ifdef LNUM_COMPLEX
2100 + if (!luai_numeq(nvalue_img_fast(t1), nvalue_img_fast(t2))) return 0;
2102 + return luai_numeq(nvalue_fast(t1), nvalue_fast(t2));
2104 return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
2105 case LUA_TLIGHTUSERDATA:
2106 @@ -86,21 +106,6 @@ int luaO_rawequalObj (const TValue *t1,
2111 -int luaO_str2d (const char *s, lua_Number *result) {
2113 - *result = lua_str2number(s, &endptr);
2114 - if (endptr == s) return 0; /* conversion failed */
2115 - if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
2116 - *result = cast_num(strtoul(s, &endptr, 16));
2117 - if (*endptr == '\0') return 1; /* most common case */
2118 - while (isspace(cast(unsigned char, *endptr))) endptr++;
2119 - if (*endptr != '\0') return 0; /* invalid trailing characters? */
2125 static void pushstr (lua_State *L, const char *str) {
2126 setsvalue2s(L, L->top, luaS_new(L, str));
2128 @@ -131,7 +136,11 @@ const char *luaO_pushvfstring (lua_State
2132 - setnvalue(L->top, cast_num(va_arg(argp, int)));
2133 + /* This is tricky for 64-bit integers; maybe they even cannot be
2134 + * supported on all compilers; depends on the conversions applied to
2135 + * variable argument lists. TBD: test!
2137 + setivalue(L->top, (lua_Integer) va_arg(argp, l_uacInteger));
2141 @@ -212,3 +221,4 @@ void luaO_chunkid (char *out, const char
2151 /* tags for values visible from Lua */
2152 -#define LAST_TAG LUA_TTHREAD
2153 +#if LUA_TINT > LUA_TTHREAD
2154 +# define LAST_TAG LUA_TINT
2156 +# define LAST_TAG LUA_TTHREAD
2159 #define NUM_TAGS (LAST_TAG+1)
2161 @@ -59,7 +63,12 @@ typedef struct GCheader {
2165 +#ifdef LNUM_COMPLEX
2174 @@ -77,7 +86,11 @@ typedef struct lua_TValue {
2176 /* Macros to test type */
2177 #define ttisnil(o) (ttype(o) == LUA_TNIL)
2178 -#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
2179 +#define ttisint(o) (ttype(o) == LUA_TINT)
2180 +#define ttisnumber(o) ((ttype(o) == LUA_TINT) || (ttype(o) == LUA_TNUMBER))
2181 +#ifdef LNUM_COMPLEX
2182 +# define ttiscomplex(o) ((ttype(o) == LUA_TNUMBER) && (nvalue_img_fast(o)!=0))
2184 #define ttisstring(o) (ttype(o) == LUA_TSTRING)
2185 #define ttistable(o) (ttype(o) == LUA_TTABLE)
2186 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
2187 @@ -90,7 +103,25 @@ typedef struct lua_TValue {
2188 #define ttype(o) ((o)->tt)
2189 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
2190 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
2191 -#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
2193 +#define ttype_ext(o) ( ttype(o) == LUA_TINT ? LUA_TNUMBER : ttype(o) )
2194 +#define ttype_ext_same(o1,o2) ( (ttype(o1)==ttype(o2)) || (ttisnumber(o1) && ttisnumber(o2)) )
2196 +/* '_fast' variants are for cases where 'ttype(o)' is known to be LUA_TNUMBER.
2198 +#ifdef LNUM_COMPLEX
2199 +# define nvalue_complex_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
2200 +# define nvalue_fast(o) ( _LF(creal) ( nvalue_complex_fast(o) ) )
2201 +# define nvalue_img_fast(o) ( _LF(cimag) ( nvalue_complex_fast(o) ) )
2202 +# define nvalue_complex(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? (o)->value.i : (o)->value.n )
2203 +# define nvalue_img(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? 0 : _LF(cimag)( (o)->value.n ) )
2204 +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : _LF(creal)((o)->value.n) )
2206 +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : (o)->value.n )
2207 +# define nvalue_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
2209 +#define ivalue(o) check_exp( ttype(o)==LUA_TINT, (o)->value.i )
2211 #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
2212 #define tsvalue(o) (&rawtsvalue(o)->tsv)
2213 #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
2214 @@ -116,8 +147,27 @@ typedef struct lua_TValue {
2215 /* Macros to set values */
2216 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
2218 -#define setnvalue(obj,x) \
2219 - { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
2220 +/* Must not have side effects, 'x' may be expression.
2222 +#define setivalue(obj,x) \
2223 + { TValue *i_o=(obj); i_o->value.i=(x); i_o->tt=LUA_TINT; }
2225 +# define setnvalue(obj,x) \
2226 + { TValue *i_o=(obj); i_o->value.n= (x); i_o->tt=LUA_TNUMBER; }
2228 +/* Note: Complex always has "inline", both are C99.
2230 +#ifdef LNUM_COMPLEX
2231 + static inline void setnvalue_complex_fast( TValue *obj, lua_Complex x ) {
2232 + lua_assert( _LF(cimag)(x) != 0 );
2233 + obj->value.n= x; obj->tt= LUA_TNUMBER;
2235 + static inline void setnvalue_complex( TValue *obj, lua_Complex x ) {
2236 + if (_LF(cimag)(x) == 0) { setnvalue(obj, _LF(creal)(x)); }
2237 + else { obj->value.n= x; obj->tt= LUA_TNUMBER; }
2242 #define setpvalue(obj,x) \
2243 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
2244 @@ -155,9 +205,6 @@ typedef struct lua_TValue {
2245 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
2246 checkliveness(G(L),i_o); }
2251 #define setobj(L,obj1,obj2) \
2252 { const TValue *o2=(obj2); TValue *o1=(obj1); \
2253 o1->value = o2->value; o1->tt=o2->tt; \
2254 @@ -185,8 +232,11 @@ typedef struct lua_TValue {
2256 #define setttype(obj, tt) (ttype(obj) = (tt))
2259 -#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
2260 +#if LUA_TINT >= LUA_TSTRING
2261 +# define iscollectable(o) ((ttype(o) >= LUA_TSTRING) && (ttype(o) != LUA_TINT))
2263 +# define iscollectable(o) (ttype(o) >= LUA_TSTRING)
2268 @@ -370,12 +420,10 @@ LUAI_FUNC int luaO_log2 (unsigned int x)
2269 LUAI_FUNC int luaO_int2fb (unsigned int x);
2270 LUAI_FUNC int luaO_fb2int (int x);
2271 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
2272 -LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
2273 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
2275 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
2276 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
2283 @@ -186,15 +186,30 @@ static int os_time (lua_State *L) {
2285 if (t == (time_t)(-1))
2288 - lua_pushnumber(L, (lua_Number)t);
2290 + /* On float systems the pushed value must be an integer, NOT a number.
2291 + * Otherwise, accuracy is lost in the time_t->float conversion.
2294 + lua_pushinteger(L, (lua_Integer) t);
2296 + lua_pushnumber(L, (lua_Number) t);
2303 static int os_difftime (lua_State *L) {
2305 + lua_Integer i= (lua_Integer)
2306 + difftime( (time_t)(luaL_checkinteger(L, 1)),
2307 + (time_t)(luaL_optinteger(L, 2, 0)));
2308 + lua_pushinteger(L, i);
2310 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
2311 (time_t)(luaL_optnumber(L, 2, 0))));
2320 #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m)
2324 ** nodes for block list (list of active blocks)
2326 @@ -72,7 +71,7 @@ static void errorlimit (FuncState *fs, i
2327 const char *msg = (fs->f->linedefined == 0) ?
2328 luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
2329 luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
2330 - fs->f->linedefined, limit, what);
2331 + (fs->f->linedefined), limit, what);
2332 luaX_lexerror(fs->ls, msg, 0);
2335 @@ -733,6 +732,18 @@ static void simpleexp (LexState *ls, exp
2336 v->u.nval = ls->t.seminfo.r;
2340 + init_exp(v, VKINT, 0);
2341 + v->u.ival = ls->t.seminfo.i;
2344 +#ifdef LNUM_COMPLEX
2345 + case TK_NUMBER2: {
2346 + init_exp(v, VKNUM2, 0);
2347 + v->u.nval = ls->t.seminfo.r;
2352 codestring(ls, v, ls->t.seminfo.ts);
2354 @@ -1079,7 +1090,7 @@ static void fornum (LexState *ls, TStrin
2355 if (testnext(ls, ','))
2356 exp1(ls); /* optional step */
2357 else { /* default step = 1 */
2358 - luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
2359 + luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_integerK(fs, 1));
2360 luaK_reserveregs(fs, 1);
2362 forbody(ls, base, line, 1, 1);
2365 @@ -31,7 +31,11 @@ typedef enum {
2366 VRELOCABLE, /* info = instruction pc */
2367 VNONRELOC, /* info = result register */
2368 VCALL, /* info = instruction pc */
2369 - VVARARG /* info = instruction pc */
2370 + VVARARG, /* info = instruction pc */
2371 + VKINT /* ival = integer value */
2372 +#ifdef LNUM_COMPLEX
2373 + ,VKNUM2 /* nval = imaginary value */
2377 typedef struct expdesc {
2378 @@ -39,6 +43,7 @@ typedef struct expdesc {
2380 struct { int info, aux; } s;
2384 int t; /* patch list of `exit when true' */
2385 int f; /* patch list of `exit when false' */
2388 @@ -43,8 +43,8 @@ static ptrdiff_t posrelat (ptrdiff_t pos
2389 static int str_sub (lua_State *L) {
2391 const char *s = luaL_checklstring(L, 1, &l);
2392 - ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
2393 - ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
2394 + ptrdiff_t start = posrelat(luaL_checkint32(L, 2), l);
2395 + ptrdiff_t end = posrelat(luaL_optint32(L, 3, -1), l);
2396 if (start < 1) start = 1;
2397 if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
2399 @@ -106,8 +106,8 @@ static int str_rep (lua_State *L) {
2400 static int str_byte (lua_State *L) {
2402 const char *s = luaL_checklstring(L, 1, &l);
2403 - ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
2404 - ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
2405 + ptrdiff_t posi = posrelat(luaL_optint32(L, 2, 1), l);
2406 + ptrdiff_t pose = posrelat(luaL_optint32(L, 3, posi), l);
2408 if (posi <= 0) posi = 1;
2409 if ((size_t)pose > l) pose = l;
2410 @@ -496,7 +496,7 @@ static int str_find_aux (lua_State *L, i
2412 const char *s = luaL_checklstring(L, 1, &l1);
2413 const char *p = luaL_checklstring(L, 2, &l2);
2414 - ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
2415 + ptrdiff_t init = posrelat(luaL_optint32(L, 3, 1), l1) - 1;
2416 if (init < 0) init = 0;
2417 else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
2418 if (find && (lua_toboolean(L, 4) || /* explicit request? */
2419 @@ -690,7 +690,7 @@ static int str_gsub (lua_State *L) {
2420 ** maximum size of each format specification (such as '%-099.99d')
2421 ** (+10 accounts for %99.99x plus margin of error)
2423 -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
2424 +#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTEGER_FMT)-2 + 10)
2427 static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
2428 @@ -747,9 +747,9 @@ static const char *scanformat (lua_State
2429 static void addintlen (char *form) {
2430 size_t l = strlen(form);
2431 char spec = form[l - 1];
2432 - strcpy(form + l - 1, LUA_INTFRMLEN);
2433 - form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
2434 - form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
2435 + const char *tmp= LUA_INTEGER_FMT; /* "%lld" or "%ld" */
2436 + strcpy(form + l - 1, tmp+1);
2437 + form[l + sizeof(LUA_INTEGER_FMT)-4] = spec;
2441 @@ -777,12 +777,12 @@ static int str_format (lua_State *L) {
2443 case 'd': case 'i': {
2445 - sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
2446 + sprintf(buff, form, luaL_checkinteger(L, arg));
2449 case 'o': case 'u': case 'x': case 'X': {
2451 - sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
2452 + sprintf(buff, form, (unsigned LUA_INTEGER)luaL_checkinteger(L, arg));
2455 case 'e': case 'E': case 'f':
2459 #include "lobject.h"
2468 #define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
2469 #define hashboolean(t,p) hashpow2(t, p)
2471 +#define hashint(t,i) hashpow2(t,i)
2474 ** for some types, it is better to avoid modulus by power of 2, as
2475 ** they tend to have many 2 factors.
2477 #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
2480 #define hashpointer(t,p) hashmod(t, IntPoint(p))
2484 -** number of ints inside a lua_Number
2486 -#define numints cast_int(sizeof(lua_Number)/sizeof(int))
2490 #define dummynode (&dummynode_)
2492 static const Node dummynode_ = {
2493 @@ -80,27 +71,46 @@ static const Node dummynode_ = {
2496 ** hash for lua_Numbers
2498 +** for non-complex modes, never called with 'lua_Integer' value range (s.a. 0)
2500 static Node *hashnum (const Table *t, lua_Number n) {
2501 - unsigned int a[numints];
2503 - if (luai_numeq(n, 0)) /* avoid problems with -0 */
2504 - return gnode(t, 0);
2505 - memcpy(a, &n, sizeof(a));
2506 - for (i = 1; i < numints; i++) a[0] += a[i];
2507 - return hashmod(t, a[0]);
2508 + const unsigned int *p= cast(const unsigned int *,&n);
2509 + unsigned int sum= *p;
2510 + unsigned int m= sizeof(lua_Number)/sizeof(int);
2512 + /* OS X Intel has 'm'==4 and gives "Bus error" if the last integer of
2513 + * 'n' is read; the actual size of long double is only 80 bits = 10 bytes.
2514 + * Linux x86 has 'm'==3, and does not require reduction.
2516 +#if defined(LNUM_LDOUBLE) && defined(__i386__)
2519 + for (i = 1; i < m; i++) sum += p[i];
2520 + return hashmod(t, sum);
2526 ** returns the `main' position of an element in a table (that is, the index
2527 ** of its hash value)
2529 +** Floating point numbers with integer value give the hash position of the
2530 +** integer (so they use the same table position).
2532 static Node *mainposition (const Table *t, const TValue *key) {
2534 switch (ttype(key)) {
2536 - return hashnum(t, nvalue(key));
2537 + if (tt_integer_valued(key,&i))
2538 + return hashint(t, i);
2539 +#ifdef LNUM_COMPLEX
2540 + if (nvalue_img_fast(key)!=0 && luai_numeq(nvalue_fast(key),0))
2541 + return gnode(t, 0); /* 0 and -0 to give same hash */
2543 + return hashnum(t, nvalue_fast(key));
2545 + return hashint(t, ivalue(key));
2547 return hashstr(t, rawtsvalue(key));
2549 @@ -116,16 +126,20 @@ static Node *mainposition (const Table *
2551 ** returns the index for `key' if `key' is an appropriate key to live in
2552 ** the array part of the table, -1 otherwise.
2554 +** Anything <=0 is taken as not being in the array part.
2556 -static int arrayindex (const TValue *key) {
2557 - if (ttisnumber(key)) {
2558 - lua_Number n = nvalue(key);
2560 - lua_number2int(k, n);
2561 - if (luai_numeq(cast_num(k), n))
2563 +static int arrayindex (const TValue *key, int max) {
2565 + switch( ttype(key) ) {
2567 + k= ivalue(key); break;
2569 + if (tt_integer_valued(key,&k)) break;
2571 + return -1; /* not to be used as array index */
2573 - return -1; /* `key' did not match some condition */
2574 + return ((k>0) && (k <= max)) ? cast_int(k) : -1;
2578 @@ -137,8 +151,8 @@ static int arrayindex (const TValue *key
2579 static int findindex (lua_State *L, Table *t, StkId key) {
2581 if (ttisnil(key)) return -1; /* first iteration */
2582 - i = arrayindex(key);
2583 - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
2584 + i = arrayindex(key, t->sizearray);
2585 + if (i>0) /* inside array part? */
2586 return i-1; /* yes; that's the index (corrected to C) */
2588 Node *n = mainposition(t, key);
2589 @@ -163,7 +177,7 @@ int luaH_next (lua_State *L, Table *t, S
2590 int i = findindex(L, t, key); /* find original element */
2591 for (i++; i < t->sizearray; i++) { /* try first array part */
2592 if (!ttisnil(&t->array[i])) { /* a non-nil value? */
2593 - setnvalue(key, cast_num(i+1));
2594 + setivalue(key, i+1);
2595 setobj2s(L, key+1, &t->array[i]);
2598 @@ -209,8 +223,8 @@ static int computesizes (int nums[], int
2601 static int countint (const TValue *key, int *nums) {
2602 - int k = arrayindex(key);
2603 - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
2604 + int k = arrayindex(key,MAXASIZE);
2605 + if (k>0) { /* appropriate array index? */
2606 nums[ceillog2(k)]++; /* count as such */
2609 @@ -308,7 +322,7 @@ static void resize (lua_State *L, Table
2610 /* re-insert elements from vanishing slice */
2611 for (i=nasize; i<oldasize; i++) {
2612 if (!ttisnil(&t->array[i]))
2613 - setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
2614 + setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]);
2617 luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
2618 @@ -409,7 +423,9 @@ static TValue *newkey (lua_State *L, Tab
2619 othern = mainposition(t, key2tval(mp));
2620 if (othern != mp) { /* is colliding node out of its main position? */
2621 /* yes; move colliding node into free position */
2622 - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
2623 + while (gnext(othern) != mp) {
2624 + othern = gnext(othern); /* find previous */
2626 gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
2627 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
2628 gnext(mp) = NULL; /* now `mp' is free */
2629 @@ -432,17 +448,18 @@ static TValue *newkey (lua_State *L, Tab
2631 ** search function for integers
2633 -const TValue *luaH_getnum (Table *t, int key) {
2634 +const TValue *luaH_getint (Table *t, lua_Integer key) {
2635 /* (1 <= key && key <= t->sizearray) */
2636 if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
2637 return &t->array[key-1];
2639 - lua_Number nk = cast_num(key);
2640 - Node *n = hashnum(t, nk);
2641 + Node *n = hashint(t, key);
2642 do { /* check whether `key' is somewhere in the chain */
2643 - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
2644 + if (ttisint(gkey(n)) && (ivalue(gkey(n)) == key)) {
2645 return gval(n); /* that's it */
2646 - else n = gnext(n);
2651 return luaO_nilobject;
2653 @@ -470,14 +487,12 @@ const TValue *luaH_get (Table *t, const
2654 switch (ttype(key)) {
2655 case LUA_TNIL: return luaO_nilobject;
2656 case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
2657 + case LUA_TINT: return luaH_getint(t, ivalue(key));
2660 - lua_Number n = nvalue(key);
2661 - lua_number2int(k, n);
2662 - if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
2663 - return luaH_getnum(t, k); /* use specialized version */
2664 - /* else go through */
2667 + if (tt_integer_valued(key,&i))
2668 + return luaH_getint(t,i);
2669 + } /* pass through */
2671 Node *n = mainposition(t, key);
2672 do { /* check whether `key' is somewhere in the chain */
2673 @@ -498,20 +513,25 @@ TValue *luaH_set (lua_State *L, Table *t
2674 return cast(TValue *, p);
2676 if (ttisnil(key)) luaG_runerror(L, "table index is nil");
2677 - else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
2678 - luaG_runerror(L, "table index is NaN");
2679 + else if (ttype(key)==LUA_TNUMBER) {
2681 + if (luai_numisnan(nvalue_fast(key)))
2682 + luaG_runerror(L, "table index is NaN");
2683 + if (tt_integer_valued(key,&k))
2684 + return luaH_setint(L, t, k);
2686 return newkey(L, t, key);
2691 -TValue *luaH_setnum (lua_State *L, Table *t, int key) {
2692 - const TValue *p = luaH_getnum(t, key);
2693 +TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key) {
2694 + const TValue *p = luaH_getint(t, key);
2695 if (p != luaO_nilobject)
2696 return cast(TValue *, p);
2699 - setnvalue(&k, cast_num(key));
2700 + setivalue(&k, key);
2701 return newkey(L, t, &k);
2704 @@ -533,20 +553,21 @@ static int unbound_search (Table *t, uns
2705 unsigned int i = j; /* i is zero or a present index */
2707 /* find `i' and `j' such that i is present and j is not */
2708 - while (!ttisnil(luaH_getnum(t, j))) {
2709 + while (!ttisnil(luaH_getint(t, j))) {
2712 if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
2713 /* table was built with bad purposes: resort to linear search */
2715 - while (!ttisnil(luaH_getnum(t, i))) i++;
2717 + for( i = 1; i<MAX_INT+1; i++ ) {
2718 + if (ttisnil(luaH_getint(t, i))) break;
2720 + return i - 1; /* up to MAX_INT */
2723 /* now do a binary search between them */
2725 unsigned int m = (i+j)/2;
2726 - if (ttisnil(luaH_getnum(t, m))) j = m;
2727 + if (ttisnil(luaH_getint(t, m))) j = m;
2734 #define key2tval(n) (&(n)->i_key.tvk)
2737 -LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
2738 -LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
2739 +LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
2740 +LUAI_FUNC TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key);
2741 LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
2742 LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
2743 LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
2751 const char *const luaT_typenames[] = {
2752 "nil", "boolean", "userdata", "number",
2753 "string", "table", "function", "userdata", "thread",
2754 @@ -67,6 +66,9 @@ const TValue *luaT_gettmbyobj (lua_State
2756 mt = uvalue(o)->metatable;
2759 + mt = G(L)->mt[LUA_TNUMBER];
2762 mt = G(L)->mt[ttype(o)];
2768 #include "lauxlib.h"
2771 +#include "llimits.h"
2774 static lua_State *globalL = NULL;
2775 @@ -382,6 +382,15 @@ int main (int argc, char **argv) {
2776 l_message(argv[0], "cannot create state: not enough memory");
2777 return EXIT_FAILURE;
2779 + /* Checking 'sizeof(lua_Integer)' cannot be made in preprocessor on all compilers.
2782 + lua_assert( sizeof(lua_Integer) == 2 );
2783 +#elif defined(LNUM_INT32)
2784 + lua_assert( sizeof(lua_Integer) == 4 );
2785 +#elif defined(LNUM_INT64)
2786 + lua_assert( sizeof(lua_Integer) == 8 );
2790 status = lua_cpcall(L, &pmain, &s);
2794 #define LUA_VERSION "Lua 5.1"
2795 #define LUA_RELEASE "Lua 5.1.4"
2796 #define LUA_VERSION_NUM 501
2797 -#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio"
2798 +#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" " (" LUA_LNUM ")"
2799 #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
2802 @@ -71,6 +71,16 @@ typedef void * (*lua_Alloc) (void *ud, v
2804 #define LUA_TNONE (-1)
2806 +/* LUA_TINT is an internal type, not visible to applications. There are three
2807 + * potential values where it can be tweaked to (code autoadjusts to these):
2809 + * -2: not 'usual' type value; good since 'LUA_TINT' is not part of the API
2810 + * LUA_TNUMBER+1: shifts other type values upwards, breaking binary compatibility
2811 + * not acceptable for 5.1, maybe 5.2 onwards?
2812 + * 9: greater than existing (5.1) type values.
2814 +#define LUA_TINT (-2)
2817 #define LUA_TBOOLEAN 1
2818 #define LUA_TLIGHTUSERDATA 2
2819 @@ -139,6 +149,8 @@ LUA_API int (lua_isuserdata)
2820 LUA_API int (lua_type) (lua_State *L, int idx);
2821 LUA_API const char *(lua_typename) (lua_State *L, int tp);
2823 +LUA_API int (lua_isinteger) (lua_State *L, int idx);
2825 LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
2826 LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
2827 LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
2828 @@ -244,6 +256,19 @@ LUA_API lua_Alloc (lua_getallocf) (lua_S
2829 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
2833 +* It is unnecessary to break Lua C API 'lua_tonumber()' compatibility, just
2834 +* because the Lua number type is complex. Most C modules would use scalars
2835 +* only. We'll introduce new 'lua_tocomplex' and 'lua_pushcomplex' for when
2836 +* the module really wants to use them.
2838 +#ifdef LNUM_COMPLEX
2839 + #include <complex.h>
2840 + typedef LUA_NUMBER complex lua_Complex;
2841 + LUA_API lua_Complex (lua_tocomplex) (lua_State *L, int idx);
2842 + LUA_API void (lua_pushcomplex) (lua_State *L, lua_Complex v);
2847 ** ===============================================================
2848 @@ -268,7 +293,12 @@ LUA_API void lua_setallocf (lua_State *L
2849 #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
2850 #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
2851 #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
2852 -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
2855 +# define lua_isnoneornil(L, n) ( (lua_type(L,(n)) <= 0) && (lua_type(L,(n)) != LUA_TINT) )
2857 +# define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
2860 #define lua_pushliteral(L, s) \
2861 lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
2862 @@ -386,3 +416,4 @@ struct lua_Debug {
2875 +# include <assert.h>
2879 ** ==================================================================
2880 @@ -136,14 +138,38 @@
2884 -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
2885 -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
2886 -** machines, ptrdiff_t gives a good choice between int or long.)
2887 +@@ LUAI_BITSINT defines the number of bits in an int.
2888 +** CHANGE here if Lua cannot automatically detect the number of bits of
2889 +** your machine. Probably you do not need to change this.
2891 -#define LUA_INTEGER ptrdiff_t
2892 +/* avoid overflows in comparison */
2893 +#if INT_MAX-20 < 32760
2894 +#define LUAI_BITSINT 16
2895 +#elif INT_MAX > 2147483640L
2896 +/* int has at least 32 bits */
2897 +#define LUAI_BITSINT 32
2899 +#error "you must define LUA_BITSINT with number of bits in an integer"
2904 +@@ LNUM_DOUBLE |Â LNUM_FLOAT |Â LNUM_LDOUBLE: Generic Lua number mode
2905 +@@ LNUM_INT32 | LNUM_INT64: Integer type
2906 +@@ LNUM_COMPLEX: Define for using 'a+bi' numbers
2908 +@@ You can combine LNUM_xxx but only one of each group. I.e. '-DLNUM_FLOAT
2909 +@@ -DLNUM_INT32 -DLNUM_COMPLEX' gives float range complex numbers, with
2910 +@@ 32-bit scalar integer range optimized.
2912 +** These are kept in a separate configuration file mainly for ease of patching
2913 +** (can be changed if integerated to Lua proper).
2915 +/*#define LNUM_DOUBLE*/
2916 +/*#define LNUM_INT32*/
2917 +#include "lnum_config.h"
2920 @@ LUA_API is a mark for all core API functions.
2921 @@ LUALIB_API is a mark for all standard library functions.
2922 ** CHANGE them if you need to define those functions in some special way.
2923 @@ -383,22 +409,6 @@
2927 -@@ LUAI_BITSINT defines the number of bits in an int.
2928 -** CHANGE here if Lua cannot automatically detect the number of bits of
2929 -** your machine. Probably you do not need to change this.
2931 -/* avoid overflows in comparison */
2932 -#if INT_MAX-20 < 32760
2933 -#define LUAI_BITSINT 16
2934 -#elif INT_MAX > 2147483640L
2935 -/* int has at least 32 bits */
2936 -#define LUAI_BITSINT 32
2938 -#error "you must define LUA_BITSINT with number of bits in an integer"
2943 @@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
2944 @@ LUAI_INT32 is an signed integer with at least 32 bits.
2945 @@ LUAI_UMEM is an unsigned integer big enough to count the total
2946 @@ -425,6 +435,15 @@
2947 #define LUAI_MEM long
2951 +@@ LUAI_BOOL carries 0 and nonzero (normally 1). It may be defined as 'char'
2952 +** (to save memory), 'int' (for speed), 'bool' (for C++) or '_Bool' (C99)
2955 +# define LUAI_BOOL bool
2957 +# define LUAI_BOOL int
2961 @@ LUAI_MAXCALLS limits the number of nested calls.
2962 @@ -490,101 +509,6 @@
2963 /* }================================================================== */
2969 -** {==================================================================
2970 -@@ LUA_NUMBER is the type of numbers in Lua.
2971 -** CHANGE the following definitions only if you want to build Lua
2972 -** with a number type different from double. You may also need to
2973 -** change lua_number2int & lua_number2integer.
2974 -** ===================================================================
2977 -#define LUA_NUMBER_DOUBLE
2978 -#define LUA_NUMBER double
2981 -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
2984 -#define LUAI_UACNUMBER double
2988 -@@ LUA_NUMBER_SCAN is the format for reading numbers.
2989 -@@ LUA_NUMBER_FMT is the format for writing numbers.
2990 -@@ lua_number2str converts a number to a string.
2991 -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
2992 -@@ lua_str2number converts a string to a number.
2994 -#define LUA_NUMBER_SCAN "%lf"
2995 -#define LUA_NUMBER_FMT "%.14g"
2996 -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
2997 -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
2998 -#define lua_str2number(s,p) strtod((s), (p))
3002 -@@ The luai_num* macros define the primitive operations over numbers.
3004 -#if defined(LUA_CORE)
3006 -#define luai_numadd(a,b) ((a)+(b))
3007 -#define luai_numsub(a,b) ((a)-(b))
3008 -#define luai_nummul(a,b) ((a)*(b))
3009 -#define luai_numdiv(a,b) ((a)/(b))
3010 -#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
3011 -#define luai_numpow(a,b) (pow(a,b))
3012 -#define luai_numunm(a) (-(a))
3013 -#define luai_numeq(a,b) ((a)==(b))
3014 -#define luai_numlt(a,b) ((a)<(b))
3015 -#define luai_numle(a,b) ((a)<=(b))
3016 -#define luai_numisnan(a) (!luai_numeq((a), (a)))
3021 -@@ lua_number2int is a macro to convert lua_Number to int.
3022 -@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
3023 -** CHANGE them if you know a faster way to convert a lua_Number to
3024 -** int (with any rounding method and without throwing errors) in your
3025 -** system. In Pentium machines, a naive typecast from double to int
3026 -** in C is extremely slow, so any alternative is worth trying.
3029 -/* On a Pentium, resort to a trick */
3030 -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
3031 - (defined(__i386) || defined (_M_IX86) || defined(__i386__))
3033 -/* On a Microsoft compiler, use assembler */
3034 -#if defined(_MSC_VER)
3036 -#define lua_number2int(i,d) __asm fld d __asm fistp i
3037 -#define lua_number2integer(i,n) lua_number2int(i, n)
3039 -/* the next trick should work on any Pentium, but sometimes clashes
3040 - with a DirectX idiosyncrasy */
3043 -union luai_Cast { double l_d; long l_l; };
3044 -#define lua_number2int(i,d) \
3045 - { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
3046 -#define lua_number2integer(i,n) lua_number2int(i, n)
3051 -/* this option always works, but may be slow */
3053 -#define lua_number2int(i,d) ((i)=(int)(d))
3054 -#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
3058 -/* }================================================================== */
3062 @@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
3063 ** CHANGE it if your system requires alignments larger than double. (For
3064 @@ -728,28 +652,6 @@ union luai_Cast { double l_d; long l_l;
3065 #define luai_userstateyield(L,n) ((void)L)
3069 -@@ LUA_INTFRMLEN is the length modifier for integer conversions
3070 -@* in 'string.format'.
3071 -@@ LUA_INTFRM_T is the integer type correspoding to the previous length
3073 -** CHANGE them if your system supports long long or does not support long.
3076 -#if defined(LUA_USELONGLONG)
3078 -#define LUA_INTFRMLEN "ll"
3079 -#define LUA_INTFRM_T long long
3083 -#define LUA_INTFRMLEN "l"
3084 -#define LUA_INTFRM_T long
3090 /* =================================================================== */
3095 @@ -73,6 +73,13 @@ static lua_Number LoadNumber(LoadState*
3099 +static lua_Integer LoadInteger(LoadState* S)
3106 static TString* LoadString(LoadState* S)
3109 @@ -119,6 +126,9 @@ static void LoadConstants(LoadState* S,
3111 setnvalue(o,LoadNumber(S));
3113 + case LUA_TINT: /* Integer type saved in bytecode (see lcode.c) */
3114 + setivalue(o,LoadInteger(S));
3117 setsvalue2n(S->L,o,LoadString(S));
3119 @@ -223,5 +233,22 @@ void luaU_header (char* h)
3120 *h++=(char)sizeof(size_t);
3121 *h++=(char)sizeof(Instruction);
3122 *h++=(char)sizeof(lua_Number);
3123 - *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
3126 + * Last byte of header (0/1 in unpatched Lua 5.1.3):
3128 + * 0: lua_Number is float or double, lua_Integer not used. (nonpatched only)
3129 + * 1: lua_Number is integer (nonpatched only)
3131 + * +2: LNUM_INT16: sizeof(lua_Integer)
3132 + * +4: LNUM_INT32: sizeof(lua_Integer)
3133 + * +8: LNUM_INT64: sizeof(lua_Integer)
3135 + * +0x80: LNUM_COMPLEX
3137 + *h++ = (char)(sizeof(lua_Integer)
3138 +#ifdef LNUM_COMPLEX
3154 /* limit for table tag-method chains (to avoid loops) */
3155 #define MAXTAGLOOP 100
3158 -const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
3161 + * If 'obj' is a string, it is tried to be interpreted as a number.
3163 +const TValue *luaV_tonumber ( const TValue *obj, TValue *n) {
3167 if (ttisnumber(obj)) return obj;
3168 - if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
3169 - setnvalue(n, num);
3175 + if (ttisstring(obj)) {
3176 + switch( luaO_str2d( svalue(obj), &d, &i ) ) {
3178 + setivalue(n,i); return n;
3180 + setnvalue(n,d); return n;
3181 +#ifdef LNUM_COMPLEX
3182 + case TK_NUMBER2: /* "N.NNNi", != 0 */
3183 + setnvalue_complex_fast(n, d*I); return n;
3191 @@ -49,8 +62,7 @@ int luaV_tostring (lua_State *L, StkId o
3194 char s[LUAI_MAXNUMBER2STR];
3195 - lua_Number n = nvalue(obj);
3196 - lua_number2str(s, n);
3197 + luaO_num2buf(s,obj);
3198 setsvalue2s(L, obj, luaS_new(L, s));
3201 @@ -218,59 +230,127 @@ static int l_strcmp (const TString *ls,
3205 +#ifdef LNUM_COMPLEX
3206 +void error_complex( lua_State *L, const TValue *l, const TValue *r )
3208 + char buf1[ LUAI_MAXNUMBER2STR ];
3209 + char buf2[ LUAI_MAXNUMBER2STR ];
3210 + luaO_num2buf( buf1, l );
3211 + luaO_num2buf( buf2, r );
3212 + luaG_runerror( L, "unable to compare: %s with %s", buf1, buf2 );
3218 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
3220 - if (ttype(l) != ttype(r))
3224 + if (!ttype_ext_same(l,r))
3225 return luaG_ordererror(L, l, r);
3226 - else if (ttisnumber(l))
3227 - return luai_numlt(nvalue(l), nvalue(r));
3228 - else if (ttisstring(l))
3229 - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
3230 - else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
3231 +#ifdef LNUM_COMPLEX
3232 + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
3233 + error_complex( L, l, r );
3235 + tl= ttype(l); tr= ttype(r);
3236 + if (tl==tr) { /* clear arithmetics */
3238 + case LUA_TINT: return ivalue(l) < ivalue(r);
3239 + case LUA_TNUMBER: return luai_numlt(nvalue_fast(l), nvalue_fast(r));
3240 + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
3242 + } else if (tl==LUA_TINT) { /* l:int, r:num */
3243 + /* Avoid accuracy losing casts: if 'r' is integer by value, do comparisons
3244 + * in integer realm. Only otherwise cast 'l' to FP (which might change its
3247 + if (tt_integer_valued(r,&tmp))
3248 + return ivalue(l) < tmp;
3250 + return luai_numlt( cast_num(ivalue(l)), nvalue_fast(r) );
3252 + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
3253 + if (tt_integer_valued(l,&tmp))
3254 + return tmp < ivalue(r);
3256 + return luai_numlt( nvalue_fast(l), cast_num(ivalue(r)) );
3258 + } else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
3261 return luaG_ordererror(L, l, r);
3265 static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
3267 - if (ttype(l) != ttype(r))
3271 + if (!ttype_ext_same(l,r))
3272 return luaG_ordererror(L, l, r);
3273 - else if (ttisnumber(l))
3274 - return luai_numle(nvalue(l), nvalue(r));
3275 - else if (ttisstring(l))
3276 - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
3277 - else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
3278 +#ifdef LNUM_COMPLEX
3279 + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
3280 + error_complex( L, l, r );
3282 + tl= ttype(l); tr= ttype(r);
3283 + if (tl==tr) { /* clear arithmetics */
3285 + case LUA_TINT: return ivalue(l) <= ivalue(r);
3286 + case LUA_TNUMBER: return luai_numle(nvalue_fast(l), nvalue_fast(r));
3287 + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
3290 + if (tl==LUA_TINT) { /* l:int, r:num */
3291 + if (tt_integer_valued(r,&tmp))
3292 + return ivalue(l) <= tmp;
3294 + return luai_numle( cast_num(ivalue(l)), nvalue_fast(r) );
3296 + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
3297 + if (tt_integer_valued(l,&tmp))
3298 + return tmp <= ivalue(r);
3300 + return luai_numle( nvalue_fast(l), cast_num(ivalue(r)) );
3302 + } else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
3304 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
3307 return luaG_ordererror(L, l, r);
3311 -int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
3312 +/* Note: 'luaV_equalval()' and 'luaO_rawequalObj()' have largely overlapping
3313 + * implementation. LUA_TNIL..LUA_TLIGHTUSERDATA cases could be handled
3314 + * simply by the 'default' case here.
3316 +int luaV_equalval (lua_State *L, const TValue *l, const TValue *r) {
3318 - lua_assert(ttype(t1) == ttype(t2));
3319 - switch (ttype(t1)) {
3320 + lua_assert(ttype_ext_same(l,r));
3321 + switch (ttype(l)) {
3322 case LUA_TNIL: return 1;
3323 - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
3324 - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
3325 - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
3327 + case LUA_TNUMBER: return luaO_rawequalObj(l,r);
3328 + case LUA_TBOOLEAN: return bvalue(l) == bvalue(r); /* true must be 1 !! */
3329 + case LUA_TLIGHTUSERDATA: return pvalue(l) == pvalue(r);
3330 case LUA_TUSERDATA: {
3331 - if (uvalue(t1) == uvalue(t2)) return 1;
3332 - tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
3334 + if (uvalue(l) == uvalue(r)) return 1;
3335 + tm = get_compTM(L, uvalue(l)->metatable, uvalue(r)->metatable, TM_EQ);
3336 break; /* will try TM */
3339 - if (hvalue(t1) == hvalue(t2)) return 1;
3340 - tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
3341 + if (hvalue(l) == hvalue(r)) return 1;
3342 + tm = get_compTM(L, hvalue(l)->metatable, hvalue(r)->metatable, TM_EQ);
3343 break; /* will try TM */
3345 - default: return gcvalue(t1) == gcvalue(t2);
3346 + default: return gcvalue(l) == gcvalue(r);
3348 if (tm == NULL) return 0; /* no TM? */
3349 - callTMres(L, L->top, tm, t1, t2); /* call TM */
3350 + callTMres(L, L->top, tm, l, r); /* call TM */
3351 return !l_isfalse(L->top);
3354 @@ -310,30 +390,6 @@ void luaV_concat (lua_State *L, int tota
3358 -static void Arith (lua_State *L, StkId ra, const TValue *rb,
3359 - const TValue *rc, TMS op) {
3360 - TValue tempb, tempc;
3361 - const TValue *b, *c;
3362 - if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
3363 - (c = luaV_tonumber(rc, &tempc)) != NULL) {
3364 - lua_Number nb = nvalue(b), nc = nvalue(c);
3366 - case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
3367 - case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
3368 - case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
3369 - case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
3370 - case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
3371 - case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
3372 - case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
3373 - default: lua_assert(0); break;
3376 - else if (!call_binTM(L, rb, rc, ra, op))
3377 - luaG_aritherror(L, rb, rc);
3383 ** some macros for common tasks in `luaV_execute'
3385 @@ -357,17 +413,154 @@ static void Arith (lua_State *L, StkId r
3386 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
3389 -#define arith_op(op,tm) { \
3390 - TValue *rb = RKB(i); \
3391 - TValue *rc = RKC(i); \
3392 - if (ttisnumber(rb) && ttisnumber(rc)) { \
3393 - lua_Number nb = nvalue(rb), nc = nvalue(rc); \
3394 - setnvalue(ra, op(nb, nc)); \
3397 - Protect(Arith(L, ra, rb, rc, tm)); \
3398 +/* Note: if called for unary operations, 'rc'=='rb'.
3400 +static void Arith (lua_State *L, StkId ra, const TValue *rb,
3401 + const TValue *rc, TMS op) {
3402 + TValue tempb, tempc;
3403 + const TValue *b, *c;
3406 + if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
3407 + (c = luaV_tonumber(rc, &tempc)) != NULL) {
3409 + /* Keep integer arithmetics in the integer realm, if possible.
3411 + if (ttisint(b) && ttisint(c)) {
3412 + lua_Integer ib = ivalue(b), ic = ivalue(c);
3413 + lua_Integer *ri = &ra->value.i;
3414 + ra->tt= LUA_TINT; /* part of 'setivalue(ra)' */
3416 + case TM_ADD: if (try_addint( ri, ib, ic)) return; break;
3417 + case TM_SUB: if (try_subint( ri, ib, ic)) return; break;
3418 + case TM_MUL: if (try_mulint( ri, ib, ic)) return; break;
3419 + case TM_DIV: if (try_divint( ri, ib, ic)) return; break;
3420 + case TM_MOD: if (try_modint( ri, ib, ic)) return; break;
3421 + case TM_POW: if (try_powint( ri, ib, ic)) return; break;
3422 + case TM_UNM: if (try_unmint( ri, ib)) return; break;
3423 + default: lua_assert(0);
3426 + /* Fallback to floating point, when leaving range. */
3428 +#ifdef LNUM_COMPLEX
3429 + if ((nvalue_img(b)!=0) || (nvalue_img(c)!=0)) {
3432 + r= -nvalue_complex_fast(b); /* never an integer (or scalar) */
3433 + setnvalue_complex_fast( ra, r );
3435 + lua_Complex bb= nvalue_complex(b), cc= nvalue_complex(c);
3437 + case TM_ADD: r= bb + cc; break;
3438 + case TM_SUB: r= bb - cc; break;
3439 + case TM_MUL: r= bb * cc; break;
3440 + case TM_DIV: r= bb / cc; break;
3442 + luaG_runerror(L, "attempt to use %% on complex numbers"); /* no return */
3443 + case TM_POW: r= luai_vectpow( bb, cc ); break;
3444 + default: lua_assert(0); r=0;
3446 + setnvalue_complex( ra, r );
3451 + nb = nvalue(b); nc = nvalue(c);
3453 + case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); return;
3454 + case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); return;
3455 + case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); return;
3456 + case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); return;
3457 + case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); return;
3458 + case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); return;
3459 + case TM_UNM: setnvalue(ra, luai_numunm(nb)); return;
3460 + default: lua_assert(0);
3464 + /* Either operand not a number */
3465 + if (!call_binTM(L, rb, rc, ra, op))
3466 + luaG_aritherror(L, rb, rc);
3469 +/* Helper macro to sort arithmetic operations into four categories:
3470 + * TK_INT: integer - integer operands
3471 + * TK_NUMBER: number - number (non complex, either may be integer)
3472 + * TK_NUMBER2: complex numbers (at least the other)
3473 + * 0: non-numeric (at least the other)
3475 +#ifdef LNUM_COMPLEX
3476 +static inline int arith_mode( const TValue *rb, const TValue *rc ) {
3477 + if (ttisint(rb) && ttisint(rc)) return TK_INT;
3478 + if (ttiscomplex(rb) || ttiscomplex(rc)) return TK_NUMBER2;
3479 + if (ttisnumber(rb) && ttisnumber(rc)) return TK_NUMBER;
3483 +# define arith_mode(rb,rc) \
3484 + ( (ttisint(rb) && ttisint(rc)) ? TK_INT : \
3485 + (ttisnumber(rb) && ttisnumber(rc)) ? TK_NUMBER : 0 )
3488 +/* arith_op macro for two operators:
3489 + * automatically chooses, which function (number, integer, complex) to use
3491 +#define ARITH_OP2_START( op_num, op_int ) \
3493 + switch( arith_mode(rb,rc) ) { \
3495 + if (op_int ( &(ra)->value.i, ivalue(rb), ivalue(rc) )) \
3496 + { ra->tt= LUA_TINT; break; } /* else flow through */ \
3498 + setnvalue(ra, op_num ( nvalue(rb), nvalue(rc) )); break;
3500 +#define ARITH_OP2_END \
3502 + failed= 1; break; \
3503 + } if (!failed) continue;
3505 +#define arith_op_continue_scalar( op_num, op_int ) \
3506 + ARITH_OP2_START( op_num, op_int ) \
3509 +#ifdef LNUM_COMPLEX
3510 +# define arith_op_continue( op_num, op_int, op_complex ) \
3511 + ARITH_OP2_START( op_num, op_int ) \
3512 + case TK_NUMBER2: \
3513 + setnvalue_complex( ra, op_complex ( nvalue_complex(rb), nvalue_complex(rc) ) ); break; \
3516 +# define arith_op_continue(op_num,op_int,_) arith_op_continue_scalar(op_num,op_int)
3519 +/* arith_op macro for one operator:
3521 +#define ARITH_OP1_START( op_num, op_int ) \
3523 + switch( arith_mode(rb,rb) ) { \
3525 + if (op_int ( &(ra)->value.i, ivalue(rb) )) \
3526 + { ra->tt= LUA_TINT; break; } /* else flow through */ \
3528 + setnvalue(ra, op_num (nvalue(rb))); break; \
3530 +#define ARITH_OP1_END \
3532 + failed= 1; break; \
3533 + } if (!failed) continue;
3535 +#ifdef LNUM_COMPLEX
3536 +# define arith_op1_continue( op_num, op_int, op_complex ) \
3537 + ARITH_OP1_START( op_num, op_int ) \
3538 + case TK_NUMBER2: \
3539 + setnvalue_complex( ra, op_complex ( nvalue_complex_fast(rb) )); break; \
3542 +# define arith_op1_continue( op_num, op_int, _ ) \
3543 + ARITH_OP1_START( op_num, op_int ) \
3548 void luaV_execute (lua_State *L, int nexeccalls) {
3549 @@ -468,38 +661,45 @@ void luaV_execute (lua_State *L, int nex
3553 - arith_op(luai_numadd, TM_ADD);
3554 + TValue *rb = RKB(i), *rc= RKC(i);
3555 + arith_op_continue( luai_numadd, try_addint, luai_vectadd );
3556 + Protect(Arith(L, ra, rb, rc, TM_ADD)); \
3560 - arith_op(luai_numsub, TM_SUB);
3561 + TValue *rb = RKB(i), *rc= RKC(i);
3562 + arith_op_continue( luai_numsub, try_subint, luai_vectsub );
3563 + Protect(Arith(L, ra, rb, rc, TM_SUB));
3567 - arith_op(luai_nummul, TM_MUL);
3568 + TValue *rb = RKB(i), *rc= RKC(i);
3569 + arith_op_continue(luai_nummul, try_mulint, luai_vectmul);
3570 + Protect(Arith(L, ra, rb, rc, TM_MUL));
3574 - arith_op(luai_numdiv, TM_DIV);
3575 + TValue *rb = RKB(i), *rc= RKC(i);
3576 + arith_op_continue(luai_numdiv, try_divint, luai_vectdiv);
3577 + Protect(Arith(L, ra, rb, rc, TM_DIV));
3581 - arith_op(luai_nummod, TM_MOD);
3582 + TValue *rb = RKB(i), *rc= RKC(i);
3583 + arith_op_continue_scalar(luai_nummod, try_modint); /* scalars only */
3584 + Protect(Arith(L, ra, rb, rc, TM_MOD));
3588 - arith_op(luai_numpow, TM_POW);
3589 + TValue *rb = RKB(i), *rc= RKC(i);
3590 + arith_op_continue(luai_numpow, try_powint, luai_vectpow);
3591 + Protect(Arith(L, ra, rb, rc, TM_POW));
3596 - if (ttisnumber(rb)) {
3597 - lua_Number nb = nvalue(rb);
3598 - setnvalue(ra, luai_numunm(nb));
3601 - Protect(Arith(L, ra, rb, rb, TM_UNM));
3603 + arith_op1_continue(luai_numunm, try_unmint, luai_vectunm);
3604 + Protect(Arith(L, ra, rb, rb, TM_UNM));
3608 @@ -511,11 +711,11 @@ void luaV_execute (lua_State *L, int nex
3609 const TValue *rb = RB(i);
3610 switch (ttype(rb)) {
3612 - setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
3613 + setivalue(ra, luaH_getn(hvalue(rb)));
3617 - setnvalue(ra, cast_num(tsvalue(rb)->len));
3618 + setivalue(ra, tsvalue(rb)->len);
3621 default: { /* try metamethod */
3622 @@ -648,14 +848,30 @@ void luaV_execute (lua_State *L, int nex
3626 - lua_Number step = nvalue(ra+2);
3627 - lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
3628 - lua_Number limit = nvalue(ra+1);
3629 - if (luai_numlt(0, step) ? luai_numle(idx, limit)
3630 - : luai_numle(limit, idx)) {
3631 - dojump(L, pc, GETARG_sBx(i)); /* jump back */
3632 - setnvalue(ra, idx); /* update internal index... */
3633 - setnvalue(ra+3, idx); /* ...and external index */
3634 + /* If start,step and limit are all integers, we don't need to check
3635 + * against overflow in the looping.
3637 + if (ttisint(ra) && ttisint(ra+1) && ttisint(ra+2)) {
3638 + lua_Integer step = ivalue(ra+2);
3639 + lua_Integer idx = ivalue(ra) + step; /* increment index */
3640 + lua_Integer limit = ivalue(ra+1);
3641 + if (step > 0 ? (idx <= limit) : (limit <= idx)) {
3642 + dojump(L, pc, GETARG_sBx(i)); /* jump back */
3643 + setivalue(ra, idx); /* update internal index... */
3644 + setivalue(ra+3, idx); /* ...and external index */
3647 + /* non-integer looping (don't use 'nvalue_fast', some may be integer!)
3649 + lua_Number step = nvalue(ra+2);
3650 + lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
3651 + lua_Number limit = nvalue(ra+1);
3652 + if (luai_numlt(0, step) ? luai_numle(idx, limit)
3653 + : luai_numle(limit, idx)) {
3654 + dojump(L, pc, GETARG_sBx(i)); /* jump back */
3655 + setnvalue(ra, idx); /* update internal index... */
3656 + setnvalue(ra+3, idx); /* ...and external index */
3661 @@ -664,13 +880,21 @@ void luaV_execute (lua_State *L, int nex
3662 const TValue *plimit = ra+1;
3663 const TValue *pstep = ra+2;
3664 L->savedpc = pc; /* next steps may throw errors */
3665 + /* Using same location for tonumber's both arguments, effectively does
3666 + * in-place modification (string->number). */
3667 if (!tonumber(init, ra))
3668 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
3669 else if (!tonumber(plimit, ra+1))
3670 luaG_runerror(L, LUA_QL("for") " limit must be a number");
3671 else if (!tonumber(pstep, ra+2))
3672 luaG_runerror(L, LUA_QL("for") " step must be a number");
3673 - setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
3674 + /* Step back one value (keep within integers if we can)
3676 + if (!( ttisint(ra) && ttisint(pstep) &&
3677 + try_subint( &ra->value.i, ivalue(ra), ivalue(pstep) ) )) {
3678 + /* don't use 'nvalue_fast()', values may be integer */
3679 + setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
3681 dojump(L, pc, GETARG_sBx(i));
3684 @@ -707,7 +931,7 @@ void luaV_execute (lua_State *L, int nex
3685 luaH_resizearray(L, h, last); /* pre-alloc it at once */
3686 for (; n > 0; n--) {
3688 - setobj2t(L, luaH_setnum(L, h, last--), val);
3689 + setobj2t(L, luaH_setint(L, h, last--), val);
3690 luaC_barriert(L, h, val);
3697 #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
3699 -#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
3700 - (((o) = luaV_tonumber(o,n)) != NULL))
3701 +#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
3703 -#define equalobj(L,o1,o2) \
3704 - (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
3705 +#define equalobj(L,o1,o2) (ttype_ext_same(o1,o2) && luaV_equalval(L, o1, o2))
3708 LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
3712 #include "lobject.h"
3713 #include "lopcodes.h"
3714 #include "lundump.h"
3717 #define PrintFunction luaU_print
3719 @@ -59,8 +60,16 @@ static void PrintConstant(const Proto* f
3721 printf(bvalue(o) ? "true" : "false");
3724 + printf(LUA_INTEGER_FMT,ivalue(o));
3727 - printf(LUA_NUMBER_FMT,nvalue(o));
3728 +#ifdef LNUM_COMPLEX
3729 + // TBD: Do we get complex values here?
3730 + { lua_Number b= nvalue_img_fast(o);
3731 + printf( LUA_NUMBER_FMT "%s" LUA_NUMBER_FMT "i", nvalue_fast(o), b>=0 ? "+":"", b ); }
3733 + printf(LUA_NUMBER_FMT,nvalue_fast(o));
3736 PrintString(rawtsvalue(o));