7 -TO_INC= lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp
8 +TO_INC= lua.h luaconf.h lualib.h lauxlib.h lnum_config.h ../etc/lua.hpp
14 @@ -25,7 +25,7 @@ PLATS= aix ansi bsd freebsd generic linu
16 CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
17 lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
18 - lundump.o lvm.o lzio.o
19 + lundump.o lvm.o lzio.o lnum.o
20 LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
21 lstrlib.o loadlib.o linit.o
23 @@ -148,6 +148,7 @@ llex.o: llex.c lua.h luaconf.h ldo.h lob
24 lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
25 lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
26 ltm.h lzio.h lmem.h ldo.h
27 +lnum.o: lnum.c lua.h llex.h lnum.h
28 loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
29 lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \
30 ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h
31 @@ -179,4 +180,18 @@ lzio.o: lzio.c lua.h luaconf.h llimits.h
32 print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \
33 ltm.h lzio.h lmem.h lopcodes.h lundump.h
35 +luaconf.h: lnum_config.h
38 +lbaselib.c: llimits.h lobject.h lapi.h
40 +liolib.c: lnum.h llex.h
43 +lobject.c: llex.h lnum.h
60 const char lua_ident[] =
61 @@ -241,12 +241,13 @@ LUA_API void lua_pushvalue (lua_State *L
63 LUA_API int lua_type (lua_State *L, int idx) {
64 StkId o = index2adr(L, idx);
65 - return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
66 + return (o == luaO_nilobject) ? LUA_TNONE : ttype_ext(o);
70 LUA_API const char *lua_typename (lua_State *L, int t) {
72 + lua_assert( t!= LUA_TINT );
73 return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
76 @@ -264,6 +265,14 @@ LUA_API int lua_isnumber (lua_State *L,
80 +LUA_API int lua_isinteger (lua_State *L, int idx) {
83 + const TValue *o = index2adr(L, idx);
84 + return tonumber(o,&tmp) && (ttisint(o) || tt_integer_valued(o,&dum));
88 LUA_API int lua_isstring (lua_State *L, int idx) {
89 int t = lua_type(L, idx);
90 return (t == LUA_TSTRING || t == LUA_TNUMBER);
91 @@ -309,31 +318,66 @@ LUA_API int lua_lessthan (lua_State *L,
96 LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
98 const TValue *o = index2adr(L, idx);
99 - if (tonumber(o, &n))
100 + if (tonumber(o, &n)) {
102 + if (nvalue_img(o) != 0)
103 + luaG_runerror(L, "expecting a real number");
113 LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
115 + /* Lua 5.1 documented behaviour is to return nonzero for non-integer:
116 + * "If the number is not an integer, it is truncated in some non-specified way."
117 + * I would suggest to change this, to return 0 for anything that would
118 + * not fit in 'lua_Integer'.
120 +#ifdef LUA_COMPAT_TOINTEGER
121 + /* Lua 5.1 compatible */
122 const TValue *o = index2adr(L, idx);
123 if (tonumber(o, &n)) {
125 - lua_Number num = nvalue(o);
126 - lua_number2integer(res, num);
130 + if (ttisint(o)) return ivalue(o);
132 +# ifdef LNUM_COMPLEX
133 + if (nvalue_img_fast(o) != 0)
134 + luaG_runerror(L, "expecting a real number");
136 + lua_number2integer(i, d);
142 + /* New suggestion */
143 + const TValue *o = index2adr(L, idx);
144 + if (tonumber(o, &n)) {
146 + if (ttisint(o)) return ivalue(o);
147 + if (tt_integer_valued(o,&i)) return i;
155 +LUA_API lua_Complex lua_tocomplex (lua_State *L, int idx) {
157 + const TValue *o = index2adr(L, idx);
158 + if (tonumber(o, &tmp))
159 + return nvalue_complex(o);
165 LUA_API int lua_toboolean (lua_State *L, int idx) {
166 const TValue *o = index2adr(L, idx);
167 return !l_isfalse(o);
168 @@ -364,6 +408,7 @@ LUA_API size_t lua_objlen (lua_State *L,
169 case LUA_TSTRING: return tsvalue(o)->len;
170 case LUA_TUSERDATA: return uvalue(o)->len;
171 case LUA_TTABLE: return luaH_getn(hvalue(o));
175 lua_lock(L); /* `luaV_tostring' may create a new string */
176 @@ -426,6 +471,8 @@ LUA_API void lua_pushnil (lua_State *L)
180 +/* 'lua_pushnumber()' may lose accuracy on integers, 'lua_pushinteger' will not.
182 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
184 setnvalue(L->top, n);
185 @@ -434,12 +481,22 @@ LUA_API void lua_pushnumber (lua_State *
189 -LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
190 +LUA_API void lua_pushinteger (lua_State *L, lua_Integer i) {
192 + setivalue(L->top, i);
199 +LUA_API void lua_pushcomplex (lua_State *L, lua_Complex v) {
201 - setnvalue(L->top, cast_num(n));
202 + setnvalue_complex( L->top, v );
209 LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
210 @@ -569,7 +626,7 @@ LUA_API void lua_rawgeti (lua_State *L,
212 o = index2adr(L, idx);
213 api_check(L, ttistable(o));
214 - setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
215 + setobj2s(L, L->top, luaH_getint(hvalue(o), n));
219 @@ -597,6 +654,9 @@ LUA_API int lua_getmetatable (lua_State
221 mt = uvalue(obj)->metatable;
224 + mt = G(L)->mt[LUA_TNUMBER];
227 mt = G(L)->mt[ttype(obj)];
229 @@ -687,7 +747,7 @@ LUA_API void lua_rawseti (lua_State *L,
230 api_checknelems(L, 1);
231 o = index2adr(L, idx);
232 api_check(L, ttistable(o));
233 - setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
234 + setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
235 luaC_barriert(L, hvalue(o), L->top-1);
238 @@ -721,7 +781,7 @@ LUA_API int lua_setmetatable (lua_State
242 - G(L)->mt[ttype(obj)] = mt;
243 + G(L)->mt[ttype_ext(obj)] = mt;
247 @@ -1085,3 +1145,32 @@ LUA_API const char *lua_setupvalue (lua_
252 +/* Help function for 'luaB_tonumber()', avoids multiple str->number
253 + * conversions for Lua "tonumber()".
255 + * Also pushes floating point numbers with integer value as integer, which
256 + * can be used by 'tonumber()' in scripts to bring values back to integer
259 + * Note: The 'back to integer realm' is _not_ to affect string conversions:
260 + * 'tonumber("4294967295.1")' should give a floating point value, although
261 + * the value would be 4294967296 (and storable in int64 realm).
263 +int lua_pushvalue_as_number (lua_State *L, int idx)
265 + const TValue *o = index2adr(L, idx);
268 + if (ttisnumber(o)) {
269 + if ( (!ttisint(o)) && tt_integer_valued(o,&i)) {
270 + lua_pushinteger( L, i );
273 + } else if (!tonumber(o, &tmp)) {
276 + if (ttisint(o)) lua_pushinteger( L, ivalue(o) );
277 + else lua_pushnumber( L, nvalue_fast(o) );
284 LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);
286 +int lua_pushvalue_as_number (lua_State *L, int idx);
296 +#include "llimits.h"
298 #define FREELIST_REF 0 /* free list of references */
300 @@ -66,7 +66,7 @@ LUALIB_API int luaL_typerror (lua_State
303 static void tag_error (lua_State *L, int narg, int tag) {
304 - luaL_typerror(L, narg, lua_typename(L, tag));
305 + luaL_typerror(L, narg, tag==LUA_TINT ? "integer" : lua_typename(L, tag));
309 @@ -188,8 +188,8 @@ LUALIB_API lua_Number luaL_optnumber (lu
311 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
312 lua_Integer d = lua_tointeger(L, narg);
313 - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
314 - tag_error(L, narg, LUA_TNUMBER);
315 + if (d == 0 && !lua_isinteger(L, narg)) /* avoid extra test when d is not 0 */
316 + tag_error(L, narg, LUA_TINT);
320 @@ -200,6 +200,16 @@ LUALIB_API lua_Integer luaL_optinteger (
325 +LUALIB_API lua_Complex luaL_checkcomplex (lua_State *L, int narg) {
326 + lua_Complex c = lua_tocomplex(L, narg);
327 + if (c == 0 && !lua_isnumber(L, narg)) /* avoid extra test when c is not 0 */
328 + tag_error(L, narg, LUA_TNUMBER);
334 LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
335 if (!lua_getmetatable(L, obj)) /* no metatable? */
339 @@ -57,6 +57,12 @@ LUALIB_API lua_Number (luaL_optnumber) (
340 LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
341 LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
343 +#define luaL_checkint32(L,narg) ((int)luaL_checkinteger(L,narg))
344 +#define luaL_optint32(L,narg,def) ((int)luaL_optinteger(L,narg,def))
347 + LUALIB_API lua_Complex (luaL_checkcomplex) (lua_State *L, int narg);
350 LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
351 LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
359 +#include "llimits.h"
360 +#include "lobject.h"
365 @@ -54,20 +56,25 @@ static int luaB_tonumber (lua_State *L)
366 int base = luaL_optint(L, 2, 10);
367 if (base == 10) { /* standard conversion */
369 - if (lua_isnumber(L, 1)) {
370 - lua_pushnumber(L, lua_tonumber(L, 1));
371 + if (lua_isnumber(L, 1)) { /* numeric string, or a number */
372 + lua_pushvalue_as_number(L,1); /* API extension (not to lose accuracy here) */
378 const char *s1 = luaL_checkstring(L, 1);
381 + unsigned LUA_INTEGER n;
382 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
383 - n = strtoul(s1, &s2, base);
384 + n = lua_str2ul(s1, &s2, base);
385 if (s1 != s2) { /* at least one valid digit? */
386 while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
387 if (*s2 == '\0') { /* no invalid trailing characters? */
389 + /* Push as number, there needs to be separate 'luaB_tointeger' for
390 + * when the caller wants to preserve the bits (matters if unsigned
391 + * values are used).
393 lua_pushnumber(L, (lua_Number)n);
396 @@ -144,7 +151,7 @@ static int luaB_setfenv (lua_State *L) {
397 luaL_checktype(L, 2, LUA_TTABLE);
400 - if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
401 + if (lua_isnumber(L, 1) && lua_tointeger(L, 1) == 0) {
402 /* change environment of current thread */
405 @@ -209,7 +216,7 @@ static int luaB_collectgarbage (lua_Stat
409 - lua_pushnumber(L, res);
410 + lua_pushinteger(L, res);
414 @@ -631,6 +638,8 @@ static void base_open (lua_State *L) {
415 luaL_register(L, "_G", base_funcs);
416 lua_pushliteral(L, LUA_VERSION);
417 lua_setglobal(L, "_VERSION"); /* set global _VERSION */
418 + lua_pushliteral(L, LUA_LNUM);
419 + lua_setglobal(L, "_LNUM"); /* "[complex] double|float|ldouble int32|int64" */
420 /* `ipairs' and `pairs' need auxiliary functions as upvalues */
421 auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
422 auxopen(L, "pairs", luaB_pairs, luaB_next);
426 #include "lopcodes.h"
432 #define hasjumps(e) ((e)->t != (e)->f)
435 static int isnumeral(expdesc *e) {
436 - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
439 + (e->k == VKNUM2) ||
441 + (e->k == VKINT) || (e->k == VKNUM);
442 + return (ek && e->t == NO_JUMP && e->f == NO_JUMP);
446 @@ -231,12 +236,16 @@ static int addk (FuncState *fs, TValue *
447 TValue *idx = luaH_set(L, fs->h, k);
449 int oldsize = f->sizek;
450 - if (ttisnumber(idx)) {
451 - lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
452 - return cast_int(nvalue(idx));
453 + if (ttype(idx)==LUA_TNUMBER) {
454 + luai_normalize(idx);
455 + lua_assert( ttype(idx)==LUA_TINT ); /* had no fraction */
457 + if (ttisint(idx)) {
458 + lua_assert(luaO_rawequalObj(&fs->f->k[ivalue(idx)], v));
459 + return cast_int(ivalue(idx));
461 else { /* constant not found; create a new entry */
462 - setnvalue(idx, cast_num(fs->nk));
463 + setivalue(idx, fs->nk);
464 luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
465 MAXARG_Bx, "constant table overflow");
466 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
467 @@ -261,6 +270,21 @@ int luaK_numberK (FuncState *fs, lua_Num
471 +int luaK_integerK (FuncState *fs, lua_Integer r) {
474 + return addk(fs, &o, &o);
479 +static int luaK_imagK (FuncState *fs, lua_Number r) {
481 + setnvalue_complex(&o, r*I);
482 + return addk(fs, &o, &o);
486 static int boolK (FuncState *fs, int b) {
489 @@ -359,6 +383,16 @@ static void discharge2reg (FuncState *fs
490 luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
494 + luaK_codeABx(fs, OP_LOADK, reg, luaK_integerK(fs, e->u.ival));
499 + luaK_codeABx(fs, OP_LOADK, reg, luaK_imagK(fs, e->u.nval));
504 Instruction *pc = &getcode(fs, e);
506 @@ -444,6 +478,10 @@ void luaK_exp2val (FuncState *fs, expdes
507 int luaK_exp2RK (FuncState *fs, expdesc *e) {
517 @@ -451,6 +489,10 @@ int luaK_exp2RK (FuncState *fs, expdesc
518 if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
519 e->u.s.info = (e->k == VNIL) ? nilK(fs) :
520 (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
521 + (e->k == VKINT) ? luaK_integerK(fs, e->u.ival) :
523 + (e->k == VKNUM2) ? luaK_imagK(fs, e->u.nval) :
525 boolK(fs, (e->k == VTRUE));
527 return RKASK(e->u.s.info);
528 @@ -540,7 +582,10 @@ void luaK_goiftrue (FuncState *fs, expde
529 int pc; /* pc of last jump */
530 luaK_dischargevars(fs, e);
532 - case VK: case VKNUM: case VTRUE: {
536 + case VKINT: case VK: case VKNUM: case VTRUE: {
537 pc = NO_JUMP; /* always true; do nothing */
540 @@ -590,7 +635,10 @@ static void codenot (FuncState *fs, expd
544 - case VK: case VKNUM: case VTRUE: {
548 + case VKINT: case VK: case VKNUM: case VTRUE: {
552 @@ -626,25 +674,70 @@ void luaK_indexed (FuncState *fs, expdes
554 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
555 lua_Number v1, v2, r;
557 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
561 + /* real and imaginary parts don't mix. */
563 + if (e1->k == VKNUM2) {
564 + if ((op != OP_UNM) && (e2->k != VKNUM2)) return 0;
566 + else if (e2->k == VKNUM2) { return 0; }
568 + if ((e1->k == VKINT) && (e2->k == VKINT)) {
569 + lua_Integer i1= e1->u.ival, i2= e2->u.ival;
572 + /* Integer/integer calculations (may end up producing floating point) */
574 + case OP_ADD: done= try_addint( &rr, i1, i2 ); break;
575 + case OP_SUB: done= try_subint( &rr, i1, i2 ); break;
576 + case OP_MUL: done= try_mulint( &rr, i1, i2 ); break;
577 + case OP_DIV: done= try_divint( &rr, i1, i2 ); break;
578 + case OP_MOD: done= try_modint( &rr, i1, i2 ); break;
579 + case OP_POW: done= try_powint( &rr, i1, i2 ); break;
580 + case OP_UNM: done= try_unmint( &rr, i1 ); break;
581 + default: done= 0; break;
584 + e1->u.ival = rr; /* remained within integer range */
588 + v1 = (e1->k == VKINT) ? ((lua_Number)e1->u.ival) : e1->u.nval;
589 + v2 = (e2->k == VKINT) ? ((lua_Number)e2->u.ival) : e2->u.nval;
592 case OP_ADD: r = luai_numadd(v1, v2); break;
593 case OP_SUB: r = luai_numsub(v1, v2); break;
594 - case OP_MUL: r = luai_nummul(v1, v2); break;
597 + if (vkres==VKNUM2) return 0; /* leave to runtime (could do here, but not worth it?) */
599 + r = luai_nummul(v1, v2); break;
601 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
602 - r = luai_numdiv(v1, v2); break;
604 + if (vkres==VKNUM2) return 0; /* leave to runtime */
606 + r = luai_numdiv(v1, v2); break;
608 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
610 + if (vkres==VKNUM2) return 0; /* leave to runtime */
612 r = luai_nummod(v1, v2); break;
613 - case OP_POW: r = luai_numpow(v1, v2); break;
616 + if (vkres==VKNUM2) return 0; /* leave to runtime */
618 + r = luai_numpow(v1, v2); break;
619 case OP_UNM: r = luai_numunm(v1); break;
620 case OP_LEN: return 0; /* no constant folding for 'len' */
621 default: lua_assert(0); r = 0; break;
623 if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
624 + e1->k = cast(expkind,vkres);
628 @@ -688,7 +781,8 @@ static void codecomp (FuncState *fs, OpC
630 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
632 - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
633 + e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
640 @@ -71,6 +71,6 @@ LUAI_FUNC void luaK_prefix (FuncState *f
641 LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
642 LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
643 LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
645 +LUAI_FUNC int luaK_integerK (FuncState *fs, lua_Integer r);
650 @@ -183,7 +183,7 @@ static void collectvalidlines (lua_State
651 int *lineinfo = f->l.p->lineinfo;
653 for (i=0; i<f->l.p->sizelineinfo; i++)
654 - setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
655 + setbvalue(luaH_setint(L, t, lineinfo[i]), 1);
656 sethvalue(L, L->top, t);
659 @@ -566,7 +566,7 @@ static int isinstack (CallInfo *ci, cons
661 void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
662 const char *name = NULL;
663 - const char *t = luaT_typenames[ttype(o)];
664 + const char *t = luaT_typenames[ttype_ext(o)];
665 const char *kind = (isinstack(L->ci, o)) ?
666 getobjname(L, L->ci, cast_int(o - L->base), &name) :
668 @@ -594,8 +594,8 @@ void luaG_aritherror (lua_State *L, cons
671 int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
672 - const char *t1 = luaT_typenames[ttype(p1)];
673 - const char *t2 = luaT_typenames[ttype(p2)];
674 + const char *t1 = luaT_typenames[ttype_ext(p1)];
675 + const char *t2 = luaT_typenames[ttype_ext(p2)];
677 luaG_runerror(L, "attempt to compare two %s values", t1);
681 @@ -220,9 +220,9 @@ static StkId adjust_varargs (lua_State *
682 luaD_checkstack(L, p->maxstacksize);
683 htab = luaH_new(L, nvar, 1); /* create `arg' table */
684 for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
685 - setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
686 + setobj2n(L, luaH_setint(L, htab, i+1), L->top - nvar + i);
687 /* store counter in field `n' */
688 - setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
689 + setivalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), nvar);
692 /* move fixed parameters to final position */
695 @@ -52,6 +52,11 @@ static void DumpNumber(lua_Number x, Dum
699 +static void DumpInteger(lua_Integer x, DumpState* D)
704 static void DumpVector(const void* b, int n, size_t size, DumpState* D)
707 @@ -93,8 +98,11 @@ static void DumpConstants(const Proto* f
708 DumpChar(bvalue(o),D);
711 - DumpNumber(nvalue(o),D);
712 + DumpNumber(nvalue_fast(o),D);
715 + DumpInteger(ivalue(o),D);
718 DumpString(rawtsvalue(o),D);
740 @@ -269,6 +271,13 @@ static int io_lines (lua_State *L) {
741 ** =======================================================
745 +* Many problems if we intend the same 'n' format specifier (see 'file:read()')
746 +* to work for both FP and integer numbers, without losing their accuracy. So
747 +* we don't. 'n' reads numbers as floating points, 'i' as integers. Old code
748 +* remains valid, but won't provide full integer accuracy (this only matters
749 +* with float FP and/or 64-bit integers).
752 static int read_number (lua_State *L, FILE *f) {
754 @@ -282,6 +291,43 @@ static int read_number (lua_State *L, FI
758 +static int read_integer (lua_State *L, FILE *f) {
760 + if (fscanf(f, LUA_INTEGER_SCAN, &i) == 1) {
761 + lua_pushinteger(L, i);
764 + else return 0; /* read fails */
768 +static int read_complex (lua_State *L, FILE *f) {
769 + /* NNN / NNNi / NNN+MMMi / NNN-MMMi */
771 + if (fscanf(f, LUA_NUMBER_SCAN, &a) == 1) {
775 + lua_pushcomplex(L, a*I);
779 + /* "i" is consumed if at the end; just 'NNN+MMM' will most likely
780 + * behave as if "i" was there? (TBD: test)
782 + if (fscanf(f, LUA_NUMBER_SCAN "i", &b) == 1) {
783 + lua_pushcomplex(L, a+ (c=='+' ? b:-b)*I);
788 + lua_pushnumber(L,a); /*real part only*/
791 + return 0; /* read fails */
796 static int test_eof (lua_State *L, FILE *f) {
798 @@ -355,6 +401,14 @@ static int g_read (lua_State *L, FILE *f
799 case 'n': /* number */
800 success = read_number(L, f);
802 + case 'i': /* integer (full accuracy) */
803 + success = read_integer(L, f);
806 + case 'c': /* complex */
807 + success = read_complex(L, f);
811 success = read_line(L, f);
813 @@ -415,9 +469,10 @@ static int g_write (lua_State *L, FILE *
815 for (; nargs--; arg++) {
816 if (lua_type(L, arg) == LUA_TNUMBER) {
817 - /* optimization: could be done exactly as for strings */
819 - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
820 + if (lua_isinteger(L,arg))
821 + status = status && fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg)) > 0;
823 + status = status && fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
827 @@ -460,7 +515,7 @@ static int f_setvbuf (lua_State *L) {
828 static const char *const modenames[] = {"no", "full", "line", NULL};
830 int op = luaL_checkoption(L, 2, NULL, modenames);
831 - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
832 + size_t sz = luaL_optint32(L, 3, LUAL_BUFFERSIZE);
833 int res = setvbuf(f, NULL, mode[op], sz);
834 return pushresult(L, res == 0, NULL);
850 -const char *const luaX_tokens [] = {
851 +static const char *const luaX_tokens [] = {
852 "and", "break", "do", "else", "elseif",
853 "end", "false", "for", "function", "if",
854 "in", "local", "nil", "not", "or", "repeat",
855 "return", "then", "true", "until", "while",
856 "..", "...", "==", ">=", "<=", "~=",
857 "<number>", "<name>", "<string>", "<eof>",
865 @@ -90,7 +95,11 @@ static const char *txtToken (LexState *l
875 return luaZ_buffer(ls->buff);
877 @@ -175,23 +184,27 @@ static void buffreplace (LexState *ls, c
878 if (p[n] == from) p[n] = to;
882 -static void trydecpoint (LexState *ls, SemInfo *seminfo) {
883 +/* TK_NUMBER (/ TK_NUMBER2) */
884 +static int trydecpoint (LexState *ls, SemInfo *seminfo) {
885 /* format error: try to update decimal point separator */
886 struct lconv *cv = localeconv();
887 char old = ls->decpoint;
889 ls->decpoint = (cv ? cv->decimal_point[0] : '.');
890 buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
891 - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
892 + ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, NULL);
894 /* format error with correct decimal point: no more options */
895 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
896 luaX_lexerror(ls, "malformed number", TK_NUMBER);
903 -static void read_numeral (LexState *ls, SemInfo *seminfo) {
904 +/* TK_NUMBER / TK_INT (/TK_NUMBER2) */
905 +static int read_numeral (LexState *ls, SemInfo *seminfo) {
907 lua_assert(isdigit(ls->current));
910 @@ -202,8 +215,9 @@ static void read_numeral (LexState *ls,
913 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
914 - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
915 - trydecpoint(ls, seminfo); /* try to update decimal point separator */
916 + ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, &seminfo->i );
917 + if (!ret) return trydecpoint(ls, seminfo); /* try to update decimal point separator */
922 @@ -331,6 +345,7 @@ static void read_string (LexState *ls, i
927 static int llex (LexState *ls, SemInfo *seminfo) {
928 luaZ_resetbuffer(ls->buff);
930 @@ -402,8 +417,7 @@ static int llex (LexState *ls, SemInfo *
932 else if (!isdigit(ls->current)) return '.';
934 - read_numeral(ls, seminfo);
936 + return read_numeral(ls, seminfo);
940 @@ -416,8 +430,7 @@ static int llex (LexState *ls, SemInfo *
943 else if (isdigit(ls->current)) {
944 - read_numeral(ls, seminfo);
946 + return read_numeral(ls, seminfo);
948 else if (isalpha(ls->current) || ls->current == '_') {
949 /* identifier or reserved word */
952 @@ -29,19 +29,22 @@ enum RESERVED {
953 TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
954 /* other terminal symbols */
955 TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
956 - TK_NAME, TK_STRING, TK_EOS
957 + TK_NAME, TK_STRING, TK_EOS, TK_INT
959 + , TK_NUMBER2 /* imaginary constants: Ni */
963 /* number of reserved words */
964 #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
967 -/* array with token `names' */
968 -LUAI_DATA const char *const luaX_tokens [];
971 +/* SemInfo is a local data structure of 'llex.c', used for carrying a string
972 + * or a number. A separate token (TK_*) will tell, how to interpret the data.
978 } SemInfo; /* semantics information */
982 @@ -49,6 +49,7 @@ typedef LUAI_USER_ALIGNMENT_T L_Umaxalig
984 /* result of a `usual argument conversion' over lua_Number */
985 typedef LUAI_UACNUMBER l_uacNumber;
986 +typedef LUAI_UACINTEGER l_uacInteger;
989 /* internal assertions for in-house debugging */
990 @@ -80,7 +81,6 @@ typedef LUAI_UACNUMBER l_uacNumber;
991 #define cast_int(i) cast(int, (i))
996 ** type for virtual-machine instructions
997 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
1001 ** See Copyright Notice in lua.h
1008 @@ -16,113 +15,210 @@
1009 #include "lauxlib.h"
1012 +/* 'luai_vectpow()' as a replacement for 'cpow()'. Defined in the header; we
1013 + * don't intrude the code libs internal functions.
1015 +#ifdef LNUM_COMPLEX
1020 -#define PI (3.14159265358979323846)
1021 -#define RADIANS_PER_DEGREE (PI/180.0)
1024 +# define PI (3.14159265358979323846F)
1025 +#elif defined(M_PI)
1028 +# define PI (3.14159265358979323846264338327950288)
1030 +#define RADIANS_PER_DEGREE (PI/180)
1034 +# define HUGE HUGE_VALF
1035 +#elif defined(LNUM_LDOUBLE)
1036 +# define HUGE HUGE_VALL
1038 +# define HUGE HUGE_VAL
1041 static int math_abs (lua_State *L) {
1042 - lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
1043 +#ifdef LNUM_COMPLEX
1044 + lua_pushnumber(L, _LF(cabs) (luaL_checkcomplex(L,1)));
1046 + lua_pushnumber(L, _LF(fabs) (luaL_checknumber(L, 1)));
1051 static int math_sin (lua_State *L) {
1052 - lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
1053 +#ifdef LNUM_COMPLEX
1054 + lua_pushcomplex(L, _LF(csin) (luaL_checkcomplex(L,1)));
1056 + lua_pushnumber(L, _LF(sin) (luaL_checknumber(L, 1)));
1061 static int math_sinh (lua_State *L) {
1062 - lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
1063 +#ifdef LNUM_COMPLEX
1064 + lua_pushcomplex(L, _LF(csinh) (luaL_checkcomplex(L,1)));
1066 + lua_pushnumber(L, _LF(sinh) (luaL_checknumber(L, 1)));
1071 static int math_cos (lua_State *L) {
1072 - lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
1073 +#ifdef LNUM_COMPLEX
1074 + lua_pushcomplex(L, _LF(ccos) (luaL_checkcomplex(L,1)));
1076 + lua_pushnumber(L, _LF(cos) (luaL_checknumber(L, 1)));
1081 static int math_cosh (lua_State *L) {
1082 - lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
1083 +#ifdef LNUM_COMPLEX
1084 + lua_pushcomplex(L, _LF(ccosh) (luaL_checkcomplex(L,1)));
1086 + lua_pushnumber(L, _LF(cosh) (luaL_checknumber(L, 1)));
1091 static int math_tan (lua_State *L) {
1092 - lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
1093 +#ifdef LNUM_COMPLEX
1094 + lua_pushcomplex(L, _LF(ctan) (luaL_checkcomplex(L,1)));
1096 + lua_pushnumber(L, _LF(tan) (luaL_checknumber(L, 1)));
1101 static int math_tanh (lua_State *L) {
1102 - lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
1103 +#ifdef LNUM_COMPLEX
1104 + lua_pushcomplex(L, _LF(ctanh) (luaL_checkcomplex(L,1)));
1106 + lua_pushnumber(L, _LF(tanh) (luaL_checknumber(L, 1)));
1111 static int math_asin (lua_State *L) {
1112 - lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
1113 +#ifdef LNUM_COMPLEX
1114 + lua_pushcomplex(L, _LF(casin) (luaL_checkcomplex(L,1)));
1116 + lua_pushnumber(L, _LF(asin) (luaL_checknumber(L, 1)));
1121 static int math_acos (lua_State *L) {
1122 - lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
1123 +#ifdef LNUM_COMPLEX
1124 + lua_pushcomplex(L, _LF(cacos) (luaL_checkcomplex(L,1)));
1126 + lua_pushnumber(L, _LF(acos) (luaL_checknumber(L, 1)));
1131 static int math_atan (lua_State *L) {
1132 - lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
1133 +#ifdef LNUM_COMPLEX
1134 + lua_pushcomplex(L, _LF(catan) (luaL_checkcomplex(L,1)));
1136 + lua_pushnumber(L, _LF(atan) (luaL_checknumber(L, 1)));
1141 static int math_atan2 (lua_State *L) {
1142 - lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1143 + /* scalars only */
1144 + lua_pushnumber(L, _LF(atan2) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1148 static int math_ceil (lua_State *L) {
1149 - lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
1150 +#ifdef LNUM_COMPLEX
1151 + lua_Complex v= luaL_checkcomplex(L, 1);
1152 + lua_pushcomplex(L, _LF(ceil) (_LF(creal)(v)) + _LF(ceil) (_LF(cimag)(v))*I);
1154 + lua_pushnumber(L, _LF(ceil) (luaL_checknumber(L, 1)));
1159 static int math_floor (lua_State *L) {
1160 - lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
1161 +#ifdef LNUM_COMPLEX
1162 + lua_Complex v= luaL_checkcomplex(L, 1);
1163 + lua_pushcomplex(L, _LF(floor) (_LF(creal)(v)) + _LF(floor) (_LF(cimag)(v))*I);
1165 + lua_pushnumber(L, _LF(floor) (luaL_checknumber(L, 1)));
1170 -static int math_fmod (lua_State *L) {
1171 - lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1172 +static int math_fmod (lua_State *L) {
1173 + /* scalars only */
1174 + lua_pushnumber(L, _LF(fmod) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1178 static int math_modf (lua_State *L) {
1180 - double fp = modf(luaL_checknumber(L, 1), &ip);
1181 + /* scalars only */
1183 + lua_Number fp = _LF(modf) (luaL_checknumber(L, 1), &ip);
1184 lua_pushnumber(L, ip);
1185 lua_pushnumber(L, fp);
1189 static int math_sqrt (lua_State *L) {
1190 - lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
1191 +#ifdef LNUM_COMPLEX
1192 + lua_pushcomplex(L, _LF(csqrt) (luaL_checkcomplex(L,1)));
1194 + lua_pushnumber(L, _LF(sqrt) (luaL_checknumber(L, 1)));
1199 static int math_pow (lua_State *L) {
1200 - lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1201 +#ifdef LNUM_COMPLEX
1202 + /* C99 'cpow' gives somewhat inaccurate results (i.e. (-1)^2 = -1+1.2246467991474e-16i).
1203 + * 'luai_vectpow' smoothens such, reusing it is the reason we need to #include "lnum.h".
1205 + lua_pushcomplex(L, luai_vectpow(luaL_checkcomplex(L,1), luaL_checkcomplex(L,2)));
1207 + lua_pushnumber(L, _LF(pow) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1212 static int math_log (lua_State *L) {
1213 - lua_pushnumber(L, log(luaL_checknumber(L, 1)));
1214 +#ifdef LNUM_COMPLEX
1215 + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)));
1217 + lua_pushnumber(L, _LF(log) (luaL_checknumber(L, 1)));
1222 static int math_log10 (lua_State *L) {
1223 - lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
1224 +#ifdef LNUM_COMPLEX
1225 + /* Not in standard <complex.h> , but easy to calculate: log_a(x) = log_b(x) / log_b(a)
1227 + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)) / _LF(log) (10));
1229 + lua_pushnumber(L, _LF(log10) (luaL_checknumber(L, 1)));
1234 static int math_exp (lua_State *L) {
1235 - lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
1236 +#ifdef LNUM_COMPLEX
1237 + lua_pushcomplex(L, _LF(cexp) (luaL_checkcomplex(L,1)));
1239 + lua_pushnumber(L, _LF(exp) (luaL_checknumber(L, 1)));
1244 @@ -138,19 +234,20 @@ static int math_rad (lua_State *L) {
1246 static int math_frexp (lua_State *L) {
1248 - lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
1249 + lua_pushnumber(L, _LF(frexp) (luaL_checknumber(L, 1), &e));
1250 lua_pushinteger(L, e);
1254 static int math_ldexp (lua_State *L) {
1255 - lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
1256 + lua_pushnumber(L, _LF(ldexp) (luaL_checknumber(L, 1), luaL_checkint(L, 2)));
1262 static int math_min (lua_State *L) {
1263 + /* scalars only */
1264 int n = lua_gettop(L); /* number of arguments */
1265 lua_Number dmin = luaL_checknumber(L, 1);
1267 @@ -165,6 +262,7 @@ static int math_min (lua_State *L) {
1270 static int math_max (lua_State *L) {
1271 + /* scalars only */
1272 int n = lua_gettop(L); /* number of arguments */
1273 lua_Number dmax = luaL_checknumber(L, 1);
1275 @@ -182,25 +280,20 @@ static int math_random (lua_State *L) {
1276 /* the `%' avoids the (rare) case of r==1, and is needed also because on
1277 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
1278 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
1279 - switch (lua_gettop(L)) { /* check number of arguments */
1280 - case 0: { /* no arguments */
1281 - lua_pushnumber(L, r); /* Number between 0 and 1 */
1284 - case 1: { /* only upper limit */
1285 - int u = luaL_checkint(L, 1);
1286 - luaL_argcheck(L, 1<=u, 1, "interval is empty");
1287 - lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
1290 - case 2: { /* lower and upper limits */
1291 - int l = luaL_checkint(L, 1);
1292 - int u = luaL_checkint(L, 2);
1293 - luaL_argcheck(L, l<=u, 2, "interval is empty");
1294 - lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
1297 - default: return luaL_error(L, "wrong number of arguments");
1298 + int n= lua_gettop(L); /* number of arguments */
1299 + if (n==0) { /* no arguments: range [0,1) */
1300 + lua_pushnumber(L, r);
1301 + } else if (n<=2) { /* int range [1,u] or [l,u] */
1302 + int l= n==1 ? 1 : luaL_checkint(L, 1);
1303 + int u = luaL_checkint(L, n);
1306 + luaL_argcheck(L, l<=u, n, "interval is empty");
1307 + d= _LF(floor)(r*(u-l+1));
1308 + lua_number2int(tmp,d);
1309 + lua_pushinteger(L, l+tmp);
1311 + return luaL_error(L, "wrong number of arguments");
1315 @@ -211,6 +304,66 @@ static int math_randomseed (lua_State *L
1320 +* Lua 5.1 does not have acosh, asinh, atanh for scalars (not ANSI C)
1322 +#if __STDC_VERSION__ >= 199901L
1323 +static int math_acosh (lua_State *L) {
1324 +# ifdef LNUM_COMPLEX
1325 + lua_pushcomplex(L, _LF(cacosh) (luaL_checkcomplex(L,1)));
1327 + lua_pushnumber(L, _LF(acosh) (luaL_checknumber(L,1)));
1331 +static int math_asinh (lua_State *L) {
1332 +# ifdef LNUM_COMPLEX
1333 + lua_pushcomplex(L, _LF(casinh) (luaL_checkcomplex(L,1)));
1335 + lua_pushnumber(L, _LF(asinh) (luaL_checknumber(L,1)));
1339 +static int math_atanh (lua_State *L) {
1340 +# ifdef LNUM_COMPLEX
1341 + lua_pushcomplex(L, _LF(catanh) (luaL_checkcomplex(L,1)));
1343 + lua_pushnumber(L, _LF(atanh) (luaL_checknumber(L,1)));
1350 + * C99 complex functions, not covered above.
1352 +#ifdef LNUM_COMPLEX
1353 +static int math_arg (lua_State *L) {
1354 + lua_pushnumber(L, _LF(carg) (luaL_checkcomplex(L,1)));
1358 +static int math_imag (lua_State *L) {
1359 + lua_pushnumber(L, _LF(cimag) (luaL_checkcomplex(L,1)));
1363 +static int math_real (lua_State *L) {
1364 + lua_pushnumber(L, _LF(creal) (luaL_checkcomplex(L,1)));
1368 +static int math_conj (lua_State *L) {
1369 + lua_pushcomplex(L, _LF(conj) (luaL_checkcomplex(L,1)));
1373 +static int math_proj (lua_State *L) {
1374 + lua_pushcomplex(L, _LF(cproj) (luaL_checkcomplex(L,1)));
1380 static const luaL_Reg mathlib[] = {
1382 @@ -241,6 +394,18 @@ static const luaL_Reg mathlib[] = {
1383 {"sqrt", math_sqrt},
1384 {"tanh", math_tanh},
1386 +#if __STDC_VERSION__ >= 199901L
1387 + {"acosh", math_acosh},
1388 + {"asinh", math_asinh},
1389 + {"atanh", math_atanh},
1391 +#ifdef LNUM_COMPLEX
1392 + {"arg", math_arg},
1393 + {"imag", math_imag},
1394 + {"real", math_real},
1395 + {"conj", math_conj},
1396 + {"proj", math_proj},
1401 @@ -252,8 +417,10 @@ LUALIB_API int luaopen_math (lua_State *
1402 luaL_register(L, LUA_MATHLIBNAME, mathlib);
1403 lua_pushnumber(L, PI);
1404 lua_setfield(L, -2, "pi");
1405 - lua_pushnumber(L, HUGE_VAL);
1406 + lua_pushnumber(L, HUGE);
1407 lua_setfield(L, -2, "huge");
1408 + lua_pushinteger(L, LUA_INTEGER_MAX );
1409 + lua_setfield(L, -2, "hugeint");
1410 #if defined(LUA_COMPAT_MOD)
1411 lua_getfield(L, -1, "fmod");
1412 lua_setfield(L, -2, "mod");
1417 +** $Id: lnum.c,v ... $
1418 +** Internal number model
1419 +** See Copyright Notice in lua.h
1422 +#include <stdlib.h>
1425 +#include <string.h>
1437 +** lua_real2str converts a (non-complex) number to a string.
1438 +** lua_str2real converts a string to a (non-complex) number.
1440 +#define lua_real2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
1443 +* Note: Only 'strtod()' is part of ANSI C; others are C99 and
1444 +* may need '--std=c99' compiler setting (at least on Ubuntu 7.10).
1446 +* Visual C++ 2008 Express does not have 'strtof()', nor 'strtold()'.
1447 +* References to '_strtold()' exist but don't compile. It seems best
1448 +* to leave Windows users with DOUBLE only (or compile with MinGW).
1450 +* In practise, using '(long double)strtod' is a risky thing, since
1451 +* it will cause accuracy loss in reading in numbers, and such losses
1452 +* will pile up in later processing. Get a real 'strtold()' or don't
1453 +* use that mode at all.
1456 +# define lua_str2real strtod
1457 +#elif defined(LNUM_FLOAT)
1458 +# define lua_str2real strtof
1459 +#elif defined(LNUM_LDOUBLE)
1460 +# define lua_str2real strtold
1463 +#define lua_integer2str(s,v) sprintf((s), LUA_INTEGER_FMT, (v))
1465 +/* 's' is expected to be LUAI_MAXNUMBER2STR long (enough for any number)
1467 +void luaO_num2buf( char *s, const TValue *o )
1470 + lua_assert( ttisnumber(o) );
1472 + /* Reason to handle integers differently is not only speed, but accuracy as
1473 + * well. We want to make any integer tostring() without roundings, at all.
1476 + lua_integer2str( s, ivalue(o) );
1479 + n= nvalue_fast(o);
1480 + lua_real2str(s, n);
1482 +#ifdef LNUM_COMPLEX
1483 + lua_Number n2= nvalue_img_fast(o);
1484 + if (n2!=0) { /* Postfix with +-Ni */
1485 + int re0= (n == 0);
1486 + char *s2= re0 ? s : strchr(s,'\0');
1487 + if ((!re0) && (n2>0)) *s2++= '+';
1488 + lua_real2str( s2, n2 );
1495 +* If a LUA_TNUMBER has integer value, give it.
1497 +int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref ) {
1501 + lua_assert( ttype(o)==LUA_TNUMBER );
1502 + lua_assert( ref );
1503 +#ifdef LNUM_COMPLEX
1504 + if (nvalue_img_fast(o)!=0) return 0;
1506 + d= nvalue_fast(o);
1507 + lua_number2integer(i, d);
1508 + if (cast_num(i) == d) {
1509 + *ref= i; return 1;
1515 + * Lua 5.1.3 (using 'strtod()') allows 0x+hex but not 0+octal. This is good,
1516 + * and we should NOT use 'autobase' 0 with 'strtoul[l]()' for this reason.
1518 + * Lua 5.1.3 allows '0x...' numbers to overflow and lose precision; this is not
1519 + * good. On Visual C++ 2008, 'strtod()' does not even take them in. Better to
1520 + * require hex values to fit 'lua_Integer' or give an error that they don't?
1522 + * Full hex range (0 .. 0xff..ff) is stored as integers, not to lose any bits.
1523 + * Numerical value of 0xff..ff will be -1, if used in calculations.
1525 + * Returns: TK_INT for a valid integer, '*endptr_ref' updated
1526 + * TK_NUMBER for seemingly numeric, to be parsed as floating point
1527 + * 0 for bad characters, not a number (or '0x' out of range)
1529 +static int luaO_str2i (const char *s, lua_Integer *res, char **endptr_ref) {
1531 + /* 'v' gets ULONG_MAX on possible overflow (which is > LUA_INTEGER_MAX);
1532 + * we don't have to check 'errno' here.
1534 + unsigned LUA_INTEGER v= lua_str2ul(s, &endptr, 10);
1535 + if (endptr == s) return 0; /* nothing numeric */
1536 + if (v==0 && *endptr=='x') {
1537 + errno= 0; /* needs to be set, 'strtoul[l]' does not clear it */
1538 + v= lua_str2ul(endptr+1, &endptr, 16); /* retry as hex, unsigned range */
1539 + if (errno==ERANGE) { /* clamped to 0xff..ff */
1540 +#if (defined(LNUM_INT32) && !defined(LNUM_FLOAT)) || defined(LNUM_LDOUBLE)
1541 + return TK_NUMBER; /* Allow to be read as floating point (has more integer range) */
1543 + return 0; /* Reject the number */
1546 + } else if ((v > LUA_INTEGER_MAX) || (*endptr && (!isspace(*endptr)))) {
1547 + return TK_NUMBER; /* not in signed range, or has '.', 'e' etc. trailing */
1549 + *res= (lua_Integer)v;
1550 + *endptr_ref= endptr;
1554 +/* 0 / TK_NUMBER / TK_INT (/ TK_NUMBER2) */
1555 +int luaO_str2d (const char *s, lua_Number *res_n, lua_Integer *res_i) {
1557 + int ret= TK_NUMBER;
1558 + /* Check integers first, if caller is allowing.
1559 + * If 'res2'==NULL, they're only looking for floating point.
1562 + ret= luaO_str2i(s,res_i,&endptr);
1563 + if (ret==0) return 0;
1565 + if (ret==TK_NUMBER) {
1566 + lua_assert(res_n);
1567 + /* Note: Visual C++ 2008 Express 'strtod()' does not read in "0x..."
1568 + * numbers; it will read '0' and spit 'x' as endptr.
1569 + * This means hex constants not fitting in 'lua_Integer' won't
1570 + * be read in at all. What to do?
1572 + *res_n = lua_str2real(s, &endptr);
1573 + if (endptr == s) return 0; /* conversion failed */
1574 + /* Visual C++ 2008 'strtod()' does not allow "0x..." input. */
1575 +#if defined(_MSC_VER) && !defined(LNUM_FLOAT) && !defined(LNUM_INT64)
1576 + if (*res_n==0 && *endptr=='x') {
1577 + /* Hex constant too big for 'lua_Integer' but that could fit in 'lua_Number'
1580 + unsigned __int64 v= _strtoui64( s, &endptr, 16 );
1581 + /* We just let > 64 bit values be clamped to _UI64_MAX (MSDN does not say 'errno'==ERANGE would be set) */
1582 + *res_n= cast_num(v);
1583 + if (*res_n != v) return 0; /* Would have lost accuracy */
1586 +#ifdef LNUM_COMPLEX
1587 + if (*endptr == 'i') { endptr++; ret= TK_NUMBER2; }
1591 + while (isspace(cast(unsigned char, *endptr))) endptr++;
1592 + if (*endptr) return 0; /* invalid trail */
1598 +/* Functions for finding out, when integer operations remain in range
1599 + * (and doing them).
1601 +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1602 + lua_Integer v= ib+ic; /* may overflow */
1603 + if (ib>0 && ic>0) { if (v < 0) return 0; /*overflow, use floats*/ }
1604 + else if (ib<0 && ic<0) { if (v >= 0) return 0; }
1609 +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1610 + lua_Integer v= ib-ic; /* may overflow */
1611 + if (ib>=0 && ic<0) { if (v < 0) return 0; /*overflow, use floats*/ }
1612 + else if (ib<0 && ic>0) { if (v >= 0) return 0; }
1617 +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1618 + if (ib!=LUA_INTEGER_MIN && ic!=LUA_INTEGER_MIN) {
1619 + lua_Integer b= luai_abs(ib), c= luai_abs(ic);
1620 + if ( (ib==0) || (LUA_INTEGER_MAX/b >= c) ) {
1621 + *r= ib*ic; /* no overflow */
1624 + } else if (ib==0 || ic==0) {
1628 + /* Result can be LUA_INTEGER_MIN; if it is, calculating it using floating
1629 + * point will not cause accuracy loss.
1631 + if ( luai_nummul( cast_num(ib), cast_num(ic) ) == LUA_INTEGER_MIN ) {
1632 + *r= LUA_INTEGER_MIN;
1638 +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1639 + /* N/0: leave to float side, to give an error
1641 + if (ic==0) return 0;
1643 + /* N/LUA_INTEGER_MIN: always non-integer results, or 0 or +1
1645 + if (ic==LUA_INTEGER_MIN) {
1646 + if (ib==LUA_INTEGER_MIN) { *r=1; return 1; }
1647 + if (ib==0) { *r=0; return 1; }
1649 + /* LUA_INTEGER_MIN (-2^31|63)/N: calculate using float side (either the division
1650 + * causes non-integer results, or there is no accuracy loss in int->fp->int
1651 + * conversions (N=2,4,8,..,256 and N=2^30,2^29,..2^23).
1653 + } else if (ib==LUA_INTEGER_MIN) {
1654 + lua_Number d= luai_numdiv( cast_num(LUA_INTEGER_MIN), cast_num(ic) );
1655 + lua_Integer i; lua_number2integer(i,d);
1656 + if (cast_num(i)==d) { *r= i; return 1; }
1659 + /* Note: We _can_ use ANSI C mod here, even on negative values, since
1660 + * we only test for == 0 (the sign would be implementation dependent).
1662 + if (ib%ic == 0) { *r= ib/ic; return 1; }
1668 +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1670 + /* ANSI C can be trusted when b%c==0, or when values are non-negative.
1671 + * b - (floor(b/c) * c)
1673 + * + +: b - (b/c) * c (b % c can be used)
1674 + * - -: b - (b/c) * c (b % c could work, but not defined by ANSI C)
1675 + * 0 -: b - (b/c) * c (=0, b % c could work, but not defined by ANSI C)
1676 + * - +: b - (b/c-1) * c (when b!=-c)
1677 + * + -: b - (b/c-1) * c (when b!=-c)
1679 + * o MIN%MIN ends up 0, via overflow in calcs but that does not matter.
1680 + * o MIN%MAX ends up MAX-1 (and other such numbers), also after overflow,
1681 + * but that does not matter, results do.
1683 + lua_Integer v= ib % ic;
1684 + if ( v!=0 && (ib<0 || ic<0) ) {
1685 + v= ib - ((ib/ic) - ((ib<=0 && ic<0) ? 0:1)) * ic;
1687 + /* Result should always have same sign as 2nd argument. (PIL2) */
1688 + lua_assert( (v<0) ? (ic<0) : (v>0) ? (ic>0) : 1 );
1692 + return 0; /* let float side return NaN */
1695 +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1697 + /* In FLOAT/INT32 or FLOAT|DOUBLE/INT64 modes, calculating integer powers
1698 + * via FP realm may lose accuracy (i.e. 7^11 = 1977326743, which fits int32
1699 + * but not 23-bit float mantissa).
1701 + * The current solution is dumb, but it works and uses little code. Use of
1702 + * integer powers is not anticipated to be very frequent (apart from 2^x,
1703 + * which is separately optimized).
1706 + else if (ic<0) return 0; /* FP realm */
1707 + else if (ib==2 && ic < (int)sizeof(lua_Integer)*8-1) *r= ((lua_Integer)1)<<ic; /* 1,2,4,...2^30 | 2^62 optimization */
1708 + else if (ic==0) *r=1;
1709 + else if (luai_abs(ib)==1) *r= (ic%2) ? ib:1;
1711 + lua_Integer x= ib;
1713 + if (!try_mulint( &x, x, ib ))
1714 + return 0; /* FP realm */
1721 +int try_unmint( lua_Integer *r, lua_Integer ib ) {
1722 + /* Negating LUA_INTEGER_MIN leaves the range. */
1723 + if ( ib != LUA_INTEGER_MIN )
1724 + { *r= -ib; return 1; }
1732 +** $Id: lnum.h,v ... $
1733 +** Internal Number model
1734 +** See Copyright Notice in lua.h
1742 +#include "lobject.h"
1745 +** The luai_num* macros define the primitive operations over 'lua_Number's
1746 +** (not 'lua_Integer's, not 'lua_Complex').
1748 +#define luai_numadd(a,b) ((a)+(b))
1749 +#define luai_numsub(a,b) ((a)-(b))
1750 +#define luai_nummul(a,b) ((a)*(b))
1751 +#define luai_numdiv(a,b) ((a)/(b))
1752 +#define luai_nummod(a,b) ((a) - _LF(floor)((a)/(b))*(b))
1753 +#define luai_numpow(a,b) (_LF(pow)(a,b))
1754 +#define luai_numunm(a) (-(a))
1755 +#define luai_numeq(a,b) ((a)==(b))
1756 +#define luai_numlt(a,b) ((a)<(b))
1757 +#define luai_numle(a,b) ((a)<=(b))
1758 +#define luai_numisnan(a) (!luai_numeq((a), (a)))
1760 +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1761 +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1762 +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1763 +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1764 +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1765 +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1766 +int try_unmint( lua_Integer *r, lua_Integer ib );
1768 +#ifdef LNUM_COMPLEX
1769 + static inline lua_Complex luai_vectunm( lua_Complex a ) { return -a; }
1770 + static inline lua_Complex luai_vectadd( lua_Complex a, lua_Complex b ) { return a+b; }
1771 + static inline lua_Complex luai_vectsub( lua_Complex a, lua_Complex b ) { return a-b; }
1772 + static inline lua_Complex luai_vectmul( lua_Complex a, lua_Complex b ) { return a*b; }
1773 + static inline lua_Complex luai_vectdiv( lua_Complex a, lua_Complex b ) { return a/b; }
1776 + * C99 does not provide modulus for complex numbers. It most likely is not
1777 + * meaningful at all.
1783 + * C99 'cpow' gives inaccurate results for many common cases s.a. (1i)^2 ->
1784 + * -1+1.2246467991474e-16i (OS X 10.4, gcc 4.0.1 build 5367)
1786 + * [(a+bi)^(c+di)] = (r^c) * exp(-d*t) * cos(c*t + d*ln(r)) +
1787 + * = (r^c) * exp(-d*t) * sin(c*t + d*ln(r)) *i
1788 + * r = sqrt(a^2+b^2), t = arctan( b/a )
1790 + * Reference: <http://home.att.net/~srschmitt/complexnumbers.html>
1791 + * Could also be calculated using: x^y = exp(ln(x)*y)
1793 + * Note: Defined here (and not in .c) so 'lmathlib.c' can share the
1797 + lua_Complex luai_vectpow( lua_Complex a, lua_Complex b )
1800 + lua_Number ar= _LF(creal)(a), ai= _LF(cimag)(a);
1801 + lua_Number br= _LF(creal)(b), bi= _LF(cimag)(b);
1803 + if (ai==0 && bi==0) { /* a^c (real) */
1804 + return luai_numpow( ar, br );
1807 + int br_int= (int)br;
1809 + if ( ai!=0 && bi==0 && br_int==br && br_int!=0 && br_int!=INT_MIN ) {
1810 + /* (a+bi)^N, N = { +-1,+-2, ... +-INT_MAX }
1812 + lua_Number k= luai_numpow( _LF(sqrt) (ar*ar + ai*ai), br );
1813 + lua_Number cos_z, sin_z;
1815 + /* Situation depends upon c (N) in the following manner:
1817 + * N%4==0 => cos(c*t)=1, sin(c*t)=0
1818 + * (N*sign(b))%4==1 or (N*sign(b))%4==-3 => cos(c*t)=0, sin(c*t)=1
1819 + * N%4==2 or N%4==-2 => cos(c*t)=-1, sin(c*t)=0
1820 + * (N*sign(b))%4==-1 or (N*sign(b))%4==3 => cos(c*t)=0, sin(c*t)=-1
1822 + int br_int_abs = br_int<0 ? -br_int:br_int;
1824 + switch( (br_int_abs%4) * (br_int<0 ? -1:1) * (ai<0 ? -1:1) ) {
1825 + case 0: cos_z=1, sin_z=0; break;
1826 + case 2: case -2: cos_z=-1, sin_z=0; break;
1827 + case 1: case -3: cos_z=0, sin_z=1; break;
1828 + case 3: case -1: cos_z=0, sin_z=-1; break;
1829 + default: lua_assert(0); return 0;
1831 + return k*cos_z + (k*sin_z)*I;
1834 + return _LF(cpow) ( a, b );
1838 +LUAI_FUNC int luaO_str2d (const char *s, lua_Number *res1, lua_Integer *res2);
1839 +LUAI_FUNC void luaO_num2buf( char *s, const TValue *o );
1841 +LUAI_FUNC int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref );
1843 +#define luai_normalize(o) \
1844 +{ lua_Integer _i; if (tt_integer_valued(o,&_i)) setivalue(o,_i); }
1848 +++ b/src/lnum_config.h
1851 +** $Id: lnum_config.h,v ... $
1852 +** Internal Number model
1853 +** See Copyright Notice in lua.h
1856 +#ifndef lnum_config_h
1857 +#define lnum_config_h
1860 +** Default number modes
1862 +#if (!defined LNUM_DOUBLE) && (!defined LNUM_FLOAT) && (!defined LNUM_LDOUBLE)
1863 +# define LNUM_FLOAT
1865 +#if (!defined LNUM_INT16) && (!defined LNUM_INT32) && (!defined LNUM_INT64)
1866 +# define LNUM_INT32
1870 +** Require C99 mode for COMPLEX, FLOAT and LDOUBLE (only DOUBLE is ANSI C).
1872 +#if defined(LNUM_COMPLEX) && (__STDC_VERSION__ < 199901L)
1873 +# error "Need C99 for complex (use '--std=c99' or similar)"
1874 +#elif defined(LNUM_LDOUBLE) && (__STDC_VERSION__ < 199901L) && !defined(_MSC_VER)
1875 +# error "Need C99 for 'long double' (use '--std=c99' or similar)"
1876 +#elif defined(LNUM_FLOAT) && (__STDC_VERSION__ < 199901L)
1877 +/* LNUM_FLOAT not supported on Windows */
1878 +# error "Need C99 for 'float' (use '--std=c99' or similar)"
1882 +** Number mode identifier to accompany the version string.
1884 +#ifdef LNUM_COMPLEX
1885 +# define _LNUM1 "complex "
1890 +# define _LNUM2 "double"
1891 +#elif defined(LNUM_FLOAT)
1892 +# define _LNUM2 "float"
1893 +#elif defined(LNUM_LDOUBLE)
1894 +# define _LNUM2 "ldouble"
1897 +# define _LNUM3 "int32"
1898 +#elif defined(LNUM_INT64)
1899 +# define _LNUM3 "int64"
1900 +#elif defined(LNUM_INT16)
1901 +# define _LNUM3 "int16"
1903 +#define LUA_LNUM _LNUM1 _LNUM2 " " _LNUM3
1906 +** LUA_NUMBER is the type of floating point number in Lua
1907 +** LUA_NUMBER_SCAN is the format for reading numbers.
1908 +** LUA_NUMBER_FMT is the format for writing numbers.
1911 +# define LUA_NUMBER float
1912 +# define LUA_NUMBER_SCAN "%f"
1913 +# define LUA_NUMBER_FMT "%g"
1914 +#elif (defined LNUM_DOUBLE)
1915 +# define LUA_NUMBER double
1916 +# define LUA_NUMBER_SCAN "%lf"
1917 +# define LUA_NUMBER_FMT "%.14g"
1918 +#elif (defined LNUM_LDOUBLE)
1919 +# define LUA_NUMBER long double
1920 +# define LUA_NUMBER_SCAN "%Lg"
1921 +# define LUA_NUMBER_FMT "%.20Lg"
1926 +** LUAI_MAXNUMBER2STR: size of a buffer fitting any number->string result.
1928 +** double: 24 (sign, x.xxxxxxxxxxxxxxe+nnnn, and \0)
1929 +** int64: 21 (19 digits, sign, and \0)
1930 +** long double: 43 for 128-bit (sign, x.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
1931 +** 30 for 80-bit (sign, x.xxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
1933 +#ifdef LNUM_LDOUBLE
1934 +# define _LUAI_MN2S 44
1936 +# define _LUAI_MN2S 24
1939 +#ifdef LNUM_COMPLEX
1940 +# define LUAI_MAXNUMBER2STR (2*_LUAI_MN2S)
1942 +# define LUAI_MAXNUMBER2STR _LUAI_MN2S
1946 +** LUA_INTEGER is the integer type used by lua_pushinteger/lua_tointeger/lua_isinteger.
1947 +** LUA_INTEGER_SCAN is the format for reading integers
1948 +** LUA_INTEGER_FMT is the format for writing integers
1950 +** Note: Visual C++ 2005 does not have 'strtoull()', use '_strtoui64()' instead.
1953 +# if LUAI_BITSINT > 16
1954 +# define LUA_INTEGER int
1955 +# define LUA_INTEGER_SCAN "%d"
1956 +# define LUA_INTEGER_FMT "%d"
1958 +/* Note: 'LUA_INTEGER' being 'ptrdiff_t' (as in Lua 5.1) causes problems with
1959 + * 'printf()' operations. Also 'unsigned ptrdiff_t' is invalid.
1961 +# define LUA_INTEGER long
1962 +# define LUA_INTEGER_SCAN "%ld"
1963 +# define LUA_INTEGER_FMT "%ld"
1965 +# define LUA_INTEGER_MAX 0x7FFFFFFF /* 2^31-1 */
1967 +#elif defined(LNUM_INT64)
1968 +# define LUA_INTEGER long long
1970 +# define lua_str2ul _strtoui64
1972 +# define lua_str2ul strtoull
1974 +# define LUA_INTEGER_SCAN "%lld"
1975 +# define LUA_INTEGER_FMT "%lld"
1976 +# define LUA_INTEGER_MAX 0x7fffffffffffffffLL /* 2^63-1 */
1977 +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX - 1LL) /* -2^63 */
1979 +#elif defined(LNUM_INT16)
1980 +# if LUAI_BITSINT > 16
1981 +# define LUA_INTEGER short
1982 +# define LUA_INTEGER_SCAN "%hd"
1983 +# define LUA_INTEGER_FMT "%hd"
1985 +# define LUA_INTEGER int
1986 +# define LUA_INTEGER_SCAN "%d"
1987 +# define LUA_INTEGER_FMT "%d"
1989 +# define LUA_INTEGER_MAX 0x7FFF /* 2^16-1 */
1993 +# define lua_str2ul (unsigned LUA_INTEGER)strtoul
1995 +#ifndef LUA_INTEGER_MIN
1996 +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX -1) /* -2^16|32 */
2000 +@@ lua_number2int is a macro to convert lua_Number to int.
2001 +@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
2002 +** CHANGE them if you know a faster way to convert a lua_Number to
2003 +** int (with any rounding method and without throwing errors) in your
2004 +** system. In Pentium machines, a naive typecast from double to int
2005 +** in C is extremely slow, so any alternative is worth trying.
2008 +/* On a Pentium, resort to a trick */
2009 +#if defined(LNUM_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
2010 + (defined(__i386) || defined (_M_IX86) || defined(__i386__))
2012 +/* On a Microsoft compiler, use assembler */
2013 +# if defined(_MSC_VER)
2014 +# define lua_number2int(i,d) __asm fld d __asm fistp i
2017 +/* the next trick should work on any Pentium, but sometimes clashes
2018 + with a DirectX idiosyncrasy */
2019 +union luai_Cast { double l_d; long l_l; };
2020 +# define lua_number2int(i,d) \
2021 + { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
2024 +# ifndef LNUM_INT64
2025 +# define lua_number2integer lua_number2int
2028 +/* this option always works, but may be slow */
2030 +# define lua_number2int(i,d) ((i)=(int)(d))
2033 +/* Note: Some compilers (OS X gcc 4.0?) may choke on double->long long conversion
2034 + * since it can lose precision. Others do require 'long long' there.
2036 +#ifndef lua_number2integer
2037 +# define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
2041 +** 'luai_abs()' to give absolute value of 'lua_Integer'
2044 +# define luai_abs abs
2045 +#elif defined(LNUM_INT64) && (__STDC_VERSION__ >= 199901L)
2046 +# define luai_abs llabs
2048 +# define luai_abs(v) ((v) >= 0 ? (v) : -(v))
2052 +** LUAI_UACNUMBER is the result of an 'usual argument conversion' over a number.
2053 +** LUAI_UACINTEGER the same, over an integer.
2055 +#define LUAI_UACNUMBER double
2056 +#define LUAI_UACINTEGER long
2058 +/* ANSI C only has math funcs for 'double. C99 required for float and long double
2062 +# define _LF(name) name
2063 +#elif defined(LNUM_FLOAT)
2064 +# define _LF(name) name ## f
2065 +#elif defined(LNUM_LDOUBLE)
2066 +# define _LF(name) name ## l
2075 #include "lstring.h"
2082 const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
2083 @@ -70,12 +71,31 @@ int luaO_log2 (unsigned int x) {
2086 int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
2087 - if (ttype(t1) != ttype(t2)) return 0;
2088 + if (!ttype_ext_same(t1,t2)) return 0;
2089 else switch (ttype(t1)) {
2093 + if (ttype(t2)==LUA_TINT)
2094 + return ivalue(t1) == ivalue(t2);
2095 + else { /* t1:int, t2:num */
2096 +#ifdef LNUM_COMPLEX
2097 + if (nvalue_img_fast(t2) != 0) return 0;
2099 + /* Avoid doing accuracy losing cast, if possible. */
2101 + if (tt_integer_valued(t2,&tmp))
2102 + return ivalue(t1) == tmp;
2104 + return luai_numeq( cast_num(ivalue(t1)), nvalue_fast(t2) );
2107 - return luai_numeq(nvalue(t1), nvalue(t2));
2108 + if (ttype(t2)==LUA_TINT)
2109 + return luaO_rawequalObj(t2, t1); /* swap LUA_TINT to left */
2110 +#ifdef LNUM_COMPLEX
2111 + if (!luai_numeq(nvalue_img_fast(t1), nvalue_img_fast(t2))) return 0;
2113 + return luai_numeq(nvalue_fast(t1), nvalue_fast(t2));
2115 return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
2116 case LUA_TLIGHTUSERDATA:
2117 @@ -86,21 +106,6 @@ int luaO_rawequalObj (const TValue *t1,
2122 -int luaO_str2d (const char *s, lua_Number *result) {
2124 - *result = lua_str2number(s, &endptr);
2125 - if (endptr == s) return 0; /* conversion failed */
2126 - if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
2127 - *result = cast_num(strtoul(s, &endptr, 16));
2128 - if (*endptr == '\0') return 1; /* most common case */
2129 - while (isspace(cast(unsigned char, *endptr))) endptr++;
2130 - if (*endptr != '\0') return 0; /* invalid trailing characters? */
2136 static void pushstr (lua_State *L, const char *str) {
2137 setsvalue2s(L, L->top, luaS_new(L, str));
2139 @@ -131,7 +136,11 @@ const char *luaO_pushvfstring (lua_State
2143 - setnvalue(L->top, cast_num(va_arg(argp, int)));
2144 + /* This is tricky for 64-bit integers; maybe they even cannot be
2145 + * supported on all compilers; depends on the conversions applied to
2146 + * variable argument lists. TBD: test!
2148 + setivalue(L->top, (lua_Integer) va_arg(argp, l_uacInteger));
2152 @@ -212,3 +221,4 @@ void luaO_chunkid (char *out, const char
2162 /* tags for values visible from Lua */
2163 -#define LAST_TAG LUA_TTHREAD
2164 +#if LUA_TINT > LUA_TTHREAD
2165 +# define LAST_TAG LUA_TINT
2167 +# define LAST_TAG LUA_TTHREAD
2170 #define NUM_TAGS (LAST_TAG+1)
2172 @@ -59,7 +63,12 @@ typedef struct GCheader {
2176 +#ifdef LNUM_COMPLEX
2185 @@ -77,7 +86,11 @@ typedef struct lua_TValue {
2187 /* Macros to test type */
2188 #define ttisnil(o) (ttype(o) == LUA_TNIL)
2189 -#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
2190 +#define ttisint(o) (ttype(o) == LUA_TINT)
2191 +#define ttisnumber(o) ((ttype(o) == LUA_TINT) || (ttype(o) == LUA_TNUMBER))
2192 +#ifdef LNUM_COMPLEX
2193 +# define ttiscomplex(o) ((ttype(o) == LUA_TNUMBER) && (nvalue_img_fast(o)!=0))
2195 #define ttisstring(o) (ttype(o) == LUA_TSTRING)
2196 #define ttistable(o) (ttype(o) == LUA_TTABLE)
2197 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
2198 @@ -90,7 +103,25 @@ typedef struct lua_TValue {
2199 #define ttype(o) ((o)->tt)
2200 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
2201 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
2202 -#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
2204 +#define ttype_ext(o) ( ttype(o) == LUA_TINT ? LUA_TNUMBER : ttype(o) )
2205 +#define ttype_ext_same(o1,o2) ( (ttype(o1)==ttype(o2)) || (ttisnumber(o1) && ttisnumber(o2)) )
2207 +/* '_fast' variants are for cases where 'ttype(o)' is known to be LUA_TNUMBER.
2209 +#ifdef LNUM_COMPLEX
2210 +# define nvalue_complex_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
2211 +# define nvalue_fast(o) ( _LF(creal) ( nvalue_complex_fast(o) ) )
2212 +# define nvalue_img_fast(o) ( _LF(cimag) ( nvalue_complex_fast(o) ) )
2213 +# define nvalue_complex(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? (o)->value.i : (o)->value.n )
2214 +# define nvalue_img(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? 0 : _LF(cimag)( (o)->value.n ) )
2215 +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : _LF(creal)((o)->value.n) )
2217 +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : (o)->value.n )
2218 +# define nvalue_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
2220 +#define ivalue(o) check_exp( ttype(o)==LUA_TINT, (o)->value.i )
2222 #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
2223 #define tsvalue(o) (&rawtsvalue(o)->tsv)
2224 #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
2225 @@ -116,8 +147,27 @@ typedef struct lua_TValue {
2226 /* Macros to set values */
2227 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
2229 -#define setnvalue(obj,x) \
2230 - { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
2231 +/* Must not have side effects, 'x' may be expression.
2233 +#define setivalue(obj,x) \
2234 + { TValue *i_o=(obj); i_o->value.i=(x); i_o->tt=LUA_TINT; }
2236 +# define setnvalue(obj,x) \
2237 + { TValue *i_o=(obj); i_o->value.n= (x); i_o->tt=LUA_TNUMBER; }
2239 +/* Note: Complex always has "inline", both are C99.
2241 +#ifdef LNUM_COMPLEX
2242 + static inline void setnvalue_complex_fast( TValue *obj, lua_Complex x ) {
2243 + lua_assert( _LF(cimag)(x) != 0 );
2244 + obj->value.n= x; obj->tt= LUA_TNUMBER;
2246 + static inline void setnvalue_complex( TValue *obj, lua_Complex x ) {
2247 + if (_LF(cimag)(x) == 0) { setnvalue(obj, _LF(creal)(x)); }
2248 + else { obj->value.n= x; obj->tt= LUA_TNUMBER; }
2253 #define setpvalue(obj,x) \
2254 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
2255 @@ -155,9 +205,6 @@ typedef struct lua_TValue {
2256 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
2257 checkliveness(G(L),i_o); }
2262 #define setobj(L,obj1,obj2) \
2263 { const TValue *o2=(obj2); TValue *o1=(obj1); \
2264 o1->value = o2->value; o1->tt=o2->tt; \
2265 @@ -185,8 +232,11 @@ typedef struct lua_TValue {
2267 #define setttype(obj, tt) (ttype(obj) = (tt))
2270 -#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
2271 +#if LUA_TINT >= LUA_TSTRING
2272 +# define iscollectable(o) ((ttype(o) >= LUA_TSTRING) && (ttype(o) != LUA_TINT))
2274 +# define iscollectable(o) (ttype(o) >= LUA_TSTRING)
2279 @@ -370,12 +420,10 @@ LUAI_FUNC int luaO_log2 (unsigned int x)
2280 LUAI_FUNC int luaO_int2fb (unsigned int x);
2281 LUAI_FUNC int luaO_fb2int (int x);
2282 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
2283 -LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
2284 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
2286 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
2287 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
2294 @@ -186,15 +186,30 @@ static int os_time (lua_State *L) {
2296 if (t == (time_t)(-1))
2299 - lua_pushnumber(L, (lua_Number)t);
2301 + /* On float systems the pushed value must be an integer, NOT a number.
2302 + * Otherwise, accuracy is lost in the time_t->float conversion.
2305 + lua_pushinteger(L, (lua_Integer) t);
2307 + lua_pushnumber(L, (lua_Number) t);
2314 static int os_difftime (lua_State *L) {
2316 + lua_Integer i= (lua_Integer)
2317 + difftime( (time_t)(luaL_checkinteger(L, 1)),
2318 + (time_t)(luaL_optinteger(L, 2, 0)));
2319 + lua_pushinteger(L, i);
2321 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
2322 (time_t)(luaL_optnumber(L, 2, 0))));
2331 #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m)
2335 ** nodes for block list (list of active blocks)
2337 @@ -72,7 +71,7 @@ static void errorlimit (FuncState *fs, i
2338 const char *msg = (fs->f->linedefined == 0) ?
2339 luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
2340 luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
2341 - fs->f->linedefined, limit, what);
2342 + (fs->f->linedefined), limit, what);
2343 luaX_lexerror(fs->ls, msg, 0);
2346 @@ -733,6 +732,18 @@ static void simpleexp (LexState *ls, exp
2347 v->u.nval = ls->t.seminfo.r;
2351 + init_exp(v, VKINT, 0);
2352 + v->u.ival = ls->t.seminfo.i;
2355 +#ifdef LNUM_COMPLEX
2356 + case TK_NUMBER2: {
2357 + init_exp(v, VKNUM2, 0);
2358 + v->u.nval = ls->t.seminfo.r;
2363 codestring(ls, v, ls->t.seminfo.ts);
2365 @@ -1079,7 +1090,7 @@ static void fornum (LexState *ls, TStrin
2366 if (testnext(ls, ','))
2367 exp1(ls); /* optional step */
2368 else { /* default step = 1 */
2369 - luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
2370 + luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_integerK(fs, 1));
2371 luaK_reserveregs(fs, 1);
2373 forbody(ls, base, line, 1, 1);
2376 @@ -31,7 +31,11 @@ typedef enum {
2377 VRELOCABLE, /* info = instruction pc */
2378 VNONRELOC, /* info = result register */
2379 VCALL, /* info = instruction pc */
2380 - VVARARG /* info = instruction pc */
2381 + VVARARG, /* info = instruction pc */
2382 + VKINT /* ival = integer value */
2383 +#ifdef LNUM_COMPLEX
2384 + ,VKNUM2 /* nval = imaginary value */
2388 typedef struct expdesc {
2389 @@ -39,6 +43,7 @@ typedef struct expdesc {
2391 struct { int info, aux; } s;
2395 int t; /* patch list of `exit when true' */
2396 int f; /* patch list of `exit when false' */
2399 @@ -43,8 +43,8 @@ static ptrdiff_t posrelat (ptrdiff_t pos
2400 static int str_sub (lua_State *L) {
2402 const char *s = luaL_checklstring(L, 1, &l);
2403 - ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
2404 - ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
2405 + ptrdiff_t start = posrelat(luaL_checkint32(L, 2), l);
2406 + ptrdiff_t end = posrelat(luaL_optint32(L, 3, -1), l);
2407 if (start < 1) start = 1;
2408 if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
2410 @@ -106,8 +106,8 @@ static int str_rep (lua_State *L) {
2411 static int str_byte (lua_State *L) {
2413 const char *s = luaL_checklstring(L, 1, &l);
2414 - ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
2415 - ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
2416 + ptrdiff_t posi = posrelat(luaL_optint32(L, 2, 1), l);
2417 + ptrdiff_t pose = posrelat(luaL_optint32(L, 3, posi), l);
2419 if (posi <= 0) posi = 1;
2420 if ((size_t)pose > l) pose = l;
2421 @@ -496,7 +496,7 @@ static int str_find_aux (lua_State *L, i
2423 const char *s = luaL_checklstring(L, 1, &l1);
2424 const char *p = luaL_checklstring(L, 2, &l2);
2425 - ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
2426 + ptrdiff_t init = posrelat(luaL_optint32(L, 3, 1), l1) - 1;
2427 if (init < 0) init = 0;
2428 else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
2429 if (find && (lua_toboolean(L, 4) || /* explicit request? */
2430 @@ -690,7 +690,7 @@ static int str_gsub (lua_State *L) {
2431 ** maximum size of each format specification (such as '%-099.99d')
2432 ** (+10 accounts for %99.99x plus margin of error)
2434 -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
2435 +#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTEGER_FMT)-2 + 10)
2438 static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
2439 @@ -747,9 +747,9 @@ static const char *scanformat (lua_State
2440 static void addintlen (char *form) {
2441 size_t l = strlen(form);
2442 char spec = form[l - 1];
2443 - strcpy(form + l - 1, LUA_INTFRMLEN);
2444 - form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
2445 - form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
2446 + const char *tmp= LUA_INTEGER_FMT; /* "%lld" or "%ld" */
2447 + strcpy(form + l - 1, tmp+1);
2448 + form[l + sizeof(LUA_INTEGER_FMT)-4] = spec;
2452 @@ -779,12 +779,12 @@ static int str_format (lua_State *L) {
2454 case 'd': case 'i': {
2456 - sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
2457 + sprintf(buff, form, luaL_checkinteger(L, arg));
2460 case 'o': case 'u': case 'x': case 'X': {
2462 - sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
2463 + sprintf(buff, form, (unsigned LUA_INTEGER)luaL_checkinteger(L, arg));
2466 case 'e': case 'E': case 'f':
2470 #include "lobject.h"
2479 #define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
2480 #define hashboolean(t,p) hashpow2(t, p)
2482 +#define hashint(t,i) hashpow2(t,i)
2485 ** for some types, it is better to avoid modulus by power of 2, as
2486 ** they tend to have many 2 factors.
2488 #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
2491 #define hashpointer(t,p) hashmod(t, IntPoint(p))
2495 -** number of ints inside a lua_Number
2497 -#define numints cast_int(sizeof(lua_Number)/sizeof(int))
2501 #define dummynode (&dummynode_)
2503 static const Node dummynode_ = {
2504 @@ -80,27 +71,46 @@ static const Node dummynode_ = {
2507 ** hash for lua_Numbers
2509 +** for non-complex modes, never called with 'lua_Integer' value range (s.a. 0)
2511 static Node *hashnum (const Table *t, lua_Number n) {
2512 - unsigned int a[numints];
2514 - if (luai_numeq(n, 0)) /* avoid problems with -0 */
2515 - return gnode(t, 0);
2516 - memcpy(a, &n, sizeof(a));
2517 - for (i = 1; i < numints; i++) a[0] += a[i];
2518 - return hashmod(t, a[0]);
2519 + const unsigned int *p= cast(const unsigned int *,&n);
2520 + unsigned int sum= *p;
2521 + unsigned int m= sizeof(lua_Number)/sizeof(int);
2523 + /* OS X Intel has 'm'==4 and gives "Bus error" if the last integer of
2524 + * 'n' is read; the actual size of long double is only 80 bits = 10 bytes.
2525 + * Linux x86 has 'm'==3, and does not require reduction.
2527 +#if defined(LNUM_LDOUBLE) && defined(__i386__)
2530 + for (i = 1; i < m; i++) sum += p[i];
2531 + return hashmod(t, sum);
2537 ** returns the `main' position of an element in a table (that is, the index
2538 ** of its hash value)
2540 +** Floating point numbers with integer value give the hash position of the
2541 +** integer (so they use the same table position).
2543 static Node *mainposition (const Table *t, const TValue *key) {
2545 switch (ttype(key)) {
2547 - return hashnum(t, nvalue(key));
2548 + if (tt_integer_valued(key,&i))
2549 + return hashint(t, i);
2550 +#ifdef LNUM_COMPLEX
2551 + if (nvalue_img_fast(key)!=0 && luai_numeq(nvalue_fast(key),0))
2552 + return gnode(t, 0); /* 0 and -0 to give same hash */
2554 + return hashnum(t, nvalue_fast(key));
2556 + return hashint(t, ivalue(key));
2558 return hashstr(t, rawtsvalue(key));
2560 @@ -116,16 +126,20 @@ static Node *mainposition (const Table *
2562 ** returns the index for `key' if `key' is an appropriate key to live in
2563 ** the array part of the table, -1 otherwise.
2565 +** Anything <=0 is taken as not being in the array part.
2567 -static int arrayindex (const TValue *key) {
2568 - if (ttisnumber(key)) {
2569 - lua_Number n = nvalue(key);
2571 - lua_number2int(k, n);
2572 - if (luai_numeq(cast_num(k), n))
2574 +static int arrayindex (const TValue *key, int max) {
2576 + switch( ttype(key) ) {
2578 + k= ivalue(key); break;
2580 + if (tt_integer_valued(key,&k)) break;
2582 + return -1; /* not to be used as array index */
2584 - return -1; /* `key' did not match some condition */
2585 + return ((k>0) && (k <= max)) ? cast_int(k) : -1;
2589 @@ -137,8 +151,8 @@ static int arrayindex (const TValue *key
2590 static int findindex (lua_State *L, Table *t, StkId key) {
2592 if (ttisnil(key)) return -1; /* first iteration */
2593 - i = arrayindex(key);
2594 - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
2595 + i = arrayindex(key, t->sizearray);
2596 + if (i>0) /* inside array part? */
2597 return i-1; /* yes; that's the index (corrected to C) */
2599 Node *n = mainposition(t, key);
2600 @@ -163,7 +177,7 @@ int luaH_next (lua_State *L, Table *t, S
2601 int i = findindex(L, t, key); /* find original element */
2602 for (i++; i < t->sizearray; i++) { /* try first array part */
2603 if (!ttisnil(&t->array[i])) { /* a non-nil value? */
2604 - setnvalue(key, cast_num(i+1));
2605 + setivalue(key, i+1);
2606 setobj2s(L, key+1, &t->array[i]);
2609 @@ -209,8 +223,8 @@ static int computesizes (int nums[], int
2612 static int countint (const TValue *key, int *nums) {
2613 - int k = arrayindex(key);
2614 - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
2615 + int k = arrayindex(key,MAXASIZE);
2616 + if (k>0) { /* appropriate array index? */
2617 nums[ceillog2(k)]++; /* count as such */
2620 @@ -308,7 +322,7 @@ static void resize (lua_State *L, Table
2621 /* re-insert elements from vanishing slice */
2622 for (i=nasize; i<oldasize; i++) {
2623 if (!ttisnil(&t->array[i]))
2624 - setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
2625 + setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]);
2628 luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
2629 @@ -409,7 +423,9 @@ static TValue *newkey (lua_State *L, Tab
2630 othern = mainposition(t, key2tval(mp));
2631 if (othern != mp) { /* is colliding node out of its main position? */
2632 /* yes; move colliding node into free position */
2633 - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
2634 + while (gnext(othern) != mp) {
2635 + othern = gnext(othern); /* find previous */
2637 gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
2638 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
2639 gnext(mp) = NULL; /* now `mp' is free */
2640 @@ -432,17 +448,18 @@ static TValue *newkey (lua_State *L, Tab
2642 ** search function for integers
2644 -const TValue *luaH_getnum (Table *t, int key) {
2645 +const TValue *luaH_getint (Table *t, lua_Integer key) {
2646 /* (1 <= key && key <= t->sizearray) */
2647 if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
2648 return &t->array[key-1];
2650 - lua_Number nk = cast_num(key);
2651 - Node *n = hashnum(t, nk);
2652 + Node *n = hashint(t, key);
2653 do { /* check whether `key' is somewhere in the chain */
2654 - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
2655 + if (ttisint(gkey(n)) && (ivalue(gkey(n)) == key)) {
2656 return gval(n); /* that's it */
2657 - else n = gnext(n);
2662 return luaO_nilobject;
2664 @@ -470,14 +487,12 @@ const TValue *luaH_get (Table *t, const
2665 switch (ttype(key)) {
2666 case LUA_TNIL: return luaO_nilobject;
2667 case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
2668 + case LUA_TINT: return luaH_getint(t, ivalue(key));
2671 - lua_Number n = nvalue(key);
2672 - lua_number2int(k, n);
2673 - if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
2674 - return luaH_getnum(t, k); /* use specialized version */
2675 - /* else go through */
2678 + if (tt_integer_valued(key,&i))
2679 + return luaH_getint(t,i);
2680 + } /* pass through */
2682 Node *n = mainposition(t, key);
2683 do { /* check whether `key' is somewhere in the chain */
2684 @@ -498,20 +513,25 @@ TValue *luaH_set (lua_State *L, Table *t
2685 return cast(TValue *, p);
2687 if (ttisnil(key)) luaG_runerror(L, "table index is nil");
2688 - else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
2689 - luaG_runerror(L, "table index is NaN");
2690 + else if (ttype(key)==LUA_TNUMBER) {
2692 + if (luai_numisnan(nvalue_fast(key)))
2693 + luaG_runerror(L, "table index is NaN");
2694 + if (tt_integer_valued(key,&k))
2695 + return luaH_setint(L, t, k);
2697 return newkey(L, t, key);
2702 -TValue *luaH_setnum (lua_State *L, Table *t, int key) {
2703 - const TValue *p = luaH_getnum(t, key);
2704 +TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key) {
2705 + const TValue *p = luaH_getint(t, key);
2706 if (p != luaO_nilobject)
2707 return cast(TValue *, p);
2710 - setnvalue(&k, cast_num(key));
2711 + setivalue(&k, key);
2712 return newkey(L, t, &k);
2715 @@ -533,20 +553,21 @@ static int unbound_search (Table *t, uns
2716 unsigned int i = j; /* i is zero or a present index */
2718 /* find `i' and `j' such that i is present and j is not */
2719 - while (!ttisnil(luaH_getnum(t, j))) {
2720 + while (!ttisnil(luaH_getint(t, j))) {
2723 if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
2724 /* table was built with bad purposes: resort to linear search */
2726 - while (!ttisnil(luaH_getnum(t, i))) i++;
2728 + for( i = 1; i<MAX_INT+1; i++ ) {
2729 + if (ttisnil(luaH_getint(t, i))) break;
2731 + return i - 1; /* up to MAX_INT */
2734 /* now do a binary search between them */
2736 unsigned int m = (i+j)/2;
2737 - if (ttisnil(luaH_getnum(t, m))) j = m;
2738 + if (ttisnil(luaH_getint(t, m))) j = m;
2745 #define key2tval(n) (&(n)->i_key.tvk)
2748 -LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
2749 -LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
2750 +LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
2751 +LUAI_FUNC TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key);
2752 LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
2753 LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
2754 LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
2762 const char *const luaT_typenames[] = {
2763 "nil", "boolean", "userdata", "number",
2764 "string", "table", "function", "userdata", "thread",
2765 @@ -67,6 +66,9 @@ const TValue *luaT_gettmbyobj (lua_State
2767 mt = uvalue(o)->metatable;
2770 + mt = G(L)->mt[LUA_TNUMBER];
2773 mt = G(L)->mt[ttype(o)];
2779 #include "lauxlib.h"
2782 +#include "llimits.h"
2785 static lua_State *globalL = NULL;
2786 @@ -382,6 +382,15 @@ int main (int argc, char **argv) {
2787 l_message(argv[0], "cannot create state: not enough memory");
2788 return EXIT_FAILURE;
2790 + /* Checking 'sizeof(lua_Integer)' cannot be made in preprocessor on all compilers.
2793 + lua_assert( sizeof(lua_Integer) == 2 );
2794 +#elif defined(LNUM_INT32)
2795 + lua_assert( sizeof(lua_Integer) == 4 );
2796 +#elif defined(LNUM_INT64)
2797 + lua_assert( sizeof(lua_Integer) == 8 );
2801 status = lua_cpcall(L, &pmain, &s);
2805 #define LUA_VERSION "Lua 5.1"
2806 #define LUA_RELEASE "Lua 5.1.5"
2807 #define LUA_VERSION_NUM 501
2808 -#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio"
2809 +#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio" " (" LUA_LNUM ")"
2810 #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
2813 @@ -71,6 +71,16 @@ typedef void * (*lua_Alloc) (void *ud, v
2815 #define LUA_TNONE (-1)
2817 +/* LUA_TINT is an internal type, not visible to applications. There are three
2818 + * potential values where it can be tweaked to (code autoadjusts to these):
2820 + * -2: not 'usual' type value; good since 'LUA_TINT' is not part of the API
2821 + * LUA_TNUMBER+1: shifts other type values upwards, breaking binary compatibility
2822 + * not acceptable for 5.1, maybe 5.2 onwards?
2823 + * 9: greater than existing (5.1) type values.
2825 +#define LUA_TINT (-2)
2828 #define LUA_TBOOLEAN 1
2829 #define LUA_TLIGHTUSERDATA 2
2830 @@ -139,6 +149,8 @@ LUA_API int (lua_isuserdata)
2831 LUA_API int (lua_type) (lua_State *L, int idx);
2832 LUA_API const char *(lua_typename) (lua_State *L, int tp);
2834 +LUA_API int (lua_isinteger) (lua_State *L, int idx);
2836 LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
2837 LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
2838 LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
2839 @@ -244,6 +256,19 @@ LUA_API lua_Alloc (lua_getallocf) (lua_S
2840 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
2844 +* It is unnecessary to break Lua C API 'lua_tonumber()' compatibility, just
2845 +* because the Lua number type is complex. Most C modules would use scalars
2846 +* only. We'll introduce new 'lua_tocomplex' and 'lua_pushcomplex' for when
2847 +* the module really wants to use them.
2849 +#ifdef LNUM_COMPLEX
2850 + #include <complex.h>
2851 + typedef LUA_NUMBER complex lua_Complex;
2852 + LUA_API lua_Complex (lua_tocomplex) (lua_State *L, int idx);
2853 + LUA_API void (lua_pushcomplex) (lua_State *L, lua_Complex v);
2858 ** ===============================================================
2859 @@ -268,7 +293,12 @@ LUA_API void lua_setallocf (lua_State *L
2860 #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
2861 #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
2862 #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
2863 -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
2866 +# define lua_isnoneornil(L, n) ( (lua_type(L,(n)) <= 0) && (lua_type(L,(n)) != LUA_TINT) )
2868 +# define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
2871 #define lua_pushliteral(L, s) \
2872 lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
2873 @@ -386,3 +416,4 @@ struct lua_Debug {
2886 +# include <assert.h>
2890 ** ==================================================================
2891 @@ -136,14 +138,38 @@
2895 -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
2896 -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
2897 -** machines, ptrdiff_t gives a good choice between int or long.)
2898 +@@ LUAI_BITSINT defines the number of bits in an int.
2899 +** CHANGE here if Lua cannot automatically detect the number of bits of
2900 +** your machine. Probably you do not need to change this.
2902 -#define LUA_INTEGER ptrdiff_t
2903 +/* avoid overflows in comparison */
2904 +#if INT_MAX-20 < 32760
2905 +#define LUAI_BITSINT 16
2906 +#elif INT_MAX > 2147483640L
2907 +/* int has at least 32 bits */
2908 +#define LUAI_BITSINT 32
2910 +#error "you must define LUA_BITSINT with number of bits in an integer"
2915 +@@ LNUM_DOUBLE |Â LNUM_FLOAT |Â LNUM_LDOUBLE: Generic Lua number mode
2916 +@@ LNUM_INT32 | LNUM_INT64: Integer type
2917 +@@ LNUM_COMPLEX: Define for using 'a+bi' numbers
2919 +@@ You can combine LNUM_xxx but only one of each group. I.e. '-DLNUM_FLOAT
2920 +@@ -DLNUM_INT32 -DLNUM_COMPLEX' gives float range complex numbers, with
2921 +@@ 32-bit scalar integer range optimized.
2923 +** These are kept in a separate configuration file mainly for ease of patching
2924 +** (can be changed if integerated to Lua proper).
2926 +/*#define LNUM_DOUBLE*/
2927 +/*#define LNUM_INT32*/
2928 +#include "lnum_config.h"
2931 @@ LUA_API is a mark for all core API functions.
2932 @@ LUALIB_API is a mark for all standard library functions.
2933 ** CHANGE them if you need to define those functions in some special way.
2934 @@ -383,22 +409,6 @@
2938 -@@ LUAI_BITSINT defines the number of bits in an int.
2939 -** CHANGE here if Lua cannot automatically detect the number of bits of
2940 -** your machine. Probably you do not need to change this.
2942 -/* avoid overflows in comparison */
2943 -#if INT_MAX-20 < 32760
2944 -#define LUAI_BITSINT 16
2945 -#elif INT_MAX > 2147483640L
2946 -/* int has at least 32 bits */
2947 -#define LUAI_BITSINT 32
2949 -#error "you must define LUA_BITSINT with number of bits in an integer"
2954 @@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
2955 @@ LUAI_INT32 is an signed integer with at least 32 bits.
2956 @@ LUAI_UMEM is an unsigned integer big enough to count the total
2957 @@ -425,6 +435,15 @@
2958 #define LUAI_MEM long
2962 +@@ LUAI_BOOL carries 0 and nonzero (normally 1). It may be defined as 'char'
2963 +** (to save memory), 'int' (for speed), 'bool' (for C++) or '_Bool' (C99)
2966 +# define LUAI_BOOL bool
2968 +# define LUAI_BOOL int
2972 @@ LUAI_MAXCALLS limits the number of nested calls.
2973 @@ -490,101 +509,6 @@
2974 /* }================================================================== */
2980 -** {==================================================================
2981 -@@ LUA_NUMBER is the type of numbers in Lua.
2982 -** CHANGE the following definitions only if you want to build Lua
2983 -** with a number type different from double. You may also need to
2984 -** change lua_number2int & lua_number2integer.
2985 -** ===================================================================
2988 -#define LUA_NUMBER_DOUBLE
2989 -#define LUA_NUMBER double
2992 -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
2995 -#define LUAI_UACNUMBER double
2999 -@@ LUA_NUMBER_SCAN is the format for reading numbers.
3000 -@@ LUA_NUMBER_FMT is the format for writing numbers.
3001 -@@ lua_number2str converts a number to a string.
3002 -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
3003 -@@ lua_str2number converts a string to a number.
3005 -#define LUA_NUMBER_SCAN "%lf"
3006 -#define LUA_NUMBER_FMT "%.14g"
3007 -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
3008 -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
3009 -#define lua_str2number(s,p) strtod((s), (p))
3013 -@@ The luai_num* macros define the primitive operations over numbers.
3015 -#if defined(LUA_CORE)
3017 -#define luai_numadd(a,b) ((a)+(b))
3018 -#define luai_numsub(a,b) ((a)-(b))
3019 -#define luai_nummul(a,b) ((a)*(b))
3020 -#define luai_numdiv(a,b) ((a)/(b))
3021 -#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
3022 -#define luai_numpow(a,b) (pow(a,b))
3023 -#define luai_numunm(a) (-(a))
3024 -#define luai_numeq(a,b) ((a)==(b))
3025 -#define luai_numlt(a,b) ((a)<(b))
3026 -#define luai_numle(a,b) ((a)<=(b))
3027 -#define luai_numisnan(a) (!luai_numeq((a), (a)))
3032 -@@ lua_number2int is a macro to convert lua_Number to int.
3033 -@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
3034 -** CHANGE them if you know a faster way to convert a lua_Number to
3035 -** int (with any rounding method and without throwing errors) in your
3036 -** system. In Pentium machines, a naive typecast from double to int
3037 -** in C is extremely slow, so any alternative is worth trying.
3040 -/* On a Pentium, resort to a trick */
3041 -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
3042 - (defined(__i386) || defined (_M_IX86) || defined(__i386__))
3044 -/* On a Microsoft compiler, use assembler */
3045 -#if defined(_MSC_VER)
3047 -#define lua_number2int(i,d) __asm fld d __asm fistp i
3048 -#define lua_number2integer(i,n) lua_number2int(i, n)
3050 -/* the next trick should work on any Pentium, but sometimes clashes
3051 - with a DirectX idiosyncrasy */
3054 -union luai_Cast { double l_d; long l_l; };
3055 -#define lua_number2int(i,d) \
3056 - { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
3057 -#define lua_number2integer(i,n) lua_number2int(i, n)
3062 -/* this option always works, but may be slow */
3064 -#define lua_number2int(i,d) ((i)=(int)(d))
3065 -#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
3069 -/* }================================================================== */
3073 @@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
3074 ** CHANGE it if your system requires alignments larger than double. (For
3075 @@ -728,28 +652,6 @@ union luai_Cast { double l_d; long l_l;
3076 #define luai_userstateyield(L,n) ((void)L)
3080 -@@ LUA_INTFRMLEN is the length modifier for integer conversions
3081 -@* in 'string.format'.
3082 -@@ LUA_INTFRM_T is the integer type correspoding to the previous length
3084 -** CHANGE them if your system supports long long or does not support long.
3087 -#if defined(LUA_USELONGLONG)
3089 -#define LUA_INTFRMLEN "ll"
3090 -#define LUA_INTFRM_T long long
3094 -#define LUA_INTFRMLEN "l"
3095 -#define LUA_INTFRM_T long
3101 /* =================================================================== */
3106 @@ -73,6 +73,13 @@ static lua_Number LoadNumber(LoadState*
3110 +static lua_Integer LoadInteger(LoadState* S)
3117 static TString* LoadString(LoadState* S)
3120 @@ -119,6 +126,9 @@ static void LoadConstants(LoadState* S,
3122 setnvalue(o,LoadNumber(S));
3124 + case LUA_TINT: /* Integer type saved in bytecode (see lcode.c) */
3125 + setivalue(o,LoadInteger(S));
3128 setsvalue2n(S->L,o,LoadString(S));
3130 @@ -223,5 +233,22 @@ void luaU_header (char* h)
3131 *h++=(char)sizeof(size_t);
3132 *h++=(char)sizeof(Instruction);
3133 *h++=(char)sizeof(lua_Number);
3134 - *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
3137 + * Last byte of header (0/1 in unpatched Lua 5.1.3):
3139 + * 0: lua_Number is float or double, lua_Integer not used. (nonpatched only)
3140 + * 1: lua_Number is integer (nonpatched only)
3142 + * +2: LNUM_INT16: sizeof(lua_Integer)
3143 + * +4: LNUM_INT32: sizeof(lua_Integer)
3144 + * +8: LNUM_INT64: sizeof(lua_Integer)
3146 + * +0x80: LNUM_COMPLEX
3148 + *h++ = (char)(sizeof(lua_Integer)
3149 +#ifdef LNUM_COMPLEX
3165 /* limit for table tag-method chains (to avoid loops) */
3166 #define MAXTAGLOOP 100
3169 -const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
3172 + * If 'obj' is a string, it is tried to be interpreted as a number.
3174 +const TValue *luaV_tonumber ( const TValue *obj, TValue *n) {
3178 if (ttisnumber(obj)) return obj;
3179 - if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
3180 - setnvalue(n, num);
3186 + if (ttisstring(obj)) {
3187 + switch( luaO_str2d( svalue(obj), &d, &i ) ) {
3189 + setivalue(n,i); return n;
3191 + setnvalue(n,d); return n;
3192 +#ifdef LNUM_COMPLEX
3193 + case TK_NUMBER2: /* "N.NNNi", != 0 */
3194 + setnvalue_complex_fast(n, d*I); return n;
3202 @@ -49,8 +62,7 @@ int luaV_tostring (lua_State *L, StkId o
3205 char s[LUAI_MAXNUMBER2STR];
3206 - lua_Number n = nvalue(obj);
3207 - lua_number2str(s, n);
3208 + luaO_num2buf(s,obj);
3209 setsvalue2s(L, obj, luaS_new(L, s));
3212 @@ -222,59 +234,127 @@ static int l_strcmp (const TString *ls,
3216 +#ifdef LNUM_COMPLEX
3217 +void error_complex( lua_State *L, const TValue *l, const TValue *r )
3219 + char buf1[ LUAI_MAXNUMBER2STR ];
3220 + char buf2[ LUAI_MAXNUMBER2STR ];
3221 + luaO_num2buf( buf1, l );
3222 + luaO_num2buf( buf2, r );
3223 + luaG_runerror( L, "unable to compare: %s with %s", buf1, buf2 );
3229 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
3231 - if (ttype(l) != ttype(r))
3235 + if (!ttype_ext_same(l,r))
3236 return luaG_ordererror(L, l, r);
3237 - else if (ttisnumber(l))
3238 - return luai_numlt(nvalue(l), nvalue(r));
3239 - else if (ttisstring(l))
3240 - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
3241 - else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
3242 +#ifdef LNUM_COMPLEX
3243 + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
3244 + error_complex( L, l, r );
3246 + tl= ttype(l); tr= ttype(r);
3247 + if (tl==tr) { /* clear arithmetics */
3249 + case LUA_TINT: return ivalue(l) < ivalue(r);
3250 + case LUA_TNUMBER: return luai_numlt(nvalue_fast(l), nvalue_fast(r));
3251 + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
3253 + } else if (tl==LUA_TINT) { /* l:int, r:num */
3254 + /* Avoid accuracy losing casts: if 'r' is integer by value, do comparisons
3255 + * in integer realm. Only otherwise cast 'l' to FP (which might change its
3258 + if (tt_integer_valued(r,&tmp))
3259 + return ivalue(l) < tmp;
3261 + return luai_numlt( cast_num(ivalue(l)), nvalue_fast(r) );
3263 + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
3264 + if (tt_integer_valued(l,&tmp))
3265 + return tmp < ivalue(r);
3267 + return luai_numlt( nvalue_fast(l), cast_num(ivalue(r)) );
3269 + } else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
3272 return luaG_ordererror(L, l, r);
3276 static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
3278 - if (ttype(l) != ttype(r))
3282 + if (!ttype_ext_same(l,r))
3283 return luaG_ordererror(L, l, r);
3284 - else if (ttisnumber(l))
3285 - return luai_numle(nvalue(l), nvalue(r));
3286 - else if (ttisstring(l))
3287 - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
3288 - else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
3289 +#ifdef LNUM_COMPLEX
3290 + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
3291 + error_complex( L, l, r );
3293 + tl= ttype(l); tr= ttype(r);
3294 + if (tl==tr) { /* clear arithmetics */
3296 + case LUA_TINT: return ivalue(l) <= ivalue(r);
3297 + case LUA_TNUMBER: return luai_numle(nvalue_fast(l), nvalue_fast(r));
3298 + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
3301 + if (tl==LUA_TINT) { /* l:int, r:num */
3302 + if (tt_integer_valued(r,&tmp))
3303 + return ivalue(l) <= tmp;
3305 + return luai_numle( cast_num(ivalue(l)), nvalue_fast(r) );
3307 + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
3308 + if (tt_integer_valued(l,&tmp))
3309 + return tmp <= ivalue(r);
3311 + return luai_numle( nvalue_fast(l), cast_num(ivalue(r)) );
3313 + } else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
3315 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
3318 return luaG_ordererror(L, l, r);
3322 -int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
3323 +/* Note: 'luaV_equalval()' and 'luaO_rawequalObj()' have largely overlapping
3324 + * implementation. LUA_TNIL..LUA_TLIGHTUSERDATA cases could be handled
3325 + * simply by the 'default' case here.
3327 +int luaV_equalval (lua_State *L, const TValue *l, const TValue *r) {
3329 - lua_assert(ttype(t1) == ttype(t2));
3330 - switch (ttype(t1)) {
3331 + lua_assert(ttype_ext_same(l,r));
3332 + switch (ttype(l)) {
3333 case LUA_TNIL: return 1;
3334 - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
3335 - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
3336 - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
3338 + case LUA_TNUMBER: return luaO_rawequalObj(l,r);
3339 + case LUA_TBOOLEAN: return bvalue(l) == bvalue(r); /* true must be 1 !! */
3340 + case LUA_TLIGHTUSERDATA: return pvalue(l) == pvalue(r);
3341 case LUA_TUSERDATA: {
3342 - if (uvalue(t1) == uvalue(t2)) return 1;
3343 - tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
3345 + if (uvalue(l) == uvalue(r)) return 1;
3346 + tm = get_compTM(L, uvalue(l)->metatable, uvalue(r)->metatable, TM_EQ);
3347 break; /* will try TM */
3350 - if (hvalue(t1) == hvalue(t2)) return 1;
3351 - tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
3352 + if (hvalue(l) == hvalue(r)) return 1;
3353 + tm = get_compTM(L, hvalue(l)->metatable, hvalue(r)->metatable, TM_EQ);
3354 break; /* will try TM */
3356 - default: return gcvalue(t1) == gcvalue(t2);
3357 + default: return gcvalue(l) == gcvalue(r);
3359 if (tm == NULL) return 0; /* no TM? */
3360 - callTMres(L, L->top, tm, t1, t2); /* call TM */
3361 + callTMres(L, L->top, tm, l, r); /* call TM */
3362 return !l_isfalse(L->top);
3365 @@ -314,30 +394,6 @@ void luaV_concat (lua_State *L, int tota
3369 -static void Arith (lua_State *L, StkId ra, const TValue *rb,
3370 - const TValue *rc, TMS op) {
3371 - TValue tempb, tempc;
3372 - const TValue *b, *c;
3373 - if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
3374 - (c = luaV_tonumber(rc, &tempc)) != NULL) {
3375 - lua_Number nb = nvalue(b), nc = nvalue(c);
3377 - case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
3378 - case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
3379 - case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
3380 - case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
3381 - case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
3382 - case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
3383 - case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
3384 - default: lua_assert(0); break;
3387 - else if (!call_binTM(L, rb, rc, ra, op))
3388 - luaG_aritherror(L, rb, rc);
3394 ** some macros for common tasks in `luaV_execute'
3396 @@ -361,17 +417,154 @@ static void Arith (lua_State *L, StkId r
3397 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
3400 -#define arith_op(op,tm) { \
3401 - TValue *rb = RKB(i); \
3402 - TValue *rc = RKC(i); \
3403 - if (ttisnumber(rb) && ttisnumber(rc)) { \
3404 - lua_Number nb = nvalue(rb), nc = nvalue(rc); \
3405 - setnvalue(ra, op(nb, nc)); \
3408 - Protect(Arith(L, ra, rb, rc, tm)); \
3409 +/* Note: if called for unary operations, 'rc'=='rb'.
3411 +static void Arith (lua_State *L, StkId ra, const TValue *rb,
3412 + const TValue *rc, TMS op) {
3413 + TValue tempb, tempc;
3414 + const TValue *b, *c;
3417 + if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
3418 + (c = luaV_tonumber(rc, &tempc)) != NULL) {
3420 + /* Keep integer arithmetics in the integer realm, if possible.
3422 + if (ttisint(b) && ttisint(c)) {
3423 + lua_Integer ib = ivalue(b), ic = ivalue(c);
3424 + lua_Integer *ri = &ra->value.i;
3425 + ra->tt= LUA_TINT; /* part of 'setivalue(ra)' */
3427 + case TM_ADD: if (try_addint( ri, ib, ic)) return; break;
3428 + case TM_SUB: if (try_subint( ri, ib, ic)) return; break;
3429 + case TM_MUL: if (try_mulint( ri, ib, ic)) return; break;
3430 + case TM_DIV: if (try_divint( ri, ib, ic)) return; break;
3431 + case TM_MOD: if (try_modint( ri, ib, ic)) return; break;
3432 + case TM_POW: if (try_powint( ri, ib, ic)) return; break;
3433 + case TM_UNM: if (try_unmint( ri, ib)) return; break;
3434 + default: lua_assert(0);
3437 + /* Fallback to floating point, when leaving range. */
3439 +#ifdef LNUM_COMPLEX
3440 + if ((nvalue_img(b)!=0) || (nvalue_img(c)!=0)) {
3443 + r= -nvalue_complex_fast(b); /* never an integer (or scalar) */
3444 + setnvalue_complex_fast( ra, r );
3446 + lua_Complex bb= nvalue_complex(b), cc= nvalue_complex(c);
3448 + case TM_ADD: r= bb + cc; break;
3449 + case TM_SUB: r= bb - cc; break;
3450 + case TM_MUL: r= bb * cc; break;
3451 + case TM_DIV: r= bb / cc; break;
3453 + luaG_runerror(L, "attempt to use %% on complex numbers"); /* no return */
3454 + case TM_POW: r= luai_vectpow( bb, cc ); break;
3455 + default: lua_assert(0); r=0;
3457 + setnvalue_complex( ra, r );
3462 + nb = nvalue(b); nc = nvalue(c);
3464 + case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); return;
3465 + case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); return;
3466 + case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); return;
3467 + case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); return;
3468 + case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); return;
3469 + case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); return;
3470 + case TM_UNM: setnvalue(ra, luai_numunm(nb)); return;
3471 + default: lua_assert(0);
3475 + /* Either operand not a number */
3476 + if (!call_binTM(L, rb, rc, ra, op))
3477 + luaG_aritherror(L, rb, rc);
3480 +/* Helper macro to sort arithmetic operations into four categories:
3481 + * TK_INT: integer - integer operands
3482 + * TK_NUMBER: number - number (non complex, either may be integer)
3483 + * TK_NUMBER2: complex numbers (at least the other)
3484 + * 0: non-numeric (at least the other)
3486 +#ifdef LNUM_COMPLEX
3487 +static inline int arith_mode( const TValue *rb, const TValue *rc ) {
3488 + if (ttisint(rb) && ttisint(rc)) return TK_INT;
3489 + if (ttiscomplex(rb) || ttiscomplex(rc)) return TK_NUMBER2;
3490 + if (ttisnumber(rb) && ttisnumber(rc)) return TK_NUMBER;
3494 +# define arith_mode(rb,rc) \
3495 + ( (ttisint(rb) && ttisint(rc)) ? TK_INT : \
3496 + (ttisnumber(rb) && ttisnumber(rc)) ? TK_NUMBER : 0 )
3499 +/* arith_op macro for two operators:
3500 + * automatically chooses, which function (number, integer, complex) to use
3502 +#define ARITH_OP2_START( op_num, op_int ) \
3504 + switch( arith_mode(rb,rc) ) { \
3506 + if (op_int ( &(ra)->value.i, ivalue(rb), ivalue(rc) )) \
3507 + { ra->tt= LUA_TINT; break; } /* else flow through */ \
3509 + setnvalue(ra, op_num ( nvalue(rb), nvalue(rc) )); break;
3511 +#define ARITH_OP2_END \
3513 + failed= 1; break; \
3514 + } if (!failed) continue;
3516 +#define arith_op_continue_scalar( op_num, op_int ) \
3517 + ARITH_OP2_START( op_num, op_int ) \
3520 +#ifdef LNUM_COMPLEX
3521 +# define arith_op_continue( op_num, op_int, op_complex ) \
3522 + ARITH_OP2_START( op_num, op_int ) \
3523 + case TK_NUMBER2: \
3524 + setnvalue_complex( ra, op_complex ( nvalue_complex(rb), nvalue_complex(rc) ) ); break; \
3527 +# define arith_op_continue(op_num,op_int,_) arith_op_continue_scalar(op_num,op_int)
3530 +/* arith_op macro for one operator:
3532 +#define ARITH_OP1_START( op_num, op_int ) \
3534 + switch( arith_mode(rb,rb) ) { \
3536 + if (op_int ( &(ra)->value.i, ivalue(rb) )) \
3537 + { ra->tt= LUA_TINT; break; } /* else flow through */ \
3539 + setnvalue(ra, op_num (nvalue(rb))); break; \
3541 +#define ARITH_OP1_END \
3543 + failed= 1; break; \
3544 + } if (!failed) continue;
3546 +#ifdef LNUM_COMPLEX
3547 +# define arith_op1_continue( op_num, op_int, op_complex ) \
3548 + ARITH_OP1_START( op_num, op_int ) \
3549 + case TK_NUMBER2: \
3550 + setnvalue_complex( ra, op_complex ( nvalue_complex_fast(rb) )); break; \
3553 +# define arith_op1_continue( op_num, op_int, _ ) \
3554 + ARITH_OP1_START( op_num, op_int ) \
3559 void luaV_execute (lua_State *L, int nexeccalls) {
3560 @@ -472,38 +665,45 @@ void luaV_execute (lua_State *L, int nex
3564 - arith_op(luai_numadd, TM_ADD);
3565 + TValue *rb = RKB(i), *rc= RKC(i);
3566 + arith_op_continue( luai_numadd, try_addint, luai_vectadd );
3567 + Protect(Arith(L, ra, rb, rc, TM_ADD)); \
3571 - arith_op(luai_numsub, TM_SUB);
3572 + TValue *rb = RKB(i), *rc= RKC(i);
3573 + arith_op_continue( luai_numsub, try_subint, luai_vectsub );
3574 + Protect(Arith(L, ra, rb, rc, TM_SUB));
3578 - arith_op(luai_nummul, TM_MUL);
3579 + TValue *rb = RKB(i), *rc= RKC(i);
3580 + arith_op_continue(luai_nummul, try_mulint, luai_vectmul);
3581 + Protect(Arith(L, ra, rb, rc, TM_MUL));
3585 - arith_op(luai_numdiv, TM_DIV);
3586 + TValue *rb = RKB(i), *rc= RKC(i);
3587 + arith_op_continue(luai_numdiv, try_divint, luai_vectdiv);
3588 + Protect(Arith(L, ra, rb, rc, TM_DIV));
3592 - arith_op(luai_nummod, TM_MOD);
3593 + TValue *rb = RKB(i), *rc= RKC(i);
3594 + arith_op_continue_scalar(luai_nummod, try_modint); /* scalars only */
3595 + Protect(Arith(L, ra, rb, rc, TM_MOD));
3599 - arith_op(luai_numpow, TM_POW);
3600 + TValue *rb = RKB(i), *rc= RKC(i);
3601 + arith_op_continue(luai_numpow, try_powint, luai_vectpow);
3602 + Protect(Arith(L, ra, rb, rc, TM_POW));
3607 - if (ttisnumber(rb)) {
3608 - lua_Number nb = nvalue(rb);
3609 - setnvalue(ra, luai_numunm(nb));
3612 - Protect(Arith(L, ra, rb, rb, TM_UNM));
3614 + arith_op1_continue(luai_numunm, try_unmint, luai_vectunm);
3615 + Protect(Arith(L, ra, rb, rb, TM_UNM));
3619 @@ -515,11 +715,11 @@ void luaV_execute (lua_State *L, int nex
3620 const TValue *rb = RB(i);
3621 switch (ttype(rb)) {
3623 - setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
3624 + setivalue(ra, luaH_getn(hvalue(rb)));
3628 - setnvalue(ra, cast_num(tsvalue(rb)->len));
3629 + setivalue(ra, tsvalue(rb)->len);
3632 default: { /* try metamethod */
3633 @@ -652,14 +852,30 @@ void luaV_execute (lua_State *L, int nex
3637 - lua_Number step = nvalue(ra+2);
3638 - lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
3639 - lua_Number limit = nvalue(ra+1);
3640 - if (luai_numlt(0, step) ? luai_numle(idx, limit)
3641 - : luai_numle(limit, idx)) {
3642 - dojump(L, pc, GETARG_sBx(i)); /* jump back */
3643 - setnvalue(ra, idx); /* update internal index... */
3644 - setnvalue(ra+3, idx); /* ...and external index */
3645 + /* If start,step and limit are all integers, we don't need to check
3646 + * against overflow in the looping.
3648 + if (ttisint(ra) && ttisint(ra+1) && ttisint(ra+2)) {
3649 + lua_Integer step = ivalue(ra+2);
3650 + lua_Integer idx = ivalue(ra) + step; /* increment index */
3651 + lua_Integer limit = ivalue(ra+1);
3652 + if (step > 0 ? (idx <= limit) : (limit <= idx)) {
3653 + dojump(L, pc, GETARG_sBx(i)); /* jump back */
3654 + setivalue(ra, idx); /* update internal index... */
3655 + setivalue(ra+3, idx); /* ...and external index */
3658 + /* non-integer looping (don't use 'nvalue_fast', some may be integer!)
3660 + lua_Number step = nvalue(ra+2);
3661 + lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
3662 + lua_Number limit = nvalue(ra+1);
3663 + if (luai_numlt(0, step) ? luai_numle(idx, limit)
3664 + : luai_numle(limit, idx)) {
3665 + dojump(L, pc, GETARG_sBx(i)); /* jump back */
3666 + setnvalue(ra, idx); /* update internal index... */
3667 + setnvalue(ra+3, idx); /* ...and external index */
3672 @@ -668,13 +884,21 @@ void luaV_execute (lua_State *L, int nex
3673 const TValue *plimit = ra+1;
3674 const TValue *pstep = ra+2;
3675 L->savedpc = pc; /* next steps may throw errors */
3676 + /* Using same location for tonumber's both arguments, effectively does
3677 + * in-place modification (string->number). */
3678 if (!tonumber(init, ra))
3679 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
3680 else if (!tonumber(plimit, ra+1))
3681 luaG_runerror(L, LUA_QL("for") " limit must be a number");
3682 else if (!tonumber(pstep, ra+2))
3683 luaG_runerror(L, LUA_QL("for") " step must be a number");
3684 - setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
3685 + /* Step back one value (keep within integers if we can)
3687 + if (!( ttisint(ra) && ttisint(pstep) &&
3688 + try_subint( &ra->value.i, ivalue(ra), ivalue(pstep) ) )) {
3689 + /* don't use 'nvalue_fast()', values may be integer */
3690 + setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
3692 dojump(L, pc, GETARG_sBx(i));
3695 @@ -711,7 +935,7 @@ void luaV_execute (lua_State *L, int nex
3696 luaH_resizearray(L, h, last); /* pre-alloc it at once */
3697 for (; n > 0; n--) {
3699 - setobj2t(L, luaH_setnum(L, h, last--), val);
3700 + setobj2t(L, luaH_setint(L, h, last--), val);
3701 luaC_barriert(L, h, val);
3708 #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
3710 -#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
3711 - (((o) = luaV_tonumber(o,n)) != NULL))
3712 +#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
3714 -#define equalobj(L,o1,o2) \
3715 - (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
3716 +#define equalobj(L,o1,o2) (ttype_ext_same(o1,o2) && luaV_equalval(L, o1, o2))
3719 LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
3723 #include "lobject.h"
3724 #include "lopcodes.h"
3725 #include "lundump.h"
3728 #define PrintFunction luaU_print
3730 @@ -59,8 +60,16 @@ static void PrintConstant(const Proto* f
3732 printf(bvalue(o) ? "true" : "false");
3735 + printf(LUA_INTEGER_FMT,ivalue(o));
3738 - printf(LUA_NUMBER_FMT,nvalue(o));
3739 +#ifdef LNUM_COMPLEX
3740 + // TBD: Do we get complex values here?
3741 + { lua_Number b= nvalue_img_fast(o);
3742 + printf( LUA_NUMBER_FMT "%s" LUA_NUMBER_FMT "i", nvalue_fast(o), b>=0 ? "+":"", b ); }
3744 + printf(LUA_NUMBER_FMT,nvalue_fast(o));
3747 PrintString(rawtsvalue(o));