mirror of
https://github.com/lua/lua.git
synced 2026-07-26 16:09:07 +00:00
Compare commits
853 Commits
v3.1-alpha
...
v4.1-alpha
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0171543704 | ||
|
|
ebd1d1f82c | ||
|
|
04320e04bf | ||
|
|
5d2d2b1752 | ||
|
|
f34001faa9 | ||
|
|
95988676d8 | ||
|
|
767abdfdc0 | ||
|
|
45b173cbf8 | ||
|
|
a94cba4b88 | ||
|
|
f9f355221f | ||
|
|
b3dd9b1bb1 | ||
|
|
d8f37bf42a | ||
|
|
a598804a04 | ||
|
|
6c8e652aa6 | ||
|
|
9f25df02d5 | ||
|
|
ae1cf64348 | ||
|
|
a264fd089e | ||
|
|
a3d03ff6b6 | ||
|
|
654b16e83a | ||
|
|
dc4e0ecdaf | ||
|
|
9924668931 | ||
|
|
89ef91a7fe | ||
|
|
00aaee0ae7 | ||
|
|
18d3e82cfe | ||
|
|
2a4afb97c8 | ||
|
|
87a5fae453 | ||
|
|
afed6c6864 | ||
|
|
770954510f | ||
|
|
b346834a09 | ||
|
|
61a036eaa5 | ||
|
|
37f3a1c045 | ||
|
|
9559c111a3 | ||
|
|
777061e441 | ||
|
|
fccadba4b5 | ||
|
|
b940f09984 | ||
|
|
878979cef7 | ||
|
|
8e586c13fc | ||
|
|
eadf2aaaff | ||
|
|
ae19b2f51e | ||
|
|
a3fbf5f5fd | ||
|
|
7fae1c17b1 | ||
|
|
89e8303f4e | ||
|
|
89c301d180 | ||
|
|
186111dc01 | ||
|
|
0a1b1acdd3 | ||
|
|
79acf5ea60 | ||
|
|
cbc59592ff | ||
|
|
4905fdd135 | ||
|
|
0267168675 | ||
|
|
ba11831d35 | ||
|
|
190ddd431d | ||
|
|
65c289b046 | ||
|
|
d5b83ead90 | ||
|
|
da673d31aa | ||
|
|
323da72577 | ||
|
|
943b8f5b18 | ||
|
|
762d059a13 | ||
|
|
572a69b6af | ||
|
|
beee01b170 | ||
|
|
6473f965ca | ||
|
|
0e0e4a480e | ||
|
|
2a50188269 | ||
|
|
9aedea6ec8 | ||
|
|
a68635a919 | ||
|
|
2112142680 | ||
|
|
dd3a63c205 | ||
|
|
cb49b088b6 | ||
|
|
c5a23cf01a | ||
|
|
6048c4f74d | ||
|
|
5e870f86a2 | ||
|
|
f81b8adb3f | ||
|
|
2b2267069b | ||
|
|
72d3d155b0 | ||
|
|
6d4db86888 | ||
|
|
e9a3820370 | ||
|
|
9e8a46daa2 | ||
|
|
7b84f9e65c | ||
|
|
1e40b4dc61 | ||
|
|
dfd7ce74cf | ||
|
|
8399df5dcf | ||
|
|
d2a64bb6d4 | ||
|
|
a90f06dcd8 | ||
|
|
35023355f2 | ||
|
|
39b7978329 | ||
|
|
d164e2294f | ||
|
|
2cc94ac81b | ||
|
|
5f37134e64 | ||
|
|
52ee91dd73 | ||
|
|
191fd35f0a | ||
|
|
d6978d03e1 | ||
|
|
68b08848d4 | ||
|
|
888f91fa24 | ||
|
|
c1db0b2bf1 | ||
|
|
099442c41f | ||
|
|
27600fe87a | ||
|
|
f8509668dc | ||
|
|
40f4e931f3 | ||
|
|
29371ecfe8 | ||
|
|
5d9b8b7cdc | ||
|
|
1c2501fad4 | ||
|
|
a4ae1475e3 | ||
|
|
b3da4ee907 | ||
|
|
3c649ce123 | ||
|
|
95da574dbc | ||
|
|
255052b6c6 | ||
|
|
1bdc156b52 | ||
|
|
03d8a9bf0d | ||
|
|
d2e340f467 | ||
|
|
6875fdc8be | ||
|
|
dc17a9cc24 | ||
|
|
81e63f75c0 | ||
|
|
c81404cae5 | ||
|
|
b6ce590433 | ||
|
|
e70e6a3b7a | ||
|
|
6548bf7462 | ||
|
|
7178a5e34a | ||
|
|
322b7b5fc5 | ||
|
|
1f917e709c | ||
|
|
d444153dbe | ||
|
|
a894499566 | ||
|
|
d1fc6244f0 | ||
|
|
7e3d3e1f70 | ||
|
|
1f9e3731d1 | ||
|
|
42224ca553 | ||
|
|
426d3e43bd | ||
|
|
8823f371a2 | ||
|
|
e506b864cd | ||
|
|
9a231afa97 | ||
|
|
4ab6acacdf | ||
|
|
6858763994 | ||
|
|
b68fb7f62e | ||
|
|
60ff79451c | ||
|
|
44a53df688 | ||
|
|
63a822c8e1 | ||
|
|
09def5da44 | ||
|
|
6b71a9cfe5 | ||
|
|
fa8c44b510 | ||
|
|
3b6f8bfbe6 | ||
|
|
caf01b5bfa | ||
|
|
ca1f28b829 | ||
|
|
b82242d4c4 | ||
|
|
ac390020e9 | ||
|
|
9b45439860 | ||
|
|
7959f3aebb | ||
|
|
bce6572579 | ||
|
|
a53d9b66ca | ||
|
|
c8559e3c8d | ||
|
|
71ae4801d6 | ||
|
|
6fda6a5302 | ||
|
|
4ac58853dc | ||
|
|
f2c451d745 | ||
|
|
619edfd9e4 | ||
|
|
a04de4f0ad | ||
|
|
a653d93a43 | ||
|
|
321c7fb6f8 | ||
|
|
dabb19fc17 | ||
|
|
08496eea8b | ||
|
|
4ff5545709 | ||
|
|
595e449537 | ||
|
|
a907aeeb1e | ||
|
|
76f62fc5a1 | ||
|
|
0183b8030c | ||
|
|
8c49e19865 | ||
|
|
6af005ec20 | ||
|
|
f8a0fd268e | ||
|
|
0066bbbb0b | ||
|
|
1db05793a0 | ||
|
|
af97be026b | ||
|
|
cce8ebd306 | ||
|
|
4894c27962 | ||
|
|
10ac68c648 | ||
|
|
01b00cc292 | ||
|
|
fc7b167ae0 | ||
|
|
96253ed8ce | ||
|
|
35d6b15057 | ||
|
|
22914afab3 | ||
|
|
523c5d8e1c | ||
|
|
40a4c76773 | ||
|
|
1385d81d20 | ||
|
|
906d5dcc41 | ||
|
|
93a5649d40 | ||
|
|
df416661cc | ||
|
|
67c1afff59 | ||
|
|
03770ecfc9 | ||
|
|
7409678b5d | ||
|
|
d1c689af40 | ||
|
|
37e9c2e744 | ||
|
|
e42a219eeb | ||
|
|
b3959d58ff | ||
|
|
f379d06e24 | ||
|
|
728ff20701 | ||
|
|
2cbbf3933a | ||
|
|
e2b6b7de1b | ||
|
|
563b1f5704 | ||
|
|
4670476584 | ||
|
|
89f98c0995 | ||
|
|
b892f0a877 | ||
|
|
aadc35449e | ||
|
|
cdc8139e29 | ||
|
|
e833bd47c9 | ||
|
|
b7ffb128cb | ||
|
|
64eecc0b82 | ||
|
|
8b88ab07f7 | ||
|
|
2779ceeb12 | ||
|
|
e93c4547fe | ||
|
|
0ffc676ce7 | ||
|
|
18fb3ddb89 | ||
|
|
2bddbe6603 | ||
|
|
e323338fd0 | ||
|
|
46b543ebef | ||
|
|
79909a92e1 | ||
|
|
d6232a0b2e | ||
|
|
ae63a0e692 | ||
|
|
cd3d446957 | ||
|
|
f01e6c6f1d | ||
|
|
ad3816d0d1 | ||
|
|
046a3d6173 | ||
|
|
001f2bdd0e | ||
|
|
cd2ddaded9 | ||
|
|
d68209e822 | ||
|
|
1088cde03c | ||
|
|
6759f3ec5e | ||
|
|
f6834f4393 | ||
|
|
78bc8e553d | ||
|
|
dad808a73a | ||
|
|
ca7fd50a4e | ||
|
|
282ab366f4 | ||
|
|
444d6a106b | ||
|
|
13635f7de7 | ||
|
|
d8a442206d | ||
|
|
c9c6f9747c | ||
|
|
c2aa7bd72d | ||
|
|
f9dd50cefc | ||
|
|
dbde9f8817 | ||
|
|
857a1204d8 | ||
|
|
d742b88fa1 | ||
|
|
eb822c314a | ||
|
|
ab7aceb980 | ||
|
|
72b12e0956 | ||
|
|
5ed3bcd4ea | ||
|
|
7508fed673 | ||
|
|
aab4fde468 | ||
|
|
6ec4a0ef76 | ||
|
|
42ca3105ac | ||
|
|
6030d5fed4 | ||
|
|
91c003dcc2 | ||
|
|
620d22f2a0 | ||
|
|
f45cba42b5 | ||
|
|
2628a42480 | ||
|
|
0b3b6850c9 | ||
|
|
5d033f33eb | ||
|
|
e143fed484 | ||
|
|
01ce1ce48c | ||
|
|
024ed4218f | ||
|
|
c1f725ba4a | ||
|
|
5d69960734 | ||
|
|
d1c351857d | ||
|
|
4ef14ba280 | ||
|
|
b8d57092eb | ||
|
|
0f59e3d7a7 | ||
|
|
55c3bc2bcb | ||
|
|
8060193702 | ||
|
|
2779e81fbb | ||
|
|
30f6e658d2 | ||
|
|
787a78f83e | ||
|
|
70c8a31092 | ||
|
|
2018380e9f | ||
|
|
6e80c1cde1 | ||
|
|
f67f324377 | ||
|
|
eebc9729e4 | ||
|
|
6990b06467 | ||
|
|
cad91499dd | ||
|
|
ebdd4e8451 | ||
|
|
9a21e81907 | ||
|
|
f0b3cd1d6f | ||
|
|
fb5e6d5ac4 | ||
|
|
d63afba9d3 | ||
|
|
100bfec39a | ||
|
|
a290b84c67 | ||
|
|
1022b3c85e | ||
|
|
a1d3e001b9 | ||
|
|
10b4799ce5 | ||
|
|
a0de89d62a | ||
|
|
21dc77b2af | ||
|
|
2d3ebba537 | ||
|
|
a97f29f154 | ||
|
|
4135f4f586 | ||
|
|
808525e417 | ||
|
|
48a968e6b5 | ||
|
|
58adbde216 | ||
|
|
9e74ca62d7 | ||
|
|
d396562b5e | ||
|
|
9d60598260 | ||
|
|
4e56c0d514 | ||
|
|
ac12f4db4b | ||
|
|
b691d4344b | ||
|
|
9fdf73bc9a | ||
|
|
f1fd9b5c2c | ||
|
|
3723d790ae | ||
|
|
b800c38b69 | ||
|
|
5c0e79847c | ||
|
|
e662e0f1cd | ||
|
|
c85162be27 | ||
|
|
bd39db46ed | ||
|
|
90fb2e18e8 | ||
|
|
4365c31c89 | ||
|
|
44eb7d91ac | ||
|
|
ddc8d94a08 | ||
|
|
5d9cbdadfb | ||
|
|
58453dc1e1 | ||
|
|
f555e493f0 | ||
|
|
d1ffd1a1c5 | ||
|
|
9e84bf18db | ||
|
|
daf09c476f | ||
|
|
e238efc536 | ||
|
|
fdd04e7a7f | ||
|
|
fb55c96706 | ||
|
|
c39bec81dc | ||
|
|
5f22f8961c | ||
|
|
a7c1390ffa | ||
|
|
0802a9df9e | ||
|
|
f90bc248b3 | ||
|
|
d9e61e8cea | ||
|
|
397905ef86 | ||
|
|
435f587ed0 | ||
|
|
ae55f3eead | ||
|
|
cfba572076 | ||
|
|
aa01d2568d | ||
|
|
014a09c509 | ||
|
|
b622282973 | ||
|
|
7f867eb0b9 | ||
|
|
5362426ffa | ||
|
|
906434011f | ||
|
|
c07cebbbf6 | ||
|
|
afef009fce | ||
|
|
b69e712713 | ||
|
|
f517759507 | ||
|
|
52062684e1 | ||
|
|
83e3ec0117 | ||
|
|
feb2083730 | ||
|
|
8d2baf719c | ||
|
|
88657809ac | ||
|
|
2e1b32d873 | ||
|
|
89a3ec08f3 | ||
|
|
e3ea307434 | ||
|
|
18b0e8270d | ||
|
|
094880ef9e | ||
|
|
1de5587184 | ||
|
|
8ca9534d04 | ||
|
|
8bcf622876 | ||
|
|
dbbf6c073b | ||
|
|
292c953018 | ||
|
|
c542aac0b9 | ||
|
|
dbfb810267 | ||
|
|
9c14b5e416 | ||
|
|
6b9bf49265 | ||
|
|
989ad7232a | ||
|
|
190c3be739 | ||
|
|
7e30900def | ||
|
|
f63d7753b8 | ||
|
|
50a82ec1b9 | ||
|
|
ed1751bc32 | ||
|
|
8e617985fa | ||
|
|
c39345fba3 | ||
|
|
ed94a85f0c | ||
|
|
58fbdc76d5 | ||
|
|
a301304612 | ||
|
|
677313da32 | ||
|
|
ef62b340e0 | ||
|
|
5c2dd7a9e0 | ||
|
|
93d93a0bfb | ||
|
|
9e1f94fc1c | ||
|
|
8714cc02d6 | ||
|
|
69b45bb4e9 | ||
|
|
843f84f4ce | ||
|
|
b803c0600e | ||
|
|
8ac0bbf64b | ||
|
|
dc1e4f5073 | ||
|
|
330e51bed3 | ||
|
|
44b71ca816 | ||
|
|
bad6365540 | ||
|
|
91f34fb05c | ||
|
|
11a7022067 | ||
|
|
35a6ed2838 | ||
|
|
cfe6ca819b | ||
|
|
fe5c37ae95 | ||
|
|
c31f02948f | ||
|
|
c9f91c6fe3 | ||
|
|
66d620674c | ||
|
|
84790bb953 | ||
|
|
534c3a64d3 | ||
|
|
b9c98cd4d9 | ||
|
|
0e8855e171 | ||
|
|
2ae9c856cf | ||
|
|
c4409f69ab | ||
|
|
89a9c36281 | ||
|
|
8f2fba5877 | ||
|
|
62824137d6 | ||
|
|
71219ccc39 | ||
|
|
870f61d299 | ||
|
|
c845ec777a | ||
|
|
e408cf61a2 | ||
|
|
4e90768635 | ||
|
|
c1666a13e3 | ||
|
|
e02750ec60 | ||
|
|
0b56646baf | ||
|
|
634c3d57e9 | ||
|
|
4aa9ad6514 | ||
|
|
d76b1a0eef | ||
|
|
e7c8393682 | ||
|
|
ceaa97ff5b | ||
|
|
2bb77cdaed | ||
|
|
f9cf402fbd | ||
|
|
0c3fe2c44b | ||
|
|
0433e42023 | ||
|
|
0810bc707f | ||
|
|
c3b73cbeb8 | ||
|
|
1eabd0549e | ||
|
|
54dd5cc7fd | ||
|
|
031978798c | ||
|
|
14251c5a56 | ||
|
|
42130ed712 | ||
|
|
d615e78e08 | ||
|
|
c6965ce551 | ||
|
|
22329e4cdf | ||
|
|
9f734094f9 | ||
|
|
4e7e9e8de5 | ||
|
|
3e45496295 | ||
|
|
e30327728c | ||
|
|
36e1390631 | ||
|
|
8f0f54ec38 | ||
|
|
556a89e537 | ||
|
|
e2c60eda16 | ||
|
|
a69356e9e0 | ||
|
|
b53dc0c485 | ||
|
|
63e230a16b | ||
|
|
102a1be615 | ||
|
|
4c03196697 | ||
|
|
54633d7c7d | ||
|
|
13578517c7 | ||
|
|
213e9febc8 | ||
|
|
47b4bf5964 | ||
|
|
415ee1a24f | ||
|
|
5f3c5ace95 | ||
|
|
441469f803 | ||
|
|
1157c2a7e4 | ||
|
|
374cc888f4 | ||
|
|
bb9605bbc9 | ||
|
|
6759299880 | ||
|
|
da7eb34cd6 | ||
|
|
2c8e28d75a | ||
|
|
4fb8e93c36 | ||
|
|
7f77431950 | ||
|
|
87367e97f3 | ||
|
|
06f08f5634 | ||
|
|
3860c5934e | ||
|
|
f0b697e01c | ||
|
|
73aa465a8e | ||
|
|
3d0577f4b9 | ||
|
|
977b8f65f0 | ||
|
|
e3cf93ddb6 | ||
|
|
88b306f495 | ||
|
|
563de491be | ||
|
|
29e0193425 | ||
|
|
61f97a8ace | ||
|
|
d1ea38580a | ||
|
|
3c9d999424 | ||
|
|
f7840a3e0b | ||
|
|
1780e2c977 | ||
|
|
fa14cbf8e8 | ||
|
|
4d4e6f07c0 | ||
|
|
5cecf0ea9f | ||
|
|
3bc925138e | ||
|
|
39e1f079bd | ||
|
|
075da266e5 | ||
|
|
228a62f799 | ||
|
|
7c2fb15bec | ||
|
|
1f01998516 | ||
|
|
c929376d59 | ||
|
|
3afe85b2ce | ||
|
|
52aad0ab59 | ||
|
|
74f1c3d025 | ||
|
|
1f691a4fcd | ||
|
|
d6b9f49aaf | ||
|
|
d83c2a8455 | ||
|
|
d11e5adf55 | ||
|
|
99e340b2ba | ||
|
|
9744255ae9 | ||
|
|
512b15b601 | ||
|
|
33d3504889 | ||
|
|
2877bad4c2 | ||
|
|
27163f032e | ||
|
|
eab57ed6fd | ||
|
|
ea45f3eb28 | ||
|
|
46ec57cbc6 | ||
|
|
62787f1b1f | ||
|
|
dcb7bb514e | ||
|
|
b3aaa048b0 | ||
|
|
aee07c6599 | ||
|
|
bcdbdaccc3 | ||
|
|
5cafe5af02 | ||
|
|
607012ece1 | ||
|
|
0652906e7a | ||
|
|
4d1b815b60 | ||
|
|
4ebc183e56 | ||
|
|
298d0abff7 | ||
|
|
4fbe775154 | ||
|
|
aabefe9d68 | ||
|
|
fb60283974 | ||
|
|
acdb0b741e | ||
|
|
5b08fcd5a1 | ||
|
|
b1b0c219f5 | ||
|
|
be3212de78 | ||
|
|
2eaf9a00ae | ||
|
|
6c79a0a80d | ||
|
|
3daeabb606 | ||
|
|
cfdebfbc53 | ||
|
|
d61d5bef9d | ||
|
|
4450efc97e | ||
|
|
de65253f2d | ||
|
|
12b45c2df2 | ||
|
|
1b15206cf9 | ||
|
|
e6d56cd2d8 | ||
|
|
c16c63cc59 | ||
|
|
dea54a7c71 | ||
|
|
544eeb1f9c | ||
|
|
b3fe203c36 | ||
|
|
9c142740c1 | ||
|
|
7621cc2aa5 | ||
|
|
ba1f504970 | ||
|
|
617be66015 | ||
|
|
f356eb010b | ||
|
|
3e38fd2463 | ||
|
|
533beedad2 | ||
|
|
968ad49da6 | ||
|
|
1fdb445e7d | ||
|
|
c7119a93f0 | ||
|
|
3c9e29f56d | ||
|
|
72afb6debb | ||
|
|
b097076678 | ||
|
|
8223ff473f | ||
|
|
fe237ad808 | ||
|
|
3181dfefee | ||
|
|
764e095d7f | ||
|
|
2b3ea61342 | ||
|
|
49af08e3e7 | ||
|
|
c5fa0895e9 | ||
|
|
d015f1fc02 | ||
|
|
790690a223 | ||
|
|
33b8a01032 | ||
|
|
d29ce75737 | ||
|
|
878ef96127 | ||
|
|
e5743adb21 | ||
|
|
514783de9d | ||
|
|
7d1499ba88 | ||
|
|
29ede6aa13 | ||
|
|
951897c093 | ||
|
|
a34677e4f4 | ||
|
|
573372665a | ||
|
|
a6755e2f1a | ||
|
|
a4628a02cf | ||
|
|
0c725b2492 | ||
|
|
d915cf4f9d | ||
|
|
53fb65d394 | ||
|
|
a7fa7bafc8 | ||
|
|
cde179b369 | ||
|
|
80b39d83c3 | ||
|
|
5a48255c9f | ||
|
|
bbab974717 | ||
|
|
910836fb53 | ||
|
|
8e7451512f | ||
|
|
4e9f2d13d5 | ||
|
|
b6ebbb2fee | ||
|
|
c5fee7615e | ||
|
|
cca78b5c71 | ||
|
|
d435b7acd8 | ||
|
|
63ccf42397 | ||
|
|
1942b58609 | ||
|
|
4343420d4d | ||
|
|
1f7103e05d | ||
|
|
dcc10accea | ||
|
|
e2fc5aa684 | ||
|
|
d53e2d2386 | ||
|
|
cf9a22396c | ||
|
|
17374d2daa | ||
|
|
04265655a8 | ||
|
|
e10788b2ff | ||
|
|
7eb2f18621 | ||
|
|
a8a75fb7ab | ||
|
|
ae3ecc2d4a | ||
|
|
2e13cd77ab | ||
|
|
ff9c0da783 | ||
|
|
f7fe04d4b7 | ||
|
|
82699d0c4f | ||
|
|
88866208f0 | ||
|
|
7d365a5c7a | ||
|
|
52a206366d | ||
|
|
b2c89ed2d2 | ||
|
|
2a03170ebd | ||
|
|
05f55cc062 | ||
|
|
c787dccd9b | ||
|
|
b44e35b773 | ||
|
|
21843f022a | ||
|
|
900257e814 | ||
|
|
a82c8185bc | ||
|
|
4bbe0679a8 | ||
|
|
438598aeea | ||
|
|
e9a670695a | ||
|
|
16024861bd | ||
|
|
3f43aaa23f | ||
|
|
3b4c831ca9 | ||
|
|
3fdab3981b | ||
|
|
88b65da4d7 | ||
|
|
521b38532a | ||
|
|
36b6fe8d17 | ||
|
|
d4dce57f5c | ||
|
|
5c19ed2a13 | ||
|
|
5caf7f4a33 | ||
|
|
3b533ea7c7 | ||
|
|
995775e1cb | ||
|
|
abfc885079 | ||
|
|
613b60c156 | ||
|
|
cdd0fe9946 | ||
|
|
bc8619342a | ||
|
|
48326500d0 | ||
|
|
c8d219798a | ||
|
|
34695d4f4a | ||
|
|
924bbe020b | ||
|
|
3aa500b524 | ||
|
|
f1861ee210 | ||
|
|
705eae9fe4 | ||
|
|
6eb1399a1c | ||
|
|
c390f73e3b | ||
|
|
73308c7605 | ||
|
|
288fa05602 | ||
|
|
7808ea3a5f | ||
|
|
732741b62f | ||
|
|
cc0f635ef7 | ||
|
|
e64dbc390a | ||
|
|
a0fd8d1787 | ||
|
|
d9d04a9274 | ||
|
|
b4ad600b93 | ||
|
|
cb7f027380 | ||
|
|
0bbd96bd5f | ||
|
|
4eb67aa710 | ||
|
|
0133610315 | ||
|
|
de04533dc0 | ||
|
|
7c9aee64c2 | ||
|
|
e0ff4e5d22 | ||
|
|
bf7f85d609 | ||
|
|
a775a2d81a | ||
|
|
e9aa98d594 | ||
|
|
3e9c6a8a24 | ||
|
|
1f4e2ba7b2 | ||
|
|
d6ff06751a | ||
|
|
7a11c7f8e4 | ||
|
|
c454dc7bdd | ||
|
|
82ad0d5770 | ||
|
|
256d1bea08 | ||
|
|
f2d35bdc78 | ||
|
|
2679461637 | ||
|
|
0870a2d1d8 | ||
|
|
78edc241e9 | ||
|
|
e907c711c0 | ||
|
|
5a8bb00df4 | ||
|
|
677188de8a | ||
|
|
6233d21c9d | ||
|
|
ab8ea5c38a | ||
|
|
ae9fd122fa | ||
|
|
da18ec5d54 | ||
|
|
038848eccd | ||
|
|
b678e465a1 | ||
|
|
72d675aba7 | ||
|
|
ba57f7d946 | ||
|
|
e63b542c9b | ||
|
|
6a853fcb8b | ||
|
|
31bea2190b | ||
|
|
4b954e9b2e | ||
|
|
055823c04d | ||
|
|
26d1e21c89 | ||
|
|
24a2c08145 | ||
|
|
9d7bae0b6a | ||
|
|
082aded149 | ||
|
|
aa9c75c06e | ||
|
|
f04c83e075 | ||
|
|
c364e9f97e | ||
|
|
e3a02e6a9c | ||
|
|
d5feffdb60 | ||
|
|
bb5627f3a4 | ||
|
|
21107d7c2c | ||
|
|
b5cd7d426f | ||
|
|
bf6d2ccf92 | ||
|
|
b82ff713e3 | ||
|
|
77113ee02f | ||
|
|
ad6c7b0dd4 | ||
|
|
8b2d97d187 | ||
|
|
fb1cf6ab2d | ||
|
|
19ca2087de | ||
|
|
7bdbd833b5 | ||
|
|
b22baf386d | ||
|
|
8fdd06ba3c | ||
|
|
028ec00ab9 | ||
|
|
1dcf1c9cbd | ||
|
|
76179a1014 | ||
|
|
bdfab46c22 | ||
|
|
5687949560 | ||
|
|
19de5b2205 | ||
|
|
cbc58af260 | ||
|
|
80001ab0eb | ||
|
|
ae29ab9858 | ||
|
|
27407fc1f5 | ||
|
|
1a17da2ff9 | ||
|
|
50248e440a | ||
|
|
0f0079f394 | ||
|
|
68267ed878 | ||
|
|
fd25d4ad85 | ||
|
|
2431534f10 | ||
|
|
fd7d0774e5 | ||
|
|
57ffc3f009 | ||
|
|
4a13f513f8 | ||
|
|
13ad46b67d | ||
|
|
1b45e967b4 | ||
|
|
933bead92e | ||
|
|
3314f49ec4 | ||
|
|
bc930aa5ff | ||
|
|
67b44c9493 | ||
|
|
758a381644 | ||
|
|
eec31aaca5 | ||
|
|
595738f6fe | ||
|
|
b5eb4f3126 | ||
|
|
3fecf187ff | ||
|
|
54840fb256 | ||
|
|
e87fddf1ad | ||
|
|
dea400bc1d | ||
|
|
fb663f768d | ||
|
|
e03767b3eb | ||
|
|
8396027516 | ||
|
|
e24f7fd2d2 | ||
|
|
8081f39dab | ||
|
|
3cc4ca821e | ||
|
|
01772cefa5 | ||
|
|
dc90d4bce3 | ||
|
|
f5bc671030 | ||
|
|
d7294c6de8 | ||
|
|
63a752f961 | ||
|
|
03d38b66fd | ||
|
|
b9c9ccfbb4 | ||
|
|
b94110a68f | ||
|
|
8278468041 | ||
|
|
4fbb2531b3 | ||
|
|
59f8e6fb77 | ||
|
|
05d89b5c05 | ||
|
|
fe5c41fb8a | ||
|
|
9a45543841 | ||
|
|
766e67ef3b | ||
|
|
4c94d8cc2c | ||
|
|
d2de2d5eda | ||
|
|
96a7695275 | ||
|
|
63166c0ca0 | ||
|
|
a881abfd1e | ||
|
|
d3ac7075a2 | ||
|
|
0c9080c7a9 | ||
|
|
b8fcb7b151 | ||
|
|
5d6de9075d | ||
|
|
21cff3015a | ||
|
|
5ca2709ba0 | ||
|
|
bb1cb7b9f1 | ||
|
|
c64f36ab2b | ||
|
|
e4830ddce3 | ||
|
|
758e330d6e | ||
|
|
8e3bd752bb | ||
|
|
a84bca67fc | ||
|
|
4ccfb2f9bc | ||
|
|
ce9609296c | ||
|
|
b1450721be | ||
|
|
b04294d3d8 | ||
|
|
22c2704842 | ||
|
|
ee22af5ced | ||
|
|
cc117253c8 | ||
|
|
8e226e6a09 | ||
|
|
1d420c2c11 | ||
|
|
5378331f2d | ||
|
|
894a264671 | ||
|
|
e1a127245d | ||
|
|
afb5ef72e1 | ||
|
|
1d8edd347d | ||
|
|
41d9ea948c | ||
|
|
ee912e5a7f | ||
|
|
ad446a0eb0 | ||
|
|
176cb39feb | ||
|
|
64ad009fb2 | ||
|
|
dcb1a08906 | ||
|
|
1788501eed | ||
|
|
bee1a5aeb2 | ||
|
|
994aba062b | ||
|
|
e869d17eb1 | ||
|
|
9a0221ef58 | ||
|
|
07008b5d45 | ||
|
|
8f31eda649 | ||
|
|
da94130160 | ||
|
|
468fbdbde7 | ||
|
|
eb45f8b631 | ||
|
|
df0df08bc5 | ||
|
|
9618aaf07d | ||
|
|
bec9bc4154 | ||
|
|
955a811aa1 | ||
|
|
c9902be294 | ||
|
|
112c9d53ab | ||
|
|
0789451458 | ||
|
|
d97af0de26 | ||
|
|
1917149fdd | ||
|
|
0845e73b6a | ||
|
|
7dfa952091 | ||
|
|
02134b4a87 | ||
|
|
bdb1db4d37 | ||
|
|
02a6891939 | ||
|
|
741c6f5006 | ||
|
|
6152973f9c | ||
|
|
243a808067 | ||
|
|
62c36a6056 | ||
|
|
74719afc33 | ||
|
|
7e59a8901d | ||
|
|
abc6eac404 | ||
|
|
054e0b888a | ||
|
|
da252eeff7 | ||
|
|
9890bedaab | ||
|
|
0a0c9593b8 | ||
|
|
d470792517 | ||
|
|
439236773b | ||
|
|
2a2b64d6ac | ||
|
|
daa937c043 | ||
|
|
21455162b5 | ||
|
|
99cc4b20f2 | ||
|
|
0969a971cd | ||
|
|
be6d215f67 | ||
|
|
e74817f8aa | ||
|
|
043c2ac258 | ||
|
|
88a2023c32 | ||
|
|
5ef1989c4b | ||
|
|
f380d627f8 | ||
|
|
aafa106d10 | ||
|
|
29b7b8e52c | ||
|
|
a9dd2c6717 | ||
|
|
aee3f97acb | ||
|
|
46968b8ffa | ||
|
|
6cdf0d8768 | ||
|
|
07ff251a17 | ||
|
|
b3b7cf7335 | ||
|
|
8622dc18bf | ||
|
|
d22e2644dd | ||
|
|
f529a22ca5 | ||
|
|
783ba75129 | ||
|
|
d49e4dd752 |
268
bugs
268
bugs
@@ -1,4 +1,272 @@
|
||||
|
||||
** lua.stx / llex.c
|
||||
Tue Dec 2 10:45:48 EDT 1997
|
||||
>> BUG: "lastline" was not reset on function entry, so debug information
|
||||
>> started only in the 2nd line of a function.
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 3.1 alpha
|
||||
|
||||
** lua.c
|
||||
Thu Jan 15 14:34:58 EDT 1998
|
||||
>> must include "stdlib.h" (for "exit()").
|
||||
|
||||
** lbuiltin.c / lobject.h
|
||||
Thu Jan 15 14:34:58 EDT 1998
|
||||
>> MAX_WORD may be bigger than MAX_INT
|
||||
(by lhf)
|
||||
|
||||
** llex.c
|
||||
Mon Jan 19 18:17:18 EDT 1998
|
||||
>> wrong line number (+1) in error report when file starts with "#..."
|
||||
|
||||
** lstrlib.c
|
||||
Tue Jan 27 15:27:49 EDT 1998
|
||||
>> formats like "%020d" were considered too big (3 digits); moreover,
|
||||
>> some sistems limit printf to at most 500 chars, so we can limit sizes
|
||||
>> to 2 digits (99).
|
||||
|
||||
** lapi.c
|
||||
Tue Jan 27 17:12:36 EDT 1998
|
||||
>> "lua_getstring" may create a new string, so should check GC
|
||||
|
||||
** lstring.c / ltable.c
|
||||
Wed Jan 28 14:48:12 EDT 1998
|
||||
>> tables can become full of "empty" slots, and keep growing without limits.
|
||||
|
||||
** lstrlib.c
|
||||
Mon Mar 9 15:26:09 EST 1998
|
||||
>> gsub('a', '(b?)%1*' ...) loops (because the capture is empty).
|
||||
|
||||
** lstrlib.c
|
||||
Mon May 18 19:20:00 EST 1998
|
||||
>> arguments for "format" 'x', 'X', 'o' and 'u' must be unsigned int.
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 3.1
|
||||
|
||||
** liolib.c / lauxlib.c
|
||||
Mon Sep 7 15:57:02 EST 1998
|
||||
>> function "luaL_argerror" prints wrong argument number (from a user's point
|
||||
of view) when functions have upvalues.
|
||||
|
||||
** lstrlib.c
|
||||
Tue Nov 10 17:29:36 EDT 1998
|
||||
>> gsub/strfind do not check whether captures are properly finished.
|
||||
(by roberto/tomas)
|
||||
|
||||
** lbuiltin.c
|
||||
Fri Dec 18 11:22:55 EDT 1998
|
||||
>> "tonumber" goes crazy with negative numbers in other bases (not 10),
|
||||
because "strtol" returns long, not unsigned long.
|
||||
(by Visual C++)
|
||||
|
||||
** lstrlib.c
|
||||
Mon Jan 4 10:41:40 EDT 1999
|
||||
>> "format" does not check size of format item (such as "%00000...00000d").
|
||||
|
||||
** lapi.c
|
||||
Wed Feb 3 14:40:21 EDT 1999
|
||||
>> getlocal cannot return the local itself, since lua_isstring and
|
||||
lua_isnumber can modify it.
|
||||
|
||||
** lstrlib.c
|
||||
Thu Feb 4 17:08:50 EDT 1999
|
||||
>> format "%s" may break limit of "sprintf" on some machines.
|
||||
(by Marcelo Sales)
|
||||
|
||||
** lzio.c
|
||||
Thu Mar 4 11:49:37 EST 1999
|
||||
>> file stream cannot call fread after EOF.
|
||||
(by lhf)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 3.2 (beta)
|
||||
|
||||
** lstrlib.c
|
||||
Fri Apr 30 11:10:20 EST 1999
|
||||
>> '$' at end of pattern was matching regular '$', too.
|
||||
(by anna; since 2.5)
|
||||
|
||||
** lbuiltin.c
|
||||
Fri May 21 17:15:11 EST 1999
|
||||
>> foreach, foreachi, foreachvar points to function in stack when stack
|
||||
can be reallocated.
|
||||
(by tomas; since 3.2 beta)
|
||||
|
||||
** lparser.c
|
||||
Wed Jun 16 10:32:46 EST 1999
|
||||
>> cannot assign to unlimited variables, because it causes overflow in
|
||||
the number of returns of a function.
|
||||
(since 3.1)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 3.2
|
||||
|
||||
** lmathlib.c
|
||||
Wed Aug 18 11:28:38 EST 1999
|
||||
>> random(0) and random(x,0) are wrong (0 is read as no argument!).
|
||||
(by Dave Bollinger; since 3.1)
|
||||
|
||||
** lparser.c
|
||||
Thu Sep 2 10:07:20 EST 1999
|
||||
>> in the (old) expression << ls->fs->f->consts[checkname(ls)] >>, checkname
|
||||
could realloc f->consts.
|
||||
(by Supratik Champati; since 3.2 beta)
|
||||
|
||||
** lobject.c / lbuiltin.c
|
||||
Wed Sep 8 17:41:54 EST 1999
|
||||
>> tonumber'e1' and tonumber(' ', x), for x!=10, gave 0 instead of nil.
|
||||
(since 3.1)
|
||||
|
||||
** lstrlib.c
|
||||
Thu Nov 11 14:36:30 EDT 1999
|
||||
>> `strfind' does not handle \0 in plain search.
|
||||
(by Jon Kleiser; since 3.1)
|
||||
|
||||
** lparser.c
|
||||
Wed Dec 29 16:05:43 EDT 1999
|
||||
>> return gives wrong line in debug information
|
||||
(by lhf; since 3.2 [at least])
|
||||
|
||||
** ldo.c
|
||||
Thu Dec 30 16:39:33 EDT 1999
|
||||
>> cannot reopen stdin (for binary mode)
|
||||
(by lhf & roberto; since 3.1)
|
||||
|
||||
** lapi.c
|
||||
Thu Mar 2 09:41:53 EST 2000
|
||||
>> lua_settable should check stack space (it could call a T.M.)
|
||||
(by lhf & celes; since 3.2; it was already fixed by fixed stack)
|
||||
|
||||
** lparser.c
|
||||
Mon Apr 3 09:59:06 EST 2000
|
||||
>> '%' should be in expfollow
|
||||
(by Edgar Toernig; since 3.1; it was already fixed)
|
||||
|
||||
** lbuiltin.c
|
||||
Mon Apr 3 10:05:05 EST 2000
|
||||
>> tostring() without arguments gives seg. fault.
|
||||
(by Edgar Toernig; since 3.0)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0 alpha
|
||||
|
||||
Tested with full test suites (as locked in Mon Apr 24 14:23:11 EST 2000)
|
||||
in the following platforms:
|
||||
* Linux - gcc, g++
|
||||
* AIX - gcc
|
||||
* Solaris - gcc, cc
|
||||
* IRIX - cc, cc-purify
|
||||
* Windows - Visual C++ (.c e .cpp, warning level=4)
|
||||
|
||||
|
||||
** lstrlib.c
|
||||
Tue May 2 15:27:58 EST 2000
|
||||
>> `strfind' gets wrong subject length when there is an offset
|
||||
(by Jon Kleiser; since 4.0a)
|
||||
|
||||
** lparser.c
|
||||
Fri May 12 15:11:12 EST 2000
|
||||
>> first element in a list constructor is not adjusted to one value
|
||||
>> (e.g. «a = {gsub('a','a','')}»)
|
||||
(by Tomas; since 4.0a)
|
||||
|
||||
** lparser.c
|
||||
Wed May 24 14:50:16 EST 2000
|
||||
>> record-constructor starting with an upvalue name gets an error
|
||||
>> (e.g. «local a; function f() x = {a=1} end»)
|
||||
(by Edgar Toernig; since 3.1)
|
||||
|
||||
** lparser.c
|
||||
Tue Aug 29 15:56:05 EST 2000
|
||||
>> error message for `for' uses `while'
|
||||
(since 4.0a; already corrected)
|
||||
|
||||
** lgc.c
|
||||
Tue Aug 29 15:57:41 EST 2000
|
||||
>> gc tag method for nil could call line hook
|
||||
(by ry; since ?)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0 Beta
|
||||
|
||||
** liolib.c
|
||||
Fri Sep 22 15:12:37 EST 2000
|
||||
>> `read("*w")' should return nil at EOF
|
||||
(by roberto; since 4.0b)
|
||||
|
||||
** lvm.c
|
||||
Mon Sep 25 11:47:48 EST 2000
|
||||
>> lua_gettable does not get key from stack top
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lgc.c
|
||||
Mon Sep 25 11:50:48 EST 2000
|
||||
>> GC may crash when checking locked C closures
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lapi.c
|
||||
Wed Sep 27 09:50:19 EST 2000
|
||||
>> lua_tag should return LUA_NOTAG for non-valid indices
|
||||
(by Paul Hankin; since 4.0b)
|
||||
|
||||
** llex.h / llex.c / lparser.c
|
||||
Wed Sep 27 13:39:45 EST 2000
|
||||
>> parser overwrites semantic information when looking ahead
|
||||
>> (e.g. «a = {print'foo'}»)
|
||||
(by Edgar Toernig; since 4.0b, deriving from previous bug)
|
||||
|
||||
** liolib.c
|
||||
Thu Oct 26 10:50:46 EDT 2000
|
||||
>> in function `read_file', realloc() doesn't free the buffer if it can't
|
||||
>> allocate new memory
|
||||
(by Mauro Vezzosi; since 4.0b)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0
|
||||
|
||||
** lparser.c
|
||||
Wed Nov 29 09:51:44 EDT 2000
|
||||
>> parser does not accept a `;' after a `return'
|
||||
(by lhf; since 4.0b)
|
||||
|
||||
** liolib.c
|
||||
Fri Dec 22 15:30:42 EDT 2000
|
||||
>> when `read' fails it must return nil (and not no value)
|
||||
(by cassino; since at least 3.1)
|
||||
|
||||
** lstring.c/lapi.c
|
||||
Thu Feb 1 11:55:45 EDT 2001
|
||||
>> lua_pushuserdata(L, NULL) is buggy
|
||||
(by Edgar Toernig; since 4.0)
|
||||
|
||||
** ldo.c
|
||||
Fri Feb 2 14:06:40 EDT 2001
|
||||
>> «while 1 dostring[[print('hello\n')]] end» never reclaims memory
|
||||
(by Andrew Paton; since 4.0b)
|
||||
|
||||
** lbaselib.c
|
||||
Tue Feb 6 11:57:13 EDT 2001
|
||||
>> ESC (which starts precompiled code) in C is \33, not \27
|
||||
(by Edgar Toernig and lhf; since 4.0b)
|
||||
|
||||
** lparser.c
|
||||
Tue Jul 10 16:59:18 EST 2001
|
||||
>> error message for `%a' gave wrong line number
|
||||
(by Leonardo Constantino; since 4.0)
|
||||
|
||||
|
||||
11
lapi.h
11
lapi.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** auxiliar functions from Lua API
|
||||
** $Id: lapi.h,v 1.19 2000/08/28 17:57:04 roberto Exp roberto $
|
||||
** Auxiliary functions from Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -8,13 +8,10 @@
|
||||
#define lapi_h
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
TObject *luaA_Address (lua_Object o);
|
||||
void luaA_pushobject (TObject *o);
|
||||
void luaA_packresults (void);
|
||||
int luaA_passresults (void);
|
||||
TObject *luaA_index (lua_State *L, int index);
|
||||
void luaA_pushobject (lua_State *L, const TObject *o);
|
||||
|
||||
#endif
|
||||
|
||||
278
lauxlib.c
278
lauxlib.c
@@ -1,101 +1,225 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.8 1998/01/09 15:06:07 roberto Exp $
|
||||
** Auxiliar functions for building Lua libraries
|
||||
** $Id: lauxlib.c,v 1.50 2001/04/23 16:35:45 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Please Notice: This file uses only the oficial API of Lua
|
||||
** Any function declared here could be written as an application
|
||||
** function. With care, these functions can be used by other libraries.
|
||||
/* This file uses only the official API of Lua.
|
||||
** Any function declared here could be written as an application function.
|
||||
** With care, these functions can be used by other libraries.
|
||||
*/
|
||||
#include "lauxlib.h"
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
void luaL_argerror (int numarg, char *extramsg)
|
||||
{
|
||||
char *funcname;
|
||||
lua_getobjname(lua_stackedfunction(0), &funcname);
|
||||
if (funcname == NULL)
|
||||
funcname = "???";
|
||||
if (extramsg == NULL)
|
||||
luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
|
||||
else
|
||||
luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
|
||||
numarg, funcname, extramsg);
|
||||
}
|
||||
|
||||
char *luaL_check_string (int numArg)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(lua_isstring(o), numArg, "string expected");
|
||||
return lua_getstring(o);
|
||||
}
|
||||
|
||||
char *luaL_opt_string (int numArg, char *def)
|
||||
{
|
||||
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||
luaL_check_string(numArg);
|
||||
}
|
||||
|
||||
double luaL_check_number (int numArg)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(lua_isnumber(o), numArg, "number expected");
|
||||
return lua_getnumber(o);
|
||||
}
|
||||
|
||||
|
||||
double luaL_opt_number (int numArg, double def)
|
||||
{
|
||||
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||
luaL_check_number(numArg);
|
||||
}
|
||||
|
||||
|
||||
lua_Object luaL_tablearg (int arg)
|
||||
{
|
||||
lua_Object o = lua_getparam(arg);
|
||||
luaL_arg_check(lua_istable(o), arg, "table expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
lua_Object luaL_functionarg (int arg)
|
||||
{
|
||||
lua_Object o = lua_getparam(arg);
|
||||
luaL_arg_check(lua_isfunction(o), arg, "function expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
lua_Object luaL_nonnullarg (int numArg)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
void luaL_openlib (struct luaL_reg *l, int n)
|
||||
{
|
||||
LUALIB_API int luaL_findstring (const l_char *name, const l_char *const list[]) {
|
||||
int i;
|
||||
lua_open(); /* make sure lua is already open */
|
||||
for (i=0; i<n; i++)
|
||||
lua_register(l[i].name, l[i].func);
|
||||
for (i=0; list[i]; i++)
|
||||
if (strcmp(list[i], name) == 0)
|
||||
return i;
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
LUALIB_API void luaL_argerror (lua_State *L, int narg, const l_char *extramsg) {
|
||||
lua_Debug ar;
|
||||
lua_getstack(L, 0, &ar);
|
||||
lua_getinfo(L, l_s("n"), &ar);
|
||||
if (ar.name == NULL)
|
||||
ar.name = l_s("?");
|
||||
luaL_verror(L, l_s("bad argument #%d to `%.50s' (%.100s)"),
|
||||
narg, ar.name, extramsg);
|
||||
}
|
||||
|
||||
|
||||
void luaL_verror (char *fmt, ...)
|
||||
{
|
||||
char buff[500];
|
||||
static void type_error (lua_State *L, int narg, const l_char *tname) {
|
||||
l_char buff[80];
|
||||
sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg));
|
||||
luaL_argerror(L, narg, buff);
|
||||
}
|
||||
|
||||
|
||||
static void tag_error (lua_State *L, int narg, int tag) {
|
||||
type_error(L, narg, lua_typename(L, tag));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const l_char *mes) {
|
||||
if (space > lua_stackspace(L))
|
||||
luaL_verror(L, l_s("stack overflow (%.30s)"), mes);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
|
||||
if (lua_rawtag(L, narg) != t)
|
||||
tag_error(L, narg, t);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkany (lua_State *L, int narg) {
|
||||
if (lua_rawtag(L, narg) == LUA_TNONE)
|
||||
luaL_argerror(L, narg, l_s("value expected"));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
|
||||
const l_char *name) {
|
||||
if (strcmp(lua_type(L, narg), name) != 0)
|
||||
type_error(L, narg, name);
|
||||
return lua_touserdata(L, narg);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const l_char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
|
||||
const l_char *s = lua_tostring(L, narg);
|
||||
if (!s) tag_error(L, narg, LUA_TSTRING);
|
||||
if (len) *len = lua_strlen(L, narg);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const l_char *luaL_opt_lstr (lua_State *L, int narg, const l_char *def, size_t *len) {
|
||||
if (lua_isnull(L, narg)) {
|
||||
if (len)
|
||||
*len = (def ? strlen(def) : 0);
|
||||
return def;
|
||||
}
|
||||
else return luaL_check_lstr(L, narg, len);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
|
||||
lua_Number d = lua_tonumber(L, narg);
|
||||
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
|
||||
tag_error(L, narg, LUA_TNUMBER);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) {
|
||||
if (lua_isnull(L, narg)) return def;
|
||||
else return luaL_check_number(L, narg);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n) {
|
||||
int i;
|
||||
for (i=0; i<n; i++)
|
||||
lua_register(L, l[i].name, l[i].func);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_verror (lua_State *L, const l_char *fmt, ...) {
|
||||
l_char buff[500];
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
vsprintf(buff, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_error(buff);
|
||||
lua_error(L, buff);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
#define buffempty(B) ((B)->p == (B)->buffer)
|
||||
#define bufflen(B) ((B)->p - (B)->buffer)
|
||||
#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
|
||||
|
||||
#define LIMIT (LUA_MINSTACK/2)
|
||||
|
||||
|
||||
static int emptybuffer (luaL_Buffer *B) {
|
||||
size_t l = bufflen(B);
|
||||
if (l == 0) return 0; /* put nothing on stack */
|
||||
else {
|
||||
lua_pushlstring(B->L, B->buffer, l);
|
||||
B->p = B->buffer;
|
||||
B->level++;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjuststack (luaL_Buffer *B) {
|
||||
if (B->level > 1) {
|
||||
lua_State *L = B->L;
|
||||
int toget = 1; /* number of levels to concat */
|
||||
size_t toplen = lua_strlen(L, -1);
|
||||
do {
|
||||
size_t l = lua_strlen(L, -(toget+1));
|
||||
if (B->level - toget + 1 >= LIMIT || toplen > l) {
|
||||
toplen += l;
|
||||
toget++;
|
||||
}
|
||||
else break;
|
||||
} while (toget < B->level);
|
||||
lua_concat(L, toget);
|
||||
B->level = B->level - toget + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API l_char *luaL_prepbuffer (luaL_Buffer *B) {
|
||||
if (emptybuffer(B))
|
||||
adjuststack(B);
|
||||
return B->buffer;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const l_char *s, size_t l) {
|
||||
while (l--)
|
||||
luaL_putchar(B, *s++);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addstring (luaL_Buffer *B, const l_char *s) {
|
||||
luaL_addlstring(B, s, strlen(s));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
||||
emptybuffer(B);
|
||||
lua_concat(B->L, B->level);
|
||||
B->level = 1;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
|
||||
lua_State *L = B->L;
|
||||
size_t vl = lua_strlen(L, -1);
|
||||
if (vl <= bufffree(B)) { /* fit into buffer? */
|
||||
memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
|
||||
B->p += vl;
|
||||
lua_pop(L, 1); /* remove from stack */
|
||||
}
|
||||
else {
|
||||
if (emptybuffer(B))
|
||||
lua_insert(L, -2); /* put buffer before new value */
|
||||
B->level++; /* add new value into B stack */
|
||||
adjuststack(B);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
|
||||
B->L = L;
|
||||
B->p = B->buffer;
|
||||
B->level = 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
115
lauxlib.h
115
lauxlib.h
@@ -1,43 +1,106 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.5 1997/12/17 20:48:58 roberto Exp roberto $
|
||||
** Auxiliar functions for building Lua libraries
|
||||
** $Id: lauxlib.h,v 1.35 2001/04/06 21:17:37 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef auxlib_h
|
||||
#define auxlib_h
|
||||
#ifndef lauxlib_h
|
||||
#define lauxlib_h
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
struct luaL_reg {
|
||||
char *name;
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API extern
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct luaL_reg {
|
||||
const lua_char *name;
|
||||
lua_CFunction func;
|
||||
};
|
||||
} luaL_reg;
|
||||
|
||||
|
||||
#define luaL_arg_check(cond,numarg,extramsg) if (!(cond)) \
|
||||
luaL_argerror(numarg,extramsg)
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n);
|
||||
LUALIB_API void luaL_argerror (lua_State *L, int numarg,
|
||||
const lua_char *extramsg);
|
||||
LUALIB_API const lua_char *luaL_check_lstr (lua_State *L, int numArg,
|
||||
size_t *len);
|
||||
LUALIB_API const lua_char *luaL_opt_lstr (lua_State *L, int numArg,
|
||||
const lua_char *def, size_t *len);
|
||||
LUALIB_API lua_Number luaL_check_number (lua_State *L, int numArg);
|
||||
LUALIB_API lua_Number luaL_opt_number (lua_State *L, int nArg, lua_Number def);
|
||||
|
||||
void luaL_openlib (struct luaL_reg *l, int n);
|
||||
void luaL_argerror (int numarg, char *extramsg);
|
||||
char *luaL_check_string (int numArg);
|
||||
char *luaL_opt_string (int numArg, char *def);
|
||||
double luaL_check_number (int numArg);
|
||||
double luaL_opt_number (int numArg, double def);
|
||||
lua_Object luaL_functionarg (int arg);
|
||||
lua_Object luaL_tablearg (int arg);
|
||||
lua_Object luaL_nonnullarg (int numArg);
|
||||
void luaL_verror (char *fmt, ...);
|
||||
char *luaL_openspace (int size);
|
||||
void luaL_resetbuffer (void);
|
||||
void luaL_addchar (int c);
|
||||
void luaL_addsize (int n);
|
||||
int luaL_newbuffer (int size);
|
||||
void luaL_oldbuffer (int old);
|
||||
char *luaL_buffer (void);
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const lua_char *msg);
|
||||
LUALIB_API void luaL_checktype (lua_State *L, int narg, int t);
|
||||
LUALIB_API void luaL_checkany (lua_State *L, int narg);
|
||||
LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
|
||||
const lua_char *name);
|
||||
|
||||
LUALIB_API void luaL_verror (lua_State *L, const lua_char *fmt, ...);
|
||||
LUALIB_API int luaL_findstring (const lua_char *name,
|
||||
const lua_char *const list[]);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** some useful macros
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#define luaL_arg_check(L, cond,numarg,extramsg) if (!(cond)) \
|
||||
luaL_argerror(L, numarg,extramsg)
|
||||
#define luaL_check_string(L,n) (luaL_check_lstr(L, (n), NULL))
|
||||
#define luaL_opt_string(L,n,d) (luaL_opt_lstr(L, (n), (d), NULL))
|
||||
#define luaL_check_int(L,n) ((int)luaL_check_number(L, n))
|
||||
#define luaL_check_long(L,n) ((long)luaL_check_number(L, n))
|
||||
#define luaL_opt_int(L,n,d) ((int)luaL_opt_number(L, n,d))
|
||||
#define luaL_opt_long(L,n,d) ((long)luaL_opt_number(L, n,d))
|
||||
#define luaL_openl(L,a) luaL_openlib(L, a, (sizeof(a)/sizeof(a[0])))
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LUAL_BUFFERSIZE
|
||||
#define LUAL_BUFFERSIZE BUFSIZ
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct luaL_Buffer {
|
||||
lua_char *p; /* current position in buffer */
|
||||
int level;
|
||||
lua_State *L;
|
||||
lua_char buffer[LUAL_BUFFERSIZE];
|
||||
} luaL_Buffer;
|
||||
|
||||
#define luaL_putchar(B,c) \
|
||||
((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
|
||||
(*(B)->p++ = (lua_char)(c)))
|
||||
|
||||
#define luaL_addsize(B,n) ((B)->p += (n))
|
||||
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B);
|
||||
LUALIB_API lua_char *luaL_prepbuffer (luaL_Buffer *B);
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const lua_char *s, size_t l);
|
||||
LUALIB_API void luaL_addstring (luaL_Buffer *B, const lua_char *s);
|
||||
LUALIB_API void luaL_addvalue (luaL_Buffer *B);
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B);
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
745
lbaselib.c
Normal file
745
lbaselib.c
Normal file
@@ -0,0 +1,745 @@
|
||||
/*
|
||||
** $Id: lbaselib.c,v 1.38 2001/06/20 17:25:30 roberto Exp roberto $
|
||||
** Basic library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static void aux_setn (lua_State *L, int t, int n) {
|
||||
lua_pushliteral(L, l_s("n"));
|
||||
lua_pushnumber(L, n);
|
||||
lua_settable(L, t);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stderr', redefine this function, or
|
||||
** redefine _ERRORMESSAGE so that it won't need _ALERT.
|
||||
*/
|
||||
static int luaB__ALERT (lua_State *L) {
|
||||
fputs(luaL_check_string(L, 1), stderr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Basic implementation of _ERRORMESSAGE.
|
||||
** The library `liolib' redefines _ERRORMESSAGE for better error information.
|
||||
*/
|
||||
static int luaB__ERRORMESSAGE (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TSTRING);
|
||||
lua_getglobal(L, l_s(LUA_ALERT));
|
||||
if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
|
||||
lua_Debug ar;
|
||||
lua_pushliteral(L, l_s("error: "));
|
||||
lua_pushvalue(L, 1);
|
||||
if (lua_getstack(L, 1, &ar)) {
|
||||
lua_getinfo(L, l_s("Sl"), &ar);
|
||||
if (ar.source && ar.currentline > 0) {
|
||||
l_char buff[100];
|
||||
sprintf(buff, l_s("\n <%.70s: line %d>"), ar.short_src, ar.currentline);
|
||||
lua_pushstring(L, buff);
|
||||
lua_concat(L, 2);
|
||||
}
|
||||
}
|
||||
lua_pushliteral(L, l_s("\n"));
|
||||
lua_concat(L, 3);
|
||||
lua_rawcall(L, 1, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stdout', you can just remove this function.
|
||||
** If you need, you can define your own `print' function, following this
|
||||
** model but changing `fputs' to put the strings at a proper place
|
||||
** (a console window or a log file, for instance).
|
||||
*/
|
||||
static int luaB_print (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
int i;
|
||||
lua_getglobal(L, l_s("tostring"));
|
||||
for (i=1; i<=n; i++) {
|
||||
const l_char *s;
|
||||
lua_pushvalue(L, -1); /* function to be called */
|
||||
lua_pushvalue(L, i); /* value to print */
|
||||
lua_rawcall(L, 1, 1);
|
||||
s = lua_tostring(L, -1); /* get result */
|
||||
if (s == NULL)
|
||||
lua_error(L, l_s("`tostring' must return a string to `print'"));
|
||||
if (i>1) fputs(l_s("\t"), stdout);
|
||||
fputs(s, stdout);
|
||||
lua_pop(L, 1); /* pop result */
|
||||
}
|
||||
fputs(l_s("\n"), stdout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tonumber (lua_State *L) {
|
||||
int base = luaL_opt_int(L, 2, 10);
|
||||
if (base == 10) { /* standard conversion */
|
||||
luaL_checkany(L, 1);
|
||||
if (lua_isnumber(L, 1)) {
|
||||
lua_pushnumber(L, lua_tonumber(L, 1));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const l_char *s1 = luaL_check_string(L, 1);
|
||||
l_char *s2;
|
||||
unsigned long n;
|
||||
luaL_arg_check(L, 2 <= base && base <= 36, 2, l_s("base out of range"));
|
||||
n = strtoul(s1, &s2, base);
|
||||
if (s1 != s2) { /* at least one valid digit? */
|
||||
while (isspace(uchar(*s2))) s2++; /* skip trailing spaces */
|
||||
if (*s2 == l_c('\0')) { /* no invalid trailing characters? */
|
||||
lua_pushnumber(L, n);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_pushnil(L); /* else not a number */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_error (lua_State *L) {
|
||||
lua_error(L, luaL_opt_string(L, 1, NULL));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
static int luaB_setglobal (lua_State *L) {
|
||||
luaL_checkany(L, 2);
|
||||
lua_setglobal(L, luaL_check_string(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int luaB_getglobal (lua_State *L) {
|
||||
lua_getglobal(L, luaL_check_string(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* auxiliary function to get `tags' */
|
||||
static int gettag (lua_State *L, int narg) {
|
||||
switch (lua_rawtag(L, narg)) {
|
||||
case LUA_TNUMBER:
|
||||
return (int)lua_tonumber(L, narg);
|
||||
case LUA_TSTRING: {
|
||||
const l_char *name = lua_tostring(L, narg);
|
||||
int tag = lua_name2tag(L, name);
|
||||
if (tag == LUA_TNONE)
|
||||
luaL_verror(L, l_s("'%.30s' is not a valid type name"), name);
|
||||
return tag;
|
||||
}
|
||||
default:
|
||||
luaL_argerror(L, narg, l_s("tag or type name expected"));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tag (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushnumber(L, lua_tag(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_settype (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushvalue(L, 1); /* push table */
|
||||
lua_settag(L, gettag(L, 2));
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
static int luaB_weakmode (lua_State *L) {
|
||||
const l_char *mode = luaL_check_string(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
if (*mode == l_c('?')) {
|
||||
l_char buff[3];
|
||||
l_char *s = buff;
|
||||
int imode = lua_getweakmode(L, 1);
|
||||
if (imode & LUA_WEAK_KEY) *s++ = 'k';
|
||||
if (imode & LUA_WEAK_VALUE) *s++ = 'v';
|
||||
*s = '\0';
|
||||
lua_pushstring(L, buff);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int imode = 0;
|
||||
if (strchr(mode, l_c('k'))) imode |= LUA_WEAK_KEY;
|
||||
if (strchr(mode, l_c('v'))) imode |= LUA_WEAK_VALUE;
|
||||
lua_pushvalue(L, 1); /* push table */
|
||||
lua_setweakmode(L, imode);
|
||||
return 1; /* return the table */
|
||||
}
|
||||
}
|
||||
|
||||
static int luaB_newtype (lua_State *L) {
|
||||
const l_char *name = luaL_opt_string(L, 1, NULL);
|
||||
lua_pushnumber(L, lua_newtype(L, name, LUA_TTABLE));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_copytagmethods (lua_State *L) {
|
||||
lua_pushnumber(L, lua_copytagmethods(L, gettag(L, 1), gettag(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_globals (lua_State *L) {
|
||||
lua_getglobals(L); /* value to be returned */
|
||||
if (!lua_isnull(L, 1)) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushvalue(L, 1); /* new table of globals */
|
||||
lua_setglobals(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_rawget (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checkany(L, 2);
|
||||
lua_rawget(L, -2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_rawset (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checkany(L, 2);
|
||||
luaL_checkany(L, 3);
|
||||
lua_rawset(L, -3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_settagmethod (lua_State *L) {
|
||||
int tag = gettag(L, 1);
|
||||
const l_char *event = luaL_check_string(L, 2);
|
||||
luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
|
||||
l_s("function or nil expected"));
|
||||
if (strcmp(event, l_s("gc")) == 0)
|
||||
lua_error(L, l_s("deprecated use: cannot set the `gc' tag method from Lua"));
|
||||
lua_gettagmethod(L, tag, event);
|
||||
lua_pushvalue(L, 3);
|
||||
lua_settagmethod(L, tag, event);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_gettagmethod (lua_State *L) {
|
||||
int tag = gettag(L, 1);
|
||||
const l_char *event = luaL_check_string(L, 2);
|
||||
if (strcmp(event, l_s("gc")) == 0)
|
||||
lua_error(L, l_s("deprecated use: cannot get the `gc' tag method from Lua"));
|
||||
lua_gettagmethod(L, tag, event);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_gcinfo (lua_State *L) {
|
||||
lua_pushnumber(L, lua_getgccount(L));
|
||||
lua_pushnumber(L, lua_getgcthreshold(L));
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_collectgarbage (lua_State *L) {
|
||||
lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_type (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushstring(L, lua_type(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_rawtype (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushstring(L, lua_tag2name(L, lua_rawtag(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_next (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
|
||||
if (lua_next(L, 1))
|
||||
return 2;
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int passresults (lua_State *L, int status, int oldtop) {
|
||||
static const l_char *const errornames[] =
|
||||
{l_s("ok"), l_s("run-time error"), l_s("file error"), l_s("syntax error"),
|
||||
l_s("memory error"), l_s("error in error handling")};
|
||||
if (status == 0) {
|
||||
int nresults = lua_gettop(L) - oldtop;
|
||||
if (nresults > 0)
|
||||
return nresults; /* results are already on the stack */
|
||||
else {
|
||||
lua_newuserdatabox(L, NULL); /* at least one result to signal no errors */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else { /* error */
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, errornames[status]); /* error code */
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_dostring (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
size_t l;
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l);
|
||||
const l_char *chunkname = luaL_opt_string(L, 2, s);
|
||||
return passresults(L, lua_dobuffer(L, s, l, chunkname), oldtop);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadstring (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
size_t l;
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l);
|
||||
const l_char *chunkname = luaL_opt_string(L, 2, s);
|
||||
return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop);
|
||||
}
|
||||
|
||||
static int luaB_dofile (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
const l_char *fname = luaL_opt_string(L, 1, NULL);
|
||||
return passresults(L, lua_dofile(L, fname), oldtop);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadfile (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
const l_char *fname = luaL_opt_string(L, 1, NULL);
|
||||
return passresults(L, lua_loadfile(L, fname), oldtop);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define LUA_PATH l_s("LUA_PATH")
|
||||
|
||||
#define LUA_PATH_SEP l_s(";")
|
||||
|
||||
#ifndef LUA_PATH_DEFAULT
|
||||
#define LUA_PATH_DEFAULT l_s("./")
|
||||
#endif
|
||||
|
||||
static int luaB_require (lua_State *L) {
|
||||
const l_char *path;
|
||||
luaL_check_string(L, 1);
|
||||
lua_settop(L, 1);
|
||||
lua_getglobal(L, LUA_PATH); /* get path */
|
||||
if (lua_isstring(L, 2)) /* is LUA_PATH defined? */
|
||||
path = lua_tostring(L, 2);
|
||||
else { /* LUA_PATH not defined */
|
||||
lua_pop(L, 1); /* pop old global value */
|
||||
path = getenv(LUA_PATH); /* try environment variable */
|
||||
if (path == NULL) path = LUA_PATH_DEFAULT; /* else use default */
|
||||
lua_pushstring(L, path);
|
||||
lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */
|
||||
lua_setglobal(L, LUA_PATH);
|
||||
}
|
||||
lua_getregistry(L);
|
||||
lua_pushliteral(L, LUA_PATH);
|
||||
lua_gettable(L, 3); /* get book-keeping table */
|
||||
if (lua_isnil(L, 4)) { /* no book-keeping table? */
|
||||
lua_pop(L, 1); /* pop the `nil' */
|
||||
lua_newtable(L); /* create book-keeping table */
|
||||
lua_pushliteral(L, LUA_PATH);
|
||||
lua_pushvalue(L, -2); /* duplicate table to leave a copy on stack */
|
||||
lua_settable(L, 3); /* store book-keeping table in registry */
|
||||
}
|
||||
lua_pushvalue(L, 1);
|
||||
lua_gettable(L, 4); /* check package's name in book-keeping table */
|
||||
if (!lua_isnil(L, -1)) /* is it there? */
|
||||
return 0; /* package is already loaded */
|
||||
else { /* must load it */
|
||||
for (;;) { /* traverse path */
|
||||
int res;
|
||||
int l = strcspn(path, LUA_PATH_SEP); /* find separator */
|
||||
lua_pushlstring(L, path, l); /* directory name */
|
||||
lua_pushvalue(L, 1); /* package name */
|
||||
lua_concat(L, 2); /* concat directory with package name */
|
||||
res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */
|
||||
lua_settop(L, 4); /* pop string and eventual results from dofile */
|
||||
if (res == 0) break; /* ok; file done */
|
||||
else if (res != LUA_ERRFILE)
|
||||
lua_error(L, NULL); /* error running package; propagate it */
|
||||
if (*(path+l) == l_c('\0')) /* no more directories? */
|
||||
luaL_verror(L, l_s("could not load package `%.20s' from path `%.200s'"),
|
||||
lua_tostring(L, 1), lua_tostring(L, 2));
|
||||
path += l+1; /* try next directory */
|
||||
}
|
||||
}
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushnumber(L, 1);
|
||||
lua_settable(L, 4); /* mark it as loaded */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int aux_unpack (lua_State *L, int arg) {
|
||||
int n, i;
|
||||
luaL_checktype(L, arg, LUA_TTABLE);
|
||||
n = lua_getn(L, arg);
|
||||
luaL_checkstack(L, n, l_s("table too big to unpack"));
|
||||
for (i=1; i<=n; i++) /* push arg[1...n] */
|
||||
lua_rawgeti(L, arg, i);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_unpack (lua_State *L) {
|
||||
return aux_unpack(L, 1);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_call (lua_State *L) {
|
||||
int oldtop;
|
||||
const l_char *options = luaL_opt_string(L, 3, l_s(""));
|
||||
int err = 0; /* index of old error method */
|
||||
int status;
|
||||
int n;
|
||||
if (!lua_isnull(L, 4)) { /* set new error method */
|
||||
lua_getglobal(L, l_s(LUA_ERRORMESSAGE));
|
||||
err = lua_gettop(L); /* get index */
|
||||
lua_pushvalue(L, 4);
|
||||
lua_setglobal(L, l_s(LUA_ERRORMESSAGE));
|
||||
}
|
||||
oldtop = lua_gettop(L); /* top before function-call preparation */
|
||||
/* push function */
|
||||
lua_pushvalue(L, 1);
|
||||
n = aux_unpack(L, 2); /* push arg[1...n] */
|
||||
status = lua_call(L, n, LUA_MULTRET);
|
||||
if (err != 0) { /* restore old error method */
|
||||
lua_pushvalue(L, err);
|
||||
lua_setglobal(L, l_s(LUA_ERRORMESSAGE));
|
||||
}
|
||||
if (status != 0) { /* error in call? */
|
||||
if (strchr(options, l_c('x')))
|
||||
lua_pushnil(L); /* return nil to signal the error */
|
||||
else
|
||||
lua_error(L, NULL); /* propagate error without additional messages */
|
||||
return 1;
|
||||
}
|
||||
if (strchr(options, l_c('p'))) /* pack results? */
|
||||
lua_error(L, l_s("deprecated option `p' in `call'"));
|
||||
return lua_gettop(L) - oldtop; /* results are already on the stack */
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tostring (lua_State *L) {
|
||||
l_char buff[64];
|
||||
switch (lua_rawtag(L, 1)) {
|
||||
case LUA_TNUMBER:
|
||||
lua_pushstring(L, lua_tostring(L, 1));
|
||||
return 1;
|
||||
case LUA_TSTRING:
|
||||
lua_pushvalue(L, 1);
|
||||
return 1;
|
||||
case LUA_TTABLE:
|
||||
sprintf(buff, l_s("%.40s: %p"), lua_type(L, 1), lua_topointer(L, 1));
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
sprintf(buff, l_s("function: %p"), lua_topointer(L, 1));
|
||||
break;
|
||||
case LUA_TUSERDATA: {
|
||||
const l_char *t = lua_type(L, 1);
|
||||
if (strcmp(t, l_s("userdata")) == 0)
|
||||
sprintf(buff, l_s("userdata(%d): %p"), lua_tag(L, 1),
|
||||
lua_touserdata(L, 1));
|
||||
else
|
||||
sprintf(buff, l_s("%.40s: %p"), t, lua_touserdata(L, 1));
|
||||
break;
|
||||
}
|
||||
case LUA_TNIL:
|
||||
lua_pushliteral(L, l_s("nil"));
|
||||
return 1;
|
||||
default:
|
||||
luaL_argerror(L, 1, l_s("value expected"));
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_foreachi (lua_State *L) {
|
||||
int n, i;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
n = lua_getn(L, 1);
|
||||
for (i=1; i<=n; i++) {
|
||||
lua_pushvalue(L, 2); /* function */
|
||||
lua_pushnumber(L, i); /* 1st argument */
|
||||
lua_rawgeti(L, 1, i); /* 2nd argument */
|
||||
lua_rawcall(L, 2, 1);
|
||||
if (!lua_isnil(L, -1))
|
||||
return 1;
|
||||
lua_pop(L, 1); /* remove nil result */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_foreach (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
lua_pushnil(L); /* first index */
|
||||
for (;;) {
|
||||
if (lua_next(L, 1) == 0)
|
||||
return 0;
|
||||
lua_pushvalue(L, 2); /* function */
|
||||
lua_pushvalue(L, -3); /* key */
|
||||
lua_pushvalue(L, -3); /* value */
|
||||
lua_rawcall(L, 2, 1);
|
||||
if (!lua_isnil(L, -1))
|
||||
return 1;
|
||||
lua_pop(L, 2); /* remove value and result */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_assert (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
if (lua_isnil(L, 1))
|
||||
luaL_verror(L, l_s("assertion failed! %.90s"), luaL_opt_string(L, 2, l_s("")));
|
||||
lua_settop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getn (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushnumber(L, lua_getn(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tinsert (lua_State *L) {
|
||||
int v = lua_gettop(L); /* last argument: to be inserted */
|
||||
int n, pos;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = lua_getn(L, 1);
|
||||
if (v == 2) /* called with only 2 arguments */
|
||||
pos = n+1;
|
||||
else
|
||||
pos = luaL_check_int(L, 2); /* 2nd argument is the position */
|
||||
aux_setn(L, 1, n+1); /* t.n = n+1 */
|
||||
for (; n>=pos; n--) {
|
||||
lua_rawgeti(L, 1, n);
|
||||
lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
|
||||
}
|
||||
lua_pushvalue(L, v);
|
||||
lua_rawseti(L, 1, pos); /* t[pos] = v */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tremove (lua_State *L) {
|
||||
int pos, n;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = lua_getn(L, 1);
|
||||
pos = luaL_opt_int(L, 2, n);
|
||||
if (n <= 0) return 0; /* table is `empty' */
|
||||
aux_setn(L, 1, n-1); /* t.n = n-1 */
|
||||
lua_rawgeti(L, 1, pos); /* result = t[pos] */
|
||||
for ( ;pos<n; pos++) {
|
||||
lua_rawgeti(L, 1, pos+1);
|
||||
lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
|
||||
}
|
||||
lua_pushnil(L);
|
||||
lua_rawseti(L, 1, n); /* t[n] = nil */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Quicksort
|
||||
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
|
||||
** Addison-Wesley, 1993.)
|
||||
*/
|
||||
|
||||
|
||||
static void set2 (lua_State *L, int i, int j) {
|
||||
lua_rawseti(L, 1, i);
|
||||
lua_rawseti(L, 1, j);
|
||||
}
|
||||
|
||||
static int sort_comp (lua_State *L, int a, int b) {
|
||||
/* WARNING: the caller (auxsort) must ensure stack space */
|
||||
if (!lua_isnil(L, 2)) { /* function? */
|
||||
int res;
|
||||
lua_pushvalue(L, 2);
|
||||
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
||||
lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
|
||||
lua_rawcall(L, 2, 1);
|
||||
res = !lua_isnil(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
}
|
||||
else /* a < b? */
|
||||
return lua_lessthan(L, a, b);
|
||||
}
|
||||
|
||||
static void auxsort (lua_State *L, int l, int u) {
|
||||
while (l < u) { /* for tail recursion */
|
||||
int i, j;
|
||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||
lua_rawgeti(L, 1, l);
|
||||
lua_rawgeti(L, 1, u);
|
||||
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
|
||||
set2(L, l, u); /* swap a[l] - a[u] */
|
||||
else
|
||||
lua_pop(L, 2);
|
||||
if (u-l == 1) break; /* only 2 elements */
|
||||
i = (l+u)/2;
|
||||
lua_rawgeti(L, 1, i);
|
||||
lua_rawgeti(L, 1, l);
|
||||
if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
|
||||
set2(L, i, l);
|
||||
else {
|
||||
lua_pop(L, 1); /* remove a[l] */
|
||||
lua_rawgeti(L, 1, u);
|
||||
if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
|
||||
set2(L, i, u);
|
||||
else
|
||||
lua_pop(L, 2);
|
||||
}
|
||||
if (u-l == 2) break; /* only 3 elements */
|
||||
lua_rawgeti(L, 1, i); /* Pivot */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_rawgeti(L, 1, u-1);
|
||||
set2(L, i, u-1);
|
||||
/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
|
||||
i = l; j = u-1;
|
||||
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
|
||||
/* repeat ++i until a[i] >= P */
|
||||
while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
||||
if (i>u) lua_error(L, l_s("invalid order function for sorting"));
|
||||
lua_pop(L, 1); /* remove a[i] */
|
||||
}
|
||||
/* repeat --j until a[j] <= P */
|
||||
while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
|
||||
if (j<l) lua_error(L, l_s("invalid order function for sorting"));
|
||||
lua_pop(L, 1); /* remove a[j] */
|
||||
}
|
||||
if (j<i) {
|
||||
lua_pop(L, 3); /* pop pivot, a[i], a[j] */
|
||||
break;
|
||||
}
|
||||
set2(L, i, j);
|
||||
}
|
||||
lua_rawgeti(L, 1, u-1);
|
||||
lua_rawgeti(L, 1, i);
|
||||
set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
|
||||
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
|
||||
/* adjust so that smaller half is in [j..i] and larger one in [l..u] */
|
||||
if (i-l < u-i) {
|
||||
j=l; i=i-1; l=i+2;
|
||||
}
|
||||
else {
|
||||
j=i+1; i=u; u=j-2;
|
||||
}
|
||||
auxsort(L, j, i); /* call recursively the smaller one */
|
||||
} /* repeat the routine for the larger one */
|
||||
}
|
||||
|
||||
static int luaB_sort (lua_State *L) {
|
||||
int n;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = lua_getn(L, 1);
|
||||
if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
lua_settop(L, 2); /* make sure there is two arguments */
|
||||
auxsort(L, 1, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
static const luaL_reg base_funcs[] = {
|
||||
{l_s(LUA_ALERT), luaB__ALERT},
|
||||
{l_s(LUA_ERRORMESSAGE), luaB__ERRORMESSAGE},
|
||||
{l_s("call"), luaB_call},
|
||||
{l_s("collectgarbage"), luaB_collectgarbage},
|
||||
{l_s("copytagmethods"), luaB_copytagmethods},
|
||||
{l_s("dofile"), luaB_dofile},
|
||||
{l_s("dostring"), luaB_dostring},
|
||||
{l_s("error"), luaB_error},
|
||||
{l_s("foreach"), luaB_foreach},
|
||||
{l_s("foreachi"), luaB_foreachi},
|
||||
{l_s("gcinfo"), luaB_gcinfo},
|
||||
{l_s("getglobal"), luaB_getglobal},
|
||||
{l_s("gettagmethod"), luaB_gettagmethod},
|
||||
{l_s("globals"), luaB_globals},
|
||||
{l_s("loadfile"), luaB_loadfile},
|
||||
{l_s("loadstring"), luaB_loadstring},
|
||||
{l_s("newtype"), luaB_newtype},
|
||||
{l_s("newtag"), luaB_newtype}, /* for compatibility 4.0 */
|
||||
{l_s("next"), luaB_next},
|
||||
{l_s("print"), luaB_print},
|
||||
{l_s("rawget"), luaB_rawget},
|
||||
{l_s("rawset"), luaB_rawset},
|
||||
{l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */
|
||||
{l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */
|
||||
{l_s("rawtype"), luaB_rawtype},
|
||||
{l_s("require"), luaB_require},
|
||||
{l_s("setglobal"), luaB_setglobal},
|
||||
{l_s("settag"), luaB_settype}, /* for compatibility 4.0 */
|
||||
{l_s("settype"), luaB_settype},
|
||||
{l_s("settagmethod"), luaB_settagmethod},
|
||||
{l_s("tag"), luaB_tag},
|
||||
{l_s("tonumber"), luaB_tonumber},
|
||||
{l_s("tostring"), luaB_tostring},
|
||||
{l_s("type"), luaB_type},
|
||||
{l_s("assert"), luaB_assert},
|
||||
{l_s("getn"), luaB_getn},
|
||||
{l_s("sort"), luaB_sort},
|
||||
{l_s("tinsert"), luaB_tinsert},
|
||||
{l_s("tremove"), luaB_tremove},
|
||||
{l_s("unpack"), luaB_unpack},
|
||||
{l_s("weakmode"), luaB_weakmode}
|
||||
};
|
||||
|
||||
|
||||
|
||||
LUALIB_API int lua_baselibopen (lua_State *L) {
|
||||
luaL_openl(L, base_funcs);
|
||||
lua_pushliteral(L, l_s(LUA_VERSION));
|
||||
lua_setglobal(L, l_s("_VERSION"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
81
lbuffer.c
81
lbuffer.c
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** Auxiliar functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*-------------------------------------------------------
|
||||
** Auxiliar buffer
|
||||
-------------------------------------------------------*/
|
||||
|
||||
#define BUFF_STEP 32
|
||||
|
||||
#define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size)
|
||||
|
||||
static void Openspace (int size)
|
||||
{
|
||||
LState *l = L; /* to optimize */
|
||||
int base = l->Mbuffbase-l->Mbuffer;
|
||||
l->Mbuffsize *= 2;
|
||||
if (l->Mbuffnext+size > l->Mbuffsize) /* still not big enough? */
|
||||
l->Mbuffsize = l->Mbuffnext+size;
|
||||
l->Mbuffer = luaM_realloc(l->Mbuffer, l->Mbuffsize);
|
||||
l->Mbuffbase = l->Mbuffer+base;
|
||||
}
|
||||
|
||||
|
||||
char *luaL_openspace (int size)
|
||||
{
|
||||
openspace(size);
|
||||
return L->Mbuffer+L->Mbuffnext;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addchar (int c)
|
||||
{
|
||||
openspace(BUFF_STEP);
|
||||
L->Mbuffer[L->Mbuffnext++] = c;
|
||||
}
|
||||
|
||||
|
||||
void luaL_resetbuffer (void)
|
||||
{
|
||||
L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addsize (int n)
|
||||
{
|
||||
L->Mbuffnext += n;
|
||||
}
|
||||
|
||||
|
||||
int luaL_newbuffer (int size)
|
||||
{
|
||||
int old = L->Mbuffbase-L->Mbuffer;
|
||||
openspace(size);
|
||||
L->Mbuffbase = L->Mbuffer+L->Mbuffnext;
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
void luaL_oldbuffer (int old)
|
||||
{
|
||||
L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
|
||||
L->Mbuffbase = L->Mbuffer+old;
|
||||
}
|
||||
|
||||
|
||||
char *luaL_buffer (void)
|
||||
{
|
||||
return L->Mbuffbase;
|
||||
}
|
||||
|
||||
516
lbuiltin.c
516
lbuiltin.c
@@ -1,516 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuiltin.c,v 1.21 1998/01/02 17:46:32 roberto Exp roberto $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lbuiltin.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
|
||||
static void pushstring (TaggedString *s)
|
||||
{
|
||||
TObject o;
|
||||
o.ttype = LUA_T_STRING;
|
||||
o.value.ts = s;
|
||||
luaA_pushobject(&o);
|
||||
}
|
||||
|
||||
|
||||
static void nextvar (void)
|
||||
{
|
||||
TObject *o = luaA_Address(luaL_nonnullarg(1));
|
||||
TaggedString *g;
|
||||
if (ttype(o) == LUA_T_NIL)
|
||||
g = (TaggedString *)L->rootglobal.next;
|
||||
else {
|
||||
luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
|
||||
g = tsvalue(o);
|
||||
/* check whether name is in global var list */
|
||||
luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
|
||||
g = (TaggedString *)g->head.next;
|
||||
}
|
||||
while (g && g->u.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
|
||||
g = (TaggedString *)g->head.next;
|
||||
if (g) {
|
||||
pushstring(g);
|
||||
luaA_pushobject(&g->u.globalval);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void foreachvar (void)
|
||||
{
|
||||
TObject f = *luaA_Address(luaL_functionarg(1));
|
||||
GCnode *g;
|
||||
StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */
|
||||
ttype(L->stack.stack+name) = LUA_T_NIL;
|
||||
L->stack.top++;
|
||||
for (g = L->rootglobal.next; g; g = g->next) {
|
||||
TaggedString *s = (TaggedString *)g;
|
||||
if (s->u.globalval.ttype != LUA_T_NIL) {
|
||||
ttype(L->stack.stack+name) = LUA_T_STRING;
|
||||
tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */
|
||||
luaA_pushobject(&f);
|
||||
pushstring(s);
|
||||
luaA_pushobject(&s->u.globalval);
|
||||
luaD_call((L->stack.top-L->stack.stack)-2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL)
|
||||
return;
|
||||
L->stack.top--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void next (void)
|
||||
{
|
||||
lua_Object o = luaL_tablearg(1);
|
||||
lua_Object r = luaL_nonnullarg(2);
|
||||
Node *n = luaH_next(luaA_Address(o), luaA_Address(r));
|
||||
if (n) {
|
||||
luaA_pushobject(&n->ref);
|
||||
luaA_pushobject(&n->val);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void foreach (void)
|
||||
{
|
||||
TObject t = *luaA_Address(luaL_tablearg(1));
|
||||
TObject f = *luaA_Address(luaL_functionarg(2));
|
||||
int i;
|
||||
for (i=0; i<avalue(&t)->nhash; i++) {
|
||||
Node *nd = &(avalue(&t)->node[i]);
|
||||
if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) {
|
||||
luaA_pushobject(&f);
|
||||
luaA_pushobject(ref(nd));
|
||||
luaA_pushobject(val(nd));
|
||||
luaD_call((L->stack.top-L->stack.stack)-2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL)
|
||||
return;
|
||||
L->stack.top--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void internaldostring (void)
|
||||
{
|
||||
if (lua_getparam(2) != LUA_NOOBJECT)
|
||||
lua_error("invalid 2nd argument (probably obsolete code)");
|
||||
if (lua_dostring(luaL_check_string(1)) == 0)
|
||||
if (luaA_passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
|
||||
static void internaldofile (void)
|
||||
{
|
||||
char *fname = luaL_opt_string(1, NULL);
|
||||
if (lua_dofile(fname) == 0)
|
||||
if (luaA_passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
|
||||
static char *to_string (lua_Object obj)
|
||||
{
|
||||
char *buff = luaL_openspace(30);
|
||||
TObject *o = luaA_Address(obj);
|
||||
switch (ttype(o)) {
|
||||
case LUA_T_NUMBER: case LUA_T_STRING:
|
||||
return lua_getstring(obj);
|
||||
case LUA_T_ARRAY: {
|
||||
sprintf(buff, "table: %p", (void *)o->value.a);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_CLOSURE: {
|
||||
sprintf(buff, "function: %p", (void *)o->value.cl);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_PROTO: {
|
||||
sprintf(buff, "function: %p", (void *)o->value.tf);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_CPROTO: {
|
||||
sprintf(buff, "function: %p", (void *)o->value.f);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_USERDATA: {
|
||||
sprintf(buff, "userdata: %p", o->value.ts->u.d.v);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_NIL:
|
||||
return "nil";
|
||||
default:
|
||||
lua_error("internal error");
|
||||
return NULL; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
static void bi_tostring (void)
|
||||
{
|
||||
lua_pushstring(to_string(lua_getparam(1)));
|
||||
}
|
||||
|
||||
|
||||
static void luaI_print (void)
|
||||
{
|
||||
int i = 1;
|
||||
lua_Object obj;
|
||||
while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
|
||||
printf("%s\t", to_string(obj));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
static void luaI_type (void)
|
||||
{
|
||||
lua_Object o = luaL_nonnullarg(1);
|
||||
lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]);
|
||||
lua_pushnumber(lua_tag(o));
|
||||
}
|
||||
|
||||
|
||||
static void tonumber (void)
|
||||
{
|
||||
int base = luaL_opt_number(2, 10);
|
||||
if (base == 10) { /* standard convertion */
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isnumber(o))
|
||||
lua_pushnumber(lua_getnumber(o));
|
||||
}
|
||||
else {
|
||||
char *s = luaL_check_string(1);
|
||||
unsigned long n;
|
||||
luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
|
||||
n = strtol(s, &s, base);
|
||||
while (isspace(*s)) s++; /* skip trailing spaces */
|
||||
if (*s) return; /* invalid format: return nil */
|
||||
lua_pushnumber(n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaI_error (void)
|
||||
{
|
||||
lua_error(lua_getstring(lua_getparam(1)));
|
||||
}
|
||||
|
||||
|
||||
static void luaI_assert (void)
|
||||
{
|
||||
lua_Object p = lua_getparam(1);
|
||||
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||
luaL_verror("assertion failed! %.100s", luaL_opt_string(2, ""));
|
||||
}
|
||||
|
||||
|
||||
static void setglobal (void)
|
||||
{
|
||||
char *n = luaL_check_string(1);
|
||||
lua_Object value = luaL_nonnullarg(2);
|
||||
lua_pushobject(value);
|
||||
lua_setglobal(n);
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
static void rawsetglobal (void)
|
||||
{
|
||||
char *n = luaL_check_string(1);
|
||||
lua_Object value = luaL_nonnullarg(2);
|
||||
lua_pushobject(value);
|
||||
lua_rawsetglobal(n);
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
static void getglobal (void)
|
||||
{
|
||||
lua_pushobject(lua_getglobal(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
static void rawgetglobal (void)
|
||||
{
|
||||
lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
static void luatag (void)
|
||||
{
|
||||
lua_pushnumber(lua_tag(lua_getparam(1)));
|
||||
}
|
||||
|
||||
|
||||
static int getnarg (lua_Object table)
|
||||
{
|
||||
lua_Object temp;
|
||||
/* temp = table.n */
|
||||
lua_pushobject(table); lua_pushstring("n"); temp = lua_rawgettable();
|
||||
return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
|
||||
}
|
||||
|
||||
static void luaI_call (void)
|
||||
{
|
||||
lua_Object f = luaL_nonnullarg(1);
|
||||
lua_Object arg = luaL_tablearg(2);
|
||||
char *options = luaL_opt_string(3, "");
|
||||
lua_Object err = lua_getparam(4);
|
||||
int narg = getnarg(arg);
|
||||
int i, status;
|
||||
if (err != LUA_NOOBJECT) { /* set new error method */
|
||||
lua_pushobject(err);
|
||||
err = lua_seterrormethod();
|
||||
}
|
||||
/* push arg[1...n] */
|
||||
for (i=0; i<narg; i++) {
|
||||
lua_Object temp;
|
||||
/* temp = arg[i+1] */
|
||||
lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_rawgettable();
|
||||
if (narg == MAX_WORD && lua_isnil(temp))
|
||||
break;
|
||||
lua_pushobject(temp);
|
||||
}
|
||||
status = lua_callfunction(f);
|
||||
if (err != LUA_NOOBJECT) { /* restore old error method */
|
||||
lua_pushobject(err);
|
||||
lua_seterrormethod();
|
||||
}
|
||||
if (status != 0) { /* error in call? */
|
||||
if (strchr(options, 'x'))
|
||||
return; /* return nil to signal the error */
|
||||
else
|
||||
lua_error(NULL);
|
||||
}
|
||||
else { /* no errors */
|
||||
if (strchr(options, 'p'))
|
||||
luaA_packresults();
|
||||
else
|
||||
luaA_passresults();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void settag (void)
|
||||
{
|
||||
lua_Object o = luaL_tablearg(1);
|
||||
lua_pushobject(o);
|
||||
lua_settag(luaL_check_number(2));
|
||||
}
|
||||
|
||||
|
||||
static void newtag (void)
|
||||
{
|
||||
lua_pushnumber(lua_newtag());
|
||||
}
|
||||
|
||||
|
||||
static void copytagmethods (void)
|
||||
{
|
||||
lua_pushnumber(lua_copytagmethods(luaL_check_number(1),
|
||||
luaL_check_number(2)));
|
||||
}
|
||||
|
||||
|
||||
static void rawgettable (void)
|
||||
{
|
||||
lua_Object t = luaL_nonnullarg(1);
|
||||
lua_Object i = luaL_nonnullarg(2);
|
||||
lua_pushobject(t);
|
||||
lua_pushobject(i);
|
||||
lua_pushobject(lua_rawgettable());
|
||||
}
|
||||
|
||||
|
||||
static void rawsettable (void)
|
||||
{
|
||||
lua_Object t = luaL_nonnullarg(1);
|
||||
lua_Object i = luaL_nonnullarg(2);
|
||||
lua_Object v = luaL_nonnullarg(3);
|
||||
lua_pushobject(t);
|
||||
lua_pushobject(i);
|
||||
lua_pushobject(v);
|
||||
lua_rawsettable();
|
||||
}
|
||||
|
||||
|
||||
static void settagmethod (void)
|
||||
{
|
||||
lua_Object nf = luaL_nonnullarg(3);
|
||||
lua_pushobject(nf);
|
||||
lua_pushobject(lua_settagmethod((int)luaL_check_number(1),
|
||||
luaL_check_string(2)));
|
||||
}
|
||||
|
||||
|
||||
static void gettagmethod (void)
|
||||
{
|
||||
lua_pushobject(lua_gettagmethod((int)luaL_check_number(1),
|
||||
luaL_check_string(2)));
|
||||
}
|
||||
|
||||
|
||||
static void seterrormethod (void)
|
||||
{
|
||||
lua_Object nf = luaL_functionarg(1);
|
||||
lua_pushobject(nf);
|
||||
lua_pushobject(lua_seterrormethod());
|
||||
}
|
||||
|
||||
|
||||
static void luaI_collectgarbage (void)
|
||||
{
|
||||
lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** some DEBUG functions
|
||||
** =======================================================
|
||||
*/
|
||||
#ifdef DEBUG
|
||||
|
||||
static void mem_query (void)
|
||||
{
|
||||
lua_pushnumber(totalmem);
|
||||
lua_pushnumber(numblocks);
|
||||
}
|
||||
|
||||
|
||||
static void countlist (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next :
|
||||
(s[0]=='p') ? L->rootproto.next : L->rootglobal.next;
|
||||
int i=0;
|
||||
while (l) {
|
||||
i++;
|
||||
l = l->next;
|
||||
}
|
||||
lua_pushnumber(i);
|
||||
}
|
||||
|
||||
|
||||
static void testC (void)
|
||||
{
|
||||
#define getnum(s) ((*s++) - '0')
|
||||
#define getname(s) (nome[0] = *s++, nome)
|
||||
|
||||
static int locks[10];
|
||||
lua_Object reg[10];
|
||||
char nome[2];
|
||||
char *s = luaL_check_string(1);
|
||||
nome[1] = 0;
|
||||
while (1) {
|
||||
switch (*s++) {
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
lua_pushnumber(*(s-1) - '0');
|
||||
break;
|
||||
|
||||
case 'c': reg[getnum(s)] = lua_createtable(); break;
|
||||
case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
|
||||
lua_pushCclosure(f, getnum(s));
|
||||
break;
|
||||
}
|
||||
case 'P': reg[getnum(s)] = lua_pop(); break;
|
||||
case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
|
||||
case 'G': { int n = getnum(s);
|
||||
reg[n] = lua_rawgetglobal(getname(s));
|
||||
break;
|
||||
}
|
||||
case 'l': locks[getnum(s)] = lua_ref(1); break;
|
||||
case 'L': locks[getnum(s)] = lua_ref(0); break;
|
||||
case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; }
|
||||
case 'u': lua_unref(locks[getnum(s)]); break;
|
||||
case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
|
||||
case '=': lua_setglobal(getname(s)); break;
|
||||
case 's': lua_pushstring(getname(s)); break;
|
||||
case 'o': lua_pushobject(reg[getnum(s)]); break;
|
||||
case 'f': lua_call(getname(s)); break;
|
||||
case 'i': reg[getnum(s)] = lua_gettable(); break;
|
||||
case 'I': reg[getnum(s)] = lua_rawgettable(); break;
|
||||
case 't': lua_settable(); break;
|
||||
case 'T': lua_rawsettable(); break;
|
||||
default: luaL_verror("unknown command in `testC': %c", *(s-1));
|
||||
}
|
||||
if (*s == 0) return;
|
||||
if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Internal functions
|
||||
*/
|
||||
static struct luaL_reg int_funcs[] = {
|
||||
#ifdef LUA_COMPAT2_5
|
||||
{"setfallback", luaT_setfallback},
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
{"testC", testC},
|
||||
{"totalmem", mem_query},
|
||||
{"count", countlist},
|
||||
#endif
|
||||
{"assert", luaI_assert},
|
||||
{"call", luaI_call},
|
||||
{"collectgarbage", luaI_collectgarbage},
|
||||
{"dofile", internaldofile},
|
||||
{"copytagmethods", copytagmethods},
|
||||
{"dostring", internaldostring},
|
||||
{"error", luaI_error},
|
||||
{"foreach", foreach},
|
||||
{"foreachvar", foreachvar},
|
||||
{"getglobal", getglobal},
|
||||
{"newtag", newtag},
|
||||
{"next", next},
|
||||
{"nextvar", nextvar},
|
||||
{"print", luaI_print},
|
||||
{"rawgetglobal", rawgetglobal},
|
||||
{"rawgettable", rawgettable},
|
||||
{"rawsetglobal", rawsetglobal},
|
||||
{"rawsettable", rawsettable},
|
||||
{"seterrormethod", seterrormethod},
|
||||
{"setglobal", setglobal},
|
||||
{"settagmethod", settagmethod},
|
||||
{"gettagmethod", gettagmethod},
|
||||
{"settag", settag},
|
||||
{"tonumber", tonumber},
|
||||
{"tostring", bi_tostring},
|
||||
{"tag", luatag},
|
||||
{"type", luaI_type}
|
||||
};
|
||||
|
||||
|
||||
#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
|
||||
|
||||
|
||||
void luaB_predefine (void)
|
||||
{
|
||||
/* pre-register mem error messages, to avoid loop when error arises */
|
||||
luaS_newfixedstring(tableEM);
|
||||
luaS_newfixedstring(memEM);
|
||||
luaL_openlib(int_funcs, (sizeof(int_funcs)/sizeof(int_funcs[0])));
|
||||
lua_pushstring(LUA_VERSION);
|
||||
lua_setglobal("_VERSION");
|
||||
}
|
||||
|
||||
14
lbuiltin.h
14
lbuiltin.h
@@ -1,14 +0,0 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lbuiltin_h
|
||||
#define lbuiltin_h
|
||||
|
||||
|
||||
void luaB_predefine (void);
|
||||
|
||||
|
||||
#endif
|
||||
801
lcode.c
Normal file
801
lcode.c
Normal file
@@ -0,0 +1,801 @@
|
||||
/*
|
||||
** $Id: lcode.c,v 1.77 2001/07/17 14:30:44 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lcode.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
|
||||
|
||||
#define hasjumps(e) ((e)->t != (e)->f)
|
||||
|
||||
#define getcode(fs,e) ((fs)->f->code[(e)->u.i.info])
|
||||
|
||||
|
||||
|
||||
void luaK_error (LexState *ls, const l_char *msg) {
|
||||
luaX_error(ls, msg, ls->t.token);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Returns the the previous instruction, for optimizations.
|
||||
** If there is a jump target between this and the current instruction,
|
||||
** returns a dummy instruction to avoid wrong optimizations.
|
||||
*/
|
||||
static Instruction previous_instruction (FuncState *fs) {
|
||||
if (fs->pc > fs->lasttarget) /* no jumps to current position? */
|
||||
return fs->f->code[fs->pc-1]; /* returns previous instruction */
|
||||
else
|
||||
return (Instruction)(-1);/* no optimizations after an invalid instruction */
|
||||
}
|
||||
|
||||
|
||||
void luaK_nil (FuncState *fs, int from, int n) {
|
||||
Instruction previous = previous_instruction(fs);
|
||||
if (GET_OPCODE(previous) == OP_LOADNIL) {
|
||||
int pfrom = GETARG_A(previous);
|
||||
int pto = GETARG_B(previous);
|
||||
if (pfrom <= from && from <= pto+1) { /* can connect both? */
|
||||
if (from+n-1 > pto)
|
||||
SETARG_B(fs->f->code[fs->pc-1], from+n-1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
|
||||
}
|
||||
|
||||
|
||||
int luaK_jump (FuncState *fs) {
|
||||
int j = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP);
|
||||
if (j == fs->lasttarget) { /* possible jumps to this jump? */
|
||||
luaK_concat(fs, &j, fs->jlt); /* keep them on hold */
|
||||
fs->jlt = NO_JUMP;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
static int luaK_condjump (FuncState *fs, OpCode op, int A, int B, int C) {
|
||||
luaK_codeABC(fs, op, A, B, C);
|
||||
return luaK_codeAsBc(fs, OP_CJMP, 0, NO_JUMP);
|
||||
}
|
||||
|
||||
|
||||
static void luaK_fixjump (FuncState *fs, int pc, int dest) {
|
||||
Instruction *jmp = &fs->f->code[pc];
|
||||
if (dest == NO_JUMP)
|
||||
SETARG_sBc(*jmp, NO_JUMP); /* point to itself to represent end of list */
|
||||
else { /* jump is relative to position following jump instruction */
|
||||
int offset = dest-(pc+1);
|
||||
if (abs(offset) > MAXARG_sBc)
|
||||
luaK_error(fs->ls, l_s("control structure too long"));
|
||||
SETARG_sBc(*jmp, offset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** prep-for instructions (OP_FORPREP & OP_TFORPREP) have a negated jump,
|
||||
** as they simulate the real jump...
|
||||
*/
|
||||
void luaK_fixfor (FuncState *fs, int pc, int dest) {
|
||||
Instruction *jmp = &fs->f->code[pc];
|
||||
int offset = dest-(pc+1);
|
||||
SETARG_sBc(*jmp, -offset);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns current `pc' and marks it as a jump target (to avoid wrong
|
||||
** optimizations with consecutive instructions not in the same basic block).
|
||||
** discharge list of jumps to last target.
|
||||
*/
|
||||
int luaK_getlabel (FuncState *fs) {
|
||||
if (fs->pc != fs->lasttarget) {
|
||||
int lasttarget = fs->lasttarget;
|
||||
fs->lasttarget = fs->pc;
|
||||
luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
|
||||
fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
|
||||
}
|
||||
return fs->pc;
|
||||
}
|
||||
|
||||
|
||||
static int luaK_getjump (FuncState *fs, int pc) {
|
||||
int offset = GETARG_sBc(fs->f->code[pc]);
|
||||
if (offset == NO_JUMP) /* point to itself represents end of list */
|
||||
return NO_JUMP; /* end of list */
|
||||
else
|
||||
return (pc+1)+offset; /* turn offset into absolute position */
|
||||
}
|
||||
|
||||
|
||||
static Instruction *getjumpcontrol (FuncState *fs, int pc) {
|
||||
Instruction *pi = &fs->f->code[pc];
|
||||
OpCode op = GET_OPCODE(*pi);
|
||||
if (op == OP_CJMP)
|
||||
return pi-1;
|
||||
else {
|
||||
lua_assert(op == OP_JMP || op == OP_FORLOOP || op == OP_TFORLOOP);
|
||||
return pi;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int need_value (FuncState *fs, int list, OpCode op) {
|
||||
/* check whether list has any jump different from `op' */
|
||||
for (; list != NO_JUMP; list = luaK_getjump(fs, list))
|
||||
if (GET_OPCODE(*getjumpcontrol(fs, list)) != op) return 1;
|
||||
return 0; /* not found */
|
||||
}
|
||||
|
||||
|
||||
static void patchtestreg (Instruction *i, int reg) {
|
||||
if (reg == NO_REG) reg = GETARG_B(*i);
|
||||
SETARG_A(*i, reg);
|
||||
}
|
||||
|
||||
|
||||
static void luaK_patchlistaux (FuncState *fs, int list,
|
||||
int ttarget, int treg, int ftarget, int freg, int dtarget) {
|
||||
while (list != NO_JUMP) {
|
||||
int next = luaK_getjump(fs, list);
|
||||
Instruction *i = getjumpcontrol(fs, list);
|
||||
switch (GET_OPCODE(*i)) {
|
||||
case OP_TESTT: {
|
||||
patchtestreg(i, treg);
|
||||
luaK_fixjump(fs, list, ttarget);
|
||||
break;
|
||||
}
|
||||
case OP_TESTF: {
|
||||
patchtestreg(i, freg);
|
||||
luaK_fixjump(fs, list, ftarget);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
luaK_fixjump(fs, list, dtarget); /* jump to default target */
|
||||
break;
|
||||
}
|
||||
}
|
||||
list = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_patchlist (FuncState *fs, int list, int target) {
|
||||
if (target == fs->lasttarget) /* same target that list `jlt'? */
|
||||
luaK_concat(fs, &fs->jlt, list); /* delay fixing */
|
||||
else
|
||||
luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
|
||||
}
|
||||
|
||||
|
||||
void luaK_concat (FuncState *fs, int *l1, int l2) {
|
||||
if (*l1 == NO_JUMP)
|
||||
*l1 = l2;
|
||||
else {
|
||||
int list = *l1;
|
||||
int next;
|
||||
while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
|
||||
list = next;
|
||||
luaK_fixjump(fs, list, l2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_reserveregs (FuncState *fs, int n) {
|
||||
fs->freereg += n;
|
||||
if (fs->freereg > fs->f->maxstacksize) {
|
||||
if (fs->freereg >= MAXSTACK)
|
||||
luaK_error(fs->ls, l_s("function or expression too complex"));
|
||||
fs->f->maxstacksize = (short)fs->freereg;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void freereg (FuncState *fs, int reg) {
|
||||
if (reg >= fs->nactloc && reg < MAXSTACK) {
|
||||
fs->freereg--;
|
||||
lua_assert(reg == fs->freereg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void freeexp (FuncState *fs, expdesc *e) {
|
||||
if (e->k == VNONRELOC)
|
||||
freereg(fs, e->u.i.info);
|
||||
}
|
||||
|
||||
|
||||
static int addk (FuncState *fs, TObject *k) {
|
||||
Proto *f = fs->f;
|
||||
luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
|
||||
MAXARG_Bc, l_s("constant table overflow"));
|
||||
setobj(&f->k[fs->nk], k);
|
||||
return fs->nk++;
|
||||
}
|
||||
|
||||
|
||||
int luaK_stringk (FuncState *fs, TString *s) {
|
||||
Proto *f = fs->f;
|
||||
int c = s->tsv.constindex;
|
||||
if (c >= fs->nk || ttype(&f->k[c]) != LUA_TSTRING || tsvalue(&f->k[c]) != s) {
|
||||
TObject o;
|
||||
setsvalue(&o, s);
|
||||
c = addk(fs, &o);
|
||||
s->tsv.constindex = (unsigned short)c; /* hint for next time */
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
static int number_constant (FuncState *fs, lua_Number r) {
|
||||
/* check whether `r' has appeared within the last LOOKBACKNUMS entries */
|
||||
TObject o;
|
||||
Proto *f = fs->f;
|
||||
int c = fs->nk;
|
||||
int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
|
||||
while (--c >= lim) {
|
||||
if (ttype(&f->k[c]) == LUA_TNUMBER && nvalue(&f->k[c]) == r)
|
||||
return c;
|
||||
}
|
||||
/* not found; create a new entry */
|
||||
setnvalue(&o, r);
|
||||
return addk(fs, &o);
|
||||
}
|
||||
|
||||
|
||||
void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
|
||||
if (e->k == VCALL) { /* expression is an open function call? */
|
||||
if (nresults == LUA_MULTRET) nresults = NO_REG;
|
||||
SETARG_C(getcode(fs, e), nresults);
|
||||
if (nresults == 1) { /* `regular' expression? */
|
||||
e->k = VNONRELOC;
|
||||
e->u.i.info = GETARG_A(getcode(fs, e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
||||
switch (e->k) {
|
||||
case VLOCAL: {
|
||||
e->k = VNONRELOC;
|
||||
break;
|
||||
}
|
||||
case VGLOBAL: {
|
||||
e->u.i.info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->u.i.info);
|
||||
e->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
case VINDEXED: {
|
||||
freereg(fs, e->u.i.aux);
|
||||
freereg(fs, e->u.i.info);
|
||||
e->u.i.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.i.info, e->u.i.aux);
|
||||
e->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
case VCALL: {
|
||||
luaK_setcallreturns(fs, e, 1);
|
||||
break;
|
||||
}
|
||||
default: break; /* there is one value available (somewhere) */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int code_label (FuncState *fs, OpCode op, int A, int sBc) {
|
||||
luaK_getlabel(fs); /* those instructions may be jump targets */
|
||||
return luaK_codeAsBc(fs, op, A, sBc);
|
||||
}
|
||||
|
||||
|
||||
static void dischargejumps (FuncState *fs, expdesc *e, int reg) {
|
||||
if (hasjumps(e)) {
|
||||
int final; /* position after whole expression */
|
||||
int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */
|
||||
int p_1 = NO_JUMP; /* position of an eventual PUSHINT */
|
||||
if (need_value(fs, e->f, OP_TESTF) || need_value(fs, e->t, OP_TESTT)) {
|
||||
/* expression needs values */
|
||||
if (e->k != VJMP)
|
||||
code_label(fs, OP_JMP, 0, 2); /* to jump over both pushes */
|
||||
p_nil = code_label(fs, OP_NILJMP, reg, 0);
|
||||
p_1 = code_label(fs, OP_LOADINT, reg, 1);
|
||||
}
|
||||
final = luaK_getlabel(fs);
|
||||
luaK_patchlistaux(fs, e->f, p_nil, NO_REG, final, reg, p_nil);
|
||||
luaK_patchlistaux(fs, e->t, final, reg, p_1, NO_REG, p_1);
|
||||
}
|
||||
e->f = e->t = NO_JUMP;
|
||||
}
|
||||
|
||||
|
||||
static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: {
|
||||
luaK_nil(fs, reg, 1);
|
||||
break;
|
||||
}
|
||||
case VNUMBER: {
|
||||
lua_Number f = e->u.n;
|
||||
int i = (int)f;
|
||||
if ((lua_Number)i == f && -MAXARG_sBc <= i && i <= MAXARG_sBc)
|
||||
luaK_codeAsBc(fs, OP_LOADINT, reg, i); /* f has a small int value */
|
||||
else
|
||||
luaK_codeABc(fs, OP_LOADK, reg, number_constant(fs, f));
|
||||
break;
|
||||
}
|
||||
case VK: {
|
||||
luaK_codeABc(fs, OP_LOADK, reg, e->u.i.info);
|
||||
break;
|
||||
}
|
||||
case VRELOCABLE: {
|
||||
Instruction *pc = &getcode(fs, e);
|
||||
SETARG_A(*pc, reg);
|
||||
break;
|
||||
}
|
||||
default: return;
|
||||
}
|
||||
e->u.i.info = reg;
|
||||
e->k = VNONRELOC;
|
||||
}
|
||||
|
||||
|
||||
static void discharge2anyreg (FuncState *fs, expdesc *e) {
|
||||
if (e->k != VNONRELOC) {
|
||||
luaK_reserveregs(fs, 1);
|
||||
discharge2reg(fs, e, fs->freereg-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
|
||||
discharge2reg(fs, e, reg);
|
||||
switch (e->k) {
|
||||
case VVOID: {
|
||||
return; /* nothing to do... */
|
||||
}
|
||||
case VNONRELOC: {
|
||||
if (reg != e->u.i.info)
|
||||
luaK_codeABC(fs, OP_MOVE, reg, e->u.i.info, 0);
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
luaK_concat(fs, &e->t, e->u.i.info); /* put this jump in `t' list */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
lua_assert(0); /* cannot happen */
|
||||
break;
|
||||
}
|
||||
}
|
||||
dischargejumps(fs, e, reg);
|
||||
e->u.i.info = reg;
|
||||
e->k = VNONRELOC;
|
||||
}
|
||||
|
||||
|
||||
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
|
||||
int reg;
|
||||
luaK_dischargevars(fs, e);
|
||||
freeexp(fs, e);
|
||||
reg = fs->freereg;
|
||||
luaK_reserveregs(fs, 1);
|
||||
luaK_exp2reg(fs, e, reg);
|
||||
}
|
||||
|
||||
|
||||
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
if (e->k == VNONRELOC) {
|
||||
if (!hasjumps(e)) return e->u.i.info; /* exp is already in a register */
|
||||
if (e->u.i.info >= fs->nactloc) { /* reg. is not a local? */
|
||||
dischargejumps(fs, e, e->u.i.info); /* put value on it */
|
||||
return e->u.i.info;
|
||||
}
|
||||
}
|
||||
luaK_exp2nextreg(fs, e); /* default */
|
||||
return e->u.i.info;
|
||||
}
|
||||
|
||||
|
||||
void luaK_exp2val (FuncState *fs, expdesc *e) {
|
||||
if (hasjumps(e))
|
||||
luaK_exp2anyreg(fs, e);
|
||||
else
|
||||
luaK_dischargevars(fs, e);
|
||||
}
|
||||
|
||||
|
||||
int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
||||
luaK_exp2val(fs, e);
|
||||
if (e->k == VNUMBER && fs->nk + MAXSTACK <= MAXARG_C) {
|
||||
e->u.i.info = number_constant(fs, e->u.n);
|
||||
e->k = VK;
|
||||
}
|
||||
else if (!(e->k == VK && e->u.i.info + MAXSTACK <= MAXARG_C))
|
||||
luaK_exp2anyreg(fs, e); /* not a constant in the right range */
|
||||
return (e->k == VK) ? e->u.i.info+MAXSTACK : e->u.i.info;
|
||||
}
|
||||
|
||||
|
||||
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
|
||||
switch (var->k) {
|
||||
case VLOCAL: {
|
||||
freeexp(fs, exp);
|
||||
luaK_exp2reg(fs, exp, var->u.i.info);
|
||||
break;
|
||||
}
|
||||
case VGLOBAL: {
|
||||
int e = luaK_exp2anyreg(fs, exp);
|
||||
freereg(fs, e);
|
||||
luaK_codeABc(fs, OP_SETGLOBAL, e, var->u.i.info);
|
||||
break;
|
||||
}
|
||||
case VINDEXED: {
|
||||
int e = luaK_exp2anyreg(fs, exp);
|
||||
freereg(fs, e);
|
||||
luaK_codeABC(fs, OP_SETTABLE, e, var->u.i.info, var->u.i.aux);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
lua_assert(0); /* invalid var kind to store */
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
|
||||
int func;
|
||||
luaK_exp2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
func = fs->freereg;
|
||||
luaK_reserveregs(fs, 2);
|
||||
luaK_codeABC(fs, OP_SELF, func, e->u.i.info, luaK_exp2RK(fs, key));
|
||||
freeexp(fs, key);
|
||||
e->u.i.info = func;
|
||||
e->k = VNONRELOC;
|
||||
}
|
||||
|
||||
|
||||
static OpCode invertoperator (OpCode op) {
|
||||
switch (op) {
|
||||
case OP_TESTNE: return OP_TESTEQ;
|
||||
case OP_TESTEQ: return OP_TESTNE;
|
||||
case OP_TESTLT: return OP_TESTGE;
|
||||
case OP_TESTLE: return OP_TESTGT;
|
||||
case OP_TESTGT: return OP_TESTLE;
|
||||
case OP_TESTGE: return OP_TESTLT;
|
||||
case OP_TESTT: return OP_TESTF;
|
||||
case OP_TESTF: return OP_TESTT;
|
||||
default: lua_assert(0); return op; /* invalid jump instruction */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void invertjump (FuncState *fs, expdesc *e) {
|
||||
Instruction *pc = getjumpcontrol(fs, e->u.i.info);
|
||||
*pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
|
||||
}
|
||||
|
||||
|
||||
static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
|
||||
if (e->k == VRELOCABLE) {
|
||||
Instruction ie = getcode(fs, e);
|
||||
if (GET_OPCODE(ie) == OP_NOT) {
|
||||
op = invertoperator(op);
|
||||
fs->pc--; /* remove previous OP_NOT */
|
||||
return luaK_condjump(fs, op, NO_REG, GETARG_B(ie), 0);
|
||||
}
|
||||
/* else go through */
|
||||
}
|
||||
discharge2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
return luaK_condjump(fs, op, NO_REG, e->u.i.info, 0);
|
||||
}
|
||||
|
||||
|
||||
void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
||||
int pc; /* pc of last jump */
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VK: case VNUMBER: {
|
||||
pc = NO_JUMP; /* always true; do nothing */
|
||||
break;
|
||||
}
|
||||
case VNIL: {
|
||||
pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
invertjump(fs, e);
|
||||
pc = e->u.i.info;
|
||||
break;
|
||||
}
|
||||
case VRELOCABLE:
|
||||
case VNONRELOC: {
|
||||
pc = jumponcond(fs, e, OP_TESTF);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
pc = 0; /* to avoid warnings */
|
||||
lua_assert(0); /* cannot happen */
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
|
||||
luaK_patchlist(fs, e->t, luaK_getlabel(fs));
|
||||
e->t = NO_JUMP;
|
||||
}
|
||||
|
||||
|
||||
static void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
||||
int pc; /* pc of last jump */
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: {
|
||||
pc = NO_JUMP; /* always false; do nothing */
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
pc = e->u.i.info;
|
||||
break;
|
||||
}
|
||||
case VK: case VNUMBER: /* cannot optimize it (`or' must keep value) */
|
||||
case VRELOCABLE:
|
||||
case VNONRELOC: {
|
||||
pc = jumponcond(fs, e, OP_TESTT);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
pc = 0; /* to avoid warnings */
|
||||
lua_assert(0); /* cannot happen */
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
|
||||
luaK_patchlist(fs, e->f, luaK_getlabel(fs));
|
||||
e->f = NO_JUMP;
|
||||
}
|
||||
|
||||
|
||||
static void codenot (FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: {
|
||||
e->u.n = 1;
|
||||
e->k = VNUMBER;
|
||||
break;
|
||||
}
|
||||
case VK: case VNUMBER: {
|
||||
e->k = VNIL;
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
invertjump(fs, e);
|
||||
break;
|
||||
}
|
||||
case VRELOCABLE:
|
||||
case VNONRELOC: {
|
||||
discharge2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
e->u.i.info = luaK_codeABC(fs, OP_NOT, 0, e->u.i.info, 0);
|
||||
e->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
lua_assert(0); /* cannot happen */
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* interchange true and false lists */
|
||||
{ int temp = e->f; e->f = e->t; e->t = temp; }
|
||||
}
|
||||
|
||||
|
||||
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
||||
t->u.i.aux = luaK_exp2RK(fs, k);
|
||||
t->k = VINDEXED;
|
||||
}
|
||||
|
||||
|
||||
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
|
||||
if (op == OPR_MINUS) {
|
||||
luaK_exp2val(fs, e);
|
||||
if (e->k == VNUMBER)
|
||||
e->u.n = -e->u.n;
|
||||
else {
|
||||
luaK_exp2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
e->u.i.info = luaK_codeABC(fs, OP_UNM, 0, e->u.i.info, 0);
|
||||
e->k = VRELOCABLE;
|
||||
}
|
||||
}
|
||||
else /* op == NOT */
|
||||
codenot(fs, e);
|
||||
}
|
||||
|
||||
|
||||
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
||||
switch (op) {
|
||||
case OPR_AND: {
|
||||
luaK_goiftrue(fs, v);
|
||||
break;
|
||||
}
|
||||
case OPR_OR: {
|
||||
luaK_goiffalse(fs, v);
|
||||
break;
|
||||
}
|
||||
case OPR_CONCAT: {
|
||||
luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
|
||||
break;
|
||||
}
|
||||
case OPR_SUB: case OPR_DIV: case OPR_POW: {
|
||||
/* non-comutative operators */
|
||||
luaK_exp2anyreg(fs, v); /* first operand must be a register */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
luaK_exp2RK(fs, v);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* opcode for each binary operator */
|
||||
static const OpCode codes[] = { /* ORDER OPR */
|
||||
OP_ADD, OP_SUB, OP_MUL, OP_DIV,
|
||||
OP_POW, OP_CONCAT,
|
||||
OP_TESTNE, OP_TESTEQ,
|
||||
OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE
|
||||
};
|
||||
|
||||
|
||||
/* `inverted' opcode for each binary operator */
|
||||
/* ( -1 means operator has no inverse) */
|
||||
static const OpCode invcodes[] = { /* ORDER OPR */
|
||||
OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1,
|
||||
(OpCode)-1, (OpCode)-1,
|
||||
OP_TESTNE, OP_TESTEQ,
|
||||
OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE
|
||||
};
|
||||
|
||||
|
||||
void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
|
||||
switch (op) {
|
||||
case OPR_AND: {
|
||||
lua_assert(e1->t == NO_JUMP); /* list must be closed */
|
||||
luaK_dischargevars(fs, e2);
|
||||
luaK_concat(fs, &e1->f, e2->f);
|
||||
e1->k = e2->k; e1->u = e2->u; e1->t = e2->t;
|
||||
break;
|
||||
}
|
||||
case OPR_OR: {
|
||||
lua_assert(e1->f == NO_JUMP); /* list must be closed */
|
||||
luaK_dischargevars(fs, e2);
|
||||
luaK_concat(fs, &e1->t, e2->t);
|
||||
e1->k = e2->k; e1->u = e2->u; e1->f = e2->f;
|
||||
break;
|
||||
}
|
||||
case OPR_CONCAT: {
|
||||
luaK_exp2val(fs, e2);
|
||||
if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
|
||||
lua_assert(e1->u.i.info == GETARG_B(getcode(fs, e2))-1);
|
||||
freeexp(fs, e1);
|
||||
SETARG_B(getcode(fs, e2), e1->u.i.info);
|
||||
e1->k = e2->k; e1->u.i.info = e2->u.i.info;
|
||||
}
|
||||
else {
|
||||
luaK_exp2nextreg(fs, e2);
|
||||
freeexp(fs, e2);
|
||||
freeexp(fs, e1);
|
||||
e1->u.i.info = luaK_codeABC(fs, codes[op], 0, e1->u.i.info,
|
||||
e2->u.i.info);
|
||||
e1->k = VRELOCABLE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OPR_EQ: case OPR_NE: {
|
||||
luaK_exp2val(fs, e2);
|
||||
if (e2->k == VNIL) { /* exp x= nil ? */
|
||||
if (e1->k == VK) { /* constant x= nil ? */
|
||||
if (op == OPR_EQ) /* constant == nil ? */
|
||||
e1->k = VNIL; /* always false */
|
||||
/* else always true (leave the constant itself) */
|
||||
}
|
||||
else {
|
||||
OpCode opc = (op == OPR_EQ) ? OP_TESTF : OP_TESTT;
|
||||
e1->u.i.info = jumponcond(fs, e1, opc);
|
||||
e1->k = VJMP;
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* else go through */
|
||||
}
|
||||
default: {
|
||||
int o1, o2;
|
||||
OpCode opc;
|
||||
if (e1->k != VK) { /* not a constant operator? */
|
||||
o1 = e1->u.i.info;
|
||||
o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
|
||||
opc = codes[op];
|
||||
}
|
||||
else { /* invert operands */
|
||||
o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
|
||||
o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
|
||||
opc = invcodes[op]; /* use inverted operator */
|
||||
}
|
||||
freeexp(fs, e2);
|
||||
freeexp(fs, e1);
|
||||
if (op < OPR_NE) { /* ORDER OPR */
|
||||
e1->u.i.info = luaK_codeABC(fs, opc, 0, o1, o2);
|
||||
e1->k = VRELOCABLE;
|
||||
}
|
||||
else { /* jump */
|
||||
e1->u.i.info = luaK_condjump(fs, opc, o1, 0, o2);
|
||||
e1->k = VJMP;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void codelineinfo (FuncState *fs) {
|
||||
Proto *f = fs->f;
|
||||
LexState *ls = fs->ls;
|
||||
if (ls->lastline > fs->lastline) {
|
||||
if (ls->lastline > fs->lastline+1) {
|
||||
luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
|
||||
MAX_INT, l_s("line info overflow"));
|
||||
f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
|
||||
}
|
||||
luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
|
||||
MAX_INT, l_s("line info overflow"));
|
||||
f->lineinfo[fs->nlineinfo++] = fs->pc;
|
||||
fs->lastline = ls->lastline;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaK_code (FuncState *fs, Instruction i) {
|
||||
Proto *f;
|
||||
codelineinfo(fs);
|
||||
f = fs->f;
|
||||
/* put new instruction in code array */
|
||||
luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
|
||||
MAX_INT, l_s("code size overflow"));
|
||||
f->code[fs->pc] = i;
|
||||
/*printf("free: %d ", fs->freereg); printopcode(f, fs->pc);*/
|
||||
return fs->pc++;
|
||||
}
|
||||
|
||||
|
||||
int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
|
||||
lua_assert(getOpMode(o) == iABC);
|
||||
return luaK_code(fs, CREATE_ABC(o, a, b, c));
|
||||
}
|
||||
|
||||
|
||||
int luaK_codeABc (FuncState *fs, OpCode o, int a, unsigned int bc) {
|
||||
lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
|
||||
return luaK_code(fs, CREATE_ABc(o, a, bc));
|
||||
}
|
||||
|
||||
67
lcode.h
Normal file
67
lcode.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
** $Id: lcode.h,v 1.23 2001/06/12 14:36:48 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lcode_h
|
||||
#define lcode_h
|
||||
|
||||
#include "llex.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
|
||||
|
||||
/*
|
||||
** Marks the end of a patch list. It is an invalid value both as an absolute
|
||||
** address, and as a list link (would link an element to itself).
|
||||
*/
|
||||
#define NO_JUMP (-1)
|
||||
|
||||
|
||||
/*
|
||||
** grep "ORDER OPR" if you change these enums
|
||||
*/
|
||||
typedef enum BinOpr {
|
||||
OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_POW,
|
||||
OPR_CONCAT,
|
||||
OPR_NE, OPR_EQ, OPR_LT, OPR_LE, OPR_GT, OPR_GE,
|
||||
OPR_AND, OPR_OR,
|
||||
OPR_NOBINOPR
|
||||
} BinOpr;
|
||||
|
||||
#define binopistest(op) ((op) >= OPR_NE)
|
||||
|
||||
typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr;
|
||||
|
||||
|
||||
#define luaK_codeAsBc(fs,o,A,sBc) luaK_codeABc(fs,o,A,(sBc)+MAXARG_sBc)
|
||||
|
||||
void luaK_error (LexState *ls, const l_char *msg);
|
||||
int luaK_codeABc (FuncState *fs, OpCode o, int A, unsigned int Bc);
|
||||
int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
|
||||
void luaK_nil (FuncState *fs, int from, int n);
|
||||
void luaK_reserveregs (FuncState *fs, int n);
|
||||
int luaK_stringk (FuncState *fs, TString *s);
|
||||
void luaK_dischargevars (FuncState *fs, expdesc *e);
|
||||
int luaK_exp2anyreg (FuncState *fs, expdesc *e);
|
||||
void luaK_exp2nextreg (FuncState *fs, expdesc *e);
|
||||
void luaK_exp2val (FuncState *fs, expdesc *e);
|
||||
int luaK_exp2RK (FuncState *fs, expdesc *e);
|
||||
void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
|
||||
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
|
||||
void luaK_goiftrue (FuncState *fs, expdesc *e);
|
||||
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
|
||||
void luaK_setcallreturns (FuncState *fs, expdesc *var, int nresults);
|
||||
int luaK_jump (FuncState *fs);
|
||||
void luaK_patchlist (FuncState *fs, int list, int target);
|
||||
void luaK_fixfor (FuncState *fs, int pc, int dest);
|
||||
void luaK_concat (FuncState *fs, int *l1, int l2);
|
||||
int luaK_getlabel (FuncState *fs);
|
||||
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v);
|
||||
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
|
||||
void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
|
||||
|
||||
|
||||
#endif
|
||||
188
ldblib.c
Normal file
188
ldblib.c
Normal file
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.36 2001/03/26 14:31:49 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static void settabss (lua_State *L, const l_char *i, const l_char *v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushstring(L, v);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void settabsi (lua_State *L, const l_char *i, int v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushnumber(L, v);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static int getinfo (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
const l_char *options = luaL_opt_string(L, 2, l_s("flnSu"));
|
||||
l_char buff[20];
|
||||
if (lua_isnumber(L, 1)) {
|
||||
if (!lua_getstack(L, (int)lua_tonumber(L, 1), &ar)) {
|
||||
lua_pushnil(L); /* level out of range */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (lua_isfunction(L, 1)) {
|
||||
lua_pushvalue(L, 1);
|
||||
sprintf(buff, l_s(">%.10s"), options);
|
||||
options = buff;
|
||||
}
|
||||
else
|
||||
luaL_argerror(L, 1, l_s("function or level expected"));
|
||||
if (!lua_getinfo(L, options, &ar))
|
||||
luaL_argerror(L, 2, l_s("invalid option"));
|
||||
lua_newtable(L);
|
||||
for (; *options; options++) {
|
||||
switch (*options) {
|
||||
case l_c('S'):
|
||||
settabss(L, l_s("source"), ar.source);
|
||||
if (ar.source)
|
||||
settabss(L, l_s("short_src"), ar.short_src);
|
||||
settabsi(L, l_s("linedefined"), ar.linedefined);
|
||||
settabss(L, l_s("what"), ar.what);
|
||||
break;
|
||||
case l_c('l'):
|
||||
settabsi(L, l_s("currentline"), ar.currentline);
|
||||
break;
|
||||
case l_c('u'):
|
||||
settabsi(L, l_s("nups"), ar.nups);
|
||||
break;
|
||||
case l_c('n'):
|
||||
settabss(L, l_s("name"), ar.name);
|
||||
settabss(L, l_s("namewhat"), ar.namewhat);
|
||||
break;
|
||||
case l_c('f'):
|
||||
lua_pushliteral(L, l_s("func"));
|
||||
lua_pushvalue(L, -3);
|
||||
lua_settable(L, -3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
|
||||
static int getlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
const l_char *name;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
luaL_argerror(L, 1, l_s("level out of range"));
|
||||
name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
|
||||
if (name) {
|
||||
lua_pushstring(L, name);
|
||||
lua_pushvalue(L, -2);
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int setlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
luaL_argerror(L, 1, l_s("level out of range"));
|
||||
luaL_checkany(L, 3);
|
||||
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define KEY_CALLHOOK l_s("luadblibCallhook")
|
||||
#define KEY_LINEHOOK l_s("luadblibLinehook")
|
||||
|
||||
|
||||
static void hookf (lua_State *L, const l_char *key) {
|
||||
lua_getregistry(L);
|
||||
lua_pushstring(L, key);
|
||||
lua_gettable(L, -2);
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushvalue(L, -3); /* original argument (below table and function) */
|
||||
lua_rawcall(L, 1, 0);
|
||||
}
|
||||
else
|
||||
lua_pop(L, 1); /* pop result from gettable */
|
||||
lua_pop(L, 1); /* pop table */
|
||||
}
|
||||
|
||||
|
||||
static void callf (lua_State *L, lua_Debug *ar) {
|
||||
lua_pushstring(L, ar->event);
|
||||
hookf(L, KEY_CALLHOOK);
|
||||
}
|
||||
|
||||
|
||||
static void linef (lua_State *L, lua_Debug *ar) {
|
||||
lua_pushnumber(L, ar->currentline);
|
||||
hookf(L, KEY_LINEHOOK);
|
||||
}
|
||||
|
||||
|
||||
static void sethook (lua_State *L, const l_char *key, lua_Hook hook,
|
||||
lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
|
||||
lua_settop(L, 1);
|
||||
if (lua_isnil(L, 1))
|
||||
(*sethookf)(L, NULL);
|
||||
else if (lua_isfunction(L, 1))
|
||||
(*sethookf)(L, hook);
|
||||
else
|
||||
luaL_argerror(L, 1, l_s("function expected"));
|
||||
lua_getregistry(L);
|
||||
lua_pushstring(L, key);
|
||||
lua_pushvalue(L, -1); /* dup key */
|
||||
lua_gettable(L, -3); /* get old value */
|
||||
lua_pushvalue(L, -2); /* key (again) */
|
||||
lua_pushvalue(L, 1);
|
||||
lua_settable(L, -5); /* set new value */
|
||||
}
|
||||
|
||||
|
||||
static int setcallhook (lua_State *L) {
|
||||
sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int setlinehook (lua_State *L) {
|
||||
sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_reg dblib[] = {
|
||||
{l_s("getlocal"), getlocal},
|
||||
{l_s("getinfo"), getinfo},
|
||||
{l_s("setcallhook"), setcallhook},
|
||||
{l_s("setlinehook"), setlinehook},
|
||||
{l_s("setlocal"), setlocal}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API int lua_dblibopen (lua_State *L) {
|
||||
luaL_openl(L, dblib);
|
||||
return 0;
|
||||
}
|
||||
|
||||
566
ldebug.c
Normal file
566
ldebug.c
Normal file
@@ -0,0 +1,566 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 1.86 2001/06/28 19:58:57 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lcode.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "luadebug.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
|
||||
static const l_char *getfuncname (lua_State *L, CallInfo *ci,
|
||||
const l_char **name);
|
||||
|
||||
|
||||
|
||||
static int isLmark (CallInfo *ci) {
|
||||
lua_assert(ci == NULL || ttype(ci->base - 1) == LUA_TFUNCTION);
|
||||
return (ci && ci->prev && !ci_func(ci)->isC);
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook;
|
||||
lua_lock(L);
|
||||
oldhook = L->callhook;
|
||||
L->callhook = func;
|
||||
lua_unlock(L);
|
||||
return oldhook;
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook;
|
||||
lua_lock(L);
|
||||
oldhook = L->linehook;
|
||||
L->linehook = func;
|
||||
lua_unlock(L);
|
||||
return oldhook;
|
||||
}
|
||||
|
||||
|
||||
static CallInfo *ci_stack (lua_State *L, StkId obj) {
|
||||
CallInfo *ci = L->ci;
|
||||
while (ci->base > obj) ci = ci->prev;
|
||||
return (ci != &L->basefunc) ? ci : NULL;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
CallInfo *ci;
|
||||
int status;
|
||||
lua_lock(L);
|
||||
ci = L->ci;
|
||||
while (level-- && ci != &L->basefunc) {
|
||||
lua_assert(ci->base > ci->prev->base);
|
||||
ci = ci->prev;
|
||||
}
|
||||
if (ci == &L->basefunc) status = 0; /* there is no such level */
|
||||
else {
|
||||
ar->_ci = ci;
|
||||
status = 1;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
|
||||
int refi;
|
||||
if (lineinfo == NULL || pc == -1)
|
||||
return -1; /* no line info or function is not active */
|
||||
refi = prefi ? *prefi : 0;
|
||||
if (lineinfo[refi] < 0)
|
||||
refline += -lineinfo[refi++];
|
||||
lua_assert(lineinfo[refi] >= 0);
|
||||
while (lineinfo[refi] > pc) {
|
||||
refline--;
|
||||
refi--;
|
||||
if (lineinfo[refi] < 0)
|
||||
refline -= -lineinfo[refi--];
|
||||
lua_assert(lineinfo[refi] >= 0);
|
||||
}
|
||||
for (;;) {
|
||||
int nextline = refline + 1;
|
||||
int nextref = refi + 1;
|
||||
if (lineinfo[nextref] < 0)
|
||||
nextline += -lineinfo[nextref++];
|
||||
lua_assert(lineinfo[nextref] >= 0);
|
||||
if (lineinfo[nextref] > pc)
|
||||
break;
|
||||
refline = nextline;
|
||||
refi = nextref;
|
||||
}
|
||||
if (prefi) *prefi = refi;
|
||||
return refline;
|
||||
}
|
||||
|
||||
|
||||
static int currentpc (CallInfo *ci) {
|
||||
lua_assert(isLmark(ci));
|
||||
if (ci->pc)
|
||||
return (*ci->pc - ci_func(ci)->f.l->code) - 1;
|
||||
else
|
||||
return -1; /* function is not active */
|
||||
}
|
||||
|
||||
|
||||
static int currentline (CallInfo *ci) {
|
||||
if (!isLmark(ci))
|
||||
return -1; /* only active lua functions have current-line information */
|
||||
else {
|
||||
int *lineinfo = ci_func(ci)->f.l->lineinfo;
|
||||
return luaG_getline(lineinfo, currentpc(ci), 1, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static Proto *getluaproto (CallInfo *ci) {
|
||||
return (isLmark(ci) ? ci_func(ci)->f.l : NULL);
|
||||
}
|
||||
|
||||
|
||||
LUA_API const l_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const l_char *name;
|
||||
CallInfo *ci;
|
||||
Proto *fp;
|
||||
lua_lock(L);
|
||||
name = NULL;
|
||||
ci = ar->_ci;
|
||||
fp = getluaproto(ci);
|
||||
if (fp) { /* is a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(ci));
|
||||
if (name)
|
||||
luaA_pushobject(L, ci->base+(n-1)); /* push value */
|
||||
}
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
LUA_API const l_char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const l_char *name;
|
||||
CallInfo *ci;
|
||||
Proto *fp;
|
||||
lua_lock(L);
|
||||
name = NULL;
|
||||
ci = ar->_ci;
|
||||
fp = getluaproto(ci);
|
||||
L->top--; /* pop new value */
|
||||
if (fp) { /* is a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(ci));
|
||||
if (!name || name[0] == l_c('(')) /* `(' starts private locals */
|
||||
name = NULL;
|
||||
else
|
||||
setobj(ci->base+(n-1), L->top);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
static void infoLproto (lua_Debug *ar, Proto *f) {
|
||||
ar->source = getstr(f->source);
|
||||
ar->linedefined = f->lineDefined;
|
||||
ar->what = l_s("Lua");
|
||||
}
|
||||
|
||||
|
||||
static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
|
||||
Closure *cl;
|
||||
if (ttype(func) == LUA_TFUNCTION)
|
||||
cl = clvalue(func);
|
||||
else {
|
||||
luaD_error(L, l_s("value for `lua_getinfo' is not a function"));
|
||||
cl = NULL; /* to avoid warnings */
|
||||
}
|
||||
if (cl->isC) {
|
||||
ar->source = l_s("=C");
|
||||
ar->linedefined = -1;
|
||||
ar->what = l_s("C");
|
||||
}
|
||||
else
|
||||
infoLproto(ar, cl->f.l);
|
||||
luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
|
||||
if (ar->linedefined == 0)
|
||||
ar->what = l_s("main");
|
||||
}
|
||||
|
||||
|
||||
static const l_char *travtagmethods (global_State *G, const TObject *o) {
|
||||
if (ttype(o) == LUA_TFUNCTION) {
|
||||
int e;
|
||||
for (e=0; e<TM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<G->ntag; t++)
|
||||
if (clvalue(o) == luaT_gettm(G, t, e))
|
||||
return luaT_eventname[e];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const l_char *travglobals (lua_State *L, const TObject *o) {
|
||||
Hash *g = L->gt;
|
||||
int i;
|
||||
for (i=0; i<g->size; i++) {
|
||||
if (luaO_equalObj(o, val(node(g, i))) &&
|
||||
ttype(key(node(g, i))) == LUA_TSTRING)
|
||||
return getstr(tsvalue(key(node(g, i))));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static void getname (lua_State *L, const TObject *f, lua_Debug *ar) {
|
||||
/* try to find a name for given function */
|
||||
if ((ar->name = travglobals(L, f)) != NULL)
|
||||
ar->namewhat = l_s("global");
|
||||
/* not found: try tag methods */
|
||||
else if ((ar->name = travtagmethods(G(L), f)) != NULL)
|
||||
ar->namewhat = l_s("tag-method");
|
||||
else ar->namewhat = l_s(""); /* not found at all */
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getinfo (lua_State *L, const l_char *what, lua_Debug *ar) {
|
||||
StkId f;
|
||||
CallInfo *ci;
|
||||
int status = 1;
|
||||
lua_lock(L);
|
||||
if (*what != l_c('>')) { /* function is active? */
|
||||
ci = ar->_ci;
|
||||
f = ci->base - 1;
|
||||
}
|
||||
else {
|
||||
what++; /* skip the `>' */
|
||||
ci = NULL;
|
||||
f = L->top - 1;
|
||||
}
|
||||
for (; *what; what++) {
|
||||
switch (*what) {
|
||||
case l_c('S'): {
|
||||
funcinfo(L, ar, f);
|
||||
break;
|
||||
}
|
||||
case l_c('l'): {
|
||||
ar->currentline = currentline(ci);
|
||||
break;
|
||||
}
|
||||
case l_c('u'): {
|
||||
ar->nups = (ttype(f) == LUA_TFUNCTION) ? clvalue(f)->nupvalues : 0;
|
||||
break;
|
||||
}
|
||||
case l_c('n'): {
|
||||
ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
|
||||
if (ar->namewhat == NULL)
|
||||
getname(L, f, ar);
|
||||
break;
|
||||
}
|
||||
case l_c('f'): {
|
||||
setobj(L->top, f);
|
||||
incr_top; /* push function */
|
||||
break;
|
||||
}
|
||||
default: status = 0; /* invalid option */
|
||||
}
|
||||
}
|
||||
if (!ci) L->top--; /* pop function */
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Symbolic Execution and code checker
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define check(x) if (!(x)) return 0;
|
||||
|
||||
#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
|
||||
|
||||
#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
|
||||
|
||||
|
||||
static int checklineinfo (const Proto *pt) {
|
||||
int *lineinfo = pt->lineinfo;
|
||||
if (lineinfo == NULL) return 1;
|
||||
check(pt->sizelineinfo >= 2 && lineinfo[pt->sizelineinfo-1] == MAX_INT);
|
||||
if (*lineinfo < 0) lineinfo++;
|
||||
check(*lineinfo == 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int precheck (const Proto *pt) {
|
||||
check(checklineinfo(pt));
|
||||
check(pt->maxstacksize <= MAXSTACK);
|
||||
check(pt->numparams+pt->is_vararg <= pt->maxstacksize);
|
||||
check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int checkopenop (Instruction i) {
|
||||
OpCode op = GET_OPCODE(i);
|
||||
switch (op) {
|
||||
case OP_CALL:
|
||||
case OP_RETURN: {
|
||||
check(GETARG_B(i) == NO_REG);
|
||||
return 1;
|
||||
}
|
||||
case OP_SETLISTO: return 1;
|
||||
default: return 0; /* invalid instruction after an open call */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
|
||||
int pc;
|
||||
int last; /* stores position of last instruction that changed `reg' */
|
||||
last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
|
||||
if (reg == NO_REG) /* full check? */
|
||||
check(precheck(pt));
|
||||
for (pc = 0; pc < lastpc; pc++) {
|
||||
const Instruction i = pt->code[pc];
|
||||
OpCode op = GET_OPCODE(i);
|
||||
int a = GETARG_A(i);
|
||||
int b = 0;
|
||||
int c = 0;
|
||||
checkreg(pt, a);
|
||||
switch (getOpMode(op)) {
|
||||
case iABC: {
|
||||
b = GETARG_B(i);
|
||||
c = GETARG_C(i);
|
||||
if (testOpMode(op, OpModeBreg))
|
||||
checkreg(pt, b);
|
||||
if (testOpMode(op, OpModeCreg))
|
||||
check(c < pt->maxstacksize ||
|
||||
(c >= MAXSTACK && c-MAXSTACK < pt->sizek));
|
||||
break;
|
||||
}
|
||||
case iABc: {
|
||||
b = GETARG_Bc(i);
|
||||
if (testOpMode(op, OpModeK)) check(b < pt->sizek);
|
||||
break;
|
||||
}
|
||||
case iAsBc: {
|
||||
b = GETARG_sBc(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (testOpMode(op, OpModesetA)) {
|
||||
if (a == reg) last = pc; /* change register `a' */
|
||||
}
|
||||
if (testOpMode(op, OpModeT))
|
||||
check(GET_OPCODE(pt->code[pc+1]) == OP_CJMP);
|
||||
switch (op) {
|
||||
case OP_LOADNIL: {
|
||||
if (a <= reg && reg <= b)
|
||||
last = pc; /* set registers from `a' to `b' */
|
||||
break;
|
||||
}
|
||||
case OP_LOADUPVAL: {
|
||||
check(b < pt->nupvalues);
|
||||
break;
|
||||
}
|
||||
case OP_GETGLOBAL:
|
||||
case OP_SETGLOBAL: {
|
||||
check(ttype(&pt->k[b]) == LUA_TSTRING);
|
||||
break;
|
||||
}
|
||||
case OP_SELF: {
|
||||
checkreg(pt, a+1);
|
||||
if (reg == a+1) last = pc;
|
||||
break;
|
||||
}
|
||||
case OP_CONCAT: {
|
||||
/* `c' is a register, and at least two operands */
|
||||
check(c < MAXSTACK && b < c);
|
||||
break;
|
||||
}
|
||||
case OP_JMP:
|
||||
case OP_CJMP: {
|
||||
int dest = pc+1+b;
|
||||
check(0 <= dest && dest < pt->sizecode);
|
||||
/* not full check and jump is forward and do not skip `lastpc'? */
|
||||
if (reg != NO_REG && pc < dest && dest <= lastpc)
|
||||
pc += b; /* do the jump */
|
||||
break;
|
||||
}
|
||||
case OP_NILJMP: {
|
||||
check(pc+2 < pt->sizecode); /* check its jump */
|
||||
break;
|
||||
}
|
||||
case OP_CALL: {
|
||||
if (b != NO_REG) {
|
||||
checkreg(pt, a+b);
|
||||
}
|
||||
if (c == NO_REG) {
|
||||
check(checkopenop(pt->code[pc+1]));
|
||||
}
|
||||
else if (c != 0)
|
||||
checkreg(pt, a+c-1);
|
||||
if (reg >= a) last = pc; /* affect all registers above base */
|
||||
break;
|
||||
}
|
||||
case OP_RETURN: {
|
||||
if (b != NO_REG && b != 0)
|
||||
checkreg(pt, a+b-1);
|
||||
break;
|
||||
}
|
||||
case OP_FORPREP:
|
||||
case OP_TFORPREP: {
|
||||
int dest = pc-b; /* jump is negated here */
|
||||
check(0 <= dest && dest < pt->sizecode &&
|
||||
GET_OPCODE(pt->code[dest]) == op+1);
|
||||
break;
|
||||
}
|
||||
case OP_FORLOOP:
|
||||
case OP_TFORLOOP: {
|
||||
int dest = pc+b;
|
||||
check(0 <= dest && dest < pt->sizecode &&
|
||||
pt->code[dest] == SET_OPCODE(i, op-1));
|
||||
checkreg(pt, a + ((op == OP_FORLOOP) ? 2 : 3));
|
||||
break;
|
||||
}
|
||||
case OP_SETLIST: {
|
||||
checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
|
||||
break;
|
||||
}
|
||||
case OP_CLOSURE: {
|
||||
check(b < pt->sizep);
|
||||
checkreg(pt, a + pt->p[b]->nupvalues - 1);
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return pt->code[last];
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
int luaG_checkcode (const Proto *pt) {
|
||||
return luaG_symbexec(pt, pt->sizecode, NO_REG);
|
||||
}
|
||||
|
||||
|
||||
static const l_char *getobjname (lua_State *L, StkId obj, const l_char **name) {
|
||||
CallInfo *ci = ci_stack(L, obj);
|
||||
if (isLmark(ci)) { /* an active Lua function? */
|
||||
Proto *p = ci_func(ci)->f.l;
|
||||
int pc = currentpc(ci);
|
||||
int stackpos = obj - ci->base;
|
||||
Instruction i;
|
||||
*name = luaF_getlocalname(p, stackpos+1, pc);
|
||||
if (*name) /* is a local? */
|
||||
return l_s("local");
|
||||
i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
|
||||
lua_assert(pc != -1);
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_GETGLOBAL: {
|
||||
lua_assert(ttype(&p->k[GETARG_Bc(i)]) == LUA_TSTRING);
|
||||
*name = svalue(&p->k[GETARG_Bc(i)]);
|
||||
return l_s("global");
|
||||
}
|
||||
case OP_MOVE: {
|
||||
int a = GETARG_A(i);
|
||||
int b = GETARG_B(i); /* move from `b' to `a' */
|
||||
if (b < a)
|
||||
return getobjname(L, ci->base+b, name); /* get name for `b' */
|
||||
break;
|
||||
}
|
||||
case OP_GETTABLE:
|
||||
case OP_SELF: {
|
||||
int c = GETARG_C(i) - MAXSTACK;
|
||||
if (c >= 0 && ttype(&p->k[c]) == LUA_TSTRING) {
|
||||
*name = svalue(&p->k[c]);
|
||||
return l_s("field");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return NULL; /* no useful name found */
|
||||
}
|
||||
|
||||
|
||||
static const l_char *getfuncname (lua_State *L, CallInfo *ci,
|
||||
const l_char **name) {
|
||||
ci = ci->prev; /* calling function */
|
||||
if (ci == &L->basefunc || !isLmark(ci))
|
||||
return NULL; /* not an active Lua function */
|
||||
else {
|
||||
Proto *p = ci_func(ci)->f.l;
|
||||
int pc = currentpc(ci);
|
||||
Instruction i;
|
||||
if (pc == -1) return NULL; /* function is not activated */
|
||||
i = p->code[pc];
|
||||
return (GET_OPCODE(i) == OP_CALL
|
||||
? getobjname(L, ci->base+GETARG_A(i), name)
|
||||
: NULL); /* no useful name found */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, StkId o, const l_char *op) {
|
||||
const l_char *name;
|
||||
const l_char *kind = getobjname(L, o, &name);
|
||||
const l_char *t = luaT_typename(G(L), o);
|
||||
if (kind)
|
||||
luaO_verror(L, l_s("attempt to %.30s %.20s `%.40s' (a %.10s value)"),
|
||||
op, kind, name, t);
|
||||
else
|
||||
luaO_verror(L, l_s("attempt to %.30s a %.10s value"), op, t);
|
||||
}
|
||||
|
||||
|
||||
void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
|
||||
if (ttype(p1) == LUA_TSTRING) p1 = p2;
|
||||
lua_assert(ttype(p1) != LUA_TSTRING);
|
||||
luaG_typeerror(L, p1, l_s("concat"));
|
||||
}
|
||||
|
||||
|
||||
void luaG_aritherror (lua_State *L, StkId p1, TObject *p2) {
|
||||
TObject temp;
|
||||
if (luaV_tonumber(p1, &temp) != NULL)
|
||||
p1 = p2; /* first operand is OK; error is in the second */
|
||||
luaG_typeerror(L, p1, l_s("perform arithmetic on"));
|
||||
}
|
||||
|
||||
|
||||
void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
|
||||
const l_char *t1 = luaT_typename(G(L), p1);
|
||||
const l_char *t2 = luaT_typename(G(L), p2);
|
||||
if (t1[2] == t2[2])
|
||||
luaO_verror(L, l_s("attempt to compare two %.10s values"), t1);
|
||||
else
|
||||
luaO_verror(L, l_s("attempt to compare %.10s with %.10s"), t1, t2);
|
||||
}
|
||||
|
||||
23
ldebug.h
Normal file
23
ldebug.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
** $Id: ldebug.h,v 1.14 2001/06/11 14:56:42 roberto Exp roberto $
|
||||
** Auxiliary functions from Debug Interface module
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ldebug_h
|
||||
#define ldebug_h
|
||||
|
||||
|
||||
#include "lstate.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, StkId o, const l_char *op);
|
||||
void luaG_concaterror (lua_State *L, StkId p1, StkId p2);
|
||||
void luaG_aritherror (lua_State *L, StkId p1, TObject *p2);
|
||||
int luaG_getline (int *lineinfo, int pc, int refline, int *refi);
|
||||
void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2);
|
||||
int luaG_checkcode (const Proto *pt);
|
||||
|
||||
|
||||
#endif
|
||||
592
ldo.c
592
ldo.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 1.20 1997/12/26 18:38:16 roberto Exp roberto $
|
||||
** $Id: ldo.c,v 1.137 2001/07/12 19:34:03 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,411 +10,339 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
#ifndef STACK_LIMIT
|
||||
#define STACK_LIMIT 6000
|
||||
#endif
|
||||
/* space to handle stack overflow errors */
|
||||
#define EXTRA_STACK (2*LUA_MINSTACK)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Error messages
|
||||
*/
|
||||
|
||||
static void stderrorim (void)
|
||||
{
|
||||
fprintf(stderr, "lua error: %s\n", lua_getstring(lua_getparam(1)));
|
||||
static void restore_stack_limit (lua_State *L) {
|
||||
StkId limit = L->stack+(L->stacksize-EXTRA_STACK)-1;
|
||||
if (L->top < limit)
|
||||
L->stack_last = limit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define STACK_UNIT 128
|
||||
|
||||
|
||||
void luaD_init (void)
|
||||
{
|
||||
L->stack.stack = luaM_newvector(STACK_UNIT, TObject);
|
||||
L->stack.top = L->stack.stack;
|
||||
L->stack.last = L->stack.stack+(STACK_UNIT-1);
|
||||
ttype(&L->errorim) = LUA_T_CPROTO;
|
||||
fvalue(&L->errorim) = stderrorim;
|
||||
void luaD_init (lua_State *L, int stacksize) {
|
||||
stacksize += EXTRA_STACK;
|
||||
L->stack = luaM_newvector(L, stacksize, TObject);
|
||||
L->stacksize = stacksize;
|
||||
L->basefunc.base = L->top = L->stack;
|
||||
restore_stack_limit(L);
|
||||
}
|
||||
|
||||
|
||||
void luaD_checkstack (int n)
|
||||
{
|
||||
struct Stack *S = &L->stack;
|
||||
if (S->last-S->top <= n) {
|
||||
StkId top = S->top-S->stack;
|
||||
int stacksize = (S->last-S->stack)+1+STACK_UNIT+n;
|
||||
S->stack = luaM_reallocvector(S->stack, stacksize, TObject);
|
||||
S->last = S->stack+(stacksize-1);
|
||||
S->top = S->stack + top;
|
||||
if (stacksize >= STACK_LIMIT) { /* stack overflow? */
|
||||
if (lua_stackedfunction(100) == LUA_NOOBJECT) /* 100 funcs on stack? */
|
||||
lua_error("Lua2C - C2Lua overflow"); /* doesn't look like a rec. loop */
|
||||
else
|
||||
lua_error("stack size overflow");
|
||||
}
|
||||
void luaD_stackerror (lua_State *L) {
|
||||
if (L->stack_last == L->stack+L->stacksize-1) {
|
||||
/* overflow while handling overflow */
|
||||
luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Adjust stack. Set top to the given value, pushing NILs if needed.
|
||||
*/
|
||||
void luaD_adjusttop (StkId newtop)
|
||||
{
|
||||
int diff = newtop-(L->stack.top-L->stack.stack);
|
||||
if (diff <= 0)
|
||||
L->stack.top += diff;
|
||||
else {
|
||||
luaD_checkstack(diff);
|
||||
while (diff--)
|
||||
ttype(L->stack.top++) = LUA_T_NIL;
|
||||
L->stack_last += EXTRA_STACK; /* to be used by error message */
|
||||
lua_assert(L->stack_last == L->stack+L->stacksize-1);
|
||||
luaD_error(L, l_s("stack overflow"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a hole below "nelems" from the L->stack.top.
|
||||
** adjust top to new value; assume that new top is valid
|
||||
*/
|
||||
void luaD_openstack (int nelems)
|
||||
{
|
||||
luaO_memup(L->stack.top-nelems+1, L->stack.top-nelems,
|
||||
nelems*sizeof(TObject));
|
||||
void luaD_adjusttop (lua_State *L, StkId newtop) {
|
||||
while (L->top < newtop)
|
||||
setnilvalue(L->top++);
|
||||
L->top = newtop; /* `newtop' could be lower than `top' */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a hole inside the stack at `pos'
|
||||
*/
|
||||
static void luaD_openstack (lua_State *L, StkId pos) {
|
||||
int i = L->top-pos;
|
||||
while (i--) setobj(pos+i+1, pos+i);
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
void luaD_lineHook (int line)
|
||||
{
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
|
||||
L->Cstack.num = 0;
|
||||
(*lua_linehook)(line);
|
||||
L->stack.top = L->stack.stack+old_top;
|
||||
L->Cstack = oldCLS;
|
||||
static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
|
||||
StkId old_top = L->top;
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||
lua_unlock(L);
|
||||
(*hook)(L, ar);
|
||||
lua_lock(L);
|
||||
lua_assert(L->allowhooks == 0);
|
||||
L->allowhooks = 1;
|
||||
L->top = old_top;
|
||||
}
|
||||
|
||||
|
||||
void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn)
|
||||
{
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
|
||||
L->Cstack.num = 0;
|
||||
if (isreturn)
|
||||
(*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
|
||||
else {
|
||||
TObject *f = L->stack.stack+base-1;
|
||||
if (tf)
|
||||
(*lua_callhook)(Ref(f), tf->fileName->str, tf->lineDefined);
|
||||
else
|
||||
(*lua_callhook)(Ref(f), "(C)", -1);
|
||||
void luaD_lineHook (lua_State *L, int line, lua_Hook linehook) {
|
||||
if (L->allowhooks) {
|
||||
lua_Debug ar;
|
||||
ar.event = l_s("line");
|
||||
ar._ci = L->ci;
|
||||
ar.currentline = line;
|
||||
dohook(L, &ar, linehook);
|
||||
}
|
||||
L->stack.top = L->stack.stack+old_top;
|
||||
L->Cstack = oldCLS;
|
||||
}
|
||||
|
||||
|
||||
static void luaD_callHook (lua_State *L, lua_Hook callhook,
|
||||
const l_char *event) {
|
||||
if (L->allowhooks) {
|
||||
lua_Debug ar;
|
||||
ar.event = event;
|
||||
ar._ci = L->ci;
|
||||
L->ci->pc = NULL; /* function is not active */
|
||||
dohook(L, &ar, callhook);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static StkId callCclosure (lua_State *L, const struct Closure *cl) {
|
||||
int nup = cl->nupvalues; /* number of upvalues */
|
||||
int n;
|
||||
luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
|
||||
for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
|
||||
setobj(L->top++, &cl->upvalue[n]);
|
||||
lua_unlock(L);
|
||||
n = (*cl->f.c)(L); /* do the actual call */
|
||||
lua_lock(L);
|
||||
return L->top - n; /* return index of first result */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call a C function.
|
||||
** Cstack.num is the number of arguments; Cstack.lua2C points to the
|
||||
** first argument. Returns an index to the first result from C.
|
||||
*/
|
||||
static StkId callC (lua_CFunction f, StkId base)
|
||||
{
|
||||
struct C_Lua_Stack *CS = &L->Cstack;
|
||||
struct C_Lua_Stack oldCLS = *CS;
|
||||
** Call a function (C or Lua). The function to be called is at *func.
|
||||
** The arguments are on the stack, right after the function.
|
||||
** When returns, all the results are on the stack, starting at the original
|
||||
** function position.
|
||||
*/
|
||||
void luaD_call (lua_State *L, StkId func) {
|
||||
lua_Hook callhook;
|
||||
StkId firstResult;
|
||||
int numarg = (L->stack.top-L->stack.stack) - base;
|
||||
CS->num = numarg;
|
||||
CS->lua2C = base;
|
||||
CS->base = base+numarg; /* == top-stack */
|
||||
if (lua_callhook)
|
||||
luaD_callHook(base, NULL, 0);
|
||||
(*f)(); /* do the actual call */
|
||||
if (lua_callhook) /* func may have changed lua_callhook */
|
||||
luaD_callHook(base, NULL, 1);
|
||||
firstResult = CS->base;
|
||||
*CS = oldCLS;
|
||||
return firstResult;
|
||||
}
|
||||
|
||||
|
||||
static StkId callCclosure (struct Closure *cl, lua_CFunction f, StkId base)
|
||||
{
|
||||
TObject *pbase;
|
||||
int nup = cl->nelems; /* number of upvalues */
|
||||
luaD_checkstack(nup);
|
||||
pbase = L->stack.stack+base; /* care: previous call may change this */
|
||||
/* open space for upvalues as extra arguments */
|
||||
luaO_memup(pbase+nup, pbase, (L->stack.top-pbase)*sizeof(TObject));
|
||||
/* copy upvalues into stack */
|
||||
memcpy(pbase, cl->consts+1, nup*sizeof(TObject));
|
||||
L->stack.top += nup;
|
||||
return callC(f, base);
|
||||
}
|
||||
|
||||
|
||||
void luaD_callTM (TObject *f, int nParams, int nResults)
|
||||
{
|
||||
luaD_openstack(nParams);
|
||||
*(L->stack.top-nParams-1) = *f;
|
||||
luaD_call((L->stack.top-L->stack.stack)-nParams, nResults);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call a function (C or Lua). The parameters must be on the L->stack.stack,
|
||||
** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1.
|
||||
** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top).
|
||||
** The number of results is nResults, unless nResults=MULT_RET.
|
||||
*/
|
||||
void luaD_call (StkId base, int nResults)
|
||||
{
|
||||
StkId firstResult;
|
||||
TObject *func = L->stack.stack+base-1;
|
||||
int i;
|
||||
switch (ttype(func)) {
|
||||
case LUA_T_CPROTO:
|
||||
ttype(func) = LUA_T_CMARK;
|
||||
firstResult = callC(fvalue(func), base);
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
ttype(func) = LUA_T_PMARK;
|
||||
firstResult = luaV_execute(NULL, tfvalue(func), base);
|
||||
break;
|
||||
case LUA_T_CLOSURE: {
|
||||
Closure *c = clvalue(func);
|
||||
TObject *proto = &(c->consts[0]);
|
||||
ttype(func) = LUA_T_CLMARK;
|
||||
firstResult = (ttype(proto) == LUA_T_CPROTO) ?
|
||||
callCclosure(c, fvalue(proto), base) :
|
||||
luaV_execute(c, tfvalue(proto), base);
|
||||
break;
|
||||
}
|
||||
default: { /* func is not a function */
|
||||
/* Check the tag method for invalid functions */
|
||||
TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error("call expression not a function");
|
||||
luaD_callTM(im, (L->stack.top-L->stack.stack)-(base-1), nResults);
|
||||
return;
|
||||
}
|
||||
CallInfo ci;
|
||||
if (ttype(func) != LUA_TFUNCTION) {
|
||||
/* `func' is not a function; check the `function' tag method */
|
||||
Closure *tm = luaT_gettmbyObj(G(L), func, TM_FUNCTION);
|
||||
if (tm == NULL)
|
||||
luaG_typeerror(L, func, l_s("call"));
|
||||
luaD_openstack(L, func);
|
||||
setclvalue(func, tm); /* tag method is the new function to be called */
|
||||
}
|
||||
/* adjust the number of results */
|
||||
if (nResults != MULT_RET)
|
||||
luaD_adjusttop(firstResult+nResults);
|
||||
/* move results to base-1 (to erase parameters and function) */
|
||||
base--;
|
||||
nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */
|
||||
for (i=0; i<nResults; i++)
|
||||
*(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i);
|
||||
L->stack.top -= firstResult-base;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Traverse all objects on L->stack.stack
|
||||
*/
|
||||
void luaD_travstack (int (*fn)(TObject *))
|
||||
{
|
||||
StkId i;
|
||||
for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
|
||||
fn(L->stack.stack+i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void message (char *s)
|
||||
{
|
||||
TObject im = L->errorim;
|
||||
if (ttype(&im) != LUA_T_NIL) {
|
||||
lua_pushstring(s);
|
||||
luaD_callTM(&im, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Reports an error, and jumps up to the available recover label
|
||||
*/
|
||||
void lua_error (char *s)
|
||||
{
|
||||
if (s) message(s);
|
||||
if (L->errorJmp)
|
||||
longjmp(*((jmp_buf *)L->errorJmp), 1);
|
||||
else {
|
||||
fprintf (stderr, "lua: exit(1). Unable to recover\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Call the function at L->Cstack.base, and incorporate results on
|
||||
** the Lua2C structure.
|
||||
*/
|
||||
static void do_callinc (int nResults)
|
||||
{
|
||||
StkId base = L->Cstack.base;
|
||||
luaD_call(base+1, nResults);
|
||||
L->Cstack.lua2C = base; /* position of the luaM_new results */
|
||||
L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */
|
||||
L->Cstack.base = base + L->Cstack.num; /* incorporate results on L->stack.stack */
|
||||
ci.prev = L->ci; /* chain new callinfo */
|
||||
L->ci = &ci;
|
||||
ci.base = func+1;
|
||||
callhook = L->callhook;
|
||||
if (callhook)
|
||||
luaD_callHook(L, callhook, l_s("call"));
|
||||
firstResult = (clvalue(func)->isC ? callCclosure(L, clvalue(func)) :
|
||||
luaV_execute(L, clvalue(func), func+1));
|
||||
if (callhook) /* same hook that was active at entry */
|
||||
luaD_callHook(L, callhook, l_s("return"));
|
||||
L->ci = ci.prev; /* unchain callinfo */
|
||||
/* move results to `func' (to erase parameters and function) */
|
||||
while (firstResult < L->top)
|
||||
setobj(func++, firstResult++);
|
||||
L->top = func;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected call. Assumes that function is at L->Cstack.base and
|
||||
** parameters are on top of it. Leave nResults on the stack.
|
||||
** Execute a protected call.
|
||||
*/
|
||||
int luaD_protectedrun (int nResults)
|
||||
{
|
||||
jmp_buf myErrorJmp;
|
||||
struct CallS { /* data to `f_call' */
|
||||
StkId func;
|
||||
int nresults;
|
||||
};
|
||||
|
||||
static void f_call (lua_State *L, void *ud) {
|
||||
struct CallS *c = (struct CallS *)ud;
|
||||
luaD_call(L, c->func);
|
||||
if (c->nresults != LUA_MULTRET)
|
||||
luaD_adjusttop(L, c->func + c->nresults);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
|
||||
StkId func;
|
||||
struct CallS c;
|
||||
int status;
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
jmp_buf *oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp) == 0) {
|
||||
do_callinc(nResults);
|
||||
status = 0;
|
||||
}
|
||||
else { /* an error occurred: restore L->Cstack and L->stack.top */
|
||||
L->Cstack = oldCLS;
|
||||
L->stack.top = L->stack.stack+L->Cstack.base;
|
||||
status = 1;
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
lua_lock(L);
|
||||
func = L->top - (nargs+1); /* function to be called */
|
||||
c.func = func; c.nresults = nresults;
|
||||
status = luaD_runprotected(L, f_call, &c);
|
||||
if (status != 0) /* an error occurred? */
|
||||
L->top = func; /* remove parameters from the stack */
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
|
||||
** Execute a protected parser.
|
||||
*/
|
||||
static int protectedparser (ZIO *z, int bin)
|
||||
{
|
||||
int status;
|
||||
TProtoFunc *tf;
|
||||
jmp_buf myErrorJmp;
|
||||
jmp_buf *oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp) == 0) {
|
||||
tf = bin ? luaU_undump1(z) : luaY_parser(z);
|
||||
status = 0;
|
||||
}
|
||||
else {
|
||||
tf = NULL;
|
||||
status = 1;
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
if (status) return 1; /* error code */
|
||||
if (tf == NULL) return 2; /* 'natural' end */
|
||||
luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */
|
||||
L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
|
||||
L->stack.stack[L->Cstack.base].value.tf = tf;
|
||||
luaV_closure(0);
|
||||
return 0;
|
||||
struct SParser { /* data to `f_parser' */
|
||||
ZIO *z;
|
||||
int bin;
|
||||
};
|
||||
|
||||
static void f_parser (lua_State *L, void *ud) {
|
||||
struct SParser *p = (struct SParser *)ud;
|
||||
Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
|
||||
luaV_Lclosure(L, tf, 0);
|
||||
}
|
||||
|
||||
|
||||
static int do_main (ZIO *z, int bin)
|
||||
{
|
||||
static int protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||
struct SParser p;
|
||||
lu_mem old_blocks;
|
||||
int status;
|
||||
do {
|
||||
long old_blocks = (luaC_checkGC(), L->nblocks);
|
||||
status = protectedparser(z, bin);
|
||||
if (status == 1) return 1; /* error */
|
||||
else if (status == 2) return 0; /* 'natural' end */
|
||||
else {
|
||||
unsigned long newelems2 = 2*(L->nblocks-old_blocks);
|
||||
L->GCthreshold += newelems2;
|
||||
status = luaD_protectedrun(MULT_RET);
|
||||
L->GCthreshold -= newelems2;
|
||||
}
|
||||
} while (bin && status == 0);
|
||||
lua_lock(L);
|
||||
p.z = z; p.bin = bin;
|
||||
/* before parsing, give a (good) chance to GC */
|
||||
if (G(L)->nblocks/8 >= G(L)->GCthreshold/10)
|
||||
luaC_collectgarbage(L);
|
||||
old_blocks = G(L)->nblocks;
|
||||
status = luaD_runprotected(L, f_parser, &p);
|
||||
if (status == 0) {
|
||||
/* add new memory to threshold (as it probably will stay) */
|
||||
lua_assert(G(L)->nblocks >= old_blocks);
|
||||
G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
|
||||
}
|
||||
else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
|
||||
status = LUA_ERRSYNTAX;
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
void luaD_gcIM (TObject *o)
|
||||
{
|
||||
TObject *im = luaT_getimbyObj(o, IM_GC);
|
||||
if (ttype(im) != LUA_T_NIL) {
|
||||
*L->stack.top = *o;
|
||||
incr_top;
|
||||
luaD_callTM(im, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int lua_dofile (char *filename)
|
||||
{
|
||||
LUA_API int lua_loadfile (lua_State *L, const l_char *filename) {
|
||||
ZIO z;
|
||||
int status;
|
||||
int c;
|
||||
int bin;
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL)
|
||||
return 2;
|
||||
if (filename == NULL)
|
||||
filename = "(stdin)";
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
bin = (c == ID_CHUNK);
|
||||
if (bin)
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
int bin; /* flag for file mode */
|
||||
int nlevel; /* level on the stack of filename */
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, l_s("r"));
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
|
||||
bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
|
||||
if (bin && f != stdin) {
|
||||
fclose(f);
|
||||
f = fopen(filename, l_s("rb")); /* reopen in binary mode */
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
|
||||
}
|
||||
lua_pushliteral(L, l_s("@"));
|
||||
lua_pushstring(L, (filename == NULL) ? l_s("(stdin)") : filename);
|
||||
lua_concat(L, 2);
|
||||
nlevel = lua_gettop(L);
|
||||
filename = lua_tostring(L, -1); /* filename = `@'..filename */
|
||||
luaZ_Fopen(&z, f, filename);
|
||||
status = do_main(&z, bin);
|
||||
status = protectedparser(L, &z, bin);
|
||||
lua_remove(L, nlevel); /* remove filename */
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
#define SIZE_PREF 20 /* size of string prefix to appear in error messages */
|
||||
#define SSIZE_PREF "20"
|
||||
|
||||
|
||||
int lua_dostring (char *str)
|
||||
{
|
||||
int status;
|
||||
char name[SIZE_PREF+25];
|
||||
char *temp;
|
||||
LUA_API int lua_loadbuffer (lua_State *L, const l_char *buff, size_t size,
|
||||
const l_char *name) {
|
||||
ZIO z;
|
||||
if (str == NULL) return 1;
|
||||
sprintf(name, "(dostring) >> %." SSIZE_PREF "s", str);
|
||||
temp = strchr(name, '\n');
|
||||
if (temp) *temp = 0; /* end string after first line */
|
||||
luaZ_sopen(&z, str, name);
|
||||
status = do_main(&z, 0);
|
||||
int status;
|
||||
if (!name) name = l_s("?");
|
||||
luaZ_mopen(&z, buff, size, name);
|
||||
status = protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
int lua_dobuffer (char *buff, int size)
|
||||
{
|
||||
int status;
|
||||
ZIO z;
|
||||
luaZ_mopen(&z, buff, size, "(buffer)");
|
||||
status = do_main(&z, 1);
|
||||
return status;
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Error-recover functions (based on long jumps)
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/* chain list of long jump buffers */
|
||||
struct lua_longjmp {
|
||||
jmp_buf b;
|
||||
struct lua_longjmp *previous;
|
||||
volatile int status; /* error code */
|
||||
};
|
||||
|
||||
|
||||
static void message (lua_State *L, const l_char *s) {
|
||||
StkId top = L->top;
|
||||
luaV_getglobal(L, luaS_newliteral(L, l_s(LUA_ERRORMESSAGE)), top);
|
||||
if (ttype(top) == LUA_TFUNCTION) {
|
||||
incr_top;
|
||||
setsvalue(top+1, luaS_new(L, s));
|
||||
incr_top;
|
||||
luaD_call(L, top);
|
||||
L->top = top;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Reports an error, and jumps up to the available recovery label
|
||||
*/
|
||||
void luaD_error (lua_State *L, const l_char *s) {
|
||||
if (s) message(L, s);
|
||||
luaD_breakrun(L, LUA_ERRRUN);
|
||||
}
|
||||
|
||||
|
||||
void luaD_breakrun (lua_State *L, int errcode) {
|
||||
if (L->errorJmp) {
|
||||
L->errorJmp->status = errcode;
|
||||
longjmp(L->errorJmp->b, 1);
|
||||
}
|
||||
else {
|
||||
if (errcode != LUA_ERRMEM)
|
||||
message(L, l_s("unable to recover; exiting\n"));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
|
||||
CallInfo *oldci = L->ci;
|
||||
StkId oldtop = L->top;
|
||||
struct lua_longjmp lj;
|
||||
int allowhooks = L->allowhooks;
|
||||
lj.status = 0;
|
||||
lj.previous = L->errorJmp; /* chain new error handler */
|
||||
L->errorJmp = &lj;
|
||||
if (setjmp(lj.b) == 0)
|
||||
(*f)(L, ud);
|
||||
else { /* an error occurred: restore the state */
|
||||
L->allowhooks = allowhooks;
|
||||
L->ci = oldci;
|
||||
L->top = oldtop;
|
||||
restore_stack_limit(L);
|
||||
}
|
||||
L->errorJmp = lj.previous; /* restore old error handler */
|
||||
return lj.status;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
34
ldo.h
34
ldo.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.h,v 1.3 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** $Id: ldo.h,v 1.33 2001/06/05 19:41:24 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,35 +12,25 @@
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
#define MULT_RET 255
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** macro to increment stack top.
|
||||
** There must be always an empty slot at the L->stack.top
|
||||
*/
|
||||
#define incr_top { if (L->stack.top >= L->stack.last) luaD_checkstack(1); \
|
||||
L->stack.top++; }
|
||||
#define incr_top {if (L->top == L->stack_last) luaD_checkstack(L, 1); L->top++;}
|
||||
|
||||
|
||||
/* macros to convert from lua_Object to (TObject *) and back */
|
||||
|
||||
#define Address(lo) ((lo)+L->stack.stack-1)
|
||||
#define Ref(st) ((st)-L->stack.stack+1)
|
||||
#define luaD_checkstack(L,n) if (L->stack_last-(n)<=L->top) luaD_stackerror(L)
|
||||
|
||||
|
||||
void luaD_init (void);
|
||||
void luaD_adjusttop (StkId newtop);
|
||||
void luaD_openstack (int nelems);
|
||||
void luaD_lineHook (int line);
|
||||
void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn);
|
||||
void luaD_call (StkId base, int nResults);
|
||||
void luaD_callTM (TObject *f, int nParams, int nResults);
|
||||
int luaD_protectedrun (int nResults);
|
||||
void luaD_gcIM (TObject *o);
|
||||
void luaD_travstack (int (*fn)(TObject *));
|
||||
void luaD_checkstack (int n);
|
||||
void luaD_init (lua_State *L, int stacksize);
|
||||
void luaD_adjusttop (lua_State *L, StkId newtop);
|
||||
void luaD_lineHook (lua_State *L, int line, lua_Hook linehook);
|
||||
void luaD_call (lua_State *L, StkId func);
|
||||
void luaD_stackerror (lua_State *L);
|
||||
|
||||
void luaD_error (lua_State *L, const l_char *s);
|
||||
void luaD_breakrun (lua_State *L, int errcode);
|
||||
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
112
lfunc.c
112
lfunc.c
@@ -1,98 +1,86 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 1.7 1997/12/09 13:35:19 roberto Exp roberto $
|
||||
** Auxiliar functions to manipulate prototypes and closures
|
||||
** $Id: lfunc.c,v 1.44 2001/06/05 18:17:01 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
#define gcsizeproto(p) 5 /* approximate "weight" for a prototype */
|
||||
#define gcsizeclosure(c) 1 /* approximate "weight" for a closure */
|
||||
|
||||
#define sizeclosure(n) ((int)sizeof(Closure) + (int)sizeof(TObject)*((n)-1))
|
||||
|
||||
|
||||
|
||||
Closure *luaF_newclosure (int nelems)
|
||||
{
|
||||
Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
|
||||
luaO_insertlist(&(L->rootcl), (GCnode *)c);
|
||||
L->nblocks += gcsizeclosure(c);
|
||||
c->nelems = nelems;
|
||||
Closure *luaF_newclosure (lua_State *L, int nelems) {
|
||||
Closure *c = (Closure *)luaM_malloc(L, sizeclosure(nelems));
|
||||
c->next = G(L)->rootcl;
|
||||
G(L)->rootcl = c;
|
||||
c->mark = c;
|
||||
c->nupvalues = nelems;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
TProtoFunc *luaF_newproto (void)
|
||||
{
|
||||
TProtoFunc *f = luaM_new(TProtoFunc);
|
||||
Proto *luaF_newproto (lua_State *L) {
|
||||
Proto *f = luaM_new(L, Proto);
|
||||
f->k = NULL;
|
||||
f->sizek = 0;
|
||||
f->p = NULL;
|
||||
f->sizep = 0;
|
||||
f->code = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->fileName = NULL;
|
||||
f->consts = NULL;
|
||||
f->nconsts = 0;
|
||||
f->sizecode = 0;
|
||||
f->nupvalues = 0;
|
||||
f->numparams = 0;
|
||||
f->is_vararg = 0;
|
||||
f->maxstacksize = 0;
|
||||
f->marked = 0;
|
||||
f->lineinfo = NULL;
|
||||
f->sizelocvars = 0;
|
||||
f->sizelineinfo = 0;
|
||||
f->locvars = NULL;
|
||||
luaO_insertlist(&(L->rootproto), (GCnode *)f);
|
||||
L->nblocks += gcsizeproto(f);
|
||||
f->lineDefined = 0;
|
||||
f->source = NULL;
|
||||
f->next = G(L)->rootproto; /* chain in list of protos */
|
||||
G(L)->rootproto = f;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void freefunc (TProtoFunc *f)
|
||||
{
|
||||
luaM_free(f->code);
|
||||
luaM_free(f->locvars);
|
||||
luaM_free(f->consts);
|
||||
luaM_free(f);
|
||||
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||
luaM_freearray(L, f->code, f->sizecode, Instruction);
|
||||
luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
|
||||
luaM_freearray(L, f->k, f->sizek, TObject);
|
||||
luaM_freearray(L, f->p, f->sizep, Proto *);
|
||||
luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
|
||||
luaM_freelem(L, f, Proto);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeproto (TProtoFunc *l)
|
||||
{
|
||||
while (l) {
|
||||
TProtoFunc *next = (TProtoFunc *)l->head.next;
|
||||
L->nblocks -= gcsizeproto(l);
|
||||
freefunc(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeclosure (Closure *l)
|
||||
{
|
||||
while (l) {
|
||||
Closure *next = (Closure *)l->head.next;
|
||||
L->nblocks -= gcsizeclosure(l);
|
||||
luaM_free(l);
|
||||
l = next;
|
||||
}
|
||||
void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
luaM_free(L, c, sizeclosure(c->nupvalues));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Look for n-th local variable at line "line" in function "func".
|
||||
** Look for n-th local variable at line `line' in function `func'.
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
char *luaF_getlocalname (TProtoFunc *func, int local_number, int line)
|
||||
{
|
||||
int count = 0;
|
||||
char *varname = NULL;
|
||||
LocVar *lv = func->locvars;
|
||||
if (lv == NULL)
|
||||
return NULL;
|
||||
for (; lv->line != -1 && lv->line < line; lv++) {
|
||||
if (lv->varname) { /* register */
|
||||
if (++count == local_number)
|
||||
varname = lv->varname->str;
|
||||
const l_char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
||||
int i;
|
||||
for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
|
||||
if (pc < f->locvars[i].endpc) { /* is variable active? */
|
||||
local_number--;
|
||||
if (local_number == 0)
|
||||
return getstr(f->locvars[i].varname);
|
||||
}
|
||||
else /* unregister */
|
||||
if (--count < local_number)
|
||||
varname = NULL;
|
||||
}
|
||||
return varname;
|
||||
return NULL; /* not found */
|
||||
}
|
||||
|
||||
|
||||
14
lfunc.h
14
lfunc.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: lfunc.h,v 1.4 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** Lua Function structures
|
||||
** $Id: lfunc.h,v 1.14 2000/12/28 12:55:41 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -12,12 +12,12 @@
|
||||
|
||||
|
||||
|
||||
TProtoFunc *luaF_newproto (void);
|
||||
Closure *luaF_newclosure (int nelems);
|
||||
void luaF_freeproto (TProtoFunc *l);
|
||||
void luaF_freeclosure (Closure *l);
|
||||
Proto *luaF_newproto (lua_State *L);
|
||||
Closure *luaF_newclosure (lua_State *L, int nelems);
|
||||
void luaF_freeproto (lua_State *L, Proto *f);
|
||||
void luaF_freeclosure (lua_State *L, Closure *c);
|
||||
|
||||
char *luaF_getlocalname (TProtoFunc *func, int local_number, int line);
|
||||
const l_char *luaF_getlocalname (const Proto *func, int local_number, int pc);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
530
lgc.c
530
lgc.c
@@ -1,9 +1,11 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 1.14 1997/12/17 20:48:58 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 1.108 2001/06/26 13:20:45 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
@@ -14,273 +16,371 @@
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
|
||||
static int markobject (TObject *o);
|
||||
typedef struct GCState {
|
||||
Hash *tmark; /* list of marked tables to be visited */
|
||||
Closure *cmark; /* list of marked closures to be visited */
|
||||
} GCState;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** REF mechanism
|
||||
** =======================================================
|
||||
*/
|
||||
/* mark a string; marks larger than 1 cannot be changed */
|
||||
#define strmark(s) {if ((s)->tsv.marked == 0) (s)->tsv.marked = 1;}
|
||||
|
||||
|
||||
int luaC_ref (TObject *o, int lock)
|
||||
{
|
||||
int ref;
|
||||
if (ttype(o) == LUA_T_NIL)
|
||||
ref = -1; /* special ref for nil */
|
||||
else {
|
||||
for (ref=0; ref<L->refSize; ref++)
|
||||
if (L->refArray[ref].status == FREE)
|
||||
goto found;
|
||||
/* no more empty spaces */ {
|
||||
int oldSize = L->refSize;
|
||||
L->refSize = luaM_growvector(&L->refArray, L->refSize, struct ref,
|
||||
refEM, MAX_WORD);
|
||||
for (ref=oldSize; ref<L->refSize; ref++)
|
||||
L->refArray[ref].status = FREE;
|
||||
ref = oldSize;
|
||||
} found:
|
||||
L->refArray[ref].o = *o;
|
||||
L->refArray[ref].status = lock ? LOCK : HOLD;
|
||||
|
||||
static void protomark (Proto *f) {
|
||||
if (!f->marked) {
|
||||
int i;
|
||||
f->marked = 1;
|
||||
strmark(f->source);
|
||||
for (i=0; i<f->sizek; i++) {
|
||||
if (ttype(f->k+i) == LUA_TSTRING)
|
||||
strmark(tsvalue(f->k+i));
|
||||
}
|
||||
for (i=0; i<f->sizep; i++)
|
||||
protomark(f->p[i]);
|
||||
for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
|
||||
strmark(f->locvars[i].varname);
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
void lua_unref (int ref)
|
||||
{
|
||||
if (ref >= 0 && ref < L->refSize)
|
||||
L->refArray[ref].status = FREE;
|
||||
static void markclosure (GCState *st, Closure *cl) {
|
||||
if (!ismarked(cl)) {
|
||||
if (!cl->isC) {
|
||||
lua_assert(cl->nupvalues == cl->f.l->nupvalues);
|
||||
protomark(cl->f.l);
|
||||
}
|
||||
cl->mark = st->cmark; /* chain it for later traversal */
|
||||
st->cmark = cl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject* luaC_getref (int ref)
|
||||
{
|
||||
if (ref == -1)
|
||||
return &luaO_nilobject;
|
||||
if (ref >= 0 && ref < L->refSize &&
|
||||
(L->refArray[ref].status == LOCK || L->refArray[ref].status == HOLD))
|
||||
return &L->refArray[ref].o;
|
||||
else
|
||||
return NULL;
|
||||
static void marktable (GCState *st, Hash *h) {
|
||||
if (!ismarked(h)) {
|
||||
h->mark = st->tmark; /* chain it for later traversal */
|
||||
st->tmark = h;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void travlock (void)
|
||||
{
|
||||
static void markobject (GCState *st, TObject *o) {
|
||||
switch (ttype(o)) {
|
||||
case LUA_TSTRING:
|
||||
strmark(tsvalue(o));
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
if (!ismarkedudata(uvalue(o)))
|
||||
switchudatamark(uvalue(o));
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
markclosure(st, clvalue(o));
|
||||
break;
|
||||
case LUA_TTABLE: {
|
||||
marktable(st, hvalue(o));
|
||||
break;
|
||||
}
|
||||
default: break; /* numbers, etc */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markstacks (lua_State *L, GCState *st) {
|
||||
lua_State *L1 = L;
|
||||
do { /* for each thread */
|
||||
StkId o, lim;
|
||||
marktable(st, L1->gt); /* mark table of globals */
|
||||
for (o=L1->stack; o<L1->top; o++)
|
||||
markobject(st, o);
|
||||
lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK
|
||||
: L1->stack_last;
|
||||
for (; o<=lim; o++) setnilvalue(o);
|
||||
lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
|
||||
L1 = L1->next;
|
||||
} while (L1 != L);
|
||||
}
|
||||
|
||||
|
||||
static void marktagmethods (global_State *G, GCState *st) {
|
||||
int t;
|
||||
for (t=0; t<G->ntag; t++) {
|
||||
struct TM *tm = &G->TMtable[t];
|
||||
int e;
|
||||
if (tm->name) strmark(tm->name);
|
||||
for (e=0; e<TM_N; e++) {
|
||||
Closure *cl = tm->method[e];
|
||||
if (cl) markclosure(st, cl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void traverseclosure (GCState *st, Closure *f) {
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++)
|
||||
if (L->refArray[i].status == LOCK)
|
||||
markobject(&L->refArray[i].o);
|
||||
for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
|
||||
markobject(st, &f->upvalue[i]);
|
||||
}
|
||||
|
||||
|
||||
static int ismarked (TObject *o)
|
||||
{
|
||||
/* valid only for locked objects */
|
||||
switch (o->ttype) {
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
return o->value.ts->head.marked;
|
||||
case LUA_T_ARRAY:
|
||||
return o->value.a->head.marked;
|
||||
case LUA_T_CLOSURE:
|
||||
return o->value.cl->head.marked;
|
||||
case LUA_T_PROTO:
|
||||
return o->value.tf->head.marked;
|
||||
#ifdef DEBUG
|
||||
case LUA_T_LINE: case LUA_T_CLMARK:
|
||||
case LUA_T_CMARK: case LUA_T_PMARK:
|
||||
lua_error("internal error");
|
||||
#endif
|
||||
default: /* nil, number or cproto */
|
||||
static void removekey (Node *n) {
|
||||
lua_assert(ttype(val(n)) == LUA_TNIL);
|
||||
if (ttype(key(n)) != LUA_TNIL && ttype(key(n)) != LUA_TNUMBER)
|
||||
setttype(key(n), LUA_TNONE); /* dead key; remove it */
|
||||
}
|
||||
|
||||
|
||||
static void traversetable (GCState *st, Hash *h) {
|
||||
int i;
|
||||
int mode = h->weakmode;
|
||||
if (mode == (LUA_WEAK_KEY | LUA_WEAK_VALUE))
|
||||
return; /* avoid traversing if both keys and values are weak */
|
||||
for (i=0; i<h->size; i++) {
|
||||
Node *n = node(h, i);
|
||||
if (ttype(val(n)) == LUA_TNIL)
|
||||
removekey(n);
|
||||
else {
|
||||
lua_assert(ttype(key(n)) != LUA_TNIL);
|
||||
if (ttype(key(n)) != LUA_TNUMBER && !(mode & LUA_WEAK_KEY))
|
||||
markobject(st, key(n));
|
||||
if (!(mode & LUA_WEAK_VALUE))
|
||||
markobject(st, val(n));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markall (lua_State *L) {
|
||||
GCState st;
|
||||
st.cmark = NULL;
|
||||
st.tmark = NULL;
|
||||
marktagmethods(G(L), &st); /* mark tag methods */
|
||||
markstacks(L, &st); /* mark all stacks */
|
||||
marktable(&st, G(L)->type2tag);
|
||||
marktable(&st, G(L)->registry);
|
||||
marktable(&st, G(L)->weakregistry);
|
||||
for (;;) { /* mark tables and closures */
|
||||
if (st.cmark) {
|
||||
Closure *f = st.cmark; /* get first closure from list */
|
||||
st.cmark = f->mark; /* remove it from list */
|
||||
traverseclosure(&st, f);
|
||||
}
|
||||
else if (st.tmark) {
|
||||
Hash *h = st.tmark; /* get first table from list */
|
||||
st.tmark = h->mark; /* remove it from list */
|
||||
traversetable(&st, h);
|
||||
}
|
||||
else break; /* nothing else to mark */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int hasmark (const TObject *o) {
|
||||
switch (ttype(o)) {
|
||||
case LUA_TSTRING:
|
||||
return tsvalue(o)->tsv.marked;
|
||||
case LUA_TUSERDATA:
|
||||
return ismarkedudata(uvalue(o));
|
||||
case LUA_TTABLE:
|
||||
return ismarked(hvalue(o));
|
||||
case LUA_TFUNCTION:
|
||||
return ismarked(clvalue(o));
|
||||
default: /* number, nil */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void invalidaterefs (void)
|
||||
{
|
||||
static void cleardeadnodes (Hash *h) {
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++)
|
||||
if (L->refArray[i].status == HOLD && !ismarked(&L->refArray[i].o))
|
||||
L->refArray[i].status = COLLECTED;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void luaC_hashcallIM (Hash *l)
|
||||
{
|
||||
TObject t;
|
||||
ttype(&t) = LUA_T_ARRAY;
|
||||
for (; l; l=(Hash *)l->head.next) {
|
||||
avalue(&t) = l;
|
||||
luaD_gcIM(&t);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_strcallIM (TaggedString *l)
|
||||
{
|
||||
TObject o;
|
||||
ttype(&o) = LUA_T_USERDATA;
|
||||
for (; l; l=(TaggedString *)l->head.next)
|
||||
if (l->constindex == -1) { /* is userdata? */
|
||||
tsvalue(&o) = l;
|
||||
luaD_gcIM(&o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static GCnode *listcollect (GCnode *l)
|
||||
{
|
||||
GCnode *frees = NULL;
|
||||
while (l) {
|
||||
GCnode *next = l->next;
|
||||
l->marked = 0;
|
||||
while (next && !next->marked) {
|
||||
l->next = next->next;
|
||||
next->next = frees;
|
||||
frees = next;
|
||||
next = l->next;
|
||||
}
|
||||
l = next;
|
||||
}
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
static void strmark (TaggedString *s)
|
||||
{
|
||||
if (!s->head.marked)
|
||||
s->head.marked = 1;
|
||||
}
|
||||
|
||||
|
||||
static void protomark (TProtoFunc *f)
|
||||
{
|
||||
if (!f->head.marked) {
|
||||
LocVar *v = f->locvars;
|
||||
int i;
|
||||
f->head.marked = 1;
|
||||
if (f->fileName)
|
||||
strmark(f->fileName);
|
||||
for (i=0; i<f->nconsts; i++)
|
||||
markobject(&f->consts[i]);
|
||||
if (v) {
|
||||
for (; v->line != -1; v++)
|
||||
if (v->varname)
|
||||
strmark(v->varname);
|
||||
for (i=0; i<h->size; i++) {
|
||||
Node *n = node(h, i);
|
||||
if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */
|
||||
if (!hasmark(val(n)) || !hasmark(key(n))) {
|
||||
setnilvalue(val(n)); /* remove value */
|
||||
removekey(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void closuremark (Closure *f)
|
||||
{
|
||||
if (!f->head.marked) {
|
||||
int i;
|
||||
f->head.marked = 1;
|
||||
for (i=f->nelems; i>=0; i--)
|
||||
markobject(&f->consts[i]);
|
||||
static void cleartables (global_State *G) {
|
||||
Hash *h;
|
||||
for (h = G->roottable; h; h = h->next) {
|
||||
if (h->weakmode && ismarked(h))
|
||||
cleardeadnodes(h);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void hashmark (Hash *h)
|
||||
{
|
||||
if (!h->head.marked) {
|
||||
int i;
|
||||
h->head.marked = 1;
|
||||
for (i=0; i<nhash(h); i++) {
|
||||
Node *n = node(h,i);
|
||||
if (ttype(ref(n)) != LUA_T_NIL) {
|
||||
markobject(&n->ref);
|
||||
markobject(&n->val);
|
||||
static void collectproto (lua_State *L) {
|
||||
Proto **p = &G(L)->rootproto;
|
||||
Proto *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (curr->marked) {
|
||||
curr->marked = 0;
|
||||
p = &curr->next;
|
||||
}
|
||||
else {
|
||||
*p = curr->next;
|
||||
luaF_freeproto(L, curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collectclosure (lua_State *L) {
|
||||
Closure **p = &G(L)->rootcl;
|
||||
Closure *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (ismarked(curr)) {
|
||||
curr->mark = curr; /* unmark */
|
||||
p = &curr->next;
|
||||
}
|
||||
else {
|
||||
*p = curr->next;
|
||||
luaF_freeclosure(L, curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collecttable (lua_State *L) {
|
||||
Hash **p = &G(L)->roottable;
|
||||
Hash *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (ismarked(curr)) {
|
||||
curr->mark = curr; /* unmark */
|
||||
p = &curr->next;
|
||||
}
|
||||
else {
|
||||
*p = curr->next;
|
||||
luaH_free(L, curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collectudata (lua_State *L, int keep) {
|
||||
Udata **p = &G(L)->rootudata;
|
||||
Udata *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (ismarkedudata(curr)) {
|
||||
switchudatamark(curr); /* unmark */
|
||||
p = &curr->uv.next;
|
||||
}
|
||||
else { /* collect */
|
||||
int tag = curr->uv.tag;
|
||||
*p = curr->uv.next;
|
||||
if (keep || /* must keep all of them (to close state)? */
|
||||
luaT_gettm(G(L), tag, TM_GC)) { /* or is there a GC tag method? */
|
||||
curr->uv.next = G(L)->TMtable[tag].collected; /* chain udata ... */
|
||||
G(L)->TMtable[tag].collected = curr; /* ... to call its TM later */
|
||||
}
|
||||
else /* no tag method; delete udata */
|
||||
luaM_free(L, curr, sizeudata(curr->uv.len));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collectstrings (lua_State *L, int all) {
|
||||
int i;
|
||||
for (i=0; i<G(L)->strt.size; i++) { /* for each list */
|
||||
TString **p = &G(L)->strt.hash[i];
|
||||
TString *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (curr->tsv.marked && !all) { /* preserve? */
|
||||
if (curr->tsv.marked < FIXMARK) /* does not change FIXMARKs */
|
||||
curr->tsv.marked = 0;
|
||||
p = &curr->tsv.nexthash;
|
||||
}
|
||||
else { /* collect */
|
||||
*p = curr->tsv.nexthash;
|
||||
G(L)->strt.nuse--;
|
||||
luaM_free(L, curr, sizestring(curr->tsv.len));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (G(L)->strt.nuse < (ls_nstr)(G(L)->strt.size/4) &&
|
||||
G(L)->strt.size > MINPOWER2)
|
||||
luaS_resize(L, G(L)->strt.size/2); /* table is too big */
|
||||
}
|
||||
|
||||
|
||||
static void globalmark (void)
|
||||
{
|
||||
TaggedString *g;
|
||||
for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
|
||||
if (g->u.globalval.ttype != LUA_T_NIL) {
|
||||
markobject(&g->u.globalval);
|
||||
strmark(g); /* cannot collect non nil global variables */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int markobject (TObject *o)
|
||||
{
|
||||
switch (ttype(o)) {
|
||||
case LUA_T_USERDATA: case LUA_T_STRING:
|
||||
strmark(tsvalue(o));
|
||||
break;
|
||||
case LUA_T_ARRAY:
|
||||
hashmark(avalue(o));
|
||||
break;
|
||||
case LUA_T_CLOSURE: case LUA_T_CLMARK:
|
||||
closuremark(o->value.cl);
|
||||
break;
|
||||
case LUA_T_PROTO: case LUA_T_PMARK:
|
||||
protomark(o->value.tf);
|
||||
break;
|
||||
default: break; /* numbers, cprotos, etc */
|
||||
#define MINBUFFER 256
|
||||
static void checkMbuffer (lua_State *L) {
|
||||
if (G(L)->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
|
||||
size_t newsize = G(L)->Mbuffsize/2; /* still larger than MINBUFFER */
|
||||
luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, newsize, l_char);
|
||||
G(L)->Mbuffsize = newsize;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void markall (void)
|
||||
{
|
||||
luaD_travstack(markobject); /* mark stack objects */
|
||||
globalmark(); /* mark global variable values and names */
|
||||
travlock(); /* mark locked objects */
|
||||
luaT_travtagmethods(markobject); /* mark fallbacks */
|
||||
static void callgcTM (lua_State *L, const TObject *obj) {
|
||||
Closure *tm = luaT_gettmbyObj(G(L), obj, TM_GC);
|
||||
if (tm != NULL) {
|
||||
int oldah = L->allowhooks;
|
||||
StkId top = L->top;
|
||||
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
|
||||
setclvalue(top, tm);
|
||||
setobj(top+1, obj);
|
||||
L->top += 2;
|
||||
luaD_call(L, top);
|
||||
L->top = top; /* restore top */
|
||||
L->allowhooks = oldah; /* restore hooks */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
long lua_collectgarbage (long limit)
|
||||
{
|
||||
unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */
|
||||
Hash *freetable;
|
||||
TaggedString *freestr;
|
||||
TProtoFunc *freefunc;
|
||||
Closure *freeclos;
|
||||
markall();
|
||||
invalidaterefs();
|
||||
freestr = luaS_collector();
|
||||
freetable = (Hash *)listcollect(&(L->roottable));
|
||||
freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
|
||||
freeclos = (Closure *)listcollect(&(L->rootcl));
|
||||
L->GCthreshold *= 4; /* to avoid GC during GC */
|
||||
luaC_hashcallIM(freetable); /* GC tag methods for tables */
|
||||
luaC_strcallIM(freestr); /* GC tag methods for userdata */
|
||||
luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
|
||||
luaH_free(freetable);
|
||||
luaS_free(freestr);
|
||||
luaF_freeproto(freefunc);
|
||||
luaF_freeclosure(freeclos);
|
||||
recovered = recovered-L->nblocks;
|
||||
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
||||
return recovered;
|
||||
static void callgcTMudata (lua_State *L) {
|
||||
int tag;
|
||||
luaD_checkstack(L, 3);
|
||||
for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
|
||||
Udata *udata;
|
||||
while ((udata = G(L)->TMtable[tag].collected) != NULL) {
|
||||
G(L)->TMtable[tag].collected = udata->uv.next; /* remove it from list */
|
||||
udata->uv.next = G(L)->rootudata; /* resurect it */
|
||||
G(L)->rootudata = udata;
|
||||
setuvalue(L->top, udata);
|
||||
L->top++; /* keep it in stack to avoid being (recursively) collected */
|
||||
callgcTM(L, L->top-1);
|
||||
uvalue(L->top-1)->uv.tag = 0; /* default tag (udata is `finalized') */
|
||||
L->top--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_checkGC (void)
|
||||
{
|
||||
if (L->nblocks >= L->GCthreshold)
|
||||
lua_collectgarbage(0);
|
||||
void luaC_callallgcTM (lua_State *L) {
|
||||
if (G(L)->rootudata) { /* avoid problems with incomplete states */
|
||||
collectudata(L, 1); /* collect all udata into tag lists */
|
||||
callgcTMudata(L); /* call their GC tag methods */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_collect (lua_State *L, int all) {
|
||||
collectudata(L, 0);
|
||||
collectstrings(L, all);
|
||||
collecttable(L);
|
||||
collectproto(L);
|
||||
collectclosure(L);
|
||||
}
|
||||
|
||||
|
||||
void luaC_collectgarbage (lua_State *L) {
|
||||
markall(L);
|
||||
cleartables(G(L));
|
||||
luaC_collect(L, 0);
|
||||
checkMbuffer(L);
|
||||
G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
|
||||
callgcTMudata(L);
|
||||
callgcTM(L, &luaO_nilobject);
|
||||
}
|
||||
|
||||
|
||||
14
lgc.h
14
lgc.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lgc.h,v 1.3 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** $Id: lgc.h,v 1.11 2001/06/12 18:43:13 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,11 +11,13 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaC_checkGC (void);
|
||||
TObject* luaC_getref (int ref);
|
||||
int luaC_ref (TObject *o, int lock);
|
||||
void luaC_hashcallIM (Hash *l);
|
||||
void luaC_strcallIM (TaggedString *l);
|
||||
#define luaC_checkGC(L) if (G(L)->nblocks >= G(L)->GCthreshold) \
|
||||
luaC_collectgarbage(L)
|
||||
|
||||
|
||||
void luaC_callallgcTM (lua_State *L);
|
||||
void luaC_collect (lua_State *L, int all);
|
||||
void luaC_collectgarbage (lua_State *L);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
8
linit.c
Normal file
8
linit.c
Normal file
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
** $Id: linit.c,v 1.5 2000/06/16 17:22:43 roberto Exp roberto $
|
||||
** Initialization of libraries for lua.c
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
>>>>>>> This module is now obsolete, and is not part of Lua. <<<<<<<<<
|
||||
|
||||
621
llex.c
621
llex.c
@@ -1,436 +1,389 @@
|
||||
/*
|
||||
** $Id: llex.c,v 1.12 1997/12/22 17:52:20 roberto Exp roberto $
|
||||
** Lexical Analizer
|
||||
** $Id: llex.c,v 1.88 2001/06/20 21:07:57 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lstx.h"
|
||||
#include "luadebug.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
int lua_debug=0;
|
||||
#define next(LS) (LS->current = zgetc(LS->z))
|
||||
|
||||
|
||||
#define next(LS) (LS->current = zgetc(LS->lex_z))
|
||||
|
||||
|
||||
static struct {
|
||||
char *name;
|
||||
int token;
|
||||
} reserved [] = {
|
||||
{"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
|
||||
{"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
|
||||
{"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
|
||||
{"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
|
||||
/* ORDER RESERVED */
|
||||
static const l_char *const token2string [] = {
|
||||
l_s("and"), l_s("break"), l_s("do"), l_s("else"), l_s("elseif"),
|
||||
l_s("end"), l_s("for"), l_s("function"), l_s("global"), l_s("if"),
|
||||
l_s("in"), l_s("local"), l_s("nil"), l_s("not"), l_s("or"), l_s("repeat"),
|
||||
l_s("return"), l_s("then"), l_s("until"), l_s("while"), l_s(""),
|
||||
l_s(".."), l_s("..."), l_s("=="), l_s(">="), l_s("<="), l_s("~="),
|
||||
l_s(""), l_s(""), l_s("<eof>")
|
||||
};
|
||||
|
||||
void luaX_init (void)
|
||||
{
|
||||
|
||||
void luaX_init (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
|
||||
TaggedString *ts = luaS_new(reserved[i].name);
|
||||
ts->head.marked = reserved[i].token; /* reserved word (always > 255) */
|
||||
for (i=0; i<NUM_RESERVED; i++) {
|
||||
TString *ts = luaS_new(L, token2string[i]);
|
||||
lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN);
|
||||
ts->tsv.marked = (unsigned short)(RESERVEDMARK+i); /* reserved word */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void firstline (LexState *LS)
|
||||
{
|
||||
int c = zgetc(LS->lex_z);
|
||||
if (c == '#') {
|
||||
LS->linenumber++;
|
||||
while ((c=zgetc(LS->lex_z)) != '\n' && c != EOZ) /* skip first line */;
|
||||
}
|
||||
zungetc(LS->lex_z);
|
||||
}
|
||||
#define MAXSRC 80
|
||||
|
||||
|
||||
void luaX_setinput (ZIO *z)
|
||||
{
|
||||
LexState *LS = L->lexstate;
|
||||
LS->current = '\n';
|
||||
LS->linelasttoken = 0;
|
||||
LS->linenumber = 0;
|
||||
LS->iflevel = 0;
|
||||
LS->ifstate[0].skip = 0;
|
||||
LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
|
||||
LS->lex_z = z;
|
||||
firstline(LS);
|
||||
luaL_resetbuffer();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** PRAGMAS
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define PRAGMASIZE 20
|
||||
|
||||
static void skipspace (LexState *LS)
|
||||
{
|
||||
while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
|
||||
next(LS);
|
||||
}
|
||||
|
||||
|
||||
static int checkcond (char *buff)
|
||||
{
|
||||
static char *opts[] = {"nil", "1", NULL};
|
||||
int i = luaO_findstring(buff, opts);
|
||||
if (i >= 0) return i;
|
||||
else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
|
||||
return luaS_globaldefined(buff);
|
||||
else {
|
||||
luaY_syntaxerror("invalid $if condition", buff);
|
||||
return 0; /* to avoid warnings */
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const l_char *msg) {
|
||||
if (val > limit) {
|
||||
l_char buff[90];
|
||||
sprintf(buff, l_s("too many %.40s (limit=%d)"), msg, limit);
|
||||
luaX_error(ls, buff, ls->t.token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void readname (LexState *LS, char *buff)
|
||||
{
|
||||
int i = 0;
|
||||
skipspace(LS);
|
||||
while (isalnum(LS->current) || LS->current == '_') {
|
||||
if (i >= PRAGMASIZE) {
|
||||
buff[PRAGMASIZE] = 0;
|
||||
luaY_syntaxerror("pragma too long", buff);
|
||||
}
|
||||
buff[i++] = LS->current;
|
||||
next(LS);
|
||||
}
|
||||
buff[i] = 0;
|
||||
void luaX_syntaxerror (LexState *ls, const l_char *s, const l_char *token) {
|
||||
l_char buff[MAXSRC];
|
||||
luaO_chunkid(buff, getstr(ls->source), MAXSRC);
|
||||
luaO_verror(ls->L,
|
||||
l_s("%.99s;\n last token read: `%.30s' at line %d in %.80s"),
|
||||
s, token, ls->linenumber, buff);
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *LS);
|
||||
|
||||
|
||||
static void ifskip (LexState *LS)
|
||||
{
|
||||
while (LS->ifstate[LS->iflevel].skip) {
|
||||
if (LS->current == '\n')
|
||||
inclinenumber(LS);
|
||||
else if (LS->current == EOZ)
|
||||
luaY_syntaxerror("input ends inside a $if", "");
|
||||
else next(LS);
|
||||
}
|
||||
void luaX_error (LexState *ls, const l_char *s, int token) {
|
||||
l_char buff[TOKEN_LEN];
|
||||
luaX_token2str(token, buff);
|
||||
if (buff[0] == l_c('\0'))
|
||||
luaX_syntaxerror(ls, s, (l_char *)G(ls->L)->Mbuffer);
|
||||
else
|
||||
luaX_syntaxerror(ls, s, buff);
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *LS)
|
||||
{
|
||||
static char *pragmas [] =
|
||||
{"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
|
||||
next(LS); /* skip '\n' */
|
||||
void luaX_token2str (int token, l_char *s) {
|
||||
if (token < 256) {
|
||||
s[0] = (l_char)token;
|
||||
s[1] = l_c('\0');
|
||||
}
|
||||
else
|
||||
strcpy(s, token2string[token-FIRST_RESERVED]);
|
||||
}
|
||||
|
||||
|
||||
static void luaX_invalidchar (LexState *ls, int c) {
|
||||
l_char buff[8];
|
||||
sprintf(buff, l_s("0x%02X"), uchar(c));
|
||||
luaX_syntaxerror(ls, l_s("invalid control char"), buff);
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *LS) {
|
||||
next(LS); /* skip `\n' */
|
||||
++LS->linenumber;
|
||||
if (LS->current == '$') { /* is a pragma? */
|
||||
char buff[PRAGMASIZE+1];
|
||||
int ifnot = 0;
|
||||
int skip = LS->ifstate[LS->iflevel].skip;
|
||||
next(LS); /* skip $ */
|
||||
readname(LS, buff);
|
||||
switch (luaO_findstring(buff, pragmas)) {
|
||||
case 0: /* debug */
|
||||
if (!skip) lua_debug = 1;
|
||||
break;
|
||||
case 1: /* nodebug */
|
||||
if (!skip) lua_debug = 0;
|
||||
break;
|
||||
case 2: /* endinput */
|
||||
if (!skip) {
|
||||
LS->current = EOZ;
|
||||
LS->iflevel = 0; /* to allow $endinput inside a $if */
|
||||
}
|
||||
break;
|
||||
case 3: /* end */
|
||||
if (LS->iflevel-- == 0)
|
||||
luaY_syntaxerror("unmatched $end", "$end");
|
||||
break;
|
||||
case 4: /* ifnot */
|
||||
ifnot = 1;
|
||||
/* go through */
|
||||
case 5: /* if */
|
||||
if (LS->iflevel == MAX_IFS-1)
|
||||
luaY_syntaxerror("too many nested $ifs", "$if");
|
||||
readname(LS, buff);
|
||||
LS->iflevel++;
|
||||
LS->ifstate[LS->iflevel].elsepart = 0;
|
||||
LS->ifstate[LS->iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
|
||||
LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
|
||||
break;
|
||||
case 6: /* else */
|
||||
if (LS->ifstate[LS->iflevel].elsepart)
|
||||
luaY_syntaxerror("unmatched $else", "$else");
|
||||
LS->ifstate[LS->iflevel].elsepart = 1;
|
||||
LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip ||
|
||||
LS->ifstate[LS->iflevel].condition;
|
||||
break;
|
||||
default:
|
||||
luaY_syntaxerror("unknown pragma", buff);
|
||||
}
|
||||
skipspace(LS);
|
||||
if (LS->current == '\n') /* pragma must end with a '\n' ... */
|
||||
inclinenumber(LS);
|
||||
else if (LS->current != EOZ) /* or eof */
|
||||
luaY_syntaxerror("invalid pragma format", buff);
|
||||
ifskip(LS);
|
||||
luaX_checklimit(LS, LS->linenumber, MAX_INT, l_s("lines in a chunk"));
|
||||
}
|
||||
|
||||
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
|
||||
LS->L = L;
|
||||
LS->lookahead.token = TK_EOS; /* no look-ahead token */
|
||||
LS->z = z;
|
||||
LS->fs = NULL;
|
||||
LS->linenumber = 1;
|
||||
LS->lastline = 1;
|
||||
LS->source = source;
|
||||
next(LS); /* read first char */
|
||||
if (LS->current == l_c('#')) {
|
||||
do { /* skip first line */
|
||||
next(LS);
|
||||
} while (LS->current != l_c('\n') && LS->current != EOZ);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** LEXICAL ANALIZER
|
||||
** LEXICAL ANALYZER
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/* use Mbuffer to store names, literal strings and numbers */
|
||||
|
||||
#define save(c) luaL_addchar(c)
|
||||
#define save_and_next(LS) (save(LS->current), next(LS))
|
||||
#define EXTRABUFF 128
|
||||
#define checkbuffer(L, n, len) \
|
||||
if (((len)+(n))*sizeof(l_char) > G(L)->Mbuffsize) \
|
||||
luaO_openspace(L, (len)+(n)+EXTRABUFF, l_char)
|
||||
|
||||
#define save(L, c, l) (((l_char *)G(L)->Mbuffer)[l++] = (l_char)c)
|
||||
#define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS))
|
||||
|
||||
|
||||
char *luaX_lasttoken (void)
|
||||
{
|
||||
save(0);
|
||||
return luaL_buffer();
|
||||
static size_t readname (LexState *LS) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
do {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
} while (isalnum(LS->current) || LS->current == l_c('_'));
|
||||
save(L, l_c('\0'), l);
|
||||
return l-1;
|
||||
}
|
||||
|
||||
|
||||
static int read_long_string (LexState *LS, YYSTYPE *l)
|
||||
{
|
||||
/* LUA_NUMBER */
|
||||
static void read_number (LexState *LS, int comma, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
if (comma) save(L, l_c('.'), l);
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
if (LS->current == l_c('.')) {
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == l_c('.')) {
|
||||
save_and_next(L, LS, l);
|
||||
save(L, l_c('\0'), l);
|
||||
luaX_error(LS,
|
||||
l_s("ambiguous syntax (decimal point x string concatenation)"),
|
||||
TK_NUMBER);
|
||||
}
|
||||
}
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
if (LS->current == l_c('e') || LS->current == l_c('E')) {
|
||||
save_and_next(L, LS, l); /* read `E' */
|
||||
if (LS->current == l_c('+') || LS->current == l_c('-'))
|
||||
save_and_next(L, LS, l); /* optional exponent sign */
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
}
|
||||
save(L, l_c('\0'), l);
|
||||
if (!luaO_str2d((l_char *)G(L)->Mbuffer, &seminfo->r))
|
||||
luaX_error(LS, l_s("malformed number"), TK_NUMBER);
|
||||
}
|
||||
|
||||
|
||||
static void read_long_string (LexState *LS, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
int cont = 0;
|
||||
while (1) {
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
save(L, l_c('['), l); /* save first `[' */
|
||||
save_and_next(L, LS, l); /* pass the second `[' */
|
||||
if (LS->current == l_c('\n')) /* string starts with a newline? */
|
||||
inclinenumber(LS); /* skip it */
|
||||
for (;;) {
|
||||
checkbuffer(L, 10, l);
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
save(0);
|
||||
return WRONGTOKEN;
|
||||
case '[':
|
||||
save_and_next(LS);
|
||||
if (LS->current == '[') {
|
||||
save(L, l_c('\0'), l);
|
||||
luaX_error(LS, l_s("unfinished long string"), TK_STRING);
|
||||
break; /* to avoid warnings */
|
||||
case l_c('['):
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == l_c('[')) {
|
||||
cont++;
|
||||
save_and_next(LS);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next(LS);
|
||||
if (LS->current == ']') {
|
||||
case l_c(']'):
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == l_c(']')) {
|
||||
if (cont == 0) goto endloop;
|
||||
cont--;
|
||||
save_and_next(LS);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
save('\n');
|
||||
case l_c('\n'):
|
||||
save(L, l_c('\n'), l);
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
default:
|
||||
save_and_next(LS);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(LS); /* pass the second ']' */
|
||||
L->Mbuffer[L->Mbuffnext-2] = 0; /* erases ']]' */
|
||||
l->pTStr = luaS_new(L->Mbuffbase+2);
|
||||
L->Mbuffer[L->Mbuffnext-2] = ']'; /* restores ']]' */
|
||||
return STRING;
|
||||
save_and_next(L, LS, l); /* skip the second `]' */
|
||||
save(L, l_c('\0'), l);
|
||||
seminfo->ts = luaS_newlstr(L, (l_char *)G(L)->Mbuffer+2, l-5);
|
||||
}
|
||||
|
||||
|
||||
/* to avoid warnings; this declaration cannot be public since YYSTYPE
|
||||
** cannot be visible in llex.h (otherwise there is an error, since
|
||||
** the parser body redefines it!)
|
||||
*/
|
||||
int luaY_lex (YYSTYPE *l);
|
||||
int luaY_lex (YYSTYPE *l)
|
||||
{
|
||||
LexState *LS = L->lexstate;
|
||||
double a;
|
||||
luaL_resetbuffer();
|
||||
if (lua_debug)
|
||||
luaY_codedebugline(LS->linelasttoken);
|
||||
LS->linelasttoken = LS->linenumber;
|
||||
while (1) {
|
||||
static void read_string (LexState *LS, int del, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
while (LS->current != del) {
|
||||
checkbuffer(L, 10, l);
|
||||
switch (LS->current) {
|
||||
case EOZ: case l_c('\n'):
|
||||
save(L, l_c('\0'), l);
|
||||
luaX_error(LS, l_s("unfinished string"), TK_STRING);
|
||||
break; /* to avoid warnings */
|
||||
case l_c('\\'):
|
||||
next(LS); /* do not save the `\' */
|
||||
switch (LS->current) {
|
||||
case l_c('a'): save(L, l_c('\a'), l); next(LS); break;
|
||||
case l_c('b'): save(L, l_c('\b'), l); next(LS); break;
|
||||
case l_c('f'): save(L, l_c('\f'), l); next(LS); break;
|
||||
case l_c('n'): save(L, l_c('\n'), l); next(LS); break;
|
||||
case l_c('r'): save(L, l_c('\r'), l); next(LS); break;
|
||||
case l_c('t'): save(L, l_c('\t'), l); next(LS); break;
|
||||
case l_c('v'): save(L, l_c('\v'), l); next(LS); break;
|
||||
case l_c('\n'): save(L, l_c('\n'), l); inclinenumber(LS); break;
|
||||
default: {
|
||||
if (!isdigit(LS->current))
|
||||
save_and_next(L, LS, l); /* handles \\, \", \', and \? */
|
||||
else { /* \xxx */
|
||||
int c = 0;
|
||||
int i = 0;
|
||||
do {
|
||||
c = 10*c + (LS->current-l_c('0'));
|
||||
next(LS);
|
||||
} while (++i<3 && isdigit(LS->current));
|
||||
if (c > UCHAR_MAX) {
|
||||
save(L, l_c('\0'), l);
|
||||
luaX_error(LS, l_s("escape sequence too large"), TK_STRING);
|
||||
}
|
||||
save(L, c, l);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
}
|
||||
save_and_next(L, LS, l); /* skip delimiter */
|
||||
save(L, l_c('\0'), l);
|
||||
seminfo->ts = luaS_newlstr(L, (l_char *)G(L)->Mbuffer+1, l-3);
|
||||
}
|
||||
|
||||
|
||||
int luaX_lex (LexState *LS, SemInfo *seminfo) {
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
|
||||
case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
|
||||
case l_c(' '): case l_c('\t'): case l_c('\r'): /* `\r' to avoid problems with DOS */
|
||||
next(LS);
|
||||
continue;
|
||||
|
||||
case '\n':
|
||||
case l_c('\n'):
|
||||
inclinenumber(LS);
|
||||
LS->linelasttoken = LS->linenumber;
|
||||
continue;
|
||||
|
||||
case '-':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '-') return '-';
|
||||
do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
|
||||
luaL_resetbuffer();
|
||||
case l_c('$'):
|
||||
luaX_error(LS,
|
||||
l_s("unexpected `$' (pragmas are no longer supported)"),
|
||||
LS->current);
|
||||
break;
|
||||
|
||||
case l_c('-'):
|
||||
next(LS);
|
||||
if (LS->current != l_c('-')) return l_c('-');
|
||||
do { next(LS); } while (LS->current != l_c('\n') && LS->current != EOZ);
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '[') return '[';
|
||||
case l_c('['):
|
||||
next(LS);
|
||||
if (LS->current != l_c('[')) return l_c('[');
|
||||
else {
|
||||
save_and_next(LS); /* pass the second '[' */
|
||||
return read_long_string(LS, l);
|
||||
read_long_string(LS, seminfo);
|
||||
return TK_STRING;
|
||||
}
|
||||
|
||||
case '=':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '=';
|
||||
else { save_and_next(LS); return EQ; }
|
||||
case l_c('='):
|
||||
next(LS);
|
||||
if (LS->current != l_c('=')) return l_c('=');
|
||||
else { next(LS); return TK_EQ; }
|
||||
|
||||
case '<':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '<';
|
||||
else { save_and_next(LS); return LE; }
|
||||
case l_c('<'):
|
||||
next(LS);
|
||||
if (LS->current != l_c('=')) return l_c('<');
|
||||
else { next(LS); return TK_LE; }
|
||||
|
||||
case '>':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '>';
|
||||
else { save_and_next(LS); return GE; }
|
||||
case l_c('>'):
|
||||
next(LS);
|
||||
if (LS->current != l_c('=')) return l_c('>');
|
||||
else { next(LS); return TK_GE; }
|
||||
|
||||
case '~':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '~';
|
||||
else { save_and_next(LS); return NE; }
|
||||
case l_c('~'):
|
||||
next(LS);
|
||||
if (LS->current != l_c('=')) return l_c('~');
|
||||
else { next(LS); return TK_NE; }
|
||||
|
||||
case '"':
|
||||
case '\'': {
|
||||
int del = LS->current;
|
||||
save_and_next(LS);
|
||||
while (LS->current != del) {
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
case '\n':
|
||||
save(0);
|
||||
return WRONGTOKEN;
|
||||
case '\\':
|
||||
next(LS); /* do not save the '\' */
|
||||
switch (LS->current) {
|
||||
case 'n': save('\n'); next(LS); break;
|
||||
case 't': save('\t'); next(LS); break;
|
||||
case 'r': save('\r'); next(LS); break;
|
||||
case '\n': save('\n'); inclinenumber(LS); break;
|
||||
default : save_and_next(LS); break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next(LS);
|
||||
case l_c('"'):
|
||||
case l_c('\''):
|
||||
read_string(LS, LS->current, seminfo);
|
||||
return TK_STRING;
|
||||
|
||||
case l_c('.'):
|
||||
next(LS);
|
||||
if (LS->current == l_c('.')) {
|
||||
next(LS);
|
||||
if (LS->current == l_c('.')) {
|
||||
next(LS);
|
||||
return TK_DOTS; /* ... */
|
||||
}
|
||||
else return TK_CONCAT; /* .. */
|
||||
}
|
||||
next(LS); /* skip delimiter */
|
||||
save(0);
|
||||
l->pTStr = luaS_new(L->Mbuffbase+1);
|
||||
L->Mbuffer[L->Mbuffnext-1] = del; /* restore delimiter */
|
||||
return STRING;
|
||||
}
|
||||
|
||||
case '.':
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.')
|
||||
{
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.')
|
||||
{
|
||||
save_and_next(LS);
|
||||
return DOTS; /* ... */
|
||||
}
|
||||
else return CONC; /* .. */
|
||||
}
|
||||
else if (!isdigit(LS->current)) return '.';
|
||||
/* LS->current is a digit: goes through to number */
|
||||
a=0.0;
|
||||
goto fraction;
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
a=0.0;
|
||||
do {
|
||||
a=10.0*a+(LS->current-'0');
|
||||
save_and_next(LS);
|
||||
} while (isdigit(LS->current));
|
||||
if (LS->current == '.') {
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.') {
|
||||
save(0);
|
||||
luaY_error(
|
||||
"ambiguous syntax (decimal point x string concatenation)");
|
||||
}
|
||||
}
|
||||
fraction:
|
||||
{ double da=0.1;
|
||||
while (isdigit(LS->current))
|
||||
{
|
||||
a+=(LS->current-'0')*da;
|
||||
da/=10.0;
|
||||
save_and_next(LS);
|
||||
}
|
||||
if (toupper(LS->current) == 'E') {
|
||||
int e=0;
|
||||
int neg;
|
||||
double ea;
|
||||
save_and_next(LS);
|
||||
neg=(LS->current=='-');
|
||||
if (LS->current == '+' || LS->current == '-') save_and_next(LS);
|
||||
if (!isdigit(LS->current)) {
|
||||
save(0); return WRONGTOKEN; }
|
||||
do {
|
||||
e=10.0*e+(LS->current-'0');
|
||||
save_and_next(LS);
|
||||
} while (isdigit(LS->current));
|
||||
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
||||
{
|
||||
if (e & 1) a*=ea;
|
||||
ea*=ea;
|
||||
}
|
||||
}
|
||||
l->vReal = a;
|
||||
return NUMBER;
|
||||
else if (!isdigit(LS->current)) return l_c('.');
|
||||
else {
|
||||
read_number(LS, 1, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
|
||||
case EOZ:
|
||||
save(0);
|
||||
if (LS->iflevel > 0)
|
||||
luaY_syntaxerror("input ends inside a $if", "");
|
||||
return 0;
|
||||
return TK_EOS;
|
||||
|
||||
default:
|
||||
if (LS->current != '_' && !isalpha(LS->current)) {
|
||||
int c = LS->current;
|
||||
save_and_next(LS);
|
||||
return c;
|
||||
default: {
|
||||
if (isdigit(LS->current)) {
|
||||
read_number(LS, 0, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
else { /* identifier or reserved word */
|
||||
TaggedString *ts;
|
||||
do {
|
||||
save_and_next(LS);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(0);
|
||||
ts = luaS_new(L->Mbuffbase);
|
||||
if (ts->head.marked > 255)
|
||||
return ts->head.marked; /* reserved word */
|
||||
l->pTStr = ts;
|
||||
return NAME;
|
||||
else if (isalpha(LS->current) || LS->current == l_c('_')) {
|
||||
/* identifier or reserved word */
|
||||
size_t l = readname(LS);
|
||||
TString *ts = luaS_newlstr(LS->L, (l_char *)G(LS->L)->Mbuffer, l);
|
||||
if (ts->tsv.marked >= RESERVEDMARK) /* reserved word? */
|
||||
return ts->tsv.marked-RESERVEDMARK+FIRST_RESERVED;
|
||||
seminfo->ts = ts;
|
||||
return TK_NAME;
|
||||
}
|
||||
else {
|
||||
l_charint c = LS->current;
|
||||
if (iscntrl(c))
|
||||
luaX_invalidchar(LS, c);
|
||||
next(LS);
|
||||
return c; /* single-char tokens (+ - / ...) */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
66
llex.h
66
llex.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.6 1997/12/17 20:48:58 roberto Exp roberto $
|
||||
** Lexical Analizer
|
||||
** $Id: llex.h,v 1.36 2001/06/20 21:07:57 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -11,31 +11,63 @@
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
#define MAX_IFS 5
|
||||
#define FIRST_RESERVED 257
|
||||
|
||||
/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
|
||||
/* maximum length of a reserved word */
|
||||
#define TOKEN_LEN (sizeof(l_s("function"))/sizeof(l_char))
|
||||
|
||||
struct ifState {
|
||||
int elsepart; /* true if its in the $else part */
|
||||
int condition; /* true if $if condition is true */
|
||||
int skip; /* true if part must be skiped */
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER RESERVED"
|
||||
*/
|
||||
enum RESERVED {
|
||||
/* terminal symbols denoted by reserved words */
|
||||
TK_AND = FIRST_RESERVED, TK_BREAK,
|
||||
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FOR, TK_FUNCTION, TK_GLOBAL, TK_IF,
|
||||
TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, TK_RETURN, TK_THEN,
|
||||
TK_UNTIL, TK_WHILE,
|
||||
/* other terminal symbols */
|
||||
TK_NAME, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
|
||||
TK_STRING, TK_EOS
|
||||
};
|
||||
|
||||
/* number of reserved words */
|
||||
#define NUM_RESERVED ((int)(TK_WHILE-FIRST_RESERVED+1))
|
||||
|
||||
|
||||
typedef union {
|
||||
lua_Number r;
|
||||
TString *ts;
|
||||
} SemInfo; /* semantics information */
|
||||
|
||||
|
||||
typedef struct Token {
|
||||
int token;
|
||||
SemInfo seminfo;
|
||||
} Token;
|
||||
|
||||
|
||||
typedef struct LexState {
|
||||
int current; /* look ahead character */
|
||||
struct zio *lex_z; /* input stream */
|
||||
l_charint current; /* current character */
|
||||
Token t; /* current token */
|
||||
Token lookahead; /* look ahead token */
|
||||
struct FuncState *fs; /* `FuncState' is private to the parser */
|
||||
struct lua_State *L;
|
||||
struct zio *z; /* input stream */
|
||||
int linenumber; /* input line counter */
|
||||
int linelasttoken; /* line where last token was read */
|
||||
int lastline; /* last line wherein a SETLINE was generated */
|
||||
int iflevel; /* level of nested $if's (for lexical analysis) */
|
||||
struct ifState ifstate[MAX_IFS];
|
||||
int lastline; /* line of last token `consumed' */
|
||||
TString *source; /* current source name */
|
||||
} LexState;
|
||||
|
||||
|
||||
void luaX_init (void);
|
||||
void luaX_setinput (ZIO *z);
|
||||
char *luaX_lasttoken (void);
|
||||
void luaX_init (lua_State *L);
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source);
|
||||
int luaX_lex (LexState *LS, SemInfo *seminfo);
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const l_char *msg);
|
||||
void luaX_syntaxerror (LexState *ls, const l_char *s, const l_char *token);
|
||||
void luaX_error (LexState *ls, const l_char *s, int token);
|
||||
void luaX_token2str (int token, l_char *s);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
133
llimits.h
Normal file
133
llimits.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.29 2001/06/05 18:17:01 roberto Exp roberto $
|
||||
** Limits, basic types, and some other `installation-dependent' definitions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef llimits_h
|
||||
#define llimits_h
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/*
|
||||
** try to find number of bits in an integer
|
||||
*/
|
||||
#ifndef BITS_INT
|
||||
/* avoid overflows in comparison */
|
||||
#if INT_MAX-20 < 32760
|
||||
#define BITS_INT 16
|
||||
#else
|
||||
#if INT_MAX > 2147483640L
|
||||
/* machine has at least 32 bits */
|
||||
#define BITS_INT 32
|
||||
#else
|
||||
#error "you must define BITS_INT with number of bits in an integer"
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** the following types define integer types for values that may not
|
||||
** fit in a `small int' (16 bits), but may waste space in a
|
||||
** `large long' (64 bits). The current definitions should work in
|
||||
** any machine, but may not be optimal.
|
||||
*/
|
||||
|
||||
/* an unsigned integer to hold hash values */
|
||||
typedef unsigned int lu_hash;
|
||||
/* its signed equivalent */
|
||||
typedef int ls_hash;
|
||||
|
||||
/* an unsigned integer big enough to count the total memory used by Lua */
|
||||
typedef unsigned long lu_mem;
|
||||
|
||||
/* an integer big enough to count the number of strings in use */
|
||||
typedef long ls_nstr;
|
||||
|
||||
|
||||
/* chars used as small naturals (so that `char' is reserved for characteres) */
|
||||
typedef unsigned char lu_byte;
|
||||
|
||||
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0)-2)
|
||||
|
||||
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
|
||||
|
||||
/*
|
||||
** conversion of pointer to integer
|
||||
** this is for hashing only; there is no problem if the integer
|
||||
** cannot hold the whole pointer value
|
||||
** (the shift removes bits that are usually 0 because of alignment)
|
||||
*/
|
||||
#define IntPoint(p) ((((lu_hash)(p)) >> 4) ^ (lu_hash)(p))
|
||||
|
||||
|
||||
|
||||
#define MINPOWER2 4 /* minimum size for `growing' vectors */
|
||||
|
||||
|
||||
|
||||
#ifndef DEFAULT_STACK_SIZE
|
||||
#define DEFAULT_STACK_SIZE 1024
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* type to ensure maximum alignment */
|
||||
#ifndef LUSER_ALIGNMENT_T
|
||||
#define LUSER_ALIGNMENT_T double
|
||||
#endif
|
||||
union L_Umaxalign { LUSER_ALIGNMENT_T u; void *s; long l; };
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** type for virtual-machine instructions
|
||||
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||
*/
|
||||
typedef unsigned long Instruction;
|
||||
|
||||
|
||||
/* maximum stack for a Lua function */
|
||||
#define MAXSTACK 250
|
||||
|
||||
|
||||
/* maximum number of local variables */
|
||||
#ifndef MAXLOCALS
|
||||
#define MAXLOCALS 200 /* arbitrary limit (<MAXSTACK) */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of upvalues */
|
||||
#ifndef MAXUPVALUES
|
||||
#define MAXUPVALUES 32 /* arbitrary limit (<MAXSTACK) */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of parameters in a function */
|
||||
#ifndef MAXPARAMS
|
||||
#define MAXPARAMS 100 /* arbitrary limit (<MAXLOCALS) */
|
||||
#endif
|
||||
|
||||
|
||||
/* number of list items to accumulate before a SETLIST instruction */
|
||||
/* (must be a power of 2) */
|
||||
#define LFIELDS_PER_FLUSH 64
|
||||
|
||||
|
||||
|
||||
/* maximum lookback to find a real constant (for code generation) */
|
||||
#ifndef LOOKBACKNUMS
|
||||
#define LOOKBACKNUMS 40 /* arbitrary constant */
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
285
lmathlib.c
285
lmathlib.c
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.7 1997/12/09 13:35:19 roberto Exp roberto $
|
||||
** Lua standard mathematical library
|
||||
** $Id: lmathlib.c,v 1.37 2001/03/06 20:09:38 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -8,194 +8,233 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
#ifdef M_PI
|
||||
#define PI M_PI
|
||||
|
||||
#undef PI
|
||||
#define PI (3.14159265358979323846)
|
||||
#define RADIANS_PER_DEGREE (PI/180.0)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** If you want Lua to operate in radians (instead of degrees),
|
||||
** define RADIANS
|
||||
*/
|
||||
#ifdef RADIANS
|
||||
#define FROMRAD(a) (a)
|
||||
#define TORAD(a) (a)
|
||||
#else
|
||||
#define PI ((double)3.14159265358979323846)
|
||||
#define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
|
||||
#define TORAD(a) ((a)*RADIANS_PER_DEGREE)
|
||||
#endif
|
||||
|
||||
|
||||
#define FROMRAD(a) ((a)*(180.0/PI))
|
||||
#define TORAD(a) ((a)*(PI/180.0))
|
||||
|
||||
|
||||
static void math_abs (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
if (d < 0) d = -d;
|
||||
lua_pushnumber(d);
|
||||
static int math_abs (lua_State *L) {
|
||||
lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_sin (void)
|
||||
{
|
||||
lua_pushnumber(sin(TORAD(luaL_check_number(1))));
|
||||
static int math_sin (lua_State *L) {
|
||||
lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_cos (void)
|
||||
{
|
||||
lua_pushnumber(cos(TORAD(luaL_check_number(1))));
|
||||
static int math_cos (lua_State *L) {
|
||||
lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_tan (void)
|
||||
{
|
||||
lua_pushnumber(tan(TORAD(luaL_check_number(1))));
|
||||
static int math_tan (lua_State *L) {
|
||||
lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_asin (void)
|
||||
{
|
||||
lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
|
||||
static int math_asin (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_acos (void)
|
||||
{
|
||||
lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
|
||||
static int math_acos (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_atan (void)
|
||||
{
|
||||
lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
|
||||
static int math_atan (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_atan2 (void)
|
||||
{
|
||||
lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2))));
|
||||
static int math_atan2 (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_ceil (void)
|
||||
{
|
||||
lua_pushnumber(ceil(luaL_check_number(1)));
|
||||
static int math_ceil (lua_State *L) {
|
||||
lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_floor (void)
|
||||
{
|
||||
lua_pushnumber(floor(luaL_check_number(1)));
|
||||
static int math_floor (lua_State *L) {
|
||||
lua_pushnumber(L, floor(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_mod (void)
|
||||
{
|
||||
lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
|
||||
static int math_mod (lua_State *L) {
|
||||
lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_sqrt (void)
|
||||
{
|
||||
lua_pushnumber(sqrt(luaL_check_number(1)));
|
||||
static int math_sqrt (lua_State *L) {
|
||||
lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_pow (void)
|
||||
{
|
||||
lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
|
||||
static int math_pow (lua_State *L) {
|
||||
lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_log (void)
|
||||
{
|
||||
lua_pushnumber(log(luaL_check_number(1)));
|
||||
static int math_log (lua_State *L) {
|
||||
lua_pushnumber(L, log(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_log10 (void)
|
||||
{
|
||||
lua_pushnumber(log10(luaL_check_number(1)));
|
||||
static int math_log10 (lua_State *L) {
|
||||
lua_pushnumber(L, log10(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_exp (void)
|
||||
{
|
||||
lua_pushnumber(exp(luaL_check_number(1)));
|
||||
static int math_exp (lua_State *L) {
|
||||
lua_pushnumber(L, exp(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_deg (void)
|
||||
{
|
||||
lua_pushnumber(luaL_check_number(1)*(180.0/PI));
|
||||
static int math_deg (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_rad (void)
|
||||
{
|
||||
lua_pushnumber(luaL_check_number(1)*(PI/180.0));
|
||||
static int math_rad (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_frexp (lua_State *L) {
|
||||
int e;
|
||||
lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e));
|
||||
lua_pushnumber(L, e);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int math_ldexp (lua_State *L) {
|
||||
lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_min (void)
|
||||
{
|
||||
int i = 1;
|
||||
double dmin = luaL_check_number(i);
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT) {
|
||||
double d = luaL_check_number(i);
|
||||
static int math_min (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
lua_Number dmin = luaL_check_number(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
lua_Number d = luaL_check_number(L, i);
|
||||
if (d < dmin)
|
||||
dmin = d;
|
||||
}
|
||||
lua_pushnumber(dmin);
|
||||
lua_pushnumber(L, dmin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_max (void)
|
||||
{
|
||||
int i = 1;
|
||||
double dmax = luaL_check_number(i);
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT) {
|
||||
double d = luaL_check_number(i);
|
||||
static int math_max (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
lua_Number dmax = luaL_check_number(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
lua_Number d = luaL_check_number(L, i);
|
||||
if (d > dmax)
|
||||
dmax = d;
|
||||
}
|
||||
lua_pushnumber(dmax);
|
||||
lua_pushnumber(L, dmax);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_random (void)
|
||||
{
|
||||
/* the '%' is needed because on some sistems (SunOS!) "rand()" may */
|
||||
/* return a value bigger than RAND_MAX... */
|
||||
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
||||
double l = luaL_opt_number(1, 0);
|
||||
if (l == 0)
|
||||
lua_pushnumber(r);
|
||||
else
|
||||
lua_pushnumber((int)(r*l)+1);
|
||||
static int math_random (lua_State *L) {
|
||||
/* 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_check_int(L, 1);
|
||||
luaL_arg_check(L, 1<=u, 1, l_s("interval is empty"));
|
||||
lua_pushnumber(L, (int)(r*u)+1); /* integer between 1 and `u' */
|
||||
break;
|
||||
}
|
||||
case 2: { /* lower and upper limits */
|
||||
int l = luaL_check_int(L, 1);
|
||||
int u = luaL_check_int(L, 2);
|
||||
luaL_arg_check(L, l<=u, 2, l_s("interval is empty"));
|
||||
lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
|
||||
break;
|
||||
}
|
||||
default: lua_error(L, l_s("wrong number of arguments"));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_randomseed (void)
|
||||
{
|
||||
srand(luaL_check_number(1));
|
||||
static int math_randomseed (lua_State *L) {
|
||||
srand(luaL_check_int(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static struct luaL_reg mathlib[] = {
|
||||
{"abs", math_abs},
|
||||
{"sin", math_sin},
|
||||
{"cos", math_cos},
|
||||
{"tan", math_tan},
|
||||
{"asin", math_asin},
|
||||
{"acos", math_acos},
|
||||
{"atan", math_atan},
|
||||
{"atan2", math_atan2},
|
||||
{"ceil", math_ceil},
|
||||
{"floor", math_floor},
|
||||
{"mod", math_mod},
|
||||
{"sqrt", math_sqrt},
|
||||
{"min", math_min},
|
||||
{"max", math_max},
|
||||
{"log", math_log},
|
||||
{"log10", math_log10},
|
||||
{"exp", math_exp},
|
||||
{"deg", math_deg},
|
||||
{"rad", math_rad},
|
||||
{"random", math_random},
|
||||
{"randomseed", math_randomseed}
|
||||
static const luaL_reg mathlib[] = {
|
||||
{l_s("abs"), math_abs},
|
||||
{l_s("sin"), math_sin},
|
||||
{l_s("cos"), math_cos},
|
||||
{l_s("tan"), math_tan},
|
||||
{l_s("asin"), math_asin},
|
||||
{l_s("acos"), math_acos},
|
||||
{l_s("atan"), math_atan},
|
||||
{l_s("atan2"), math_atan2},
|
||||
{l_s("ceil"), math_ceil},
|
||||
{l_s("floor"), math_floor},
|
||||
{l_s("mod"), math_mod},
|
||||
{l_s("frexp"), math_frexp},
|
||||
{l_s("ldexp"), math_ldexp},
|
||||
{l_s("sqrt"), math_sqrt},
|
||||
{l_s("min"), math_min},
|
||||
{l_s("max"), math_max},
|
||||
{l_s("log"), math_log},
|
||||
{l_s("log10"), math_log10},
|
||||
{l_s("exp"), math_exp},
|
||||
{l_s("deg"), math_deg},
|
||||
{l_s("rad"), math_rad},
|
||||
{l_s("random"), math_random},
|
||||
{l_s("randomseed"), math_randomseed}
|
||||
};
|
||||
|
||||
/*
|
||||
** Open math library
|
||||
*/
|
||||
void lua_mathlibopen (void)
|
||||
{
|
||||
luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
|
||||
lua_pushstring("deg"); lua_setglobal("_TRIGMODE");
|
||||
lua_pushcfunction(math_pow);
|
||||
lua_pushnumber(0); /* to get its tag */
|
||||
lua_settagmethod(lua_tag(lua_pop()), "pow");
|
||||
lua_pushnumber(PI); lua_setglobal("PI");
|
||||
LUALIB_API int lua_mathlibopen (lua_State *L) {
|
||||
luaL_openl(L, mathlib);
|
||||
lua_pushcfunction(L, math_pow);
|
||||
lua_settagmethod(L, LUA_TNUMBER, l_s("pow"));
|
||||
lua_pushnumber(L, PI);
|
||||
lua_setglobal(L, l_s("PI"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
132
lmem.c
132
lmem.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.3 1997/12/01 20:30:44 roberto Exp roberto $
|
||||
** $Id: lmem.c,v 1.48 2001/02/23 17:17:25 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,104 +7,62 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
int luaM_growaux (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit)
|
||||
{
|
||||
if (nelems >= limit)
|
||||
lua_error(errormsg);
|
||||
nelems = (nelems == 0) ? 32 : nelems*2;
|
||||
if (nelems > limit)
|
||||
nelems = limit;
|
||||
*block = luaM_realloc(*block, nelems*size);
|
||||
return (int)nelems;
|
||||
|
||||
#ifndef l_realloc
|
||||
#define l_realloc(b,os,s) realloc(b,s)
|
||||
#define l_free(b,s) free(b)
|
||||
#endif
|
||||
|
||||
|
||||
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
|
||||
int limit, const l_char *errormsg) {
|
||||
void *newblock;
|
||||
int newsize = (*size)*2;
|
||||
if (newsize < MINPOWER2)
|
||||
newsize = MINPOWER2; /* minimum size */
|
||||
else if (*size >= limit/2) { /* cannot double it? */
|
||||
if (*size < limit - MINPOWER2) /* try something smaller... */
|
||||
newsize = limit; /* still have at least MINPOWER2 free places */
|
||||
else luaD_error(L, errormsg);
|
||||
}
|
||||
newblock = luaM_realloc(L, block, (lu_mem)(*size)*(lu_mem)size_elems,
|
||||
(lu_mem)newsize*(lu_mem)size_elems);
|
||||
*size = newsize; /* update only when everything else is OK */
|
||||
return newblock;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifndef DEBUG
|
||||
|
||||
/*
|
||||
** generic allocation routine.
|
||||
** real ANSI systems do not need some of these tests,
|
||||
** since realloc(NULL, s)==malloc(s) and realloc(b, 0)==free(b).
|
||||
** But some systems (e.g. Sun OS) are not that ANSI...
|
||||
*/
|
||||
void *luaM_realloc (void *block, unsigned long size)
|
||||
{
|
||||
size_t s = (size_t)size;
|
||||
if (s != size)
|
||||
lua_error("Allocation Error: Block too big");
|
||||
void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
|
||||
if (size == 0) {
|
||||
if (block) {
|
||||
free(block);
|
||||
}
|
||||
return NULL;
|
||||
l_free(block, oldsize); /* block may be NULL; that is OK for free */
|
||||
block = NULL;
|
||||
}
|
||||
else if (size >= MAX_SIZET)
|
||||
luaD_error(L, l_s("memory allocation error: block too big"));
|
||||
else {
|
||||
block = l_realloc(block, oldsize, size);
|
||||
if (block == NULL) {
|
||||
if (L)
|
||||
luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
|
||||
else return NULL; /* error before creating state! */
|
||||
}
|
||||
}
|
||||
if (L && G(L)) {
|
||||
G(L)->nblocks -= oldsize;
|
||||
G(L)->nblocks += size;
|
||||
}
|
||||
block = block ? realloc(block, s) : malloc(s);
|
||||
if (block == NULL)
|
||||
lua_error(memEM);
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
/* DEBUG */
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define MARK 55
|
||||
|
||||
unsigned long numblocks = 0;
|
||||
unsigned long totalmem = 0;
|
||||
|
||||
|
||||
static void *checkblock (void *block)
|
||||
{
|
||||
unsigned long *b = (unsigned long *)block - 1;
|
||||
unsigned long size = *b;
|
||||
assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
|
||||
numblocks--;
|
||||
totalmem -= size;
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
void *luaM_realloc (void *block, unsigned long size)
|
||||
{
|
||||
unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
|
||||
if (realsize != (size_t)realsize)
|
||||
lua_error("Allocation Error: Block too big");
|
||||
if (size == 0) { /* ANSI doen't need this, but some machines... */
|
||||
if (block) {
|
||||
memset(block, -1, *((unsigned long *)block-1)); /* erase block */
|
||||
block = checkblock(block);
|
||||
free(block);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
if (block) {
|
||||
block = checkblock(block);
|
||||
block = (unsigned long *)realloc(block, realsize);
|
||||
}
|
||||
else
|
||||
block = (unsigned long *)malloc(realsize);
|
||||
if (block == NULL)
|
||||
lua_error(memEM);
|
||||
totalmem += size;
|
||||
numblocks++;
|
||||
*(unsigned long *)block = size;
|
||||
*(((char *)block)+size+sizeof(unsigned long)) = MARK;
|
||||
return (unsigned long *)block+1;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
46
lmem.h
46
lmem.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.4 1997/12/01 20:30:44 roberto Exp roberto $
|
||||
** $Id: lmem.h,v 1.21 2001/02/20 18:15:33 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,35 +8,33 @@
|
||||
#define lmem_h
|
||||
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
/* memory error messages */
|
||||
#define codeEM "code size overflow"
|
||||
#define constantEM "constant table overflow"
|
||||
#define refEM "reference table overflow"
|
||||
#define tableEM "table overflow"
|
||||
#define memEM "not enough memory"
|
||||
void *luaM_realloc (lua_State *L, void *oldblock, lu_mem oldsize, lu_mem size);
|
||||
|
||||
void *luaM_realloc (void *oldblock, unsigned long size);
|
||||
int luaM_growaux (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit);
|
||||
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elem,
|
||||
int limit, const l_char *errormsg);
|
||||
|
||||
#define luaM_free(b) luaM_realloc((b), 0)
|
||||
#define luaM_malloc(t) luaM_realloc(NULL, (t))
|
||||
#define luaM_new(t) ((t *)luaM_malloc(sizeof(t)))
|
||||
#define luaM_newvector(n,t) ((t *)luaM_malloc((n)*sizeof(t)))
|
||||
#define luaM_growvector(old,n,t,e,l) \
|
||||
(luaM_growaux((void**)old,n,sizeof(t),e,l))
|
||||
#define luaM_reallocvector(v,n,t) ((t *)luaM_realloc(v,(n)*sizeof(t)))
|
||||
#define luaM_free(L, b, s) luaM_realloc(L, (b), (s), 0)
|
||||
#define luaM_freelem(L, b, t) luaM_realloc(L, (b), sizeof(t), 0)
|
||||
#define luaM_freearray(L, b, n, t) luaM_realloc(L, (b), \
|
||||
((lu_mem)(n)*(lu_mem)sizeof(t)), 0)
|
||||
|
||||
#define luaM_malloc(L, t) luaM_realloc(L, NULL, 0, (t))
|
||||
#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t)))
|
||||
#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, \
|
||||
(lu_mem)(n)*(lu_mem)sizeof(t)))
|
||||
|
||||
#ifdef DEBUG
|
||||
extern unsigned long numblocks;
|
||||
extern unsigned long totalmem;
|
||||
#endif
|
||||
#define luaM_growvector(L,v,nelems,size,t,limit,e) \
|
||||
if (((nelems)+1) > (size)) \
|
||||
((v)=(t *)luaM_growaux(L,v,&(size),sizeof(t),limit,e))
|
||||
|
||||
#define luaM_reallocvector(L, v,oldn,n,t) \
|
||||
((v)=(t *)luaM_realloc(L, v,(lu_mem)(oldn)*(lu_mem)sizeof(t), \
|
||||
(lu_mem)(n)*(lu_mem)sizeof(t)))
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
141
lobject.c
141
lobject.c
@@ -1,94 +1,105 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 1.9 1997/12/26 18:38:16 roberto Exp roberto $
|
||||
** $Id: lobject.c,v 1.69 2001/03/07 12:27:06 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lobject.h"
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
char *luaO_typenames[] = { /* ORDER LUA_T */
|
||||
"userdata", "number", "string", "table", "function", "function",
|
||||
"nil", "function", "mark", "mark", "mark", "line", NULL
|
||||
};
|
||||
|
||||
|
||||
TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
|
||||
/* hash dimensions values */
|
||||
static long dimensions[] =
|
||||
{5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
|
||||
12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
|
||||
1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
|
||||
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
|
||||
|
||||
|
||||
int luaO_redimension (int oldsize)
|
||||
{
|
||||
int i;
|
||||
for (i=0; dimensions[i]<MAX_INT; i++) {
|
||||
if (dimensions[i] > oldsize)
|
||||
return dimensions[i];
|
||||
}
|
||||
lua_error("table overflow");
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
int luaO_equalObj (TObject *t1, TObject *t2)
|
||||
{
|
||||
int luaO_equalObj (const TObject *t1, const TObject *t2) {
|
||||
if (ttype(t1) != ttype(t2)) return 0;
|
||||
switch (ttype(t1)) {
|
||||
case LUA_T_NIL: return 1;
|
||||
case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
|
||||
case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2);
|
||||
case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
|
||||
case LUA_T_PROTO: return tfvalue(t1) == tfvalue(t2);
|
||||
case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2);
|
||||
case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl;
|
||||
default:
|
||||
lua_error("internal error in `lua_equalObj'");
|
||||
return 0; /* UNREACHEABLE */
|
||||
case LUA_TNUMBER:
|
||||
return nvalue(t1) == nvalue(t2);
|
||||
case LUA_TNIL:
|
||||
return 1;
|
||||
default: /* all other types are equal if pointers are equal */
|
||||
return tsvalue(t1) == tsvalue(t2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaO_findstring (char *name, char *list[])
|
||||
{
|
||||
int i;
|
||||
for (i=0; list[i]; i++)
|
||||
if (strcmp(list[i], name) == 0)
|
||||
return i;
|
||||
return -1; /* name not found */
|
||||
void *luaO_openspaceaux (lua_State *L, size_t n) {
|
||||
if (n > G(L)->Mbuffsize) {
|
||||
G(L)->Mbuffer = luaM_realloc(L, G(L)->Mbuffer, G(L)->Mbuffsize, n);
|
||||
G(L)->Mbuffsize = n;
|
||||
}
|
||||
return G(L)->Mbuffer;
|
||||
}
|
||||
|
||||
|
||||
void luaO_insertlist (GCnode *root, GCnode *node)
|
||||
{
|
||||
node->next = root->next;
|
||||
root->next = node;
|
||||
node->marked = 0;
|
||||
int luaO_str2d (const l_char *s, lua_Number *result) {
|
||||
l_char *endptr;
|
||||
lua_Number res = lua_str2number(s, &endptr);
|
||||
if (endptr == s) return 0; /* no conversion */
|
||||
while (isspace(uchar(*endptr))) endptr++;
|
||||
if (*endptr != l_c('\0')) return 0; /* invalid trailing characters? */
|
||||
*result = res;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef OLD_ANSI
|
||||
void luaO_memup (void *dest, void *src, int size)
|
||||
{
|
||||
char *d = dest;
|
||||
char *s = src;
|
||||
while (size--) d[size]=s[size];
|
||||
|
||||
/* maximum length of a string format for `luaO_verror' */
|
||||
#define MAX_VERROR 280
|
||||
|
||||
/* this function needs to handle only '%d' and '%.XXs' formats */
|
||||
void luaO_verror (lua_State *L, const l_char *fmt, ...) {
|
||||
va_list argp;
|
||||
l_char buff[MAX_VERROR]; /* to hold formatted message */
|
||||
va_start(argp, fmt);
|
||||
vsprintf(buff, fmt, argp);
|
||||
va_end(argp);
|
||||
luaD_error(L, buff);
|
||||
}
|
||||
|
||||
void luaO_memdown (void *dest, void *src, int size)
|
||||
{
|
||||
char *d = dest;
|
||||
char *s = src;
|
||||
int i;
|
||||
for (i=0; i<size; i++) d[i]=s[i];
|
||||
}
|
||||
#endif
|
||||
|
||||
void luaO_chunkid (l_char *out, const l_char *source, int bufflen) {
|
||||
if (*source == l_c('=')) {
|
||||
strncpy(out, source+1, bufflen); /* remove first char */
|
||||
out[bufflen-1] = l_c('\0'); /* ensures null termination */
|
||||
}
|
||||
else {
|
||||
if (*source == l_c('@')) {
|
||||
int l;
|
||||
source++; /* skip the `@' */
|
||||
bufflen -= sizeof(l_s("file `...%s'"));
|
||||
l = strlen(source);
|
||||
if (l>bufflen) {
|
||||
source += (l-bufflen); /* get last part of file name */
|
||||
sprintf(out, l_s("file `...%.99s'"), source);
|
||||
}
|
||||
else
|
||||
sprintf(out, l_s("file `%.99s'"), source);
|
||||
}
|
||||
else {
|
||||
int len = strcspn(source, l_s("\n")); /* stop at first newline */
|
||||
bufflen -= sizeof(l_s("string \"%.*s...\""));
|
||||
if (len > bufflen) len = bufflen;
|
||||
if (source[len] != l_c('\0')) { /* must truncate? */
|
||||
strcpy(out, l_s("string \""));
|
||||
out += strlen(out);
|
||||
strncpy(out, source, len);
|
||||
strcpy(out+len, l_s("...\""));
|
||||
}
|
||||
else
|
||||
sprintf(out, l_s("string \"%.99s\""), source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
275
lobject.h
275
lobject.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 1.15 1998/01/13 13:27:25 roberto Exp $
|
||||
** $Id: lobject.h,v 1.108 2001/06/28 14:48:44 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,177 +8,224 @@
|
||||
#define lobject_h
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/*
|
||||
** "real" is the type "number" of Lua
|
||||
** GREP LUA_NUMBER to change that
|
||||
*/
|
||||
#ifndef real
|
||||
#define real float
|
||||
#ifndef lua_assert
|
||||
#define lua_assert(c) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
#define Byte lua_Byte /* some systems have Byte as a predefined type */
|
||||
typedef unsigned char Byte; /* unsigned 8 bits */
|
||||
|
||||
#define MAX_WORD (65534U) /* maximum value of a word (-2 for safety) */
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of a int (-2 for safety) */
|
||||
|
||||
typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
|
||||
#ifndef UNUSED
|
||||
#define UNUSED(x) ((void)(x)) /* to avoid warnings */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Lua TYPES
|
||||
** WARNING: if you change the order of this enumeration,
|
||||
** grep "ORDER LUA_T"
|
||||
*/
|
||||
typedef enum {
|
||||
LUA_T_USERDATA = 0, /* tag default for userdata */
|
||||
LUA_T_NUMBER = -1, /* fixed tag for numbers */
|
||||
LUA_T_STRING = -2, /* fixed tag for strings */
|
||||
LUA_T_ARRAY = -3, /* tag default for tables (or arrays) */
|
||||
LUA_T_PROTO = -4, /* fixed tag for functions */
|
||||
LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */
|
||||
LUA_T_NIL = -6, /* last "pre-defined" tag */
|
||||
LUA_T_CLOSURE = -7,
|
||||
LUA_T_CLMARK = -8, /* mark for closures */
|
||||
LUA_T_PMARK = -9, /* mark for Lua prototypes */
|
||||
LUA_T_CMARK = -10, /* mark for C prototypes */
|
||||
LUA_T_LINE = -11
|
||||
} lua_Type;
|
||||
|
||||
#define NUM_TYPES 11
|
||||
#define NUM_TAGS 7
|
||||
/* tags for values visible from Lua == first user-created tag */
|
||||
#define NUM_TAGS 6
|
||||
|
||||
|
||||
typedef union {
|
||||
lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
|
||||
real n; /* LUA_T_NUMBER */
|
||||
struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
|
||||
struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
|
||||
struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
|
||||
struct Hash *a; /* LUA_T_ARRAY */
|
||||
int i; /* LUA_T_LINE */
|
||||
union TString *ts;
|
||||
union Udata *u;
|
||||
struct Closure *cl;
|
||||
struct Hash *h;
|
||||
lua_Number n; /* LUA_TNUMBER */
|
||||
} Value;
|
||||
|
||||
|
||||
typedef struct TObject {
|
||||
lua_Type ttype;
|
||||
typedef struct lua_TObject {
|
||||
int tt;
|
||||
Value value;
|
||||
} TObject;
|
||||
|
||||
|
||||
/* Macros to access values */
|
||||
#define ttype(o) ((o)->tt)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define uvalue(o) ((o)->value.u)
|
||||
#define clvalue(o) ((o)->value.cl)
|
||||
#define hvalue(o) ((o)->value.h)
|
||||
|
||||
/*
|
||||
** generic header for garbage collector lists
|
||||
*/
|
||||
typedef struct GCnode {
|
||||
struct GCnode *next;
|
||||
int marked;
|
||||
} GCnode;
|
||||
|
||||
/* Macros to set values */
|
||||
#define setnvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TNUMBER; _o->value.n=(x); }
|
||||
|
||||
#define chgnvalue(obj,x) ((obj)->value.n=(x))
|
||||
|
||||
#define setsvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TSTRING; _o->value.ts=(x); }
|
||||
|
||||
#define setuvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TUSERDATA; _o->value.u=(x); }
|
||||
|
||||
#define setclvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TFUNCTION; _o->value.cl=(x); }
|
||||
|
||||
#define sethvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TTABLE; _o->value.h=(x); }
|
||||
|
||||
#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
|
||||
|
||||
#define setobj(obj1,obj2) \
|
||||
{ TObject *o1=(obj1); const TObject *o2=(obj2); \
|
||||
o1->tt=o2->tt; o1->value = o2->value; }
|
||||
|
||||
#define setttype(obj, tt) (ttype(obj) = (tt))
|
||||
|
||||
|
||||
|
||||
typedef TObject *StkId; /* index to stack elements */
|
||||
|
||||
|
||||
/*
|
||||
** String headers for string table
|
||||
*/
|
||||
typedef union TString {
|
||||
union L_Umaxalign dummy; /* ensures maximum alignment for strings */
|
||||
struct {
|
||||
lu_hash hash;
|
||||
size_t len;
|
||||
unsigned short constindex; /* hint to reuse constants */
|
||||
short marked;
|
||||
union TString *nexthash; /* chain for hash table */
|
||||
} tsv;
|
||||
} TString;
|
||||
|
||||
typedef struct TaggedString {
|
||||
GCnode head;
|
||||
int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
|
||||
unsigned long hash;
|
||||
union {
|
||||
TObject globalval;
|
||||
struct {
|
||||
int tag;
|
||||
void *v; /* if this is a userdata, here is its value */
|
||||
} d;
|
||||
} u;
|
||||
char str[1]; /* \0 byte already reserved */
|
||||
} TaggedString;
|
||||
|
||||
#define getstr(ts) ((l_char *)((ts) + 1))
|
||||
#define svalue(o) getstr(tsvalue(o))
|
||||
|
||||
|
||||
|
||||
typedef union Udata {
|
||||
union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
|
||||
struct {
|
||||
int tag; /* negative means `marked' (only during GC) */
|
||||
void *value;
|
||||
size_t len;
|
||||
union Udata *next; /* chain for list of all udata */
|
||||
} uv;
|
||||
} Udata;
|
||||
|
||||
|
||||
#define switchudatamark(u) ((u)->uv.tag = (-((u)->uv.tag+1)))
|
||||
#define ismarkedudata(u) ((u)->uv.tag < 0)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Function Prototypes
|
||||
*/
|
||||
typedef struct TProtoFunc {
|
||||
GCnode head;
|
||||
struct TObject *consts;
|
||||
int nconsts;
|
||||
Byte *code; /* ends with opcode ENDCODE */
|
||||
typedef struct Proto {
|
||||
TObject *k; /* constants used by the function */
|
||||
int sizek; /* size of `k' */
|
||||
struct Proto **p; /* functions defined inside the function */
|
||||
int sizep; /* size of `p' */
|
||||
Instruction *code;
|
||||
int sizecode;
|
||||
short nupvalues;
|
||||
short numparams;
|
||||
short is_vararg;
|
||||
short maxstacksize;
|
||||
short marked;
|
||||
struct Proto *next;
|
||||
/* debug information */
|
||||
int *lineinfo; /* map from opcodes to source lines */
|
||||
int sizelineinfo; /* size of `lineinfo' */
|
||||
struct LocVar *locvars; /* information about local variables */
|
||||
int sizelocvars;
|
||||
int lineDefined;
|
||||
TaggedString *fileName;
|
||||
struct LocVar *locvars; /* ends with line = -1 */
|
||||
} TProtoFunc;
|
||||
TString *source;
|
||||
} Proto;
|
||||
|
||||
|
||||
typedef struct LocVar {
|
||||
TaggedString *varname; /* NULL signals end of scope */
|
||||
int line;
|
||||
TString *varname;
|
||||
int startpc; /* first point where variable is active */
|
||||
int endpc; /* first point where variable is dead */
|
||||
} LocVar;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Macros to access structure members */
|
||||
#define ttype(o) ((o)->ttype)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define svalue(o) ((o)->value.ts->str)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define clvalue(o) ((o)->value.cl)
|
||||
#define avalue(o) ((o)->value.a)
|
||||
#define fvalue(o) ((o)->value.f)
|
||||
#define tfvalue(o) ((o)->value.tf)
|
||||
|
||||
#define protovalue(o) ((o)->value.cl->consts)
|
||||
|
||||
|
||||
/*
|
||||
** Closures
|
||||
*/
|
||||
typedef struct Closure {
|
||||
GCnode head;
|
||||
int nelems; /* not included the first one (always the prototype) */
|
||||
TObject consts[1]; /* at least one for prototype */
|
||||
int isC; /* 0 for Lua functions, 1 for C functions */
|
||||
int nupvalues;
|
||||
union {
|
||||
lua_CFunction c; /* C functions */
|
||||
struct Proto *l; /* Lua functions */
|
||||
} f;
|
||||
struct Closure *next;
|
||||
struct Closure *mark; /* marked closures (point to itself when not marked) */
|
||||
TObject upvalue[1];
|
||||
} Closure;
|
||||
|
||||
|
||||
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
|
||||
|
||||
typedef struct node {
|
||||
TObject ref;
|
||||
|
||||
typedef struct Node {
|
||||
struct Node *next; /* for chaining */
|
||||
TObject key;
|
||||
TObject val;
|
||||
} Node;
|
||||
|
||||
|
||||
typedef struct Hash {
|
||||
GCnode head;
|
||||
Node *node;
|
||||
int nhash;
|
||||
int nuse;
|
||||
int htag;
|
||||
int size;
|
||||
Node *firstfree; /* this position is free; all positions after it are full */
|
||||
struct Hash *next;
|
||||
struct Hash *mark; /* marked tables (point to itself when not marked) */
|
||||
int weakmode;
|
||||
} Hash;
|
||||
|
||||
|
||||
extern char *luaO_typenames[];
|
||||
/* unmarked tables and closures are represented by pointing `mark' to
|
||||
** themselves
|
||||
*/
|
||||
#define ismarked(x) ((x)->mark != (x))
|
||||
|
||||
extern TObject luaO_nilobject;
|
||||
|
||||
int luaO_equalObj (TObject *t1, TObject *t2);
|
||||
int luaO_redimension (int oldsize);
|
||||
int luaO_findstring (char *name, char *list[]);
|
||||
void luaO_insertlist (GCnode *root, GCnode *node);
|
||||
/*
|
||||
** `module' operation for hashing (size is always a power of 2)
|
||||
*/
|
||||
#define lmod(s,size) ((int)((s) & ((size)-1)))
|
||||
|
||||
|
||||
/*
|
||||
** informations about a call (for debugging)
|
||||
*/
|
||||
typedef struct CallInfo {
|
||||
struct CallInfo *prev; /* linked list */
|
||||
StkId base; /* base for called function */
|
||||
const Instruction **pc; /* current pc of called function */
|
||||
int lastpc; /* last pc traced */
|
||||
int line; /* current line */
|
||||
int refi; /* current index in `lineinfo' */
|
||||
} CallInfo;
|
||||
|
||||
#define ci_func(ci) (clvalue((ci)->base - 1))
|
||||
|
||||
|
||||
extern const TObject luaO_nilobject;
|
||||
|
||||
|
||||
#define luaO_openspace(L,n,t) ((t *)luaO_openspaceaux(L,(n)*sizeof(t)))
|
||||
void *luaO_openspaceaux (lua_State *L, size_t n);
|
||||
|
||||
int luaO_equalObj (const TObject *t1, const TObject *t2);
|
||||
int luaO_str2d (const l_char *s, lua_Number *result);
|
||||
|
||||
void luaO_verror (lua_State *L, const l_char *fmt, ...);
|
||||
void luaO_chunkid (l_char *out, const l_char *source, int len);
|
||||
|
||||
#ifdef OLD_ANSI
|
||||
void luaO_memup (void *dest, void *src, int size);
|
||||
void luaO_memdown (void *dest, void *src, int size);
|
||||
#else
|
||||
#include <string.h>
|
||||
#define luaO_memup(d,s,n) memmove(d,s,n)
|
||||
#define luaO_memdown(d,s,n) memmove(d,s,n)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
107
lopcodes.c
Normal file
107
lopcodes.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
** $Id:$
|
||||
** extracted automatically from lopcodes.h by mkprint.lua
|
||||
** DO NOT EDIT
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lopcodes.h"
|
||||
|
||||
|
||||
#ifdef LUA_OPNAMES
|
||||
|
||||
const l_char *const luaP_opnames[] = {
|
||||
l_s("MOVE"),
|
||||
l_s("LOADK"),
|
||||
l_s("LOADINT"),
|
||||
l_s("LOADNIL"),
|
||||
l_s("LOADUPVAL"),
|
||||
l_s("GETGLOBAL"),
|
||||
l_s("GETTABLE"),
|
||||
l_s("SETGLOBAL"),
|
||||
l_s("SETTABLE"),
|
||||
l_s("NEWTABLE"),
|
||||
l_s("SELF"),
|
||||
l_s("ADD"),
|
||||
l_s("SUB"),
|
||||
l_s("MUL"),
|
||||
l_s("DIV"),
|
||||
l_s("POW"),
|
||||
l_s("UNM"),
|
||||
l_s("NOT"),
|
||||
l_s("CONCAT"),
|
||||
l_s("JMP"),
|
||||
l_s("CJMP"),
|
||||
l_s("TESTEQ"),
|
||||
l_s("TESTNE"),
|
||||
l_s("TESTLT"),
|
||||
l_s("TESTLE"),
|
||||
l_s("TESTGT"),
|
||||
l_s("TESTGE"),
|
||||
l_s("TESTT"),
|
||||
l_s("TESTF"),
|
||||
l_s("NILJMP"),
|
||||
l_s("CALL"),
|
||||
l_s("RETURN"),
|
||||
l_s("FORPREP"),
|
||||
l_s("FORLOOP"),
|
||||
l_s("TFORPREP"),
|
||||
l_s("TFORLOOP"),
|
||||
l_s("SETLIST"),
|
||||
l_s("SETLISTO"),
|
||||
l_s("CLOSURE")
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#define opmode(t,x,b,c,sa,k,m) (((t)<<OpModeT) | \
|
||||
((b)<<OpModeBreg) | ((c)<<OpModeCreg) | \
|
||||
((sa)<<OpModesetA) | ((k)<<OpModeK) | (m))
|
||||
|
||||
const lu_byte luaP_opmodes[] = {
|
||||
/* T _ B C sA K mode opcode */
|
||||
opmode(0,0,1,0, 1,0,iABC) /* OP_MOVE */
|
||||
,opmode(0,0,0,0, 1,1,iABc) /* OP_LOADK */
|
||||
,opmode(0,0,0,0, 1,0,iAsBc) /* OP_LOADINT */
|
||||
,opmode(0,0,1,0, 1,0,iABC) /* OP_LOADNIL */
|
||||
,opmode(0,0,0,0, 1,0,iABc) /* OP_LOADUPVAL */
|
||||
,opmode(0,0,0,0, 1,1,iABc) /* OP_GETGLOBAL */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_GETTABLE */
|
||||
,opmode(0,0,0,0, 0,1,iABc) /* OP_SETGLOBAL */
|
||||
,opmode(0,0,1,1, 0,0,iABC) /* OP_SETTABLE */
|
||||
,opmode(0,0,0,0, 1,0,iABc) /* OP_NEWTABLE */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_SELF */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_ADD */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_SUB */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_MUL */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_DIV */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_POW */
|
||||
,opmode(0,0,1,0, 1,0,iABC) /* OP_UNM */
|
||||
,opmode(0,0,1,0, 1,0,iABC) /* OP_NOT */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_CONCAT */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_JMP */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_CJMP */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTEQ */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTNE */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTLT */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTLE */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTGT */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTGE */
|
||||
,opmode(1,0,1,0, 1,0,iABC) /* OP_TESTT */
|
||||
,opmode(1,0,1,0, 1,0,iABC) /* OP_TESTF */
|
||||
,opmode(0,0,0,0, 1,0,iABc) /* OP_NILJMP */
|
||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_CALL */
|
||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_RETURN */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_FORPREP */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_FORLOOP */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_TFORPREP */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_TFORLOOP */
|
||||
,opmode(0,0,0,0, 0,0,iABc) /* OP_SETLIST */
|
||||
,opmode(0,0,0,0, 0,0,iABc) /* OP_SETLISTO */
|
||||
,opmode(0,0,0,0, 1,0,iABc) /* OP_CLOSURE */
|
||||
};
|
||||
|
||||
340
lopcodes.h
340
lopcodes.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.14 1997/12/30 19:08:23 roberto Exp roberto $
|
||||
** $Id: lopcodes.h,v 1.77 2001/07/03 17:01:34 roberto Exp roberto $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,176 +7,222 @@
|
||||
#ifndef lopcodes_h
|
||||
#define lopcodes_h
|
||||
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*===========================================================================
|
||||
We assume that instructions are unsigned numbers.
|
||||
All instructions have an opcode in the first 6 bits.
|
||||
Instructions can have the following fields:
|
||||
`A' : 8 bits (25-32)
|
||||
`B' : 8 bits (17-24)
|
||||
`C' : 10 bits (7-16)
|
||||
`Bc' : 18 bits (`B' and `C' together)
|
||||
`sBc' : signed Bc
|
||||
|
||||
A signed argument is represented in excess K; that is, the number
|
||||
value is the unsigned value minus K. K is exactly the maximum value
|
||||
for that argument (so that -max is represented by 0, and +max is
|
||||
represented by 2*max), which is half the maximum for the corresponding
|
||||
unsigned argument.
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
enum OpMode {iABC, iABc, iAsBc}; /* basic instruction format */
|
||||
|
||||
|
||||
/*
|
||||
** NOTICE: variants of the same opcode must be consecutive: First, those
|
||||
** with byte parameter, then with built-in parameters, and last with
|
||||
** word parameter.
|
||||
** size and position of opcode arguments.
|
||||
*/
|
||||
#define SIZE_C 10
|
||||
#define SIZE_B 8
|
||||
#define SIZE_Bc (SIZE_C + SIZE_B)
|
||||
#define SIZE_A 8
|
||||
|
||||
#define SIZE_OP 6
|
||||
|
||||
#define POS_C SIZE_OP
|
||||
#define POS_B (POS_C + SIZE_C)
|
||||
#define POS_Bc POS_C
|
||||
#define POS_A (POS_B + SIZE_B)
|
||||
|
||||
|
||||
/*
|
||||
** limits for opcode arguments.
|
||||
** we use (signed) int to manipulate most arguments,
|
||||
** so they must fit in BITS_INT-1 bits (-1 for sign)
|
||||
*/
|
||||
#if SIZE_Bc < BITS_INT-1
|
||||
#define MAXARG_Bc ((1<<SIZE_Bc)-1)
|
||||
#define MAXARG_sBc (MAXARG_Bc>>1) /* `sBc' is signed */
|
||||
#else
|
||||
#define MAXARG_Bc MAX_INT
|
||||
#define MAXARG_sBc MAX_INT
|
||||
#endif
|
||||
|
||||
|
||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||
#define MAXARG_C ((1<<SIZE_C)-1)
|
||||
|
||||
|
||||
/* creates a mask with `n' 1 bits at position `p' */
|
||||
#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
|
||||
|
||||
/* creates a mask with `n' 0 bits at position `p' */
|
||||
#define MASK0(n,p) (~MASK1(n,p))
|
||||
|
||||
/*
|
||||
** the following macros help to manipulate instructions
|
||||
*/
|
||||
|
||||
#define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
|
||||
#define SET_OPCODE(i,o) (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o))
|
||||
|
||||
#define GETARG_A(i) ((int)((i)>>POS_A))
|
||||
#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
|
||||
((Instruction)(u)<<POS_A)))
|
||||
|
||||
#define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
|
||||
#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
|
||||
((Instruction)(b)<<POS_B)))
|
||||
|
||||
#define GETARG_C(i) ((int)(((i)>>POS_C) & MASK1(SIZE_C,0)))
|
||||
#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
|
||||
((Instruction)(b)<<POS_C)))
|
||||
|
||||
#define GETARG_Bc(i) ((int)(((i)>>POS_Bc) & MASK1(SIZE_Bc,0)))
|
||||
#define SETARG_Bc(i,b) ((i) = (((i)&MASK0(SIZE_Bc,POS_Bc)) | \
|
||||
((Instruction)(b)<<POS_Bc)))
|
||||
|
||||
#define GETARG_sBc(i) (GETARG_Bc(i)-MAXARG_sBc)
|
||||
#define SETARG_sBc(i,b) SETARG_Bc((i),(unsigned int)((b)+MAXARG_sBc))
|
||||
|
||||
|
||||
#define CREATE_ABC(o,a,b,c) ((Instruction)(o) \
|
||||
| ((Instruction)(a)<<POS_A) \
|
||||
| ((Instruction)(b)<<POS_B) \
|
||||
| ((Instruction)(c)<<POS_C))
|
||||
|
||||
#define CREATE_ABc(o,a,bc) ((Instruction)(o) \
|
||||
| ((Instruction)(a)<<POS_A) \
|
||||
| ((Instruction)(bc)<<POS_Bc))
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** an invalid register that fits in 8 bits
|
||||
*/
|
||||
#define NO_REG MAXARG_A
|
||||
|
||||
|
||||
/*
|
||||
** R(x) - register
|
||||
** Kst(x) - constant (in constant table)
|
||||
** R/K(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
/* name parm before after side effect
|
||||
-----------------------------------------------------------------------------*/
|
||||
ENDCODE,/* - - - */
|
||||
/*----------------------------------------------------------------------
|
||||
name args description
|
||||
------------------------------------------------------------------------*/
|
||||
OP_MOVE,/* A B R(A) := R(B) */
|
||||
OP_LOADK,/* A Bc R(A) := Kst(Bc) */
|
||||
OP_LOADINT,/* A sBc R(A) := (Number)sBc */
|
||||
OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
|
||||
OP_LOADUPVAL,/* A Bc R(A) := UpValue[Bc] */
|
||||
|
||||
PUSHNIL,/* b - nil_0...nil_b */
|
||||
PUSHNIL0,/* - - nil */
|
||||
OP_GETGLOBAL,/* A Bc R(A) := Gbl[Kst(Bc)] */
|
||||
OP_GETTABLE,/* A B C R(A) := R(B)[R/K(C)] */
|
||||
|
||||
PUSHNUMBER,/* b - (float)b */
|
||||
PUSHNUMBER0,/* - - 0.0 */
|
||||
PUSHNUMBER1,/* - - 1.0 */
|
||||
PUSHNUMBER2,/* - - 2.0 */
|
||||
PUSHNUMBERW,/* w - (float)w */
|
||||
OP_SETGLOBAL,/* A Bc Gbl[Kst(Bc)] := R(A) */
|
||||
OP_SETTABLE,/* A B C R(B)[R/K(C)] := R(A) */
|
||||
|
||||
PUSHCONSTANT,/* b - CNST[b] */
|
||||
PUSHCONSTANT0,/*- - CNST[0] */
|
||||
PUSHCONSTANT1,/*- - CNST[1] */
|
||||
PUSHCONSTANT2,/*- - CNST[2] */
|
||||
PUSHCONSTANT3,/*- - CNST[3] */
|
||||
PUSHCONSTANT4,/*- - CNST[4] */
|
||||
PUSHCONSTANT5,/*- - CNST[5] */
|
||||
PUSHCONSTANT6,/*- - CNST[6] */
|
||||
PUSHCONSTANT7,/*- - CNST[7] */
|
||||
PUSHCONSTANTW,/*w - CNST[w] */
|
||||
OP_NEWTABLE,/* A Bc R(A) := {} (size = Bc) */
|
||||
|
||||
PUSHUPVALUE,/* b - Closure[b] */
|
||||
PUSHUPVALUE0,/* - - Closure[0] */
|
||||
PUSHUPVALUE1,/* - - Closure[1] */
|
||||
OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[R/K(C)] */
|
||||
|
||||
PUSHLOCAL,/* b - LOC[b] */
|
||||
PUSHLOCAL0,/* - - LOC[0] */
|
||||
PUSHLOCAL1,/* - - LOC[1] */
|
||||
PUSHLOCAL2,/* - - LOC[2] */
|
||||
PUSHLOCAL3,/* - - LOC[3] */
|
||||
PUSHLOCAL4,/* - - LOC[4] */
|
||||
PUSHLOCAL5,/* - - LOC[5] */
|
||||
PUSHLOCAL6,/* - - LOC[6] */
|
||||
PUSHLOCAL7,/* - - LOC[7] */
|
||||
OP_ADD,/* A B C R(A) := R(B) + R/K(C) */
|
||||
OP_SUB,/* A B C R(A) := R(B) - R/K(C) */
|
||||
OP_MUL,/* A B C R(A) := R(B) * R/K(C) */
|
||||
OP_DIV,/* A B C R(A) := R(B) / R/K(C) */
|
||||
OP_POW,/* A B C R(A) := R(B) ^ R/K(C) */
|
||||
OP_UNM,/* A B R(A) := -R(B) */
|
||||
OP_NOT,/* A B R(A) := not R(B) */
|
||||
|
||||
GETGLOBAL,/* b - VAR[CNST[b]] */
|
||||
GETGLOBAL0,/* - - VAR[CNST[0]] */
|
||||
GETGLOBAL1,/* - - VAR[CNST[1]] */
|
||||
GETGLOBAL2,/* - - VAR[CNST[2]] */
|
||||
GETGLOBAL3,/* - - VAR[CNST[3]] */
|
||||
GETGLOBAL4,/* - - VAR[CNST[4]] */
|
||||
GETGLOBAL5,/* - - VAR[CNST[5]] */
|
||||
GETGLOBAL6,/* - - VAR[CNST[6]] */
|
||||
GETGLOBAL7,/* - - VAR[CNST[7]] */
|
||||
GETGLOBALW,/* w - VAR[CNST[w]] */
|
||||
OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
|
||||
|
||||
GETTABLE,/* - i t t[i] */
|
||||
OP_JMP,/* sBc PC += sBc */
|
||||
OP_CJMP,/* sBc if test then PC += sBc (see (1)) */
|
||||
|
||||
GETDOTTED,/* b t t[CONST[b]] */
|
||||
GETDOTTED0,/* - t t[CONST[0]] */
|
||||
GETDOTTED1,/* - t t[CONST[1]] */
|
||||
GETDOTTED2,/* - t t[CONST[2]] */
|
||||
GETDOTTED3,/* - t t[CONST[3]] */
|
||||
GETDOTTED4,/* - t t[CONST[4]] */
|
||||
GETDOTTED5,/* - t t[CONST[5]] */
|
||||
GETDOTTED6,/* - t t[CONST[6]] */
|
||||
GETDOTTED7,/* - t t[CONST[7]] */
|
||||
GETDOTTEDW,/* w t t[CONST[w]] */
|
||||
OP_TESTEQ,/* A C test := (R(A) == R/K(C)) */
|
||||
OP_TESTNE,/* A C test := (R(A) ~= R/K(C)) */
|
||||
OP_TESTLT,/* A C test := (R(A) < R/K(C)) */
|
||||
OP_TESTLE,/* A C test := (R(A) <= R/K(C)) */
|
||||
OP_TESTGT,/* A C test := (R(A) > R/K(C)) */
|
||||
OP_TESTGE,/* A C test := (R(A) >= R/K(C)) */
|
||||
|
||||
PUSHSELF,/* b t t t[CNST[b]] */
|
||||
PUSHSELF0,/* - t t t[CNST[0]] */
|
||||
PUSHSELF1,/* - t t t[CNST[1]] */
|
||||
PUSHSELF2,/* - t t t[CNST[2]] */
|
||||
PUSHSELF3,/* - t t t[CNST[3]] */
|
||||
PUSHSELF4,/* - t t t[CNST[4]] */
|
||||
PUSHSELF5,/* - t t t[CNST[5]] */
|
||||
PUSHSELF6,/* - t t t[CNST[6]] */
|
||||
PUSHSELF7,/* - t t t[CNST[7]] */
|
||||
PUSHSELFW,/* w t t t[CNST[w]] */
|
||||
OP_TESTT,/* A B test := R(B); if (test) R(A) := R(B) */
|
||||
OP_TESTF,/* A B test := not R(B); if (test) R(A) := nil */
|
||||
|
||||
CREATEARRAY,/* b - newarray(size = b) */
|
||||
CREATEARRAY0,/* - - newarray(size = 0) */
|
||||
CREATEARRAY1,/* - - newarray(size = 1) */
|
||||
CREATEARRAYW,/* w - newarray(size = w) */
|
||||
OP_NILJMP,/* A Bc R(A) := nil; PC++; */
|
||||
|
||||
SETLOCAL,/* b x - LOC[b]=x */
|
||||
SETLOCAL0,/* - x - LOC[0]=x */
|
||||
SETLOCAL1,/* - x - LOC[1]=x */
|
||||
SETLOCAL2,/* - x - LOC[2]=x */
|
||||
SETLOCAL3,/* - x - LOC[3]=x */
|
||||
SETLOCAL4,/* - x - LOC[4]=x */
|
||||
SETLOCAL5,/* - x - LOC[5]=x */
|
||||
SETLOCAL6,/* - x - LOC[6]=x */
|
||||
SETLOCAL7,/* - x - LOC[7]=x */
|
||||
OP_CALL,/* A B C R(A), ... ,R(A+C-1) := R(A)(R(A+1), ... ,R(A+B))*/
|
||||
OP_RETURN,/* A B return R(A), ... ,R(A+B-1) (see (3)) */
|
||||
|
||||
SETGLOBAL,/* b x - VAR[CNST[b]]=x */
|
||||
SETGLOBAL0,/* - x - VAR[CNST[0]]=x */
|
||||
SETGLOBAL1,/* - x - VAR[CNST[1]]=x */
|
||||
SETGLOBAL2,/* - x - VAR[CNST[2]]=x */
|
||||
SETGLOBAL3,/* - x - VAR[CNST[3]]=x */
|
||||
SETGLOBAL4,/* - x - VAR[CNST[4]]=x */
|
||||
SETGLOBAL5,/* - x - VAR[CNST[5]]=x */
|
||||
SETGLOBAL6,/* - x - VAR[CNST[6]]=x */
|
||||
SETGLOBAL7,/* - x - VAR[CNST[7]]=x */
|
||||
SETGLOBALW,/* w x - VAR[CNST[w]]=x */
|
||||
OP_FORPREP,/* A sBc */
|
||||
OP_FORLOOP,/* A sBc */
|
||||
|
||||
SETTABLE0,/* - v i t - t[i]=v */
|
||||
OP_TFORPREP,/* A sBc */
|
||||
OP_TFORLOOP,/* A sBc */
|
||||
|
||||
SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
|
||||
|
||||
SETLIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */
|
||||
SETLIST0,/* b v_b...v_1 t - t[i]=v_i */
|
||||
SETLISTW,/* w c v_b...v_1 t - t[i+c*FPF]=v_i */
|
||||
|
||||
SETMAP,/* b v_b k_b ...v_0 k_0 t t t[k_i]=v_i */
|
||||
SETMAP0,/* - v_0 k_0 t t t[k_0]=v_0 */
|
||||
|
||||
EQOP,/* - y x (x==y)? 1 : nil */
|
||||
NEQOP,/* - y x (x~=y)? 1 : nil */
|
||||
LTOP,/* - y x (x<y)? 1 : nil */
|
||||
LEOP,/* - y x (x<y)? 1 : nil */
|
||||
GTOP,/* - y x (x>y)? 1 : nil */
|
||||
GEOP,/* - y x (x>=y)? 1 : nil */
|
||||
ADDOP,/* - y x x+y */
|
||||
SUBOP,/* - y x x-y */
|
||||
MULTOP,/* - y x x*y */
|
||||
DIVOP,/* - y x x/y */
|
||||
POWOP,/* - y x x^y */
|
||||
CONCOP,/* - y x x..y */
|
||||
MINUSOP,/* - x -x */
|
||||
NOTOP,/* - x (x==nil)? 1 : nil */
|
||||
|
||||
ONTJMP,/* b x (x!=nil)? x : - (x!=nil)? PC+=b */
|
||||
ONTJMPW,/* w x (x!=nil)? x : - (x!=nil)? PC+=w */
|
||||
ONFJMP,/* b x (x==nil)? x : - (x==nil)? PC+=b */
|
||||
ONFJMPW,/* w x (x==nil)? x : - (x==nil)? PC+=w */
|
||||
JMP,/* b - - PC+=b */
|
||||
JMPW,/* w - - PC+=w */
|
||||
IFFJMP,/* b x - (x==nil)? PC+=b */
|
||||
IFFJMPW,/* w x - (x==nil)? PC+=w */
|
||||
IFTUPJMP,/* b x - (x!=nil)? PC-=b */
|
||||
IFTUPJMPW,/* w x - (x!=nil)? PC-=w */
|
||||
IFFUPJMP,/* b x - (x==nil)? PC-=b */
|
||||
IFFUPJMPW,/* w x - (x==nil)? PC-=w */
|
||||
|
||||
CLOSURE,/* b proto v_b...v_1 c(proto) */
|
||||
CLOSURE0,/* - proto c(proto) */
|
||||
CLOSURE1,/* - proto v_1 c(proto) */
|
||||
|
||||
CALLFUNC,/* b c v_c...v_1 f r_b...r_1 f(v1,...,v_c) */
|
||||
CALLFUNC0,/* b v_b...v_1 f - f(v1,...,v_b) */
|
||||
CALLFUNC1,/* b v_b...v_1 f r_1 f(v1,...,v_b) */
|
||||
|
||||
RETCODE,/* b - - */
|
||||
|
||||
SETLINE,/* b - - LINE=b */
|
||||
SETLINEW,/* w - - LINE=w */
|
||||
|
||||
POP,/* b - - TOP-=(b+1) */
|
||||
POP0,/* - - - TOP-=1 */
|
||||
POP1/* - - - TOP-=2 */
|
||||
OP_SETLIST,/* A Bc R(A)[Bc-Bc%FPF+i] := R(A+i), 1 <= i <= Bc%FPF+1 */
|
||||
OP_SETLISTO,/* A Bc */
|
||||
|
||||
OP_CLOSURE /* A Bc R(A) := closure(KPROTO[Bc], R(A), ... ,R(A+n)) */
|
||||
} OpCode;
|
||||
|
||||
|
||||
#define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
|
||||
#define LFIELDS_PER_FLUSH 64 /* lists (SETLIST) */
|
||||
#define NUM_OPCODES ((int)OP_CLOSURE+1)
|
||||
|
||||
|
||||
|
||||
/*===========================================================================
|
||||
Notes:
|
||||
(1) In the current implementation there is no `test' variable;
|
||||
instructions OP_TEST* and OP_CJMP must always occur together.
|
||||
|
||||
(2) In OP_CALL, if (B == NO_REG) then B = top. C is the number of returns,
|
||||
and can be NO_REG. OP_CALL can set `top' to last_result+1, so
|
||||
next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.
|
||||
|
||||
(3) In OP_RETURN, if (B == NO_REG) then return up to `top'
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** masks for instruction properties
|
||||
*/
|
||||
enum OpModeMask {
|
||||
OpModeBreg = 2, /* B is a register */
|
||||
OpModeCreg, /* C is a register/constant */
|
||||
OpModesetA, /* instruction set register A */
|
||||
OpModeK, /* Bc is a constant */
|
||||
OpModeT /* operator is a test */
|
||||
};
|
||||
|
||||
extern const lu_byte luaP_opmodes[];
|
||||
|
||||
#define getOpMode(m) ((enum OpMode)(luaP_opmodes[m] & 3))
|
||||
#define testOpMode(m, b) (luaP_opmodes[m] & (1 << (b)))
|
||||
|
||||
|
||||
/*
|
||||
** opcode names (only included when compiled with LUA_OPNAMES)
|
||||
*/
|
||||
extern const l_char *const luaP_opnames[];
|
||||
|
||||
#define ZEROVARARG 64
|
||||
|
||||
#endif
|
||||
|
||||
62
lparser.h
62
lparser.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $
|
||||
** Syntax analizer and code generator
|
||||
** $Id: lparser.h,v 1.31 2001/06/05 18:17:01 roberto Exp roberto $
|
||||
** LL(1) Parser and code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -11,10 +11,60 @@
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
void luaY_codedebugline (int line);
|
||||
TProtoFunc *luaY_parser (ZIO *z);
|
||||
void luaY_error (char *s);
|
||||
void luaY_syntaxerror (char *s, char *token);
|
||||
/*
|
||||
** Expression descriptor
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
VVOID, /* no value */
|
||||
VNIL,
|
||||
VNUMBER, /* n = value */
|
||||
VK, /* info = index of constant in `k' */
|
||||
VGLOBAL, /* info = index of global name in `k' */
|
||||
VLOCAL, /* info = local register */
|
||||
VINDEXED, /* info = table register; aux = index register (or `k') */
|
||||
VRELOCABLE, /* info = instruction pc */
|
||||
VNONRELOC, /* info = result register */
|
||||
VJMP, /* info = result register */
|
||||
VCALL /* info = result register */
|
||||
} expkind;
|
||||
|
||||
typedef struct expdesc {
|
||||
expkind k;
|
||||
union {
|
||||
struct {
|
||||
int info, aux;
|
||||
} i;
|
||||
lua_Number n;
|
||||
} u;
|
||||
int t; /* patch list of `exit when true' */
|
||||
int f; /* patch list of `exit when false' */
|
||||
} expdesc;
|
||||
|
||||
|
||||
/* state needed to generate code for a given function */
|
||||
typedef struct FuncState {
|
||||
Proto *f; /* current function header */
|
||||
struct FuncState *prev; /* enclosing function */
|
||||
struct LexState *ls; /* lexical state */
|
||||
struct lua_State *L; /* copy of the Lua state */
|
||||
int pc; /* next position to code (equivalent to `ncode') */
|
||||
int lasttarget; /* `pc' of last `jump target' */
|
||||
int jlt; /* list of jumps to `lasttarget' */
|
||||
int freereg; /* first free register */
|
||||
int nk; /* number of elements in `k' */
|
||||
int np; /* number of elements in `p' */
|
||||
int nlineinfo; /* number of elements in `lineinfo' */
|
||||
int nlocvars; /* number of elements in `locvars' */
|
||||
int nactloc; /* number of active local variables */
|
||||
int lastline; /* line where last `lineinfo' was generated */
|
||||
struct Breaklabel *bl; /* chain of breakable blocks */
|
||||
expdesc upvalues[MAXUPVALUES]; /* upvalues */
|
||||
int actloc[MAXLOCALS]; /* local-variable stack (indices to locvars) */
|
||||
} FuncState;
|
||||
|
||||
|
||||
Proto *luaY_parser (lua_State *L, ZIO *z);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
176
lstate.c
176
lstate.c
@@ -1,13 +1,16 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 1.4 1997/12/11 14:48:46 roberto Exp roberto $
|
||||
** $Id: lstate.c,v 1.65 2001/06/21 16:41:34 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include "lbuiltin.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
@@ -17,62 +20,123 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
LState *lua_state = NULL;
|
||||
struct Sopen {
|
||||
int stacksize;
|
||||
lua_State *L;
|
||||
};
|
||||
|
||||
|
||||
void lua_open (void)
|
||||
{
|
||||
if (lua_state) return;
|
||||
lua_state = luaM_new(LState);
|
||||
L->numCblocks = 0;
|
||||
L->Cstack.base = 0;
|
||||
L->Cstack.lua2C = 0;
|
||||
L->Cstack.num = 0;
|
||||
L->errorJmp = NULL;
|
||||
L->rootproto.next = NULL;
|
||||
L->rootproto.marked = 0;
|
||||
L->rootcl.next = NULL;
|
||||
L->rootcl.marked = 0;
|
||||
L->rootglobal.next = NULL;
|
||||
L->rootglobal.marked = 0;
|
||||
L->roottable.next = NULL;
|
||||
L->roottable.marked = 0;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
L->Mbuffsize = 0;
|
||||
L->Mbuffnext = 0;
|
||||
L->Mbuffbase = NULL;
|
||||
L->Mbuffer = NULL;
|
||||
L->GCthreshold = GARBAGE_BLOCK;
|
||||
L->nblocks = 0;
|
||||
luaD_init();
|
||||
luaS_init();
|
||||
luaX_init();
|
||||
luaT_init();
|
||||
luaB_predefine();
|
||||
static void close_state (lua_State *L, lua_State *OL);
|
||||
|
||||
|
||||
/*
|
||||
** open parts that may cause memory-allocation errors
|
||||
*/
|
||||
static void f_luaopen (lua_State *L, void *ud) {
|
||||
struct Sopen *so = (struct Sopen *)ud;
|
||||
if (so->stacksize == 0)
|
||||
so->stacksize = DEFAULT_STACK_SIZE;
|
||||
else
|
||||
so->stacksize += LUA_MINSTACK;
|
||||
if (so->L != NULL) { /* shared global state? */
|
||||
L->G = G(so->L);
|
||||
L->gt = so->L->gt; /* share table of globals */
|
||||
so->L->next->previous = L; /* insert L into linked list */
|
||||
L->next = so->L->next;
|
||||
so->L->next = L;
|
||||
L->previous = so->L;
|
||||
luaD_init(L, so->stacksize); /* init stack */
|
||||
}
|
||||
else { /* create a new global state */
|
||||
L->G = luaM_new(L, global_State);
|
||||
G(L)->strt.size = 0;
|
||||
G(L)->strt.nuse = 0;
|
||||
G(L)->strt.hash = NULL;
|
||||
G(L)->Mbuffer = NULL;
|
||||
G(L)->Mbuffsize = 0;
|
||||
G(L)->rootproto = NULL;
|
||||
G(L)->rootcl = NULL;
|
||||
G(L)->roottable = NULL;
|
||||
G(L)->rootudata = NULL;
|
||||
G(L)->TMtable = NULL;
|
||||
G(L)->sizeTM = 0;
|
||||
G(L)->ntag = 0;
|
||||
G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
|
||||
luaD_init(L, so->stacksize); /* init stack */
|
||||
L->gt = luaH_new(L, 10); /* table of globals */
|
||||
G(L)->type2tag = luaH_new(L, 10);
|
||||
G(L)->registry = luaH_new(L, 0);
|
||||
G(L)->weakregistry = luaH_new(L, 0);
|
||||
/* make weakregistry weak */
|
||||
G(L)->weakregistry->weakmode = LUA_WEAK_KEY | LUA_WEAK_VALUE;
|
||||
luaS_resize(L, MINPOWER2);
|
||||
luaX_init(L);
|
||||
luaT_init(L);
|
||||
G(L)->GCthreshold = 4*G(L)->nblocks;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void lua_close (void)
|
||||
{
|
||||
TaggedString *alludata = luaS_collectudata();
|
||||
L->GCthreshold = MAX_INT; /* to avoid GC during GC */
|
||||
luaC_hashcallIM((Hash *)L->roottable.next); /* GC t.methods for tables */
|
||||
luaC_strcallIM(alludata); /* GC tag methods for userdata */
|
||||
luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
|
||||
luaH_free((Hash *)L->roottable.next);
|
||||
luaF_freeproto((TProtoFunc *)L->rootproto.next);
|
||||
luaF_freeclosure((Closure *)L->rootcl.next);
|
||||
luaS_free(alludata);
|
||||
luaS_freeall();
|
||||
luaM_free(L->stack.stack);
|
||||
luaM_free(L->IMtable);
|
||||
luaM_free(L->refArray);
|
||||
luaM_free(L->Mbuffer);
|
||||
luaM_free(L);
|
||||
L = NULL;
|
||||
#ifdef DEBUG
|
||||
printf("total de blocos: %ld\n", numblocks);
|
||||
printf("total de memoria: %ld\n", totalmem);
|
||||
#endif
|
||||
LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
|
||||
struct Sopen so;
|
||||
lua_State *L;
|
||||
if (OL) lua_lock(OL);
|
||||
L = luaM_new(OL, lua_State);
|
||||
if (L) { /* allocation OK? */
|
||||
L->G = NULL;
|
||||
L->stack = NULL;
|
||||
L->stacksize = 0;
|
||||
L->ci = &L->basefunc;
|
||||
L->basefunc.prev = NULL;
|
||||
L->errorJmp = NULL;
|
||||
L->callhook = NULL;
|
||||
L->linehook = NULL;
|
||||
L->allowhooks = 1;
|
||||
L->next = L->previous = L;
|
||||
so.stacksize = stacksize;
|
||||
so.L = OL;
|
||||
if (luaD_runprotected(L, f_luaopen, &so) != 0) {
|
||||
/* memory allocation error: free partial state */
|
||||
close_state(L, OL);
|
||||
L = NULL;
|
||||
}
|
||||
}
|
||||
if (OL) lua_unlock(OL);
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
static void close_state (lua_State *L, lua_State *OL) {
|
||||
if (OL != NULL) { /* are there other threads? */
|
||||
lua_assert(L->previous != L);
|
||||
L->previous->next = L->next;
|
||||
L->next->previous = L->previous;
|
||||
}
|
||||
else if (G(L)) { /* last thread; close global state */
|
||||
luaC_callallgcTM(L); /* call GC tag methods for all udata */
|
||||
luaC_collect(L, 1); /* collect all elements */
|
||||
lua_assert(G(L)->rootproto == NULL);
|
||||
lua_assert(G(L)->rootudata == NULL);
|
||||
lua_assert(G(L)->rootcl == NULL);
|
||||
lua_assert(G(L)->roottable == NULL);
|
||||
luaS_freeall(L);
|
||||
luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM);
|
||||
luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, l_char);
|
||||
luaM_freelem(NULL, L->G, global_State);
|
||||
}
|
||||
luaM_freearray(OL, L->stack, L->stacksize, TObject);
|
||||
luaM_freelem(OL, L, lua_State);
|
||||
}
|
||||
|
||||
LUA_API void lua_close (lua_State *L) {
|
||||
lua_State *OL;
|
||||
lua_assert(L != lua_state || lua_gettop(L) == 0);
|
||||
lua_lock(L);
|
||||
OL = L->next; /* any surviving thread (if there is one) */
|
||||
if (OL == L) OL = NULL; /* no surviving threads */
|
||||
close_state(L, OL);
|
||||
if (OL) lua_unlock(OL); /* cannot unlock over a freed state */
|
||||
lua_assert(L != lua_state || memdebug_numblocks == 0);
|
||||
lua_assert(L != lua_state || memdebug_total == 0);
|
||||
}
|
||||
|
||||
|
||||
128
lstate.h
128
lstate.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 1.6 1997/12/17 20:48:58 roberto Exp roberto $
|
||||
** $Id: lstate.h,v 1.57 2001/06/06 18:00:19 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,74 +8,94 @@
|
||||
#define lstate_h
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
#define MAX_C_BLOCKS 10
|
||||
/*
|
||||
** macros for thread syncronization inside Lua core machine:
|
||||
** all accesses to the global state and to global objects are syncronized.
|
||||
** Because threads can read the stack of other threads
|
||||
** (when running garbage collection),
|
||||
** a thread must also syncronize any write-access to its own stack.
|
||||
** Unsyncronized accesses are allowed only when reading its own stack,
|
||||
** or when reading immutable fields from global objects
|
||||
** (such as string values and udata values).
|
||||
*/
|
||||
#ifndef lua_lock
|
||||
#define lua_lock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
#define GARBAGE_BLOCK 150
|
||||
#ifndef lua_unlock
|
||||
#define lua_unlock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
** macro to allow the inclusion of user information in Lua state
|
||||
*/
|
||||
#ifndef LUA_USERSTATE
|
||||
#define LUA_USERSTATE
|
||||
#endif
|
||||
|
||||
|
||||
typedef int StkId; /* index to stack elements */
|
||||
|
||||
struct Stack {
|
||||
TObject *top;
|
||||
TObject *stack;
|
||||
TObject *last;
|
||||
};
|
||||
|
||||
struct C_Lua_Stack {
|
||||
StkId base; /* when Lua calls C or C calls Lua, points to */
|
||||
/* the first slot after the last parameter. */
|
||||
StkId lua2C; /* points to first element of "array" lua2C */
|
||||
int num; /* size of "array" lua2C */
|
||||
};
|
||||
struct lua_longjmp; /* defined in ldo.c */
|
||||
struct TM; /* defined in ltm.h */
|
||||
|
||||
|
||||
typedef struct {
|
||||
typedef struct stringtable {
|
||||
int size;
|
||||
int nuse; /* number of elements (including EMPTYs) */
|
||||
TaggedString **hash;
|
||||
ls_nstr nuse; /* number of elements */
|
||||
TString **hash;
|
||||
} stringtable;
|
||||
|
||||
|
||||
struct ref {
|
||||
TObject o;
|
||||
enum {LOCK, HOLD, FREE, COLLECTED} status;
|
||||
/*
|
||||
** `global state', shared by all threads of this state
|
||||
*/
|
||||
typedef struct global_State {
|
||||
void *Mbuffer; /* global buffer */
|
||||
size_t Mbuffsize; /* size of Mbuffer */
|
||||
Proto *rootproto; /* list of all prototypes */
|
||||
Closure *rootcl; /* list of all closures */
|
||||
Hash *roottable; /* list of all tables */
|
||||
Udata *rootudata; /* list of all userdata */
|
||||
stringtable strt; /* hash table for strings */
|
||||
Hash *type2tag; /* hash table from type names to tags */
|
||||
Hash *registry; /* (strong) registry table */
|
||||
Hash *weakregistry; /* weakregistry table */
|
||||
struct TM *TMtable; /* table for tag methods */
|
||||
int sizeTM; /* size of TMtable */
|
||||
int ntag; /* number of tags in TMtable */
|
||||
lu_mem GCthreshold;
|
||||
lu_mem nblocks; /* number of `bytes' currently allocated */
|
||||
} global_State;
|
||||
|
||||
|
||||
/*
|
||||
** `per thread' state
|
||||
*/
|
||||
struct lua_State {
|
||||
LUA_USERSTATE
|
||||
StkId top; /* first free slot in the stack */
|
||||
CallInfo *ci; /* call info for current function */
|
||||
StkId stack_last; /* last free slot in the stack */
|
||||
Hash *gt; /* table for globals */
|
||||
global_State *G;
|
||||
StkId stack; /* stack base */
|
||||
int stacksize;
|
||||
lua_Hook callhook;
|
||||
lua_Hook linehook;
|
||||
int allowhooks;
|
||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||
lua_State *next; /* circular double linked list of states */
|
||||
lua_State *previous;
|
||||
CallInfo basefunc;
|
||||
};
|
||||
|
||||
|
||||
typedef struct LState {
|
||||
struct Stack stack; /* Lua stack */
|
||||
struct C_Lua_Stack Cstack; /* C2lua struct */
|
||||
void *errorJmp; /* current error recover point */
|
||||
TObject errorim; /* error tag method */
|
||||
GCnode rootproto; /* list of all prototypes */
|
||||
GCnode rootcl; /* list of all closures */
|
||||
GCnode roottable; /* list of all tables */
|
||||
GCnode rootglobal; /* list of strings with global values */
|
||||
stringtable *string_root; /* array of hash tables for strings and udata */
|
||||
struct IM *IMtable; /* table for tag methods */
|
||||
int IMtable_size; /* size of IMtable */
|
||||
int last_tag; /* last used tag in IMtable */
|
||||
struct FuncState *mainState, *currState; /* point to local structs in yacc */
|
||||
struct LexState *lexstate; /* point to local struct in yacc */
|
||||
struct ref *refArray; /* locked objects */
|
||||
int refSize; /* size of refArray */
|
||||
unsigned long GCthreshold;
|
||||
unsigned long nblocks; /* number of 'blocks' currently allocated */
|
||||
char *Mbuffer; /* global buffer */
|
||||
char *Mbuffbase; /* current first position of Mbuffer */
|
||||
int Mbuffsize; /* size of Mbuffer */
|
||||
int Mbuffnext; /* next position to fill in Mbuffer */
|
||||
struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
|
||||
int numCblocks; /* number of nested Cblocks */
|
||||
} LState;
|
||||
|
||||
|
||||
extern LState *lua_state;
|
||||
|
||||
|
||||
#define L lua_state
|
||||
#define G(L) (L->G)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
284
lstring.c
284
lstring.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 1.9 1997/12/30 19:15:52 roberto Exp roberto $
|
||||
** $Id: lstring.c,v 1.64 2001/06/07 15:01:21 roberto Exp roberto $
|
||||
** String table (keeps all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,256 +7,92 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define NUM_HASHS 61
|
||||
|
||||
|
||||
#define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */
|
||||
|
||||
|
||||
|
||||
static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}};
|
||||
|
||||
|
||||
void luaS_init (void)
|
||||
{
|
||||
int i;
|
||||
L->string_root = luaM_newvector(NUM_HASHS, stringtable);
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
L->string_root[i].size = 0;
|
||||
L->string_root[i].nuse = 0;
|
||||
L->string_root[i].hash = NULL;
|
||||
}
|
||||
void luaS_freeall (lua_State *L) {
|
||||
lua_assert(G(L)->strt.nuse==0);
|
||||
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
|
||||
}
|
||||
|
||||
|
||||
static unsigned long hash (char *s, int tag)
|
||||
{
|
||||
unsigned long h;
|
||||
if (tag != LUA_T_STRING)
|
||||
h = (unsigned long)s;
|
||||
else {
|
||||
h = 0;
|
||||
while (*s)
|
||||
h = ((h<<5)-h)^(unsigned char)*(s++);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
static void grow (stringtable *tb)
|
||||
{
|
||||
int newsize = luaO_redimension(tb->size);
|
||||
TaggedString **newhash = luaM_newvector(newsize, TaggedString *);
|
||||
void luaS_resize (lua_State *L, int newsize) {
|
||||
TString **newhash = luaM_newvector(L, newsize, TString *);
|
||||
stringtable *tb = &G(L)->strt;
|
||||
int i;
|
||||
for (i=0; i<newsize; i++)
|
||||
newhash[i] = NULL;
|
||||
for (i=0; i<newsize; i++) newhash[i] = NULL;
|
||||
/* rehash */
|
||||
tb->nuse = 0;
|
||||
for (i=0; i<tb->size; i++) {
|
||||
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
|
||||
int h = tb->hash[i]->hash%newsize;
|
||||
while (newhash[h])
|
||||
h = (h+1)%newsize;
|
||||
newhash[h] = tb->hash[i];
|
||||
tb->nuse++;
|
||||
TString *p = tb->hash[i];
|
||||
while (p) { /* for each node in the list */
|
||||
TString *next = p->tsv.nexthash; /* save next */
|
||||
lu_hash h = p->tsv.hash;
|
||||
int h1 = lmod(h, newsize); /* new position */
|
||||
lua_assert((int)(h%newsize) == lmod(h, newsize));
|
||||
p->tsv.nexthash = newhash[h1]; /* chain it in new position */
|
||||
newhash[h1] = p;
|
||||
p = next;
|
||||
}
|
||||
}
|
||||
luaM_free(tb->hash);
|
||||
luaM_freearray(L, tb->hash, tb->size, TString *);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static TaggedString *newone (char *buff, int tag, unsigned long h)
|
||||
{
|
||||
TaggedString *ts;
|
||||
if (tag == LUA_T_STRING) {
|
||||
long l = strlen(buff);
|
||||
ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
|
||||
strcpy(ts->str, buff);
|
||||
ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */
|
||||
ts->constindex = 0;
|
||||
L->nblocks += gcsizestring(l);
|
||||
}
|
||||
else {
|
||||
ts = (TaggedString *)luaM_malloc(sizeof(TaggedString));
|
||||
ts->u.d.v = buff;
|
||||
ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag;
|
||||
ts->constindex = -1; /* tag -> this is a userdata */
|
||||
L->nblocks++;
|
||||
}
|
||||
ts->head.marked = 0;
|
||||
ts->head.next = (GCnode *)ts; /* signal it is in no list */
|
||||
ts->hash = h;
|
||||
static TString *newlstr (lua_State *L, const l_char *str, size_t l, lu_hash h) {
|
||||
TString *ts = (TString *)luaM_malloc(L, sizestring(l));
|
||||
stringtable *tb;
|
||||
ts->tsv.nexthash = NULL;
|
||||
ts->tsv.len = l;
|
||||
ts->tsv.hash = h;
|
||||
ts->tsv.marked = 0;
|
||||
ts->tsv.constindex = 0;
|
||||
memcpy(getstr(ts), str, l*sizeof(l_char));
|
||||
getstr(ts)[l] = l_c('\0'); /* ending 0 */
|
||||
tb = &G(L)->strt;
|
||||
h = lmod(h, tb->size);
|
||||
ts->tsv.nexthash = tb->hash[h]; /* chain new entry */
|
||||
tb->hash[h] = ts;
|
||||
tb->nuse++;
|
||||
if (tb->nuse > (ls_nstr)tb->size && tb->size <= MAX_INT/2)
|
||||
luaS_resize(L, tb->size*2); /* too crowded */
|
||||
return ts;
|
||||
}
|
||||
|
||||
static TaggedString *insert (char *buff, int tag, stringtable *tb)
|
||||
{
|
||||
TaggedString *ts;
|
||||
unsigned long h = hash(buff, tag);
|
||||
int size = tb->size;
|
||||
int i;
|
||||
int j = -1;
|
||||
if ((long)tb->nuse*3 >= (long)size*2) {
|
||||
grow(tb);
|
||||
size = tb->size;
|
||||
}
|
||||
for (i = h%size; (ts = tb->hash[i]) != NULL; ) {
|
||||
if (ts == &EMPTY)
|
||||
j = i;
|
||||
else if ((ts->constindex >= 0) ? /* is a string? */
|
||||
(tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
|
||||
((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v))
|
||||
|
||||
TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) {
|
||||
TString *ts;
|
||||
lu_hash h = l; /* seed */
|
||||
size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
|
||||
size_t l1;
|
||||
for (l1=l; l1>=step; l1-=step) /* compute hash */
|
||||
h = h ^ ((h<<5)+(h>>2)+uchar(str[l1-1]));
|
||||
for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
|
||||
ts != NULL;
|
||||
ts = ts->tsv.nexthash) {
|
||||
if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
|
||||
return ts;
|
||||
if (++i == size) i=0;
|
||||
}
|
||||
/* not found */
|
||||
if (j != -1) /* is there an EMPTY space? */
|
||||
i = j;
|
||||
else
|
||||
tb->nuse++;
|
||||
ts = tb->hash[i] = newone(buff, tag, h);
|
||||
return ts;
|
||||
}
|
||||
|
||||
TaggedString *luaS_createudata (void *udata, int tag)
|
||||
{
|
||||
return insert(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
|
||||
}
|
||||
|
||||
TaggedString *luaS_new (char *str)
|
||||
{
|
||||
return insert(str, LUA_T_STRING, &L->string_root[(unsigned)str[0]%NUM_HASHS]);
|
||||
}
|
||||
|
||||
TaggedString *luaS_newfixedstring (char *str)
|
||||
{
|
||||
TaggedString *ts = luaS_new(str);
|
||||
if (ts->head.marked == 0)
|
||||
ts->head.marked = 2; /* avoid GC */
|
||||
return ts;
|
||||
return newlstr(L, str, l, h); /* not found */
|
||||
}
|
||||
|
||||
|
||||
void luaS_free (TaggedString *l)
|
||||
{
|
||||
while (l) {
|
||||
TaggedString *next = (TaggedString *)l->head.next;
|
||||
L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str));
|
||||
luaM_free(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage collection functions.
|
||||
*/
|
||||
|
||||
static void remove_from_list (GCnode *l)
|
||||
{
|
||||
while (l) {
|
||||
GCnode *next = l->next;
|
||||
while (next && !next->marked)
|
||||
next = l->next = next->next;
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TaggedString *luaS_collector (void)
|
||||
{
|
||||
TaggedString *frees = NULL;
|
||||
int i;
|
||||
remove_from_list(&(L->rootglobal));
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t == NULL) continue;
|
||||
if (t->head.marked == 1)
|
||||
t->head.marked = 0;
|
||||
else if (!t->head.marked) {
|
||||
t->head.next = (GCnode *)frees;
|
||||
frees = t;
|
||||
tb->hash[j] = &EMPTY;
|
||||
}
|
||||
}
|
||||
}
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
TaggedString *luaS_collectudata (void)
|
||||
{
|
||||
TaggedString *frees = NULL;
|
||||
int i;
|
||||
L->rootglobal.next = NULL; /* empty list of globals */
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t == NULL || t == &EMPTY || t->constindex != -1)
|
||||
continue; /* get only user datas */
|
||||
t->head.next = (GCnode *)frees;
|
||||
frees = t;
|
||||
tb->hash[j] = &EMPTY;
|
||||
}
|
||||
}
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
void luaS_freeall (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t == &EMPTY) continue;
|
||||
luaM_free(t);
|
||||
}
|
||||
luaM_free(tb->hash);
|
||||
}
|
||||
luaM_free(L->string_root);
|
||||
}
|
||||
|
||||
|
||||
void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
|
||||
{
|
||||
ts->u.globalval = *newval;
|
||||
if (ts->head.next == (GCnode *)ts) { /* is not in list? */
|
||||
ts->head.next = L->rootglobal.next;
|
||||
L->rootglobal.next = (GCnode *)ts;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char *luaS_travsymbol (int (*fn)(TObject *))
|
||||
{
|
||||
TaggedString *g;
|
||||
for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
|
||||
if (fn(&g->u.globalval))
|
||||
return g->str;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int luaS_globaldefined (char *name)
|
||||
{
|
||||
TaggedString *ts = luaS_new(name);
|
||||
return ts->u.globalval.ttype != LUA_T_NIL;
|
||||
Udata *luaS_newudata (lua_State *L, size_t s) {
|
||||
Udata *u = (Udata *)luaM_malloc(L, sizeudata(s));
|
||||
u->uv.len = s;
|
||||
u->uv.tag = 0;
|
||||
u->uv.value = u + 1;
|
||||
/* chain it on udata list */
|
||||
u->uv.next = G(L)->rootudata;
|
||||
G(L)->rootudata = u;
|
||||
return u;
|
||||
}
|
||||
|
||||
|
||||
36
lstring.h
36
lstring.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.5 1997/11/26 18:53:45 roberto Exp roberto $
|
||||
** $Id: lstring.h,v 1.32 2001/06/06 18:00:19 roberto Exp roberto $
|
||||
** String table (keep all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,19 +9,31 @@
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
void luaS_init (void);
|
||||
TaggedString *luaS_createudata (void *udata, int tag);
|
||||
TaggedString *luaS_collector (void);
|
||||
void luaS_free (TaggedString *l);
|
||||
TaggedString *luaS_new (char *str);
|
||||
TaggedString *luaS_newfixedstring (char *str);
|
||||
void luaS_rawsetglobal (TaggedString *ts, TObject *newval);
|
||||
char *luaS_travsymbol (int (*fn)(TObject *));
|
||||
int luaS_globaldefined (char *name);
|
||||
TaggedString *luaS_collectudata (void);
|
||||
void luaS_freeall (void);
|
||||
|
||||
/*
|
||||
** any TString with mark>=FIXMARK is never collected.
|
||||
** Marks>=RESERVEDMARK are used to identify reserved words.
|
||||
*/
|
||||
#define FIXMARK 2
|
||||
#define RESERVEDMARK 3
|
||||
|
||||
|
||||
#define sizestring(l) ((lu_mem)sizeof(union TString)+ \
|
||||
((lu_mem)(l)+1)*sizeof(l_char))
|
||||
|
||||
#define sizeudata(l) ((lu_mem)sizeof(union Udata)+(l))
|
||||
|
||||
#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s)))
|
||||
#define luaS_newliteral(L, s) (luaS_newlstr(L, l_s("") s, \
|
||||
(sizeof(s)/sizeof(l_char))-1))
|
||||
|
||||
void luaS_resize (lua_State *L, int newsize);
|
||||
Udata *luaS_newudata (lua_State *L, size_t s);
|
||||
void luaS_freeall (lua_State *L);
|
||||
TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
418
ltable.c
418
ltable.c
@@ -1,216 +1,302 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 1.10 1998/01/09 14:44:55 roberto Exp roberto $
|
||||
** $Id: ltable.c,v 1.82 2001/06/26 13:20:45 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
/*
|
||||
** Implementation of tables (aka arrays, objects, or hash tables);
|
||||
** uses a mix of chained scatter table with Brent's variation.
|
||||
** A main invariant of these tables is that, if an element is not
|
||||
** in its main position (i.e. the `original' position that its hash gives
|
||||
** to it), then the colliding element is in its own main position.
|
||||
** In other words, there are collisions only when two elements have the
|
||||
** same main position (i.e. the same hash values for that table size).
|
||||
** Because of that, the load factor of these tables can be 100% without
|
||||
** performance penalties.
|
||||
*/
|
||||
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "ltable.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define gcsize(n) (1+(n/16))
|
||||
|
||||
#define nuse(t) ((t)->nuse)
|
||||
#define nodevector(t) ((t)->node)
|
||||
|
||||
|
||||
#define REHASH_LIMIT 0.70 /* avoid more than this % full */
|
||||
|
||||
#define TagDefault LUA_T_ARRAY;
|
||||
|
||||
|
||||
|
||||
static long int hashindex (TObject *ref)
|
||||
{
|
||||
long int h;
|
||||
switch (ttype(ref)) {
|
||||
case LUA_T_NUMBER:
|
||||
h = (long int)nvalue(ref);
|
||||
break;
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
h = (IntPoint)tsvalue(ref);
|
||||
break;
|
||||
case LUA_T_ARRAY:
|
||||
h = (IntPoint)avalue(ref);
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
h = (IntPoint)tfvalue(ref);
|
||||
break;
|
||||
case LUA_T_CPROTO:
|
||||
h = (IntPoint)fvalue(ref);
|
||||
break;
|
||||
case LUA_T_CLOSURE:
|
||||
h = (IntPoint)clvalue(ref);
|
||||
break;
|
||||
default:
|
||||
lua_error("unexpected type to index table");
|
||||
h = 0; /* to avoid warnings */
|
||||
}
|
||||
return (h >= 0 ? h : -(h+1));
|
||||
}
|
||||
#define TagDefault LUA_TTABLE
|
||||
|
||||
|
||||
static int present (Hash *t, TObject *key)
|
||||
{
|
||||
int tsize = nhash(t);
|
||||
long int h = hashindex(key);
|
||||
int h1 = h%tsize;
|
||||
TObject *rf = ref(node(t, h1));
|
||||
if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {
|
||||
int h2 = h%(tsize-2) + 1;
|
||||
do {
|
||||
h1 += h2;
|
||||
if (h1 >= tsize) h1 -= tsize;
|
||||
rf = ref(node(t, h1));
|
||||
} while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf));
|
||||
}
|
||||
return h1;
|
||||
}
|
||||
#define hashnum(t,n) (node(t, lmod((lu_hash)(ls_hash)(n), t->size)))
|
||||
#define hashstr(t,str) (node(t, lmod((str)->tsv.hash, t->size)))
|
||||
#define hashpointer(t,p) (node(t, lmod(IntPoint(p), t->size)))
|
||||
|
||||
|
||||
/*
|
||||
** Alloc a vector node
|
||||
** returns the `main' position of an element in a table (that is, the index
|
||||
** of its hash value)
|
||||
*/
|
||||
static Node *hashnodecreate (int nhash)
|
||||
{
|
||||
Node *v = luaM_newvector(nhash, Node);
|
||||
Node *luaH_mainposition (const Hash *t, const TObject *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TNUMBER:
|
||||
return hashnum(t, nvalue(key));
|
||||
case LUA_TSTRING:
|
||||
return hashstr(t, tsvalue(key));
|
||||
default: /* all other types are hashed as (void *) */
|
||||
return hashpointer(t, tsvalue(key));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Node *luaH_next (lua_State *L, Hash *t, const TObject *key) {
|
||||
int i;
|
||||
for (i=0; i<nhash; i++)
|
||||
ttype(ref(&v[i])) = LUA_T_NIL;
|
||||
return v;
|
||||
if (ttype(key) == LUA_TNIL)
|
||||
i = 0; /* first iteration */
|
||||
else {
|
||||
const TObject *v = luaH_get(t, key);
|
||||
if (v == &luaO_nilobject)
|
||||
luaD_error(L, l_s("invalid key for `next'"));
|
||||
i = (int)(((const lu_byte *)v -
|
||||
(const lu_byte *)(val(node(t, 0)))) / sizeof(Node)) + 1;
|
||||
}
|
||||
for (; i<t->size; i++) {
|
||||
Node *n = node(t, i);
|
||||
if (ttype(val(n)) != LUA_TNIL)
|
||||
return n;
|
||||
}
|
||||
return NULL; /* no more elements */
|
||||
}
|
||||
|
||||
|
||||
int luaH_nexti (Hash *t, int i) {
|
||||
while ((++i)<t->size) {
|
||||
if (ttype(val(node(t, i))) != LUA_TNIL) /* a non-nil value? */
|
||||
return i;
|
||||
}
|
||||
return -1; /* no more elements */
|
||||
}
|
||||
|
||||
|
||||
#define check_grow(L, p, n) \
|
||||
if ((p) >= MAX_INT/(n)) luaD_error(L, l_s("table overflow"));
|
||||
|
||||
/*
|
||||
** Delete a hash
|
||||
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
||||
*/
|
||||
static void hashdelete (Hash *t)
|
||||
{
|
||||
luaM_free(nodevector(t));
|
||||
luaM_free(t);
|
||||
}
|
||||
|
||||
|
||||
void luaH_free (Hash *frees)
|
||||
{
|
||||
while (frees) {
|
||||
Hash *next = (Hash *)frees->head.next;
|
||||
L->nblocks -= gcsize(frees->nhash);
|
||||
hashdelete(frees);
|
||||
frees = next;
|
||||
static int power2 (lua_State *L, int n) {
|
||||
int p = MINPOWER2;
|
||||
while (p <= n) {
|
||||
check_grow(L, p, 2);
|
||||
p *= 2;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
Hash *luaH_new (int nhash)
|
||||
{
|
||||
Hash *t = luaM_new(Hash);
|
||||
nhash = luaO_redimension((int)((float)nhash/REHASH_LIMIT));
|
||||
nodevector(t) = hashnodecreate(nhash);
|
||||
nhash(t) = nhash;
|
||||
nuse(t) = 0;
|
||||
static void setnodevector (lua_State *L, Hash *t, int size) {
|
||||
int i;
|
||||
t->node = luaM_newvector(L, size, Node);
|
||||
for (i=0; i<size; i++) {
|
||||
t->node[i].next = NULL;
|
||||
setnilvalue(key(node(t, i)));
|
||||
setnilvalue(val(node(t, i)));
|
||||
}
|
||||
t->size = size;
|
||||
t->firstfree = node(t, size-1); /* first free position to be used */
|
||||
}
|
||||
|
||||
|
||||
Hash *luaH_new (lua_State *L, int size) {
|
||||
Hash *t = luaM_new(L, Hash);
|
||||
t->htag = TagDefault;
|
||||
luaO_insertlist(&(L->roottable), (GCnode *)t);
|
||||
L->nblocks += gcsize(nhash);
|
||||
t->next = G(L)->roottable;
|
||||
G(L)->roottable = t;
|
||||
t->mark = t;
|
||||
t->size = 0;
|
||||
t->weakmode = 0;
|
||||
t->node = NULL;
|
||||
setnodevector(L, t, power2(L, size));
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Rehash:
|
||||
** Check if table has deleted slots. It it has, it does not need to
|
||||
** grow, since rehash will reuse them.
|
||||
*/
|
||||
static int emptyslots (Hash *t)
|
||||
{
|
||||
void luaH_free (lua_State *L, Hash *t) {
|
||||
luaM_freearray(L, t->node, t->size, Node);
|
||||
luaM_freelem(L, t, Hash);
|
||||
}
|
||||
|
||||
|
||||
static int numuse (const Hash *t) {
|
||||
Node *v = t->node;
|
||||
int size = t->size;
|
||||
int realuse = 0;
|
||||
int i;
|
||||
for (i=nhash(t)-1; i>=0; i--) {
|
||||
Node *n = node(t, i);
|
||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL)
|
||||
return 1;
|
||||
for (i=0; i<size; i++) {
|
||||
if (ttype(&v[i].val) != LUA_TNIL)
|
||||
realuse++;
|
||||
}
|
||||
return 0;
|
||||
return realuse;
|
||||
}
|
||||
|
||||
static void rehash (Hash *t)
|
||||
{
|
||||
int nold = nhash(t);
|
||||
Node *vold = nodevector(t);
|
||||
|
||||
static void rehash (lua_State *L, Hash *t) {
|
||||
int oldsize = t->size;
|
||||
Node *nold = t->node;
|
||||
int nelems = numuse(t);
|
||||
int i;
|
||||
if (!emptyslots(t))
|
||||
nhash(t) = luaO_redimension(nhash(t));
|
||||
nodevector(t) = hashnodecreate(nhash(t));
|
||||
for (i=0; i<nold; i++) {
|
||||
Node *n = vold+i;
|
||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
|
||||
*node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
|
||||
lua_assert(nelems<=oldsize);
|
||||
if (nelems >= oldsize-oldsize/4) { /* using more than 3/4? */
|
||||
check_grow(L, oldsize, 2);
|
||||
setnodevector(L, t, oldsize*2); /* grow array */
|
||||
}
|
||||
L->nblocks += gcsize(t->nhash)-gcsize(nold);
|
||||
luaM_free(vold);
|
||||
}
|
||||
|
||||
/*
|
||||
** If the hash node is present, return its pointer, otherwise return
|
||||
** null.
|
||||
*/
|
||||
TObject *luaH_get (Hash *t, TObject *ref)
|
||||
{
|
||||
int h = present(t, ref);
|
||||
if (ttype(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
|
||||
else return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If the hash node is present, return its pointer, otherwise create a luaM_new
|
||||
** node for the given reference and also return its pointer.
|
||||
*/
|
||||
TObject *luaH_set (Hash *t, TObject *ref)
|
||||
{
|
||||
Node *n = node(t, present(t, ref));
|
||||
if (ttype(ref(n)) == LUA_T_NIL) {
|
||||
nuse(t)++;
|
||||
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
|
||||
rehash(t);
|
||||
n = node(t, present(t, ref));
|
||||
else if (nelems <= oldsize/4 && /* less than 1/4? */
|
||||
oldsize > MINPOWER2)
|
||||
setnodevector(L, t, oldsize/2); /* shrink array */
|
||||
else
|
||||
setnodevector(L, t, oldsize); /* just rehash; keep the same size */
|
||||
for (i=0; i<oldsize; i++) {
|
||||
Node *old = nold+i;
|
||||
if (ttype(val(old)) != LUA_TNIL) {
|
||||
TObject *v = luaH_set(L, t, key(old));
|
||||
setobj(v, val(old));
|
||||
}
|
||||
*ref(n) = *ref;
|
||||
ttype(val(n)) = LUA_T_NIL;
|
||||
}
|
||||
return (val(n));
|
||||
luaM_freearray(L, nold, oldsize, Node); /* free old array */
|
||||
}
|
||||
|
||||
|
||||
static Node *hashnext (Hash *t, int i)
|
||||
{
|
||||
Node *n;
|
||||
int tsize = nhash(t);
|
||||
if (i >= tsize)
|
||||
return NULL;
|
||||
n = node(t, i);
|
||||
while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
|
||||
if (++i >= tsize)
|
||||
return NULL;
|
||||
n = node(t, i);
|
||||
/*
|
||||
** inserts a new key into a hash table; first, check whether key's main
|
||||
** position is free. If not, check whether colliding node is in its main
|
||||
** position or not: if it is not, move colliding node to an empty place and
|
||||
** put new key in its main position; otherwise (colliding node is in its main
|
||||
** position), new key goes to an empty position.
|
||||
*/
|
||||
static TObject *newkey (lua_State *L, Hash *t, const TObject *key) {
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */
|
||||
Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */
|
||||
Node *n = t->firstfree; /* get a free place */
|
||||
if (othern != mp) { /* is colliding node out of its main position? */
|
||||
/* yes; move colliding node into free position */
|
||||
while (othern->next != mp) othern = othern->next; /* find previous */
|
||||
othern->next = n; /* redo the chain with `n' in place of `mp' */
|
||||
*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
|
||||
mp->next = NULL; /* now `mp' is free */
|
||||
setnilvalue(val(mp));
|
||||
}
|
||||
else { /* colliding node is in its own main position */
|
||||
/* new node will go into free position */
|
||||
n->next = mp->next; /* chain new position */
|
||||
mp->next = n;
|
||||
mp = n;
|
||||
}
|
||||
}
|
||||
return node(t, i);
|
||||
setobj(key(mp), key);
|
||||
lua_assert(ttype(val(mp)) == LUA_TNIL);
|
||||
for (;;) { /* correct `firstfree' */
|
||||
if (ttype(key(t->firstfree)) == LUA_TNIL)
|
||||
return val(mp); /* OK; table still has a free place */
|
||||
else if (t->firstfree == t->node) break; /* cannot decrement from here */
|
||||
else (t->firstfree)--;
|
||||
}
|
||||
rehash(L, t); /* no more free places */
|
||||
return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
|
||||
}
|
||||
|
||||
Node *luaH_next (TObject *o, TObject *r)
|
||||
{
|
||||
Hash *t = avalue(o);
|
||||
if (ttype(r) == LUA_T_NIL)
|
||||
return hashnext(t, 0);
|
||||
|
||||
/*
|
||||
** generic search function
|
||||
*/
|
||||
static const TObject *luaH_getany (Hash *t, const TObject *key) {
|
||||
if (ttype(key) == LUA_TNIL) return &luaO_nilobject;
|
||||
else {
|
||||
int i = present(t, r);
|
||||
Node *n = node(t, i);
|
||||
luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
|
||||
2, "key not found");
|
||||
return hashnext(t, i+1);
|
||||
Node *n = luaH_mainposition(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_equalObj(key(n), key)) return val(n); /* that's it */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** search function for integers
|
||||
*/
|
||||
const TObject *luaH_getnum (Hash *t, int key) {
|
||||
Node *n = hashnum(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttype(key(n)) == LUA_TNUMBER && nvalue(key(n)) == (lua_Number)key)
|
||||
return val(n); /* that's it */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** search function for strings
|
||||
*/
|
||||
const TObject *luaH_getstr (Hash *t, TString *key) {
|
||||
Node *n = hashstr(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttype(key(n)) == LUA_TSTRING && tsvalue(key(n)) == key)
|
||||
return val(n); /* that's it */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** main search function
|
||||
*/
|
||||
const TObject *luaH_get (Hash *t, const TObject *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
|
||||
case LUA_TNUMBER: {
|
||||
int k = (int)nvalue(key);
|
||||
if ((lua_Number)k == nvalue(key)) /* is an integer index? */
|
||||
return luaH_getnum(t, k); /* use specialized version */
|
||||
/* else go through */
|
||||
}
|
||||
default: return luaH_getany(t, key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
||||
const TObject *p = luaH_get(t, key);
|
||||
if (p != &luaO_nilobject) return (TObject *)p;
|
||||
else if (ttype(key) == LUA_TNIL) luaD_error(L, l_s("table index is nil"));
|
||||
return newkey(L, t, key);
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
|
||||
const TObject *p = luaH_getstr(t, key);
|
||||
if (p != &luaO_nilobject) return (TObject *)p;
|
||||
else {
|
||||
TObject k;
|
||||
setsvalue(&k, key);
|
||||
return newkey(L, t, &k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_setnum (lua_State *L, Hash *t, int key) {
|
||||
const TObject *p = luaH_getnum(t, key);
|
||||
if (p != &luaO_nilobject) return (TObject *)p;
|
||||
else {
|
||||
TObject k;
|
||||
setnvalue(&k, key);
|
||||
return newkey(L, t, &k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
28
ltable.h
28
ltable.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 1.4 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** $Id: ltable.h,v 1.33 2001/06/26 13:20:45 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,15 +10,23 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define node(t,i) (&(t)->node[i])
|
||||
#define ref(n) (&(n)->ref)
|
||||
#define val(n) (&(n)->val)
|
||||
#define nhash(t) ((t)->nhash)
|
||||
#define node(_t,_i) (&(_t)->node[_i])
|
||||
#define key(_n) (&(_n)->key)
|
||||
#define val(_n) (&(_n)->val)
|
||||
|
||||
const TObject *luaH_getnum (Hash *t, int key);
|
||||
TObject *luaH_setnum (lua_State *L, Hash *t, int key);
|
||||
const TObject *luaH_getstr (Hash *t, TString *key);
|
||||
TObject *luaH_setstr (lua_State *L, Hash *t, TString *key);
|
||||
const TObject *luaH_get (Hash *t, const TObject *key);
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key);
|
||||
Hash *luaH_new (lua_State *L, int nhash);
|
||||
void luaH_free (lua_State *L, Hash *t);
|
||||
Node *luaH_next (lua_State *L, Hash *t, const TObject *r);
|
||||
int luaH_nexti (Hash *t, int i);
|
||||
|
||||
/* exported only for debugging */
|
||||
Node *luaH_mainposition (const Hash *t, const TObject *key);
|
||||
|
||||
Hash *luaH_new (int nhash);
|
||||
void luaH_free (Hash *frees);
|
||||
TObject *luaH_get (Hash *t, TObject *ref);
|
||||
TObject *luaH_set (Hash *t, TObject *ref);
|
||||
Node *luaH_next (TObject *o, TObject *r);
|
||||
|
||||
#endif
|
||||
|
||||
676
ltests.c
Normal file
676
ltests.c
Normal file
@@ -0,0 +1,676 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 1.87 2001/07/05 20:31:14 roberto Exp roberto $
|
||||
** Internal Module for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lcode.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** The whole module only makes sense with LUA_DEBUG on
|
||||
*/
|
||||
#ifdef LUA_DEBUG
|
||||
|
||||
|
||||
lua_State *lua_state = NULL;
|
||||
|
||||
int islocked = 0;
|
||||
|
||||
|
||||
|
||||
static void setnameval (lua_State *L, const l_char *name, int val) {
|
||||
lua_pushstring(L, name);
|
||||
lua_pushnumber(L, val);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================================
|
||||
** Controlled version for realloc.
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
|
||||
/* ensures maximum alignment for HEADER */
|
||||
#define HEADER (sizeof(union L_Umaxalign))
|
||||
|
||||
#define MARKSIZE 32
|
||||
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
||||
|
||||
|
||||
#define blocksize(b) ((size_t *)((l_char *)(b) - HEADER))
|
||||
|
||||
unsigned long memdebug_numblocks = 0;
|
||||
unsigned long memdebug_total = 0;
|
||||
unsigned long memdebug_maxmem = 0;
|
||||
unsigned long memdebug_memlimit = ULONG_MAX;
|
||||
|
||||
|
||||
static void *checkblock (void *block) {
|
||||
size_t *b = blocksize(block);
|
||||
size_t size = *b;
|
||||
int i;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
lua_assert(*(((l_char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
static void freeblock (void *block) {
|
||||
if (block) {
|
||||
size_t size = *blocksize(block);
|
||||
block = checkblock(block);
|
||||
memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
|
||||
free(block); /* free original block */
|
||||
memdebug_numblocks--;
|
||||
memdebug_total -= size;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void *debug_realloc (void *block, size_t oldsize, size_t size) {
|
||||
lua_assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
|
||||
if (size == 0) {
|
||||
freeblock(block);
|
||||
return NULL;
|
||||
}
|
||||
else if (memdebug_total+size-oldsize > memdebug_memlimit)
|
||||
return NULL; /* to test memory allocation errors */
|
||||
else {
|
||||
l_char *newblock;
|
||||
int i;
|
||||
size_t realsize = HEADER+size+MARKSIZE;
|
||||
if (realsize < size) return NULL; /* overflow! */
|
||||
newblock = (l_char *)malloc(realsize); /* alloc a new block */
|
||||
if (newblock == NULL) return NULL;
|
||||
if (oldsize > size) oldsize = size;
|
||||
if (block) {
|
||||
memcpy(newblock+HEADER, block, oldsize);
|
||||
freeblock(block); /* erase (and check) old copy */
|
||||
}
|
||||
/* initialize new part of the block with something `weird' */
|
||||
memset(newblock+HEADER+oldsize, -MARK, size-oldsize);
|
||||
memdebug_total += size;
|
||||
if (memdebug_total > memdebug_maxmem)
|
||||
memdebug_maxmem = memdebug_total;
|
||||
memdebug_numblocks++;
|
||||
*(size_t *)newblock = size;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
*(newblock+HEADER+size+i) = (l_char)(MARK+i);
|
||||
return newblock+HEADER;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Disassembler
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static l_char *buildop (Proto *p, int pc, l_char *buff) {
|
||||
Instruction i = p->code[pc];
|
||||
OpCode o = GET_OPCODE(i);
|
||||
const l_char *name = luaP_opnames[o];
|
||||
sprintf(buff, l_s("%4d - "), pc);
|
||||
switch (getOpMode(o)) {
|
||||
case iABC:
|
||||
sprintf(buff+strlen(buff), l_s("%-12s%4d %4d %4d"), name,
|
||||
GETARG_A(i), GETARG_B(i), GETARG_C(i));
|
||||
break;
|
||||
case iABc:
|
||||
sprintf(buff+strlen(buff), l_s("%-12s%4d %4d"), name, GETARG_A(i), GETARG_Bc(i));
|
||||
break;
|
||||
case iAsBc:
|
||||
sprintf(buff+strlen(buff), l_s("%-12s%4d %4d"), name, GETARG_A(i), GETARG_sBc(i));
|
||||
break;
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
||||
|
||||
static int listcode (lua_State *L) {
|
||||
int pc;
|
||||
Proto *p;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, l_s("Lua function expected"));
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
lua_newtable(L);
|
||||
setnameval(L, l_s("maxstack"), p->maxstacksize);
|
||||
setnameval(L, l_s("numparams"), p->numparams);
|
||||
for (pc=0; pc<p->sizecode; pc++) {
|
||||
l_char buff[100];
|
||||
lua_pushnumber(L, pc+1);
|
||||
lua_pushstring(L, buildop(p, pc, buff));
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int listk (lua_State *L) {
|
||||
Proto *p;
|
||||
int i;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, l_s("Lua function expected"));
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
lua_newtable(L);
|
||||
for (i=0; i<p->sizek; i++) {
|
||||
lua_pushnumber(L, i+1);
|
||||
luaA_pushobject(L, p->k+i);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int listlocals (lua_State *L) {
|
||||
Proto *p;
|
||||
int pc = luaL_check_int(L, 2) - 1;
|
||||
int i = 0;
|
||||
const l_char *name;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, l_s("Lua function expected"));
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
||||
lua_pushstring(L, name);
|
||||
return i-1;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static int pushbool (lua_State *L, int b) {
|
||||
if (b) lua_pushnumber(L, 1);
|
||||
else lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int get_limits (lua_State *L) {
|
||||
lua_newtable(L);
|
||||
setnameval(L, l_s("BITS_INT"), BITS_INT);
|
||||
setnameval(L, l_s("LFPF"), LFIELDS_PER_FLUSH);
|
||||
setnameval(L, l_s("MAXLOCALS"), MAXLOCALS);
|
||||
setnameval(L, l_s("MAXPARAMS"), MAXPARAMS);
|
||||
setnameval(L, l_s("MAXSTACK"), MAXSTACK);
|
||||
setnameval(L, l_s("MAXUPVALUES"), MAXUPVALUES);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int mem_query (lua_State *L) {
|
||||
if (lua_isnull(L, 1)) {
|
||||
lua_pushnumber(L, memdebug_total);
|
||||
lua_pushnumber(L, memdebug_numblocks);
|
||||
lua_pushnumber(L, memdebug_maxmem);
|
||||
return 3;
|
||||
}
|
||||
else {
|
||||
memdebug_memlimit = luaL_check_int(L, 1);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int hash_query (lua_State *L) {
|
||||
if (lua_isnull(L, 2)) {
|
||||
luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, l_s("string expected"));
|
||||
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->tsv.hash);
|
||||
}
|
||||
else {
|
||||
TObject *o = luaA_index(L, 1);
|
||||
Hash *t;
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
t = hvalue(luaA_index(L, 2));
|
||||
lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int table_query (lua_State *L) {
|
||||
const Hash *t;
|
||||
int i = luaL_opt_int(L, 2, -1);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
t = hvalue(luaA_index(L, 1));
|
||||
if (i == -1) {
|
||||
lua_pushnumber(L, t->size);
|
||||
lua_pushnumber(L, t->firstfree - t->node);
|
||||
return 2;
|
||||
}
|
||||
else if (i < t->size) {
|
||||
if (ttype(val(node(t, i))) != LUA_TNIL ||
|
||||
ttype(key(node(t, i))) == LUA_TNIL ||
|
||||
ttype(key(node(t, i))) == LUA_TNUMBER) {
|
||||
luaA_pushobject(L, key(node(t, i)));
|
||||
}
|
||||
else
|
||||
lua_pushstring(L, "<undef>");
|
||||
luaA_pushobject(L, &t->node[i].val);
|
||||
if (t->node[i].next) {
|
||||
lua_pushnumber(L, t->node[i].next - t->node);
|
||||
return 3;
|
||||
}
|
||||
else
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int string_query (lua_State *L) {
|
||||
stringtable *tb = &G(L)->strt;
|
||||
int s = luaL_opt_int(L, 2, 0) - 1;
|
||||
if (s==-1) {
|
||||
lua_pushnumber(L ,tb->nuse);
|
||||
lua_pushnumber(L ,tb->size);
|
||||
return 2;
|
||||
}
|
||||
else if (s < tb->size) {
|
||||
TString *ts;
|
||||
int n = 0;
|
||||
for (ts = tb->hash[s]; ts; ts = ts->tsv.nexthash) {
|
||||
setsvalue(L->top, ts);
|
||||
incr_top;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int tref (lua_State *L) {
|
||||
int level = lua_gettop(L);
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
|
||||
assert(lua_gettop(L) == level+1); /* +1 for result */
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getref (lua_State *L) {
|
||||
int level = lua_gettop(L);
|
||||
if (lua_getref(L, luaL_check_int(L, 1))) {
|
||||
assert(lua_gettop(L) == level+1);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
assert(lua_gettop(L) == level);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int unref (lua_State *L) {
|
||||
int level = lua_gettop(L);
|
||||
lua_unref(L, luaL_check_int(L, 1));
|
||||
assert(lua_gettop(L) == level);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int newuserdata (lua_State *L) {
|
||||
size_t size = luaL_check_int(L, 1);
|
||||
l_char *p = (l_char *)lua_newuserdata(L, size);
|
||||
while (size--) *p++ = l_c('\0');
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int newuserdatabox (lua_State *L) {
|
||||
lua_newuserdatabox(L, (void *)luaL_check_int(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int settag (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushvalue(L, 1); /* push value */
|
||||
lua_settag(L, luaL_check_int(L, 2));
|
||||
return 1; /* return value */
|
||||
}
|
||||
|
||||
static int udataval (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
lua_pushnumber(L, (int)lua_touserdata(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int newtag (lua_State *L) {
|
||||
lua_pushnumber(L, lua_newtype(L, lua_tostring(L, 1),
|
||||
(int)lua_tonumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int doonnewstack (lua_State *L) {
|
||||
lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1));
|
||||
if (L1 == NULL) return 0;
|
||||
*((int **)L1) = &islocked; /* initialize the lock */
|
||||
lua_dostring(L1, luaL_check_string(L, 2));
|
||||
lua_pushnumber(L, 1);
|
||||
lua_close(L1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int s2d (lua_State *L) {
|
||||
lua_pushnumber(L, *(double *)luaL_check_string(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int d2s (lua_State *L) {
|
||||
double d = luaL_check_number(L, 1);
|
||||
lua_pushlstring(L, (l_char *)&d, sizeof(d));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int newstate (lua_State *L) {
|
||||
lua_State *L1 = lua_open(luaL_check_int(L, 1));
|
||||
if (L1) {
|
||||
*((int **)L1) = &islocked; /* initialize the lock */
|
||||
lua_pushnumber(L, (unsigned long)L1);
|
||||
}
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int loadlib (lua_State *L) {
|
||||
lua_State *L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
|
||||
lua_register(L1, "mathlibopen", lua_mathlibopen);
|
||||
lua_register(L1, "strlibopen", lua_strlibopen);
|
||||
lua_register(L1, "iolibopen", lua_iolibopen);
|
||||
lua_register(L1, "dblibopen", lua_dblibopen);
|
||||
lua_register(L1, "baselibopen", lua_baselibopen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int closestate (lua_State *L) {
|
||||
lua_State *L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
|
||||
lua_close(L1);
|
||||
lua_unlock(L); /* close cannot unlock that */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int doremote (lua_State *L) {
|
||||
lua_State *L1;
|
||||
const l_char *code = luaL_check_string(L, 2);
|
||||
int status;
|
||||
L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
|
||||
status = lua_dostring(L1, code);
|
||||
if (status != 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushnumber(L, status);
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
int i = 0;
|
||||
while (!lua_isnull(L1, ++i))
|
||||
lua_pushstring(L, lua_tostring(L1, i));
|
||||
lua_pop(L1, i-1);
|
||||
return i-1;
|
||||
}
|
||||
}
|
||||
|
||||
static int settagmethod (lua_State *L) {
|
||||
int tag = luaL_check_int(L, 1);
|
||||
const l_char *event = luaL_check_string(L, 2);
|
||||
luaL_checkany(L, 3);
|
||||
lua_gettagmethod(L, tag, event);
|
||||
lua_pushvalue(L, 3);
|
||||
lua_settagmethod(L, tag, event);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int equal (lua_State *L) {
|
||||
return pushbool(L, lua_equal(L, 1, 2));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** function to test the API with C. It interprets a kind of assembler
|
||||
** language with calls to the API, so the test can be driven by Lua code
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static const l_char *const delimits = l_s(" \t\n,;");
|
||||
|
||||
static void skip (const l_char **pc) {
|
||||
while (**pc != l_c('\0') && strchr(delimits, **pc)) (*pc)++;
|
||||
}
|
||||
|
||||
static int getnum (lua_State *L, const l_char **pc) {
|
||||
int res = 0;
|
||||
int sig = 1;
|
||||
skip(pc);
|
||||
if (**pc == l_c('.')) {
|
||||
res = (int)lua_tonumber(L, -1);
|
||||
lua_pop(L, 1);
|
||||
(*pc)++;
|
||||
return res;
|
||||
}
|
||||
else if (**pc == l_c('-')) {
|
||||
sig = -1;
|
||||
(*pc)++;
|
||||
}
|
||||
while (isdigit(**pc)) res = res*10 + (*(*pc)++) - l_c('0');
|
||||
return sig*res;
|
||||
}
|
||||
|
||||
static const l_char *getname (l_char *buff, const l_char **pc) {
|
||||
int i = 0;
|
||||
skip(pc);
|
||||
while (**pc != l_c('\0') && !strchr(delimits, **pc))
|
||||
buff[i++] = *(*pc)++;
|
||||
buff[i] = l_c('\0');
|
||||
return buff;
|
||||
}
|
||||
|
||||
|
||||
#define EQ(s1) (strcmp(s1, inst) == 0)
|
||||
|
||||
#define getnum ((getnum)(L, &pc))
|
||||
#define getname ((getname)(buff, &pc))
|
||||
|
||||
|
||||
static int testC (lua_State *L) {
|
||||
l_char buff[30];
|
||||
const l_char *pc = luaL_check_string(L, 1);
|
||||
for (;;) {
|
||||
const l_char *inst = getname;
|
||||
if EQ(l_s("")) return 0;
|
||||
else if EQ(l_s("isnumber")) {
|
||||
lua_pushnumber(L, lua_isnumber(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isstring")) {
|
||||
lua_pushnumber(L, lua_isstring(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("istable")) {
|
||||
lua_pushnumber(L, lua_istable(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("iscfunction")) {
|
||||
lua_pushnumber(L, lua_iscfunction(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isfunction")) {
|
||||
lua_pushnumber(L, lua_isfunction(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isuserdata")) {
|
||||
lua_pushnumber(L, lua_isuserdata(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isnil")) {
|
||||
lua_pushnumber(L, lua_isnil(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isnull")) {
|
||||
lua_pushnumber(L, lua_isnull(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("tonumber")) {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("tostring")) {
|
||||
const l_char *s = lua_tostring(L, getnum);
|
||||
lua_pushstring(L, s);
|
||||
}
|
||||
else if EQ(l_s("tonumber")) {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("strlen")) {
|
||||
lua_pushnumber(L, lua_strlen(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("tocfunction")) {
|
||||
lua_pushcfunction(L, lua_tocfunction(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("return")) {
|
||||
return getnum;
|
||||
}
|
||||
else if EQ(l_s("gettop")) {
|
||||
lua_pushnumber(L, lua_gettop(L));
|
||||
}
|
||||
else if EQ(l_s("settop")) {
|
||||
lua_settop(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("pop")) {
|
||||
lua_pop(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("pushnum")) {
|
||||
lua_pushnumber(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("pushvalue")) {
|
||||
lua_pushvalue(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("remove")) {
|
||||
lua_remove(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("insert")) {
|
||||
lua_insert(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("gettable")) {
|
||||
lua_gettable(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("settable")) {
|
||||
lua_settable(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("next")) {
|
||||
lua_next(L, -2);
|
||||
}
|
||||
else if EQ(l_s("concat")) {
|
||||
lua_concat(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("lessthan")) {
|
||||
int a = getnum;
|
||||
if (lua_lessthan(L, a, getnum))
|
||||
lua_pushnumber(L, 1);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
}
|
||||
else if EQ(l_s("rawcall")) {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_rawcall(L, narg, nres);
|
||||
}
|
||||
else if EQ(l_s("call")) {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_call(L, narg, nres);
|
||||
}
|
||||
else if EQ(l_s("dostring")) {
|
||||
lua_dostring(L, luaL_check_string(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("settagmethod")) {
|
||||
int tag = getnum;
|
||||
const l_char *event = getname;
|
||||
lua_settagmethod(L, tag, event);
|
||||
}
|
||||
else if EQ(l_s("gettagmethod")) {
|
||||
int tag = getnum;
|
||||
const l_char *event = getname;
|
||||
lua_gettagmethod(L, tag, event);
|
||||
}
|
||||
else if EQ(l_s("type")) {
|
||||
lua_pushstring(L, lua_type(L, getnum));
|
||||
}
|
||||
else luaL_verror(L, l_s("unknown instruction %.30s"), buff);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
static const struct luaL_reg tests_funcs[] = {
|
||||
{l_s("hash"), hash_query},
|
||||
{l_s("limits"), get_limits},
|
||||
{l_s("listcode"), listcode},
|
||||
{l_s("listk"), listk},
|
||||
{l_s("listlocals"), listlocals},
|
||||
{l_s("loadlib"), loadlib},
|
||||
{l_s("querystr"), string_query},
|
||||
{l_s("querytab"), table_query},
|
||||
{l_s("testC"), testC},
|
||||
{l_s("ref"), tref},
|
||||
{l_s("getref"), getref},
|
||||
{l_s("unref"), unref},
|
||||
{l_s("d2s"), d2s},
|
||||
{l_s("s2d"), s2d},
|
||||
{l_s("newuserdata"), newuserdata},
|
||||
{l_s("newuserdatabox"), newuserdatabox},
|
||||
{l_s("settag"), settag},
|
||||
{l_s("udataval"), udataval},
|
||||
{l_s("newtag"), newtag},
|
||||
{l_s("doonnewstack"), doonnewstack},
|
||||
{l_s("newstate"), newstate},
|
||||
{l_s("closestate"), closestate},
|
||||
{l_s("doremote"), doremote},
|
||||
{l_s("settagmethod"), settagmethod},
|
||||
{l_s("equal"), equal},
|
||||
{l_s("totalmem"), mem_query}
|
||||
};
|
||||
|
||||
|
||||
void luaB_opentests (lua_State *L) {
|
||||
*((int **)L) = &islocked; /* init lock */
|
||||
lua_state = L; /* keep first state to be opened */
|
||||
/* open lib in a new table */
|
||||
lua_newtable(L);
|
||||
lua_getglobals(L);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_setglobals(L);
|
||||
luaL_openl(L, tests_funcs); /* open functions inside new table */
|
||||
lua_setglobals(L); /* restore old table of globals */
|
||||
lua_setglobal(L, l_s("T")); /* set new table as global T */
|
||||
}
|
||||
|
||||
#endif
|
||||
58
ltests.h
Normal file
58
ltests.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
** $Id: ltests.h,v 1.6 2001/03/02 17:27:50 roberto Exp roberto $
|
||||
** Internal Header for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ltests_h
|
||||
#define ltests_h
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define LUA_DEBUG
|
||||
|
||||
#define LUA_OPNAMES
|
||||
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
#define lua_assert(c) assert(c)
|
||||
#define api_check(L, o) lua_assert(o)
|
||||
|
||||
|
||||
/* to avoid warnings, and to make sure value is really unused */
|
||||
#define UNUSED(x) (x=0, (void)(x))
|
||||
|
||||
|
||||
/* memory allocator control variables */
|
||||
extern unsigned long memdebug_numblocks;
|
||||
extern unsigned long memdebug_total;
|
||||
extern unsigned long memdebug_maxmem;
|
||||
extern unsigned long memdebug_memlimit;
|
||||
|
||||
|
||||
#define l_malloc(s) debug_realloc(NULL, 0, s)
|
||||
#define l_realloc(b, os, s) debug_realloc(b, os, s)
|
||||
#define l_free(b, s) debug_realloc(b, s, 0)
|
||||
|
||||
void *debug_realloc (void *block, size_t oldsize, size_t size);
|
||||
|
||||
|
||||
|
||||
/* test for lock/unlock */
|
||||
#define LUA_USERSTATE int *lock;
|
||||
extern int islocked;
|
||||
#define lua_lock(L) lua_assert((**((int **)L))++ == 0)
|
||||
#define lua_unlock(L) lua_assert(--(**((int **)L)) == 0)
|
||||
|
||||
|
||||
extern lua_State *lua_state;
|
||||
|
||||
|
||||
void luaB_opentests (lua_State *L);
|
||||
|
||||
#define LUA_USERINIT(L) (luaB_opentests(L), openstdlibs(L))
|
||||
|
||||
|
||||
#endif
|
||||
345
ltm.c
345
ltm.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltm.c,v 1.12 1997/12/15 16:17:20 roberto Exp roberto $
|
||||
** $Id: ltm.c,v 1.75 2001/07/23 19:56:00 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,256 +8,185 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
char *luaT_eventname[] = { /* ORDER IM */
|
||||
"gettable", "settable", "index", "getglobal", "setglobal", "add",
|
||||
"sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
|
||||
"concat", "gc", "function", NULL
|
||||
const l_char *const luaT_eventname[] = { /* ORDER TM */
|
||||
l_s("gettable"), l_s("settable"), l_s("index"), l_s("getglobal"),
|
||||
l_s("setglobal"), l_s("add"), l_s("sub"), l_s("mul"), l_s("div"),
|
||||
l_s("pow"), l_s("unm"), l_s("lt"), l_s("concat"), l_s("gc"),
|
||||
l_s("function"),
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
static int luaI_checkevent (char *name, char *list[])
|
||||
{
|
||||
int e = luaO_findstring(name, list);
|
||||
static int findevent (const l_char *name) {
|
||||
int i;
|
||||
for (i=0; luaT_eventname[i]; i++)
|
||||
if (strcmp(luaT_eventname[i], name) == 0)
|
||||
return i;
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
|
||||
static int luaI_checkevent (lua_State *L, const l_char *name) {
|
||||
int e = findevent(name);
|
||||
if (e < 0)
|
||||
luaL_verror("`%.50s' is not a valid event name", name);
|
||||
luaO_verror(L, l_s("`%.50s' is not a valid event name"), name);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* events in LUA_T_NIL are all allowed, since this is used as a
|
||||
* 'placeholder' for "default" fallbacks
|
||||
/* events in LUA_TNIL are all allowed, since this is used as a
|
||||
* `placeholder' for default fallbacks
|
||||
*/
|
||||
static char validevents[NUM_TAGS][IM_N] = { /* ORDER LUA_T, ORDER IM */
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_PROTO */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CPROTO */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
|
||||
/* ORDER LUA_T, ORDER TM */
|
||||
static const lu_byte luaT_validevents[NUM_TAGS][TM_N] = {
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_TNIL */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_TSTRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TTABLE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0} /* LUA_TFUNCTION */
|
||||
};
|
||||
|
||||
|
||||
static int validevent (int t, int e)
|
||||
{ /* ORDER LUA_T */
|
||||
return (t < LUA_T_NIL) ? 1 : validevents[-t][e];
|
||||
int luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
||||
return (t >= NUM_TAGS) ? 1 : (int)luaT_validevents[t][e];
|
||||
}
|
||||
|
||||
|
||||
static void init_entry (int tag)
|
||||
{
|
||||
void luaT_init (lua_State *L) {
|
||||
static const l_char *const typenames[NUM_TAGS] = {
|
||||
l_s("userdata"), l_s("nil"), l_s("number"), l_s("string"),
|
||||
l_s("table"), l_s("function")
|
||||
};
|
||||
int i;
|
||||
for (i=0; i<IM_N; i++)
|
||||
ttype(luaT_getim(tag, i)) = LUA_T_NIL;
|
||||
for (i=0; i<NUM_TAGS; i++)
|
||||
luaT_newtag(L, typenames[i], i);
|
||||
}
|
||||
|
||||
|
||||
void luaT_init (void)
|
||||
{
|
||||
int t;
|
||||
L->IMtable_size = NUM_TAGS*2;
|
||||
L->last_tag = -(NUM_TAGS-1);
|
||||
L->IMtable = luaM_newvector(L->IMtable_size, struct IM);
|
||||
for (t=L->last_tag; t<=0; t++)
|
||||
init_entry(t);
|
||||
}
|
||||
|
||||
|
||||
int lua_newtag (void)
|
||||
{
|
||||
--L->last_tag;
|
||||
if ((-L->last_tag) >= L->IMtable_size)
|
||||
L->IMtable_size = luaM_growvector(&L->IMtable, L->IMtable_size,
|
||||
struct IM, memEM, MAX_INT);
|
||||
init_entry(L->last_tag);
|
||||
return L->last_tag;
|
||||
}
|
||||
|
||||
|
||||
static void checktag (int tag)
|
||||
{
|
||||
if (!(L->last_tag <= tag && tag <= 0))
|
||||
luaL_verror("%d is not a valid tag", tag);
|
||||
}
|
||||
|
||||
void luaT_realtag (int tag)
|
||||
{
|
||||
if (!(L->last_tag <= tag && tag < LUA_T_NIL))
|
||||
luaL_verror("tag %d is not result of `newtag'", tag);
|
||||
}
|
||||
|
||||
|
||||
int lua_copytagmethods (int tagto, int tagfrom)
|
||||
{
|
||||
int e;
|
||||
checktag(tagto);
|
||||
checktag(tagfrom);
|
||||
for (e=0; e<IM_N; e++) {
|
||||
if (validevent(tagto, e))
|
||||
*luaT_getim(tagto, e) = *luaT_getim(tagfrom, e);
|
||||
int luaT_newtag (lua_State *L, const l_char *name, int basictype) {
|
||||
int tag;
|
||||
int i;
|
||||
TString *ts = NULL;
|
||||
luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM,
|
||||
MAX_INT, l_s("tag table overflow"));
|
||||
tag = G(L)->ntag;
|
||||
if (name) {
|
||||
TObject *v;
|
||||
ts = luaS_new(L, name);
|
||||
v = luaH_setstr(L, G(L)->type2tag, ts);
|
||||
if (ttype(v) == LUA_TNUMBER) return (int)nvalue(v);
|
||||
setnvalue(v, tag);
|
||||
}
|
||||
for (i=0; i<TM_N; i++)
|
||||
luaT_gettm(G(L), tag, i) = NULL;
|
||||
G(L)->TMtable[tag].collected = NULL;
|
||||
G(L)->TMtable[tag].name = ts;
|
||||
G(L)->TMtable[tag].basictype = basictype;
|
||||
G(L)->ntag++;
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
||||
static void checktag (lua_State *L, int tag) {
|
||||
if (!(0 <= tag && tag < G(L)->ntag))
|
||||
luaO_verror(L, l_s("%d is not a valid tag"), tag);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
|
||||
int e;
|
||||
lua_lock(L);
|
||||
checktag(L, tagto);
|
||||
checktag(L, tagfrom);
|
||||
for (e=0; e<TM_N; e++) {
|
||||
if (luaT_validevent(tagto, e))
|
||||
luaT_gettm(G(L), tagto, e) = luaT_gettm(G(L), tagfrom, e);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return tagto;
|
||||
}
|
||||
|
||||
|
||||
int luaT_efectivetag (TObject *o)
|
||||
{
|
||||
int t;
|
||||
switch (t = ttype(o)) {
|
||||
case LUA_T_ARRAY:
|
||||
return o->value.a->htag;
|
||||
case LUA_T_USERDATA: {
|
||||
int tag = o->value.ts->u.d.tag;
|
||||
return (tag >= 0) ? LUA_T_USERDATA : tag;
|
||||
}
|
||||
case LUA_T_CLOSURE:
|
||||
return o->value.cl->consts[0].ttype;
|
||||
#ifdef DEBUG
|
||||
case LUA_T_PMARK: case LUA_T_CMARK:
|
||||
case LUA_T_CLMARK: case LUA_T_LINE:
|
||||
lua_error("internal error");
|
||||
#endif
|
||||
int luaT_tag (const TObject *o) {
|
||||
int t = ttype(o);
|
||||
switch (t) {
|
||||
case LUA_TUSERDATA: return uvalue(o)->uv.tag;
|
||||
case LUA_TTABLE: return hvalue(o)->htag;
|
||||
default: return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const l_char *luaT_typename (global_State *G, const TObject *o) {
|
||||
int t = ttype(o);
|
||||
int tag;
|
||||
TString *ts;
|
||||
switch (t) {
|
||||
case LUA_TUSERDATA:
|
||||
tag = uvalue(o)->uv.tag;
|
||||
break;
|
||||
case LUA_TTABLE:
|
||||
tag = hvalue(o)->htag;
|
||||
break;
|
||||
default:
|
||||
return t;
|
||||
tag = t;
|
||||
}
|
||||
ts = G->TMtable[tag].name;
|
||||
if (ts == NULL)
|
||||
ts = G->TMtable[t].name;
|
||||
return getstr(ts);
|
||||
}
|
||||
|
||||
|
||||
TObject *luaT_gettagmethod (int t, char *event)
|
||||
{
|
||||
int e = luaI_checkevent(event, luaT_eventname);
|
||||
checktag(t);
|
||||
if (validevent(t, e))
|
||||
return luaT_getim(t,e);
|
||||
else
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
|
||||
|
||||
void luaT_settagmethod (int t, char *event, TObject *func)
|
||||
{
|
||||
TObject temp = *func;
|
||||
int e = luaI_checkevent(event, luaT_eventname);
|
||||
checktag(t);
|
||||
if (!validevent(t, e))
|
||||
luaL_verror("settagmethod: cannot change internal method `%.20s' for tag %d",
|
||||
luaT_eventname[e], t);
|
||||
*func = *luaT_getim(t,e);
|
||||
*luaT_getim(t, e) = temp;
|
||||
}
|
||||
|
||||
|
||||
char *luaT_travtagmethods (int (*fn)(TObject *))
|
||||
{
|
||||
LUA_API void lua_gettagmethod (lua_State *L, int t, const l_char *event) {
|
||||
int e;
|
||||
if (fn(&L->errorim))
|
||||
return "error";
|
||||
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
|
||||
int t;
|
||||
for (t=0; t>=L->last_tag; t--)
|
||||
if (fn(luaT_getim(t,e)))
|
||||
return luaT_eventname[e];
|
||||
lua_lock(L);
|
||||
e = luaI_checkevent(L, event);
|
||||
checktag(L, t);
|
||||
if (luaT_validevent(t, e) && luaT_gettm(G(L), t, e)) {
|
||||
setclvalue(L->top, luaT_gettm(G(L), t, e));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ===================================================================
|
||||
* compatibility with old fallback system
|
||||
*/
|
||||
#ifdef LUA_COMPAT2_5
|
||||
|
||||
#include "lapi.h"
|
||||
|
||||
static void errorFB (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isstring(o))
|
||||
fprintf(stderr, "lua: %s\n", lua_getstring(o));
|
||||
else
|
||||
fprintf(stderr, "lua: unknown error\n");
|
||||
setnilvalue(L->top);
|
||||
incr_top;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
static void nilFB (void) { }
|
||||
|
||||
|
||||
static void typeFB (void)
|
||||
{
|
||||
lua_error("unexpected type");
|
||||
}
|
||||
|
||||
|
||||
static void fillvalids (IMS e, TObject *func)
|
||||
{
|
||||
int t;
|
||||
for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
|
||||
if (validevent(t, e))
|
||||
*luaT_getim(t, e) = *func;
|
||||
}
|
||||
|
||||
|
||||
void luaT_setfallback (void)
|
||||
{
|
||||
static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
|
||||
TObject oldfunc;
|
||||
lua_CFunction replace;
|
||||
char *name = luaL_check_string(1);
|
||||
lua_Object func = lua_getparam(2);
|
||||
luaL_arg_check(lua_isfunction(func), 2, "function expected");
|
||||
switch (luaO_findstring(name, oldnames)) {
|
||||
case 0: /* old error fallback */
|
||||
oldfunc = L->errorim;
|
||||
L->errorim = *luaA_Address(func);
|
||||
replace = errorFB;
|
||||
LUA_API void lua_settagmethod (lua_State *L, int t, const l_char *event) {
|
||||
int e;
|
||||
lua_lock(L);
|
||||
e = luaI_checkevent(L, event);
|
||||
checktag(L, t);
|
||||
if (!luaT_validevent(t, e))
|
||||
luaO_verror(L, l_s("cannot change `%.20s' tag method for type `%.20s'%.20s"),
|
||||
luaT_eventname[e], typenamebytag(G(L), t),
|
||||
(t == LUA_TTABLE || t == LUA_TUSERDATA) ?
|
||||
l_s(" with default tag") : l_s(""));
|
||||
switch (ttype(L->top - 1)) {
|
||||
case LUA_TNIL:
|
||||
luaT_gettm(G(L), t, e) = NULL;
|
||||
break;
|
||||
case 1: /* old getglobal fallback */
|
||||
oldfunc = *luaT_getim(LUA_T_NIL, IM_GETGLOBAL);
|
||||
*luaT_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaA_Address(func);
|
||||
replace = nilFB;
|
||||
case LUA_TFUNCTION:
|
||||
luaT_gettm(G(L), t, e) = clvalue(L->top - 1);
|
||||
break;
|
||||
case 2: { /* old arith fallback */
|
||||
int i;
|
||||
oldfunc = *luaT_getim(LUA_T_NUMBER, IM_POW);
|
||||
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
|
||||
fillvalids(i, luaA_Address(func));
|
||||
replace = typeFB;
|
||||
break;
|
||||
}
|
||||
case 3: { /* old order fallback */
|
||||
int i;
|
||||
oldfunc = *luaT_getim(LUA_T_NIL, IM_LT);
|
||||
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
|
||||
fillvalids(i, luaA_Address(func));
|
||||
replace = typeFB;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
int e;
|
||||
if ((e = luaO_findstring(name, luaT_eventname)) >= 0) {
|
||||
oldfunc = *luaT_getim(LUA_T_NIL, e);
|
||||
fillvalids(e, luaA_Address(func));
|
||||
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
|
||||
}
|
||||
else {
|
||||
luaL_verror("`%.50s' is not a valid fallback name", name);
|
||||
replace = NULL; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
default:
|
||||
luaD_error(L, l_s("tag method must be a function (or nil)"));
|
||||
}
|
||||
if (oldfunc.ttype != LUA_T_NIL)
|
||||
luaA_pushobject(&oldfunc);
|
||||
else
|
||||
lua_pushcfunction(replace);
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
88
ltm.h
88
ltm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltm.h,v 1.3 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** $Id: ltm.h,v 1.25 2001/06/06 18:00:19 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,50 +13,68 @@
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER IM"
|
||||
* grep "ORDER TM"
|
||||
*/
|
||||
typedef enum {
|
||||
IM_GETTABLE = 0,
|
||||
IM_SETTABLE,
|
||||
IM_INDEX,
|
||||
IM_GETGLOBAL,
|
||||
IM_SETGLOBAL,
|
||||
IM_ADD,
|
||||
IM_SUB,
|
||||
IM_MUL,
|
||||
IM_DIV,
|
||||
IM_POW,
|
||||
IM_UNM,
|
||||
IM_LT,
|
||||
IM_LE,
|
||||
IM_GT,
|
||||
IM_GE,
|
||||
IM_CONCAT,
|
||||
IM_GC,
|
||||
IM_FUNCTION
|
||||
} IMS;
|
||||
|
||||
#define IM_N 18
|
||||
TM_GETTABLE = 0,
|
||||
TM_SETTABLE,
|
||||
TM_INDEX,
|
||||
TM_GETGLOBAL,
|
||||
TM_SETGLOBAL,
|
||||
TM_ADD,
|
||||
TM_SUB,
|
||||
TM_MUL,
|
||||
TM_DIV,
|
||||
TM_POW,
|
||||
TM_UNM,
|
||||
TM_LT,
|
||||
TM_CONCAT,
|
||||
TM_GC,
|
||||
TM_FUNCTION,
|
||||
TM_N /* number of elements in the enum */
|
||||
} TMS;
|
||||
|
||||
|
||||
struct IM {
|
||||
TObject int_method[IM_N];
|
||||
|
||||
/*
|
||||
** masks for allowable tag methods
|
||||
** (see `luaT_validevents')
|
||||
*/
|
||||
#define HAS_TM_GETGLOBAL(L,t) (1<<(t) & ((1<<LUA_TUSERDATA) | \
|
||||
(1<<LUA_TTABLE) | \
|
||||
(1<<LUA_TNIL)))
|
||||
|
||||
#define HAS_TM_SETGLOBAL(L,t) (1<<(t) & ((1<<LUA_TUSERDATA) | \
|
||||
(1<<LUA_TTABLE) | \
|
||||
(1<<LUA_TNIL) | \
|
||||
(1<<LUA_TFUNCTION)))
|
||||
|
||||
|
||||
|
||||
struct TM {
|
||||
Closure *method[TM_N];
|
||||
Udata *collected; /* list of garbage-collected udata with this tag */
|
||||
TString *name; /* type name */
|
||||
int basictype;
|
||||
};
|
||||
|
||||
|
||||
#define luaT_getim(tag,event) (&L->IMtable[-(tag)].int_method[event])
|
||||
#define luaT_getimbyObj(o,e) (luaT_getim(luaT_efectivetag(o),(e)))
|
||||
#define luaT_gettm(G,tag,event) (G->TMtable[tag].method[event])
|
||||
#define luaT_gettmbyObj(G,o,e) (luaT_gettm((G),luaT_tag(o),(e)))
|
||||
|
||||
extern char *luaT_eventname[];
|
||||
#define typenamebytag(G, t) getstr(G->TMtable[t].name)
|
||||
|
||||
|
||||
void luaT_init (void);
|
||||
void luaT_realtag (int tag);
|
||||
int luaT_efectivetag (TObject *o);
|
||||
void luaT_settagmethod (int t, char *event, TObject *func);
|
||||
TObject *luaT_gettagmethod (int t, char *event);
|
||||
char *luaT_travtagmethods (int (*fn)(TObject *));
|
||||
#define validtag(G,t) (NUM_TAGS <= (t) && (t) < G->ntag)
|
||||
|
||||
extern const l_char *const luaT_eventname[];
|
||||
|
||||
|
||||
void luaT_init (lua_State *L);
|
||||
int luaT_newtag (lua_State *L, const l_char *name, int basictype);
|
||||
const l_char *luaT_typename (global_State *G, const TObject *o);
|
||||
int luaT_tag (const TObject *o);
|
||||
int luaT_validevent (int t, int e); /* used by compatibility module */
|
||||
|
||||
void luaT_setfallback (void); /* only if LUA_COMPAT2_5 */
|
||||
|
||||
#endif
|
||||
|
||||
420
lua.c
420
lua.c
@@ -1,159 +1,335 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.11 1997/12/22 18:05:23 roberto Exp roberto $
|
||||
** $Id: lua.c,v 1.67 2001/06/06 18:00:19 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#ifndef OLD_ANSI
|
||||
#include <locale.h>
|
||||
#else
|
||||
#define setlocale(a,b) 0
|
||||
#endif
|
||||
|
||||
#ifdef _POSIX_SOURCE
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#define isatty(x) (x==0) /* assume stdin is a tty */
|
||||
static int isatty (int x) { return x==0; } /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
|
||||
static void print_message (void)
|
||||
{
|
||||
|
||||
#ifndef PROMPT
|
||||
#define PROMPT l_s("> ")
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef LUA_USERINIT
|
||||
#define LUA_USERINIT(L) openstdlibs(L)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** global options
|
||||
*/
|
||||
struct Options {
|
||||
int toclose;
|
||||
int stacksize;
|
||||
};
|
||||
|
||||
|
||||
static lua_State *L = NULL;
|
||||
|
||||
|
||||
typedef void (*handler)(int); /* type for signal actions */
|
||||
|
||||
static void laction (int i);
|
||||
|
||||
|
||||
static lua_Hook old_linehook = NULL;
|
||||
static lua_Hook old_callhook = NULL;
|
||||
|
||||
|
||||
|
||||
static handler lreset (void) {
|
||||
return signal(SIGINT, laction);
|
||||
}
|
||||
|
||||
|
||||
static void lstop (void) {
|
||||
lua_setlinehook(L, old_linehook);
|
||||
lua_setcallhook(L, old_callhook);
|
||||
lreset();
|
||||
lua_error(L, l_s("interrupted!"));
|
||||
}
|
||||
|
||||
|
||||
static void laction (int i) {
|
||||
(void)i; /* to avoid warnings */
|
||||
signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
|
||||
terminate process (default action) */
|
||||
old_linehook = lua_setlinehook(L, (lua_Hook)lstop);
|
||||
old_callhook = lua_setcallhook(L, (lua_Hook)lstop);
|
||||
}
|
||||
|
||||
|
||||
static int ldo (int (*f)(lua_State *l, const l_char *), const l_char *name) {
|
||||
int res;
|
||||
handler h = lreset();
|
||||
int top = lua_gettop(L);
|
||||
res = f(L, name); /* dostring | dofile */
|
||||
lua_settop(L, top); /* remove eventual results */
|
||||
signal(SIGINT, h); /* restore old action */
|
||||
/* Lua gives no message in such cases, so lua.c provides one */
|
||||
if (res == LUA_ERRMEM) {
|
||||
fprintf(stderr, l_s("lua: memory allocation error\n"));
|
||||
}
|
||||
else if (res == LUA_ERRERR)
|
||||
fprintf(stderr, l_s("lua: error in error message\n"));
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static void print_message (void) {
|
||||
fprintf(stderr,
|
||||
"Lua: command line options:\n"
|
||||
" -v print version information\n"
|
||||
" -d turn debug on\n"
|
||||
" -e stat dostring `stat'\n"
|
||||
" -q interactive mode without prompt\n"
|
||||
" -i interactive mode with prompt\n"
|
||||
" - executes stdin as a file\n"
|
||||
" a=b sets global `a' with string `b'\n"
|
||||
" name dofile `name'\n\n");
|
||||
l_s("usage: lua [options]. Available options are:\n")
|
||||
l_s(" - execute stdin as a file\n")
|
||||
l_s(" -c close Lua when exiting\n")
|
||||
l_s(" -e stat execute string `stat'\n")
|
||||
l_s(" -f name execute file `name' with remaining arguments in table `arg'\n")
|
||||
l_s(" -i enter interactive mode with prompt\n")
|
||||
l_s(" -q enter interactive mode without prompt\n")
|
||||
l_s(" -sNUM set stack size to NUM (must be the first option)\n")
|
||||
l_s(" -v print version information\n")
|
||||
l_s(" a=b set global `a' to string `b'\n")
|
||||
l_s(" name execute file `name'\n")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
static void assign (char *arg)
|
||||
{
|
||||
if (strlen(arg) >= 500)
|
||||
fprintf(stderr, "lua: shell argument too long");
|
||||
else {
|
||||
char buffer[500];
|
||||
char *eq = strchr(arg, '=');
|
||||
lua_pushstring(eq+1);
|
||||
strncpy(buffer, arg, eq-arg);
|
||||
buffer[eq-arg] = 0;
|
||||
lua_setglobal(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
#define BUF_SIZE 512
|
||||
|
||||
static void manual_input (int prompt)
|
||||
{
|
||||
int cont = 1;
|
||||
while (cont) {
|
||||
char buffer[BUF_SIZE];
|
||||
int i = 0;
|
||||
lua_beginblock();
|
||||
if (prompt)
|
||||
printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
|
||||
for(;;) {
|
||||
int c = getchar();
|
||||
if (c == EOF) {
|
||||
cont = 0;
|
||||
break;
|
||||
}
|
||||
else if (c == '\n') {
|
||||
if (i>0 && buffer[i-1] == '\\')
|
||||
buffer[i-1] = '\n';
|
||||
else break;
|
||||
}
|
||||
else if (i >= BUF_SIZE-1) {
|
||||
fprintf(stderr, "lua: argument line too long\n");
|
||||
break;
|
||||
}
|
||||
else buffer[i++] = c;
|
||||
}
|
||||
buffer[i] = 0;
|
||||
lua_dostring(buffer);
|
||||
lua_endblock();
|
||||
}
|
||||
printf("\n");
|
||||
static void print_version (void) {
|
||||
printf(l_s("%.80s %.80s\n"), l_s(LUA_VERSION), l_s(LUA_COPYRIGHT));
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
static void assign (l_char *arg) {
|
||||
l_char *eq = strchr(arg, l_c('='));
|
||||
*eq = l_c('\0'); /* spilt `arg' in two strings (name & value) */
|
||||
lua_pushstring(L, eq+1);
|
||||
lua_setglobal(L, arg);
|
||||
}
|
||||
|
||||
|
||||
static void getargs (l_char *argv[]) {
|
||||
int i;
|
||||
setlocale(LC_ALL, "");
|
||||
lua_iolibopen();
|
||||
lua_strlibopen();
|
||||
lua_mathlibopen();
|
||||
lua_pushstring("> "); lua_setglobal("_PROMPT");
|
||||
if (argc < 2) { /* no arguments? */
|
||||
lua_newtable(L);
|
||||
for (i=0; argv[i]; i++) {
|
||||
/* arg[i] = argv[i] */
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
/* arg.n = maximum index in table `arg' */
|
||||
lua_pushliteral(L, l_s("n"));
|
||||
lua_pushnumber(L, i-1);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static int l_getargs (lua_State *l) {
|
||||
l_char **argv = (l_char **)lua_touserdata(l, -1);
|
||||
getargs(argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int file_input (const l_char *argv) {
|
||||
int result = ldo(lua_dofile, argv);
|
||||
if (result) {
|
||||
if (result == LUA_ERRFILE) {
|
||||
fprintf(stderr, l_s("lua: cannot execute file "));
|
||||
perror(argv);
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
else
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/* maximum length of an input line */
|
||||
#ifndef MAXINPUT
|
||||
#define MAXINPUT 512
|
||||
#endif
|
||||
|
||||
|
||||
static const l_char *get_prompt (int prompt) {
|
||||
if (!prompt)
|
||||
return l_s("");
|
||||
else {
|
||||
const l_char *s;
|
||||
lua_getglobal(L, l_s("_PROMPT"));
|
||||
s = lua_tostring(L, -1);
|
||||
if (!s) s = PROMPT;
|
||||
lua_pop(L, 1); /* remove global */
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void manual_input (int version, int prompt) {
|
||||
if (version) print_version();
|
||||
for (;;) {
|
||||
fputs(get_prompt(prompt), stdout); /* show prompt */
|
||||
for(;;) {
|
||||
l_char buffer[MAXINPUT];
|
||||
size_t l;
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
|
||||
printf(l_s("\n"));
|
||||
return;
|
||||
}
|
||||
l = strlen(buffer);
|
||||
if (buffer[l-1] == l_c('\n') && buffer[l-2] == l_c('\\')) {
|
||||
buffer[l-2] = l_c('\n');
|
||||
lua_pushlstring(L, buffer, l-1);
|
||||
}
|
||||
else {
|
||||
lua_pushlstring(L, buffer, l);
|
||||
break;
|
||||
}
|
||||
}
|
||||
lua_concat(L, lua_gettop(L));
|
||||
ldo(lua_dostring, lua_tostring(L, -1));
|
||||
lua_settop(L, 0); /* remove eventual results */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int handle_argv (l_char *argv[], struct Options *opt) {
|
||||
if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */
|
||||
if (*argv == NULL) { /* no more arguments? */
|
||||
if (isatty(0)) {
|
||||
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
|
||||
manual_input(1);
|
||||
manual_input(1, 1);
|
||||
}
|
||||
else
|
||||
lua_dofile(NULL); /* executes stdin as a file */
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
}
|
||||
else for (i=1; i<argc; i++) {
|
||||
if (argv[i][0] == '-') { /* option? */
|
||||
switch (argv[i][1]) {
|
||||
case 0:
|
||||
lua_dofile(NULL); /* executes stdin as a file */
|
||||
break;
|
||||
case 'i':
|
||||
manual_input(1);
|
||||
break;
|
||||
case 'q':
|
||||
manual_input(0);
|
||||
break;
|
||||
case 'd':
|
||||
lua_debug = 1;
|
||||
break;
|
||||
case 'v':
|
||||
printf("%s %s\n(written by %s)\n\n",
|
||||
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
|
||||
break;
|
||||
case 'e':
|
||||
i++;
|
||||
if (lua_dostring(argv[i]) != 0) {
|
||||
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
print_message();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else if (strchr(argv[i], '='))
|
||||
assign(argv[i]);
|
||||
else {
|
||||
int result = lua_dofile(argv[i]);
|
||||
if (result) {
|
||||
if (result == 2) {
|
||||
fprintf(stderr, "lua: cannot execute file ");
|
||||
perror(argv[i]);
|
||||
else { /* other arguments; loop over them */
|
||||
int i;
|
||||
for (i = 0; argv[i] != NULL; i++) {
|
||||
if (argv[i][0] != l_c('-')) { /* not an option? */
|
||||
if (strchr(argv[i], l_c('=')))
|
||||
assign(argv[i]);
|
||||
else
|
||||
if (file_input(argv[i]) != EXIT_SUCCESS)
|
||||
return EXIT_FAILURE; /* stop if file fails */
|
||||
}
|
||||
else switch (argv[i][1]) { /* option */
|
||||
case 0: {
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
break;
|
||||
}
|
||||
case l_c('i'): {
|
||||
manual_input(0, 1);
|
||||
break;
|
||||
}
|
||||
case l_c('q'): {
|
||||
manual_input(0, 0);
|
||||
break;
|
||||
}
|
||||
case l_c('c'): {
|
||||
opt->toclose = 1;
|
||||
break;
|
||||
}
|
||||
case l_c('v'): {
|
||||
print_version();
|
||||
break;
|
||||
}
|
||||
case l_c('e'): {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (ldo(lua_dostring, argv[i]) != 0) {
|
||||
fprintf(stderr, l_s("lua: error running argument `%.99s'\n"), argv[i]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case l_c('f'): {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
getargs(argv+i); /* collect remaining arguments */
|
||||
lua_setglobal(L, l_s("arg"));
|
||||
return file_input(argv[i]); /* stop scanning arguments */
|
||||
}
|
||||
case l_c('s'): {
|
||||
fprintf(stderr, l_s("lua: stack size (`-s') must be the first option\n"));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
default: {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG
|
||||
lua_close();
|
||||
#endif
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static void getstacksize (int argc, l_char *argv[], struct Options *opt) {
|
||||
if (argc >= 2 && argv[1][0] == l_c('-') && argv[1][1] == l_c('s')) {
|
||||
int stacksize = strtol(&argv[1][2], NULL, 10);
|
||||
if (stacksize <= 0) {
|
||||
fprintf(stderr, l_s("lua: invalid stack size ('%.20s')\n"), &argv[1][2]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
opt->stacksize = stacksize;
|
||||
}
|
||||
else
|
||||
opt->stacksize = 0; /* no stack size */
|
||||
}
|
||||
|
||||
|
||||
static void register_getargs (l_char *argv[]) {
|
||||
lua_newuserdatabox(L, argv);
|
||||
lua_pushcclosure(L, l_getargs, 1);
|
||||
lua_setglobal(L, l_s("getargs"));
|
||||
}
|
||||
|
||||
|
||||
static void openstdlibs (lua_State *l) {
|
||||
lua_baselibopen(l);
|
||||
lua_iolibopen(l);
|
||||
lua_strlibopen(l);
|
||||
lua_mathlibopen(l);
|
||||
lua_dblibopen(l);
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, l_char *argv[]) {
|
||||
struct Options opt;
|
||||
int status;
|
||||
opt.toclose = 0;
|
||||
getstacksize(argc, argv, &opt); /* handle option `-s' */
|
||||
L = lua_open(opt.stacksize); /* create state */
|
||||
LUA_USERINIT(L); /* open libraries */
|
||||
register_getargs(argv); /* create `getargs' function */
|
||||
status = handle_argv(argv+1, &opt);
|
||||
if (opt.toclose)
|
||||
lua_close(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
449
lua.h
449
lua.h
@@ -1,178 +1,349 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.13 1998/01/02 17:46:32 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.101 2001/07/12 18:11:58 roberto Exp roberto $
|
||||
** Lua - An Extensible Extension Language
|
||||
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
|
||||
** e-mail: lua@tecgraf.puc-rio.br
|
||||
** www: http://www.tecgraf.puc-rio.br/lua/
|
||||
** e-mail: info@lua.org
|
||||
** www: http://www.lua.org
|
||||
** See Copyright Notice at the end of this file
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Copyright (c) 1994-1998 TeCGraf, PUC-Rio.
|
||||
* Written by Waldemar Celes Filho, Roberto Ierusalimschy and
|
||||
* Luiz Henrique de Figueiredo.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, without written agreement and with
|
||||
* out license or royalty fees, to use, copy, modify, and distribute
|
||||
* this software and its documentation for any purpose, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall ap
|
||||
* pear in all copies or substantial portions of this software.
|
||||
*
|
||||
* The name "Lua" cannot be used for any modified form of this soft
|
||||
* ware that does not originate from the authors. Nevertheless, the
|
||||
* name "Lua" may and should be used to designate the language im
|
||||
* plemented and described in this package, even if embedded in any
|
||||
* other system, as long as its syntax and semantics remain un
|
||||
* changed.
|
||||
*
|
||||
* The authors specifically disclaim any warranties, including, but
|
||||
* not limited to, the implied warranties of merchantability and
|
||||
* fitness for a particular purpose. The software provided hereunder
|
||||
* is on an "as is" basis, and the authors have no obligation to
|
||||
* provide maintenance, support, updates, enhancements, or modifica
|
||||
* tions. In no event shall TeCGraf, PUC-Rio, or the authors be li
|
||||
* able to any party for direct, indirect, special, incidental, or
|
||||
* consequential damages arising out of the use of this software and
|
||||
* its documentation.
|
||||
*********************************************************************/
|
||||
|
||||
|
||||
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
#define LUA_VERSION "Lua 3.1 (alpha)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-1998 TeCGraf"
|
||||
|
||||
/* definition of `size_t' */
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
|
||||
#define LUA_VERSION "Lua 4.1 (alpha)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2001 TeCGraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
|
||||
|
||||
#define LUA_NOOBJECT 0
|
||||
|
||||
#define LUA_ANYTAG (-1)
|
||||
|
||||
typedef void (*lua_CFunction) (void);
|
||||
typedef unsigned int lua_Object;
|
||||
/* name of global variable with error handler */
|
||||
#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
|
||||
|
||||
|
||||
void lua_open (void);
|
||||
void lua_close (void);
|
||||
|
||||
lua_Object lua_settagmethod (int tag, char *event); /* In: new method */
|
||||
lua_Object lua_gettagmethod (int tag, char *event);
|
||||
lua_Object lua_seterrormethod (void); /* In: new method */
|
||||
|
||||
int lua_newtag (void);
|
||||
int lua_copytagmethods (int tagto, int tagfrom);
|
||||
void lua_settag (int tag); /* In: object */
|
||||
|
||||
void lua_error (char *s);
|
||||
int lua_dofile (char *filename); /* Out: returns */
|
||||
int lua_dostring (char *string); /* Out: returns */
|
||||
int lua_callfunction (lua_Object f);
|
||||
/* In: parameters; Out: returns */
|
||||
|
||||
void lua_beginblock (void);
|
||||
void lua_endblock (void);
|
||||
|
||||
lua_Object lua_lua2C (int number);
|
||||
#define lua_getparam(_) lua_lua2C(_)
|
||||
#define lua_getresult(_) lua_lua2C(_)
|
||||
|
||||
int lua_isnil (lua_Object object);
|
||||
int lua_istable (lua_Object object);
|
||||
int lua_isuserdata (lua_Object object);
|
||||
int lua_iscfunction (lua_Object object);
|
||||
int lua_isnumber (lua_Object object);
|
||||
int lua_isstring (lua_Object object);
|
||||
int lua_isfunction (lua_Object object);
|
||||
|
||||
double lua_getnumber (lua_Object object);
|
||||
char *lua_getstring (lua_Object object);
|
||||
lua_CFunction lua_getcfunction (lua_Object object);
|
||||
void *lua_getuserdata (lua_Object object);
|
||||
/* pre-defined references */
|
||||
#define LUA_NOREF (-2)
|
||||
#define LUA_REFNIL (-1)
|
||||
|
||||
|
||||
void lua_pushnil (void);
|
||||
void lua_pushnumber (double n);
|
||||
void lua_pushstring (char *s);
|
||||
void lua_pushCclosure (lua_CFunction fn, int n);
|
||||
void lua_pushusertag (void *u, int tag);
|
||||
void lua_pushobject (lua_Object object);
|
||||
|
||||
lua_Object lua_pop (void);
|
||||
|
||||
lua_Object lua_getglobal (char *name);
|
||||
lua_Object lua_rawgetglobal (char *name);
|
||||
void lua_setglobal (char *name); /* In: value */
|
||||
void lua_rawsetglobal (char *name); /* In: value */
|
||||
|
||||
void lua_settable (void); /* In: table, index, value */
|
||||
void lua_rawsettable (void); /* In: table, index, value */
|
||||
lua_Object lua_gettable (void); /* In: table, index */
|
||||
lua_Object lua_rawgettable (void); /* In: table, index */
|
||||
|
||||
int lua_tag (lua_Object object);
|
||||
/* option for multiple returns in `lua_call' */
|
||||
#define LUA_MULTRET (-1)
|
||||
|
||||
|
||||
int lua_ref (int lock); /* In: value */
|
||||
lua_Object lua_getref (int ref);
|
||||
void lua_unref (int ref);
|
||||
/* error codes for `lua_do*' and the like */
|
||||
#define LUA_ERRRUN 1
|
||||
#define LUA_ERRFILE 2
|
||||
#define LUA_ERRSYNTAX 3
|
||||
#define LUA_ERRMEM 4
|
||||
#define LUA_ERRERR 5
|
||||
|
||||
lua_Object lua_createtable (void);
|
||||
|
||||
long lua_collectgarbage (long limit);
|
||||
/* weak-table modes */
|
||||
#define LUA_WEAK_KEY 1
|
||||
#define LUA_WEAK_VALUE 2
|
||||
|
||||
|
||||
/* =============================================================== */
|
||||
/* some useful macros */
|
||||
typedef struct lua_State lua_State;
|
||||
|
||||
#define lua_call(name) lua_callfunction(lua_getglobal(name))
|
||||
|
||||
#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
|
||||
|
||||
#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))
|
||||
|
||||
#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n))
|
||||
|
||||
#define lua_pushuserdata(u) lua_pushusertag(u, 0)
|
||||
|
||||
#define lua_pushcfunction(f) lua_pushCclosure(f, 0)
|
||||
|
||||
#define lua_clonetag(t) lua_copytagmethods(lua_newtag(), (t))
|
||||
typedef int (*lua_CFunction) (lua_State *L);
|
||||
|
||||
|
||||
/* ==========================================================================
|
||||
** for compatibility with old versions. Avoid using these macros/functions
|
||||
** If your program does need any of these, define LUA_COMPAT2_5
|
||||
/*
|
||||
** an invalid `tag'
|
||||
*/
|
||||
#define LUA_NOTAG (-1)
|
||||
|
||||
/*
|
||||
** tags for basic types
|
||||
*/
|
||||
#define LUA_TNONE LUA_NOTAG
|
||||
|
||||
#define LUA_TUSERDATA 0
|
||||
#define LUA_TNIL 1
|
||||
#define LUA_TNUMBER 2
|
||||
#define LUA_TSTRING 3
|
||||
#define LUA_TTABLE 4
|
||||
#define LUA_TFUNCTION 5
|
||||
|
||||
|
||||
/* minimum Lua stack available to a C function */
|
||||
#define LUA_MINSTACK 20
|
||||
|
||||
|
||||
/*
|
||||
** generic extra include file
|
||||
*/
|
||||
#ifdef LUA_USER_H
|
||||
#include LUA_USER_H
|
||||
#endif
|
||||
|
||||
|
||||
/* Lua numerical type */
|
||||
#ifndef LUA_NUMBER
|
||||
#define LUA_NUMBER double
|
||||
#endif
|
||||
typedef LUA_NUMBER lua_Number;
|
||||
|
||||
/* Lua character type */
|
||||
#ifndef L_CHAR
|
||||
#define L_CHAR char
|
||||
#endif
|
||||
typedef L_CHAR lua_char;
|
||||
|
||||
|
||||
/* mark for all API functions */
|
||||
#ifndef LUA_API
|
||||
#define LUA_API extern
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** state manipulation
|
||||
*/
|
||||
LUA_API lua_State *lua_newthread (lua_State *L, int stacksize);
|
||||
LUA_API void lua_close (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** basic stack manipulation
|
||||
*/
|
||||
LUA_API int lua_gettop (lua_State *L);
|
||||
LUA_API void lua_settop (lua_State *L, int index);
|
||||
LUA_API void lua_pushvalue (lua_State *L, int index);
|
||||
LUA_API void lua_remove (lua_State *L, int index);
|
||||
LUA_API void lua_insert (lua_State *L, int index);
|
||||
LUA_API int lua_stackspace (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** access functions (stack -> C)
|
||||
*/
|
||||
|
||||
LUA_API const lua_char *lua_type (lua_State *L, int index);
|
||||
LUA_API int lua_isnumber (lua_State *L, int index);
|
||||
LUA_API int lua_isstring (lua_State *L, int index);
|
||||
LUA_API int lua_iscfunction (lua_State *L, int index);
|
||||
LUA_API int lua_tag (lua_State *L, int index);
|
||||
LUA_API int lua_rawtag (lua_State *L, int index);
|
||||
|
||||
#ifdef LUA_COMPAT2_5
|
||||
LUA_API int lua_equal (lua_State *L, int index1, int index2);
|
||||
LUA_API int lua_lessthan (lua_State *L, int index1, int index2);
|
||||
|
||||
LUA_API lua_Number lua_tonumber (lua_State *L, int index);
|
||||
LUA_API const lua_char *lua_tostring (lua_State *L, int index);
|
||||
LUA_API size_t lua_strlen (lua_State *L, int index);
|
||||
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index);
|
||||
LUA_API void *lua_touserdata (lua_State *L, int index);
|
||||
LUA_API const void *lua_topointer (lua_State *L, int index);
|
||||
|
||||
|
||||
lua_Object lua_setfallback (char *event, lua_CFunction fallback);
|
||||
/*
|
||||
** push functions (C -> stack)
|
||||
*/
|
||||
LUA_API void lua_pushnil (lua_State *L);
|
||||
LUA_API void lua_pushnumber (lua_State *L, lua_Number n);
|
||||
LUA_API void lua_pushlstring (lua_State *L, const lua_char *s, size_t len);
|
||||
LUA_API void lua_pushstring (lua_State *L, const lua_char *s);
|
||||
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
|
||||
|
||||
#define lua_storeglobal lua_setglobal
|
||||
#define lua_type lua_tag
|
||||
|
||||
#define lua_lockobject(o) lua_refobject(o,1)
|
||||
#define lua_lock() lua_ref(1)
|
||||
#define lua_getlocked lua_getref
|
||||
#define lua_pushlocked lua_pushref
|
||||
#define lua_unlock lua_unref
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
LUA_API void lua_getglobal (lua_State *L, const lua_char *name);
|
||||
LUA_API void lua_gettable (lua_State *L, int index);
|
||||
LUA_API void lua_rawget (lua_State *L, int index);
|
||||
LUA_API void lua_rawgeti (lua_State *L, int index, int n);
|
||||
LUA_API void lua_getglobals (lua_State *L);
|
||||
LUA_API void lua_gettagmethod (lua_State *L, int tag, const lua_char *event);
|
||||
LUA_API int lua_getref (lua_State *L, int ref);
|
||||
LUA_API void lua_newtable (lua_State *L);
|
||||
LUA_API void lua_getregistry (lua_State *L);
|
||||
LUA_API void lua_getweakregistry (lua_State *L);
|
||||
|
||||
#define lua_pushliteral(o) lua_pushstring(o)
|
||||
|
||||
#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_gettable())
|
||||
#define lua_getfield(o,f) (lua_pushobject(o), lua_pushstring(f), lua_gettable())
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
LUA_API void lua_setglobal (lua_State *L, const lua_char *name);
|
||||
LUA_API void lua_settable (lua_State *L, int index);
|
||||
LUA_API void lua_rawset (lua_State *L, int index);
|
||||
LUA_API void lua_rawseti (lua_State *L, int index, int n);
|
||||
LUA_API void lua_setglobals (lua_State *L);
|
||||
LUA_API void lua_settagmethod (lua_State *L, int tag, const lua_char *event);
|
||||
LUA_API int lua_ref (lua_State *L, int lock);
|
||||
|
||||
#define lua_copystring(o) (strdup(lua_getstring(o)))
|
||||
|
||||
#define lua_getsubscript lua_gettable
|
||||
#define lua_storesubscript lua_settable
|
||||
/*
|
||||
** `load' and `do' functions (load and run Lua code)
|
||||
*/
|
||||
LUA_API int lua_call (lua_State *L, int nargs, int nresults);
|
||||
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults);
|
||||
LUA_API int lua_loadfile (lua_State *L, const lua_char *filename);
|
||||
LUA_API int lua_dofile (lua_State *L, const lua_char *filename);
|
||||
LUA_API int lua_dostring (lua_State *L, const lua_char *str);
|
||||
LUA_API int lua_loadbuffer (lua_State *L, const lua_char *buff, size_t size,
|
||||
const lua_char *name);
|
||||
LUA_API int lua_dobuffer (lua_State *L, const lua_char *buff, size_t size,
|
||||
const lua_char *name);
|
||||
|
||||
/*
|
||||
** Garbage-collection functions
|
||||
*/
|
||||
LUA_API int lua_getgcthreshold (lua_State *L);
|
||||
LUA_API int lua_getgccount (lua_State *L);
|
||||
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
|
||||
|
||||
/*
|
||||
** miscellaneous functions
|
||||
*/
|
||||
LUA_API int lua_newtype (lua_State *L, const lua_char *name, int basictype);
|
||||
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
||||
LUA_API void lua_settag (lua_State *L, int tag);
|
||||
|
||||
LUA_API int lua_name2tag (lua_State *L, const lua_char *name);
|
||||
LUA_API const lua_char *lua_tag2name (lua_State *L, int tag);
|
||||
|
||||
LUA_API void lua_error (lua_State *L, const lua_char *s);
|
||||
|
||||
LUA_API void lua_unref (lua_State *L, int ref);
|
||||
|
||||
LUA_API int lua_next (lua_State *L, int index);
|
||||
LUA_API int lua_getn (lua_State *L, int index);
|
||||
|
||||
LUA_API void lua_concat (lua_State *L, int n);
|
||||
|
||||
LUA_API void *lua_newuserdata (lua_State *L, size_t size);
|
||||
LUA_API void lua_newuserdatabox (lua_State *L, void *u);
|
||||
|
||||
LUA_API void lua_setweakmode (lua_State *L, int mode);
|
||||
LUA_API int lua_getweakmode (lua_State *L, int index);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** some useful macros
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#define lua_open(n) lua_newthread(NULL, (n))
|
||||
|
||||
#define lua_pop(L,n) lua_settop(L, -(n)-1)
|
||||
|
||||
#define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n))
|
||||
#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
|
||||
#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
|
||||
|
||||
#define lua_isfunction(L,n) (lua_rawtag(L,n) == LUA_TFUNCTION)
|
||||
#define lua_istable(L,n) (lua_rawtag(L,n) == LUA_TTABLE)
|
||||
#define lua_isuserdata(L,n) (lua_rawtag(L,n) == LUA_TUSERDATA)
|
||||
#define lua_isnil(L,n) (lua_rawtag(L,n) == LUA_TNIL)
|
||||
#define lua_isnull(L,n) (lua_rawtag(L,n) == LUA_TNONE)
|
||||
|
||||
#define lua_pushliteral(L, s) lua_pushlstring(L, s, \
|
||||
(sizeof(s)/sizeof(lua_char))-1)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** compatibility macros
|
||||
*/
|
||||
#define lua_newtag(L) lua_newtype(L, NULL, LUA_TNONE)
|
||||
#define lua_typename lua_tag2name
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================================
|
||||
** useful definitions for Lua kernel and libraries
|
||||
*/
|
||||
#ifdef LUA_PRIVATE
|
||||
|
||||
#define l_char lua_char
|
||||
|
||||
/* macro to control type of literal strings */
|
||||
#ifndef l_s
|
||||
#define l_s(x) x
|
||||
#endif
|
||||
|
||||
/* macro to control type of literal chars */
|
||||
#ifndef l_c
|
||||
#define l_c(x) x
|
||||
#endif
|
||||
|
||||
/* macro to `unsign' a character */
|
||||
#ifndef uchar
|
||||
#define uchar(c) ((unsigned char)(c))
|
||||
#endif
|
||||
|
||||
/* integer type to hold the result of fgetc */
|
||||
#ifndef l_charint
|
||||
#define l_charint int
|
||||
#endif
|
||||
|
||||
/*
|
||||
** formats for Lua numbers
|
||||
*/
|
||||
#ifndef LUA_NUMBER_SCAN
|
||||
#define LUA_NUMBER_SCAN "%lf"
|
||||
#endif
|
||||
#ifndef LUA_NUMBER_FMT
|
||||
#define LUA_NUMBER_FMT "%.16g"
|
||||
#endif
|
||||
/* function to convert a lua_Number to a string */
|
||||
#ifndef lua_number2str
|
||||
#define lua_number2str(s,n) sprintf((s), l_s(LUA_NUMBER_FMT), (n))
|
||||
#endif
|
||||
|
||||
/* function to convert a string to a lua_Number */
|
||||
#ifndef lua_str2number
|
||||
#define lua_str2number(s,p) strtod((s), (p))
|
||||
#endif
|
||||
|
||||
#endif
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (C) 1994-2001 TeCGraf, PUC-Rio. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, without written agreement and without license
|
||||
* or royalty fees, to use, copy, modify, and distribute this software and its
|
||||
* documentation for any purpose, including commercial applications, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* - The above copyright notice and this permission notice shall appear in all
|
||||
* copies or substantial portions of this software.
|
||||
*
|
||||
* - The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software in a
|
||||
* product, an acknowledgment in the product documentation would be greatly
|
||||
* appreciated (but it is not required).
|
||||
*
|
||||
* - Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* The authors specifically disclaim any warranties, including, but not limited
|
||||
* to, the implied warranties of merchantability and fitness for a particular
|
||||
* purpose. The software provided hereunder is on an "as is" basis, and the
|
||||
* authors have no obligation to provide maintenance, support, updates,
|
||||
* enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the
|
||||
* authors be held liable to any party for direct, indirect, special,
|
||||
* incidental, or consequential damages arising out of the use of this software
|
||||
* and its documentation.
|
||||
*
|
||||
* The Lua language and this implementation have been entirely designed and
|
||||
* written by Waldemar Celes Filho, Roberto Ierusalimschy and
|
||||
* Luiz Henrique de Figueiredo at TeCGraf, PUC-Rio.
|
||||
*
|
||||
* This implementation contains no third-party code.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
939
lua.stx
939
lua.stx
@@ -1,939 +0,0 @@
|
||||
%{
|
||||
/*
|
||||
** $Id: lua.stx,v 1.32 1998/01/12 13:00:51 roberto Exp roberto $
|
||||
** Syntax analizer and code generator
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
int luaY_parse (void);
|
||||
|
||||
|
||||
#define MES_LIM(x) "(limit=" x ")"
|
||||
|
||||
|
||||
/* size of a "normal" jump instruction: OpCode + 1 byte */
|
||||
#define JMPSIZE 2
|
||||
|
||||
/* maximum number of local variables */
|
||||
#define MAXLOCALS 32
|
||||
#define SMAXLOCALS "32"
|
||||
|
||||
#define MINGLOBAL (MAXLOCALS+1)
|
||||
|
||||
/* maximum number of variables in a multiple assignment */
|
||||
#define MAXVAR 32
|
||||
#define SMAXVAR "32"
|
||||
|
||||
/* maximum number of nested functions */
|
||||
#define MAXSTATES 6
|
||||
#define SMAXSTATES "6"
|
||||
|
||||
/* maximum number of upvalues */
|
||||
#define MAXUPVALUES 16
|
||||
#define SMAXUPVALUES "16"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Variable descriptor:
|
||||
** if 0<n<MINGLOBAL, represents local variable indexed by (n-1);
|
||||
** if MINGLOBAL<=n, represents global variable at position (n-MINGLOBAL);
|
||||
** if n<0, indexed variable with index (-n)-1 (table on top of stack);
|
||||
** if n==0, an indexed variable (table and index on top of stack)
|
||||
** Must be long to store negative Word values.
|
||||
*/
|
||||
typedef long vardesc;
|
||||
|
||||
#define isglobal(v) (MINGLOBAL<=(v))
|
||||
#define globalindex(v) ((v)-MINGLOBAL)
|
||||
#define islocal(v) (0<(v) && (v)<MINGLOBAL)
|
||||
#define localindex(v) ((v)-1)
|
||||
#define isdot(v) (v<0)
|
||||
#define dotindex(v) ((-(v))-1)
|
||||
|
||||
/* state needed to generate code for a given function */
|
||||
typedef struct FuncState {
|
||||
TProtoFunc *f; /* current function header */
|
||||
int pc; /* next position to code */
|
||||
TaggedString *localvar[MAXLOCALS]; /* store local variable names */
|
||||
int stacksize; /* number of values on activation register */
|
||||
int maxstacksize; /* maximum number of values on activation register */
|
||||
int nlocalvar; /* number of active local variables */
|
||||
int nupvalues; /* number of upvalues */
|
||||
int nvars; /* number of entries in f->locvars */
|
||||
int maxcode; /* size of f->code */
|
||||
int maxvars; /* size of f->locvars (-1 if no debug information) */
|
||||
int maxconsts; /* size of f->consts */
|
||||
vardesc varbuffer[MAXVAR]; /* variables in an assignment list */
|
||||
vardesc upvalues[MAXUPVALUES]; /* upvalues */
|
||||
} FuncState;
|
||||
|
||||
|
||||
|
||||
#define YYPURE 1
|
||||
|
||||
|
||||
void luaY_syntaxerror (char *s, char *token)
|
||||
{
|
||||
if (token[0] == 0)
|
||||
token = "<eof>";
|
||||
luaL_verror("%.100s;\n last token read: \"%.50s\" at line %d in file %.50s",
|
||||
s, token, L->lexstate->linenumber, L->mainState->f->fileName->str);
|
||||
}
|
||||
|
||||
|
||||
void luaY_error (char *s)
|
||||
{
|
||||
luaY_syntaxerror(s, luaX_lasttoken());
|
||||
}
|
||||
|
||||
|
||||
static void check_pc (int n)
|
||||
{
|
||||
FuncState *fs = L->currState;
|
||||
if (fs->pc+n > fs->maxcode)
|
||||
fs->maxcode = luaM_growvector(&fs->f->code, fs->maxcode,
|
||||
Byte, codeEM, MAX_INT);
|
||||
}
|
||||
|
||||
|
||||
static void code_byte (Byte c)
|
||||
{
|
||||
check_pc(1);
|
||||
L->currState->f->code[L->currState->pc++] = c;
|
||||
}
|
||||
|
||||
|
||||
static void deltastack (int delta)
|
||||
{
|
||||
FuncState *fs = L->currState;
|
||||
fs->stacksize += delta;
|
||||
if (fs->stacksize > fs->maxstacksize) {
|
||||
if (fs->stacksize > 255)
|
||||
luaY_error("function/expression too complex");
|
||||
fs->maxstacksize = fs->stacksize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
|
||||
{
|
||||
Byte *code = L->currState->f->code;
|
||||
deltastack(delta);
|
||||
if (arg < builtin) {
|
||||
code[pc] = op+1+arg;
|
||||
return 1;
|
||||
}
|
||||
else if (arg <= 255) {
|
||||
code[pc] = op;
|
||||
code[pc+1] = arg;
|
||||
return 2;
|
||||
}
|
||||
else if (arg <= MAX_WORD) {
|
||||
code[pc] = op+1+builtin;
|
||||
code[pc+1] = arg&0xFF;
|
||||
code[pc+2] = arg>>8;
|
||||
return 3;
|
||||
}
|
||||
else luaY_error("code too long " MES_LIM("64K"));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
static int fix_opcode (int pc, OpCode op, int builtin, int arg)
|
||||
{
|
||||
FuncState *fs = L->currState;
|
||||
if (arg < builtin) { /* close space */
|
||||
luaO_memdown(fs->f->code+pc+1, fs->f->code+pc+2, fs->pc-(pc+2));
|
||||
fs->pc--;
|
||||
}
|
||||
else if (arg > 255) { /* open space */
|
||||
check_pc(1);
|
||||
luaO_memup(fs->f->code+pc+1, fs->f->code+pc, fs->pc-pc);
|
||||
fs->pc++;
|
||||
}
|
||||
return code_oparg_at(pc, op, builtin, arg, 0) - 2;
|
||||
}
|
||||
|
||||
|
||||
static void code_oparg (OpCode op, int builtin, int arg, int delta)
|
||||
{
|
||||
check_pc(3); /* maximum code size */
|
||||
L->currState->pc += code_oparg_at(L->currState->pc, op, builtin, arg, delta);
|
||||
}
|
||||
|
||||
|
||||
static void code_opcode (OpCode op, int delta)
|
||||
{
|
||||
deltastack(delta);
|
||||
code_byte(op);
|
||||
}
|
||||
|
||||
|
||||
static void code_pop (OpCode op)
|
||||
{
|
||||
code_opcode(op, -1);
|
||||
}
|
||||
|
||||
/* binary operations get 2 arguments and leave one, so they pop one */
|
||||
#define code_binop(op) code_pop(op)
|
||||
|
||||
|
||||
static void code_neutralop (OpCode op)
|
||||
{
|
||||
code_opcode(op, 0);
|
||||
}
|
||||
|
||||
/* unary operations get 1 argument and leave one, so they are neutral */
|
||||
#define code_unop(op) code_neutralop(op)
|
||||
|
||||
|
||||
static void code_constant (int c)
|
||||
{
|
||||
code_oparg(PUSHCONSTANT, 8, c, 1);
|
||||
}
|
||||
|
||||
|
||||
static int next_constant (FuncState *cs)
|
||||
{
|
||||
TProtoFunc *f = cs->f;
|
||||
if (f->nconsts >= cs->maxconsts) {
|
||||
cs->maxconsts = luaM_growvector(&f->consts, cs->maxconsts, TObject,
|
||||
constantEM, MAX_WORD);
|
||||
}
|
||||
return f->nconsts++;
|
||||
}
|
||||
|
||||
|
||||
static int string_constant (TaggedString *s, FuncState *cs)
|
||||
{
|
||||
TProtoFunc *f = cs->f;
|
||||
int c = s->constindex;
|
||||
if (!(c < f->nconsts &&
|
||||
ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
|
||||
c = next_constant(cs);
|
||||
ttype(&f->consts[c]) = LUA_T_STRING;
|
||||
tsvalue(&f->consts[c]) = s;
|
||||
s->constindex = c; /* hint for next time */
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
static void code_string (TaggedString *s)
|
||||
{
|
||||
code_constant(string_constant(s, L->currState));
|
||||
}
|
||||
|
||||
|
||||
#define LIM 20
|
||||
static int real_constant (real r)
|
||||
{
|
||||
/* check whether 'r' has appeared within the last LIM entries */
|
||||
TObject *cnt = L->currState->f->consts;
|
||||
int c = L->currState->f->nconsts;
|
||||
int lim = c < LIM ? 0 : c-LIM;
|
||||
while (--c >= lim) {
|
||||
if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
|
||||
return c;
|
||||
}
|
||||
/* not found; create a luaM_new entry */
|
||||
c = next_constant(L->currState);
|
||||
cnt = L->currState->f->consts; /* 'next_constant' may reallocate this vector */
|
||||
ttype(&cnt[c]) = LUA_T_NUMBER;
|
||||
nvalue(&cnt[c]) = r;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
static void code_number (real f)
|
||||
{
|
||||
int i;
|
||||
if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(int)f) == f)
|
||||
code_oparg(PUSHNUMBER, 3, i, 1); /* f has an (short) integer value */
|
||||
else
|
||||
code_constant(real_constant(f));
|
||||
}
|
||||
|
||||
|
||||
static void flush_record (int n)
|
||||
{
|
||||
if (n > 0)
|
||||
code_oparg(SETMAP, 1, n-1, -2*n);
|
||||
}
|
||||
|
||||
static void flush_list (int m, int n)
|
||||
{
|
||||
if (n == 0) return;
|
||||
code_oparg(SETLIST, 1, m, -n);
|
||||
code_byte(n);
|
||||
}
|
||||
|
||||
|
||||
static void luaI_registerlocalvar (TaggedString *varname, int line)
|
||||
{
|
||||
FuncState *fs = L->currState;
|
||||
if (fs->maxvars != -1) { /* debug information? */
|
||||
if (fs->nvars >= fs->maxvars)
|
||||
fs->maxvars = luaM_growvector(&fs->f->locvars, fs->maxvars,
|
||||
LocVar, "", MAX_WORD);
|
||||
fs->f->locvars[fs->nvars].varname = varname;
|
||||
fs->f->locvars[fs->nvars].line = line;
|
||||
fs->nvars++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaI_unregisterlocalvar (int line)
|
||||
{
|
||||
luaI_registerlocalvar(NULL, line);
|
||||
}
|
||||
|
||||
|
||||
static void store_localvar (TaggedString *name, int n)
|
||||
{
|
||||
if (L->currState->nlocalvar+n < MAXLOCALS)
|
||||
L->currState->localvar[L->currState->nlocalvar+n] = name;
|
||||
else
|
||||
luaY_error("too many local variables " MES_LIM(SMAXLOCALS));
|
||||
luaI_registerlocalvar(name, L->lexstate->linenumber);
|
||||
}
|
||||
|
||||
static void add_localvar (TaggedString *name)
|
||||
{
|
||||
store_localvar(name, 0);
|
||||
L->currState->nlocalvar++;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** dotted variables <a.x> must be stored like regular indexed vars <a["x"]>
|
||||
*/
|
||||
static vardesc var2store (vardesc var)
|
||||
{
|
||||
if (isdot(var)) {
|
||||
code_constant(dotindex(var));
|
||||
var = 0;
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
|
||||
static void add_varbuffer (vardesc var, int n)
|
||||
{
|
||||
if (n >= MAXVAR)
|
||||
luaY_error("variable buffer overflow " MES_LIM(SMAXVAR));
|
||||
L->currState->varbuffer[n] = var2store(var);
|
||||
}
|
||||
|
||||
|
||||
static int aux_localname (TaggedString *n, FuncState *st)
|
||||
{
|
||||
int i;
|
||||
for (i=st->nlocalvar-1; i >= 0; i--)
|
||||
if (n == st->localvar[i]) return i; /* local var index */
|
||||
return -1; /* not found */
|
||||
}
|
||||
|
||||
|
||||
static vardesc singlevar (TaggedString *n, FuncState *st)
|
||||
{
|
||||
int i = aux_localname(n, st);
|
||||
if (i == -1) { /* check shadowing */
|
||||
int l;
|
||||
for (l=1; l<=(st-L->mainState); l++)
|
||||
if (aux_localname(n, st-l) >= 0)
|
||||
luaY_syntaxerror("cannot access a variable in outer scope", n->str);
|
||||
return string_constant(n, st)+MINGLOBAL; /* global value */
|
||||
}
|
||||
else return i+1; /* local value */
|
||||
}
|
||||
|
||||
|
||||
static int indexupvalue (TaggedString *n)
|
||||
{
|
||||
vardesc v = singlevar(n, L->currState-1);
|
||||
int i;
|
||||
for (i=0; i<L->currState->nupvalues; i++) {
|
||||
if (L->currState->upvalues[i] == v)
|
||||
return i;
|
||||
}
|
||||
/* new one */
|
||||
if (++(L->currState->nupvalues) > MAXUPVALUES)
|
||||
luaY_error("too many upvalues in a single function " MES_LIM(SMAXUPVALUES));
|
||||
L->currState->upvalues[i] = v; /* i = L->currState->nupvalues - 1 */
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
static void pushupvalue (TaggedString *n)
|
||||
{
|
||||
int i;
|
||||
if (L->currState == L->mainState)
|
||||
luaY_error("cannot access upvalue in main");
|
||||
if (aux_localname(n, L->currState) >= 0)
|
||||
luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
|
||||
i = indexupvalue(n);
|
||||
code_oparg(PUSHUPVALUE, 2, i, 1);
|
||||
}
|
||||
|
||||
|
||||
void luaY_codedebugline (int line)
|
||||
{
|
||||
if (lua_debug && line != L->lexstate->lastline) {
|
||||
code_oparg(SETLINE, 0, line, 0);
|
||||
L->lexstate->lastline = line;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjuststack (int n)
|
||||
{
|
||||
if (n > 0)
|
||||
code_oparg(POP, 2, n-1, -n);
|
||||
else if (n < 0)
|
||||
code_oparg(PUSHNIL, 1, (-n)-1, -n);
|
||||
}
|
||||
|
||||
|
||||
static long adjust_functioncall (long exp, int nresults)
|
||||
{
|
||||
if (exp <= 0)
|
||||
return -exp; /* exp is -list length */
|
||||
else {
|
||||
int temp = L->currState->f->code[exp];
|
||||
int nparams = L->currState->f->code[exp-1];
|
||||
exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
|
||||
L->currState->f->code[exp] = nparams;
|
||||
if (nresults != MULT_RET)
|
||||
deltastack(nresults);
|
||||
deltastack(-(nparams+1));
|
||||
return temp+nresults;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjust_mult_assign (int vars, long exps)
|
||||
{
|
||||
if (exps > 0) { /* must correct function call */
|
||||
int diff = L->currState->f->code[exps] - vars;
|
||||
if (diff < 0)
|
||||
adjust_functioncall(exps, -diff);
|
||||
else {
|
||||
adjust_functioncall(exps, 0);
|
||||
adjuststack(diff);
|
||||
}
|
||||
}
|
||||
else adjuststack((-exps)-vars);
|
||||
}
|
||||
|
||||
|
||||
static void code_args (int nparams, int dots)
|
||||
{
|
||||
L->currState->nlocalvar += nparams; /* "self" may already be there */
|
||||
nparams = L->currState->nlocalvar;
|
||||
if (!dots) {
|
||||
L->currState->f->code[1] = nparams; /* fill-in arg information */
|
||||
deltastack(nparams);
|
||||
}
|
||||
else {
|
||||
L->currState->f->code[1] = nparams+ZEROVARARG;
|
||||
deltastack(nparams+1);
|
||||
add_localvar(luaS_new("arg"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void lua_pushvar (vardesc var)
|
||||
{
|
||||
if (isglobal(var))
|
||||
code_oparg(GETGLOBAL, 8, globalindex(var), 1);
|
||||
else if (islocal(var))
|
||||
code_oparg(PUSHLOCAL, 8, localindex(var), 1);
|
||||
else if (isdot(var))
|
||||
code_oparg(GETDOTTED, 8, dotindex(var), 0);
|
||||
else
|
||||
code_pop(GETTABLE);
|
||||
}
|
||||
|
||||
|
||||
static void storevar (vardesc var)
|
||||
{
|
||||
if (var == 0) /* indexed var */
|
||||
code_opcode(SETTABLE0, -3);
|
||||
else if (isglobal(var))
|
||||
code_oparg(SETGLOBAL, 8, globalindex(var), -1);
|
||||
else /* local var */
|
||||
code_oparg(SETLOCAL, 8, localindex(var), -1);
|
||||
}
|
||||
|
||||
|
||||
/* returns how many elements are left as 'garbage' on the stack */
|
||||
static int lua_codestore (int i, int left)
|
||||
{
|
||||
if (L->currState->varbuffer[i] != 0 || /* global or local var or */
|
||||
left+i == 0) { /* indexed var without values in between */
|
||||
storevar(L->currState->varbuffer[i]);
|
||||
return left;
|
||||
}
|
||||
else { /* indexed var with values in between*/
|
||||
code_oparg(SETTABLE, 0, left+i, -1);
|
||||
return left+2; /* table/index are not popped, since they are not on top */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int fix_jump (int pc, OpCode op, int n)
|
||||
{
|
||||
/* jump is relative to position following jump instruction */
|
||||
return fix_opcode(pc, op, 0, n-(pc+JMPSIZE));
|
||||
}
|
||||
|
||||
|
||||
static void fix_upjmp (OpCode op, int pos)
|
||||
{
|
||||
int delta = L->currState->pc+JMPSIZE - pos; /* jump is relative */
|
||||
if (delta > 255) delta++;
|
||||
code_oparg(op, 0, delta, 0);
|
||||
}
|
||||
|
||||
|
||||
static void codeIf (int thenAdd, int elseAdd)
|
||||
{
|
||||
int elseinit = elseAdd+JMPSIZE;
|
||||
if (L->currState->pc == elseinit) { /* no else part */
|
||||
L->currState->pc -= JMPSIZE;
|
||||
elseinit = L->currState->pc;
|
||||
}
|
||||
else
|
||||
elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
|
||||
fix_jump(thenAdd, IFFJMP, elseinit);
|
||||
}
|
||||
|
||||
|
||||
static void code_shortcircuit (int pc, OpCode op)
|
||||
{
|
||||
fix_jump(pc, op, L->currState->pc);
|
||||
}
|
||||
|
||||
|
||||
static void codereturn (void)
|
||||
{
|
||||
code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
|
||||
L->currState->stacksize = L->currState->nlocalvar;
|
||||
}
|
||||
|
||||
|
||||
static void func_onstack (TProtoFunc *f)
|
||||
{
|
||||
int i;
|
||||
int nupvalues = (L->currState+1)->nupvalues;
|
||||
int c = next_constant(L->currState);
|
||||
ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
|
||||
L->currState->f->consts[c].value.tf = (L->currState+1)->f;
|
||||
if (nupvalues == 0)
|
||||
code_constant(c);
|
||||
else {
|
||||
for (i=0; i<nupvalues; i++)
|
||||
lua_pushvar((L->currState+1)->upvalues[i]);
|
||||
code_constant(c);
|
||||
code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void init_state (TaggedString *filename)
|
||||
{
|
||||
TProtoFunc *f = luaF_newproto();
|
||||
FuncState *fs = L->currState;
|
||||
fs->stacksize = 0;
|
||||
fs->maxstacksize = 0;
|
||||
fs->nlocalvar = 0;
|
||||
fs->nupvalues = 0;
|
||||
fs->f = f;
|
||||
f->fileName = filename;
|
||||
fs->pc = 0;
|
||||
fs->maxcode = 0;
|
||||
f->code = NULL;
|
||||
fs->maxconsts = 0;
|
||||
if (lua_debug) {
|
||||
fs->nvars = 0;
|
||||
fs->maxvars = 0;
|
||||
}
|
||||
else
|
||||
fs->maxvars = -1; /* flag no debug information */
|
||||
code_byte(0); /* to be filled with stacksize */
|
||||
code_byte(0); /* to be filled with arg information */
|
||||
L->lexstate->lastline = 0; /* invalidate it */
|
||||
}
|
||||
|
||||
|
||||
static void init_func (void)
|
||||
{
|
||||
if (L->currState-L->mainState >= MAXSTATES-1)
|
||||
luaY_error("too many nested functions " MES_LIM(SMAXSTATES));
|
||||
L->currState++;
|
||||
init_state(L->mainState->f->fileName);
|
||||
luaY_codedebugline(L->lexstate->linenumber);
|
||||
L->currState->f->lineDefined = L->lexstate->linenumber;
|
||||
}
|
||||
|
||||
static TProtoFunc *close_func (void)
|
||||
{
|
||||
TProtoFunc *f = L->currState->f;
|
||||
code_neutralop(ENDCODE);
|
||||
f->code[0] = L->currState->maxstacksize;
|
||||
f->code = luaM_reallocvector(f->code, L->currState->pc, Byte);
|
||||
f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
|
||||
if (L->currState->maxvars != -1) { /* debug information? */
|
||||
luaI_registerlocalvar(NULL, -1); /* flag end of vector */
|
||||
f->locvars = luaM_reallocvector(f->locvars, L->currState->nvars, LocVar);
|
||||
}
|
||||
L->currState--;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Parse Lua code.
|
||||
*/
|
||||
TProtoFunc *luaY_parser (ZIO *z)
|
||||
{
|
||||
struct LexState lexstate;
|
||||
FuncState state[MAXSTATES];
|
||||
L->currState = L->mainState = &state[0];
|
||||
L->lexstate = &lexstate;
|
||||
luaX_setinput(z);
|
||||
init_state(luaS_new(zname(z)));
|
||||
if (luaY_parse()) lua_error("parse error");
|
||||
return close_func();
|
||||
}
|
||||
|
||||
|
||||
%}
|
||||
|
||||
|
||||
%union
|
||||
{
|
||||
int vInt;
|
||||
real vReal;
|
||||
char *pChar;
|
||||
long vLong;
|
||||
TaggedString *pTStr;
|
||||
TProtoFunc *pFunc;
|
||||
}
|
||||
|
||||
%start chunk
|
||||
|
||||
%token WRONGTOKEN
|
||||
%token NIL
|
||||
%token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
|
||||
%token RETURN
|
||||
%token LOCAL
|
||||
%token FUNCTION
|
||||
%token DOTS
|
||||
%token <vReal> NUMBER
|
||||
%token <pTStr> NAME STRING
|
||||
|
||||
%type <vInt> SaveWord, cond, GetPC, SaveWordPop, SaveWordPush
|
||||
%type <vLong> exprlist, exprlist1 /* if > 0, points to function return
|
||||
counter (which has list length); if <= 0, -list lenght */
|
||||
%type <vLong> functioncall, expr, sexp /* if != 0, points to function return
|
||||
counter */
|
||||
%type <vInt> varlist1, funcParams, funcvalue
|
||||
%type <vInt> fieldlist, localnamelist, decinit
|
||||
%type <vInt> ffieldlist1, lfieldlist1, ffieldlist, lfieldlist, part
|
||||
%type <vLong> var, varname, funcname /* vardesc */
|
||||
|
||||
|
||||
%left AND OR
|
||||
%left EQ NE '>' '<' LE GE
|
||||
%left CONC
|
||||
%left '+' '-'
|
||||
%left '*' '/'
|
||||
%left UNARY NOT
|
||||
%right '^'
|
||||
|
||||
|
||||
%% /* beginning of rules section */
|
||||
|
||||
|
||||
chunk : statlist ret ;
|
||||
|
||||
statlist : /* empty */
|
||||
| statlist stat sc
|
||||
;
|
||||
|
||||
sc : /* empty */ | ';' ;
|
||||
|
||||
stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
|
||||
|
||||
| DO block END
|
||||
|
||||
| WHILE GetPC cond DO block END
|
||||
{{
|
||||
FuncState *fs = L->currState;
|
||||
int expsize = $3-$2;
|
||||
int newpos = $2+JMPSIZE;
|
||||
check_pc(expsize);
|
||||
memcpy(fs->f->code+fs->pc, fs->f->code+$2, expsize);
|
||||
luaO_memdown(fs->f->code+$2, fs->f->code+$3, fs->pc-$2);
|
||||
newpos += fix_jump($2, JMP, fs->pc-expsize);
|
||||
fix_upjmp(IFTUPJMP, newpos);
|
||||
}}
|
||||
|
||||
| REPEAT GetPC block UNTIL expr1
|
||||
{
|
||||
fix_upjmp(IFFUPJMP, $2);
|
||||
deltastack(-1); /* pops condition */
|
||||
}
|
||||
|
||||
| varlist1 '=' exprlist1
|
||||
{{
|
||||
int i;
|
||||
int left = 0;
|
||||
adjust_mult_assign($1, $3);
|
||||
for (i=$1-1; i>=0; i--)
|
||||
left = lua_codestore(i, left);
|
||||
adjuststack(left); /* remove eventual 'garbage' left on stack */
|
||||
}}
|
||||
|
||||
| functioncall { adjust_functioncall($1, 0); }
|
||||
|
||||
| LOCAL localnamelist decinit
|
||||
{
|
||||
L->currState->nlocalvar += $2;
|
||||
adjust_mult_assign($2, $3);
|
||||
}
|
||||
|
||||
| FUNCTION funcname body { storevar($2); }
|
||||
;
|
||||
|
||||
block : {$<vInt>$ = L->currState->nlocalvar;} chunk
|
||||
{
|
||||
adjuststack(L->currState->nlocalvar - $<vInt>1);
|
||||
for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
|
||||
luaI_unregisterlocalvar(L->lexstate->linenumber);
|
||||
}
|
||||
;
|
||||
|
||||
funcname : varname { $$ = $1; init_func(); }
|
||||
| fvarname '.' fname
|
||||
{
|
||||
$$ = 0; /* flag indexed variable */
|
||||
init_func();
|
||||
}
|
||||
| fvarname ':' fname
|
||||
{
|
||||
$$ = 0; /* flag indexed variable */
|
||||
init_func();
|
||||
add_localvar(luaS_new("self"));
|
||||
}
|
||||
;
|
||||
|
||||
fvarname : varname { lua_pushvar($1); } ;
|
||||
|
||||
fname : NAME { code_string($1); } ;
|
||||
|
||||
body : '(' parlist ')' chunk END { func_onstack(close_func()); } ;
|
||||
|
||||
elsepart : /* empty */
|
||||
| ELSE block
|
||||
| ELSEIF cond THEN block SaveWord elsepart { codeIf($2, $5); }
|
||||
;
|
||||
|
||||
ret : /* empty */
|
||||
| RETURN exprlist sc
|
||||
{
|
||||
adjust_functioncall($2, MULT_RET);
|
||||
codereturn();
|
||||
}
|
||||
;
|
||||
|
||||
GetPC : /* empty */ { $$ = L->currState->pc; } ;
|
||||
|
||||
SaveWord : /* empty */
|
||||
{ $$ = L->currState->pc;
|
||||
check_pc(JMPSIZE);
|
||||
L->currState->pc += JMPSIZE; /* open space */
|
||||
}
|
||||
;
|
||||
|
||||
SaveWordPop : SaveWord { $$ = $1; deltastack(-1); /* pop condition */ } ;
|
||||
|
||||
SaveWordPush : SaveWord { $$ = $1; deltastack(1); /* push a value */ } ;
|
||||
|
||||
cond : expr1 SaveWordPop { $$ = $2; } ;
|
||||
|
||||
expr1 : expr { adjust_functioncall($1, 1); } ;
|
||||
|
||||
expr : '(' expr ')' { $$ = $2; }
|
||||
| expr1 EQ expr1 { code_binop(EQOP); $$ = 0; }
|
||||
| expr1 '<' expr1 { code_binop(LTOP); $$ = 0; }
|
||||
| expr1 '>' expr1 { code_binop(GTOP); $$ = 0; }
|
||||
| expr1 NE expr1 { code_binop(NEQOP); $$ = 0; }
|
||||
| expr1 LE expr1 { code_binop(LEOP); $$ = 0; }
|
||||
| expr1 GE expr1 { code_binop(GEOP); $$ = 0; }
|
||||
| expr1 '+' expr1 { code_binop(ADDOP); $$ = 0; }
|
||||
| expr1 '-' expr1 { code_binop(SUBOP); $$ = 0; }
|
||||
| expr1 '*' expr1 { code_binop(MULTOP); $$ = 0; }
|
||||
| expr1 '/' expr1 { code_binop(DIVOP); $$ = 0; }
|
||||
| expr1 '^' expr1 { code_binop(POWOP); $$ = 0; }
|
||||
| expr1 CONC expr1 { code_binop(CONCOP); $$ = 0; }
|
||||
| '-' expr1 %prec UNARY { code_unop(MINUSOP); $$ = 0;}
|
||||
| NOT expr1 { code_unop(NOTOP); $$ = 0;}
|
||||
| sexp { $$ = $1; /* simple expressions */ }
|
||||
| table { $$ = 0; }
|
||||
| NUMBER { code_number($1); $$ = 0; }
|
||||
| STRING { code_string($1); $$ = 0; }
|
||||
| NIL { adjuststack(-1); $$ = 0; }
|
||||
| FUNCTION { init_func(); } body { $$ = 0; }
|
||||
| expr1 AND SaveWordPop expr1 { code_shortcircuit($3, ONFJMP); $$ = 0; }
|
||||
| expr1 OR SaveWordPop expr1 { code_shortcircuit($3, ONTJMP); $$ = 0; }
|
||||
;
|
||||
|
||||
sexp1 : sexp { adjust_functioncall($1, 1); } ;
|
||||
|
||||
sexp : var { lua_pushvar($1); $$ = 0; }
|
||||
| '%' NAME { pushupvalue($2); $$ = 0; }
|
||||
| functioncall { $$ = $1; }
|
||||
;
|
||||
|
||||
var : varname { $$ = $1; }
|
||||
| sexp1 '[' expr1 ']' { $$ = 0; } /* indexed variable */
|
||||
| sexp1 '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
|
||||
;
|
||||
|
||||
varname : NAME { $$ = singlevar($1, L->currState); } ;
|
||||
|
||||
table : '{' SaveWordPush fieldlist '}' { fix_opcode($2, CREATEARRAY, 2, $3); } ;
|
||||
|
||||
functioncall : funcvalue funcParams
|
||||
{
|
||||
code_byte(0); /* save space for opcode */
|
||||
code_byte($1+$2); /* number of parameters */
|
||||
$$ = L->currState->pc;
|
||||
code_byte(0); /* must be adjusted by other rules */
|
||||
}
|
||||
;
|
||||
|
||||
funcvalue : sexp1 { $$ = 0; }
|
||||
| sexp1 ':' NAME
|
||||
{
|
||||
code_oparg(PUSHSELF, 8, string_constant($3, L->currState), 1);
|
||||
$$ = 1;
|
||||
}
|
||||
;
|
||||
|
||||
funcParams : '(' exprlist ')' { $$ = adjust_functioncall($2, 1); }
|
||||
| table { $$ = 1; }
|
||||
| STRING { code_string($1); $$ = 1; }
|
||||
;
|
||||
|
||||
exprlist : /* empty */ { $$ = 0; }
|
||||
| exprlist1 { $$ = $1; }
|
||||
;
|
||||
|
||||
exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
|
||||
| exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
|
||||
{
|
||||
if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
|
||||
else {
|
||||
L->currState->f->code[$4] = $<vLong>3; /* store list length */
|
||||
$$ = $4;
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
parlist : /* empty */ { code_args(0, 0); }
|
||||
| DOTS { code_args(0, 1); }
|
||||
| localnamelist { code_args($1, 0); }
|
||||
| localnamelist ',' DOTS { code_args($1, 1); }
|
||||
;
|
||||
|
||||
fieldlist : part { $$ = abs($1); }
|
||||
| part ';' part
|
||||
{
|
||||
if ($1*$3 > 0) /* repeated parts? */
|
||||
luaY_error("invalid constructor syntax");
|
||||
$$ = abs($1)+abs($3);
|
||||
}
|
||||
;
|
||||
|
||||
part : /* empty */ { $$ = 0; }
|
||||
| ffieldlist { $$ = $1; }
|
||||
| lfieldlist { $$ = $1; }
|
||||
;
|
||||
|
||||
lastcomma : /* empty */ | ',' ;
|
||||
|
||||
ffieldlist : ffieldlist1 lastcomma
|
||||
{
|
||||
flush_record($1%RFIELDS_PER_FLUSH);
|
||||
$$ = -$1; /* negative signals a "record" part */
|
||||
}
|
||||
;
|
||||
|
||||
lfieldlist : lfieldlist1 lastcomma
|
||||
{
|
||||
flush_list($1/LFIELDS_PER_FLUSH, $1%LFIELDS_PER_FLUSH);
|
||||
$$ = $1;
|
||||
}
|
||||
;
|
||||
|
||||
ffieldlist1 : ffield {$$=1;}
|
||||
| ffieldlist1 ',' ffield
|
||||
{
|
||||
$$=$1+1;
|
||||
if ($$%RFIELDS_PER_FLUSH == 0)
|
||||
flush_record(RFIELDS_PER_FLUSH);
|
||||
}
|
||||
;
|
||||
|
||||
ffield : ffieldkey '=' expr1 ;
|
||||
|
||||
ffieldkey : '[' expr1 ']'
|
||||
| fname
|
||||
;
|
||||
|
||||
lfieldlist1 : expr1 {$$=1;}
|
||||
| lfieldlist1 ',' expr1
|
||||
{
|
||||
$$=$1+1;
|
||||
if ($$%LFIELDS_PER_FLUSH == 0)
|
||||
flush_list($$/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
|
||||
}
|
||||
;
|
||||
|
||||
varlist1 : var { $$ = 1; add_varbuffer($1, 0); }
|
||||
| varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; }
|
||||
;
|
||||
|
||||
localnamelist : NAME {store_localvar($1, 0); $$ = 1;}
|
||||
| localnamelist ',' NAME { store_localvar($3, $1); $$ = $1+1; }
|
||||
;
|
||||
|
||||
decinit : /* empty */ { $$ = 0; }
|
||||
| '=' exprlist1 { $$ = $2; }
|
||||
;
|
||||
|
||||
%%
|
||||
|
||||
45
luadebug.h
45
luadebug.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** Debuging API
|
||||
** $Id: luadebug.h,v 1.19 2001/03/07 18:09:25 roberto Exp roberto $
|
||||
** Debugging API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -11,23 +11,36 @@
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
typedef lua_Object lua_Function;
|
||||
typedef struct lua_Debug lua_Debug; /* activation record */
|
||||
typedef struct lua_Localvar lua_Localvar;
|
||||
|
||||
typedef void (*lua_LHFunction) (int line);
|
||||
typedef void (*lua_CHFunction) (lua_Function func, char *file, int line);
|
||||
|
||||
lua_Function lua_stackedfunction (int level);
|
||||
void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
|
||||
int lua_currentline (lua_Function func);
|
||||
char *lua_getobjname (lua_Object o, char **name);
|
||||
|
||||
lua_Object lua_getlocal (lua_Function func, int local_number, char **name);
|
||||
int lua_setlocal (lua_Function func, int local_number);
|
||||
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
||||
|
||||
|
||||
extern lua_LHFunction lua_linehook;
|
||||
extern lua_CHFunction lua_callhook;
|
||||
extern int lua_debug;
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
|
||||
LUA_API int lua_getinfo (lua_State *L, const lua_char *what, lua_Debug *ar);
|
||||
LUA_API const lua_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
|
||||
LUA_API const lua_char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
|
||||
|
||||
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
|
||||
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
|
||||
|
||||
|
||||
#define LUA_IDSIZE 60
|
||||
|
||||
struct lua_Debug {
|
||||
const lua_char *event; /* `call', `return' */
|
||||
int currentline; /* (l) */
|
||||
const lua_char *name; /* (n) */
|
||||
const lua_char *namewhat; /* (n) `global', `tag method', `local', `field' */
|
||||
int nups; /* (u) number of upvalues */
|
||||
int linedefined; /* (S) */
|
||||
const lua_char *what; /* (S) `Lua' function, `C' function, Lua `main' */
|
||||
const lua_char *source; /* (S) */
|
||||
lua_char short_src[LUA_IDSIZE]; /* (S) */
|
||||
/* private part */
|
||||
struct CallInfo *_ci; /* active function */
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
34
lualib.h
34
lualib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lualib.h,v 1.2 1997/11/26 18:53:45 roberto Exp roberto $
|
||||
** $Id: lualib.h,v 1.20 2001/03/06 20:09:38 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,24 +11,18 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
void lua_iolibopen (void);
|
||||
void lua_strlibopen (void);
|
||||
void lua_mathlibopen (void);
|
||||
|
||||
|
||||
|
||||
|
||||
/* To keep compatibility with old versions */
|
||||
|
||||
#define iolib_open lua_iolibopen
|
||||
#define strlib_open lua_strlibopen
|
||||
#define mathlib_open lua_mathlibopen
|
||||
|
||||
|
||||
|
||||
/* auxiliar functions (private) */
|
||||
|
||||
int luaI_singlematch (int c, char *p, char **ep);
|
||||
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API extern
|
||||
#endif
|
||||
|
||||
|
||||
#define LUA_ALERT "_ALERT"
|
||||
|
||||
LUALIB_API int lua_baselibopen (lua_State *L);
|
||||
LUALIB_API int lua_iolibopen (lua_State *L);
|
||||
LUALIB_API int lua_strlibopen (lua_State *L);
|
||||
LUALIB_API int lua_mathlibopen (lua_State *L);
|
||||
LUALIB_API int lua_dblibopen (lua_State *L);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
386
lundump.c
386
lundump.c
@@ -1,272 +1,264 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.4 1998/01/13 20:05:24 lhf Exp $
|
||||
** load bytecodes from files
|
||||
** $Id: lundump.c,v 1.36 2001/07/19 14:34:06 lhf Exp lhf $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lauxlib.h"
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstring.h"
|
||||
#include "lundump.h"
|
||||
|
||||
typedef struct {
|
||||
ZIO* Z;
|
||||
int SwapNumber;
|
||||
int LoadFloat;
|
||||
} Sundump;
|
||||
#define LoadByte ezgetc
|
||||
#define LoadShort (short) LoadInt
|
||||
|
||||
static void unexpectedEOZ(ZIO* Z)
|
||||
static const l_char* ZNAME (ZIO* Z)
|
||||
{
|
||||
luaL_verror("unexpected end of binary file %s",Z->name);
|
||||
const l_char* s=zname(Z);
|
||||
return (*s==l_c('@')) ? s+1 : s;
|
||||
}
|
||||
|
||||
static int ezgetc(ZIO* Z)
|
||||
static void unexpectedEOZ (lua_State* L, ZIO* Z)
|
||||
{
|
||||
luaO_verror(L,l_s("unexpected end of file in `%.99s'"),ZNAME(Z));
|
||||
}
|
||||
|
||||
static int ezgetc (lua_State* L, ZIO* Z)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==EOZ) unexpectedEOZ(Z);
|
||||
if (c==EOZ) unexpectedEOZ(L,Z);
|
||||
return c;
|
||||
}
|
||||
|
||||
static int ezread(ZIO* Z, void* b, int n)
|
||||
static void ezread (lua_State* L, ZIO* Z, void* b, int n)
|
||||
{
|
||||
int r=zread(Z,b,n);
|
||||
if (r!=0) unexpectedEOZ(Z);
|
||||
return r;
|
||||
if (r!=0) unexpectedEOZ(L,Z);
|
||||
}
|
||||
|
||||
static int LoadWord(ZIO* Z)
|
||||
static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
|
||||
{
|
||||
int hi=ezgetc(Z);
|
||||
int lo=ezgetc(Z);
|
||||
return (hi<<8)|lo;
|
||||
if (swap)
|
||||
{
|
||||
l_char *p=(l_char*) b+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(l_char)ezgetc(L,Z);
|
||||
}
|
||||
else
|
||||
ezread(L,Z,b,size);
|
||||
}
|
||||
|
||||
static void* LoadBlock(int size, ZIO* Z)
|
||||
static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
|
||||
{
|
||||
void* b=luaM_malloc(size);
|
||||
ezread(Z,b,size);
|
||||
return b;
|
||||
if (swap)
|
||||
{
|
||||
l_char *q=(l_char*) b;
|
||||
while (m--)
|
||||
{
|
||||
l_char *p=q+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(l_char)ezgetc(L,Z);
|
||||
q+=size;
|
||||
}
|
||||
}
|
||||
else
|
||||
ezread(L,Z,b,m*size);
|
||||
}
|
||||
|
||||
static int LoadSize(ZIO* Z)
|
||||
static int LoadInt (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
int hi=LoadWord(Z);
|
||||
int lo=LoadWord(Z);
|
||||
int s=(hi<<16)|lo;
|
||||
if (hi!=0 && s==lo)
|
||||
luaL_verror("code too long (%ld bytes)",(hi<<16)|(long)lo);
|
||||
return s;
|
||||
int x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
return x;
|
||||
}
|
||||
|
||||
static char* LoadString(ZIO* Z)
|
||||
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
int size=LoadWord(Z);
|
||||
size_t x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
return x;
|
||||
}
|
||||
|
||||
static lua_Number LoadNumber (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
lua_Number x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
return x;
|
||||
}
|
||||
|
||||
static TString* LoadString (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
size_t size=LoadSize(L,Z,swap);
|
||||
if (size==0)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
char* b=luaL_openspace(size);
|
||||
ezread(Z,b,size);
|
||||
return b;
|
||||
l_char* s=luaO_openspace(L,size,l_char);
|
||||
LoadBlock(L,s,size,Z,0);
|
||||
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
|
||||
}
|
||||
}
|
||||
|
||||
static TaggedString* LoadTString(ZIO* Z)
|
||||
static void LoadCode (lua_State* L, Proto* f, ZIO* Z, int swap)
|
||||
{
|
||||
char* s=LoadString(Z);
|
||||
return (s==NULL) ? NULL : luaS_new(s);
|
||||
int size=LoadInt(L,Z,swap);
|
||||
f->code=luaM_newvector(L,size,Instruction);
|
||||
f->sizecode=size;
|
||||
LoadVector(L,f->code,size,sizeof(*f->code),Z,swap);
|
||||
}
|
||||
|
||||
static void SwapFloat(float* f)
|
||||
static void LoadLocals (lua_State* L, Proto* f, ZIO* Z, int swap)
|
||||
{
|
||||
Byte* p=(Byte*)f;
|
||||
Byte* q=p+sizeof(float)-1;
|
||||
Byte t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
}
|
||||
|
||||
static void SwapDouble(double* f)
|
||||
{
|
||||
Byte* p=(Byte*)f;
|
||||
Byte* q=p+sizeof(double)-1;
|
||||
Byte t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
}
|
||||
|
||||
static real LoadNumber(Sundump* S)
|
||||
{
|
||||
if (S->LoadFloat)
|
||||
{
|
||||
float f;
|
||||
ezread(S->Z,&f,sizeof(f));
|
||||
if (S->SwapNumber) SwapFloat(&f);
|
||||
return f;
|
||||
}
|
||||
else
|
||||
{
|
||||
double f;
|
||||
ezread(S->Z,&f,sizeof(f));
|
||||
if (S->SwapNumber) SwapDouble(&f);
|
||||
return f;
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadLocals(TProtoFunc* tf, ZIO* Z)
|
||||
{
|
||||
int i,n=LoadWord(Z);
|
||||
if (n==0) return;
|
||||
tf->locvars=luaM_newvector(n+1,LocVar);
|
||||
int i,n;
|
||||
n=LoadInt(L,Z,swap);
|
||||
f->locvars=luaM_newvector(L,n,LocVar);
|
||||
f->sizelocvars=n;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->locvars[i].line=LoadWord(Z);
|
||||
tf->locvars[i].varname=LoadTString(Z);
|
||||
f->locvars[i].varname=LoadString(L,Z,swap);
|
||||
f->locvars[i].startpc=LoadInt(L,Z,swap);
|
||||
f->locvars[i].endpc=LoadInt(L,Z,swap);
|
||||
}
|
||||
tf->locvars[i].line=-1; /* flag end of vector */
|
||||
tf->locvars[i].varname=NULL;
|
||||
}
|
||||
|
||||
static void LoadConstants(TProtoFunc* tf, Sundump* S)
|
||||
static void LoadLines (lua_State* L, Proto* f, ZIO* Z, int swap)
|
||||
{
|
||||
int i,n=LoadWord(S->Z);
|
||||
tf->nconsts=n;
|
||||
if (n==0) return;
|
||||
tf->consts=luaM_newvector(n,TObject);
|
||||
int n;
|
||||
n=LoadInt(L,Z,swap);
|
||||
f->lineinfo=luaM_newvector(L,n,int);
|
||||
f->sizelineinfo=n;
|
||||
LoadVector(L,f->lineinfo,n,sizeof(*f->lineinfo),Z,swap);
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap);
|
||||
|
||||
static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap)
|
||||
{
|
||||
int i,n;
|
||||
n=LoadInt(L,Z,swap);
|
||||
f->k=luaM_newvector(L,n,TObject);
|
||||
f->sizek=n;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
TObject* o=tf->consts+i;
|
||||
int c=ezgetc(S->Z);
|
||||
switch (c)
|
||||
TObject* o=&f->k[i];
|
||||
ttype(o)=LoadByte(L,Z);
|
||||
switch (ttype(o))
|
||||
{
|
||||
case ID_NUM:
|
||||
ttype(o)=LUA_T_NUMBER;
|
||||
nvalue(o)=LoadNumber(S);
|
||||
case LUA_TNUMBER:
|
||||
nvalue(o)=LoadNumber(L,Z,swap);
|
||||
break;
|
||||
case ID_STR:
|
||||
ttype(o)=LUA_T_STRING;
|
||||
tsvalue(o)=LoadTString(S->Z);
|
||||
case LUA_TSTRING:
|
||||
tsvalue(o)=LoadString(L,Z,swap);
|
||||
break;
|
||||
case ID_FUN:
|
||||
ttype(o)=LUA_T_PROTO;
|
||||
tfvalue(o)=NULL;
|
||||
break;
|
||||
#ifdef DEBUG
|
||||
default: /* cannot happen */
|
||||
luaL_verror("internal error in LoadConstants: "
|
||||
"bad constant #%d type=%d ('%c')\n",i,c,c);
|
||||
default:
|
||||
luaO_verror(L,l_s("bad constant type (%d) in `%.99s'"),ttype(o),ZNAME(Z));
|
||||
break;
|
||||
}
|
||||
}
|
||||
n=LoadInt(L,Z,swap);
|
||||
f->p=luaM_newvector(L,n,Proto*);
|
||||
f->sizep=n;
|
||||
for (i=0; i<n; i++) f->p[i]=LoadFunction(L,f->source,Z,swap);
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap)
|
||||
{
|
||||
Proto* f=luaF_newproto(L);
|
||||
f->source=LoadString(L,Z,swap); if (f->source==NULL) f->source=p;
|
||||
f->lineDefined=LoadInt(L,Z,swap);
|
||||
f->nupvalues=LoadShort(L,Z,swap);
|
||||
f->numparams=LoadShort(L,Z,swap);
|
||||
f->is_vararg=LoadShort(L,Z,swap);
|
||||
f->maxstacksize=LoadShort(L,Z,swap);
|
||||
LoadLocals(L,f,Z,swap);
|
||||
LoadLines(L,f,Z,swap);
|
||||
LoadConstants(L,f,Z,swap);
|
||||
LoadCode(L,f,Z,swap);
|
||||
#ifndef TRUST_BINARIES
|
||||
if (!luaG_checkcode(f)) luaO_verror(L,l_s("bad code in `%.99s'"),ZNAME(Z));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadFunction(Sundump* S);
|
||||
|
||||
static void LoadFunctions(TProtoFunc* tf, Sundump* S)
|
||||
static void LoadSignature (lua_State* L, ZIO* Z)
|
||||
{
|
||||
while (zgetc(S->Z)==ID_FUNCTION)
|
||||
{
|
||||
int i=LoadWord(S->Z);
|
||||
TProtoFunc* t=LoadFunction(S);
|
||||
TObject* o=tf->consts+i;
|
||||
tfvalue(o)=t;
|
||||
}
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadFunction(Sundump* S)
|
||||
{
|
||||
ZIO* Z=S->Z;
|
||||
TProtoFunc* tf=luaF_newproto();
|
||||
tf->lineDefined=LoadWord(Z);
|
||||
tf->fileName=LoadTString(Z);
|
||||
tf->code=LoadBlock(LoadSize(Z),Z);
|
||||
LoadConstants(tf,S);
|
||||
LoadLocals(tf,Z);
|
||||
LoadFunctions(tf,S);
|
||||
return tf;
|
||||
}
|
||||
|
||||
static void LoadSignature(ZIO* Z)
|
||||
{
|
||||
char* s=SIGNATURE;
|
||||
while (*s!=0 && ezgetc(Z)==*s)
|
||||
const l_char* s=l_s(LUA_SIGNATURE);
|
||||
while (*s!=0 && ezgetc(L,Z)==*s)
|
||||
++s;
|
||||
if (*s!=0) luaL_verror("bad signature in binary file %s",Z->name);
|
||||
if (*s!=0) luaO_verror(L,l_s("bad signature in `%.99s'"),ZNAME(Z));
|
||||
}
|
||||
|
||||
static void LoadHeader(Sundump* S)
|
||||
static void TestSize (lua_State* L, int s, const l_char* what, ZIO* Z)
|
||||
{
|
||||
ZIO* Z=S->Z;
|
||||
int version,sizeofR;
|
||||
LoadSignature(Z);
|
||||
version=ezgetc(Z);
|
||||
int r=LoadByte(L,Z);
|
||||
if (r!=s)
|
||||
luaO_verror(L,l_s("virtual machine mismatch in `%.99s':\n")
|
||||
l_s(" size of %.20s is %d but read %d"),ZNAME(Z),what,s,r);
|
||||
}
|
||||
|
||||
#define TESTSIZE(s,w) TestSize(L,s,w,Z)
|
||||
#define V(v) v/16,v%16
|
||||
|
||||
static int LoadHeader (lua_State* L, ZIO* Z)
|
||||
{
|
||||
int version,swap;
|
||||
lua_Number x=0,tx=TEST_NUMBER;
|
||||
LoadSignature(L,Z);
|
||||
version=LoadByte(L,Z);
|
||||
if (version>VERSION)
|
||||
luaL_verror(
|
||||
"binary file %s too new: version=0x%02x; expected at most 0x%02x",
|
||||
Z->name,version,VERSION);
|
||||
if (version<0x31) /* major change in 3.1 */
|
||||
luaL_verror(
|
||||
"binary file %s too old: version=0x%02x; expected at least 0x%02x",
|
||||
Z->name,version,0x31);
|
||||
sizeofR=ezgetc(Z); /* test float representation */
|
||||
if (sizeofR==sizeof(float))
|
||||
{
|
||||
float f,tf=TEST_FLOAT;
|
||||
ezread(Z,&f,sizeof(f));
|
||||
if (f!=tf)
|
||||
{
|
||||
SwapFloat(&f);
|
||||
if (f!=tf)
|
||||
luaL_verror("unknown float representation in binary file %s",Z->name);
|
||||
S->SwapNumber=1;
|
||||
}
|
||||
S->LoadFloat=1;
|
||||
}
|
||||
else if (sizeofR==sizeof(double))
|
||||
{
|
||||
double f,tf=TEST_FLOAT;
|
||||
ezread(Z,&f,sizeof(f));
|
||||
if (f!=tf)
|
||||
{
|
||||
SwapDouble(&f);
|
||||
if (f!=tf)
|
||||
luaL_verror("unknown float representation in binary file %s",Z->name);
|
||||
S->SwapNumber=1;
|
||||
}
|
||||
S->LoadFloat=0;
|
||||
}
|
||||
else
|
||||
luaL_verror(
|
||||
"floats in binary file %s have %d bytes; "
|
||||
"expected %d (float) or %d (double)",
|
||||
Z->name,sizeofR,sizeof(float),sizeof(double));
|
||||
luaO_verror(L,l_s("`%.99s' too new:\n")
|
||||
l_s(" read version %d.%d; expected at most %d.%d"),
|
||||
ZNAME(Z),V(version),V(VERSION));
|
||||
if (version<VERSION0) /* check last major change */
|
||||
luaO_verror(L,l_s("`%.99s' too old:\n")
|
||||
l_s(" read version %d.%d; expected at least %d.%d"),
|
||||
ZNAME(Z),V(version),V(VERSION));
|
||||
swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */
|
||||
TESTSIZE(sizeof(int),l_s("int"));
|
||||
TESTSIZE(sizeof(size_t), l_s("size_t"));
|
||||
TESTSIZE(sizeof(Instruction), l_s("size_t"));
|
||||
TESTSIZE(SIZE_OP, l_s("OP"));
|
||||
TESTSIZE(SIZE_A, l_s("A"));
|
||||
TESTSIZE(SIZE_B, l_s("B"));
|
||||
TESTSIZE(SIZE_C, l_s("C"));
|
||||
TESTSIZE(sizeof(lua_Number), l_s("number"));
|
||||
x=LoadNumber(L,Z,swap);
|
||||
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
|
||||
luaO_verror(L,l_s("unknown number format in `%.99s':\n")
|
||||
l_s(" read ") l_s(LUA_NUMBER_FMT) l_s("; expected ") l_s(LUA_NUMBER_FMT),
|
||||
ZNAME(Z),x,tx);
|
||||
return swap;
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadChunk(Sundump* S)
|
||||
static Proto* LoadChunk (lua_State* L, ZIO* Z)
|
||||
{
|
||||
LoadHeader(S);
|
||||
return LoadFunction(S);
|
||||
return LoadFunction(L,NULL,Z,LoadHeader(L,Z));
|
||||
}
|
||||
|
||||
/*
|
||||
** load one chunk from a file or buffer
|
||||
** return main if ok and NULL at EOF
|
||||
*/
|
||||
TProtoFunc* luaU_undump1(ZIO* Z)
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
Sundump S;
|
||||
S.Z=Z;
|
||||
S.SwapNumber=0;
|
||||
S.LoadFloat=1;
|
||||
if (c==ID_CHUNK)
|
||||
return LoadChunk(&S);
|
||||
else if (c!=EOZ)
|
||||
luaL_verror("%s is not a lua binary file",Z->name);
|
||||
return NULL;
|
||||
Proto* f=LoadChunk(L,Z);
|
||||
if (zgetc(Z)!=EOZ)
|
||||
luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z));
|
||||
return f;
|
||||
}
|
||||
|
||||
/*
|
||||
** find byte order
|
||||
*/
|
||||
int luaU_endianness (void)
|
||||
{
|
||||
int x=1;
|
||||
return *(l_char*)&x;
|
||||
}
|
||||
|
||||
25
lundump.h
25
lundump.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.4 1998/01/13 20:05:24 lhf Exp $
|
||||
** $Id: lundump.h,v 1.23 2001/06/28 13:55:17 lhf Exp lhf $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,18 +10,19 @@
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
#define ID_CHUNK 27 /* ESC */
|
||||
#define ID_FUNCTION '#'
|
||||
#define ID_END '$'
|
||||
#define ID_NUM 'N'
|
||||
#define ID_STR 'S'
|
||||
#define ID_FUN 'F'
|
||||
#define SIGNATURE "Lua"
|
||||
#define VERSION 0x31 /* last format change was in 3.1 */
|
||||
#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
|
||||
/* load one chunk */
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z);
|
||||
|
||||
#define IsMain(f) (f->lineDefined==0)
|
||||
/* find byte order */
|
||||
int luaU_endianness (void);
|
||||
|
||||
TProtoFunc* luaU_undump1(ZIO* Z); /* load one chunk */
|
||||
/* definitions for headers of binary files */
|
||||
#define VERSION 0x41 /* last format change was in 4.1 */
|
||||
#define VERSION0 0x41 /* last major change was in 4.1 */
|
||||
#define LUA_SIGNATURE "\033Lua" /* binary files start with <esc>Lua */
|
||||
|
||||
/* a multiple of PI for testing native format */
|
||||
/* multiplying by 1E8 gives non-trivial integer values */
|
||||
#define TEST_NUMBER 3.14159265358979323846E8
|
||||
|
||||
#endif
|
||||
|
||||
26
lvm.h
26
lvm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 1.3 1997/10/16 10:59:34 roberto Exp roberto $
|
||||
** $Id: lvm.h,v 1.29 2001/02/07 18:13:49 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,20 +10,22 @@
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lobject.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (luaV_tonumber(o) != 0))
|
||||
#define tostring(o) ((ttype(o) != LUA_T_STRING) && (luaV_tostring(o) != 0))
|
||||
#define tostring(L,o) ((ttype(o) != LUA_TSTRING) && (luaV_tostring(L, o) != 0))
|
||||
|
||||
|
||||
void luaV_pack (StkId firstel, int nvararg, TObject *tab);
|
||||
int luaV_tonumber (TObject *obj);
|
||||
int luaV_tostring (TObject *obj);
|
||||
void luaV_gettable (void);
|
||||
void luaV_settable (TObject *t, int mode);
|
||||
void luaV_getglobal (TaggedString *ts);
|
||||
void luaV_setglobal (TaggedString *ts);
|
||||
StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base);
|
||||
void luaV_closure (int nelems);
|
||||
const TObject *luaV_tonumber (const TObject *obj, TObject *n);
|
||||
int luaV_tostring (lua_State *L, TObject *obj);
|
||||
void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res);
|
||||
void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val);
|
||||
void luaV_getglobal (lua_State *L, TString *s, StkId res);
|
||||
void luaV_setglobal (lua_State *L, TString *s, StkId val);
|
||||
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base);
|
||||
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems);
|
||||
void luaV_Lclosure (lua_State *L, Proto *l, int nelems);
|
||||
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r);
|
||||
void luaV_strconc (lua_State *L, int total, StkId top);
|
||||
|
||||
#endif
|
||||
|
||||
82
lzio.c
82
lzio.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.2 1997/11/21 19:00:46 roberto Exp roberto $
|
||||
** $Id: lzio.c,v 1.13 2000/06/12 13:52:05 roberto Exp roberto $
|
||||
** a generic input stream interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,71 +9,75 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
/* ----------------------------------------------------- memory buffers --- */
|
||||
|
||||
static int zmfilbuf (ZIO* z)
|
||||
{
|
||||
return EOZ;
|
||||
static int zmfilbuf (ZIO* z) {
|
||||
(void)z; /* to avoid warnings */
|
||||
return EOZ;
|
||||
}
|
||||
|
||||
ZIO* zmopen (ZIO* z, char* b, int size, char *name)
|
||||
{
|
||||
if (b==NULL) return NULL;
|
||||
z->n=size;
|
||||
z->p= (unsigned char *)b;
|
||||
z->filbuf=zmfilbuf;
|
||||
z->u=NULL;
|
||||
z->name=name;
|
||||
return z;
|
||||
|
||||
ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name) {
|
||||
if (b==NULL) return NULL;
|
||||
z->n = size;
|
||||
z->p = (const unsigned char *)b;
|
||||
z->filbuf = zmfilbuf;
|
||||
z->u = NULL;
|
||||
z->name = name;
|
||||
return z;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ strings --- */
|
||||
|
||||
ZIO* zsopen (ZIO* z, char* s, char *name)
|
||||
{
|
||||
if (s==NULL) return NULL;
|
||||
return zmopen(z,s,strlen(s),name);
|
||||
ZIO* zsopen (ZIO* z, const char* s, const char *name) {
|
||||
if (s==NULL) return NULL;
|
||||
return zmopen(z, s, strlen(s), name);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------- FILEs --- */
|
||||
|
||||
static int zffilbuf (ZIO* z)
|
||||
{
|
||||
int n=fread(z->buffer,1,ZBSIZE,z->u);
|
||||
if (n==0) return EOZ;
|
||||
z->n=n-1;
|
||||
z->p=z->buffer;
|
||||
return *(z->p++);
|
||||
static int zffilbuf (ZIO* z) {
|
||||
size_t n;
|
||||
if (feof((FILE *)z->u)) return EOZ;
|
||||
n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u);
|
||||
if (n==0) return EOZ;
|
||||
z->n = n-1;
|
||||
z->p = z->buffer;
|
||||
return *(z->p++);
|
||||
}
|
||||
|
||||
|
||||
ZIO* zFopen (ZIO* z, FILE* f, char *name)
|
||||
{
|
||||
if (f==NULL) return NULL;
|
||||
z->n=0;
|
||||
z->p=z->buffer;
|
||||
z->filbuf=zffilbuf;
|
||||
z->u=f;
|
||||
z->name=name;
|
||||
return z;
|
||||
ZIO* zFopen (ZIO* z, FILE* f, const char *name) {
|
||||
if (f==NULL) return NULL;
|
||||
z->n = 0;
|
||||
z->p = z->buffer;
|
||||
z->filbuf = zffilbuf;
|
||||
z->u = f;
|
||||
z->name = name;
|
||||
return z;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- read --- */
|
||||
int zread (ZIO *z, void *b, int n)
|
||||
{
|
||||
size_t zread (ZIO *z, void *b, size_t n) {
|
||||
while (n) {
|
||||
int m;
|
||||
size_t m;
|
||||
if (z->n == 0) {
|
||||
if (z->filbuf(z) == EOZ)
|
||||
return n; /* retorna quantos faltaram ler */
|
||||
zungetc(z); /* poe o resultado de filbuf no buffer */
|
||||
return n; /* return number of missing bytes */
|
||||
else {
|
||||
++z->n; /* filbuf removed first byte; put back it */
|
||||
--z->p;
|
||||
}
|
||||
}
|
||||
m = (n <= z->n) ? n : z->n; /* minimo de n e z->n */
|
||||
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
||||
memcpy(b, z->p, m);
|
||||
z->n -= m;
|
||||
z->p += m;
|
||||
|
||||
28
lzio.h
28
lzio.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.h,v 1.3 1997/12/22 20:57:18 roberto Exp roberto $
|
||||
** $Id: lzio.h,v 1.7 2000/10/20 16:36:32 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -22,28 +22,30 @@
|
||||
|
||||
typedef struct zio ZIO;
|
||||
|
||||
ZIO* zFopen (ZIO* z, FILE* f, char *name); /* open FILEs */
|
||||
ZIO* zsopen (ZIO* z, char* s, char *name); /* string */
|
||||
ZIO* zmopen (ZIO* z, char* b, int size, char *name); /* memory */
|
||||
ZIO* zFopen (ZIO* z, FILE* f, const char *name); /* open FILEs */
|
||||
ZIO* zsopen (ZIO* z, const char* s, const char *name); /* string */
|
||||
ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name); /* memory */
|
||||
|
||||
int zread (ZIO* z, void* b, int n); /* read next n bytes */
|
||||
size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
|
||||
|
||||
#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||
#define zungetc(z) (++(z)->n,--(z)->p)
|
||||
#define zgetc(z) (((z)->n--)>0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||
#define zname(z) ((z)->name)
|
||||
|
||||
|
||||
|
||||
/* --------- Private Part ------------------ */
|
||||
|
||||
#ifndef ZBSIZE
|
||||
#define ZBSIZE 256 /* buffer size */
|
||||
#endif
|
||||
|
||||
struct zio {
|
||||
int n; /* bytes still unread */
|
||||
unsigned char* p; /* current position in buffer */
|
||||
int (*filbuf)(ZIO* z);
|
||||
void* u; /* additional data */
|
||||
char *name;
|
||||
unsigned char buffer[ZBSIZE]; /* buffer */
|
||||
size_t n; /* bytes still unread */
|
||||
const unsigned char* p; /* current position in buffer */
|
||||
int (*filbuf)(ZIO* z);
|
||||
void* u; /* additional data */
|
||||
const char *name;
|
||||
unsigned char buffer[ZBSIZE]; /* buffer */
|
||||
};
|
||||
|
||||
|
||||
|
||||
155
makefile
155
makefile
@@ -1,5 +1,5 @@
|
||||
#
|
||||
## $Id: makefile,v 1.9 1998/01/02 17:46:32 roberto Exp roberto $
|
||||
## $Id: makefile,v 1.31 2001/07/24 17:25:30 roberto Exp roberto $
|
||||
## Makefile
|
||||
## See Copyright Notice in lua.h
|
||||
#
|
||||
@@ -12,20 +12,38 @@
|
||||
# define (undefine) _POSIX_SOURCE if your system is (not) POSIX compliant
|
||||
#
|
||||
# define (undefine) OLD_ANSI if your system does NOT have some new ANSI
|
||||
# facilities (e.g. strerror, locale.h, memmove). Although they are ANSI,
|
||||
# SunOS does not comply; so, add "-DOLD_ANSI" on SunOS
|
||||
# facilities (e.g. strerror, locale.h, memmove). SunOS does not comply;
|
||||
# so, add "-DOLD_ANSI" on SunOS
|
||||
#
|
||||
# define LUA_NUM_TYPE if you need numbers to be different from double
|
||||
# (for instance, -DLUA_NUM_TYPE=float)
|
||||
# you may need to adapat the code, too.
|
||||
#
|
||||
# define LUA_COMPAT2_5 if yous system does need to be compatible with
|
||||
# version 2.5 (or older)
|
||||
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||
#CONFIG = -DLUA_COMPAT2_5 -DOLD_ANSI -DDEBUG
|
||||
#EXTRA_H=-DLUA_USER_H='"ltests.h"'
|
||||
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE $(EXTRA_H)
|
||||
#CONFIG = -DOLD_ANSI
|
||||
|
||||
|
||||
# Compilation parameters
|
||||
CC = gcc
|
||||
CWARNS = -Wall -Wmissing-prototypes -Wshadow -pedantic -Wpointer-arith -Wcast-align -Waggregate-return
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O2
|
||||
CWARNS = -Wall -W -pedantic \
|
||||
-Waggregate-return \
|
||||
-Wcast-align \
|
||||
-Wmissing-prototypes \
|
||||
-Wnested-externs \
|
||||
-Wpointer-arith \
|
||||
-Wshadow \
|
||||
-Wwrite-strings \
|
||||
# -Wcast-qual
|
||||
# -Wtraditional
|
||||
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O3 -fomit-frame-pointer
|
||||
|
||||
|
||||
# To make early versions
|
||||
CO_OPTIONS =
|
||||
|
||||
|
||||
AR = ar
|
||||
@@ -34,29 +52,33 @@ ARFLAGS = rvl
|
||||
|
||||
# Aplication modules
|
||||
LUAOBJS = \
|
||||
lapi.o \
|
||||
lauxlib.o \
|
||||
lbuffer.o \
|
||||
lbuiltin.o \
|
||||
ldo.o \
|
||||
lfunc.o \
|
||||
lgc.o \
|
||||
llex.o \
|
||||
lmem.o \
|
||||
lobject.o \
|
||||
lstate.o \
|
||||
lstx.o \
|
||||
lapi.o \
|
||||
lmem.o \
|
||||
lstring.o \
|
||||
ltable.o \
|
||||
ltm.o \
|
||||
lvm.o \
|
||||
ldo.o \
|
||||
lobject.o \
|
||||
lfunc.o \
|
||||
lgc.o \
|
||||
lcode.o \
|
||||
lparser.o \
|
||||
llex.o \
|
||||
lopcodes.o \
|
||||
lundump.o \
|
||||
lzio.o
|
||||
lzio.o \
|
||||
ldebug.o \
|
||||
ltests.o
|
||||
|
||||
LIBOBJS = \
|
||||
lauxlib.o \
|
||||
lbaselib.o \
|
||||
liolib.o \
|
||||
lmathlib.o \
|
||||
lstrlib.o
|
||||
lstrlib.o \
|
||||
ldblib.o
|
||||
|
||||
|
||||
lua : lua.o liblua.a liblualib.a
|
||||
@@ -73,53 +95,64 @@ liblualib.a : $(LIBOBJS)
|
||||
liblua.so.1.0 : lua.o
|
||||
ld -o liblua.so.1.0 $(LUAOBJS)
|
||||
|
||||
lstx.c lstx.h : lua.stx
|
||||
bison -o lstx.c -p luaY_ -d lua.stx
|
||||
# yacc -d lua.stx
|
||||
# sed -e 's/yy/luaY_/g' -e 's/malloc\.h/stdlib\.h/g' y.tab.c > lstx.c
|
||||
# sed -e 's/yy/luaY_/g' y.tab.h > lstx.h
|
||||
|
||||
clear :
|
||||
rcsclean
|
||||
rm -f *.o
|
||||
rm -f lstx.c lstx.h
|
||||
co lua.h lualib.h luadebug.h
|
||||
rm -f *.o *.a
|
||||
|
||||
|
||||
%.h : RCS/%.h,v
|
||||
co $@
|
||||
co $(CO_OPTIONS) $@
|
||||
|
||||
%.c : RCS/%.c,v
|
||||
co $@
|
||||
co $(CO_OPTIONS) $@
|
||||
|
||||
lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lstate.h lfunc.h \
|
||||
lgc.h lmem.h lstring.h ltable.h ltm.h luadebug.h lvm.h
|
||||
lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h
|
||||
lbuffer.o: lbuffer.c lauxlib.h lua.h lmem.h lstate.h lobject.h
|
||||
lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \
|
||||
ldo.h lstate.h lfunc.h lmem.h lstring.h ltable.h ltm.h
|
||||
ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
|
||||
lparser.h lzio.h ltm.h luadebug.h lundump.h lvm.h
|
||||
lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lstate.h
|
||||
lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
|
||||
lstring.h ltable.h ltm.h
|
||||
liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h
|
||||
llex.o: llex.c lauxlib.h lua.h llex.h lobject.h lzio.h lmem.h \
|
||||
lparser.h lstate.h lstring.h lstx.h luadebug.h
|
||||
lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h
|
||||
lmem.o: lmem.c lmem.h lstate.h lobject.h lua.h
|
||||
lobject.o: lobject.c lobject.h lua.h
|
||||
lstate.o: lstate.c lbuiltin.h ldo.h lobject.h lua.h lstate.h lfunc.h \
|
||||
|
||||
lapi.o: lapi.c lua.h lapi.h lobject.h llimits.h ldo.h lstate.h \
|
||||
luadebug.h lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h lvm.h
|
||||
lauxlib.o: lauxlib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
lbaselib.o: lbaselib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
lcode.o: lcode.c lua.h lcode.h llex.h lobject.h llimits.h lzio.h \
|
||||
lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h lmem.h
|
||||
ldblib.o: ldblib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
ldebug.o: ldebug.c lua.h lapi.h lobject.h llimits.h lcode.h llex.h \
|
||||
lzio.h lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h \
|
||||
lfunc.h lstring.h ltable.h ltm.h lvm.h
|
||||
ldo.o: ldo.c lua.h ldebug.h lstate.h lobject.h llimits.h luadebug.h \
|
||||
ldo.h lgc.h lmem.h lparser.h lzio.h lstring.h ltable.h ltm.h \
|
||||
lundump.h lvm.h
|
||||
lfunc.o: lfunc.c lua.h lfunc.h lobject.h llimits.h lmem.h lstate.h \
|
||||
luadebug.h
|
||||
lgc.o: lgc.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h
|
||||
liolib.o: liolib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
llex.o: llex.c lua.h llex.h lobject.h llimits.h lzio.h lparser.h \
|
||||
lstate.h luadebug.h lstring.h
|
||||
lmathlib.o: lmathlib.c lua.h lauxlib.h lualib.h
|
||||
lmem.o: lmem.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h
|
||||
lobject.o: lobject.c lua.h ldo.h lobject.h llimits.h lstate.h \
|
||||
luadebug.h lmem.h
|
||||
lopcodes.o: lopcodes.c lua.h lopcodes.h llimits.h
|
||||
lparser.o: lparser.c lua.h lcode.h llex.h lobject.h llimits.h lzio.h \
|
||||
lopcodes.h lparser.h ldebug.h lstate.h luadebug.h lfunc.h lmem.h \
|
||||
lstring.h
|
||||
lstate.o: lstate.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lgc.h llex.h lzio.h lmem.h lstring.h ltable.h ltm.h
|
||||
lstring.o: lstring.c lmem.h lobject.h lua.h lstate.h lstring.h
|
||||
lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h
|
||||
lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h llex.h \
|
||||
lzio.h lmem.h lopcodes.h lparser.h lstring.h luadebug.h
|
||||
ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h lstate.h ltable.h
|
||||
ltm.o: ltm.c lauxlib.h lua.h lmem.h lobject.h lstate.h ltm.h lapi.h
|
||||
lstring.o: lstring.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h lstring.h
|
||||
lstrlib.o: lstrlib.c lua.h lauxlib.h lualib.h
|
||||
ltable.o: ltable.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h ltable.h
|
||||
ltests.o: ltests.c lua.h lapi.h lobject.h llimits.h lauxlib.h lcode.h \
|
||||
llex.h lzio.h lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h \
|
||||
lfunc.h lmem.h lstring.h ltable.h lualib.h
|
||||
ltm.o: ltm.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h lstring.h ltable.h ltm.h
|
||||
lua.o: lua.c lua.h luadebug.h lualib.h
|
||||
lundump.o: lundump.c lauxlib.h lua.h lfunc.h lobject.h lmem.h \
|
||||
lstring.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h lgc.h \
|
||||
lmem.h lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h
|
||||
lzio.o: lzio.c lzio.h
|
||||
lundump.o: lundump.c lua.h ldebug.h lstate.h lobject.h llimits.h \
|
||||
luadebug.h lfunc.h lmem.h lopcodes.h lstring.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lua.h lapi.h lobject.h llimits.h ldebug.h lstate.h \
|
||||
luadebug.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h ltm.h \
|
||||
lvm.h
|
||||
lzio.o: lzio.c lua.h lzio.h
|
||||
|
||||
4788
manual.tex
4788
manual.tex
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user