mirror of
https://github.com/lua/lua.git
synced 2026-07-26 16:09:07 +00:00
Compare commits
1554 Commits
v2.4
...
v5.0-alpha
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa5b15f271 | ||
|
|
653416d5c0 | ||
|
|
82a3be0671 | ||
|
|
94b5ad31f4 | ||
|
|
90d26b9266 | ||
|
|
a048d71ef3 | ||
|
|
27f8a4a69e | ||
|
|
8e4ac679ff | ||
|
|
221b5be7b7 | ||
|
|
b6791f5fa5 | ||
|
|
3b058177af | ||
|
|
586e510577 | ||
|
|
9998082839 | ||
|
|
08da48a73c | ||
|
|
00a40f4d6a | ||
|
|
3bb5079dd4 | ||
|
|
5016f43aa4 | ||
|
|
c1c100a0c0 | ||
|
|
b6e2f1a86e | ||
|
|
260e35f576 | ||
|
|
a56e01a9a0 | ||
|
|
4664f2e927 | ||
|
|
2e38c6ae5a | ||
|
|
d3dd337fca | ||
|
|
634344d61f | ||
|
|
a2fa48a570 | ||
|
|
8b2b8790b5 | ||
|
|
0b3d380f9f | ||
|
|
4e23699aa6 | ||
|
|
1c0ac3c0f5 | ||
|
|
4a4f243795 | ||
|
|
0417a4bb0b | ||
|
|
5037196f6f | ||
|
|
9fb80bde3c | ||
|
|
2dcc31574f | ||
|
|
390fc99a5c | ||
|
|
79c8edb6c4 | ||
|
|
e5146fb01f | ||
|
|
ce6b930464 | ||
|
|
ac7006d374 | ||
|
|
12bee999dd | ||
|
|
a8c9eec426 | ||
|
|
a885b826b3 | ||
|
|
a960e62c3e | ||
|
|
c51aa6ee33 | ||
|
|
d88860131a | ||
|
|
44752fc9ce | ||
|
|
39b2d58c39 | ||
|
|
d2d24f0971 | ||
|
|
0436c96866 | ||
|
|
f8279f6cd8 | ||
|
|
1fe280df72 | ||
|
|
1ede98157d | ||
|
|
3e52573c72 | ||
|
|
84ad1eea81 | ||
|
|
b30ec2532c | ||
|
|
68ee518e81 | ||
|
|
76a73cb2ee | ||
|
|
aa3da1605c | ||
|
|
9f4b5b5232 | ||
|
|
5fabed21a1 | ||
|
|
f67ccfbdeb | ||
|
|
cfcf200806 | ||
|
|
f32a8c0477 | ||
|
|
88607acc2c | ||
|
|
6de93e2932 | ||
|
|
25dc9b7faf | ||
|
|
78c507b7b8 | ||
|
|
69906cb56f | ||
|
|
9d6556fd87 | ||
|
|
35707e94f3 | ||
|
|
89b102bd1d | ||
|
|
fdfd5b44ee | ||
|
|
1a4c428d6d | ||
|
|
2394604d10 | ||
|
|
922f36a05b | ||
|
|
ad41fc11eb | ||
|
|
e182cf452f | ||
|
|
e34f282365 | ||
|
|
3941af53ad | ||
|
|
5610fdd776 | ||
|
|
b449a5e574 | ||
|
|
e572dffa15 | ||
|
|
5893fcb71a | ||
|
|
eec0905173 | ||
|
|
a44f37513b | ||
|
|
6ee2dbdfe9 | ||
|
|
1dbe708aa8 | ||
|
|
8f080fd683 | ||
|
|
d8678edddc | ||
|
|
e812aa2002 | ||
|
|
c31494df26 | ||
|
|
8fd0f6a82b | ||
|
|
eb3de8768a | ||
|
|
864c96f36c | ||
|
|
0052930ffe | ||
|
|
eeab473fc8 | ||
|
|
6b8cdc9cdd | ||
|
|
000d081fd0 | ||
|
|
65d66ba275 | ||
|
|
cff22f57dd | ||
|
|
168ea16acb | ||
|
|
dc6e6c48bb | ||
|
|
e3cddc950c | ||
|
|
630acb2048 | ||
|
|
16f4723398 | ||
|
|
f1c43bbe19 | ||
|
|
5a8f383e60 | ||
|
|
711b936849 | ||
|
|
5b8ee9fa8d | ||
|
|
2cd6161060 | ||
|
|
a102221a0b | ||
|
|
6dd0b6c62b | ||
|
|
fa23301251 | ||
|
|
94c2449d25 | ||
|
|
c398a02110 | ||
|
|
ad7103ea3a | ||
|
|
0079e0f57c | ||
|
|
cfff013586 | ||
|
|
35a22ed1ab | ||
|
|
ff91b355f4 | ||
|
|
5094c37988 | ||
|
|
46c471d7e9 | ||
|
|
859ecf36b6 | ||
|
|
a1700bbc50 | ||
|
|
8876a1bf92 | ||
|
|
f53fd8d5f5 | ||
|
|
955def0348 | ||
|
|
9c3b3f82fe | ||
|
|
b7a0503c1d | ||
|
|
1c328a191a | ||
|
|
58bf77bc7f | ||
|
|
8da6fe62d8 | ||
|
|
c18fe57e54 | ||
|
|
af19d55635 | ||
|
|
b55fded18c | ||
|
|
2dadc81822 | ||
|
|
b487975344 | ||
|
|
94e5545806 | ||
|
|
78b40bf57d | ||
|
|
01f1ac36b1 | ||
|
|
a4d06736d4 | ||
|
|
dea6b6da94 | ||
|
|
71144e3ff0 | ||
|
|
0dbf0c5953 | ||
|
|
85dcb411a8 | ||
|
|
3c6a383d62 | ||
|
|
9a0f0dcc77 | ||
|
|
751cd867d3 | ||
|
|
b36b2a061c | ||
|
|
ab52fc6097 | ||
|
|
e68d0df1c6 | ||
|
|
9b38a696d5 | ||
|
|
383e8b9e77 | ||
|
|
f1a1bb23fe | ||
|
|
151ba1cc7b | ||
|
|
ee4859b3e3 | ||
|
|
f388ee4a82 | ||
|
|
30ad4c75db | ||
|
|
3ee1efa7b2 | ||
|
|
c11d374c59 | ||
|
|
13230c451b | ||
|
|
391c5459cb | ||
|
|
62dd4cbe91 | ||
|
|
ae5283dc36 | ||
|
|
1ce6cb6032 | ||
|
|
11c63bc3af | ||
|
|
f1f271ae76 | ||
|
|
6c7334a9ac | ||
|
|
8e1e618606 | ||
|
|
af4721f7a3 | ||
|
|
018e50ad7f | ||
|
|
7b65328c8e | ||
|
|
d2e05589d7 | ||
|
|
237969724f | ||
|
|
f438d00ef3 | ||
|
|
c3b90061ea | ||
|
|
a2e414d679 | ||
|
|
93bcb65a9b | ||
|
|
49c95648a0 | ||
|
|
13ab5a6bb1 | ||
|
|
cd99bbcd0d | ||
|
|
2cbbf7e95a | ||
|
|
e5919be1a7 | ||
|
|
b795d4b561 | ||
|
|
c371413530 | ||
|
|
d1f220217b | ||
|
|
405e3a4597 | ||
|
|
81215cd59f | ||
|
|
0b9b53e21c | ||
|
|
44a5484d73 | ||
|
|
801aaf37b1 | ||
|
|
00af2faae7 | ||
|
|
e9ef7ed2d3 | ||
|
|
2626708b72 | ||
|
|
19ac0fadc9 | ||
|
|
ca2ceef659 | ||
|
|
5023aaee0a | ||
|
|
88c9bf99de | ||
|
|
63a614e145 | ||
|
|
48e732e07d | ||
|
|
938092489b | ||
|
|
578d1da00d | ||
|
|
24d6d0528d | ||
|
|
0b00e7f1a2 | ||
|
|
b7ed502dea | ||
|
|
58badbab99 | ||
|
|
da81845cd5 | ||
|
|
9681ff09db | ||
|
|
7ff21273d6 | ||
|
|
207dad8606 | ||
|
|
c97dc294a9 | ||
|
|
71c716e5a8 | ||
|
|
51ad2f1162 | ||
|
|
ecda9c1972 | ||
|
|
e19eba7219 | ||
|
|
bd8b9c94b3 | ||
|
|
4db04555f0 | ||
|
|
c16a35d669 | ||
|
|
8f837e83b2 | ||
|
|
6658b9588f | ||
|
|
457d88eaaa | ||
|
|
8a1a512c64 | ||
|
|
6b0c38c2e7 | ||
|
|
1a3f175640 | ||
|
|
88c7b574cb | ||
|
|
51bf91a970 | ||
|
|
1b6315ba10 | ||
|
|
3b001d9a9d | ||
|
|
c36b5cc726 | ||
|
|
2c1eb62a08 | ||
|
|
63e2d92123 | ||
|
|
e966dd93be | ||
|
|
2f8e3c5543 | ||
|
|
b0a5e156b8 | ||
|
|
ac178ee478 | ||
|
|
1e602a61b3 | ||
|
|
57fb51f975 | ||
|
|
1c423d2661 | ||
|
|
b3ffaca638 | ||
|
|
bee86e23be | ||
|
|
0056ed4135 | ||
|
|
e01f5e6809 | ||
|
|
cbfc581990 | ||
|
|
d3651c1d88 | ||
|
|
57a7baafb9 | ||
|
|
252e01e398 | ||
|
|
87b0e3d477 | ||
|
|
cb50fcf42d | ||
|
|
c6a108e012 | ||
|
|
d5ceb369b1 | ||
|
|
86a4de256e | ||
|
|
53aaee6ee6 | ||
|
|
67bda7e136 | ||
|
|
7c8f1823e7 | ||
|
|
ca181f31e4 | ||
|
|
38b0e6128d | ||
|
|
addbe8c8b0 | ||
|
|
6bb5cb1cbd | ||
|
|
d070506a25 | ||
|
|
0a87d9d334 | ||
|
|
f856fdeabd | ||
|
|
653977a0ac | ||
|
|
d6fd33e76f | ||
|
|
50e2952593 | ||
|
|
b217ae644e | ||
|
|
a048cc9676 | ||
|
|
eb262bc617 | ||
|
|
74907fb71e | ||
|
|
b38e594ed7 | ||
|
|
448517e47e | ||
|
|
931ee346e3 | ||
|
|
dea98702c9 | ||
|
|
7ab7703b53 | ||
|
|
566310fa04 | ||
|
|
6272c843de | ||
|
|
5d14ce612b | ||
|
|
d56d4cf776 | ||
|
|
b7ae43d457 | ||
|
|
f083812c02 | ||
|
|
3533382a1e | ||
|
|
ea2a75d19e | ||
|
|
b3bb0f132b | ||
|
|
facfec0687 | ||
|
|
34df9976a9 | ||
|
|
cfaa8fbf1d | ||
|
|
f42cc90d2d | ||
|
|
9d2e454d6f | ||
|
|
09e15692f3 | ||
|
|
42754c0f15 | ||
|
|
18afb90349 | ||
|
|
22dd271cbb | ||
|
|
e04f7ed450 | ||
|
|
101cee3032 | ||
|
|
eb7312d9be | ||
|
|
21259a50e1 | ||
|
|
9aff171f3b | ||
|
|
ed9be5e1f0 | ||
|
|
9d801f43d4 | ||
|
|
e043b72a55 | ||
|
|
a4c35a3269 | ||
|
|
9cd36059ad | ||
|
|
592a309177 | ||
|
|
413fc7334b | ||
|
|
fca0a12e23 | ||
|
|
72659a0605 | ||
|
|
dfaf8c5291 | ||
|
|
657f65211a | ||
|
|
39395e1211 | ||
|
|
26bf2adace | ||
|
|
fd48dcc7c8 | ||
|
|
617008f552 | ||
|
|
ec9d8308b4 | ||
|
|
af59848219 | ||
|
|
46347d768e | ||
|
|
36eb665859 | ||
|
|
070204300c | ||
|
|
21aa7e55f2 | ||
|
|
fffb6f3814 | ||
|
|
0b551a24f8 | ||
|
|
8069f77ca4 | ||
|
|
1e81da51ba | ||
|
|
7cd37142f4 | ||
|
|
e7c2eebd87 | ||
|
|
0c3ea96541 | ||
|
|
babaa96eab | ||
|
|
15462edb0f | ||
|
|
6f936bc793 | ||
|
|
b840a7518d | ||
|
|
f54cdb33a5 | ||
|
|
8e9b1e4ae9 | ||
|
|
328d53e77b | ||
|
|
c04d9b9ecb | ||
|
|
c44c68450a | ||
|
|
abdbe883a8 | ||
|
|
4d0935ec0f | ||
|
|
e1d072571e | ||
|
|
7651a5c6b2 | ||
|
|
8c8ad5f3ff | ||
|
|
34a09b65f3 | ||
|
|
29d883b9bd | ||
|
|
b0d5bd8c70 | ||
|
|
9fca43f5b0 | ||
|
|
33d820d41d | ||
|
|
c3d72096c4 | ||
|
|
7afc74ff07 | ||
|
|
022bf27202 | ||
|
|
fd40af92e4 | ||
|
|
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 | ||
|
|
981fddea02 | ||
|
|
81b953f27e | ||
|
|
b9acf4b4af | ||
|
|
44ace0aefd | ||
|
|
5981161360 | ||
|
|
763c64be9b | ||
|
|
f0dffaa209 | ||
|
|
77a6836fef | ||
|
|
9f043e8017 | ||
|
|
6ac047afc4 | ||
|
|
0e1058cfdd | ||
|
|
26679b1a48 | ||
|
|
e04c2b9aa8 | ||
|
|
0c031dcc8b | ||
|
|
c332c4e927 | ||
|
|
964c503a63 | ||
|
|
90d87e3a78 | ||
|
|
f76bca23ef | ||
|
|
a5fd7d722c | ||
|
|
4e0bf95622 | ||
|
|
498a934abf | ||
|
|
ce53872684 | ||
|
|
da96eb2cce | ||
|
|
fada8efd01 | ||
|
|
d916487d7c | ||
|
|
1bf762ba38 | ||
|
|
541e722360 | ||
|
|
807ba6301c | ||
|
|
03f3f9e707 | ||
|
|
a78eecee48 | ||
|
|
43461d267f | ||
|
|
fae0b52825 | ||
|
|
22439a7511 | ||
|
|
7ecc3ce827 | ||
|
|
4e91384e14 | ||
|
|
de79e7fc58 | ||
|
|
8b5b42563c | ||
|
|
502343b402 | ||
|
|
82d09fbf0d | ||
|
|
9be85d1648 | ||
|
|
45e533599f | ||
|
|
94144a7821 | ||
|
|
4daae2165d | ||
|
|
cdd261f332 | ||
|
|
034f16892e | ||
|
|
c759520bc8 | ||
|
|
80b3d28f4a | ||
|
|
69d97712ec | ||
|
|
5d89dad9b8 | ||
|
|
525a91fed3 | ||
|
|
868d16dee0 | ||
|
|
3393fd7f25 | ||
|
|
00c122cc29 | ||
|
|
03160920cf | ||
|
|
b42cc6a4d2 | ||
|
|
a6ad644bf2 | ||
|
|
39fd5bb9b0 | ||
|
|
5482992dec | ||
|
|
024528e0c2 | ||
|
|
ef37c87e93 | ||
|
|
9e029f98b9 | ||
|
|
e962330df9 | ||
|
|
b291e50006 | ||
|
|
9ae0c082a3 | ||
|
|
accd7bc253 | ||
|
|
6153200bc2 | ||
|
|
2e7595522d | ||
|
|
b79ffdc4ce | ||
|
|
592a3f289b | ||
|
|
9cdeb275e7 | ||
|
|
c957b270d2 | ||
|
|
92791b9dd6 | ||
|
|
45cad43c3f | ||
|
|
dad5a01fb0 | ||
|
|
66713181c1 | ||
|
|
7135803cc8 | ||
|
|
b7567b6673 | ||
|
|
f8c95fa9e8 | ||
|
|
9c965d0ffb | ||
|
|
6103dca8ee | ||
|
|
18cd7adac6 | ||
|
|
41223a01ec | ||
|
|
e78cf96c97 | ||
|
|
0cb3843956 | ||
|
|
907368ead5 | ||
|
|
81489beea1 | ||
|
|
ac30aad09b | ||
|
|
2c89651fc6 | ||
|
|
3a89c973ff | ||
|
|
52d5e8032c | ||
|
|
19c178fa14 | ||
|
|
45ccb0e881 | ||
|
|
4be18fa889 | ||
|
|
7c261a13b5 | ||
|
|
2bb94d9e22 | ||
|
|
a3235ad270 | ||
|
|
f6a9cc9a67 | ||
|
|
28d47a0aaa | ||
|
|
eb617df2d8 | ||
|
|
a580480b07 | ||
|
|
0dd6d1080e | ||
|
|
3c820d622e | ||
|
|
d6c867ea50 | ||
|
|
2079cfe8fa | ||
|
|
dfe03c7abe | ||
|
|
8cd67ac676 | ||
|
|
9828893f7e | ||
|
|
6990da0057 | ||
|
|
d985dc0629 | ||
|
|
451124005b | ||
|
|
2f1fa3d427 | ||
|
|
189d64409b | ||
|
|
60cc473bcf | ||
|
|
43a2ee6ea1 | ||
|
|
4b91e9cde6 | ||
|
|
26c5f56ad1 | ||
|
|
daa858ef27 | ||
|
|
ea169d2083 | ||
|
|
c31aa863ac | ||
|
|
ff08b0f406 | ||
|
|
c1801e623f | ||
|
|
a404f6e0e6 | ||
|
|
2d2440a753 | ||
|
|
0c4ed2b3dc | ||
|
|
b945fae40e | ||
|
|
dadba4d6ed | ||
|
|
d600a6b5b3 | ||
|
|
75ac0d2172 | ||
|
|
9f3785a2f3 | ||
|
|
84e92e0976 | ||
|
|
b8a049abed | ||
|
|
e18f681333 | ||
|
|
dd1aa28390 | ||
|
|
abbf14cd32 | ||
|
|
e8292f076d | ||
|
|
3037dccaf6 | ||
|
|
a7793468aa | ||
|
|
caa987faad | ||
|
|
0892f0e5b7 | ||
|
|
1d7857bc63 | ||
|
|
72a1d81b51 | ||
|
|
2c580a0afb | ||
|
|
05e8b0ae80 | ||
|
|
16dd77e8d9 | ||
|
|
0600f968c3 | ||
|
|
971b1d557d | ||
|
|
11d97c34d5 | ||
|
|
66be42549e | ||
|
|
067db30d71 | ||
|
|
da4dbe65b2 | ||
|
|
4321fde2a7 | ||
|
|
8f3df1d471 | ||
|
|
1a17211707 | ||
|
|
d56e3a6481 | ||
|
|
7820a47184 | ||
|
|
88b185ada1 | ||
|
|
ef3c45d523 | ||
|
|
20a48a818f | ||
|
|
c9e6ed1d9f | ||
|
|
56fdab67d8 | ||
|
|
3a98c8670d | ||
|
|
da585783e3 | ||
|
|
e81f184164 | ||
|
|
3e43275308 | ||
|
|
f97307b548 | ||
|
|
6402bfb1f8 | ||
|
|
27b6bbdcf3 | ||
|
|
5a198de3c1 | ||
|
|
c5d7ba7347 | ||
|
|
f84c2ebc4a | ||
|
|
c8897f2b08 | ||
|
|
6bb2cac3db | ||
|
|
88f020b626 | ||
|
|
a38f093f05 | ||
|
|
b6adfdd01b | ||
|
|
b90b4bbd3a | ||
|
|
f6d95140ae | ||
|
|
eca17b24ea | ||
|
|
6b61582e47 | ||
|
|
60bdc370d9 | ||
|
|
b9e31f73ee | ||
|
|
ea98620d98 | ||
|
|
6b78040840 | ||
|
|
e931c7c0f6 | ||
|
|
70160320b1 | ||
|
|
bfbf56f15a | ||
|
|
732ef82c72 | ||
|
|
2a1da64872 | ||
|
|
3c5d71cf77 | ||
|
|
1848bcc15b | ||
|
|
4d2de484f6 | ||
|
|
312f78d925 | ||
|
|
e4b69d6c9c | ||
|
|
c9a2dfeb2c | ||
|
|
9fe5be3acf | ||
|
|
611680af08 | ||
|
|
cb1d8f0fa0 | ||
|
|
33f4fef410 | ||
|
|
e77534c08f | ||
|
|
a22b54bbb6 | ||
|
|
414ebe8ce1 | ||
|
|
33ace03b5a | ||
|
|
085181a08a | ||
|
|
dd22ea4da5 | ||
|
|
5fdcfeb353 | ||
|
|
d68d8287aa | ||
|
|
e1249970c2 | ||
|
|
9d6f4e48a6 | ||
|
|
b546a042e8 | ||
|
|
bd9e68cfcd | ||
|
|
9747f3c87a | ||
|
|
12d9731a49 | ||
|
|
4aade854bb | ||
|
|
369dd65318 | ||
|
|
209602ac31 | ||
|
|
6251d889ca | ||
|
|
faec0b0824 | ||
|
|
64d93e7cf9 | ||
|
|
d73c5bcfe7 | ||
|
|
e78c1c54aa | ||
|
|
42049b42f1 | ||
|
|
a8cd072c76 | ||
|
|
83e9897cf0 | ||
|
|
f93704728f | ||
|
|
c9e3d32182 | ||
|
|
00050b8a6b | ||
|
|
19a1e19ae1 | ||
|
|
3a9516ffc8 | ||
|
|
42fa305649 | ||
|
|
9319735744 | ||
|
|
1ea6e8064c | ||
|
|
c1746340c3 | ||
|
|
d0339e7b72 | ||
|
|
075b7918c3 | ||
|
|
7c99149a76 | ||
|
|
27d95f1880 | ||
|
|
f0d523887d | ||
|
|
f29fbf2bf6 | ||
|
|
4355e1afcd | ||
|
|
cae1eff901 | ||
|
|
f83bc86e51 | ||
|
|
59744f0311 | ||
|
|
bb2e2bec18 | ||
|
|
efaaf99c42 | ||
|
|
f8a571ee35 | ||
|
|
397e32399b | ||
|
|
373363cb69 | ||
|
|
ad5574c4c9 | ||
|
|
264f8c5e7b | ||
|
|
9e9e2ea287 | ||
|
|
03d917f229 | ||
|
|
c3488f5115 | ||
|
|
6fef372fb8 | ||
|
|
052a1cc46c | ||
|
|
ae067dcddd | ||
|
|
5d60470508 | ||
|
|
88d7ffb0d0 | ||
|
|
47527d2d3c | ||
|
|
1444d28476 | ||
|
|
2de803c250 | ||
|
|
fa08b42dd8 | ||
|
|
eea734aa88 | ||
|
|
b6d91e24e2 | ||
|
|
a82ab0852e | ||
|
|
b8af9c56c9 | ||
|
|
c3c0b52a1f | ||
|
|
ec422ec7d0 | ||
|
|
a8a15ff1bd | ||
|
|
131d66efd2 | ||
|
|
bbf1b3060a | ||
|
|
8b7f271ea2 | ||
|
|
bc323435ee | ||
|
|
6769f34817 | ||
|
|
0b110f7922 | ||
|
|
205ee1ec84 | ||
|
|
b48847c5fa | ||
|
|
1143bf9286 | ||
|
|
d9ecc13545 | ||
|
|
f00d692445 | ||
|
|
d8b0bbb2ad | ||
|
|
1f4ee4a4d2 | ||
|
|
6a9efa8b8e | ||
|
|
65459e7938 | ||
|
|
42359b8b13 | ||
|
|
169870e37d | ||
|
|
78e454d864 | ||
|
|
dbfe28e199 | ||
|
|
d59c52753f | ||
|
|
62e1a4c84d | ||
|
|
81411e8913 | ||
|
|
62aa717f7e | ||
|
|
a5614eae3c | ||
|
|
536bae5871 | ||
|
|
679eddf296 | ||
|
|
d991def36c | ||
|
|
8b195533d2 | ||
|
|
3ccdd57c26 | ||
|
|
a103455dda | ||
|
|
60242e1930 | ||
|
|
a0e9bfbb48 | ||
|
|
2f19e0ba16 | ||
|
|
ab7fdcbbed | ||
|
|
48cf1de356 | ||
|
|
8d50122af0 | ||
|
|
fd379b38f7 | ||
|
|
aa4d865077 | ||
|
|
3e94febfc1 | ||
|
|
243b3a1a47 | ||
|
|
389e808c60 | ||
|
|
450465c4d4 | ||
|
|
2f44cc9f4d | ||
|
|
d106f3f43c | ||
|
|
bf3091d94f | ||
|
|
4dbf7285a8 | ||
|
|
a1e41e3a12 | ||
|
|
9d0044ce53 | ||
|
|
37bf74efb7 | ||
|
|
8c37d3b9d6 | ||
|
|
0af581f0bf | ||
|
|
2a506ea9d2 | ||
|
|
e5ec547eb3 | ||
|
|
6d383202dc | ||
|
|
7b8166d7b3 | ||
|
|
3636bbad3a | ||
|
|
82f9f3e552 | ||
|
|
c96ad1c945 | ||
|
|
5b9fbfa006 | ||
|
|
f0cc2d5506 | ||
|
|
d289ac81d3 | ||
|
|
15791f93fe | ||
|
|
d763b69740 | ||
|
|
36dd1af92d | ||
|
|
25b6dae7c0 | ||
|
|
1630c2533a | ||
|
|
1d373d77de | ||
|
|
f025b0d160 | ||
|
|
cc02b4729b | ||
|
|
2bb3830fc1 | ||
|
|
7a38bdd4b3 | ||
|
|
7614b17e85 | ||
|
|
6dfdb76538 | ||
|
|
9a3c51cff1 | ||
|
|
6336d2f9e1 | ||
|
|
ec6677e551 | ||
|
|
20cbca699a | ||
|
|
3211a9648a | ||
|
|
0baa915343 | ||
|
|
5cddb264d4 | ||
|
|
9863223fbf | ||
|
|
9a1948e67d | ||
|
|
f9deeac632 | ||
|
|
29f0021837 | ||
|
|
7acddb871d |
288
bugs
Normal file
288
bugs
Normal file
@@ -0,0 +1,288 @@
|
||||
|
||||
** 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)
|
||||
|
||||
** lbaselib.c
|
||||
Fri Dec 21 15:21:05 EDT 2001
|
||||
>> seg. fault when rawget/rawset get extra arguments
|
||||
(by Eric Mauger; since 4.0b)
|
||||
|
||||
** lvm.c
|
||||
Wed Jun 19 13:28:20 EST 2002
|
||||
>> line hook gets wrong `ar'
|
||||
(by Daniel C. Sinclair; since 4.0.b)
|
||||
|
||||
** ldo.c
|
||||
Wed Jun 19 13:31:49 EST 2002
|
||||
>> `protectedparser' may run GC, and then collect `filename'
|
||||
>> (in function `parse_file')
|
||||
(by Alex Bilyk; since 4.0)
|
||||
|
||||
191
fallback.c
191
fallback.c
@@ -1,191 +0,0 @@
|
||||
/*
|
||||
** fallback.c
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_fallback="$Id: fallback.c,v 1.24 1996/04/22 18:00:37 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mem.h"
|
||||
#include "fallback.h"
|
||||
#include "opcode.h"
|
||||
#include "lua.h"
|
||||
#include "table.h"
|
||||
|
||||
|
||||
static void errorFB (void);
|
||||
static void indexFB (void);
|
||||
static void gettableFB (void);
|
||||
static void arithFB (void);
|
||||
static void concatFB (void);
|
||||
static void orderFB (void);
|
||||
static void GDFB (void);
|
||||
static void funcFB (void);
|
||||
|
||||
|
||||
/*
|
||||
** Warning: This list must be in the same order as the #define's
|
||||
*/
|
||||
struct FB luaI_fallBacks[] = {
|
||||
{"error", {LUA_T_CFUNCTION, {errorFB}}, 1, 0},
|
||||
{"index", {LUA_T_CFUNCTION, {indexFB}}, 2, 1},
|
||||
{"gettable", {LUA_T_CFUNCTION, {gettableFB}}, 2, 1},
|
||||
{"arith", {LUA_T_CFUNCTION, {arithFB}}, 3, 1},
|
||||
{"order", {LUA_T_CFUNCTION, {orderFB}}, 3, 1},
|
||||
{"concat", {LUA_T_CFUNCTION, {concatFB}}, 2, 1},
|
||||
{"settable", {LUA_T_CFUNCTION, {gettableFB}}, 3, 0},
|
||||
{"gc", {LUA_T_CFUNCTION, {GDFB}}, 1, 0},
|
||||
{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1},
|
||||
/* no fixed number of params or results */
|
||||
{"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1}
|
||||
/* same default behavior of index FB */
|
||||
};
|
||||
|
||||
#define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
|
||||
|
||||
void luaI_setfallback (void)
|
||||
{
|
||||
int i;
|
||||
char *name = lua_getstring(lua_getparam(1));
|
||||
lua_Object func = lua_getparam(2);
|
||||
if (name == NULL || !lua_isfunction(func))
|
||||
lua_error("incorrect argument to function `setfallback'");
|
||||
for (i=0; i<N_FB; i++)
|
||||
{
|
||||
if (strcmp(luaI_fallBacks[i].kind, name) == 0)
|
||||
{
|
||||
luaI_pushobject(&luaI_fallBacks[i].function);
|
||||
luaI_fallBacks[i].function = *luaI_Address(func);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* name not found */
|
||||
lua_error("incorrect argument to function `setfallback'");
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
|
||||
static void indexFB (void)
|
||||
{
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
|
||||
static void gettableFB (void)
|
||||
{
|
||||
lua_error("indexed expression not a table");
|
||||
}
|
||||
|
||||
|
||||
static void arithFB (void)
|
||||
{
|
||||
lua_error("unexpected type at conversion to number");
|
||||
}
|
||||
|
||||
static void concatFB (void)
|
||||
{
|
||||
lua_error("unexpected type at conversion to string");
|
||||
}
|
||||
|
||||
|
||||
static void orderFB (void)
|
||||
{
|
||||
lua_error("unexpected type at comparison");
|
||||
}
|
||||
|
||||
static void GDFB (void) { }
|
||||
|
||||
static void funcFB (void)
|
||||
{
|
||||
lua_error("call expression not a function");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Reference routines
|
||||
*/
|
||||
|
||||
static struct ref {
|
||||
Object o;
|
||||
enum {LOCK, HOLD, FREE, COLLECTED} status;
|
||||
} *refArray = NULL;
|
||||
static int refSize = 0;
|
||||
|
||||
int luaI_ref (Object *object, int lock)
|
||||
{
|
||||
int i;
|
||||
int oldSize;
|
||||
if (tag(object) == LUA_T_NIL)
|
||||
return -1; /* special ref for nil */
|
||||
for (i=0; i<refSize; i++)
|
||||
if (refArray[i].status == FREE)
|
||||
goto found;
|
||||
/* no more empty spaces */
|
||||
oldSize = refSize;
|
||||
refSize = growvector(&refArray, refSize, struct ref, refEM, MAX_WORD);
|
||||
for (i=oldSize; i<refSize; i++)
|
||||
refArray[i].status = FREE;
|
||||
i = oldSize;
|
||||
found:
|
||||
refArray[i].o = *object;
|
||||
refArray[i].status = lock ? LOCK : HOLD;
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
void lua_unref (int ref)
|
||||
{
|
||||
if (ref >= 0 && ref < refSize)
|
||||
refArray[ref].status = FREE;
|
||||
}
|
||||
|
||||
|
||||
Object *luaI_getref (int ref)
|
||||
{
|
||||
static Object nul = {LUA_T_NIL, {0}};
|
||||
if (ref == -1)
|
||||
return &nul;
|
||||
if (ref >= 0 && ref < refSize &&
|
||||
(refArray[ref].status == LOCK || refArray[ref].status == HOLD))
|
||||
return &refArray[ref].o;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void luaI_travlock (int (*fn)(Object *))
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<refSize; i++)
|
||||
if (refArray[i].status == LOCK)
|
||||
fn(&refArray[i].o);
|
||||
}
|
||||
|
||||
|
||||
void luaI_invalidaterefs (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<refSize; i++)
|
||||
if (refArray[i].status == HOLD && !luaI_ismarked(&refArray[i].o))
|
||||
refArray[i].status = COLLECTED;
|
||||
}
|
||||
|
||||
char *luaI_travfallbacks (int (*fn)(Object *))
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<N_FB; i++)
|
||||
if (fn(&luaI_fallBacks[i].function))
|
||||
return luaI_fallBacks[i].kind;
|
||||
return NULL;
|
||||
}
|
||||
37
fallback.h
37
fallback.h
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
** $Id: fallback.h,v 1.12 1996/04/22 18:00:37 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef fallback_h
|
||||
#define fallback_h
|
||||
|
||||
#include "lua.h"
|
||||
#include "opcode.h"
|
||||
|
||||
extern struct FB {
|
||||
char *kind;
|
||||
Object function;
|
||||
int nParams;
|
||||
int nResults;
|
||||
} luaI_fallBacks[];
|
||||
|
||||
#define FB_ERROR 0
|
||||
#define FB_INDEX 1
|
||||
#define FB_GETTABLE 2
|
||||
#define FB_ARITH 3
|
||||
#define FB_ORDER 4
|
||||
#define FB_CONCAT 5
|
||||
#define FB_SETTABLE 6
|
||||
#define FB_GC 7
|
||||
#define FB_FUNCTION 8
|
||||
#define FB_GETGLOBAL 9
|
||||
|
||||
void luaI_setfallback (void);
|
||||
int luaI_ref (Object *object, int lock);
|
||||
Object *luaI_getref (int ref);
|
||||
void luaI_travlock (int (*fn)(Object *));
|
||||
void luaI_invalidaterefs (void);
|
||||
char *luaI_travfallbacks (int (*fn)(Object *));
|
||||
|
||||
#endif
|
||||
|
||||
156
func.c
156
func.c
@@ -1,156 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "luadebug.h"
|
||||
#include "table.h"
|
||||
#include "mem.h"
|
||||
#include "func.h"
|
||||
#include "opcode.h"
|
||||
|
||||
|
||||
static TFunc *function_root = NULL;
|
||||
static LocVar *currvars = NULL;
|
||||
static int numcurrvars = 0;
|
||||
static int maxcurrvars = 0;
|
||||
|
||||
|
||||
/*
|
||||
** Initialize TFunc struct
|
||||
*/
|
||||
void luaI_initTFunc (TFunc *f)
|
||||
{
|
||||
f->next = NULL;
|
||||
f->marked = 0;
|
||||
f->size = 0;
|
||||
f->code = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->fileName = NULL;
|
||||
f->locvars = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
** Insert function in list for GC
|
||||
*/
|
||||
void luaI_insertfunction (TFunc *f)
|
||||
{
|
||||
lua_pack();
|
||||
f->next = function_root;
|
||||
function_root = f;
|
||||
f->marked = 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Free function
|
||||
*/
|
||||
void luaI_freefunc (TFunc *f)
|
||||
{
|
||||
luaI_free (f->code);
|
||||
luaI_free (f->locvars);
|
||||
luaI_free (f);
|
||||
}
|
||||
|
||||
/*
|
||||
** Garbage collection function.
|
||||
** This function traverse the function list freeing unindexed functions
|
||||
*/
|
||||
Long luaI_funccollector (void)
|
||||
{
|
||||
TFunc *curr = function_root;
|
||||
TFunc *prev = NULL;
|
||||
Long counter = 0;
|
||||
while (curr)
|
||||
{
|
||||
TFunc *next = curr->next;
|
||||
if (!curr->marked)
|
||||
{
|
||||
if (prev == NULL)
|
||||
function_root = next;
|
||||
else
|
||||
prev->next = next;
|
||||
luaI_freefunc (curr);
|
||||
++counter;
|
||||
}
|
||||
else
|
||||
{
|
||||
curr->marked = 0;
|
||||
prev = curr;
|
||||
}
|
||||
curr = next;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
|
||||
void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
|
||||
{
|
||||
Object *f = luaI_Address(func);
|
||||
if (f->tag == LUA_T_MARK || f->tag == LUA_T_FUNCTION)
|
||||
{
|
||||
*filename = f->value.tf->fileName;
|
||||
*linedefined = f->value.tf->lineDefined;
|
||||
}
|
||||
else if (f->tag == LUA_T_CMARK || f->tag == LUA_T_CFUNCTION)
|
||||
{
|
||||
*filename = "(C)";
|
||||
*linedefined = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Stores information to know that variable has been declared in given line
|
||||
*/
|
||||
void luaI_registerlocalvar (TaggedString *varname, int line)
|
||||
{
|
||||
if (numcurrvars >= maxcurrvars)
|
||||
maxcurrvars = growvector(&currvars, maxcurrvars, LocVar, "", MAX_WORD);
|
||||
currvars[numcurrvars].varname = varname;
|
||||
currvars[numcurrvars].line = line;
|
||||
numcurrvars++;
|
||||
}
|
||||
|
||||
/*
|
||||
** Stores information to know that variable has been out of scope in given line
|
||||
*/
|
||||
void luaI_unregisterlocalvar (int line)
|
||||
{
|
||||
luaI_registerlocalvar(NULL, line);
|
||||
}
|
||||
|
||||
/*
|
||||
** Copies "currvars" into a new area and store it in function header.
|
||||
** The values (varname = NULL, line = -1) signal the end of vector.
|
||||
*/
|
||||
void luaI_closelocalvars (TFunc *func)
|
||||
{
|
||||
func->locvars = newvector (numcurrvars+1, LocVar);
|
||||
memcpy (func->locvars, currvars, numcurrvars*sizeof(LocVar));
|
||||
func->locvars[numcurrvars].varname = NULL;
|
||||
func->locvars[numcurrvars].line = -1;
|
||||
numcurrvars = 0; /* prepares for next function */
|
||||
}
|
||||
|
||||
/*
|
||||
** Look for n-esim local variable at line "line" in function "func".
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
char *luaI_getlocalname (TFunc *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;
|
||||
}
|
||||
else /* unregister */
|
||||
if (--count < local_number)
|
||||
varname = NULL;
|
||||
}
|
||||
return varname;
|
||||
}
|
||||
|
||||
44
func.h
44
func.h
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
** $Id: func.h,v 1.7 1996/03/08 12:04:04 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef func_h
|
||||
#define func_h
|
||||
|
||||
#include "types.h"
|
||||
#include "lua.h"
|
||||
#include "tree.h"
|
||||
|
||||
typedef struct LocVar
|
||||
{
|
||||
TaggedString *varname; /* NULL signals end of scope */
|
||||
int line;
|
||||
} LocVar;
|
||||
|
||||
|
||||
/*
|
||||
** Function Headers
|
||||
*/
|
||||
typedef struct TFunc
|
||||
{
|
||||
struct TFunc *next;
|
||||
int marked;
|
||||
int size;
|
||||
Byte *code;
|
||||
int lineDefined;
|
||||
char *fileName;
|
||||
LocVar *locvars;
|
||||
} TFunc;
|
||||
|
||||
Long luaI_funccollector (void);
|
||||
void luaI_insertfunction (TFunc *f);
|
||||
|
||||
void luaI_initTFunc (TFunc *f);
|
||||
void luaI_freefunc (TFunc *f);
|
||||
|
||||
void luaI_registerlocalvar (TaggedString *varname, int line);
|
||||
void luaI_unregisterlocalvar (int line);
|
||||
void luaI_closelocalvars (TFunc *func);
|
||||
char *luaI_getlocalname (TFunc *func, int local_number, int line);
|
||||
|
||||
#endif
|
||||
323
hash.c
323
hash.c
@@ -1,323 +0,0 @@
|
||||
/*
|
||||
** hash.c
|
||||
** hash manager for lua
|
||||
*/
|
||||
|
||||
char *rcs_hash="$Id: hash.c,v 2.29 1996/02/14 18:25:04 roberto Exp roberto $";
|
||||
|
||||
|
||||
#include "mem.h"
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define nhash(t) ((t)->nhash)
|
||||
#define nuse(t) ((t)->nuse)
|
||||
#define markarray(t) ((t)->mark)
|
||||
#define nodevector(t) ((t)->node)
|
||||
#define node(t,i) (&(t)->node[i])
|
||||
#define ref(n) (&(n)->ref)
|
||||
#define val(n) (&(n)->val)
|
||||
|
||||
|
||||
#define REHASH_LIMIT 0.70 /* avoid more than this % full */
|
||||
|
||||
|
||||
static Hash *listhead = NULL;
|
||||
|
||||
|
||||
/* hash dimensions values */
|
||||
static Long dimensions[] =
|
||||
{3L, 5L, 7L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
|
||||
12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
|
||||
1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
|
||||
|
||||
int luaI_redimension (int nhash)
|
||||
{
|
||||
int i;
|
||||
for (i=0; dimensions[i]<MAX_INT; i++)
|
||||
{
|
||||
if (dimensions[i] > nhash)
|
||||
return dimensions[i];
|
||||
}
|
||||
lua_error("table overflow");
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
static int hashindex (Hash *t, Object *ref) /* hash function */
|
||||
{
|
||||
switch (tag(ref))
|
||||
{
|
||||
case LUA_T_NIL:
|
||||
lua_error ("unexpected type to index table");
|
||||
return -1; /* UNREACHEABLE */
|
||||
case LUA_T_NUMBER:
|
||||
return (((int)nvalue(ref))%nhash(t));
|
||||
case LUA_T_STRING:
|
||||
return (int)((tsvalue(ref)->hash)%nhash(t)); /* make it a valid index */
|
||||
case LUA_T_FUNCTION:
|
||||
return (((IntPoint)ref->value.tf)%nhash(t));
|
||||
case LUA_T_CFUNCTION:
|
||||
return (((IntPoint)fvalue(ref))%nhash(t));
|
||||
case LUA_T_ARRAY:
|
||||
return (((IntPoint)avalue(ref))%nhash(t));
|
||||
default: /* user data */
|
||||
return (((IntPoint)uvalue(ref))%nhash(t));
|
||||
}
|
||||
}
|
||||
|
||||
int lua_equalObj (Object *t1, Object *t2)
|
||||
{
|
||||
if (tag(t1) != tag(t2)) return 0;
|
||||
switch (tag(t1))
|
||||
{
|
||||
case LUA_T_NIL: return 1;
|
||||
case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
|
||||
case LUA_T_STRING: return svalue(t1) == svalue(t2);
|
||||
case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
|
||||
case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;
|
||||
case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
|
||||
default: return uvalue(t1) == uvalue(t2);
|
||||
}
|
||||
}
|
||||
|
||||
static int present (Hash *t, Object *ref)
|
||||
{
|
||||
int h = hashindex(t, ref);
|
||||
while (tag(ref(node(t, h))) != LUA_T_NIL)
|
||||
{
|
||||
if (lua_equalObj(ref, ref(node(t, h))))
|
||||
return h;
|
||||
h = (h+1) % nhash(t);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Alloc a vector node
|
||||
*/
|
||||
static Node *hashnodecreate (int nhash)
|
||||
{
|
||||
int i;
|
||||
Node *v = newvector (nhash, Node);
|
||||
for (i=0; i<nhash; i++)
|
||||
tag(ref(&v[i])) = LUA_T_NIL;
|
||||
return v;
|
||||
}
|
||||
|
||||
/*
|
||||
** Create a new hash. Return the hash pointer or NULL on error.
|
||||
*/
|
||||
static Hash *hashcreate (int nhash)
|
||||
{
|
||||
Hash *t = new(Hash);
|
||||
nhash = luaI_redimension((int)((float)nhash/REHASH_LIMIT));
|
||||
nodevector(t) = hashnodecreate(nhash);
|
||||
nhash(t) = nhash;
|
||||
nuse(t) = 0;
|
||||
markarray(t) = 0;
|
||||
return t;
|
||||
}
|
||||
|
||||
/*
|
||||
** Delete a hash
|
||||
*/
|
||||
static void hashdelete (Hash *t)
|
||||
{
|
||||
luaI_free (nodevector(t));
|
||||
luaI_free(t);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Mark a hash and check its elements
|
||||
*/
|
||||
void lua_hashmark (Hash *h)
|
||||
{
|
||||
if (markarray(h) == 0)
|
||||
{
|
||||
int i;
|
||||
markarray(h) = 1;
|
||||
for (i=0; i<nhash(h); i++)
|
||||
{
|
||||
Node *n = node(h,i);
|
||||
if (tag(ref(n)) != LUA_T_NIL)
|
||||
{
|
||||
lua_markobject(&n->ref);
|
||||
lua_markobject(&n->val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void call_fallbacks (void)
|
||||
{
|
||||
Hash *curr_array;
|
||||
Object t;
|
||||
tag(&t) = LUA_T_ARRAY;
|
||||
for (curr_array = listhead; curr_array; curr_array = curr_array->next)
|
||||
if (markarray(curr_array) != 1)
|
||||
{
|
||||
avalue(&t) = curr_array;
|
||||
luaI_gcFB(&t);
|
||||
}
|
||||
tag(&t) = LUA_T_NIL;
|
||||
luaI_gcFB(&t); /* end of list */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage collection to arrays
|
||||
** Delete all unmarked arrays.
|
||||
*/
|
||||
Long lua_hashcollector (void)
|
||||
{
|
||||
Hash *curr_array = listhead, *prev = NULL;
|
||||
Long counter = 0;
|
||||
call_fallbacks();
|
||||
while (curr_array != NULL)
|
||||
{
|
||||
Hash *next = curr_array->next;
|
||||
if (markarray(curr_array) != 1)
|
||||
{
|
||||
if (prev == NULL) listhead = next;
|
||||
else prev->next = next;
|
||||
hashdelete(curr_array);
|
||||
++counter;
|
||||
}
|
||||
else
|
||||
{
|
||||
markarray(curr_array) = 0;
|
||||
prev = curr_array;
|
||||
}
|
||||
curr_array = next;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Create a new array
|
||||
** This function inserts the new array in the array list. It also
|
||||
** executes garbage collection if the number of arrays created
|
||||
** exceed a pre-defined range.
|
||||
*/
|
||||
Hash *lua_createarray (int nhash)
|
||||
{
|
||||
Hash *array;
|
||||
lua_pack();
|
||||
array = hashcreate(nhash);
|
||||
array->next = listhead;
|
||||
listhead = array;
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Re-hash
|
||||
*/
|
||||
static void rehash (Hash *t)
|
||||
{
|
||||
int i;
|
||||
int nold = nhash(t);
|
||||
Node *vold = nodevector(t);
|
||||
nhash(t) = luaI_redimension(nhash(t));
|
||||
nodevector(t) = hashnodecreate(nhash(t));
|
||||
for (i=0; i<nold; i++)
|
||||
{
|
||||
Node *n = vold+i;
|
||||
if (tag(ref(n)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL)
|
||||
*node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
|
||||
}
|
||||
luaI_free(vold);
|
||||
}
|
||||
|
||||
/*
|
||||
** If the hash node is present, return its pointer, otherwise return
|
||||
** null.
|
||||
*/
|
||||
Object *lua_hashget (Hash *t, Object *ref)
|
||||
{
|
||||
int h = present(t, ref);
|
||||
if (tag(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 new
|
||||
** node for the given reference and also return its pointer.
|
||||
*/
|
||||
Object *lua_hashdefine (Hash *t, Object *ref)
|
||||
{
|
||||
int h;
|
||||
Node *n;
|
||||
h = present(t, ref);
|
||||
n = node(t, h);
|
||||
if (tag(ref(n)) == LUA_T_NIL)
|
||||
{
|
||||
nuse(t)++;
|
||||
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)
|
||||
{
|
||||
rehash(t);
|
||||
h = present(t, ref);
|
||||
n = node(t, h);
|
||||
}
|
||||
*ref(n) = *ref;
|
||||
tag(val(n)) = LUA_T_NIL;
|
||||
}
|
||||
return (val(n));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Internal function to manipulate arrays.
|
||||
** Given an array object and a reference value, return the next element
|
||||
** in the hash.
|
||||
** This function pushs the element value and its reference to the stack.
|
||||
*/
|
||||
static void hashnext (Hash *t, int i)
|
||||
{
|
||||
if (i >= nhash(t))
|
||||
{
|
||||
lua_pushnil(); lua_pushnil();
|
||||
return;
|
||||
}
|
||||
while (tag(ref(node(t,i))) == LUA_T_NIL || tag(val(node(t,i))) == LUA_T_NIL)
|
||||
{
|
||||
if (++i >= nhash(t))
|
||||
{
|
||||
lua_pushnil(); lua_pushnil();
|
||||
return;
|
||||
}
|
||||
}
|
||||
luaI_pushobject(ref(node(t,i)));
|
||||
luaI_pushobject(val(node(t,i)));
|
||||
}
|
||||
|
||||
void lua_next (void)
|
||||
{
|
||||
Hash *t;
|
||||
lua_Object o = lua_getparam(1);
|
||||
lua_Object r = lua_getparam(2);
|
||||
if (o == LUA_NOOBJECT || r == LUA_NOOBJECT)
|
||||
lua_error ("too few arguments to function `next'");
|
||||
if (lua_getparam(3) != LUA_NOOBJECT)
|
||||
lua_error ("too many arguments to function `next'");
|
||||
if (!lua_istable(o))
|
||||
lua_error ("first argument of function `next' is not a table");
|
||||
t = avalue(luaI_Address(o));
|
||||
if (lua_isnil(r))
|
||||
{
|
||||
hashnext(t, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
int h = present (t, luaI_Address(r));
|
||||
hashnext(t, h+1);
|
||||
}
|
||||
}
|
||||
38
hash.h
38
hash.h
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
** hash.h
|
||||
** hash manager for lua
|
||||
** $Id: hash.h,v 2.11 1996/03/08 12:04:04 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef hash_h
|
||||
#define hash_h
|
||||
|
||||
#include "types.h"
|
||||
#include "opcode.h"
|
||||
|
||||
typedef struct node
|
||||
{
|
||||
Object ref;
|
||||
Object val;
|
||||
} Node;
|
||||
|
||||
typedef struct Hash
|
||||
{
|
||||
struct Hash *next;
|
||||
Node *node;
|
||||
int nhash;
|
||||
int nuse;
|
||||
char mark;
|
||||
} Hash;
|
||||
|
||||
|
||||
int lua_equalObj (Object *t1, Object *t2);
|
||||
int luaI_redimension (int nhash);
|
||||
Hash *lua_createarray (int nhash);
|
||||
void lua_hashmark (Hash *h);
|
||||
Long lua_hashcollector (void);
|
||||
Object *lua_hashget (Hash *t, Object *ref);
|
||||
Object *lua_hashdefine (Hash *t, Object *ref);
|
||||
void lua_next (void);
|
||||
|
||||
#endif
|
||||
246
inout.c
246
inout.c
@@ -1,246 +0,0 @@
|
||||
/*
|
||||
** inout.c
|
||||
** Provide function to realise the input/output function and debugger
|
||||
** facilities.
|
||||
** Also provides some predefined lua functions.
|
||||
*/
|
||||
|
||||
char *rcs_inout="$Id: inout.c,v 2.35 1996/03/19 16:50:24 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lex.h"
|
||||
#include "opcode.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
#include "tree.h"
|
||||
#include "lua.h"
|
||||
#include "mem.h"
|
||||
|
||||
|
||||
/* Exported variables */
|
||||
Word lua_linenumber;
|
||||
char *lua_parsedfile;
|
||||
|
||||
|
||||
static FILE *fp;
|
||||
static char *st;
|
||||
|
||||
/*
|
||||
** Function to get the next character from the input file
|
||||
*/
|
||||
static int fileinput (void)
|
||||
{
|
||||
return fgetc (fp);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to get the next character from the input string
|
||||
*/
|
||||
static int stringinput (void)
|
||||
{
|
||||
return *st++;
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to open a file to be input unit.
|
||||
** Return the file.
|
||||
*/
|
||||
FILE *lua_openfile (char *fn)
|
||||
{
|
||||
lua_setinput (fileinput);
|
||||
if (fn == NULL)
|
||||
{
|
||||
fp = stdin;
|
||||
fn = "(stdin)";
|
||||
}
|
||||
else
|
||||
fp = fopen (fn, "r");
|
||||
if (fp == NULL)
|
||||
return NULL;
|
||||
lua_linenumber = 1;
|
||||
lua_parsedfile = luaI_createfixedstring(fn)->str;
|
||||
return fp;
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to close an opened file
|
||||
*/
|
||||
void lua_closefile (void)
|
||||
{
|
||||
if (fp != NULL && fp != stdin)
|
||||
{
|
||||
fclose (fp);
|
||||
fp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to open a string to be input unit
|
||||
*/
|
||||
void lua_openstring (char *s)
|
||||
{
|
||||
lua_setinput (stringinput);
|
||||
st = s;
|
||||
lua_linenumber = 1;
|
||||
lua_parsedfile = luaI_createfixedstring("(string)")->str;
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to close an opened string
|
||||
*/
|
||||
void lua_closestring (void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Internal function: do a string
|
||||
*/
|
||||
void lua_internaldostring (void)
|
||||
{
|
||||
lua_Object obj = lua_getparam (1);
|
||||
if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
|
||||
lua_pushnumber(1);
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: do a file
|
||||
*/
|
||||
void lua_internaldofile (void)
|
||||
{
|
||||
lua_Object obj = lua_getparam (1);
|
||||
char *fname = NULL;
|
||||
if (lua_isstring(obj))
|
||||
fname = lua_getstring(obj);
|
||||
else if (obj != LUA_NOOBJECT)
|
||||
lua_error("invalid argument to function `dofile'");
|
||||
/* else fname = NULL */
|
||||
if (!lua_dofile(fname))
|
||||
lua_pushnumber(1);
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
|
||||
static char *tostring (lua_Object obj)
|
||||
{
|
||||
char *buff = luaI_buffer(20);
|
||||
if (lua_isstring(obj)) /* get strings and numbers */
|
||||
return lua_getstring(obj);
|
||||
else switch(lua_type(obj))
|
||||
{
|
||||
case LUA_T_FUNCTION:
|
||||
sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
|
||||
break;
|
||||
case LUA_T_CFUNCTION:
|
||||
sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
|
||||
break;
|
||||
case LUA_T_ARRAY:
|
||||
sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
|
||||
break;
|
||||
case LUA_T_NIL:
|
||||
sprintf(buff, "nil");
|
||||
break;
|
||||
default:
|
||||
sprintf(buff, "userdata: %p", lua_getuserdata(obj));
|
||||
break;
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
||||
void luaI_tostring (void)
|
||||
{
|
||||
lua_pushstring(tostring(lua_getparam(1)));
|
||||
}
|
||||
|
||||
void luaI_print (void)
|
||||
{
|
||||
int i = 1;
|
||||
lua_Object obj;
|
||||
while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
|
||||
printf("%s\n", tostring(obj));
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: return an object type.
|
||||
*/
|
||||
void luaI_type (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
int t;
|
||||
if (o == LUA_NOOBJECT)
|
||||
lua_error("no parameter to function 'type'");
|
||||
t = lua_type(o);
|
||||
switch (t)
|
||||
{
|
||||
case LUA_T_NIL :
|
||||
lua_pushliteral("nil");
|
||||
break;
|
||||
case LUA_T_NUMBER :
|
||||
lua_pushliteral("number");
|
||||
break;
|
||||
case LUA_T_STRING :
|
||||
lua_pushliteral("string");
|
||||
break;
|
||||
case LUA_T_ARRAY :
|
||||
lua_pushliteral("table");
|
||||
break;
|
||||
case LUA_T_FUNCTION :
|
||||
case LUA_T_CFUNCTION :
|
||||
lua_pushliteral("function");
|
||||
break;
|
||||
default :
|
||||
lua_pushliteral("userdata");
|
||||
break;
|
||||
}
|
||||
lua_pushnumber(t);
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: convert an object to a number
|
||||
*/
|
||||
void lua_obj2number (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isnumber(o))
|
||||
lua_pushnumber(lua_getnumber(o));
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
|
||||
void luaI_error (void)
|
||||
{
|
||||
char *s = lua_getstring(lua_getparam(1));
|
||||
if (s == NULL) s = "(no message)";
|
||||
lua_error(s);
|
||||
}
|
||||
|
||||
void luaI_assert (void)
|
||||
{
|
||||
lua_Object p = lua_getparam(1);
|
||||
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||
lua_error("assertion failed!");
|
||||
}
|
||||
|
||||
void luaI_setglobal (void)
|
||||
{
|
||||
lua_Object name = lua_getparam(1);
|
||||
lua_Object value = lua_getparam(2);
|
||||
if (!lua_isstring(name))
|
||||
lua_error("incorrect argument to function `setglobal'");
|
||||
lua_pushobject(value);
|
||||
lua_storeglobal(lua_getstring(name));
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
void luaI_getglobal (void)
|
||||
{
|
||||
lua_Object name = lua_getparam(1);
|
||||
if (!lua_isstring(name))
|
||||
lua_error("incorrect argument to function `getglobal'");
|
||||
lua_pushobject(lua_getglobal(lua_getstring(name)));
|
||||
}
|
||||
33
inout.h
33
inout.h
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
** $Id: inout.h,v 1.14 1996/03/15 13:13:13 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef inout_h
|
||||
#define inout_h
|
||||
|
||||
#include "types.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
extern Word lua_linenumber;
|
||||
extern Word lua_debugline;
|
||||
extern char *lua_parsedfile;
|
||||
|
||||
FILE *lua_openfile (char *fn);
|
||||
void lua_closefile (void);
|
||||
void lua_openstring (char *s);
|
||||
void lua_closestring (void);
|
||||
|
||||
void lua_internaldofile (void);
|
||||
void lua_internaldostring (void);
|
||||
void luaI_tostring (void);
|
||||
void luaI_print (void);
|
||||
void luaI_type (void);
|
||||
void lua_obj2number (void);
|
||||
void luaI_error (void);
|
||||
void luaI_assert (void);
|
||||
void luaI_setglobal (void);
|
||||
void luaI_getglobal (void);
|
||||
|
||||
#endif
|
||||
618
iolib.c
618
iolib.c
@@ -1,618 +0,0 @@
|
||||
/*
|
||||
** iolib.c
|
||||
** Input/output library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_iolib="$Id: iolib.c,v 1.43 1996/04/30 21:13:55 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
static FILE *in=stdin, *out=stdout;
|
||||
|
||||
|
||||
#ifdef POPEN
|
||||
FILE *popen();
|
||||
int pclose();
|
||||
#else
|
||||
#define popen(x,y) NULL /* that is, popen always fails */
|
||||
#define pclose(x) (-1)
|
||||
#endif
|
||||
|
||||
|
||||
static void pushresult (int i)
|
||||
{
|
||||
if (i)
|
||||
lua_pushnumber (1);
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
static void closeread (void)
|
||||
{
|
||||
if (in != stdin)
|
||||
{
|
||||
if (pclose(in) == -1)
|
||||
fclose(in);
|
||||
in = stdin;
|
||||
}
|
||||
}
|
||||
|
||||
static void closewrite (void)
|
||||
{
|
||||
if (out != stdout)
|
||||
{
|
||||
if (pclose(out) == -1)
|
||||
fclose(out);
|
||||
out = stdout;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Open a file to read.
|
||||
** LUA interface:
|
||||
** status = readfrom (filename)
|
||||
** where:
|
||||
** status = 1 -> success
|
||||
** status = nil -> error
|
||||
*/
|
||||
static void io_readfrom (void)
|
||||
{
|
||||
if (lua_getparam (1) == LUA_NOOBJECT)
|
||||
{ /* restore standart input */
|
||||
closeread();
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *s = lua_check_string(1, "readfrom");
|
||||
FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
|
||||
if (fp == NULL)
|
||||
lua_pushnil();
|
||||
else
|
||||
{
|
||||
closeread();
|
||||
in = fp;
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a file to write.
|
||||
** LUA interface:
|
||||
** status = writeto (filename)
|
||||
** where:
|
||||
** status = 1 -> success
|
||||
** status = nil -> error
|
||||
*/
|
||||
static void io_writeto (void)
|
||||
{
|
||||
if (lua_getparam (1) == LUA_NOOBJECT) /* restore standart output */
|
||||
{
|
||||
closewrite();
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *s = lua_check_string(1, "writeto");
|
||||
FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
|
||||
if (fp == NULL)
|
||||
lua_pushnil();
|
||||
else
|
||||
{
|
||||
closewrite();
|
||||
out = fp;
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a file to write appended.
|
||||
** LUA interface:
|
||||
** status = appendto (filename)
|
||||
** where:
|
||||
** status = 1 -> success
|
||||
** status = nil -> error
|
||||
*/
|
||||
static void io_appendto (void)
|
||||
{
|
||||
char *s = lua_check_string(1, "appendto");
|
||||
FILE *fp = fopen (s, "a");
|
||||
if (fp == NULL)
|
||||
lua_pushnil();
|
||||
else
|
||||
{
|
||||
if (out != stdout) fclose (out);
|
||||
out = fp;
|
||||
lua_pushnumber(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static char getformat (char *f, int *just, int *m, int *n)
|
||||
{
|
||||
int t;
|
||||
switch (*f++)
|
||||
{
|
||||
case 'q': case 'Q':
|
||||
case 's': case 'S':
|
||||
case 'i': case 'I':
|
||||
t = tolower(*(f-1));
|
||||
break;
|
||||
case 'f': case 'F': case 'g': case 'G': case 'e': case 'E':
|
||||
t = 'f';
|
||||
break;
|
||||
default:
|
||||
t = 0; /* to avoid compiler warnings */
|
||||
lua_arg_error("read/write (format)");
|
||||
}
|
||||
*just = (*f == '<' || *f == '>' || *f == '|') ? *f++ : '>';
|
||||
if (isdigit(*f))
|
||||
{
|
||||
*m = 0;
|
||||
while (isdigit(*f))
|
||||
*m = *m*10 + (*f++ - '0');
|
||||
}
|
||||
else
|
||||
*m = -1;
|
||||
if (*f == '.')
|
||||
{
|
||||
f++; /* skip point */
|
||||
*n = 0;
|
||||
while (isdigit(*f))
|
||||
*n = *n*10 + (*f++ - '0');
|
||||
}
|
||||
else
|
||||
*n = -1;
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Read a variable. On error put nil on stack.
|
||||
** LUA interface:
|
||||
** variable = read ([format])
|
||||
**
|
||||
** O formato pode ter um dos seguintes especificadores:
|
||||
**
|
||||
** s ou S -> para string
|
||||
** f ou F, g ou G, e ou E -> para reais
|
||||
** i ou I -> para inteiros
|
||||
**
|
||||
** Estes especificadores podem vir seguidos de numero que representa
|
||||
** o numero de campos a serem lidos.
|
||||
*/
|
||||
|
||||
static int read_until_char (int del)
|
||||
{
|
||||
int c;
|
||||
while((c = fgetc(in)) != EOF && c != del)
|
||||
luaI_addchar(c);
|
||||
return c;
|
||||
}
|
||||
|
||||
static void read_until_blank (void)
|
||||
{
|
||||
int c;
|
||||
while((c = fgetc(in)) != EOF && !isspace(c))
|
||||
luaI_addchar(c);
|
||||
if (c != EOF) ungetc(c,in);
|
||||
}
|
||||
|
||||
static void read_m (int m)
|
||||
{
|
||||
int c;
|
||||
while (m-- && (c = fgetc(in)) != EOF)
|
||||
luaI_addchar(c);
|
||||
}
|
||||
|
||||
|
||||
static void read_free (void)
|
||||
{
|
||||
int c;
|
||||
while (isspace(c=fgetc(in)))
|
||||
;
|
||||
if (c == EOF)
|
||||
{
|
||||
lua_pushnil();
|
||||
return;
|
||||
}
|
||||
if (c == '\"' || c == '\'')
|
||||
{ /* string */
|
||||
c = read_until_char(c);
|
||||
if (c == EOF)
|
||||
lua_pushnil();
|
||||
else
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
else
|
||||
{
|
||||
double d;
|
||||
char dummy;
|
||||
char *s;
|
||||
luaI_addchar(c);
|
||||
read_until_blank();
|
||||
s = luaI_addchar(0);
|
||||
if (sscanf(s, "%lf %c", &d, &dummy) == 1)
|
||||
lua_pushnumber(d);
|
||||
else
|
||||
lua_pushstring(s);
|
||||
}
|
||||
}
|
||||
|
||||
static void io_read (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
luaI_addchar(0); /* initialize buffer */
|
||||
if (o == LUA_NOOBJECT) /* free format */
|
||||
read_free();
|
||||
else /* formatted */
|
||||
{
|
||||
int m, dummy1, dummy2;
|
||||
switch (getformat(lua_check_string(1, "read"), &dummy1, &m, &dummy2))
|
||||
{
|
||||
case 's':
|
||||
{
|
||||
char *s;
|
||||
if (m < 0)
|
||||
read_until_blank();
|
||||
else
|
||||
read_m(m);
|
||||
s = luaI_addchar(0);
|
||||
if ((m >= 0 && strlen(s) == m) || (m < 0 && strlen(s) > 0))
|
||||
lua_pushstring(s);
|
||||
else
|
||||
lua_pushnil();
|
||||
break;
|
||||
}
|
||||
|
||||
case 'i': /* can read as float, since it makes no difference to Lua */
|
||||
case 'f':
|
||||
{
|
||||
double d;
|
||||
int result;
|
||||
if (m < 0)
|
||||
result = fscanf(in, "%lf", &d);
|
||||
else
|
||||
{
|
||||
read_m(m);
|
||||
result = sscanf(luaI_addchar(0), "%lf", &d);
|
||||
}
|
||||
if (result == 1)
|
||||
lua_pushnumber(d);
|
||||
else
|
||||
lua_pushnil();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
lua_arg_error("read (format)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Read characters until a given one. The delimiter is not read.
|
||||
*/
|
||||
static void io_readuntil (void)
|
||||
{
|
||||
int del, c;
|
||||
lua_Object p = lua_getparam(1);
|
||||
luaI_addchar(0); /* initialize buffer */
|
||||
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||
del = EOF;
|
||||
else
|
||||
del = *lua_check_string(1, "readuntil");
|
||||
c = read_until_char(del);
|
||||
if (c != EOF) ungetc(c,in);
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Write a variable. On error put 0 on stack, otherwise put 1.
|
||||
** LUA interface:
|
||||
** status = write (variable [,format])
|
||||
**
|
||||
** O formato pode ter um dos seguintes especificadores:
|
||||
**
|
||||
** s ou S -> para string
|
||||
** f ou F, g ou G, e ou E -> para reais
|
||||
** i ou I -> para inteiros
|
||||
**
|
||||
** Estes especificadores podem vir seguidos de:
|
||||
**
|
||||
** [?][m][.n]
|
||||
**
|
||||
** onde:
|
||||
** ? -> indica justificacao
|
||||
** < = esquerda
|
||||
** | = centro
|
||||
** > = direita (default)
|
||||
** m -> numero maximo de campos (se exceder estoura)
|
||||
** n -> indica precisao para
|
||||
** reais -> numero de casas decimais
|
||||
** inteiros -> numero minimo de digitos
|
||||
** string -> nao se aplica
|
||||
*/
|
||||
|
||||
static int write_fill (int n, int c)
|
||||
{
|
||||
while (n--)
|
||||
if (fputc(c, out) == EOF)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int write_string (char *s, int just, int m)
|
||||
{
|
||||
int status;
|
||||
int l = strlen(s);
|
||||
int pre; /* number of blanks before string */
|
||||
if (m < 0) m = l;
|
||||
else if (l > m)
|
||||
{
|
||||
write_fill(m, '*');
|
||||
return 0;
|
||||
}
|
||||
pre = (just == '<') ? 0 : (just == '>') ? m-l : (m-l)/2;
|
||||
status = write_fill(pre, ' ');
|
||||
status = status && fprintf(out, "%s", s) >= 0;
|
||||
status = status && write_fill(m-(l+pre), ' ');
|
||||
return status;
|
||||
}
|
||||
|
||||
static int write_quoted (int just, int m)
|
||||
{
|
||||
luaI_addchar(0);
|
||||
luaI_addquoted(lua_check_string(1, "write"));
|
||||
return write_string(luaI_addchar(0), just, m);
|
||||
}
|
||||
|
||||
static int write_float (int just, int m, int n)
|
||||
{
|
||||
char buffer[100];
|
||||
lua_Object p = lua_getparam(1);
|
||||
float number;
|
||||
if (!lua_isnumber(p)) return 0;
|
||||
number = lua_getnumber(p);
|
||||
if (n >= 0)
|
||||
sprintf(buffer, "%.*f", n, number);
|
||||
else
|
||||
sprintf(buffer, "%g", number);
|
||||
return write_string(buffer, just, m);
|
||||
}
|
||||
|
||||
|
||||
static int write_int (int just, int m, int n)
|
||||
{
|
||||
char buffer[100];
|
||||
lua_Object p = lua_getparam(1);
|
||||
int number;
|
||||
if (!lua_isnumber(p)) return 0;
|
||||
number = (int)lua_getnumber(p);
|
||||
if (n >= 0)
|
||||
sprintf(buffer, "%.*d", n, number);
|
||||
else
|
||||
sprintf(buffer, "%d", number);
|
||||
return write_string(buffer, just, m);
|
||||
}
|
||||
|
||||
|
||||
static void io_write (void)
|
||||
{
|
||||
int status = 0;
|
||||
if (lua_getparam (2) == LUA_NOOBJECT) /* free format */
|
||||
{
|
||||
lua_Object o1 = lua_getparam(1);
|
||||
int t = lua_type(o1);
|
||||
if (t == LUA_T_NUMBER)
|
||||
status = fprintf (out, "%g", lua_getnumber(o1)) >= 0;
|
||||
else if (t == LUA_T_STRING)
|
||||
status = fprintf (out, "%s", lua_getstring(o1)) >= 0;
|
||||
}
|
||||
else /* formated */
|
||||
{
|
||||
int just, m, n;
|
||||
switch (getformat(lua_check_string(2, "write"), &just, &m, &n))
|
||||
{
|
||||
case 's':
|
||||
{
|
||||
lua_Object p = lua_getparam(1);
|
||||
if (lua_isstring(p))
|
||||
status = write_string(lua_getstring(p), just, m);
|
||||
else
|
||||
status = 0;
|
||||
break;
|
||||
}
|
||||
case 'q':
|
||||
status = write_quoted(just, m);
|
||||
break;
|
||||
case 'f':
|
||||
status = write_float(just, m, n);
|
||||
break;
|
||||
case 'i':
|
||||
status = write_int(just, m, n);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (status)
|
||||
lua_pushnumber(status);
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
/*
|
||||
** Execute a executable program using "system".
|
||||
** Return the result of execution.
|
||||
*/
|
||||
static void io_execute (void)
|
||||
{
|
||||
lua_pushnumber(system(lua_check_string(1, "execute")));
|
||||
}
|
||||
|
||||
/*
|
||||
** Remove a file. On error return nil.
|
||||
*/
|
||||
static void io_remove (void)
|
||||
{
|
||||
pushresult(remove(lua_check_string(1, "remove")) == 0);
|
||||
}
|
||||
|
||||
static void io_rename (void)
|
||||
{
|
||||
char *f1 = lua_check_string(1, "rename");
|
||||
char *f2 = lua_check_string(2, "rename");
|
||||
pushresult(rename(f1, f2) == 0);
|
||||
}
|
||||
|
||||
static void io_tmpname (void)
|
||||
{
|
||||
lua_pushstring(tmpnam(NULL));
|
||||
}
|
||||
|
||||
static void io_errorno (void)
|
||||
{
|
||||
/* lua_pushstring(strerror(errno));*/
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** To get a environment variable
|
||||
*/
|
||||
static void io_getenv (void)
|
||||
{
|
||||
char *env = getenv(lua_check_string(1, "getenv"));
|
||||
lua_pushstring(env); /* if NULL push nil */
|
||||
}
|
||||
|
||||
/*
|
||||
** Return user formatted time stamp
|
||||
*/
|
||||
static void io_date (void)
|
||||
{
|
||||
time_t t;
|
||||
struct tm *tm;
|
||||
char *s;
|
||||
char b[BUFSIZ];
|
||||
if (lua_getparam(1) == LUA_NOOBJECT)
|
||||
s = "%c";
|
||||
else
|
||||
s = lua_check_string(1, "date");
|
||||
time(&t); tm = localtime(&t);
|
||||
if (strftime(b,sizeof(b),s,tm))
|
||||
lua_pushstring(b);
|
||||
else
|
||||
lua_error("invalid `date' format");
|
||||
}
|
||||
|
||||
/*
|
||||
** To exit
|
||||
*/
|
||||
static void io_exit (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
int code = lua_isnumber(o) ? (int)lua_getnumber(o) : 1;
|
||||
exit(code);
|
||||
}
|
||||
|
||||
/*
|
||||
** To debug a lua program. Start a dialog with the user, interpreting
|
||||
lua commands until an 'cont'.
|
||||
*/
|
||||
static void io_debug (void)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
char buffer[250];
|
||||
fprintf(stderr, "lua_debug> ");
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
|
||||
if (strcmp(buffer, "cont") == 0) return;
|
||||
lua_dostring(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void lua_printstack (FILE *f)
|
||||
{
|
||||
int level = 0;
|
||||
lua_Object func;
|
||||
fprintf(f, "Active Stack:\n");
|
||||
while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT)
|
||||
{
|
||||
char *name;
|
||||
int currentline;
|
||||
fprintf(f, "\t");
|
||||
switch (*lua_getobjname(func, &name))
|
||||
{
|
||||
case 'g':
|
||||
fprintf(f, "function %s", name);
|
||||
break;
|
||||
case 'f':
|
||||
fprintf(f, "`%s' fallback", name);
|
||||
break;
|
||||
default:
|
||||
{
|
||||
char *filename;
|
||||
int linedefined;
|
||||
lua_funcinfo(func, &filename, &linedefined);
|
||||
if (linedefined == 0)
|
||||
fprintf(f, "main of %s", filename);
|
||||
else if (linedefined < 0)
|
||||
fprintf(f, "%s", filename);
|
||||
else
|
||||
fprintf(f, "function (%s:%d)", filename, linedefined);
|
||||
}
|
||||
}
|
||||
if ((currentline = lua_currentline(func)) > 0)
|
||||
fprintf(f, " at line %d", currentline);
|
||||
fprintf(f, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void errorfb (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
char *s = lua_isstring(o) ? lua_getstring(o) : "(no messsage)";
|
||||
fprintf(stderr, "lua: %s\n", s);
|
||||
lua_printstack(stderr);
|
||||
}
|
||||
|
||||
|
||||
static struct lua_reg iolib[] = {
|
||||
{"readfrom", io_readfrom},
|
||||
{"writeto", io_writeto},
|
||||
{"appendto", io_appendto},
|
||||
{"read", io_read},
|
||||
{"readuntil",io_readuntil},
|
||||
{"write", io_write},
|
||||
{"execute", io_execute},
|
||||
{"remove", io_remove},
|
||||
{"rename", io_rename},
|
||||
{"tmpname", io_tmpname},
|
||||
{"ioerror", io_errorno},
|
||||
{"getenv", io_getenv},
|
||||
{"date", io_date},
|
||||
{"exit", io_exit},
|
||||
{"debug", io_debug},
|
||||
{"print_stack", errorfb}
|
||||
};
|
||||
|
||||
void iolib_open (void)
|
||||
{
|
||||
luaI_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
|
||||
lua_setfallback("error", errorfb);
|
||||
}
|
||||
765
lapi.c
Normal file
765
lapi.c
Normal file
@@ -0,0 +1,765 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 1.210 2002/08/07 14:24:24 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
const char lua_ident[] =
|
||||
"$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
||||
"$Authors: " LUA_AUTHORS " $\n"
|
||||
"$URL: www.lua.org $\n";
|
||||
|
||||
|
||||
|
||||
#ifndef api_check
|
||||
#define api_check(L, o) ((void)1)
|
||||
#endif
|
||||
|
||||
#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->ci->base))
|
||||
|
||||
#define api_incr_top(L) (api_check(L, L->top<L->ci->top), L->top++)
|
||||
|
||||
|
||||
|
||||
|
||||
static TObject *negindex (lua_State *L, int index) {
|
||||
if (index > LUA_REGISTRYINDEX) {
|
||||
api_check(L, index != 0 && -index <= L->top - L->ci->base);
|
||||
return L->top+index;
|
||||
}
|
||||
else switch (index) { /* pseudo-indices */
|
||||
case LUA_REGISTRYINDEX: return registry(L);
|
||||
case LUA_GLOBALSINDEX: return gt(L);
|
||||
default: {
|
||||
TObject *func = (L->ci->base - 1);
|
||||
index = LUA_GLOBALSINDEX - index;
|
||||
api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues);
|
||||
return &clvalue(func)->c.upvalue[index-1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define luaA_index(L, index) \
|
||||
( (index > 0) ? \
|
||||
(api_check(L, index <= L->top - L->ci->base), L->ci->base+index-1) : \
|
||||
negindex(L, index))
|
||||
|
||||
|
||||
static TObject *luaA_indexAcceptable (lua_State *L, int index) {
|
||||
if (index > 0) {
|
||||
TObject *o = L->ci->base+(index-1);
|
||||
api_check(L, index <= L->stack_last - L->ci->base);
|
||||
if (o >= L->top) return NULL;
|
||||
else return o;
|
||||
}
|
||||
else
|
||||
return negindex(L, index);
|
||||
}
|
||||
|
||||
|
||||
void luaA_pushobject (lua_State *L, const TObject *o) {
|
||||
setobj(L->top, o);
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_checkstack (lua_State *L, int size) {
|
||||
int res;
|
||||
lua_lock(L);
|
||||
if ((L->top - L->ci->base + size) > LUA_MAXCSTACK)
|
||||
res = 0; /* stack overflow */
|
||||
else {
|
||||
luaD_checkstack(L, size);
|
||||
if (L->ci->top < L->top + size)
|
||||
L->ci->top = L->top + size;
|
||||
res = 1;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
|
||||
lua_CFunction old;
|
||||
lua_lock(L);
|
||||
old = G(L)->panic;
|
||||
G(L)->panic = panicf;
|
||||
lua_unlock(L);
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** basic stack manipulation
|
||||
*/
|
||||
|
||||
|
||||
LUA_API int lua_gettop (lua_State *L) {
|
||||
return (L->top - L->ci->base);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_settop (lua_State *L, int index) {
|
||||
lua_lock(L);
|
||||
if (index >= 0) {
|
||||
api_check(L, index <= L->stack_last - L->ci->base);
|
||||
while (L->top < L->ci->base + index)
|
||||
setnilvalue(L->top++);
|
||||
L->top = L->ci->base + index;
|
||||
}
|
||||
else {
|
||||
api_check(L, -(index+1) <= (L->top - L->ci->base));
|
||||
L->top += index+1; /* `subtract' index (index is negative) */
|
||||
}
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_remove (lua_State *L, int index) {
|
||||
StkId p;
|
||||
lua_lock(L);
|
||||
p = luaA_index(L, index);
|
||||
while (++p < L->top) setobj(p-1, p);
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_insert (lua_State *L, int index) {
|
||||
StkId p;
|
||||
StkId q;
|
||||
lua_lock(L);
|
||||
p = luaA_index(L, index);
|
||||
for (q = L->top; q>p; q--) setobj(q, q-1);
|
||||
setobj(p, L->top);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_replace (lua_State *L, int index) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
setobj(luaA_index(L, index), L->top - 1);
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushvalue (lua_State *L, int index) {
|
||||
lua_lock(L);
|
||||
setobj(L->top, luaA_index(L, index));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** access functions (stack -> C)
|
||||
*/
|
||||
|
||||
|
||||
LUA_API int lua_type (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL) ? LUA_TNONE : ttype(o);
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_typename (lua_State *L, int t) {
|
||||
UNUSED(L);
|
||||
return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_iscfunction (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL) ? 0 : iscfunction(o);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_isnumber (lua_State *L, int index) {
|
||||
TObject n;
|
||||
const TObject *o = luaA_indexAcceptable(L, index);
|
||||
return (o != NULL && tonumber(o, &n));
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_isstring (lua_State *L, int index) {
|
||||
int t = lua_type(L, index);
|
||||
return (t == LUA_TSTRING || t == LUA_TNUMBER);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
|
||||
StkId o1 = luaA_indexAcceptable(L, index1);
|
||||
StkId o2 = luaA_indexAcceptable(L, index2);
|
||||
return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
|
||||
: luaO_rawequalObj(o1, o2);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_equal (lua_State *L, int index1, int index2) {
|
||||
StkId o1, o2;
|
||||
int i;
|
||||
lua_lock(L); /* may call tag method */
|
||||
o1 = luaA_indexAcceptable(L, index1);
|
||||
o2 = luaA_indexAcceptable(L, index2);
|
||||
i = (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
|
||||
: equalobj(L, o1, o2);
|
||||
lua_unlock(L);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
|
||||
StkId o1, o2;
|
||||
int i;
|
||||
lua_lock(L); /* may call tag method */
|
||||
o1 = luaA_indexAcceptable(L, index1);
|
||||
o2 = luaA_indexAcceptable(L, index2);
|
||||
i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
|
||||
: luaV_lessthan(L, o1, o2);
|
||||
lua_unlock(L);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
|
||||
TObject n;
|
||||
const TObject *o = luaA_indexAcceptable(L, index);
|
||||
if (o != NULL && tonumber(o, &n))
|
||||
return nvalue(o);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_toboolean (lua_State *L, int index) {
|
||||
const TObject *o = luaA_indexAcceptable(L, index);
|
||||
return (o != NULL) && !l_isfalse(o);
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_tostring (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
if (o == NULL)
|
||||
return NULL;
|
||||
else if (ttisstring(o))
|
||||
return svalue(o);
|
||||
else {
|
||||
const char *s;
|
||||
lua_lock(L); /* `luaV_tostring' may create a new string */
|
||||
s = (luaV_tostring(L, o) ? svalue(o) : NULL);
|
||||
lua_unlock(L);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API size_t lua_strlen (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
if (o == NULL)
|
||||
return 0;
|
||||
else if (ttisstring(o))
|
||||
return tsvalue(o)->tsv.len;
|
||||
else {
|
||||
size_t l;
|
||||
lua_lock(L); /* `luaV_tostring' may create a new string */
|
||||
l = (luaV_tostring(L, o) ? tsvalue(o)->tsv.len : 0);
|
||||
lua_unlock(L);
|
||||
return l;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->c.f;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void *lua_touserdata (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
if (o == NULL) return NULL;
|
||||
switch (ttype(o)) {
|
||||
case LUA_TUSERDATA: return (uvalue(o) + 1);
|
||||
case LUA_TLIGHTUSERDATA: return pvalue(o);
|
||||
default: return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API const void *lua_topointer (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
if (o == NULL) return NULL;
|
||||
else {
|
||||
switch (ttype(o)) {
|
||||
case LUA_TTABLE: return hvalue(o);
|
||||
case LUA_TFUNCTION: return clvalue(o);
|
||||
case LUA_TUSERDATA:
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
return lua_touserdata(L, index);
|
||||
default: return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** push functions (C -> stack)
|
||||
*/
|
||||
|
||||
|
||||
LUA_API void lua_pushnil (lua_State *L) {
|
||||
lua_lock(L);
|
||||
setnilvalue(L->top);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
||||
lua_lock(L);
|
||||
setnvalue(L->top, n);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
||||
lua_lock(L);
|
||||
setsvalue(L->top, luaS_newlstr(L, s, len));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushstring (lua_State *L, const char *s) {
|
||||
if (s == NULL)
|
||||
lua_pushnil(L);
|
||||
else
|
||||
lua_pushlstring(L, s, strlen(s));
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
|
||||
va_list argp) {
|
||||
const char *ret;
|
||||
lua_lock(L);
|
||||
ret = luaO_pushvfstring(L, fmt, argp);
|
||||
lua_unlock(L);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
|
||||
const char *ret;
|
||||
va_list argp;
|
||||
lua_lock(L);
|
||||
va_start(argp, fmt);
|
||||
ret = luaO_pushvfstring(L, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_unlock(L);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
Closure *cl;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
cl = luaF_newCclosure(L, n);
|
||||
cl->c.f = fn;
|
||||
L->top -= n;
|
||||
while (n--)
|
||||
setobj(&cl->c.upvalue[n], L->top+n);
|
||||
setclvalue(L->top, cl);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushboolean (lua_State *L, int b) {
|
||||
lua_lock(L);
|
||||
setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
|
||||
lua_lock(L);
|
||||
setpvalue(L->top, p);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
|
||||
|
||||
LUA_API void lua_gettable (lua_State *L, int index) {
|
||||
StkId t;
|
||||
const TObject *v;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
v = luaV_gettable(L, t, L->top-1, 0);
|
||||
setobj(L->top - 1, v);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawget (lua_State *L, int index) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttistable(t));
|
||||
setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
|
||||
StkId o;
|
||||
lua_lock(L);
|
||||
o = luaA_index(L, index);
|
||||
api_check(L, ttistable(o));
|
||||
setobj(L->top, luaH_getnum(hvalue(o), n));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_newtable (lua_State *L) {
|
||||
lua_lock(L);
|
||||
sethvalue(L->top, luaH_new(L, 0, 0));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_getmode (lua_State *L, int index) {
|
||||
static const char *const modes[] = {"", "k", "v", "kv"};
|
||||
int mode = 0;
|
||||
TObject *t;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttistable(t));
|
||||
if (hvalue(t)->mode & WEAKKEY) mode += 1;
|
||||
if (hvalue(t)->mode & WEAKVALUE) mode += 2;
|
||||
lua_unlock(L);
|
||||
return modes[mode];
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
||||
StkId obj;
|
||||
Table *mt;
|
||||
int res;
|
||||
lua_lock(L);
|
||||
obj = luaA_indexAcceptable(L, objindex);
|
||||
switch (ttype(obj)) {
|
||||
case LUA_TTABLE:
|
||||
mt = hvalue(obj)->metatable;
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
mt = uvalue(obj)->uv.metatable;
|
||||
break;
|
||||
default:
|
||||
mt = hvalue(defaultmeta(L));
|
||||
}
|
||||
if (mt == hvalue(defaultmeta(L)))
|
||||
res = 0;
|
||||
else {
|
||||
sethvalue(L->top, mt);
|
||||
api_incr_top(L);
|
||||
res = 1;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_getglobals (lua_State *L, int index) {
|
||||
StkId o;
|
||||
lua_lock(L);
|
||||
o = luaA_index(L, index);
|
||||
setobj(L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
|
||||
|
||||
LUA_API void lua_settable (lua_State *L, int index) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
t = luaA_index(L, index);
|
||||
luaV_settable(L, t, L->top - 2, L->top - 1);
|
||||
L->top -= 2; /* pop index and value */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawset (lua_State *L, int index) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttistable(t));
|
||||
setobj(luaH_set(L, hvalue(t), L->top-2), L->top-1);
|
||||
L->top -= 2;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawseti (lua_State *L, int index, int n) {
|
||||
StkId o;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
o = luaA_index(L, index);
|
||||
api_check(L, ttistable(o));
|
||||
setobj(luaH_setnum(L, hvalue(o), n), L->top-1);
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_setmode (lua_State *L, int index, const char *mode) {
|
||||
TObject *t;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttistable(t));
|
||||
hvalue(t)->mode &= ~(WEAKKEY | WEAKVALUE); /* clear bits */
|
||||
if (strchr(mode, 'k')) hvalue(t)->mode |= WEAKKEY;
|
||||
if (strchr(mode, 'v')) hvalue(t)->mode |= WEAKVALUE;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
||||
TObject *obj, *mt;
|
||||
int res = 1;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
obj = luaA_index(L, objindex);
|
||||
mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L);
|
||||
api_check(L, ttistable(mt));
|
||||
switch (ttype(obj)) {
|
||||
case LUA_TTABLE: {
|
||||
hvalue(obj)->metatable = hvalue(mt);
|
||||
break;
|
||||
}
|
||||
case LUA_TUSERDATA: {
|
||||
uvalue(obj)->uv.metatable = hvalue(mt);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
res = 0; /* cannot set */
|
||||
break;
|
||||
}
|
||||
}
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_setglobals (lua_State *L, int index) {
|
||||
StkId o;
|
||||
int res = 0;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
o = luaA_index(L, index);
|
||||
L->top--;
|
||||
api_check(L, ttistable(L->top));
|
||||
if (isLfunction(o)) {
|
||||
res = 1;
|
||||
clvalue(o)->l.g = *(L->top);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** `load' and `call' functions (run Lua code)
|
||||
*/
|
||||
|
||||
LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
|
||||
StkId func;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, nargs+1);
|
||||
func = L->top - (nargs+1);
|
||||
luaD_call(L, func, nresults);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
|
||||
int status;
|
||||
ptrdiff_t func;
|
||||
lua_lock(L);
|
||||
func = (errfunc == 0) ? 0 : savestack(L, luaA_index(L, errfunc));
|
||||
status = luaD_pcall(L, nargs, nresults, func);
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
|
||||
const char *chunkname) {
|
||||
ZIO z;
|
||||
int status;
|
||||
int c;
|
||||
lua_lock(L);
|
||||
if (!chunkname) chunkname = "?";
|
||||
luaZ_init(&z, reader, data, chunkname);
|
||||
c = luaZ_lookahead(&z);
|
||||
status = luaD_protectedparser(L, &z, (c == LUA_SIGNATURE[0]));
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage-collection functions
|
||||
*/
|
||||
|
||||
/* GC values are expressed in Kbytes: #bytes/2^10 */
|
||||
#define GCscale(x) (cast(int, (x)>>10))
|
||||
#define GCunscale(x) (cast(lu_mem, (x)<<10))
|
||||
|
||||
LUA_API int lua_getgcthreshold (lua_State *L) {
|
||||
int threshold;
|
||||
lua_lock(L);
|
||||
threshold = GCscale(G(L)->GCthreshold);
|
||||
lua_unlock(L);
|
||||
return threshold;
|
||||
}
|
||||
|
||||
LUA_API int lua_getgccount (lua_State *L) {
|
||||
int count;
|
||||
lua_lock(L);
|
||||
count = GCscale(G(L)->nblocks);
|
||||
lua_unlock(L);
|
||||
return count;
|
||||
}
|
||||
|
||||
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
|
||||
lua_lock(L);
|
||||
if (newthreshold > GCscale(ULONG_MAX))
|
||||
G(L)->GCthreshold = ULONG_MAX;
|
||||
else
|
||||
G(L)->GCthreshold = GCunscale(newthreshold);
|
||||
luaC_checkGC(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** miscellaneous functions
|
||||
*/
|
||||
|
||||
|
||||
LUA_API int lua_error (lua_State *L) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
luaG_errormsg(L);
|
||||
lua_unlock(L);
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_next (lua_State *L, int index) {
|
||||
StkId t;
|
||||
int more;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttistable(t));
|
||||
more = luaH_next(L, hvalue(t), L->top - 1);
|
||||
if (more) {
|
||||
api_incr_top(L);
|
||||
}
|
||||
else /* no more elements */
|
||||
L->top -= 1; /* remove key */
|
||||
lua_unlock(L);
|
||||
return more;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_concat (lua_State *L, int n) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
if (n >= 2) {
|
||||
luaV_concat(L, n, L->top - L->ci->base - 1);
|
||||
L->top -= (n-1);
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
else if (n == 0) { /* push empty string */
|
||||
setsvalue(L->top, luaS_newlstr(L, NULL, 0));
|
||||
api_incr_top(L);
|
||||
}
|
||||
/* else n == 1; nothing to do */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
|
||||
Udata *u;
|
||||
lua_lock(L);
|
||||
u = luaS_newudata(L, size);
|
||||
setuvalue(L->top, u);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return u + 1;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_pushupvalues (lua_State *L) {
|
||||
Closure *func;
|
||||
int n, i;
|
||||
lua_lock(L);
|
||||
api_check(L, iscfunction(L->ci->base - 1));
|
||||
func = clvalue(L->ci->base - 1);
|
||||
n = func->c.nupvalues;
|
||||
luaD_checkstack(L, n + LUA_MINSTACK);
|
||||
for (i=0; i<n; i++) {
|
||||
setobj(L->top, &func->c.upvalue[i]);
|
||||
L->top++;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
16
lapi.h
Normal file
16
lapi.h
Normal file
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
** $Id: lapi.h,v 1.20 2000/08/31 14:08:27 roberto Exp roberto $
|
||||
** Auxiliary functions from Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lapi_h
|
||||
#define lapi_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaA_pushobject (lua_State *L, const TObject *o);
|
||||
|
||||
#endif
|
||||
446
lauxlib.c
Normal file
446
lauxlib.c
Normal file
@@ -0,0 +1,446 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.82 2002/08/06 18:01:50 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef lua_filerror
|
||||
#include <errno.h>
|
||||
#define lua_fileerror (strerror(errno))
|
||||
#endif
|
||||
|
||||
|
||||
/* This file uses only the official API of Lua.
|
||||
** Any function declared here could be written as an application function.
|
||||
*/
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Error-report functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
|
||||
lua_Debug ar;
|
||||
lua_getstack(L, 0, &ar);
|
||||
lua_getinfo(L, "n", &ar);
|
||||
if (strcmp(ar.namewhat, "method") == 0) {
|
||||
narg--; /* do not count `self' */
|
||||
if (narg == 0) /* error is in the self argument itself? */
|
||||
return luaL_error(L,
|
||||
"calling %s on bad self (perhaps using `:' instead of `.')",
|
||||
ar.name);
|
||||
}
|
||||
if (ar.name == NULL)
|
||||
ar.name = "?";
|
||||
return luaL_error(L, "bad argument #%d to `%s' (%s)",
|
||||
narg, ar.name, extramsg);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
|
||||
const char *msg = lua_pushfstring(L, "%s expected, got %s",
|
||||
tname, lua_typename(L, lua_type(L,narg)));
|
||||
return luaL_argerror(L, narg, msg);
|
||||
}
|
||||
|
||||
|
||||
static void tag_error (lua_State *L, int narg, int tag) {
|
||||
luaL_typerror(L, narg, lua_typename(L, tag));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_where (lua_State *L, int level) {
|
||||
lua_Debug ar;
|
||||
if (lua_getstack(L, level, &ar)) { /* check function at level */
|
||||
lua_getinfo(L, "Snl", &ar); /* get info about it */
|
||||
if (ar.currentline > 0) { /* is there info? */
|
||||
lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
|
||||
return;
|
||||
}
|
||||
}
|
||||
lua_pushliteral(L, ""); /* else, no information available... */
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
luaL_where(L, 1);
|
||||
lua_pushvfstring(L, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_pushliteral(L, "\n");
|
||||
lua_concat(L, 3);
|
||||
return lua_error(L);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
|
||||
int i;
|
||||
for (i=0; list[i]; i++)
|
||||
if (strcmp(list[i], name) == 0)
|
||||
return i;
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) {
|
||||
if (!lua_checkstack(L, space))
|
||||
luaL_error(L, "stack overflow (%s)", mes);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_check_type (lua_State *L, int narg, int t) {
|
||||
if (lua_type(L, narg) != t)
|
||||
tag_error(L, narg, t);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_check_any (lua_State *L, int narg) {
|
||||
if (lua_type(L, narg) == LUA_TNONE)
|
||||
luaL_argerror(L, narg, "value expected");
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
|
||||
const 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 char *luaL_opt_lstr (lua_State *L, int narg, const char *def, size_t *len) {
|
||||
if (lua_isnoneornil(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_isnoneornil(L, narg)) return def;
|
||||
else return luaL_check_number(L, narg);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
|
||||
if (!lua_getmetatable(L, obj)) /* no metatable? */
|
||||
return 0;
|
||||
lua_pushstring(L, event);
|
||||
lua_rawget(L, -2);
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 2); /* remove metatable and metafield */
|
||||
return 0;
|
||||
}
|
||||
lua_pushvalue(L, obj);
|
||||
lua_call(L, 1, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup) {
|
||||
for (; l->name; l++) {
|
||||
int i;
|
||||
lua_pushstring(L, l->name);
|
||||
for (i=0; i<nup; i++) /* copy upvalues to the top */
|
||||
lua_pushvalue(L, -(nup+1));
|
||||
lua_pushcclosure(L, l->func, nup);
|
||||
lua_settable(L, -(nup+3));
|
||||
}
|
||||
lua_pop(L, nup);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
|
||||
const luaL_reg *l, int nup) {
|
||||
lua_pushstring(L, libname);
|
||||
lua_insert(L, -(nup+1));
|
||||
lua_newtable(L);
|
||||
lua_insert(L, -(nup+1));
|
||||
luaL_openlib(L, l, nup);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** 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 char *luaL_prepbuffer (luaL_Buffer *B) {
|
||||
if (emptybuffer(B))
|
||||
adjuststack(B);
|
||||
return B->buffer;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
|
||||
while (l--)
|
||||
luaL_putchar(B, *s++);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addstring (luaL_Buffer *B, const 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;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
int ref;
|
||||
lua_rawgeti(L, t, 0); /* get first free element */
|
||||
ref = (int)lua_tonumber(L, -1); /* ref = t[0] */
|
||||
lua_pop(L, 1); /* remove it from stack */
|
||||
if (ref != 0) { /* any free element? */
|
||||
lua_rawgeti(L, t, ref); /* remove it from list */
|
||||
lua_rawseti(L, t, 0); /* (that is, t[0] = t[ref]) */
|
||||
}
|
||||
else { /* no free elements */
|
||||
lua_pushliteral(L, "n");
|
||||
lua_pushvalue(L, -1);
|
||||
lua_rawget(L, t); /* get t.n */
|
||||
ref = (int)lua_tonumber(L, -1) + 1; /* ref = t.n + 1 */
|
||||
lua_pop(L, 1); /* pop t.n */
|
||||
lua_pushnumber(L, ref);
|
||||
lua_rawset(L, t); /* t.n = t.n + 1 */
|
||||
}
|
||||
lua_rawseti(L, t, ref);
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
||||
if (ref >= 0) {
|
||||
lua_rawgeti(L, t, 0);
|
||||
lua_rawseti(L, t, ref); /* t[ref] = t[0] */
|
||||
lua_pushnumber(L, ref);
|
||||
lua_rawseti(L, t, 0); /* t[0] = ref */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Load functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
typedef struct LoadF {
|
||||
FILE *f;
|
||||
char buff[LUAL_BUFFERSIZE];
|
||||
} LoadF;
|
||||
|
||||
|
||||
static const char *getF (lua_State *L, void *ud, size_t *size) {
|
||||
LoadF *lf = (LoadF *)ud;
|
||||
(void)L;
|
||||
if (feof(lf->f)) return NULL;
|
||||
*size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
|
||||
return (*size > 0) ? lf->buff : NULL;
|
||||
}
|
||||
|
||||
|
||||
static int errfile (lua_State *L, const char *filename) {
|
||||
if (filename == NULL) filename = "stdin";
|
||||
lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror);
|
||||
return LUA_ERRFILE;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
|
||||
LoadF lf;
|
||||
int status;
|
||||
int c;
|
||||
int old_top = lua_gettop(L);
|
||||
lf.f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (lf.f == NULL) return errfile(L, filename); /* unable to open file */
|
||||
c = ungetc(getc(lf.f), lf.f);
|
||||
if (!(isspace(c) || isprint(c)) && lf.f != stdin) { /* binary file? */
|
||||
fclose(lf.f);
|
||||
lf.f = fopen(filename, "rb"); /* reopen in binary mode */
|
||||
if (lf.f == NULL) return errfile(L, filename); /* unable to reopen file */
|
||||
}
|
||||
if (filename == NULL)
|
||||
lua_pushliteral(L, "=stdin");
|
||||
else
|
||||
lua_pushfstring(L, "@%s", filename);
|
||||
status = lua_load(L, getF, &lf, lua_tostring(L, -1));
|
||||
lua_remove(L, old_top+1); /* remove filename from stack */
|
||||
if (ferror(lf.f)) {
|
||||
lua_settop(L, old_top); /* ignore results from `lua_load' */
|
||||
return errfile(L, filename);
|
||||
}
|
||||
if (lf.f != stdin)
|
||||
fclose(lf.f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
typedef struct LoadS {
|
||||
const char *s;
|
||||
size_t size;
|
||||
} LoadS;
|
||||
|
||||
|
||||
static const char *getS (lua_State *L, void *ud, size_t *size) {
|
||||
LoadS *ls = (LoadS *)ud;
|
||||
(void)L;
|
||||
if (ls->size == 0) return NULL;
|
||||
*size = ls->size;
|
||||
ls->size = 0;
|
||||
return ls->s;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name) {
|
||||
LoadS ls;
|
||||
ls.s = buff;
|
||||
ls.size = size;
|
||||
return lua_load(L, getS, &ls, name);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** compatibility code
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static void callalert (lua_State *L, int status) {
|
||||
if (status != 0) {
|
||||
lua_getglobal(L, "_ALERT");
|
||||
lua_insert(L, -2);
|
||||
lua_call(L, 1, 0);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int aux_do (lua_State *L, int status) {
|
||||
if (status == 0) { /* parse OK? */
|
||||
status = lua_pcall(L, 0, LUA_MULTRET, 0); /* call main */
|
||||
}
|
||||
callalert(L, status);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int lua_dofile (lua_State *L, const char *filename) {
|
||||
return aux_do(L, luaL_loadfile(L, filename));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name) {
|
||||
return aux_do(L, luaL_loadbuffer(L, buff, size, name));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int lua_dostring (lua_State *L, const char *str) {
|
||||
return lua_dobuffer(L, str, strlen(str), str);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
130
lauxlib.h
Normal file
130
lauxlib.h
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.51 2002/07/01 19:23:58 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lauxlib_h
|
||||
#define lauxlib_h
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API extern
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
typedef struct luaL_reg {
|
||||
const char *name;
|
||||
lua_CFunction func;
|
||||
} luaL_reg;
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup);
|
||||
LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
|
||||
const luaL_reg *l, int nup);
|
||||
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event);
|
||||
LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname);
|
||||
LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg);
|
||||
LUALIB_API const char *luaL_check_lstr (lua_State *L, int numArg,
|
||||
size_t *len);
|
||||
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int numArg,
|
||||
const 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);
|
||||
|
||||
LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *msg);
|
||||
LUALIB_API void luaL_check_type (lua_State *L, int narg, int t);
|
||||
LUALIB_API void luaL_check_any (lua_State *L, int narg);
|
||||
|
||||
LUALIB_API void luaL_where (lua_State *L, int level);
|
||||
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...);
|
||||
|
||||
LUALIB_API int luaL_findstring (const char *name,
|
||||
const char *const list[]);
|
||||
|
||||
LUALIB_API int luaL_ref (lua_State *L, int t);
|
||||
LUALIB_API void luaL_unref (lua_State *L, int t, int ref);
|
||||
|
||||
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename);
|
||||
LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** 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))
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LUAL_BUFFERSIZE
|
||||
#define LUAL_BUFFERSIZE BUFSIZ
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct luaL_Buffer {
|
||||
char *p; /* current position in buffer */
|
||||
int level;
|
||||
lua_State *L;
|
||||
char buffer[LUAL_BUFFERSIZE];
|
||||
} luaL_Buffer;
|
||||
|
||||
#define luaL_putchar(B,c) \
|
||||
((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
|
||||
(*(B)->p++ = (char)(c)))
|
||||
|
||||
#define luaL_addsize(B,n) ((B)->p += (n))
|
||||
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B);
|
||||
LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B);
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
|
||||
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s);
|
||||
LUALIB_API void luaL_addvalue (luaL_Buffer *B);
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B);
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Compatibility macros
|
||||
*/
|
||||
|
||||
#define luaL_checktype luaL_check_type
|
||||
#define luaL_checkany luaL_check_any
|
||||
|
||||
LUALIB_API int lua_dofile (lua_State *L, const char *filename);
|
||||
LUALIB_API int lua_dostring (lua_State *L, const char *str);
|
||||
LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
625
lbaselib.c
Normal file
625
lbaselib.c
Normal file
@@ -0,0 +1,625 @@
|
||||
/*
|
||||
** $Id: lbaselib.c,v 1.96 2002/08/06 18:54:18 roberto Exp roberto $
|
||||
** Basic library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** 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, "tostring");
|
||||
for (i=1; i<=n; i++) {
|
||||
const char *s;
|
||||
lua_pushvalue(L, -1); /* function to be called */
|
||||
lua_pushvalue(L, i); /* value to print */
|
||||
lua_call(L, 1, 1);
|
||||
s = lua_tostring(L, -1); /* get result */
|
||||
if (s == NULL)
|
||||
return luaL_error(L, "`tostring' must return a string to `print'");
|
||||
if (i>1) fputs("\t", stdout);
|
||||
fputs(s, stdout);
|
||||
lua_pop(L, 1); /* pop result */
|
||||
}
|
||||
fputs("\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_check_any(L, 1);
|
||||
if (lua_isnumber(L, 1)) {
|
||||
lua_pushnumber(L, lua_tonumber(L, 1));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const char *s1 = luaL_check_string(L, 1);
|
||||
char *s2;
|
||||
unsigned long n;
|
||||
luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
n = strtoul(s1, &s2, base);
|
||||
if (s1 != s2) { /* at least one valid digit? */
|
||||
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
|
||||
if (*s2 == '\0') { /* no invalid trailing characters? */
|
||||
lua_pushnumber(L, n);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_pushnil(L); /* else not a number */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_error (lua_State *L) {
|
||||
int level = luaL_opt_int(L, 2, 1);
|
||||
luaL_check_any(L, 1);
|
||||
if (!lua_isstring(L, 1) || level == 0)
|
||||
lua_pushvalue(L, 1); /* propagate error mesage without changes */
|
||||
else { /* add extra information */
|
||||
luaL_where(L, level);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushliteral(L, "\n");
|
||||
lua_concat(L, 3);
|
||||
}
|
||||
return lua_error(L);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getmode (lua_State *L) {
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
lua_pushstring(L, lua_getmode(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_setmode (lua_State *L) {
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
lua_setmode(L, 1, luaL_check_string(L, 2));
|
||||
lua_settop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getmetatable (lua_State *L) {
|
||||
luaL_check_any(L, 1);
|
||||
if (!lua_getmetatable(L, 1)) {
|
||||
lua_pushnil(L);
|
||||
return 1; /* no metatable */
|
||||
}
|
||||
else {
|
||||
lua_pushliteral(L, "__metatable");
|
||||
lua_rawget(L, -2);
|
||||
if (lua_isnil(L, -1))
|
||||
lua_pop(L, 1);
|
||||
/* otherwise returns metatable.__metatable */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_setmetatable (lua_State *L) {
|
||||
int t = lua_type(L, 2);
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
|
||||
"nil or table expected");
|
||||
lua_settop(L, 2);
|
||||
lua_setmetatable(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void getfunc (lua_State *L) {
|
||||
if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
|
||||
else {
|
||||
lua_Debug ar;
|
||||
int level = luaL_opt_int(L, 1, 1);
|
||||
luaL_arg_check(L, level >= 0, 1, "level must be non-negative");
|
||||
if (lua_getstack(L, level, &ar) == 0)
|
||||
luaL_argerror(L, 1, "invalid level");
|
||||
lua_getinfo(L, "f", &ar);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getglobals (lua_State *L) {
|
||||
getfunc(L);
|
||||
lua_getglobals(L, -1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_setglobals (lua_State *L) {
|
||||
luaL_check_type(L, 2, LUA_TTABLE);
|
||||
getfunc(L);
|
||||
lua_pushvalue(L, 2);
|
||||
if (lua_setglobals(L, -2) == 0)
|
||||
luaL_error(L, "cannot change global table of given function");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_rawequal (lua_State *L) {
|
||||
luaL_check_any(L, 1);
|
||||
luaL_check_any(L, 2);
|
||||
lua_pushboolean(L, lua_rawequal(L, 1, 2));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_rawget (lua_State *L) {
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
luaL_check_any(L, 2);
|
||||
lua_rawget(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_rawset (lua_State *L) {
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
luaL_check_any(L, 2);
|
||||
luaL_check_any(L, 3);
|
||||
lua_rawset(L, 1);
|
||||
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_check_any(L, 1);
|
||||
lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_next (lua_State *L) {
|
||||
luaL_check_type(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 luaB_pairs (lua_State *L) {
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
lua_getglobal(L, "next"); /* return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushnil(L); /* and initial value */
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_ipairs (lua_State *L) {
|
||||
lua_Number i = lua_tonumber(L, 2);
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
if (i == 0 && lua_isnull(L, 2)) { /* `for' start? */
|
||||
lua_getglobal(L, "ipairs"); /* return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushnumber(L, 0); /* and initial value */
|
||||
return 3;
|
||||
}
|
||||
else { /* `for' step */
|
||||
i++; /* next value */
|
||||
lua_pushnumber(L, i);
|
||||
lua_rawgeti(L, 1, (int)i);
|
||||
return (lua_isnil(L, -1)) ? 0 : 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int passresults (lua_State *L, int status) {
|
||||
if (status == 0) return 1;
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
lua_insert(L, -2);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadstring (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
const char *chunkname = luaL_opt_string(L, 2, s);
|
||||
return passresults(L, luaL_loadbuffer(L, s, l, chunkname));
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadfile (lua_State *L) {
|
||||
const char *fname = luaL_opt_string(L, 1, NULL);
|
||||
return passresults(L, luaL_loadfile(L, fname));
|
||||
}
|
||||
|
||||
|
||||
static int luaB_dofile (lua_State *L) {
|
||||
const char *fname = luaL_opt_string(L, 1, NULL);
|
||||
int status = luaL_loadfile(L, fname);
|
||||
if (status != 0) lua_error(L);
|
||||
lua_call(L, 0, LUA_MULTRET);
|
||||
return lua_gettop(L) - 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_assert (lua_State *L) {
|
||||
luaL_check_any(L, 1);
|
||||
if (!lua_toboolean(L, 1))
|
||||
return luaL_error(L, "%s", luaL_opt_string(L, 2, "assertion failed!"));
|
||||
lua_settop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_unpack (lua_State *L) {
|
||||
int n, i;
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
lua_pushliteral(L, "n");
|
||||
lua_rawget(L, 1);
|
||||
n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1;
|
||||
for (i=0; i<n || n==-1; i++) { /* push arg[1...n] */
|
||||
luaL_check_stack(L, LUA_MINSTACK, "table too big to unpack");
|
||||
lua_rawgeti(L, 1, i+1);
|
||||
if (n == -1) { /* no explicit limit? */
|
||||
if (lua_isnil(L, -1)) { /* stop at first `nil' element */
|
||||
lua_pop(L, 1); /* remove `nil' */
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_pcall (lua_State *L) {
|
||||
int status;
|
||||
luaL_check_any(L, 1);
|
||||
status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
|
||||
lua_pushboolean(L, (status == 0));
|
||||
lua_insert(L, 1);
|
||||
return lua_gettop(L); /* return status + all results */
|
||||
}
|
||||
|
||||
|
||||
static int luaB_xpcall (lua_State *L) {
|
||||
int status;
|
||||
luaL_check_any(L, 2);
|
||||
lua_settop(L, 2);
|
||||
lua_insert(L, 1); /* put error function under function to be called */
|
||||
status = lua_pcall(L, 0, LUA_MULTRET, 1);
|
||||
lua_pushboolean(L, (status == 0));
|
||||
lua_replace(L, 1);
|
||||
return lua_gettop(L); /* return status + all results */
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tostring (lua_State *L) {
|
||||
char buff[64];
|
||||
luaL_checkany(L, 1);
|
||||
if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
|
||||
return 1; /* use its value */
|
||||
switch (lua_type(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_TBOOLEAN:
|
||||
lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
|
||||
return 1;
|
||||
case LUA_TTABLE:
|
||||
sprintf(buff, "table: %p", lua_topointer(L, 1));
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
sprintf(buff, "function: %p", lua_topointer(L, 1));
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
|
||||
break;
|
||||
case LUA_TNIL:
|
||||
lua_pushliteral(L, "nil");
|
||||
return 1;
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_newproxy (lua_State *L) {
|
||||
lua_settop(L, 1);
|
||||
lua_newuserdata(L, 0); /* create proxy */
|
||||
if (lua_toboolean(L, 1) == 0)
|
||||
return 1; /* no metatable */
|
||||
else if (lua_isboolean(L, 1)) {
|
||||
lua_newtable(L); /* create a new metatable `m' ... */
|
||||
lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
|
||||
lua_pushboolean(L, 1);
|
||||
lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
|
||||
}
|
||||
else {
|
||||
int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
|
||||
if (lua_getmetatable(L, 1)) {
|
||||
lua_rawget(L, lua_upvalueindex(1));
|
||||
validproxy = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1); /* remove value */
|
||||
}
|
||||
luaL_arg_check(L, validproxy, 1, "boolean/proxy expected");
|
||||
lua_getmetatable(L, 1); /* metatable is valid; get it */
|
||||
}
|
||||
lua_setmetatable(L, 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** `require' function
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/* name of global that holds table with loaded packages */
|
||||
#define REQTAB "_LOADED"
|
||||
|
||||
/* name of global that holds the search path for packages */
|
||||
#define LUA_PATH "LUA_PATH"
|
||||
|
||||
#ifndef LUA_PATH_SEP
|
||||
#define LUA_PATH_SEP ';'
|
||||
#endif
|
||||
|
||||
#ifndef LUA_PATH_DEFAULT
|
||||
#define LUA_PATH_DEFAULT "?;?.lua"
|
||||
#endif
|
||||
|
||||
|
||||
static const char *getpath (lua_State *L) {
|
||||
const char *path;
|
||||
lua_getglobal(L, LUA_PATH); /* try global variable */
|
||||
path = lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
if (path) return path;
|
||||
path = getenv(LUA_PATH); /* else try environment variable */
|
||||
if (path) return path;
|
||||
return LUA_PATH_DEFAULT; /* else use default */
|
||||
}
|
||||
|
||||
|
||||
static const char *pushnextpath (lua_State *L, const char *path) {
|
||||
const char *l;
|
||||
if (*path == '\0') return NULL; /* no more pathes */
|
||||
if (*path == LUA_PATH_SEP) path++; /* skip separator */
|
||||
l = strchr(path, LUA_PATH_SEP); /* find next separator */
|
||||
if (l == NULL) l = path+strlen(path);
|
||||
lua_pushlstring(L, path, l - path); /* directory name */
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
static void pushcomposename (lua_State *L) {
|
||||
const char *path = lua_tostring(L, -1);
|
||||
const char *wild = strchr(path, '?');
|
||||
if (wild == NULL) return; /* no wild char; path is the file name */
|
||||
lua_pushlstring(L, path, wild - path);
|
||||
lua_pushvalue(L, 1); /* package name */
|
||||
lua_pushstring(L, wild + 1);
|
||||
lua_concat(L, 3);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_require (lua_State *L) {
|
||||
const char *path;
|
||||
int status = LUA_ERRFILE; /* not found (yet) */
|
||||
luaL_check_string(L, 1);
|
||||
lua_settop(L, 1);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_setglobal(L, "_REQUIREDNAME");
|
||||
lua_getglobal(L, REQTAB);
|
||||
if (!lua_istable(L, 2)) return luaL_error(L, "`" REQTAB "' is not a table");
|
||||
path = getpath(L);
|
||||
lua_pushvalue(L, 1); /* check package's name in book-keeping table */
|
||||
lua_rawget(L, 2);
|
||||
if (!lua_isnil(L, -1)) /* is it there? */
|
||||
return 0; /* package is already loaded */
|
||||
else { /* must load it */
|
||||
while (status == LUA_ERRFILE) {
|
||||
lua_settop(L, 3); /* reset stack position */
|
||||
if ((path = pushnextpath(L, path)) == NULL) break;
|
||||
pushcomposename(L);
|
||||
status = luaL_loadfile(L, lua_tostring(L, -1)); /* try to load it */
|
||||
}
|
||||
}
|
||||
switch (status) {
|
||||
case 0: {
|
||||
lua_call(L, 0, 0); /* run loaded module */
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushboolean(L, 1);
|
||||
lua_rawset(L, 2); /* mark it as loaded */
|
||||
return 0;
|
||||
}
|
||||
case LUA_ERRFILE: { /* file not found */
|
||||
return luaL_error(L, "could not load package `%s' from path `%s'",
|
||||
lua_tostring(L, 1), getpath(L));
|
||||
}
|
||||
default: {
|
||||
return luaL_error(L, "error loading package\n%s", lua_tostring(L, -1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static const luaL_reg base_funcs[] = {
|
||||
{"error", luaB_error},
|
||||
{"getmetatable", luaB_getmetatable},
|
||||
{"setmetatable", luaB_setmetatable},
|
||||
{"getglobals", luaB_getglobals},
|
||||
{"setglobals", luaB_setglobals},
|
||||
{"getmode", luaB_getmode},
|
||||
{"setmode", luaB_setmode},
|
||||
{"next", luaB_next},
|
||||
{"ipairs", luaB_ipairs},
|
||||
{"pairs", luaB_pairs},
|
||||
{"print", luaB_print},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
{"type", luaB_type},
|
||||
{"assert", luaB_assert},
|
||||
{"unpack", luaB_unpack},
|
||||
{"rawequal", luaB_rawequal},
|
||||
{"rawget", luaB_rawget},
|
||||
{"rawset", luaB_rawset},
|
||||
{"pcall", luaB_pcall},
|
||||
{"xpcall", luaB_xpcall},
|
||||
{"collectgarbage", luaB_collectgarbage},
|
||||
{"gcinfo", luaB_gcinfo},
|
||||
{"loadfile", luaB_loadfile},
|
||||
{"dofile", luaB_dofile},
|
||||
{"loadstring", luaB_loadstring},
|
||||
{"require", luaB_require},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Coroutine library
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static int luaB_resume (lua_State *L) {
|
||||
lua_State *co = (lua_State *)lua_unboxpointer(L, lua_upvalueindex(1));
|
||||
int status;
|
||||
lua_settop(L, 0);
|
||||
status = lua_resume(L, co);
|
||||
if (status != 0)
|
||||
return lua_error(L);
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int gc_coroutine (lua_State *L) {
|
||||
lua_State *co = (lua_State *)lua_unboxpointer(L, 1);
|
||||
lua_closethread(L, co);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_coroutine (lua_State *L) {
|
||||
lua_State *NL;
|
||||
int ref;
|
||||
int i;
|
||||
int n = lua_gettop(L);
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
|
||||
"Lua function expected");
|
||||
NL = lua_newthread(L);
|
||||
if (NL == NULL) return luaL_error(L, "unable to create new thread");
|
||||
/* move function and arguments from L to NL */
|
||||
for (i=0; i<n; i++) {
|
||||
ref = lua_ref(L, 1);
|
||||
lua_getref(NL, ref);
|
||||
lua_insert(NL, 1);
|
||||
lua_unref(L, ref);
|
||||
}
|
||||
lua_cobegin(NL, n-1);
|
||||
lua_boxpointer(L, NL);
|
||||
lua_pushliteral(L, "Coroutine");
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
lua_setmetatable(L, -2);
|
||||
lua_pushcclosure(L, luaB_resume, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_yield (lua_State *L) {
|
||||
return lua_yield(L, lua_gettop(L));
|
||||
}
|
||||
|
||||
static const luaL_reg co_funcs[] = {
|
||||
{"create", luaB_coroutine},
|
||||
{"yield", luaB_yield},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
static void co_open (lua_State *L) {
|
||||
luaL_opennamedlib(L, LUA_COLIBNAME, co_funcs, 0);
|
||||
/* create metatable for coroutines */
|
||||
lua_pushliteral(L, "Coroutine");
|
||||
lua_newtable(L);
|
||||
lua_pushliteral(L, "__gc");
|
||||
lua_pushcfunction(L, gc_coroutine);
|
||||
lua_rawset(L, -3);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
static void base_open (lua_State *L) {
|
||||
lua_pushliteral(L, "_G");
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
luaL_openlib(L, base_funcs, 0); /* open lib into global table */
|
||||
lua_pushliteral(L, "_VERSION");
|
||||
lua_pushliteral(L, LUA_VERSION);
|
||||
lua_rawset(L, -3); /* set global _VERSION */
|
||||
/* `newproxy' needs a weaktable as upvalue */
|
||||
lua_pushliteral(L, "newproxy");
|
||||
lua_newtable(L); /* new table `w' */
|
||||
lua_setmode(L, -1, "k");
|
||||
lua_pushcclosure(L, luaB_newproxy, 1);
|
||||
lua_rawset(L, -3); /* set global `newproxy' */
|
||||
lua_rawset(L, -1); /* set global _G */
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int lua_baselibopen (lua_State *L) {
|
||||
base_open(L);
|
||||
co_open(L);
|
||||
lua_newtable(L);
|
||||
lua_setglobal(L, REQTAB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
744
lcode.c
Normal file
744
lcode.c
Normal file
@@ -0,0 +1,744 @@
|
||||
/*
|
||||
** $Id: lcode.c,v 1.108 2002/06/13 13:39:55 roberto Exp $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#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"
|
||||
#include "ltable.h"
|
||||
|
||||
|
||||
#define hasjumps(e) ((e)->t != (e)->f)
|
||||
|
||||
|
||||
void luaK_nil (FuncState *fs, int from, int n) {
|
||||
Instruction *previous;
|
||||
if (fs->pc > fs->lasttarget && /* no jumps to current position? */
|
||||
GET_OPCODE(*(previous = &fs->f->code[fs->pc-1])) == 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(*previous, from+n-1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
|
||||
}
|
||||
|
||||
|
||||
int luaK_jump (FuncState *fs) {
|
||||
int jpc = fs->jpc; /* save list of jumps to here */
|
||||
int j;
|
||||
fs->jpc = NO_JUMP;
|
||||
j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
|
||||
luaK_concat(fs, &j, jpc); /* keep them on hold */
|
||||
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_jump(fs);
|
||||
}
|
||||
|
||||
|
||||
static void luaK_fixjump (FuncState *fs, int pc, int dest) {
|
||||
Instruction *jmp = &fs->f->code[pc];
|
||||
int offset = dest-(pc+1);
|
||||
lua_assert(dest != NO_JUMP);
|
||||
if (abs(offset) > MAXARG_sBx)
|
||||
luaX_syntaxerror(fs->ls, "control structure too long");
|
||||
SETARG_sBx(*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).
|
||||
*/
|
||||
int luaK_getlabel (FuncState *fs) {
|
||||
fs->lasttarget = fs->pc;
|
||||
return fs->pc;
|
||||
}
|
||||
|
||||
|
||||
static int luaK_getjump (FuncState *fs, int pc) {
|
||||
int offset = GETARG_sBx(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];
|
||||
if (pc >= 1 && testOpMode(GET_OPCODE(*(pi-1)), OpModeT))
|
||||
return pi-1;
|
||||
else
|
||||
return pi;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** check whether list has any jump that do not produce a value
|
||||
** (or produce an inverted value)
|
||||
*/
|
||||
static int need_value (FuncState *fs, int list, int cond) {
|
||||
for (; list != NO_JUMP; list = luaK_getjump(fs, list)) {
|
||||
Instruction i = *getjumpcontrol(fs, list);
|
||||
if (GET_OPCODE(i) != OP_TEST || GETARG_B(i) != cond) return 1;
|
||||
}
|
||||
return 0; /* not found */
|
||||
}
|
||||
|
||||
|
||||
static void patchtestreg (Instruction *i, int reg) {
|
||||
if (reg == NO_REG) reg = GETARG_C(*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);
|
||||
if (GET_OPCODE(*i) != OP_TEST) {
|
||||
lua_assert(dtarget != NO_JUMP);
|
||||
luaK_fixjump(fs, list, dtarget); /* jump to default target */
|
||||
}
|
||||
else {
|
||||
if (GETARG_B(*i)) {
|
||||
lua_assert(ttarget != NO_JUMP);
|
||||
patchtestreg(i, treg);
|
||||
luaK_fixjump(fs, list, ttarget);
|
||||
}
|
||||
else {
|
||||
lua_assert(ftarget != NO_JUMP);
|
||||
patchtestreg(i, freg);
|
||||
luaK_fixjump(fs, list, ftarget);
|
||||
}
|
||||
}
|
||||
list = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaK_dischargejpc (FuncState *fs) {
|
||||
luaK_patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc, NO_REG, fs->pc);
|
||||
fs->jpc = NO_JUMP;
|
||||
}
|
||||
|
||||
|
||||
void luaK_patchlist (FuncState *fs, int list, int target) {
|
||||
if (target == fs->pc)
|
||||
luaK_patchtohere(fs, list);
|
||||
else {
|
||||
lua_assert(target < fs->pc);
|
||||
luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_patchtohere (FuncState *fs, int list) {
|
||||
luaK_getlabel(fs);
|
||||
luaK_concat(fs, &fs->jpc, list);
|
||||
}
|
||||
|
||||
|
||||
void luaK_concat (FuncState *fs, int *l1, int l2) {
|
||||
if (l2 == NO_JUMP) return;
|
||||
else 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaK_checkstack (FuncState *fs, int n) {
|
||||
int newstack = fs->freereg + n;
|
||||
if (newstack > fs->f->maxstacksize) {
|
||||
if (newstack >= MAXSTACK)
|
||||
luaX_syntaxerror(fs->ls, "function or expression too complex");
|
||||
fs->f->maxstacksize = cast(lu_byte, newstack);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_reserveregs (FuncState *fs, int n) {
|
||||
luaK_checkstack(fs, n);
|
||||
fs->freereg += n;
|
||||
}
|
||||
|
||||
|
||||
static void freereg (FuncState *fs, int reg) {
|
||||
if (reg >= fs->nactvar && reg < MAXSTACK) {
|
||||
fs->freereg--;
|
||||
lua_assert(reg == fs->freereg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void freeexp (FuncState *fs, expdesc *e) {
|
||||
if (e->k == VNONRELOC)
|
||||
freereg(fs, e->info);
|
||||
}
|
||||
|
||||
|
||||
static int addk (FuncState *fs, TObject *k, TObject *v) {
|
||||
const TObject *index = luaH_get(fs->h, k);
|
||||
if (ttisnumber(index)) {
|
||||
lua_assert(luaO_rawequalObj(&fs->f->k[cast(int, nvalue(index))], v));
|
||||
return cast(int, nvalue(index));
|
||||
}
|
||||
else { /* constant not found; create a new entry */
|
||||
Proto *f = fs->f;
|
||||
luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
|
||||
MAXARG_Bx, "constant table overflow");
|
||||
setobj(&f->k[fs->nk], v);
|
||||
setnvalue(luaH_set(fs->L, fs->h, k), fs->nk);
|
||||
return fs->nk++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaK_stringK (FuncState *fs, TString *s) {
|
||||
TObject o;
|
||||
setsvalue(&o, s);
|
||||
return addk(fs, &o, &o);
|
||||
}
|
||||
|
||||
|
||||
int luaK_numberK (FuncState *fs, lua_Number r) {
|
||||
TObject o;
|
||||
setnvalue(&o, r);
|
||||
return addk(fs, &o, &o);
|
||||
}
|
||||
|
||||
|
||||
static int nil_constant (FuncState *fs) {
|
||||
TObject k, v;
|
||||
setnilvalue(&v);
|
||||
sethvalue(&k, fs->h); /* cannot use nil as key; instead use table itself */
|
||||
return addk(fs, &k, &v);
|
||||
}
|
||||
|
||||
|
||||
void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
|
||||
if (e->k == VCALL) { /* expression is an open function call? */
|
||||
SETARG_C(getcode(fs, e), nresults+1);
|
||||
if (nresults == 1) { /* `regular' expression? */
|
||||
e->k = VNONRELOC;
|
||||
e->info = GETARG_A(getcode(fs, e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
||||
switch (e->k) {
|
||||
case VLOCAL: {
|
||||
e->k = VNONRELOC;
|
||||
break;
|
||||
}
|
||||
case VUPVAL: {
|
||||
e->info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->info, 0);
|
||||
e->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
case VGLOBAL: {
|
||||
e->info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->info);
|
||||
e->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
case VINDEXED: {
|
||||
freereg(fs, e->aux);
|
||||
freereg(fs, e->info);
|
||||
e->info = luaK_codeABC(fs, OP_GETTABLE, 0, e->info, e->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, int A, int b, int jump) {
|
||||
luaK_getlabel(fs); /* those instructions may be jump targets */
|
||||
return luaK_codeABC(fs, OP_LOADBOOL, A, b, 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 VFALSE: case VTRUE: {
|
||||
luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
|
||||
break;
|
||||
}
|
||||
case VK: {
|
||||
luaK_codeABx(fs, OP_LOADK, reg, e->info);
|
||||
break;
|
||||
}
|
||||
case VRELOCABLE: {
|
||||
Instruction *pc = &getcode(fs, e);
|
||||
SETARG_A(*pc, reg);
|
||||
break;
|
||||
}
|
||||
case VNONRELOC: {
|
||||
if (reg != e->info)
|
||||
luaK_codeABC(fs, OP_MOVE, reg, e->info, 0);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
lua_assert(e->k == VVOID || e->k == VJMP);
|
||||
return; /* nothing to do... */
|
||||
}
|
||||
}
|
||||
e->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);
|
||||
if (e->k == VJMP)
|
||||
luaK_concat(fs, &e->t, e->info); /* put this jump in `t' list */
|
||||
if (hasjumps(e)) {
|
||||
int final; /* position after whole expression */
|
||||
int p_f = NO_JUMP; /* position of an eventual LOAD false */
|
||||
int p_t = NO_JUMP; /* position of an eventual LOAD true */
|
||||
if (need_value(fs, e->t, 1) || need_value(fs, e->f, 0)) {
|
||||
int fj = NO_JUMP; /* first jump (over LOAD ops.) */
|
||||
if (e->k != VJMP)
|
||||
fj = luaK_jump(fs);
|
||||
p_f = code_label(fs, reg, 0, 1);
|
||||
p_t = code_label(fs, reg, 1, 0);
|
||||
luaK_patchtohere(fs, fj);
|
||||
}
|
||||
final = luaK_getlabel(fs);
|
||||
luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
|
||||
luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
|
||||
}
|
||||
e->f = e->t = NO_JUMP;
|
||||
e->info = reg;
|
||||
e->k = VNONRELOC;
|
||||
}
|
||||
|
||||
|
||||
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
freeexp(fs, e);
|
||||
luaK_reserveregs(fs, 1);
|
||||
luaK_exp2reg(fs, e, fs->freereg - 1);
|
||||
}
|
||||
|
||||
|
||||
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
if (e->k == VNONRELOC) {
|
||||
if (!hasjumps(e)) return e->info; /* exp is already in a register */
|
||||
if (e->info >= fs->nactvar) { /* reg. is not a local? */
|
||||
luaK_exp2reg(fs, e, e->info); /* put value on it */
|
||||
return e->info;
|
||||
}
|
||||
}
|
||||
luaK_exp2nextreg(fs, e); /* default */
|
||||
return e->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);
|
||||
switch (e->k) {
|
||||
case VNIL: {
|
||||
if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
|
||||
e->info = nil_constant(fs);
|
||||
e->k = VK;
|
||||
return e->info + MAXSTACK;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
case VK: {
|
||||
if (e->info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
|
||||
return e->info + MAXSTACK;
|
||||
else break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
/* not a constant in the right range: put in a register */
|
||||
return luaK_exp2anyreg(fs, e);
|
||||
}
|
||||
|
||||
|
||||
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
|
||||
switch (var->k) {
|
||||
case VLOCAL: {
|
||||
freeexp(fs, exp);
|
||||
luaK_exp2reg(fs, exp, var->info);
|
||||
return;
|
||||
}
|
||||
case VUPVAL: {
|
||||
int e = luaK_exp2anyreg(fs, exp);
|
||||
luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0);
|
||||
break;
|
||||
}
|
||||
case VGLOBAL: {
|
||||
int e = luaK_exp2anyreg(fs, exp);
|
||||
luaK_codeABx(fs, OP_SETGLOBAL, e, var->info);
|
||||
break;
|
||||
}
|
||||
case VINDEXED: {
|
||||
int e = luaK_exp2anyreg(fs, exp);
|
||||
luaK_codeABC(fs, OP_SETTABLE, e, var->info, var->aux);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
lua_assert(0); /* invalid var kind to store */
|
||||
break;
|
||||
}
|
||||
}
|
||||
freeexp(fs, exp);
|
||||
}
|
||||
|
||||
|
||||
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->info, luaK_exp2RK(fs, key));
|
||||
freeexp(fs, key);
|
||||
e->info = func;
|
||||
e->k = VNONRELOC;
|
||||
}
|
||||
|
||||
|
||||
static void invertjump (FuncState *fs, expdesc *e) {
|
||||
Instruction *pc = getjumpcontrol(fs, e->info);
|
||||
lua_assert(testOpMode(GET_OPCODE(*pc), OpModeT));
|
||||
SETARG_B(*pc, !(GETARG_B(*pc)));
|
||||
}
|
||||
|
||||
|
||||
static int jumponcond (FuncState *fs, expdesc *e, int cond) {
|
||||
if (e->k == VRELOCABLE) {
|
||||
Instruction ie = getcode(fs, e);
|
||||
if (GET_OPCODE(ie) == OP_NOT) {
|
||||
fs->pc--; /* remove previous OP_NOT */
|
||||
return luaK_condjump(fs, OP_TEST, NO_REG, !cond ,GETARG_B(ie));
|
||||
}
|
||||
/* else go through */
|
||||
}
|
||||
discharge2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
return luaK_condjump(fs, OP_TEST, NO_REG, cond, e->info);
|
||||
}
|
||||
|
||||
|
||||
void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
||||
int pc; /* pc of last jump */
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VK: case VTRUE: {
|
||||
pc = NO_JUMP; /* always true; do nothing */
|
||||
break;
|
||||
}
|
||||
case VFALSE: {
|
||||
pc = luaK_jump(fs); /* always jump */
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
invertjump(fs, e);
|
||||
pc = e->info;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
pc = jumponcond(fs, e, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
|
||||
}
|
||||
|
||||
|
||||
void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
||||
int pc; /* pc of last jump */
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: case VFALSE: {
|
||||
pc = NO_JUMP; /* always false; do nothing */
|
||||
break;
|
||||
}
|
||||
case VTRUE: {
|
||||
pc = luaK_jump(fs); /* always jump */
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
pc = e->info;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
pc = jumponcond(fs, e, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
|
||||
}
|
||||
|
||||
|
||||
static void codenot (FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: case VFALSE: {
|
||||
e->k = VTRUE;
|
||||
break;
|
||||
}
|
||||
case VK: case VTRUE: {
|
||||
e->k = VFALSE;
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
invertjump(fs, e);
|
||||
break;
|
||||
}
|
||||
case VRELOCABLE:
|
||||
case VNONRELOC: {
|
||||
discharge2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
e->info = luaK_codeABC(fs, OP_NOT, 0, e->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->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 == VK && ttisnumber(&fs->f->k[e->info]))
|
||||
e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
|
||||
else {
|
||||
luaK_exp2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
e->info = luaK_codeABC(fs, OP_UNM, 0, e->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);
|
||||
luaK_patchtohere(fs, v->t);
|
||||
v->t = NO_JUMP;
|
||||
break;
|
||||
}
|
||||
case OPR_OR: {
|
||||
luaK_goiffalse(fs, v);
|
||||
luaK_patchtohere(fs, v->f);
|
||||
v->f = NO_JUMP;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void codebinop (FuncState *fs, expdesc *res, BinOpr op,
|
||||
int o1, int o2, int ic) {
|
||||
switch (op) {
|
||||
case OPR_SUB:
|
||||
case OPR_DIV:
|
||||
case OPR_POW:
|
||||
lua_assert(!ic);
|
||||
/* go through */
|
||||
case OPR_ADD:
|
||||
case OPR_MULT: { /* ORDER OPR */
|
||||
OpCode opc = cast(OpCode, (op - OPR_ADD) + OP_ADD);
|
||||
res->info = luaK_codeABC(fs, opc, 0, o1, o2);
|
||||
res->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
case OPR_NE:
|
||||
case OPR_EQ: {
|
||||
res->info = luaK_condjump(fs, OP_EQ, o1, (op == OPR_EQ), o2);
|
||||
res->k = VJMP;
|
||||
break;
|
||||
}
|
||||
case OPR_LT:
|
||||
case OPR_LE:
|
||||
case OPR_GT:
|
||||
case OPR_GE: { /* ORDER OPR */
|
||||
OpCode opc;
|
||||
int i = op - OPR_LT;
|
||||
if (ic) /* operands were interchanged? */
|
||||
i = (i+2)&3; /* correct operator */
|
||||
opc = cast(OpCode, i + OP_LT);
|
||||
res->info = luaK_condjump(fs, opc, o1, 1, o2);
|
||||
res->k = VJMP;
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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->info = e2->info; e1->aux = e2->aux; 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->info = e2->info; e1->aux = e2->aux; 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->info == GETARG_B(getcode(fs, e2))-1);
|
||||
freeexp(fs, e1);
|
||||
SETARG_B(getcode(fs, e2), e1->info);
|
||||
e1->k = e2->k; e1->info = e2->info;
|
||||
}
|
||||
else {
|
||||
luaK_exp2nextreg(fs, e2);
|
||||
freeexp(fs, e2);
|
||||
freeexp(fs, e1);
|
||||
e1->info = luaK_codeABC(fs, OP_CONCAT, 0, e1->info, e2->info);
|
||||
e1->k = VRELOCABLE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
int o1, o2;
|
||||
int ic; /* interchange flag */
|
||||
if (e1->k != VK) { /* not a constant operator? */
|
||||
o1 = e1->info;
|
||||
o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
|
||||
ic = 0;
|
||||
}
|
||||
else { /* interchange operands */
|
||||
o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
|
||||
o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
|
||||
ic = 1;
|
||||
}
|
||||
freeexp(fs, e2);
|
||||
freeexp(fs, e1);
|
||||
codebinop(fs, e1, op, o1, o2, ic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_fixline (FuncState *fs, int line) {
|
||||
fs->f->lineinfo[fs->pc - 1] = line;
|
||||
}
|
||||
|
||||
|
||||
int luaK_code (FuncState *fs, Instruction i, int line) {
|
||||
Proto *f = fs->f;
|
||||
int oldsize = f->sizecode;
|
||||
luaK_dischargejpc(fs); /* `pc' will change */
|
||||
/* put new instruction in code array */
|
||||
luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
|
||||
MAX_INT, "code size overflow");
|
||||
f->code[fs->pc] = i;
|
||||
if (f->sizecode != oldsize)
|
||||
luaM_reallocvector(fs->L, f->lineinfo, oldsize, f->sizecode, int);
|
||||
f->lineinfo[fs->pc] = line;
|
||||
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), fs->ls->lastline);
|
||||
}
|
||||
|
||||
|
||||
int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
|
||||
lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
|
||||
return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
|
||||
}
|
||||
|
||||
73
lcode.h
Normal file
73
lcode.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
** $Id: lcode.h,v 1.36 2002/05/13 13:07: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 getcode(fs,e) ((fs)->f->code[(e)->info])
|
||||
|
||||
#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
|
||||
|
||||
int luaK_code (FuncState *fs, Instruction i, int line);
|
||||
int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
|
||||
int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
|
||||
void luaK_fixline (FuncState *fs, int line);
|
||||
void luaK_nil (FuncState *fs, int from, int n);
|
||||
void luaK_reserveregs (FuncState *fs, int n);
|
||||
int luaK_stringK (FuncState *fs, TString *s);
|
||||
int luaK_numberK (FuncState *fs, lua_Number r);
|
||||
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_goiffalse (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_patchtohere (FuncState *fs, int list);
|
||||
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
|
||||
264
ldblib.c
Normal file
264
ldblib.c
Normal file
@@ -0,0 +1,264 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.66 2002/08/06 18:01:50 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>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static void settabss (lua_State *L, const char *i, const char *v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushstring(L, v);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void settabsi (lua_State *L, const char *i, int v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushnumber(L, v);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static int getinfo (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
const char *options = luaL_opt_string(L, 2, "flnSu");
|
||||
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_pushfstring(L, ">%s", options);
|
||||
options = lua_tostring(L, -1);
|
||||
lua_pushvalue(L, 1);
|
||||
}
|
||||
else
|
||||
return luaL_argerror(L, 1, "function or level expected");
|
||||
if (!lua_getinfo(L, options, &ar))
|
||||
return luaL_argerror(L, 2, "invalid option");
|
||||
lua_newtable(L);
|
||||
for (; *options; options++) {
|
||||
switch (*options) {
|
||||
case 'S':
|
||||
settabss(L, "source", ar.source);
|
||||
settabss(L, "short_src", ar.short_src);
|
||||
settabsi(L, "linedefined", ar.linedefined);
|
||||
settabss(L, "what", ar.what);
|
||||
break;
|
||||
case 'l':
|
||||
settabsi(L, "currentline", ar.currentline);
|
||||
break;
|
||||
case 'u':
|
||||
settabsi(L, "nups", ar.nups);
|
||||
break;
|
||||
case 'n':
|
||||
settabss(L, "name", ar.name);
|
||||
settabss(L, "namewhat", ar.namewhat);
|
||||
break;
|
||||
case 'f':
|
||||
lua_pushliteral(L, "func");
|
||||
lua_pushvalue(L, -3);
|
||||
lua_rawset(L, -3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
|
||||
static int getlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
const char *name;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
return luaL_argerror(L, 1, "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? */
|
||||
return luaL_argerror(L, 1, "level out of range");
|
||||
luaL_check_any(L, 3);
|
||||
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static const char KEY_HOOK = 'h';
|
||||
|
||||
|
||||
static void hookf (lua_State *L, lua_Debug *ar) {
|
||||
static const char *const hooknames[] = {"call", "return", "line", "count"};
|
||||
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushstring(L, hooknames[(int)ar->event]);
|
||||
if (ar->currentline >= 0) lua_pushnumber(L, ar->currentline);
|
||||
else lua_pushnil(L);
|
||||
lua_assert(lua_getinfo(L, "lS", ar));
|
||||
lua_call(L, 2, 0);
|
||||
}
|
||||
else
|
||||
lua_pop(L, 1); /* pop result from gettable */
|
||||
}
|
||||
|
||||
|
||||
static unsigned long makemask (const char *smask, int count) {
|
||||
unsigned long mask = 0;
|
||||
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
|
||||
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
|
||||
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
|
||||
return mask | LUA_MASKCOUNT(count);
|
||||
}
|
||||
|
||||
|
||||
static char *unmakemask (unsigned long mask, char *smask) {
|
||||
int i = 0;
|
||||
if (mask & LUA_MASKCALL) smask[i++] = 'c';
|
||||
if (mask & LUA_MASKRET) smask[i++] = 'r';
|
||||
if (mask & LUA_MASKLINE) smask[i++] = 'l';
|
||||
smask[i] = '\0';
|
||||
return smask;
|
||||
}
|
||||
|
||||
|
||||
static int sethook (lua_State *L) {
|
||||
if (lua_isnoneornil(L, 1)) {
|
||||
lua_settop(L, 1);
|
||||
lua_sethook(L, NULL, 0); /* turn off hooks */
|
||||
}
|
||||
else {
|
||||
const char *smask = luaL_check_string(L, 2);
|
||||
lua_Number count = luaL_opt_number(L, 3, 0);
|
||||
luaL_check_type(L, 1, LUA_TFUNCTION);
|
||||
luaL_arg_check(L, count <= LUA_MAXCOUNT, 2, "count too large (>= 2^24)");
|
||||
lua_sethook(L, hookf, makemask(smask, count));
|
||||
}
|
||||
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX); /* set new hook */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int gethook (lua_State *L) {
|
||||
char buff[5];
|
||||
unsigned long mask = lua_gethookmask(L);
|
||||
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */
|
||||
lua_pushstring(L, unmakemask(mask, buff));
|
||||
lua_pushnumber(L, lua_getmaskcount(mask));
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
static int debug (lua_State *L) {
|
||||
for (;;) {
|
||||
char buffer[250];
|
||||
fputs("lua_debug> ", stderr);
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||
strcmp(buffer, "cont\n") == 0)
|
||||
return 0;
|
||||
lua_dostring(L, buffer);
|
||||
lua_settop(L, 0); /* remove eventual returns */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define LEVELS1 12 /* size of the first part of the stack */
|
||||
#define LEVELS2 10 /* size of the second part of the stack */
|
||||
|
||||
static int errorfb (lua_State *L) {
|
||||
int level = 1; /* skip level 0 (it's this function) */
|
||||
int firstpart = 1; /* still before eventual `...' */
|
||||
lua_Debug ar;
|
||||
if (lua_gettop(L) == 0)
|
||||
lua_pushliteral(L, "");
|
||||
else if (!lua_isstring(L, 1)) return 1; /* no string message */
|
||||
lua_pushliteral(L, "stack traceback:\n");
|
||||
while (lua_getstack(L, level++, &ar)) {
|
||||
if (level > LEVELS1 && firstpart) {
|
||||
/* no more than `LEVELS2' more levels? */
|
||||
if (!lua_getstack(L, level+LEVELS2, &ar))
|
||||
level--; /* keep going */
|
||||
else {
|
||||
lua_pushliteral(L, "\t...\n"); /* too many levels */
|
||||
while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
|
||||
level++;
|
||||
}
|
||||
firstpart = 0;
|
||||
continue;
|
||||
}
|
||||
lua_pushliteral(L, "\t");
|
||||
lua_getinfo(L, "Snl", &ar);
|
||||
lua_pushfstring(L, "%s:", ar.short_src);
|
||||
if (ar.currentline > 0)
|
||||
lua_pushfstring(L, "%d:", ar.currentline);
|
||||
switch (*ar.namewhat) {
|
||||
case 'g': /* global */
|
||||
case 'l': /* local */
|
||||
case 'f': /* field */
|
||||
case 'm': /* method */
|
||||
lua_pushfstring(L, " in function `%s'", ar.name);
|
||||
break;
|
||||
default: {
|
||||
if (*ar.what == 'm') /* main? */
|
||||
lua_pushfstring(L, " in main chunk");
|
||||
else if (*ar.what == 'C') /* C function? */
|
||||
lua_pushfstring(L, "%s", ar.short_src);
|
||||
else
|
||||
lua_pushfstring(L, " in function <%s:%d>",
|
||||
ar.short_src, ar.linedefined);
|
||||
}
|
||||
}
|
||||
lua_pushliteral(L, "\n");
|
||||
lua_concat(L, lua_gettop(L));
|
||||
}
|
||||
lua_concat(L, lua_gettop(L));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_reg dblib[] = {
|
||||
{"getlocal", getlocal},
|
||||
{"getinfo", getinfo},
|
||||
{"gethook", gethook},
|
||||
{"sethook", sethook},
|
||||
{"setlocal", setlocal},
|
||||
{"debug", debug},
|
||||
{"traceback", errorfb},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API int lua_dblibopen (lua_State *L) {
|
||||
luaL_opennamedlib(L, LUA_DBLIBNAME, dblib, 0);
|
||||
lua_pushliteral(L, "_TRACEBACK");
|
||||
lua_pushcfunction(L, errorfb);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
return 0;
|
||||
}
|
||||
|
||||
552
ldebug.c
Normal file
552
ldebug.c
Normal file
@@ -0,0 +1,552 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 1.131 2002/08/08 20:08:41 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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 "lvm.h"
|
||||
|
||||
|
||||
|
||||
static const char *getfuncname (CallInfo *ci, const char **name);
|
||||
|
||||
|
||||
#define isLua(ci) (!((ci)->state & CI_C))
|
||||
|
||||
|
||||
static int currentpc (CallInfo *ci) {
|
||||
if (!isLua(ci)) return -1; /* function is not a Lua function? */
|
||||
if (ci->state & CI_HASFRAME) /* function has a frame? */
|
||||
ci->u.l.savedpc = *ci->u.l.pc; /* use `pc' from there */
|
||||
/* function's pc is saved */
|
||||
return pcRel(ci->u.l.savedpc, ci_func(ci)->l.p);
|
||||
}
|
||||
|
||||
|
||||
static int currentline (CallInfo *ci) {
|
||||
int pc = currentpc(ci);
|
||||
if (pc < 0)
|
||||
return -1; /* only active lua functions have current-line information */
|
||||
else
|
||||
return getline(ci_func(ci)->l.p, pc);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_sethook (lua_State *L, lua_Hook func, unsigned long mask) {
|
||||
int allow;
|
||||
CallInfo *ci;
|
||||
lua_lock(L);
|
||||
allow = allowhook(L);
|
||||
if (func == NULL) mask = 0;
|
||||
else if (mask == 0) func = NULL;
|
||||
L->hook = func;
|
||||
L->hookmask = mask;
|
||||
setallowhook(L, allow);
|
||||
resethookcount(L);
|
||||
for (ci = L->ci; ci != L->base_ci; ci--) /* update all `savedpc's */
|
||||
currentpc(ci);
|
||||
lua_unlock(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_Hook lua_gethook (lua_State *L) {
|
||||
return L->hook;
|
||||
}
|
||||
|
||||
|
||||
LUA_API unsigned long lua_gethookmask (lua_State *L) {
|
||||
return L->hookmask;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
int status;
|
||||
lua_lock(L);
|
||||
if (L->ci - L->base_ci <= level) status = 0; /* there is no such level */
|
||||
else {
|
||||
ar->i_ci = (L->ci - L->base_ci) - level;
|
||||
status = 1;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static Proto *getluaproto (CallInfo *ci) {
|
||||
return (isLua(ci) ? ci_func(ci)->l.p : NULL);
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const char *name;
|
||||
CallInfo *ci;
|
||||
Proto *fp;
|
||||
lua_lock(L);
|
||||
name = NULL;
|
||||
ci = L->base_ci + ar->i_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 char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const char *name;
|
||||
CallInfo *ci;
|
||||
Proto *fp;
|
||||
lua_lock(L);
|
||||
name = NULL;
|
||||
ci = L->base_ci + ar->i_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] == '(') /* `(' 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 = "Lua";
|
||||
}
|
||||
|
||||
|
||||
static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
|
||||
Closure *cl;
|
||||
if (ttisfunction(func))
|
||||
cl = clvalue(func);
|
||||
else {
|
||||
luaG_runerror(L, "value for `lua_getinfo' is not a function");
|
||||
cl = NULL; /* to avoid warnings */
|
||||
}
|
||||
if (cl->c.isC) {
|
||||
ar->source = "=[C]";
|
||||
ar->linedefined = -1;
|
||||
ar->what = "C";
|
||||
}
|
||||
else
|
||||
infoLproto(ar, cl->l.p);
|
||||
luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
|
||||
if (ar->linedefined == 0)
|
||||
ar->what = "main";
|
||||
}
|
||||
|
||||
|
||||
static const char *travglobals (lua_State *L, const TObject *o) {
|
||||
Table *g = hvalue(gt(L));
|
||||
int i = sizenode(g);
|
||||
while (i--) {
|
||||
Node *n = node(g, i);
|
||||
if (luaO_rawequalObj(o, val(n)) && ttisstring(key(n)))
|
||||
return getstr(tsvalue(key(n)));
|
||||
}
|
||||
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 = "global";
|
||||
else ar->namewhat = ""; /* not found */
|
||||
}
|
||||
|
||||
|
||||
|
||||
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
StkId f;
|
||||
CallInfo *ci;
|
||||
int status = 1;
|
||||
lua_lock(L);
|
||||
if (*what != '>') { /* function is active? */
|
||||
ci = L->base_ci + ar->i_ci;
|
||||
f = ci->base - 1;
|
||||
}
|
||||
else {
|
||||
what++; /* skip the `>' */
|
||||
ci = NULL;
|
||||
f = L->top - 1;
|
||||
}
|
||||
for (; *what; what++) {
|
||||
switch (*what) {
|
||||
case 'S': {
|
||||
funcinfo(L, ar, f);
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
ar->currentline = (ci) ? currentline(ci) : -1;
|
||||
break;
|
||||
}
|
||||
case 'u': {
|
||||
ar->nups = (ttisfunction(f)) ? clvalue(f)->c.nupvalues : 0;
|
||||
break;
|
||||
}
|
||||
case 'n': {
|
||||
ar->namewhat = (ci) ? getfuncname(ci, &ar->name) : NULL;
|
||||
if (ar->namewhat == NULL)
|
||||
getname(L, f, ar);
|
||||
break;
|
||||
}
|
||||
case 'f': {
|
||||
setobj(L->top, f);
|
||||
status = 2;
|
||||
break;
|
||||
}
|
||||
default: status = 0; /* invalid option */
|
||||
}
|
||||
}
|
||||
if (!ci) L->top--; /* pop function */
|
||||
if (status == 2) incr_top(L);
|
||||
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 precheck (const Proto *pt) {
|
||||
check(pt->maxstacksize <= MAXSTACK);
|
||||
lua_assert(pt->numparams+pt->is_vararg <= pt->maxstacksize);
|
||||
check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int checkopenop (const Proto *pt, int pc) {
|
||||
Instruction i = pt->code[pc+1];
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_CALL:
|
||||
case OP_TAILCALL:
|
||||
case OP_RETURN: {
|
||||
check(GETARG_B(i) == 0);
|
||||
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) */
|
||||
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 iABx: {
|
||||
b = GETARG_Bx(i);
|
||||
if (testOpMode(op, OpModeK)) check(b < pt->sizek);
|
||||
break;
|
||||
}
|
||||
case iAsBx: {
|
||||
b = GETARG_sBx(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (testOpMode(op, OpModesetA)) {
|
||||
if (a == reg) last = pc; /* change register `a' */
|
||||
}
|
||||
if (testOpMode(op, OpModeT)) {
|
||||
check(pc+2 < pt->sizecode); /* check skip */
|
||||
check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
|
||||
}
|
||||
switch (op) {
|
||||
case OP_LOADBOOL: {
|
||||
check(c == 0 || pc+2 < pt->sizecode); /* check its jump */
|
||||
break;
|
||||
}
|
||||
case OP_LOADNIL: {
|
||||
if (a <= reg && reg <= b)
|
||||
last = pc; /* set registers from `a' to `b' */
|
||||
break;
|
||||
}
|
||||
case OP_GETUPVAL:
|
||||
case OP_SETUPVAL: {
|
||||
check(b < pt->nupvalues);
|
||||
break;
|
||||
}
|
||||
case OP_GETGLOBAL:
|
||||
case OP_SETGLOBAL: {
|
||||
check(ttisstring(&pt->k[b]));
|
||||
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_TFORLOOP:
|
||||
checkreg(pt, a+2+c);
|
||||
/* go through */
|
||||
case OP_FORLOOP:
|
||||
checkreg(pt, a+2);
|
||||
/* go through */
|
||||
case OP_JMP: {
|
||||
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_CALL:
|
||||
case OP_TAILCALL: {
|
||||
if (b != 0) {
|
||||
checkreg(pt, a+b-1);
|
||||
}
|
||||
c--; /* c = num. returns */
|
||||
if (c == LUA_MULTRET) {
|
||||
check(checkopenop(pt, pc));
|
||||
}
|
||||
else if (c != 0)
|
||||
checkreg(pt, a+c-1);
|
||||
if (reg >= a) last = pc; /* affect all registers above base */
|
||||
break;
|
||||
}
|
||||
case OP_RETURN: {
|
||||
b--; /* b = num. returns */
|
||||
if (b > 0) checkreg(pt, a+b-1);
|
||||
break;
|
||||
}
|
||||
case OP_SETLIST: {
|
||||
checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
|
||||
break;
|
||||
}
|
||||
case OP_CLOSURE: {
|
||||
int nup;
|
||||
check(b < pt->sizep);
|
||||
nup = pt->p[b]->nupvalues;
|
||||
check(pc + nup < pt->sizecode);
|
||||
for (; nup>0; nup--) {
|
||||
OpCode op1 = GET_OPCODE(pt->code[pc+nup]);
|
||||
check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return pt->code[last];
|
||||
}
|
||||
|
||||
#undef check
|
||||
#undef checkjump
|
||||
#undef checkreg
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
int luaG_checkcode (const Proto *pt) {
|
||||
return luaG_symbexec(pt, pt->sizecode, NO_REG);
|
||||
}
|
||||
|
||||
|
||||
static const char *kname (Proto *p, int c) {
|
||||
c = c - MAXSTACK;
|
||||
if (c >= 0 && ttisstring(&p->k[c]))
|
||||
return svalue(&p->k[c]);
|
||||
else
|
||||
return "?";
|
||||
}
|
||||
|
||||
|
||||
static const char *getobjname (CallInfo *ci, int stackpos, const char **name) {
|
||||
if (isLua(ci)) { /* a Lua function? */
|
||||
Proto *p = ci_func(ci)->l.p;
|
||||
int pc = currentpc(ci);
|
||||
Instruction i;
|
||||
*name = luaF_getlocalname(p, stackpos+1, pc);
|
||||
if (*name) /* is a local? */
|
||||
return "local";
|
||||
i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
|
||||
lua_assert(pc != -1);
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_GETGLOBAL: {
|
||||
lua_assert(ttisstring(&p->k[GETARG_Bx(i)]));
|
||||
*name = svalue(&p->k[GETARG_Bx(i)]);
|
||||
return "global";
|
||||
}
|
||||
case OP_MOVE: {
|
||||
int a = GETARG_A(i);
|
||||
int b = GETARG_B(i); /* move from `b' to `a' */
|
||||
if (b < a)
|
||||
return getobjname(ci, b, name); /* get name for `b' */
|
||||
break;
|
||||
}
|
||||
case OP_GETTABLE: {
|
||||
*name = kname(p, GETARG_C(i));
|
||||
return "field";
|
||||
}
|
||||
case OP_SELF: {
|
||||
*name = kname(p, GETARG_C(i));
|
||||
return "method";
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return NULL; /* no useful name found */
|
||||
}
|
||||
|
||||
|
||||
static Instruction getcurrentinstr (CallInfo *ci) {
|
||||
return (!isLua(ci)) ? (Instruction)(-1) :
|
||||
ci_func(ci)->l.p->code[currentpc(ci)];
|
||||
}
|
||||
|
||||
|
||||
static const char *getfuncname (CallInfo *ci, const char **name) {
|
||||
Instruction i;
|
||||
ci--; /* calling function */
|
||||
i = getcurrentinstr(ci);
|
||||
return (GET_OPCODE(i) == OP_CALL ? getobjname(ci, GETARG_A(i), name)
|
||||
: NULL); /* no useful name found */
|
||||
}
|
||||
|
||||
|
||||
/* only ANSI way to check whether a pointer points to an array */
|
||||
static int isinstack (CallInfo *ci, const TObject *o) {
|
||||
StkId p;
|
||||
for (p = ci->base; p < ci->top; p++)
|
||||
if (o == p) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
|
||||
const char *name = NULL;
|
||||
const char *t = luaT_typenames[ttype(o)];
|
||||
const char *kind = (isinstack(L->ci, o)) ?
|
||||
getobjname(L->ci, o - L->ci->base, &name) : NULL;
|
||||
if (kind)
|
||||
luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
|
||||
op, kind, name, t);
|
||||
else
|
||||
luaG_runerror(L, "attempt to %s a %s value", op, t);
|
||||
}
|
||||
|
||||
|
||||
void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
|
||||
if (ttisstring(p1)) p1 = p2;
|
||||
lua_assert(!ttisstring(p1));
|
||||
luaG_typeerror(L, p1, "concat");
|
||||
}
|
||||
|
||||
|
||||
void luaG_aritherror (lua_State *L, StkId p1, const TObject *p2) {
|
||||
TObject temp;
|
||||
if (luaV_tonumber(p1, &temp) == NULL)
|
||||
p2 = p1; /* first operand is wrong */
|
||||
luaG_typeerror(L, p2, "perform arithmetic on");
|
||||
}
|
||||
|
||||
|
||||
int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
|
||||
const char *t1 = luaT_typenames[ttype(p1)];
|
||||
const char *t2 = luaT_typenames[ttype(p2)];
|
||||
if (t1[2] == t2[2])
|
||||
luaG_runerror(L, "attempt to compare two %s values", t1);
|
||||
else
|
||||
luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void addinfo (lua_State *L, const char *msg) {
|
||||
CallInfo *ci = L->ci;
|
||||
if (!isLua(ci)) { /* no Lua code? */
|
||||
luaO_pushfstring(L, "%s\n", msg); /* no extra info; just add '\n' */
|
||||
}
|
||||
else { /* add file:line information */
|
||||
char buff[LUA_IDSIZE];
|
||||
int line = currentline(ci);
|
||||
luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
|
||||
luaO_pushfstring(L, "%s:%d: %s\n", buff, line, msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaG_errormsg (lua_State *L) {
|
||||
if (L->errfunc != 0) { /* is there an error handling function? */
|
||||
StkId errfunc = restorestack(L, L->errfunc);
|
||||
if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
|
||||
setobj(L->top, L->top - 1); /* move argument */
|
||||
setobj(L->top - 1, errfunc); /* push function */
|
||||
incr_top(L);
|
||||
luaD_call(L, L->top - 2, 1); /* call it */
|
||||
}
|
||||
luaD_throw(L, LUA_ERRRUN);
|
||||
}
|
||||
|
||||
|
||||
void luaG_runerror (lua_State *L, const char *fmt, ...) {
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
addinfo(L, luaO_pushvfstring(L, fmt, argp));
|
||||
va_end(argp);
|
||||
luaG_errormsg(L);
|
||||
}
|
||||
|
||||
33
ldebug.h
Normal file
33
ldebug.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
** $Id: ldebug.h,v 1.29 2002/08/08 20:08:41 roberto Exp roberto $
|
||||
** Auxiliary functions from Debug Interface module
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ldebug_h
|
||||
#define ldebug_h
|
||||
|
||||
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
|
||||
|
||||
#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0)
|
||||
|
||||
#define resethookcount(L) (L->hookcount = lua_getmaskcount(L->hookmask))
|
||||
|
||||
#define setallowhook(L,cond) ((L->hookmask) = ((L->hookmask) & ~1) | (cond))
|
||||
#define allowhook(L) ((L->hookmask) & 1)
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, const TObject *o, const char *opname);
|
||||
void luaG_concaterror (lua_State *L, StkId p1, StkId p2);
|
||||
void luaG_aritherror (lua_State *L, StkId p1, const TObject *p2);
|
||||
int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2);
|
||||
void luaG_runerror (lua_State *L, const char *fmt, ...);
|
||||
void luaG_errormsg (lua_State *L);
|
||||
int luaG_checkcode (const Proto *pt);
|
||||
|
||||
|
||||
#endif
|
||||
461
ldo.c
Normal file
461
ldo.c
Normal file
@@ -0,0 +1,461 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 1.191 2002/08/07 19:22:39 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Error-recovery functions (based on long jumps)
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/* chain list of long jump buffers */
|
||||
struct lua_longjmp {
|
||||
struct lua_longjmp *previous;
|
||||
jmp_buf b;
|
||||
volatile int status; /* error code */
|
||||
};
|
||||
|
||||
|
||||
static void seterrorobj (lua_State *L, int errcode) {
|
||||
switch (errcode) {
|
||||
case LUA_ERRMEM: {
|
||||
setsvalue(L->top, luaS_new(L, MEMERRMSG));
|
||||
break;
|
||||
}
|
||||
case LUA_ERRERR: {
|
||||
setsvalue(L->top, luaS_new(L, "error in error handling"));
|
||||
break;
|
||||
}
|
||||
case LUA_ERRSYNTAX:
|
||||
case LUA_ERRRUN: {
|
||||
return; /* error message already on top */
|
||||
}
|
||||
}
|
||||
L->top++;
|
||||
}
|
||||
|
||||
|
||||
void luaD_throw (lua_State *L, int errcode) {
|
||||
if (L->errorJmp) {
|
||||
L->errorJmp->status = errcode;
|
||||
longjmp(L->errorJmp->b, 1);
|
||||
}
|
||||
else {
|
||||
G(L)->panic(L);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
||||
struct lua_longjmp lj;
|
||||
lj.status = 0;
|
||||
lj.previous = L->errorJmp; /* chain new error handler */
|
||||
L->errorJmp = &lj;
|
||||
if (setjmp(lj.b) == 0)
|
||||
(*f)(L, ud);
|
||||
L->errorJmp = lj.previous; /* restore old error handler */
|
||||
return lj.status;
|
||||
}
|
||||
|
||||
|
||||
static void restore_stack_limit (lua_State *L) {
|
||||
L->stack_last = L->stack+L->stacksize-1;
|
||||
if (L->size_ci > LUA_MAXCALLS) { /* there was an overflow? */
|
||||
int inuse = (L->ci - L->base_ci);
|
||||
if (inuse + 1 < LUA_MAXCALLS) /* can `undo' overflow? */
|
||||
luaD_reallocCI(L, LUA_MAXCALLS);
|
||||
}
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static void correctstack (lua_State *L, TObject *oldstack) {
|
||||
CallInfo *ci;
|
||||
UpVal *up;
|
||||
L->top = (L->top - oldstack) + L->stack;
|
||||
for (up = L->openupval; up != NULL; up = up->next)
|
||||
up->v = (up->v - oldstack) + L->stack;
|
||||
for (ci = L->base_ci; ci <= L->ci; ci++) {
|
||||
ci->base = (ci->base - oldstack) + L->stack;
|
||||
ci->top = (ci->top - oldstack) + L->stack;
|
||||
if (ci->state & CI_HASFRAME) /* Lua function with active frame? */
|
||||
*ci->u.l.pb = (*ci->u.l.pb - oldstack) + L->stack; /* correct frame */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaD_reallocstack (lua_State *L, int newsize) {
|
||||
TObject *oldstack = L->stack;
|
||||
luaM_reallocvector(L, L->stack, L->stacksize, newsize, TObject);
|
||||
L->stacksize = newsize;
|
||||
L->stack_last = L->stack+newsize-1-EXTRA_STACK;
|
||||
correctstack(L, oldstack);
|
||||
}
|
||||
|
||||
|
||||
void luaD_reallocCI (lua_State *L, int newsize) {
|
||||
CallInfo *oldci = L->base_ci;
|
||||
luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
|
||||
L->size_ci = newsize;
|
||||
L->ci = (L->ci - oldci) + L->base_ci;
|
||||
L->end_ci = L->base_ci + L->size_ci;
|
||||
}
|
||||
|
||||
|
||||
void luaD_growstack (lua_State *L, int n) {
|
||||
if (n <= L->stacksize) /* double size is enough? */
|
||||
luaD_reallocstack(L, 2*L->stacksize);
|
||||
else
|
||||
luaD_reallocstack(L, L->stacksize + n + EXTRA_STACK);
|
||||
}
|
||||
|
||||
|
||||
static void luaD_growCI (lua_State *L) {
|
||||
if (L->size_ci > LUA_MAXCALLS) /* overflow while handling overflow? */
|
||||
luaD_throw(L, LUA_ERRERR);
|
||||
else {
|
||||
luaD_reallocCI(L, 2*L->size_ci);
|
||||
if (L->size_ci > LUA_MAXCALLS)
|
||||
luaG_runerror(L, "stack overflow");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaD_callhook (lua_State *L, lua_Hookevent event, int line) {
|
||||
lua_Hook hook = L->hook;
|
||||
if (hook && allowhook(L)) {
|
||||
ptrdiff_t top = savestack(L, L->top);
|
||||
ptrdiff_t ci_top = savestack(L, L->ci->top);
|
||||
lua_Debug ar;
|
||||
ar.event = event;
|
||||
ar.currentline = line;
|
||||
ar.i_ci = L->ci - L->base_ci;
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
L->ci->top = L->top + LUA_MINSTACK;
|
||||
setallowhook(L, 0); /* cannot call hooks inside a hook */
|
||||
lua_unlock(L);
|
||||
(*hook)(L, &ar);
|
||||
lua_lock(L);
|
||||
lua_assert(!allowhook(L));
|
||||
setallowhook(L, 1);
|
||||
L->ci->top = restorestack(L, ci_top);
|
||||
L->top = restorestack(L, top);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjust_varargs (lua_State *L, int nfixargs, StkId base) {
|
||||
int i;
|
||||
Table *htab;
|
||||
TObject nname;
|
||||
int actual = L->top - base; /* actual number of arguments */
|
||||
if (actual < nfixargs) {
|
||||
luaD_checkstack(L, nfixargs - actual);
|
||||
for (; actual < nfixargs; ++actual)
|
||||
setnilvalue(L->top++);
|
||||
}
|
||||
actual -= nfixargs; /* number of extra arguments */
|
||||
htab = luaH_new(L, 0, 0); /* create `arg' table */
|
||||
for (i=0; i<actual; i++) /* put extra arguments into `arg' table */
|
||||
setobj(luaH_setnum(L, htab, i+1), L->top - actual + i);
|
||||
/* store counter in field `n' */
|
||||
setsvalue(&nname, luaS_newliteral(L, "n"));
|
||||
setnvalue(luaH_set(L, htab, &nname), actual);
|
||||
L->top -= actual; /* remove extra elements from the stack */
|
||||
sethvalue(L->top, htab);
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
static StkId tryfuncTM (lua_State *L, StkId func) {
|
||||
const TObject *tm = luaT_gettmbyobj(L, func, TM_CALL);
|
||||
StkId p;
|
||||
ptrdiff_t funcr = savestack(L, func);
|
||||
if (!ttisfunction(tm))
|
||||
luaG_typeerror(L, func, "call");
|
||||
/* Open a hole inside the stack at `func' */
|
||||
for (p = L->top; p > func; p--) setobj(p, p-1);
|
||||
incr_top(L);
|
||||
func = restorestack(L, funcr); /* previous call may change stack */
|
||||
setobj(func, tm); /* tag method is the new function to be called */
|
||||
return func;
|
||||
}
|
||||
|
||||
|
||||
StkId luaD_precall (lua_State *L, StkId func) {
|
||||
LClosure *cl;
|
||||
ptrdiff_t funcr = savestack(L, func);
|
||||
if (!ttisfunction(func)) /* `func' is not a function? */
|
||||
func = tryfuncTM(L, func); /* check the `function' tag method */
|
||||
if (L->ci + 1 == L->end_ci) luaD_growCI(L);
|
||||
cl = &clvalue(func)->l;
|
||||
if (!cl->isC) { /* Lua function? prepare its call */
|
||||
CallInfo *ci;
|
||||
Proto *p = cl->p;
|
||||
if (p->is_vararg) /* varargs? */
|
||||
adjust_varargs(L, p->numparams, func+1);
|
||||
luaD_checkstack(L, p->maxstacksize);
|
||||
ci = ++L->ci; /* now `enter' new function */
|
||||
ci->base = restorestack(L, funcr) + 1;
|
||||
ci->top = ci->base + p->maxstacksize;
|
||||
ci->u.l.savedpc = p->code; /* starting point */
|
||||
ci->state = CI_SAVEDPC;
|
||||
while (L->top < ci->top)
|
||||
setnilvalue(L->top++);
|
||||
L->top = ci->top;
|
||||
return NULL;
|
||||
}
|
||||
else { /* if is a C function, call it */
|
||||
CallInfo *ci;
|
||||
int n;
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
ci = ++L->ci; /* now `enter' new function */
|
||||
ci->base = restorestack(L, funcr) + 1;
|
||||
ci->top = L->top + LUA_MINSTACK;
|
||||
ci->state = CI_C; /* a C function */
|
||||
if (L->hookmask & LUA_MASKCALL) {
|
||||
luaD_callhook(L, LUA_HOOKCALL, -1);
|
||||
ci = L->ci; /* previous call may realocate `ci' */
|
||||
}
|
||||
lua_unlock(L);
|
||||
#ifdef LUA_COMPATUPVALUES
|
||||
lua_pushupvalues(L);
|
||||
#endif
|
||||
n = (*clvalue(ci->base-1)->c.f)(L); /* do the actual call */
|
||||
lua_lock(L);
|
||||
return L->top - n;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
|
||||
StkId res;
|
||||
if (L->hookmask & LUA_MASKRET) {
|
||||
ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
|
||||
luaD_callhook(L, LUA_HOOKRET, -1);
|
||||
firstResult = restorestack(L, fr);
|
||||
}
|
||||
res = L->ci->base - 1; /* res == final position of 1st result */
|
||||
L->ci--;
|
||||
/* move results to correct place */
|
||||
while (wanted != 0 && firstResult < L->top) {
|
||||
setobj(res++, firstResult++);
|
||||
wanted--;
|
||||
}
|
||||
while (wanted-- > 0)
|
||||
setnilvalue(res++);
|
||||
L->top = res;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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, int nResults) {
|
||||
StkId firstResult = luaD_precall(L, func);
|
||||
if (firstResult == NULL) { /* is a Lua function? */
|
||||
firstResult = luaV_execute(L); /* call it */
|
||||
if (firstResult == NULL) {
|
||||
luaD_poscall(L, 0, L->top);
|
||||
luaG_runerror(L, "attempt to yield across tag-method/C-call boundary");
|
||||
}
|
||||
}
|
||||
luaD_poscall(L, nResults, firstResult);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_cobegin (lua_State *L, int nargs) {
|
||||
lua_lock(L);
|
||||
luaD_precall(L, L->top - (nargs+1));
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
static void move_results (lua_State *L, TObject *from, TObject *to) {
|
||||
while (from < to) {
|
||||
setobj(L->top, from);
|
||||
from++;
|
||||
incr_top(L);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void resume (lua_State *L, void *ud) {
|
||||
StkId firstResult;
|
||||
CallInfo *ci = L->ci;
|
||||
if (ci->state & CI_C) { /* not first time (i.e. inside a yield)? */
|
||||
/* finish interrupted execution of `OP_CALL' */
|
||||
int nresults;
|
||||
lua_assert((ci-1)->state & CI_SAVEDPC);
|
||||
lua_assert(GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_CALL ||
|
||||
GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_TAILCALL);
|
||||
nresults = GETARG_C(*((ci-1)->u.l.savedpc - 1)) - 1;
|
||||
luaD_poscall(L, nresults, L->top); /* complete it */
|
||||
if (nresults >= 0) L->top = L->ci->top;
|
||||
}
|
||||
firstResult = luaV_execute(L);
|
||||
if (firstResult == NULL) /* yield? */
|
||||
*cast(int *, ud) = L->ci->u.c.yield_results;
|
||||
else { /* return */
|
||||
*cast(int *, ud) = L->top - firstResult;
|
||||
luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_resume (lua_State *L, lua_State *co) {
|
||||
CallInfo *ci;
|
||||
int numres;
|
||||
int status;
|
||||
lua_lock(L);
|
||||
ci = co->ci;
|
||||
if (ci == co->base_ci) /* no activation record? ?? */
|
||||
luaG_runerror(L, "cannot resume dead thread");
|
||||
if (co->errorJmp != NULL) /* ?? */
|
||||
luaG_runerror(L, "cannot resume active thread");
|
||||
status = luaD_rawrunprotected(co, resume, &numres);
|
||||
if (status == 0)
|
||||
move_results(L, co->top - numres, co->top);
|
||||
else {
|
||||
setobj(L->top++, co->top - 1); /* move error message to other stack */
|
||||
co->ci = co->base_ci; /* `kill' thread */
|
||||
}
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_yield (lua_State *L, int nresults) {
|
||||
CallInfo *ci;
|
||||
lua_lock(L);
|
||||
ci = L->ci;
|
||||
if ((ci-1)->state & CI_C)
|
||||
luaG_runerror(L, "cannot yield a C function");
|
||||
lua_assert(ci->state & CI_C); /* current function is not Lua */
|
||||
ci->u.c.yield_results = nresults;
|
||||
lua_unlock(L);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected call.
|
||||
*/
|
||||
struct CallS { /* data to `f_call' */
|
||||
StkId func;
|
||||
int nresults;
|
||||
};
|
||||
|
||||
|
||||
static void f_call (lua_State *L, void *ud) {
|
||||
struct CallS *c = cast(struct CallS *, ud);
|
||||
luaD_call(L, c->func, c->nresults);
|
||||
}
|
||||
|
||||
|
||||
int luaD_pcall (lua_State *L, int nargs, int nresults, ptrdiff_t errfunc) {
|
||||
struct CallS c;
|
||||
int status;
|
||||
ptrdiff_t old_top = savestack(L, L->top);
|
||||
ptrdiff_t old_ci = saveci(L, L->ci);
|
||||
int old_allowhooks = allowhook(L);
|
||||
ptrdiff_t old_errfunc = L->errfunc;
|
||||
L->errfunc = errfunc;
|
||||
c.func = L->top - (nargs+1); /* function to be called */
|
||||
c.nresults = nresults;
|
||||
status = luaD_rawrunprotected(L, &f_call, &c);
|
||||
if (status != 0) { /* an error occurred? */
|
||||
StkId err; /* error msg. position */
|
||||
seterrorobj(L, status);
|
||||
err = L->top - 1;
|
||||
/* remove parameters and func from the stack */
|
||||
L->top = restorestack(L, old_top) - (nargs+1);
|
||||
setobj(L->top++, err); /* copy error message to corrected top */
|
||||
luaF_close(L, L->top); /* close eventual pending closures */
|
||||
L->ci = restoreci(L, old_ci);
|
||||
setallowhook(L, old_allowhooks);
|
||||
restore_stack_limit(L);
|
||||
}
|
||||
L->errfunc = old_errfunc;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected parser.
|
||||
*/
|
||||
struct SParser { /* data to `f_parser' */
|
||||
ZIO *z;
|
||||
int bin;
|
||||
};
|
||||
|
||||
static void f_parser (lua_State *L, void *ud) {
|
||||
struct SParser *p = cast(struct SParser *, ud);
|
||||
Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
|
||||
Closure *cl = luaF_newLclosure(L, 0, gt(L));
|
||||
cl->l.p = tf;
|
||||
setclvalue(L->top, cl);
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||
struct SParser p;
|
||||
lu_mem old_blocks;
|
||||
int status;
|
||||
ptrdiff_t oldtopr = savestack(L, L->top); /* save current top */
|
||||
p.z = z; p.bin = bin;
|
||||
/* before parsing, give a (good) chance to GC */
|
||||
if (G(L)->nblocks + G(L)->nblocks/4 >= G(L)->GCthreshold)
|
||||
luaC_collectgarbage(L);
|
||||
old_blocks = G(L)->nblocks;
|
||||
status = luaD_rawrunprotected(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 { /* error */
|
||||
StkId oldtop = restorestack(L, oldtopr);
|
||||
seterrorobj(L, status);
|
||||
setobj(oldtop, L->top - 1); /* copy error message to old top */
|
||||
L->top = oldtop+1;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
52
ldo.h
Normal file
52
ldo.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
** $Id: ldo.h,v 1.50 2002/08/06 15:32:22 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ldo_h
|
||||
#define ldo_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
/*
|
||||
** macro to increment stack top.
|
||||
** There must be always an empty slot at the L->stack.top
|
||||
*/
|
||||
#define incr_top(L) \
|
||||
{if (L->top >= L->stack_last) luaD_growstack(L, 1); L->top++;}
|
||||
|
||||
#define luaD_checkstack(L,n) \
|
||||
if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TObject)) \
|
||||
luaD_growstack(L, n)
|
||||
|
||||
#define savestack(L,p) ((char *)(p) - (char *)L->stack)
|
||||
#define restorestack(L,n) ((TObject *)((char *)L->stack + (n)))
|
||||
|
||||
#define saveci(L,p) ((char *)(p) - (char *)L->base_ci)
|
||||
#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n)))
|
||||
|
||||
|
||||
/* type of protected functions, to be ran by `runprotected' */
|
||||
typedef void (*Pfunc) (lua_State *L, void *ud);
|
||||
|
||||
void luaD_resetprotection (lua_State *L);
|
||||
int luaD_protectedparser (lua_State *L, ZIO *z, int bin);
|
||||
void luaD_callhook (lua_State *L, lua_Hookevent event, int line);
|
||||
StkId luaD_precall (lua_State *L, StkId func);
|
||||
void luaD_call (lua_State *L, StkId func, int nResults);
|
||||
int luaD_pcall (lua_State *L, int nargs, int nresults, ptrdiff_t errfunc);
|
||||
void luaD_poscall (lua_State *L, int wanted, StkId firstResult);
|
||||
void luaD_reallocCI (lua_State *L, int newsize);
|
||||
void luaD_reallocstack (lua_State *L, int newsize);
|
||||
void luaD_growstack (lua_State *L, int n);
|
||||
|
||||
void luaD_throw (lua_State *L, int errcode);
|
||||
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
||||
|
||||
|
||||
#endif
|
||||
326
lex.c
326
lex.c
@@ -1,326 +0,0 @@
|
||||
char *rcs_lex = "$Id: lex.c,v 2.31 1996/03/19 16:50:24 roberto Exp roberto $";
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mem.h"
|
||||
#include "tree.h"
|
||||
#include "table.h"
|
||||
#include "lex.h"
|
||||
#include "inout.h"
|
||||
#include "luadebug.h"
|
||||
#include "parser.h"
|
||||
|
||||
#define MINBUFF 260
|
||||
|
||||
#define next() { current = input(); }
|
||||
#define save(x) { *yytextLast++ = (x); }
|
||||
#define save_and_next() { save(current); next(); }
|
||||
|
||||
static int current;
|
||||
static char *yytext = NULL;
|
||||
static int textsize = 0;
|
||||
static char *yytextLast;
|
||||
|
||||
static Input input;
|
||||
|
||||
void lua_setinput (Input fn)
|
||||
{
|
||||
current = ' ';
|
||||
input = fn;
|
||||
if (yytext == NULL)
|
||||
{
|
||||
textsize = MINBUFF;
|
||||
yytext = newvector(textsize, char);
|
||||
}
|
||||
}
|
||||
|
||||
char *lua_lasttext (void)
|
||||
{
|
||||
*yytextLast = 0;
|
||||
return yytext;
|
||||
}
|
||||
|
||||
|
||||
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} };
|
||||
|
||||
|
||||
#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
|
||||
|
||||
|
||||
void luaI_addReserved (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<RESERVEDSIZE; i++)
|
||||
{
|
||||
TaggedString *ts = lua_createstring(reserved[i].name);
|
||||
ts->marked = reserved[i].token; /* reserved word (always > 255) */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void growtext (void)
|
||||
{
|
||||
int size = yytextLast - yytext;
|
||||
textsize = growvector(&yytext, textsize, char, lexEM, MAX_WORD);
|
||||
yytextLast = yytext + size;
|
||||
}
|
||||
|
||||
|
||||
static int read_long_string (void)
|
||||
{
|
||||
int cont = 0;
|
||||
int spaceleft = textsize - (yytextLast - yytext);
|
||||
while (1)
|
||||
{
|
||||
if (spaceleft <= 2) /* may read more than 1 char in one cicle */
|
||||
{
|
||||
growtext();
|
||||
spaceleft = textsize - (yytextLast - yytext);
|
||||
}
|
||||
switch (current)
|
||||
{
|
||||
case EOF:
|
||||
case 0:
|
||||
return WRONGTOKEN;
|
||||
case '[':
|
||||
save_and_next(); spaceleft--;
|
||||
if (current == '[')
|
||||
{
|
||||
cont++;
|
||||
save_and_next(); spaceleft--;
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next(); spaceleft--;
|
||||
if (current == ']')
|
||||
{
|
||||
if (cont == 0) return STRING;
|
||||
cont--;
|
||||
save_and_next(); spaceleft--;
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
lua_linenumber++; /* goes through */
|
||||
default:
|
||||
save_and_next(); spaceleft--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaY_lex (void)
|
||||
{
|
||||
double a;
|
||||
static int linelasttoken = 0;
|
||||
if (lua_debug)
|
||||
luaI_codedebugline(linelasttoken);
|
||||
linelasttoken = lua_linenumber;
|
||||
while (1)
|
||||
{
|
||||
yytextLast = yytext;
|
||||
switch (current)
|
||||
{
|
||||
case EOF:
|
||||
case 0:
|
||||
return 0;
|
||||
case '\n': linelasttoken = ++lua_linenumber;
|
||||
case ' ':
|
||||
case '\r': /* CR: to avoid problems with DOS/Windows */
|
||||
case '\t':
|
||||
next();
|
||||
continue;
|
||||
|
||||
case '$':
|
||||
next();
|
||||
while (isalnum(current) || current == '_')
|
||||
save_and_next();
|
||||
*yytextLast = 0;
|
||||
if (strcmp(yytext, "debug") == 0)
|
||||
{
|
||||
luaY_lval.vInt = 1;
|
||||
return DEBUG;
|
||||
}
|
||||
else if (strcmp(yytext, "nodebug") == 0)
|
||||
{
|
||||
luaY_lval.vInt = 0;
|
||||
return DEBUG;
|
||||
}
|
||||
return WRONGTOKEN;
|
||||
|
||||
case '-':
|
||||
save_and_next();
|
||||
if (current != '-') return '-';
|
||||
do { next(); } while (current != '\n' && current != 0);
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
save_and_next();
|
||||
if (current != '[') return '[';
|
||||
else
|
||||
{
|
||||
save_and_next(); /* pass the second '[' */
|
||||
if (read_long_string() == WRONGTOKEN)
|
||||
return WRONGTOKEN;
|
||||
save_and_next(); /* pass the second ']' */
|
||||
*(yytextLast-2) = 0; /* erases ']]' */
|
||||
luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
|
||||
return STRING;
|
||||
}
|
||||
|
||||
case '=':
|
||||
save_and_next();
|
||||
if (current != '=') return '=';
|
||||
else { save_and_next(); return EQ; }
|
||||
|
||||
case '<':
|
||||
save_and_next();
|
||||
if (current != '=') return '<';
|
||||
else { save_and_next(); return LE; }
|
||||
|
||||
case '>':
|
||||
save_and_next();
|
||||
if (current != '=') return '>';
|
||||
else { save_and_next(); return GE; }
|
||||
|
||||
case '~':
|
||||
save_and_next();
|
||||
if (current != '=') return '~';
|
||||
else { save_and_next(); return NE; }
|
||||
|
||||
case '"':
|
||||
case '\'':
|
||||
{
|
||||
int del = current;
|
||||
int spaceleft = textsize - (yytextLast - yytext);
|
||||
next(); /* skip the delimiter */
|
||||
while (current != del)
|
||||
{
|
||||
if (spaceleft <= 2) /* may read more than 1 char in one cicle */
|
||||
{
|
||||
growtext();
|
||||
spaceleft = textsize - (yytextLast - yytext);
|
||||
}
|
||||
spaceleft--;
|
||||
switch (current)
|
||||
{
|
||||
case EOF:
|
||||
case 0:
|
||||
case '\n':
|
||||
return WRONGTOKEN;
|
||||
case '\\':
|
||||
next(); /* do not save the '\' */
|
||||
switch (current)
|
||||
{
|
||||
case 'n': save('\n'); next(); break;
|
||||
case 't': save('\t'); next(); break;
|
||||
case 'r': save('\r'); next(); break;
|
||||
case '\n': lua_linenumber++; /* goes through */
|
||||
default : save(current); next(); break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next();
|
||||
}
|
||||
}
|
||||
next(); /* skip the delimiter */
|
||||
*yytextLast = 0;
|
||||
luaY_lval.vWord = luaI_findconstantbyname(yytext);
|
||||
return STRING;
|
||||
}
|
||||
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e':
|
||||
case 'f': case 'g': case 'h': case 'i': case 'j':
|
||||
case 'k': case 'l': case 'm': case 'n': case 'o':
|
||||
case 'p': case 'q': case 'r': case 's': case 't':
|
||||
case 'u': case 'v': case 'w': case 'x': case 'y':
|
||||
case 'z':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E':
|
||||
case 'F': case 'G': case 'H': case 'I': case 'J':
|
||||
case 'K': case 'L': case 'M': case 'N': case 'O':
|
||||
case 'P': case 'Q': case 'R': case 'S': case 'T':
|
||||
case 'U': case 'V': case 'W': case 'X': case 'Y':
|
||||
case 'Z':
|
||||
case '_':
|
||||
{
|
||||
TaggedString *ts;
|
||||
do { save_and_next(); } while (isalnum(current) || current == '_');
|
||||
*yytextLast = 0;
|
||||
ts = lua_createstring(yytext);
|
||||
if (ts->marked > 2)
|
||||
return ts->marked; /* reserved word */
|
||||
luaY_lval.pTStr = ts;
|
||||
ts->marked = 2; /* avoid GC */
|
||||
return NAME;
|
||||
}
|
||||
|
||||
case '.':
|
||||
save_and_next();
|
||||
if (current == '.')
|
||||
{
|
||||
save_and_next();
|
||||
return CONC;
|
||||
}
|
||||
else if (!isdigit(current)) return '.';
|
||||
/* 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+(current-'0'); save_and_next(); } while (isdigit(current));
|
||||
if (current == '.') save_and_next();
|
||||
fraction:
|
||||
{ double da=0.1;
|
||||
while (isdigit(current))
|
||||
{a+=(current-'0')*da; da/=10.0; save_and_next()};
|
||||
if (current == 'e' || current == 'E')
|
||||
{
|
||||
int e=0;
|
||||
int neg;
|
||||
double ea;
|
||||
save_and_next();
|
||||
neg=(current=='-');
|
||||
if (current == '+' || current == '-') save_and_next();
|
||||
if (!isdigit(current)) return WRONGTOKEN;
|
||||
do { e=10.0*e+(current-'0'); save_and_next(); } while (isdigit(current));
|
||||
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
||||
{
|
||||
if (e & 1) a*=ea;
|
||||
ea*=ea;
|
||||
}
|
||||
}
|
||||
luaY_lval.vFloat = a;
|
||||
return NUMBER;
|
||||
}
|
||||
|
||||
default: /* also end of file */
|
||||
{
|
||||
save_and_next();
|
||||
return yytext[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
lex.h
19
lex.h
@@ -1,19 +0,0 @@
|
||||
/*
|
||||
** lex.h
|
||||
** TecCGraf - PUC-Rio
|
||||
** $Id: lex.h,v 1.1 1996/02/13 17:30:39 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef lex_h
|
||||
#define lex_h
|
||||
|
||||
|
||||
typedef int (*Input) (void);
|
||||
|
||||
void lua_setinput (Input fn);
|
||||
char *lua_lasttext (void);
|
||||
int luaY_lex (void);
|
||||
void luaI_addReserved (void);
|
||||
|
||||
|
||||
#endif
|
||||
133
lfunc.c
Normal file
133
lfunc.c
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 1.56 2002/05/02 13:06:20 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
|
||||
cast(int, sizeof(TObject)*((n)-1)))
|
||||
|
||||
#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
|
||||
cast(int, sizeof(TObject *)*((n)-1)))
|
||||
|
||||
|
||||
|
||||
Closure *luaF_newCclosure (lua_State *L, int nelems) {
|
||||
Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
|
||||
c->c.isC = 1;
|
||||
c->c.next = G(L)->rootcl;
|
||||
G(L)->rootcl = c;
|
||||
c->c.marked = 0;
|
||||
c->c.nupvalues = cast(lu_byte, nelems);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *gt) {
|
||||
Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
|
||||
c->l.isC = 0;
|
||||
c->c.next = G(L)->rootcl;
|
||||
G(L)->rootcl = c;
|
||||
c->l.marked = 0;
|
||||
c->l.g = *gt;
|
||||
c->l.nupvalues = cast(lu_byte, nelems);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
UpVal *luaF_findupval (lua_State *L, StkId level) {
|
||||
UpVal **pp = &L->openupval;
|
||||
UpVal *p;
|
||||
while ((p = *pp) != NULL && p->v >= level) {
|
||||
if (p->v == level) return p;
|
||||
pp = &p->next;
|
||||
}
|
||||
p = luaM_new(L, UpVal); /* not found: create a new one */
|
||||
p->v = level; /* current value lives in the stack */
|
||||
p->next = *pp; /* chain it in the proper position */
|
||||
*pp = p;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
void luaF_close (lua_State *L, StkId level) {
|
||||
UpVal *p;
|
||||
while ((p = L->openupval) != NULL && p->v >= level) {
|
||||
setobj(&p->value, p->v); /* save current value */
|
||||
p->v = &p->value; /* now current value lives here */
|
||||
L->openupval = p->next; /* remove from `open' list */
|
||||
p->next = G(L)->rootupval; /* chain in `closed' list */
|
||||
G(L)->rootupval = p;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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->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->locvars = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->source = NULL;
|
||||
f->next = G(L)->rootproto; /* chain in list of protos */
|
||||
G(L)->rootproto = f;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||
luaM_freearray(L, f->code, f->sizecode, Instruction);
|
||||
if (f->lineinfo)
|
||||
luaM_freearray(L, f->lineinfo, f->sizecode, int);
|
||||
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_freelem(L, f);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
|
||||
sizeLclosure(c->l.nupvalues);
|
||||
luaM_free(L, c, size);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Look for n-th local variable at line `line' in function `func'.
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
const 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);
|
||||
}
|
||||
}
|
||||
return NULL; /* not found */
|
||||
}
|
||||
|
||||
25
lfunc.h
Normal file
25
lfunc.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
** $Id: lfunc.h,v 1.19 2001/11/29 20:22:22 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lfunc_h
|
||||
#define lfunc_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
Proto *luaF_newproto (lua_State *L);
|
||||
Closure *luaF_newCclosure (lua_State *L, int nelems);
|
||||
Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *gt);
|
||||
UpVal *luaF_findupval (lua_State *L, StkId level);
|
||||
void luaF_close (lua_State *L, StkId level);
|
||||
void luaF_freeproto (lua_State *L, Proto *f);
|
||||
void luaF_freeclosure (lua_State *L, Closure *c);
|
||||
|
||||
const char *luaF_getlocalname (const Proto *func, int local_number, int pc);
|
||||
|
||||
|
||||
#endif
|
||||
494
lgc.c
Normal file
494
lgc.c
Normal file
@@ -0,0 +1,494 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 1.144 2002/08/05 14:50:39 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
typedef struct GCState {
|
||||
Table *tmark; /* list of marked tables to be visited */
|
||||
Table *toclear; /* list of visited weak tables (to be cleared after GC) */
|
||||
lua_State *L;
|
||||
} GCState;
|
||||
|
||||
|
||||
/* mark a string; marks larger than 1 cannot be changed */
|
||||
#define strmark(s) {if ((s)->tsv.marked == 0) (s)->tsv.marked = 1;}
|
||||
|
||||
|
||||
/* unmarked tables are represented by pointing `mark' to themselves */
|
||||
#define ismarked(x) ((x)->mark != (x))
|
||||
|
||||
|
||||
|
||||
/* mark tricks for userdata */
|
||||
#define isudmarked(u) (u->uv.len & 1)
|
||||
#define markud(u) (u->uv.len |= 1)
|
||||
#define unmarkud(u) (u->uv.len &= (~(size_t)1))
|
||||
|
||||
#define isfinalized(u) (u->uv.len & 2)
|
||||
#define markfinalized(u) (u->uv.len |= 2)
|
||||
|
||||
|
||||
/* mark tricks for upvalues (assume that open upvalues are always marked) */
|
||||
#define isupvalmarked(uv) ((uv)->v != &(uv)->value)
|
||||
|
||||
|
||||
|
||||
#define ismarkable(o) (!((1 << ttype(o)) & \
|
||||
((1 << LUA_TNIL) | (1 << LUA_TNUMBER) | \
|
||||
(1 << LUA_TBOOLEAN) | (1 << LUA_TLIGHTUSERDATA))))
|
||||
|
||||
static void reallymarkobject (GCState *st, TObject *o);
|
||||
|
||||
#define markobject(st,o) if (ismarkable(o)) reallymarkobject(st,o)
|
||||
|
||||
|
||||
static void protomark (Proto *f) {
|
||||
if (!f->marked) {
|
||||
int i;
|
||||
f->marked = 1;
|
||||
strmark(f->source);
|
||||
for (i=0; i<f->sizek; i++) {
|
||||
if (ttisstring(f->k+i))
|
||||
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);
|
||||
}
|
||||
lua_assert(luaG_checkcode(f));
|
||||
}
|
||||
|
||||
|
||||
static void marktable (GCState *st, Table *h) {
|
||||
if (!ismarked(h)) {
|
||||
h->mark = st->tmark; /* chain it for later traversal */
|
||||
st->tmark = h;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markclosure (GCState *st, Closure *cl) {
|
||||
if (!cl->c.marked) {
|
||||
cl->c.marked = 1;
|
||||
if (cl->c.isC) {
|
||||
int i;
|
||||
for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
|
||||
markobject(st, &cl->c.upvalue[i]);
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
lua_assert(cl->l.nupvalues == cl->l.p->nupvalues);
|
||||
marktable(st, hvalue(&cl->l.g));
|
||||
protomark(cl->l.p);
|
||||
for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */
|
||||
UpVal *u = cl->l.upvals[i];
|
||||
if (!isupvalmarked(u)) {
|
||||
markobject(st, &u->value);
|
||||
u->v = NULL; /* mark it! */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markudata (GCState *st, Udata *u) {
|
||||
markud(u);
|
||||
marktable(st, u->uv.metatable);
|
||||
}
|
||||
|
||||
|
||||
static void reallymarkobject (GCState *st, TObject *o) {
|
||||
switch (ttype(o)) {
|
||||
case LUA_TSTRING:
|
||||
strmark(tsvalue(o));
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
if (!isudmarked(uvalue(o)))
|
||||
markudata(st, uvalue(o));
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
markclosure(st, clvalue(o));
|
||||
break;
|
||||
case LUA_TTABLE: {
|
||||
marktable(st, hvalue(o));
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0); /* should not be called with other types */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void checkstacksizes (lua_State *L, StkId max) {
|
||||
int used = L->ci - L->base_ci; /* number of `ci' in use */
|
||||
if (4*used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
|
||||
luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
|
||||
used = max - L->stack; /* part of stack in use */
|
||||
if (4*used < L->stacksize && 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
|
||||
luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
|
||||
}
|
||||
|
||||
|
||||
static void markstacks (GCState *st) {
|
||||
lua_State *L1 = st->L;
|
||||
do { /* for each thread */
|
||||
StkId o, lim;
|
||||
CallInfo *ci;
|
||||
if (ttisnil(defaultmeta(L1))) { /* incomplete state? */
|
||||
lua_assert(L1 != st->L);
|
||||
L1 = L1->next;
|
||||
luaE_closethread(st->L, L1->previous); /* collect it */
|
||||
continue;
|
||||
}
|
||||
markobject(st, defaultmeta(L1));
|
||||
markobject(st, gt(L1));
|
||||
markobject(st, registry(L1));
|
||||
for (o=L1->stack; o<L1->top; o++)
|
||||
markobject(st, o);
|
||||
lim = o;
|
||||
for (ci = L1->base_ci; ci <= L1->ci; ci++) {
|
||||
lua_assert(ci->top <= L1->stack_last);
|
||||
if (lim < ci->top) lim = ci->top;
|
||||
}
|
||||
for (; o<=lim; o++) setnilvalue(o);
|
||||
checkstacksizes(L1, lim);
|
||||
lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
|
||||
L1 = L1->next;
|
||||
} while (L1 != st->L);
|
||||
}
|
||||
|
||||
|
||||
static void marktmu (GCState *st) {
|
||||
Udata *u;
|
||||
for (u = G(st->L)->tmudata; u; u = u->uv.next)
|
||||
markudata(st, u);
|
||||
}
|
||||
|
||||
|
||||
/* move `dead' udata that need finalization to list `tmudata' */
|
||||
static void separateudata (lua_State *L) {
|
||||
Udata **p = &G(L)->rootudata;
|
||||
Udata *curr;
|
||||
Udata *collected = NULL; /* to collect udata with gc event */
|
||||
Udata **lastcollected = &collected;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (isudmarked(curr) || isfinalized(curr) ||
|
||||
(fasttm(L, curr->uv.metatable, TM_GC) == NULL))
|
||||
p = &curr->uv.next;
|
||||
else { /* must call its gc method */
|
||||
*p = curr->uv.next;
|
||||
curr->uv.next = NULL; /* link `curr' at the end of `collected' list */
|
||||
*lastcollected = curr;
|
||||
lastcollected = &curr->uv.next;
|
||||
}
|
||||
}
|
||||
/* insert collected udata with gc event into `tmudata' list */
|
||||
*lastcollected = G(L)->tmudata;
|
||||
G(L)->tmudata = collected;
|
||||
}
|
||||
|
||||
|
||||
static void removekey (Node *n) {
|
||||
setnilvalue(val(n)); /* remove corresponding value ... */
|
||||
if (ismarkable(key(n)))
|
||||
setttype(key(n), LUA_TNONE); /* dead key; remove it */
|
||||
}
|
||||
|
||||
|
||||
static void traversetable (GCState *st, Table *h) {
|
||||
int i;
|
||||
int weakkey = 0;
|
||||
int weakvalue = 0;
|
||||
marktable(st, h->metatable);
|
||||
lua_assert(h->lsizenode || h->node == G(st->L)->dummynode);
|
||||
if (h->mode & (WEAKKEY | WEAKVALUE)) { /* weak table? */
|
||||
weakkey = h->mode & WEAKKEY;
|
||||
weakvalue = h->mode & WEAKVALUE;
|
||||
h->mark = st->toclear; /* must be cleared after GC, ... */
|
||||
st->toclear = h; /* ... so put in the appropriate list */
|
||||
}
|
||||
if (!weakvalue) {
|
||||
i = sizearray(h);
|
||||
while (i--)
|
||||
markobject(st, &h->array[i]);
|
||||
}
|
||||
i = sizenode(h);
|
||||
while (i--) {
|
||||
Node *n = node(h, i);
|
||||
if (!ttisnil(val(n))) {
|
||||
lua_assert(!ttisnil(key(n)));
|
||||
if (!weakkey) markobject(st, key(n));
|
||||
if (!weakvalue) markobject(st, val(n));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void propagatemarks (GCState *st) {
|
||||
while (st->tmark) { /* traverse marked tables */
|
||||
Table *h = st->tmark; /* get first table from list */
|
||||
st->tmark = h->mark; /* remove it from list */
|
||||
traversetable(st, h);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int hasmark (const TObject *o) {
|
||||
switch (ttype(o)) {
|
||||
case LUA_TUSERDATA:
|
||||
return isudmarked(uvalue(o));
|
||||
case LUA_TTABLE:
|
||||
return ismarked(hvalue(o));
|
||||
case LUA_TFUNCTION:
|
||||
return clvalue(o)->c.marked;
|
||||
case LUA_TSTRING:
|
||||
strmark(tsvalue(o)); /* strings are `values', so are never weak */
|
||||
/* go through */
|
||||
default: /* number, nil, boolean, udataval */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** clear collected keys from weaktables
|
||||
*/
|
||||
static void cleartablekeys (GCState *st) {
|
||||
Table *h;
|
||||
for (h = st->toclear; h; h = h->mark) {
|
||||
lua_assert(h->mode & (WEAKKEY | WEAKVALUE));
|
||||
if ((h->mode & WEAKKEY)) { /* table may have collected keys? */
|
||||
int i = sizenode(h);
|
||||
while (i--) {
|
||||
Node *n = node(h, i);
|
||||
if (!hasmark(key(n))) /* key was collected? */
|
||||
removekey(n); /* remove entry from table */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** clear collected values from weaktables
|
||||
*/
|
||||
static void cleartablevalues (GCState *st) {
|
||||
Table *h;
|
||||
for (h = st->toclear; h; h = h->mark) {
|
||||
if ((h->mode & WEAKVALUE)) { /* table may have collected values? */
|
||||
int i = sizearray(h);
|
||||
while (i--) {
|
||||
TObject *o = &h->array[i];
|
||||
if (!hasmark(o)) /* value was collected? */
|
||||
setnilvalue(o); /* remove value */
|
||||
}
|
||||
i = sizenode(h);
|
||||
while (i--) {
|
||||
Node *n = node(h, i);
|
||||
if (!hasmark(val(n))) /* value was collected? */
|
||||
removekey(n); /* remove entry from table */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 collectclosures (lua_State *L) {
|
||||
Closure **p = &G(L)->rootcl;
|
||||
Closure *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (curr->c.marked) {
|
||||
curr->c.marked = 0;
|
||||
p = &curr->c.next;
|
||||
}
|
||||
else {
|
||||
*p = curr->c.next;
|
||||
luaF_freeclosure(L, curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collectupval (lua_State *L) {
|
||||
UpVal **v = &G(L)->rootupval;
|
||||
UpVal *curr;
|
||||
while ((curr = *v) != NULL) {
|
||||
if (isupvalmarked(curr)) {
|
||||
lua_assert(curr->v == NULL);
|
||||
curr->v = &curr->value; /* unmark */
|
||||
v = &curr->next; /* next */
|
||||
}
|
||||
else {
|
||||
*v = curr->next; /* next */
|
||||
luaM_freelem(L, curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collecttable (lua_State *L) {
|
||||
Table **p = &G(L)->roottable;
|
||||
Table *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) {
|
||||
Udata **p = &G(L)->rootudata;
|
||||
Udata *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (isudmarked(curr)) {
|
||||
unmarkud(curr);
|
||||
p = &curr->uv.next;
|
||||
}
|
||||
else {
|
||||
*p = curr->uv.next;
|
||||
luaM_free(L, curr, sizeudata(curr->uv.len & (~(size_t)3)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 < cast(ls_nstr, G(L)->strt.size/4) &&
|
||||
G(L)->strt.size > MINSTRTABSIZE*2)
|
||||
luaS_resize(L, G(L)->strt.size/2); /* table is too big */
|
||||
}
|
||||
|
||||
|
||||
|
||||
#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, char);
|
||||
G(L)->Mbuffsize = newsize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void do1gcTM (lua_State *L, Udata *udata) {
|
||||
const TObject *tm = fasttm(L, udata->uv.metatable, TM_GC);
|
||||
if (tm != NULL) {
|
||||
setobj(L->top, tm);
|
||||
setuvalue(L->top+1, udata);
|
||||
L->top += 2;
|
||||
luaD_call(L, L->top - 2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void callGCTM (lua_State *L) {
|
||||
int oldah = allowhook(L);
|
||||
setallowhook(L, 0); /* stop debug hooks during GC tag methods */
|
||||
L->top++; /* reserve space to keep udata while runs its gc method */
|
||||
while (G(L)->tmudata != NULL) {
|
||||
Udata *udata = G(L)->tmudata;
|
||||
G(L)->tmudata = udata->uv.next; /* remove udata from `tmudata' */
|
||||
udata->uv.next = G(L)->rootudata; /* return it to `root' list */
|
||||
G(L)->rootudata = udata;
|
||||
setuvalue(L->top - 1, udata); /* keep a reference to it */
|
||||
unmarkud(udata);
|
||||
markfinalized(udata);
|
||||
do1gcTM(L, udata);
|
||||
}
|
||||
L->top--;
|
||||
setallowhook(L, oldah); /* restore hooks */
|
||||
}
|
||||
|
||||
|
||||
void luaC_callallgcTM (lua_State *L) {
|
||||
separateudata(L);
|
||||
callGCTM(L); /* call their GC tag methods */
|
||||
}
|
||||
|
||||
|
||||
void luaC_collect (lua_State *L, int all) {
|
||||
collectudata(L);
|
||||
collectstrings(L, all);
|
||||
collecttable(L);
|
||||
collectproto(L);
|
||||
collectupval(L);
|
||||
collectclosures(L);
|
||||
}
|
||||
|
||||
|
||||
void luaC_collectgarbage (lua_State *L) {
|
||||
GCState st;
|
||||
st.L = L;
|
||||
st.tmark = NULL;
|
||||
st.toclear = NULL;
|
||||
markstacks(&st); /* mark all stacks */
|
||||
propagatemarks(&st); /* mark all reachable objects */
|
||||
cleartablevalues(&st);
|
||||
separateudata(L); /* separate userdata to be preserved */
|
||||
marktmu(&st); /* mark `preserved' userdata */
|
||||
propagatemarks(&st); /* remark */
|
||||
cleartablekeys(&st);
|
||||
luaC_collect(L, 0);
|
||||
checkMbuffer(L);
|
||||
G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
|
||||
callGCTM(L);
|
||||
}
|
||||
|
||||
23
lgc.h
Normal file
23
lgc.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
** $Id: lgc.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lgc_h
|
||||
#define lgc_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#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. <<<<<<<<<
|
||||
|
||||
630
liolib.c
Normal file
630
liolib.c
Normal file
@@ -0,0 +1,630 @@
|
||||
/*
|
||||
** $Id: liolib.c,v 2.14 2002/07/17 16:25:13 roberto Exp roberto $
|
||||
** Standard I/O (and system) library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <locale.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** FILE Operations
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
#ifndef POPEN
|
||||
#define pclose(f) (-1)
|
||||
#endif
|
||||
|
||||
|
||||
#define FILEHANDLE "FileHandle"
|
||||
#define CLOSEDFILEHANDLE "ClosedFileHandle"
|
||||
|
||||
#define IO_INPUT "_input"
|
||||
#define IO_OUTPUT "_output"
|
||||
|
||||
|
||||
static int pushresult (lua_State *L, int i) {
|
||||
if (i) {
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, strerror(errno));
|
||||
lua_pushnumber(L, errno);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static FILE *tofile (lua_State *L, int findex) {
|
||||
FILE **f = (FILE **)lua_touserdata(L, findex);
|
||||
if (f && lua_getmetatable(L, findex) &&
|
||||
lua_rawequal(L, -1, lua_upvalueindex(1))) {
|
||||
lua_pop(L, 1);
|
||||
return *f;
|
||||
}
|
||||
if (findex > 0)
|
||||
luaL_argerror(L, findex, "bad file");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static void newfile (lua_State *L, FILE *f) {
|
||||
lua_boxpointer(L, f);
|
||||
lua_pushliteral(L, FILEHANDLE);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
|
||||
static void registerfile (lua_State *L, FILE *f, const char *name,
|
||||
const char *impname) {
|
||||
lua_pushstring(L, name);
|
||||
newfile(L, f);
|
||||
if (impname) {
|
||||
lua_pushstring(L, impname);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, -6);
|
||||
}
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static int setnewfile (lua_State *L, FILE *f) {
|
||||
if (f == NULL)
|
||||
return pushresult(L, 0);
|
||||
else {
|
||||
newfile(L, f);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int io_close (lua_State *L) {
|
||||
FILE *f;
|
||||
int status = 1;
|
||||
if (lua_isnone(L, 1)) {
|
||||
lua_pushstring(L, IO_OUTPUT);
|
||||
lua_rawget(L, lua_upvalueindex(1));
|
||||
}
|
||||
f = tofile(L, 1);
|
||||
if (f != stdin && f != stdout && f != stderr) {
|
||||
lua_settop(L, 1); /* make sure file is on top */
|
||||
lua_pushliteral(L, CLOSEDFILEHANDLE);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
lua_setmetatable(L, 1);
|
||||
status = (pclose(f) != -1) || (fclose(f) == 0);
|
||||
}
|
||||
return pushresult(L, status);
|
||||
}
|
||||
|
||||
|
||||
static int io_open (lua_State *L) {
|
||||
FILE *f = fopen(luaL_check_string(L, 1), luaL_opt_string(L, 2, "r"));
|
||||
return setnewfile(L, f);
|
||||
}
|
||||
|
||||
|
||||
static int io_popen (lua_State *L) {
|
||||
#ifndef POPEN
|
||||
luaL_error(L, "`popen' not supported");
|
||||
return 0;
|
||||
#else
|
||||
FILE *f = popen(luaL_check_string(L, 1), luaL_opt_string(L, 2, "r"));
|
||||
return setnewfile(L, f);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
static int io_tmpfile (lua_State *L) {
|
||||
return setnewfile(L, tmpfile());
|
||||
}
|
||||
|
||||
|
||||
static FILE *getiofile (lua_State *L, const char *name) {
|
||||
FILE *f;
|
||||
lua_pushstring(L, name);
|
||||
lua_rawget(L, lua_upvalueindex(1));
|
||||
f = tofile(L, -1);
|
||||
if (f == NULL)
|
||||
luaL_error(L, "%s is closed", name);
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
static int g_iofile (lua_State *L, const char *name, const char *mode) {
|
||||
if (lua_isnoneornil(L, 1)) {
|
||||
lua_pushstring(L, name);
|
||||
lua_rawget(L, lua_upvalueindex(1));
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
const char *filename = lua_tostring(L, 1);
|
||||
lua_pushstring(L, name);
|
||||
if (filename) {
|
||||
FILE *f = fopen(filename, mode);
|
||||
luaL_arg_check(L, f, 1, strerror(errno));
|
||||
newfile(L, f);
|
||||
}
|
||||
else {
|
||||
tofile(L, 1); /* check that it's a valid file handle */
|
||||
lua_pushvalue(L, 1);
|
||||
}
|
||||
lua_rawset(L, lua_upvalueindex(1));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int io_input (lua_State *L) {
|
||||
return g_iofile(L, IO_INPUT, "r");
|
||||
}
|
||||
|
||||
|
||||
static int io_output (lua_State *L) {
|
||||
return g_iofile(L, IO_OUTPUT, "w");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** READ
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static int read_number (lua_State *L, FILE *f) {
|
||||
lua_Number d;
|
||||
if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
|
||||
lua_pushnumber(L, d);
|
||||
return 1;
|
||||
}
|
||||
else return 0; /* read fails */
|
||||
}
|
||||
|
||||
|
||||
static int test_eof (lua_State *L, FILE *f) {
|
||||
int c = getc(f);
|
||||
ungetc(c, f);
|
||||
lua_pushlstring(L, NULL, 0);
|
||||
return (c != EOF);
|
||||
}
|
||||
|
||||
|
||||
static int read_line (lua_State *L, FILE *f) {
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
for (;;) {
|
||||
size_t l;
|
||||
char *p = luaL_prepbuffer(&b);
|
||||
if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
|
||||
luaL_pushresult(&b); /* close buffer */
|
||||
return (lua_strlen(L, -1) > 0); /* check whether read something */
|
||||
}
|
||||
l = strlen(p);
|
||||
if (p[l-1] != '\n')
|
||||
luaL_addsize(&b, l);
|
||||
else {
|
||||
luaL_addsize(&b, l - 1); /* do not include `eol' */
|
||||
luaL_pushresult(&b); /* close buffer */
|
||||
return 1; /* read at least an `eol' */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int read_chars (lua_State *L, FILE *f, size_t n) {
|
||||
size_t rlen; /* how much to read */
|
||||
size_t nr; /* number of chars actually read */
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
|
||||
do {
|
||||
char *p = luaL_prepbuffer(&b);
|
||||
if (rlen > n) rlen = n; /* cannot read more than asked */
|
||||
nr = fread(p, sizeof(char), rlen, f);
|
||||
luaL_addsize(&b, nr);
|
||||
n -= nr; /* still have to read `n' chars */
|
||||
} while (n > 0 && nr == rlen); /* until end of count or eof */
|
||||
luaL_pushresult(&b); /* close buffer */
|
||||
return (n == 0 || lua_strlen(L, -1) > 0);
|
||||
}
|
||||
|
||||
|
||||
static int g_read (lua_State *L, FILE *f, int first) {
|
||||
int nargs = lua_gettop(L) - 1;
|
||||
int success;
|
||||
int n;
|
||||
if (nargs == 0) { /* no arguments? */
|
||||
success = read_line(L, f);
|
||||
n = first+1; /* to return 1 result */
|
||||
}
|
||||
else { /* ensure stack space for all results and for auxlib's buffer */
|
||||
luaL_check_stack(L, nargs+LUA_MINSTACK, "too many arguments");
|
||||
success = 1;
|
||||
for (n = first; nargs-- && success; n++) {
|
||||
if (lua_type(L, n) == LUA_TNUMBER) {
|
||||
size_t l = (size_t)lua_tonumber(L, n);
|
||||
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
|
||||
}
|
||||
else {
|
||||
const char *p = lua_tostring(L, n);
|
||||
if (!p || p[0] != '*')
|
||||
return luaL_error(L, "invalid `read' option");
|
||||
switch (p[1]) {
|
||||
case 'n': /* number */
|
||||
success = read_number(L, f);
|
||||
break;
|
||||
case 'l': /* line */
|
||||
success = read_line(L, f);
|
||||
break;
|
||||
case 'a': /* file */
|
||||
read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
|
||||
success = 1; /* always success */
|
||||
break;
|
||||
case 'w': /* word */
|
||||
return luaL_error(L, "obsolete option `*w'");
|
||||
default:
|
||||
return luaL_argerror(L, n, "invalid format");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!success) {
|
||||
lua_pop(L, 1); /* remove last result */
|
||||
lua_pushnil(L); /* push nil instead */
|
||||
}
|
||||
return n - first;
|
||||
}
|
||||
|
||||
|
||||
static int io_read (lua_State *L) {
|
||||
return g_read(L, getiofile(L, IO_INPUT), 1);
|
||||
}
|
||||
|
||||
|
||||
static int f_read (lua_State *L) {
|
||||
return g_read(L, tofile(L, 1), 2);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static int g_write (lua_State *L, FILE *f, int arg) {
|
||||
int nargs = lua_gettop(L) - 1;
|
||||
int status = 1;
|
||||
for (; nargs--; arg++) {
|
||||
if (lua_type(L, arg) == LUA_TNUMBER) {
|
||||
/* optimization: could be done exactly as for strings */
|
||||
status = status &&
|
||||
fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
|
||||
}
|
||||
else {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, arg, &l);
|
||||
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
||||
}
|
||||
}
|
||||
pushresult(L, status);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int io_write (lua_State *L) {
|
||||
return g_write(L, getiofile(L, IO_OUTPUT), 1);
|
||||
}
|
||||
|
||||
|
||||
static int f_write (lua_State *L) {
|
||||
return g_write(L, tofile(L, 1), 2);
|
||||
}
|
||||
|
||||
|
||||
static int f_seek (lua_State *L) {
|
||||
static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
|
||||
static const char *const modenames[] = {"set", "cur", "end", NULL};
|
||||
FILE *f = tofile(L, 1);
|
||||
int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
|
||||
long offset = luaL_opt_long(L, 3, 0);
|
||||
luaL_arg_check(L, op != -1, 2, "invalid mode");
|
||||
op = fseek(f, offset, mode[op]);
|
||||
if (op)
|
||||
return pushresult(L, 0); /* error */
|
||||
else {
|
||||
lua_pushnumber(L, ftell(f));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int io_flush (lua_State *L) {
|
||||
return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static int f_flush (lua_State *L) {
|
||||
return pushresult(L, fflush(tofile(L, 1)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static const luaL_reg iolib[] = {
|
||||
{"input", io_input},
|
||||
{"output", io_output},
|
||||
{"close", io_close},
|
||||
{"flush", io_flush},
|
||||
{"open", io_open},
|
||||
{"popen", io_popen},
|
||||
{"read", io_read},
|
||||
{"tmpfile", io_tmpfile},
|
||||
{"write", io_write},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
static const luaL_reg flib[] = {
|
||||
{"flush", f_flush},
|
||||
{"read", f_read},
|
||||
{"seek", f_seek},
|
||||
{"write", f_write},
|
||||
{"close", io_close},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
static void createmeta (lua_State *L) {
|
||||
lua_pushliteral(L, FILEHANDLE); /* S: FH */
|
||||
lua_newtable(L); /* S: mt FH */
|
||||
/* close files when collected */
|
||||
lua_pushliteral(L, "__gc"); /* S: `gc' mt FH */
|
||||
lua_pushvalue(L, -2); /* S: mt `gc' mt FH */
|
||||
lua_pushcclosure(L, io_close, 1); /* S: close `gc' mt FH */
|
||||
lua_rawset(L, -3); /* S: mt FH */
|
||||
/* file methods */
|
||||
lua_pushliteral(L, "__gettable"); /* S: `gettable' mt FH */
|
||||
lua_pushvalue(L, -2); /* S: mt `gettable' mt FH */
|
||||
lua_rawset(L, -3); /* S: mt FH */
|
||||
lua_pushvalue(L, -1); /* S: mt mt FH */
|
||||
luaL_openlib(L, flib, 1); /* S: mt FH */
|
||||
/* put new metatable into registry */
|
||||
lua_rawset(L, LUA_REGISTRYINDEX); /* S: empty */
|
||||
/* meta table for CLOSEDFILEHANDLE */
|
||||
lua_pushliteral(L, CLOSEDFILEHANDLE);
|
||||
lua_newtable(L);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Other O.S. Operations
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static int io_execute (lua_State *L) {
|
||||
lua_pushnumber(L, system(luaL_check_string(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int io_remove (lua_State *L) {
|
||||
return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static int io_rename (lua_State *L) {
|
||||
return pushresult(L, rename(luaL_check_string(L, 1),
|
||||
luaL_check_string(L, 2)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static int io_tmpname (lua_State *L) {
|
||||
char buff[L_tmpnam];
|
||||
if (tmpnam(buff) != buff)
|
||||
return luaL_error(L, "unable to generate a unique filename");
|
||||
lua_pushstring(L, buff);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int io_getenv (lua_State *L) {
|
||||
lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int io_clock (lua_State *L) {
|
||||
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Time/Date operations
|
||||
** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
|
||||
** wday=%w+1, yday=%j, isdst=? }
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void setfield (lua_State *L, const char *key, int value) {
|
||||
lua_pushstring(L, key);
|
||||
lua_pushnumber(L, value);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
static void setboolfield (lua_State *L, const char *key, int value) {
|
||||
lua_pushstring(L, key);
|
||||
lua_pushboolean(L, value);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
static int getboolfield (lua_State *L, const char *key) {
|
||||
int res;
|
||||
lua_pushstring(L, key);
|
||||
lua_gettable(L, -2);
|
||||
res = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static int getfield (lua_State *L, const char *key, int d) {
|
||||
int res;
|
||||
lua_pushstring(L, key);
|
||||
lua_gettable(L, -2);
|
||||
if (lua_isnumber(L, -1))
|
||||
res = (int)(lua_tonumber(L, -1));
|
||||
else {
|
||||
if (d == -2)
|
||||
return luaL_error(L, "field `%s' missing in date table", key);
|
||||
res = d;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static int io_date (lua_State *L) {
|
||||
const char *s = luaL_opt_string(L, 1, "%c");
|
||||
time_t t = (time_t)(luaL_opt_number(L, 2, -1));
|
||||
struct tm *stm;
|
||||
if (t == (time_t)(-1)) /* no time given? */
|
||||
t = time(NULL); /* use current time */
|
||||
if (*s == '!') { /* UTC? */
|
||||
stm = gmtime(&t);
|
||||
s++; /* skip `!' */
|
||||
}
|
||||
else
|
||||
stm = localtime(&t);
|
||||
if (stm == NULL) /* invalid date? */
|
||||
lua_pushnil(L);
|
||||
else if (strcmp(s, "*t") == 0) {
|
||||
lua_newtable(L);
|
||||
setfield(L, "sec", stm->tm_sec);
|
||||
setfield(L, "min", stm->tm_min);
|
||||
setfield(L, "hour", stm->tm_hour);
|
||||
setfield(L, "day", stm->tm_mday);
|
||||
setfield(L, "month", stm->tm_mon+1);
|
||||
setfield(L, "year", stm->tm_year+1900);
|
||||
setfield(L, "wday", stm->tm_wday+1);
|
||||
setfield(L, "yday", stm->tm_yday+1);
|
||||
setboolfield(L, "isdst", stm->tm_isdst);
|
||||
}
|
||||
else {
|
||||
char b[256];
|
||||
if (strftime(b, sizeof(b), s, stm))
|
||||
lua_pushstring(L, b);
|
||||
else
|
||||
return luaL_error(L, "`date' format too long");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int io_time (lua_State *L) {
|
||||
if (lua_isnoneornil(L, 1)) /* called without args? */
|
||||
lua_pushnumber(L, time(NULL)); /* return current time */
|
||||
else {
|
||||
time_t t;
|
||||
struct tm ts;
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
lua_settop(L, 1); /* make sure table is at the top */
|
||||
ts.tm_sec = getfield(L, "sec", 0);
|
||||
ts.tm_min = getfield(L, "min", 0);
|
||||
ts.tm_hour = getfield(L, "hour", 12);
|
||||
ts.tm_mday = getfield(L, "day", -2);
|
||||
ts.tm_mon = getfield(L, "month", -2) - 1;
|
||||
ts.tm_year = getfield(L, "year", -2) - 1900;
|
||||
ts.tm_isdst = getboolfield(L, "isdst");
|
||||
t = mktime(&ts);
|
||||
if (t == (time_t)(-1))
|
||||
lua_pushnil(L);
|
||||
else
|
||||
lua_pushnumber(L, t);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int io_difftime (lua_State *L) {
|
||||
lua_pushnumber(L, difftime((time_t)(luaL_check_number(L, 1)),
|
||||
(time_t)(luaL_opt_number(L, 2, 0))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static int io_setloc (lua_State *L) {
|
||||
static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
|
||||
LC_NUMERIC, LC_TIME};
|
||||
static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
|
||||
"numeric", "time", NULL};
|
||||
const char *l = lua_tostring(L, 1);
|
||||
int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
|
||||
luaL_arg_check(L, l || lua_isnoneornil(L, 1), 1, "string expected");
|
||||
luaL_arg_check(L, op != -1, 2, "invalid option");
|
||||
lua_pushstring(L, setlocale(cat[op], l));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int io_exit (lua_State *L) {
|
||||
exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
static const luaL_reg syslib[] = {
|
||||
{"clock", io_clock},
|
||||
{"date", io_date},
|
||||
{"difftime", io_difftime},
|
||||
{"execute", io_execute},
|
||||
{"exit", io_exit},
|
||||
{"getenv", io_getenv},
|
||||
{"remove", io_remove},
|
||||
{"rename", io_rename},
|
||||
{"setlocale", io_setloc},
|
||||
{"time", io_time},
|
||||
{"tmpname", io_tmpname},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
LUALIB_API int lua_iolibopen (lua_State *L) {
|
||||
createmeta(L);
|
||||
luaL_opennamedlib(L, LUA_OSLIBNAME, syslib, 0);
|
||||
lua_pushliteral(L, FILEHANDLE); /* S: FH */
|
||||
lua_rawget(L, LUA_REGISTRYINDEX); /* S: mt */
|
||||
lua_pushvalue(L, -1); /* S: mt mt */
|
||||
luaL_opennamedlib(L, LUA_IOLIBNAME, iolib, 1); /* S: mt */
|
||||
lua_pushliteral(L, LUA_IOLIBNAME); /* S: `io' mt */
|
||||
lua_gettable(L, LUA_GLOBALSINDEX); /* S: io mt */
|
||||
/* put predefined file handles into `io' table */
|
||||
registerfile(L, stdin, "stdin", IO_INPUT);
|
||||
registerfile(L, stdout, "stdout", IO_OUTPUT);
|
||||
registerfile(L, stderr, "stderr", NULL);
|
||||
lua_pop(L, 2); /* S: empty */
|
||||
return 0;
|
||||
}
|
||||
|
||||
409
llex.c
Normal file
409
llex.c
Normal file
@@ -0,0 +1,409 @@
|
||||
/*
|
||||
** $Id: llex.c,v 1.107 2002/07/08 18:14:36 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "llex.h"
|
||||
#include "lobject.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
#define next(LS) (LS->current = zgetc(LS->z))
|
||||
|
||||
|
||||
|
||||
/* ORDER RESERVED */
|
||||
static const char *const token2string [] = {
|
||||
"and", "break", "do", "else", "elseif",
|
||||
"end", "false", "for", "function", "global", "if",
|
||||
"in", "local", "nil", "not", "or", "repeat",
|
||||
"return", "then", "true", "until", "while", "*name",
|
||||
"..", "...", "==", ">=", "<=", "~=",
|
||||
"*number", "*string", "<eof>"
|
||||
};
|
||||
|
||||
|
||||
void luaX_init (lua_State *L) {
|
||||
int i;
|
||||
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 = cast(unsigned short, RESERVEDMARK+i); /* reserved word */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define MAXSRC 80
|
||||
|
||||
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
|
||||
if (val > limit) {
|
||||
msg = luaO_pushfstring(ls->L, "too many %s (limit=%d)", msg, limit);
|
||||
luaX_syntaxerror(ls, msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaX_error (LexState *ls, const char *s, const char *token) {
|
||||
lua_State *L = ls->L;
|
||||
char buff[MAXSRC];
|
||||
luaO_chunkid(buff, getstr(ls->source), MAXSRC);
|
||||
luaO_pushfstring(L, "%s:%d: %s near `%s'\n", buff, ls->linenumber, s, token);
|
||||
luaD_throw(L, LUA_ERRSYNTAX);
|
||||
}
|
||||
|
||||
|
||||
void luaX_syntaxerror (LexState *ls, const char *msg) {
|
||||
const char *lasttoken;
|
||||
switch (ls->t.token) {
|
||||
case TK_NAME:
|
||||
lasttoken = luaO_pushfstring(ls->L, "%s", getstr(ls->t.seminfo.ts));
|
||||
break;
|
||||
case TK_STRING:
|
||||
lasttoken = luaO_pushfstring(ls->L, "\"%s\"", getstr(ls->t.seminfo.ts));
|
||||
break;
|
||||
case TK_NUMBER:
|
||||
lasttoken = luaO_pushfstring(ls->L, "%f", ls->t.seminfo.r);
|
||||
break;
|
||||
default:
|
||||
lasttoken = luaX_token2str(ls, ls->t.token);
|
||||
break;
|
||||
}
|
||||
luaX_error(ls, msg, lasttoken);
|
||||
}
|
||||
|
||||
|
||||
const char *luaX_token2str (LexState *ls, int token) {
|
||||
if (token < FIRST_RESERVED) {
|
||||
lua_assert(token == (char)token);
|
||||
return luaO_pushfstring(ls->L, "%c", token);
|
||||
}
|
||||
else
|
||||
return token2string[token-FIRST_RESERVED];
|
||||
}
|
||||
|
||||
|
||||
static void luaX_lexerror (LexState *ls, const char *s, int token) {
|
||||
if (token == TK_EOS)
|
||||
luaX_error(ls, s, luaX_token2str(ls, token));
|
||||
else
|
||||
luaX_error(ls, s, cast(char *, G(ls->L)->Mbuffer));
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *LS) {
|
||||
next(LS); /* skip `\n' */
|
||||
++LS->linenumber;
|
||||
luaX_checklimit(LS, LS->linenumber, MAX_INT, "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 == '#') {
|
||||
do { /* skip first line */
|
||||
next(LS);
|
||||
} while (LS->current != '\n' && LS->current != EOZ);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** LEXICAL ANALYZER
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/* use Mbuffer to store names, literal strings and numbers */
|
||||
|
||||
#define EXTRABUFF 128
|
||||
#define checkbuffer(L, len) \
|
||||
if (((len)+10)*sizeof(char) > G(L)->Mbuffsize) \
|
||||
luaO_openspace(L, (len)+EXTRABUFF, char)
|
||||
|
||||
#define save(L, c, l) (cast(char *, G(L)->Mbuffer)[l++] = cast(char, c))
|
||||
#define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS))
|
||||
|
||||
|
||||
static size_t readname (LexState *LS) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, l);
|
||||
do {
|
||||
checkbuffer(L, l);
|
||||
save_and_next(L, LS, l);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(L, '\0', l);
|
||||
return l-1;
|
||||
}
|
||||
|
||||
|
||||
/* LUA_NUMBER */
|
||||
static void read_numeral (LexState *LS, int comma, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, l);
|
||||
if (comma) save(L, '.', l);
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
if (LS->current == '.') {
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == '.') {
|
||||
save_and_next(L, LS, l);
|
||||
save(L, '\0', l);
|
||||
luaX_lexerror(LS,
|
||||
"ambiguous syntax (decimal point x string concatenation)",
|
||||
TK_NUMBER);
|
||||
}
|
||||
}
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
if (LS->current == 'e' || LS->current == 'E') {
|
||||
save_and_next(L, LS, l); /* read `E' */
|
||||
if (LS->current == '+' || LS->current == '-')
|
||||
save_and_next(L, LS, l); /* optional exponent sign */
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
}
|
||||
save(L, '\0', l);
|
||||
if (!luaO_str2d(cast(char *, G(L)->Mbuffer), &seminfo->r))
|
||||
luaX_lexerror(LS, "malformed number", TK_NUMBER);
|
||||
}
|
||||
|
||||
|
||||
static void read_long_string (LexState *LS, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
int cont = 0;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, l);
|
||||
save(L, '[', l); /* save first `[' */
|
||||
save_and_next(L, LS, l); /* pass the second `[' */
|
||||
if (LS->current == '\n') /* string starts with a newline? */
|
||||
inclinenumber(LS); /* skip it */
|
||||
for (;;) {
|
||||
checkbuffer(L, l);
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
save(L, '\0', l);
|
||||
luaX_lexerror(LS, (seminfo) ? "unfinished long string" :
|
||||
"unfinished long comment", TK_EOS);
|
||||
break; /* to avoid warnings */
|
||||
case '[':
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == '[') {
|
||||
cont++;
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == ']') {
|
||||
if (cont == 0) goto endloop;
|
||||
cont--;
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
save(L, '\n', l);
|
||||
inclinenumber(LS);
|
||||
if (!seminfo) l = 0; /* reset buffer to avoid wasting space */
|
||||
continue;
|
||||
default:
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(L, LS, l); /* skip the second `]' */
|
||||
save(L, '\0', l);
|
||||
if (seminfo)
|
||||
seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+2, l-5);
|
||||
}
|
||||
|
||||
|
||||
static void read_string (LexState *LS, int del, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, l);
|
||||
save_and_next(L, LS, l);
|
||||
while (LS->current != del) {
|
||||
checkbuffer(L, l);
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
save(L, '\0', l);
|
||||
luaX_lexerror(LS, "unfinished string", TK_EOS);
|
||||
break; /* to avoid warnings */
|
||||
case '\n':
|
||||
save(L, '\0', l);
|
||||
luaX_lexerror(LS, "unfinished string", TK_STRING);
|
||||
break; /* to avoid warnings */
|
||||
case '\\':
|
||||
next(LS); /* do not save the `\' */
|
||||
switch (LS->current) {
|
||||
case 'a': save(L, '\a', l); next(LS); break;
|
||||
case 'b': save(L, '\b', l); next(LS); break;
|
||||
case 'f': save(L, '\f', l); next(LS); break;
|
||||
case 'n': save(L, '\n', l); next(LS); break;
|
||||
case 'r': save(L, '\r', l); next(LS); break;
|
||||
case 't': save(L, '\t', l); next(LS); break;
|
||||
case 'v': save(L, '\v', l); next(LS); break;
|
||||
case '\n': save(L, '\n', l); inclinenumber(LS); break;
|
||||
case EOZ: break; /* will raise an error next loop */
|
||||
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-'0');
|
||||
next(LS);
|
||||
} while (++i<3 && isdigit(LS->current));
|
||||
if (c > UCHAR_MAX) {
|
||||
save(L, '\0', l);
|
||||
luaX_lexerror(LS, "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, '\0', l);
|
||||
seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+1, l-3);
|
||||
}
|
||||
|
||||
|
||||
int luaX_lex (LexState *LS, SemInfo *seminfo) {
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
|
||||
case '\n': {
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
}
|
||||
case '-': {
|
||||
next(LS);
|
||||
if (LS->current != '-') return '-';
|
||||
/* else is a comment */
|
||||
next(LS);
|
||||
if (LS->current == '[' && (next(LS), LS->current == '['))
|
||||
read_long_string(LS, NULL); /* long comment */
|
||||
else /* short comment */
|
||||
while (LS->current != '\n' && LS->current != EOZ)
|
||||
next(LS);
|
||||
continue;
|
||||
}
|
||||
case '[': {
|
||||
next(LS);
|
||||
if (LS->current != '[') return '[';
|
||||
else {
|
||||
read_long_string(LS, seminfo);
|
||||
return TK_STRING;
|
||||
}
|
||||
}
|
||||
case '=': {
|
||||
next(LS);
|
||||
if (LS->current != '=') return '=';
|
||||
else { next(LS); return TK_EQ; }
|
||||
}
|
||||
case '<': {
|
||||
next(LS);
|
||||
if (LS->current != '=') return '<';
|
||||
else { next(LS); return TK_LE; }
|
||||
}
|
||||
case '>': {
|
||||
next(LS);
|
||||
if (LS->current != '=') return '>';
|
||||
else { next(LS); return TK_GE; }
|
||||
}
|
||||
case '~': {
|
||||
next(LS);
|
||||
if (LS->current != '=') return '~';
|
||||
else { next(LS); return TK_NE; }
|
||||
}
|
||||
case '"':
|
||||
case '\'': {
|
||||
read_string(LS, LS->current, seminfo);
|
||||
return TK_STRING;
|
||||
}
|
||||
case '.': {
|
||||
next(LS);
|
||||
if (LS->current == '.') {
|
||||
next(LS);
|
||||
if (LS->current == '.') {
|
||||
next(LS);
|
||||
return TK_DOTS; /* ... */
|
||||
}
|
||||
else return TK_CONCAT; /* .. */
|
||||
}
|
||||
else if (!isdigit(LS->current)) return '.';
|
||||
else {
|
||||
read_numeral(LS, 1, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
}
|
||||
case EOZ: {
|
||||
return TK_EOS;
|
||||
}
|
||||
default: {
|
||||
if (isspace(LS->current)) {
|
||||
next(LS);
|
||||
continue;
|
||||
}
|
||||
else if (isdigit(LS->current)) {
|
||||
read_numeral(LS, 0, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
else if (isalpha(LS->current) || LS->current == '_') {
|
||||
/* identifier or reserved word */
|
||||
size_t l = readname(LS);
|
||||
TString *ts = luaS_newlstr(LS->L, cast(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 {
|
||||
int c = LS->current;
|
||||
if (iscntrl(c))
|
||||
luaX_error(LS, "invalid control char",
|
||||
luaO_pushfstring(LS->L, "char(%d)", c));
|
||||
next(LS);
|
||||
return c; /* single-char tokens (+ - / ...) */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#undef next
|
||||
72
llex.h
Normal file
72
llex.h
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.42 2002/02/08 22:42:41 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef llex_h
|
||||
#define llex_h
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
#define FIRST_RESERVED 257
|
||||
|
||||
/* maximum length of a reserved word */
|
||||
#define TOKEN_LEN (sizeof("function")/sizeof(char))
|
||||
|
||||
|
||||
/*
|
||||
* 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_FALSE, 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_TRUE, 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 (cast(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; /* current character (charint) */
|
||||
int linenumber; /* input line counter */
|
||||
int lastline; /* line of last token `consumed' */
|
||||
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 */
|
||||
TString *source; /* current source name */
|
||||
} LexState;
|
||||
|
||||
|
||||
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 char *msg);
|
||||
void luaX_syntaxerror (LexState *ls, const char *s);
|
||||
const char *luaX_token2str (LexState *ls, int token);
|
||||
|
||||
|
||||
#endif
|
||||
153
llimits.h
Normal file
153
llimits.h
Normal file
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.44 2002/06/13 13:45:31 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; */
|
||||
/* it should be at least as large as size_t */
|
||||
typedef unsigned long lu_mem;
|
||||
|
||||
/* an integer big enough to count the number of strings in use */
|
||||
typedef long ls_nstr;
|
||||
|
||||
/* an integer big enough to count the number of steps when calling a
|
||||
** `count' hook */
|
||||
typedef long ls_count;
|
||||
|
||||
|
||||
/* 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
|
||||
*/
|
||||
#define IntPoint(p) ((lu_hash)(p))
|
||||
|
||||
|
||||
|
||||
/* 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; };
|
||||
|
||||
|
||||
#ifndef lua_assert
|
||||
#define lua_assert(c) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef check_exp
|
||||
#define check_exp(c,e) (e)
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef UNUSED
|
||||
#define UNUSED(x) ((void)(x)) /* to avoid warnings */
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef cast
|
||||
#define cast(t, exp) ((t)(exp))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** type for virtual-machine instructions
|
||||
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||
*/
|
||||
typedef unsigned long Instruction;
|
||||
|
||||
|
||||
/* maximum depth for calls */
|
||||
#ifndef LUA_MAXCALLS
|
||||
#define LUA_MAXCALLS 4096
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum size for the C stack */
|
||||
#ifndef LUA_MAXCSTACK
|
||||
#define LUA_MAXCSTACK 2048
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum stack for a Lua function */
|
||||
#define MAXSTACK 250
|
||||
|
||||
|
||||
/* maximum number of variables declared in a function */
|
||||
#ifndef MAXVARS
|
||||
#define MAXVARS 200 /* arbitrary limit (<MAXSTACK) */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of upvalues per function */
|
||||
#ifndef MAXUPVALUES
|
||||
#define MAXUPVALUES 32
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of parameters in a function */
|
||||
#ifndef MAXPARAMS
|
||||
#define MAXPARAMS 100 /* arbitrary limit (<MAXLOCALS) */
|
||||
#endif
|
||||
|
||||
|
||||
/* minimum size for the string table (must be power of 2) */
|
||||
#ifndef MINSTRTABSIZE
|
||||
#define MINSTRTABSIZE 32
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
247
lmathlib.c
Normal file
247
lmathlib.c
Normal file
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.50 2002/08/14 20:07:43 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#undef PI
|
||||
#define PI (3.14159265358979323846)
|
||||
#define RADIANS_PER_DEGREE (PI/180.0)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** If you want Lua to operate in degrees (instead of radians),
|
||||
** define USE_DEGREES
|
||||
*/
|
||||
#ifdef USE_DEGREES
|
||||
#define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
|
||||
#define TORAD(a) ((a)*RADIANS_PER_DEGREE)
|
||||
#else
|
||||
#define FROMRAD(a) (a)
|
||||
#define TORAD(a) (a)
|
||||
#endif
|
||||
|
||||
|
||||
static int math_abs (lua_State *L) {
|
||||
lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_sin (lua_State *L) {
|
||||
lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_cos (lua_State *L) {
|
||||
lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_tan (lua_State *L) {
|
||||
lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_asin (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_acos (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_atan (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
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 int math_ceil (lua_State *L) {
|
||||
lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_floor (lua_State *L) {
|
||||
lua_pushnumber(L, floor(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_mod (lua_State *L) {
|
||||
lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_sqrt (lua_State *L) {
|
||||
lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_pow (lua_State *L) {
|
||||
lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_log (lua_State *L) {
|
||||
lua_pushnumber(L, log(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_log10 (lua_State *L) {
|
||||
lua_pushnumber(L, log10(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_exp (lua_State *L) {
|
||||
lua_pushnumber(L, exp(luaL_check_number(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_deg (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
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 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(L, dmin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
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(L, dmax);
|
||||
return 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, "interval is empty");
|
||||
lua_pushnumber(L, (int)floor(r*u)+1); /* int 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, "interval is empty");
|
||||
lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */
|
||||
break;
|
||||
}
|
||||
default: return luaL_error(L, "wrong number of arguments");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_randomseed (lua_State *L) {
|
||||
srand(luaL_check_int(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const 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},
|
||||
{"frexp", math_frexp},
|
||||
{"ldexp", math_ldexp},
|
||||
{"sqrt", math_sqrt},
|
||||
{"min", math_min},
|
||||
{"max", math_max},
|
||||
{"log", math_log},
|
||||
{"log10", math_log10},
|
||||
{"exp", math_exp},
|
||||
{"deg", math_deg},
|
||||
{"pow", math_pow},
|
||||
{"rad", math_rad},
|
||||
{"random", math_random},
|
||||
{"randomseed", math_randomseed},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Open math library
|
||||
*/
|
||||
LUALIB_API int lua_mathlibopen (lua_State *L) {
|
||||
lua_pushliteral(L, LUA_MATHLIBNAME);
|
||||
lua_newtable(L);
|
||||
luaL_openlib(L, mathlib, 0);
|
||||
lua_pushliteral(L, "pi");
|
||||
lua_pushnumber(L, PI);
|
||||
lua_settable(L, -3);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
lua_pushliteral(L, "__pow");
|
||||
lua_pushcfunction(L, math_pow);
|
||||
lua_settable(L, LUA_REGISTRYINDEX);
|
||||
return 0;
|
||||
}
|
||||
|
||||
79
lmem.c
Normal file
79
lmem.c
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.56 2002/06/11 16:23:47 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** definition for realloc function. It must assure that
|
||||
** l_realloc(block, x, 0) frees the block, and l_realloc(NULL, 0, x)
|
||||
** allocates a new block (ANSI C assures that).
|
||||
** (`os' is the old block size; some allocators may use that.)
|
||||
*/
|
||||
#ifndef l_realloc
|
||||
#define l_realloc(b,os,s) realloc(b,s)
|
||||
#endif
|
||||
|
||||
|
||||
#define MINSIZEARRAY 4
|
||||
|
||||
|
||||
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
|
||||
int limit, const char *errormsg) {
|
||||
void *newblock;
|
||||
int newsize = (*size)*2;
|
||||
if (newsize < MINSIZEARRAY)
|
||||
newsize = MINSIZEARRAY; /* minimum size */
|
||||
else if (*size >= limit/2) { /* cannot double it? */
|
||||
if (*size < limit - MINSIZEARRAY) /* try something smaller... */
|
||||
newsize = limit; /* still have at least MINSIZEARRAY free places */
|
||||
else luaG_runerror(L, errormsg);
|
||||
}
|
||||
newblock = luaM_realloc(L, block,
|
||||
cast(lu_mem, *size)*cast(lu_mem, size_elems),
|
||||
cast(lu_mem, newsize)*cast(lu_mem, size_elems));
|
||||
*size = newsize; /* update only when everything else is OK */
|
||||
return newblock;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** generic allocation routine.
|
||||
*/
|
||||
void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
|
||||
if (size == 0) {
|
||||
if (block != NULL) {
|
||||
l_realloc(block, oldsize, size);
|
||||
block = NULL;
|
||||
}
|
||||
}
|
||||
else if (size >= MAX_SIZET)
|
||||
luaG_runerror(L, "memory allocation error: block too big");
|
||||
else {
|
||||
block = l_realloc(block, oldsize, size);
|
||||
if (block == NULL) {
|
||||
if (L)
|
||||
luaD_throw(L, LUA_ERRMEM);
|
||||
else return NULL; /* error before creating state! */
|
||||
}
|
||||
}
|
||||
if (L && G(L)) {
|
||||
G(L)->nblocks -= oldsize;
|
||||
G(L)->nblocks += size;
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
44
lmem.h
Normal file
44
lmem.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.25 2001/11/28 20:13:13 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lmem_h
|
||||
#define lmem_h
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
#define MEMERRMSG "not enough memory"
|
||||
|
||||
|
||||
void *luaM_realloc (lua_State *L, void *oldblock, lu_mem oldsize, lu_mem size);
|
||||
|
||||
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elem,
|
||||
int limit, const char *errormsg);
|
||||
|
||||
#define luaM_free(L, b, s) luaM_realloc(L, (b), (s), 0)
|
||||
#define luaM_freelem(L, b) luaM_realloc(L, (b), sizeof(*(b)), 0)
|
||||
#define luaM_freearray(L, b, n, t) luaM_realloc(L, (b), \
|
||||
cast(lu_mem, n)*cast(lu_mem, sizeof(t)), 0)
|
||||
|
||||
#define luaM_malloc(L, t) luaM_realloc(L, NULL, 0, (t))
|
||||
#define luaM_new(L, t) cast(t *, luaM_malloc(L, sizeof(t)))
|
||||
#define luaM_newvector(L, n,t) cast(t *, luaM_malloc(L, \
|
||||
cast(lu_mem, n)*cast(lu_mem, sizeof(t))))
|
||||
|
||||
#define luaM_growvector(L,v,nelems,size,t,limit,e) \
|
||||
if (((nelems)+1) > (size)) \
|
||||
((v)=cast(t *, luaM_growaux(L,v,&(size),sizeof(t),limit,e)))
|
||||
|
||||
#define luaM_reallocvector(L, v,oldn,n,t) \
|
||||
((v)=cast(t *, luaM_realloc(L, v,cast(lu_mem, oldn)*cast(lu_mem, sizeof(t)), \
|
||||
cast(lu_mem, n)*cast(lu_mem, sizeof(t)))))
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
195
lobject.c
Normal file
195
lobject.c
Normal file
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 1.85 2002/07/17 16:25:13 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
/* function to convert a string to a lua_Number */
|
||||
#ifndef lua_str2number
|
||||
#define lua_str2number(s,p) strtod((s), (p))
|
||||
#endif
|
||||
|
||||
|
||||
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
|
||||
|
||||
|
||||
int luaO_log2 (unsigned int x) {
|
||||
static const lu_byte log_8[255] = {
|
||||
0,
|
||||
1,1,
|
||||
2,2,2,2,
|
||||
3,3,3,3,3,3,3,3,
|
||||
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
|
||||
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
|
||||
};
|
||||
if (x >= 0x00010000) {
|
||||
if (x >= 0x01000000) return log_8[((x>>24) & 0xff) - 1]+24;
|
||||
else return log_8[((x>>16) & 0xff) - 1]+16;
|
||||
}
|
||||
else {
|
||||
if (x >= 0x00000100) return log_8[((x>>8) & 0xff) - 1]+8;
|
||||
else if (x) return log_8[(x & 0xff) - 1];
|
||||
return -1; /* special `log' for 0 */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaO_rawequalObj (const TObject *t1, const TObject *t2) {
|
||||
if (ttype(t1) != ttype(t2)) return 0;
|
||||
switch (ttype(t1)) {
|
||||
case LUA_TNUMBER:
|
||||
return nvalue(t1) == nvalue(t2);
|
||||
case LUA_TNIL:
|
||||
return 1;
|
||||
case LUA_TSTRING:
|
||||
return tsvalue(t1) == tsvalue(t2);
|
||||
case LUA_TBOOLEAN:
|
||||
return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
return pvalue(t1) == pvalue(t2);
|
||||
case LUA_TUSERDATA:
|
||||
return uvalue(t1) == uvalue(t2);
|
||||
case LUA_TTABLE:
|
||||
return hvalue(t1) == hvalue(t2);
|
||||
case LUA_TFUNCTION:
|
||||
return clvalue(t1) == clvalue(t2);
|
||||
}
|
||||
lua_assert(0);
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
int luaO_str2d (const char *s, lua_Number *result) {
|
||||
char *endptr;
|
||||
lua_Number res = lua_str2number(s, &endptr);
|
||||
if (endptr == s) return 0; /* no conversion */
|
||||
while (isspace((unsigned char)(*endptr))) endptr++;
|
||||
if (*endptr != '\0') return 0; /* invalid trailing characters? */
|
||||
*result = res;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void pushstr (lua_State *L, const char *str) {
|
||||
setsvalue(L->top, luaS_new(L, str));
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
/* this function handles only `%d', `%c', %f, and `%s' formats */
|
||||
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
int n = 1;
|
||||
pushstr(L, "");
|
||||
for (;;) {
|
||||
const char *e = strchr(fmt, '%');
|
||||
if (e == NULL) break;
|
||||
setsvalue(L->top, luaS_newlstr(L, fmt, e-fmt));
|
||||
incr_top(L);
|
||||
switch (*(e+1)) {
|
||||
case 's':
|
||||
pushstr(L, va_arg(argp, char *));
|
||||
break;
|
||||
case 'c': {
|
||||
char buff[2];
|
||||
buff[0] = cast(char, va_arg(argp, int));
|
||||
buff[1] = '\0';
|
||||
pushstr(L, buff);
|
||||
break;
|
||||
}
|
||||
case 'd':
|
||||
setnvalue(L->top, va_arg(argp, int));
|
||||
incr_top(L);
|
||||
break;
|
||||
case 'f':
|
||||
setnvalue(L->top, va_arg(argp, lua_Number));
|
||||
incr_top(L);
|
||||
break;
|
||||
case '%':
|
||||
pushstr(L, "%");
|
||||
break;
|
||||
default: lua_assert(0);
|
||||
}
|
||||
n += 2;
|
||||
fmt = e+2;
|
||||
}
|
||||
pushstr(L, fmt);
|
||||
luaV_concat(L, n+1, L->top - L->ci->base - 1);
|
||||
L->top -= n;
|
||||
return svalue(L->top - 1);
|
||||
}
|
||||
|
||||
|
||||
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
|
||||
const char *msg;
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
msg = luaO_pushvfstring(L, fmt, argp);
|
||||
va_end(argp);
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
void luaO_chunkid (char *out, const char *source, int bufflen) {
|
||||
if (*source == '=') {
|
||||
strncpy(out, source+1, bufflen); /* remove first char */
|
||||
out[bufflen-1] = '\0'; /* ensures null termination */
|
||||
}
|
||||
else { /* out = "source", or "...source" */
|
||||
if (*source == '@') {
|
||||
int l;
|
||||
source++; /* skip the `@' */
|
||||
bufflen -= sizeof(" `...' ");
|
||||
l = strlen(source);
|
||||
strcpy(out, "");
|
||||
if (l>bufflen) {
|
||||
source += (l-bufflen); /* get last part of file name */
|
||||
strcat(out, "...");
|
||||
}
|
||||
strcat(out, source);
|
||||
}
|
||||
else { /* out = [string "string"] */
|
||||
int len = strcspn(source, "\n"); /* stop at first newline */
|
||||
bufflen -= sizeof(" [string \"...\"] ");
|
||||
if (len > bufflen) len = bufflen;
|
||||
strcpy(out, "[string \"");
|
||||
if (source[len] != '\0') { /* must truncate? */
|
||||
strncat(out, source, len);
|
||||
strcat(out, "...");
|
||||
}
|
||||
else
|
||||
strcat(out, source);
|
||||
strcat(out, "\"]");
|
||||
}
|
||||
}
|
||||
}
|
||||
263
lobject.h
Normal file
263
lobject.h
Normal file
@@ -0,0 +1,263 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 1.141 2002/08/05 14:08:02 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lobject_h
|
||||
#define lobject_h
|
||||
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/* tags for values visible from Lua */
|
||||
#define NUM_TAGS LUA_TFUNCTION
|
||||
|
||||
|
||||
typedef union {
|
||||
void *p;
|
||||
union TString *ts;
|
||||
union Udata *u;
|
||||
union Closure *cl;
|
||||
struct Table *h;
|
||||
lua_Number n;
|
||||
int b;
|
||||
} Value;
|
||||
|
||||
|
||||
typedef struct lua_TObject {
|
||||
int tt;
|
||||
Value value;
|
||||
} TObject;
|
||||
|
||||
|
||||
/* Macros to test type */
|
||||
#define ttisnil(o) (ttype(o) == LUA_TNIL)
|
||||
#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
|
||||
#define ttisstring(o) (ttype(o) == LUA_TSTRING)
|
||||
#define ttistable(o) (ttype(o) == LUA_TTABLE)
|
||||
#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
|
||||
#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
|
||||
#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
|
||||
#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
|
||||
|
||||
/* Macros to access values */
|
||||
#define ttype(o) ((o)->tt)
|
||||
#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
|
||||
#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
|
||||
#define tsvalue(o) check_exp(ttisstring(o), (o)->value.ts)
|
||||
#define uvalue(o) check_exp(ttisuserdata(o), (o)->value.u)
|
||||
#define clvalue(o) check_exp(ttisfunction(o), (o)->value.cl)
|
||||
#define hvalue(o) check_exp(ttistable(o), (o)->value.h)
|
||||
#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
|
||||
|
||||
#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
|
||||
|
||||
/* Macros to set values */
|
||||
#define setnvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TNUMBER; i_o->value.n=(x); }
|
||||
|
||||
#define chgnvalue(obj,x) \
|
||||
check_exp(ttype(obj)==LUA_TNUMBER, (obj)->value.n=(x))
|
||||
|
||||
#define setpvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TLIGHTUSERDATA; i_o->value.p=(x); }
|
||||
|
||||
#define setbvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); }
|
||||
|
||||
#define setsvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TSTRING; i_o->value.ts=(x); }
|
||||
|
||||
#define setuvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; i_o->value.u=(x); }
|
||||
|
||||
#define setclvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; i_o->value.cl=(x); }
|
||||
|
||||
#define sethvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TTABLE; i_o->value.h=(x); }
|
||||
|
||||
#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
|
||||
|
||||
#define setobj(obj1,obj2) \
|
||||
{ const TObject *o2=(obj2); TObject *o1=(obj1); \
|
||||
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;
|
||||
int marked;
|
||||
union TString *nexthash; /* chain for hash table */
|
||||
} tsv;
|
||||
} TString;
|
||||
|
||||
|
||||
#define getstr(ts) cast(const char *, (ts) + 1)
|
||||
#define svalue(o) getstr(tsvalue(o))
|
||||
|
||||
|
||||
|
||||
typedef union Udata {
|
||||
union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
|
||||
struct {
|
||||
struct Table *metatable;
|
||||
union Udata *next; /* chain for list of all udata */
|
||||
size_t len; /* least 2 bits reserved for gc mark */
|
||||
} uv;
|
||||
} Udata;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Function Prototypes
|
||||
*/
|
||||
typedef struct Proto {
|
||||
TObject *k; /* constants used by the function */
|
||||
Instruction *code;
|
||||
struct Proto **p; /* functions defined inside the function */
|
||||
struct Proto *next;
|
||||
int *lineinfo; /* map from opcodes to source lines */
|
||||
struct LocVar *locvars; /* information about local variables */
|
||||
TString *source;
|
||||
int sizek; /* size of `k' */
|
||||
int sizecode;
|
||||
int sizep; /* size of `p' */
|
||||
int sizelocvars;
|
||||
int lineDefined;
|
||||
lu_byte nupvalues;
|
||||
lu_byte numparams;
|
||||
lu_byte is_vararg;
|
||||
lu_byte maxstacksize;
|
||||
lu_byte marked;
|
||||
} Proto;
|
||||
|
||||
|
||||
typedef struct LocVar {
|
||||
TString *varname;
|
||||
int startpc; /* first point where variable is active */
|
||||
int endpc; /* first point where variable is dead */
|
||||
} LocVar;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Upvalues
|
||||
*/
|
||||
|
||||
typedef struct UpVal {
|
||||
TObject *v; /* points to stack or to its own value */
|
||||
struct UpVal *next;
|
||||
TObject value; /* the value (when closed) */
|
||||
} UpVal;
|
||||
|
||||
|
||||
/*
|
||||
** Closures
|
||||
*/
|
||||
|
||||
typedef struct CClosure {
|
||||
lu_byte isC; /* 0 for Lua functions, 1 for C functions */
|
||||
lu_byte nupvalues;
|
||||
lu_byte marked;
|
||||
union Closure *next;
|
||||
lua_CFunction f;
|
||||
TObject upvalue[1];
|
||||
} CClosure;
|
||||
|
||||
|
||||
typedef struct LClosure {
|
||||
lu_byte isC;
|
||||
lu_byte nupvalues;
|
||||
lu_byte marked;
|
||||
union Closure *next; /* first four fields must be equal to CClosure!! */
|
||||
struct Proto *p;
|
||||
TObject g; /* global table for this closure */
|
||||
UpVal *upvals[1];
|
||||
} LClosure;
|
||||
|
||||
|
||||
typedef union Closure {
|
||||
CClosure c;
|
||||
LClosure l;
|
||||
} Closure;
|
||||
|
||||
|
||||
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
|
||||
#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
|
||||
|
||||
|
||||
/*
|
||||
** Tables
|
||||
*/
|
||||
|
||||
typedef struct Node {
|
||||
TObject i_key;
|
||||
TObject i_val;
|
||||
struct Node *next; /* for chaining */
|
||||
} Node;
|
||||
|
||||
|
||||
typedef struct Table {
|
||||
struct Table *metatable;
|
||||
TObject *array; /* array part */
|
||||
Node *node;
|
||||
Node *firstfree; /* this position is free; all positions after it are full */
|
||||
struct Table *next;
|
||||
struct Table *mark; /* marked tables (point to itself when not marked) */
|
||||
int sizearray; /* size of `array' array */
|
||||
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
|
||||
lu_byte lsizenode; /* log2 of size of `node' array */
|
||||
lu_byte mode;
|
||||
} Table;
|
||||
|
||||
/* bit masks for `mode' */
|
||||
#define WEAKKEY 1
|
||||
#define WEAKVALUE 2
|
||||
|
||||
|
||||
/*
|
||||
** `module' operation for hashing (size is always a power of 2)
|
||||
*/
|
||||
#define lmod(s,size) \
|
||||
check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))
|
||||
|
||||
|
||||
#define twoto(x) (1<<(x))
|
||||
#define sizenode(t) (twoto((t)->lsizenode))
|
||||
#define sizearray(t) ((t)->sizearray)
|
||||
|
||||
|
||||
|
||||
extern const TObject luaO_nilobject;
|
||||
|
||||
int luaO_log2 (unsigned int x);
|
||||
|
||||
|
||||
#define luaO_openspace(L,n,t) cast(t *, luaO_openspaceaux(L,(n)*sizeof(t)))
|
||||
void *luaO_openspaceaux (lua_State *L, size_t n);
|
||||
|
||||
int luaO_rawequalObj (const TObject *t1, const TObject *t2);
|
||||
int luaO_str2d (const char *s, lua_Number *result);
|
||||
|
||||
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp);
|
||||
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
|
||||
void luaO_chunkid (char *out, const char *source, int len);
|
||||
|
||||
|
||||
#endif
|
||||
104
lopcodes.c
Normal file
104
lopcodes.c
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
** $Id: lopcodes.c,v 1.19 2002/05/13 13:09:00 roberto Exp roberto $
|
||||
** extracted automatically from lopcodes.h by mkprint.lua
|
||||
** DO NOT EDIT
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
|
||||
|
||||
#ifdef LUA_OPNAMES
|
||||
|
||||
const char *const luaP_opnames[] = {
|
||||
"MOVE",
|
||||
"LOADK",
|
||||
"LOADBOOL",
|
||||
"LOADNIL",
|
||||
"GETUPVAL",
|
||||
"GETGLOBAL",
|
||||
"GETTABLE",
|
||||
"SETGLOBAL",
|
||||
"SETUPVAL",
|
||||
"SETTABLE",
|
||||
"NEWTABLE",
|
||||
"SELF",
|
||||
"ADD",
|
||||
"SUB",
|
||||
"MUL",
|
||||
"DIV",
|
||||
"POW",
|
||||
"UNM",
|
||||
"NOT",
|
||||
"CONCAT",
|
||||
"JMP",
|
||||
"EQ",
|
||||
"LT",
|
||||
"LE",
|
||||
"GT",
|
||||
"GE",
|
||||
"TEST",
|
||||
"CALL",
|
||||
"TAILCALL",
|
||||
"RETURN",
|
||||
"FORLOOP",
|
||||
"TFORLOOP",
|
||||
"TFORPREP",
|
||||
"SETLIST",
|
||||
"SETLISTO",
|
||||
"CLOSE",
|
||||
"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[NUM_OPCODES] = {
|
||||
/* T _ B C sA K mode opcode */
|
||||
opmode(0,0,1,0, 1,0,iABC) /* OP_MOVE */
|
||||
,opmode(0,0,0,0, 1,1,iABx) /* OP_LOADK */
|
||||
,opmode(0,0,0,0, 1,0,iABC) /* OP_LOADBOOL */
|
||||
,opmode(0,0,1,0, 1,0,iABC) /* OP_LOADNIL */
|
||||
,opmode(0,0,0,0, 1,0,iABC) /* OP_GETUPVAL */
|
||||
,opmode(0,0,0,0, 1,1,iABx) /* OP_GETGLOBAL */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_GETTABLE */
|
||||
,opmode(0,0,0,0, 0,1,iABx) /* OP_SETGLOBAL */
|
||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_SETUPVAL */
|
||||
,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,iAsBx) /* OP_JMP */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_EQ */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_LT */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_LE */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_GT */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_GE */
|
||||
,opmode(1,0,0,1, 1,0,iABC) /* OP_TEST */
|
||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_CALL */
|
||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_TAILCALL */
|
||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_RETURN */
|
||||
,opmode(0,0,0,0, 0,0,iAsBx) /* OP_FORLOOP */
|
||||
,opmode(1,0,0,0, 0,0,iABC) /* OP_TFORLOOP */
|
||||
,opmode(0,0,0,0, 0,0,iAsBx) /* OP_TFORPREP */
|
||||
,opmode(0,0,0,0, 0,0,iABx) /* OP_SETLIST */
|
||||
,opmode(0,0,0,0, 0,0,iABx) /* OP_SETLISTO */
|
||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_CLOSE */
|
||||
,opmode(0,0,0,0, 1,0,iABx) /* OP_CLOSURE */
|
||||
};
|
||||
|
||||
233
lopcodes.h
Normal file
233
lopcodes.h
Normal file
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.99 2002/06/12 14:51:31 roberto Exp $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#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)
|
||||
`Bx' : 18 bits (`B' and `C' together)
|
||||
`sBx' : signed Bx
|
||||
|
||||
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, iABx, iAsBx}; /* basic instruction format */
|
||||
|
||||
|
||||
/*
|
||||
** size and position of opcode arguments.
|
||||
*/
|
||||
#define SIZE_C 10
|
||||
#define SIZE_B 8
|
||||
#define SIZE_Bx (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_Bx 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_Bx < BITS_INT-1
|
||||
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
|
||||
#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */
|
||||
#else
|
||||
#define MAXARG_Bx MAX_INT
|
||||
#define MAXARG_sBx 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) (cast(OpCode, (i)&MASK1(SIZE_OP,0)))
|
||||
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | cast(Instruction, o)))
|
||||
|
||||
#define GETARG_A(i) (cast(int, (i)>>POS_A))
|
||||
#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
|
||||
((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))
|
||||
|
||||
#define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))
|
||||
#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
|
||||
((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))
|
||||
|
||||
#define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))
|
||||
#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
|
||||
((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))
|
||||
|
||||
#define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))
|
||||
#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \
|
||||
((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))
|
||||
|
||||
#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
|
||||
#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
|
||||
|
||||
|
||||
#define CREATE_ABC(o,a,b,c) (cast(Instruction, o) \
|
||||
| (cast(Instruction, a)<<POS_A) \
|
||||
| (cast(Instruction, b)<<POS_B) \
|
||||
| (cast(Instruction, c)<<POS_C))
|
||||
|
||||
#define CREATE_ABx(o,a,bc) (cast(Instruction, o) \
|
||||
| (cast(Instruction, a)<<POS_A) \
|
||||
| (cast(Instruction, bc)<<POS_Bx))
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** invalid registers that fits in 8 bits
|
||||
*/
|
||||
#define NO_REG MAXARG_A
|
||||
#define NO_REG1 (NO_REG+1)
|
||||
|
||||
|
||||
/*
|
||||
** 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 args description
|
||||
------------------------------------------------------------------------*/
|
||||
OP_MOVE,/* A B R(A) := R(B) */
|
||||
OP_LOADK,/* A Bx R(A) := Kst(Bx) */
|
||||
OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) PC++ */
|
||||
OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
|
||||
OP_GETUPVAL,/* A B R(A) := UpValue[B] */
|
||||
|
||||
OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */
|
||||
OP_GETTABLE,/* A B C R(A) := R(B)[R/K(C)] */
|
||||
|
||||
OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */
|
||||
OP_SETUPVAL,/* A B UpValue[B] := R(A) */
|
||||
OP_SETTABLE,/* A B C R(B)[R/K(C)] := R(A) */
|
||||
|
||||
OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
|
||||
|
||||
OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[R/K(C)] */
|
||||
|
||||
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) */
|
||||
|
||||
OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
|
||||
|
||||
OP_JMP,/* sBx PC += sBx */
|
||||
|
||||
OP_EQ,/* A B C if ((R(A) == R/K(C)) ~= B) then pc++ */
|
||||
OP_LT,/* A B C if ((R(A) < R/K(C)) ~= B) then pc++ */
|
||||
OP_LE,/* A B C if ((R(A) <= R/K(C)) ~= B) then pc++ */
|
||||
OP_GT,/* A B C if ((R(A) > R/K(C)) ~= B) then pc++ */
|
||||
OP_GE,/* A B C if ((R(A) >= R/K(C)) ~= B) then pc++ */
|
||||
|
||||
OP_TEST,/* A B C if (R(C) <=> B) then R(A) := R(C) else pc++ */
|
||||
|
||||
OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
|
||||
OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
|
||||
OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
|
||||
|
||||
OP_FORLOOP,/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then PC+= sBx */
|
||||
|
||||
OP_TFORLOOP,/* A C R(A+2), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
|
||||
if R(A+2) ~= nil then pc++ */
|
||||
OP_TFORPREP,/* A sBx if type(R(A)) == table then R(A+1):=R(A), R(A):=next;
|
||||
PC += sBx */
|
||||
|
||||
OP_SETLIST,/* A Bx R(A)[Bx-Bx%FPF+i] := R(A+i), 1 <= i <= Bx%FPF+1 */
|
||||
OP_SETLISTO,/* A Bx */
|
||||
|
||||
OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
|
||||
OP_CLOSURE/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */
|
||||
} OpCode;
|
||||
|
||||
|
||||
#define NUM_OPCODES (cast(int, OP_CLOSURE+1))
|
||||
|
||||
|
||||
|
||||
/*===========================================================================
|
||||
Notes:
|
||||
(1) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,
|
||||
and can be 0: OP_CALL then sets `top' to last_result+1, so
|
||||
next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.
|
||||
|
||||
(2) In OP_RETURN, if (B == 0) then return up to `top'
|
||||
|
||||
(3) For comparisons, B specifies what conditions the test should accept.
|
||||
|
||||
(4) All `skips' (pc++) assume that next instruction is a jump
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
/*
|
||||
** masks for instruction properties
|
||||
*/
|
||||
enum OpModeMask {
|
||||
OpModeBreg = 2, /* B is a register */
|
||||
OpModeCreg, /* C is a register/constant */
|
||||
OpModesetA, /* instruction set register A */
|
||||
OpModeK, /* Bx is a constant */
|
||||
OpModeT /* operator is a test */
|
||||
};
|
||||
|
||||
extern const lu_byte luaP_opmodes[NUM_OPCODES];
|
||||
|
||||
#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))
|
||||
#define testOpMode(m, b) (luaP_opmodes[m] & (1 << (b)))
|
||||
|
||||
|
||||
#ifdef LUA_OPNAMES
|
||||
extern const char *const luaP_opnames[]; /* opcode names */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* number of list items to accumulate before a SETLIST instruction */
|
||||
/* (must be a power of 2) */
|
||||
#define LFIELDS_PER_FLUSH 32
|
||||
|
||||
|
||||
#endif
|
||||
71
lparser.h
Normal file
71
lparser.h
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.43 2002/05/10 19:22:11 roberto Exp roberto $
|
||||
** Lua Parser
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lparser_h
|
||||
#define lparser_h
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lobject.h"
|
||||
#include "ltable.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
/*
|
||||
** Expression descriptor
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
VVOID, /* no value */
|
||||
VNIL,
|
||||
VTRUE,
|
||||
VFALSE,
|
||||
VK, /* info = index of constant in `k' */
|
||||
VLOCAL, /* info = local register */
|
||||
VUPVAL, /* info = index of upvalue in `upvalues' */
|
||||
VGLOBAL, /* info = index of table; aux = index of global name in `k' */
|
||||
VINDEXED, /* info = table register; aux = index register (or `k') */
|
||||
VJMP, /* info = instruction pc */
|
||||
VRELOCABLE, /* info = instruction pc */
|
||||
VNONRELOC, /* info = result register */
|
||||
VCALL /* info = result register */
|
||||
} expkind;
|
||||
|
||||
typedef struct expdesc {
|
||||
expkind k;
|
||||
int info, aux;
|
||||
int t; /* patch list of `exit when true' */
|
||||
int f; /* patch list of `exit when false' */
|
||||
} expdesc;
|
||||
|
||||
|
||||
struct BlockCnt; /* defined in lparser.c */
|
||||
|
||||
|
||||
/* state needed to generate code for a given function */
|
||||
typedef struct FuncState {
|
||||
Proto *f; /* current function header */
|
||||
Table *h; /* table to find (and reuse) elements in `k' */
|
||||
struct FuncState *prev; /* enclosing function */
|
||||
struct LexState *ls; /* lexical state */
|
||||
struct lua_State *L; /* copy of the Lua state */
|
||||
struct BlockCnt *bl; /* chain of current blocks */
|
||||
int pc; /* next position to code (equivalent to `ncode') */
|
||||
int lasttarget; /* `pc' of last `jump target' */
|
||||
int jpc; /* list of pending jumps to `pc' */
|
||||
int freereg; /* first free register */
|
||||
int nk; /* number of elements in `k' */
|
||||
int np; /* number of elements in `p' */
|
||||
int nlocvars; /* number of elements in `locvars' */
|
||||
int nactvar; /* number of active local variables */
|
||||
expdesc upvalues[MAXUPVALUES]; /* upvalues */
|
||||
int actvar[MAXVARS]; /* declared-variable stack */
|
||||
} FuncState;
|
||||
|
||||
|
||||
Proto *luaY_parser (lua_State *L, ZIO *z);
|
||||
|
||||
|
||||
#endif
|
||||
192
lstate.c
Normal file
192
lstate.c
Normal file
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 1.102 2002/08/06 15:32:22 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
|
||||
|
||||
static void close_state (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** you can change this function through the official API
|
||||
** call `lua_setpanicf'
|
||||
*/
|
||||
static int default_panic (lua_State *L) {
|
||||
UNUSED(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void stack_init (lua_State *L, lua_State *OL) {
|
||||
L->stack = luaM_newvector(OL, BASIC_STACK_SIZE + EXTRA_STACK, TObject);
|
||||
L->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
|
||||
L->top = L->stack;
|
||||
L->stack_last = L->stack+(L->stacksize - EXTRA_STACK)-1;
|
||||
L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo);
|
||||
L->ci = L->base_ci;
|
||||
L->ci->state = CI_C; /* not a Lua function */
|
||||
setnilvalue(L->top++); /* `function' entry for this `ci' */
|
||||
L->ci->base = L->top;
|
||||
L->ci->top = L->top + LUA_MINSTACK;
|
||||
L->size_ci = BASIC_CI_SIZE;
|
||||
L->end_ci = L->base_ci + L->size_ci;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** open parts that may cause memory-allocation errors
|
||||
*/
|
||||
static void f_luaopen (lua_State *L, void *ud) {
|
||||
UNUSED(ud);
|
||||
/* create a new global state */
|
||||
L->l_G = luaM_new(L, global_State);
|
||||
G(L)->GCthreshold = 0; /* mark it as unfinished 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)->panic = &default_panic;
|
||||
G(L)->rootproto = NULL;
|
||||
G(L)->rootcl = NULL;
|
||||
G(L)->roottable = NULL;
|
||||
G(L)->rootupval = NULL;
|
||||
G(L)->rootudata = NULL;
|
||||
G(L)->tmudata = NULL;
|
||||
setnilvalue(key(G(L)->dummynode));
|
||||
setnilvalue(val(G(L)->dummynode));
|
||||
G(L)->dummynode->next = NULL;
|
||||
G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
|
||||
stack_init(L, L); /* init stack */
|
||||
/* create default meta table with a dummy table, and then close the loop */
|
||||
sethvalue(defaultmeta(L), NULL);
|
||||
sethvalue(defaultmeta(L), luaH_new(L, 0, 4));
|
||||
hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L));
|
||||
sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */
|
||||
sethvalue(registry(L), luaH_new(L, 0, 0)); /* registry */
|
||||
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
|
||||
luaT_init(L);
|
||||
luaX_init(L);
|
||||
luaS_fix(luaS_newliteral(L, MEMERRMSG));
|
||||
G(L)->GCthreshold = 4*G(L)->nblocks;
|
||||
}
|
||||
|
||||
|
||||
static void preinit_state (lua_State *L) {
|
||||
L->stack = NULL;
|
||||
L->stacksize = 0;
|
||||
L->errorJmp = NULL;
|
||||
L->hook = NULL;
|
||||
L->hookmask = 0;
|
||||
setallowhook(L, 1);
|
||||
resethookcount(L);
|
||||
L->openupval = NULL;
|
||||
L->size_ci = 0;
|
||||
L->base_ci = L->ci = NULL;
|
||||
L->errfunc = 0;
|
||||
setnilvalue(defaultmeta(L));
|
||||
setnilvalue(gt(L));
|
||||
setnilvalue(registry(L));
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_State *lua_newthread (lua_State *OL) {
|
||||
lua_State *L;
|
||||
lua_lock(OL);
|
||||
L = luaM_new(OL, lua_State);
|
||||
preinit_state(L);
|
||||
L->l_G = OL->l_G;
|
||||
OL->next->previous = L; /* insert L into linked list */
|
||||
L->next = OL->next;
|
||||
OL->next = L;
|
||||
L->previous = OL;
|
||||
stack_init(L, OL); /* init stack */
|
||||
setobj(defaultmeta(L), defaultmeta(OL)); /* share default meta table */
|
||||
setobj(gt(L), gt(OL)); /* share table of globals */
|
||||
setobj(registry(L), registry(OL)); /* share registry */
|
||||
lua_unlock(OL);
|
||||
lua_userstateopen(L);
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_State *lua_open (void) {
|
||||
lua_State *L;
|
||||
L = luaM_new(NULL, lua_State);
|
||||
if (L) { /* allocation OK? */
|
||||
preinit_state(L);
|
||||
L->l_G = NULL;
|
||||
L->next = L->previous = L;
|
||||
if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
|
||||
/* memory allocation error: free partial state */
|
||||
close_state(L);
|
||||
L = NULL;
|
||||
}
|
||||
}
|
||||
lua_userstateopen(L);
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
void luaE_closethread (lua_State *OL, lua_State *L) {
|
||||
luaF_close(L, L->stack); /* close all upvalues for this thread */
|
||||
lua_assert(L->openupval == NULL);
|
||||
L->previous->next = L->next;
|
||||
L->next->previous = L->previous;
|
||||
luaM_freearray(OL, L->base_ci, L->size_ci, CallInfo);
|
||||
luaM_freearray(OL, L->stack, L->stacksize, TObject);
|
||||
luaM_freelem(OL, L);
|
||||
}
|
||||
|
||||
|
||||
static void close_state (lua_State *L) {
|
||||
luaF_close(L, L->stack); /* close all upvalues for this thread */
|
||||
if (G(L)) { /* close global state */
|
||||
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)->rootupval == NULL);
|
||||
lua_assert(G(L)->roottable == NULL);
|
||||
luaS_freeall(L);
|
||||
luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
|
||||
luaM_freelem(NULL, L->l_G);
|
||||
}
|
||||
luaE_closethread(NULL, L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_closethread (lua_State *L, lua_State *thread) {
|
||||
lua_lock(L);
|
||||
if (L == thread) luaG_runerror(L, "cannot close only thread of a state");
|
||||
luaE_closethread(L, thread);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_close (lua_State *L) {
|
||||
lua_lock(L);
|
||||
luaC_callallgcTM(L); /* call GC tag methods for all udata */
|
||||
lua_assert(G(L)->tmudata == NULL);
|
||||
while (L->next != L) /* then, close all other threads */
|
||||
luaE_closethread(L, L->next);
|
||||
close_state(L);
|
||||
}
|
||||
|
||||
172
lstate.h
Normal file
172
lstate.h
Normal file
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 1.93 2002/08/07 19:22:39 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lstate_h
|
||||
#define lstate_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lobject.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
/*
|
||||
** 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
|
||||
|
||||
#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
|
||||
|
||||
#ifndef lua_userstateopen
|
||||
#define lua_userstateopen(l)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
struct lua_longjmp; /* defined in ldo.c */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** array of `global' objects
|
||||
*/
|
||||
|
||||
#define NUMGLOBS 3
|
||||
|
||||
/* default meta table (both for tables and udata) */
|
||||
#define defaultmeta(L) (L->globs)
|
||||
|
||||
/* table of globals */
|
||||
#define gt(L) (L->globs + 1)
|
||||
|
||||
/* registry */
|
||||
#define registry(L) (L->globs + 2)
|
||||
|
||||
|
||||
/* extra stack space to handle TM calls and some other extras */
|
||||
#define EXTRA_STACK 5
|
||||
|
||||
|
||||
#define BASIC_CI_SIZE 8
|
||||
|
||||
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
|
||||
|
||||
|
||||
|
||||
typedef struct stringtable {
|
||||
TString **hash;
|
||||
ls_nstr nuse; /* number of elements */
|
||||
int size;
|
||||
} stringtable;
|
||||
|
||||
|
||||
/*
|
||||
** informations about a call
|
||||
*/
|
||||
typedef struct CallInfo {
|
||||
StkId base; /* base for called function */
|
||||
StkId top; /* top for this function */
|
||||
int state; /* bit fields; see below */
|
||||
union {
|
||||
struct { /* for Lua functions */
|
||||
const Instruction *savedpc;
|
||||
const Instruction **pc; /* points to `pc' variable in `luaV_execute' */
|
||||
StkId *pb; /* points to `base' variable in `luaV_execute' */
|
||||
} l;
|
||||
struct { /* for C functions */
|
||||
int yield_results;
|
||||
} c;
|
||||
} u;
|
||||
} CallInfo;
|
||||
|
||||
|
||||
/*
|
||||
** bit fields for `CallInfo.state'
|
||||
*/
|
||||
#define CI_C 1 /* 1 if function is a C function */
|
||||
/* 1 if (Lua) function has an active `luaV_execute' running it */
|
||||
#define CI_HASFRAME 2
|
||||
/* 1 if Lua function is calling another Lua function (and therefore its
|
||||
`pc' is being used by the other, and therefore CI_SAVEDPC is 1 too) */
|
||||
#define CI_CALLING 4
|
||||
#define CI_SAVEDPC 8 /* 1 if `savedpc' is updated */
|
||||
|
||||
|
||||
#define ci_func(ci) (clvalue((ci)->base - 1))
|
||||
|
||||
|
||||
/*
|
||||
** `global state', shared by all threads of this state
|
||||
*/
|
||||
typedef struct global_State {
|
||||
stringtable strt; /* hash table for strings */
|
||||
Proto *rootproto; /* list of all prototypes */
|
||||
Closure *rootcl; /* list of all closures */
|
||||
Table *roottable; /* list of all tables */
|
||||
UpVal *rootupval; /* list of closed up values */
|
||||
Udata *rootudata; /* list of all userdata */
|
||||
Udata *tmudata; /* list of userdata to be GC */
|
||||
void *Mbuffer; /* global buffer */
|
||||
size_t Mbuffsize; /* size of Mbuffer */
|
||||
lu_mem GCthreshold;
|
||||
lu_mem nblocks; /* number of `bytes' currently allocated */
|
||||
lua_CFunction panic; /* to be called in unprotected errors */
|
||||
Node dummynode[1]; /* common node array for all empty tables */
|
||||
TString *tmname[TM_N]; /* array with tag-method names */
|
||||
} global_State;
|
||||
|
||||
|
||||
/*
|
||||
** `per thread' state
|
||||
*/
|
||||
struct lua_State {
|
||||
LUA_USERSTATE
|
||||
StkId top; /* first free slot in the stack */
|
||||
global_State *l_G;
|
||||
CallInfo *ci; /* call info for current function */
|
||||
StkId stack_last; /* last free slot in the stack */
|
||||
StkId stack; /* stack base */
|
||||
int stacksize;
|
||||
CallInfo *end_ci; /* points after end of ci array*/
|
||||
CallInfo *base_ci; /* array of CallInfo's */
|
||||
int size_ci; /* size of array `base_ci' */
|
||||
unsigned long hookmask;
|
||||
ls_count hookcount;
|
||||
lua_Hook hook;
|
||||
UpVal *openupval; /* list of open upvalues in this stack */
|
||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
||||
lua_State *next; /* circular double linked list of states */
|
||||
lua_State *previous;
|
||||
TObject globs[NUMGLOBS]; /* registry, table of globals, etc. */
|
||||
};
|
||||
|
||||
|
||||
#define G(L) (L->l_G)
|
||||
|
||||
|
||||
void luaE_closethread (lua_State *OL, lua_State *L);
|
||||
|
||||
#endif
|
||||
|
||||
97
lstring.c
Normal file
97
lstring.c
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 1.73 2002/03/20 18:37:13 roberto Exp roberto $
|
||||
** String table (keeps all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
|
||||
|
||||
|
||||
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 *);
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
/* rehash */
|
||||
for (i=0; i<tb->size; i++) {
|
||||
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(cast(int, h%newsize) == lmod(h, newsize));
|
||||
p->tsv.nexthash = newhash[h1]; /* chain it in new position */
|
||||
newhash[h1] = p;
|
||||
p = next;
|
||||
}
|
||||
}
|
||||
luaM_freearray(L, tb->hash, tb->size, TString *);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) {
|
||||
TString *ts = cast(TString *, luaM_malloc(L, sizestring(l)));
|
||||
stringtable *tb;
|
||||
ts->tsv.nexthash = NULL;
|
||||
ts->tsv.len = l;
|
||||
ts->tsv.hash = h;
|
||||
ts->tsv.marked = 0;
|
||||
memcpy(ts+1, str, l*sizeof(char));
|
||||
((char *)(ts+1))[l] = '\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 > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2)
|
||||
luaS_resize(L, tb->size*2); /* too crowded */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||
TString *ts;
|
||||
lu_hash h = (lu_hash)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)+(unsigned char)(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;
|
||||
}
|
||||
return newlstr(L, str, l, h); /* not found */
|
||||
}
|
||||
|
||||
|
||||
Udata *luaS_newudata (lua_State *L, size_t s) {
|
||||
Udata *u;
|
||||
s = (s+3) & (~(size_t)3); /* make sure size is multiple of 4 */
|
||||
u = cast(Udata *, luaM_malloc(L, sizeudata(s)));
|
||||
u->uv.len = s;
|
||||
u->uv.metatable = hvalue(defaultmeta(L));
|
||||
/* chain it on udata list */
|
||||
u->uv.next = G(L)->rootudata;
|
||||
G(L)->rootudata = u;
|
||||
return u;
|
||||
}
|
||||
|
||||
41
lstring.h
Normal file
41
lstring.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.35 2001/11/28 20:13:13 roberto Exp roberto $
|
||||
** String table (keep all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lstring_h
|
||||
#define lstring_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** 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) (cast(lu_mem, sizeof(union TString))+ \
|
||||
(cast(lu_mem, l)+1)*sizeof(char))
|
||||
|
||||
#define sizeudata(l) (cast(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, "" s, \
|
||||
(sizeof(s)/sizeof(char))-1))
|
||||
|
||||
#define luaS_fix(s) ((s)->tsv.marked = FIXMARK)
|
||||
|
||||
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 char *str, size_t l);
|
||||
|
||||
|
||||
#endif
|
||||
712
lstrlib.c
Normal file
712
lstrlib.c
Normal file
@@ -0,0 +1,712 @@
|
||||
/*
|
||||
** $Id: lstrlib.c,v 1.86 2002/06/25 19:16:44 roberto Exp roberto $
|
||||
** Standard library for string operations and pattern-matching
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
/* macro to `unsign' a character */
|
||||
#ifndef uchar
|
||||
#define uchar(c) ((unsigned char)(c))
|
||||
#endif
|
||||
|
||||
|
||||
typedef long sint32; /* a signed version for size_t */
|
||||
|
||||
|
||||
static int str_len (lua_State *L) {
|
||||
size_t l;
|
||||
luaL_check_lstr(L, 1, &l);
|
||||
lua_pushnumber(L, l);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static sint32 posrelat (sint32 pos, size_t len) {
|
||||
/* relative string position: negative means back from end */
|
||||
return (pos>=0) ? pos : (sint32)len+pos+1;
|
||||
}
|
||||
|
||||
|
||||
static int str_sub (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
sint32 start = posrelat(luaL_check_long(L, 2), l);
|
||||
sint32 end = posrelat(luaL_opt_long(L, 3, -1), l);
|
||||
if (start < 1) start = 1;
|
||||
if (end > (sint32)l) end = l;
|
||||
if (start <= end)
|
||||
lua_pushlstring(L, s+start-1, end-start+1);
|
||||
else lua_pushliteral(L, "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int str_lower (lua_State *L) {
|
||||
size_t l;
|
||||
size_t i;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=0; i<l; i++)
|
||||
luaL_putchar(&b, tolower(uchar(s[i])));
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int str_upper (lua_State *L) {
|
||||
size_t l;
|
||||
size_t i;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=0; i<l; i++)
|
||||
luaL_putchar(&b, toupper(uchar(s[i])));
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int str_rep (lua_State *L) {
|
||||
size_t l;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
int n = luaL_check_int(L, 2);
|
||||
luaL_buffinit(L, &b);
|
||||
while (n-- > 0)
|
||||
luaL_addlstring(&b, s, l);
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int str_byte (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l);
|
||||
luaL_arg_check(L, 0 < pos && (size_t)(pos) <= l, 2, "out of range");
|
||||
lua_pushnumber(L, uchar(s[pos-1]));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int str_char (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
int i;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=1; i<=n; i++) {
|
||||
int c = luaL_check_int(L, i);
|
||||
luaL_arg_check(L, uchar(c) == c, i, "invalid value");
|
||||
luaL_putchar(&b, uchar(c));
|
||||
}
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** PATTERN MATCHING
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#ifndef MAX_CAPTURES
|
||||
#define MAX_CAPTURES 32 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
|
||||
#define CAP_UNFINISHED (-1)
|
||||
#define CAP_POSITION (-2)
|
||||
|
||||
typedef struct MatchState {
|
||||
const char *src_init; /* init of source string */
|
||||
const char *src_end; /* end (`\0') of source string */
|
||||
lua_State *L;
|
||||
int level; /* total number of captures (finished or unfinished) */
|
||||
struct {
|
||||
const char *init;
|
||||
sint32 len;
|
||||
} capture[MAX_CAPTURES];
|
||||
} MatchState;
|
||||
|
||||
|
||||
#define ESC '%'
|
||||
#define SPECIALS "^$*+?.([%-"
|
||||
|
||||
|
||||
static int check_capture (MatchState *ms, int l) {
|
||||
l -= '1';
|
||||
if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
|
||||
return luaL_error(ms->L, "invalid capture index");
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
static int capture_to_close (MatchState *ms) {
|
||||
int level = ms->level;
|
||||
for (level--; level>=0; level--)
|
||||
if (ms->capture[level].len == CAP_UNFINISHED) return level;
|
||||
return luaL_error(ms->L, "invalid pattern capture");
|
||||
}
|
||||
|
||||
|
||||
static const char *luaI_classend (MatchState *ms, const char *p) {
|
||||
switch (*p++) {
|
||||
case ESC:
|
||||
if (*p == '\0')
|
||||
luaL_error(ms->L, "malformed pattern (ends with `%')");
|
||||
return p+1;
|
||||
case '[':
|
||||
if (*p == '^') p++;
|
||||
do { /* look for a `]' */
|
||||
if (*p == '\0')
|
||||
luaL_error(ms->L, "malformed pattern (missing `]')");
|
||||
if (*(p++) == ESC && *p != '\0')
|
||||
p++; /* skip escapes (e.g. `%]') */
|
||||
} while (*p != ']');
|
||||
return p+1;
|
||||
default:
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int match_class (int c, int cl) {
|
||||
int res;
|
||||
switch (tolower(cl)) {
|
||||
case 'a' : res = isalpha(c); break;
|
||||
case 'c' : res = iscntrl(c); break;
|
||||
case 'd' : res = isdigit(c); break;
|
||||
case 'l' : res = islower(c); break;
|
||||
case 'p' : res = ispunct(c); break;
|
||||
case 's' : res = isspace(c); break;
|
||||
case 'u' : res = isupper(c); break;
|
||||
case 'w' : res = isalnum(c); break;
|
||||
case 'x' : res = isxdigit(c); break;
|
||||
case 'z' : res = (c == 0); break;
|
||||
default: return (cl == c);
|
||||
}
|
||||
return (islower(cl) ? res : !res);
|
||||
}
|
||||
|
||||
|
||||
static int matchbracketclass (int c, const char *p, const char *ec) {
|
||||
int sig = 1;
|
||||
if (*(p+1) == '^') {
|
||||
sig = 0;
|
||||
p++; /* skip the `^' */
|
||||
}
|
||||
while (++p < ec) {
|
||||
if (*p == ESC) {
|
||||
p++;
|
||||
if (match_class(c, *p))
|
||||
return sig;
|
||||
}
|
||||
else if ((*(p+1) == '-') && (p+2 < ec)) {
|
||||
p+=2;
|
||||
if (uchar(*(p-2)) <= c && c <= uchar(*p))
|
||||
return sig;
|
||||
}
|
||||
else if (uchar(*p) == c) return sig;
|
||||
}
|
||||
return !sig;
|
||||
}
|
||||
|
||||
|
||||
static int luaI_singlematch (int c, const char *p, const char *ep) {
|
||||
switch (*p) {
|
||||
case '.': /* matches any char */
|
||||
return 1;
|
||||
case ESC:
|
||||
return match_class(c, *(p+1));
|
||||
case '[':
|
||||
return matchbracketclass(c, p, ep-1);
|
||||
default:
|
||||
return (uchar(*p) == c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *match (MatchState *ms, const char *s, const char *p);
|
||||
|
||||
|
||||
static const char *matchbalance (MatchState *ms, const char *s,
|
||||
const char *p) {
|
||||
if (*p == 0 || *(p+1) == 0)
|
||||
luaL_error(ms->L, "unbalanced pattern");
|
||||
if (*s != *p) return NULL;
|
||||
else {
|
||||
int b = *p;
|
||||
int e = *(p+1);
|
||||
int cont = 1;
|
||||
while (++s < ms->src_end) {
|
||||
if (*s == e) {
|
||||
if (--cont == 0) return s+1;
|
||||
}
|
||||
else if (*s == b) cont++;
|
||||
}
|
||||
}
|
||||
return NULL; /* string ends out of balance */
|
||||
}
|
||||
|
||||
|
||||
static const char *max_expand (MatchState *ms, const char *s,
|
||||
const char *p, const char *ep) {
|
||||
sint32 i = 0; /* counts maximum expand for item */
|
||||
while ((s+i)<ms->src_end && luaI_singlematch(uchar(*(s+i)), p, ep))
|
||||
i++;
|
||||
/* keeps trying to match with the maximum repetitions */
|
||||
while (i>=0) {
|
||||
const char *res = match(ms, (s+i), ep+1);
|
||||
if (res) return res;
|
||||
i--; /* else didn't match; reduce 1 repetition to try again */
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char *min_expand (MatchState *ms, const char *s,
|
||||
const char *p, const char *ep) {
|
||||
for (;;) {
|
||||
const char *res = match(ms, s, ep+1);
|
||||
if (res != NULL)
|
||||
return res;
|
||||
else if (s<ms->src_end && luaI_singlematch(uchar(*s), p, ep))
|
||||
s++; /* try with one more repetition */
|
||||
else return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *start_capture (MatchState *ms, const char *s,
|
||||
const char *p, int what) {
|
||||
const char *res;
|
||||
int level = ms->level;
|
||||
if (level >= MAX_CAPTURES) luaL_error(ms->L, "too many captures");
|
||||
ms->capture[level].init = s;
|
||||
ms->capture[level].len = what;
|
||||
ms->level = level+1;
|
||||
if ((res=match(ms, s, p)) == NULL) /* match failed? */
|
||||
ms->level--; /* undo capture */
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static const char *end_capture (MatchState *ms, const char *s,
|
||||
const char *p) {
|
||||
int l = capture_to_close(ms);
|
||||
const char *res;
|
||||
ms->capture[l].len = s - ms->capture[l].init; /* close capture */
|
||||
if ((res = match(ms, s, p)) == NULL) /* match failed? */
|
||||
ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static const char *match_capture (MatchState *ms, const char *s, int l) {
|
||||
size_t len;
|
||||
l = check_capture(ms, l);
|
||||
len = ms->capture[l].len;
|
||||
if ((size_t)(ms->src_end-s) >= len &&
|
||||
memcmp(ms->capture[l].init, s, len) == 0)
|
||||
return s+len;
|
||||
else return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char *match (MatchState *ms, const char *s, const char *p) {
|
||||
init: /* using goto's to optimize tail recursion */
|
||||
switch (*p) {
|
||||
case '(': /* start capture */
|
||||
if (*(p+1) == ')') /* position capture? */
|
||||
return start_capture(ms, s, p+2, CAP_POSITION);
|
||||
else
|
||||
return start_capture(ms, s, p+1, CAP_UNFINISHED);
|
||||
case ')': /* end capture */
|
||||
return end_capture(ms, s, p+1);
|
||||
case ESC: /* may be %[0-9] or %b */
|
||||
if (isdigit(uchar(*(p+1)))) { /* capture? */
|
||||
s = match_capture(ms, s, *(p+1));
|
||||
if (s == NULL) return NULL;
|
||||
p+=2; goto init; /* else return match(ms, s, p+2) */
|
||||
}
|
||||
else if (*(p+1) == 'b') { /* balanced string? */
|
||||
s = matchbalance(ms, s, p+2);
|
||||
if (s == NULL) return NULL;
|
||||
p+=4; goto init; /* else return match(ms, s, p+4); */
|
||||
}
|
||||
else goto dflt; /* case default */
|
||||
case '\0': /* end of pattern */
|
||||
return s; /* match succeeded */
|
||||
case '$':
|
||||
if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
|
||||
return (s == ms->src_end) ? s : NULL; /* check end of string */
|
||||
else goto dflt;
|
||||
default: dflt: { /* it is a pattern item */
|
||||
const char *ep = luaI_classend(ms, p); /* points to what is next */
|
||||
int m = s<ms->src_end && luaI_singlematch(uchar(*s), p, ep);
|
||||
switch (*ep) {
|
||||
case '?': { /* optional */
|
||||
const char *res;
|
||||
if (m && ((res=match(ms, s+1, ep+1)) != NULL))
|
||||
return res;
|
||||
p=ep+1; goto init; /* else return match(ms, s, ep+1); */
|
||||
}
|
||||
case '*': /* 0 or more repetitions */
|
||||
return max_expand(ms, s, p, ep);
|
||||
case '+': /* 1 or more repetitions */
|
||||
return (m ? max_expand(ms, s+1, p, ep) : NULL);
|
||||
case '-': /* 0 or more repetitions (minimum) */
|
||||
return min_expand(ms, s, p, ep);
|
||||
default:
|
||||
if (!m) return NULL;
|
||||
s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static const char *lmemfind (const char *s1, size_t l1,
|
||||
const char *s2, size_t l2) {
|
||||
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
|
||||
else {
|
||||
const char *init; /* to search for a `*s2' inside `s1' */
|
||||
l2--; /* 1st char will be checked by `memchr' */
|
||||
l1 = l1-l2; /* `s2' cannot be found after that */
|
||||
while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
|
||||
init++; /* 1st char is already checked */
|
||||
if (memcmp(init, s2+1, l2) == 0)
|
||||
return init-1;
|
||||
else { /* correct `l1' and `s1' to try again */
|
||||
l1 -= init-s1;
|
||||
s1 = init;
|
||||
}
|
||||
}
|
||||
return NULL; /* not found */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void push_onecapture (MatchState *ms, int i) {
|
||||
int l = ms->capture[i].len;
|
||||
if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
|
||||
if (l == CAP_POSITION)
|
||||
lua_pushnumber(ms->L, ms->capture[i].init - ms->src_init + 1);
|
||||
else
|
||||
lua_pushlstring(ms->L, ms->capture[i].init, l);
|
||||
}
|
||||
|
||||
|
||||
static int push_captures (MatchState *ms, const char *s, const char *e) {
|
||||
int i;
|
||||
luaL_check_stack(ms->L, ms->level, "too many captures");
|
||||
if (ms->level == 0 && s) { /* no explicit captures? */
|
||||
lua_pushlstring(ms->L, s, e-s); /* return whole match */
|
||||
return 1;
|
||||
}
|
||||
else { /* return all captures */
|
||||
for (i=0; i<ms->level; i++)
|
||||
push_onecapture(ms, i);
|
||||
return ms->level; /* number of strings pushed */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int str_find (lua_State *L) {
|
||||
size_t l1, l2;
|
||||
const char *s = luaL_check_lstr(L, 1, &l1);
|
||||
const char *p = luaL_check_lstr(L, 2, &l2);
|
||||
sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
|
||||
luaL_arg_check(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range");
|
||||
if (lua_toboolean(L, 4) || /* explicit request? */
|
||||
strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
|
||||
/* do a plain search */
|
||||
const char *s2 = lmemfind(s+init, l1-init, p, l2);
|
||||
if (s2) {
|
||||
lua_pushnumber(L, s2-s+1);
|
||||
lua_pushnumber(L, s2-s+l2);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
else {
|
||||
MatchState ms;
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
const char *s1=s+init;
|
||||
ms.L = L;
|
||||
ms.src_init = s;
|
||||
ms.src_end = s+l1;
|
||||
do {
|
||||
const char *res;
|
||||
ms.level = 0;
|
||||
if ((res=match(&ms, s1, p)) != NULL) {
|
||||
lua_pushnumber(L, s1-s+1); /* start */
|
||||
lua_pushnumber(L, res-s); /* end */
|
||||
return push_captures(&ms, NULL, 0) + 2;
|
||||
}
|
||||
} while (s1++<ms.src_end && !anchor);
|
||||
}
|
||||
lua_pushnil(L); /* not found */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int gfind_aux (lua_State *L) {
|
||||
MatchState ms;
|
||||
const char *s = lua_tostring(L, lua_upvalueindex(1));
|
||||
size_t ls = lua_strlen(L, lua_upvalueindex(1));
|
||||
const char *p = lua_tostring(L, lua_upvalueindex(2));
|
||||
const char *src;
|
||||
ms.L = L;
|
||||
ms.src_init = s;
|
||||
ms.src_end = s+ls;
|
||||
for (src = s + (size_t)lua_tonumber(L, lua_upvalueindex(3));
|
||||
src <= ms.src_end;
|
||||
src++) {
|
||||
const char *e;
|
||||
ms.level = 0;
|
||||
if ((e = match(&ms, src, p)) != NULL) {
|
||||
int newstart = e-s;
|
||||
if (e == src) newstart++; /* empty match? go at least one position */
|
||||
lua_pushnumber(L, newstart);
|
||||
lua_replace(L, lua_upvalueindex(3));
|
||||
return push_captures(&ms, src, e);
|
||||
}
|
||||
}
|
||||
return 0; /* not found */
|
||||
}
|
||||
|
||||
|
||||
static int gfind (lua_State *L) {
|
||||
luaL_check_string(L, 1);
|
||||
luaL_check_string(L, 2);
|
||||
lua_settop(L, 2);
|
||||
lua_pushnumber(L, 0);
|
||||
lua_pushcclosure(L, gfind_aux, 3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void add_s (MatchState *ms, luaL_Buffer *b,
|
||||
const char *s, const char *e) {
|
||||
lua_State *L = ms->L;
|
||||
if (lua_isstring(L, 3)) {
|
||||
const char *news = lua_tostring(L, 3);
|
||||
size_t l = lua_strlen(L, 3);
|
||||
size_t i;
|
||||
for (i=0; i<l; i++) {
|
||||
if (news[i] != ESC)
|
||||
luaL_putchar(b, news[i]);
|
||||
else {
|
||||
i++; /* skip ESC */
|
||||
if (!isdigit(uchar(news[i])))
|
||||
luaL_putchar(b, news[i]);
|
||||
else {
|
||||
int level = check_capture(ms, news[i]);
|
||||
push_onecapture(ms, level);
|
||||
luaL_addvalue(b); /* add capture to accumulated result */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else { /* is a function */
|
||||
int n;
|
||||
lua_pushvalue(L, 3);
|
||||
n = push_captures(ms, s, e);
|
||||
lua_call(L, n, 1);
|
||||
if (lua_isstring(L, -1))
|
||||
luaL_addvalue(b); /* add return to accumulated result */
|
||||
else
|
||||
lua_pop(L, 1); /* function result is not a string: pop it */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int str_gsub (lua_State *L) {
|
||||
size_t srcl;
|
||||
const char *src = luaL_check_lstr(L, 1, &srcl);
|
||||
const char *p = luaL_check_string(L, 2);
|
||||
int max_s = luaL_opt_int(L, 4, srcl+1);
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
int n = 0;
|
||||
MatchState ms;
|
||||
luaL_Buffer b;
|
||||
luaL_arg_check(L,
|
||||
lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
|
||||
3, "string or function expected");
|
||||
luaL_buffinit(L, &b);
|
||||
ms.L = L;
|
||||
ms.src_init = src;
|
||||
ms.src_end = src+srcl;
|
||||
while (n < max_s) {
|
||||
const char *e;
|
||||
ms.level = 0;
|
||||
e = match(&ms, src, p);
|
||||
if (e) {
|
||||
n++;
|
||||
add_s(&ms, &b, src, e);
|
||||
}
|
||||
if (e && e>src) /* non empty match? */
|
||||
src = e; /* skip it */
|
||||
else if (src < ms.src_end)
|
||||
luaL_putchar(&b, *src++);
|
||||
else break;
|
||||
if (anchor) break;
|
||||
}
|
||||
luaL_addlstring(&b, src, ms.src_end-src);
|
||||
luaL_pushresult(&b);
|
||||
lua_pushnumber(L, n); /* number of substitutions */
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
|
||||
#define MAX_ITEM 512
|
||||
/* maximum size of each format specification (such as '%-099.99d') */
|
||||
#define MAX_FORMAT 20
|
||||
|
||||
|
||||
static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, arg, &l);
|
||||
luaL_putchar(b, '"');
|
||||
while (l--) {
|
||||
switch (*s) {
|
||||
case '"': case '\\': case '\n':
|
||||
luaL_putchar(b, '\\');
|
||||
luaL_putchar(b, *s);
|
||||
break;
|
||||
case '\0': luaL_addlstring(b, "\\000", 4); break;
|
||||
default: luaL_putchar(b, *s);
|
||||
}
|
||||
s++;
|
||||
}
|
||||
luaL_putchar(b, '"');
|
||||
}
|
||||
|
||||
|
||||
static const char *scanformat (lua_State *L, const char *strfrmt,
|
||||
char *form, int *hasprecision) {
|
||||
const char *p = strfrmt;
|
||||
while (strchr("-+ #0", *p)) p++; /* skip flags */
|
||||
if (isdigit(uchar(*p))) p++; /* skip width */
|
||||
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
|
||||
if (*p == '.') {
|
||||
p++;
|
||||
*hasprecision = 1;
|
||||
if (isdigit(uchar(*p))) p++; /* skip precision */
|
||||
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
|
||||
}
|
||||
if (isdigit(uchar(*p)))
|
||||
luaL_error(L, "invalid format (width or precision too long)");
|
||||
if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */
|
||||
luaL_error(L, "invalid format (too long)");
|
||||
form[0] = '%';
|
||||
strncpy(form+1, strfrmt, p-strfrmt+1);
|
||||
form[p-strfrmt+2] = 0;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static int str_format (lua_State *L) {
|
||||
int arg = 1;
|
||||
size_t sfl;
|
||||
const char *strfrmt = luaL_check_lstr(L, arg, &sfl);
|
||||
const char *strfrmt_end = strfrmt+sfl;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
while (strfrmt < strfrmt_end) {
|
||||
if (*strfrmt != '%')
|
||||
luaL_putchar(&b, *strfrmt++);
|
||||
else if (*++strfrmt == '%')
|
||||
luaL_putchar(&b, *strfrmt++); /* %% */
|
||||
else { /* format item */
|
||||
char form[MAX_FORMAT]; /* to store the format (`%...') */
|
||||
char buff[MAX_ITEM]; /* to store the formatted item */
|
||||
int hasprecision = 0;
|
||||
if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == '$')
|
||||
return luaL_error(L, "obsolete `format' option (d$)");
|
||||
arg++;
|
||||
strfrmt = scanformat(L, strfrmt, form, &hasprecision);
|
||||
switch (*strfrmt++) {
|
||||
case 'c': case 'd': case 'i':
|
||||
sprintf(buff, form, luaL_check_int(L, arg));
|
||||
break;
|
||||
case 'o': case 'u': case 'x': case 'X':
|
||||
sprintf(buff, form, (unsigned int)(luaL_check_number(L, arg)));
|
||||
break;
|
||||
case 'e': case 'E': case 'f':
|
||||
case 'g': case 'G':
|
||||
sprintf(buff, form, luaL_check_number(L, arg));
|
||||
break;
|
||||
case 'q':
|
||||
luaI_addquoted(L, &b, arg);
|
||||
continue; /* skip the `addsize' at the end */
|
||||
case 's': {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, arg, &l);
|
||||
if (!hasprecision && l >= 100) {
|
||||
/* no precision and string is too long to be formatted;
|
||||
keep original string */
|
||||
lua_pushvalue(L, arg);
|
||||
luaL_addvalue(&b);
|
||||
continue; /* skip the `addsize' at the end */
|
||||
}
|
||||
else {
|
||||
sprintf(buff, form, s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
default: /* also treat cases `pnLlh' */
|
||||
return luaL_error(L, "invalid option in `format'");
|
||||
}
|
||||
luaL_addlstring(&b, buff, strlen(buff));
|
||||
}
|
||||
}
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_reg strlib[] = {
|
||||
{"len", str_len},
|
||||
{"sub", str_sub},
|
||||
{"lower", str_lower},
|
||||
{"upper", str_upper},
|
||||
{"char", str_char},
|
||||
{"rep", str_rep},
|
||||
{"byte", str_byte},
|
||||
{"format", str_format},
|
||||
{"find", str_find},
|
||||
{"gfind", gfind},
|
||||
{"gsub", str_gsub},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Open string library
|
||||
*/
|
||||
LUALIB_API int lua_strlibopen (lua_State *L) {
|
||||
luaL_opennamedlib(L, LUA_STRLIBNAME, strlib, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
489
ltable.c
Normal file
489
ltable.c
Normal file
@@ -0,0 +1,489 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 1.115 2002/08/05 14:45:32 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** Implementation of tables (aka arrays, objects, or hash tables).
|
||||
** Tables keep its elements in two parts: an array part and a hash part.
|
||||
** Non-negative integer keys are all candidates to be kept in the array
|
||||
** part. The actual size of the array is the largest `n' such that at
|
||||
** least half the slots between 0 and n are in use.
|
||||
** Hash 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.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "ltable.h"
|
||||
|
||||
|
||||
/*
|
||||
** max size of array part is 2^MAXBITS
|
||||
*/
|
||||
#if BITS_INT > 26
|
||||
#define MAXBITS 24
|
||||
#else
|
||||
#define MAXBITS (BITS_INT-2)
|
||||
#endif
|
||||
|
||||
/* check whether `x' < 2^MAXBITS */
|
||||
#define toobig(x) ((((x)-1) >> MAXBITS) != 0)
|
||||
|
||||
|
||||
/* function to convert a lua_Number to int (with any rounding method) */
|
||||
#ifndef lua_number2int
|
||||
#define lua_number2int(i,n) ((i)=(int)(n))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#define hashnum(t,n) \
|
||||
(node(t, lmod(cast(lu_hash, cast(ls_hash, n)), sizenode(t))))
|
||||
#define hashstr(t,str) (node(t, lmod((str)->tsv.hash, sizenode(t))))
|
||||
#define hashboolean(t,p) (node(t, lmod(p, sizenode(t))))
|
||||
|
||||
/*
|
||||
** avoid modulus by power of 2 for pointers, as they tend to have many
|
||||
** 2 factors.
|
||||
*/
|
||||
#define hashpointer(t,p) (node(t, (IntPoint(p) % ((sizenode(t)-1)|1))))
|
||||
|
||||
|
||||
/*
|
||||
** returns the `main' position of an element in a table (that is, the index
|
||||
** of its hash value)
|
||||
*/
|
||||
Node *luaH_mainposition (const Table *t, const TObject *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TNUMBER: {
|
||||
int ikey;
|
||||
lua_number2int(ikey, nvalue(key));
|
||||
return hashnum(t, ikey);
|
||||
}
|
||||
case LUA_TSTRING:
|
||||
return hashstr(t, tsvalue(key));
|
||||
case LUA_TBOOLEAN:
|
||||
return hashboolean(t, bvalue(key));
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
return hashpointer(t, pvalue(key));
|
||||
case LUA_TUSERDATA:
|
||||
return hashpointer(t, uvalue(key));
|
||||
case LUA_TFUNCTION:
|
||||
return hashpointer(t, clvalue(key));
|
||||
case LUA_TTABLE:
|
||||
return hashpointer(t, hvalue(key));
|
||||
}
|
||||
lua_assert(0);
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns the index for `key' if `key' is an appropriate key to live in
|
||||
** the array part of the table, -1 otherwise.
|
||||
*/
|
||||
static int arrayindex (const TObject *key) {
|
||||
if (ttisnumber(key)) {
|
||||
int k;
|
||||
lua_number2int(k, (nvalue(key)));
|
||||
if (cast(lua_Number, k) == nvalue(key) && k >= 1 && !toobig(k))
|
||||
return k;
|
||||
}
|
||||
return -1; /* `key' did not match some condition */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns the index of a `key' for table traversals. First goes all
|
||||
** elements in the array part, then elements in the hash part. The
|
||||
** beginning and end of a traversal are signalled by -1.
|
||||
*/
|
||||
static int luaH_index (lua_State *L, Table *t, const TObject *key) {
|
||||
int i;
|
||||
if (ttisnil(key)) return -1; /* first iteration */
|
||||
i = arrayindex(key);
|
||||
if (0 <= i && i <= t->sizearray) { /* is `key' inside array part? */
|
||||
return i-1; /* yes; that's the index (corrected to C) */
|
||||
}
|
||||
else {
|
||||
const TObject *v = luaH_get(t, key);
|
||||
if (v == &luaO_nilobject)
|
||||
luaG_runerror(L, "invalid key for `next'");
|
||||
i = cast(int, (cast(const lu_byte *, v) -
|
||||
cast(const lu_byte *, val(node(t, 0)))) / sizeof(Node));
|
||||
return i + t->sizearray; /* hash elements are numbered after array ones */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaH_next (lua_State *L, Table *t, TObject *key) {
|
||||
int i = luaH_index(L, t, key); /* find original element */
|
||||
for (i++; i < t->sizearray; i++) { /* try first array part */
|
||||
if (!ttisnil(&t->array[i])) { /* a non-nil value? */
|
||||
setnvalue(key, i+1);
|
||||
setobj(key+1, &t->array[i]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
|
||||
if (!ttisnil(val(node(t, i)))) { /* a non-nil value? */
|
||||
setobj(key, key(node(t, i)));
|
||||
setobj(key+1, val(node(t, i)));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0; /* no more elements */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {=============================================================
|
||||
** Rehash
|
||||
** ==============================================================
|
||||
*/
|
||||
|
||||
|
||||
static void computesizes (int nums[], int ntotal, int *narray, int *nhash) {
|
||||
int i;
|
||||
int a = nums[0]; /* number of elements smaller than 2^i */
|
||||
int na = a; /* number of elements to go to array part */
|
||||
int n = (na == 0) ? -1 : 0; /* (log of) optimal size for array part */
|
||||
for (i = 1; i <= MAXBITS && *narray >= twoto(i-1); i++) {
|
||||
if (nums[i] > 0) {
|
||||
a += nums[i];
|
||||
if (a >= twoto(i-1)) { /* more than half elements in use? */
|
||||
n = i;
|
||||
na = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_assert(na <= *narray && *narray <= ntotal);
|
||||
*nhash = ntotal - na;
|
||||
*narray = (n == -1) ? 0 : twoto(n);
|
||||
lua_assert(na <= *narray && na >= *narray/2);
|
||||
}
|
||||
|
||||
|
||||
static void numuse (const Table *t, int *narray, int *nhash) {
|
||||
int nums[MAXBITS+1];
|
||||
int i;
|
||||
int totaluse = 0;
|
||||
for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* init `nums' */
|
||||
/* count elements in array part */
|
||||
i = luaO_log2(t->sizearray) + 1; /* number of `slices' */
|
||||
while (i--) { /* for each slice [2^(i-1) to 2^i) */
|
||||
int to = twoto(i);
|
||||
int from = to/2;
|
||||
if (to > t->sizearray) to = t->sizearray;
|
||||
for (; from < to; from++)
|
||||
if (!ttisnil(&t->array[from])) {
|
||||
nums[i]++;
|
||||
totaluse++;
|
||||
}
|
||||
}
|
||||
*narray = totaluse; /* all previous uses were in array part */
|
||||
/* count elements in hash part */
|
||||
i = sizenode(t);
|
||||
while (i--) {
|
||||
if (!ttisnil(val(&t->node[i]))) {
|
||||
int k = arrayindex(key(&t->node[i]));
|
||||
if (k >= 0) { /* is `key' an appropriate array index? */
|
||||
nums[luaO_log2(k-1)+1]++; /* count as such */
|
||||
(*narray)++;
|
||||
}
|
||||
totaluse++;
|
||||
}
|
||||
}
|
||||
computesizes(nums, totaluse, narray, nhash);
|
||||
}
|
||||
|
||||
|
||||
static void setarrayvector (lua_State *L, Table *t, int size) {
|
||||
int i;
|
||||
luaM_reallocvector(L, t->array, t->sizearray, size, TObject);
|
||||
for (i=t->sizearray; i<size; i++)
|
||||
setnilvalue(&t->array[i]);
|
||||
t->sizearray = size;
|
||||
}
|
||||
|
||||
|
||||
static void setnodevector (lua_State *L, Table *t, int lsize) {
|
||||
int i;
|
||||
int size = twoto(lsize);
|
||||
if (lsize > MAXBITS)
|
||||
luaG_runerror(L, "table overflow");
|
||||
if (lsize == 0) { /* no elements to hash part? */
|
||||
t->node = G(L)->dummynode; /* use common `dummynode' */
|
||||
lua_assert(ttisnil(key(t->node))); /* assert invariants: */
|
||||
lua_assert(ttisnil(val(t->node)));
|
||||
lua_assert(t->node->next == NULL); /* (`dummynode' must be empty) */
|
||||
}
|
||||
else {
|
||||
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->lsizenode = cast(lu_byte, lsize);
|
||||
t->firstfree = node(t, size-1); /* first free position to be used */
|
||||
}
|
||||
|
||||
|
||||
static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
||||
int i;
|
||||
int oldasize = t->sizearray;
|
||||
int oldhsize = t->lsizenode;
|
||||
Node *nold;
|
||||
Node temp[1];
|
||||
if (oldhsize)
|
||||
nold = t->node; /* save old hash ... */
|
||||
else { /* old hash is `dummynode' */
|
||||
lua_assert(t->node == G(L)->dummynode);
|
||||
temp[0] = t->node[0]; /* copy it to `temp' */
|
||||
nold = temp;
|
||||
setnilvalue(key(G(L)->dummynode)); /* restate invariant */
|
||||
setnilvalue(val(G(L)->dummynode));
|
||||
lua_assert(G(L)->dummynode->next == NULL);
|
||||
}
|
||||
if (nasize > oldasize) /* array part must grow? */
|
||||
setarrayvector(L, t, nasize);
|
||||
/* create new hash part with appropriate size */
|
||||
setnodevector(L, t, nhsize);
|
||||
/* re-insert elements */
|
||||
if (nasize < oldasize) { /* array part must shrink? */
|
||||
t->sizearray = nasize;
|
||||
/* re-insert elements from vanishing slice */
|
||||
for (i=nasize; i<oldasize; i++) {
|
||||
if (!ttisnil(&t->array[i]))
|
||||
setobj(luaH_setnum(L, t, i+1), &t->array[i]);
|
||||
}
|
||||
/* shrink array */
|
||||
luaM_reallocvector(L, t->array, oldasize, nasize, TObject);
|
||||
}
|
||||
/* re-insert elements in hash part */
|
||||
for (i = twoto(oldhsize) - 1; i >= 0; i--) {
|
||||
Node *old = nold+i;
|
||||
if (!ttisnil(val(old)))
|
||||
setobj(luaH_set(L, t, key(old)), val(old));
|
||||
}
|
||||
if (oldhsize)
|
||||
luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
|
||||
}
|
||||
|
||||
|
||||
static void rehash (lua_State *L, Table *t) {
|
||||
int nasize, nhsize;
|
||||
numuse(t, &nasize, &nhsize); /* compute new sizes for array and hash parts */
|
||||
resize(L, t, nasize, luaO_log2(nhsize)+1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** }=============================================================
|
||||
*/
|
||||
|
||||
|
||||
Table *luaH_new (lua_State *L, int narray, int lnhash) {
|
||||
Table *t = luaM_new(L, Table);
|
||||
t->metatable = hvalue(defaultmeta(L));
|
||||
t->next = G(L)->roottable;
|
||||
G(L)->roottable = t;
|
||||
t->mark = t;
|
||||
t->flags = cast(lu_byte, ~0);
|
||||
t->mode = 0;
|
||||
/* temporary values (kept only if some malloc fails) */
|
||||
t->array = NULL;
|
||||
t->sizearray = 0;
|
||||
t->lsizenode = 0;
|
||||
t->node = NULL;
|
||||
setarrayvector(L, t, narray);
|
||||
setnodevector(L, t, lnhash);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
void luaH_free (lua_State *L, Table *t) {
|
||||
if (t->lsizenode)
|
||||
luaM_freearray(L, t->node, sizenode(t), Node);
|
||||
luaM_freearray(L, t->array, t->sizearray, TObject);
|
||||
luaM_freelem(L, t);
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
/*
|
||||
** try to remove an element from a hash table; cannot move any element
|
||||
** (because gc can call `remove' during a table traversal)
|
||||
*/
|
||||
void luaH_remove (Table *t, Node *e) {
|
||||
Node *mp = luaH_mainposition(t, key(e));
|
||||
if (e != mp) { /* element not in its main position? */
|
||||
while (mp->next != e) mp = mp->next; /* find previous */
|
||||
mp->next = e->next; /* remove `e' from its list */
|
||||
}
|
||||
else {
|
||||
if (e->next != NULL) ??
|
||||
}
|
||||
lua_assert(ttisnil(val(node)));
|
||||
setnilvalue(key(e)); /* clear node `e' */
|
||||
e->next = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** 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, Table *t, const TObject *key) {
|
||||
TObject *val;
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
if (!ttisnil(val(mp))) { /* 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;
|
||||
}
|
||||
}
|
||||
setobj(key(mp), key);
|
||||
lua_assert(ttisnil(val(mp)));
|
||||
for (;;) { /* correct `firstfree' */
|
||||
if (ttisnil(key(t->firstfree)))
|
||||
return val(mp); /* OK; table still has a free place */
|
||||
else if (t->firstfree == t->node) break; /* cannot decrement from here */
|
||||
else (t->firstfree)--;
|
||||
}
|
||||
/* no more free places; must create one */
|
||||
setbvalue(val(mp), 0); /* avoid new key being removed */
|
||||
rehash(L, t); /* grow table */
|
||||
val = cast(TObject *, luaH_get(t, key)); /* get new position */
|
||||
lua_assert(ttisboolean(val));
|
||||
setnilvalue(val);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** generic search function
|
||||
*/
|
||||
static const TObject *luaH_getany (Table *t, const TObject *key) {
|
||||
if (ttisnil(key)) return &luaO_nilobject;
|
||||
else {
|
||||
Node *n = luaH_mainposition(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_rawequalObj(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 (Table *t, int key) {
|
||||
if (1 <= key && key <= t->sizearray)
|
||||
return &t->array[key-1];
|
||||
else {
|
||||
Node *n = hashnum(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttisnumber(key(n)) && 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 (Table *t, TString *key) {
|
||||
Node *n = hashstr(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttisstring(key(n)) && 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 (Table *t, const TObject *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
|
||||
case LUA_TNUMBER: {
|
||||
int k;
|
||||
lua_number2int(k, (nvalue(key)));
|
||||
if (cast(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, Table *t, const TObject *key) {
|
||||
const TObject *p = luaH_get(t, key);
|
||||
t->flags = 0;
|
||||
if (p != &luaO_nilobject)
|
||||
return cast(TObject *, p);
|
||||
else {
|
||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||
else if (ttisnumber(key) && nvalue(key) != nvalue(key))
|
||||
luaG_runerror(L, "table index is NaN");
|
||||
return newkey(L, t, key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_setnum (lua_State *L, Table *t, int key) {
|
||||
const TObject *p = luaH_getnum(t, key);
|
||||
if (p != &luaO_nilobject)
|
||||
return cast(TObject *, p);
|
||||
else {
|
||||
TObject k;
|
||||
setnvalue(&k, key);
|
||||
return newkey(L, t, &k);
|
||||
}
|
||||
}
|
||||
|
||||
31
ltable.h
Normal file
31
ltable.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 1.41 2002/03/11 12:45:00 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ltable_h
|
||||
#define ltable_h
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define node(t,i) (&(t)->node[i])
|
||||
#define key(n) (&(n)->i_key)
|
||||
#define val(n) (&(n)->i_val)
|
||||
|
||||
|
||||
const TObject *luaH_getnum (Table *t, int key);
|
||||
TObject *luaH_setnum (lua_State *L, Table *t, int key);
|
||||
const TObject *luaH_getstr (Table *t, TString *key);
|
||||
const TObject *luaH_get (Table *t, const TObject *key);
|
||||
TObject *luaH_set (lua_State *L, Table *t, const TObject *key);
|
||||
Table *luaH_new (lua_State *L, int narray, int lnhash);
|
||||
void luaH_free (lua_State *L, Table *t);
|
||||
int luaH_next (lua_State *L, Table *t, TObject *key);
|
||||
|
||||
/* exported only for debugging */
|
||||
Node *luaH_mainposition (const Table *t, const TObject *key);
|
||||
|
||||
|
||||
#endif
|
||||
294
ltablib.c
Normal file
294
ltablib.c
Normal file
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
** $Id: ltablib.c,v 1.11 2002/08/06 17:06:56 roberto Exp roberto $
|
||||
** Library for Table Manipulation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static int checkint (lua_State *L) {
|
||||
int n = (int)lua_tonumber(L, -1);
|
||||
if (n == 0 && !lua_isnumber(L, -1)) n = -1;
|
||||
lua_pop(L, 1);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
static void aux_setn (lua_State *L, int t, int n) {
|
||||
lua_pushliteral(L, "n");
|
||||
lua_rawget(L, t);
|
||||
if (checkint(L) >= 0) {
|
||||
lua_pushliteral(L, "n"); /* use it */
|
||||
lua_pushnumber(L, n);
|
||||
lua_rawset(L, t);
|
||||
}
|
||||
else { /* use N */
|
||||
lua_pushvalue(L, t);
|
||||
lua_pushnumber(L, n);
|
||||
lua_rawset(L, lua_upvalueindex(1)); /* N[t] = n */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int aux_getn (lua_State *L, int t) {
|
||||
int n;
|
||||
luaL_check_type(L, t, LUA_TTABLE);
|
||||
lua_pushliteral(L, "n"); /* try t.n */
|
||||
lua_rawget(L, t);
|
||||
if ((n = checkint(L)) >= 0) return n;
|
||||
lua_pushvalue(L, t); /* try N[t] */
|
||||
lua_rawget(L, lua_upvalueindex(1));
|
||||
if ((n = checkint(L)) >= 0) return n;
|
||||
else { /* must count elements */
|
||||
n = 0;
|
||||
for (;;) {
|
||||
lua_rawgeti(L, t, ++n);
|
||||
if (lua_isnil(L, -1)) break;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
aux_setn(L, t, n - 1);
|
||||
return n - 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_foreachi (lua_State *L) {
|
||||
int i;
|
||||
int n = aux_getn(L, 1);
|
||||
luaL_check_type(L, 2, LUA_TFUNCTION);
|
||||
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_call(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_check_type(L, 1, LUA_TTABLE);
|
||||
luaL_check_type(L, 2, LUA_TFUNCTION);
|
||||
lua_pushnil(L); /* first key */
|
||||
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_call(L, 2, 1);
|
||||
if (!lua_isnil(L, -1))
|
||||
return 1;
|
||||
lua_pop(L, 2); /* remove value and result */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getn (lua_State *L) {
|
||||
lua_pushnumber(L, aux_getn(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_setn (lua_State *L) {
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
aux_setn(L, 1, luaL_check_int(L, 2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tinsert (lua_State *L) {
|
||||
int v = lua_gettop(L); /* number of arguments */
|
||||
int n = aux_getn(L, 1) + 1;
|
||||
int pos; /* where to insert new element */
|
||||
if (v == 2) /* called with only 2 arguments */
|
||||
pos = n; /* insert new element at the end */
|
||||
else {
|
||||
pos = luaL_check_int(L, 2); /* 2nd argument is the position */
|
||||
if (pos > n) n = pos; /* `grow' array if necessary */
|
||||
v = 3; /* function may be called with more than 3 args */
|
||||
}
|
||||
aux_setn(L, 1, n); /* new size */
|
||||
while (--n >= pos) { /* move up elements */
|
||||
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 n = aux_getn(L, 1);
|
||||
int 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); /* t[pos] = t[pos+1] */
|
||||
}
|
||||
lua_pushnil(L);
|
||||
lua_rawseti(L, 1, n); /* t[n] = nil */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int str_concat (lua_State *L) {
|
||||
luaL_Buffer b;
|
||||
int n = aux_getn(L, 1);
|
||||
size_t lsep;
|
||||
const char *sep = luaL_opt_lstr(L, 2, "", &lsep);
|
||||
int i;
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=1; i<=n; i++) {
|
||||
lua_rawgeti(L, 1, i);
|
||||
luaL_arg_check(L, lua_isstring(L, -1), 1,
|
||||
"table contains non-strings");
|
||||
luaL_addvalue(&b);
|
||||
if (i != n)
|
||||
luaL_addlstring(&b, sep, lsep);
|
||||
}
|
||||
luaL_pushresult(&b);
|
||||
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) {
|
||||
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_call(L, 2, 1);
|
||||
res = lua_toboolean(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) luaL_error(L, "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) luaL_error(L, "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 = aux_getn(L, 1);
|
||||
luaL_check_stack(L, 40, ""); /* assume array is smaller than 2^40 */
|
||||
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
|
||||
luaL_check_type(L, 2, LUA_TFUNCTION);
|
||||
lua_settop(L, 2); /* make sure there is two arguments */
|
||||
auxsort(L, 1, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static const luaL_reg tab_funcs[] = {
|
||||
{"concat", str_concat},
|
||||
{"foreach", luaB_foreach},
|
||||
{"foreachi", luaB_foreachi},
|
||||
{"getn", luaB_getn},
|
||||
{"setn", luaB_setn},
|
||||
{"sort", luaB_sort},
|
||||
{"insert", luaB_tinsert},
|
||||
{"remove", luaB_tremove},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API int lua_tablibopen (lua_State *L) {
|
||||
lua_newtable(L); /* create N (table to store num. elements in tables) */
|
||||
lua_setmode(L, -1, "k"); /* make it a weak table */
|
||||
luaL_opennamedlib(L, LUA_TABLIBNAME, tab_funcs, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
722
ltests.c
Normal file
722
ltests.c
Normal file
@@ -0,0 +1,722 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 1.132 2002/08/06 15:32:22 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>
|
||||
|
||||
|
||||
#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 "lualib.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** The whole module only makes sense with LUA_DEBUG on
|
||||
*/
|
||||
#ifdef LUA_DEBUG
|
||||
|
||||
|
||||
static lua_State *lua_state = NULL;
|
||||
|
||||
int islocked = 0;
|
||||
|
||||
|
||||
#define index(L,k) (L->ci->base+(k) - 1)
|
||||
|
||||
|
||||
static void setnameval (lua_State *L, const 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) (cast(size_t *, b) - HEADER/sizeof(size_t))
|
||||
|
||||
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(*(cast(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));
|
||||
lua_assert(oldsize == 0 || oldsize == *blocksize(block));
|
||||
/* ISO does not specify what realloc(NULL, 0) does */
|
||||
lua_assert(block != NULL || size > 0);
|
||||
if (size == 0) {
|
||||
freeblock(block);
|
||||
return NULL;
|
||||
}
|
||||
else if (memdebug_total+size-oldsize > memdebug_memlimit)
|
||||
return NULL; /* to test memory allocation errors */
|
||||
else {
|
||||
void *newblock;
|
||||
int i;
|
||||
size_t realsize = HEADER+size+MARKSIZE;
|
||||
if (realsize < size) return NULL; /* overflow! */
|
||||
newblock = malloc(realsize); /* alloc a new block */
|
||||
if (newblock == NULL) return NULL;
|
||||
if (oldsize > size) oldsize = size;
|
||||
if (block) {
|
||||
memcpy(cast(char *, newblock)+HEADER, block, oldsize);
|
||||
freeblock(block); /* erase (and check) old copy */
|
||||
}
|
||||
/* initialize new part of the block with something `weird' */
|
||||
memset(cast(char *, newblock)+HEADER+oldsize, -MARK, size-oldsize);
|
||||
memdebug_total += size;
|
||||
if (memdebug_total > memdebug_maxmem)
|
||||
memdebug_maxmem = memdebug_total;
|
||||
memdebug_numblocks++;
|
||||
*cast(size_t *, newblock) = size;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
*(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
|
||||
return cast(char *, newblock)+HEADER;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Disassembler
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static char *buildop (Proto *p, int pc, char *buff) {
|
||||
Instruction i = p->code[pc];
|
||||
OpCode o = GET_OPCODE(i);
|
||||
const char *name = luaP_opnames[o];
|
||||
int line = getline(p, pc);
|
||||
sprintf(buff, "(%4d) %4d - ", line, pc);
|
||||
switch (getOpMode(o)) {
|
||||
case iABC:
|
||||
sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
|
||||
GETARG_A(i), GETARG_B(i), GETARG_C(i));
|
||||
break;
|
||||
case iABx:
|
||||
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
|
||||
break;
|
||||
case iAsBx:
|
||||
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
|
||||
break;
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
void luaI_printcode (Proto *pt, int size) {
|
||||
int pc;
|
||||
for (pc=0; pc<size; pc++) {
|
||||
char buff[100];
|
||||
printf("%s\n", buildop(pt, pc, buff));
|
||||
}
|
||||
printf("-------\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static int listcode (lua_State *L) {
|
||||
int pc;
|
||||
Proto *p;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, "Lua function expected");
|
||||
p = clvalue(index(L, 1))->l.p;
|
||||
lua_newtable(L);
|
||||
setnameval(L, "maxstack", p->maxstacksize);
|
||||
setnameval(L, "numparams", p->numparams);
|
||||
for (pc=0; pc<p->sizecode; pc++) {
|
||||
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, "Lua function expected");
|
||||
p = clvalue(index(L, 1))->l.p;
|
||||
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 char *name;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, "Lua function expected");
|
||||
p = clvalue(index(L, 1))->l.p;
|
||||
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
||||
lua_pushstring(L, name);
|
||||
return i-1;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
|
||||
static int get_limits (lua_State *L) {
|
||||
lua_newtable(L);
|
||||
setnameval(L, "BITS_INT", BITS_INT);
|
||||
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
||||
setnameval(L, "MAXVARS", MAXVARS);
|
||||
setnameval(L, "MAXPARAMS", MAXPARAMS);
|
||||
setnameval(L, "MAXSTACK", MAXSTACK);
|
||||
setnameval(L, "MAXUPVALUES", MAXUPVALUES);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int mem_query (lua_State *L) {
|
||||
if (lua_isnone(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_isnone(L, 2)) {
|
||||
luaL_arg_check(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
|
||||
lua_pushnumber(L, tsvalue(index(L, 1))->tsv.hash);
|
||||
}
|
||||
else {
|
||||
TObject *o = index(L, 1);
|
||||
Table *t;
|
||||
luaL_check_type(L, 2, LUA_TTABLE);
|
||||
t = hvalue(index(L, 2));
|
||||
lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int stacklevel (lua_State *L) {
|
||||
unsigned long a = 0;
|
||||
lua_pushnumber(L, (int)(L->top - L->stack));
|
||||
lua_pushnumber(L, (int)(L->stack_last - L->stack));
|
||||
lua_pushnumber(L, (int)(L->ci - L->base_ci));
|
||||
lua_pushnumber(L, (int)(L->end_ci - L->base_ci));
|
||||
lua_pushnumber(L, (unsigned long)&a);
|
||||
return 5;
|
||||
}
|
||||
|
||||
|
||||
static int table_query (lua_State *L) {
|
||||
const Table *t;
|
||||
int i = luaL_opt_int(L, 2, -1);
|
||||
luaL_check_type(L, 1, LUA_TTABLE);
|
||||
t = hvalue(index(L, 1));
|
||||
if (i == -1) {
|
||||
lua_pushnumber(L, t->sizearray);
|
||||
lua_pushnumber(L, sizenode(t));
|
||||
lua_pushnumber(L, t->firstfree - t->node);
|
||||
}
|
||||
else if (i < t->sizearray) {
|
||||
lua_pushnumber(L, i);
|
||||
luaA_pushobject(L, &t->array[i]);
|
||||
lua_pushnil(L);
|
||||
}
|
||||
else if ((i -= t->sizearray) < sizenode(t)) {
|
||||
if (!ttisnil(val(node(t, i))) ||
|
||||
ttisnil(key(node(t, i))) ||
|
||||
ttisnumber(key(node(t, i)))) {
|
||||
luaA_pushobject(L, key(node(t, i)));
|
||||
}
|
||||
else
|
||||
lua_pushstring(L, "<undef>");
|
||||
luaA_pushobject(L, val(&t->node[i]));
|
||||
if (t->node[i].next)
|
||||
lua_pushnumber(L, t->node[i].next - t->node);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
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(L);
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int tref (lua_State *L) {
|
||||
int level = lua_gettop(L);
|
||||
int lock = luaL_opt_int(L, 2, 1);
|
||||
luaL_check_any(L, 1);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushnumber(L, lua_ref(L, lock));
|
||||
assert(lua_gettop(L) == level+1); /* +1 for result */
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getref (lua_State *L) {
|
||||
int level = lua_gettop(L);
|
||||
lua_getref(L, luaL_check_int(L, 1));
|
||||
assert(lua_gettop(L) == level+1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
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 metatable (lua_State *L) {
|
||||
luaL_check_any(L, 1);
|
||||
if (lua_isnone(L, 2)) {
|
||||
if (lua_getmetatable(L, 1) == 0)
|
||||
lua_pushnil(L);
|
||||
}
|
||||
else {
|
||||
lua_settop(L, 2);
|
||||
luaL_check_type(L, 2, LUA_TTABLE);
|
||||
lua_setmetatable(L, 1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int newuserdata (lua_State *L) {
|
||||
size_t size = luaL_check_int(L, 1);
|
||||
char *p = cast(char *, lua_newuserdata(L, size));
|
||||
while (size--) *p++ = '\0';
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int pushuserdata (lua_State *L) {
|
||||
lua_pushlightuserdata(L, cast(void *, luaL_check_int(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int udataval (lua_State *L) {
|
||||
lua_pushnumber(L, cast(int, lua_touserdata(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int doonnewstack (lua_State *L) {
|
||||
lua_State *L1 = lua_newthread(L);
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
int status = luaL_loadbuffer(L1, s, l, s);
|
||||
if (status == 0)
|
||||
status = lua_pcall(L1, 0, 0, 0);
|
||||
lua_pushnumber(L, status);
|
||||
lua_closethread(L, L1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int s2d (lua_State *L) {
|
||||
lua_pushnumber(L, *cast(const double *, luaL_check_string(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int d2s (lua_State *L) {
|
||||
double d = luaL_check_number(L, 1);
|
||||
lua_pushlstring(L, cast(char *, &d), sizeof(d));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int newstate (lua_State *L) {
|
||||
lua_State *L1 = lua_open();
|
||||
if (L1) {
|
||||
*cast(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 = cast(lua_State *, cast(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 = cast(lua_State *, cast(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 = cast(lua_State *,cast(unsigned long,luaL_check_number(L, 1)));
|
||||
size_t lcode;
|
||||
const char *code = luaL_check_lstr(L, 2, &lcode);
|
||||
int status;
|
||||
lua_settop(L1, 0);
|
||||
status = luaL_loadbuffer(L1, code, lcode, code);
|
||||
if (status == 0)
|
||||
status = lua_pcall(L1, 0, LUA_MULTRET, 0);
|
||||
if (status != 0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushnumber(L, status);
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
int i = 0;
|
||||
while (!lua_isnone(L1, ++i))
|
||||
lua_pushstring(L, lua_tostring(L1, i));
|
||||
lua_pop(L1, i-1);
|
||||
return i-1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int log2_aux (lua_State *L) {
|
||||
lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** 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 char *const delimits = " \t\n,;";
|
||||
|
||||
static void skip (const char **pc) {
|
||||
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
||||
}
|
||||
|
||||
static int getnum_aux (lua_State *L, const char **pc) {
|
||||
int res = 0;
|
||||
int sig = 1;
|
||||
skip(pc);
|
||||
if (**pc == '.') {
|
||||
res = cast(int, lua_tonumber(L, -1));
|
||||
lua_pop(L, 1);
|
||||
(*pc)++;
|
||||
return res;
|
||||
}
|
||||
else if (**pc == '-') {
|
||||
sig = -1;
|
||||
(*pc)++;
|
||||
}
|
||||
while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
|
||||
return sig*res;
|
||||
}
|
||||
|
||||
static const char *getname_aux (char *buff, const char **pc) {
|
||||
int i = 0;
|
||||
skip(pc);
|
||||
while (**pc != '\0' && !strchr(delimits, **pc))
|
||||
buff[i++] = *(*pc)++;
|
||||
buff[i] = '\0';
|
||||
return buff;
|
||||
}
|
||||
|
||||
|
||||
#define EQ(s1) (strcmp(s1, inst) == 0)
|
||||
|
||||
#define getnum (getnum_aux(L, &pc))
|
||||
#define getname (getname_aux(buff, &pc))
|
||||
|
||||
|
||||
static int testC (lua_State *L) {
|
||||
char buff[30];
|
||||
const char *pc = luaL_check_string(L, 1);
|
||||
for (;;) {
|
||||
const char *inst = getname;
|
||||
if EQ("") return 0;
|
||||
else if EQ("isnumber") {
|
||||
lua_pushnumber(L, lua_isnumber(L, getnum));
|
||||
}
|
||||
else if EQ("isstring") {
|
||||
lua_pushnumber(L, lua_isstring(L, getnum));
|
||||
}
|
||||
else if EQ("istable") {
|
||||
lua_pushnumber(L, lua_istable(L, getnum));
|
||||
}
|
||||
else if EQ("iscfunction") {
|
||||
lua_pushnumber(L, lua_iscfunction(L, getnum));
|
||||
}
|
||||
else if EQ("isfunction") {
|
||||
lua_pushnumber(L, lua_isfunction(L, getnum));
|
||||
}
|
||||
else if EQ("isuserdata") {
|
||||
lua_pushnumber(L, lua_isuserdata(L, getnum));
|
||||
}
|
||||
else if EQ("isudataval") {
|
||||
lua_pushnumber(L, lua_islightuserdata(L, getnum));
|
||||
}
|
||||
else if EQ("isnil") {
|
||||
lua_pushnumber(L, lua_isnil(L, getnum));
|
||||
}
|
||||
else if EQ("isnull") {
|
||||
lua_pushnumber(L, lua_isnone(L, getnum));
|
||||
}
|
||||
else if EQ("tonumber") {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
}
|
||||
else if EQ("tostring") {
|
||||
const char *s = lua_tostring(L, getnum);
|
||||
lua_pushstring(L, s);
|
||||
}
|
||||
else if EQ("tonumber") {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
}
|
||||
else if EQ("strlen") {
|
||||
lua_pushnumber(L, lua_strlen(L, getnum));
|
||||
}
|
||||
else if EQ("tocfunction") {
|
||||
lua_pushcfunction(L, lua_tocfunction(L, getnum));
|
||||
}
|
||||
else if EQ("return") {
|
||||
return getnum;
|
||||
}
|
||||
else if EQ("gettop") {
|
||||
lua_pushnumber(L, lua_gettop(L));
|
||||
}
|
||||
else if EQ("settop") {
|
||||
lua_settop(L, getnum);
|
||||
}
|
||||
else if EQ("pop") {
|
||||
lua_pop(L, getnum);
|
||||
}
|
||||
else if EQ("pushnum") {
|
||||
lua_pushnumber(L, getnum);
|
||||
}
|
||||
else if EQ("pushnil") {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
else if EQ("pushbool") {
|
||||
lua_pushboolean(L, getnum);
|
||||
}
|
||||
else if EQ("tobool") {
|
||||
lua_pushnumber(L, lua_toboolean(L, getnum));
|
||||
}
|
||||
else if EQ("pushvalue") {
|
||||
lua_pushvalue(L, getnum);
|
||||
}
|
||||
else if EQ("pushcclosure") {
|
||||
lua_pushcclosure(L, testC, getnum);
|
||||
}
|
||||
else if EQ("pushupvalues") {
|
||||
lua_pushupvalues(L);
|
||||
}
|
||||
else if EQ("remove") {
|
||||
lua_remove(L, getnum);
|
||||
}
|
||||
else if EQ("insert") {
|
||||
lua_insert(L, getnum);
|
||||
}
|
||||
else if EQ("replace") {
|
||||
lua_replace(L, getnum);
|
||||
}
|
||||
else if EQ("gettable") {
|
||||
lua_gettable(L, getnum);
|
||||
}
|
||||
else if EQ("settable") {
|
||||
lua_settable(L, getnum);
|
||||
}
|
||||
else if EQ("next") {
|
||||
lua_next(L, -2);
|
||||
}
|
||||
else if EQ("concat") {
|
||||
lua_concat(L, getnum);
|
||||
}
|
||||
else if EQ("lessthan") {
|
||||
int a = getnum;
|
||||
lua_pushboolean(L, lua_lessthan(L, a, getnum));
|
||||
}
|
||||
else if EQ("equal") {
|
||||
int a = getnum;
|
||||
lua_pushboolean(L, lua_equal(L, a, getnum));
|
||||
}
|
||||
else if EQ("rawcall") {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_call(L, narg, nres);
|
||||
}
|
||||
else if EQ("call") {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_pcall(L, narg, nres, 0);
|
||||
}
|
||||
else if EQ("loadstring") {
|
||||
size_t sl;
|
||||
const char *s = luaL_check_lstr(L, getnum, &sl);
|
||||
luaL_loadbuffer(L, s, sl, s);
|
||||
}
|
||||
else if EQ("loadfile") {
|
||||
luaL_loadfile(L, luaL_check_string(L, getnum));
|
||||
}
|
||||
else if EQ("setmetatable") {
|
||||
lua_setmetatable(L, getnum);
|
||||
}
|
||||
else if EQ("getmetatable") {
|
||||
if (lua_getmetatable(L, getnum) == 0)
|
||||
lua_pushnil(L);
|
||||
}
|
||||
else if EQ("type") {
|
||||
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
|
||||
}
|
||||
else luaL_error(L, "unknown instruction %s", buff);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
static const struct luaL_reg tests_funcs[] = {
|
||||
{"hash", hash_query},
|
||||
{"limits", get_limits},
|
||||
{"listcode", listcode},
|
||||
{"listk", listk},
|
||||
{"listlocals", listlocals},
|
||||
{"loadlib", loadlib},
|
||||
{"stacklevel", stacklevel},
|
||||
{"querystr", string_query},
|
||||
{"querytab", table_query},
|
||||
{"testC", testC},
|
||||
{"ref", tref},
|
||||
{"getref", getref},
|
||||
{"unref", unref},
|
||||
{"d2s", d2s},
|
||||
{"s2d", s2d},
|
||||
{"metatable", metatable},
|
||||
{"newuserdata", newuserdata},
|
||||
{"pushuserdata", pushuserdata},
|
||||
{"udataval", udataval},
|
||||
{"doonnewstack", doonnewstack},
|
||||
{"newstate", newstate},
|
||||
{"closestate", closestate},
|
||||
{"doremote", doremote},
|
||||
{"log2", log2_aux},
|
||||
{"totalmem", mem_query},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
static void fim (void) {
|
||||
if (!islocked)
|
||||
lua_close(lua_state);
|
||||
lua_assert(memdebug_numblocks == 0);
|
||||
lua_assert(memdebug_total == 0);
|
||||
}
|
||||
|
||||
|
||||
int luaB_opentests (lua_State *L) {
|
||||
*cast(int **, L) = &islocked; /* init lock */
|
||||
lua_state = L; /* keep first state to be opened */
|
||||
luaL_opennamedlib(L, "T", tests_funcs, 0);
|
||||
atexit(fim);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
61
ltests.h
Normal file
61
ltests.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
** $Id: ltests.h,v 1.14 2002/06/13 13:45:31 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 check_exp(c,e) (lua_assert(c), (e))
|
||||
#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_realloc(b, os, s) debug_realloc(b, os, s)
|
||||
|
||||
void *debug_realloc (void *block, size_t oldsize, size_t size);
|
||||
|
||||
|
||||
|
||||
/* test for lock/unlock */
|
||||
extern int islocked;
|
||||
#define LUA_USERSTATE int *lock;
|
||||
#define lua_userstateopen(l) if (l != NULL) *cast(int **, l) = &islocked;
|
||||
#define lua_lock(L) lua_assert((**cast(int **, L))++ == 0)
|
||||
#define lua_unlock(L) lua_assert(--(**cast(int **, L)) == 0)
|
||||
|
||||
|
||||
int luaB_opentests (lua_State *L);
|
||||
|
||||
#define lua_userinit(L) (luaB_opentests(L) + openstdlibs(L))
|
||||
|
||||
|
||||
|
||||
/* change some sizes to give some bugs a chance */
|
||||
|
||||
#define LUAL_BUFFERSIZE 27
|
||||
#define MINSTRTABSIZE 2
|
||||
|
||||
#endif
|
||||
69
ltm.c
Normal file
69
ltm.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
** $Id: ltm.c,v 1.99 2002/08/05 14:09:06 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
|
||||
const char *const luaT_typenames[] = {
|
||||
"nil", "number", "string", "boolean", "table",
|
||||
"function", "userdata", "userdata"
|
||||
};
|
||||
|
||||
|
||||
void luaT_init (lua_State *L) {
|
||||
static const char *const luaT_eventname[] = { /* ORDER TM */
|
||||
"__index", "__newindex",
|
||||
"__gc", "__eq",
|
||||
"__gettable", "__settable",
|
||||
"__add", "__sub", "__mul", "__div",
|
||||
"__pow", "__unm", "__lt", "__le",
|
||||
"__concat", "__call"
|
||||
};
|
||||
int i;
|
||||
for (i=0; i<TM_N; i++) {
|
||||
G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
|
||||
luaS_fix(G(L)->tmname[i]); /* never collect these names */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** function to be used with macro "fasttm": optimized for absence of
|
||||
** tag methods
|
||||
*/
|
||||
const TObject *luaT_gettm (Table *events, TMS event, TString *ename) {
|
||||
const TObject *tm = luaH_getstr(events, ename);
|
||||
lua_assert(event <= TM_EQ);
|
||||
if (ttisnil(tm)) { /* no tag method? */
|
||||
events->flags |= (1u<<event); /* cache this fact */
|
||||
return NULL;
|
||||
}
|
||||
else return tm;
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) {
|
||||
TString *ename = G(L)->tmname[event];
|
||||
switch (ttype(o)) {
|
||||
case LUA_TTABLE:
|
||||
return luaH_getstr(hvalue(o)->metatable, ename);
|
||||
case LUA_TUSERDATA:
|
||||
return luaH_getstr(uvalue(o)->uv.metatable, ename);
|
||||
default:
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
}
|
||||
|
||||
50
ltm.h
Normal file
50
ltm.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
** $Id: ltm.h,v 1.38 2002/07/01 17:06:58 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ltm_h
|
||||
#define ltm_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER TM"
|
||||
*/
|
||||
typedef enum {
|
||||
TM_INDEX,
|
||||
TM_NEWINDEX,
|
||||
TM_GC,
|
||||
TM_EQ, /* last tag method with `fast' access */
|
||||
TM_GETTABLE,
|
||||
TM_SETTABLE,
|
||||
TM_ADD,
|
||||
TM_SUB,
|
||||
TM_MUL,
|
||||
TM_DIV,
|
||||
TM_POW,
|
||||
TM_UNM,
|
||||
TM_LT,
|
||||
TM_LE,
|
||||
TM_CONCAT,
|
||||
TM_CALL,
|
||||
TM_N /* number of elements in the enum */
|
||||
} TMS;
|
||||
|
||||
|
||||
|
||||
#define fasttm(l,et,e) \
|
||||
(((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, G(l)->tmname[e]))
|
||||
|
||||
|
||||
const TObject *luaT_gettm (Table *events, TMS event, TString *ename);
|
||||
const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event);
|
||||
void luaT_init (lua_State *L);
|
||||
|
||||
extern const char *const luaT_typenames[];
|
||||
|
||||
#endif
|
||||
431
lua.c
431
lua.c
@@ -1,100 +1,385 @@
|
||||
/*
|
||||
** lua.c
|
||||
** Linguagem para Usuarios de Aplicacao
|
||||
** $Id: lua.c,v 1.102 2002/08/12 17:23:12 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
char *rcs_lua="$Id: lua.c,v 1.9 1996/04/23 12:43:07 roberto Exp roberto $";
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
static int lua_argc;
|
||||
static char **lua_argv;
|
||||
|
||||
#ifdef POSIX
|
||||
/*
|
||||
** although this function is POSIX, there is no standard header file that
|
||||
** defines it
|
||||
** generic extra include file
|
||||
*/
|
||||
int isatty (int fd);
|
||||
#else
|
||||
#define isatty(x) (x==0) /* assume stdin is a tty */
|
||||
#ifdef LUA_USERCONFIG
|
||||
#include LUA_USERCONFIG
|
||||
#endif
|
||||
|
||||
/*
|
||||
%F Allow Lua code to access argv strings.
|
||||
%i Receive from Lua the argument number (starting with 1).
|
||||
%o Return to Lua the argument, or nil if it does not exist.
|
||||
*/
|
||||
static void lua_getargv (void)
|
||||
{
|
||||
lua_Object lo = lua_getparam(1);
|
||||
if (!lua_isnumber(lo))
|
||||
lua_pushnil();
|
||||
else
|
||||
{
|
||||
int n = (int)lua_getnumber(lo);
|
||||
if (n < 1 || n > lua_argc) lua_pushnil();
|
||||
else lua_pushstring(lua_argv[n]);
|
||||
}
|
||||
|
||||
#ifdef _POSIX_SOURCE
|
||||
#include <unistd.h>
|
||||
#else
|
||||
static int isatty (int x) { return x==0; } /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef PROMPT
|
||||
#define PROMPT "> "
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef PROMPT2
|
||||
#define PROMPT2 ">> "
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef lua_userinit
|
||||
#define lua_userinit(L) openstdlibs(L)
|
||||
#endif
|
||||
|
||||
|
||||
static lua_State *L = NULL;
|
||||
|
||||
static const char *progname;
|
||||
|
||||
|
||||
static lua_Hook old_hook = NULL;
|
||||
static unsigned long old_mask = 0;
|
||||
|
||||
|
||||
static void lstop (lua_State *l, lua_Debug *ar) {
|
||||
(void)ar; /* unused arg. */
|
||||
lua_sethook(l, old_hook, old_mask);
|
||||
luaL_error(l, "interrupted!");
|
||||
}
|
||||
|
||||
|
||||
static void manual_input (void)
|
||||
{
|
||||
if (isatty(0))
|
||||
{
|
||||
char buffer[250];
|
||||
while (fgets(buffer, sizeof(buffer), stdin) != 0)
|
||||
lua_dostring(buffer);
|
||||
static void laction (int i) {
|
||||
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
|
||||
terminate process (default action) */
|
||||
old_hook = lua_gethook(L);
|
||||
old_mask = lua_gethookmask(L);
|
||||
lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT(1));
|
||||
}
|
||||
|
||||
|
||||
static void print_usage (void) {
|
||||
fprintf(stderr,
|
||||
"usage: %s [options] [prog [args]].\n"
|
||||
"Available options are:\n"
|
||||
" - execute stdin as a file\n"
|
||||
" -e stat execute string `stat'\n"
|
||||
" -i enter interactive mode after executing `prog'\n"
|
||||
" -l name execute file `name'\n"
|
||||
" -v print version information\n"
|
||||
" -- stop handling options\n" ,
|
||||
progname);
|
||||
}
|
||||
|
||||
|
||||
static void l_message (const char *pname, const char *msg) {
|
||||
size_t l = strlen(msg);
|
||||
if (pname) fprintf(stderr, "%s: ", pname);
|
||||
fprintf(stderr, "%s", msg);
|
||||
if (l > 0 && msg[l-1] != '\n') /* does not end with newline? */
|
||||
fprintf(stderr, "\n"); /* add a newline */
|
||||
}
|
||||
|
||||
|
||||
static void report (int status) {
|
||||
const char *msg;
|
||||
if (status) {
|
||||
msg = lua_tostring(L, -1);
|
||||
if (msg == NULL) msg = "(error with no message)";
|
||||
l_message(progname, msg);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int lcall (int clear) {
|
||||
int status;
|
||||
int top = lua_gettop(L);
|
||||
lua_getglobal(L, "_TRACEBACK"); /* get traceback function */
|
||||
lua_insert(L, top); /* put it under chunk */
|
||||
signal(SIGINT, laction);
|
||||
status = lua_pcall(L, 0, LUA_MULTRET, -2);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
lua_remove(L, top); /* remove traceback function */
|
||||
if (status == 0 && clear)
|
||||
lua_settop(L, top); /* remove eventual results */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int l_panic (lua_State *l) {
|
||||
(void)l;
|
||||
l_message(progname, "unable to recover; exiting");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void print_version (void) {
|
||||
l_message(NULL, LUA_VERSION " " LUA_COPYRIGHT);
|
||||
}
|
||||
|
||||
|
||||
static void getargs (char *argv[], int n) {
|
||||
int i;
|
||||
lua_newtable(L);
|
||||
for (i=0; argv[i]; i++) {
|
||||
lua_pushnumber(L, i - n);
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
/* arg.n = maximum index in table `arg' */
|
||||
lua_pushliteral(L, "n");
|
||||
lua_pushnumber(L, i-n-1);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static int docall (int status) {
|
||||
if (status == 0) status = lcall(1);
|
||||
report(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int file_input (const char *name) {
|
||||
return docall(luaL_loadfile(L, name));
|
||||
}
|
||||
|
||||
|
||||
static int dostring (const char *s, const char *name) {
|
||||
return docall(luaL_loadbuffer(L, s, strlen(s), name));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** this macro can be used by some `history' system to save lines
|
||||
** read in manual input
|
||||
*/
|
||||
#ifndef lua_saveline
|
||||
#define lua_saveline(L,line) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** this macro defines a function to show the prompt and reads the
|
||||
** next line for manual input
|
||||
*/
|
||||
#ifndef lua_readline
|
||||
#define lua_readline(L,prompt) readline(L,prompt)
|
||||
|
||||
/* maximum length of an input line */
|
||||
#ifndef MAXINPUT
|
||||
#define MAXINPUT 512
|
||||
#endif
|
||||
|
||||
|
||||
static int readline (lua_State *l, const char *prompt) {
|
||||
static char buffer[MAXINPUT];
|
||||
if (prompt) {
|
||||
fputs(prompt, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == NULL)
|
||||
return 0; /* read fails */
|
||||
else {
|
||||
lua_pushstring(l, buffer);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
static const char *get_prompt (int firstline) {
|
||||
const char *p = NULL;
|
||||
lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
|
||||
p = lua_tostring(L, -1);
|
||||
if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
|
||||
lua_pop(L, 1); /* remove global */
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static int incomplete (int status) {
|
||||
if (status == LUA_ERRSYNTAX &&
|
||||
strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
|
||||
lua_pop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
lua_dofile(NULL); /* executes stdin as a file */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int result = 0;
|
||||
iolib_open ();
|
||||
strlib_open ();
|
||||
mathlib_open ();
|
||||
|
||||
lua_register("argv", lua_getargv);
|
||||
|
||||
if (argc < 2)
|
||||
manual_input();
|
||||
else
|
||||
{
|
||||
for (i=1; i<argc; i++)
|
||||
if (strcmp(argv[i], "--") == 0)
|
||||
{
|
||||
lua_argc = argc-i-1;
|
||||
lua_argv = argv+i;
|
||||
break;
|
||||
}
|
||||
for (i=1; i<argc; i++)
|
||||
{
|
||||
if (strcmp(argv[i], "--") == 0)
|
||||
break;
|
||||
else if (strcmp(argv[i], "-") == 0)
|
||||
manual_input();
|
||||
else if (strcmp(argv[i], "-v") == 0)
|
||||
printf("%s %s\n(written by %s)\n\n",
|
||||
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
|
||||
else
|
||||
{
|
||||
result = lua_dofile (argv[i]);
|
||||
if (result)
|
||||
fprintf(stderr, "lua: error trying to run file %s\n", argv[i]);
|
||||
}
|
||||
static int load_string (void) {
|
||||
int status;
|
||||
lua_settop(L, 0);
|
||||
if (lua_readline(L, get_prompt(1)) == 0) /* no input? */
|
||||
return -1;
|
||||
if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */
|
||||
lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */
|
||||
lua_remove(L, -2); /* remove original line */
|
||||
}
|
||||
}
|
||||
return result;
|
||||
for (;;) { /* repeat until gets a complete line */
|
||||
status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
|
||||
if (!incomplete(status)) break; /* cannot try to add lines? */
|
||||
if (lua_readline(L, get_prompt(0)) == 0) /* no more input? */
|
||||
return -1;
|
||||
lua_concat(L, lua_gettop(L)); /* join lines */
|
||||
}
|
||||
lua_saveline(L, lua_tostring(L, 1));
|
||||
lua_remove(L, 1); /* remove line */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static void manual_input (void) {
|
||||
int status;
|
||||
const char *oldprogname = progname;
|
||||
progname = NULL;
|
||||
while ((status = load_string()) != -1) {
|
||||
if (status == 0) status = lcall(0);
|
||||
report(status);
|
||||
if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
|
||||
lua_getglobal(L, "print");
|
||||
lua_insert(L, 1);
|
||||
if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
|
||||
l_message(progname, "error calling `print'");
|
||||
}
|
||||
}
|
||||
lua_settop(L, 0); /* clear stack */
|
||||
fputs("\n", stdout);
|
||||
progname = oldprogname;
|
||||
}
|
||||
|
||||
|
||||
static int handle_argv (char *argv[], int *interactive) {
|
||||
if (argv[1] == NULL) { /* no more arguments? */
|
||||
if (isatty(0)) {
|
||||
print_version();
|
||||
manual_input();
|
||||
}
|
||||
else
|
||||
file_input(NULL); /* executes stdin as a file */
|
||||
}
|
||||
else { /* other arguments; loop over them */
|
||||
int i;
|
||||
for (i = 1; argv[i] != NULL; i++) {
|
||||
if (argv[i][0] != '-') break; /* not an option? */
|
||||
switch (argv[i][1]) { /* option */
|
||||
case '-': { /* `--' */
|
||||
i++; /* skip this argument */
|
||||
goto endloop; /* stop handling arguments */
|
||||
}
|
||||
case '\0': {
|
||||
file_input(NULL); /* executes stdin as a file */
|
||||
break;
|
||||
}
|
||||
case 'i': {
|
||||
*interactive = 1;
|
||||
break;
|
||||
}
|
||||
case 'v': {
|
||||
print_version();
|
||||
break;
|
||||
}
|
||||
case 'e': {
|
||||
const char *chunk = argv[i] + 2;
|
||||
if (*chunk == '\0') chunk = argv[++i];
|
||||
if (chunk == NULL) {
|
||||
print_usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (dostring(chunk, "=<command line>") != 0)
|
||||
return EXIT_FAILURE;
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
const char *filename = argv[i] + 2;
|
||||
if (*filename == '\0') filename = argv[++i];
|
||||
if (filename == NULL) {
|
||||
print_usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (file_input(filename))
|
||||
return EXIT_FAILURE; /* stop if file fails */
|
||||
break;
|
||||
}
|
||||
case 'c': {
|
||||
l_message(progname, "option `-c' is deprecated");
|
||||
break;
|
||||
}
|
||||
case 's': {
|
||||
l_message(progname, "option `-s' is deprecated");
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
print_usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
} endloop:
|
||||
if (argv[i] != NULL) {
|
||||
const char *filename = argv[i];
|
||||
getargs(argv, i); /* collect remaining arguments */
|
||||
lua_setglobal(L, "arg");
|
||||
return file_input(filename); /* stop scanning arguments */
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static int openstdlibs (lua_State *l) {
|
||||
int res = 0;
|
||||
res += lua_baselibopen(l);
|
||||
res += lua_tablibopen(l);
|
||||
res += lua_iolibopen(l);
|
||||
res += lua_strlibopen(l);
|
||||
res += lua_mathlibopen(l);
|
||||
res += lua_dblibopen(l);
|
||||
/* add your libraries here */
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static int handle_luainit (void) {
|
||||
const char *init = getenv("LUA_INIT");
|
||||
if (init == NULL) return 0; /* status OK */
|
||||
else if (init[0] == '@')
|
||||
return file_input(init+1);
|
||||
else
|
||||
return dostring(init, "=LUA_INIT");
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
int status;
|
||||
int interactive = 0;
|
||||
(void)argc; /* to avoid warnings */
|
||||
progname = argv[0];
|
||||
L = lua_open(); /* create state */
|
||||
lua_atpanic(L, l_panic);
|
||||
lua_pop(L, lua_userinit(L)); /* open libraries, discard any results */
|
||||
status = handle_luainit();
|
||||
if (status != 0) return status;
|
||||
status = handle_argv(argv, &interactive);
|
||||
if (status == 0 && interactive) manual_input();
|
||||
lua_close(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
452
lua.h
452
lua.h
@@ -1,116 +1,382 @@
|
||||
/*
|
||||
** LUA - Linguagem para Usuarios de Aplicacao
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: lua.h,v 3.27 1996/04/25 14:10:00 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.153 2002/08/06 18:54:18 roberto Exp roberto $
|
||||
** Lua - An Extensible Extension Language
|
||||
** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
|
||||
** http://www.lua.org mailto:info@lua.org
|
||||
** See Copyright Notice at the end of this file
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
#define LUA_VERSION "Lua 2.4"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994, 1995 TeCGraf"
|
||||
|
||||
/* definition of `va_list' */
|
||||
#include <stdarg.h>
|
||||
|
||||
/* definition of `size_t' */
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
|
||||
#define LUA_VERSION "Lua 5.0 (alpha)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2002 Tecgraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
|
||||
|
||||
/* Private Part */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
LUA_T_NIL = -1,
|
||||
LUA_T_NUMBER = -2,
|
||||
LUA_T_STRING = -3,
|
||||
LUA_T_ARRAY = -4,
|
||||
LUA_T_FUNCTION = -5,
|
||||
LUA_T_CFUNCTION= -6,
|
||||
LUA_T_MARK = -7,
|
||||
LUA_T_CMARK = -8,
|
||||
LUA_T_LINE = -9,
|
||||
LUA_T_USERDATA = 0
|
||||
} lua_Type;
|
||||
|
||||
|
||||
/* Public Part */
|
||||
|
||||
#define LUA_NOOBJECT 0
|
||||
|
||||
typedef void (*lua_CFunction) (void);
|
||||
typedef unsigned int lua_Object;
|
||||
|
||||
lua_Object lua_setfallback (char *name, lua_CFunction fallback);
|
||||
|
||||
void lua_error (char *s);
|
||||
int lua_dofile (char *filename);
|
||||
int lua_dostring (char *string);
|
||||
int lua_callfunction (lua_Object function);
|
||||
int lua_call (char *funcname);
|
||||
|
||||
void lua_beginblock (void);
|
||||
void lua_endblock (void);
|
||||
|
||||
lua_Object lua_getparam (int number);
|
||||
#define lua_getresult(_) lua_getparam(_)
|
||||
|
||||
#define lua_isnil(_) (lua_type(_)==LUA_T_NIL)
|
||||
#define lua_istable(_) (lua_type(_)==LUA_T_ARRAY)
|
||||
#define lua_isuserdata(_) (lua_type(_)>=LUA_T_USERDATA)
|
||||
#define lua_iscfunction(_) (lua_type(_)==LUA_T_CFUNCTION)
|
||||
int lua_isnumber (lua_Object object);
|
||||
int lua_isstring (lua_Object object);
|
||||
int lua_isfunction (lua_Object object);
|
||||
|
||||
float lua_getnumber (lua_Object object);
|
||||
char *lua_getstring (lua_Object object);
|
||||
lua_CFunction lua_getcfunction (lua_Object object);
|
||||
void *lua_getuserdata (lua_Object object);
|
||||
|
||||
void lua_pushnil (void);
|
||||
void lua_pushnumber (float n);
|
||||
void lua_pushstring (char *s);
|
||||
void lua_pushcfunction (lua_CFunction fn);
|
||||
void lua_pushusertag (void *u, int tag);
|
||||
void lua_pushobject (lua_Object object);
|
||||
|
||||
lua_Object lua_getglobal (char *name);
|
||||
void lua_storeglobal (char *name);
|
||||
|
||||
void lua_storesubscript (void);
|
||||
lua_Object lua_getsubscript (void);
|
||||
|
||||
int lua_type (lua_Object object);
|
||||
/* option for multiple returns in `lua_pcall' and `lua_call' */
|
||||
#define LUA_MULTRET (-1)
|
||||
|
||||
|
||||
int lua_ref (int lock);
|
||||
lua_Object lua_getref (int ref);
|
||||
void lua_pushref (int ref);
|
||||
void lua_unref (int ref);
|
||||
|
||||
lua_Object lua_createtable (void);
|
||||
/*
|
||||
** pseudo-indices
|
||||
*/
|
||||
#define LUA_REGISTRYINDEX (-10000)
|
||||
#define LUA_GLOBALSINDEX (-10001)
|
||||
#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
|
||||
|
||||
|
||||
/* some useful macros */
|
||||
|
||||
#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))
|
||||
|
||||
#define lua_register(n,f) (lua_pushcfunction(f), lua_storeglobal(n))
|
||||
|
||||
#define lua_pushuserdata(u) lua_pushusertag(u, LUA_T_USERDATA)
|
||||
/* error codes for `lua_load' and `lua_pcall' */
|
||||
#define LUA_ERRRUN 1
|
||||
#define LUA_ERRFILE 2
|
||||
#define LUA_ERRSYNTAX 3
|
||||
#define LUA_ERRMEM 4
|
||||
#define LUA_ERRERR 5
|
||||
#define LUA_ERRTHROW 6
|
||||
|
||||
|
||||
/* for compatibility with old versions. Avoid using these macros */
|
||||
typedef struct lua_State lua_State;
|
||||
|
||||
#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
|
||||
typedef int (*lua_CFunction) (lua_State *L);
|
||||
|
||||
#define lua_pushliteral(o) lua_pushstring(o)
|
||||
|
||||
#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getsubscript())
|
||||
#define lua_getfield(o,f) (lua_pushobject(o), lua_pushliteral(f), lua_getsubscript())
|
||||
/*
|
||||
** functions that read blocks when loading Lua chunk
|
||||
*/
|
||||
typedef const char * (*lua_Chunkreader) (lua_State *L, void *ud, size_t *size);
|
||||
|
||||
|
||||
/*
|
||||
** basic types
|
||||
*/
|
||||
#define LUA_TNONE (-1)
|
||||
|
||||
#define LUA_TNIL 0
|
||||
#define LUA_TNUMBER 1
|
||||
#define LUA_TSTRING 2
|
||||
#define LUA_TBOOLEAN 3
|
||||
#define LUA_TTABLE 4
|
||||
#define LUA_TFUNCTION 5
|
||||
#define LUA_TUSERDATA 6
|
||||
#define LUA_TLIGHTUSERDATA 7
|
||||
|
||||
|
||||
/* 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
|
||||
|
||||
|
||||
/* type of Numbers in Lua */
|
||||
#ifndef LUA_NUMBER
|
||||
#define LUA_NUMBER double
|
||||
#endif
|
||||
typedef LUA_NUMBER lua_Number;
|
||||
|
||||
|
||||
/* mark for all API functions */
|
||||
#ifndef LUA_API
|
||||
#define LUA_API extern
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** state manipulation
|
||||
*/
|
||||
LUA_API lua_State *lua_open (void);
|
||||
LUA_API void lua_close (lua_State *L);
|
||||
LUA_API lua_State *lua_newthread (lua_State *L);
|
||||
LUA_API void lua_closethread (lua_State *L, lua_State *thread);
|
||||
|
||||
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);
|
||||
|
||||
|
||||
/*
|
||||
** 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 void lua_replace (lua_State *L, int index);
|
||||
LUA_API int lua_checkstack (lua_State *L, int size);
|
||||
|
||||
|
||||
/*
|
||||
** access functions (stack -> C)
|
||||
*/
|
||||
|
||||
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_type (lua_State *L, int index);
|
||||
LUA_API const char *lua_typename (lua_State *L, int type);
|
||||
|
||||
LUA_API int lua_equal (lua_State *L, int index1, int index2);
|
||||
LUA_API int lua_rawequal (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 int lua_toboolean (lua_State *L, int index);
|
||||
LUA_API const 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);
|
||||
|
||||
|
||||
/*
|
||||
** 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 char *s, size_t len);
|
||||
LUA_API void lua_pushstring (lua_State *L, const char *s);
|
||||
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
|
||||
va_list argp);
|
||||
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...);
|
||||
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
|
||||
LUA_API void lua_pushboolean (lua_State *L, int b);
|
||||
LUA_API void lua_pushlightuserdata (lua_State *L, void *p);
|
||||
|
||||
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
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_newtable (lua_State *L);
|
||||
LUA_API int lua_getmetatable (lua_State *L, int objindex);
|
||||
LUA_API const char *lua_getmode (lua_State *L, int index);
|
||||
LUA_API void lua_getglobals (lua_State *L, int index);
|
||||
|
||||
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
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_setmode (lua_State *L, int index, const char *mode);
|
||||
LUA_API int lua_setmetatable (lua_State *L, int objindex);
|
||||
LUA_API int lua_setglobals (lua_State *L, int index);
|
||||
|
||||
|
||||
/*
|
||||
** `load' and `call' functions (load and run Lua code)
|
||||
*/
|
||||
LUA_API void lua_call (lua_State *L, int nargs, int nresults);
|
||||
LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
|
||||
LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
|
||||
const char *chunkname);
|
||||
|
||||
|
||||
/*
|
||||
** coroutine functions
|
||||
*/
|
||||
LUA_API void lua_cobegin (lua_State *L, int nargs);
|
||||
LUA_API int lua_yield (lua_State *L, int nresults);
|
||||
LUA_API int lua_resume (lua_State *L, lua_State *co);
|
||||
|
||||
/*
|
||||
** 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_error (lua_State *L);
|
||||
|
||||
LUA_API int lua_next (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);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** some useful macros
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#define lua_boxpointer(L,u) \
|
||||
(*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
|
||||
|
||||
#define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i)))
|
||||
|
||||
#define lua_pop(L,n) lua_settop(L, -(n)-1)
|
||||
|
||||
#define lua_register(L,n,f) \
|
||||
(lua_pushstring(L, n), \
|
||||
lua_pushcfunction(L, f), \
|
||||
lua_settable(L, LUA_GLOBALSINDEX))
|
||||
|
||||
#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
|
||||
|
||||
#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
|
||||
#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
|
||||
#define lua_isuserdata(L,n) (lua_type(L,n) >= LUA_TUSERDATA)
|
||||
#define lua_islightuserdata(L,n) (lua_type(L,n) == LUA_TLIGHTUSERDATA)
|
||||
#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
|
||||
#define lua_isboolean(L,n) (lua_type(L,n) == LUA_TBOOLEAN)
|
||||
#define lua_isnone(L,n) (lua_type(L,n) == LUA_TNONE)
|
||||
#define lua_isnoneornil(L, n) (lua_type(L,n) <= 0)
|
||||
|
||||
#define lua_pushliteral(L, s) \
|
||||
lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** compatibility macros and functions
|
||||
*/
|
||||
|
||||
|
||||
LUA_API int lua_pushupvalues (lua_State *L);
|
||||
|
||||
#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
|
||||
#define lua_setglobal(L,s) \
|
||||
(lua_pushstring(L, s), lua_insert(L, -2), lua_settable(L, LUA_GLOBALSINDEX))
|
||||
|
||||
#define lua_getglobal(L,s) \
|
||||
(lua_pushstring(L, s), lua_gettable(L, LUA_GLOBALSINDEX))
|
||||
|
||||
#define lua_isnull lua_isnone
|
||||
|
||||
|
||||
/* compatibility with ref system */
|
||||
|
||||
/* pre-defined references */
|
||||
#define LUA_NOREF (-2)
|
||||
#define LUA_REFNIL (-1)
|
||||
|
||||
#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
|
||||
(lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))
|
||||
|
||||
#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
|
||||
|
||||
#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================================
|
||||
** useful definitions for Lua kernel and libraries
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
/* formats for Lua numbers */
|
||||
#ifndef LUA_NUMBER_SCAN
|
||||
#define LUA_NUMBER_SCAN "%lf"
|
||||
#endif
|
||||
|
||||
#ifndef LUA_NUMBER_FMT
|
||||
#define LUA_NUMBER_FMT "%.16g"
|
||||
#endif
|
||||
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================================
|
||||
** Debug API
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
typedef enum lua_Hookevent {
|
||||
LUA_HOOKCALL, LUA_HOOKRET, LUA_HOOKLINE, LUA_HOOKCOUNT
|
||||
} lua_Hookevent;
|
||||
|
||||
|
||||
#define LUA_MASKCALL (2 << LUA_HOOKCALL)
|
||||
#define LUA_MASKRET (2 << LUA_HOOKRET)
|
||||
#define LUA_MASKLINE (2 << LUA_HOOKLINE)
|
||||
#define LUA_MASKCOUNT(count) ((unsigned long)(count) << 8)
|
||||
#define lua_getmaskcount(mask) ((mask) >> 8)
|
||||
|
||||
#define LUA_MAXCOUNT ((1<<24) - 1)
|
||||
|
||||
typedef struct lua_Debug lua_Debug; /* activation record */
|
||||
|
||||
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
||||
|
||||
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
|
||||
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
|
||||
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
|
||||
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
|
||||
|
||||
LUA_API int lua_sethook (lua_State *L, lua_Hook func, unsigned long mask);
|
||||
LUA_API lua_Hook lua_gethook (lua_State *L);
|
||||
LUA_API unsigned long lua_gethookmask (lua_State *L);
|
||||
|
||||
|
||||
#define LUA_IDSIZE 60
|
||||
|
||||
struct lua_Debug {
|
||||
lua_Hookevent event;
|
||||
const char *name; /* (n) */
|
||||
const char *namewhat; /* (n) `global', `local', `field', `method' */
|
||||
const char *what; /* (S) `Lua' function, `C' function, Lua `main' */
|
||||
const char *source; /* (S) */
|
||||
int currentline; /* (l) */
|
||||
int nups; /* (u) number of upvalues */
|
||||
int linedefined; /* (S) */
|
||||
char short_src[LUA_IDSIZE]; /* (S) */
|
||||
/* private part */
|
||||
int i_ci; /* active function */
|
||||
};
|
||||
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (C) 2002 Tecgraf, PUC-Rio. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#define lua_copystring(o) (strdup(lua_getstring(o)))
|
||||
|
||||
#endif
|
||||
|
||||
788
lua.stx
788
lua.stx
@@ -1,788 +0,0 @@
|
||||
%{
|
||||
|
||||
char *rcs_luastx = "$Id: lua.stx,v 3.35 1996/03/08 12:02:37 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "luadebug.h"
|
||||
#include "mem.h"
|
||||
#include "lex.h"
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "tree.h"
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
#include "func.h"
|
||||
|
||||
/* to avoid warnings generated by yacc */
|
||||
int yyparse (void);
|
||||
#define malloc luaI_malloc
|
||||
#define realloc luaI_realloc
|
||||
#define free luaI_free
|
||||
|
||||
#ifndef LISTING
|
||||
#define LISTING 0
|
||||
#endif
|
||||
|
||||
#ifndef CODE_BLOCK
|
||||
#define CODE_BLOCK 256
|
||||
#endif
|
||||
static int maxcode;
|
||||
static int maxmain;
|
||||
static int maxcurr;
|
||||
static Byte *funcCode = NULL;
|
||||
static Byte **initcode;
|
||||
static Byte *basepc;
|
||||
static int maincode;
|
||||
static int pc;
|
||||
|
||||
|
||||
#define MAXVAR 32
|
||||
static Long varbuffer[MAXVAR]; /* variables in an assignment list;
|
||||
it's long to store negative Word values */
|
||||
static int nvarbuffer=0; /* number of variables at a list */
|
||||
|
||||
#define MAXLOCALS 32
|
||||
static TaggedString *localvar[MAXLOCALS]; /* store local variable names */
|
||||
static int nlocalvar=0; /* number of local variables */
|
||||
|
||||
#define MAXFIELDS FIELDS_PER_FLUSH*2
|
||||
static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
|
||||
static int nfields=0;
|
||||
|
||||
int lua_debug = 0;
|
||||
|
||||
/* Internal functions */
|
||||
|
||||
static void yyerror (char *s)
|
||||
{
|
||||
char msg[256];
|
||||
char *token = lua_lasttext();
|
||||
if (token[0] == 0)
|
||||
token = "<eof>";
|
||||
sprintf (msg,"%s; last token read: \"%s\" at line %d in file `%s'",
|
||||
s, token, lua_linenumber, lua_parsedfile);
|
||||
lua_error (msg);
|
||||
}
|
||||
|
||||
static void code_byte (Byte c)
|
||||
{
|
||||
if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
|
||||
maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT);
|
||||
basepc[pc++] = c;
|
||||
}
|
||||
|
||||
static void code_word (Word n)
|
||||
{
|
||||
CodeWord code;
|
||||
code.w = n;
|
||||
code_byte(code.m.c1);
|
||||
code_byte(code.m.c2);
|
||||
}
|
||||
|
||||
static void code_float (float n)
|
||||
{
|
||||
CodeFloat code;
|
||||
code.f = n;
|
||||
code_byte(code.m.c1);
|
||||
code_byte(code.m.c2);
|
||||
code_byte(code.m.c3);
|
||||
code_byte(code.m.c4);
|
||||
}
|
||||
|
||||
static void code_code (TFunc *tf)
|
||||
{
|
||||
CodeCode code;
|
||||
code.tf = tf;
|
||||
code_byte(code.m.c1);
|
||||
code_byte(code.m.c2);
|
||||
code_byte(code.m.c3);
|
||||
code_byte(code.m.c4);
|
||||
}
|
||||
|
||||
static void code_word_at (Byte *p, int n)
|
||||
{
|
||||
CodeWord code;
|
||||
if ((Word)n != n)
|
||||
yyerror("block too big");
|
||||
code.w = (Word)n;
|
||||
*p++ = code.m.c1;
|
||||
*p++ = code.m.c2;
|
||||
}
|
||||
|
||||
static void push_field (Word name)
|
||||
{
|
||||
if (nfields < MAXFIELDS)
|
||||
fields[nfields++] = name;
|
||||
else
|
||||
yyerror ("too many fields in nested constructors");
|
||||
}
|
||||
|
||||
static void flush_record (int n)
|
||||
{
|
||||
int i;
|
||||
if (n == 0) return;
|
||||
code_byte(STORERECORD);
|
||||
code_byte(n);
|
||||
for (i=0; i<n; i++)
|
||||
code_word(fields[--nfields]);
|
||||
}
|
||||
|
||||
static void flush_list (int m, int n)
|
||||
{
|
||||
if (n == 0) return;
|
||||
if (m == 0)
|
||||
code_byte(STORELIST0);
|
||||
else
|
||||
if (m < 255)
|
||||
{
|
||||
code_byte(STORELIST);
|
||||
code_byte(m);
|
||||
}
|
||||
else
|
||||
yyerror ("list constructor too long");
|
||||
code_byte(n);
|
||||
}
|
||||
|
||||
static void store_localvar (TaggedString *name, int n)
|
||||
{
|
||||
if (nlocalvar+n < MAXLOCALS)
|
||||
localvar[nlocalvar+n] = name;
|
||||
else
|
||||
yyerror ("too many local variables");
|
||||
if (lua_debug)
|
||||
luaI_registerlocalvar(name, lua_linenumber);
|
||||
}
|
||||
|
||||
static void add_localvar (TaggedString *name)
|
||||
{
|
||||
store_localvar(name, 0);
|
||||
nlocalvar++;
|
||||
}
|
||||
|
||||
static void add_varbuffer (Long var)
|
||||
{
|
||||
if (nvarbuffer < MAXVAR)
|
||||
varbuffer[nvarbuffer++] = var;
|
||||
else
|
||||
yyerror ("variable buffer overflow");
|
||||
}
|
||||
|
||||
static void code_number (float f)
|
||||
{
|
||||
Word i = (Word)f;
|
||||
if (f == (float)i) /* f has an (short) integer value */
|
||||
{
|
||||
if (i <= 2) code_byte(PUSH0 + i);
|
||||
else if (i <= 255)
|
||||
{
|
||||
code_byte(PUSHBYTE);
|
||||
code_byte(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
code_byte(PUSHWORD);
|
||||
code_word(i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
code_byte(PUSHFLOAT);
|
||||
code_float(f);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Search a local name and if find return its index. If do not find return -1
|
||||
*/
|
||||
static int lua_localname (TaggedString *n)
|
||||
{
|
||||
int i;
|
||||
for (i=nlocalvar-1; i >= 0; i--)
|
||||
if (n == localvar[i]) return i; /* local var */
|
||||
return -1; /* global var */
|
||||
}
|
||||
|
||||
/*
|
||||
** Push a variable given a number. If number is positive, push global variable
|
||||
** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
|
||||
** Otherwise, if zero, push indexed variable (record).
|
||||
*/
|
||||
static void lua_pushvar (Long number)
|
||||
{
|
||||
if (number > 0) /* global var */
|
||||
{
|
||||
code_byte(PUSHGLOBAL);
|
||||
code_word(number-1);
|
||||
}
|
||||
else if (number < 0) /* local var */
|
||||
{
|
||||
number = (-number) - 1;
|
||||
if (number < 10) code_byte(PUSHLOCAL0 + number);
|
||||
else
|
||||
{
|
||||
code_byte(PUSHLOCAL);
|
||||
code_byte(number);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
code_byte(PUSHINDEXED);
|
||||
}
|
||||
}
|
||||
|
||||
static void lua_codeadjust (int n)
|
||||
{
|
||||
if (n+nlocalvar == 0)
|
||||
code_byte(ADJUST0);
|
||||
else
|
||||
{
|
||||
code_byte(ADJUST);
|
||||
code_byte(n+nlocalvar);
|
||||
}
|
||||
}
|
||||
|
||||
static void change2main (void)
|
||||
{
|
||||
/* (re)store main values */
|
||||
pc=maincode; basepc=*initcode; maxcurr=maxmain;
|
||||
nlocalvar=0;
|
||||
}
|
||||
|
||||
static void savemain (void)
|
||||
{
|
||||
/* save main values */
|
||||
maincode=pc; *initcode=basepc; maxmain=maxcurr;
|
||||
}
|
||||
|
||||
static void init_func (void)
|
||||
{
|
||||
if (funcCode == NULL) /* first function */
|
||||
{
|
||||
funcCode = newvector(CODE_BLOCK, Byte);
|
||||
maxcode = CODE_BLOCK;
|
||||
}
|
||||
savemain(); /* save main values */
|
||||
/* set func values */
|
||||
pc=0; basepc=funcCode; maxcurr=maxcode;
|
||||
nlocalvar = 0;
|
||||
luaI_codedebugline(lua_linenumber);
|
||||
}
|
||||
|
||||
static void codereturn (void)
|
||||
{
|
||||
if (nlocalvar == 0)
|
||||
code_byte(RETCODE0);
|
||||
else
|
||||
{
|
||||
code_byte(RETCODE);
|
||||
code_byte(nlocalvar);
|
||||
}
|
||||
}
|
||||
|
||||
void luaI_codedebugline (int line)
|
||||
{
|
||||
static int lastline = 0;
|
||||
if (lua_debug && line != lastline)
|
||||
{
|
||||
code_byte(SETLINE);
|
||||
code_word(line);
|
||||
lastline = line;
|
||||
}
|
||||
}
|
||||
|
||||
static int adjust_functioncall (Long exp, int i)
|
||||
{
|
||||
if (exp <= 0)
|
||||
return -exp; /* exp is -list length */
|
||||
else
|
||||
{
|
||||
int temp = basepc[exp];
|
||||
basepc[exp] = i;
|
||||
return temp+i;
|
||||
}
|
||||
}
|
||||
|
||||
static void adjust_mult_assign (int vars, Long exps, int temps)
|
||||
{
|
||||
if (exps > 0)
|
||||
{ /* must correct function call */
|
||||
int diff = vars - basepc[exps];
|
||||
if (diff >= 0)
|
||||
adjust_functioncall(exps, diff);
|
||||
else
|
||||
{
|
||||
adjust_functioncall(exps, 0);
|
||||
lua_codeadjust(temps);
|
||||
}
|
||||
}
|
||||
else if (vars != -exps)
|
||||
lua_codeadjust(temps);
|
||||
}
|
||||
|
||||
static void storesinglevar (Long v)
|
||||
{
|
||||
if (v > 0) /* global var */
|
||||
{
|
||||
code_byte(STOREGLOBAL);
|
||||
code_word(v-1);
|
||||
}
|
||||
else if (v < 0) /* local var */
|
||||
{
|
||||
int number = (-v) - 1;
|
||||
if (number < 10) code_byte(STORELOCAL0 + number);
|
||||
else
|
||||
{
|
||||
code_byte(STORELOCAL);
|
||||
code_byte(number);
|
||||
}
|
||||
}
|
||||
else
|
||||
code_byte(STOREINDEXED0);
|
||||
}
|
||||
|
||||
static void lua_codestore (int i)
|
||||
{
|
||||
if (varbuffer[i] != 0) /* global or local var */
|
||||
storesinglevar(varbuffer[i]);
|
||||
else /* indexed var */
|
||||
{
|
||||
int j;
|
||||
int upper=0; /* number of indexed variables upper */
|
||||
int param; /* number of itens until indexed expression */
|
||||
for (j=i+1; j <nvarbuffer; j++)
|
||||
if (varbuffer[j] == 0) upper++;
|
||||
param = upper*2 + i;
|
||||
if (param == 0)
|
||||
code_byte(STOREINDEXED0);
|
||||
else
|
||||
{
|
||||
code_byte(STOREINDEXED);
|
||||
code_byte(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void codeIf (Long thenAdd, Long elseAdd)
|
||||
{
|
||||
Long elseinit = elseAdd+sizeof(Word)+1;
|
||||
if (pc == elseinit) /* no else */
|
||||
{
|
||||
pc -= sizeof(Word)+1;
|
||||
elseinit = pc;
|
||||
}
|
||||
else
|
||||
{
|
||||
basepc[elseAdd] = JMP;
|
||||
code_word_at(basepc+elseAdd+1, pc-elseinit);
|
||||
}
|
||||
basepc[thenAdd] = IFFJMP;
|
||||
code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Parse LUA code.
|
||||
*/
|
||||
void lua_parse (TFunc *tf)
|
||||
{
|
||||
initcode = &(tf->code);
|
||||
*initcode = newvector(CODE_BLOCK, Byte);
|
||||
maincode = 0;
|
||||
maxmain = CODE_BLOCK;
|
||||
change2main();
|
||||
if (yyparse ()) lua_error("parse error");
|
||||
savemain();
|
||||
(*initcode)[maincode++] = RETCODE0;
|
||||
tf->size = maincode;
|
||||
#if LISTING
|
||||
{ static void PrintCode (Byte *c, Byte *end);
|
||||
PrintCode(*initcode,*initcode+maincode); }
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
%}
|
||||
|
||||
|
||||
%union
|
||||
{
|
||||
int vInt;
|
||||
float vFloat;
|
||||
char *pChar;
|
||||
Word vWord;
|
||||
Long vLong;
|
||||
TFunc *pFunc;
|
||||
TaggedString *pTStr;
|
||||
}
|
||||
|
||||
%start functionlist
|
||||
|
||||
%token WRONGTOKEN
|
||||
%token NIL
|
||||
%token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
|
||||
%token RETURN
|
||||
%token LOCAL
|
||||
%token FUNCTION
|
||||
%token <vFloat> NUMBER
|
||||
%token <vWord> STRING
|
||||
%token <pTStr> NAME
|
||||
%token <vInt> DEBUG
|
||||
|
||||
%type <vLong> PrepJump
|
||||
%type <vLong> exprlist, exprlist1 /* if > 0, points to function return
|
||||
counter (which has list length); if <= 0, -list lenght */
|
||||
%type <vLong> functioncall, expr /* if != 0, points to function return
|
||||
counter */
|
||||
%type <vInt> varlist1, funcParams, funcvalue
|
||||
%type <vInt> fieldlist, localdeclist, decinit
|
||||
%type <vInt> ffieldlist, ffieldlist1, semicolonpart
|
||||
%type <vInt> lfieldlist, lfieldlist1
|
||||
%type <vInt> parlist
|
||||
%type <vLong> var, singlevar, funcname
|
||||
%type <pFunc> body
|
||||
|
||||
%left AND OR
|
||||
%left EQ NE '>' '<' LE GE
|
||||
%left CONC
|
||||
%left '+' '-'
|
||||
%left '*' '/'
|
||||
%left UNARY NOT
|
||||
%right '^'
|
||||
|
||||
|
||||
%% /* beginning of rules section */
|
||||
|
||||
|
||||
functionlist : /* empty */
|
||||
| functionlist globalstat
|
||||
| functionlist function
|
||||
;
|
||||
|
||||
globalstat : stat sc
|
||||
| setdebug
|
||||
;
|
||||
|
||||
function : FUNCTION funcname body
|
||||
{
|
||||
code_byte(PUSHFUNCTION);
|
||||
code_code($3);
|
||||
storesinglevar($2);
|
||||
}
|
||||
;
|
||||
|
||||
funcname : var { $$ =$1; init_func(); }
|
||||
| varexp ':' NAME
|
||||
{
|
||||
code_byte(PUSHSTRING);
|
||||
code_word(luaI_findconstant($3));
|
||||
$$ = 0; /* indexed variable */
|
||||
init_func();
|
||||
add_localvar(luaI_createfixedstring("self"));
|
||||
}
|
||||
;
|
||||
|
||||
body : '(' parlist ')' block END
|
||||
{
|
||||
codereturn();
|
||||
$$ = new(TFunc);
|
||||
luaI_initTFunc($$);
|
||||
$$->size = pc;
|
||||
$$->code = newvector(pc, Byte);
|
||||
$$->fileName = lua_parsedfile;
|
||||
$$->lineDefined = $2;
|
||||
memcpy($$->code, basepc, pc*sizeof(Byte));
|
||||
if (lua_debug)
|
||||
luaI_closelocalvars($$);
|
||||
/* save func values */
|
||||
funcCode = basepc; maxcode=maxcurr;
|
||||
#if LISTING
|
||||
PrintCode(funcCode,funcCode+pc);
|
||||
#endif
|
||||
change2main(); /* change back to main code */
|
||||
}
|
||||
;
|
||||
|
||||
statlist : /* empty */
|
||||
| statlist stat sc
|
||||
;
|
||||
|
||||
sc : /* empty */ | ';' ;
|
||||
|
||||
stat : IF expr1 THEN PrepJump block PrepJump elsepart END
|
||||
{ codeIf($4, $6); }
|
||||
|
||||
| WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
|
||||
{
|
||||
basepc[$5] = IFFJMP;
|
||||
code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
|
||||
basepc[$7] = UPJMP;
|
||||
code_word_at(basepc+$7+1, pc - ($<vLong>2));
|
||||
}
|
||||
|
||||
| REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump
|
||||
{
|
||||
basepc[$6] = IFFUPJMP;
|
||||
code_word_at(basepc+$6+1, pc - ($<vLong>2));
|
||||
}
|
||||
|
||||
| varlist1 '=' exprlist1
|
||||
{
|
||||
{
|
||||
int i;
|
||||
adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
|
||||
for (i=nvarbuffer-1; i>=0; i--)
|
||||
lua_codestore (i);
|
||||
if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
|
||||
lua_codeadjust (0);
|
||||
}
|
||||
}
|
||||
| functioncall
|
||||
| LOCAL localdeclist decinit
|
||||
{ nlocalvar += $2;
|
||||
adjust_mult_assign($2, $3, 0);
|
||||
}
|
||||
;
|
||||
|
||||
elsepart : /* empty */
|
||||
| ELSE block
|
||||
| ELSEIF expr1 THEN PrepJump block PrepJump elsepart
|
||||
{ codeIf($4, $6); }
|
||||
;
|
||||
|
||||
block : {$<vInt>$ = nlocalvar;} statlist ret
|
||||
{
|
||||
if (nlocalvar != $<vInt>1)
|
||||
{
|
||||
if (lua_debug)
|
||||
for (; nlocalvar > $<vInt>1; nlocalvar--)
|
||||
luaI_unregisterlocalvar(lua_linenumber);
|
||||
else
|
||||
nlocalvar = $<vInt>1;
|
||||
lua_codeadjust (0);
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
ret : /* empty */
|
||||
| RETURN exprlist sc
|
||||
{
|
||||
adjust_functioncall($2, MULT_RET);
|
||||
codereturn();
|
||||
}
|
||||
;
|
||||
|
||||
PrepJump : /* empty */
|
||||
{
|
||||
$$ = pc;
|
||||
code_byte(0); /* open space */
|
||||
code_word (0);
|
||||
}
|
||||
|
||||
expr1 : expr { adjust_functioncall($1, 1); }
|
||||
;
|
||||
|
||||
expr : '(' expr ')' { $$ = $2; }
|
||||
| expr1 EQ expr1 { code_byte(EQOP); $$ = 0; }
|
||||
| expr1 '<' expr1 { code_byte(LTOP); $$ = 0; }
|
||||
| expr1 '>' expr1 { code_byte(GTOP); $$ = 0; }
|
||||
| expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 0; }
|
||||
| expr1 LE expr1 { code_byte(LEOP); $$ = 0; }
|
||||
| expr1 GE expr1 { code_byte(GEOP); $$ = 0; }
|
||||
| expr1 '+' expr1 { code_byte(ADDOP); $$ = 0; }
|
||||
| expr1 '-' expr1 { code_byte(SUBOP); $$ = 0; }
|
||||
| expr1 '*' expr1 { code_byte(MULTOP); $$ = 0; }
|
||||
| expr1 '/' expr1 { code_byte(DIVOP); $$ = 0; }
|
||||
| expr1 '^' expr1 { code_byte(POWOP); $$ = 0; }
|
||||
| expr1 CONC expr1 { code_byte(CONCOP); $$ = 0; }
|
||||
| '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 0;}
|
||||
| table { $$ = 0; }
|
||||
| varexp { $$ = 0;}
|
||||
| NUMBER { code_number($1); $$ = 0; }
|
||||
| STRING
|
||||
{
|
||||
code_byte(PUSHSTRING);
|
||||
code_word($1);
|
||||
$$ = 0;
|
||||
}
|
||||
| NIL {code_byte(PUSHNIL); $$ = 0; }
|
||||
| functioncall { $$ = $1; }
|
||||
| NOT expr1 { code_byte(NOTOP); $$ = 0;}
|
||||
| expr1 AND PrepJump {code_byte(POP); } expr1
|
||||
{
|
||||
basepc[$3] = ONFJMP;
|
||||
code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
|
||||
$$ = 0;
|
||||
}
|
||||
| expr1 OR PrepJump {code_byte(POP); } expr1
|
||||
{
|
||||
basepc[$3] = ONTJMP;
|
||||
code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
|
||||
$$ = 0;
|
||||
}
|
||||
;
|
||||
|
||||
table :
|
||||
{
|
||||
code_byte(CREATEARRAY);
|
||||
$<vLong>$ = pc; code_word(0);
|
||||
}
|
||||
'{' fieldlist '}'
|
||||
{
|
||||
code_word_at(basepc+$<vLong>1, $3);
|
||||
}
|
||||
;
|
||||
|
||||
functioncall : funcvalue funcParams
|
||||
{
|
||||
code_byte(CALLFUNC);
|
||||
code_byte($1+$2);
|
||||
$$ = pc;
|
||||
code_byte(0); /* may be modified by other rules */
|
||||
}
|
||||
;
|
||||
|
||||
funcvalue : varexp { $$ = 0; }
|
||||
| varexp ':' NAME
|
||||
{
|
||||
code_byte(PUSHSELF);
|
||||
code_word(luaI_findconstant($3));
|
||||
$$ = 1;
|
||||
}
|
||||
;
|
||||
|
||||
funcParams : '(' exprlist ')'
|
||||
{ $$ = adjust_functioncall($2, 1); }
|
||||
| table { $$ = 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
|
||||
{
|
||||
adjust_functioncall($4, $<vLong>3);
|
||||
$$ = $4;
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
parlist : /* empty */ { lua_codeadjust(0); $$ = lua_linenumber; }
|
||||
| parlist1 { lua_codeadjust(0); $$ = lua_linenumber; }
|
||||
;
|
||||
|
||||
parlist1 : NAME { add_localvar($1); }
|
||||
| parlist1 ',' NAME { add_localvar($3); }
|
||||
;
|
||||
|
||||
fieldlist : lfieldlist
|
||||
{ flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
|
||||
semicolonpart
|
||||
{ $$ = $1+$3; }
|
||||
| ffieldlist1 lastcomma
|
||||
{ $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
|
||||
;
|
||||
|
||||
semicolonpart : /* empty */
|
||||
{ $$ = 0; }
|
||||
| ';' ffieldlist
|
||||
{ $$ = $2; flush_record($2%FIELDS_PER_FLUSH); }
|
||||
;
|
||||
|
||||
lastcomma : /* empty */
|
||||
| ','
|
||||
;
|
||||
|
||||
ffieldlist : /* empty */ { $$ = 0; }
|
||||
| ffieldlist1 lastcomma { $$ = $1; }
|
||||
;
|
||||
|
||||
ffieldlist1 : ffield {$$=1;}
|
||||
| ffieldlist1 ',' ffield
|
||||
{
|
||||
$$=$1+1;
|
||||
if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
|
||||
}
|
||||
;
|
||||
|
||||
ffield : NAME '=' expr1
|
||||
{
|
||||
push_field(luaI_findconstant($1));
|
||||
}
|
||||
;
|
||||
|
||||
lfieldlist : /* empty */ { $$ = 0; }
|
||||
| lfieldlist1 lastcomma { $$ = $1; }
|
||||
;
|
||||
|
||||
lfieldlist1 : expr1 {$$=1;}
|
||||
| lfieldlist1 ',' expr1
|
||||
{
|
||||
$$=$1+1;
|
||||
if ($$%FIELDS_PER_FLUSH == 0)
|
||||
flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
|
||||
}
|
||||
;
|
||||
|
||||
varlist1 : var
|
||||
{
|
||||
nvarbuffer = 0;
|
||||
add_varbuffer($1);
|
||||
$$ = ($1 == 0) ? 1 : 0;
|
||||
}
|
||||
| varlist1 ',' var
|
||||
{
|
||||
add_varbuffer($3);
|
||||
$$ = ($3 == 0) ? $1 + 1 : $1;
|
||||
}
|
||||
;
|
||||
|
||||
var : singlevar { $$ = $1; }
|
||||
| varexp '[' expr1 ']'
|
||||
{
|
||||
$$ = 0; /* indexed variable */
|
||||
}
|
||||
| varexp '.' NAME
|
||||
{
|
||||
code_byte(PUSHSTRING);
|
||||
code_word(luaI_findconstant($3));
|
||||
$$ = 0; /* indexed variable */
|
||||
}
|
||||
;
|
||||
|
||||
singlevar : NAME
|
||||
{
|
||||
int local = lua_localname($1);
|
||||
if (local == -1) /* global var */
|
||||
$$ = luaI_findsymbol($1)+1; /* return positive value */
|
||||
else
|
||||
$$ = -(local+1); /* return negative value */
|
||||
}
|
||||
;
|
||||
|
||||
varexp : var { lua_pushvar($1); }
|
||||
;
|
||||
|
||||
localdeclist : NAME {store_localvar($1, 0); $$ = 1;}
|
||||
| localdeclist ',' NAME
|
||||
{
|
||||
store_localvar($3, $1);
|
||||
$$ = $1+1;
|
||||
}
|
||||
;
|
||||
|
||||
decinit : /* empty */ { $$ = 0; }
|
||||
| '=' exprlist1 { $$ = $2; }
|
||||
;
|
||||
|
||||
setdebug : DEBUG { lua_debug = $1; }
|
||||
;
|
||||
|
||||
%%
|
||||
31
luadebug.h
31
luadebug.h
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
** LUA - Linguagem para Usuarios de Aplicacao
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: luadebug.h,v 1.5 1996/02/08 17:03:20 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef luadebug_h
|
||||
#define luadebug_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
typedef lua_Object lua_Function;
|
||||
|
||||
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);
|
||||
|
||||
extern lua_LHFunction lua_linehook;
|
||||
extern lua_CHFunction lua_callhook;
|
||||
extern int lua_debug;
|
||||
|
||||
#endif
|
||||
55
lualib.h
55
lualib.h
@@ -1,33 +1,44 @@
|
||||
/*
|
||||
** Libraries to be used in LUA programs
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: lualib.h,v 1.7 1996/03/14 15:53:09 roberto Exp roberto $
|
||||
** $Id: lualib.h,v 1.24 2002/06/18 17:42:52 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lualib_h
|
||||
#define lualib_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
void iolib_open (void);
|
||||
void strlib_open (void);
|
||||
void mathlib_open (void);
|
||||
|
||||
|
||||
/* auxiliar functions (private) */
|
||||
|
||||
struct lua_reg {
|
||||
char *name;
|
||||
lua_CFunction func;
|
||||
};
|
||||
|
||||
void luaI_openlib (struct lua_reg *l, int n);
|
||||
void lua_arg_error(char *funcname);
|
||||
char *lua_check_string (int numArg, char *funcname);
|
||||
double lua_check_number (int numArg, char *funcname);
|
||||
char *luaI_addchar (int c);
|
||||
void luaI_addquoted (char *s);
|
||||
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API extern
|
||||
#endif
|
||||
|
||||
|
||||
#define LUA_COLIBNAME "coroutine"
|
||||
LUALIB_API int lua_baselibopen (lua_State *L);
|
||||
|
||||
#define LUA_TABLIBNAME "table"
|
||||
LUALIB_API int lua_tablibopen (lua_State *L);
|
||||
|
||||
#define LUA_IOLIBNAME "io"
|
||||
#define LUA_OSLIBNAME "os"
|
||||
LUALIB_API int lua_iolibopen (lua_State *L);
|
||||
|
||||
#define LUA_STRLIBNAME "string"
|
||||
LUALIB_API int lua_strlibopen (lua_State *L);
|
||||
|
||||
#define LUA_MATHLIBNAME "math"
|
||||
LUALIB_API int lua_mathlibopen (lua_State *L);
|
||||
|
||||
#define LUA_DBLIBNAME "debug"
|
||||
LUALIB_API int lua_dblibopen (lua_State *L);
|
||||
|
||||
|
||||
/* to help testing the libraries */
|
||||
#ifndef lua_assert
|
||||
#define lua_assert(c) /* empty */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
67
luamem.c
67
luamem.c
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
** mem.c
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_mem = "$Id: mem.c,v 1.11 1996/03/21 18:54:29 roberto Exp roberto $";
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "mem.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define mem_error() lua_error(memEM)
|
||||
|
||||
|
||||
void luaI_free (void *block)
|
||||
{
|
||||
if (block)
|
||||
{
|
||||
*((int *)block) = -1; /* to catch errors */
|
||||
free(block);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void *luaI_malloc (unsigned long size)
|
||||
{
|
||||
void *block = malloc((size_t)size);
|
||||
if (block == NULL)
|
||||
mem_error();
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
void *luaI_realloc (void *oldblock, unsigned long size)
|
||||
{
|
||||
void *block = oldblock ? realloc(oldblock, (size_t)size) :
|
||||
malloc((size_t)size);
|
||||
if (block == NULL)
|
||||
mem_error();
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
int luaI_growvector (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit)
|
||||
{
|
||||
if (nelems >= limit)
|
||||
lua_error(errormsg);
|
||||
nelems = (nelems == 0) ? 20 : nelems*2;
|
||||
if (nelems > limit)
|
||||
nelems = limit;
|
||||
*block = luaI_realloc(*block, nelems*size);
|
||||
return (int) nelems;
|
||||
}
|
||||
|
||||
|
||||
void* luaI_buffer (unsigned long size)
|
||||
{
|
||||
static unsigned long buffsize = 0;
|
||||
static char* buffer = NULL;
|
||||
if (size > buffsize)
|
||||
buffer = luaI_realloc(buffer, buffsize=size);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
39
luamem.h
39
luamem.h
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
** mem.c
|
||||
** memory manager for lua
|
||||
** $Id: mem.h,v 1.6 1996/03/21 18:54:29 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef mem_h
|
||||
#define mem_h
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
|
||||
/* memory error messages */
|
||||
#define codeEM "code size overflow"
|
||||
#define symbolEM "symbol table overflow"
|
||||
#define constantEM "constant table overflow"
|
||||
#define stackEM "stack size overflow"
|
||||
#define lexEM "lex buffer overflow"
|
||||
#define refEM "reference table overflow"
|
||||
#define tableEM "table overflow"
|
||||
#define memEM "not enough memory"
|
||||
|
||||
|
||||
void luaI_free (void *block);
|
||||
void *luaI_malloc (unsigned long size);
|
||||
void *luaI_realloc (void *oldblock, unsigned long size);
|
||||
void *luaI_buffer (unsigned long size);
|
||||
int luaI_growvector (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit);
|
||||
|
||||
#define new(s) ((s *)luaI_malloc(sizeof(s)))
|
||||
#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
|
||||
#define growvector(old,n,s,e,l) \
|
||||
(luaI_growvector((void**)old,n,sizeof(s),e,l))
|
||||
|
||||
#endif
|
||||
|
||||
274
lundump.c
Normal file
274
lundump.c
Normal file
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.43 2002/08/07 00:36:03 lhf Exp $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstring.h"
|
||||
#include "lundump.h"
|
||||
#include "lzio.h"
|
||||
|
||||
#define LoadByte (lu_byte) ezgetc
|
||||
|
||||
typedef struct {
|
||||
lua_State* L;
|
||||
ZIO* Z;
|
||||
int swap;
|
||||
const char* name;
|
||||
} LoadState;
|
||||
|
||||
static void unexpectedEOZ (LoadState* S)
|
||||
{
|
||||
luaG_runerror(S->L,"unexpected end of file in %s",S->name);
|
||||
}
|
||||
|
||||
static int ezgetc (LoadState* S)
|
||||
{
|
||||
int c=zgetc(S->Z);
|
||||
if (c==EOZ) unexpectedEOZ(S);
|
||||
return c;
|
||||
}
|
||||
|
||||
static void ezread (LoadState* S, void* b, int n)
|
||||
{
|
||||
int r=luaZ_read(S->Z,b,n);
|
||||
if (r!=0) unexpectedEOZ(S);
|
||||
}
|
||||
|
||||
static void LoadBlock (LoadState* S, void* b, size_t size)
|
||||
{
|
||||
if (S->swap)
|
||||
{
|
||||
char *p=(char*) b+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(S);
|
||||
}
|
||||
else
|
||||
ezread(S,b,size);
|
||||
}
|
||||
|
||||
static void LoadVector (LoadState* S, void* b, int m, size_t size)
|
||||
{
|
||||
if (S->swap)
|
||||
{
|
||||
char *q=(char*) b;
|
||||
while (m--)
|
||||
{
|
||||
char *p=q+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(S);
|
||||
q+=size;
|
||||
}
|
||||
}
|
||||
else
|
||||
ezread(S,b,m*size);
|
||||
}
|
||||
|
||||
static int LoadInt (LoadState* S)
|
||||
{
|
||||
int x;
|
||||
LoadBlock(S,&x,sizeof(x));
|
||||
if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name);
|
||||
return x;
|
||||
}
|
||||
|
||||
static size_t LoadSize (LoadState* S)
|
||||
{
|
||||
size_t x;
|
||||
LoadBlock(S,&x,sizeof(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static lua_Number LoadNumber (LoadState* S)
|
||||
{
|
||||
lua_Number x;
|
||||
LoadBlock(S,&x,sizeof(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static TString* LoadString (LoadState* S)
|
||||
{
|
||||
size_t size=LoadSize(S);
|
||||
if (size==0)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
char* s=luaO_openspace(S->L,size,char);
|
||||
ezread(S,s,size);
|
||||
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadCode (LoadState* S, Proto* f)
|
||||
{
|
||||
int size=LoadInt(S);
|
||||
f->code=luaM_newvector(S->L,size,Instruction);
|
||||
f->sizecode=size;
|
||||
LoadVector(S,f->code,size,sizeof(*f->code));
|
||||
}
|
||||
|
||||
static void LoadLocals (LoadState* S, Proto* f)
|
||||
{
|
||||
int i,n;
|
||||
n=LoadInt(S);
|
||||
f->locvars=luaM_newvector(S->L,n,LocVar);
|
||||
f->sizelocvars=n;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
f->locvars[i].varname=LoadString(S);
|
||||
f->locvars[i].startpc=LoadInt(S);
|
||||
f->locvars[i].endpc=LoadInt(S);
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadLines (LoadState* S, Proto* f)
|
||||
{
|
||||
int n;
|
||||
n=LoadInt(S);
|
||||
f->lineinfo=luaM_newvector(S->L,n,int);
|
||||
LoadVector(S,f->lineinfo,n,sizeof(*f->lineinfo));
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (LoadState* S, TString* p);
|
||||
|
||||
static void LoadConstants (LoadState* S, Proto* f)
|
||||
{
|
||||
int i,n;
|
||||
n=LoadInt(S);
|
||||
f->k=luaM_newvector(S->L,n,TObject);
|
||||
f->sizek=n;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
TObject* o=&f->k[i];
|
||||
int t=LoadByte(S);
|
||||
switch (t)
|
||||
{
|
||||
case LUA_TNUMBER:
|
||||
setnvalue(o,LoadNumber(S));
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
setsvalue(o,LoadString(S));
|
||||
break;
|
||||
case LUA_TNIL:
|
||||
setnilvalue(o);
|
||||
break;
|
||||
default:
|
||||
luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
n=LoadInt(S);
|
||||
f->p=luaM_newvector(S->L,n,Proto*);
|
||||
f->sizep=n;
|
||||
for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (LoadState* S, TString* p)
|
||||
{
|
||||
Proto* f=luaF_newproto(S->L);
|
||||
f->source=LoadString(S); if (f->source==NULL) f->source=p;
|
||||
f->lineDefined=LoadInt(S);
|
||||
f->nupvalues=LoadByte(S);
|
||||
f->numparams=LoadByte(S);
|
||||
f->is_vararg=LoadByte(S);
|
||||
f->maxstacksize=LoadByte(S);
|
||||
LoadLocals(S,f);
|
||||
LoadLines(S,f);
|
||||
LoadConstants(S,f);
|
||||
LoadCode(S,f);
|
||||
#ifndef TRUST_BINARIES
|
||||
if (!luaG_checkcode(f)) luaG_runerror(S->L,"bad code in %s",S->name);
|
||||
#endif
|
||||
return f;
|
||||
}
|
||||
|
||||
static void LoadSignature (LoadState* S)
|
||||
{
|
||||
const char* s=LUA_SIGNATURE;
|
||||
while (*s!=0 && ezgetc(S)==*s)
|
||||
++s;
|
||||
if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name);
|
||||
}
|
||||
|
||||
static void TestSize (LoadState* S, int s, const char* what)
|
||||
{
|
||||
int r=LoadByte(S);
|
||||
if (r!=s)
|
||||
luaG_runerror(S->L,"virtual machine mismatch in %s: "
|
||||
"size of %s is %d but read %d",S->name,what,s,r);
|
||||
}
|
||||
|
||||
#define TESTSIZE(s,w) TestSize(S,s,w)
|
||||
#define V(v) v/16,v%16
|
||||
|
||||
static void LoadHeader (LoadState* S)
|
||||
{
|
||||
int version;
|
||||
lua_Number x=0,tx=TEST_NUMBER;
|
||||
LoadSignature(S);
|
||||
version=LoadByte(S);
|
||||
if (version>VERSION)
|
||||
luaG_runerror(S->L,"%s too new: "
|
||||
"read version %d.%d; expected at most %d.%d",
|
||||
S->name,V(version),V(VERSION));
|
||||
if (version<VERSION0) /* check last major change */
|
||||
luaG_runerror(S->L,"%s too old: "
|
||||
"read version %d.%d; expected at least %d.%d",
|
||||
S->name,V(version),V(VERSION0));
|
||||
S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */
|
||||
TESTSIZE(sizeof(int),"int");
|
||||
TESTSIZE(sizeof(size_t), "size_t");
|
||||
TESTSIZE(sizeof(Instruction), "Instruction");
|
||||
TESTSIZE(SIZE_OP, "OP");
|
||||
TESTSIZE(SIZE_A, "A");
|
||||
TESTSIZE(SIZE_B, "B");
|
||||
TESTSIZE(SIZE_C, "C");
|
||||
TESTSIZE(sizeof(lua_Number), "number");
|
||||
x=LoadNumber(S);
|
||||
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
|
||||
luaG_runerror(S->L,"unknown number format in %s: read %f; expected %f",
|
||||
S->name,x,tx);
|
||||
}
|
||||
|
||||
static Proto* LoadChunk (LoadState* S)
|
||||
{
|
||||
LoadHeader(S);
|
||||
return LoadFunction(S,NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
** load one chunk from a file or buffer
|
||||
*/
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z)
|
||||
{
|
||||
LoadState S;
|
||||
Proto* f;
|
||||
const char* s=zname(Z);
|
||||
if (*s=='@' || *s=='=')
|
||||
S.name=s+1;
|
||||
else if (*s==LUA_SIGNATURE[0])
|
||||
S.name="binary string";
|
||||
else
|
||||
S.name=s;
|
||||
S.L=L;
|
||||
S.Z=Z;
|
||||
f=LoadChunk(&S);
|
||||
if (zgetc(Z)!=EOZ)
|
||||
luaG_runerror(L,"%s apparently contains more than one chunk",S.name);
|
||||
return f;
|
||||
}
|
||||
|
||||
/*
|
||||
** find byte order
|
||||
*/
|
||||
int luaU_endianness (void)
|
||||
{
|
||||
int x=1;
|
||||
return *(char*)&x;
|
||||
}
|
||||
36
lundump.h
Normal file
36
lundump.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.26 2002/08/07 00:36:03 lhf Exp $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lundump_h
|
||||
#define lundump_h
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
typedef int (*Writer)(const void* p, size_t size, void* u);
|
||||
|
||||
/* load one chunk; from lundump.c */
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z);
|
||||
|
||||
/* find byte order; from lundump.c */
|
||||
int luaU_endianness (void);
|
||||
|
||||
/* dump one chunk; from dump.c */
|
||||
void luaU_dump (const Proto* Main, Writer w, void* data);
|
||||
|
||||
/* print one chunk; from print.c */
|
||||
void luaU_print (const Proto* Main);
|
||||
|
||||
/* definitions for headers of binary files */
|
||||
#define LUA_SIGNATURE "\033Lua" /* binary files start with <esc>Lua */
|
||||
#define VERSION 0x50 /* last format change was in 5.0 */
|
||||
#define VERSION0 0x50 /* last major change was in 5.0 */
|
||||
|
||||
/* a multiple of PI for testing native format */
|
||||
/* multiplying by 1E8 gives non-trivial integer values */
|
||||
#define TEST_NUMBER 3.14159265358979323846E8
|
||||
|
||||
#endif
|
||||
737
lvm.c
Normal file
737
lvm.c
Normal file
@@ -0,0 +1,737 @@
|
||||
/*
|
||||
** $Id: lvm.c,v 1.251 2002/08/07 19:22:39 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
|
||||
/* function to convert a lua_Number to a string */
|
||||
#ifndef lua_number2str
|
||||
#include <stdio.h>
|
||||
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
|
||||
#endif
|
||||
|
||||
|
||||
/* limit for table tag-method chains (to avoid loops) */
|
||||
#define MAXTAGLOOP 100
|
||||
|
||||
|
||||
static void luaV_checkGC (lua_State *L, StkId top) {
|
||||
if (G(L)->nblocks >= G(L)->GCthreshold) {
|
||||
L->top = top; /* limit for active registers */
|
||||
luaC_collectgarbage(L);
|
||||
L->top = L->ci->top; /* restore old top position */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
|
||||
lua_Number num;
|
||||
if (ttisnumber(obj)) return obj;
|
||||
if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
|
||||
setnvalue(n, num);
|
||||
return n;
|
||||
}
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int luaV_tostring (lua_State *L, TObject *obj) {
|
||||
if (!ttisnumber(obj))
|
||||
return 0;
|
||||
else {
|
||||
char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
|
||||
lua_number2str(s, nvalue(obj));
|
||||
setsvalue(obj, luaS_new(L, s));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void traceexec (lua_State *L) {
|
||||
unsigned long mask = L->hookmask;
|
||||
if (mask > LUA_MASKLINE) { /* instruction-hook set? */
|
||||
if (L->hookcount == 0) {
|
||||
luaD_callhook(L, LUA_HOOKCOUNT, -1);
|
||||
resethookcount(L);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (mask & LUA_MASKLINE) {
|
||||
CallInfo *ci = L->ci;
|
||||
Proto *p = ci_func(ci)->l.p;
|
||||
int newline = getline(p, pcRel(*ci->u.l.pc, p));
|
||||
lua_assert(ci->state & CI_HASFRAME);
|
||||
if (pcRel(*ci->u.l.pc, p) == 0) /* tracing may be starting now? */
|
||||
ci->u.l.savedpc = *ci->u.l.pc; /* initialize `savedpc' */
|
||||
/* calls linehook when enters a new line or jumps back (loop) */
|
||||
if (*ci->u.l.pc <= ci->u.l.savedpc ||
|
||||
newline != getline(p, pcRel(ci->u.l.savedpc, p))) {
|
||||
luaD_callhook(L, LUA_HOOKLINE, newline);
|
||||
ci = L->ci; /* previous call may reallocate `ci' */
|
||||
}
|
||||
ci->u.l.savedpc = *ci->u.l.pc;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void callTMres (lua_State *L, const TObject *f,
|
||||
const TObject *p1, const TObject *p2) {
|
||||
setobj(L->top, f); /* push function */
|
||||
setobj(L->top+1, p1); /* 1st argument */
|
||||
setobj(L->top+2, p2); /* 2nd argument */
|
||||
luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */
|
||||
L->top += 3;
|
||||
luaD_call(L, L->top - 3, 1);
|
||||
L->top--; /* result will be in L->top */
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void callTM (lua_State *L, const TObject *f,
|
||||
const TObject *p1, const TObject *p2, const TObject *p3) {
|
||||
setobj(L->top, f); /* push function */
|
||||
setobj(L->top+1, p1); /* 1st argument */
|
||||
setobj(L->top+2, p2); /* 2nd argument */
|
||||
setobj(L->top+3, p3); /* 3th argument */
|
||||
luaD_checkstack(L, 4); /* cannot check before (could invalidate p1...p3) */
|
||||
L->top += 4;
|
||||
luaD_call(L, L->top - 4, 0);
|
||||
}
|
||||
|
||||
|
||||
static const TObject *luaV_index (lua_State *L, const TObject *t,
|
||||
TObject *key, int loop) {
|
||||
const TObject *tm = fasttm(L, hvalue(t)->metatable, TM_INDEX);
|
||||
if (tm == NULL) return &luaO_nilobject; /* no TM */
|
||||
if (ttisfunction(tm)) {
|
||||
callTMres(L, tm, t, key);
|
||||
return L->top;
|
||||
}
|
||||
else return luaV_gettable(L, tm, key, loop);
|
||||
}
|
||||
|
||||
static const TObject *luaV_getnotable (lua_State *L, const TObject *t,
|
||||
TObject *key, int loop) {
|
||||
const TObject *tm = luaT_gettmbyobj(L, t, TM_GETTABLE);
|
||||
if (ttisnil(tm))
|
||||
luaG_typeerror(L, t, "index");
|
||||
if (ttisfunction(tm)) {
|
||||
callTMres(L, tm, t, key);
|
||||
return L->top;
|
||||
}
|
||||
else return luaV_gettable(L, tm, key, loop);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Function to index a table.
|
||||
** Receives the table at `t' and the key at `key'.
|
||||
** leaves the result at `res'.
|
||||
*/
|
||||
const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key,
|
||||
int loop) {
|
||||
if (loop > MAXTAGLOOP)
|
||||
luaG_runerror(L, "loop in gettable");
|
||||
if (ttistable(t)) { /* `t' is a table? */
|
||||
Table *h = hvalue(t);
|
||||
const TObject *v = luaH_get(h, key); /* do a primitive get */
|
||||
if (!ttisnil(v)) return v;
|
||||
else return luaV_index(L, t, key, loop+1);
|
||||
}
|
||||
else return luaV_getnotable(L, t, key, loop+1);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Receives table at `t', key at `key' and value at `val'.
|
||||
*/
|
||||
void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
|
||||
const TObject *tm;
|
||||
int loop = 0;
|
||||
do {
|
||||
if (ttistable(t)) { /* `t' is a table? */
|
||||
Table *h = hvalue(t);
|
||||
TObject *oldval = luaH_set(L, h, key); /* do a primitive set */
|
||||
if (!ttisnil(oldval) || /* result is no nil? */
|
||||
(tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
|
||||
setobj(oldval, val);
|
||||
return;
|
||||
}
|
||||
/* else will try the tag method */
|
||||
}
|
||||
else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)))
|
||||
luaG_typeerror(L, t, "index");
|
||||
if (ttisfunction(tm)) {
|
||||
callTM(L, tm, t, key, val);
|
||||
return;
|
||||
}
|
||||
t = tm; /* else repeat with `tm' */
|
||||
} while (++loop <= MAXTAGLOOP);
|
||||
luaG_runerror(L, "loop in settable");
|
||||
}
|
||||
|
||||
|
||||
static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
|
||||
TObject *res, TMS event) {
|
||||
ptrdiff_t result = savestack(L, res);
|
||||
const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
|
||||
if (ttisnil(tm))
|
||||
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
|
||||
if (!ttisfunction(tm)) return 0;
|
||||
callTMres(L, tm, p1, p2);
|
||||
res = restorestack(L, result); /* previous call may change stack */
|
||||
setobj(res, L->top);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaV_strcmp (const TString *ls, const TString *rs) {
|
||||
const char *l = getstr(ls);
|
||||
size_t ll = ls->tsv.len;
|
||||
const char *r = getstr(rs);
|
||||
size_t lr = rs->tsv.len;
|
||||
for (;;) {
|
||||
int temp = strcoll(l, r);
|
||||
if (temp != 0) return temp;
|
||||
else { /* strings are equal up to a `\0' */
|
||||
size_t len = strlen(l); /* index of first `\0' in both strings */
|
||||
if (len == lr) /* r is finished? */
|
||||
return (len == ll) ? 0 : 1;
|
||||
else if (len == ll) /* l is finished? */
|
||||
return -1; /* l is smaller than r (because r is not finished) */
|
||||
/* both strings longer than `len'; go on comparing (after the `\0') */
|
||||
len++;
|
||||
l += len; ll -= len; r += len; lr -= len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) {
|
||||
if (ttype(l) != ttype(r))
|
||||
return luaG_ordererror(L, l, r);
|
||||
else if (ttisnumber(l))
|
||||
return nvalue(l) < nvalue(r);
|
||||
else if (ttisstring(l))
|
||||
return luaV_strcmp(tsvalue(l), tsvalue(r)) < 0;
|
||||
else if (call_binTM(L, l, r, L->top, TM_LT))
|
||||
return !l_isfalse(L->top);
|
||||
return luaG_ordererror(L, l, r);
|
||||
}
|
||||
|
||||
|
||||
static int luaV_lessequal (lua_State *L, const TObject *l, const TObject *r) {
|
||||
if (ttype(l) != ttype(r))
|
||||
return luaG_ordererror(L, l, r);
|
||||
else if (ttisnumber(l))
|
||||
return nvalue(l) <= nvalue(r);
|
||||
else if (ttisstring(l))
|
||||
return luaV_strcmp(tsvalue(l), tsvalue(r)) <= 0;
|
||||
else if (call_binTM(L, l, r, L->top, TM_LE)) /* first try `le' */
|
||||
return !l_isfalse(L->top);
|
||||
else if (call_binTM(L, r, l, L->top, TM_LT)) /* else try `lt' */
|
||||
return l_isfalse(L->top);
|
||||
return luaG_ordererror(L, l, r);
|
||||
}
|
||||
|
||||
|
||||
int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2) {
|
||||
const TObject *tm = NULL;
|
||||
lua_assert(ttype(t1) == ttype(t2));
|
||||
switch (ttype(t1)) {
|
||||
case LUA_TNIL: return 1;
|
||||
case LUA_TNUMBER: return nvalue(t1) == nvalue(t2);
|
||||
case LUA_TSTRING: return tsvalue(t1) == tsvalue(t2);
|
||||
case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
|
||||
case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
|
||||
case LUA_TFUNCTION: return clvalue(t1) == clvalue(t2);
|
||||
case LUA_TUSERDATA:
|
||||
if (uvalue(t1) == uvalue(t2)) return 1;
|
||||
else if ((tm = fasttm(L, uvalue(t1)->uv.metatable, TM_EQ)) == NULL &&
|
||||
(tm = fasttm(L, uvalue(t2)->uv.metatable, TM_EQ)) == NULL)
|
||||
return 0; /* no TM */
|
||||
else break; /* will try TM */
|
||||
case LUA_TTABLE:
|
||||
if (hvalue(t1) == hvalue(t2)) return 1;
|
||||
else if ((tm = fasttm(L, hvalue(t1)->metatable, TM_EQ)) == NULL &&
|
||||
(tm = fasttm(L, hvalue(t2)->metatable, TM_EQ)) == NULL)
|
||||
return 0; /* no TM */
|
||||
else break; /* will try TM */
|
||||
}
|
||||
callTMres(L, tm, t1, t2); /* call TM */
|
||||
return !l_isfalse(L->top);
|
||||
}
|
||||
|
||||
|
||||
void luaV_concat (lua_State *L, int total, int last) {
|
||||
do {
|
||||
StkId top = L->ci->base + last + 1;
|
||||
int n = 2; /* number of elements handled in this pass (at least 2) */
|
||||
if (!tostring(L, top-2) || !tostring(L, top-1)) {
|
||||
if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
|
||||
luaG_concaterror(L, top-2, top-1);
|
||||
} else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */
|
||||
/* at least two string values; get as many as possible */
|
||||
lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) +
|
||||
cast(lu_mem, tsvalue(top-2)->tsv.len);
|
||||
char *buffer;
|
||||
int i;
|
||||
while (n < total && tostring(L, top-n-1)) { /* collect total length */
|
||||
tl += tsvalue(top-n-1)->tsv.len;
|
||||
n++;
|
||||
}
|
||||
if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow");
|
||||
buffer = luaO_openspace(L, tl, char);
|
||||
tl = 0;
|
||||
for (i=n; i>0; i--) { /* concat all strings */
|
||||
size_t l = tsvalue(top-i)->tsv.len;
|
||||
memcpy(buffer+tl, svalue(top-i), l);
|
||||
tl += l;
|
||||
}
|
||||
setsvalue(top-n, luaS_newlstr(L, buffer, tl));
|
||||
}
|
||||
total -= n-1; /* got `n' strings to create 1 new */
|
||||
last -= n-1;
|
||||
} while (total > 1); /* repeat until only 1 result left */
|
||||
}
|
||||
|
||||
|
||||
static void Arith (lua_State *L, StkId ra, StkId rb, StkId rc, TMS op) {
|
||||
TObject tempb, tempc;
|
||||
const TObject *b, *c;
|
||||
if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
|
||||
(c = luaV_tonumber(rc, &tempc)) != NULL) {
|
||||
switch (op) {
|
||||
case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break;
|
||||
case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break;
|
||||
case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break;
|
||||
case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break;
|
||||
case TM_POW: {
|
||||
const TObject *f = luaH_getstr(hvalue(registry(L)),
|
||||
G(L)->tmname[TM_POW]);
|
||||
ptrdiff_t res = savestack(L, ra);
|
||||
if (!ttisfunction(f))
|
||||
luaG_runerror(L, "`pow' (for `^' operator) is not a function");
|
||||
callTMres(L, f, b, c);
|
||||
ra = restorestack(L, res); /* previous call may change stack */
|
||||
setobj(ra, L->top);
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0); break;
|
||||
}
|
||||
}
|
||||
else if (!call_binTM(L, rb, rc, ra, op))
|
||||
luaG_aritherror(L, rb, rc);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** some macros for common tasks in `luaV_execute'
|
||||
*/
|
||||
|
||||
#define runtime_check(L, c) { if (!(c)) return 0; }
|
||||
|
||||
#define RA(i) (base+GETARG_A(i))
|
||||
#define RB(i) (base+GETARG_B(i))
|
||||
#define RC(i) (base+GETARG_C(i))
|
||||
#define RKC(i) ((GETARG_C(i) < MAXSTACK) ? \
|
||||
base+GETARG_C(i) : \
|
||||
k+GETARG_C(i)-MAXSTACK)
|
||||
#define KBx(i) (k+GETARG_Bx(i))
|
||||
|
||||
|
||||
#define dojump(pc, i) ((pc) += (i))
|
||||
|
||||
|
||||
StkId luaV_execute (lua_State *L) {
|
||||
StkId base;
|
||||
LClosure *cl;
|
||||
TObject *k;
|
||||
const Instruction *pc;
|
||||
callentry: /* entry point when calling new functions */
|
||||
L->ci->u.l.pb = &base;
|
||||
L->ci->u.l.pc = &pc;
|
||||
if (L->hookmask & LUA_MASKCALL)
|
||||
luaD_callhook(L, LUA_HOOKCALL, -1);
|
||||
retentry: /* entry point when returning to old functions */
|
||||
lua_assert(L->ci->state & CI_SAVEDPC);
|
||||
L->ci->state = CI_HASFRAME; /* activate frame */
|
||||
pc = L->ci->u.l.savedpc;
|
||||
base = L->ci->base;
|
||||
cl = &clvalue(base - 1)->l;
|
||||
k = cl->p->k;
|
||||
/* main loop of interpreter */
|
||||
for (;;) {
|
||||
const Instruction i = *pc++;
|
||||
StkId ra;
|
||||
if (L->hookmask >= LUA_MASKLINE &&
|
||||
(--L->hookcount == 0 || L->hookmask & LUA_MASKLINE))
|
||||
traceexec(L);
|
||||
/* warning!! several calls may realloc the stack and invalidate `ra' */
|
||||
ra = RA(i);
|
||||
lua_assert(L->top <= L->stack + L->stacksize && L->top >= L->ci->base);
|
||||
lua_assert(L->top == L->ci->top ||
|
||||
GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
|
||||
GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_MOVE: {
|
||||
setobj(ra, RB(i));
|
||||
break;
|
||||
}
|
||||
case OP_LOADK: {
|
||||
setobj(ra, KBx(i));
|
||||
break;
|
||||
}
|
||||
case OP_LOADBOOL: {
|
||||
setbvalue(ra, GETARG_B(i));
|
||||
if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
|
||||
break;
|
||||
}
|
||||
case OP_LOADNIL: {
|
||||
TObject *rb = RB(i);
|
||||
do {
|
||||
setnilvalue(rb--);
|
||||
} while (rb >= ra);
|
||||
break;
|
||||
}
|
||||
case OP_GETUPVAL: {
|
||||
int b = GETARG_B(i);
|
||||
setobj(ra, cl->upvals[b]->v);
|
||||
break;
|
||||
}
|
||||
case OP_GETGLOBAL: {
|
||||
StkId rb = KBx(i);
|
||||
const TObject *v;
|
||||
lua_assert(ttisstring(rb) && ttistable(&cl->g));
|
||||
v = luaH_getstr(hvalue(&cl->g), tsvalue(rb));
|
||||
if (!ttisnil(v)) { setobj(ra, v); }
|
||||
else
|
||||
setobj(RA(i), luaV_index(L, &cl->g, rb, 0));
|
||||
break;
|
||||
}
|
||||
case OP_GETTABLE: {
|
||||
StkId rb = RB(i);
|
||||
TObject *rc = RKC(i);
|
||||
if (ttistable(rb)) {
|
||||
const TObject *v = luaH_get(hvalue(rb), rc);
|
||||
if (!ttisnil(v)) { setobj(ra, v); }
|
||||
else
|
||||
setobj(RA(i), luaV_index(L, rb, rc, 0));
|
||||
}
|
||||
else
|
||||
setobj(RA(i), luaV_getnotable(L, rb, rc, 0));
|
||||
break;
|
||||
}
|
||||
case OP_SETGLOBAL: {
|
||||
lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g));
|
||||
luaV_settable(L, &cl->g, KBx(i), ra);
|
||||
break;
|
||||
}
|
||||
case OP_SETUPVAL: {
|
||||
int b = GETARG_B(i);
|
||||
setobj(cl->upvals[b]->v, ra);
|
||||
break;
|
||||
}
|
||||
case OP_SETTABLE: {
|
||||
luaV_settable(L, RB(i), RKC(i), ra);
|
||||
break;
|
||||
}
|
||||
case OP_NEWTABLE: {
|
||||
int b = GETARG_B(i);
|
||||
if (b > 0) b = twoto(b-1);
|
||||
sethvalue(ra, luaH_new(L, b, GETARG_C(i)));
|
||||
luaV_checkGC(L, ra+1);
|
||||
break;
|
||||
}
|
||||
case OP_SELF: {
|
||||
StkId rb = RB(i);
|
||||
TObject *rc = RKC(i);
|
||||
runtime_check(L, ttisstring(rc));
|
||||
setobj(ra+1, rb);
|
||||
if (ttistable(rb)) {
|
||||
const TObject *v = luaH_getstr(hvalue(rb), tsvalue(rc));
|
||||
if (!ttisnil(v)) { setobj(ra, v); }
|
||||
else
|
||||
setobj(RA(i), luaV_index(L, rb, rc, 0));
|
||||
}
|
||||
else
|
||||
setobj(RA(i), luaV_getnotable(L, rb, rc, 0));
|
||||
break;
|
||||
}
|
||||
case OP_ADD: {
|
||||
StkId rb = RB(i);
|
||||
StkId rc = RKC(i);
|
||||
if (ttisnumber(rb) && ttisnumber(rc)) {
|
||||
setnvalue(ra, nvalue(rb) + nvalue(rc));
|
||||
}
|
||||
else
|
||||
Arith(L, ra, rb, rc, TM_ADD);
|
||||
break;
|
||||
}
|
||||
case OP_SUB: {
|
||||
StkId rb = RB(i);
|
||||
StkId rc = RKC(i);
|
||||
if (ttisnumber(rb) && ttisnumber(rc)) {
|
||||
setnvalue(ra, nvalue(rb) - nvalue(rc));
|
||||
}
|
||||
else
|
||||
Arith(L, ra, rb, rc, TM_SUB);
|
||||
break;
|
||||
}
|
||||
case OP_MUL: {
|
||||
StkId rb = RB(i);
|
||||
StkId rc = RKC(i);
|
||||
if (ttisnumber(rb) && ttisnumber(rc)) {
|
||||
setnvalue(ra, nvalue(rb) * nvalue(rc));
|
||||
}
|
||||
else
|
||||
Arith(L, ra, rb, rc, TM_MUL);
|
||||
break;
|
||||
}
|
||||
case OP_DIV: {
|
||||
StkId rb = RB(i);
|
||||
StkId rc = RKC(i);
|
||||
if (ttisnumber(rb) && ttisnumber(rc)) {
|
||||
setnvalue(ra, nvalue(rb) / nvalue(rc));
|
||||
}
|
||||
else
|
||||
Arith(L, ra, rb, rc, TM_DIV);
|
||||
break;
|
||||
}
|
||||
case OP_POW: {
|
||||
Arith(L, ra, RB(i), RKC(i), TM_POW);
|
||||
break;
|
||||
}
|
||||
case OP_UNM: {
|
||||
const TObject *rb = RB(i);
|
||||
TObject temp;
|
||||
if (tonumber(rb, &temp)) {
|
||||
setnvalue(ra, -nvalue(rb));
|
||||
}
|
||||
else {
|
||||
setnilvalue(&temp);
|
||||
if (!call_binTM(L, RB(i), &temp, ra, TM_UNM))
|
||||
luaG_aritherror(L, RB(i), &temp);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_NOT: {
|
||||
int res = l_isfalse(RB(i)); /* next assignment may change this value */
|
||||
setbvalue(ra, res);
|
||||
break;
|
||||
}
|
||||
case OP_CONCAT: {
|
||||
int b = GETARG_B(i);
|
||||
int c = GETARG_C(i);
|
||||
luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */
|
||||
setobj(RA(i), base+b);
|
||||
luaV_checkGC(L, base+c+1);
|
||||
break;
|
||||
}
|
||||
case OP_JMP: {
|
||||
dojump(pc, GETARG_sBx(i));
|
||||
break;
|
||||
}
|
||||
case OP_EQ: { /* skip next instruction if test fails */
|
||||
if (equalobj(L, ra, RKC(i)) != GETARG_B(i)) pc++;
|
||||
else dojump(pc, GETARG_sBx(*pc) + 1);
|
||||
break;
|
||||
}
|
||||
case OP_LT: {
|
||||
if (luaV_lessthan(L, ra, RKC(i)) != GETARG_B(i)) pc++;
|
||||
else dojump(pc, GETARG_sBx(*pc) + 1);
|
||||
break;
|
||||
}
|
||||
case OP_LE: {
|
||||
if (luaV_lessequal(L, ra, RKC(i)) != GETARG_B(i)) pc++;
|
||||
else dojump(pc, GETARG_sBx(*pc) + 1);
|
||||
break;
|
||||
}
|
||||
case OP_GT: {
|
||||
if (luaV_lessthan(L, RKC(i), ra) != GETARG_B(i)) pc++;
|
||||
else dojump(pc, GETARG_sBx(*pc) + 1);
|
||||
break;
|
||||
}
|
||||
case OP_GE: {
|
||||
if (luaV_lessequal(L, RKC(i), ra) != GETARG_B(i)) pc++;
|
||||
else dojump(pc, GETARG_sBx(*pc) + 1);
|
||||
break;
|
||||
}
|
||||
case OP_TEST: {
|
||||
StkId rc = RKC(i);
|
||||
if (l_isfalse(rc) == GETARG_B(i)) pc++;
|
||||
else {
|
||||
setobj(ra, rc);
|
||||
dojump(pc, GETARG_sBx(*pc) + 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_CALL:
|
||||
case OP_TAILCALL: {
|
||||
StkId firstResult;
|
||||
int b = GETARG_B(i);
|
||||
int nresults;
|
||||
if (b != 0) L->top = ra+b; /* else previous instruction set top */
|
||||
nresults = GETARG_C(i) - 1;
|
||||
firstResult = luaD_precall(L, ra);
|
||||
if (firstResult) {
|
||||
if (firstResult > L->top) { /* yield? */
|
||||
(L->ci - 1)->u.l.savedpc = pc;
|
||||
(L->ci - 1)->state = CI_SAVEDPC;
|
||||
return NULL;
|
||||
}
|
||||
/* it was a C function (`precall' called it); adjust results */
|
||||
luaD_poscall(L, nresults, firstResult);
|
||||
if (nresults >= 0) L->top = L->ci->top;
|
||||
}
|
||||
else { /* it is a Lua function */
|
||||
if (GET_OPCODE(i) == OP_CALL) { /* regular call? */
|
||||
(L->ci-1)->u.l.savedpc = pc; /* save `pc' to return later */
|
||||
(L->ci-1)->state = (CI_SAVEDPC | CI_CALLING);
|
||||
}
|
||||
else { /* tail call: put new frame in place of previous one */
|
||||
int aux;
|
||||
StkId ra1 = RA(i); /* `luaD_precall' may change the stack */
|
||||
if (L->openupval) luaF_close(L, base);
|
||||
for (aux = 0; ra1+aux < L->top; aux++) /* move frame down */
|
||||
setobj(base+aux-1, ra1+aux);
|
||||
(L->ci - 1)->top = L->top = base+aux; /* correct top */
|
||||
lua_assert(L->ci->state & CI_SAVEDPC);
|
||||
(L->ci - 1)->u.l.savedpc = L->ci->u.l.savedpc;
|
||||
(L->ci - 1)->state = CI_SAVEDPC;
|
||||
L->ci--; /* remove previous frame */
|
||||
}
|
||||
goto callentry;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_RETURN: {
|
||||
CallInfo *ci = L->ci - 1;
|
||||
int b = GETARG_B(i);
|
||||
if (b != 0) L->top = ra+b-1;
|
||||
lua_assert(L->ci->state & CI_HASFRAME);
|
||||
if (L->openupval) luaF_close(L, base);
|
||||
L->ci->state = CI_SAVEDPC; /* deactivate current function */
|
||||
L->ci->u.l.savedpc = pc;
|
||||
/* previous function was running `here'? */
|
||||
if (!(ci->state & CI_CALLING))
|
||||
return ra; /* no: return */
|
||||
else { /* yes: continue its execution (go through) */
|
||||
int nresults;
|
||||
lua_assert(ttisfunction(ci->base - 1));
|
||||
lua_assert(ci->state & CI_SAVEDPC);
|
||||
lua_assert(GET_OPCODE(*(ci->u.l.savedpc - 1)) == OP_CALL);
|
||||
nresults = GETARG_C(*(ci->u.l.savedpc - 1)) - 1;
|
||||
luaD_poscall(L, nresults, ra);
|
||||
if (nresults >= 0) L->top = L->ci->top;
|
||||
goto retentry;
|
||||
}
|
||||
}
|
||||
case OP_FORLOOP: {
|
||||
lua_Number step, index, limit;
|
||||
const TObject *plimit = ra+1;
|
||||
const TObject *pstep = ra+2;
|
||||
if (!ttisnumber(ra))
|
||||
luaG_runerror(L, "`for' initial value must be a number");
|
||||
if (!tonumber(plimit, ra+1))
|
||||
luaG_runerror(L, "`for' limit must be a number");
|
||||
if (!tonumber(pstep, ra+2))
|
||||
luaG_runerror(L, "`for' step must be a number");
|
||||
step = nvalue(pstep);
|
||||
index = nvalue(ra) + step; /* increment index */
|
||||
limit = nvalue(plimit);
|
||||
if (step > 0 ? index <= limit : index >= limit) {
|
||||
dojump(pc, GETARG_sBx(i)); /* jump back */
|
||||
chgnvalue(ra, index); /* update index */
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_TFORLOOP: {
|
||||
setobj(ra+4, ra+2);
|
||||
setobj(ra+3, ra+1);
|
||||
setobj(ra+2, ra);
|
||||
L->top = ra+5;
|
||||
luaD_call(L, ra+2, GETARG_C(i) + 1);
|
||||
L->top = L->ci->top;
|
||||
if (ttisnil(RA(i)+2)) pc++; /* skip jump (break loop) */
|
||||
else dojump(pc, GETARG_sBx(*pc) + 1); /* else jump back */
|
||||
break;
|
||||
}
|
||||
case OP_TFORPREP: { /* for compatibility only */
|
||||
if (ttistable(ra)) {
|
||||
setobj(ra+1, ra);
|
||||
setobj(ra, luaH_getstr(hvalue(gt(L)), luaS_new(L, "next")));
|
||||
}
|
||||
dojump(pc, GETARG_sBx(i));
|
||||
break;
|
||||
}
|
||||
case OP_SETLIST:
|
||||
case OP_SETLISTO: {
|
||||
int bc;
|
||||
int n;
|
||||
Table *h;
|
||||
runtime_check(L, ttistable(ra));
|
||||
h = hvalue(ra);
|
||||
bc = GETARG_Bx(i);
|
||||
if (GET_OPCODE(i) == OP_SETLIST)
|
||||
n = (bc&(LFIELDS_PER_FLUSH-1)) + 1;
|
||||
else {
|
||||
n = L->top - ra - 1;
|
||||
L->top = L->ci->top;
|
||||
}
|
||||
bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
|
||||
for (; n > 0; n--)
|
||||
setobj(luaH_setnum(L, h, bc+n), ra+n);
|
||||
break;
|
||||
}
|
||||
case OP_CLOSE: {
|
||||
luaF_close(L, ra);
|
||||
break;
|
||||
}
|
||||
case OP_CLOSURE: {
|
||||
Proto *p;
|
||||
Closure *ncl;
|
||||
int nup, j;
|
||||
p = cl->p->p[GETARG_Bx(i)];
|
||||
nup = p->nupvalues;
|
||||
ncl = luaF_newLclosure(L, nup, &cl->g);
|
||||
ncl->l.p = p;
|
||||
for (j=0; j<nup; j++, pc++) {
|
||||
if (GET_OPCODE(*pc) == OP_GETUPVAL)
|
||||
ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
|
||||
else {
|
||||
lua_assert(GET_OPCODE(*pc) == OP_MOVE);
|
||||
ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
|
||||
}
|
||||
}
|
||||
setclvalue(ra, ncl);
|
||||
luaV_checkGC(L, L->top);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
35
lvm.h
Normal file
35
lvm.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 1.45 2002/08/05 17:36:24 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lvm_h
|
||||
#define lvm_h
|
||||
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lobject.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
|
||||
|
||||
#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
|
||||
(((o) = luaV_tonumber(o,n)) != NULL))
|
||||
|
||||
#define equalobj(L,o1,o2) \
|
||||
(ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
|
||||
|
||||
|
||||
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r);
|
||||
int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2);
|
||||
const TObject *luaV_tonumber (const TObject *obj, TObject *n);
|
||||
int luaV_tostring (lua_State *L, TObject *obj);
|
||||
const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key,
|
||||
int loop);
|
||||
void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val);
|
||||
StkId luaV_execute (lua_State *L);
|
||||
void luaV_concat (lua_State *L, int total, int last);
|
||||
|
||||
#endif
|
||||
68
lzio.c
Normal file
68
lzio.c
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.20 2002/08/05 18:45:02 roberto Exp roberto $
|
||||
** a generic input stream interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
int luaZ_fill (ZIO *z) {
|
||||
size_t size;
|
||||
const char *buff = z->reader(NULL, z->data, &size);
|
||||
if (buff == NULL || size == 0) return EOZ;
|
||||
z->n = size - 1;
|
||||
z->p = buff;
|
||||
return *(z->p++);
|
||||
}
|
||||
|
||||
|
||||
int luaZ_lookahead (ZIO *z) {
|
||||
if (z->n == 0) {
|
||||
int c = luaZ_fill(z);
|
||||
if (c == EOZ) return c;
|
||||
z->n++;
|
||||
z->p--;
|
||||
}
|
||||
return *z->p;
|
||||
}
|
||||
|
||||
|
||||
void luaZ_init (ZIO *z, lua_Chunkreader reader, void *data, const char *name) {
|
||||
z->reader = reader;
|
||||
z->data = data;
|
||||
z->name = name;
|
||||
z->n = 0;
|
||||
z->p = NULL;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- read --- */
|
||||
size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
||||
while (n) {
|
||||
size_t m;
|
||||
if (z->n == 0) {
|
||||
if (luaZ_fill(z) == EOZ)
|
||||
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; /* min. between n and z->n */
|
||||
memcpy(b, z->p, m);
|
||||
z->n -= m;
|
||||
z->p += m;
|
||||
b = (char *)b + m;
|
||||
n -= m;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
42
lzio.h
Normal file
42
lzio.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
** $Id: lzio.h,v 1.12 2002/06/06 12:40:22 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lzio_h
|
||||
#define lzio_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define EOZ (-1) /* end of stream */
|
||||
|
||||
typedef struct zio ZIO;
|
||||
|
||||
#define zgetc(z) (((z)->n--)>0 ? \
|
||||
cast(int, cast(unsigned char, *(z)->p++)) : \
|
||||
luaZ_fill(z))
|
||||
|
||||
#define zname(z) ((z)->name)
|
||||
|
||||
void luaZ_init (ZIO *z, lua_Chunkreader reader, void *data, const char *name);
|
||||
size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
|
||||
int luaZ_lookahead (ZIO *z);
|
||||
|
||||
|
||||
/* --------- Private Part ------------------ */
|
||||
|
||||
struct zio {
|
||||
size_t n; /* bytes still unread */
|
||||
const char *p; /* current position in buffer */
|
||||
lua_Chunkreader reader;
|
||||
void* data; /* additional data */
|
||||
const char *name;
|
||||
};
|
||||
|
||||
|
||||
int luaZ_fill (ZIO *z);
|
||||
|
||||
#endif
|
||||
188
makefile
188
makefile
@@ -1,92 +1,160 @@
|
||||
# $Id: makefile,v 1.25 1996/05/06 14:38:40 roberto Exp $
|
||||
#
|
||||
## $Id: makefile,v 1.35 2002/03/18 18:18:35 roberto Exp roberto $
|
||||
## Makefile
|
||||
## See Copyright Notice in lua.h
|
||||
#
|
||||
|
||||
#configuration
|
||||
|
||||
#CONFIGURATION
|
||||
|
||||
# define (undefine) POPEN if your system (does not) support piped I/O
|
||||
# define (undefine) POSIX if your system is (not) POSIX compliant
|
||||
CONFIG = -DPOPEN -DPOSIX
|
||||
#
|
||||
# define (undefine) _POSIX_SOURCE if your system is (not) POSIX compliant
|
||||
#
|
||||
# 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.
|
||||
|
||||
|
||||
# DEBUG = -g -DLUA_USER_H='"ltests.h"'
|
||||
OPTIMIZE = -O2 \
|
||||
-D'lua_number2int(i,d)=__asm__("fldl %1\nfistpl %0":"=m"(i):"m"(d))' \
|
||||
-fomit-frame-pointer
|
||||
|
||||
CONFIG = -D_POSIX_SOURCE -DPOPEN $(DEBUG) $(OPTIMIZE)
|
||||
|
||||
|
||||
|
||||
# Compilation parameters
|
||||
CC = gcc
|
||||
CFLAGS = $(CONFIG) -I/usr/5include -Wall -Wmissing-prototypes -Wshadow -ansi -O2
|
||||
CWARNS = -Wall -pedantic \
|
||||
-Waggregate-return \
|
||||
-Wcast-align \
|
||||
-Wmissing-prototypes \
|
||||
-Wstrict-prototypes \
|
||||
-Wnested-externs \
|
||||
-Wpointer-arith \
|
||||
-Wshadow \
|
||||
-Wwrite-strings \
|
||||
# -Wtraditional \
|
||||
# -Wcast-qual
|
||||
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi
|
||||
|
||||
|
||||
# To make early versions
|
||||
CO_OPTIONS =
|
||||
|
||||
#CC = acc
|
||||
#CFLAGS = -fast -I/usr/5include
|
||||
|
||||
AR = ar
|
||||
ARFLAGS = rvl
|
||||
|
||||
|
||||
# Aplication modules
|
||||
LUAOBJS = \
|
||||
parser.o \
|
||||
lex.o \
|
||||
opcode.o \
|
||||
hash.o \
|
||||
table.o \
|
||||
inout.o \
|
||||
tree.o \
|
||||
fallback.o \
|
||||
mem.o \
|
||||
func.o \
|
||||
undump.o
|
||||
lstate.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 \
|
||||
ldebug.o \
|
||||
ltests.o
|
||||
|
||||
LIBOBJS = \
|
||||
iolib.o \
|
||||
mathlib.o \
|
||||
strlib.o
|
||||
lauxlib.o \
|
||||
lbaselib.o \
|
||||
ltablib.o \
|
||||
lmathlib.o \
|
||||
liolib.o \
|
||||
lstrlib.o \
|
||||
ldblib.o
|
||||
|
||||
|
||||
lua : lua.o lua.a lualib.a
|
||||
$(CC) $(CFLAGS) -o $@ lua.o lua.a lualib.a -lm
|
||||
lua : lua.o liblua.a liblualib.a
|
||||
$(CC) $(CFLAGS) -o $@ lua.o -L. -llua -llualib -lm
|
||||
|
||||
lua.a : $(LUAOBJS)
|
||||
liblua.a : $(LUAOBJS)
|
||||
$(AR) $(ARFLAGS) $@ $?
|
||||
ranlib lua.a
|
||||
ranlib $@
|
||||
|
||||
lualib.a : $(LIBOBJS)
|
||||
liblualib.a : $(LIBOBJS)
|
||||
$(AR) $(ARFLAGS) $@ $?
|
||||
ranlib $@
|
||||
|
||||
liblua.so.1.0 : lua.o
|
||||
ld -o liblua.so.1.0 $(LUAOBJS)
|
||||
|
||||
#y.tab.c y.tab.h : lua.stx
|
||||
# yacc++ -d lua.stx
|
||||
|
||||
#parser.c : y.tab.c
|
||||
# sed -e 's/yy/luaY_/g' -e 's/malloc\.h/stdlib\.h/g' y.tab.c > parser.c
|
||||
|
||||
#parser.h : y.tab.h
|
||||
# sed -e 's/yy/luaY_/g' y.tab.h > parser.h
|
||||
|
||||
clear :
|
||||
rcsclean
|
||||
rm -f *.o
|
||||
rm -f parser.c parser.h y.tab.c y.tab.h
|
||||
co lua.h lualib.h luadebug.h
|
||||
rm -f *.o *.a
|
||||
|
||||
|
||||
#% : RCS/%,v
|
||||
# co $@
|
||||
%.h : RCS/%.h,v
|
||||
co $(CO_OPTIONS) $@
|
||||
|
||||
%.c : RCS/%.c,v
|
||||
co $(CO_OPTIONS) $@
|
||||
|
||||
|
||||
fallback.o : fallback.c mem.h fallback.h lua.h opcode.h types.h tree.h func.h \
|
||||
table.h
|
||||
func.o : func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h mem.h
|
||||
hash.o : hash.c mem.h opcode.h lua.h types.h tree.h func.h hash.h table.h
|
||||
inout.o : inout.c lex.h opcode.h lua.h types.h tree.h func.h inout.h table.h \
|
||||
mem.h
|
||||
iolib.o : iolib.c lua.h luadebug.h lualib.h
|
||||
lex.o : lex.c mem.h tree.h types.h table.h opcode.h lua.h func.h lex.h inout.h \
|
||||
luadebug.h parser.h
|
||||
lua.o : lua.c lua.h lualib.h
|
||||
mathlib.o : mathlib.c lualib.h lua.h
|
||||
mem.o : mem.c mem.h lua.h table.h tree.h types.h opcode.h func.h
|
||||
opcode.o : opcode.c luadebug.h lua.h mem.h opcode.h types.h tree.h func.h hash.h \
|
||||
inout.h table.h fallback.h undump.h
|
||||
parser.o : parser.c luadebug.h lua.h mem.h lex.h opcode.h types.h tree.h func.h \
|
||||
hash.h inout.h table.h
|
||||
strlib.o : strlib.c lua.h lualib.h
|
||||
table.o : table.c mem.h opcode.h lua.h types.h tree.h func.h hash.h table.h \
|
||||
inout.h fallback.h luadebug.h
|
||||
tree.o : tree.c mem.h lua.h tree.h types.h lex.h hash.h opcode.h func.h table.h
|
||||
undump.o : undump.c opcode.h lua.h types.h tree.h func.h mem.h table.h undump.h
|
||||
lapi.o: lapi.c lua.h lapi.h lobject.h llimits.h ldebug.h lstate.h \
|
||||
ltm.h ldo.h lzio.h lfunc.h lgc.h lmem.h lstring.h ltable.h lundump.h \
|
||||
lvm.h
|
||||
lauxlib.o: lauxlib.c lua.h lauxlib.h
|
||||
lbaselib.o: lbaselib.c lua.h lauxlib.h lualib.h
|
||||
lcode.o: lcode.c lua.h lcode.h llex.h lobject.h llimits.h lzio.h \
|
||||
lopcodes.h lparser.h ltable.h ldebug.h lstate.h ltm.h ldo.h lmem.h
|
||||
ldblib.o: ldblib.c lua.h lauxlib.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 ltable.h ldebug.h lstate.h ltm.h ldo.h \
|
||||
lfunc.h lstring.h lvm.h
|
||||
ldo.o: ldo.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h ldo.h \
|
||||
lzio.h lfunc.h lgc.h lmem.h lopcodes.h lparser.h ltable.h lstring.h \
|
||||
lundump.h lvm.h
|
||||
lfunc.o: lfunc.c lua.h lfunc.h lobject.h llimits.h lmem.h lstate.h \
|
||||
ltm.h
|
||||
lgc.o: lgc.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h ldo.h \
|
||||
lzio.h lfunc.h lgc.h lmem.h lstring.h ltable.h
|
||||
liolib.o: liolib.c lua.h lauxlib.h lualib.h
|
||||
llex.o: llex.c lua.h ldo.h lobject.h llimits.h lstate.h ltm.h lzio.h \
|
||||
llex.h lparser.h ltable.h lstring.h
|
||||
lmathlib.o: lmathlib.c lua.h lauxlib.h lualib.h
|
||||
lmem.o: lmem.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h ldo.h \
|
||||
lzio.h lmem.h
|
||||
lobject.o: lobject.c lua.h ldo.h lobject.h llimits.h lstate.h ltm.h \
|
||||
lzio.h lmem.h lstring.h lvm.h
|
||||
lopcodes.o: lopcodes.c lua.h lobject.h llimits.h lopcodes.h
|
||||
lparser.o: lparser.c lua.h lcode.h llex.h lobject.h llimits.h lzio.h \
|
||||
lopcodes.h lparser.h ltable.h ldebug.h lstate.h ltm.h lfunc.h lmem.h \
|
||||
lstring.h
|
||||
lstate.o: lstate.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
||||
ldo.h lzio.h lfunc.h lgc.h llex.h lmem.h lstring.h ltable.h
|
||||
lstring.o: lstring.c lua.h lmem.h llimits.h lobject.h lstate.h ltm.h \
|
||||
lstring.h
|
||||
lstrlib.o: lstrlib.c lua.h lauxlib.h lualib.h
|
||||
ltable.o: ltable.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
||||
ldo.h lzio.h lmem.h ltable.h
|
||||
ltablib.o: ltablib.c lua.h lauxlib.h lualib.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 ltable.h ldebug.h lstate.h ltm.h \
|
||||
ldo.h lfunc.h lmem.h lstring.h lualib.h
|
||||
ltm.o: ltm.c lua.h lobject.h llimits.h lstate.h ltm.h lstring.h \
|
||||
ltable.h
|
||||
lua.o: lua.c lua.h lauxlib.h lualib.h
|
||||
lundump.o: lundump.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
||||
lfunc.h lmem.h lopcodes.h lstring.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h ldo.h \
|
||||
lzio.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
|
||||
lzio.o: lzio.c lua.h llimits.h lzio.h
|
||||
|
||||
5737
manual.tex
5737
manual.tex
File diff suppressed because it is too large
Load Diff
230
mathlib.c
230
mathlib.c
@@ -1,230 +0,0 @@
|
||||
/*
|
||||
** mathlib.c
|
||||
** Mathematics library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_mathlib="$Id: mathlib.c,v 1.16 1996/04/25 14:10:00 roberto Exp roberto $";
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lualib.h"
|
||||
#include "lua.h"
|
||||
|
||||
#ifndef PI
|
||||
#define PI 3.14159265358979323846
|
||||
#endif
|
||||
#define TODEGREE(a) ((a)*180.0/PI)
|
||||
#define TORAD(a) ((a)*PI/180.0)
|
||||
|
||||
static void math_abs (void)
|
||||
{
|
||||
double d = lua_check_number(1, "abs");
|
||||
if (d < 0) d = -d;
|
||||
lua_pushnumber (d);
|
||||
}
|
||||
|
||||
|
||||
static void math_sin (void)
|
||||
{
|
||||
double d = lua_check_number(1, "sin");
|
||||
lua_pushnumber (sin(TORAD(d)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_cos (void)
|
||||
{
|
||||
double d = lua_check_number(1, "cos");
|
||||
lua_pushnumber (cos(TORAD(d)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_tan (void)
|
||||
{
|
||||
double d = lua_check_number(1, "tan");
|
||||
lua_pushnumber (tan(TORAD(d)));
|
||||
}
|
||||
|
||||
|
||||
static void math_asin (void)
|
||||
{
|
||||
double d = lua_check_number(1, "asin");
|
||||
lua_pushnumber (TODEGREE(asin(d)));
|
||||
}
|
||||
|
||||
|
||||
static void math_acos (void)
|
||||
{
|
||||
double d = lua_check_number(1, "acos");
|
||||
lua_pushnumber (TODEGREE(acos(d)));
|
||||
}
|
||||
|
||||
|
||||
static void math_atan (void)
|
||||
{
|
||||
double d = lua_check_number(1, "atan");
|
||||
lua_pushnumber (TODEGREE(atan(d)));
|
||||
}
|
||||
|
||||
|
||||
static void math_atan2 (void)
|
||||
{
|
||||
double d1 = lua_check_number(1, "atan2");
|
||||
double d2 = lua_check_number(2, "atan2");
|
||||
lua_pushnumber (TODEGREE(atan2(d1, d2)));
|
||||
}
|
||||
|
||||
|
||||
static void math_ceil (void)
|
||||
{
|
||||
double d = lua_check_number(1, "ceil");
|
||||
lua_pushnumber (ceil(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_floor (void)
|
||||
{
|
||||
double d = lua_check_number(1, "floor");
|
||||
lua_pushnumber (floor(d));
|
||||
}
|
||||
|
||||
static void math_mod (void)
|
||||
{
|
||||
int d1 = (int)lua_check_number(1, "mod");
|
||||
int d2 = (int)lua_check_number(2, "mod");
|
||||
lua_pushnumber (d1%d2);
|
||||
}
|
||||
|
||||
|
||||
static void math_sqrt (void)
|
||||
{
|
||||
double d = lua_check_number(1, "sqrt");
|
||||
lua_pushnumber (sqrt(d));
|
||||
}
|
||||
|
||||
static int old_pow;
|
||||
|
||||
static void math_pow (void)
|
||||
{
|
||||
lua_Object o1 = lua_getparam (1);
|
||||
lua_Object o2 = lua_getparam (2);
|
||||
lua_Object op = lua_getparam(3);
|
||||
if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
|
||||
{
|
||||
lua_Object old = lua_getref(old_pow);
|
||||
lua_pushobject(o1);
|
||||
lua_pushobject(o2);
|
||||
lua_pushobject(op);
|
||||
if (lua_callfunction(old) != 0)
|
||||
lua_error(NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
double d1 = lua_getnumber(o1);
|
||||
double d2 = lua_getnumber(o2);
|
||||
lua_pushnumber (pow(d1,d2));
|
||||
}
|
||||
}
|
||||
|
||||
static void math_min (void)
|
||||
{
|
||||
int i=1;
|
||||
double dmin = lua_check_number(i, "min");
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT)
|
||||
{
|
||||
double d = lua_check_number(i, "min");
|
||||
if (d < dmin) dmin = d;
|
||||
}
|
||||
lua_pushnumber (dmin);
|
||||
}
|
||||
|
||||
static void math_max (void)
|
||||
{
|
||||
int i=1;
|
||||
double dmax = lua_check_number(i, "max");
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT)
|
||||
{
|
||||
double d = lua_check_number(i, "max");
|
||||
if (d > dmax) dmax = d;
|
||||
}
|
||||
lua_pushnumber (dmax);
|
||||
}
|
||||
|
||||
static void math_log (void)
|
||||
{
|
||||
double d = lua_check_number(1, "log");
|
||||
lua_pushnumber (log(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_log10 (void)
|
||||
{
|
||||
double d = lua_check_number(1, "log10");
|
||||
lua_pushnumber (log10(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_exp (void)
|
||||
{
|
||||
double d = lua_check_number(1, "exp");
|
||||
lua_pushnumber (exp(d));
|
||||
}
|
||||
|
||||
static void math_deg (void)
|
||||
{
|
||||
float d = lua_check_number(1, "deg");
|
||||
lua_pushnumber (d*180./PI);
|
||||
}
|
||||
|
||||
static void math_rad (void)
|
||||
{
|
||||
float d = lua_check_number(1, "rad");
|
||||
lua_pushnumber (d/180.*PI);
|
||||
}
|
||||
|
||||
static void math_random (void)
|
||||
{
|
||||
lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX);
|
||||
}
|
||||
|
||||
static void math_randomseed (void)
|
||||
{
|
||||
srand(lua_check_number(1, "randomseed"));
|
||||
}
|
||||
|
||||
|
||||
static struct lua_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}
|
||||
};
|
||||
|
||||
/*
|
||||
** Open math library
|
||||
*/
|
||||
void mathlib_open (void)
|
||||
{
|
||||
luaI_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
|
||||
old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1);
|
||||
}
|
||||
|
||||
13
mathlib.h
13
mathlib.h
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
** Math library to LUA
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef strlib_h
|
||||
|
||||
void mathlib_open (void);
|
||||
|
||||
#endif
|
||||
|
||||
143
opcode.h
143
opcode.h
@@ -1,143 +0,0 @@
|
||||
/*
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: opcode.h,v 3.19 1996/03/06 13:11:23 roberto Exp $
|
||||
*/
|
||||
|
||||
#ifndef opcode_h
|
||||
#define opcode_h
|
||||
|
||||
#include "lua.h"
|
||||
#include "types.h"
|
||||
#include "tree.h"
|
||||
#include "func.h"
|
||||
|
||||
|
||||
#define FIELDS_PER_FLUSH 40
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
PUSHNIL,
|
||||
PUSH0, PUSH1, PUSH2,
|
||||
PUSHBYTE,
|
||||
PUSHWORD,
|
||||
PUSHFLOAT,
|
||||
PUSHSTRING,
|
||||
PUSHFUNCTION,
|
||||
PUSHLOCAL0, PUSHLOCAL1, PUSHLOCAL2, PUSHLOCAL3, PUSHLOCAL4,
|
||||
PUSHLOCAL5, PUSHLOCAL6, PUSHLOCAL7, PUSHLOCAL8, PUSHLOCAL9,
|
||||
PUSHLOCAL,
|
||||
PUSHGLOBAL,
|
||||
PUSHINDEXED,
|
||||
PUSHSELF,
|
||||
STORELOCAL0, STORELOCAL1, STORELOCAL2, STORELOCAL3, STORELOCAL4,
|
||||
STORELOCAL5, STORELOCAL6, STORELOCAL7, STORELOCAL8, STORELOCAL9,
|
||||
STORELOCAL,
|
||||
STOREGLOBAL,
|
||||
STOREINDEXED0,
|
||||
STOREINDEXED,
|
||||
STORELIST0,
|
||||
STORELIST,
|
||||
STORERECORD,
|
||||
ADJUST0,
|
||||
ADJUST,
|
||||
CREATEARRAY,
|
||||
EQOP,
|
||||
LTOP,
|
||||
LEOP,
|
||||
GTOP,
|
||||
GEOP,
|
||||
ADDOP,
|
||||
SUBOP,
|
||||
MULTOP,
|
||||
DIVOP,
|
||||
POWOP,
|
||||
CONCOP,
|
||||
MINUSOP,
|
||||
NOTOP,
|
||||
ONTJMP,
|
||||
ONFJMP,
|
||||
JMP,
|
||||
UPJMP,
|
||||
IFFJMP,
|
||||
IFFUPJMP,
|
||||
POP,
|
||||
CALLFUNC,
|
||||
RETCODE0,
|
||||
RETCODE,
|
||||
SETLINE
|
||||
} OpCode;
|
||||
|
||||
#define MULT_RET 255
|
||||
|
||||
|
||||
typedef union
|
||||
{
|
||||
lua_CFunction f;
|
||||
real n;
|
||||
TaggedString *ts;
|
||||
TFunc *tf;
|
||||
struct Hash *a;
|
||||
void *u;
|
||||
int i;
|
||||
} Value;
|
||||
|
||||
typedef struct Object
|
||||
{
|
||||
lua_Type tag;
|
||||
Value value;
|
||||
} Object;
|
||||
|
||||
|
||||
/* Macros to access structure members */
|
||||
#define tag(o) ((o)->tag)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define svalue(o) ((o)->value.ts->str)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define avalue(o) ((o)->value.a)
|
||||
#define fvalue(o) ((o)->value.f)
|
||||
#define uvalue(o) ((o)->value.u)
|
||||
|
||||
/* Macros to access symbol table */
|
||||
#define s_object(i) (lua_table[i].object)
|
||||
#define s_tag(i) (tag(&s_object(i)))
|
||||
#define s_nvalue(i) (nvalue(&s_object(i)))
|
||||
#define s_svalue(i) (svalue(&s_object(i)))
|
||||
#define s_avalue(i) (avalue(&s_object(i)))
|
||||
#define s_fvalue(i) (fvalue(&s_object(i)))
|
||||
#define s_uvalue(i) (uvalue(&s_object(i)))
|
||||
|
||||
typedef union
|
||||
{
|
||||
struct {Byte c1; Byte c2;} m;
|
||||
Word w;
|
||||
} CodeWord;
|
||||
#define get_word(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;}
|
||||
|
||||
typedef union
|
||||
{
|
||||
struct {Byte c1; Byte c2; Byte c3; Byte c4;} m;
|
||||
float f;
|
||||
} CodeFloat;
|
||||
#define get_float(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
|
||||
code.m.c3 = *pc++; code.m.c4 = *pc++;}
|
||||
|
||||
typedef union
|
||||
{
|
||||
struct {Byte c1; Byte c2; Byte c3; Byte c4;} m;
|
||||
TFunc *tf;
|
||||
} CodeCode;
|
||||
#define get_code(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
|
||||
code.m.c3 = *pc++; code.m.c4 = *pc++;}
|
||||
|
||||
|
||||
/* Exported functions */
|
||||
void lua_parse (TFunc *tf); /* from "lua.stx" module */
|
||||
void luaI_codedebugline (int line); /* from "lua.stx" module */
|
||||
void lua_travstack (int (*fn)(Object *));
|
||||
Object *luaI_Address (lua_Object o);
|
||||
void luaI_pushobject (Object *o);
|
||||
void luaI_gcFB (Object *o);
|
||||
int luaI_dorun (TFunc *tf);
|
||||
|
||||
#endif
|
||||
276
strlib.c
276
strlib.c
@@ -1,276 +0,0 @@
|
||||
/*
|
||||
** strlib.c
|
||||
** String library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_strlib="$Id: strlib.c,v 1.22 1996/03/22 17:57:24 roberto Exp roberto $";
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
void lua_arg_error(char *funcname)
|
||||
{
|
||||
char buff[100];
|
||||
sprintf(buff, "incorrect arguments to function `%s'", funcname);
|
||||
lua_error(buff);
|
||||
}
|
||||
|
||||
char *lua_check_string (int numArg, char *funcname)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
if (!lua_isstring(o))
|
||||
lua_arg_error(funcname);
|
||||
return lua_getstring(o);
|
||||
}
|
||||
|
||||
double lua_check_number (int numArg, char *funcname)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
if (!lua_isnumber(o))
|
||||
lua_arg_error(funcname);
|
||||
return lua_getnumber(o);
|
||||
}
|
||||
|
||||
static int lua_opt_number (int numArg, int def, char *funcname)
|
||||
{
|
||||
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||
(int)lua_check_number(numArg, funcname);
|
||||
}
|
||||
|
||||
char *luaI_addchar (int c)
|
||||
{
|
||||
static char *buff = NULL;
|
||||
static int max = 0;
|
||||
static int n = 0;
|
||||
if (n >= max)
|
||||
{
|
||||
if (max == 0)
|
||||
{
|
||||
max = 100;
|
||||
buff = (char *)malloc(max);
|
||||
}
|
||||
else
|
||||
{
|
||||
max *= 2;
|
||||
buff = (char *)realloc(buff, max);
|
||||
}
|
||||
if (buff == NULL)
|
||||
lua_error("memory overflow");
|
||||
}
|
||||
buff[n++] = c;
|
||||
if (c == 0)
|
||||
n = 0; /* prepare for next string */
|
||||
return buff;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Return the position of the first caracter of a substring into a string
|
||||
** LUA interface:
|
||||
** n = strfind (string, substring, init, end)
|
||||
*/
|
||||
static void str_find (void)
|
||||
{
|
||||
char *s1 = lua_check_string(1, "strfind");
|
||||
char *s2 = lua_check_string(2, "strfind");
|
||||
int init = lua_opt_number(3, 1, "strfind") - 1;
|
||||
char *f = (init>=0 && init<=strlen(s1)) ? strstr(s1+init,s2) : NULL;
|
||||
if (f != NULL)
|
||||
{
|
||||
int pos = f-s1+1;
|
||||
if (lua_opt_number(4, INT_MAX, "strfind") >= pos+strlen(s2)-1)
|
||||
lua_pushnumber (pos);
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
/*
|
||||
** Return the string length
|
||||
** LUA interface:
|
||||
** n = strlen (string)
|
||||
*/
|
||||
static void str_len (void)
|
||||
{
|
||||
char *s = lua_check_string(1, "strlen");
|
||||
lua_pushnumber(strlen(s));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Return the substring of a string, from start to end
|
||||
** LUA interface:
|
||||
** substring = strsub (string, start, end)
|
||||
*/
|
||||
static void str_sub (void)
|
||||
{
|
||||
char *s = lua_check_string(1, "strsub");
|
||||
int start = (int)lua_check_number(2, "strsub");
|
||||
int end = lua_opt_number(3, strlen(s), "strsub");
|
||||
if (end < start || start < 1 || end > strlen(s))
|
||||
lua_pushliteral("");
|
||||
else
|
||||
{
|
||||
luaI_addchar(0);
|
||||
while (start <= end)
|
||||
luaI_addchar(s[start++ - 1]);
|
||||
lua_pushstring (luaI_addchar(0));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Convert a string to lower case.
|
||||
** LUA interface:
|
||||
** lowercase = strlower (string)
|
||||
*/
|
||||
static void str_lower (void)
|
||||
{
|
||||
char *s = lua_check_string(1, "strlower");
|
||||
luaI_addchar(0);
|
||||
while (*s)
|
||||
luaI_addchar(tolower(*s++));
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Convert a string to upper case.
|
||||
** LUA interface:
|
||||
** uppercase = strupper (string)
|
||||
*/
|
||||
static void str_upper (void)
|
||||
{
|
||||
char *s = lua_check_string(1, "strupper");
|
||||
luaI_addchar(0);
|
||||
while (*s)
|
||||
luaI_addchar(toupper(*s++));
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
/*
|
||||
** get ascii value of a character in a string
|
||||
*/
|
||||
static void str_ascii (void)
|
||||
{
|
||||
char *s = lua_check_string(1, "ascii");
|
||||
int pos = lua_opt_number(2, 1, "ascii") - 1;
|
||||
if (pos<0 || pos>=strlen(s))
|
||||
lua_arg_error("ascii");
|
||||
lua_pushnumber(s[pos]);
|
||||
}
|
||||
|
||||
void luaI_addquoted (char *s)
|
||||
{
|
||||
luaI_addchar('"');
|
||||
for (; *s; s++)
|
||||
{
|
||||
if (*s == '"' || *s == '\\' || *s == '\n')
|
||||
luaI_addchar('\\');
|
||||
luaI_addchar(*s);
|
||||
}
|
||||
luaI_addchar('"');
|
||||
}
|
||||
|
||||
#define MAX_CONVERTION 2000
|
||||
#define MAX_FORMAT 50
|
||||
|
||||
static void str_format (void)
|
||||
{
|
||||
int arg = 1;
|
||||
char *strfrmt = lua_check_string(arg++, "format");
|
||||
luaI_addchar(0); /* initialize */
|
||||
while (*strfrmt)
|
||||
{
|
||||
if (*strfrmt != '%')
|
||||
luaI_addchar(*strfrmt++);
|
||||
else if (*++strfrmt == '%')
|
||||
luaI_addchar(*strfrmt++); /* %% */
|
||||
else
|
||||
{ /* format item */
|
||||
char form[MAX_FORMAT]; /* store the format ('%...') */
|
||||
char buff[MAX_CONVERTION]; /* store the formated value */
|
||||
int size = 0;
|
||||
int i = 0;
|
||||
form[i++] = '%';
|
||||
form[i] = *strfrmt++;
|
||||
while (!isalpha(form[i]))
|
||||
{
|
||||
if (isdigit(form[i]))
|
||||
{
|
||||
size = size*10 + form[i]-'0';
|
||||
if (size >= MAX_CONVERTION)
|
||||
lua_error("format size/precision too long in function `format'");
|
||||
}
|
||||
else if (form[i] == '.')
|
||||
size = 0; /* re-start */
|
||||
if (++i >= MAX_FORMAT)
|
||||
lua_error("bad format in function `format'");
|
||||
form[i] = *strfrmt++;
|
||||
}
|
||||
form[i+1] = 0; /* ends string */
|
||||
switch (form[i])
|
||||
{
|
||||
case 'q':
|
||||
luaI_addquoted(lua_check_string(arg++, "format"));
|
||||
buff[0] = '\0'; /* addchar already done */
|
||||
break;
|
||||
case 's':
|
||||
{
|
||||
char *s = lua_check_string(arg++, "format");
|
||||
if (strlen(s) >= MAX_CONVERTION)
|
||||
lua_error("string argument too long in function `format'");
|
||||
sprintf(buff, form, s);
|
||||
break;
|
||||
}
|
||||
case 'c': case 'd': case 'i': case 'o':
|
||||
case 'u': case 'x': case 'X':
|
||||
sprintf(buff, form, (int)lua_check_number(arg++, "format"));
|
||||
break;
|
||||
case 'e': case 'E': case 'f': case 'g':
|
||||
sprintf(buff, form, lua_check_number(arg++, "format"));
|
||||
break;
|
||||
default: /* also treat cases 'pnLlh' */
|
||||
lua_error("invalid format option in function `format'");
|
||||
}
|
||||
for (i=0; buff[i]; i++) /* move formated value to result */
|
||||
luaI_addchar(buff[i]);
|
||||
}
|
||||
}
|
||||
lua_pushstring(luaI_addchar(0)); /* push the result */
|
||||
}
|
||||
|
||||
|
||||
void luaI_openlib (struct lua_reg *l, int n)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<n; i++)
|
||||
lua_register(l[i].name, l[i].func);
|
||||
}
|
||||
|
||||
static struct lua_reg strlib[] = {
|
||||
{"strfind", str_find},
|
||||
{"strlen", str_len},
|
||||
{"strsub", str_sub},
|
||||
{"strlower", str_lower},
|
||||
{"strupper", str_upper},
|
||||
{"ascii", str_ascii},
|
||||
{"format", str_format}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Open string library
|
||||
*/
|
||||
void strlib_open (void)
|
||||
{
|
||||
luaI_openlib(strlib, (sizeof(strlib)/sizeof(strlib[0])));
|
||||
}
|
||||
13
strlib.h
13
strlib.h
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
** String library to LUA
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef strlib_h
|
||||
|
||||
void strlib_open (void);
|
||||
|
||||
#endif
|
||||
|
||||
279
table.c
279
table.c
@@ -1,279 +0,0 @@
|
||||
/*
|
||||
** table.c
|
||||
** Module to control static tables
|
||||
*/
|
||||
|
||||
char *rcs_table="$Id: table.c,v 2.53 1996/04/29 18:53:53 roberto Exp roberto $";
|
||||
|
||||
#include "mem.h"
|
||||
#include "opcode.h"
|
||||
#include "tree.h"
|
||||
#include "hash.h"
|
||||
#include "table.h"
|
||||
#include "inout.h"
|
||||
#include "lua.h"
|
||||
#include "fallback.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
#define BUFFER_BLOCK 256
|
||||
|
||||
Symbol *lua_table = NULL;
|
||||
Word lua_ntable = 0;
|
||||
static Long lua_maxsymbol = 0;
|
||||
|
||||
TaggedString **lua_constant = NULL;
|
||||
Word lua_nconstant = 0;
|
||||
static Long lua_maxconstant = 0;
|
||||
|
||||
|
||||
#define GARBAGE_BLOCK 50
|
||||
|
||||
static void lua_nextvar (void);
|
||||
|
||||
/*
|
||||
** Internal functions
|
||||
*/
|
||||
static struct {
|
||||
char *name;
|
||||
lua_CFunction func;
|
||||
} int_funcs[] = {
|
||||
{"assert", luaI_assert},
|
||||
{"dofile", lua_internaldofile},
|
||||
{"dostring", lua_internaldostring},
|
||||
{"error", luaI_error},
|
||||
{"getglobal", luaI_getglobal},
|
||||
{"next", lua_next},
|
||||
{"nextvar", lua_nextvar},
|
||||
{"print", luaI_print},
|
||||
{"setfallback", luaI_setfallback},
|
||||
{"setglobal", luaI_setglobal},
|
||||
{"tonumber", lua_obj2number},
|
||||
{"tostring", luaI_tostring},
|
||||
{"type", luaI_type}
|
||||
};
|
||||
|
||||
#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
|
||||
|
||||
|
||||
void luaI_initsymbol (void)
|
||||
{
|
||||
int i;
|
||||
lua_maxsymbol = BUFFER_BLOCK;
|
||||
lua_table = newvector(lua_maxsymbol, Symbol);
|
||||
for (i=0; i<INTFUNCSIZE; i++)
|
||||
{
|
||||
Word n = luaI_findsymbolbyname(int_funcs[i].name);
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Initialise constant table with pre-defined constants
|
||||
*/
|
||||
void luaI_initconstant (void)
|
||||
{
|
||||
lua_maxconstant = BUFFER_BLOCK;
|
||||
lua_constant = newvector(lua_maxconstant, TaggedString *);
|
||||
/* pre-register mem error messages, to avoid loop when error arises */
|
||||
luaI_findconstantbyname(tableEM);
|
||||
luaI_findconstantbyname(memEM);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Given a name, search it at symbol table and return its index. If not
|
||||
** found, allocate it.
|
||||
*/
|
||||
Word luaI_findsymbol (TaggedString *t)
|
||||
{
|
||||
if (t->varindex == NOT_USED)
|
||||
{
|
||||
if (lua_ntable == lua_maxsymbol)
|
||||
lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
|
||||
symbolEM, MAX_WORD);
|
||||
t->varindex = lua_ntable;
|
||||
lua_table[lua_ntable].varname = t;
|
||||
s_tag(lua_ntable) = LUA_T_NIL;
|
||||
lua_ntable++;
|
||||
}
|
||||
return t->varindex;
|
||||
}
|
||||
|
||||
|
||||
Word luaI_findsymbolbyname (char *name)
|
||||
{
|
||||
return luaI_findsymbol(luaI_createfixedstring(name));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Given a tree node, check it is has a correspondent constant index. If not,
|
||||
** allocate it.
|
||||
*/
|
||||
Word luaI_findconstant (TaggedString *t)
|
||||
{
|
||||
if (t->constindex == NOT_USED)
|
||||
{
|
||||
if (lua_nconstant == lua_maxconstant)
|
||||
lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
|
||||
constantEM, MAX_WORD);
|
||||
t->constindex = lua_nconstant;
|
||||
lua_constant[lua_nconstant] = t;
|
||||
lua_nconstant++;
|
||||
}
|
||||
return t->constindex;
|
||||
}
|
||||
|
||||
|
||||
Word luaI_findconstantbyname (char *name)
|
||||
{
|
||||
return luaI_findconstant(luaI_createfixedstring(name));
|
||||
}
|
||||
|
||||
TaggedString *luaI_createfixedstring (char *name)
|
||||
{
|
||||
TaggedString *ts = lua_createstring(name);
|
||||
if (!ts->marked)
|
||||
ts->marked = 2; /* avoid GC */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Traverse symbol table objects
|
||||
*/
|
||||
static char *lua_travsymbol (int (*fn)(Object *))
|
||||
{
|
||||
Word i;
|
||||
for (i=0; i<lua_ntable; i++)
|
||||
if (fn(&s_object(i)))
|
||||
return lua_table[i].varname->str;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Mark an object if it is a string or a unmarked array.
|
||||
*/
|
||||
int lua_markobject (Object *o)
|
||||
{/* if already marked, does not change mark value */
|
||||
if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
|
||||
tsvalue(o)->marked = 1;
|
||||
else if (tag(o) == LUA_T_ARRAY)
|
||||
lua_hashmark (avalue(o));
|
||||
else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)
|
||||
&& !o->value.tf->marked)
|
||||
o->value.tf->marked = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns 0 if the object is going to be (garbage) collected
|
||||
*/
|
||||
int luaI_ismarked (Object *o)
|
||||
{
|
||||
switch (o->tag)
|
||||
{
|
||||
case LUA_T_STRING:
|
||||
return o->value.ts->marked;
|
||||
case LUA_T_FUNCTION:
|
||||
return o->value.tf->marked;
|
||||
case LUA_T_ARRAY:
|
||||
return o->value.a->mark;
|
||||
default: /* nil, number, cfunction, or user data */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage collection.
|
||||
** Delete all unused strings and arrays.
|
||||
*/
|
||||
Long luaI_collectgarbage (void)
|
||||
{
|
||||
Long recovered = 0;
|
||||
lua_travstack(lua_markobject); /* mark stack objects */
|
||||
lua_travsymbol(lua_markobject); /* mark symbol table objects */
|
||||
luaI_travlock(lua_markobject); /* mark locked objects */
|
||||
luaI_travfallbacks(lua_markobject); /* mark fallbacks */
|
||||
luaI_invalidaterefs();
|
||||
recovered += lua_strcollector();
|
||||
recovered += lua_hashcollector();
|
||||
recovered += luaI_funccollector();
|
||||
return recovered;
|
||||
}
|
||||
|
||||
void lua_pack (void)
|
||||
{
|
||||
static unsigned long block = GARBAGE_BLOCK;
|
||||
static unsigned long nentity = 0; /* total of strings, arrays, etc */
|
||||
unsigned long recovered = 0;
|
||||
if (nentity++ < block) return;
|
||||
recovered = luaI_collectgarbage();
|
||||
block = block*2*(1.0 - (float)recovered/nentity);
|
||||
nentity -= recovered;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Internal function: return next global variable
|
||||
*/
|
||||
static void lua_nextvar (void)
|
||||
{
|
||||
Word next;
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (o == LUA_NOOBJECT)
|
||||
lua_error("too few arguments to function `nextvar'");
|
||||
if (lua_getparam(2) != LUA_NOOBJECT)
|
||||
lua_error("too many arguments to function `nextvar'");
|
||||
if (lua_isnil(o))
|
||||
next = 0;
|
||||
else if (!lua_isstring(o))
|
||||
{
|
||||
lua_error("incorrect argument to function `nextvar'");
|
||||
return; /* to avoid warnings */
|
||||
}
|
||||
else
|
||||
next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
|
||||
while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
|
||||
if (next >= lua_ntable)
|
||||
{
|
||||
lua_pushnil();
|
||||
lua_pushnil();
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_pushstring(lua_table[next].varname->str);
|
||||
luaI_pushobject(&s_object(next));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static Object *functofind;
|
||||
static int checkfunc (Object *o)
|
||||
{
|
||||
if (o->tag == LUA_T_FUNCTION)
|
||||
return
|
||||
((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
|
||||
&& (functofind->value.tf == o->value.tf));
|
||||
if (o->tag == LUA_T_CFUNCTION)
|
||||
return
|
||||
((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)
|
||||
&& (functofind->value.f == o->value.f));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
char *lua_getobjname (lua_Object o, char **name)
|
||||
{ /* try to find a name for given function */
|
||||
functofind = luaI_Address(o);
|
||||
if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
|
||||
return "fallback";
|
||||
else if ((*name = lua_travsymbol(checkfunc)) != NULL)
|
||||
return "global";
|
||||
else return "";
|
||||
}
|
||||
|
||||
38
table.h
38
table.h
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
** Module to control static tables
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: table.h,v 2.20 1996/03/14 15:57:19 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef table_h
|
||||
#define table_h
|
||||
|
||||
#include "tree.h"
|
||||
#include "opcode.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Object object;
|
||||
TaggedString *varname;
|
||||
} Symbol;
|
||||
|
||||
|
||||
extern Symbol *lua_table;
|
||||
extern Word lua_ntable;
|
||||
extern TaggedString **lua_constant;
|
||||
extern Word lua_nconstant;
|
||||
|
||||
void luaI_initsymbol (void);
|
||||
void luaI_initconstant (void);
|
||||
Word luaI_findsymbolbyname (char *name);
|
||||
Word luaI_findsymbol (TaggedString *t);
|
||||
Word luaI_findconstant (TaggedString *t);
|
||||
Word luaI_findconstantbyname (char *name);
|
||||
TaggedString *luaI_createfixedstring (char *str);
|
||||
int lua_markobject (Object *o);
|
||||
int luaI_ismarked (Object *o);
|
||||
Long luaI_collectgarbage (void);
|
||||
void lua_pack (void);
|
||||
|
||||
|
||||
#endif
|
||||
145
tree.c
145
tree.c
@@ -1,145 +0,0 @@
|
||||
/*
|
||||
** tree.c
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_tree="$Id: tree.c,v 1.19 1996/02/22 20:34:33 roberto Exp $";
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "mem.h"
|
||||
#include "lua.h"
|
||||
#include "tree.h"
|
||||
#include "lex.h"
|
||||
#include "hash.h"
|
||||
#include "table.h"
|
||||
|
||||
|
||||
#define NUM_HASHS 64
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
int nuse; /* number of elements (including EMPTYs) */
|
||||
TaggedString **hash;
|
||||
} stringtable;
|
||||
|
||||
static int initialized = 0;
|
||||
|
||||
static stringtable string_root[NUM_HASHS];
|
||||
|
||||
static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 2, {0}};
|
||||
|
||||
|
||||
static unsigned long hash (char *str)
|
||||
{
|
||||
unsigned long h = 0;
|
||||
while (*str)
|
||||
h = ((h<<5)-h)^(unsigned char)*(str++);
|
||||
return h;
|
||||
}
|
||||
|
||||
static void initialize (void)
|
||||
{
|
||||
initialized = 1;
|
||||
luaI_addReserved();
|
||||
luaI_initsymbol();
|
||||
luaI_initconstant();
|
||||
}
|
||||
|
||||
|
||||
static void grow (stringtable *tb)
|
||||
{
|
||||
int newsize = luaI_redimension(tb->size);
|
||||
TaggedString **newhash = newvector(newsize, TaggedString *);
|
||||
int i;
|
||||
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++;
|
||||
}
|
||||
luaI_free(tb->hash);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
static TaggedString *insert (char *str, stringtable *tb)
|
||||
{
|
||||
TaggedString *ts;
|
||||
unsigned long h = hash(str);
|
||||
int i;
|
||||
int j = -1;
|
||||
if ((Long)tb->nuse*3 >= (Long)tb->size*2)
|
||||
{
|
||||
if (!initialized)
|
||||
initialize();
|
||||
grow(tb);
|
||||
}
|
||||
i = h%tb->size;
|
||||
while (tb->hash[i])
|
||||
{
|
||||
if (tb->hash[i] == &EMPTY)
|
||||
j = i;
|
||||
else if (strcmp(str, tb->hash[i]->str) == 0)
|
||||
return tb->hash[i];
|
||||
i = (i+1)%tb->size;
|
||||
}
|
||||
/* not found */
|
||||
lua_pack();
|
||||
if (j != -1) /* is there an EMPTY space? */
|
||||
i = j;
|
||||
else
|
||||
tb->nuse++;
|
||||
ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str));
|
||||
strcpy(ts->str, str);
|
||||
ts->marked = 0;
|
||||
ts->hash = h;
|
||||
ts->varindex = ts->constindex = NOT_USED;
|
||||
return ts;
|
||||
}
|
||||
|
||||
TaggedString *lua_createstring (char *str)
|
||||
{
|
||||
return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage collection function.
|
||||
** This function traverse the string list freeing unindexed strings
|
||||
*/
|
||||
Long lua_strcollector (void)
|
||||
{
|
||||
Long counter = 0;
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++)
|
||||
{
|
||||
stringtable *tb = &string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++)
|
||||
{
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t != NULL && t->marked <= 1)
|
||||
{
|
||||
if (t->marked)
|
||||
t->marked = 0;
|
||||
else
|
||||
{
|
||||
luaI_free(t);
|
||||
tb->hash[j] = &EMPTY;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
28
tree.h
28
tree.h
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
** tree.h
|
||||
** TecCGraf - PUC-Rio
|
||||
** $Id: tree.h,v 1.13 1996/02/14 13:35:51 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef tree_h
|
||||
#define tree_h
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define NOT_USED 0xFFFE
|
||||
|
||||
|
||||
typedef struct TaggedString
|
||||
{
|
||||
Word varindex; /* != NOT_USED if this is a symbol */
|
||||
Word constindex; /* != NOT_USED if this is a constant */
|
||||
unsigned long hash; /* 0 if not initialized */
|
||||
int marked; /* for garbage collection; never collect (nor change) if > 1 */
|
||||
char str[1]; /* \0 byte already reserved */
|
||||
} TaggedString;
|
||||
|
||||
|
||||
TaggedString *lua_createstring (char *str);
|
||||
Long lua_strcollector (void);
|
||||
|
||||
#endif
|
||||
29
types.h
29
types.h
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: types.h,v 1.3 1995/02/06 19:32:43 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef types_h
|
||||
#define types_h
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#ifndef real
|
||||
#define real float
|
||||
#endif
|
||||
|
||||
#define Byte lua_Byte /* some systems have Byte as a predefined type */
|
||||
typedef unsigned char Byte; /* unsigned 8 bits */
|
||||
|
||||
#define Word lua_Word /* some systems have Word as a predefined type */
|
||||
typedef unsigned short Word; /* unsigned 16 bits */
|
||||
|
||||
#define MAX_WORD (USHRT_MAX-2) /* maximum value of a word (-2 for safety) */
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of a int (-2 for safety) */
|
||||
|
||||
#define Long lua_Long /* some systems have Long as a predefined type */
|
||||
typedef signed long Long; /* 32 bits */
|
||||
|
||||
typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
|
||||
|
||||
#endif
|
||||
326
undump.c
326
undump.c
@@ -1,326 +0,0 @@
|
||||
/*
|
||||
** undump.c
|
||||
** load bytecodes from files
|
||||
*/
|
||||
|
||||
char* rcs_undump="$Id: undump.c,v 1.13 1996/03/12 20:00:40 lhf Exp lhf $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "opcode.h"
|
||||
#include "mem.h"
|
||||
#include "table.h"
|
||||
#include "undump.h"
|
||||
|
||||
static int swapword=0;
|
||||
static int swapfloat=0;
|
||||
static TFunc* Main=NULL; /* functions in a chunk */
|
||||
static TFunc* lastF=NULL;
|
||||
|
||||
static void warn(char* s) /* TODO: remove */
|
||||
{
|
||||
#if 0
|
||||
fprintf(stderr,"undump: %s\n",s);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void FixCode(Byte* code, Byte* end) /* swap words */
|
||||
{
|
||||
Byte* p;
|
||||
for (p=code; p!=end;)
|
||||
{
|
||||
OpCode op=(OpCode)*p;
|
||||
switch (op)
|
||||
{
|
||||
case PUSHNIL:
|
||||
case PUSH0:
|
||||
case PUSH1:
|
||||
case PUSH2:
|
||||
case PUSHLOCAL0:
|
||||
case PUSHLOCAL1:
|
||||
case PUSHLOCAL2:
|
||||
case PUSHLOCAL3:
|
||||
case PUSHLOCAL4:
|
||||
case PUSHLOCAL5:
|
||||
case PUSHLOCAL6:
|
||||
case PUSHLOCAL7:
|
||||
case PUSHLOCAL8:
|
||||
case PUSHLOCAL9:
|
||||
case PUSHINDEXED:
|
||||
case STORELOCAL0:
|
||||
case STORELOCAL1:
|
||||
case STORELOCAL2:
|
||||
case STORELOCAL3:
|
||||
case STORELOCAL4:
|
||||
case STORELOCAL5:
|
||||
case STORELOCAL6:
|
||||
case STORELOCAL7:
|
||||
case STORELOCAL8:
|
||||
case STORELOCAL9:
|
||||
case STOREINDEXED0:
|
||||
case ADJUST0:
|
||||
case EQOP:
|
||||
case LTOP:
|
||||
case LEOP:
|
||||
case GTOP:
|
||||
case GEOP:
|
||||
case ADDOP:
|
||||
case SUBOP:
|
||||
case MULTOP:
|
||||
case DIVOP:
|
||||
case POWOP:
|
||||
case CONCOP:
|
||||
case MINUSOP:
|
||||
case NOTOP:
|
||||
case POP:
|
||||
case RETCODE0:
|
||||
p++;
|
||||
break;
|
||||
case PUSHBYTE:
|
||||
case PUSHLOCAL:
|
||||
case STORELOCAL:
|
||||
case STOREINDEXED:
|
||||
case STORELIST0:
|
||||
case ADJUST:
|
||||
case RETCODE:
|
||||
p+=2;
|
||||
break;
|
||||
case STORELIST:
|
||||
case CALLFUNC:
|
||||
p+=3;
|
||||
break;
|
||||
case PUSHFUNCTION:
|
||||
p+=5;
|
||||
break;
|
||||
case PUSHWORD:
|
||||
case PUSHSELF:
|
||||
case CREATEARRAY:
|
||||
case ONTJMP:
|
||||
case ONFJMP:
|
||||
case JMP:
|
||||
case UPJMP:
|
||||
case IFFJMP:
|
||||
case IFFUPJMP:
|
||||
case SETLINE:
|
||||
case PUSHSTRING:
|
||||
case PUSHGLOBAL:
|
||||
case STOREGLOBAL:
|
||||
{
|
||||
Byte t;
|
||||
t=p[1]; p[1]=p[2]; p[2]=t;
|
||||
p+=3;
|
||||
break;
|
||||
}
|
||||
case PUSHFLOAT:
|
||||
{
|
||||
Byte t;
|
||||
t=p[1]; p[1]=p[4]; p[4]=t;
|
||||
t=p[2]; p[2]=p[3]; p[3]=t;
|
||||
p+=5;
|
||||
break;
|
||||
}
|
||||
case STORERECORD:
|
||||
{
|
||||
int n=*++p;
|
||||
p++;
|
||||
while (n--)
|
||||
{
|
||||
Byte t;
|
||||
t=p[0]; p[0]=p[1]; p[1]=t;
|
||||
p+=2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
lua_error("corrupt binary file");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Unthread(Byte* code, int i, int v)
|
||||
{
|
||||
while (i!=0)
|
||||
{
|
||||
CodeWord c;
|
||||
Byte* p=code+i;
|
||||
get_word(c,p);
|
||||
i=c.w;
|
||||
c.w=v;
|
||||
p[-2]=c.m.c1;
|
||||
p[-1]=c.m.c2;
|
||||
}
|
||||
}
|
||||
|
||||
static int LoadWord(FILE* D)
|
||||
{
|
||||
Word w;
|
||||
fread(&w,sizeof(w),1,D);
|
||||
if (swapword)
|
||||
{
|
||||
Byte* p=(Byte*)&w; /* TODO: need union? */
|
||||
Byte t;
|
||||
t=p[0]; p[0]=p[1]; p[1]=t;
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
static int LoadSize(FILE* D)
|
||||
{
|
||||
Word hi=LoadWord(D);
|
||||
Word lo=LoadWord(D);
|
||||
int s=(hi<<16)|lo;
|
||||
if ((Word)s != s) lua_error("code too long");
|
||||
return s;
|
||||
}
|
||||
|
||||
static char* LoadBlock(int size, FILE* D)
|
||||
{
|
||||
char* b=luaI_malloc(size);
|
||||
fread(b,size,1,D);
|
||||
return b;
|
||||
}
|
||||
|
||||
static char* LoadString(FILE* D)
|
||||
{
|
||||
int size=LoadWord(D);
|
||||
char *b=luaI_buffer(size);
|
||||
fread(b,size,1,D);
|
||||
return b;
|
||||
}
|
||||
|
||||
static char* LoadNewString(FILE* D)
|
||||
{
|
||||
return LoadBlock(LoadWord(D),D);
|
||||
}
|
||||
|
||||
static void LoadFunction(FILE* D)
|
||||
{
|
||||
TFunc* tf=new(TFunc);
|
||||
tf->next=NULL;
|
||||
tf->locvars=NULL;
|
||||
tf->size=LoadSize(D);
|
||||
tf->lineDefined=LoadWord(D);
|
||||
if (IsMain(tf)) /* new main */
|
||||
{
|
||||
tf->fileName=LoadNewString(D);
|
||||
Main=lastF=tf;
|
||||
}
|
||||
else /* fix PUSHFUNCTION */
|
||||
{
|
||||
CodeCode c;
|
||||
Byte* p;
|
||||
tf->marked=LoadWord(D);
|
||||
tf->fileName=Main->fileName;
|
||||
p=Main->code+tf->marked;
|
||||
c.tf=tf;
|
||||
*p++=c.m.c1; *p++=c.m.c2; *p++=c.m.c3; *p++=c.m.c4;
|
||||
lastF=lastF->next=tf;
|
||||
}
|
||||
tf->code=LoadBlock(tf->size,D);
|
||||
if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
|
||||
while (1) /* unthread */
|
||||
{
|
||||
int c=getc(D);
|
||||
if (c==ID_VAR) /* global var */
|
||||
{
|
||||
int i=LoadWord(D);
|
||||
char* s=LoadString(D);
|
||||
int v=luaI_findsymbolbyname(s);
|
||||
Unthread(tf->code,i,v);
|
||||
}
|
||||
else if (c==ID_STR) /* constant string */
|
||||
{
|
||||
int i=LoadWord(D);
|
||||
char* s=LoadString(D);
|
||||
int v=luaI_findconstantbyname(s);
|
||||
Unthread(tf->code,i,v);
|
||||
}
|
||||
else
|
||||
{
|
||||
ungetc(c,D);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadSignature(FILE* D)
|
||||
{
|
||||
char* s=SIGNATURE;
|
||||
while (*s!=0 && getc(D)==*s)
|
||||
++s;
|
||||
if (*s!=0) lua_error("bad signature");
|
||||
}
|
||||
|
||||
static void LoadHeader(FILE* D) /* TODO: error handling */
|
||||
{
|
||||
Word w,tw=TEST_WORD;
|
||||
float f,tf=TEST_FLOAT;
|
||||
LoadSignature(D);
|
||||
getc(D); /* skip version */
|
||||
fread(&w,sizeof(w),1,D); /* test word */
|
||||
if (w!=tw)
|
||||
{
|
||||
swapword=1;
|
||||
warn("different byte order");
|
||||
}
|
||||
fread(&f,sizeof(f),1,D); /* test float */
|
||||
if (f!=tf)
|
||||
{
|
||||
Byte* p=(Byte*)&f; /* TODO: need union? */
|
||||
Byte t;
|
||||
swapfloat=1;
|
||||
t=p[0]; p[0]=p[3]; p[3]=t;
|
||||
t=p[1]; p[1]=p[2]; p[2]=t;
|
||||
if (f!=tf) /* TODO: try another perm? */
|
||||
lua_error("different float representation");
|
||||
else
|
||||
warn("different byte order in floats");
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadChunk(FILE* D)
|
||||
{
|
||||
LoadHeader(D);
|
||||
while (1)
|
||||
{
|
||||
int c=getc(D);
|
||||
if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; }
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** load one chunk from a file.
|
||||
** return list of functions found, headed by main, or NULL at EOF.
|
||||
*/
|
||||
TFunc* luaI_undump1(FILE* D)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
int c=getc(D);
|
||||
if (c==ID_CHUNK)
|
||||
{
|
||||
LoadChunk(D);
|
||||
return Main;
|
||||
}
|
||||
else if (c==EOF)
|
||||
return NULL;
|
||||
else
|
||||
lua_error("not a lua binary file");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** load and run all chunks in a file
|
||||
*/
|
||||
int luaI_undump(FILE* D)
|
||||
{
|
||||
TFunc* m;
|
||||
while ((m=luaI_undump1(D)))
|
||||
{
|
||||
int status=luaI_dorun(m);
|
||||
luaI_freefunc(m);
|
||||
if (status!=0) return status;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
23
undump.h
23
undump.h
@@ -1,23 +0,0 @@
|
||||
/*
|
||||
** undump.h
|
||||
** definitions for lua decompiler
|
||||
** $Id: undump.h,v 1.1 1996/03/08 21:43:21 lhf Exp lhf $
|
||||
*/
|
||||
|
||||
#include "func.h"
|
||||
|
||||
#define IsMain(f) (f->lineDefined==0)
|
||||
|
||||
/* definitions for chunk headers */
|
||||
|
||||
#define ID_CHUNK 27 /* ESC */
|
||||
#define ID_FUN 'F'
|
||||
#define ID_VAR 'V'
|
||||
#define ID_STR 'S'
|
||||
#define SIGNATURE "Lua"
|
||||
#define VERSION 0x23 /* 2.3 */
|
||||
#define TEST_WORD 0x1234 /* a word for testing byte ordering */
|
||||
#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
|
||||
|
||||
TFunc* luaI_undump1(FILE* D); /* load one chunk */
|
||||
int luaI_undump(FILE* D); /* load all chunks */
|
||||
Reference in New Issue
Block a user