1 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/Makefile lua-5.1.3-patched/src/Makefile
2 --- ../lua-5.1.3/src/Makefile 2008-01-19 21:37:58.000000000 +0200
3 +++ lua-5.1.3-patched/src/Makefile 2008-03-26 13:05:24.000000000 +0200
6 CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
7 lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
8 - lundump.o lvm.o lzio.o
9 + lundump.o lvm.o lzio.o lnum.o
10 LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
11 lstrlib.o loadlib.o linit.o
14 lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
15 lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
16 ltm.h lzio.h lmem.h ldo.h
17 +lnum.o: lnum.c lua.h llex.h lnum.h
18 loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
19 lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \
20 ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h
22 print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \
23 ltm.h lzio.h lmem.h lopcodes.h lundump.h
25 +luaconf.h: lnum_config.h
28 +lbaselib.c: llimits.h lobject.h lapi.h
30 +liolib.c: lnum.h llex.h
33 +lobject.c: llex.h lnum.h
40 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lapi.c lua-5.1.3-patched/src/lapi.c
41 --- ../lua-5.1.3/src/lapi.c 2008-01-03 17:20:39.000000000 +0200
42 +++ lua-5.1.3-patched/src/lapi.c 2008-03-19 09:52:15.000000000 +0200
51 const char lua_ident[] =
54 LUA_API int lua_type (lua_State *L, int idx) {
55 StkId o = index2adr(L, idx);
56 - return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
57 + return (o == luaO_nilobject) ? LUA_TNONE : ttype_ext(o);
61 LUA_API const char *lua_typename (lua_State *L, int t) {
63 + lua_assert( t!= LUA_TINT );
64 return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
71 +LUA_API int lua_isinteger (lua_State *L, int idx) {
74 + const TValue *o = index2adr(L, idx);
75 + return tonumber(o,&tmp) && (ttisint(o) || tt_integer_valued(o,&dum));
79 LUA_API int lua_isstring (lua_State *L, int idx) {
80 int t = lua_type(L, idx);
81 return (t == LUA_TSTRING || t == LUA_TNUMBER);
87 LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
89 const TValue *o = index2adr(L, idx);
90 - if (tonumber(o, &n))
91 + if (tonumber(o, &n)) {
93 + if (nvalue_img(o) != 0)
94 + luaG_runerror(L, "expecting a real number");
104 LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
106 + /* Lua 5.1 documented behaviour is to return nonzero for non-integer:
107 + * "If the number is not an integer, it is truncated in some non-specified way."
108 + * I would suggest to change this, to return 0 for anything that would
109 + * not fit in 'lua_Integer'.
111 +#ifdef LUA_COMPAT_TOINTEGER
112 + /* Lua 5.1 compatible */
113 const TValue *o = index2adr(L, idx);
114 if (tonumber(o, &n)) {
116 - lua_Number num = nvalue(o);
117 - lua_number2integer(res, num);
121 + if (ttisint(o)) return ivalue(o);
123 +# ifdef LNUM_COMPLEX
124 + if (nvalue_img_fast(o) != 0)
125 + luaG_runerror(L, "expecting a real number");
127 + lua_number2integer(i, d);
133 + /* New suggestion */
134 + const TValue *o = index2adr(L, idx);
135 + if (tonumber(o, &n)) {
137 + if (ttisint(o)) return ivalue(o);
138 + if (tt_integer_valued(o,&i)) return i;
146 +LUA_API lua_Complex lua_tocomplex (lua_State *L, int idx) {
148 + const TValue *o = index2adr(L, idx);
149 + if (tonumber(o, &tmp))
150 + return nvalue_complex(o);
156 LUA_API int lua_toboolean (lua_State *L, int idx) {
157 const TValue *o = index2adr(L, idx);
158 return !l_isfalse(o);
160 case LUA_TSTRING: return tsvalue(o)->len;
161 case LUA_TUSERDATA: return uvalue(o)->len;
162 case LUA_TTABLE: return luaH_getn(hvalue(o));
166 lua_lock(L); /* `luaV_tostring' may create a new string */
171 +/* 'lua_pushnumber()' may lose accuracy on integers, 'lua_pushinteger' will not.
173 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
175 setnvalue(L->top, n);
176 @@ -435,12 +482,22 @@
180 -LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
181 +LUA_API void lua_pushinteger (lua_State *L, lua_Integer i) {
183 + setivalue(L->top, i);
190 +LUA_API void lua_pushcomplex (lua_State *L, lua_Complex v) {
192 - setnvalue(L->top, cast_num(n));
193 + setnvalue_complex( L->top, v );
200 LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
203 o = index2adr(L, idx);
204 api_check(L, ttistable(o));
205 - setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
206 + setobj2s(L, L->top, luaH_getint(hvalue(o), n));
212 mt = uvalue(obj)->metatable;
215 + mt = G(L)->mt[LUA_TNUMBER];
218 mt = G(L)->mt[ttype(obj)];
221 api_checknelems(L, 1);
222 o = index2adr(L, idx);
223 api_check(L, ttistable(o));
224 - setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
225 + setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
226 luaC_barriert(L, hvalue(o), L->top-1);
233 - G(L)->mt[ttype(obj)] = mt;
234 + G(L)->mt[ttype_ext(obj)] = mt;
238 @@ -1083,3 +1143,32 @@
243 +/* Help function for 'luaB_tonumber()', avoids multiple str->number
244 + * conversions for Lua "tonumber()".
246 + * Also pushes floating point numbers with integer value as integer, which
247 + * can be used by 'tonumber()' in scripts to bring values back to integer
250 + * Note: The 'back to integer realm' is _not_ to affect string conversions:
251 + * 'tonumber("4294967295.1")' should give a floating point value, although
252 + * the value would be 4294967296 (and storable in int64 realm).
254 +int lua_pushvalue_as_number (lua_State *L, int idx)
256 + const TValue *o = index2adr(L, idx);
259 + if (ttisnumber(o)) {
260 + if ( (!ttisint(o)) && tt_integer_valued(o,&i)) {
261 + lua_pushinteger( L, i );
264 + } else if (!tonumber(o, &tmp)) {
267 + if (ttisint(o)) lua_pushinteger( L, ivalue(o) );
268 + else lua_pushnumber( L, nvalue_fast(o) );
271 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lapi.h lua-5.1.3-patched/src/lapi.h
272 --- ../lua-5.1.3/src/lapi.h 2007-12-27 15:02:25.000000000 +0200
273 +++ lua-5.1.3-patched/src/lapi.h 2008-03-03 12:47:53.000000000 +0200
276 LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);
278 +int lua_pushvalue_as_number (lua_State *L, int idx);
281 Binary files ../lua-5.1.3/src/lapi.o and lua-5.1.3-patched/src/lapi.o differ
282 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lauxlib.c lua-5.1.3-patched/src/lauxlib.c
283 --- ../lua-5.1.3/src/lauxlib.c 2008-01-21 15:20:51.000000000 +0200
284 +++ lua-5.1.3-patched/src/lauxlib.c 2008-03-19 09:51:27.000000000 +0200
290 +#include "llimits.h"
292 #define FREELIST_REF 0 /* free list of references */
297 static void tag_error (lua_State *L, int narg, int tag) {
298 - luaL_typerror(L, narg, lua_typename(L, tag));
299 + luaL_typerror(L, narg, tag==LUA_TINT ? "integer" : lua_typename(L, tag));
305 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
306 lua_Integer d = lua_tointeger(L, narg);
307 - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
308 - tag_error(L, narg, LUA_TNUMBER);
309 + if (d == 0 && !lua_isinteger(L, narg)) /* avoid extra test when d is not 0 */
310 + tag_error(L, narg, LUA_TINT);
319 +LUALIB_API lua_Complex luaL_checkcomplex (lua_State *L, int narg) {
320 + lua_Complex c = lua_tocomplex(L, narg);
321 + if (c == 0 && !lua_isnumber(L, narg)) /* avoid extra test when c is not 0 */
322 + tag_error(L, narg, LUA_TNUMBER);
328 LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
329 if (!lua_getmetatable(L, obj)) /* no metatable? */
331 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lauxlib.h lua-5.1.3-patched/src/lauxlib.h
332 --- ../lua-5.1.3/src/lauxlib.h 2007-12-27 15:02:25.000000000 +0200
333 +++ lua-5.1.3-patched/src/lauxlib.h 2008-03-12 23:15:22.000000000 +0200
335 LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
336 LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
338 +#define luaL_checkint32(L,narg) ((int)luaL_checkinteger(L,narg))
339 +#define luaL_optint32(L,narg,def) ((int)luaL_optinteger(L,narg,def))
342 + LUALIB_API lua_Complex (luaL_checkcomplex) (lua_State *L, int narg);
345 LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
346 LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
347 Binary files ../lua-5.1.3/src/lauxlib.o and lua-5.1.3-patched/src/lauxlib.o differ
348 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lbaselib.c lua-5.1.3-patched/src/lbaselib.c
349 --- ../lua-5.1.3/src/lbaselib.c 2008-01-20 15:53:22.000000000 +0200
350 +++ lua-5.1.3-patched/src/lbaselib.c 2008-03-13 10:42:50.000000000 +0200
356 +#include "llimits.h"
357 +#include "lobject.h"
363 int base = luaL_optint(L, 2, 10);
364 if (base == 10) { /* standard conversion */
366 - if (lua_isnumber(L, 1)) {
367 - lua_pushnumber(L, lua_tonumber(L, 1));
368 + if (lua_isnumber(L, 1)) { /* numeric string, or a number */
369 + lua_pushvalue_as_number(L,1); /* API extension (not to lose accuracy here) */
375 const char *s1 = luaL_checkstring(L, 1);
378 + unsigned LUA_INTEGER n;
379 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
380 - n = strtoul(s1, &s2, base);
381 + n = lua_str2ul(s1, &s2, base);
382 if (s1 != s2) { /* at least one valid digit? */
383 while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
384 if (*s2 == '\0') { /* no invalid trailing characters? */
386 + /* Push as number, there needs to be separate 'luaB_tointeger' for
387 + * when the caller wants to preserve the bits (matters if unsigned
388 + * values are used).
390 lua_pushnumber(L, (lua_Number)n);
394 luaL_checktype(L, 2, LUA_TTABLE);
397 - if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
398 + if (lua_isnumber(L, 1) && lua_tointeger(L, 1) == 0) {
399 /* change environment of current thread */
406 - lua_pushnumber(L, res);
407 + lua_pushinteger(L, res);
412 luaL_register(L, "_G", base_funcs);
413 lua_pushliteral(L, LUA_VERSION);
414 lua_setglobal(L, "_VERSION"); /* set global _VERSION */
415 + lua_pushliteral(L, LUA_LNUM);
416 + lua_setglobal(L, "_LNUM"); /* "[complex] double|float|ldouble int32|int64" */
417 /* `ipairs' and `pairs' need auxliliary functions as upvalues */
418 auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
419 auxopen(L, "pairs", luaB_pairs, luaB_next);
420 Binary files ../lua-5.1.3/src/lbaselib.o and lua-5.1.3-patched/src/lbaselib.o differ
421 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lcode.c lua-5.1.3-patched/src/lcode.c
422 --- ../lua-5.1.3/src/lcode.c 2007-12-28 17:32:23.000000000 +0200
423 +++ lua-5.1.3-patched/src/lcode.c 2008-03-19 10:02:51.000000000 +0200
425 #include "lopcodes.h"
431 #define hasjumps(e) ((e)->t != (e)->f)
434 static int isnumeral(expdesc *e) {
435 - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
438 + (e->k == VKNUM2) ||
440 + (e->k == VKINT) || (e->k == VKNUM);
441 + return (ek && e->t == NO_JUMP && e->f == NO_JUMP);
445 @@ -231,12 +236,16 @@
446 TValue *idx = luaH_set(L, fs->h, k);
448 int oldsize = f->sizek;
449 - if (ttisnumber(idx)) {
450 - lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
451 - return cast_int(nvalue(idx));
452 + if (ttype(idx)==LUA_TNUMBER) {
453 + luai_normalize(idx);
454 + lua_assert( ttype(idx)==LUA_TINT ); /* had no fraction */
456 + if (ttisint(idx)) {
457 + lua_assert(luaO_rawequalObj(&fs->f->k[ivalue(idx)], v));
458 + return cast_int(ivalue(idx));
460 else { /* constant not found; create a new entry */
461 - setnvalue(idx, cast_num(fs->nk));
462 + setivalue(idx, fs->nk);
463 luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
464 MAXARG_Bx, "constant table overflow");
465 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
470 +int luaK_integerK (FuncState *fs, lua_Integer r) {
473 + return addk(fs, &o, &o);
478 +static int luaK_imagK (FuncState *fs, lua_Number r) {
480 + setnvalue_complex(&o, r*I);
481 + return addk(fs, &o, &o);
485 static int boolK (FuncState *fs, int b) {
489 luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
493 + luaK_codeABx(fs, OP_LOADK, reg, luaK_integerK(fs, e->u.ival));
498 + luaK_codeABx(fs, OP_LOADK, reg, luaK_imagK(fs, e->u.nval));
503 Instruction *pc = &getcode(fs, e);
506 int luaK_exp2RK (FuncState *fs, expdesc *e) {
517 if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
518 e->u.s.info = (e->k == VNIL) ? nilK(fs) :
519 (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
520 + (e->k == VKINT) ? luaK_integerK(fs, e->u.ival) :
522 + (e->k == VKNUM2) ? luaK_imagK(fs, e->u.nval) :
524 boolK(fs, (e->k == VTRUE));
526 return RKASK(e->u.s.info);
528 int pc; /* pc of last jump */
529 luaK_dischargevars(fs, e);
531 - case VK: case VKNUM: case VTRUE: {
535 + case VKINT: case VK: case VKNUM: case VTRUE: {
536 pc = NO_JUMP; /* always true; do nothing */
543 - case VK: case VKNUM: case VTRUE: {
547 + case VKINT: case VK: case VKNUM: case VTRUE: {
551 @@ -634,25 +682,70 @@
553 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
554 lua_Number v1, v2, r;
556 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
560 + /* real and imaginary parts don't mix. */
562 + if (e1->k == VKNUM2) {
563 + if ((op != OP_UNM) && (e2->k != VKNUM2)) return 0;
565 + else if (e2->k == VKNUM2) { return 0; }
567 + if ((e1->k == VKINT) && (e2->k == VKINT)) {
568 + lua_Integer i1= e1->u.ival, i2= e2->u.ival;
571 + /* Integer/integer calculations (may end up producing floating point) */
573 + case OP_ADD: done= try_addint( &rr, i1, i2 ); break;
574 + case OP_SUB: done= try_subint( &rr, i1, i2 ); break;
575 + case OP_MUL: done= try_mulint( &rr, i1, i2 ); break;
576 + case OP_DIV: done= try_divint( &rr, i1, i2 ); break;
577 + case OP_MOD: done= try_modint( &rr, i1, i2 ); break;
578 + case OP_POW: done= try_powint( &rr, i1, i2 ); break;
579 + case OP_UNM: done= try_unmint( &rr, i1 ); break;
580 + default: done= 0; break;
583 + e1->u.ival = rr; /* remained within integer range */
587 + v1 = (e1->k == VKINT) ? ((lua_Number)e1->u.ival) : e1->u.nval;
588 + v2 = (e2->k == VKINT) ? ((lua_Number)e2->u.ival) : e2->u.nval;
591 case OP_ADD: r = luai_numadd(v1, v2); break;
592 case OP_SUB: r = luai_numsub(v1, v2); break;
593 - case OP_MUL: r = luai_nummul(v1, v2); break;
596 + if (vkres==VKNUM2) return 0; /* leave to runtime (could do here, but not worth it?) */
598 + r = luai_nummul(v1, v2); break;
600 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
601 - r = luai_numdiv(v1, v2); break;
603 + if (vkres==VKNUM2) return 0; /* leave to runtime */
605 + r = luai_numdiv(v1, v2); break;
607 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
609 + if (vkres==VKNUM2) return 0; /* leave to runtime */
611 r = luai_nummod(v1, v2); break;
612 - case OP_POW: r = luai_numpow(v1, v2); break;
615 + if (vkres==VKNUM2) return 0; /* leave to runtime */
617 + r = luai_numpow(v1, v2); break;
618 case OP_UNM: r = luai_numunm(v1); break;
619 case OP_LEN: return 0; /* no constant folding for 'len' */
620 default: lua_assert(0); r = 0; break;
622 if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
623 + e1->k = cast(expkind,vkres);
629 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
631 - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
632 + e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
637 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lcode.h lua-5.1.3-patched/src/lcode.h
638 --- ../lua-5.1.3/src/lcode.h 2007-12-27 15:02:25.000000000 +0200
639 +++ lua-5.1.3-patched/src/lcode.h 2008-03-03 12:47:53.000000000 +0200
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);
648 Binary files ../lua-5.1.3/src/lcode.o and lua-5.1.3-patched/src/lcode.o differ
649 Binary files ../lua-5.1.3/src/ldblib.o and lua-5.1.3-patched/src/ldblib.o differ
650 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/ldebug.c lua-5.1.3-patched/src/ldebug.c
651 --- ../lua-5.1.3/src/ldebug.c 2007-12-28 17:32:23.000000000 +0200
652 +++ lua-5.1.3-patched/src/ldebug.c 2008-03-06 22:41:08.000000000 +0200
654 int *lineinfo = f->l.p->lineinfo;
656 for (i=0; i<f->l.p->sizelineinfo; i++)
657 - setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
658 + setbvalue(luaH_setint(L, t, lineinfo[i]), 1);
659 sethvalue(L, L->top, t);
664 void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
665 const char *name = NULL;
666 - const char *t = luaT_typenames[ttype(o)];
667 + const char *t = luaT_typenames[ttype_ext(o)];
668 const char *kind = (isinstack(L->ci, o)) ?
669 getobjname(L, L->ci, cast_int(o - L->base), &name) :
674 int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
675 - const char *t1 = luaT_typenames[ttype(p1)];
676 - const char *t2 = luaT_typenames[ttype(p2)];
677 + const char *t1 = luaT_typenames[ttype_ext(p1)];
678 + const char *t2 = luaT_typenames[ttype_ext(p2)];
680 luaG_runerror(L, "attempt to compare two %s values", t1);
682 Binary files ../lua-5.1.3/src/ldebug.o and lua-5.1.3-patched/src/ldebug.o differ
683 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/ldo.c lua-5.1.3-patched/src/ldo.c
684 --- ../lua-5.1.3/src/ldo.c 2008-01-19 00:31:22.000000000 +0200
685 +++ lua-5.1.3-patched/src/ldo.c 2008-03-06 22:41:31.000000000 +0200
688 htab = luaH_new(L, nvar, 1); /* create `arg' table */
689 for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
690 - setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
691 + setobj2n(L, luaH_setint(L, htab, i+1), L->top - nvar + i);
692 /* store counter in field `n' */
693 - setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
694 + setivalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), nvar);
697 /* move fixed parameters to final position */
698 Binary files ../lua-5.1.3/src/ldo.o and lua-5.1.3-patched/src/ldo.o differ
699 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/ldump.c lua-5.1.3-patched/src/ldump.c
700 --- ../lua-5.1.3/src/ldump.c 2007-12-27 15:02:25.000000000 +0200
701 +++ lua-5.1.3-patched/src/ldump.c 2008-03-03 12:47:53.000000000 +0200
706 +static void DumpInteger(lua_Integer x, DumpState* D)
711 static void DumpVector(const void* b, int n, size_t size, DumpState* D)
715 DumpChar(bvalue(o),D);
718 - DumpNumber(nvalue(o),D);
719 + DumpNumber(nvalue_fast(o),D);
722 + DumpInteger(ivalue(o),D);
725 DumpString(rawtsvalue(o),D);
727 Binary files ../lua-5.1.3/src/ldump.o and lua-5.1.3-patched/src/ldump.o differ
728 Binary files ../lua-5.1.3/src/lfunc.o and lua-5.1.3-patched/src/lfunc.o differ
729 Binary files ../lua-5.1.3/src/lgc.o and lua-5.1.3-patched/src/lgc.o differ
730 Binary files ../lua-5.1.3/src/liblua.a and lua-5.1.3-patched/src/liblua.a differ
731 Binary files ../lua-5.1.3/src/linit.o and lua-5.1.3-patched/src/linit.o differ
732 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/liolib.c lua-5.1.3-patched/src/liolib.c
733 --- ../lua-5.1.3/src/liolib.c 2008-01-18 19:47:43.000000000 +0200
734 +++ lua-5.1.3-patched/src/liolib.c 2008-03-19 10:10:09.000000000 +0200
754 ** =======================================================
758 +* Many problems if we intend the same 'n' format specifier (see 'file:read()')
759 +* to work for both FP and integer numbers, without losing their accuracy. So
760 +* we don't. 'n' reads numbers as floating points, 'i' as integers. Old code
761 +* remains valid, but won't provide full integer accuracy (this only matters
762 +* with float FP and/or 64-bit integers).
765 static int read_number (lua_State *L, FILE *f) {
768 else return 0; /* read fails */
771 +static int read_integer (lua_State *L, FILE *f) {
773 + if (fscanf(f, LUA_INTEGER_SCAN, &i) == 1) {
774 + lua_pushinteger(L, i);
777 + else return 0; /* read fails */
781 +static int read_complex (lua_State *L, FILE *f) {
782 + /* NNN / NNNi / NNN+MMMi / NNN-MMMi */
784 + if (fscanf(f, LUA_NUMBER_SCAN, &a) == 1) {
788 + lua_pushcomplex(L, a*I);
792 + /* "i" is consumed if at the end; just 'NNN+MMM' will most likely
793 + * behave as if "i" was there? (TBD: test)
795 + if (fscanf(f, LUA_NUMBER_SCAN "i", &b) == 1) {
796 + lua_pushcomplex(L, a+ (c=='+' ? b:-b)*I);
801 + lua_pushnumber(L,a); /*real part only*/
804 + return 0; /* read fails */
809 static int test_eof (lua_State *L, FILE *f) {
812 case 'n': /* number */
813 success = read_number(L, f);
815 + case 'i': /* integer (full accuracy) */
816 + success = read_integer(L, f);
819 + case 'c': /* complex */
820 + success = read_complex(L, f);
824 success = read_line(L, f);
828 for (; nargs--; arg++) {
829 if (lua_type(L, arg) == LUA_TNUMBER) {
830 - /* optimization: could be done exactly as for strings */
832 - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
833 + if (lua_isinteger(L,arg))
834 + status = status && fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg)) > 0;
836 + status = status && fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
841 static const char *const modenames[] = {"no", "full", "line", NULL};
843 int op = luaL_checkoption(L, 2, NULL, modenames);
844 - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
845 + size_t sz = luaL_optint32(L, 3, LUAL_BUFFERSIZE);
846 int res = setvbuf(f, NULL, mode[op], sz);
847 return pushresult(L, res == 0, NULL);
849 Binary files ../lua-5.1.3/src/liolib.o and lua-5.1.3-patched/src/liolib.o differ
850 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/llex.c lua-5.1.3-patched/src/llex.c
851 --- ../lua-5.1.3/src/llex.c 2007-12-27 15:02:25.000000000 +0200
852 +++ lua-5.1.3-patched/src/llex.c 2008-03-04 11:10:30.000000000 +0200
865 -const char *const luaX_tokens [] = {
866 +static const char *const luaX_tokens [] = {
867 "and", "break", "do", "else", "elseif",
868 "end", "false", "for", "function", "if",
869 "in", "local", "nil", "not", "or", "repeat",
870 "return", "then", "true", "until", "while",
871 "..", "...", "==", ">=", "<=", "~=",
872 "<number>", "<name>", "<string>", "<eof>",
890 return luaZ_buffer(ls->buff);
892 @@ -173,23 +182,27 @@
893 if (p[n] == from) p[n] = to;
897 -static void trydecpoint (LexState *ls, SemInfo *seminfo) {
898 +/* TK_NUMBER (/ TK_NUMBER2) */
899 +static int trydecpoint (LexState *ls, SemInfo *seminfo) {
900 /* format error: try to update decimal point separator */
901 struct lconv *cv = localeconv();
902 char old = ls->decpoint;
904 ls->decpoint = (cv ? cv->decimal_point[0] : '.');
905 buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
906 - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
907 + ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, NULL);
909 /* format error with correct decimal point: no more options */
910 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
911 luaX_lexerror(ls, "malformed number", TK_NUMBER);
918 -static void read_numeral (LexState *ls, SemInfo *seminfo) {
919 +/* TK_NUMBER / TK_INT (/TK_NUMBER2) */
920 +static int read_numeral (LexState *ls, SemInfo *seminfo) {
922 lua_assert(isdigit(ls->current));
928 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
929 - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
930 - trydecpoint(ls, seminfo); /* try to update decimal point separator */
931 + ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, &seminfo->i );
932 + if (!ret) return trydecpoint(ls, seminfo); /* try to update decimal point separator */
942 static int llex (LexState *ls, SemInfo *seminfo) {
943 luaZ_resetbuffer(ls->buff);
947 else if (!isdigit(ls->current)) return '.';
949 - read_numeral(ls, seminfo);
951 + return read_numeral(ls, seminfo);
958 else if (isdigit(ls->current)) {
959 - read_numeral(ls, seminfo);
961 + return read_numeral(ls, seminfo);
963 else if (isalpha(ls->current) || ls->current == '_') {
964 /* identifier or reserved word */
965 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/llex.h lua-5.1.3-patched/src/llex.h
966 --- ../lua-5.1.3/src/llex.h 2007-12-27 15:02:25.000000000 +0200
967 +++ lua-5.1.3-patched/src/llex.h 2008-03-04 11:12:08.000000000 +0200
969 TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
970 /* other terminal symbols */
971 TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
972 - TK_NAME, TK_STRING, TK_EOS
973 + TK_NAME, TK_STRING, TK_EOS, TK_INT
975 + , TK_NUMBER2 /* imaginary constants: Ni */
979 /* number of reserved words */
980 #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
983 -/* array with token `names' */
984 -LUAI_DATA const char *const luaX_tokens [];
987 +/* SemInfo is a local data structure of 'llex.c', used for carrying a string
988 + * or a number. A separate token (TK_*) will tell, how to interpret the data.
994 } SemInfo; /* semantics information */
996 Binary files ../lua-5.1.3/src/llex.o and lua-5.1.3-patched/src/llex.o differ
997 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/llimits.h lua-5.1.3-patched/src/llimits.h
998 --- ../lua-5.1.3/src/llimits.h 2007-12-27 15:02:25.000000000 +0200
999 +++ lua-5.1.3-patched/src/llimits.h 2008-03-04 11:16:43.000000000 +0200
1002 /* result of a `usual argument conversion' over lua_Number */
1003 typedef LUAI_UACNUMBER l_uacNumber;
1004 +typedef LUAI_UACINTEGER l_uacInteger;
1007 /* internal assertions for in-house debugging */
1009 #define cast_int(i) cast(int, (i))
1014 ** type for virtual-machine instructions
1015 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
1016 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lmathlib.c lua-5.1.3-patched/src/lmathlib.c
1017 --- ../lua-5.1.3/src/lmathlib.c 2007-12-27 15:02:25.000000000 +0200
1018 +++ lua-5.1.3-patched/src/lmathlib.c 2008-03-26 11:32:25.000000000 +0200
1020 ** See Copyright Notice in lua.h
1027 @@ -16,113 +15,210 @@
1028 #include "lauxlib.h"
1031 +/* 'luai_vectpow()' as a replacement for 'cpow()'. Defined in the header; we
1032 + * don't intrude the code libs internal functions.
1034 +#ifdef LNUM_COMPLEX
1039 -#define PI (3.14159265358979323846)
1040 -#define RADIANS_PER_DEGREE (PI/180.0)
1043 +# define PI (3.14159265358979323846F)
1044 +#elif defined(M_PI)
1047 +# define PI (3.14159265358979323846264338327950288)
1049 +#define RADIANS_PER_DEGREE (PI/180)
1053 +# define HUGE HUGE_VALF
1054 +#elif defined(LNUM_LDOUBLE)
1055 +# define HUGE HUGE_VALL
1057 +# define HUGE HUGE_VAL
1060 static int math_abs (lua_State *L) {
1061 - lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
1062 +#ifdef LNUM_COMPLEX
1063 + lua_pushnumber(L, _LF(cabs) (luaL_checkcomplex(L,1)));
1065 + lua_pushnumber(L, _LF(fabs) (luaL_checknumber(L, 1)));
1070 static int math_sin (lua_State *L) {
1071 - lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
1072 +#ifdef LNUM_COMPLEX
1073 + lua_pushcomplex(L, _LF(csin) (luaL_checkcomplex(L,1)));
1075 + lua_pushnumber(L, _LF(sin) (luaL_checknumber(L, 1)));
1080 static int math_sinh (lua_State *L) {
1081 - lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
1082 +#ifdef LNUM_COMPLEX
1083 + lua_pushcomplex(L, _LF(csinh) (luaL_checkcomplex(L,1)));
1085 + lua_pushnumber(L, _LF(sinh) (luaL_checknumber(L, 1)));
1090 static int math_cos (lua_State *L) {
1091 - lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
1092 +#ifdef LNUM_COMPLEX
1093 + lua_pushcomplex(L, _LF(ccos) (luaL_checkcomplex(L,1)));
1095 + lua_pushnumber(L, _LF(cos) (luaL_checknumber(L, 1)));
1100 static int math_cosh (lua_State *L) {
1101 - lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
1102 +#ifdef LNUM_COMPLEX
1103 + lua_pushcomplex(L, _LF(ccosh) (luaL_checkcomplex(L,1)));
1105 + lua_pushnumber(L, _LF(cosh) (luaL_checknumber(L, 1)));
1110 static int math_tan (lua_State *L) {
1111 - lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
1112 +#ifdef LNUM_COMPLEX
1113 + lua_pushcomplex(L, _LF(ctan) (luaL_checkcomplex(L,1)));
1115 + lua_pushnumber(L, _LF(tan) (luaL_checknumber(L, 1)));
1120 static int math_tanh (lua_State *L) {
1121 - lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
1122 +#ifdef LNUM_COMPLEX
1123 + lua_pushcomplex(L, _LF(ctanh) (luaL_checkcomplex(L,1)));
1125 + lua_pushnumber(L, _LF(tanh) (luaL_checknumber(L, 1)));
1130 static int math_asin (lua_State *L) {
1131 - lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
1132 +#ifdef LNUM_COMPLEX
1133 + lua_pushcomplex(L, _LF(casin) (luaL_checkcomplex(L,1)));
1135 + lua_pushnumber(L, _LF(asin) (luaL_checknumber(L, 1)));
1140 static int math_acos (lua_State *L) {
1141 - lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
1142 +#ifdef LNUM_COMPLEX
1143 + lua_pushcomplex(L, _LF(cacos) (luaL_checkcomplex(L,1)));
1145 + lua_pushnumber(L, _LF(acos) (luaL_checknumber(L, 1)));
1150 static int math_atan (lua_State *L) {
1151 - lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
1152 +#ifdef LNUM_COMPLEX
1153 + lua_pushcomplex(L, _LF(catan) (luaL_checkcomplex(L,1)));
1155 + lua_pushnumber(L, _LF(atan) (luaL_checknumber(L, 1)));
1160 static int math_atan2 (lua_State *L) {
1161 - lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1162 + /* scalars only */
1163 + lua_pushnumber(L, _LF(atan2) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1167 static int math_ceil (lua_State *L) {
1168 - lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
1169 +#ifdef LNUM_COMPLEX
1170 + lua_Complex v= luaL_checkcomplex(L, 1);
1171 + lua_pushcomplex(L, _LF(ceil) (_LF(creal)(v)) + _LF(ceil) (_LF(cimag)(v))*I);
1173 + lua_pushnumber(L, _LF(ceil) (luaL_checknumber(L, 1)));
1178 static int math_floor (lua_State *L) {
1179 - lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
1180 +#ifdef LNUM_COMPLEX
1181 + lua_Complex v= luaL_checkcomplex(L, 1);
1182 + lua_pushcomplex(L, _LF(floor) (_LF(creal)(v)) + _LF(floor) (_LF(cimag)(v))*I);
1184 + lua_pushnumber(L, _LF(floor) (luaL_checknumber(L, 1)));
1189 -static int math_fmod (lua_State *L) {
1190 - lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1191 +static int math_fmod (lua_State *L) {
1192 + /* scalars only */
1193 + lua_pushnumber(L, _LF(fmod) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1197 static int math_modf (lua_State *L) {
1199 - double fp = modf(luaL_checknumber(L, 1), &ip);
1200 + /* scalars only */
1202 + lua_Number fp = _LF(modf) (luaL_checknumber(L, 1), &ip);
1203 lua_pushnumber(L, ip);
1204 lua_pushnumber(L, fp);
1208 static int math_sqrt (lua_State *L) {
1209 - lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
1210 +#ifdef LNUM_COMPLEX
1211 + lua_pushcomplex(L, _LF(csqrt) (luaL_checkcomplex(L,1)));
1213 + lua_pushnumber(L, _LF(sqrt) (luaL_checknumber(L, 1)));
1218 static int math_pow (lua_State *L) {
1219 - lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1220 +#ifdef LNUM_COMPLEX
1221 + /* C99 'cpow' gives somewhat inaccurate results (i.e. (-1)^2 = -1+1.2246467991474e-16i).
1222 + * 'luai_vectpow' smoothens such, reusing it is the reason we need to #include "lnum.h".
1224 + lua_pushcomplex(L, luai_vectpow(luaL_checkcomplex(L,1), luaL_checkcomplex(L,2)));
1226 + lua_pushnumber(L, _LF(pow) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1231 static int math_log (lua_State *L) {
1232 - lua_pushnumber(L, log(luaL_checknumber(L, 1)));
1233 +#ifdef LNUM_COMPLEX
1234 + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)));
1236 + lua_pushnumber(L, _LF(log) (luaL_checknumber(L, 1)));
1241 static int math_log10 (lua_State *L) {
1242 - lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
1243 +#ifdef LNUM_COMPLEX
1244 + /* Not in standard <complex.h> , but easy to calculate: log_a(x) = log_b(x) / log_b(a)
1246 + lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)) / _LF(log) (10));
1248 + lua_pushnumber(L, _LF(log10) (luaL_checknumber(L, 1)));
1253 static int math_exp (lua_State *L) {
1254 - lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
1255 +#ifdef LNUM_COMPLEX
1256 + lua_pushcomplex(L, _LF(cexp) (luaL_checkcomplex(L,1)));
1258 + lua_pushnumber(L, _LF(exp) (luaL_checknumber(L, 1)));
1263 @@ -138,19 +234,20 @@
1265 static int math_frexp (lua_State *L) {
1267 - lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
1268 + lua_pushnumber(L, _LF(frexp) (luaL_checknumber(L, 1), &e));
1269 lua_pushinteger(L, e);
1273 static int math_ldexp (lua_State *L) {
1274 - lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
1275 + lua_pushnumber(L, _LF(ldexp) (luaL_checknumber(L, 1), luaL_checkint(L, 2)));
1281 static int math_min (lua_State *L) {
1282 + /* scalars only */
1283 int n = lua_gettop(L); /* number of arguments */
1284 lua_Number dmin = luaL_checknumber(L, 1);
1289 static int math_max (lua_State *L) {
1290 + /* scalars only */
1291 int n = lua_gettop(L); /* number of arguments */
1292 lua_Number dmax = luaL_checknumber(L, 1);
1294 @@ -182,25 +280,20 @@
1295 /* the `%' avoids the (rare) case of r==1, and is needed also because on
1296 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
1297 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
1298 - switch (lua_gettop(L)) { /* check number of arguments */
1299 - case 0: { /* no arguments */
1300 - lua_pushnumber(L, r); /* Number between 0 and 1 */
1303 - case 1: { /* only upper limit */
1304 - int u = luaL_checkint(L, 1);
1305 - luaL_argcheck(L, 1<=u, 1, "interval is empty");
1306 - lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
1309 - case 2: { /* lower and upper limits */
1310 - int l = luaL_checkint(L, 1);
1311 - int u = luaL_checkint(L, 2);
1312 - luaL_argcheck(L, l<=u, 2, "interval is empty");
1313 - lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
1316 - default: return luaL_error(L, "wrong number of arguments");
1317 + int n= lua_gettop(L); /* number of arguments */
1318 + if (n==0) { /* no arguments: range [0,1) */
1319 + lua_pushnumber(L, r);
1320 + } else if (n<=2) { /* int range [1,u] or [l,u] */
1321 + int l= n==1 ? 1 : luaL_checkint(L, 1);
1322 + int u = luaL_checkint(L, n);
1325 + luaL_argcheck(L, l<=u, n, "interval is empty");
1326 + d= _LF(floor)(r*(u-l+1));
1327 + lua_number2int(tmp,d);
1328 + lua_pushinteger(L, l+tmp);
1330 + return luaL_error(L, "wrong number of arguments");
1334 @@ -211,6 +304,66 @@
1339 +* Lua 5.1 does not have acosh, asinh, atanh for scalars (not ANSI C)
1341 +#if __STDC_VERSION__ >= 199901L
1342 +static int math_acosh (lua_State *L) {
1343 +# ifdef LNUM_COMPLEX
1344 + lua_pushcomplex(L, _LF(cacosh) (luaL_checkcomplex(L,1)));
1346 + lua_pushnumber(L, _LF(acosh) (luaL_checknumber(L,1)));
1350 +static int math_asinh (lua_State *L) {
1351 +# ifdef LNUM_COMPLEX
1352 + lua_pushcomplex(L, _LF(casinh) (luaL_checkcomplex(L,1)));
1354 + lua_pushnumber(L, _LF(asinh) (luaL_checknumber(L,1)));
1358 +static int math_atanh (lua_State *L) {
1359 +# ifdef LNUM_COMPLEX
1360 + lua_pushcomplex(L, _LF(catanh) (luaL_checkcomplex(L,1)));
1362 + lua_pushnumber(L, _LF(atanh) (luaL_checknumber(L,1)));
1369 + * C99 complex functions, not covered above.
1371 +#ifdef LNUM_COMPLEX
1372 +static int math_arg (lua_State *L) {
1373 + lua_pushnumber(L, _LF(carg) (luaL_checkcomplex(L,1)));
1377 +static int math_imag (lua_State *L) {
1378 + lua_pushnumber(L, _LF(cimag) (luaL_checkcomplex(L,1)));
1382 +static int math_real (lua_State *L) {
1383 + lua_pushnumber(L, _LF(creal) (luaL_checkcomplex(L,1)));
1387 +static int math_conj (lua_State *L) {
1388 + lua_pushcomplex(L, _LF(conj) (luaL_checkcomplex(L,1)));
1392 +static int math_proj (lua_State *L) {
1393 + lua_pushcomplex(L, _LF(cproj) (luaL_checkcomplex(L,1)));
1399 static const luaL_Reg mathlib[] = {
1401 @@ -241,6 +394,18 @@
1402 {"sqrt", math_sqrt},
1403 {"tanh", math_tanh},
1405 +#if __STDC_VERSION__ >= 199901L
1406 + {"acosh", math_acosh},
1407 + {"asinh", math_asinh},
1408 + {"atanh", math_atanh},
1410 +#ifdef LNUM_COMPLEX
1411 + {"arg", math_arg},
1412 + {"imag", math_imag},
1413 + {"real", math_real},
1414 + {"conj", math_conj},
1415 + {"proj", math_proj},
1420 @@ -252,8 +417,10 @@
1421 luaL_register(L, LUA_MATHLIBNAME, mathlib);
1422 lua_pushnumber(L, PI);
1423 lua_setfield(L, -2, "pi");
1424 - lua_pushnumber(L, HUGE_VAL);
1425 + lua_pushnumber(L, HUGE);
1426 lua_setfield(L, -2, "huge");
1427 + lua_pushinteger(L, LUA_INTEGER_MAX );
1428 + lua_setfield(L, -2, "hugeint");
1429 #if defined(LUA_COMPAT_MOD)
1430 lua_getfield(L, -1, "fmod");
1431 lua_setfield(L, -2, "mod");
1432 Binary files ../lua-5.1.3/src/lmathlib.o and lua-5.1.3-patched/src/lmathlib.o differ
1433 Binary files ../lua-5.1.3/src/lmem.o and lua-5.1.3-patched/src/lmem.o differ
1434 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lnum.c lua-5.1.3-patched/src/lnum.c
1435 --- ../lua-5.1.3/src/lnum.c 1970-01-01 02:00:00.000000000 +0200
1436 +++ lua-5.1.3-patched/src/lnum.c 2008-03-26 11:32:25.000000000 +0200
1439 +** $Id: lnum.c,v ... $
1440 +** Internal number model
1441 +** See Copyright Notice in lua.h
1444 +#include <stdlib.h>
1447 +#include <string.h>
1459 +** lua_real2str converts a (non-complex) number to a string.
1460 +** lua_str2real converts a string to a (non-complex) number.
1462 +#define lua_real2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
1465 +* Note: Only 'strtod()' is part of ANSI C; others are C99 and
1466 +* may need '--std=c99' compiler setting (at least on Ubuntu 7.10).
1468 +* Visual C++ 2008 Express does not have 'strtof()', nor 'strtold()'.
1469 +* References to '_strtold()' exist but don't compile. It seems best
1470 +* to leave Windows users with DOUBLE only (or compile with MinGW).
1472 +* In practise, using '(long double)strtod' is a risky thing, since
1473 +* it will cause accuracy loss in reading in numbers, and such losses
1474 +* will pile up in later processing. Get a real 'strtold()' or don't
1475 +* use that mode at all.
1478 +# define lua_str2real strtod
1479 +#elif defined(LNUM_FLOAT)
1480 +# define lua_str2real strtof
1481 +#elif defined(LNUM_LDOUBLE)
1482 +# define lua_str2real strtold
1485 +#define lua_integer2str(s,v) sprintf((s), LUA_INTEGER_FMT, (v))
1487 +/* 's' is expected to be LUAI_MAXNUMBER2STR long (enough for any number)
1489 +void luaO_num2buf( char *s, const TValue *o )
1492 + lua_assert( ttisnumber(o) );
1494 + /* Reason to handle integers differently is not only speed, but accuracy as
1495 + * well. We want to make any integer tostring() without roundings, at all.
1498 + lua_integer2str( s, ivalue(o) );
1501 + n= nvalue_fast(o);
1502 + lua_real2str(s, n);
1504 +#ifdef LNUM_COMPLEX
1505 + lua_Number n2= nvalue_img_fast(o);
1506 + if (n2!=0) { /* Postfix with +-Ni */
1507 + int re0= (n == 0);
1508 + char *s2= re0 ? s : strchr(s,'\0');
1509 + if ((!re0) && (n2>0)) *s2++= '+';
1510 + lua_real2str( s2, n2 );
1517 +* If a LUA_TNUMBER has integer value, give it.
1519 +int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref ) {
1523 + lua_assert( ttype(o)==LUA_TNUMBER );
1524 + lua_assert( ref );
1525 +#ifdef LNUM_COMPLEX
1526 + if (nvalue_img_fast(o)!=0) return 0;
1528 + d= nvalue_fast(o);
1529 + lua_number2integer(i, d);
1530 + if (cast_num(i) == d) {
1531 + *ref= i; return 1;
1537 + * Lua 5.1.3 (using 'strtod()') allows 0x+hex but not 0+octal. This is good,
1538 + * and we should NOT use 'autobase' 0 with 'strtoul[l]()' for this reason.
1540 + * Lua 5.1.3 allows '0x...' numbers to overflow and lose precision; this is not
1541 + * good. On Visual C++ 2008, 'strtod()' does not even take them in. Better to
1542 + * require hex values to fit 'lua_Integer' or give an error that they don't?
1544 + * Full hex range (0 .. 0xff..ff) is stored as integers, not to lose any bits.
1545 + * Numerical value of 0xff..ff will be -1, if used in calculations.
1547 + * Returns: TK_INT for a valid integer, '*endptr_ref' updated
1548 + * TK_NUMBER for seemingly numeric, to be parsed as floating point
1549 + * 0 for bad characters, not a number (or '0x' out of range)
1551 +static int luaO_str2i (const char *s, lua_Integer *res, char **endptr_ref) {
1553 + /* 'v' gets ULONG_MAX on possible overflow (which is > LUA_INTEGER_MAX);
1554 + * we don't have to check 'errno' here.
1556 + unsigned LUA_INTEGER v= lua_str2ul(s, &endptr, 10);
1557 + if (endptr == s) return 0; /* nothing numeric */
1558 + if (v==0 && *endptr=='x') {
1559 + errno= 0; /* needs to be set, 'strtoul[l]' does not clear it */
1560 + v= lua_str2ul(endptr+1, &endptr, 16); /* retry as hex, unsigned range */
1561 + if (errno==ERANGE) { /* clamped to 0xff..ff */
1562 +#if (defined(LNUM_INT32) && !defined(LNUM_FLOAT)) || defined(LNUM_LDOUBLE)
1563 + return TK_NUMBER; /* Allow to be read as floating point (has more integer range) */
1565 + return 0; /* Reject the number */
1568 + } else if ((v > LUA_INTEGER_MAX) || (*endptr && (!isspace(*endptr)))) {
1569 + return TK_NUMBER; /* not in signed range, or has '.', 'e' etc. trailing */
1571 + *res= (lua_Integer)v;
1572 + *endptr_ref= endptr;
1576 +/* 0 / TK_NUMBER / TK_INT (/ TK_NUMBER2) */
1577 +int luaO_str2d (const char *s, lua_Number *res_n, lua_Integer *res_i) {
1579 + int ret= TK_NUMBER;
1580 + /* Check integers first, if caller is allowing.
1581 + * If 'res2'==NULL, they're only looking for floating point.
1584 + ret= luaO_str2i(s,res_i,&endptr);
1585 + if (ret==0) return 0;
1587 + if (ret==TK_NUMBER) {
1588 + lua_assert(res_n);
1589 + /* Note: Visual C++ 2008 Express 'strtod()' does not read in "0x..."
1590 + * numbers; it will read '0' and spit 'x' as endptr.
1591 + * This means hex constants not fitting in 'lua_Integer' won't
1592 + * be read in at all. What to do?
1594 + *res_n = lua_str2real(s, &endptr);
1595 + if (endptr == s) return 0; /* conversion failed */
1596 + /* Visual C++ 2008 'strtod()' does not allow "0x..." input. */
1597 +#if defined(_MSC_VER) && !defined(LNUM_FLOAT) && !defined(LNUM_INT64)
1598 + if (*res_n==0 && *endptr=='x') {
1599 + /* Hex constant too big for 'lua_Integer' but that could fit in 'lua_Number'
1602 + unsigned __int64 v= _strtoui64( s, &endptr, 16 );
1603 + /* We just let > 64 bit values be clamped to _UI64_MAX (MSDN does not say 'errno'==ERANGE would be set) */
1604 + *res_n= cast_num(v);
1605 + if (*res_n != v) return 0; /* Would have lost accuracy */
1608 +#ifdef LNUM_COMPLEX
1609 + if (*endptr == 'i') { endptr++; ret= TK_NUMBER2; }
1613 + while (isspace(cast(unsigned char, *endptr))) endptr++;
1614 + if (*endptr) return 0; /* invalid trail */
1620 +/* Functions for finding out, when integer operations remain in range
1621 + * (and doing them).
1623 +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1624 + lua_Integer v= ib+ic; /* may overflow */
1625 + if (ib>0 && ic>0) { if (v < 0) return 0; /*overflow, use floats*/ }
1626 + else if (ib<0 && ic<0) { if (v >= 0) return 0; }
1631 +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1632 + lua_Integer v= ib-ic; /* may overflow */
1633 + if (ib>=0 && ic<0) { if (v < 0) return 0; /*overflow, use floats*/ }
1634 + else if (ib<0 && ic>0) { if (v >= 0) return 0; }
1639 +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1640 + if (ib!=LUA_INTEGER_MIN && ic!=LUA_INTEGER_MIN) {
1641 + lua_Integer b= luai_abs(ib), c= luai_abs(ic);
1642 + if ( (ib==0) || (LUA_INTEGER_MAX/b >= c) ) {
1643 + *r= ib*ic; /* no overflow */
1646 + } else if (ib==0 || ic==0) {
1650 + /* Result can be LUA_INTEGER_MIN; if it is, calculating it using floating
1651 + * point will not cause accuracy loss.
1653 + if ( luai_nummul( cast_num(ib), cast_num(ic) ) == LUA_INTEGER_MIN ) {
1654 + *r= LUA_INTEGER_MIN;
1660 +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1661 + /* N/0: leave to float side, to give an error
1663 + if (ic==0) return 0;
1665 + /* N/LUA_INTEGER_MIN: always non-integer results, or 0 or +1
1667 + if (ic==LUA_INTEGER_MIN) {
1668 + if (ib==LUA_INTEGER_MIN) { *r=1; return 1; }
1669 + if (ib==0) { *r=0; return 1; }
1671 + /* LUA_INTEGER_MIN (-2^31|63)/N: calculate using float side (either the division
1672 + * causes non-integer results, or there is no accuracy loss in int->fp->int
1673 + * conversions (N=2,4,8,..,256 and N=2^30,2^29,..2^23).
1675 + } else if (ib==LUA_INTEGER_MIN) {
1676 + lua_Number d= luai_numdiv( cast_num(LUA_INTEGER_MIN), cast_num(ic) );
1677 + lua_Integer i; lua_number2integer(i,d);
1678 + if (cast_num(i)==d) { *r= i; return 1; }
1681 + /* Note: We _can_ use ANSI C mod here, even on negative values, since
1682 + * we only test for == 0 (the sign would be implementation dependent).
1684 + if (ib%ic == 0) { *r= ib/ic; return 1; }
1690 +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1692 + /* ANSI C can be trusted when b%c==0, or when values are non-negative.
1693 + * b - (floor(b/c) * c)
1695 + * + +: b - (b/c) * c (b % c can be used)
1696 + * - -: b - (b/c) * c (b % c could work, but not defined by ANSI C)
1697 + * 0 -: b - (b/c) * c (=0, b % c could work, but not defined by ANSI C)
1698 + * - +: b - (b/c-1) * c (when b!=-c)
1699 + * + -: b - (b/c-1) * c (when b!=-c)
1701 + * o MIN%MIN ends up 0, via overflow in calcs but that does not matter.
1702 + * o MIN%MAX ends up MAX-1 (and other such numbers), also after overflow,
1703 + * but that does not matter, results do.
1705 + lua_Integer v= ib % ic;
1706 + if ( v!=0 && (ib<0 || ic<0) ) {
1707 + v= ib - ((ib/ic) - ((ib<=0 && ic<0) ? 0:1)) * ic;
1709 + /* Result should always have same sign as 2nd argument. (PIL2) */
1710 + lua_assert( (v<0) ? (ic<0) : (v>0) ? (ic>0) : 1 );
1714 + return 0; /* let float side return NaN */
1717 +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1719 + /* In FLOAT/INT32 or FLOAT|DOUBLE/INT64 modes, calculating integer powers
1720 + * via FP realm may lose accuracy (i.e. 7^11 = 1977326743, which fits int32
1721 + * but not 23-bit float mantissa).
1723 + * The current solution is dumb, but it works and uses little code. Use of
1724 + * integer powers is not anticipated to be very frequent (apart from 2^x,
1725 + * which is separately optimized).
1728 + else if (ic<0) return 0; /* FP realm */
1729 + else if (ib==2 && ic < (int)sizeof(lua_Integer)*8-1) *r= ((lua_Integer)1)<<ic; /* 1,2,4,...2^30 | 2^62 optimization */
1730 + else if (ic==0) *r=1;
1731 + else if (luai_abs(ib)==1) *r= (ic%2) ? ib:1;
1733 + lua_Integer x= ib;
1735 + if (!try_mulint( &x, x, ib ))
1736 + return 0; /* FP realm */
1743 +int try_unmint( lua_Integer *r, lua_Integer ib ) {
1744 + /* Negating LUA_INTEGER_MIN leaves the range. */
1745 + if ( ib != LUA_INTEGER_MIN )
1746 + { *r= -ib; return 1; }
1750 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lnum.h lua-5.1.3-patched/src/lnum.h
1751 --- ../lua-5.1.3/src/lnum.h 1970-01-01 02:00:00.000000000 +0200
1752 +++ lua-5.1.3-patched/src/lnum.h 2008-03-19 11:35:51.000000000 +0200
1755 +** $Id: lnum.h,v ... $
1756 +** Internal Number model
1757 +** See Copyright Notice in lua.h
1765 +#include "lobject.h"
1768 +** The luai_num* macros define the primitive operations over 'lua_Number's
1769 +** (not 'lua_Integer's, not 'lua_Complex').
1771 +#define luai_numadd(a,b) ((a)+(b))
1772 +#define luai_numsub(a,b) ((a)-(b))
1773 +#define luai_nummul(a,b) ((a)*(b))
1774 +#define luai_numdiv(a,b) ((a)/(b))
1775 +#define luai_nummod(a,b) ((a) - _LF(floor)((a)/(b))*(b))
1776 +#define luai_numpow(a,b) (_LF(pow)(a,b))
1777 +#define luai_numunm(a) (-(a))
1778 +#define luai_numeq(a,b) ((a)==(b))
1779 +#define luai_numlt(a,b) ((a)<(b))
1780 +#define luai_numle(a,b) ((a)<=(b))
1781 +#define luai_numisnan(a) (!luai_numeq((a), (a)))
1783 +int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1784 +int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1785 +int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1786 +int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1787 +int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1788 +int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1789 +int try_unmint( lua_Integer *r, lua_Integer ib );
1791 +#ifdef LNUM_COMPLEX
1792 + static inline lua_Complex luai_vectunm( lua_Complex a ) { return -a; }
1793 + static inline lua_Complex luai_vectadd( lua_Complex a, lua_Complex b ) { return a+b; }
1794 + static inline lua_Complex luai_vectsub( lua_Complex a, lua_Complex b ) { return a-b; }
1795 + static inline lua_Complex luai_vectmul( lua_Complex a, lua_Complex b ) { return a*b; }
1796 + static inline lua_Complex luai_vectdiv( lua_Complex a, lua_Complex b ) { return a/b; }
1799 + * C99 does not provide modulus for complex numbers. It most likely is not
1800 + * meaningful at all.
1806 + * C99 'cpow' gives inaccurate results for many common cases s.a. (1i)^2 ->
1807 + * -1+1.2246467991474e-16i (OS X 10.4, gcc 4.0.1 build 5367)
1809 + * [(a+bi)^(c+di)] = (r^c) * exp(-d*t) * cos(c*t + d*ln(r)) +
1810 + * = (r^c) * exp(-d*t) * sin(c*t + d*ln(r)) *i
1811 + * r = sqrt(a^2+b^2), t = arctan( b/a )
1813 + * Reference: <http://home.att.net/~srschmitt/complexnumbers.html>
1814 + * Could also be calculated using: x^y = exp(ln(x)*y)
1816 + * Note: Defined here (and not in .c) so 'lmathlib.c' can share the
1820 + lua_Complex luai_vectpow( lua_Complex a, lua_Complex b )
1823 + lua_Number ar= _LF(creal)(a), ai= _LF(cimag)(a);
1824 + lua_Number br= _LF(creal)(b), bi= _LF(cimag)(b);
1826 + if (ai==0 && bi==0) { /* a^c (real) */
1827 + return luai_numpow( ar, br );
1830 + int br_int= (int)br;
1832 + if ( ai!=0 && bi==0 && br_int==br && br_int!=0 && br_int!=INT_MIN ) {
1833 + /* (a+bi)^N, N = { +-1,+-2, ... +-INT_MAX }
1835 + lua_Number k= luai_numpow( _LF(sqrt) (ar*ar + ai*ai), br );
1836 + lua_Number cos_z, sin_z;
1838 + /* Situation depends upon c (N) in the following manner:
1840 + * N%4==0 => cos(c*t)=1, sin(c*t)=0
1841 + * (N*sign(b))%4==1 or (N*sign(b))%4==-3 => cos(c*t)=0, sin(c*t)=1
1842 + * N%4==2 or N%4==-2 => cos(c*t)=-1, sin(c*t)=0
1843 + * (N*sign(b))%4==-1 or (N*sign(b))%4==3 => cos(c*t)=0, sin(c*t)=-1
1845 + int br_int_abs = br_int<0 ? -br_int:br_int;
1847 + switch( (br_int_abs%4) * (br_int<0 ? -1:1) * (ai<0 ? -1:1) ) {
1848 + case 0: cos_z=1, sin_z=0; break;
1849 + case 2: case -2: cos_z=-1, sin_z=0; break;
1850 + case 1: case -3: cos_z=0, sin_z=1; break;
1851 + case 3: case -1: cos_z=0, sin_z=-1; break;
1852 + default: lua_assert(0); return 0;
1854 + return k*cos_z + (k*sin_z)*I;
1857 + return _LF(cpow) ( a, b );
1861 +LUAI_FUNC int luaO_str2d (const char *s, lua_Number *res1, lua_Integer *res2);
1862 +LUAI_FUNC void luaO_num2buf( char *s, const TValue *o );
1864 +LUAI_FUNC int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref );
1866 +#define luai_normalize(o) \
1867 +{ lua_Integer _i; if (tt_integer_valued(o,&_i)) setivalue(o,_i); }
1870 Binary files ../lua-5.1.3/src/lnum.o and lua-5.1.3-patched/src/lnum.o differ
1871 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lnum_config.h lua-5.1.3-patched/src/lnum_config.h
1872 --- ../lua-5.1.3/src/lnum_config.h 1970-01-01 02:00:00.000000000 +0200
1873 +++ lua-5.1.3-patched/src/lnum_config.h 2008-03-26 11:32:25.000000000 +0200
1876 +** $Id: lnum_config.h,v ... $
1877 +** Internal Number model
1878 +** See Copyright Notice in lua.h
1881 +#ifndef lnum_config_h
1882 +#define lnum_config_h
1885 +** Default number modes
1887 +#if (!defined LNUM_DOUBLE) && (!defined LNUM_FLOAT) && (!defined LNUM_LDOUBLE)
1888 +# define LNUM_DOUBLE
1890 +#if (!defined LNUM_INT16) && (!defined LNUM_INT32) && (!defined LNUM_INT64)
1891 +# define LNUM_INT32
1895 +** Require C99 mode for COMPLEX, FLOAT and LDOUBLE (only DOUBLE is ANSI C).
1897 +#if defined(LNUM_COMPLEX) && (__STDC_VERSION__ < 199901L)
1898 +# error "Need C99 for complex (use '--std=c99' or similar)"
1899 +#elif defined(LNUM_LDOUBLE) && (__STDC_VERSION__ < 199901L) && !defined(_MSC_VER)
1900 +# error "Need C99 for 'long double' (use '--std=c99' or similar)"
1901 +#elif defined(LNUM_FLOAT) && (__STDC_VERSION__ < 199901L)
1902 +/* LNUM_FLOAT not supported on Windows */
1903 +# error "Need C99 for 'float' (use '--std=c99' or similar)"
1907 +** Number mode identifier to accompany the version string.
1909 +#ifdef LNUM_COMPLEX
1910 +# define _LNUM1 "complex "
1915 +# define _LNUM2 "double"
1916 +#elif defined(LNUM_FLOAT)
1917 +# define _LNUM2 "float"
1918 +#elif defined(LNUM_LDOUBLE)
1919 +# define _LNUM2 "ldouble"
1922 +# define _LNUM3 "int32"
1923 +#elif defined(LNUM_INT64)
1924 +# define _LNUM3 "int64"
1925 +#elif defined(LNUM_INT16)
1926 +# define _LNUM3 "int16"
1928 +#define LUA_LNUM _LNUM1 _LNUM2 " " _LNUM3
1931 +** LUA_NUMBER is the type of floating point number in Lua
1932 +** LUA_NUMBER_SCAN is the format for reading numbers.
1933 +** LUA_NUMBER_FMT is the format for writing numbers.
1936 +# define LUA_NUMBER float
1937 +# define LUA_NUMBER_SCAN "%f"
1938 +# define LUA_NUMBER_FMT "%g"
1939 +#elif (defined LNUM_DOUBLE)
1940 +# define LUA_NUMBER double
1941 +# define LUA_NUMBER_SCAN "%lf"
1942 +# define LUA_NUMBER_FMT "%.14g"
1943 +#elif (defined LNUM_LDOUBLE)
1944 +# define LUA_NUMBER long double
1945 +# define LUA_NUMBER_SCAN "%Lg"
1946 +# define LUA_NUMBER_FMT "%.20Lg"
1951 +** LUAI_MAXNUMBER2STR: size of a buffer fitting any number->string result.
1953 +** double: 24 (sign, x.xxxxxxxxxxxxxxe+nnnn, and \0)
1954 +** int64: 21 (19 digits, sign, and \0)
1955 +** long double: 43 for 128-bit (sign, x.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
1956 +** 30 for 80-bit (sign, x.xxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
1958 +#ifdef LNUM_LDOUBLE
1959 +# define _LUAI_MN2S 44
1961 +# define _LUAI_MN2S 24
1964 +#ifdef LNUM_COMPLEX
1965 +# define LUAI_MAXNUMBER2STR (2*_LUAI_MN2S)
1967 +# define LUAI_MAXNUMBER2STR _LUAI_MN2S
1971 +** LUA_INTEGER is the integer type used by lua_pushinteger/lua_tointeger/lua_isinteger.
1972 +** LUA_INTEGER_SCAN is the format for reading integers
1973 +** LUA_INTEGER_FMT is the format for writing integers
1975 +** Note: Visual C++ 2005 does not have 'strtoull()', use '_strtoui64()' instead.
1978 +# if LUAI_BITSINT > 16
1979 +# define LUA_INTEGER int
1980 +# define LUA_INTEGER_SCAN "%d"
1981 +# define LUA_INTEGER_FMT "%d"
1983 +/* Note: 'LUA_INTEGER' being 'ptrdiff_t' (as in Lua 5.1) causes problems with
1984 + * 'printf()' operations. Also 'unsigned ptrdiff_t' is invalid.
1986 +# define LUA_INTEGER long
1987 +# define LUA_INTEGER_SCAN "%ld"
1988 +# define LUA_INTEGER_FMT "%ld"
1990 +# define LUA_INTEGER_MAX 0x7FFFFFFF /* 2^31-1 */
1992 +#elif defined(LNUM_INT64)
1993 +# define LUA_INTEGER long long
1995 +# define lua_str2ul _strtoui64
1997 +# define lua_str2ul strtoull
1999 +# define LUA_INTEGER_SCAN "%lld"
2000 +# define LUA_INTEGER_FMT "%lld"
2001 +# define LUA_INTEGER_MAX 0x7fffffffffffffffLL /* 2^63-1 */
2002 +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX - 1LL) /* -2^63 */
2004 +#elif defined(LNUM_INT16)
2005 +# if LUAI_BITSINT > 16
2006 +# define LUA_INTEGER short
2007 +# define LUA_INTEGER_SCAN "%hd"
2008 +# define LUA_INTEGER_FMT "%hd"
2010 +# define LUA_INTEGER int
2011 +# define LUA_INTEGER_SCAN "%d"
2012 +# define LUA_INTEGER_FMT "%d"
2014 +# define LUA_INTEGER_MAX 0x7FFF /* 2^16-1 */
2018 +# define lua_str2ul (unsigned LUA_INTEGER)strtoul
2020 +#ifndef LUA_INTEGER_MIN
2021 +# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX -1) /* -2^16|32 */
2025 +@@ lua_number2int is a macro to convert lua_Number to int.
2026 +@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
2027 +** CHANGE them if you know a faster way to convert a lua_Number to
2028 +** int (with any rounding method and without throwing errors) in your
2029 +** system. In Pentium machines, a naive typecast from double to int
2030 +** in C is extremely slow, so any alternative is worth trying.
2033 +/* On a Pentium, resort to a trick */
2034 +#if defined(LNUM_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
2035 + (defined(__i386) || defined (_M_IX86) || defined(__i386__))
2037 +/* On a Microsoft compiler, use assembler */
2038 +# if defined(_MSC_VER)
2039 +# define lua_number2int(i,d) __asm fld d __asm fistp i
2042 +/* the next trick should work on any Pentium, but sometimes clashes
2043 + with a DirectX idiosyncrasy */
2044 +union luai_Cast { double l_d; long l_l; };
2045 +# define lua_number2int(i,d) \
2046 + { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
2049 +# ifndef LNUM_INT64
2050 +# define lua_number2integer lua_number2int
2053 +/* this option always works, but may be slow */
2055 +# define lua_number2int(i,d) ((i)=(int)(d))
2058 +/* Note: Some compilers (OS X gcc 4.0?) may choke on double->long long conversion
2059 + * since it can lose precision. Others do require 'long long' there.
2061 +#ifndef lua_number2integer
2062 +# define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
2066 +** 'luai_abs()' to give absolute value of 'lua_Integer'
2069 +# define luai_abs abs
2070 +#elif defined(LNUM_INT64) && (__STDC_VERSION__ >= 199901L)
2071 +# define luai_abs llabs
2073 +# define luai_abs(v) ((v) >= 0 ? (v) : -(v))
2077 +** LUAI_UACNUMBER is the result of an 'usual argument conversion' over a number.
2078 +** LUAI_UACINTEGER the same, over an integer.
2080 +#define LUAI_UACNUMBER double
2081 +#define LUAI_UACINTEGER long
2083 +/* ANSI C only has math funcs for 'double. C99 required for float and long double
2087 +# define _LF(name) name
2088 +#elif defined(LNUM_FLOAT)
2089 +# define _LF(name) name ## f
2090 +#elif defined(LNUM_LDOUBLE)
2091 +# define _LF(name) name ## l
2096 Binary files ../lua-5.1.3/src/loadlib.o and lua-5.1.3-patched/src/loadlib.o differ
2097 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lobject.c lua-5.1.3-patched/src/lobject.c
2098 --- ../lua-5.1.3/src/lobject.c 2007-12-27 15:02:25.000000000 +0200
2099 +++ lua-5.1.3-patched/src/lobject.c 2008-03-14 11:08:49.000000000 +0200
2102 #include "lstring.h"
2109 const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
2113 int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
2114 - if (ttype(t1) != ttype(t2)) return 0;
2115 + if (!ttype_ext_same(t1,t2)) return 0;
2116 else switch (ttype(t1)) {
2120 + if (ttype(t2)==LUA_TINT)
2121 + return ivalue(t1) == ivalue(t2);
2122 + else { /* t1:int, t2:num */
2123 +#ifdef LNUM_COMPLEX
2124 + if (nvalue_img_fast(t2) != 0) return 0;
2126 + /* Avoid doing accuracy losing cast, if possible. */
2128 + if (tt_integer_valued(t2,&tmp))
2129 + return ivalue(t1) == tmp;
2131 + return luai_numeq( cast_num(ivalue(t1)), nvalue_fast(t2) );
2134 - return luai_numeq(nvalue(t1), nvalue(t2));
2135 + if (ttype(t2)==LUA_TINT)
2136 + return luaO_rawequalObj(t2, t1); /* swap LUA_TINT to left */
2137 +#ifdef LNUM_COMPLEX
2138 + if (!luai_numeq(nvalue_img_fast(t1), nvalue_img_fast(t2))) return 0;
2140 + return luai_numeq(nvalue_fast(t1), nvalue_fast(t2));
2142 return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
2143 case LUA_TLIGHTUSERDATA:
2149 -int luaO_str2d (const char *s, lua_Number *result) {
2151 - *result = lua_str2number(s, &endptr);
2152 - if (endptr == s) return 0; /* conversion failed */
2153 - if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
2154 - *result = cast_num(strtoul(s, &endptr, 16));
2155 - if (*endptr == '\0') return 1; /* most common case */
2156 - while (isspace(cast(unsigned char, *endptr))) endptr++;
2157 - if (*endptr != '\0') return 0; /* invalid trailing characters? */
2163 static void pushstr (lua_State *L, const char *str) {
2164 setsvalue2s(L, L->top, luaS_new(L, str));
2166 @@ -131,7 +136,11 @@
2170 - setnvalue(L->top, cast_num(va_arg(argp, int)));
2171 + /* This is tricky for 64-bit integers; maybe they even cannot be
2172 + * supported on all compilers; depends on the conversions applied to
2173 + * variable argument lists. TBD: test!
2175 + setivalue(L->top, (lua_Integer) va_arg(argp, l_uacInteger));
2184 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lobject.h lua-5.1.3-patched/src/lobject.h
2185 --- ../lua-5.1.3/src/lobject.h 2007-12-27 15:02:25.000000000 +0200
2186 +++ lua-5.1.3-patched/src/lobject.h 2008-03-19 11:40:13.000000000 +0200
2190 /* tags for values visible from Lua */
2191 -#define LAST_TAG LUA_TTHREAD
2192 +#if LUA_TINT > LUA_TTHREAD
2193 +# define LAST_TAG LUA_TINT
2195 +# define LAST_TAG LUA_TTHREAD
2198 #define NUM_TAGS (LAST_TAG+1)
2204 +#ifdef LNUM_COMPLEX
2215 /* Macros to test type */
2216 #define ttisnil(o) (ttype(o) == LUA_TNIL)
2217 -#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
2218 +#define ttisint(o) (ttype(o) == LUA_TINT)
2219 +#define ttisnumber(o) ((ttype(o) == LUA_TINT) || (ttype(o) == LUA_TNUMBER))
2220 +#ifdef LNUM_COMPLEX
2221 +# define ttiscomplex(o) ((ttype(o) == LUA_TNUMBER) && (nvalue_img_fast(o)!=0))
2223 #define ttisstring(o) (ttype(o) == LUA_TSTRING)
2224 #define ttistable(o) (ttype(o) == LUA_TTABLE)
2225 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
2227 #define ttype(o) ((o)->tt)
2228 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
2229 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
2230 -#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
2232 +#define ttype_ext(o) ( ttype(o) == LUA_TINT ? LUA_TNUMBER : ttype(o) )
2233 +#define ttype_ext_same(o1,o2) ( (ttype(o1)==ttype(o2)) || (ttisnumber(o1) && ttisnumber(o2)) )
2235 +/* '_fast' variants are for cases where 'ttype(o)' is known to be LUA_TNUMBER.
2237 +#ifdef LNUM_COMPLEX
2238 +# define nvalue_complex_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
2239 +# define nvalue_fast(o) ( _LF(creal) ( nvalue_complex_fast(o) ) )
2240 +# define nvalue_img_fast(o) ( _LF(cimag) ( nvalue_complex_fast(o) ) )
2241 +# define nvalue_complex(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? (o)->value.i : (o)->value.n )
2242 +# define nvalue_img(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? 0 : _LF(cimag)( (o)->value.n ) )
2243 +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : _LF(creal)((o)->value.n) )
2245 +# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : (o)->value.n )
2246 +# define nvalue_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
2248 +#define ivalue(o) check_exp( ttype(o)==LUA_TINT, (o)->value.i )
2250 #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
2251 #define tsvalue(o) (&rawtsvalue(o)->tsv)
2252 #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
2253 @@ -116,8 +147,27 @@
2254 /* Macros to set values */
2255 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
2257 -#define setnvalue(obj,x) \
2258 - { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
2259 +/* Must not have side effects, 'x' may be expression.
2261 +#define setivalue(obj,x) \
2262 + { TValue *i_o=(obj); i_o->value.i=(x); i_o->tt=LUA_TINT; }
2264 +# define setnvalue(obj,x) \
2265 + { TValue *i_o=(obj); i_o->value.n= (x); i_o->tt=LUA_TNUMBER; }
2267 +/* Note: Complex always has "inline", both are C99.
2269 +#ifdef LNUM_COMPLEX
2270 + static inline void setnvalue_complex_fast( TValue *obj, lua_Complex x ) {
2271 + lua_assert( _LF(cimag)(x) != 0 );
2272 + obj->value.n= x; obj->tt= LUA_TNUMBER;
2274 + static inline void setnvalue_complex( TValue *obj, lua_Complex x ) {
2275 + if (_LF(cimag)(x) == 0) { setnvalue(obj, _LF(creal)(x)); }
2276 + else { obj->value.n= x; obj->tt= LUA_TNUMBER; }
2281 #define setpvalue(obj,x) \
2282 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
2284 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
2285 checkliveness(G(L),i_o); }
2290 #define setobj(L,obj1,obj2) \
2291 { const TValue *o2=(obj2); TValue *o1=(obj1); \
2292 o1->value = o2->value; o1->tt=o2->tt; \
2293 @@ -185,8 +232,11 @@
2295 #define setttype(obj, tt) (ttype(obj) = (tt))
2298 -#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
2299 +#if LUA_TINT >= LUA_TSTRING
2300 +# define iscollectable(o) ((ttype(o) >= LUA_TSTRING) && (ttype(o) != LUA_TINT))
2302 +# define iscollectable(o) (ttype(o) >= LUA_TSTRING)
2307 @@ -370,12 +420,10 @@
2308 LUAI_FUNC int luaO_int2fb (unsigned int x);
2309 LUAI_FUNC int luaO_fb2int (int x);
2310 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
2311 -LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
2312 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
2314 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
2315 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
2320 Binary files ../lua-5.1.3/src/lobject.o and lua-5.1.3-patched/src/lobject.o differ
2321 Binary files ../lua-5.1.3/src/lopcodes.o and lua-5.1.3-patched/src/lopcodes.o differ
2322 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/loslib.c lua-5.1.3-patched/src/loslib.c
2323 --- ../lua-5.1.3/src/loslib.c 2008-01-18 18:38:18.000000000 +0200
2324 +++ lua-5.1.3-patched/src/loslib.c 2008-03-19 11:02:22.000000000 +0200
2325 @@ -186,15 +186,30 @@
2327 if (t == (time_t)(-1))
2330 - lua_pushnumber(L, (lua_Number)t);
2332 + /* On float systems the pushed value must be an integer, NOT a number.
2333 + * Otherwise, accuracy is lost in the time_t->float conversion.
2336 + lua_pushinteger(L, (lua_Integer) t);
2338 + lua_pushnumber(L, (lua_Number) t);
2345 static int os_difftime (lua_State *L) {
2347 + lua_Integer i= (lua_Integer)
2348 + difftime( (time_t)(luaL_checkinteger(L, 1)),
2349 + (time_t)(luaL_optinteger(L, 2, 0)));
2350 + lua_pushinteger(L, i);
2352 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
2353 (time_t)(luaL_optnumber(L, 2, 0))));
2358 Binary files ../lua-5.1.3/src/loslib.o and lua-5.1.3-patched/src/loslib.o differ
2359 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lparser.c lua-5.1.3-patched/src/lparser.c
2360 --- ../lua-5.1.3/src/lparser.c 2007-12-28 17:32:23.000000000 +0200
2361 +++ lua-5.1.3-patched/src/lparser.c 2008-03-06 21:58:41.000000000 +0200
2364 #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m)
2368 ** nodes for block list (list of active blocks)
2371 const char *msg = (fs->f->linedefined == 0) ?
2372 luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
2373 luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
2374 - fs->f->linedefined, limit, what);
2375 + (fs->f->linedefined), limit, what);
2376 luaX_lexerror(fs->ls, msg, 0);
2379 @@ -733,6 +732,18 @@
2380 v->u.nval = ls->t.seminfo.r;
2384 + init_exp(v, VKINT, 0);
2385 + v->u.ival = ls->t.seminfo.i;
2388 +#ifdef LNUM_COMPLEX
2389 + case TK_NUMBER2: {
2390 + init_exp(v, VKNUM2, 0);
2391 + v->u.nval = ls->t.seminfo.r;
2396 codestring(ls, v, ls->t.seminfo.ts);
2398 @@ -1079,7 +1090,7 @@
2399 if (testnext(ls, ','))
2400 exp1(ls); /* optional step */
2401 else { /* default step = 1 */
2402 - luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
2403 + luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_integerK(fs, 1));
2404 luaK_reserveregs(fs, 1);
2406 forbody(ls, base, line, 1, 1);
2407 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lparser.h lua-5.1.3-patched/src/lparser.h
2408 --- ../lua-5.1.3/src/lparser.h 2007-12-27 15:02:25.000000000 +0200
2409 +++ lua-5.1.3-patched/src/lparser.h 2008-03-06 22:01:11.000000000 +0200
2411 VRELOCABLE, /* info = instruction pc */
2412 VNONRELOC, /* info = result register */
2413 VCALL, /* info = instruction pc */
2414 - VVARARG /* info = instruction pc */
2415 + VVARARG, /* info = instruction pc */
2416 + VKINT /* ival = integer value */
2417 +#ifdef LNUM_COMPLEX
2418 + ,VKNUM2 /* nval = imaginary value */
2422 typedef struct expdesc {
2425 struct { int info, aux; } s;
2429 int t; /* patch list of `exit when true' */
2430 int f; /* patch list of `exit when false' */
2431 Binary files ../lua-5.1.3/src/lparser.o and lua-5.1.3-patched/src/lparser.o differ
2432 Binary files ../lua-5.1.3/src/lstate.o and lua-5.1.3-patched/src/lstate.o differ
2433 Binary files ../lua-5.1.3/src/lstring.o and lua-5.1.3-patched/src/lstring.o differ
2434 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lstrlib.c lua-5.1.3-patched/src/lstrlib.c
2435 --- ../lua-5.1.3/src/lstrlib.c 2007-12-28 17:32:23.000000000 +0200
2436 +++ lua-5.1.3-patched/src/lstrlib.c 2008-03-12 23:15:22.000000000 +0200
2438 static int str_sub (lua_State *L) {
2440 const char *s = luaL_checklstring(L, 1, &l);
2441 - ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
2442 - ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
2443 + ptrdiff_t start = posrelat(luaL_checkint32(L, 2), l);
2444 + ptrdiff_t end = posrelat(luaL_optint32(L, 3, -1), l);
2445 if (start < 1) start = 1;
2446 if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
2449 static int str_byte (lua_State *L) {
2451 const char *s = luaL_checklstring(L, 1, &l);
2452 - ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
2453 - ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
2454 + ptrdiff_t posi = posrelat(luaL_optint32(L, 2, 1), l);
2455 + ptrdiff_t pose = posrelat(luaL_optint32(L, 3, posi), l);
2457 if (posi <= 0) posi = 1;
2458 if ((size_t)pose > l) pose = l;
2461 const char *s = luaL_checklstring(L, 1, &l1);
2462 const char *p = luaL_checklstring(L, 2, &l2);
2463 - ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
2464 + ptrdiff_t init = posrelat(luaL_optint32(L, 3, 1), l1) - 1;
2465 if (init < 0) init = 0;
2466 else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
2467 if (find && (lua_toboolean(L, 4) || /* explicit request? */
2469 ** maximum size of each format specification (such as '%-099.99d')
2470 ** (+10 accounts for %99.99x plus margin of error)
2472 -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
2473 +#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTEGER_FMT)-2 + 10)
2476 static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
2478 static void addintlen (char *form) {
2479 size_t l = strlen(form);
2480 char spec = form[l - 1];
2481 - strcpy(form + l - 1, LUA_INTFRMLEN);
2482 - form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
2483 - form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
2484 + const char *tmp= LUA_INTEGER_FMT; /* "%lld" or "%ld" */
2485 + strcpy(form + l - 1, tmp+1);
2486 + form[l + sizeof(LUA_INTEGER_FMT)-4] = spec;
2490 @@ -776,12 +776,12 @@
2492 case 'd': case 'i': {
2494 - sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
2495 + sprintf(buff, form, luaL_checkinteger(L, arg));
2498 case 'o': case 'u': case 'x': case 'X': {
2500 - sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
2501 + sprintf(buff, form, (unsigned LUA_INTEGER)luaL_checkinteger(L, arg));
2504 case 'e': case 'E': case 'f':
2505 Binary files ../lua-5.1.3/src/lstrlib.o and lua-5.1.3-patched/src/lstrlib.o differ
2506 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/ltable.c lua-5.1.3-patched/src/ltable.c
2507 --- ../lua-5.1.3/src/ltable.c 2007-12-28 17:32:23.000000000 +0200
2508 +++ lua-5.1.3-patched/src/ltable.c 2008-03-26 13:04:20.000000000 +0200
2510 #include "lobject.h"
2519 #define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
2520 #define hashboolean(t,p) hashpow2(t, p)
2522 +#define hashint(t,i) hashpow2(t,i)
2525 ** for some types, it is better to avoid modulus by power of 2, as
2526 ** they tend to have many 2 factors.
2528 #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
2531 #define hashpointer(t,p) hashmod(t, IntPoint(p))
2535 -** number of ints inside a lua_Number
2537 -#define numints cast_int(sizeof(lua_Number)/sizeof(int))
2541 #define dummynode (&dummynode_)
2543 static const Node dummynode_ = {
2547 ** hash for lua_Numbers
2549 +** for non-complex modes, never called with 'lua_Integer' value range (s.a. 0)
2551 static Node *hashnum (const Table *t, lua_Number n) {
2552 - unsigned int a[numints];
2554 - if (luai_numeq(n, 0)) /* avoid problems with -0 */
2555 - return gnode(t, 0);
2556 - memcpy(a, &n, sizeof(a));
2557 - for (i = 1; i < numints; i++) a[0] += a[i];
2558 - return hashmod(t, a[0]);
2559 + const unsigned int *p= cast(const unsigned int *,&n);
2560 + unsigned int sum= *p;
2561 + unsigned int m= sizeof(lua_Number)/sizeof(int);
2563 + /* OS X Intel has 'm'==4 and gives "Bus error" if the last integer of
2564 + * 'n' is read; the actual size of long double is only 80 bits = 10 bytes.
2565 + * Linux x86 has 'm'==3, and does not require reduction.
2567 +#if defined(LNUM_LDOUBLE) && defined(__i386__)
2570 + for (i = 1; i < m; i++) sum += p[i];
2571 + return hashmod(t, sum);
2577 ** returns the `main' position of an element in a table (that is, the index
2578 ** of its hash value)
2580 +** Floating point numbers with integer value give the hash position of the
2581 +** integer (so they use the same table position).
2583 static Node *mainposition (const Table *t, const TValue *key) {
2585 switch (ttype(key)) {
2587 - return hashnum(t, nvalue(key));
2588 + if (tt_integer_valued(key,&i))
2589 + return hashint(t, i);
2590 +#ifdef LNUM_COMPLEX
2591 + if (nvalue_img_fast(key)!=0 && luai_numeq(nvalue_fast(key),0))
2592 + return gnode(t, 0); /* 0 and -0 to give same hash */
2594 + return hashnum(t, nvalue_fast(key));
2596 + return hashint(t, ivalue(key));
2598 return hashstr(t, rawtsvalue(key));
2600 @@ -116,16 +126,20 @@
2602 ** returns the index for `key' if `key' is an appropriate key to live in
2603 ** the array part of the table, -1 otherwise.
2605 +** Anything <=0 is taken as not being in the array part.
2607 -static int arrayindex (const TValue *key) {
2608 - if (ttisnumber(key)) {
2609 - lua_Number n = nvalue(key);
2611 - lua_number2int(k, n);
2612 - if (luai_numeq(cast_num(k), n))
2614 +static int arrayindex (const TValue *key, int max) {
2616 + switch( ttype(key) ) {
2618 + k= ivalue(key); break;
2620 + if (tt_integer_valued(key,&k)) break;
2622 + return -1; /* not to be used as array index */
2624 - return -1; /* `key' did not match some condition */
2625 + return ((k>0) && (k <= max)) ? cast_int(k) : -1;
2630 static int findindex (lua_State *L, Table *t, StkId key) {
2632 if (ttisnil(key)) return -1; /* first iteration */
2633 - i = arrayindex(key);
2634 - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
2635 + i = arrayindex(key, t->sizearray);
2636 + if (i>0) /* inside array part? */
2637 return i-1; /* yes; that's the index (corrected to C) */
2639 Node *n = mainposition(t, key);
2641 int i = findindex(L, t, key); /* find original element */
2642 for (i++; i < t->sizearray; i++) { /* try first array part */
2643 if (!ttisnil(&t->array[i])) { /* a non-nil value? */
2644 - setnvalue(key, cast_num(i+1));
2645 + setivalue(key, i+1);
2646 setobj2s(L, key+1, &t->array[i]);
2652 static int countint (const TValue *key, int *nums) {
2653 - int k = arrayindex(key);
2654 - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
2655 + int k = arrayindex(key,MAXASIZE);
2656 + if (k>0) { /* appropriate array index? */
2657 nums[ceillog2(k)]++; /* count as such */
2661 /* re-insert elements from vanishing slice */
2662 for (i=nasize; i<oldasize; i++) {
2663 if (!ttisnil(&t->array[i]))
2664 - setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
2665 + setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]);
2668 luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
2670 othern = mainposition(t, key2tval(mp));
2671 if (othern != mp) { /* is colliding node out of its main position? */
2672 /* yes; move colliding node into free position */
2673 - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
2674 + while (gnext(othern) != mp) {
2675 + othern = gnext(othern); /* find previous */
2677 gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
2678 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
2679 gnext(mp) = NULL; /* now `mp' is free */
2680 @@ -432,17 +448,18 @@
2682 ** search function for integers
2684 -const TValue *luaH_getnum (Table *t, int key) {
2685 +const TValue *luaH_getint (Table *t, lua_Integer key) {
2686 /* (1 <= key && key <= t->sizearray) */
2687 if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
2688 return &t->array[key-1];
2690 - lua_Number nk = cast_num(key);
2691 - Node *n = hashnum(t, nk);
2692 + Node *n = hashint(t, key);
2693 do { /* check whether `key' is somewhere in the chain */
2694 - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
2695 + if (ttisint(gkey(n)) && (ivalue(gkey(n)) == key)) {
2696 return gval(n); /* that's it */
2697 - else n = gnext(n);
2702 return luaO_nilobject;
2704 @@ -470,14 +487,12 @@
2705 switch (ttype(key)) {
2706 case LUA_TNIL: return luaO_nilobject;
2707 case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
2708 + case LUA_TINT: return luaH_getint(t, ivalue(key));
2711 - lua_Number n = nvalue(key);
2712 - lua_number2int(k, n);
2713 - if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
2714 - return luaH_getnum(t, k); /* use specialized version */
2715 - /* else go through */
2718 + if (tt_integer_valued(key,&i))
2719 + return luaH_getint(t,i);
2720 + } /* pass through */
2722 Node *n = mainposition(t, key);
2723 do { /* check whether `key' is somewhere in the chain */
2724 @@ -498,20 +513,25 @@
2725 return cast(TValue *, p);
2727 if (ttisnil(key)) luaG_runerror(L, "table index is nil");
2728 - else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
2729 - luaG_runerror(L, "table index is NaN");
2730 + else if (ttype(key)==LUA_TNUMBER) {
2732 + if (luai_numisnan(nvalue_fast(key)))
2733 + luaG_runerror(L, "table index is NaN");
2734 + if (tt_integer_valued(key,&k))
2735 + return luaH_setint(L, t, k);
2737 return newkey(L, t, key);
2742 -TValue *luaH_setnum (lua_State *L, Table *t, int key) {
2743 - const TValue *p = luaH_getnum(t, key);
2744 +TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key) {
2745 + const TValue *p = luaH_getint(t, key);
2746 if (p != luaO_nilobject)
2747 return cast(TValue *, p);
2750 - setnvalue(&k, cast_num(key));
2751 + setivalue(&k, key);
2752 return newkey(L, t, &k);
2755 @@ -533,20 +553,21 @@
2756 unsigned int i = j; /* i is zero or a present index */
2758 /* find `i' and `j' such that i is present and j is not */
2759 - while (!ttisnil(luaH_getnum(t, j))) {
2760 + while (!ttisnil(luaH_getint(t, j))) {
2763 if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
2764 /* table was built with bad purposes: resort to linear search */
2766 - while (!ttisnil(luaH_getnum(t, i))) i++;
2768 + for( i = 1; i<MAX_INT+1; i++ ) {
2769 + if (ttisnil(luaH_getint(t, i))) break;
2771 + return i - 1; /* up to MAX_INT */
2774 /* now do a binary search between them */
2776 unsigned int m = (i+j)/2;
2777 - if (ttisnil(luaH_getnum(t, m))) j = m;
2778 + if (ttisnil(luaH_getint(t, m))) j = m;
2782 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/ltable.h lua-5.1.3-patched/src/ltable.h
2783 --- ../lua-5.1.3/src/ltable.h 2007-12-27 15:02:25.000000000 +0200
2784 +++ lua-5.1.3-patched/src/ltable.h 2008-03-16 20:49:18.000000000 +0200
2786 #define key2tval(n) (&(n)->i_key.tvk)
2789 -LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
2790 -LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
2791 +LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
2792 +LUAI_FUNC TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key);
2793 LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
2794 LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
2795 LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
2796 Binary files ../lua-5.1.3/src/ltable.o and lua-5.1.3-patched/src/ltable.o differ
2797 Binary files ../lua-5.1.3/src/ltablib.o and lua-5.1.3-patched/src/ltablib.o differ
2798 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/ltm.c lua-5.1.3-patched/src/ltm.c
2799 --- ../lua-5.1.3/src/ltm.c 2007-12-27 15:02:25.000000000 +0200
2800 +++ lua-5.1.3-patched/src/ltm.c 2008-03-06 22:47:33.000000000 +0200
2806 const char *const luaT_typenames[] = {
2807 "nil", "boolean", "userdata", "number",
2808 "string", "table", "function", "userdata", "thread",
2811 mt = uvalue(o)->metatable;
2814 + mt = G(L)->mt[LUA_TNUMBER];
2817 mt = G(L)->mt[ttype(o)];
2819 Binary files ../lua-5.1.3/src/ltm.o and lua-5.1.3-patched/src/ltm.o differ
2820 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lua.c lua-5.1.3-patched/src/lua.c
2821 --- ../lua-5.1.3/src/lua.c 2007-12-28 17:32:23.000000000 +0200
2822 +++ lua-5.1.3-patched/src/lua.c 2008-03-26 11:32:25.000000000 +0200
2825 #include "lauxlib.h"
2828 +#include "llimits.h"
2831 static lua_State *globalL = NULL;
2832 @@ -382,6 +382,15 @@
2833 l_message(argv[0], "cannot create state: not enough memory");
2834 return EXIT_FAILURE;
2836 + /* Checking 'sizeof(lua_Integer)' cannot be made in preprocessor on all compilers.
2839 + lua_assert( sizeof(lua_Integer) == 2 );
2840 +#elif defined(LNUM_INT32)
2841 + lua_assert( sizeof(lua_Integer) == 4 );
2842 +#elif defined(LNUM_INT64)
2843 + lua_assert( sizeof(lua_Integer) == 8 );
2847 status = lua_cpcall(L, &pmain, &s);
2848 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lua.h lua-5.1.3-patched/src/lua.h
2849 --- ../lua-5.1.3/src/lua.h 2008-01-03 17:41:15.000000000 +0200
2850 +++ lua-5.1.3-patched/src/lua.h 2008-03-19 11:19:34.000000000 +0200
2852 #define LUA_VERSION "Lua 5.1"
2853 #define LUA_RELEASE "Lua 5.1.3"
2854 #define LUA_VERSION_NUM 501
2855 -#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio"
2856 +#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" " (" LUA_LNUM ")"
2857 #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
2862 #define LUA_TNONE (-1)
2864 +/* LUA_TINT is an internal type, not visible to applications. There are three
2865 + * potential values where it can be tweaked to (code autoadjusts to these):
2867 + * -2: not 'usual' type value; good since 'LUA_TINT' is not part of the API
2868 + * LUA_TNUMBER+1: shifts other type values upwards, breaking binary compatibility
2869 + * not acceptable for 5.1, maybe 5.2 onwards?
2870 + * 9: greater than existing (5.1) type values.
2872 +#define LUA_TINT (-2)
2875 #define LUA_TBOOLEAN 1
2876 #define LUA_TLIGHTUSERDATA 2
2878 LUA_API int (lua_type) (lua_State *L, int idx);
2879 LUA_API const char *(lua_typename) (lua_State *L, int tp);
2881 +LUA_API int (lua_isinteger) (lua_State *L, int idx);
2883 LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
2884 LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
2885 LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
2886 @@ -244,6 +256,19 @@
2887 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
2891 +* It is unnecessary to break Lua C API 'lua_tonumber()' compatibility, just
2892 +* because the Lua number type is complex. Most C modules would use scalars
2893 +* only. We'll introduce new 'lua_tocomplex' and 'lua_pushcomplex' for when
2894 +* the module really wants to use them.
2896 +#ifdef LNUM_COMPLEX
2897 + #include <complex.h>
2898 + typedef LUA_NUMBER complex lua_Complex;
2899 + LUA_API lua_Complex (lua_tocomplex) (lua_State *L, int idx);
2900 + LUA_API void (lua_pushcomplex) (lua_State *L, lua_Complex v);
2905 ** ===============================================================
2906 @@ -268,7 +293,12 @@
2907 #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
2908 #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
2909 #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
2910 -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
2913 +# define lua_isnoneornil(L, n) ( (lua_type(L,(n)) <= 0) && (lua_type(L,(n)) != LUA_TINT) )
2915 +# define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
2918 #define lua_pushliteral(L, s) \
2919 lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
2925 Binary files ../lua-5.1.3/src/lua.o and lua-5.1.3-patched/src/lua.o differ
2926 Binary files ../lua-5.1.3/src/luac and lua-5.1.3-patched/src/luac differ
2927 Binary files ../lua-5.1.3/src/luac.o and lua-5.1.3-patched/src/luac.o differ
2928 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/luaconf.h lua-5.1.3-patched/src/luaconf.h
2929 --- ../lua-5.1.3/src/luaconf.h 2008-01-18 19:07:48.000000000 +0200
2930 +++ lua-5.1.3-patched/src/luaconf.h 2008-03-24 20:28:57.000000000 +0200
2937 +# include <assert.h>
2941 ** ==================================================================
2942 @@ -136,14 +138,38 @@
2946 -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
2947 -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
2948 -** machines, ptrdiff_t gives a good choice between int or long.)
2949 +@@ LUAI_BITSINT defines the number of bits in an int.
2950 +** CHANGE here if Lua cannot automatically detect the number of bits of
2951 +** your machine. Probably you do not need to change this.
2953 -#define LUA_INTEGER ptrdiff_t
2954 +/* avoid overflows in comparison */
2955 +#if INT_MAX-20 < 32760
2956 +#define LUAI_BITSINT 16
2957 +#elif INT_MAX > 2147483640L
2958 +/* int has at least 32 bits */
2959 +#define LUAI_BITSINT 32
2961 +#error "you must define LUA_BITSINT with number of bits in an integer"
2966 +@@ LNUM_DOUBLE |Â LNUM_FLOAT |Â LNUM_LDOUBLE: Generic Lua number mode
2967 +@@ LNUM_INT32 | LNUM_INT64: Integer type
2968 +@@ LNUM_COMPLEX: Define for using 'a+bi' numbers
2970 +@@ You can combine LNUM_xxx but only one of each group. I.e. '-DLNUM_FLOAT
2971 +@@ -DLNUM_INT32 -DLNUM_COMPLEX' gives float range complex numbers, with
2972 +@@ 32-bit scalar integer range optimized.
2974 +** These are kept in a separate configuration file mainly for ease of patching
2975 +** (can be changed if integerated to Lua proper).
2977 +/*#define LNUM_DOUBLE*/
2978 +/*#define LNUM_INT32*/
2979 +#include "lnum_config.h"
2982 @@ LUA_API is a mark for all core API functions.
2983 @@ LUALIB_API is a mark for all standard library functions.
2984 ** CHANGE them if you need to define those functions in some special way.
2985 @@ -383,22 +409,6 @@
2989 -@@ LUAI_BITSINT defines the number of bits in an int.
2990 -** CHANGE here if Lua cannot automatically detect the number of bits of
2991 -** your machine. Probably you do not need to change this.
2993 -/* avoid overflows in comparison */
2994 -#if INT_MAX-20 < 32760
2995 -#define LUAI_BITSINT 16
2996 -#elif INT_MAX > 2147483640L
2997 -/* int has at least 32 bits */
2998 -#define LUAI_BITSINT 32
3000 -#error "you must define LUA_BITSINT with number of bits in an integer"
3005 @@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
3006 @@ LUAI_INT32 is an signed integer with at least 32 bits.
3007 @@ LUAI_UMEM is an unsigned integer big enough to count the total
3008 @@ -425,6 +435,15 @@
3009 #define LUAI_MEM long
3013 +@@ LUAI_BOOL carries 0 and nonzero (normally 1). It may be defined as 'char'
3014 +** (to save memory), 'int' (for speed), 'bool' (for C++) or '_Bool' (C99)
3017 +# define LUAI_BOOL bool
3019 +# define LUAI_BOOL int
3023 @@ LUAI_MAXCALLS limits the number of nested calls.
3024 @@ -490,101 +509,6 @@
3025 /* }================================================================== */
3031 -** {==================================================================
3032 -@@ LUA_NUMBER is the type of numbers in Lua.
3033 -** CHANGE the following definitions only if you want to build Lua
3034 -** with a number type different from double. You may also need to
3035 -** change lua_number2int & lua_number2integer.
3036 -** ===================================================================
3039 -#define LUA_NUMBER_DOUBLE
3040 -#define LUA_NUMBER double
3043 -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
3046 -#define LUAI_UACNUMBER double
3050 -@@ LUA_NUMBER_SCAN is the format for reading numbers.
3051 -@@ LUA_NUMBER_FMT is the format for writing numbers.
3052 -@@ lua_number2str converts a number to a string.
3053 -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
3054 -@@ lua_str2number converts a string to a number.
3056 -#define LUA_NUMBER_SCAN "%lf"
3057 -#define LUA_NUMBER_FMT "%.14g"
3058 -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
3059 -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
3060 -#define lua_str2number(s,p) strtod((s), (p))
3064 -@@ The luai_num* macros define the primitive operations over numbers.
3066 -#if defined(LUA_CORE)
3068 -#define luai_numadd(a,b) ((a)+(b))
3069 -#define luai_numsub(a,b) ((a)-(b))
3070 -#define luai_nummul(a,b) ((a)*(b))
3071 -#define luai_numdiv(a,b) ((a)/(b))
3072 -#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
3073 -#define luai_numpow(a,b) (pow(a,b))
3074 -#define luai_numunm(a) (-(a))
3075 -#define luai_numeq(a,b) ((a)==(b))
3076 -#define luai_numlt(a,b) ((a)<(b))
3077 -#define luai_numle(a,b) ((a)<=(b))
3078 -#define luai_numisnan(a) (!luai_numeq((a), (a)))
3083 -@@ lua_number2int is a macro to convert lua_Number to int.
3084 -@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
3085 -** CHANGE them if you know a faster way to convert a lua_Number to
3086 -** int (with any rounding method and without throwing errors) in your
3087 -** system. In Pentium machines, a naive typecast from double to int
3088 -** in C is extremely slow, so any alternative is worth trying.
3091 -/* On a Pentium, resort to a trick */
3092 -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
3093 - (defined(__i386) || defined (_M_IX86) || defined(__i386__))
3095 -/* On a Microsoft compiler, use assembler */
3096 -#if defined(_MSC_VER)
3098 -#define lua_number2int(i,d) __asm fld d __asm fistp i
3099 -#define lua_number2integer(i,n) lua_number2int(i, n)
3101 -/* the next trick should work on any Pentium, but sometimes clashes
3102 - with a DirectX idiosyncrasy */
3105 -union luai_Cast { double l_d; long l_l; };
3106 -#define lua_number2int(i,d) \
3107 - { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
3108 -#define lua_number2integer(i,n) lua_number2int(i, n)
3113 -/* this option always works, but may be slow */
3115 -#define lua_number2int(i,d) ((i)=(int)(d))
3116 -#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
3120 -/* }================================================================== */
3124 @@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
3125 ** CHANGE it if your system requires alignments larger than double. (For
3126 @@ -728,28 +652,6 @@
3127 #define luai_userstateyield(L,n) ((void)L)
3131 -@@ LUA_INTFRMLEN is the length modifier for integer conversions
3132 -@* in 'string.format'.
3133 -@@ LUA_INTFRM_T is the integer type correspoding to the previous length
3135 -** CHANGE them if your system supports long long or does not support long.
3138 -#if defined(LUA_USELONGLONG)
3140 -#define LUA_INTFRMLEN "ll"
3141 -#define LUA_INTFRM_T long long
3145 -#define LUA_INTFRMLEN "l"
3146 -#define LUA_INTFRM_T long
3152 /* =================================================================== */
3155 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lundump.c lua-5.1.3-patched/src/lundump.c
3156 --- ../lua-5.1.3/src/lundump.c 2008-01-18 18:39:11.000000000 +0200
3157 +++ lua-5.1.3-patched/src/lundump.c 2008-03-26 04:33:51.000000000 +0200
3162 +static lua_Integer LoadInteger(LoadState* S)
3169 static TString* LoadString(LoadState* S)
3174 setnvalue(o,LoadNumber(S));
3176 + case LUA_TINT: /* Integer type saved in bytecode (see lcode.c) */
3177 + setivalue(o,LoadInteger(S));
3180 setsvalue2n(S->L,o,LoadString(S));
3182 @@ -221,5 +231,22 @@
3183 *h++=(char)sizeof(size_t);
3184 *h++=(char)sizeof(Instruction);
3185 *h++=(char)sizeof(lua_Number);
3186 - *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
3189 + * Last byte of header (0/1 in unpatched Lua 5.1.3):
3191 + * 0: lua_Number is float or double, lua_Integer not used. (nonpatched only)
3192 + * 1: lua_Number is integer (nonpatched only)
3194 + * +2: LNUM_INT16: sizeof(lua_Integer)
3195 + * +4: LNUM_INT32: sizeof(lua_Integer)
3196 + * +8: LNUM_INT64: sizeof(lua_Integer)
3198 + * +0x80: LNUM_COMPLEX
3200 + *h++ = (char)(sizeof(lua_Integer)
3201 +#ifdef LNUM_COMPLEX
3206 Binary files ../lua-5.1.3/src/lundump.o and lua-5.1.3-patched/src/lundump.o differ
3207 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lvm.c lua-5.1.3-patched/src/lvm.c
3208 --- ../lua-5.1.3/src/lvm.c 2007-12-28 17:32:23.000000000 +0200
3209 +++ lua-5.1.3-patched/src/lvm.c 2008-03-14 11:13:17.000000000 +0200
3219 /* limit for table tag-method chains (to avoid loops) */
3220 #define MAXTAGLOOP 100
3223 -const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
3226 + * If 'obj' is a string, it is tried to be interpreted as a number.
3228 +const TValue *luaV_tonumber ( const TValue *obj, TValue *n) {
3232 if (ttisnumber(obj)) return obj;
3233 - if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
3234 - setnvalue(n, num);
3240 + if (ttisstring(obj)) {
3241 + switch( luaO_str2d( svalue(obj), &d, &i ) ) {
3243 + setivalue(n,i); return n;
3245 + setnvalue(n,d); return n;
3246 +#ifdef LNUM_COMPLEX
3247 + case TK_NUMBER2: /* "N.NNNi", != 0 */
3248 + setnvalue_complex_fast(n, d*I); return n;
3259 char s[LUAI_MAXNUMBER2STR];
3260 - lua_Number n = nvalue(obj);
3261 - lua_number2str(s, n);
3262 + luaO_num2buf(s,obj);
3263 setsvalue2s(L, obj, luaS_new(L, s));
3266 @@ -218,59 +230,127 @@
3270 +#ifdef LNUM_COMPLEX
3271 +void error_complex( lua_State *L, const TValue *l, const TValue *r )
3273 + char buf1[ LUAI_MAXNUMBER2STR ];
3274 + char buf2[ LUAI_MAXNUMBER2STR ];
3275 + luaO_num2buf( buf1, l );
3276 + luaO_num2buf( buf2, r );
3277 + luaG_runerror( L, "unable to compare: %s with %s", buf1, buf2 );
3283 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
3285 - if (ttype(l) != ttype(r))
3289 + if (!ttype_ext_same(l,r))
3290 return luaG_ordererror(L, l, r);
3291 - else if (ttisnumber(l))
3292 - return luai_numlt(nvalue(l), nvalue(r));
3293 - else if (ttisstring(l))
3294 - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
3295 - else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
3296 +#ifdef LNUM_COMPLEX
3297 + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
3298 + error_complex( L, l, r );
3300 + tl= ttype(l); tr= ttype(r);
3301 + if (tl==tr) { /* clear arithmetics */
3303 + case LUA_TINT: return ivalue(l) < ivalue(r);
3304 + case LUA_TNUMBER: return luai_numlt(nvalue_fast(l), nvalue_fast(r));
3305 + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
3307 + } else if (tl==LUA_TINT) { /* l:int, r:num */
3308 + /* Avoid accuracy losing casts: if 'r' is integer by value, do comparisons
3309 + * in integer realm. Only otherwise cast 'l' to FP (which might change its
3312 + if (tt_integer_valued(r,&tmp))
3313 + return ivalue(l) < tmp;
3315 + return luai_numlt( cast_num(ivalue(l)), nvalue_fast(r) );
3317 + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
3318 + if (tt_integer_valued(l,&tmp))
3319 + return tmp < ivalue(r);
3321 + return luai_numlt( nvalue_fast(l), cast_num(ivalue(r)) );
3323 + } else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
3326 return luaG_ordererror(L, l, r);
3330 static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
3332 - if (ttype(l) != ttype(r))
3336 + if (!ttype_ext_same(l,r))
3337 return luaG_ordererror(L, l, r);
3338 - else if (ttisnumber(l))
3339 - return luai_numle(nvalue(l), nvalue(r));
3340 - else if (ttisstring(l))
3341 - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
3342 - else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
3343 +#ifdef LNUM_COMPLEX
3344 + if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
3345 + error_complex( L, l, r );
3347 + tl= ttype(l); tr= ttype(r);
3348 + if (tl==tr) { /* clear arithmetics */
3350 + case LUA_TINT: return ivalue(l) <= ivalue(r);
3351 + case LUA_TNUMBER: return luai_numle(nvalue_fast(l), nvalue_fast(r));
3352 + case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
3355 + if (tl==LUA_TINT) { /* l:int, r:num */
3356 + if (tt_integer_valued(r,&tmp))
3357 + return ivalue(l) <= tmp;
3359 + return luai_numle( cast_num(ivalue(l)), nvalue_fast(r) );
3361 + } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
3362 + if (tt_integer_valued(l,&tmp))
3363 + return tmp <= ivalue(r);
3365 + return luai_numle( nvalue_fast(l), cast_num(ivalue(r)) );
3367 + } else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
3369 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
3372 return luaG_ordererror(L, l, r);
3376 -int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
3377 +/* Note: 'luaV_equalval()' and 'luaO_rawequalObj()' have largely overlapping
3378 + * implementation. LUA_TNIL..LUA_TLIGHTUSERDATA cases could be handled
3379 + * simply by the 'default' case here.
3381 +int luaV_equalval (lua_State *L, const TValue *l, const TValue *r) {
3383 - lua_assert(ttype(t1) == ttype(t2));
3384 - switch (ttype(t1)) {
3385 + lua_assert(ttype_ext_same(l,r));
3386 + switch (ttype(l)) {
3387 case LUA_TNIL: return 1;
3388 - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
3389 - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
3390 - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
3392 + case LUA_TNUMBER: return luaO_rawequalObj(l,r);
3393 + case LUA_TBOOLEAN: return bvalue(l) == bvalue(r); /* true must be 1 !! */
3394 + case LUA_TLIGHTUSERDATA: return pvalue(l) == pvalue(r);
3395 case LUA_TUSERDATA: {
3396 - if (uvalue(t1) == uvalue(t2)) return 1;
3397 - tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
3399 + if (uvalue(l) == uvalue(r)) return 1;
3400 + tm = get_compTM(L, uvalue(l)->metatable, uvalue(r)->metatable, TM_EQ);
3401 break; /* will try TM */
3404 - if (hvalue(t1) == hvalue(t2)) return 1;
3405 - tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
3406 + if (hvalue(l) == hvalue(r)) return 1;
3407 + tm = get_compTM(L, hvalue(l)->metatable, hvalue(r)->metatable, TM_EQ);
3408 break; /* will try TM */
3410 - default: return gcvalue(t1) == gcvalue(t2);
3411 + default: return gcvalue(l) == gcvalue(r);
3413 if (tm == NULL) return 0; /* no TM? */
3414 - callTMres(L, L->top, tm, t1, t2); /* call TM */
3415 + callTMres(L, L->top, tm, l, r); /* call TM */
3416 return !l_isfalse(L->top);
3419 @@ -310,30 +390,6 @@
3423 -static void Arith (lua_State *L, StkId ra, const TValue *rb,
3424 - const TValue *rc, TMS op) {
3425 - TValue tempb, tempc;
3426 - const TValue *b, *c;
3427 - if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
3428 - (c = luaV_tonumber(rc, &tempc)) != NULL) {
3429 - lua_Number nb = nvalue(b), nc = nvalue(c);
3431 - case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
3432 - case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
3433 - case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
3434 - case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
3435 - case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
3436 - case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
3437 - case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
3438 - default: lua_assert(0); break;
3441 - else if (!call_binTM(L, rb, rc, ra, op))
3442 - luaG_aritherror(L, rb, rc);
3448 ** some macros for common tasks in `luaV_execute'
3450 @@ -357,17 +413,154 @@
3451 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
3454 -#define arith_op(op,tm) { \
3455 - TValue *rb = RKB(i); \
3456 - TValue *rc = RKC(i); \
3457 - if (ttisnumber(rb) && ttisnumber(rc)) { \
3458 - lua_Number nb = nvalue(rb), nc = nvalue(rc); \
3459 - setnvalue(ra, op(nb, nc)); \
3462 - Protect(Arith(L, ra, rb, rc, tm)); \
3463 +/* Note: if called for unary operations, 'rc'=='rb'.
3465 +static void Arith (lua_State *L, StkId ra, const TValue *rb,
3466 + const TValue *rc, TMS op) {
3467 + TValue tempb, tempc;
3468 + const TValue *b, *c;
3471 + if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
3472 + (c = luaV_tonumber(rc, &tempc)) != NULL) {
3474 + /* Keep integer arithmetics in the integer realm, if possible.
3476 + if (ttisint(b) && ttisint(c)) {
3477 + lua_Integer ib = ivalue(b), ic = ivalue(c);
3478 + lua_Integer *ri = &ra->value.i;
3479 + ra->tt= LUA_TINT; /* part of 'setivalue(ra)' */
3481 + case TM_ADD: if (try_addint( ri, ib, ic)) return; break;
3482 + case TM_SUB: if (try_subint( ri, ib, ic)) return; break;
3483 + case TM_MUL: if (try_mulint( ri, ib, ic)) return; break;
3484 + case TM_DIV: if (try_divint( ri, ib, ic)) return; break;
3485 + case TM_MOD: if (try_modint( ri, ib, ic)) return; break;
3486 + case TM_POW: if (try_powint( ri, ib, ic)) return; break;
3487 + case TM_UNM: if (try_unmint( ri, ib)) return; break;
3488 + default: lua_assert(0);
3491 + /* Fallback to floating point, when leaving range. */
3493 +#ifdef LNUM_COMPLEX
3494 + if ((nvalue_img(b)!=0) || (nvalue_img(c)!=0)) {
3497 + r= -nvalue_complex_fast(b); /* never an integer (or scalar) */
3498 + setnvalue_complex_fast( ra, r );
3500 + lua_Complex bb= nvalue_complex(b), cc= nvalue_complex(c);
3502 + case TM_ADD: r= bb + cc; break;
3503 + case TM_SUB: r= bb - cc; break;
3504 + case TM_MUL: r= bb * cc; break;
3505 + case TM_DIV: r= bb / cc; break;
3507 + luaG_runerror(L, "attempt to use %% on complex numbers"); /* no return */
3508 + case TM_POW: r= luai_vectpow( bb, cc ); break;
3509 + default: lua_assert(0); r=0;
3511 + setnvalue_complex( ra, r );
3516 + nb = nvalue(b); nc = nvalue(c);
3518 + case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); return;
3519 + case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); return;
3520 + case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); return;
3521 + case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); return;
3522 + case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); return;
3523 + case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); return;
3524 + case TM_UNM: setnvalue(ra, luai_numunm(nb)); return;
3525 + default: lua_assert(0);
3529 + /* Either operand not a number */
3530 + if (!call_binTM(L, rb, rc, ra, op))
3531 + luaG_aritherror(L, rb, rc);
3534 +/* Helper macro to sort arithmetic operations into four categories:
3535 + * TK_INT: integer - integer operands
3536 + * TK_NUMBER: number - number (non complex, either may be integer)
3537 + * TK_NUMBER2: complex numbers (at least the other)
3538 + * 0: non-numeric (at least the other)
3540 +#ifdef LNUM_COMPLEX
3541 +static inline int arith_mode( const TValue *rb, const TValue *rc ) {
3542 + if (ttisint(rb) && ttisint(rc)) return TK_INT;
3543 + if (ttiscomplex(rb) || ttiscomplex(rc)) return TK_NUMBER2;
3544 + if (ttisnumber(rb) && ttisnumber(rc)) return TK_NUMBER;
3548 +# define arith_mode(rb,rc) \
3549 + ( (ttisint(rb) && ttisint(rc)) ? TK_INT : \
3550 + (ttisnumber(rb) && ttisnumber(rc)) ? TK_NUMBER : 0 )
3553 +/* arith_op macro for two operators:
3554 + * automatically chooses, which function (number, integer, complex) to use
3556 +#define ARITH_OP2_START( op_num, op_int ) \
3558 + switch( arith_mode(rb,rc) ) { \
3560 + if (op_int ( &(ra)->value.i, ivalue(rb), ivalue(rc) )) \
3561 + { ra->tt= LUA_TINT; break; } /* else flow through */ \
3563 + setnvalue(ra, op_num ( nvalue(rb), nvalue(rc) )); break;
3565 +#define ARITH_OP2_END \
3567 + failed= 1; break; \
3568 + } if (!failed) continue;
3570 +#define arith_op_continue_scalar( op_num, op_int ) \
3571 + ARITH_OP2_START( op_num, op_int ) \
3574 +#ifdef LNUM_COMPLEX
3575 +# define arith_op_continue( op_num, op_int, op_complex ) \
3576 + ARITH_OP2_START( op_num, op_int ) \
3577 + case TK_NUMBER2: \
3578 + setnvalue_complex( ra, op_complex ( nvalue_complex(rb), nvalue_complex(rc) ) ); break; \
3581 +# define arith_op_continue(op_num,op_int,_) arith_op_continue_scalar(op_num,op_int)
3584 +/* arith_op macro for one operator:
3586 +#define ARITH_OP1_START( op_num, op_int ) \
3588 + switch( arith_mode(rb,rb) ) { \
3590 + if (op_int ( &(ra)->value.i, ivalue(rb) )) \
3591 + { ra->tt= LUA_TINT; break; } /* else flow through */ \
3593 + setnvalue(ra, op_num (nvalue(rb))); break; \
3595 +#define ARITH_OP1_END \
3597 + failed= 1; break; \
3598 + } if (!failed) continue;
3600 +#ifdef LNUM_COMPLEX
3601 +# define arith_op1_continue( op_num, op_int, op_complex ) \
3602 + ARITH_OP1_START( op_num, op_int ) \
3603 + case TK_NUMBER2: \
3604 + setnvalue_complex( ra, op_complex ( nvalue_complex_fast(rb) )); break; \
3607 +# define arith_op1_continue( op_num, op_int, _ ) \
3608 + ARITH_OP1_START( op_num, op_int ) \
3613 void luaV_execute (lua_State *L, int nexeccalls) {
3614 @@ -468,38 +661,45 @@
3618 - arith_op(luai_numadd, TM_ADD);
3619 + TValue *rb = RKB(i), *rc= RKC(i);
3620 + arith_op_continue( luai_numadd, try_addint, luai_vectadd );
3621 + Protect(Arith(L, ra, rb, rc, TM_ADD)); \
3625 - arith_op(luai_numsub, TM_SUB);
3626 + TValue *rb = RKB(i), *rc= RKC(i);
3627 + arith_op_continue( luai_numsub, try_subint, luai_vectsub );
3628 + Protect(Arith(L, ra, rb, rc, TM_SUB));
3632 - arith_op(luai_nummul, TM_MUL);
3633 + TValue *rb = RKB(i), *rc= RKC(i);
3634 + arith_op_continue(luai_nummul, try_mulint, luai_vectmul);
3635 + Protect(Arith(L, ra, rb, rc, TM_MUL));
3639 - arith_op(luai_numdiv, TM_DIV);
3640 + TValue *rb = RKB(i), *rc= RKC(i);
3641 + arith_op_continue(luai_numdiv, try_divint, luai_vectdiv);
3642 + Protect(Arith(L, ra, rb, rc, TM_DIV));
3646 - arith_op(luai_nummod, TM_MOD);
3647 + TValue *rb = RKB(i), *rc= RKC(i);
3648 + arith_op_continue_scalar(luai_nummod, try_modint); /* scalars only */
3649 + Protect(Arith(L, ra, rb, rc, TM_MOD));
3653 - arith_op(luai_numpow, TM_POW);
3654 + TValue *rb = RKB(i), *rc= RKC(i);
3655 + arith_op_continue(luai_numpow, try_powint, luai_vectpow);
3656 + Protect(Arith(L, ra, rb, rc, TM_POW));
3661 - if (ttisnumber(rb)) {
3662 - lua_Number nb = nvalue(rb);
3663 - setnvalue(ra, luai_numunm(nb));
3666 - Protect(Arith(L, ra, rb, rb, TM_UNM));
3668 + arith_op1_continue(luai_numunm, try_unmint, luai_vectunm);
3669 + Protect(Arith(L, ra, rb, rb, TM_UNM));
3673 @@ -511,11 +711,11 @@
3674 const TValue *rb = RB(i);
3675 switch (ttype(rb)) {
3677 - setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
3678 + setivalue(ra, luaH_getn(hvalue(rb)));
3682 - setnvalue(ra, cast_num(tsvalue(rb)->len));
3683 + setivalue(ra, tsvalue(rb)->len);
3686 default: { /* try metamethod */
3687 @@ -648,14 +848,30 @@
3691 - lua_Number step = nvalue(ra+2);
3692 - lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
3693 - lua_Number limit = nvalue(ra+1);
3694 - if (luai_numlt(0, step) ? luai_numle(idx, limit)
3695 - : luai_numle(limit, idx)) {
3696 - dojump(L, pc, GETARG_sBx(i)); /* jump back */
3697 - setnvalue(ra, idx); /* update internal index... */
3698 - setnvalue(ra+3, idx); /* ...and external index */
3699 + /* If start,step and limit are all integers, we don't need to check
3700 + * against overflow in the looping.
3702 + if (ttisint(ra) && ttisint(ra+1) && ttisint(ra+2)) {
3703 + lua_Integer step = ivalue(ra+2);
3704 + lua_Integer idx = ivalue(ra) + step; /* increment index */
3705 + lua_Integer limit = ivalue(ra+1);
3706 + if (step > 0 ? (idx <= limit) : (limit <= idx)) {
3707 + dojump(L, pc, GETARG_sBx(i)); /* jump back */
3708 + setivalue(ra, idx); /* update internal index... */
3709 + setivalue(ra+3, idx); /* ...and external index */
3712 + /* non-integer looping (don't use 'nvalue_fast', some may be integer!)
3714 + lua_Number step = nvalue(ra+2);
3715 + lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
3716 + lua_Number limit = nvalue(ra+1);
3717 + if (luai_numlt(0, step) ? luai_numle(idx, limit)
3718 + : luai_numle(limit, idx)) {
3719 + dojump(L, pc, GETARG_sBx(i)); /* jump back */
3720 + setnvalue(ra, idx); /* update internal index... */
3721 + setnvalue(ra+3, idx); /* ...and external index */
3726 @@ -664,13 +880,21 @@
3727 const TValue *plimit = ra+1;
3728 const TValue *pstep = ra+2;
3729 L->savedpc = pc; /* next steps may throw errors */
3730 + /* Using same location for tonumber's both arguments, effectively does
3731 + * in-place modification (string->number). */
3732 if (!tonumber(init, ra))
3733 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
3734 else if (!tonumber(plimit, ra+1))
3735 luaG_runerror(L, LUA_QL("for") " limit must be a number");
3736 else if (!tonumber(pstep, ra+2))
3737 luaG_runerror(L, LUA_QL("for") " step must be a number");
3738 - setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
3739 + /* Step back one value (keep within integers if we can)
3741 + if (!( ttisint(ra) && ttisint(pstep) &&
3742 + try_subint( &ra->value.i, ivalue(ra), ivalue(pstep) ) )) {
3743 + /* don't use 'nvalue_fast()', values may be integer */
3744 + setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
3746 dojump(L, pc, GETARG_sBx(i));
3750 luaH_resizearray(L, h, last); /* pre-alloc it at once */
3751 for (; n > 0; n--) {
3753 - setobj2t(L, luaH_setnum(L, h, last--), val);
3754 + setobj2t(L, luaH_setint(L, h, last--), val);
3755 luaC_barriert(L, h, val);
3758 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/lvm.h lua-5.1.3-patched/src/lvm.h
3759 --- ../lua-5.1.3/src/lvm.h 2007-12-27 15:02:25.000000000 +0200
3760 +++ lua-5.1.3-patched/src/lvm.h 2008-03-19 10:49:34.000000000 +0200
3763 #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
3765 -#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
3766 - (((o) = luaV_tonumber(o,n)) != NULL))
3767 +#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
3769 -#define equalobj(L,o1,o2) \
3770 - (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
3771 +#define equalobj(L,o1,o2) (ttype_ext_same(o1,o2) && luaV_equalval(L, o1, o2))
3774 LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
3775 Binary files ../lua-5.1.3/src/lvm.o and lua-5.1.3-patched/src/lvm.o differ
3776 Binary files ../lua-5.1.3/src/lzio.o and lua-5.1.3-patched/src/lzio.o differ
3777 diff -urN --exclude=.svn --exclude=.DS_Store ../lua-5.1.3/src/print.c lua-5.1.3-patched/src/print.c
3778 --- ../lua-5.1.3/src/print.c 2007-03-26 03:17:38.000000000 +0300
3779 +++ lua-5.1.3-patched/src/print.c 2008-03-19 11:04:51.000000000 +0200
3781 #include "lobject.h"
3782 #include "lopcodes.h"
3783 #include "lundump.h"
3786 #define PrintFunction luaU_print
3790 printf(bvalue(o) ? "true" : "false");
3793 + printf(LUA_INTEGER_FMT,ivalue(o));
3796 - printf(LUA_NUMBER_FMT,nvalue(o));
3797 +#ifdef LNUM_COMPLEX
3798 + // TBD: Do we get complex values here?
3799 + { lua_Number b= nvalue_img_fast(o);
3800 + printf( LUA_NUMBER_FMT "%s" LUA_NUMBER_FMT "i", nvalue_fast(o), b>=0 ? "+":"", b ); }
3802 + printf(LUA_NUMBER_FMT,nvalue_fast(o));
3805 PrintString(rawtsvalue(o));
3806 Binary files ../lua-5.1.3/src/print.o and lua-5.1.3-patched/src/print.o differ