Root/package/lua/patches-host/010-lua-5.1.3-lnum-full-260308.patch

1Index: lua-5.1.4/src/Makefile
2===================================================================
3--- lua-5.1.4.orig/src/Makefile 2008-08-24 16:46:37.000000000 +0200
4+++ lua-5.1.4/src/Makefile 2008-08-24 16:48:20.000000000 +0200
5@@ -25,7 +25,7 @@
6 LUA_A= liblua.a
7 CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
8     lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
9- lundump.o lvm.o lzio.o
10+ lundump.o lvm.o lzio.o lnum.o
11 LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
12     lstrlib.o loadlib.o linit.o
13 
14@@ -148,6 +148,7 @@
15 lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
16 lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
17   ltm.h lzio.h lmem.h ldo.h
18+lnum.o: lnum.c lua.h llex.h lnum.h
19 loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
20 lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \
21   ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h
22@@ -179,4 +180,18 @@
23 print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \
24   ltm.h lzio.h lmem.h lopcodes.h lundump.h
25 
26+luaconf.h: lnum_config.h
27+lapi.c: lnum.h
28+lauxlib.c: llimits.h
29+lbaselib.c: llimits.h lobject.h lapi.h
30+lcode.c: lnum.h
31+liolib.c: lnum.h llex.h
32+llex.c: lnum.h
33+lnum.h: lobject.h
34+lobject.c: llex.h lnum.h
35+ltable.c: lnum.h
36+lua.c: llimits.h
37+lvm.c: llex.h lnum.h
38+print.c: lnum.h
39+
40 # (end of Makefile)
41Index: lua-5.1.4/src/lapi.c
42===================================================================
43--- lua-5.1.4.orig/src/lapi.c 2008-08-24 16:46:37.000000000 +0200
44+++ lua-5.1.4/src/lapi.c 2008-08-24 16:48:20.000000000 +0200
45@@ -28,7 +28,7 @@
46 #include "ltm.h"
47 #include "lundump.h"
48 #include "lvm.h"
49-
50+#include "lnum.h"
51 
52 
53 const char lua_ident[] =
54@@ -241,12 +241,13 @@
55 
56 LUA_API int lua_type (lua_State *L, int idx) {
57   StkId o = index2adr(L, idx);
58- return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
59+ return (o == luaO_nilobject) ? LUA_TNONE : ttype_ext(o);
60 }
61 
62 
63 LUA_API const char *lua_typename (lua_State *L, int t) {
64   UNUSED(L);
65+ lua_assert( t!= LUA_TINT );
66   return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
67 }
68 
69@@ -264,6 +265,14 @@
70 }
71 
72 
73+LUA_API int lua_isinteger (lua_State *L, int idx) {
74+ TValue tmp;
75+ lua_Integer dum;
76+ const TValue *o = index2adr(L, idx);
77+ return tonumber(o,&tmp) && (ttisint(o) || tt_integer_valued(o,&dum));
78+}
79+
80+
81 LUA_API int lua_isstring (lua_State *L, int idx) {
82   int t = lua_type(L, idx);
83   return (t == LUA_TSTRING || t == LUA_TNUMBER);
84@@ -309,31 +318,66 @@
85 }
86 
87 
88-
89 LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
90   TValue n;
91   const TValue *o = index2adr(L, idx);
92- if (tonumber(o, &n))
93+ if (tonumber(o, &n)) {
94+#ifdef LNUM_COMPLEX
95+ if (nvalue_img(o) != 0)
96+ luaG_runerror(L, "expecting a real number");
97+#endif
98     return nvalue(o);
99- else
100- return 0;
101+ }
102+ return 0;
103 }
104 
105 
106 LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
107   TValue n;
108+ /* Lua 5.1 documented behaviour is to return nonzero for non-integer:
109+ * "If the number is not an integer, it is truncated in some non-specified way."
110+ * I would suggest to change this, to return 0 for anything that would
111+ * not fit in 'lua_Integer'.
112+ */
113+#ifdef LUA_COMPAT_TOINTEGER
114+ /* Lua 5.1 compatible */
115   const TValue *o = index2adr(L, idx);
116   if (tonumber(o, &n)) {
117- lua_Integer res;
118- lua_Number num = nvalue(o);
119- lua_number2integer(res, num);
120- return res;
121+ lua_Integer i;
122+ lua_Number d;
123+ if (ttisint(o)) return ivalue(o);
124+ d= nvalue_fast(o);
125+# ifdef LNUM_COMPLEX
126+ if (nvalue_img_fast(o) != 0)
127+ luaG_runerror(L, "expecting a real number");
128+# endif
129+ lua_number2integer(i, d);
130+ return i;
131   }
132- else
133- return 0;
134+#else
135+ /* New suggestion */
136+ const TValue *o = index2adr(L, idx);
137+ if (tonumber(o, &n)) {
138+ lua_Integer i;
139+ if (ttisint(o)) return ivalue(o);
140+ if (tt_integer_valued(o,&i)) return i;
141+ }
142+#endif
143+ return 0;
144 }
145 
146 
147+#ifdef LNUM_COMPLEX
148+LUA_API lua_Complex lua_tocomplex (lua_State *L, int idx) {
149+ TValue tmp;
150+ const TValue *o = index2adr(L, idx);
151+ if (tonumber(o, &tmp))
152+ return nvalue_complex(o);
153+ return 0;
154+}
155+#endif
156+
157+
158 LUA_API int lua_toboolean (lua_State *L, int idx) {
159   const TValue *o = index2adr(L, idx);
160   return !l_isfalse(o);
161@@ -364,6 +408,7 @@
162     case LUA_TSTRING: return tsvalue(o)->len;
163     case LUA_TUSERDATA: return uvalue(o)->len;
164     case LUA_TTABLE: return luaH_getn(hvalue(o));
165+ case LUA_TINT:
166     case LUA_TNUMBER: {
167       size_t l;
168       lua_lock(L); /* `luaV_tostring' may create a new string */
169@@ -426,6 +471,8 @@
170 }
171 
172 
173+/* 'lua_pushnumber()' may lose accuracy on integers, 'lua_pushinteger' will not.
174+ */
175 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
176   lua_lock(L);
177   setnvalue(L->top, n);
178@@ -434,12 +481,22 @@
179 }
180 
181 
182-LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
183+LUA_API void lua_pushinteger (lua_State *L, lua_Integer i) {
184+ lua_lock(L);
185+ setivalue(L->top, i);
186+ api_incr_top(L);
187+ lua_unlock(L);
188+}
189+
190+
191+#ifdef LNUM_COMPLEX
192+LUA_API void lua_pushcomplex (lua_State *L, lua_Complex v) {
193   lua_lock(L);
194- setnvalue(L->top, cast_num(n));
195+ setnvalue_complex( L->top, v );
196   api_incr_top(L);
197   lua_unlock(L);
198 }
199+#endif
200 
201 
202 LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
203@@ -569,7 +626,7 @@
204   lua_lock(L);
205   o = index2adr(L, idx);
206   api_check(L, ttistable(o));
207- setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
208+ setobj2s(L, L->top, luaH_getint(hvalue(o), n));
209   api_incr_top(L);
210   lua_unlock(L);
211 }
212@@ -597,6 +654,9 @@
213     case LUA_TUSERDATA:
214       mt = uvalue(obj)->metatable;
215       break;
216+ case LUA_TINT:
217+ mt = G(L)->mt[LUA_TNUMBER];
218+ break;
219     default:
220       mt = G(L)->mt[ttype(obj)];
221       break;
222@@ -687,7 +747,7 @@
223   api_checknelems(L, 1);
224   o = index2adr(L, idx);
225   api_check(L, ttistable(o));
226- setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
227+ setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
228   luaC_barriert(L, hvalue(o), L->top-1);
229   L->top--;
230   lua_unlock(L);
231@@ -721,7 +781,7 @@
232       break;
233     }
234     default: {
235- G(L)->mt[ttype(obj)] = mt;
236+ G(L)->mt[ttype_ext(obj)] = mt;
237       break;
238     }
239   }
240@@ -1085,3 +1145,32 @@
241   return name;
242 }
243 
244+
245+/* Help function for 'luaB_tonumber()', avoids multiple str->number
246+ * conversions for Lua "tonumber()".
247+ *
248+ * Also pushes floating point numbers with integer value as integer, which
249+ * can be used by 'tonumber()' in scripts to bring values back to integer
250+ * realm.
251+ *
252+ * Note: The 'back to integer realm' is _not_ to affect string conversions:
253+ * 'tonumber("4294967295.1")' should give a floating point value, although
254+ * the value would be 4294967296 (and storable in int64 realm).
255+ */
256+int lua_pushvalue_as_number (lua_State *L, int idx)
257+{
258+ const TValue *o = index2adr(L, idx);
259+ TValue tmp;
260+ lua_Integer i;
261+ if (ttisnumber(o)) {
262+ if ( (!ttisint(o)) && tt_integer_valued(o,&i)) {
263+ lua_pushinteger( L, i );
264+ return 1;
265+ }
266+ } else if (!tonumber(o, &tmp)) {
267+ return 0;
268+ }
269+ if (ttisint(o)) lua_pushinteger( L, ivalue(o) );
270+ else lua_pushnumber( L, nvalue_fast(o) );
271+ return 1;
272+}
273Index: lua-5.1.4/src/lapi.h
274===================================================================
275--- lua-5.1.4.orig/src/lapi.h 2008-08-24 16:46:37.000000000 +0200
276+++ lua-5.1.4/src/lapi.h 2008-08-24 16:48:20.000000000 +0200
277@@ -13,4 +13,6 @@
278 
279 LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);
280 
281+int lua_pushvalue_as_number (lua_State *L, int idx);
282+
283 #endif
284Index: lua-5.1.4/src/lauxlib.c
285===================================================================
286--- lua-5.1.4.orig/src/lauxlib.c 2008-08-24 16:46:37.000000000 +0200
287+++ lua-5.1.4/src/lauxlib.c 2008-08-24 16:48:20.000000000 +0200
288@@ -23,7 +23,7 @@
289 #include "lua.h"
290 
291 #include "lauxlib.h"
292-
293+#include "llimits.h"
294 
295 #define FREELIST_REF 0 /* free list of references */
296 
297@@ -66,7 +66,7 @@
298 
299 
300 static void tag_error (lua_State *L, int narg, int tag) {
301- luaL_typerror(L, narg, lua_typename(L, tag));
302+ luaL_typerror(L, narg, tag==LUA_TINT ? "integer" : lua_typename(L, tag));
303 }
304 
305 
306@@ -188,8 +188,8 @@
307 
308 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
309   lua_Integer d = lua_tointeger(L, narg);
310- if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
311- tag_error(L, narg, LUA_TNUMBER);
312+ if (d == 0 && !lua_isinteger(L, narg)) /* avoid extra test when d is not 0 */
313+ tag_error(L, narg, LUA_TINT);
314   return d;
315 }
316 
317@@ -200,6 +200,16 @@
318 }
319 
320 
321+#ifdef LNUM_COMPLEX
322+LUALIB_API lua_Complex luaL_checkcomplex (lua_State *L, int narg) {
323+ lua_Complex c = lua_tocomplex(L, narg);
324+ if (c == 0 && !lua_isnumber(L, narg)) /* avoid extra test when c is not 0 */
325+ tag_error(L, narg, LUA_TNUMBER);
326+ return c;
327+}
328+#endif
329+
330+
331 LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
332   if (!lua_getmetatable(L, obj)) /* no metatable? */
333     return 0;
334Index: lua-5.1.4/src/lauxlib.h
335===================================================================
336--- lua-5.1.4.orig/src/lauxlib.h 2008-08-24 16:46:37.000000000 +0200
337+++ lua-5.1.4/src/lauxlib.h 2008-08-24 16:48:20.000000000 +0200
338@@ -57,6 +57,12 @@
339 LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
340 LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
341                                           lua_Integer def);
342+#define luaL_checkint32(L,narg) ((int)luaL_checkinteger(L,narg))
343+#define luaL_optint32(L,narg,def) ((int)luaL_optinteger(L,narg,def))
344+
345+#ifdef LNUM_COMPLEX
346+ LUALIB_API lua_Complex (luaL_checkcomplex) (lua_State *L, int narg);
347+#endif
348 
349 LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
350 LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
351Index: lua-5.1.4/src/lbaselib.c
352===================================================================
353--- lua-5.1.4.orig/src/lbaselib.c 2008-08-24 16:46:37.000000000 +0200
354+++ lua-5.1.4/src/lbaselib.c 2008-08-24 16:48:20.000000000 +0200
355@@ -18,7 +18,9 @@
356 
357 #include "lauxlib.h"
358 #include "lualib.h"
359-
360+#include "llimits.h"
361+#include "lobject.h"
362+#include "lapi.h"
363 
364 
365 
366@@ -54,20 +56,25 @@
367   int base = luaL_optint(L, 2, 10);
368   if (base == 10) { /* standard conversion */
369     luaL_checkany(L, 1);
370- if (lua_isnumber(L, 1)) {
371- lua_pushnumber(L, lua_tonumber(L, 1));
372+ if (lua_isnumber(L, 1)) { /* numeric string, or a number */
373+ lua_pushvalue_as_number(L,1); /* API extension (not to lose accuracy here) */
374       return 1;
375- }
376+ }
377   }
378   else {
379     const char *s1 = luaL_checkstring(L, 1);
380     char *s2;
381- unsigned long n;
382+ unsigned LUA_INTEGER n;
383     luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
384- n = strtoul(s1, &s2, base);
385+ n = lua_str2ul(s1, &s2, base);
386     if (s1 != s2) { /* at least one valid digit? */
387       while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
388       if (*s2 == '\0') { /* no invalid trailing characters? */
389+
390+ /* Push as number, there needs to be separate 'luaB_tointeger' for
391+ * when the caller wants to preserve the bits (matters if unsigned
392+ * values are used).
393+ */
394         lua_pushnumber(L, (lua_Number)n);
395         return 1;
396       }
397@@ -144,7 +151,7 @@
398   luaL_checktype(L, 2, LUA_TTABLE);
399   getfunc(L, 0);
400   lua_pushvalue(L, 2);
401- if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
402+ if (lua_isnumber(L, 1) && lua_tointeger(L, 1) == 0) {
403     /* change environment of current thread */
404     lua_pushthread(L);
405     lua_insert(L, -2);
406@@ -209,7 +216,7 @@
407       return 1;
408     }
409     default: {
410- lua_pushnumber(L, res);
411+ lua_pushinteger(L, res);
412       return 1;
413     }
414   }
415@@ -631,6 +638,8 @@
416   luaL_register(L, "_G", base_funcs);
417   lua_pushliteral(L, LUA_VERSION);
418   lua_setglobal(L, "_VERSION"); /* set global _VERSION */
419+ lua_pushliteral(L, LUA_LNUM);
420+ lua_setglobal(L, "_LNUM"); /* "[complex] double|float|ldouble int32|int64" */
421   /* `ipairs' and `pairs' need auxliliary functions as upvalues */
422   auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
423   auxopen(L, "pairs", luaB_pairs, luaB_next);
424Index: lua-5.1.4/src/lcode.c
425===================================================================
426--- lua-5.1.4.orig/src/lcode.c 2008-08-24 16:46:37.000000000 +0200
427+++ lua-5.1.4/src/lcode.c 2008-08-24 16:48:20.000000000 +0200
428@@ -22,13 +22,18 @@
429 #include "lopcodes.h"
430 #include "lparser.h"
431 #include "ltable.h"
432+#include "lnum.h"
433 
434 
435 #define hasjumps(e) ((e)->t != (e)->f)
436 
437-
438 static int isnumeral(expdesc *e) {
439- return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
440+ int ek=
441+#ifdef LNUM_COMPLEX
442+ (e->k == VKNUM2) ||
443+#endif
444+ (e->k == VKINT) || (e->k == VKNUM);
445+ return (ek && e->t == NO_JUMP && e->f == NO_JUMP);
446 }
447 
448 
449@@ -231,12 +236,16 @@
450   TValue *idx = luaH_set(L, fs->h, k);
451   Proto *f = fs->f;
452   int oldsize = f->sizek;
453- if (ttisnumber(idx)) {
454- lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
455- return cast_int(nvalue(idx));
456+ if (ttype(idx)==LUA_TNUMBER) {
457+ luai_normalize(idx);
458+ lua_assert( ttype(idx)==LUA_TINT ); /* had no fraction */
459+ }
460+ if (ttisint(idx)) {
461+ lua_assert(luaO_rawequalObj(&fs->f->k[ivalue(idx)], v));
462+ return cast_int(ivalue(idx));
463   }
464   else { /* constant not found; create a new entry */
465- setnvalue(idx, cast_num(fs->nk));
466+ setivalue(idx, fs->nk);
467     luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
468                     MAXARG_Bx, "constant table overflow");
469     while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
470@@ -261,6 +270,21 @@
471 }
472 
473 
474+int luaK_integerK (FuncState *fs, lua_Integer r) {
475+ TValue o;
476+ setivalue(&o, r);
477+ return addk(fs, &o, &o);
478+}
479+
480+
481+#ifdef LNUM_COMPLEX
482+static int luaK_imagK (FuncState *fs, lua_Number r) {
483+ TValue o;
484+ setnvalue_complex(&o, r*I);
485+ return addk(fs, &o, &o);
486+}
487+#endif
488+
489 static int boolK (FuncState *fs, int b) {
490   TValue o;
491   setbvalue(&o, b);
492@@ -359,6 +383,16 @@
493       luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
494       break;
495     }
496+ case VKINT: {
497+ luaK_codeABx(fs, OP_LOADK, reg, luaK_integerK(fs, e->u.ival));
498+ break;
499+ }
500+#ifdef LNUM_COMPLEX
501+ case VKNUM2: {
502+ luaK_codeABx(fs, OP_LOADK, reg, luaK_imagK(fs, e->u.nval));
503+ break;
504+ }
505+#endif
506     case VRELOCABLE: {
507       Instruction *pc = &getcode(fs, e);
508       SETARG_A(*pc, reg);
509@@ -444,6 +478,10 @@
510 int luaK_exp2RK (FuncState *fs, expdesc *e) {
511   luaK_exp2val(fs, e);
512   switch (e->k) {
513+#ifdef LNUM_COMPLEX
514+ case VKNUM2:
515+#endif
516+ case VKINT:
517     case VKNUM:
518     case VTRUE:
519     case VFALSE:
520@@ -451,6 +489,10 @@
521       if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
522         e->u.s.info = (e->k == VNIL) ? nilK(fs) :
523                       (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
524+ (e->k == VKINT) ? luaK_integerK(fs, e->u.ival) :
525+#ifdef LNUM_COMPLEX
526+ (e->k == VKNUM2) ? luaK_imagK(fs, e->u.nval) :
527+#endif
528                                         boolK(fs, (e->k == VTRUE));
529         e->k = VK;
530         return RKASK(e->u.s.info);
531@@ -540,7 +582,10 @@
532   int pc; /* pc of last jump */
533   luaK_dischargevars(fs, e);
534   switch (e->k) {
535- case VK: case VKNUM: case VTRUE: {
536+#ifdef LNUM_COMPLEX
537+ case VKNUM2:
538+#endif
539+ case VKINT: case VK: case VKNUM: case VTRUE: {
540       pc = NO_JUMP; /* always true; do nothing */
541       break;
542     }
543@@ -598,7 +643,10 @@
544       e->k = VTRUE;
545       break;
546     }
547- case VK: case VKNUM: case VTRUE: {
548+#ifdef LNUM_COMPLEX
549+ case VKNUM2:
550+#endif
551+ case VKINT: case VK: case VKNUM: case VTRUE: {
552       e->k = VFALSE;
553       break;
554     }
555@@ -634,25 +682,70 @@
556 
557 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
558   lua_Number v1, v2, r;
559+ int vkres= VKNUM;
560   if (!isnumeral(e1) || !isnumeral(e2)) return 0;
561- v1 = e1->u.nval;
562- v2 = e2->u.nval;
563+
564+ /* real and imaginary parts don't mix. */
565+#ifdef LNUM_COMPLEX
566+ if (e1->k == VKNUM2) {
567+ if ((op != OP_UNM) && (e2->k != VKNUM2)) return 0;
568+ vkres= VKNUM2; }
569+ else if (e2->k == VKNUM2) { return 0; }
570+#endif
571+ if ((e1->k == VKINT) && (e2->k == VKINT)) {
572+ lua_Integer i1= e1->u.ival, i2= e2->u.ival;
573+ lua_Integer rr;
574+ int done= 0;
575+ /* Integer/integer calculations (may end up producing floating point) */
576+ switch (op) {
577+ case OP_ADD: done= try_addint( &rr, i1, i2 ); break;
578+ case OP_SUB: done= try_subint( &rr, i1, i2 ); break;
579+ case OP_MUL: done= try_mulint( &rr, i1, i2 ); break;
580+ case OP_DIV: done= try_divint( &rr, i1, i2 ); break;
581+ case OP_MOD: done= try_modint( &rr, i1, i2 ); break;
582+ case OP_POW: done= try_powint( &rr, i1, i2 ); break;
583+ case OP_UNM: done= try_unmint( &rr, i1 ); break;
584+ default: done= 0; break;
585+ }
586+ if (done) {
587+ e1->u.ival = rr; /* remained within integer range */
588+ return 1;
589+ }
590+ }
591+ v1 = (e1->k == VKINT) ? ((lua_Number)e1->u.ival) : e1->u.nval;
592+ v2 = (e2->k == VKINT) ? ((lua_Number)e2->u.ival) : e2->u.nval;
593+
594   switch (op) {
595     case OP_ADD: r = luai_numadd(v1, v2); break;
596     case OP_SUB: r = luai_numsub(v1, v2); break;
597- case OP_MUL: r = luai_nummul(v1, v2); break;
598+ case OP_MUL:
599+#ifdef LNUM_COMPLEX
600+ if (vkres==VKNUM2) return 0; /* leave to runtime (could do here, but not worth it?) */
601+#endif
602+ r = luai_nummul(v1, v2); break;
603     case OP_DIV:
604       if (v2 == 0) return 0; /* do not attempt to divide by 0 */
605- r = luai_numdiv(v1, v2); break;
606+#ifdef LNUM_COMPLEX
607+ if (vkres==VKNUM2) return 0; /* leave to runtime */
608+#endif
609+ r = luai_numdiv(v1, v2); break;
610     case OP_MOD:
611       if (v2 == 0) return 0; /* do not attempt to divide by 0 */
612+#ifdef LNUM_COMPLEX
613+ if (vkres==VKNUM2) return 0; /* leave to runtime */
614+#endif
615       r = luai_nummod(v1, v2); break;
616- case OP_POW: r = luai_numpow(v1, v2); break;
617+ case OP_POW:
618+#ifdef LNUM_COMPLEX
619+ if (vkres==VKNUM2) return 0; /* leave to runtime */
620+#endif
621+ r = luai_numpow(v1, v2); break;
622     case OP_UNM: r = luai_numunm(v1); break;
623     case OP_LEN: return 0; /* no constant folding for 'len' */
624     default: lua_assert(0); r = 0; break;
625   }
626   if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
627+ e1->k = cast(expkind,vkres);
628   e1->u.nval = r;
629   return 1;
630 }
631@@ -696,7 +789,8 @@
632 
633 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
634   expdesc e2;
635- e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
636+ e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
637+
638   switch (op) {
639     case OPR_MINUS: {
640       if (!isnumeral(e))
641Index: lua-5.1.4/src/lcode.h
642===================================================================
643--- lua-5.1.4.orig/src/lcode.h 2008-08-24 16:46:37.000000000 +0200
644+++ lua-5.1.4/src/lcode.h 2008-08-24 16:48:20.000000000 +0200
645@@ -71,6 +71,6 @@
646 LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
647 LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
648 LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
649-
650+LUAI_FUNC int luaK_integerK (FuncState *fs, lua_Integer r);
651 
652 #endif
653Index: lua-5.1.4/src/ldebug.c
654===================================================================
655--- lua-5.1.4.orig/src/ldebug.c 2008-08-24 16:46:37.000000000 +0200
656+++ lua-5.1.4/src/ldebug.c 2008-08-24 16:48:20.000000000 +0200
657@@ -183,7 +183,7 @@
658     int *lineinfo = f->l.p->lineinfo;
659     int i;
660     for (i=0; i<f->l.p->sizelineinfo; i++)
661- setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
662+ setbvalue(luaH_setint(L, t, lineinfo[i]), 1);
663     sethvalue(L, L->top, t);
664   }
665   incr_top(L);
666@@ -566,7 +566,7 @@
667 
668 void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
669   const char *name = NULL;
670- const char *t = luaT_typenames[ttype(o)];
671+ const char *t = luaT_typenames[ttype_ext(o)];
672   const char *kind = (isinstack(L->ci, o)) ?
673                          getobjname(L, L->ci, cast_int(o - L->base), &name) :
674                          NULL;
675@@ -594,8 +594,8 @@
676 
677 
678 int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
679- const char *t1 = luaT_typenames[ttype(p1)];
680- const char *t2 = luaT_typenames[ttype(p2)];
681+ const char *t1 = luaT_typenames[ttype_ext(p1)];
682+ const char *t2 = luaT_typenames[ttype_ext(p2)];
683   if (t1[2] == t2[2])
684     luaG_runerror(L, "attempt to compare two %s values", t1);
685   else
686Index: lua-5.1.4/src/ldo.c
687===================================================================
688--- lua-5.1.4.orig/src/ldo.c 2008-08-24 16:46:37.000000000 +0200
689+++ lua-5.1.4/src/ldo.c 2008-08-24 16:48:20.000000000 +0200
690@@ -219,9 +219,9 @@
691     luaC_checkGC(L);
692     htab = luaH_new(L, nvar, 1); /* create `arg' table */
693     for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
694- setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
695+ setobj2n(L, luaH_setint(L, htab, i+1), L->top - nvar + i);
696     /* store counter in field `n' */
697- setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
698+ setivalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), nvar);
699   }
700 #endif
701   /* move fixed parameters to final position */
702Index: lua-5.1.4/src/ldump.c
703===================================================================
704--- lua-5.1.4.orig/src/ldump.c 2008-08-24 16:46:37.000000000 +0200
705+++ lua-5.1.4/src/ldump.c 2008-08-24 16:48:20.000000000 +0200
706@@ -52,6 +52,11 @@
707  DumpVar(x,D);
708 }
709 
710+static void DumpInteger(lua_Integer x, DumpState* D)
711+{
712+ DumpVar(x,D);
713+}
714+
715 static void DumpVector(const void* b, int n, size_t size, DumpState* D)
716 {
717  DumpInt(n,D);
718@@ -93,8 +98,11 @@
719     DumpChar(bvalue(o),D);
720     break;
721    case LUA_TNUMBER:
722- DumpNumber(nvalue(o),D);
723+ DumpNumber(nvalue_fast(o),D);
724     break;
725+ case LUA_TINT:
726+ DumpInteger(ivalue(o),D);
727+ break;
728    case LUA_TSTRING:
729     DumpString(rawtsvalue(o),D);
730     break;
731Index: lua-5.1.4/src/liolib.c
732===================================================================
733--- lua-5.1.4.orig/src/liolib.c 2008-08-24 16:46:37.000000000 +0200
734+++ lua-5.1.4/src/liolib.c 2008-08-24 16:48:20.000000000 +0200
735@@ -9,6 +9,7 @@
736 #include <stdio.h>
737 #include <stdlib.h>
738 #include <string.h>
739+#include <ctype.h>
740 
741 #define liolib_c
742 #define LUA_LIB
743@@ -18,7 +19,8 @@
744 #include "lauxlib.h"
745 #include "lualib.h"
746 
747-
748+#include "lnum.h"
749+#include "llex.h"
750 
751 #define IO_INPUT 1
752 #define IO_OUTPUT 2
753@@ -269,6 +271,13 @@
754 ** =======================================================
755 */
756 
757+/*
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).
763+*/
764 
765 static int read_number (lua_State *L, FILE *f) {
766   lua_Number d;
767@@ -279,6 +288,43 @@
768   else return 0; /* read fails */
769 }
770 
771+static int read_integer (lua_State *L, FILE *f) {
772+ lua_Integer i;
773+ if (fscanf(f, LUA_INTEGER_SCAN, &i) == 1) {
774+ lua_pushinteger(L, i);
775+ return 1;
776+ }
777+ else return 0; /* read fails */
778+}
779+
780+#ifdef LNUM_COMPLEX
781+static int read_complex (lua_State *L, FILE *f) {
782+ /* NNN / NNNi / NNN+MMMi / NNN-MMMi */
783+ lua_Number a,b;
784+ if (fscanf(f, LUA_NUMBER_SCAN, &a) == 1) {
785+ int c=fgetc(f);
786+ switch(c) {
787+ case 'i':
788+ lua_pushcomplex(L, a*I);
789+ return 1;
790+ case '+':
791+ case '-':
792+ /* "i" is consumed if at the end; just 'NNN+MMM' will most likely
793+ * behave as if "i" was there? (TBD: test)
794+ */
795+ if (fscanf(f, LUA_NUMBER_SCAN "i", &b) == 1) {
796+ lua_pushcomplex(L, a+ (c=='+' ? b:-b)*I);
797+ return 1;
798+ }
799+ }
800+ ungetc( c,f );
801+ lua_pushnumber(L,a); /*real part only*/
802+ return 1;
803+ }
804+ return 0; /* read fails */
805+}
806+#endif
807+
808 
809 static int test_eof (lua_State *L, FILE *f) {
810   int c = getc(f);
811@@ -352,6 +398,14 @@
812           case 'n': /* number */
813             success = read_number(L, f);
814             break;
815+ case 'i': /* integer (full accuracy) */
816+ success = read_integer(L, f);
817+ break;
818+#ifdef LNUM_COMPLEX
819+ case 'c': /* complex */
820+ success = read_complex(L, f);
821+ break;
822+#endif
823           case 'l': /* line */
824             success = read_line(L, f);
825             break;
826@@ -412,9 +466,10 @@
827   int status = 1;
828   for (; nargs--; arg++) {
829     if (lua_type(L, arg) == LUA_TNUMBER) {
830- /* optimization: could be done exactly as for strings */
831- status = status &&
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;
835+ else
836+ status = status && fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
837     }
838     else {
839       size_t l;
840@@ -457,7 +512,7 @@
841   static const char *const modenames[] = {"no", "full", "line", NULL};
842   FILE *f = tofile(L);
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);
848 }
849Index: lua-5.1.4/src/llex.c
850===================================================================
851--- lua-5.1.4.orig/src/llex.c 2008-08-24 16:46:37.000000000 +0200
852+++ lua-5.1.4/src/llex.c 2008-08-24 16:48:20.000000000 +0200
853@@ -22,6 +22,7 @@
854 #include "lstring.h"
855 #include "ltable.h"
856 #include "lzio.h"
857+#include "lnum.h"
858 
859 
860 
861@@ -34,13 +35,17 @@
862 
863 
864 /* ORDER RESERVED */
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>",
873+ "<integer>",
874+#ifdef LNUM_COMPLEX
875+ "<number2>",
876+#endif
877     NULL
878 };
879 
880@@ -90,7 +95,11 @@
881   switch (token) {
882     case TK_NAME:
883     case TK_STRING:
884+ case TK_INT:
885     case TK_NUMBER:
886+#ifdef LNUM_COMPLEX
887+ case TK_NUMBER2:
888+#endif
889       save(ls, '\0');
890       return luaZ_buffer(ls->buff);
891     default:
892@@ -173,23 +182,27 @@
893     if (p[n] == from) p[n] = to;
894 }
895 
896-
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;
903+ int ret;
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);
908+ if (!ret) {
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);
912   }
913+ return ret;
914 }
915 
916 
917-/* LUA_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) {
921+ int ret;
922   lua_assert(isdigit(ls->current));
923   do {
924     save_and_next(ls);
925@@ -200,8 +213,9 @@
926     save_and_next(ls);
927   save(ls, '\0');
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 */
933+ return ret;
934 }
935 
936 
937@@ -329,6 +343,7 @@
938 }
939 
940 
941+/* char / TK_* */
942 static int llex (LexState *ls, SemInfo *seminfo) {
943   luaZ_resetbuffer(ls->buff);
944   for (;;) {
945@@ -400,8 +415,7 @@
946         }
947         else if (!isdigit(ls->current)) return '.';
948         else {
949- read_numeral(ls, seminfo);
950- return TK_NUMBER;
951+ return read_numeral(ls, seminfo);
952         }
953       }
954       case EOZ: {
955@@ -414,8 +428,7 @@
956           continue;
957         }
958         else if (isdigit(ls->current)) {
959- read_numeral(ls, seminfo);
960- return TK_NUMBER;
961+ return read_numeral(ls, seminfo);
962         }
963         else if (isalpha(ls->current) || ls->current == '_') {
964           /* identifier or reserved word */
965Index: lua-5.1.4/src/llex.h
966===================================================================
967--- lua-5.1.4.orig/src/llex.h 2008-08-24 16:46:37.000000000 +0200
968+++ lua-5.1.4/src/llex.h 2008-08-24 16:48:20.000000000 +0200
969@@ -29,19 +29,22 @@
970   TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
971   /* other terminal symbols */
972   TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
973- TK_NAME, TK_STRING, TK_EOS
974+ TK_NAME, TK_STRING, TK_EOS, TK_INT
975+#ifdef LNUM_COMPLEX
976+ , TK_NUMBER2 /* imaginary constants: Ni */
977+#endif
978 };
979 
980 /* number of reserved words */
981 #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
982 
983 
984-/* array with token `names' */
985-LUAI_DATA const char *const luaX_tokens [];
986-
987-
988+/* SemInfo is a local data structure of 'llex.c', used for carrying a string
989+ * or a number. A separate token (TK_*) will tell, how to interpret the data.
990+ */
991 typedef union {
992   lua_Number r;
993+ lua_Integer i;
994   TString *ts;
995 } SemInfo; /* semantics information */
996 
997Index: lua-5.1.4/src/llimits.h
998===================================================================
999--- lua-5.1.4.orig/src/llimits.h 2008-08-24 16:46:37.000000000 +0200
1000+++ lua-5.1.4/src/llimits.h 2008-08-24 16:48:20.000000000 +0200
1001@@ -49,6 +49,7 @@
1002 
1003 /* result of a `usual argument conversion' over lua_Number */
1004 typedef LUAI_UACNUMBER l_uacNumber;
1005+typedef LUAI_UACINTEGER l_uacInteger;
1006 
1007 
1008 /* internal assertions for in-house debugging */
1009@@ -80,7 +81,6 @@
1010 #define cast_int(i) cast(int, (i))
1011 
1012 
1013-
1014 /*
1015 ** type for virtual-machine instructions
1016 ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
1017Index: lua-5.1.4/src/lmathlib.c
1018===================================================================
1019--- lua-5.1.4.orig/src/lmathlib.c 2008-08-24 16:46:37.000000000 +0200
1020+++ lua-5.1.4/src/lmathlib.c 2008-08-24 16:48:20.000000000 +0200
1021@@ -4,7 +4,6 @@
1022 ** See Copyright Notice in lua.h
1023 */
1024 
1025-
1026 #include <stdlib.h>
1027 #include <math.h>
1028 
1029@@ -16,113 +15,210 @@
1030 #include "lauxlib.h"
1031 #include "lualib.h"
1032 
1033+/* 'luai_vectpow()' as a replacement for 'cpow()'. Defined in the header; we
1034+ * don't intrude the code libs internal functions.
1035+ */
1036+#ifdef LNUM_COMPLEX
1037+# include "lnum.h"
1038+#endif
1039 
1040 #undef PI
1041-#define PI (3.14159265358979323846)
1042-#define RADIANS_PER_DEGREE (PI/180.0)
1043-
1044+#ifdef LNUM_FLOAT
1045+# define PI (3.14159265358979323846F)
1046+#elif defined(M_PI)
1047+# define PI M_PI
1048+#else
1049+# define PI (3.14159265358979323846264338327950288)
1050+#endif
1051+#define RADIANS_PER_DEGREE (PI/180)
1052 
1053+#undef HUGE
1054+#ifdef LNUM_FLOAT
1055+# define HUGE HUGE_VALF
1056+#elif defined(LNUM_LDOUBLE)
1057+# define HUGE HUGE_VALL
1058+#else
1059+# define HUGE HUGE_VAL
1060+#endif
1061 
1062 static int math_abs (lua_State *L) {
1063- lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
1064+#ifdef LNUM_COMPLEX
1065+ lua_pushnumber(L, _LF(cabs) (luaL_checkcomplex(L,1)));
1066+#else
1067+ lua_pushnumber(L, _LF(fabs) (luaL_checknumber(L, 1)));
1068+#endif
1069   return 1;
1070 }
1071 
1072 static int math_sin (lua_State *L) {
1073- lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
1074+#ifdef LNUM_COMPLEX
1075+ lua_pushcomplex(L, _LF(csin) (luaL_checkcomplex(L,1)));
1076+#else
1077+ lua_pushnumber(L, _LF(sin) (luaL_checknumber(L, 1)));
1078+#endif
1079   return 1;
1080 }
1081 
1082 static int math_sinh (lua_State *L) {
1083- lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
1084+#ifdef LNUM_COMPLEX
1085+ lua_pushcomplex(L, _LF(csinh) (luaL_checkcomplex(L,1)));
1086+#else
1087+ lua_pushnumber(L, _LF(sinh) (luaL_checknumber(L, 1)));
1088+#endif
1089   return 1;
1090 }
1091 
1092 static int math_cos (lua_State *L) {
1093- lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
1094+#ifdef LNUM_COMPLEX
1095+ lua_pushcomplex(L, _LF(ccos) (luaL_checkcomplex(L,1)));
1096+#else
1097+ lua_pushnumber(L, _LF(cos) (luaL_checknumber(L, 1)));
1098+#endif
1099   return 1;
1100 }
1101 
1102 static int math_cosh (lua_State *L) {
1103- lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
1104+#ifdef LNUM_COMPLEX
1105+ lua_pushcomplex(L, _LF(ccosh) (luaL_checkcomplex(L,1)));
1106+#else
1107+ lua_pushnumber(L, _LF(cosh) (luaL_checknumber(L, 1)));
1108+#endif
1109   return 1;
1110 }
1111 
1112 static int math_tan (lua_State *L) {
1113- lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
1114+#ifdef LNUM_COMPLEX
1115+ lua_pushcomplex(L, _LF(ctan) (luaL_checkcomplex(L,1)));
1116+#else
1117+ lua_pushnumber(L, _LF(tan) (luaL_checknumber(L, 1)));
1118+#endif
1119   return 1;
1120 }
1121 
1122 static int math_tanh (lua_State *L) {
1123- lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
1124+#ifdef LNUM_COMPLEX
1125+ lua_pushcomplex(L, _LF(ctanh) (luaL_checkcomplex(L,1)));
1126+#else
1127+ lua_pushnumber(L, _LF(tanh) (luaL_checknumber(L, 1)));
1128+#endif
1129   return 1;
1130 }
1131 
1132 static int math_asin (lua_State *L) {
1133- lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
1134+#ifdef LNUM_COMPLEX
1135+ lua_pushcomplex(L, _LF(casin) (luaL_checkcomplex(L,1)));
1136+#else
1137+ lua_pushnumber(L, _LF(asin) (luaL_checknumber(L, 1)));
1138+#endif
1139   return 1;
1140 }
1141 
1142 static int math_acos (lua_State *L) {
1143- lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
1144+#ifdef LNUM_COMPLEX
1145+ lua_pushcomplex(L, _LF(cacos) (luaL_checkcomplex(L,1)));
1146+#else
1147+ lua_pushnumber(L, _LF(acos) (luaL_checknumber(L, 1)));
1148+#endif
1149   return 1;
1150 }
1151 
1152 static int math_atan (lua_State *L) {
1153- lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
1154+#ifdef LNUM_COMPLEX
1155+ lua_pushcomplex(L, _LF(catan) (luaL_checkcomplex(L,1)));
1156+#else
1157+ lua_pushnumber(L, _LF(atan) (luaL_checknumber(L, 1)));
1158+#endif
1159   return 1;
1160 }
1161 
1162 static int math_atan2 (lua_State *L) {
1163- lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1164+ /* scalars only */
1165+ lua_pushnumber(L, _LF(atan2) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1166   return 1;
1167 }
1168 
1169 static int math_ceil (lua_State *L) {
1170- lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
1171+#ifdef LNUM_COMPLEX
1172+ lua_Complex v= luaL_checkcomplex(L, 1);
1173+ lua_pushcomplex(L, _LF(ceil) (_LF(creal)(v)) + _LF(ceil) (_LF(cimag)(v))*I);
1174+#else
1175+ lua_pushnumber(L, _LF(ceil) (luaL_checknumber(L, 1)));
1176+#endif
1177   return 1;
1178 }
1179 
1180 static int math_floor (lua_State *L) {
1181- lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
1182+#ifdef LNUM_COMPLEX
1183+ lua_Complex v= luaL_checkcomplex(L, 1);
1184+ lua_pushcomplex(L, _LF(floor) (_LF(creal)(v)) + _LF(floor) (_LF(cimag)(v))*I);
1185+#else
1186+ lua_pushnumber(L, _LF(floor) (luaL_checknumber(L, 1)));
1187+#endif
1188   return 1;
1189 }
1190 
1191-static int math_fmod (lua_State *L) {
1192- lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1193+static int math_fmod (lua_State *L) {
1194+ /* scalars only */
1195+ lua_pushnumber(L, _LF(fmod) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1196   return 1;
1197 }
1198 
1199 static int math_modf (lua_State *L) {
1200- double ip;
1201- double fp = modf(luaL_checknumber(L, 1), &ip);
1202+ /* scalars only */
1203+ lua_Number ip;
1204+ lua_Number fp = _LF(modf) (luaL_checknumber(L, 1), &ip);
1205   lua_pushnumber(L, ip);
1206   lua_pushnumber(L, fp);
1207   return 2;
1208 }
1209 
1210 static int math_sqrt (lua_State *L) {
1211- lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
1212+#ifdef LNUM_COMPLEX
1213+ lua_pushcomplex(L, _LF(csqrt) (luaL_checkcomplex(L,1)));
1214+#else
1215+ lua_pushnumber(L, _LF(sqrt) (luaL_checknumber(L, 1)));
1216+#endif
1217   return 1;
1218 }
1219 
1220 static int math_pow (lua_State *L) {
1221- lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1222+#ifdef LNUM_COMPLEX
1223+ /* C99 'cpow' gives somewhat inaccurate results (i.e. (-1)^2 = -1+1.2246467991474e-16i).
1224+ * 'luai_vectpow' smoothens such, reusing it is the reason we need to #include "lnum.h".
1225+ */
1226+ lua_pushcomplex(L, luai_vectpow(luaL_checkcomplex(L,1), luaL_checkcomplex(L,2)));
1227+#else
1228+ lua_pushnumber(L, _LF(pow) (luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
1229+#endif
1230   return 1;
1231 }
1232 
1233 static int math_log (lua_State *L) {
1234- lua_pushnumber(L, log(luaL_checknumber(L, 1)));
1235+#ifdef LNUM_COMPLEX
1236+ lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)));
1237+#else
1238+ lua_pushnumber(L, _LF(log) (luaL_checknumber(L, 1)));
1239+#endif
1240   return 1;
1241 }
1242 
1243 static int math_log10 (lua_State *L) {
1244- lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
1245+#ifdef LNUM_COMPLEX
1246+ /* Not in standard <complex.h> , but easy to calculate: log_a(x) = log_b(x) / log_b(a)
1247+ */
1248+ lua_pushcomplex(L, _LF(clog) (luaL_checkcomplex(L,1)) / _LF(log) (10));
1249+#else
1250+ lua_pushnumber(L, _LF(log10) (luaL_checknumber(L, 1)));
1251+#endif
1252   return 1;
1253 }
1254 
1255 static int math_exp (lua_State *L) {
1256- lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
1257+#ifdef LNUM_COMPLEX
1258+ lua_pushcomplex(L, _LF(cexp) (luaL_checkcomplex(L,1)));
1259+#else
1260+ lua_pushnumber(L, _LF(exp) (luaL_checknumber(L, 1)));
1261+#endif
1262   return 1;
1263 }
1264 
1265@@ -138,19 +234,20 @@
1266 
1267 static int math_frexp (lua_State *L) {
1268   int e;
1269- lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
1270+ lua_pushnumber(L, _LF(frexp) (luaL_checknumber(L, 1), &e));
1271   lua_pushinteger(L, e);
1272   return 2;
1273 }
1274 
1275 static int math_ldexp (lua_State *L) {
1276- lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
1277+ lua_pushnumber(L, _LF(ldexp) (luaL_checknumber(L, 1), luaL_checkint(L, 2)));
1278   return 1;
1279 }
1280 
1281 
1282 
1283 static int math_min (lua_State *L) {
1284+ /* scalars only */
1285   int n = lua_gettop(L); /* number of arguments */
1286   lua_Number dmin = luaL_checknumber(L, 1);
1287   int i;
1288@@ -165,6 +262,7 @@
1289 
1290 
1291 static int math_max (lua_State *L) {
1292+ /* scalars only */
1293   int n = lua_gettop(L); /* number of arguments */
1294   lua_Number dmax = luaL_checknumber(L, 1);
1295   int i;
1296@@ -182,25 +280,20 @@
1297   /* the `%' avoids the (rare) case of r==1, and is needed also because on
1298      some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
1299   lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
1300- switch (lua_gettop(L)) { /* check number of arguments */
1301- case 0: { /* no arguments */
1302- lua_pushnumber(L, r); /* Number between 0 and 1 */
1303- break;
1304- }
1305- case 1: { /* only upper limit */
1306- int u = luaL_checkint(L, 1);
1307- luaL_argcheck(L, 1<=u, 1, "interval is empty");
1308- lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
1309- break;
1310- }
1311- case 2: { /* lower and upper limits */
1312- int l = luaL_checkint(L, 1);
1313- int u = luaL_checkint(L, 2);
1314- luaL_argcheck(L, l<=u, 2, "interval is empty");
1315- lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
1316- break;
1317- }
1318- default: return luaL_error(L, "wrong number of arguments");
1319+ int n= lua_gettop(L); /* number of arguments */
1320+ if (n==0) { /* no arguments: range [0,1) */
1321+ lua_pushnumber(L, r);
1322+ } else if (n<=2) { /* int range [1,u] or [l,u] */
1323+ int l= n==1 ? 1 : luaL_checkint(L, 1);
1324+ int u = luaL_checkint(L, n);
1325+ int tmp;
1326+ lua_Number d;
1327+ luaL_argcheck(L, l<=u, n, "interval is empty");
1328+ d= _LF(floor)(r*(u-l+1));
1329+ lua_number2int(tmp,d);
1330+ lua_pushinteger(L, l+tmp);
1331+ } else {
1332+ return luaL_error(L, "wrong number of arguments");
1333   }
1334   return 1;
1335 }
1336@@ -211,6 +304,66 @@
1337   return 0;
1338 }
1339 
1340+/*
1341+* Lua 5.1 does not have acosh, asinh, atanh for scalars (not ANSI C)
1342+*/
1343+#if __STDC_VERSION__ >= 199901L
1344+static int math_acosh (lua_State *L) {
1345+# ifdef LNUM_COMPLEX
1346+ lua_pushcomplex(L, _LF(cacosh) (luaL_checkcomplex(L,1)));
1347+# else
1348+ lua_pushnumber(L, _LF(acosh) (luaL_checknumber(L,1)));
1349+# endif
1350+ return 1;
1351+}
1352+static int math_asinh (lua_State *L) {
1353+# ifdef LNUM_COMPLEX
1354+ lua_pushcomplex(L, _LF(casinh) (luaL_checkcomplex(L,1)));
1355+# else
1356+ lua_pushnumber(L, _LF(asinh) (luaL_checknumber(L,1)));
1357+# endif
1358+ return 1;
1359+}
1360+static int math_atanh (lua_State *L) {
1361+# ifdef LNUM_COMPLEX
1362+ lua_pushcomplex(L, _LF(catanh) (luaL_checkcomplex(L,1)));
1363+# else
1364+ lua_pushnumber(L, _LF(atanh) (luaL_checknumber(L,1)));
1365+# endif
1366+ return 1;
1367+}
1368+#endif
1369+
1370+/*
1371+ * C99 complex functions, not covered above.
1372+*/
1373+#ifdef LNUM_COMPLEX
1374+static int math_arg (lua_State *L) {
1375+ lua_pushnumber(L, _LF(carg) (luaL_checkcomplex(L,1)));
1376+ return 1;
1377+}
1378+
1379+static int math_imag (lua_State *L) {
1380+ lua_pushnumber(L, _LF(cimag) (luaL_checkcomplex(L,1)));
1381+ return 1;
1382+}
1383+
1384+static int math_real (lua_State *L) {
1385+ lua_pushnumber(L, _LF(creal) (luaL_checkcomplex(L,1)));
1386+ return 1;
1387+}
1388+
1389+static int math_conj (lua_State *L) {
1390+ lua_pushcomplex(L, _LF(conj) (luaL_checkcomplex(L,1)));
1391+ return 1;
1392+}
1393+
1394+static int math_proj (lua_State *L) {
1395+ lua_pushcomplex(L, _LF(cproj) (luaL_checkcomplex(L,1)));
1396+ return 1;
1397+}
1398+#endif
1399+
1400 
1401 static const luaL_Reg mathlib[] = {
1402   {"abs", math_abs},
1403@@ -241,6 +394,18 @@
1404   {"sqrt", math_sqrt},
1405   {"tanh", math_tanh},
1406   {"tan", math_tan},
1407+#if __STDC_VERSION__ >= 199901L
1408+ {"acosh", math_acosh},
1409+ {"asinh", math_asinh},
1410+ {"atanh", math_atanh},
1411+#endif
1412+#ifdef LNUM_COMPLEX
1413+ {"arg", math_arg},
1414+ {"imag", math_imag},
1415+ {"real", math_real},
1416+ {"conj", math_conj},
1417+ {"proj", math_proj},
1418+#endif
1419   {NULL, NULL}
1420 };
1421 
1422@@ -252,8 +417,10 @@
1423   luaL_register(L, LUA_MATHLIBNAME, mathlib);
1424   lua_pushnumber(L, PI);
1425   lua_setfield(L, -2, "pi");
1426- lua_pushnumber(L, HUGE_VAL);
1427+ lua_pushnumber(L, HUGE);
1428   lua_setfield(L, -2, "huge");
1429+ lua_pushinteger(L, LUA_INTEGER_MAX );
1430+ lua_setfield(L, -2, "hugeint");
1431 #if defined(LUA_COMPAT_MOD)
1432   lua_getfield(L, -1, "fmod");
1433   lua_setfield(L, -2, "mod");
1434Index: lua-5.1.4/src/lnum.c
1435===================================================================
1436--- /dev/null 1970-01-01 00:00:00.000000000 +0000
1437+++ lua-5.1.4/src/lnum.c 2008-08-24 16:48:20.000000000 +0200
1438@@ -0,0 +1,312 @@
1439+/*
1440+** $Id: lnum.c,v ... $
1441+** Internal number model
1442+** See Copyright Notice in lua.h
1443+*/
1444+
1445+#include <stdlib.h>
1446+#include <math.h>
1447+#include <ctype.h>
1448+#include <string.h>
1449+#include <stdio.h>
1450+#include <errno.h>
1451+
1452+#define lnum_c
1453+#define LUA_CORE
1454+
1455+#include "lua.h"
1456+#include "llex.h"
1457+#include "lnum.h"
1458+
1459+/*
1460+** lua_real2str converts a (non-complex) number to a string.
1461+** lua_str2real converts a string to a (non-complex) number.
1462+*/
1463+#define lua_real2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
1464+
1465+/*
1466+* Note: Only 'strtod()' is part of ANSI C; others are C99 and
1467+* may need '--std=c99' compiler setting (at least on Ubuntu 7.10).
1468+*
1469+* Visual C++ 2008 Express does not have 'strtof()', nor 'strtold()'.
1470+* References to '_strtold()' exist but don't compile. It seems best
1471+* to leave Windows users with DOUBLE only (or compile with MinGW).
1472+*
1473+* In practise, using '(long double)strtod' is a risky thing, since
1474+* it will cause accuracy loss in reading in numbers, and such losses
1475+* will pile up in later processing. Get a real 'strtold()' or don't
1476+* use that mode at all.
1477+*/
1478+#ifdef LNUM_DOUBLE
1479+# define lua_str2real strtod
1480+#elif defined(LNUM_FLOAT)
1481+# define lua_str2real strtof
1482+#elif defined(LNUM_LDOUBLE)
1483+# define lua_str2real strtold
1484+#endif
1485+
1486+#define lua_integer2str(s,v) sprintf((s), LUA_INTEGER_FMT, (v))
1487+
1488+/* 's' is expected to be LUAI_MAXNUMBER2STR long (enough for any number)
1489+*/
1490+void luaO_num2buf( char *s, const TValue *o )
1491+{
1492+ lua_Number n;
1493+ lua_assert( ttisnumber(o) );
1494+
1495+ /* Reason to handle integers differently is not only speed, but accuracy as
1496+ * well. We want to make any integer tostring() without roundings, at all.
1497+ */
1498+ if (ttisint(o)) {
1499+ lua_integer2str( s, ivalue(o) );
1500+ return;
1501+ }
1502+ n= nvalue_fast(o);
1503+ lua_real2str(s, n);
1504+
1505+#ifdef LNUM_COMPLEX
1506+ lua_Number n2= nvalue_img_fast(o);
1507+ if (n2!=0) { /* Postfix with +-Ni */
1508+ int re0= (n == 0);
1509+ char *s2= re0 ? s : strchr(s,'\0');
1510+ if ((!re0) && (n2>0)) *s2++= '+';
1511+ lua_real2str( s2, n2 );
1512+ strcat(s2,"i");
1513+ }
1514+#endif
1515+}
1516+
1517+/*
1518+* If a LUA_TNUMBER has integer value, give it.
1519+*/
1520+int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref ) {
1521+ lua_Number d;
1522+ lua_Integer i;
1523+
1524+ lua_assert( ttype(o)==LUA_TNUMBER );
1525+ lua_assert( ref );
1526+#ifdef LNUM_COMPLEX
1527+ if (nvalue_img_fast(o)!=0) return 0;
1528+#endif
1529+ d= nvalue_fast(o);
1530+ lua_number2integer(i, d);
1531+ if (cast_num(i) == d) {
1532+ *ref= i; return 1;
1533+ }
1534+ return 0;
1535+}
1536+
1537+/*
1538+ * Lua 5.1.3 (using 'strtod()') allows 0x+hex but not 0+octal. This is good,
1539+ * and we should NOT use 'autobase' 0 with 'strtoul[l]()' for this reason.
1540+ *
1541+ * Lua 5.1.3 allows '0x...' numbers to overflow and lose precision; this is not
1542+ * good. On Visual C++ 2008, 'strtod()' does not even take them in. Better to
1543+ * require hex values to fit 'lua_Integer' or give an error that they don't?
1544+ *
1545+ * Full hex range (0 .. 0xff..ff) is stored as integers, not to lose any bits.
1546+ * Numerical value of 0xff..ff will be -1, if used in calculations.
1547+ *
1548+ * Returns: TK_INT for a valid integer, '*endptr_ref' updated
1549+ * TK_NUMBER for seemingly numeric, to be parsed as floating point
1550+ * 0 for bad characters, not a number (or '0x' out of range)
1551+ */
1552+static int luaO_str2i (const char *s, lua_Integer *res, char **endptr_ref) {
1553+ char *endptr;
1554+ /* 'v' gets ULONG_MAX on possible overflow (which is > LUA_INTEGER_MAX);
1555+ * we don't have to check 'errno' here.
1556+ */
1557+ unsigned LUA_INTEGER v= lua_str2ul(s, &endptr, 10);
1558+ if (endptr == s) return 0; /* nothing numeric */
1559+ if (v==0 && *endptr=='x') {
1560+ errno= 0; /* needs to be set, 'strtoul[l]' does not clear it */
1561+ v= lua_str2ul(endptr+1, &endptr, 16); /* retry as hex, unsigned range */
1562+ if (errno==ERANGE) { /* clamped to 0xff..ff */
1563+#if (defined(LNUM_INT32) && !defined(LNUM_FLOAT)) || defined(LNUM_LDOUBLE)
1564+ return TK_NUMBER; /* Allow to be read as floating point (has more integer range) */
1565+#else
1566+ return 0; /* Reject the number */
1567+#endif
1568+ }
1569+ } else if ((v > LUA_INTEGER_MAX) || (*endptr && (!isspace(*endptr)))) {
1570+ return TK_NUMBER; /* not in signed range, or has '.', 'e' etc. trailing */
1571+ }
1572+ *res= (lua_Integer)v;
1573+ *endptr_ref= endptr;
1574+ return TK_INT;
1575+}
1576+
1577+/* 0 / TK_NUMBER / TK_INT (/ TK_NUMBER2) */
1578+int luaO_str2d (const char *s, lua_Number *res_n, lua_Integer *res_i) {
1579+ char *endptr;
1580+ int ret= TK_NUMBER;
1581+ /* Check integers first, if caller is allowing.
1582+ * If 'res2'==NULL, they're only looking for floating point.
1583+ */
1584+ if (res_i) {
1585+ ret= luaO_str2i(s,res_i,&endptr);
1586+ if (ret==0) return 0;
1587+ }
1588+ if (ret==TK_NUMBER) {
1589+ lua_assert(res_n);
1590+ /* Note: Visual C++ 2008 Express 'strtod()' does not read in "0x..."
1591+ * numbers; it will read '0' and spit 'x' as endptr.
1592+ * This means hex constants not fitting in 'lua_Integer' won't
1593+ * be read in at all. What to do?
1594+ */
1595+ *res_n = lua_str2real(s, &endptr);
1596+ if (endptr == s) return 0; /* conversion failed */
1597+ /* Visual C++ 2008 'strtod()' does not allow "0x..." input. */
1598+#if defined(_MSC_VER) && !defined(LNUM_FLOAT) && !defined(LNUM_INT64)
1599+ if (*res_n==0 && *endptr=='x') {
1600+ /* Hex constant too big for 'lua_Integer' but that could fit in 'lua_Number'
1601+ * integer bits
1602+ */
1603+ unsigned __int64 v= _strtoui64( s, &endptr, 16 );
1604+ /* We just let > 64 bit values be clamped to _UI64_MAX (MSDN does not say 'errno'==ERANGE would be set) */
1605+ *res_n= cast_num(v);
1606+ if (*res_n != v) return 0; /* Would have lost accuracy */
1607+ }
1608+#endif
1609+#ifdef LNUM_COMPLEX
1610+ if (*endptr == 'i') { endptr++; ret= TK_NUMBER2; }
1611+#endif
1612+ }
1613+ if (*endptr) {
1614+ while (isspace(cast(unsigned char, *endptr))) endptr++;
1615+ if (*endptr) return 0; /* invalid trail */
1616+ }
1617+ return ret;
1618+}
1619+
1620+
1621+/* Functions for finding out, when integer operations remain in range
1622+ * (and doing them).
1623+ */
1624+int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1625+ lua_Integer v= ib+ic; /* may overflow */
1626+ if (ib>0 && ic>0) { if (v < 0) return 0; /*overflow, use floats*/ }
1627+ else if (ib<0 && ic<0) { if (v >= 0) return 0; }
1628+ *r= v;
1629+ return 1;
1630+}
1631+
1632+int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1633+ lua_Integer v= ib-ic; /* may overflow */
1634+ if (ib>=0 && ic<0) { if (v < 0) return 0; /*overflow, use floats*/ }
1635+ else if (ib<0 && ic>0) { if (v >= 0) return 0; }
1636+ *r= v;
1637+ return 1;
1638+}
1639+
1640+int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1641+ if (ib!=LUA_INTEGER_MIN && ic!=LUA_INTEGER_MIN) {
1642+ lua_Integer b= luai_abs(ib), c= luai_abs(ic);
1643+ if ( (ib==0) || (LUA_INTEGER_MAX/b >= c) ) {
1644+ *r= ib*ic; /* no overflow */
1645+ return 1;
1646+ }
1647+ } else if (ib==0 || ic==0) {
1648+ *r= 0; return 1;
1649+ }
1650+
1651+ /* Result can be LUA_INTEGER_MIN; if it is, calculating it using floating
1652+ * point will not cause accuracy loss.
1653+ */
1654+ if ( luai_nummul( cast_num(ib), cast_num(ic) ) == LUA_INTEGER_MIN ) {
1655+ *r= LUA_INTEGER_MIN;
1656+ return 1;
1657+ }
1658+ return 0;
1659+}
1660+
1661+int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1662+ /* N/0: leave to float side, to give an error
1663+ */
1664+ if (ic==0) return 0;
1665+
1666+ /* N/LUA_INTEGER_MIN: always non-integer results, or 0 or +1
1667+ */
1668+ if (ic==LUA_INTEGER_MIN) {
1669+ if (ib==LUA_INTEGER_MIN) { *r=1; return 1; }
1670+ if (ib==0) { *r=0; return 1; }
1671+
1672+ /* LUA_INTEGER_MIN (-2^31|63)/N: calculate using float side (either the division
1673+ * causes non-integer results, or there is no accuracy loss in int->fp->int
1674+ * conversions (N=2,4,8,..,256 and N=2^30,2^29,..2^23).
1675+ */
1676+ } else if (ib==LUA_INTEGER_MIN) {
1677+ lua_Number d= luai_numdiv( cast_num(LUA_INTEGER_MIN), cast_num(ic) );
1678+ lua_Integer i; lua_number2integer(i,d);
1679+ if (cast_num(i)==d) { *r= i; return 1; }
1680+
1681+ } else {
1682+ /* Note: We _can_ use ANSI C mod here, even on negative values, since
1683+ * we only test for == 0 (the sign would be implementation dependent).
1684+ */
1685+ if (ib%ic == 0) { *r= ib/ic; return 1; }
1686+ }
1687+
1688+ return 0;
1689+}
1690+
1691+int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1692+ if (ic!=0) {
1693+ /* ANSI C can be trusted when b%c==0, or when values are non-negative.
1694+ * b - (floor(b/c) * c)
1695+ * -->
1696+ * + +: b - (b/c) * c (b % c can be used)
1697+ * - -: b - (b/c) * c (b % c could work, but not defined by ANSI C)
1698+ * 0 -: b - (b/c) * c (=0, b % c could work, but not defined by ANSI C)
1699+ * - +: b - (b/c-1) * c (when b!=-c)
1700+ * + -: b - (b/c-1) * c (when b!=-c)
1701+ *
1702+ * o MIN%MIN ends up 0, via overflow in calcs but that does not matter.
1703+ * o MIN%MAX ends up MAX-1 (and other such numbers), also after overflow,
1704+ * but that does not matter, results do.
1705+ */
1706+ lua_Integer v= ib % ic;
1707+ if ( v!=0 && (ib<0 || ic<0) ) {
1708+ v= ib - ((ib/ic) - ((ib<=0 && ic<0) ? 0:1)) * ic;
1709+ }
1710+ /* Result should always have same sign as 2nd argument. (PIL2) */
1711+ lua_assert( (v<0) ? (ic<0) : (v>0) ? (ic>0) : 1 );
1712+ *r= v;
1713+ return 1;
1714+ }
1715+ return 0; /* let float side return NaN */
1716+}
1717+
1718+int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic ) {
1719+
1720+ /* In FLOAT/INT32 or FLOAT|DOUBLE/INT64 modes, calculating integer powers
1721+ * via FP realm may lose accuracy (i.e. 7^11 = 1977326743, which fits int32
1722+ * but not 23-bit float mantissa).
1723+ *
1724+ * The current solution is dumb, but it works and uses little code. Use of
1725+ * integer powers is not anticipated to be very frequent (apart from 2^x,
1726+ * which is separately optimized).
1727+ */
1728+ if (ib==0) *r=0;
1729+ else if (ic<0) return 0; /* FP realm */
1730+ else if (ib==2 && ic < (int)sizeof(lua_Integer)*8-1) *r= ((lua_Integer)1)<<ic; /* 1,2,4,...2^30 | 2^62 optimization */
1731+ else if (ic==0) *r=1;
1732+ else if (luai_abs(ib)==1) *r= (ic%2) ? ib:1;
1733+ else {
1734+ lua_Integer x= ib;
1735+ while( --ic ) {
1736+ if (!try_mulint( &x, x, ib ))
1737+ return 0; /* FP realm */
1738+ }
1739+ *r= x;
1740+ }
1741+ return 1;
1742+}
1743+
1744+int try_unmint( lua_Integer *r, lua_Integer ib ) {
1745+ /* Negating LUA_INTEGER_MIN leaves the range. */
1746+ if ( ib != LUA_INTEGER_MIN )
1747+ { *r= -ib; return 1; }
1748+ return 0;
1749+}
1750+
1751Index: lua-5.1.4/src/lnum.h
1752===================================================================
1753--- /dev/null 1970-01-01 00:00:00.000000000 +0000
1754+++ lua-5.1.4/src/lnum.h 2008-08-24 16:48:20.000000000 +0200
1755@@ -0,0 +1,116 @@
1756+/*
1757+** $Id: lnum.h,v ... $
1758+** Internal Number model
1759+** See Copyright Notice in lua.h
1760+*/
1761+
1762+#ifndef lnum_h
1763+#define lnum_h
1764+
1765+#include <math.h>
1766+
1767+#include "lobject.h"
1768+
1769+/*
1770+** The luai_num* macros define the primitive operations over 'lua_Number's
1771+** (not 'lua_Integer's, not 'lua_Complex').
1772+*/
1773+#define luai_numadd(a,b) ((a)+(b))
1774+#define luai_numsub(a,b) ((a)-(b))
1775+#define luai_nummul(a,b) ((a)*(b))
1776+#define luai_numdiv(a,b) ((a)/(b))
1777+#define luai_nummod(a,b) ((a) - _LF(floor)((a)/(b))*(b))
1778+#define luai_numpow(a,b) (_LF(pow)(a,b))
1779+#define luai_numunm(a) (-(a))
1780+#define luai_numeq(a,b) ((a)==(b))
1781+#define luai_numlt(a,b) ((a)<(b))
1782+#define luai_numle(a,b) ((a)<=(b))
1783+#define luai_numisnan(a) (!luai_numeq((a), (a)))
1784+
1785+int try_addint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1786+int try_subint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1787+int try_mulint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1788+int try_divint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1789+int try_modint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1790+int try_powint( lua_Integer *r, lua_Integer ib, lua_Integer ic );
1791+int try_unmint( lua_Integer *r, lua_Integer ib );
1792+
1793+#ifdef LNUM_COMPLEX
1794+ static inline lua_Complex luai_vectunm( lua_Complex a ) { return -a; }
1795+ static inline lua_Complex luai_vectadd( lua_Complex a, lua_Complex b ) { return a+b; }
1796+ static inline lua_Complex luai_vectsub( lua_Complex a, lua_Complex b ) { return a-b; }
1797+ static inline lua_Complex luai_vectmul( lua_Complex a, lua_Complex b ) { return a*b; }
1798+ static inline lua_Complex luai_vectdiv( lua_Complex a, lua_Complex b ) { return a/b; }
1799+
1800+/*
1801+ * C99 does not provide modulus for complex numbers. It most likely is not
1802+ * meaningful at all.
1803+ */
1804+
1805+/*
1806+ * Complex power
1807+ *
1808+ * C99 'cpow' gives inaccurate results for many common cases s.a. (1i)^2 ->
1809+ * -1+1.2246467991474e-16i (OS X 10.4, gcc 4.0.1 build 5367)
1810+ *
1811+ * [(a+bi)^(c+di)] = (r^c) * exp(-d*t) * cos(c*t + d*ln(r)) +
1812+ * = (r^c) * exp(-d*t) * sin(c*t + d*ln(r)) *i
1813+ * r = sqrt(a^2+b^2), t = arctan( b/a )
1814+ *
1815+ * Reference: <http://home.att.net/~srschmitt/complexnumbers.html>
1816+ * Could also be calculated using: x^y = exp(ln(x)*y)
1817+ *
1818+ * Note: Defined here (and not in .c) so 'lmathlib.c' can share the
1819+ * implementation.
1820+ */
1821+ static inline
1822+ lua_Complex luai_vectpow( lua_Complex a, lua_Complex b )
1823+ {
1824+# if 1
1825+ lua_Number ar= _LF(creal)(a), ai= _LF(cimag)(a);
1826+ lua_Number br= _LF(creal)(b), bi= _LF(cimag)(b);
1827+
1828+ if (ai==0 && bi==0) { /* a^c (real) */
1829+ return luai_numpow( ar, br );
1830+ }
1831+
1832+ int br_int= (int)br;
1833+
1834+ if ( ai!=0 && bi==0 && br_int==br && br_int!=0 && br_int!=INT_MIN ) {
1835+ /* (a+bi)^N, N = { +-1,+-2, ... +-INT_MAX }
1836+ */
1837+ lua_Number k= luai_numpow( _LF(sqrt) (ar*ar + ai*ai), br );
1838+ lua_Number cos_z, sin_z;
1839+
1840+ /* Situation depends upon c (N) in the following manner:
1841+ *
1842+ * N%4==0 => 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
1844+ * N%4==2 or N%4==-2 => cos(c*t)=-1, sin(c*t)=0
1845+ * (N*sign(b))%4==-1 or (N*sign(b))%4==3 => cos(c*t)=0, sin(c*t)=-1
1846+ */
1847+ int br_int_abs = br_int<0 ? -br_int:br_int;
1848+
1849+ switch( (br_int_abs%4) * (br_int<0 ? -1:1) * (ai<0 ? -1:1) ) {
1850+ case 0: cos_z=1, sin_z=0; break;
1851+ case 2: case -2: cos_z=-1, sin_z=0; break;
1852+ case 1: case -3: cos_z=0, sin_z=1; break;
1853+ case 3: case -1: cos_z=0, sin_z=-1; break;
1854+ default: lua_assert(0); return 0;
1855+ }
1856+ return k*cos_z + (k*sin_z)*I;
1857+ }
1858+# endif
1859+ return _LF(cpow) ( a, b );
1860+ }
1861+#endif
1862+
1863+LUAI_FUNC int luaO_str2d (const char *s, lua_Number *res1, lua_Integer *res2);
1864+LUAI_FUNC void luaO_num2buf( char *s, const TValue *o );
1865+
1866+LUAI_FUNC int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref );
1867+
1868+#define luai_normalize(o) \
1869+{ lua_Integer _i; if (tt_integer_valued(o,&_i)) setivalue(o,_i); }
1870+
1871+#endif
1872Index: lua-5.1.4/src/lnum_config.h
1873===================================================================
1874--- /dev/null 1970-01-01 00:00:00.000000000 +0000
1875+++ lua-5.1.4/src/lnum_config.h 2008-08-24 16:48:20.000000000 +0200
1876@@ -0,0 +1,221 @@
1877+/*
1878+** $Id: lnum_config.h,v ... $
1879+** Internal Number model
1880+** See Copyright Notice in lua.h
1881+*/
1882+
1883+#ifndef lnum_config_h
1884+#define lnum_config_h
1885+
1886+/*
1887+** Default number modes
1888+*/
1889+#if (!defined LNUM_DOUBLE) && (!defined LNUM_FLOAT) && (!defined LNUM_LDOUBLE)
1890+# define LNUM_FLOAT
1891+#endif
1892+#if (!defined LNUM_INT16) && (!defined LNUM_INT32) && (!defined LNUM_INT64)
1893+# define LNUM_INT32
1894+#endif
1895+
1896+/*
1897+** Require C99 mode for COMPLEX, FLOAT and LDOUBLE (only DOUBLE is ANSI C).
1898+*/
1899+#if defined(LNUM_COMPLEX) && (__STDC_VERSION__ < 199901L)
1900+# error "Need C99 for complex (use '--std=c99' or similar)"
1901+#elif defined(LNUM_LDOUBLE) && (__STDC_VERSION__ < 199901L) && !defined(_MSC_VER)
1902+# error "Need C99 for 'long double' (use '--std=c99' or similar)"
1903+#elif defined(LNUM_FLOAT) && (__STDC_VERSION__ < 199901L)
1904+/* LNUM_FLOAT not supported on Windows */
1905+# error "Need C99 for 'float' (use '--std=c99' or similar)"
1906+#endif
1907+
1908+/*
1909+** Number mode identifier to accompany the version string.
1910+*/
1911+#ifdef LNUM_COMPLEX
1912+# define _LNUM1 "complex "
1913+#else
1914+# define _LNUM1 ""
1915+#endif
1916+#ifdef LNUM_DOUBLE
1917+# define _LNUM2 "double"
1918+#elif defined(LNUM_FLOAT)
1919+# define _LNUM2 "float"
1920+#elif defined(LNUM_LDOUBLE)
1921+# define _LNUM2 "ldouble"
1922+#endif
1923+#ifdef LNUM_INT32
1924+# define _LNUM3 "int32"
1925+#elif defined(LNUM_INT64)
1926+# define _LNUM3 "int64"
1927+#elif defined(LNUM_INT16)
1928+# define _LNUM3 "int16"
1929+#endif
1930+#define LUA_LNUM _LNUM1 _LNUM2 " " _LNUM3
1931+
1932+/*
1933+** LUA_NUMBER is the type of floating point number in Lua
1934+** LUA_NUMBER_SCAN is the format for reading numbers.
1935+** LUA_NUMBER_FMT is the format for writing numbers.
1936+*/
1937+#ifdef LNUM_FLOAT
1938+# define LUA_NUMBER float
1939+# define LUA_NUMBER_SCAN "%f"
1940+# define LUA_NUMBER_FMT "%g"
1941+#elif (defined LNUM_DOUBLE)
1942+# define LUA_NUMBER double
1943+# define LUA_NUMBER_SCAN "%lf"
1944+# define LUA_NUMBER_FMT "%.14g"
1945+#elif (defined LNUM_LDOUBLE)
1946+# define LUA_NUMBER long double
1947+# define LUA_NUMBER_SCAN "%Lg"
1948+# define LUA_NUMBER_FMT "%.20Lg"
1949+#endif
1950+
1951+
1952+/*
1953+** LUAI_MAXNUMBER2STR: size of a buffer fitting any number->string result.
1954+**
1955+** double: 24 (sign, x.xxxxxxxxxxxxxxe+nnnn, and \0)
1956+** int64: 21 (19 digits, sign, and \0)
1957+** long double: 43 for 128-bit (sign, x.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
1958+** 30 for 80-bit (sign, x.xxxxxxxxxxxxxxxxxxxxe+nnnn, and \0)
1959+*/
1960+#ifdef LNUM_LDOUBLE
1961+# define _LUAI_MN2S 44
1962+#else
1963+# define _LUAI_MN2S 24
1964+#endif
1965+
1966+#ifdef LNUM_COMPLEX
1967+# define LUAI_MAXNUMBER2STR (2*_LUAI_MN2S)
1968+#else
1969+# define LUAI_MAXNUMBER2STR _LUAI_MN2S
1970+#endif
1971+
1972+/*
1973+** LUA_INTEGER is the integer type used by lua_pushinteger/lua_tointeger/lua_isinteger.
1974+** LUA_INTEGER_SCAN is the format for reading integers
1975+** LUA_INTEGER_FMT is the format for writing integers
1976+**
1977+** Note: Visual C++ 2005 does not have 'strtoull()', use '_strtoui64()' instead.
1978+*/
1979+#ifdef LNUM_INT32
1980+# if LUAI_BITSINT > 16
1981+# define LUA_INTEGER int
1982+# define LUA_INTEGER_SCAN "%d"
1983+# define LUA_INTEGER_FMT "%d"
1984+# else
1985+/* Note: 'LUA_INTEGER' being 'ptrdiff_t' (as in Lua 5.1) causes problems with
1986+ * 'printf()' operations. Also 'unsigned ptrdiff_t' is invalid.
1987+ */
1988+# define LUA_INTEGER long
1989+# define LUA_INTEGER_SCAN "%ld"
1990+# define LUA_INTEGER_FMT "%ld"
1991+# endif
1992+# define LUA_INTEGER_MAX 0x7FFFFFFF /* 2^31-1 */
1993+/* */
1994+#elif defined(LNUM_INT64)
1995+# define LUA_INTEGER long long
1996+# ifdef _MSC_VER
1997+# define lua_str2ul _strtoui64
1998+# else
1999+# define lua_str2ul strtoull
2000+# endif
2001+# define LUA_INTEGER_SCAN "%lld"
2002+# define LUA_INTEGER_FMT "%lld"
2003+# define LUA_INTEGER_MAX 0x7fffffffffffffffLL /* 2^63-1 */
2004+# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX - 1LL) /* -2^63 */
2005+/* */
2006+#elif defined(LNUM_INT16)
2007+# if LUAI_BITSINT > 16
2008+# define LUA_INTEGER short
2009+# define LUA_INTEGER_SCAN "%hd"
2010+# define LUA_INTEGER_FMT "%hd"
2011+# else
2012+# define LUA_INTEGER int
2013+# define LUA_INTEGER_SCAN "%d"
2014+# define LUA_INTEGER_FMT "%d"
2015+# endif
2016+# define LUA_INTEGER_MAX 0x7FFF /* 2^16-1 */
2017+#endif
2018+
2019+#ifndef lua_str2ul
2020+# define lua_str2ul (unsigned LUA_INTEGER)strtoul
2021+#endif
2022+#ifndef LUA_INTEGER_MIN
2023+# define LUA_INTEGER_MIN (-LUA_INTEGER_MAX -1) /* -2^16|32 */
2024+#endif
2025+
2026+/*
2027+@@ lua_number2int is a macro to convert lua_Number to int.
2028+@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
2029+** CHANGE them if you know a faster way to convert a lua_Number to
2030+** int (with any rounding method and without throwing errors) in your
2031+** system. In Pentium machines, a naive typecast from double to int
2032+** in C is extremely slow, so any alternative is worth trying.
2033+*/
2034+
2035+/* On a Pentium, resort to a trick */
2036+#if defined(LNUM_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
2037+ (defined(__i386) || defined (_M_IX86) || defined(__i386__))
2038+
2039+/* On a Microsoft compiler, use assembler */
2040+# if defined(_MSC_VER)
2041+# define lua_number2int(i,d) __asm fld d __asm fistp i
2042+# else
2043+
2044+/* the next trick should work on any Pentium, but sometimes clashes
2045+ with a DirectX idiosyncrasy */
2046+union luai_Cast { double l_d; long l_l; };
2047+# define lua_number2int(i,d) \
2048+ { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
2049+# endif
2050+
2051+# ifndef LNUM_INT64
2052+# define lua_number2integer lua_number2int
2053+# endif
2054+
2055+/* this option always works, but may be slow */
2056+#else
2057+# define lua_number2int(i,d) ((i)=(int)(d))
2058+#endif
2059+
2060+/* Note: Some compilers (OS X gcc 4.0?) may choke on double->long long conversion
2061+ * since it can lose precision. Others do require 'long long' there.
2062+ */
2063+#ifndef lua_number2integer
2064+# define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
2065+#endif
2066+
2067+/*
2068+** 'luai_abs()' to give absolute value of 'lua_Integer'
2069+*/
2070+#ifdef LNUM_INT32
2071+# define luai_abs abs
2072+#elif defined(LNUM_INT64) && (__STDC_VERSION__ >= 199901L)
2073+# define luai_abs llabs
2074+#else
2075+# define luai_abs(v) ((v) >= 0 ? (v) : -(v))
2076+#endif
2077+
2078+/*
2079+** LUAI_UACNUMBER is the result of an 'usual argument conversion' over a number.
2080+** LUAI_UACINTEGER the same, over an integer.
2081+*/
2082+#define LUAI_UACNUMBER double
2083+#define LUAI_UACINTEGER long
2084+
2085+/* ANSI C only has math funcs for 'double. C99 required for float and long double
2086+ * variants.
2087+ */
2088+#ifdef LNUM_DOUBLE
2089+# define _LF(name) name
2090+#elif defined(LNUM_FLOAT)
2091+# define _LF(name) name ## f
2092+#elif defined(LNUM_LDOUBLE)
2093+# define _LF(name) name ## l
2094+#endif
2095+
2096+#endif
2097+
2098Index: lua-5.1.4/src/lobject.c
2099===================================================================
2100--- lua-5.1.4.orig/src/lobject.c 2008-08-24 16:46:37.000000000 +0200
2101+++ lua-5.1.4/src/lobject.c 2008-08-24 16:48:20.000000000 +0200
2102@@ -21,7 +21,8 @@
2103 #include "lstate.h"
2104 #include "lstring.h"
2105 #include "lvm.h"
2106-
2107+#include "llex.h"
2108+#include "lnum.h"
2109 
2110 
2111 const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
2112@@ -70,12 +71,31 @@
2113 
2114 
2115 int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
2116- if (ttype(t1) != ttype(t2)) return 0;
2117+ if (!ttype_ext_same(t1,t2)) return 0;
2118   else switch (ttype(t1)) {
2119     case LUA_TNIL:
2120       return 1;
2121+ case LUA_TINT:
2122+ if (ttype(t2)==LUA_TINT)
2123+ return ivalue(t1) == ivalue(t2);
2124+ else { /* t1:int, t2:num */
2125+#ifdef LNUM_COMPLEX
2126+ if (nvalue_img_fast(t2) != 0) return 0;
2127+#endif
2128+ /* Avoid doing accuracy losing cast, if possible. */
2129+ lua_Integer tmp;
2130+ if (tt_integer_valued(t2,&tmp))
2131+ return ivalue(t1) == tmp;
2132+ else
2133+ return luai_numeq( cast_num(ivalue(t1)), nvalue_fast(t2) );
2134+ }
2135     case LUA_TNUMBER:
2136- return luai_numeq(nvalue(t1), nvalue(t2));
2137+ if (ttype(t2)==LUA_TINT)
2138+ return luaO_rawequalObj(t2, t1); /* swap LUA_TINT to left */
2139+#ifdef LNUM_COMPLEX
2140+ if (!luai_numeq(nvalue_img_fast(t1), nvalue_img_fast(t2))) return 0;
2141+#endif
2142+ return luai_numeq(nvalue_fast(t1), nvalue_fast(t2));
2143     case LUA_TBOOLEAN:
2144       return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
2145     case LUA_TLIGHTUSERDATA:
2146@@ -86,21 +106,6 @@
2147   }
2148 }
2149 
2150-
2151-int luaO_str2d (const char *s, lua_Number *result) {
2152- char *endptr;
2153- *result = lua_str2number(s, &endptr);
2154- if (endptr == s) return 0; /* conversion failed */
2155- if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
2156- *result = cast_num(strtoul(s, &endptr, 16));
2157- if (*endptr == '\0') return 1; /* most common case */
2158- while (isspace(cast(unsigned char, *endptr))) endptr++;
2159- if (*endptr != '\0') return 0; /* invalid trailing characters? */
2160- return 1;
2161-}
2162-
2163-
2164-
2165 static void pushstr (lua_State *L, const char *str) {
2166   setsvalue2s(L, L->top, luaS_new(L, str));
2167   incr_top(L);
2168@@ -131,7 +136,11 @@
2169         break;
2170       }
2171       case 'd': {
2172- setnvalue(L->top, cast_num(va_arg(argp, int)));
2173+ /* This is tricky for 64-bit integers; maybe they even cannot be
2174+ * supported on all compilers; depends on the conversions applied to
2175+ * variable argument lists. TBD: test!
2176+ */
2177+ setivalue(L->top, (lua_Integer) va_arg(argp, l_uacInteger));
2178         incr_top(L);
2179         break;
2180       }
2181@@ -212,3 +221,4 @@
2182     }
2183   }
2184 }
2185+
2186Index: lua-5.1.4/src/lobject.h
2187===================================================================
2188--- lua-5.1.4.orig/src/lobject.h 2008-08-24 16:46:37.000000000 +0200
2189+++ lua-5.1.4/src/lobject.h 2008-08-24 16:48:20.000000000 +0200
2190@@ -17,7 +17,11 @@
2191 
2192 
2193 /* tags for values visible from Lua */
2194-#define LAST_TAG LUA_TTHREAD
2195+#if LUA_TINT > LUA_TTHREAD
2196+# define LAST_TAG LUA_TINT
2197+#else
2198+# define LAST_TAG LUA_TTHREAD
2199+#endif
2200 
2201 #define NUM_TAGS (LAST_TAG+1)
2202 
2203@@ -59,7 +63,12 @@
2204 typedef union {
2205   GCObject *gc;
2206   void *p;
2207+#ifdef LNUM_COMPLEX
2208+ lua_Complex n;
2209+#else
2210   lua_Number n;
2211+#endif
2212+ lua_Integer i;
2213   int b;
2214 } Value;
2215 
2216@@ -77,7 +86,11 @@
2217 
2218 /* Macros to test type */
2219 #define ttisnil(o) (ttype(o) == LUA_TNIL)
2220-#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
2221+#define ttisint(o) (ttype(o) == LUA_TINT)
2222+#define ttisnumber(o) ((ttype(o) == LUA_TINT) || (ttype(o) == LUA_TNUMBER))
2223+#ifdef LNUM_COMPLEX
2224+# define ttiscomplex(o) ((ttype(o) == LUA_TNUMBER) && (nvalue_img_fast(o)!=0))
2225+#endif
2226 #define ttisstring(o) (ttype(o) == LUA_TSTRING)
2227 #define ttistable(o) (ttype(o) == LUA_TTABLE)
2228 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
2229@@ -90,7 +103,25 @@
2230 #define ttype(o) ((o)->tt)
2231 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
2232 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
2233-#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
2234+
2235+#define ttype_ext(o) ( ttype(o) == LUA_TINT ? LUA_TNUMBER : ttype(o) )
2236+#define ttype_ext_same(o1,o2) ( (ttype(o1)==ttype(o2)) || (ttisnumber(o1) && ttisnumber(o2)) )
2237+
2238+/* '_fast' variants are for cases where 'ttype(o)' is known to be LUA_TNUMBER.
2239+ */
2240+#ifdef LNUM_COMPLEX
2241+# define nvalue_complex_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
2242+# define nvalue_fast(o) ( _LF(creal) ( nvalue_complex_fast(o) ) )
2243+# define nvalue_img_fast(o) ( _LF(cimag) ( nvalue_complex_fast(o) ) )
2244+# define nvalue_complex(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? (o)->value.i : (o)->value.n )
2245+# define nvalue_img(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? 0 : _LF(cimag)( (o)->value.n ) )
2246+# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : _LF(creal)((o)->value.n) )
2247+#else
2248+# define nvalue(o) check_exp( ttisnumber(o), (ttype(o)==LUA_TINT) ? cast_num((o)->value.i) : (o)->value.n )
2249+# define nvalue_fast(o) check_exp( ttype(o)==LUA_TNUMBER, (o)->value.n )
2250+#endif
2251+#define ivalue(o) check_exp( ttype(o)==LUA_TINT, (o)->value.i )
2252+
2253 #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
2254 #define tsvalue(o) (&rawtsvalue(o)->tsv)
2255 #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
2256@@ -116,8 +147,27 @@
2257 /* Macros to set values */
2258 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
2259 
2260-#define setnvalue(obj,x) \
2261- { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
2262+/* Must not have side effects, 'x' may be expression.
2263+*/
2264+#define setivalue(obj,x) \
2265+ { TValue *i_o=(obj); i_o->value.i=(x); i_o->tt=LUA_TINT; }
2266+
2267+# define setnvalue(obj,x) \
2268+ { TValue *i_o=(obj); i_o->value.n= (x); i_o->tt=LUA_TNUMBER; }
2269+
2270+/* Note: Complex always has "inline", both are C99.
2271+*/
2272+#ifdef LNUM_COMPLEX
2273+ static inline void setnvalue_complex_fast( TValue *obj, lua_Complex x ) {
2274+ lua_assert( _LF(cimag)(x) != 0 );
2275+ obj->value.n= x; obj->tt= LUA_TNUMBER;
2276+ }
2277+ static inline void setnvalue_complex( TValue *obj, lua_Complex x ) {
2278+ if (_LF(cimag)(x) == 0) { setnvalue(obj, _LF(creal)(x)); }
2279+ else { obj->value.n= x; obj->tt= LUA_TNUMBER; }
2280+ }
2281+#endif
2282+
2283 
2284 #define setpvalue(obj,x) \
2285   { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
2286@@ -155,9 +205,6 @@
2287     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
2288     checkliveness(G(L),i_o); }
2289 
2290-
2291-
2292-
2293 #define setobj(L,obj1,obj2) \
2294   { const TValue *o2=(obj2); TValue *o1=(obj1); \
2295     o1->value = o2->value; o1->tt=o2->tt; \
2296@@ -185,8 +232,11 @@
2297 
2298 #define setttype(obj, tt) (ttype(obj) = (tt))
2299 
2300-
2301-#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
2302+#if LUA_TINT >= LUA_TSTRING
2303+# define iscollectable(o) ((ttype(o) >= LUA_TSTRING) && (ttype(o) != LUA_TINT))
2304+#else
2305+# define iscollectable(o) (ttype(o) >= LUA_TSTRING)
2306+#endif
2307 
2308 
2309 
2310@@ -370,12 +420,10 @@
2311 LUAI_FUNC int luaO_int2fb (unsigned int x);
2312 LUAI_FUNC int luaO_fb2int (int x);
2313 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
2314-LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
2315 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
2316                                                        va_list argp);
2317 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
2318 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
2319 
2320-
2321 #endif
2322 
2323Index: lua-5.1.4/src/loslib.c
2324===================================================================
2325--- lua-5.1.4.orig/src/loslib.c 2008-08-24 16:46:37.000000000 +0200
2326+++ lua-5.1.4/src/loslib.c 2008-08-24 16:48:20.000000000 +0200
2327@@ -186,15 +186,30 @@
2328   }
2329   if (t == (time_t)(-1))
2330     lua_pushnil(L);
2331- else
2332- lua_pushnumber(L, (lua_Number)t);
2333+ else {
2334+ /* On float systems the pushed value must be an integer, NOT a number.
2335+ * Otherwise, accuracy is lost in the time_t->float conversion.
2336+ */
2337+#ifdef LNUM_FLOAT
2338+ lua_pushinteger(L, (lua_Integer) t);
2339+#else
2340+ lua_pushnumber(L, (lua_Number) t);
2341+#endif
2342+ }
2343   return 1;
2344 }
2345 
2346 
2347 static int os_difftime (lua_State *L) {
2348+#ifdef LNUM_FLOAT
2349+ lua_Integer i= (lua_Integer)
2350+ difftime( (time_t)(luaL_checkinteger(L, 1)),
2351+ (time_t)(luaL_optinteger(L, 2, 0)));
2352+ lua_pushinteger(L, i);
2353+#else
2354   lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
2355                              (time_t)(luaL_optnumber(L, 2, 0))));
2356+#endif
2357   return 1;
2358 }
2359 
2360Index: lua-5.1.4/src/lparser.c
2361===================================================================
2362--- lua-5.1.4.orig/src/lparser.c 2008-08-24 16:46:37.000000000 +0200
2363+++ lua-5.1.4/src/lparser.c 2008-08-24 16:48:20.000000000 +0200
2364@@ -33,7 +33,6 @@
2365 
2366 #define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m)
2367 
2368-
2369 /*
2370 ** nodes for block list (list of active blocks)
2371 */
2372@@ -72,7 +71,7 @@
2373   const char *msg = (fs->f->linedefined == 0) ?
2374     luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
2375     luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
2376- fs->f->linedefined, limit, what);
2377+ (fs->f->linedefined), limit, what);
2378   luaX_lexerror(fs->ls, msg, 0);
2379 }
2380 
2381@@ -733,6 +732,18 @@
2382       v->u.nval = ls->t.seminfo.r;
2383       break;
2384     }
2385+ case TK_INT: {
2386+ init_exp(v, VKINT, 0);
2387+ v->u.ival = ls->t.seminfo.i;
2388+ break;
2389+ }
2390+#ifdef LNUM_COMPLEX
2391+ case TK_NUMBER2: {
2392+ init_exp(v, VKNUM2, 0);
2393+ v->u.nval = ls->t.seminfo.r;
2394+ break;
2395+ }
2396+#endif
2397     case TK_STRING: {
2398       codestring(ls, v, ls->t.seminfo.ts);
2399       break;
2400@@ -1079,7 +1090,7 @@
2401   if (testnext(ls, ','))
2402     exp1(ls); /* optional step */
2403   else { /* default step = 1 */
2404- luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
2405+ luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_integerK(fs, 1));
2406     luaK_reserveregs(fs, 1);
2407   }
2408   forbody(ls, base, line, 1, 1);
2409Index: lua-5.1.4/src/lparser.h
2410===================================================================
2411--- lua-5.1.4.orig/src/lparser.h 2008-08-24 16:46:37.000000000 +0200
2412+++ lua-5.1.4/src/lparser.h 2008-08-24 16:48:20.000000000 +0200
2413@@ -31,7 +31,11 @@
2414   VRELOCABLE, /* info = instruction pc */
2415   VNONRELOC, /* info = result register */
2416   VCALL, /* info = instruction pc */
2417- VVARARG /* info = instruction pc */
2418+ VVARARG, /* info = instruction pc */
2419+ VKINT /* ival = integer value */
2420+#ifdef LNUM_COMPLEX
2421+ ,VKNUM2 /* nval = imaginary value */
2422+#endif
2423 } expkind;
2424 
2425 typedef struct expdesc {
2426@@ -39,6 +43,7 @@
2427   union {
2428     struct { int info, aux; } s;
2429     lua_Number nval;
2430+ lua_Integer ival;
2431   } u;
2432   int t; /* patch list of `exit when true' */
2433   int f; /* patch list of `exit when false' */
2434Index: lua-5.1.4/src/lstrlib.c
2435===================================================================
2436--- lua-5.1.4.orig/src/lstrlib.c 2008-08-24 16:46:37.000000000 +0200
2437+++ lua-5.1.4/src/lstrlib.c 2008-08-24 16:48:20.000000000 +0200
2438@@ -43,8 +43,8 @@
2439 static int str_sub (lua_State *L) {
2440   size_t l;
2441   const char *s = luaL_checklstring(L, 1, &l);
2442- ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
2443- ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
2444+ ptrdiff_t start = posrelat(luaL_checkint32(L, 2), l);
2445+ ptrdiff_t end = posrelat(luaL_optint32(L, 3, -1), l);
2446   if (start < 1) start = 1;
2447   if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
2448   if (start <= end)
2449@@ -106,8 +106,8 @@
2450 static int str_byte (lua_State *L) {
2451   size_t l;
2452   const char *s = luaL_checklstring(L, 1, &l);
2453- ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
2454- ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
2455+ ptrdiff_t posi = posrelat(luaL_optint32(L, 2, 1), l);
2456+ ptrdiff_t pose = posrelat(luaL_optint32(L, 3, posi), l);
2457   int n, i;
2458   if (posi <= 0) posi = 1;
2459   if ((size_t)pose > l) pose = l;
2460@@ -496,7 +496,7 @@
2461   size_t l1, l2;
2462   const char *s = luaL_checklstring(L, 1, &l1);
2463   const char *p = luaL_checklstring(L, 2, &l2);
2464- ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
2465+ ptrdiff_t init = posrelat(luaL_optint32(L, 3, 1), l1) - 1;
2466   if (init < 0) init = 0;
2467   else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
2468   if (find && (lua_toboolean(L, 4) || /* explicit request? */
2469@@ -690,7 +690,7 @@
2470 ** maximum size of each format specification (such as '%-099.99d')
2471 ** (+10 accounts for %99.99x plus margin of error)
2472 */
2473-#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
2474+#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTEGER_FMT)-2 + 10)
2475 
2476 
2477 static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
2478@@ -747,9 +747,9 @@
2479 static void addintlen (char *form) {
2480   size_t l = strlen(form);
2481   char spec = form[l - 1];
2482- strcpy(form + l - 1, LUA_INTFRMLEN);
2483- form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
2484- form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
2485+ const char *tmp= LUA_INTEGER_FMT; /* "%lld" or "%ld" */
2486+ strcpy(form + l - 1, tmp+1);
2487+ form[l + sizeof(LUA_INTEGER_FMT)-4] = spec;
2488 }
2489 
2490 
2491@@ -777,12 +777,12 @@
2492         }
2493         case 'd': case 'i': {
2494           addintlen(form);
2495- sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
2496+ sprintf(buff, form, luaL_checkinteger(L, arg));
2497           break;
2498         }
2499         case 'o': case 'u': case 'x': case 'X': {
2500           addintlen(form);
2501- sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
2502+ sprintf(buff, form, (unsigned LUA_INTEGER)luaL_checkinteger(L, arg));
2503           break;
2504         }
2505         case 'e': case 'E': case 'f':
2506Index: lua-5.1.4/src/ltable.c
2507===================================================================
2508--- lua-5.1.4.orig/src/ltable.c 2008-08-24 16:46:37.000000000 +0200
2509+++ lua-5.1.4/src/ltable.c 2008-08-24 16:48:20.000000000 +0200
2510@@ -33,6 +33,7 @@
2511 #include "lobject.h"
2512 #include "lstate.h"
2513 #include "ltable.h"
2514+#include "lnum.h"
2515 
2516 
2517 /*
2518@@ -51,25 +52,15 @@
2519   
2520 #define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
2521 #define hashboolean(t,p) hashpow2(t, p)
2522-
2523+#define hashint(t,i) hashpow2(t,i)
2524 
2525 /*
2526 ** for some types, it is better to avoid modulus by power of 2, as
2527 ** they tend to have many 2 factors.
2528 */
2529 #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
2530-
2531-
2532 #define hashpointer(t,p) hashmod(t, IntPoint(p))
2533 
2534-
2535-/*
2536-** number of ints inside a lua_Number
2537-*/
2538-#define numints cast_int(sizeof(lua_Number)/sizeof(int))
2539-
2540-
2541-
2542 #define dummynode (&dummynode_)
2543 
2544 static const Node dummynode_ = {
2545@@ -80,27 +71,46 @@
2546 
2547 /*
2548 ** hash for lua_Numbers
2549+**
2550+** for non-complex modes, never called with 'lua_Integer' value range (s.a. 0)
2551 */
2552 static Node *hashnum (const Table *t, lua_Number n) {
2553- unsigned int a[numints];
2554- int i;
2555- if (luai_numeq(n, 0)) /* avoid problems with -0 */
2556- return gnode(t, 0);
2557- memcpy(a, &n, sizeof(a));
2558- for (i = 1; i < numints; i++) a[0] += a[i];
2559- return hashmod(t, a[0]);
2560+ const unsigned int *p= cast(const unsigned int *,&n);
2561+ unsigned int sum= *p;
2562+ unsigned int m= sizeof(lua_Number)/sizeof(int);
2563+ unsigned int i;
2564+ /* OS X Intel has 'm'==4 and gives "Bus error" if the last integer of
2565+ * 'n' is read; the actual size of long double is only 80 bits = 10 bytes.
2566+ * Linux x86 has 'm'==3, and does not require reduction.
2567+ */
2568+#if defined(LNUM_LDOUBLE) && defined(__i386__)
2569+ if (m>3) m--;
2570+#endif
2571+ for (i = 1; i < m; i++) sum += p[i];
2572+ return hashmod(t, sum);
2573 }
2574 
2575 
2576-
2577 /*
2578 ** returns the `main' position of an element in a table (that is, the index
2579 ** of its hash value)
2580+**
2581+** Floating point numbers with integer value give the hash position of the
2582+** integer (so they use the same table position).
2583 */
2584 static Node *mainposition (const Table *t, const TValue *key) {
2585+ lua_Integer i;
2586   switch (ttype(key)) {
2587     case LUA_TNUMBER:
2588- return hashnum(t, nvalue(key));
2589+ if (tt_integer_valued(key,&i))
2590+ return hashint(t, i);
2591+#ifdef LNUM_COMPLEX
2592+ if (nvalue_img_fast(key)!=0 && luai_numeq(nvalue_fast(key),0))
2593+ return gnode(t, 0); /* 0 and -0 to give same hash */
2594+#endif
2595+ return hashnum(t, nvalue_fast(key));
2596+ case LUA_TINT:
2597+ return hashint(t, ivalue(key));
2598     case LUA_TSTRING:
2599       return hashstr(t, rawtsvalue(key));
2600     case LUA_TBOOLEAN:
2601@@ -116,16 +126,20 @@
2602 /*
2603 ** returns the index for `key' if `key' is an appropriate key to live in
2604 ** the array part of the table, -1 otherwise.
2605+**
2606+** Anything <=0 is taken as not being in the array part.
2607 */
2608-static int arrayindex (const TValue *key) {
2609- if (ttisnumber(key)) {
2610- lua_Number n = nvalue(key);
2611- int k;
2612- lua_number2int(k, n);
2613- if (luai_numeq(cast_num(k), n))
2614- return k;
2615+static int arrayindex (const TValue *key, int max) {
2616+ lua_Integer k;
2617+ switch( ttype(key) ) {
2618+ case LUA_TINT:
2619+ k= ivalue(key); break;
2620+ case LUA_TNUMBER:
2621+ if (tt_integer_valued(key,&k)) break;
2622+ default:
2623+ return -1; /* not to be used as array index */
2624   }
2625- return -1; /* `key' did not match some condition */
2626+ return ((k>0) && (k <= max)) ? cast_int(k) : -1;
2627 }
2628 
2629 
2630@@ -137,8 +151,8 @@
2631 static int findindex (lua_State *L, Table *t, StkId key) {
2632   int i;
2633   if (ttisnil(key)) return -1; /* first iteration */
2634- i = arrayindex(key);
2635- if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
2636+ i = arrayindex(key, t->sizearray);
2637+ if (i>0) /* inside array part? */
2638     return i-1; /* yes; that's the index (corrected to C) */
2639   else {
2640     Node *n = mainposition(t, key);
2641@@ -163,7 +177,7 @@
2642   int i = findindex(L, t, key); /* find original element */
2643   for (i++; i < t->sizearray; i++) { /* try first array part */
2644     if (!ttisnil(&t->array[i])) { /* a non-nil value? */
2645- setnvalue(key, cast_num(i+1));
2646+ setivalue(key, i+1);
2647       setobj2s(L, key+1, &t->array[i]);
2648       return 1;
2649     }
2650@@ -209,8 +223,8 @@
2651 
2652 
2653 static int countint (const TValue *key, int *nums) {
2654- int k = arrayindex(key);
2655- if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
2656+ int k = arrayindex(key,MAXASIZE);
2657+ if (k>0) { /* appropriate array index? */
2658     nums[ceillog2(k)]++; /* count as such */
2659     return 1;
2660   }
2661@@ -308,7 +322,7 @@
2662     /* re-insert elements from vanishing slice */
2663     for (i=nasize; i<oldasize; i++) {
2664       if (!ttisnil(&t->array[i]))
2665- setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
2666+ setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]);
2667     }
2668     /* shrink array */
2669     luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
2670@@ -409,7 +423,9 @@
2671     othern = mainposition(t, key2tval(mp));
2672     if (othern != mp) { /* is colliding node out of its main position? */
2673       /* yes; move colliding node into free position */
2674- while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
2675+ while (gnext(othern) != mp) {
2676+ othern = gnext(othern); /* find previous */
2677+ }
2678       gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
2679       *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
2680       gnext(mp) = NULL; /* now `mp' is free */
2681@@ -432,17 +448,18 @@
2682 /*
2683 ** search function for integers
2684 */
2685-const TValue *luaH_getnum (Table *t, int key) {
2686+const TValue *luaH_getint (Table *t, lua_Integer key) {
2687   /* (1 <= key && key <= t->sizearray) */
2688   if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
2689     return &t->array[key-1];
2690   else {
2691- lua_Number nk = cast_num(key);
2692- Node *n = hashnum(t, nk);
2693+ Node *n = hashint(t, key);
2694     do { /* check whether `key' is somewhere in the chain */
2695- if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
2696+ if (ttisint(gkey(n)) && (ivalue(gkey(n)) == key)) {
2697         return gval(n); /* that's it */
2698- else n = gnext(n);
2699+ } else {
2700+ n = gnext(n);
2701+ }
2702     } while (n);
2703     return luaO_nilobject;
2704   }
2705@@ -470,14 +487,12 @@
2706   switch (ttype(key)) {
2707     case LUA_TNIL: return luaO_nilobject;
2708     case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
2709+ case LUA_TINT: return luaH_getint(t, ivalue(key));
2710     case LUA_TNUMBER: {
2711- int k;
2712- lua_Number n = nvalue(key);
2713- lua_number2int(k, n);
2714- if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
2715- return luaH_getnum(t, k); /* use specialized version */
2716- /* else go through */
2717- }
2718+ lua_Integer i;
2719+ if (tt_integer_valued(key,&i))
2720+ return luaH_getint(t,i);
2721+ } /* pass through */
2722     default: {
2723       Node *n = mainposition(t, key);
2724       do { /* check whether `key' is somewhere in the chain */
2725@@ -498,20 +513,25 @@
2726     return cast(TValue *, p);
2727   else {
2728     if (ttisnil(key)) luaG_runerror(L, "table index is nil");
2729- else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
2730- luaG_runerror(L, "table index is NaN");
2731+ else if (ttype(key)==LUA_TNUMBER) {
2732+ lua_Integer k;
2733+ if (luai_numisnan(nvalue_fast(key)))
2734+ luaG_runerror(L, "table index is NaN");
2735+ if (tt_integer_valued(key,&k))
2736+ return luaH_setint(L, t, k);
2737+ }
2738     return newkey(L, t, key);
2739   }
2740 }
2741 
2742 
2743-TValue *luaH_setnum (lua_State *L, Table *t, int key) {
2744- const TValue *p = luaH_getnum(t, key);
2745+TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key) {
2746+ const TValue *p = luaH_getint(t, key);
2747   if (p != luaO_nilobject)
2748     return cast(TValue *, p);
2749   else {
2750     TValue k;
2751- setnvalue(&k, cast_num(key));
2752+ setivalue(&k, key);
2753     return newkey(L, t, &k);
2754   }
2755 }
2756@@ -533,20 +553,21 @@
2757   unsigned int i = j; /* i is zero or a present index */
2758   j++;
2759   /* find `i' and `j' such that i is present and j is not */
2760- while (!ttisnil(luaH_getnum(t, j))) {
2761+ while (!ttisnil(luaH_getint(t, j))) {
2762     i = j;
2763     j *= 2;
2764     if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
2765       /* table was built with bad purposes: resort to linear search */
2766- i = 1;
2767- while (!ttisnil(luaH_getnum(t, i))) i++;
2768- return i - 1;
2769+ for( i = 1; i<MAX_INT+1; i++ ) {
2770+ if (ttisnil(luaH_getint(t, i))) break;
2771+ }
2772+ return i - 1; /* up to MAX_INT */
2773     }
2774   }
2775   /* now do a binary search between them */
2776   while (j - i > 1) {
2777     unsigned int m = (i+j)/2;
2778- if (ttisnil(luaH_getnum(t, m))) j = m;
2779+ if (ttisnil(luaH_getint(t, m))) j = m;
2780     else i = m;
2781   }
2782   return i;
2783Index: lua-5.1.4/src/ltable.h
2784===================================================================
2785--- lua-5.1.4.orig/src/ltable.h 2008-08-24 16:46:37.000000000 +0200
2786+++ lua-5.1.4/src/ltable.h 2008-08-24 16:48:20.000000000 +0200
2787@@ -18,8 +18,8 @@
2788 #define key2tval(n) (&(n)->i_key.tvk)
2789 
2790 
2791-LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
2792-LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
2793+LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
2794+LUAI_FUNC TValue *luaH_setint (lua_State *L, Table *t, lua_Integer key);
2795 LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
2796 LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
2797 LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
2798Index: lua-5.1.4/src/ltm.c
2799===================================================================
2800--- lua-5.1.4.orig/src/ltm.c 2008-08-24 16:46:38.000000000 +0200
2801+++ lua-5.1.4/src/ltm.c 2008-08-24 16:48:20.000000000 +0200
2802@@ -19,7 +19,6 @@
2803 #include "ltm.h"
2804 
2805 
2806-
2807 const char *const luaT_typenames[] = {
2808   "nil", "boolean", "userdata", "number",
2809   "string", "table", "function", "userdata", "thread",
2810@@ -67,6 +66,9 @@
2811     case LUA_TUSERDATA:
2812       mt = uvalue(o)->metatable;
2813       break;
2814+ case LUA_TINT:
2815+ mt = G(L)->mt[LUA_TNUMBER];
2816+ break;
2817     default:
2818       mt = G(L)->mt[ttype(o)];
2819   }
2820Index: lua-5.1.4/src/lua.c
2821===================================================================
2822--- lua-5.1.4.orig/src/lua.c 2008-08-24 16:46:38.000000000 +0200
2823+++ lua-5.1.4/src/lua.c 2008-08-24 16:48:20.000000000 +0200
2824@@ -16,7 +16,7 @@
2825 
2826 #include "lauxlib.h"
2827 #include "lualib.h"
2828-
2829+#include "llimits.h"
2830 
2831 
2832 static lua_State *globalL = NULL;
2833@@ -382,6 +382,15 @@
2834     l_message(argv[0], "cannot create state: not enough memory");
2835     return EXIT_FAILURE;
2836   }
2837+ /* Checking 'sizeof(lua_Integer)' cannot be made in preprocessor on all compilers.
2838+ */
2839+#ifdef LNUM_INT16
2840+ lua_assert( sizeof(lua_Integer) == 2 );
2841+#elif defined(LNUM_INT32)
2842+ lua_assert( sizeof(lua_Integer) == 4 );
2843+#elif defined(LNUM_INT64)
2844+ lua_assert( sizeof(lua_Integer) == 8 );
2845+#endif
2846   s.argc = argc;
2847   s.argv = argv;
2848   status = lua_cpcall(L, &pmain, &s);
2849Index: lua-5.1.4/src/lua.h
2850===================================================================
2851--- lua-5.1.4.orig/src/lua.h 2008-08-24 16:46:38.000000000 +0200
2852+++ lua-5.1.4/src/lua.h 2008-08-24 16:48:20.000000000 +0200
2853@@ -19,7 +19,7 @@
2854 #define LUA_VERSION "Lua 5.1"
2855 #define LUA_RELEASE "Lua 5.1.4"
2856 #define LUA_VERSION_NUM 501
2857-#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio"
2858+#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" " (" LUA_LNUM ")"
2859 #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
2860 
2861 
2862@@ -71,6 +71,16 @@
2863 */
2864 #define LUA_TNONE (-1)
2865 
2866+/* LUA_TINT is an internal type, not visible to applications. There are three
2867+ * potential values where it can be tweaked to (code autoadjusts to these):
2868+ *
2869+ * -2: not 'usual' type value; good since 'LUA_TINT' is not part of the API
2870+ * LUA_TNUMBER+1: shifts other type values upwards, breaking binary compatibility
2871+ * not acceptable for 5.1, maybe 5.2 onwards?
2872+ * 9: greater than existing (5.1) type values.
2873+*/
2874+#define LUA_TINT (-2)
2875+
2876 #define LUA_TNIL 0
2877 #define LUA_TBOOLEAN 1
2878 #define LUA_TLIGHTUSERDATA 2
2879@@ -139,6 +149,8 @@
2880 LUA_API int (lua_type) (lua_State *L, int idx);
2881 LUA_API const char *(lua_typename) (lua_State *L, int tp);
2882 
2883+LUA_API int (lua_isinteger) (lua_State *L, int idx);
2884+
2885 LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
2886 LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
2887 LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
2888@@ -244,6 +256,19 @@
2889 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
2890 
2891 
2892+/*
2893+* It is unnecessary to break Lua C API 'lua_tonumber()' compatibility, just
2894+* because the Lua number type is complex. Most C modules would use scalars
2895+* only. We'll introduce new 'lua_tocomplex' and 'lua_pushcomplex' for when
2896+* the module really wants to use them.
2897+*/
2898+#ifdef LNUM_COMPLEX
2899+ #include <complex.h>
2900+ typedef LUA_NUMBER complex lua_Complex;
2901+ LUA_API lua_Complex (lua_tocomplex) (lua_State *L, int idx);
2902+ LUA_API void (lua_pushcomplex) (lua_State *L, lua_Complex v);
2903+#endif
2904+
2905 
2906 /*
2907 ** ===============================================================
2908@@ -268,7 +293,12 @@
2909 #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
2910 #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
2911 #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
2912-#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
2913+
2914+#if LUA_TINT < 0
2915+# define lua_isnoneornil(L, n) ( (lua_type(L,(n)) <= 0) && (lua_type(L,(n)) != LUA_TINT) )
2916+#else
2917+# define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
2918+#endif
2919 
2920 #define lua_pushliteral(L, s) \
2921     lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
2922@@ -386,3 +416,4 @@
2923 
2924 
2925 #endif
2926+
2927Index: lua-5.1.4/src/luaconf.h
2928===================================================================
2929--- lua-5.1.4.orig/src/luaconf.h 2008-08-24 16:46:38.000000000 +0200
2930+++ lua-5.1.4/src/luaconf.h 2008-08-24 16:48:20.000000000 +0200
2931@@ -10,7 +10,9 @@
2932 
2933 #include <limits.h>
2934 #include <stddef.h>
2935-
2936+#ifdef lua_assert
2937+# include <assert.h>
2938+#endif
2939 
2940 /*
2941 ** ==================================================================
2942@@ -136,14 +138,38 @@
2943 
2944 
2945 /*
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.
2952 */
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
2960+#else
2961+#error "you must define LUA_BITSINT with number of bits in an integer"
2962+#endif
2963 
2964 
2965 /*
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
2969+@@
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.
2973+**
2974+** These are kept in a separate configuration file mainly for ease of patching
2975+** (can be changed if integerated to Lua proper).
2976+*/
2977+/*#define LNUM_DOUBLE*/
2978+/*#define LNUM_INT32*/
2979+#include "lnum_config.h"
2980+
2981+/*
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 @@
2986 
2987 
2988 /*
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.
2992-*/
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
2999-#else
3000-#error "you must define LUA_BITSINT with number of bits in an integer"
3001-#endif
3002-
3003-
3004-/*
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
3010 #endif
3011 
3012+/*
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)
3015+*/
3016+#ifdef __cplusplus
3017+# define LUAI_BOOL bool
3018+#else
3019+# define LUAI_BOOL int
3020+#endif
3021 
3022 /*
3023 @@ LUAI_MAXCALLS limits the number of nested calls.
3024@@ -490,101 +509,6 @@
3025 /* }================================================================== */
3026 
3027 
3028-
3029-
3030-/*
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-** ===================================================================
3037-*/
3038-
3039-#define LUA_NUMBER_DOUBLE
3040-#define LUA_NUMBER double
3041-
3042-/*
3043-@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
3044-@* over a number.
3045-*/
3046-#define LUAI_UACNUMBER double
3047-
3048-
3049-/*
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.
3055-*/
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))
3061-
3062-
3063-/*
3064-@@ The luai_num* macros define the primitive operations over numbers.
3065-*/
3066-#if defined(LUA_CORE)
3067-#include <math.h>
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)))
3079-#endif
3080-
3081-
3082-/*
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.
3089-*/
3090-
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__))
3094-
3095-/* On a Microsoft compiler, use assembler */
3096-#if defined(_MSC_VER)
3097-
3098-#define lua_number2int(i,d) __asm fld d __asm fistp i
3099-#define lua_number2integer(i,n) lua_number2int(i, n)
3100-
3101-/* the next trick should work on any Pentium, but sometimes clashes
3102- with a DirectX idiosyncrasy */
3103-#else
3104-
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)
3109-
3110-#endif
3111-
3112-
3113-/* this option always works, but may be slow */
3114-#else
3115-#define lua_number2int(i,d) ((i)=(int)(d))
3116-#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
3117-
3118-#endif
3119-
3120-/* }================================================================== */
3121-
3122-
3123 /*
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)
3128 
3129 
3130-/*
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
3134-@* modifier.
3135-** CHANGE them if your system supports long long or does not support long.
3136-*/
3137-
3138-#if defined(LUA_USELONGLONG)
3139-
3140-#define LUA_INTFRMLEN "ll"
3141-#define LUA_INTFRM_T long long
3142-
3143-#else
3144-
3145-#define LUA_INTFRMLEN "l"
3146-#define LUA_INTFRM_T long
3147-
3148-#endif
3149-
3150-
3151-
3152 /* =================================================================== */
3153 
3154 /*
3155Index: lua-5.1.4/src/lundump.c
3156===================================================================
3157--- lua-5.1.4.orig/src/lundump.c 2008-08-24 16:46:38.000000000 +0200
3158+++ lua-5.1.4/src/lundump.c 2008-08-24 16:48:20.000000000 +0200
3159@@ -73,6 +73,13 @@
3160  return x;
3161 }
3162 
3163+static lua_Integer LoadInteger(LoadState* S)
3164+{
3165+ lua_Integer x;
3166+ LoadVar(S,x);
3167+ return x;
3168+}
3169+
3170 static TString* LoadString(LoadState* S)
3171 {
3172  size_t size;
3173@@ -119,6 +126,9 @@
3174    case LUA_TNUMBER:
3175     setnvalue(o,LoadNumber(S));
3176     break;
3177+ case LUA_TINT: /* Integer type saved in bytecode (see lcode.c) */
3178+ setivalue(o,LoadInteger(S));
3179+ break;
3180    case LUA_TSTRING:
3181     setsvalue2n(S->L,o,LoadString(S));
3182     break;
3183@@ -223,5 +233,22 @@
3184  *h++=(char)sizeof(size_t);
3185  *h++=(char)sizeof(Instruction);
3186  *h++=(char)sizeof(lua_Number);
3187- *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
3188+
3189+ /*
3190+ * Last byte of header (0/1 in unpatched Lua 5.1.3):
3191+ *
3192+ * 0: lua_Number is float or double, lua_Integer not used. (nonpatched only)
3193+ * 1: lua_Number is integer (nonpatched only)
3194+ *
3195+ * +2: LNUM_INT16: sizeof(lua_Integer)
3196+ * +4: LNUM_INT32: sizeof(lua_Integer)
3197+ * +8: LNUM_INT64: sizeof(lua_Integer)
3198+ *
3199+ * +0x80: LNUM_COMPLEX
3200+ */
3201+ *h++ = (char)(sizeof(lua_Integer)
3202+#ifdef LNUM_COMPLEX
3203+ | 0x80
3204+#endif
3205+ );
3206 }
3207Index: lua-5.1.4/src/lvm.c
3208===================================================================
3209--- lua-5.1.4.orig/src/lvm.c 2008-08-24 16:46:38.000000000 +0200
3210+++ lua-5.1.4/src/lvm.c 2008-08-24 16:48:20.000000000 +0200
3211@@ -25,22 +25,35 @@
3212 #include "ltable.h"
3213 #include "ltm.h"
3214 #include "lvm.h"
3215-
3216-
3217+#include "llex.h"
3218+#include "lnum.h"
3219 
3220 /* limit for table tag-method chains (to avoid loops) */
3221 #define MAXTAGLOOP 100
3222 
3223 
3224-const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
3225- lua_Number num;
3226+/*
3227+ * If 'obj' is a string, it is tried to be interpreted as a number.
3228+ */
3229+const TValue *luaV_tonumber ( const TValue *obj, TValue *n) {
3230+ lua_Number d;
3231+ lua_Integer i;
3232+
3233   if (ttisnumber(obj)) return obj;
3234- if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
3235- setnvalue(n, num);
3236- return n;
3237- }
3238- else
3239- return NULL;
3240+
3241+ if (ttisstring(obj)) {
3242+ switch( luaO_str2d( svalue(obj), &d, &i ) ) {
3243+ case TK_INT:
3244+ setivalue(n,i); return n;
3245+ case TK_NUMBER:
3246+ setnvalue(n,d); return n;
3247+#ifdef LNUM_COMPLEX
3248+ case TK_NUMBER2: /* "N.NNNi", != 0 */
3249+ setnvalue_complex_fast(n, d*I); return n;
3250+#endif
3251+ }
3252+ }
3253+ return NULL;
3254 }
3255 
3256 
3257@@ -49,8 +62,7 @@
3258     return 0;
3259   else {
3260     char s[LUAI_MAXNUMBER2STR];
3261- lua_Number n = nvalue(obj);
3262- lua_number2str(s, n);
3263+ luaO_num2buf(s,obj);
3264     setsvalue2s(L, obj, luaS_new(L, s));
3265     return 1;
3266   }
3267@@ -218,59 +230,127 @@
3268 }
3269 
3270 
3271+#ifdef LNUM_COMPLEX
3272+void error_complex( lua_State *L, const TValue *l, const TValue *r )
3273+{
3274+ char buf1[ LUAI_MAXNUMBER2STR ];
3275+ char buf2[ LUAI_MAXNUMBER2STR ];
3276+ luaO_num2buf( buf1, l );
3277+ luaO_num2buf( buf2, r );
3278+ luaG_runerror( L, "unable to compare: %s with %s", buf1, buf2 );
3279+ /* no return */
3280+}
3281+#endif
3282+
3283+
3284 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
3285   int res;
3286- if (ttype(l) != ttype(r))
3287+ int tl,tr;
3288+ lua_Integer tmp;
3289+
3290+ if (!ttype_ext_same(l,r))
3291     return luaG_ordererror(L, l, r);
3292- else if (ttisnumber(l))
3293- return luai_numlt(nvalue(l), nvalue(r));
3294- else if (ttisstring(l))
3295- return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
3296- else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
3297+#ifdef LNUM_COMPLEX
3298+ if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
3299+ error_complex( L, l, r );
3300+#endif
3301+ tl= ttype(l); tr= ttype(r);
3302+ if (tl==tr) { /* clear arithmetics */
3303+ switch(tl) {
3304+ case LUA_TINT: return ivalue(l) < ivalue(r);
3305+ case LUA_TNUMBER: return luai_numlt(nvalue_fast(l), nvalue_fast(r));
3306+ case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
3307+ }
3308+ } else if (tl==LUA_TINT) { /* l:int, r:num */
3309+ /* Avoid accuracy losing casts: if 'r' is integer by value, do comparisons
3310+ * in integer realm. Only otherwise cast 'l' to FP (which might change its
3311+ * value).
3312+ */
3313+ if (tt_integer_valued(r,&tmp))
3314+ return ivalue(l) < tmp;
3315+ else
3316+ return luai_numlt( cast_num(ivalue(l)), nvalue_fast(r) );
3317+
3318+ } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
3319+ if (tt_integer_valued(l,&tmp))
3320+ return tmp < ivalue(r);
3321+ else
3322+ return luai_numlt( nvalue_fast(l), cast_num(ivalue(r)) );
3323+
3324+ } else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
3325     return res;
3326+
3327   return luaG_ordererror(L, l, r);
3328 }
3329 
3330 
3331 static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
3332   int res;
3333- if (ttype(l) != ttype(r))
3334+ int tl, tr;
3335+ lua_Integer tmp;
3336+
3337+ if (!ttype_ext_same(l,r))
3338     return luaG_ordererror(L, l, r);
3339- else if (ttisnumber(l))
3340- return luai_numle(nvalue(l), nvalue(r));
3341- else if (ttisstring(l))
3342- return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
3343- else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
3344+#ifdef LNUM_COMPLEX
3345+ if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
3346+ error_complex( L, l, r );
3347+#endif
3348+ tl= ttype(l); tr= ttype(r);
3349+ if (tl==tr) { /* clear arithmetics */
3350+ switch(tl) {
3351+ case LUA_TINT: return ivalue(l) <= ivalue(r);
3352+ case LUA_TNUMBER: return luai_numle(nvalue_fast(l), nvalue_fast(r));
3353+ case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
3354+ }
3355+ }
3356+ if (tl==LUA_TINT) { /* l:int, r:num */
3357+ if (tt_integer_valued(r,&tmp))
3358+ return ivalue(l) <= tmp;
3359+ else
3360+ return luai_numle( cast_num(ivalue(l)), nvalue_fast(r) );
3361+
3362+ } else if (tl==LUA_TNUMBER) { /* l:num, r:int */
3363+ if (tt_integer_valued(l,&tmp))
3364+ return tmp <= ivalue(r);
3365+ else
3366+ return luai_numle( nvalue_fast(l), cast_num(ivalue(r)) );
3367+
3368+ } else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
3369     return res;
3370   else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
3371     return !res;
3372+
3373   return luaG_ordererror(L, l, r);
3374 }
3375 
3376 
3377-int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
3378+/* Note: 'luaV_equalval()' and 'luaO_rawequalObj()' have largely overlapping
3379+ * implementation. LUA_TNIL..LUA_TLIGHTUSERDATA cases could be handled
3380+ * simply by the 'default' case here.
3381+ */
3382+int luaV_equalval (lua_State *L, const TValue *l, const TValue *r) {
3383   const TValue *tm;
3384- lua_assert(ttype(t1) == ttype(t2));
3385- switch (ttype(t1)) {
3386+ lua_assert(ttype_ext_same(l,r));
3387+ switch (ttype(l)) {
3388     case LUA_TNIL: return 1;
3389- case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
3390- case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
3391- case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
3392+ case LUA_TINT:
3393+ case LUA_TNUMBER: return luaO_rawequalObj(l,r);
3394+ case LUA_TBOOLEAN: return bvalue(l) == bvalue(r); /* true must be 1 !! */
3395+ case LUA_TLIGHTUSERDATA: return pvalue(l) == pvalue(r);
3396     case LUA_TUSERDATA: {
3397- if (uvalue(t1) == uvalue(t2)) return 1;
3398- tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
3399- TM_EQ);
3400+ if (uvalue(l) == uvalue(r)) return 1;
3401+ tm = get_compTM(L, uvalue(l)->metatable, uvalue(r)->metatable, TM_EQ);
3402       break; /* will try TM */
3403     }
3404     case LUA_TTABLE: {
3405- if (hvalue(t1) == hvalue(t2)) return 1;
3406- tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
3407+ if (hvalue(l) == hvalue(r)) return 1;
3408+ tm = get_compTM(L, hvalue(l)->metatable, hvalue(r)->metatable, TM_EQ);
3409       break; /* will try TM */
3410     }
3411- default: return gcvalue(t1) == gcvalue(t2);
3412+ default: return gcvalue(l) == gcvalue(r);
3413   }
3414   if (tm == NULL) return 0; /* no TM? */
3415- callTMres(L, L->top, tm, t1, t2); /* call TM */
3416+ callTMres(L, L->top, tm, l, r); /* call TM */
3417   return !l_isfalse(L->top);
3418 }
3419 
3420@@ -310,30 +390,6 @@
3421 }
3422 
3423 
3424-static void Arith (lua_State *L, StkId ra, const TValue *rb,
3425- const TValue *rc, TMS op) {
3426- TValue tempb, tempc;
3427- const TValue *b, *c;
3428- if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
3429- (c = luaV_tonumber(rc, &tempc)) != NULL) {
3430- lua_Number nb = nvalue(b), nc = nvalue(c);
3431- switch (op) {
3432- case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
3433- case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
3434- case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
3435- case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
3436- case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
3437- case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
3438- case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
3439- default: lua_assert(0); break;
3440- }
3441- }
3442- else if (!call_binTM(L, rb, rc, ra, op))
3443- luaG_aritherror(L, rb, rc);
3444-}
3445-
3446-
3447-
3448 /*
3449 ** some macros for common tasks in `luaV_execute'
3450 */
3451@@ -357,17 +413,154 @@
3452 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
3453 
3454 
3455-#define arith_op(op,tm) { \
3456- TValue *rb = RKB(i); \
3457- TValue *rc = RKC(i); \
3458- if (ttisnumber(rb) && ttisnumber(rc)) { \
3459- lua_Number nb = nvalue(rb), nc = nvalue(rc); \
3460- setnvalue(ra, op(nb, nc)); \
3461- } \
3462- else \
3463- Protect(Arith(L, ra, rb, rc, tm)); \
3464+/* Note: if called for unary operations, 'rc'=='rb'.
3465+ */
3466+static void Arith (lua_State *L, StkId ra, const TValue *rb,
3467+ const TValue *rc, TMS op) {
3468+ TValue tempb, tempc;
3469+ const TValue *b, *c;
3470+ lua_Number nb,nc;
3471+
3472+ if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
3473+ (c = luaV_tonumber(rc, &tempc)) != NULL) {
3474+
3475+ /* Keep integer arithmetics in the integer realm, if possible.
3476+ */
3477+ if (ttisint(b) && ttisint(c)) {
3478+ lua_Integer ib = ivalue(b), ic = ivalue(c);
3479+ lua_Integer *ri = &ra->value.i;
3480+ ra->tt= LUA_TINT; /* part of 'setivalue(ra)' */
3481+ switch (op) {
3482+ case TM_ADD: if (try_addint( ri, ib, ic)) return; break;
3483+ case TM_SUB: if (try_subint( ri, ib, ic)) return; break;
3484+ case TM_MUL: if (try_mulint( ri, ib, ic)) return; break;
3485+ case TM_DIV: if (try_divint( ri, ib, ic)) return; break;
3486+ case TM_MOD: if (try_modint( ri, ib, ic)) return; break;
3487+ case TM_POW: if (try_powint( ri, ib, ic)) return; break;
3488+ case TM_UNM: if (try_unmint( ri, ib)) return; break;
3489+ default: lua_assert(0);
3490+ }
3491+ }
3492+ /* Fallback to floating point, when leaving range. */
3493+
3494+#ifdef LNUM_COMPLEX
3495+ if ((nvalue_img(b)!=0) || (nvalue_img(c)!=0)) {
3496+ lua_Complex r;
3497+ if (op==TM_UNM) {
3498+ r= -nvalue_complex_fast(b); /* never an integer (or scalar) */
3499+ setnvalue_complex_fast( ra, r );
3500+ } else {
3501+ lua_Complex bb= nvalue_complex(b), cc= nvalue_complex(c);
3502+ switch (op) {
3503+ case TM_ADD: r= bb + cc; break;
3504+ case TM_SUB: r= bb - cc; break;
3505+ case TM_MUL: r= bb * cc; break;
3506+ case TM_DIV: r= bb / cc; break;
3507+ case TM_MOD:
3508+ luaG_runerror(L, "attempt to use %% on complex numbers"); /* no return */
3509+ case TM_POW: r= luai_vectpow( bb, cc ); break;
3510+ default: lua_assert(0); r=0;
3511+ }
3512+ setnvalue_complex( ra, r );
3513       }
3514+ return;
3515+ }
3516+#endif
3517+ nb = nvalue(b); nc = nvalue(c);
3518+ switch (op) {
3519+ case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); return;
3520+ case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); return;
3521+ case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); return;
3522+ case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); return;
3523+ case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); return;
3524+ case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); return;
3525+ case TM_UNM: setnvalue(ra, luai_numunm(nb)); return;
3526+ default: lua_assert(0);
3527+ }
3528+ }
3529+
3530+ /* Either operand not a number */
3531+ if (!call_binTM(L, rb, rc, ra, op))
3532+ luaG_aritherror(L, rb, rc);
3533+}
3534 
3535+/* Helper macro to sort arithmetic operations into four categories:
3536+ * TK_INT: integer - integer operands
3537+ * TK_NUMBER: number - number (non complex, either may be integer)
3538+ * TK_NUMBER2: complex numbers (at least the other)
3539+ * 0: non-numeric (at least the other)
3540+*/
3541+#ifdef LNUM_COMPLEX
3542+static inline int arith_mode( const TValue *rb, const TValue *rc ) {
3543+ if (ttisint(rb) && ttisint(rc)) return TK_INT;
3544+ if (ttiscomplex(rb) || ttiscomplex(rc)) return TK_NUMBER2;
3545+ if (ttisnumber(rb) && ttisnumber(rc)) return TK_NUMBER;
3546+ return 0;
3547+}
3548+#else
3549+# define arith_mode(rb,rc) \
3550+ ( (ttisint(rb) && ttisint(rc)) ? TK_INT : \
3551+ (ttisnumber(rb) && ttisnumber(rc)) ? TK_NUMBER : 0 )
3552+#endif
3553+
3554+/* arith_op macro for two operators:
3555+ * automatically chooses, which function (number, integer, complex) to use
3556+ */
3557+#define ARITH_OP2_START( op_num, op_int ) \
3558+ int failed= 0; \
3559+ switch( arith_mode(rb,rc) ) { \
3560+ case TK_INT: \
3561+ if (op_int ( &(ra)->value.i, ivalue(rb), ivalue(rc) )) \
3562+ { ra->tt= LUA_TINT; break; } /* else flow through */ \
3563+ case TK_NUMBER: \
3564+ setnvalue(ra, op_num ( nvalue(rb), nvalue(rc) )); break;
3565+
3566+#define ARITH_OP2_END \
3567+ default: \
3568+ failed= 1; break; \
3569+ } if (!failed) continue;
3570+
3571+#define arith_op_continue_scalar( op_num, op_int ) \
3572+ ARITH_OP2_START( op_num, op_int ) \
3573+ ARITH_OP2_END
3574+
3575+#ifdef LNUM_COMPLEX
3576+# define arith_op_continue( op_num, op_int, op_complex ) \
3577+ ARITH_OP2_START( op_num, op_int ) \
3578+ case TK_NUMBER2: \
3579+ setnvalue_complex( ra, op_complex ( nvalue_complex(rb), nvalue_complex(rc) ) ); break; \
3580+ ARITH_OP2_END
3581+#else
3582+# define arith_op_continue(op_num,op_int,_) arith_op_continue_scalar(op_num,op_int)
3583+#endif
3584+
3585+/* arith_op macro for one operator:
3586+ */
3587+#define ARITH_OP1_START( op_num, op_int ) \
3588+ int failed= 0; \
3589+ switch( arith_mode(rb,rb) ) { \
3590+ case TK_INT: \
3591+ if (op_int ( &(ra)->value.i, ivalue(rb) )) \
3592+ { ra->tt= LUA_TINT; break; } /* else flow through */ \
3593+ case TK_NUMBER: \
3594+ setnvalue(ra, op_num (nvalue(rb))); break; \
3595+
3596+#define ARITH_OP1_END \
3597+ default: \
3598+ failed= 1; break; \
3599+ } if (!failed) continue;
3600+
3601+#ifdef LNUM_COMPLEX
3602+# define arith_op1_continue( op_num, op_int, op_complex ) \
3603+ ARITH_OP1_START( op_num, op_int ) \
3604+ case TK_NUMBER2: \
3605+ setnvalue_complex( ra, op_complex ( nvalue_complex_fast(rb) )); break; \
3606+ ARITH_OP1_END
3607+#else
3608+# define arith_op1_continue( op_num, op_int, _ ) \
3609+ ARITH_OP1_START( op_num, op_int ) \
3610+ ARITH_OP1_END
3611+#endif
3612 
3613 
3614 void luaV_execute (lua_State *L, int nexeccalls) {
3615@@ -468,38 +661,45 @@
3616         continue;
3617       }
3618       case OP_ADD: {
3619- arith_op(luai_numadd, TM_ADD);
3620+ TValue *rb = RKB(i), *rc= RKC(i);
3621+ arith_op_continue( luai_numadd, try_addint, luai_vectadd );
3622+ Protect(Arith(L, ra, rb, rc, TM_ADD)); \
3623         continue;
3624       }
3625       case OP_SUB: {
3626- arith_op(luai_numsub, TM_SUB);
3627+ TValue *rb = RKB(i), *rc= RKC(i);
3628+ arith_op_continue( luai_numsub, try_subint, luai_vectsub );
3629+ Protect(Arith(L, ra, rb, rc, TM_SUB));
3630         continue;
3631       }
3632       case OP_MUL: {
3633- arith_op(luai_nummul, TM_MUL);
3634+ TValue *rb = RKB(i), *rc= RKC(i);
3635+ arith_op_continue(luai_nummul, try_mulint, luai_vectmul);
3636+ Protect(Arith(L, ra, rb, rc, TM_MUL));
3637         continue;
3638       }
3639       case OP_DIV: {
3640- arith_op(luai_numdiv, TM_DIV);
3641+ TValue *rb = RKB(i), *rc= RKC(i);
3642+ arith_op_continue(luai_numdiv, try_divint, luai_vectdiv);
3643+ Protect(Arith(L, ra, rb, rc, TM_DIV));
3644         continue;
3645       }
3646       case OP_MOD: {
3647- arith_op(luai_nummod, TM_MOD);
3648+ TValue *rb = RKB(i), *rc= RKC(i);
3649+ arith_op_continue_scalar(luai_nummod, try_modint); /* scalars only */
3650+ Protect(Arith(L, ra, rb, rc, TM_MOD));
3651         continue;
3652       }
3653       case OP_POW: {
3654- arith_op(luai_numpow, TM_POW);
3655+ TValue *rb = RKB(i), *rc= RKC(i);
3656+ arith_op_continue(luai_numpow, try_powint, luai_vectpow);
3657+ Protect(Arith(L, ra, rb, rc, TM_POW));
3658         continue;
3659       }
3660       case OP_UNM: {
3661         TValue *rb = RB(i);
3662- if (ttisnumber(rb)) {
3663- lua_Number nb = nvalue(rb);
3664- setnvalue(ra, luai_numunm(nb));
3665- }
3666- else {
3667- Protect(Arith(L, ra, rb, rb, TM_UNM));
3668- }
3669+ arith_op1_continue(luai_numunm, try_unmint, luai_vectunm);
3670+ Protect(Arith(L, ra, rb, rb, TM_UNM));
3671         continue;
3672       }
3673       case OP_NOT: {
3674@@ -511,11 +711,11 @@
3675         const TValue *rb = RB(i);
3676         switch (ttype(rb)) {
3677           case LUA_TTABLE: {
3678- setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
3679+ setivalue(ra, luaH_getn(hvalue(rb)));
3680             break;
3681           }
3682           case LUA_TSTRING: {
3683- setnvalue(ra, cast_num(tsvalue(rb)->len));
3684+ setivalue(ra, tsvalue(rb)->len);
3685             break;
3686           }
3687           default: { /* try metamethod */
3688@@ -648,14 +848,30 @@
3689         }
3690       }
3691       case OP_FORLOOP: {
3692- lua_Number step = nvalue(ra+2);
3693- lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
3694- lua_Number limit = nvalue(ra+1);
3695- if (luai_numlt(0, step) ? luai_numle(idx, limit)
3696- : luai_numle(limit, idx)) {
3697- dojump(L, pc, GETARG_sBx(i)); /* jump back */
3698- setnvalue(ra, idx); /* update internal index... */
3699- setnvalue(ra+3, idx); /* ...and external index */
3700+ /* If start,step and limit are all integers, we don't need to check
3701+ * against overflow in the looping.
3702+ */
3703+ if (ttisint(ra) && ttisint(ra+1) && ttisint(ra+2)) {
3704+ lua_Integer step = ivalue(ra+2);
3705+ lua_Integer idx = ivalue(ra) + step; /* increment index */
3706+ lua_Integer limit = ivalue(ra+1);
3707+ if (step > 0 ? (idx <= limit) : (limit <= idx)) {
3708+ dojump(L, pc, GETARG_sBx(i)); /* jump back */
3709+ setivalue(ra, idx); /* update internal index... */
3710+ setivalue(ra+3, idx); /* ...and external index */
3711+ }
3712+ } else {
3713+ /* non-integer looping (don't use 'nvalue_fast', some may be integer!)
3714+ */
3715+ lua_Number step = nvalue(ra+2);
3716+ lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
3717+ lua_Number limit = nvalue(ra+1);
3718+ if (luai_numlt(0, step) ? luai_numle(idx, limit)
3719+ : luai_numle(limit, idx)) {
3720+ dojump(L, pc, GETARG_sBx(i)); /* jump back */
3721+ setnvalue(ra, idx); /* update internal index... */
3722+ setnvalue(ra+3, idx); /* ...and external index */
3723+ }
3724         }
3725         continue;
3726       }
3727@@ -664,13 +880,21 @@
3728         const TValue *plimit = ra+1;
3729         const TValue *pstep = ra+2;
3730         L->savedpc = pc; /* next steps may throw errors */
3731+ /* Using same location for tonumber's both arguments, effectively does
3732+ * in-place modification (string->number). */
3733         if (!tonumber(init, ra))
3734           luaG_runerror(L, LUA_QL("for") " initial value must be a number");
3735         else if (!tonumber(plimit, ra+1))
3736           luaG_runerror(L, LUA_QL("for") " limit must be a number");
3737         else if (!tonumber(pstep, ra+2))
3738           luaG_runerror(L, LUA_QL("for") " step must be a number");
3739- setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
3740+ /* Step back one value (keep within integers if we can)
3741+ */
3742+ if (!( ttisint(ra) && ttisint(pstep) &&
3743+ try_subint( &ra->value.i, ivalue(ra), ivalue(pstep) ) )) {
3744+ /* don't use 'nvalue_fast()', values may be integer */
3745+ setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
3746+ }
3747         dojump(L, pc, GETARG_sBx(i));
3748         continue;
3749       }
3750@@ -707,7 +931,7 @@
3751           luaH_resizearray(L, h, last); /* pre-alloc it at once */
3752         for (; n > 0; n--) {
3753           TValue *val = ra+n;
3754- setobj2t(L, luaH_setnum(L, h, last--), val);
3755+ setobj2t(L, luaH_setint(L, h, last--), val);
3756           luaC_barriert(L, h, val);
3757         }
3758         continue;
3759Index: lua-5.1.4/src/lvm.h
3760===================================================================
3761--- lua-5.1.4.orig/src/lvm.h 2008-08-24 16:46:38.000000000 +0200
3762+++ lua-5.1.4/src/lvm.h 2008-08-24 16:48:20.000000000 +0200
3763@@ -15,11 +15,9 @@
3764 
3765 #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
3766 
3767-#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
3768- (((o) = luaV_tonumber(o,n)) != NULL))
3769+#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
3770 
3771-#define equalobj(L,o1,o2) \
3772- (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
3773+#define equalobj(L,o1,o2) (ttype_ext_same(o1,o2) && luaV_equalval(L, o1, o2))
3774 
3775 
3776 LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
3777Index: lua-5.1.4/src/print.c
3778===================================================================
3779--- lua-5.1.4.orig/src/print.c 2008-08-24 16:46:38.000000000 +0200
3780+++ lua-5.1.4/src/print.c 2008-08-24 16:48:20.000000000 +0200
3781@@ -14,6 +14,7 @@
3782 #include "lobject.h"
3783 #include "lopcodes.h"
3784 #include "lundump.h"
3785+#include "lnum.h"
3786 
3787 #define PrintFunction luaU_print
3788 
3789@@ -59,8 +60,16 @@
3790   case LUA_TBOOLEAN:
3791     printf(bvalue(o) ? "true" : "false");
3792     break;
3793+ case LUA_TINT:
3794+ printf(LUA_INTEGER_FMT,ivalue(o));
3795+ break;
3796   case LUA_TNUMBER:
3797- printf(LUA_NUMBER_FMT,nvalue(o));
3798+#ifdef LNUM_COMPLEX
3799+ // TBD: Do we get complex values here?
3800+ { lua_Number b= nvalue_img_fast(o);
3801+ printf( LUA_NUMBER_FMT "%s" LUA_NUMBER_FMT "i", nvalue_fast(o), b>=0 ? "+":"", b ); }
3802+#endif
3803+ printf(LUA_NUMBER_FMT,nvalue_fast(o));
3804     break;
3805   case LUA_TSTRING:
3806     PrintString(rawtsvalue(o));
3807

Archive Download this file



interactive