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