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