mirror of
https://github.com/lua/lua.git
synced 2026-07-26 16:09:07 +00:00
Compare commits
652 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28021c5c66 | ||
|
|
73517e86b0 | ||
|
|
9e8face8d8 | ||
|
|
64864f094b | ||
|
|
9bd3fc1039 | ||
|
|
cc4a22ebe2 | ||
|
|
acf62ddfbe | ||
|
|
04f95ce879 | ||
|
|
27c6b4d422 | ||
|
|
0bb8eb5151 | ||
|
|
38da8c0d7d | ||
|
|
76de732745 | ||
|
|
90d7892007 | ||
|
|
041cb96992 | ||
|
|
c75c3cfd19 | ||
|
|
24471525d7 | ||
|
|
1f55dbc699 | ||
|
|
c194a46bc5 | ||
|
|
8954bdc706 | ||
|
|
5a228bb1d8 | ||
|
|
5f698f8b6f | ||
|
|
9b1c586b2f | ||
|
|
d9340154ac | ||
|
|
e47baca75a | ||
|
|
b8a680c114 | ||
|
|
709b27b456 | ||
|
|
dbc5451bea | ||
|
|
2d2d45976c | ||
|
|
0050d983fc | ||
|
|
04b143ddf9 | ||
|
|
007f229568 | ||
|
|
84488c5670 | ||
|
|
5142e630bf | ||
|
|
010bbd9d9c | ||
|
|
be00cd2a6b | ||
|
|
2e4e888de0 | ||
|
|
ddc144e4d2 | ||
|
|
b48c6e7680 | ||
|
|
aff97fffc4 | ||
|
|
1648674653 | ||
|
|
d404f0c276 | ||
|
|
48e42261ac | ||
|
|
be762f38bd | ||
|
|
c8a79057f7 | ||
|
|
642af82e81 | ||
|
|
636c629e56 | ||
|
|
96ba5d0bc2 | ||
|
|
f0185f7628 | ||
|
|
9bab2cf55d | ||
|
|
c4ae00a3d1 | ||
|
|
f4d3bc52f4 | ||
|
|
18765d900e | ||
|
|
43013b39cc | ||
|
|
94912d99fc | ||
|
|
b40c9f7a3c | ||
|
|
41fd639cab | ||
|
|
a845a46cc8 | ||
|
|
097edd3884 | ||
|
|
5c5d9b2703 | ||
|
|
43d1a6af12 | ||
|
|
3010eb0536 | ||
|
|
2f91f95d94 | ||
|
|
42dd080a2e | ||
|
|
9957f7d598 | ||
|
|
ac27b7a842 | ||
|
|
81e142fc14 | ||
|
|
63633c5b5f | ||
|
|
dff9be4224 | ||
|
|
118347d8c3 | ||
|
|
6820da5096 | ||
|
|
c3eb4675b9 | ||
|
|
7e0f880bc5 | ||
|
|
118e9cd843 | ||
|
|
de00d0d0ad | ||
|
|
1713b64065 | ||
|
|
671dc6eec2 | ||
|
|
e356a43dc0 | ||
|
|
f36038e42a | ||
|
|
96e15b8501 | ||
|
|
0fd91b1b08 | ||
|
|
a2b353e044 | ||
|
|
92a2ef9dca | ||
|
|
4fb9110a6e | ||
|
|
11886dc7b0 | ||
|
|
81bc5711a8 | ||
|
|
6a77a6b73f | ||
|
|
84e0b1bc97 | ||
|
|
6823a2f57f | ||
|
|
313b2fe902 | ||
|
|
425e5813b0 | ||
|
|
6505503b49 | ||
|
|
ec748fcb0a | ||
|
|
c196348717 | ||
|
|
669129a6d8 | ||
|
|
46b063ef59 | ||
|
|
e1d5153a33 | ||
|
|
b3d0682fb9 | ||
|
|
02afc892d5 | ||
|
|
fa2f1ec7ba | ||
|
|
829befcc41 | ||
|
|
d477e4ffd6 | ||
|
|
ce09af1e25 | ||
|
|
98d0b79613 | ||
|
|
e8f35fc4ff | ||
|
|
6fb0fd5063 | ||
|
|
565e6d74e1 | ||
|
|
f6bc7884be | ||
|
|
32a5d96dfc | ||
|
|
8dae071f42 | ||
|
|
afe1305b1a | ||
|
|
728fa167d2 | ||
|
|
1bdde38bd2 | ||
|
|
b2bc3b44b6 | ||
|
|
7c0ccdfd61 | ||
|
|
4964e7c8a0 | ||
|
|
2c670baf24 | ||
|
|
fdafd4f4a8 | ||
|
|
beeff4ccaf | ||
|
|
12c4e48506 | ||
|
|
c78aaa9a70 | ||
|
|
4d7a554921 | ||
|
|
fbc23d0245 | ||
|
|
81a8845e4f | ||
|
|
6c283b2f4f | ||
|
|
576bded513 | ||
|
|
ecc7769de2 | ||
|
|
da19c436cc | ||
|
|
0039feb9da | ||
|
|
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 |
68
bugs
68
bugs
@@ -235,3 +235,71 @@ Thu Oct 26 10:50:46 EDT 2000
|
||||
>> 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)
|
||||
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 5.0 alpha
|
||||
|
||||
** lgc.c
|
||||
Fri Aug 30 13:49:14 EST 2002
|
||||
>> GC metamethod stored in a weak metatable being collected together with
|
||||
>> userdata may not be cleared properly
|
||||
(by Roberto; since 5.0a)
|
||||
|
||||
** lparser.c
|
||||
Fri Dec 6 17:06:40 UTC 2002
|
||||
>> scope of generic for variables is not sound
|
||||
(by Gavin Wraith; since 5.0a)
|
||||
|
||||
|
||||
3
lapi.h
3
lapi.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.h,v 1.19 2000/08/28 17:57:04 roberto Exp roberto $
|
||||
** $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
|
||||
*/
|
||||
@@ -11,7 +11,6 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
TObject *luaA_index (lua_State *L, int index);
|
||||
void luaA_pushobject (lua_State *L, const TObject *o);
|
||||
|
||||
#endif
|
||||
|
||||
376
lauxlib.c
376
lauxlib.c
@@ -1,25 +1,90 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.42 2000/10/30 12:38:50 roberto Exp roberto $
|
||||
** $Id: lauxlib.c,v 1.90 2002/11/14 15:41:38 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/* This file uses only the official API of Lua.
|
||||
** Any function declared here could be written as an application function.
|
||||
** With care, these functions can be used by other libraries.
|
||||
*/
|
||||
|
||||
#define lauxlib_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "luadebug.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_concat(L, 2);
|
||||
return lua_error(L);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
|
||||
int i;
|
||||
@@ -29,34 +94,16 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
LUALIB_API void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
|
||||
lua_Debug ar;
|
||||
lua_getstack(L, 0, &ar);
|
||||
lua_getinfo(L, "n", &ar);
|
||||
if (ar.name == NULL)
|
||||
ar.name = "?";
|
||||
luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)",
|
||||
narg, ar.name, extramsg);
|
||||
}
|
||||
|
||||
|
||||
static void type_error (lua_State *L, int narg, int t) {
|
||||
char buff[50];
|
||||
sprintf(buff, "%.8s expected, got %.8s", lua_typename(L, t),
|
||||
lua_typename(L, lua_type(L, narg)));
|
||||
luaL_argerror(L, narg, buff);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
|
||||
if (space > lua_stackspace(L))
|
||||
luaL_verror(L, "stack overflow (%.30s)", mes);
|
||||
if (!lua_checkstack(L, space))
|
||||
luaL_error(L, "stack overflow (%s)", mes);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
|
||||
LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
|
||||
if (lua_type(L, narg) != t)
|
||||
type_error(L, narg, t);
|
||||
tag_error(L, narg, t);
|
||||
}
|
||||
|
||||
|
||||
@@ -66,55 +113,90 @@ LUALIB_API void luaL_checkany (lua_State *L, int narg) {
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
|
||||
LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
|
||||
const char *s = lua_tostring(L, narg);
|
||||
if (!s) type_error(L, narg, LUA_TSTRING);
|
||||
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_isnull(L, narg)) {
|
||||
LUALIB_API const char *luaL_optlstring (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);
|
||||
else return luaL_checklstring(L, narg, len);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API double luaL_check_number (lua_State *L, int narg) {
|
||||
double d = lua_tonumber(L, narg);
|
||||
LUALIB_API lua_Number luaL_checknumber (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 */
|
||||
type_error(L, narg, LUA_TNUMBER);
|
||||
tag_error(L, narg, LUA_TNUMBER);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API double luaL_opt_number (lua_State *L, int narg, double def) {
|
||||
if (lua_isnull(L, narg)) return def;
|
||||
else return luaL_check_number(L, narg);
|
||||
LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
|
||||
if (lua_isnoneornil(L, narg)) return def;
|
||||
else return luaL_checknumber(L, narg);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
|
||||
int i;
|
||||
for (i=0; i<n; i++)
|
||||
lua_register(L, l[i].name, l[i].func);
|
||||
LUALIB_API int luaL_getmetafield (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;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...) {
|
||||
char buff[500];
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
vsprintf(buff, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_error(L, buff);
|
||||
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
|
||||
if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
|
||||
return 0;
|
||||
lua_pushvalue(L, obj);
|
||||
lua_call(L, 1, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
|
||||
const luaL_reg *l, int nup) {
|
||||
if (libname) {
|
||||
lua_pushstring(L, libname);
|
||||
lua_gettable(L, LUA_GLOBALSINDEX); /* check whether lib already exists */
|
||||
if (lua_isnil(L, -1)) { /* no? */
|
||||
lua_pop(L, 1);
|
||||
lua_newtable(L); /* create it */
|
||||
}
|
||||
lua_insert(L, -(nup+1)); /* move library table to below upvalues */
|
||||
}
|
||||
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); /* remove upvalues */
|
||||
if (libname) {
|
||||
lua_pushstring(L, libname);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
@@ -135,29 +217,27 @@ static int emptybuffer (luaL_Buffer *B) {
|
||||
else {
|
||||
lua_pushlstring(B->L, B->buffer, l);
|
||||
B->p = B->buffer;
|
||||
B->level++;
|
||||
B->lvl++;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjuststack (luaL_Buffer *B) {
|
||||
if (B->level > 1) {
|
||||
if (B->lvl > 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) {
|
||||
if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
|
||||
toplen += l;
|
||||
toget++;
|
||||
}
|
||||
else break;
|
||||
} while (toget < B->level);
|
||||
if (toget >= 2) {
|
||||
lua_concat(L, toget);
|
||||
B->level = B->level - toget + 1;
|
||||
}
|
||||
} while (toget < B->lvl);
|
||||
lua_concat(L, toget);
|
||||
B->lvl = B->lvl - toget + 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,11 +262,8 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
|
||||
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
||||
emptybuffer(B);
|
||||
if (B->level == 0)
|
||||
lua_pushlstring(B->L, NULL, 0);
|
||||
else if (B->level > 1)
|
||||
lua_concat(B->L, B->level);
|
||||
B->level = 1;
|
||||
lua_concat(B->L, B->lvl);
|
||||
B->lvl = 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -201,7 +278,7 @@ LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
|
||||
else {
|
||||
if (emptybuffer(B))
|
||||
lua_insert(L, -2); /* put buffer before new value */
|
||||
B->level++; /* add new value into B stack */
|
||||
B->lvl++; /* add new value into B stack */
|
||||
adjuststack(B);
|
||||
}
|
||||
}
|
||||
@@ -210,7 +287,182 @@ LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
|
||||
B->L = L;
|
||||
B->p = B->buffer;
|
||||
B->level = 0;
|
||||
B->lvl = 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
int ref;
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1); /* remove from stack */
|
||||
return LUA_REFNIL; /* `nil' has a unique fixed reference */
|
||||
}
|
||||
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, int fnameindex) {
|
||||
const char *filename = lua_tostring(L, fnameindex) + 1;
|
||||
lua_pushfstring(L, "cannot read %s: %s", filename, strerror(errno));
|
||||
lua_remove(L, fnameindex);
|
||||
return LUA_ERRFILE;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
|
||||
LoadF lf;
|
||||
int status, readstatus;
|
||||
int c;
|
||||
int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
|
||||
if (filename == NULL) {
|
||||
lua_pushliteral(L, "=stdin");
|
||||
lf.f = stdin;
|
||||
}
|
||||
else {
|
||||
lua_pushfstring(L, "@%s", filename);
|
||||
lf.f = fopen(filename, "r");
|
||||
}
|
||||
if (lf.f == NULL) return errfile(L, fnameindex); /* 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, fnameindex); /* unable to reopen file */
|
||||
}
|
||||
status = lua_load(L, getF, &lf, lua_tostring(L, -1));
|
||||
readstatus = ferror(lf.f);
|
||||
if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */
|
||||
if (readstatus) {
|
||||
lua_settop(L, fnameindex); /* ignore results from `lua_load' */
|
||||
return errfile(L, fnameindex);
|
||||
}
|
||||
lua_remove(L, fnameindex);
|
||||
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");
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_insert(L, -2);
|
||||
lua_call(L, 1, 0);
|
||||
}
|
||||
else { /* no _ALERT function; print it on stderr */
|
||||
fprintf(stderr, "%s\n", lua_tostring(L, -2));
|
||||
lua_pop(L, 2); /* remove error message and _ALERT */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
82
lauxlib.h
82
lauxlib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.29 2000/10/27 16:15:53 roberto Exp roberto $
|
||||
** $Id: lauxlib.h,v 1.54 2002/09/16 19:49:45 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -20,25 +20,40 @@
|
||||
#endif
|
||||
|
||||
|
||||
struct luaL_reg {
|
||||
|
||||
typedef struct luaL_reg {
|
||||
const char *name;
|
||||
lua_CFunction func;
|
||||
};
|
||||
} luaL_reg;
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
|
||||
LUALIB_API void 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 double luaL_check_number (lua_State *L, int numArg);
|
||||
LUALIB_API double luaL_opt_number (lua_State *L, int numArg, double def);
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
|
||||
const luaL_reg *l, int nup);
|
||||
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *e);
|
||||
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *e);
|
||||
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_checklstring (lua_State *L, int numArg, size_t *l);
|
||||
LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg,
|
||||
const char *def, size_t *l);
|
||||
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int numArg);
|
||||
LUALIB_API lua_Number luaL_optnumber (lua_State *L, int nArg, lua_Number def);
|
||||
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg);
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int sz, const char *msg);
|
||||
LUALIB_API void luaL_checktype (lua_State *L, int narg, int t);
|
||||
LUALIB_API void luaL_checkany (lua_State *L, int narg);
|
||||
|
||||
LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...);
|
||||
LUALIB_API int luaL_findstring (const char *name, const char *const list[]);
|
||||
LUALIB_API void luaL_where (lua_State *L, int lvl);
|
||||
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...);
|
||||
|
||||
LUALIB_API int luaL_findstring (const char *st, const char *const lst[]);
|
||||
|
||||
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 sz,
|
||||
const char *name);
|
||||
|
||||
|
||||
|
||||
@@ -48,15 +63,14 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]);
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#define luaL_arg_check(L, cond,numarg,extramsg) if (!(cond)) \
|
||||
#define luaL_argcheck(L, cond,numarg,extramsg) if (!(cond)) \
|
||||
luaL_argerror(L, numarg,extramsg)
|
||||
#define luaL_check_string(L,n) (luaL_check_lstr(L, (n), NULL))
|
||||
#define luaL_opt_string(L,n,d) (luaL_opt_lstr(L, (n), (d), NULL))
|
||||
#define luaL_check_int(L,n) ((int)luaL_check_number(L, n))
|
||||
#define luaL_check_long(L,n) ((long)luaL_check_number(L, n))
|
||||
#define luaL_opt_int(L,n,d) ((int)luaL_opt_number(L, n,d))
|
||||
#define luaL_opt_long(L,n,d) ((long)luaL_opt_number(L, n,d))
|
||||
#define luaL_openl(L,a) luaL_openlib(L, a, (sizeof(a)/sizeof(a[0])))
|
||||
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
|
||||
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
|
||||
#define luaL_checkint(L,n) ((int)luaL_checknumber(L, n))
|
||||
#define luaL_checklong(L,n) ((long)luaL_checknumber(L, n))
|
||||
#define luaL_optint(L,n,d) ((int)luaL_optnumber(L, n,d))
|
||||
#define luaL_optlong(L,n,d) ((long)luaL_optnumber(L, n,d))
|
||||
|
||||
|
||||
/*
|
||||
@@ -73,13 +87,13 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]);
|
||||
|
||||
typedef struct luaL_Buffer {
|
||||
char *p; /* current position in buffer */
|
||||
int level;
|
||||
int lvl; /* number of strings in the stack (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)), \
|
||||
((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
|
||||
(*(B)->p++ = (char)(c)))
|
||||
|
||||
#define luaL_addsize(B,n) ((B)->p += (n))
|
||||
@@ -95,6 +109,30 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B);
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Compatibility macros
|
||||
*/
|
||||
|
||||
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 sz,
|
||||
const char *n);
|
||||
|
||||
/*
|
||||
#define luaL_check_lstr luaL_checklstring
|
||||
#define luaL_opt_lstr luaL_optlstring
|
||||
#define luaL_check_number luaL_checknumber
|
||||
#define luaL_opt_number luaL_optnumber
|
||||
#define luaL_arg_check luaL_argcheck
|
||||
#define luaL_check_string luaL_checkstring
|
||||
#define luaL_opt_string luaL_optstring
|
||||
#define luaL_check_int luaL_checkint
|
||||
#define luaL_check_long luaL_checklong
|
||||
#define luaL_opt_int luaL_optint
|
||||
#define luaL_opt_long luaL_optlong
|
||||
*/
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
910
lbaselib.c
910
lbaselib.c
File diff suppressed because it is too large
Load Diff
58
lcode.h
58
lcode.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcode.h,v 1.15 2000/06/28 20:20:36 roberto Exp roberto $
|
||||
** $Id: lcode.h,v 1.37 2002/06/12 19:16:00 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -26,45 +26,49 @@
|
||||
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_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;
|
||||
|
||||
|
||||
enum Mode {iO, iU, iS, iAB}; /* instruction format */
|
||||
#define getcode(fs,e) ((fs)->f->code[(e)->info])
|
||||
|
||||
#define VD 100 /* flag for variable delta */
|
||||
#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
|
||||
|
||||
extern const struct OpProperties {
|
||||
char mode;
|
||||
unsigned char push;
|
||||
unsigned char pop;
|
||||
} luaK_opproperties[];
|
||||
|
||||
|
||||
void luaK_error (LexState *ls, const char *msg);
|
||||
int luaK_code0 (FuncState *fs, OpCode o);
|
||||
int luaK_code1 (FuncState *fs, OpCode o, int arg1);
|
||||
int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2);
|
||||
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);
|
||||
void luaK_checkstack (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);
|
||||
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue);
|
||||
int luaK_getlabel (FuncState *fs);
|
||||
void luaK_deltastack (FuncState *fs, int delta);
|
||||
void luaK_kstr (LexState *ls, int c);
|
||||
void luaK_number (FuncState *fs, Number f);
|
||||
void luaK_adjuststack (FuncState *fs, int n);
|
||||
int luaK_lastisopen (FuncState *fs);
|
||||
void luaK_setcallreturns (FuncState *fs, int nresults);
|
||||
void luaK_tostack (LexState *ls, expdesc *v, int onlyone);
|
||||
void luaK_storevar (LexState *ls, const expdesc *var);
|
||||
void luaK_prefix (LexState *ls, UnOpr op, expdesc *v);
|
||||
void luaK_infix (LexState *ls, BinOpr op, expdesc *v);
|
||||
void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2);
|
||||
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
|
||||
|
||||
217
ldblib.c
217
ldblib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.28 2000/11/06 13:19:08 roberto Exp roberto $
|
||||
** $Id: ldblib.c,v 1.74 2002/12/04 17:38:31 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,10 +9,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ldblib_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
@@ -20,43 +21,41 @@
|
||||
static void settabss (lua_State *L, const char *i, const char *v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushstring(L, v);
|
||||
lua_settable(L, -3);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void settabsi (lua_State *L, const char *i, int v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushnumber(L, v);
|
||||
lua_settable(L, -3);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static int getinfo (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
const char *options = luaL_opt_string(L, 2, "flnSu");
|
||||
char buff[20];
|
||||
const char *options = luaL_optstring(L, 2, "flnSu");
|
||||
if (lua_isnumber(L, 1)) {
|
||||
if (!lua_getstack(L, (int)lua_tonumber(L, 1), &ar)) {
|
||||
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);
|
||||
sprintf(buff, ">%.10s", options);
|
||||
options = buff;
|
||||
}
|
||||
else
|
||||
luaL_argerror(L, 1, "function or level expected");
|
||||
return luaL_argerror(L, 1, "function or level expected");
|
||||
if (!lua_getinfo(L, options, &ar))
|
||||
luaL_argerror(L, 2, "invalid option");
|
||||
return luaL_argerror(L, 2, "invalid option");
|
||||
lua_newtable(L);
|
||||
for (; *options; options++) {
|
||||
switch (*options) {
|
||||
case 'S':
|
||||
settabss(L, "source", ar.source);
|
||||
if (ar.source)
|
||||
settabss(L, "short_src", ar.short_src);
|
||||
settabss(L, "short_src", ar.short_src);
|
||||
settabsi(L, "linedefined", ar.linedefined);
|
||||
settabss(L, "what", ar.what);
|
||||
break;
|
||||
@@ -71,9 +70,9 @@ static int getinfo (lua_State *L) {
|
||||
settabss(L, "namewhat", ar.namewhat);
|
||||
break;
|
||||
case 'f':
|
||||
lua_pushstring(L, "func");
|
||||
lua_pushliteral(L, "func");
|
||||
lua_pushvalue(L, -3);
|
||||
lua_settable(L, -3);
|
||||
lua_rawset(L, -3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -84,9 +83,9 @@ static int getinfo (lua_State *L) {
|
||||
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? */
|
||||
luaL_argerror(L, 1, "level out of range");
|
||||
name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
|
||||
if (!lua_getstack(L, luaL_checkint(L, 1), &ar)) /* level out of range? */
|
||||
return luaL_argerror(L, 1, "level out of range");
|
||||
name = lua_getlocal(L, &ar, luaL_checkint(L, 2));
|
||||
if (name) {
|
||||
lua_pushstring(L, name);
|
||||
lua_pushvalue(L, -2);
|
||||
@@ -101,88 +100,172 @@ static int getlocal (lua_State *L) {
|
||||
|
||||
static int setlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
luaL_argerror(L, 1, "level out of range");
|
||||
if (!lua_getstack(L, luaL_checkint(L, 1), &ar)) /* level out of range? */
|
||||
return luaL_argerror(L, 1, "level out of range");
|
||||
luaL_checkany(L, 3);
|
||||
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
|
||||
lua_pushstring(L, lua_setlocal(L, &ar, luaL_checkint(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* dummy variables (to define unique addresses) */
|
||||
static char key1, key2;
|
||||
#define KEY_CALLHOOK (&key1)
|
||||
#define KEY_LINEHOOK (&key2)
|
||||
static const char KEY_HOOK = 'h';
|
||||
|
||||
|
||||
static void hookf (lua_State *L, void *key) {
|
||||
lua_getregistry(L);
|
||||
lua_pushuserdata(L, key);
|
||||
lua_gettable(L, -2);
|
||||
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_pushvalue(L, 1);
|
||||
lua_rawcall(L, 1, 0);
|
||||
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 */
|
||||
lua_pop(L, 1); /* pop table */
|
||||
}
|
||||
|
||||
|
||||
static void callf (lua_State *L, lua_Debug *ar) {
|
||||
lua_pushstring(L, ar->event);
|
||||
hookf(L, KEY_CALLHOOK);
|
||||
static int makemask (const char *smask, int count) {
|
||||
int mask = 0;
|
||||
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
|
||||
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
|
||||
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
|
||||
if (count > 0) mask |= LUA_MASKCOUNT;
|
||||
return mask;
|
||||
}
|
||||
|
||||
|
||||
static void linef (lua_State *L, lua_Debug *ar) {
|
||||
lua_pushnumber(L, ar->currentline);
|
||||
hookf(L, KEY_LINEHOOK);
|
||||
static char *unmakemask (int 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 void sethook (lua_State *L, void *key, lua_Hook hook,
|
||||
lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
|
||||
lua_settop(L, 1);
|
||||
if (lua_isnil(L, 1))
|
||||
(*sethookf)(L, NULL);
|
||||
else if (lua_isfunction(L, 1))
|
||||
(*sethookf)(L, hook);
|
||||
else
|
||||
luaL_argerror(L, 1, "function expected");
|
||||
lua_getregistry(L);
|
||||
lua_pushuserdata(L, key);
|
||||
lua_pushvalue(L, -1); /* dup key */
|
||||
lua_gettable(L, -3); /* get old value */
|
||||
lua_pushvalue(L, -2); /* key (again) */
|
||||
static int sethook (lua_State *L) {
|
||||
if (lua_isnoneornil(L, 1)) {
|
||||
lua_settop(L, 1);
|
||||
lua_sethook(L, NULL, 0, 0); /* turn off hooks */
|
||||
}
|
||||
else {
|
||||
const char *smask = luaL_checkstring(L, 2);
|
||||
int count = luaL_optint(L, 3, 0);
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
lua_sethook(L, hookf, makemask(smask, count), count);
|
||||
}
|
||||
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_settable(L, -5); /* set new value */
|
||||
lua_rawset(L, LUA_REGISTRYINDEX); /* set new hook */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int setcallhook (lua_State *L) {
|
||||
sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
|
||||
static int gethook (lua_State *L) {
|
||||
char buff[5];
|
||||
int mask = lua_gethookmask(L);
|
||||
lua_Hook hook = lua_gethook(L);
|
||||
if (hook != NULL && hook != hookf) /* external hook? */
|
||||
lua_pushliteral(L, "external hook");
|
||||
else {
|
||||
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */
|
||||
}
|
||||
lua_pushstring(L, unmakemask(mask, buff));
|
||||
lua_pushnumber(L, lua_gethookcount(L));
|
||||
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 */
|
||||
else lua_pushliteral(L, "\n");
|
||||
lua_pushliteral(L, "stack traceback:");
|
||||
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, "\n\t..."); /* too many levels */
|
||||
while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
|
||||
level++;
|
||||
}
|
||||
firstpart = 0;
|
||||
continue;
|
||||
}
|
||||
lua_pushliteral(L, "\n\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_concat(L, lua_gettop(L));
|
||||
}
|
||||
lua_concat(L, lua_gettop(L));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int setlinehook (lua_State *L) {
|
||||
sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_reg dblib[] = {
|
||||
static const luaL_reg dblib[] = {
|
||||
{"getlocal", getlocal},
|
||||
{"getinfo", getinfo},
|
||||
{"setcallhook", setcallhook},
|
||||
{"setlinehook", setlinehook},
|
||||
{"setlocal", setlocal}
|
||||
{"gethook", gethook},
|
||||
{"sethook", sethook},
|
||||
{"setlocal", setlocal},
|
||||
{"debug", debug},
|
||||
{"traceback", errorfb},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API void lua_dblibopen (lua_State *L) {
|
||||
luaL_openl(L, dblib);
|
||||
LUALIB_API int lua_dblibopen (lua_State *L) {
|
||||
luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
|
||||
lua_pushliteral(L, "_TRACEBACK");
|
||||
lua_pushcfunction(L, errorfb);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
708
ldebug.c
708
ldebug.c
@@ -1,11 +1,14 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 1.49 2000/10/27 11:39:52 roberto Exp roberto $
|
||||
** $Id: ldebug.c,v 1.142 2002/12/06 17:15:35 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ldebug_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
@@ -20,447 +23,552 @@
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "luadebug.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
|
||||
static const char *getfuncname (lua_State *L, StkId f, const char **name);
|
||||
static const char *getfuncname (CallInfo *ci, const char **name);
|
||||
|
||||
|
||||
static void setnormalized (TObject *d, const TObject *s) {
|
||||
if (ttype(s) == LUA_TMARK) {
|
||||
clvalue(d) = infovalue(s)->func;
|
||||
ttype(d) = LUA_TFUNCTION;
|
||||
#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);
|
||||
}
|
||||
|
||||
|
||||
void luaG_inithooks (lua_State *L) {
|
||||
CallInfo *ci;
|
||||
for (ci = L->ci; ci != L->base_ci; ci--) /* update all `savedpc's */
|
||||
currentpc(ci);
|
||||
L->hookinit = 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** this function can be called asynchronous (e.g. during a signal)
|
||||
*/
|
||||
LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
|
||||
if (func == NULL || mask == 0) { /* turn off hooks? */
|
||||
mask = 0;
|
||||
func = NULL;
|
||||
}
|
||||
else *d = *s;
|
||||
L->hook = func;
|
||||
L->basehookcount = count;
|
||||
resethookcount(L);
|
||||
L->hookmask = cast(lu_byte, mask);
|
||||
L->hookinit = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int isLmark (StkId o) {
|
||||
return (o && ttype(o) == LUA_TMARK && !infovalue(o)->func->isC);
|
||||
LUA_API lua_Hook lua_gethook (lua_State *L) {
|
||||
return L->hook;
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook = L->callhook;
|
||||
L->callhook = func;
|
||||
return oldhook;
|
||||
LUA_API int lua_gethookmask (lua_State *L) {
|
||||
return L->hookmask;
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook = L->linehook;
|
||||
L->linehook = func;
|
||||
return oldhook;
|
||||
}
|
||||
|
||||
|
||||
static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
|
||||
int i;
|
||||
for (i = (top-1) - L->stack; i>=0; i--) {
|
||||
if (is_T_MARK(L->stack[i].ttype)) {
|
||||
if (level == 0)
|
||||
return L->stack+i;
|
||||
level--;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
LUA_API int lua_gethookcount (lua_State *L) {
|
||||
return L->basehookcount;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
StkId f = aux_stackedfunction(L, level, L->top);
|
||||
if (f == NULL) return 0; /* there is no such level */
|
||||
int status;
|
||||
int ci;
|
||||
lua_lock(L);
|
||||
ci = (L->ci - L->base_ci) - level;
|
||||
if (ci <= 0) status = 0; /* there is no such level */
|
||||
else {
|
||||
ar->_func = f;
|
||||
return 1;
|
||||
ar->i_ci = ci;
|
||||
status = 1;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int nups (StkId f) {
|
||||
switch (ttype(f)) {
|
||||
case LUA_TFUNCTION:
|
||||
return clvalue(f)->nupvalues;
|
||||
case LUA_TMARK:
|
||||
return infovalue(f)->func->nupvalues;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
|
||||
int refi;
|
||||
if (lineinfo == NULL || pc == -1)
|
||||
return -1; /* no line info or function is not active */
|
||||
refi = prefi ? *prefi : 0;
|
||||
if (lineinfo[refi] < 0)
|
||||
refline += -lineinfo[refi++];
|
||||
LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
|
||||
while (lineinfo[refi] > pc) {
|
||||
refline--;
|
||||
refi--;
|
||||
if (lineinfo[refi] < 0)
|
||||
refline -= -lineinfo[refi--];
|
||||
LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
|
||||
}
|
||||
for (;;) {
|
||||
int nextline = refline + 1;
|
||||
int nextref = refi + 1;
|
||||
if (lineinfo[nextref] < 0)
|
||||
nextline += -lineinfo[nextref++];
|
||||
LUA_ASSERT(lineinfo[nextref] >= 0, "invalid line info");
|
||||
if (lineinfo[nextref] > pc)
|
||||
break;
|
||||
refline = nextline;
|
||||
refi = nextref;
|
||||
}
|
||||
if (prefi) *prefi = refi;
|
||||
return refline;
|
||||
}
|
||||
|
||||
|
||||
static int currentpc (StkId f) {
|
||||
CallInfo *ci = infovalue(f);
|
||||
LUA_ASSERT(isLmark(f), "function has no pc");
|
||||
if (ci->pc)
|
||||
return (*ci->pc - ci->func->f.l->code) - 1;
|
||||
else
|
||||
return -1; /* function is not active */
|
||||
}
|
||||
|
||||
|
||||
static int currentline (StkId f) {
|
||||
if (!isLmark(f))
|
||||
return -1; /* only active lua functions have current-line information */
|
||||
else {
|
||||
CallInfo *ci = infovalue(f);
|
||||
int *lineinfo = ci->func->f.l->lineinfo;
|
||||
return luaG_getline(lineinfo, currentpc(f), 1, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static Proto *getluaproto (StkId f) {
|
||||
return (isLmark(f) ? infovalue(f)->func->f.l : NULL);
|
||||
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;
|
||||
StkId f = ar->_func;
|
||||
Proto *fp = getluaproto(f);
|
||||
if (!fp) return NULL; /* `f' is not a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(f));
|
||||
if (!name) return NULL;
|
||||
luaA_pushobject(L, (f+1)+(n-1)); /* push value */
|
||||
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;
|
||||
StkId f = ar->_func;
|
||||
Proto *fp = getluaproto(f);
|
||||
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) return NULL; /* `f' is not a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(f));
|
||||
if (!name || name[0] == '(') return NULL; /* `(' starts private locals */
|
||||
*((f+1)+(n-1)) = *L->top;
|
||||
if (fp) { /* is a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(ci));
|
||||
if (!name || name[0] == '(') /* `(' starts private locals */
|
||||
name = NULL;
|
||||
else
|
||||
setobjs2s(ci->base+(n-1), L->top);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
static void infoLproto (lua_Debug *ar, Proto *f) {
|
||||
ar->source = f->source->str;
|
||||
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 = NULL;
|
||||
switch (ttype(func)) {
|
||||
case LUA_TFUNCTION:
|
||||
cl = clvalue(func);
|
||||
break;
|
||||
case LUA_TMARK:
|
||||
cl = infovalue(func)->func;
|
||||
break;
|
||||
default:
|
||||
lua_error(L, "value for `lua_getinfo' is not a function");
|
||||
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->isC) {
|
||||
ar->source = "=C";
|
||||
if (cl->c.isC) {
|
||||
ar->source = "=[C]";
|
||||
ar->linedefined = -1;
|
||||
ar->what = "C";
|
||||
}
|
||||
else
|
||||
infoLproto(ar, cl->f.l);
|
||||
luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
|
||||
infoLproto(ar, cl->l.p);
|
||||
luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
|
||||
if (ar->linedefined == 0)
|
||||
ar->what = "main";
|
||||
}
|
||||
|
||||
|
||||
static const char *travtagmethods (lua_State *L, const TObject *o) {
|
||||
if (ttype(o) == LUA_TFUNCTION) {
|
||||
int e;
|
||||
for (e=0; e<TM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
if (clvalue(o) == luaT_gettm(L, t, e))
|
||||
return luaT_eventname[e];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char *travglobals (lua_State *L, const TObject *o) {
|
||||
Hash *g = L->gt;
|
||||
int i;
|
||||
for (i=0; i<g->size; i++) {
|
||||
if (luaO_equalObj(o, val(node(g, i))) &&
|
||||
ttype(key(node(g, i))) == LUA_TSTRING)
|
||||
return tsvalue(key(node(g, i)))->str;
|
||||
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, StkId f, lua_Debug *ar) {
|
||||
TObject o;
|
||||
setnormalized(&o, f);
|
||||
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, &o)) != NULL)
|
||||
if ((ar->name = travglobals(L, f)) != NULL)
|
||||
ar->namewhat = "global";
|
||||
/* not found: try tag methods */
|
||||
else if ((ar->name = travtagmethods(L, &o)) != NULL)
|
||||
ar->namewhat = "tag-method";
|
||||
else ar->namewhat = ""; /* not found at all */
|
||||
else ar->namewhat = ""; /* not found */
|
||||
}
|
||||
|
||||
|
||||
|
||||
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
StkId func;
|
||||
int isactive = (*what != '>');
|
||||
if (isactive)
|
||||
func = ar->_func;
|
||||
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 '>' */
|
||||
func = L->top - 1;
|
||||
what++; /* skip the `>' */
|
||||
ci = NULL;
|
||||
f = L->top - 1;
|
||||
}
|
||||
for (; *what; what++) {
|
||||
switch (*what) {
|
||||
case 'S': {
|
||||
funcinfo(L, ar, func);
|
||||
funcinfo(L, ar, f);
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
ar->currentline = currentline(func);
|
||||
ar->currentline = (ci) ? currentline(ci) : -1;
|
||||
break;
|
||||
}
|
||||
case 'u': {
|
||||
ar->nups = nups(func);
|
||||
ar->nups = (ttisfunction(f)) ? clvalue(f)->c.nupvalues : 0;
|
||||
break;
|
||||
}
|
||||
case 'n': {
|
||||
ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
|
||||
ar->namewhat = (ci) ? getfuncname(ci, &ar->name) : NULL;
|
||||
if (ar->namewhat == NULL)
|
||||
getname(L, func, ar);
|
||||
getname(L, f, ar);
|
||||
break;
|
||||
}
|
||||
case 'f': {
|
||||
setnormalized(L->top, func);
|
||||
incr_top; /* push function */
|
||||
setobj2s(L->top, f);
|
||||
status = 2;
|
||||
break;
|
||||
}
|
||||
default: return 0; /* invalid option */
|
||||
default: status = 0; /* invalid option */
|
||||
}
|
||||
}
|
||||
if (!isactive) L->top--; /* pop function */
|
||||
return 1;
|
||||
if (!ci) L->top--; /* pop function */
|
||||
if (status == 2) incr_top(L);
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Symbolic Execution
|
||||
** Symbolic Execution and code checker
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define check(x) if (!(x)) return 0;
|
||||
|
||||
static int pushpc (int *stack, int pc, int top, int n) {
|
||||
while (n--)
|
||||
stack[top++] = pc-1;
|
||||
return top;
|
||||
#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);
|
||||
check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
|
||||
lua_assert(pt->numparams+pt->is_vararg <= pt->maxstacksize);
|
||||
check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
|
||||
int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
|
||||
const Instruction *code = pt->code;
|
||||
int top = pt->numparams;
|
||||
int pc = 0;
|
||||
if (pt->is_vararg) /* varargs? */
|
||||
top++; /* `arg' */
|
||||
while (pc < lastpc) {
|
||||
const Instruction i = code[pc++];
|
||||
LUA_ASSERT(0 <= top && top <= pt->maxstacksize, "wrong stack");
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_RETURN: {
|
||||
LUA_ASSERT(top >= GETARG_U(i), "wrong stack");
|
||||
top = GETARG_U(i);
|
||||
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 int checkRK (const Proto *pt, int r) {
|
||||
return (r < pt->maxstacksize || (r >= MAXSTACK && r-MAXSTACK < pt->sizek));
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
else if (testOpMode(op, OpModeBrk))
|
||||
check(checkRK(pt, b));
|
||||
if (testOpMode(op, OpModeCrk))
|
||||
check(checkRK(pt, c));
|
||||
break;
|
||||
}
|
||||
case OP_TAILCALL: {
|
||||
LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
|
||||
top = GETARG_B(i);
|
||||
case iABx: {
|
||||
b = GETARG_Bx(i);
|
||||
if (testOpMode(op, OpModeK)) check(b < pt->sizek);
|
||||
break;
|
||||
}
|
||||
case OP_CALL: {
|
||||
int nresults = GETARG_B(i);
|
||||
if (nresults == MULT_RET) nresults = 1;
|
||||
LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
|
||||
top = pushpc(stack, pc, GETARG_A(i), nresults);
|
||||
case iAsBx: {
|
||||
b = GETARG_sBx(i);
|
||||
break;
|
||||
}
|
||||
case OP_PUSHNIL: {
|
||||
top = pushpc(stack, pc, top, GETARG_U(i));
|
||||
}
|
||||
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_POP: {
|
||||
top -= GETARG_U(i);
|
||||
case OP_LOADNIL: {
|
||||
if (a <= reg && reg <= b)
|
||||
last = pc; /* set registers from `a' to `b' */
|
||||
break;
|
||||
}
|
||||
case OP_SETTABLE:
|
||||
case OP_SETLIST: {
|
||||
top -= GETARG_B(i);
|
||||
case OP_GETUPVAL:
|
||||
case OP_SETUPVAL: {
|
||||
check(b < pt->nupvalues);
|
||||
break;
|
||||
}
|
||||
case OP_SETMAP: {
|
||||
top -= 2*GETARG_U(i);
|
||||
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: {
|
||||
top -= GETARG_U(i);
|
||||
stack[top++] = pc-1;
|
||||
/* `c' is a register, and at least two operands */
|
||||
check(c < MAXSTACK && b < c);
|
||||
break;
|
||||
}
|
||||
case OP_TFORLOOP:
|
||||
checkreg(pt, a+c+5);
|
||||
if (reg >= a) last = pc; /* affect all registers above base */
|
||||
/* 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: {
|
||||
top -= GETARG_B(i);
|
||||
stack[top++] = pc-1;
|
||||
break;
|
||||
}
|
||||
case OP_JMPONT:
|
||||
case OP_JMPONF: {
|
||||
int newpc = pc + GETARG_S(i);
|
||||
/* jump is forward and do not skip `lastpc'? */
|
||||
if (pc < newpc && newpc <= lastpc) {
|
||||
stack[top-1] = pc-1; /* value comes from `and'/`or' */
|
||||
pc = newpc; /* do the jump */
|
||||
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);
|
||||
}
|
||||
else
|
||||
top--; /* do not jump; pop value */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
OpCode op = GET_OPCODE(i);
|
||||
LUA_ASSERT(luaK_opproperties[op].push != VD,
|
||||
"invalid opcode for default");
|
||||
top -= luaK_opproperties[op].pop;
|
||||
LUA_ASSERT(top >= 0, "wrong stack");
|
||||
top = pushpc(stack, pc, top, luaK_opproperties[op].push);
|
||||
}
|
||||
}
|
||||
}
|
||||
return code[stack[stackpos]];
|
||||
}
|
||||
|
||||
|
||||
static const char *getobjname (lua_State *L, StkId obj, const char **name) {
|
||||
StkId func = aux_stackedfunction(L, 0, obj);
|
||||
if (!isLmark(func))
|
||||
return NULL; /* not an active Lua function */
|
||||
else {
|
||||
Proto *p = infovalue(func)->func->f.l;
|
||||
int pc = currentpc(func);
|
||||
int stackpos = obj - (func+1); /* func+1 == function base */
|
||||
Instruction i = luaG_symbexec(p, pc, stackpos);
|
||||
LUA_ASSERT(pc != -1, "function must be active");
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_GETGLOBAL: {
|
||||
*name = p->kstr[GETARG_U(i)]->str;
|
||||
return "global";
|
||||
}
|
||||
case OP_GETLOCAL: {
|
||||
*name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
|
||||
LUA_ASSERT(*name, "local must exist");
|
||||
return "local";
|
||||
}
|
||||
case OP_PUSHSELF:
|
||||
case OP_GETDOTTED: {
|
||||
*name = p->kstr[GETARG_U(i)]->str;
|
||||
return "field";
|
||||
}
|
||||
default:
|
||||
return NULL; /* no useful name found */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *getfuncname (lua_State *L, StkId f, const char **name) {
|
||||
StkId func = aux_stackedfunction(L, 0, f); /* calling function */
|
||||
if (!isLmark(func))
|
||||
return NULL; /* not an active Lua function */
|
||||
else {
|
||||
Proto *p = infovalue(func)->func->f.l;
|
||||
int pc = currentpc(func);
|
||||
Instruction i;
|
||||
if (pc == -1) return NULL; /* function is not activated */
|
||||
i = p->code[pc];
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_CALL: case OP_TAILCALL:
|
||||
return getobjname(L, (func+1)+GETARG_A(i), name);
|
||||
default:
|
||||
return NULL; /* no useful name found */
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return pt->code[last];
|
||||
}
|
||||
|
||||
#undef check
|
||||
#undef checkjump
|
||||
#undef checkreg
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, StkId o, const char *op) {
|
||||
const char *name;
|
||||
const char *kind = getobjname(L, o, &name);
|
||||
const char *t = luaO_typename(o);
|
||||
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->base, &name) : NULL;
|
||||
if (kind)
|
||||
luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
|
||||
luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
|
||||
op, kind, name, t);
|
||||
else
|
||||
luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
|
||||
luaG_runerror(L, "attempt to %s a %s value", op, t);
|
||||
}
|
||||
|
||||
|
||||
void luaG_binerror (lua_State *L, StkId p1, int t, const char *op) {
|
||||
if (ttype(p1) == t) p1++;
|
||||
LUA_ASSERT(ttype(p1) != t, "must be an error");
|
||||
luaG_typeerror(L, p1, op);
|
||||
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_ordererror (lua_State *L, StkId top) {
|
||||
const char *t1 = luaO_typename(top-2);
|
||||
const char *t2 = luaO_typename(top-1);
|
||||
void luaG_aritherror (lua_State *L, const TObject *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])
|
||||
luaO_verror(L, "attempt to compare two %.10s values", t1);
|
||||
luaG_runerror(L, "attempt to compare two %s values", t1);
|
||||
else
|
||||
luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
|
||||
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)) { /* is Lua code? */
|
||||
char buff[LUA_IDSIZE]; /* add file:line information */
|
||||
int line = currentline(ci);
|
||||
luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
|
||||
luaO_pushfstring(L, "%s:%d: %s", 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);
|
||||
setobjs2s(L->top, L->top - 1); /* move argument */
|
||||
setobjs2s(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);
|
||||
}
|
||||
|
||||
|
||||
22
ldebug.h
22
ldebug.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldebug.h,v 1.6 2000/10/02 20:10:55 roberto Exp roberto $
|
||||
** $Id: ldebug.h,v 1.31 2002/08/20 20:03:05 roberto Exp roberto $
|
||||
** Auxiliary functions from Debug Interface module
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,13 +9,23 @@
|
||||
|
||||
|
||||
#include "lstate.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, StkId o, const char *op);
|
||||
void luaG_binerror (lua_State *L, StkId p1, int t, const char *op);
|
||||
int luaG_getline (int *lineinfo, int pc, int refline, int *refi);
|
||||
void luaG_ordererror (lua_State *L, StkId top);
|
||||
#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 = L->basehookcount)
|
||||
|
||||
|
||||
void luaG_inithooks (lua_State *L);
|
||||
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, const TObject *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
|
||||
|
||||
646
ldo.c
646
ldo.c
@@ -1,22 +1,25 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 1.108 2000/10/20 16:39:03 roberto Exp roberto $
|
||||
** $Id: ldo.c,v 1.210 2002/12/04 17:28:27 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ldo_c
|
||||
|
||||
#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"
|
||||
@@ -27,359 +30,424 @@
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
/* space to handle stack overflow errors */
|
||||
#define EXTRA_STACK (2*LUA_MINSTACK)
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Error-recovery functions (based on long jumps)
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
void luaD_init (lua_State *L, int stacksize) {
|
||||
L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
|
||||
L->nblocks += stacksize*sizeof(TObject);
|
||||
L->stack_last = L->stack+(stacksize-1);
|
||||
L->stacksize = stacksize;
|
||||
L->Cbase = L->top = L->stack;
|
||||
/* 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, StkId oldtop) {
|
||||
switch (errcode) {
|
||||
case LUA_ERRMEM: {
|
||||
setsvalue2s(oldtop, luaS_new(L, MEMERRMSG));
|
||||
break;
|
||||
}
|
||||
case LUA_ERRERR: {
|
||||
setsvalue2s(oldtop, luaS_new(L, "error in error handling"));
|
||||
break;
|
||||
}
|
||||
case LUA_ERRSYNTAX:
|
||||
case LUA_ERRRUN: {
|
||||
setobjs2s(oldtop, L->top - 1); /* error message on current top */
|
||||
break;
|
||||
}
|
||||
}
|
||||
L->top = oldtop + 1;
|
||||
}
|
||||
|
||||
|
||||
void luaD_checkstack (lua_State *L, int n) {
|
||||
if (L->stack_last - L->top <= n) { /* stack overflow? */
|
||||
if (L->stack_last-L->stack > (L->stacksize-1)) {
|
||||
/* overflow while handling overflow */
|
||||
luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
|
||||
}
|
||||
else {
|
||||
L->stack_last += EXTRA_STACK; /* to be used by error message */
|
||||
lua_error(L, "stack overflow");
|
||||
}
|
||||
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) {
|
||||
if (L->top - L->stack < L->stacksize - 1)
|
||||
L->stack_last = L->stack + (L->stacksize-1);
|
||||
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;
|
||||
GCObject *up;
|
||||
L->top = (L->top - oldstack) + L->stack;
|
||||
for (up = L->openupval; up != NULL; up = up->gch.next)
|
||||
gcotouv(up)->v = (gcotouv(up)->v - oldstack) + L->stack;
|
||||
for (ci = L->base_ci; ci <= L->ci; ci++) {
|
||||
ci->top = (ci->top - oldstack) + L->stack;
|
||||
ci->base = (ci->base - oldstack) + L->stack;
|
||||
}
|
||||
L->base = L->ci->base;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Adjust stack. Set top to base+extra, pushing NILs if needed.
|
||||
** (we cannot add base+extra unless we are sure it fits in the stack;
|
||||
** otherwise the result of such operation on pointers is undefined)
|
||||
*/
|
||||
void luaD_adjusttop (lua_State *L, StkId base, int extra) {
|
||||
int diff = extra-(L->top-base);
|
||||
if (diff <= 0)
|
||||
L->top = base+extra;
|
||||
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 = cast(unsigned short, 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_checkstack(L, diff);
|
||||
while (diff--)
|
||||
ttype(L->top++) = LUA_TNIL;
|
||||
luaD_reallocCI(L, 2*L->size_ci);
|
||||
if (L->size_ci > LUA_MAXCALLS)
|
||||
luaG_runerror(L, "stack overflow");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a hole inside the stack at `pos'
|
||||
*/
|
||||
static void luaD_openstack (lua_State *L, StkId pos) {
|
||||
int i = L->top-pos;
|
||||
while (i--) pos[i+1] = pos[i];
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
|
||||
StkId old_Cbase = L->Cbase;
|
||||
StkId old_top = L->Cbase = L->top;
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||
(*hook)(L, ar);
|
||||
LUA_ASSERT(L->allowhooks == 0, "invalid allow");
|
||||
L->allowhooks = 1;
|
||||
L->top = old_top;
|
||||
L->Cbase = old_Cbase;
|
||||
}
|
||||
|
||||
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) {
|
||||
if (L->allowhooks) {
|
||||
void luaD_callhook (lua_State *L, int event, int line) {
|
||||
lua_Hook hook = L->hook;
|
||||
if (hook && L->allowhook) {
|
||||
ptrdiff_t top = savestack(L, L->top);
|
||||
ptrdiff_t ci_top = savestack(L, L->ci->top);
|
||||
lua_Debug ar;
|
||||
ar._func = func;
|
||||
ar.event = "line";
|
||||
ar.currentline = line;
|
||||
dohook(L, &ar, linehook);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
|
||||
const char *event) {
|
||||
if (L->allowhooks) {
|
||||
lua_Debug ar;
|
||||
ar._func = func;
|
||||
ar.event = event;
|
||||
infovalue(func)->pc = NULL; /* function is not active */
|
||||
dohook(L, &ar, callhook);
|
||||
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;
|
||||
L->allowhook = 0; /* cannot call hooks inside a hook */
|
||||
lua_unlock(L);
|
||||
(*hook)(L, &ar);
|
||||
lua_lock(L);
|
||||
lua_assert(!L->allowhook);
|
||||
L->allowhook = 1;
|
||||
L->ci->top = restorestack(L, ci_top);
|
||||
L->top = restorestack(L, top);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
|
||||
int nup = cl->nupvalues; /* number of upvalues */
|
||||
StkId old_Cbase = L->Cbase;
|
||||
int n;
|
||||
L->Cbase = base; /* new base for C function */
|
||||
luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
|
||||
for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
|
||||
*(L->top++) = cl->upvalue[n];
|
||||
n = (*cl->f.c)(L); /* do the actual call */
|
||||
L->Cbase = old_Cbase; /* restore old C base */
|
||||
return L->top - n; /* return index of first result */
|
||||
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 */
|
||||
setobj2n(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);
|
||||
}
|
||||
|
||||
|
||||
void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults) {
|
||||
StkId base = L->top - nParams;
|
||||
luaD_openstack(L, base);
|
||||
clvalue(base) = f;
|
||||
ttype(base) = LUA_TFUNCTION;
|
||||
luaD_call(L, base, nResults);
|
||||
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--) setobjs2s(p, p-1);
|
||||
incr_top(L);
|
||||
func = restorestack(L, funcr); /* previous call may change stack */
|
||||
setobj2s(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);
|
||||
else condhardstacktests(luaD_reallocCI(L, L->size_ci));
|
||||
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 */
|
||||
L->base = L->ci->base = restorestack(L, funcr) + 1;
|
||||
ci->top = L->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 */
|
||||
L->base = L->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 reallocate `ci' */
|
||||
}
|
||||
lua_unlock(L);
|
||||
#ifdef LUA_COMPATUPVALUES
|
||||
lua_pushupvalues(L);
|
||||
#endif
|
||||
n = (*clvalue(L->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->base - 1; /* res == final position of 1st result */
|
||||
L->ci--;
|
||||
L->base = L->ci->base; /* restore base */
|
||||
/* move results to correct place */
|
||||
while (wanted != 0 && firstResult < L->top) {
|
||||
setobjs2s(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, the results are on the stack, starting at the original
|
||||
** When returns, all the results are on the stack, starting at the original
|
||||
** function position.
|
||||
** The number of results is nResults, unless nResults=LUA_MULTRET.
|
||||
*/
|
||||
void luaD_call (lua_State *L, StkId func, int nResults) {
|
||||
lua_Hook callhook;
|
||||
StkId firstResult;
|
||||
CallInfo ci;
|
||||
Closure *cl;
|
||||
if (ttype(func) != LUA_TFUNCTION) {
|
||||
/* `func' is not a function; check the `function' tag method */
|
||||
Closure *tm = luaT_gettmbyObj(L, func, TM_FUNCTION);
|
||||
if (tm == NULL)
|
||||
luaG_typeerror(L, func, "call");
|
||||
luaD_openstack(L, func);
|
||||
clvalue(func) = tm; /* tag method is the new function to be called */
|
||||
ttype(func) = LUA_TFUNCTION;
|
||||
if (++L->nCcalls >= LUA_MAXCCALLS) {
|
||||
if (L->nCcalls == LUA_MAXCCALLS)
|
||||
luaG_runerror(L, "stack overflow");
|
||||
else if (L->nCcalls >= (LUA_MAXCCALLS + (LUA_MAXCCALLS>>3)))
|
||||
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
|
||||
}
|
||||
cl = clvalue(func);
|
||||
ci.func = cl;
|
||||
infovalue(func) = &ci;
|
||||
ttype(func) = LUA_TMARK;
|
||||
callhook = L->callhook;
|
||||
if (callhook)
|
||||
luaD_callHook(L, func, callhook, "call");
|
||||
firstResult = (cl->isC ? callCclosure(L, cl, func+1) :
|
||||
luaV_execute(L, cl, func+1));
|
||||
if (callhook) /* same hook that was active at entry */
|
||||
luaD_callHook(L, func, callhook, "return");
|
||||
LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag");
|
||||
/* move results to `func' (to erase parameters and function) */
|
||||
if (nResults == LUA_MULTRET) {
|
||||
while (firstResult < L->top) /* copy all results */
|
||||
*func++ = *firstResult++;
|
||||
L->top = func;
|
||||
firstResult = luaD_precall(L, func);
|
||||
if (firstResult == NULL) /* is a Lua function? */
|
||||
firstResult = luaV_execute(L); /* call it */
|
||||
luaD_poscall(L, nResults, firstResult);
|
||||
L->nCcalls--;
|
||||
}
|
||||
|
||||
|
||||
static void resume (lua_State *L, void *ud) {
|
||||
StkId firstResult;
|
||||
int nargs = *cast(int *, ud);
|
||||
CallInfo *ci = L->ci;
|
||||
if (ci == L->base_ci) { /* no activation record? */
|
||||
if (nargs >= L->top - L->base)
|
||||
luaG_runerror(L, "cannot resume dead coroutine");
|
||||
luaD_precall(L, L->top - (nargs + 1)); /* start coroutine */
|
||||
}
|
||||
else { /* copy at most `nResults' */
|
||||
for (; nResults > 0 && firstResult < L->top; nResults--)
|
||||
*func++ = *firstResult++;
|
||||
L->top = func;
|
||||
for (; nResults > 0; nResults--) { /* if there are not enough results */
|
||||
ttype(L->top) = LUA_TNIL; /* adjust the stack */
|
||||
incr_top; /* must check stack space */
|
||||
else if (ci->state & CI_YIELD) { /* inside a yield? */
|
||||
if (ci->state & CI_C) { /* `common' 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 - nargs); /* complete it */
|
||||
if (nresults >= 0) L->top = L->ci->top;
|
||||
}
|
||||
else { /* yielded inside a hook: just continue its execution */
|
||||
ci->state &= ~CI_YIELD;
|
||||
}
|
||||
}
|
||||
luaC_checkGC(L);
|
||||
else
|
||||
luaG_runerror(L, "cannot resume non-suspended coroutine");
|
||||
firstResult = luaV_execute(L);
|
||||
if (firstResult != NULL) /* return? */
|
||||
luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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 = (struct CallS *)ud;
|
||||
luaD_call(L, c->func, c->nresults);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
|
||||
StkId func = L->top - (nargs+1); /* function to be called */
|
||||
struct CallS c;
|
||||
LUA_API int lua_resume (lua_State *L, int nargs) {
|
||||
int status;
|
||||
c.func = func; c.nresults = nresults;
|
||||
status = luaD_runprotected(L, f_call, &c);
|
||||
if (status != 0) /* an error occurred? */
|
||||
L->top = func; /* remove parameters from the stack */
|
||||
lu_byte old_allowhooks;
|
||||
lua_lock(L);
|
||||
old_allowhooks = L->allowhook;
|
||||
lua_assert(L->errfunc == 0 && L->nCcalls == 0);
|
||||
status = luaD_rawrunprotected(L, resume, &nargs);
|
||||
if (status != 0) { /* error? */
|
||||
L->ci = L->base_ci; /* go back to initial level */
|
||||
L->base = L->ci->base;
|
||||
L->nCcalls = 0;
|
||||
luaF_close(L, L->base); /* close eventual pending closures */
|
||||
seterrorobj(L, status, L->base);
|
||||
L->allowhook = old_allowhooks;
|
||||
restore_stack_limit(L);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_yield (lua_State *L, int nresults) {
|
||||
CallInfo *ci;
|
||||
lua_lock(L);
|
||||
ci = L->ci;
|
||||
if (L->nCcalls > 0)
|
||||
luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
|
||||
if (ci->state & CI_C) { /* usual yield */
|
||||
if ((ci-1)->state & CI_C)
|
||||
luaG_runerror(L, "cannot yield a C function");
|
||||
if (L->top - nresults > L->base) { /* is there garbage in the stack? */
|
||||
int i;
|
||||
for (i=0; i<nresults; i++) /* move down results */
|
||||
setobjs2s(L->base + i, L->top - nresults + i);
|
||||
L->top = L->base + nresults;
|
||||
}
|
||||
} /* else it's an yield inside a hook: nothing to do */
|
||||
ci->state |= CI_YIELD;
|
||||
lua_unlock(L);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||
ptrdiff_t old_top, ptrdiff_t ef) {
|
||||
int status;
|
||||
unsigned short oldnCcalls = L->nCcalls;
|
||||
ptrdiff_t old_ci = saveci(L, L->ci);
|
||||
lu_byte old_allowhooks = L->allowhook;
|
||||
ptrdiff_t old_errfunc = L->errfunc;
|
||||
L->errfunc = ef;
|
||||
status = luaD_rawrunprotected(L, func, u);
|
||||
if (status != 0) { /* an error occurred? */
|
||||
StkId oldtop = restorestack(L, old_top);
|
||||
luaF_close(L, oldtop); /* close eventual pending closures */
|
||||
seterrorobj(L, status, oldtop);
|
||||
L->nCcalls = oldnCcalls;
|
||||
L->ci = restoreci(L, old_ci);
|
||||
L->base = L->ci->base;
|
||||
L->allowhook = old_allowhooks;
|
||||
restore_stack_limit(L);
|
||||
}
|
||||
L->errfunc = old_errfunc;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected parser.
|
||||
*/
|
||||
struct ParserS { /* data to `f_parser' */
|
||||
struct SParser { /* data to `f_parser' */
|
||||
ZIO *z;
|
||||
Mbuffer buff; /* buffer to be used by the scanner */
|
||||
int bin;
|
||||
};
|
||||
|
||||
static void f_parser (lua_State *L, void *ud) {
|
||||
struct ParserS *p = (struct ParserS *)ud;
|
||||
Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
|
||||
luaV_Lclosure(L, tf, 0);
|
||||
struct SParser *p = cast(struct SParser *, ud);
|
||||
Proto *tf = p->bin ? luaU_undump(L, p->z, &p->buff) :
|
||||
luaY_parser(L, p->z, &p->buff);
|
||||
Closure *cl = luaF_newLclosure(L, 0, gt(L));
|
||||
cl->l.p = tf;
|
||||
setclvalue(L->top, cl);
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
static int protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||
struct ParserS p;
|
||||
unsigned long old_blocks;
|
||||
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;
|
||||
luaC_checkGC(L);
|
||||
old_blocks = L->nblocks;
|
||||
status = luaD_runprotected(L, f_parser, &p);
|
||||
luaZ_initbuffer(L, &p.buff);
|
||||
/* 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);
|
||||
luaZ_freebuffer(L, &p.buff);
|
||||
if (status == 0) {
|
||||
/* add new memory to threshold (as it probably will stay) */
|
||||
L->GCthreshold += (L->nblocks - old_blocks);
|
||||
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, oldtop);
|
||||
}
|
||||
else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
|
||||
status = LUA_ERRSYNTAX;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int parse_file (lua_State *L, const char *filename) {
|
||||
ZIO z;
|
||||
int status;
|
||||
int bin; /* flag for file mode */
|
||||
int c; /* look ahead char */
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
bin = (c == ID_CHUNK);
|
||||
if (bin && f != stdin) {
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
|
||||
}
|
||||
lua_pushstring(L, "@");
|
||||
lua_pushstring(L, (filename == NULL) ? "(stdin)" : filename);
|
||||
lua_concat(L, 2);
|
||||
filename = lua_tostring(L, -1); /* filename = '@'..filename */
|
||||
lua_pop(L, 1); /* OK: there is no GC during parser */
|
||||
luaZ_Fopen(&z, f, filename);
|
||||
status = protectedparser(L, &z, bin);
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_dofile (lua_State *L, const char *filename) {
|
||||
int status = parse_file(L, filename);
|
||||
if (status == 0) /* parse OK? */
|
||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int parse_buffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name) {
|
||||
ZIO z;
|
||||
if (!name) name = "?";
|
||||
luaZ_mopen(&z, buff, size, name);
|
||||
return protectedparser(L, &z, buff[0]==ID_CHUNK);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name) {
|
||||
int status = parse_buffer(L, buff, size, name);
|
||||
if (status == 0) /* parse OK? */
|
||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_dostring (lua_State *L, const char *str) {
|
||||
return lua_dobuffer(L, str, strlen(str), str);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Error-recover functions (based on long jumps)
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/* chain list of long jump buffers */
|
||||
struct lua_longjmp {
|
||||
jmp_buf b;
|
||||
struct lua_longjmp *previous;
|
||||
volatile int status; /* error code */
|
||||
};
|
||||
|
||||
|
||||
static void message (lua_State *L, const char *s) {
|
||||
const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
|
||||
if (ttype(em) == LUA_TFUNCTION) {
|
||||
*L->top = *em;
|
||||
incr_top;
|
||||
lua_pushstring(L, s);
|
||||
luaD_call(L, L->top-2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Reports an error, and jumps up to the available recovery label
|
||||
*/
|
||||
LUA_API void lua_error (lua_State *L, const char *s) {
|
||||
if (s) message(L, s);
|
||||
luaD_breakrun(L, LUA_ERRRUN);
|
||||
}
|
||||
|
||||
|
||||
void luaD_breakrun (lua_State *L, int errcode) {
|
||||
if (L->errorJmp) {
|
||||
L->errorJmp->status = errcode;
|
||||
longjmp(L->errorJmp->b, 1);
|
||||
}
|
||||
else {
|
||||
if (errcode != LUA_ERRMEM)
|
||||
message(L, "unable to recover; exiting\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
|
||||
StkId oldCbase = L->Cbase;
|
||||
StkId oldtop = L->top;
|
||||
struct lua_longjmp lj;
|
||||
int allowhooks = L->allowhooks;
|
||||
lj.status = 0;
|
||||
lj.previous = L->errorJmp; /* chain new error handler */
|
||||
L->errorJmp = &lj;
|
||||
if (setjmp(lj.b) == 0)
|
||||
(*f)(L, ud);
|
||||
else { /* an error occurred: restore the state */
|
||||
L->allowhooks = allowhooks;
|
||||
L->Cbase = oldCbase;
|
||||
L->top = oldtop;
|
||||
restore_stack_limit(L);
|
||||
}
|
||||
L->errorJmp = lj.previous; /* restore old error handler */
|
||||
return lj.status;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
51
ldo.h
51
ldo.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.h,v 1.27 2000/10/05 13:00:17 roberto Exp roberto $
|
||||
** $Id: ldo.h,v 1.55 2002/11/21 17:41:25 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,24 +10,51 @@
|
||||
|
||||
#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 {if (L->top == L->stack_last) luaD_checkstack(L, 1); L->top++;}
|
||||
** macro to control inclusion of some hard tests on stack reallocation
|
||||
*/
|
||||
#ifndef HARDSTACKTESTS
|
||||
#define condhardstacktests(x) { /* empty */ }
|
||||
#else
|
||||
#define condhardstacktests(x) x
|
||||
#endif
|
||||
|
||||
|
||||
void luaD_init (lua_State *L, int stacksize);
|
||||
void luaD_adjusttop (lua_State *L, StkId base, int extra);
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook);
|
||||
#define luaD_checkstack(L,n) \
|
||||
if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TObject)) \
|
||||
luaD_growstack(L, n); \
|
||||
else condhardstacktests(luaD_reallocstack(L, L->stacksize));
|
||||
|
||||
|
||||
#define incr_top(L) {luaD_checkstack(L,1); L->top++;}
|
||||
|
||||
#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, int event, int line);
|
||||
StkId luaD_precall (lua_State *L, StkId func);
|
||||
void luaD_call (lua_State *L, StkId func, int nResults);
|
||||
void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults);
|
||||
void luaD_checkstack (lua_State *L, int n);
|
||||
int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||
ptrdiff_t oldtop, ptrdiff_t ef);
|
||||
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_breakrun (lua_State *L, int errcode);
|
||||
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud);
|
||||
void luaD_throw (lua_State *L, int errcode);
|
||||
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
160
ldump.c
Normal file
160
ldump.c
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
** $Id: ldump.c,v 1.1 2002/10/25 21:31:28 roberto Exp roberto $
|
||||
** save bytecodes
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define ldump_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstate.h"
|
||||
#include "lundump.h"
|
||||
|
||||
#define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D)
|
||||
#define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D)
|
||||
|
||||
typedef struct {
|
||||
lua_State *L;
|
||||
lua_Chunkwriter write;
|
||||
void *data;
|
||||
} DumpState;
|
||||
|
||||
|
||||
static void DumpBlock(const void *b, size_t size, DumpState* D)
|
||||
{
|
||||
lua_unlock(D->L);
|
||||
(*D->write)(D->L,b,size,D->data);
|
||||
lua_lock(D->L);
|
||||
}
|
||||
|
||||
static void DumpByte(int y, DumpState* D)
|
||||
{
|
||||
char x=(char)y;
|
||||
DumpBlock(&x,sizeof(x),D);
|
||||
}
|
||||
|
||||
static void DumpInt(int x, DumpState* D)
|
||||
{
|
||||
DumpBlock(&x,sizeof(x),D);
|
||||
}
|
||||
|
||||
static void DumpSize(size_t x, DumpState* D)
|
||||
{
|
||||
DumpBlock(&x,sizeof(x),D);
|
||||
}
|
||||
|
||||
static void DumpNumber(lua_Number x, DumpState* D)
|
||||
{
|
||||
DumpBlock(&x,sizeof(x),D);
|
||||
}
|
||||
|
||||
static void DumpString(TString* s, DumpState* D)
|
||||
{
|
||||
if (s==NULL || getstr(s)==NULL)
|
||||
DumpSize(0,D);
|
||||
else
|
||||
{
|
||||
size_t size=s->tsv.len+1; /* include trailing '\0' */
|
||||
DumpSize(size,D);
|
||||
DumpBlock(getstr(s),size,D);
|
||||
}
|
||||
}
|
||||
|
||||
static void DumpCode(const Proto* f, DumpState* D)
|
||||
{
|
||||
DumpInt(f->sizecode,D);
|
||||
DumpVector(f->code,f->sizecode,sizeof(*f->code),D);
|
||||
}
|
||||
|
||||
static void DumpLocals(const Proto* f, DumpState* D)
|
||||
{
|
||||
int i,n=f->sizelocvars;
|
||||
DumpInt(n,D);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
DumpString(f->locvars[i].varname,D);
|
||||
DumpInt(f->locvars[i].startpc,D);
|
||||
DumpInt(f->locvars[i].endpc,D);
|
||||
}
|
||||
}
|
||||
|
||||
static void DumpLines(const Proto* f, DumpState* D)
|
||||
{
|
||||
DumpInt(f->sizelineinfo,D);
|
||||
DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D);
|
||||
}
|
||||
|
||||
static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
|
||||
|
||||
static void DumpConstants(const Proto* f, DumpState* D)
|
||||
{
|
||||
int i,n;
|
||||
DumpInt(n=f->sizek,D);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
const TObject* o=&f->k[i];
|
||||
DumpByte(ttype(o),D);
|
||||
switch (ttype(o))
|
||||
{
|
||||
case LUA_TNUMBER:
|
||||
DumpNumber(nvalue(o),D);
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
DumpString(tsvalue(o),D);
|
||||
break;
|
||||
case LUA_TNIL:
|
||||
break;
|
||||
default:
|
||||
lua_assert(0); /* cannot happen */
|
||||
break;
|
||||
}
|
||||
}
|
||||
DumpInt(n=f->sizep,D);
|
||||
for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
|
||||
}
|
||||
|
||||
static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
|
||||
{
|
||||
DumpString((f->source==p) ? NULL : f->source,D);
|
||||
DumpInt(f->lineDefined,D);
|
||||
DumpByte(f->nupvalues,D);
|
||||
DumpByte(f->numparams,D);
|
||||
DumpByte(f->is_vararg,D);
|
||||
DumpByte(f->maxstacksize,D);
|
||||
DumpLocals(f,D);
|
||||
DumpLines(f,D);
|
||||
DumpConstants(f,D);
|
||||
DumpCode(f,D);
|
||||
}
|
||||
|
||||
static void DumpHeader(DumpState* D)
|
||||
{
|
||||
DumpLiteral(LUA_SIGNATURE,D);
|
||||
DumpByte(VERSION,D);
|
||||
DumpByte(luaU_endianness(),D);
|
||||
DumpByte(sizeof(int),D);
|
||||
DumpByte(sizeof(size_t),D);
|
||||
DumpByte(sizeof(Instruction),D);
|
||||
DumpByte(SIZE_OP,D);
|
||||
DumpByte(SIZE_A,D);
|
||||
DumpByte(SIZE_B,D);
|
||||
DumpByte(SIZE_C,D);
|
||||
DumpByte(sizeof(lua_Number),D);
|
||||
DumpNumber(TEST_NUMBER,D);
|
||||
}
|
||||
|
||||
void luaU_dump(lua_State *L, const Proto* Main, lua_Chunkwriter w, void* data)
|
||||
{
|
||||
DumpState D;
|
||||
D.L=L;
|
||||
D.write=w;
|
||||
D.data=data;
|
||||
DumpHeader(&D);
|
||||
DumpFunction(Main,NULL,&D);
|
||||
}
|
||||
|
||||
127
lfunc.c
127
lfunc.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 1.33 2000/10/18 17:19:09 roberto Exp roberto $
|
||||
** $Id: lfunc.c,v 1.63 2002/11/14 16:15:53 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,87 +7,110 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define lfunc_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
#define sizeclosure(n) ((int)sizeof(Closure) + (int)sizeof(TObject)*((n)-1))
|
||||
#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_newclosure (lua_State *L, int nelems) {
|
||||
int size = sizeclosure(nelems);
|
||||
Closure *c = (Closure *)luaM_malloc(L, size);
|
||||
c->next = L->rootcl;
|
||||
L->rootcl = c;
|
||||
c->mark = c;
|
||||
c->nupvalues = nelems;
|
||||
L->nblocks += size;
|
||||
|
||||
Closure *luaF_newCclosure (lua_State *L, int nelems) {
|
||||
Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
|
||||
luaC_link(L, valtogco(c), LUA_TFUNCTION);
|
||||
c->c.isC = 1;
|
||||
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)));
|
||||
luaC_link(L, valtogco(c), LUA_TFUNCTION);
|
||||
c->l.isC = 0;
|
||||
c->l.g = *gt;
|
||||
c->l.nupvalues = cast(lu_byte, nelems);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
UpVal *luaF_findupval (lua_State *L, StkId level) {
|
||||
GCObject **pp = &L->openupval;
|
||||
UpVal *p;
|
||||
UpVal *v;
|
||||
while ((p = ngcotouv(*pp)) != NULL && p->v >= level) {
|
||||
if (p->v == level) return p;
|
||||
pp = &p->next;
|
||||
}
|
||||
v = luaM_new(L, UpVal); /* not found: create a new one */
|
||||
v->tt = LUA_TUPVAL;
|
||||
v->marked = 1; /* open upvalues should not be collected */
|
||||
v->v = level; /* current value lives in the stack */
|
||||
v->next = *pp; /* chain it in the proper position */
|
||||
*pp = valtogco(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
void luaF_close (lua_State *L, StkId level) {
|
||||
UpVal *p;
|
||||
while ((p = ngcotouv(L->openupval)) != NULL && p->v >= level) {
|
||||
setobj(&p->value, p->v); /* save current value (write barrier) */
|
||||
p->v = &p->value; /* now current value lives here */
|
||||
L->openupval = p->next; /* remove from `open' list */
|
||||
luaC_link(L, valtogco(p), LUA_TUPVAL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Proto *luaF_newproto (lua_State *L) {
|
||||
Proto *f = luaM_new(L, Proto);
|
||||
f->knum = NULL;
|
||||
f->nknum = 0;
|
||||
f->kstr = NULL;
|
||||
f->nkstr = 0;
|
||||
f->kproto = NULL;
|
||||
f->nkproto = 0;
|
||||
luaC_link(L, valtogco(f), LUA_TPROTO);
|
||||
f->k = NULL;
|
||||
f->sizek = 0;
|
||||
f->p = NULL;
|
||||
f->sizep = 0;
|
||||
f->code = NULL;
|
||||
f->ncode = 0;
|
||||
f->sizecode = 0;
|
||||
f->sizelineinfo = 0;
|
||||
f->nupvalues = 0;
|
||||
f->numparams = 0;
|
||||
f->is_vararg = 0;
|
||||
f->maxstacksize = 0;
|
||||
f->marked = 0;
|
||||
f->lineinfo = NULL;
|
||||
f->nlineinfo = 0;
|
||||
f->nlocvars = 0;
|
||||
f->sizelocvars = 0;
|
||||
f->locvars = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->source = NULL;
|
||||
f->next = L->rootproto; /* chain in list of protos */
|
||||
L->rootproto = f;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
static size_t protosize (Proto *f) {
|
||||
return sizeof(Proto)
|
||||
+ f->nknum*sizeof(Number)
|
||||
+ f->nkstr*sizeof(TString *)
|
||||
+ f->nkproto*sizeof(Proto *)
|
||||
+ f->ncode*sizeof(Instruction)
|
||||
+ f->nlocvars*sizeof(struct LocVar)
|
||||
+ f->nlineinfo*sizeof(int);
|
||||
}
|
||||
|
||||
|
||||
void luaF_protook (lua_State *L, Proto *f, int pc) {
|
||||
f->ncode = pc; /* signal that proto was properly created */
|
||||
L->nblocks += protosize(f);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||
if (f->ncode > 0) /* function was properly created? */
|
||||
L->nblocks -= protosize(f);
|
||||
luaM_free(L, f->code);
|
||||
luaM_free(L, f->locvars);
|
||||
luaM_free(L, f->kstr);
|
||||
luaM_free(L, f->knum);
|
||||
luaM_free(L, f->kproto);
|
||||
luaM_free(L, f->lineinfo);
|
||||
luaM_free(L, f);
|
||||
luaM_freearray(L, f->code, f->sizecode, Instruction);
|
||||
luaM_freearray(L, f->p, f->sizep, Proto *);
|
||||
luaM_freearray(L, f->k, f->sizek, TObject);
|
||||
luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
|
||||
luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
|
||||
luaM_freelem(L, f);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
L->nblocks -= sizeclosure(c->nupvalues);
|
||||
luaM_free(L, c);
|
||||
int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
|
||||
sizeLclosure(c->l.nupvalues);
|
||||
luaM_free(L, c, size);
|
||||
}
|
||||
|
||||
|
||||
@@ -97,11 +120,11 @@ void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
*/
|
||||
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
||||
int i;
|
||||
for (i = 0; i<f->nlocvars && f->locvars[i].startpc <= pc; 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 f->locvars[i].varname->str;
|
||||
return getstr(f->locvars[i].varname);
|
||||
}
|
||||
}
|
||||
return NULL; /* not found */
|
||||
|
||||
9
lfunc.h
9
lfunc.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lfunc.h,v 1.12 2000/06/26 19:28:31 roberto Exp roberto $
|
||||
** $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
|
||||
*/
|
||||
@@ -11,10 +11,11 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
|
||||
Proto *luaF_newproto (lua_State *L);
|
||||
void luaF_protook (lua_State *L, Proto *f, int pc);
|
||||
Closure *luaF_newclosure (lua_State *L, int nelems);
|
||||
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);
|
||||
|
||||
|
||||
671
lgc.c
671
lgc.c
@@ -1,11 +1,16 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 1.71 2000/10/05 13:00:17 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 1.165 2002/12/02 12:06:10 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define lgc_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
@@ -18,336 +23,474 @@
|
||||
|
||||
|
||||
typedef struct GCState {
|
||||
Hash *tmark; /* list of marked tables to be visited */
|
||||
Closure *cmark; /* list of marked closures to be visited */
|
||||
GCObject *tmark; /* list of marked objects to be traversed */
|
||||
GCObject *wk; /* list of traversed key-weak tables (to be cleared) */
|
||||
GCObject *wv; /* list of traversed value-weak tables */
|
||||
GCObject *wkv; /* list of traversed key-value weak tables */
|
||||
global_State *G;
|
||||
} GCState;
|
||||
|
||||
|
||||
/*
|
||||
** some userful bit tricks
|
||||
*/
|
||||
#define setbit(x,b) ((x) |= (1<<(b)))
|
||||
#define resetbit(x,b) ((x) &= cast(lu_byte, ~(1<<(b))))
|
||||
#define testbit(x,b) ((x) & (1<<(b)))
|
||||
|
||||
static void markobject (GCState *st, TObject *o);
|
||||
#define unmark(x) resetbit((x)->gch.marked, 0)
|
||||
#define ismarked(x) ((x)->gch.marked & ((1<<4)|1))
|
||||
|
||||
#define stringmark(s) setbit((s)->tsv.marked, 0)
|
||||
|
||||
|
||||
/* mark a string; marks larger than 1 cannot be changed */
|
||||
#define strmark(s) {if ((s)->marked == 0) (s)->marked = 1;}
|
||||
#define isfinalized(u) (!testbit((u)->uv.marked, 1))
|
||||
#define markfinalized(u) resetbit((u)->uv.marked, 1)
|
||||
|
||||
|
||||
#define KEYWEAKBIT 1
|
||||
#define VALUEWEAKBIT 2
|
||||
#define KEYWEAK (1<<KEYWEAKBIT)
|
||||
#define VALUEWEAK (1<<VALUEWEAKBIT)
|
||||
|
||||
|
||||
|
||||
static void protomark (Proto *f) {
|
||||
if (!f->marked) {
|
||||
int i;
|
||||
f->marked = 1;
|
||||
strmark(f->source);
|
||||
for (i=0; i<f->nkstr; i++)
|
||||
strmark(f->kstr[i]);
|
||||
for (i=0; i<f->nkproto; i++)
|
||||
protomark(f->kproto[i]);
|
||||
for (i=0; i<f->nlocvars; i++) /* mark local-variable names */
|
||||
strmark(f->locvars[i].varname);
|
||||
}
|
||||
}
|
||||
#define markobject(st,o) { checkconsistency(o); \
|
||||
if (iscollectable(o) && !ismarked(gcvalue(o))) reallymarkobject(st,gcvalue(o)); }
|
||||
|
||||
#define condmarkobject(st,o,c) { checkconsistency(o); \
|
||||
if (iscollectable(o) && !ismarked(gcvalue(o)) && (c)) \
|
||||
reallymarkobject(st,gcvalue(o)); }
|
||||
|
||||
#define markvalue(st,t) { if (!ismarked(valtogco(t))) \
|
||||
reallymarkobject(st, valtogco(t)); }
|
||||
|
||||
|
||||
static void markstack (lua_State *L, GCState *st) {
|
||||
StkId o;
|
||||
for (o=L->stack; o<L->top; o++)
|
||||
markobject(st, o);
|
||||
}
|
||||
|
||||
|
||||
static void marklock (lua_State *L, GCState *st) {
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++) {
|
||||
if (L->refArray[i].st == LOCK)
|
||||
markobject(st, &L->refArray[i].o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markclosure (GCState *st, Closure *cl) {
|
||||
if (!ismarked(cl)) {
|
||||
if (!cl->isC)
|
||||
protomark(cl->f.l);
|
||||
cl->mark = st->cmark; /* chain it for later traversal */
|
||||
st->cmark = cl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void marktagmethods (lua_State *L, GCState *st) {
|
||||
int e;
|
||||
for (e=0; e<TM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++) {
|
||||
Closure *cl = luaT_gettm(L, t, e);
|
||||
if (cl) markclosure(st, cl);
|
||||
static void reallymarkobject (GCState *st, GCObject *o) {
|
||||
lua_assert(!ismarked(o));
|
||||
setbit(o->gch.marked, 0); /* mark object */
|
||||
switch (o->gch.tt) {
|
||||
case LUA_TUSERDATA: {
|
||||
markvalue(st, gcotou(o)->uv.metatable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markobject (GCState *st, TObject *o) {
|
||||
switch (ttype(o)) {
|
||||
case LUA_TUSERDATA: case LUA_TSTRING:
|
||||
strmark(tsvalue(o));
|
||||
break;
|
||||
case LUA_TMARK:
|
||||
markclosure(st, infovalue(o)->func);
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
markclosure(st, clvalue(o));
|
||||
case LUA_TFUNCTION: {
|
||||
gcotocl(o)->c.gclist = st->tmark;
|
||||
st->tmark = o;
|
||||
break;
|
||||
}
|
||||
case LUA_TTABLE: {
|
||||
if (!ismarked(hvalue(o))) {
|
||||
hvalue(o)->mark = st->tmark; /* chain it in list of marked */
|
||||
st->tmark = hvalue(o);
|
||||
}
|
||||
gcotoh(o)->gclist = st->tmark;
|
||||
st->tmark = o;
|
||||
break;
|
||||
}
|
||||
default: break; /* numbers, etc */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markall (lua_State *L) {
|
||||
GCState st;
|
||||
st.cmark = NULL;
|
||||
st.tmark = L->gt; /* put table of globals in mark list */
|
||||
L->gt->mark = NULL;
|
||||
marktagmethods(L, &st); /* mark tag methods */
|
||||
markstack(L, &st); /* mark stack objects */
|
||||
marklock(L, &st); /* mark locked objects */
|
||||
for (;;) { /* mark tables and closures */
|
||||
if (st.cmark) {
|
||||
int i;
|
||||
Closure *f = st.cmark; /* get first closure from list */
|
||||
st.cmark = f->mark; /* remove it from list */
|
||||
for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
|
||||
markobject(&st, &f->upvalue[i]);
|
||||
case LUA_TTHREAD: {
|
||||
gcototh(o)->gclist = st->tmark;
|
||||
st->tmark = o;
|
||||
break;
|
||||
}
|
||||
else if (st.tmark) {
|
||||
int i;
|
||||
Hash *h = st.tmark; /* get first table from list */
|
||||
st.tmark = h->mark; /* remove it from list */
|
||||
for (i=0; i<h->size; i++) {
|
||||
Node *n = node(h, i);
|
||||
if (ttype(key(n)) != LUA_TNIL) {
|
||||
if (ttype(val(n)) == LUA_TNIL)
|
||||
luaH_remove(h, key(n)); /* dead element; try to remove it */
|
||||
markobject(&st, &n->key);
|
||||
markobject(&st, &n->val);
|
||||
}
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
gcotop(o)->gclist = st->tmark;
|
||||
st->tmark = o;
|
||||
break;
|
||||
}
|
||||
else break; /* nothing else to mark */
|
||||
default: lua_assert(o->gch.tt == LUA_TSTRING);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int hasmark (const TObject *o) {
|
||||
/* valid only for locked objects */
|
||||
switch (o->ttype) {
|
||||
case LUA_TSTRING: case LUA_TUSERDATA:
|
||||
return tsvalue(o)->marked;
|
||||
case LUA_TTABLE:
|
||||
return ismarked(hvalue(o));
|
||||
case LUA_TFUNCTION:
|
||||
return ismarked(clvalue(o));
|
||||
default: /* number */
|
||||
return 1;
|
||||
static void marktmu (GCState *st) {
|
||||
GCObject *u;
|
||||
for (u = st->G->tmudata; u; u = u->gch.next) {
|
||||
unmark(u); /* may be marked, if left from previous GC */
|
||||
reallymarkobject(st, u);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* macro for internal debugging; check if a link of free refs is valid */
|
||||
#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
|
||||
/* move `dead' udata that need finalization to list `tmudata' */
|
||||
static void separateudata (lua_State *L) {
|
||||
GCObject **p = &G(L)->rootudata;
|
||||
GCObject *curr;
|
||||
GCObject *collected = NULL; /* to collect udata with gc event */
|
||||
GCObject **lastcollected = &collected;
|
||||
while ((curr = *p) != NULL) {
|
||||
lua_assert(curr->gch.tt == LUA_TUSERDATA);
|
||||
if (ismarked(curr) || isfinalized(gcotou(curr)))
|
||||
p = &curr->gch.next; /* don't bother with them */
|
||||
|
||||
static void invalidaterefs (lua_State *L) {
|
||||
int n = L->refSize;
|
||||
else if (fasttm(L, gcotou(curr)->uv.metatable, TM_GC) == NULL) {
|
||||
markfinalized(gcotou(curr)); /* don't need finalization */
|
||||
p = &curr->gch.next;
|
||||
}
|
||||
else { /* must call its gc method */
|
||||
*p = curr->gch.next;
|
||||
curr->gch.next = NULL; /* link `curr' at the end of `collected' list */
|
||||
*lastcollected = curr;
|
||||
lastcollected = &curr->gch.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 (iscollectable(key(n)))
|
||||
setttype(key(n), LUA_TNONE); /* dead key; remove it */
|
||||
}
|
||||
|
||||
|
||||
static void traversetable (GCState *st, Table *h) {
|
||||
int i;
|
||||
for (i=0; i<n; i++) {
|
||||
struct Ref *r = &L->refArray[i];
|
||||
if (r->st == HOLD && !hasmark(&r->o))
|
||||
r->st = COLLECTED;
|
||||
LUA_ASSERT((r->st == LOCK && hasmark(&r->o)) ||
|
||||
(r->st == HOLD && hasmark(&r->o)) ||
|
||||
r->st == COLLECTED ||
|
||||
r->st == NONEXT ||
|
||||
(r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
|
||||
"inconsistent ref table");
|
||||
}
|
||||
LUA_ASSERT(VALIDLINK(L, L->refFree, n), "inconsistent ref table");
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void collectproto (lua_State *L) {
|
||||
Proto **p = &L->rootproto;
|
||||
Proto *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->marked) {
|
||||
next->marked = 0;
|
||||
p = &next->next;
|
||||
int weakkey = 0;
|
||||
int weakvalue = 0;
|
||||
const TObject *mode;
|
||||
markvalue(st, h->metatable);
|
||||
lua_assert(h->lsizenode || h->node == st->G->dummynode);
|
||||
mode = gfasttm(st->G, h->metatable, TM_MODE);
|
||||
if (mode && ttisstring(mode)) { /* is there a weak mode? */
|
||||
weakkey = (strchr(svalue(mode), 'k') != NULL);
|
||||
weakvalue = (strchr(svalue(mode), 'v') != NULL);
|
||||
if (weakkey || weakvalue) { /* is really weak? */
|
||||
GCObject **weaklist;
|
||||
h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */
|
||||
h->marked |= (weakkey << KEYWEAKBIT) | (weakvalue << VALUEWEAKBIT);
|
||||
weaklist = (weakkey && weakvalue) ? &st->wkv :
|
||||
(weakkey) ? &st->wk :
|
||||
&st->wv;
|
||||
h->gclist = *weaklist; /* must be cleared after GC, ... */
|
||||
*weaklist = valtogco(h); /* ... so put in the appropriate list */
|
||||
}
|
||||
else {
|
||||
*p = next->next;
|
||||
luaF_freeproto(L, next);
|
||||
}
|
||||
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)));
|
||||
condmarkobject(st, key(n), !weakkey);
|
||||
condmarkobject(st, val(n), !weakvalue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collectclosure (lua_State *L) {
|
||||
Closure **p = &L->rootcl;
|
||||
Closure *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (ismarked(next)) {
|
||||
next->mark = next; /* unmark */
|
||||
p = &next->next;
|
||||
}
|
||||
else {
|
||||
*p = next->next;
|
||||
luaF_freeclosure(L, next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collecttable (lua_State *L) {
|
||||
Hash **p = &L->roottable;
|
||||
Hash *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (ismarked(next)) {
|
||||
next->mark = next; /* unmark */
|
||||
p = &next->next;
|
||||
}
|
||||
else {
|
||||
*p = next->next;
|
||||
luaH_free(L, next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void checktab (lua_State *L, stringtable *tb) {
|
||||
if (tb->nuse < (lint32)(tb->size/4) && tb->size > 10)
|
||||
luaS_resize(L, tb, tb->size/2); /* table is too big */
|
||||
}
|
||||
|
||||
|
||||
static void collectstrings (lua_State *L, int all) {
|
||||
static void traverseproto (GCState *st, Proto *f) {
|
||||
int i;
|
||||
for (i=0; i<L->strt.size; i++) { /* for each list */
|
||||
TString **p = &L->strt.hash[i];
|
||||
TString *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->marked && !all) { /* preserve? */
|
||||
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
||||
next->marked = 0;
|
||||
p = &next->nexthash;
|
||||
}
|
||||
else { /* collect */
|
||||
*p = next->nexthash;
|
||||
L->strt.nuse--;
|
||||
L->nblocks -= sizestring(next->len);
|
||||
luaM_free(L, next);
|
||||
stringmark(f->source);
|
||||
for (i=0; i<f->sizek; i++) {
|
||||
if (ttisstring(f->k+i))
|
||||
stringmark(tsvalue(f->k+i));
|
||||
}
|
||||
for (i=0; i<f->sizep; i++)
|
||||
markvalue(st, f->p[i]);
|
||||
for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
|
||||
stringmark(f->locvars[i].varname);
|
||||
lua_assert(luaG_checkcode(f));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void traverseclosure (GCState *st, Closure *cl) {
|
||||
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);
|
||||
markvalue(st, hvalue(&cl->l.g));
|
||||
markvalue(st, cl->l.p);
|
||||
for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */
|
||||
UpVal *u = cl->l.upvals[i];
|
||||
if (!u->marked) {
|
||||
markobject(st, &u->value);
|
||||
u->marked = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
checktab(L, &L->strt);
|
||||
}
|
||||
|
||||
|
||||
static void collectudata (lua_State *L, int all) {
|
||||
int i;
|
||||
for (i=0; i<L->udt.size; i++) { /* for each list */
|
||||
TString **p = &L->udt.hash[i];
|
||||
TString *next;
|
||||
while ((next = *p) != NULL) {
|
||||
LUA_ASSERT(next->marked <= 1, "udata cannot be fixed");
|
||||
if (next->marked && !all) { /* preserve? */
|
||||
next->marked = 0;
|
||||
p = &next->nexthash;
|
||||
}
|
||||
else { /* collect */
|
||||
int tag = next->u.d.tag;
|
||||
*p = next->nexthash;
|
||||
next->nexthash = L->TMtable[tag].collected; /* chain udata */
|
||||
L->TMtable[tag].collected = next;
|
||||
L->nblocks -= sizestring(next->len);
|
||||
L->udt.nuse--;
|
||||
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... */
|
||||
else condhardstacktests(luaD_reallocCI(L, L->size_ci));
|
||||
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... */
|
||||
else condhardstacktests(luaD_reallocstack(L, L->stacksize));
|
||||
}
|
||||
|
||||
|
||||
static void traversestack (GCState *st, lua_State *L1) {
|
||||
StkId o, lim;
|
||||
CallInfo *ci;
|
||||
markobject(st, gt(L1));
|
||||
lim = L1->top;
|
||||
for (ci = L1->base_ci; ci <= L1->ci; ci++) {
|
||||
lua_assert(ci->top <= L1->stack_last);
|
||||
lua_assert(ci->state & (CI_C | CI_HASFRAME | CI_SAVEDPC));
|
||||
if (!(ci->state & CI_C) && lim < ci->top)
|
||||
lim = ci->top;
|
||||
}
|
||||
for (o = L1->stack; o < L1->top; o++)
|
||||
markobject(st, o);
|
||||
for (; o <= lim; o++)
|
||||
setnilvalue(o);
|
||||
checkstacksizes(L1, lim);
|
||||
}
|
||||
|
||||
|
||||
static void propagatemarks (GCState *st) {
|
||||
while (st->tmark) { /* traverse marked objects */
|
||||
switch (st->tmark->gch.tt) {
|
||||
case LUA_TTABLE: {
|
||||
Table *h = gcotoh(st->tmark);
|
||||
st->tmark = h->gclist;
|
||||
traversetable(st, h);
|
||||
break;
|
||||
}
|
||||
case LUA_TFUNCTION: {
|
||||
Closure *cl = gcotocl(st->tmark);
|
||||
st->tmark = cl->c.gclist;
|
||||
traverseclosure(st, cl);
|
||||
break;
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
lua_State *th = gcototh(st->tmark);
|
||||
st->tmark = th->gclist;
|
||||
traversestack(st, th);
|
||||
break;
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
Proto *p = gcotop(st->tmark);
|
||||
st->tmark = p->gclist;
|
||||
traverseproto(st, p);
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
checktab(L, &L->udt);
|
||||
}
|
||||
|
||||
|
||||
#define MINBUFFER 256
|
||||
static void checkMbuffer (lua_State *L) {
|
||||
if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
|
||||
size_t newsize = L->Mbuffsize/2; /* still larger than MINBUFFER */
|
||||
L->nblocks += (newsize - L->Mbuffsize)*sizeof(char);
|
||||
L->Mbuffsize = newsize;
|
||||
luaM_reallocvector(L, L->Mbuffer, newsize, char);
|
||||
static int valismarked (const TObject *o) {
|
||||
if (ttisstring(o))
|
||||
stringmark(tsvalue(o)); /* strings are `values', so are never weak */
|
||||
return !iscollectable(o) || testbit(o->value.gc->gch.marked, 0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** clear collected keys from weaktables
|
||||
*/
|
||||
static void cleartablekeys (GCObject *l) {
|
||||
while (l) {
|
||||
Table *h = gcotoh(l);
|
||||
int i = sizenode(h);
|
||||
lua_assert(h->marked & KEYWEAK);
|
||||
while (i--) {
|
||||
Node *n = node(h, i);
|
||||
if (!valismarked(key(n))) /* key was collected? */
|
||||
removekey(n); /* remove entry from table */
|
||||
}
|
||||
l = h->gclist;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void callgcTM (lua_State *L, const TObject *o) {
|
||||
Closure *tm = luaT_gettmbyObj(L, o, TM_GC);
|
||||
/*
|
||||
** clear collected values from weaktables
|
||||
*/
|
||||
static void cleartablevalues (GCObject *l) {
|
||||
while (l) {
|
||||
Table *h = gcotoh(l);
|
||||
int i = sizearray(h);
|
||||
lua_assert(h->marked & VALUEWEAK);
|
||||
while (i--) {
|
||||
TObject *o = &h->array[i];
|
||||
if (!valismarked(o)) /* value was collected? */
|
||||
setnilvalue(o); /* remove value */
|
||||
}
|
||||
i = sizenode(h);
|
||||
while (i--) {
|
||||
Node *n = node(h, i);
|
||||
if (!valismarked(val(n))) /* value was collected? */
|
||||
removekey(n); /* remove entry from table */
|
||||
}
|
||||
l = h->gclist;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void freeobj (lua_State *L, GCObject *o) {
|
||||
switch (o->gch.tt) {
|
||||
case LUA_TPROTO: luaF_freeproto(L, gcotop(o)); break;
|
||||
case LUA_TFUNCTION: luaF_freeclosure(L, gcotocl(o)); break;
|
||||
case LUA_TUPVAL: luaM_freelem(L, gcotouv(o)); break;
|
||||
case LUA_TTABLE: luaH_free(L, gcotoh(o)); break;
|
||||
case LUA_TTHREAD: {
|
||||
lua_assert(gcototh(o) != L && gcototh(o) != G(L)->mainthread);
|
||||
luaE_freethread(L, gcototh(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TSTRING: {
|
||||
luaM_free(L, o, sizestring(gcotots(o)->tsv.len));
|
||||
break;
|
||||
}
|
||||
case LUA_TUSERDATA: {
|
||||
luaM_free(L, o, sizeudata(gcotou(o)->uv.len));
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int sweeplist (lua_State *L, GCObject **p, int limit) {
|
||||
GCObject *curr;
|
||||
int count = 0; /* number of collected items */
|
||||
while ((curr = *p) != NULL) {
|
||||
if (curr->gch.marked > limit) {
|
||||
unmark(curr);
|
||||
p = &curr->gch.next;
|
||||
}
|
||||
else {
|
||||
count++;
|
||||
*p = curr->gch.next;
|
||||
freeobj(L, curr);
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
static void sweepstrings (lua_State *L, int all) {
|
||||
int i;
|
||||
for (i=0; i<G(L)->strt.size; i++) { /* for each list */
|
||||
G(L)->strt.nuse -= sweeplist(L, &G(L)->strt.hash[i], all);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void checkSizes (lua_State *L) {
|
||||
/* check size of string hash */
|
||||
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 */
|
||||
/* check size of buffer */
|
||||
if (luaZ_sizebuffer(&G(L)->buff) > LUA_MINBUFFER*2) { /* buffer too big? */
|
||||
size_t newsize = luaZ_sizebuffer(&G(L)->buff) / 2;
|
||||
luaZ_resizebuffer(L, &G(L)->buff, newsize);
|
||||
}
|
||||
G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
|
||||
}
|
||||
|
||||
|
||||
static void do1gcTM (lua_State *L, Udata *udata) {
|
||||
const TObject *tm = fasttm(L, udata->uv.metatable, TM_GC);
|
||||
if (tm != NULL) {
|
||||
int oldah = L->allowhooks;
|
||||
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
|
||||
luaD_checkstack(L, 2);
|
||||
clvalue(L->top) = tm;
|
||||
ttype(L->top) = LUA_TFUNCTION;
|
||||
*(L->top+1) = *o;
|
||||
setobj2s(L->top, tm);
|
||||
setuvalue(L->top+1, udata);
|
||||
L->top += 2;
|
||||
luaD_call(L, L->top-2, 0);
|
||||
L->allowhooks = oldah; /* restore hooks */
|
||||
luaD_call(L, L->top - 2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void callgcTMudata (lua_State *L) {
|
||||
int tag;
|
||||
TObject o;
|
||||
ttype(&o) = LUA_TUSERDATA;
|
||||
L->GCthreshold = 2*L->nblocks; /* avoid GC during tag methods */
|
||||
for (tag=L->last_tag; tag>=0; tag--) { /* for each tag (in reverse order) */
|
||||
TString *udata;
|
||||
while ((udata = L->TMtable[tag].collected) != NULL) {
|
||||
L->TMtable[tag].collected = udata->nexthash; /* remove it from list */
|
||||
tsvalue(&o) = udata;
|
||||
callgcTM(L, &o);
|
||||
luaM_free(L, udata);
|
||||
}
|
||||
static void callGCTM (lua_State *L) {
|
||||
lu_byte oldah = L->allowhook;
|
||||
L->allowhook = 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) {
|
||||
GCObject *o = G(L)->tmudata;
|
||||
Udata *udata = gcotou(o);
|
||||
G(L)->tmudata = udata->uv.next; /* remove udata from `tmudata' */
|
||||
udata->uv.next = G(L)->rootudata; /* return it to `root' list */
|
||||
G(L)->rootudata = o;
|
||||
setuvalue(L->top - 1, udata); /* keep a reference to it */
|
||||
unmark(o);
|
||||
markfinalized(udata);
|
||||
do1gcTM(L, udata);
|
||||
}
|
||||
L->top--;
|
||||
L->allowhook = oldah; /* restore hooks */
|
||||
}
|
||||
|
||||
|
||||
void luaC_collect (lua_State *L, int all) {
|
||||
collectudata(L, all);
|
||||
callgcTMudata(L);
|
||||
collectstrings(L, all);
|
||||
collecttable(L);
|
||||
collectproto(L);
|
||||
collectclosure(L);
|
||||
void luaC_callallgcTM (lua_State *L) {
|
||||
separateudata(L);
|
||||
callGCTM(L); /* call their GC tag methods */
|
||||
}
|
||||
|
||||
|
||||
static void luaC_collectgarbage (lua_State *L) {
|
||||
markall(L);
|
||||
invalidaterefs(L); /* check unlocked references */
|
||||
luaC_collect(L, 0);
|
||||
checkMbuffer(L);
|
||||
L->GCthreshold = 2*L->nblocks; /* set new threshold */
|
||||
callgcTM(L, &luaO_nilobject);
|
||||
void luaC_sweep (lua_State *L, int all) {
|
||||
if (all) all = 256; /* larger than any mark */
|
||||
sweeplist(L, &G(L)->rootudata, all);
|
||||
sweepstrings(L, all);
|
||||
sweeplist(L, &G(L)->rootgc, all);
|
||||
}
|
||||
|
||||
|
||||
void luaC_checkGC (lua_State *L) {
|
||||
if (L->nblocks >= L->GCthreshold)
|
||||
luaC_collectgarbage(L);
|
||||
/* mark root set */
|
||||
static void markroot (GCState *st, lua_State *L) {
|
||||
global_State *G = st->G;
|
||||
markobject(st, defaultmeta(L));
|
||||
markobject(st, registry(L));
|
||||
traversestack(st, G->mainthread);
|
||||
if (L != G->mainthread) /* another thread is running? */
|
||||
markvalue(st, L); /* cannot collect it */
|
||||
}
|
||||
|
||||
|
||||
static void mark (lua_State *L) {
|
||||
GCState st;
|
||||
GCObject *wkv;
|
||||
st.G = G(L);
|
||||
st.tmark = NULL;
|
||||
st.wkv = st.wk = st.wv = NULL;
|
||||
markroot(&st, L);
|
||||
propagatemarks(&st); /* mark all reachable objects */
|
||||
cleartablevalues(st.wkv);
|
||||
cleartablevalues(st.wv);
|
||||
wkv = st.wkv; /* keys must be cleared after preserving udata */
|
||||
st.wkv = NULL;
|
||||
st.wv = NULL;
|
||||
separateudata(L); /* separate userdata to be preserved */
|
||||
marktmu(&st); /* mark `preserved' userdata */
|
||||
propagatemarks(&st); /* remark, to propagate `preserveness' */
|
||||
cleartablekeys(wkv);
|
||||
/* `propagatemarks' may resuscitate some weak tables; clear them too */
|
||||
cleartablekeys(st.wk);
|
||||
cleartablevalues(st.wv);
|
||||
cleartablekeys(st.wkv);
|
||||
cleartablevalues(st.wkv);
|
||||
}
|
||||
|
||||
|
||||
void luaC_collectgarbage (lua_State *L) {
|
||||
mark(L);
|
||||
luaC_sweep(L, 0);
|
||||
checkSizes(L);
|
||||
callGCTM(L);
|
||||
}
|
||||
|
||||
|
||||
void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
|
||||
o->gch.next = G(L)->rootgc;
|
||||
G(L)->rootgc = o;
|
||||
o->gch.marked = 0;
|
||||
o->gch.tt = tt;
|
||||
}
|
||||
|
||||
|
||||
12
lgc.h
12
lgc.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lgc.h,v 1.7 1999/11/22 13:12:07 roberto Exp roberto $
|
||||
** $Id: lgc.h,v 1.16 2002/08/30 19:09:21 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,8 +11,14 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaC_collect (lua_State *L, int all);
|
||||
void luaC_checkGC (lua_State *L);
|
||||
#define luaC_checkGC(L) if (G(L)->nblocks >= G(L)->GCthreshold) \
|
||||
luaC_collectgarbage(L)
|
||||
|
||||
|
||||
void luaC_callallgcTM (lua_State *L);
|
||||
void luaC_sweep (lua_State *L, int all);
|
||||
void luaC_collectgarbage (lua_State *L);
|
||||
void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
360
llex.c
360
llex.c
@@ -1,24 +1,23 @@
|
||||
/*
|
||||
** $Id: llex.c,v 1.71 2000/09/27 17:41:58 roberto Exp roberto $
|
||||
** $Id: llex.c,v 1.116 2002/10/23 19:08:13 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define llex_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "luadebug.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
@@ -29,16 +28,22 @@
|
||||
|
||||
/* ORDER RESERVED */
|
||||
static const char *const token2string [] = {
|
||||
"and", "break", "do", "else", "elseif", "end", "for",
|
||||
"function", "if", "local", "nil", "not", "or", "repeat", "return", "then",
|
||||
"until", "while", "", "..", "...", "==", ">=", "<=", "~=", "", "", "<eof>"};
|
||||
"and", "break", "do", "else", "elseif",
|
||||
"end", "false", "for", "function", "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]);
|
||||
ts->marked = (unsigned char)(RESERVEDMARK+i); /* reserved word */
|
||||
luaS_fix(ts); /* reserved words are never collected */
|
||||
lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN);
|
||||
ts->tsv.reserved = cast(lu_byte, i+1); /* reserved word */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,50 +53,59 @@ void luaX_init (lua_State *L) {
|
||||
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
|
||||
if (val > limit) {
|
||||
char buff[100];
|
||||
sprintf(buff, "too many %.50s (limit=%d)", msg, limit);
|
||||
luaX_error(ls, buff, ls->t.token);
|
||||
msg = luaO_pushfstring(ls->L, "too many %s (limit=%d)", msg, limit);
|
||||
luaX_syntaxerror(ls, msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
|
||||
static void luaX_error (LexState *ls, const char *s, const char *token) {
|
||||
lua_State *L = ls->L;
|
||||
char buff[MAXSRC];
|
||||
luaO_chunkid(buff, ls->source->str, sizeof(buff));
|
||||
luaO_verror(ls->L, "%.99s;\n last token read: `%.30s' at line %d in %.80s",
|
||||
s, token, ls->linenumber, buff);
|
||||
luaO_chunkid(buff, getstr(ls->source), MAXSRC);
|
||||
luaO_pushfstring(L, "%s:%d: %s near `%s'", buff, ls->linenumber, s, token);
|
||||
luaD_throw(L, LUA_ERRSYNTAX);
|
||||
}
|
||||
|
||||
|
||||
void luaX_error (LexState *ls, const char *s, int token) {
|
||||
char buff[TOKEN_LEN];
|
||||
luaX_token2str(token, buff);
|
||||
if (buff[0] == '\0')
|
||||
luaX_syntaxerror(ls, s, ls->L->Mbuffer);
|
||||
else
|
||||
luaX_syntaxerror(ls, s, buff);
|
||||
void luaX_syntaxerror (LexState *ls, const char *msg) {
|
||||
const char *lasttoken;
|
||||
switch (ls->t.token) {
|
||||
case TK_NAME:
|
||||
lasttoken = getstr(ls->t.seminfo.ts);
|
||||
break;
|
||||
case TK_STRING:
|
||||
case TK_NUMBER:
|
||||
lasttoken = luaZ_buffer(ls->buff);
|
||||
break;
|
||||
default:
|
||||
lasttoken = luaX_token2str(ls, ls->t.token);
|
||||
break;
|
||||
}
|
||||
luaX_error(ls, msg, lasttoken);
|
||||
}
|
||||
|
||||
|
||||
void luaX_token2str (int token, char *s) {
|
||||
if (token < 256) {
|
||||
s[0] = (char)token;
|
||||
s[1] = '\0';
|
||||
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
|
||||
strcpy(s, token2string[token-FIRST_RESERVED]);
|
||||
return token2string[token-FIRST_RESERVED];
|
||||
}
|
||||
|
||||
|
||||
static void luaX_invalidchar (LexState *ls, int c) {
|
||||
char buff[8];
|
||||
sprintf(buff, "0x%02X", c);
|
||||
luaX_syntaxerror(ls, "invalid control char", buff);
|
||||
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, luaZ_buffer(ls->buff));
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *LS) {
|
||||
next(LS); /* skip '\n' */
|
||||
next(LS); /* skip `\n' */
|
||||
++LS->linenumber;
|
||||
luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
|
||||
}
|
||||
@@ -122,159 +136,173 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
|
||||
*/
|
||||
|
||||
|
||||
/* use Mbuffer to store names, literal strings and numbers */
|
||||
/* use buffer to store names, literal strings and numbers */
|
||||
|
||||
#define EXTRABUFF 128
|
||||
#define checkbuffer(L, n, len) if ((len)+(n) > L->Mbuffsize) \
|
||||
luaO_openspace(L, (len)+(n)+EXTRABUFF)
|
||||
/* extra space to allocate when growing buffer */
|
||||
#define EXTRABUFF 32
|
||||
|
||||
#define save(L, c, l) (L->Mbuffer[l++] = (char)c)
|
||||
#define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS))
|
||||
/* maximum number of chars that can be read without checking buffer size */
|
||||
#define MAXNOCHECK 5
|
||||
|
||||
#define checkbuffer(LS, len) \
|
||||
if (((len)+MAXNOCHECK)*sizeof(char) > luaZ_sizebuffer((LS)->buff)) \
|
||||
luaZ_openspace((LS)->L, (LS)->buff, (len)+EXTRABUFF)
|
||||
|
||||
#define save(LS, c, l) \
|
||||
(luaZ_buffer((LS)->buff)[l++] = cast(char, c))
|
||||
#define save_and_next(LS, l) (save(LS, LS->current, l), next(LS))
|
||||
|
||||
|
||||
static const char *readname (LexState *LS) {
|
||||
lua_State *L = LS->L;
|
||||
static size_t readname (LexState *LS) {
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
checkbuffer(LS, l);
|
||||
do {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
checkbuffer(LS, l);
|
||||
save_and_next(LS, l);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(L, '\0', l);
|
||||
return L->Mbuffer;
|
||||
save(LS, '\0', l);
|
||||
return l-1;
|
||||
}
|
||||
|
||||
|
||||
/* LUA_NUMBER */
|
||||
static void read_number (LexState *LS, int comma, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
static void read_numeral (LexState *LS, int comma, SemInfo *seminfo) {
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
if (comma) save(L, '.', l);
|
||||
checkbuffer(LS, l);
|
||||
if (comma) save(LS, '.', l);
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
checkbuffer(LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
if (LS->current == '.') {
|
||||
save_and_next(L, LS, l);
|
||||
save_and_next(LS, l);
|
||||
if (LS->current == '.') {
|
||||
save_and_next(L, LS, l);
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "ambiguous syntax"
|
||||
" (decimal point x string concatenation)", TK_NUMBER);
|
||||
save_and_next(LS, l);
|
||||
save(LS, '\0', l);
|
||||
luaX_lexerror(LS,
|
||||
"ambiguous syntax (decimal point x string concatenation)",
|
||||
TK_NUMBER);
|
||||
}
|
||||
}
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
checkbuffer(LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
if (LS->current == 'e' || LS->current == 'E') {
|
||||
save_and_next(L, LS, l); /* read 'E' */
|
||||
save_and_next(LS, l); /* read `E' */
|
||||
if (LS->current == '+' || LS->current == '-')
|
||||
save_and_next(L, LS, l); /* optional exponent sign */
|
||||
save_and_next(LS, l); /* optional exponent sign */
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
checkbuffer(LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
}
|
||||
save(L, '\0', l);
|
||||
if (!luaO_str2d(L->Mbuffer, &seminfo->r))
|
||||
luaX_error(LS, "malformed number", TK_NUMBER);
|
||||
save(LS, '\0', l);
|
||||
if (!luaO_str2d(luaZ_buffer(LS->buff), &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, 10, l);
|
||||
save(L, '[', l); /* save first '[' */
|
||||
save_and_next(L, LS, l); /* pass the second '[' */
|
||||
checkbuffer(LS, l);
|
||||
save(LS, '[', l); /* save first `[' */
|
||||
save_and_next(LS, l); /* pass the second `[' */
|
||||
if (LS->current == '\n') /* string starts with a newline? */
|
||||
inclinenumber(LS); /* skip it */
|
||||
for (;;) {
|
||||
checkbuffer(L, 10, l);
|
||||
checkbuffer(LS, l);
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "unfinished long string", TK_STRING);
|
||||
save(LS, '\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);
|
||||
save_and_next(LS, l);
|
||||
if (LS->current == '[') {
|
||||
cont++;
|
||||
save_and_next(L, LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next(L, LS, l);
|
||||
save_and_next(LS, l);
|
||||
if (LS->current == ']') {
|
||||
if (cont == 0) goto endloop;
|
||||
cont--;
|
||||
save_and_next(L, LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
save(L, '\n', l);
|
||||
save(LS, '\n', l);
|
||||
inclinenumber(LS);
|
||||
if (!seminfo) l = 0; /* reset buffer to avoid wasting space */
|
||||
continue;
|
||||
default:
|
||||
save_and_next(L, LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(L, LS, l); /* skip the second ']' */
|
||||
save(L, '\0', l);
|
||||
seminfo->ts = luaS_newlstr(L, L->Mbuffer+2, l-5);
|
||||
save_and_next(LS, l); /* skip the second `]' */
|
||||
save(LS, '\0', l);
|
||||
if (seminfo)
|
||||
seminfo->ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff) + 2, l - 5);
|
||||
}
|
||||
|
||||
|
||||
static void read_string (LexState *LS, int del, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
checkbuffer(LS, l);
|
||||
save_and_next(LS, l);
|
||||
while (LS->current != del) {
|
||||
checkbuffer(L, 10, l);
|
||||
checkbuffer(LS, l);
|
||||
switch (LS->current) {
|
||||
case EOZ: case '\n':
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "unfinished string", TK_STRING);
|
||||
case EOZ:
|
||||
save(LS, '\0', l);
|
||||
luaX_lexerror(LS, "unfinished string", TK_EOS);
|
||||
break; /* to avoid warnings */
|
||||
case '\n':
|
||||
save(LS, '\0', l);
|
||||
luaX_lexerror(LS, "unfinished string", TK_STRING);
|
||||
break; /* to avoid warnings */
|
||||
case '\\':
|
||||
next(LS); /* do not save the '\' */
|
||||
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 '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9': {
|
||||
int c = 0;
|
||||
int i = 0;
|
||||
do {
|
||||
c = 10*c + (LS->current-'0');
|
||||
next(LS);
|
||||
} while (++i<3 && isdigit(LS->current));
|
||||
if (c != (unsigned char)c) {
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "escape sequence too large", TK_STRING);
|
||||
case 'a': save(LS, '\a', l); next(LS); break;
|
||||
case 'b': save(LS, '\b', l); next(LS); break;
|
||||
case 'f': save(LS, '\f', l); next(LS); break;
|
||||
case 'n': save(LS, '\n', l); next(LS); break;
|
||||
case 'r': save(LS, '\r', l); next(LS); break;
|
||||
case 't': save(LS, '\t', l); next(LS); break;
|
||||
case 'v': save(LS, '\v', l); next(LS); break;
|
||||
case '\n': save(LS, '\n', l); inclinenumber(LS); break;
|
||||
case EOZ: break; /* will raise an error next loop */
|
||||
default: {
|
||||
if (!isdigit(LS->current))
|
||||
save_and_next(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(LS, '\0', l);
|
||||
luaX_lexerror(LS, "escape sequence too large", TK_STRING);
|
||||
}
|
||||
save(LS, c, l);
|
||||
}
|
||||
save(L, c, l);
|
||||
break;
|
||||
}
|
||||
default: /* handles \\, \", \', and \? */
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next(L, LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
}
|
||||
save_and_next(L, LS, l); /* skip delimiter */
|
||||
save(L, '\0', l);
|
||||
seminfo->ts = luaS_newlstr(L, L->Mbuffer+1, l-3);
|
||||
save_and_next(LS, l); /* skip delimiter */
|
||||
save(LS, '\0', l);
|
||||
seminfo->ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff) + 1, l - 3);
|
||||
}
|
||||
|
||||
|
||||
@@ -282,58 +310,56 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) {
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
|
||||
case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
|
||||
next(LS);
|
||||
continue;
|
||||
|
||||
case '\n':
|
||||
case '\n': {
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
|
||||
case '$':
|
||||
luaX_error(LS, "unexpected `$' (pragmas are no longer supported)", '$');
|
||||
break;
|
||||
|
||||
case '-':
|
||||
}
|
||||
case '-': {
|
||||
next(LS);
|
||||
if (LS->current != '-') return '-';
|
||||
do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
|
||||
/* 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 '[':
|
||||
}
|
||||
case '[': {
|
||||
next(LS);
|
||||
if (LS->current != '[') return '[';
|
||||
else {
|
||||
read_long_string(LS, seminfo);
|
||||
return TK_STRING;
|
||||
}
|
||||
|
||||
case '=':
|
||||
}
|
||||
case '=': {
|
||||
next(LS);
|
||||
if (LS->current != '=') return '=';
|
||||
else { next(LS); return TK_EQ; }
|
||||
|
||||
case '<':
|
||||
}
|
||||
case '<': {
|
||||
next(LS);
|
||||
if (LS->current != '=') return '<';
|
||||
else { next(LS); return TK_LE; }
|
||||
|
||||
case '>':
|
||||
}
|
||||
case '>': {
|
||||
next(LS);
|
||||
if (LS->current != '=') return '>';
|
||||
else { next(LS); return TK_GE; }
|
||||
|
||||
case '~':
|
||||
}
|
||||
case '~': {
|
||||
next(LS);
|
||||
if (LS->current != '=') return '~';
|
||||
else { next(LS); return TK_NE; }
|
||||
|
||||
}
|
||||
case '"':
|
||||
case '\'':
|
||||
case '\'': {
|
||||
read_string(LS, LS->current, seminfo);
|
||||
return TK_STRING;
|
||||
|
||||
case '.':
|
||||
}
|
||||
case '.': {
|
||||
next(LS);
|
||||
if (LS->current == '.') {
|
||||
next(LS);
|
||||
@@ -345,36 +371,42 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) {
|
||||
}
|
||||
else if (!isdigit(LS->current)) return '.';
|
||||
else {
|
||||
read_number(LS, 1, seminfo);
|
||||
read_numeral(LS, 1, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
read_number(LS, 0, seminfo);
|
||||
return TK_NUMBER;
|
||||
|
||||
case EOZ:
|
||||
}
|
||||
case EOZ: {
|
||||
return TK_EOS;
|
||||
|
||||
case '_': goto tname;
|
||||
|
||||
default:
|
||||
if (!isalpha(LS->current)) {
|
||||
int c = LS->current;
|
||||
if (iscntrl(c))
|
||||
luaX_invalidchar(LS, c);
|
||||
}
|
||||
default: {
|
||||
if (isspace(LS->current)) {
|
||||
next(LS);
|
||||
return c;
|
||||
continue;
|
||||
}
|
||||
tname: { /* identifier or reserved word */
|
||||
TString *ts = luaS_new(LS->L, readname(LS));
|
||||
if (ts->marked >= RESERVEDMARK) /* reserved word? */
|
||||
return ts->marked-RESERVEDMARK+FIRST_RESERVED;
|
||||
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, luaZ_buffer(LS->buff), l);
|
||||
if (ts->tsv.reserved > 0) /* reserved word? */
|
||||
return ts->tsv.reserved - 1 + 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
|
||||
|
||||
30
llex.h
30
llex.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.30 2000/06/21 18:13:56 roberto Exp roberto $
|
||||
** $Id: llex.h,v 1.45 2002/10/08 18:46:08 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,8 +13,8 @@
|
||||
|
||||
#define FIRST_RESERVED 257
|
||||
|
||||
/* maximum length of a reserved word (+1 for final 0) */
|
||||
#define TOKEN_LEN 15
|
||||
/* maximum length of a reserved word */
|
||||
#define TOKEN_LEN (sizeof("function")/sizeof(char))
|
||||
|
||||
|
||||
/*
|
||||
@@ -24,19 +24,20 @@
|
||||
enum RESERVED {
|
||||
/* terminal symbols denoted by reserved words */
|
||||
TK_AND = FIRST_RESERVED, TK_BREAK,
|
||||
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FOR, TK_FUNCTION, TK_IF, TK_LOCAL,
|
||||
TK_NIL, TK_NOT, TK_OR, TK_REPEAT, TK_RETURN, TK_THEN, TK_UNTIL, TK_WHILE,
|
||||
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
|
||||
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 ((int)(TK_WHILE-FIRST_RESERVED+1))
|
||||
#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
|
||||
|
||||
|
||||
typedef union {
|
||||
Number r;
|
||||
lua_Number r;
|
||||
TString *ts;
|
||||
} SemInfo; /* semantics information */
|
||||
|
||||
@@ -48,15 +49,17 @@ typedef struct Token {
|
||||
|
||||
|
||||
typedef struct LexState {
|
||||
int current; /* current character */
|
||||
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 */
|
||||
int linenumber; /* input line counter */
|
||||
int lastline; /* line of last token `consumed' */
|
||||
ZIO *z; /* input stream */
|
||||
Mbuffer *buff; /* buffer for tokens */
|
||||
TString *source; /* current source name */
|
||||
int nestlevel; /* level of nested non-terminals */
|
||||
} LexState;
|
||||
|
||||
|
||||
@@ -64,9 +67,8 @@ 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 *token);
|
||||
void luaX_error (LexState *ls, const char *s, int token);
|
||||
void luaX_token2str (int token, char *s);
|
||||
void luaX_syntaxerror (LexState *ls, const char *s);
|
||||
const char *luaX_token2str (LexState *ls, int token);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
204
llimits.h
204
llimits.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.18 2000/10/09 13:47:32 roberto Exp roberto $
|
||||
** Limits, basic types, and some other "installation-dependent" definitions
|
||||
** $Id: llimits.h,v 1.50 2002/11/22 18:01:46 roberto Exp roberto $
|
||||
** Limits, basic types, and some other `installation-dependent' definitions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/*
|
||||
** try to find number of bits in an integer
|
||||
@@ -32,25 +34,26 @@
|
||||
|
||||
|
||||
/*
|
||||
** Define the type `number' of Lua
|
||||
** GREP LUA_NUMBER to change that
|
||||
** 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.
|
||||
*/
|
||||
#ifndef LUA_NUM_TYPE
|
||||
#define LUA_NUM_TYPE double
|
||||
#endif
|
||||
|
||||
typedef LUA_NUM_TYPE Number;
|
||||
/* an unsigned integer to hold hash values */
|
||||
typedef unsigned int lu_hash;
|
||||
/* its signed equivalent */
|
||||
typedef int ls_hash;
|
||||
|
||||
/* function to convert a Number to a string */
|
||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||
#define lua_number2str(s,n) sprintf((s), NUMBER_FMT, (n))
|
||||
/* 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;
|
||||
|
||||
/* function to convert a string to a Number */
|
||||
#define lua_str2number(s,p) strtod((s), (p))
|
||||
/* an integer big enough to count the number of strings in use */
|
||||
typedef long ls_nstr;
|
||||
|
||||
|
||||
|
||||
typedef unsigned long lint32; /* unsigned int with at least 32 bits */
|
||||
/* chars used as small naturals (so that `char' is reserved for characters) */
|
||||
typedef unsigned char lu_byte;
|
||||
|
||||
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0)-2)
|
||||
@@ -59,117 +62,93 @@ typedef unsigned long lint32; /* unsigned int with at least 32 bits */
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
|
||||
|
||||
/*
|
||||
** conversion of pointer to int (for hashing only)
|
||||
** (the shift removes bits that are usually 0 because of alignment)
|
||||
** 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) (((unsigned long)(p)) >> 3)
|
||||
|
||||
|
||||
|
||||
#define MINPOWER2 4 /* minimum size for "growing" vectors */
|
||||
|
||||
|
||||
|
||||
#ifndef DEFAULT_STACK_SIZE
|
||||
#define DEFAULT_STACK_SIZE 1024
|
||||
#endif
|
||||
#define IntPoint(p) ((lu_hash)(p))
|
||||
|
||||
|
||||
|
||||
/* type to ensure maximum alignment */
|
||||
union L_Umaxalign { double d; char *s; long l; };
|
||||
#ifndef LUSER_ALIGNMENT_T
|
||||
typedef union { double u; void *s; long l; } L_Umaxalign;
|
||||
#else
|
||||
typedef LUSER_ALIGNMENT_T L_Umaxalign;
|
||||
#endif
|
||||
|
||||
|
||||
/* result of `usual argument conversion' over lua_Number */
|
||||
#ifndef LUA_UACNUMBER
|
||||
typedef double l_uacNumber;
|
||||
#else
|
||||
typedef LUA_UACNUMBER l_uacNumber;
|
||||
#endif
|
||||
|
||||
|
||||
#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)
|
||||
** For a very small machine, you may change that to 2 bytes (and adjust
|
||||
** the following limits accordingly)
|
||||
*/
|
||||
typedef unsigned long Instruction;
|
||||
|
||||
|
||||
/*
|
||||
** size and position of opcode arguments.
|
||||
** For an instruction with 2 bytes, size is 16, and size_b can be 5
|
||||
** (accordingly, size_u will be 10, and size_a will be 5)
|
||||
*/
|
||||
#define SIZE_INSTRUCTION 32
|
||||
#define SIZE_B 9
|
||||
|
||||
#define SIZE_OP 6
|
||||
#define SIZE_U (SIZE_INSTRUCTION-SIZE_OP)
|
||||
#define POS_U SIZE_OP
|
||||
#define POS_B SIZE_OP
|
||||
#define SIZE_A (SIZE_INSTRUCTION-(SIZE_OP+SIZE_B))
|
||||
#define POS_A (SIZE_OP+SIZE_B)
|
||||
/* maximum depth for calls (unsigned short) */
|
||||
#ifndef LUA_MAXCALLS
|
||||
#define LUA_MAXCALLS 4096
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** limits for opcode arguments.
|
||||
** we use (signed) int to manipulate most arguments,
|
||||
** so they must fit in BITS_INT-1 bits (-1 for sign)
|
||||
** maximum depth for C calls (unsigned short): Not too big, or may
|
||||
** overflow the C stack...
|
||||
*/
|
||||
#if SIZE_U < BITS_INT-1
|
||||
#define MAXARG_U ((1<<SIZE_U)-1)
|
||||
#define MAXARG_S (MAXARG_U>>1) /* `S' is signed */
|
||||
#else
|
||||
#define MAXARG_U MAX_INT
|
||||
#define MAXARG_S MAX_INT
|
||||
#endif
|
||||
|
||||
#if SIZE_A < BITS_INT-1
|
||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||
#else
|
||||
#define MAXARG_A MAX_INT
|
||||
#endif
|
||||
|
||||
#if SIZE_B < BITS_INT-1
|
||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||
#else
|
||||
#define MAXARG_B MAX_INT
|
||||
#ifndef LUA_MAXCCALLS
|
||||
#define LUA_MAXCCALLS 200
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum stack size in a function */
|
||||
#ifndef MAXSTACK
|
||||
/* maximum size for the C stack */
|
||||
#ifndef LUA_MAXCSTACK
|
||||
#define LUA_MAXCSTACK 2048
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum stack for a Lua function */
|
||||
#define MAXSTACK 250
|
||||
#endif
|
||||
|
||||
#if MAXSTACK > MAXARG_B
|
||||
#undef MAXSTACK
|
||||
#define MAXSTACK MAXARG_B
|
||||
|
||||
/* maximum number of variables declared in a function */
|
||||
#ifndef MAXVARS
|
||||
#define MAXVARS 200 /* arbitrary limit (<MAXSTACK) */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of local variables */
|
||||
#ifndef MAXLOCALS
|
||||
#define MAXLOCALS 200 /* arbitrary limit (<MAXSTACK) */
|
||||
#endif
|
||||
#if MAXLOCALS>=MAXSTACK
|
||||
#undef MAXLOCALS
|
||||
#define MAXLOCALS (MAXSTACK-1)
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of upvalues */
|
||||
/* maximum number of upvalues per function */
|
||||
#ifndef MAXUPVALUES
|
||||
#define MAXUPVALUES 32 /* arbitrary limit (<=MAXARG_B) */
|
||||
#endif
|
||||
#if MAXUPVALUES>MAXARG_B
|
||||
#undef MAXUPVALUES
|
||||
#define MAXUPVALUES MAXARG_B
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of variables in the left side of an assignment */
|
||||
#ifndef MAXVARSLH
|
||||
#define MAXVARSLH 100 /* arbitrary limit (<MULT_RET) */
|
||||
#endif
|
||||
#if MAXVARSLH>=MULT_RET
|
||||
#undef MAXVARSLH
|
||||
#define MAXVARSLH (MULT_RET-1)
|
||||
#define MAXUPVALUES 32
|
||||
#endif
|
||||
|
||||
|
||||
@@ -177,27 +156,26 @@ typedef unsigned long Instruction;
|
||||
#ifndef MAXPARAMS
|
||||
#define MAXPARAMS 100 /* arbitrary limit (<MAXLOCALS) */
|
||||
#endif
|
||||
#if MAXPARAMS>=MAXLOCALS
|
||||
#undef MAXPARAMS
|
||||
#define MAXPARAMS (MAXLOCALS-1)
|
||||
|
||||
|
||||
/* minimum size for the string table (must be power of 2) */
|
||||
#ifndef MINSTRTABSIZE
|
||||
#define MINSTRTABSIZE 32
|
||||
#endif
|
||||
|
||||
|
||||
/* number of list items to accumulate before a SETLIST instruction */
|
||||
#define LFIELDS_PER_FLUSH 64
|
||||
#if LFIELDS_PER_FLUSH>(MAXSTACK/4)
|
||||
#undef LFIELDS_PER_FLUSH
|
||||
#define LFIELDS_PER_FLUSH (MAXSTACK/4)
|
||||
/* minimum size for string buffer */
|
||||
#ifndef LUA_MINBUFFER
|
||||
#define LUA_MINBUFFER 32
|
||||
#endif
|
||||
|
||||
/* number of record items to accumulate before a SETMAP instruction */
|
||||
/* (each item counts 2 elements on the stack: an index and a value) */
|
||||
#define RFIELDS_PER_FLUSH (LFIELDS_PER_FLUSH/2)
|
||||
|
||||
|
||||
/* maximum lookback to find a real constant (for code generation) */
|
||||
#ifndef LOOKBACKNUMS
|
||||
#define LOOKBACKNUMS 20 /* arbitrary constant */
|
||||
/*
|
||||
** maximum number of syntactical nested non-terminals: Not too big,
|
||||
** or may overflow the C stack...
|
||||
*/
|
||||
#ifndef LUA_MAXPARSERLEVEL
|
||||
#define LUA_MAXPARSERLEVEL 200
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
152
lmathlib.c
152
lmathlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.31 2000/10/27 16:15:53 roberto Exp roberto $
|
||||
** $Id: lmathlib.c,v 1.52 2002/11/14 15:41:38 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define lmathlib_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -21,117 +23,117 @@
|
||||
|
||||
|
||||
/*
|
||||
** If you want Lua to operate in radians (instead of degrees),
|
||||
** define RADIANS
|
||||
** If you want Lua to operate in degrees (instead of radians),
|
||||
** define USE_DEGREES
|
||||
*/
|
||||
#ifdef RADIANS
|
||||
#define FROMRAD(a) (a)
|
||||
#define TORAD(a) (a)
|
||||
#else
|
||||
#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)));
|
||||
lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_sin (lua_State *L) {
|
||||
lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
|
||||
lua_pushnumber(L, sin(TORAD(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_cos (lua_State *L) {
|
||||
lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
|
||||
lua_pushnumber(L, cos(TORAD(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_tan (lua_State *L) {
|
||||
lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
|
||||
lua_pushnumber(L, tan(TORAD(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_asin (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
|
||||
lua_pushnumber(L, FROMRAD(asin(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_acos (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
|
||||
lua_pushnumber(L, FROMRAD(acos(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_atan (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
|
||||
lua_pushnumber(L, FROMRAD(atan(luaL_checknumber(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))));
|
||||
lua_pushnumber(L, FROMRAD(atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_ceil (lua_State *L) {
|
||||
lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
|
||||
lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_floor (lua_State *L) {
|
||||
lua_pushnumber(L, floor(luaL_check_number(L, 1)));
|
||||
lua_pushnumber(L, floor(luaL_checknumber(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)));
|
||||
lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_sqrt (lua_State *L) {
|
||||
lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
|
||||
lua_pushnumber(L, sqrt(luaL_checknumber(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)));
|
||||
lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_log (lua_State *L) {
|
||||
lua_pushnumber(L, log(luaL_check_number(L, 1)));
|
||||
lua_pushnumber(L, log(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_log10 (lua_State *L) {
|
||||
lua_pushnumber(L, log10(luaL_check_number(L, 1)));
|
||||
lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_exp (lua_State *L) {
|
||||
lua_pushnumber(L, exp(luaL_check_number(L, 1)));
|
||||
lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_deg (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
|
||||
lua_pushnumber(L, luaL_checknumber(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);
|
||||
lua_pushnumber(L, luaL_checknumber(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, frexp(luaL_checknumber(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)));
|
||||
lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -139,10 +141,10 @@ static int math_ldexp (lua_State *L) {
|
||||
|
||||
static int math_min (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
double dmin = luaL_check_number(L, 1);
|
||||
lua_Number dmin = luaL_checknumber(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
double d = luaL_check_number(L, i);
|
||||
lua_Number d = luaL_checknumber(L, i);
|
||||
if (d < dmin)
|
||||
dmin = d;
|
||||
}
|
||||
@@ -153,10 +155,10 @@ static int math_min (lua_State *L) {
|
||||
|
||||
static int math_max (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
double dmax = luaL_check_number(L, 1);
|
||||
lua_Number dmax = luaL_checknumber(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
double d = luaL_check_number(L, i);
|
||||
lua_Number d = luaL_checknumber(L, i);
|
||||
if (d > dmax)
|
||||
dmax = d;
|
||||
}
|
||||
@@ -166,73 +168,79 @@ static int math_max (lua_State *L) {
|
||||
|
||||
|
||||
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 */
|
||||
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
||||
/* 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)(r*u)+1); /* integer between 1 and `u' */
|
||||
int u = luaL_checkint(L, 1);
|
||||
luaL_argcheck(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)(r*(u-l+1))+l); /* integer between `l' and `u' */
|
||||
int l = luaL_checkint(L, 1);
|
||||
int u = luaL_checkint(L, 2);
|
||||
luaL_argcheck(L, l<=u, 2, "interval is empty");
|
||||
lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */
|
||||
break;
|
||||
}
|
||||
default: lua_error(L, "wrong number of arguments");
|
||||
default: return luaL_error(L, "wrong number of arguments");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_randomseed (lua_State *L) {
|
||||
srand(luaL_check_int(L, 1));
|
||||
srand(luaL_checkint(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_reg mathlib[] = {
|
||||
{"abs", math_abs},
|
||||
{"sin", math_sin},
|
||||
{"cos", math_cos},
|
||||
{"tan", math_tan},
|
||||
{"asin", math_asin},
|
||||
{"acos", math_acos},
|
||||
{"atan", math_atan},
|
||||
{"atan2", math_atan2},
|
||||
{"ceil", math_ceil},
|
||||
{"floor", math_floor},
|
||||
{"mod", math_mod},
|
||||
{"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},
|
||||
{"rad", math_rad},
|
||||
{"random", math_random},
|
||||
{"randomseed", math_randomseed}
|
||||
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 void lua_mathlibopen (lua_State *L) {
|
||||
luaL_openl(L, mathlib);
|
||||
lua_pushcfunction(L, math_pow);
|
||||
lua_settagmethod(L, LUA_TNUMBER, "pow");
|
||||
LUALIB_API int lua_mathlibopen (lua_State *L) {
|
||||
luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
|
||||
lua_pushliteral(L, "pi");
|
||||
lua_pushnumber(L, PI);
|
||||
lua_setglobal(L, "PI");
|
||||
lua_settable(L, -3);
|
||||
lua_pushliteral(L, "__pow");
|
||||
lua_pushcfunction(L, math_pow);
|
||||
lua_settable(L, LUA_REGISTRYINDEX);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
165
lmem.c
165
lmem.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.38 2000/10/26 12:47:05 roberto Exp roberto $
|
||||
** $Id: lmem.c,v 1.60 2002/11/21 14:14:42 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,8 +7,11 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define lmem_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
@@ -16,135 +19,73 @@
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef LUA_DEBUG
|
||||
/*
|
||||
** {======================================================================
|
||||
** Controlled version for realloc.
|
||||
** =======================================================================
|
||||
** definition for realloc function. It must assure that 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
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#define realloc(b, s) debug_realloc(b, s)
|
||||
#define malloc(b) debug_realloc(NULL, b)
|
||||
#define free(b) debug_realloc(b, 0)
|
||||
|
||||
|
||||
/* ensures maximum alignment for HEADER */
|
||||
#define HEADER (sizeof(union L_Umaxalign))
|
||||
|
||||
#define MARKSIZE 16
|
||||
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
||||
|
||||
|
||||
#define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
|
||||
|
||||
unsigned long memdebug_numblocks = 0;
|
||||
unsigned long memdebug_total = 0;
|
||||
unsigned long memdebug_maxmem = 0;
|
||||
unsigned long memdebug_memlimit = LONG_MAX;
|
||||
|
||||
|
||||
static void *checkblock (void *block) {
|
||||
unsigned long *b = blocksize(block);
|
||||
unsigned long size = *b;
|
||||
int i;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
|
||||
memdebug_numblocks--;
|
||||
memdebug_total -= size;
|
||||
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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void *debug_realloc (void *block, size_t size) {
|
||||
if (size == 0) {
|
||||
freeblock(block);
|
||||
return NULL;
|
||||
}
|
||||
else if (memdebug_total+size > memdebug_memlimit)
|
||||
return NULL; /* to test memory allocation errors */
|
||||
else {
|
||||
size_t realsize = HEADER+size+MARKSIZE;
|
||||
char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
|
||||
int i;
|
||||
if (realsize < size) return NULL; /* overflow! */
|
||||
if (newblock == NULL) return NULL;
|
||||
if (block) {
|
||||
size_t oldsize = *blocksize(block);
|
||||
if (oldsize > size) oldsize = size;
|
||||
memcpy(newblock+HEADER, block, oldsize);
|
||||
freeblock(block); /* erase (and check) old copy */
|
||||
}
|
||||
memdebug_total += size;
|
||||
if (memdebug_total > memdebug_maxmem) memdebug_maxmem = memdebug_total;
|
||||
memdebug_numblocks++;
|
||||
*(unsigned long *)newblock = size;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
*(newblock+HEADER+size+i) = (char)(MARK+i);
|
||||
return newblock+HEADER;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================================== */
|
||||
/*
|
||||
** definition for free function. (`os' is the old block size; some
|
||||
** allocators may use that.)
|
||||
*/
|
||||
#ifndef l_free
|
||||
#define l_free(b,os) free(b)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Real ISO (ANSI) systems do not need these tests;
|
||||
** but some systems (Sun OS) are not that ISO...
|
||||
*/
|
||||
#ifdef OLD_ANSI
|
||||
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
||||
#define free(b) if (b) (free)(b)
|
||||
#endif
|
||||
#define MINSIZEARRAY 4
|
||||
|
||||
|
||||
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
|
||||
int inc, size_t size, const char *errormsg, size_t limit) {
|
||||
size_t newn = nelems+inc;
|
||||
if (nelems >= limit-inc) lua_error(L, errormsg);
|
||||
if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
|
||||
(nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
|
||||
return block; /* do not need to reallocate */
|
||||
else /* it crossed a power-of-2 boundary; grow to next power */
|
||||
return luaM_realloc(L, block, luaO_power2(newn)*size);
|
||||
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, lint32 size) {
|
||||
void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
|
||||
lua_assert((oldsize == 0) == (block == NULL));
|
||||
if (size == 0) {
|
||||
free(block); /* block may be NULL; that is OK for free */
|
||||
return NULL;
|
||||
if (block != NULL) {
|
||||
l_free(block, oldsize);
|
||||
block = NULL;
|
||||
}
|
||||
else return NULL; /* avoid `nblocks' computations when oldsize==size==0 */
|
||||
}
|
||||
else if (size >= MAX_SIZET)
|
||||
lua_error(L, "memory allocation error: block too big");
|
||||
block = realloc(block, size);
|
||||
if (block == NULL) {
|
||||
if (L)
|
||||
luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
|
||||
else return NULL; /* error before creating state! */
|
||||
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) {
|
||||
lua_assert(G(L) != NULL && G(L)->nblocks > 0);
|
||||
G(L)->nblocks -= oldsize;
|
||||
G(L)->nblocks += size;
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
46
lmem.h
46
lmem.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.15 2000/08/07 18:39:16 roberto Exp roberto $
|
||||
** $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
|
||||
*/
|
||||
@@ -13,29 +13,31 @@
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
void *luaM_realloc (lua_State *L, void *oldblock, lint32 size);
|
||||
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
|
||||
int inc, size_t size, const char *errormsg,
|
||||
size_t limit);
|
||||
|
||||
#define luaM_free(L, b) luaM_realloc(L, (b), 0)
|
||||
#define luaM_malloc(L, t) luaM_realloc(L, NULL, (t))
|
||||
#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t)))
|
||||
#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*(lint32)sizeof(t)))
|
||||
|
||||
#define luaM_growvector(L, v,nelems,inc,t,e,l) \
|
||||
((v)=(t *)luaM_growaux(L, v,nelems,inc,sizeof(t),e,l))
|
||||
|
||||
#define luaM_reallocvector(L, v,n,t) \
|
||||
((v)=(t *)luaM_realloc(L, v,(n)*(lint32)sizeof(t)))
|
||||
#define MEMERRMSG "not enough memory"
|
||||
|
||||
|
||||
#ifdef LUA_DEBUG
|
||||
extern unsigned long memdebug_numblocks;
|
||||
extern unsigned long memdebug_total;
|
||||
extern unsigned long memdebug_maxmem;
|
||||
extern unsigned long memdebug_memlimit;
|
||||
#endif
|
||||
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
|
||||
|
||||
172
lobject.c
172
lobject.c
@@ -1,92 +1,148 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 1.54 2000/10/10 19:53:20 roberto Exp roberto $
|
||||
** $Id: lobject.c,v 1.93 2002/11/21 15:16:04 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lobject_c
|
||||
|
||||
#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}};
|
||||
|
||||
|
||||
const char *const luaO_typenames[] = {
|
||||
"userdata", "nil", "number", "string", "table", "function"
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
||||
*/
|
||||
lint32 luaO_power2 (lint32 n) {
|
||||
lint32 p = MINPOWER2;
|
||||
while (p<=n) p<<=1;
|
||||
return p;
|
||||
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_equalObj (const TObject *t1, const TObject *t2) {
|
||||
int luaO_rawequalObj (const TObject *t1, const TObject *t2) {
|
||||
if (ttype(t1) != ttype(t2)) return 0;
|
||||
switch (ttype(t1)) {
|
||||
if (iscollectable(t1)) return gcvalue(t1) == gcvalue(t2);
|
||||
else switch (ttype(t1)) {
|
||||
case LUA_TNIL:
|
||||
return 1;
|
||||
case LUA_TBOOLEAN:
|
||||
return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
return pvalue(t1) == pvalue(t2);
|
||||
case LUA_TNUMBER:
|
||||
return nvalue(t1) == nvalue(t2);
|
||||
case LUA_TSTRING: case LUA_TUSERDATA:
|
||||
return tsvalue(t1) == tsvalue(t2);
|
||||
case LUA_TTABLE:
|
||||
return hvalue(t1) == hvalue(t2);
|
||||
case LUA_TFUNCTION:
|
||||
return clvalue(t1) == clvalue(t2);
|
||||
default:
|
||||
LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type");
|
||||
return 1; /* LUA_TNIL */
|
||||
}
|
||||
lua_assert(0);
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
char *luaO_openspace (lua_State *L, size_t n) {
|
||||
if (n > L->Mbuffsize) {
|
||||
luaM_reallocvector(L, L->Mbuffer, n, char);
|
||||
L->nblocks += (n - L->Mbuffsize)*sizeof(char);
|
||||
L->Mbuffsize = n;
|
||||
}
|
||||
return L->Mbuffer;
|
||||
}
|
||||
|
||||
|
||||
int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
|
||||
int luaO_str2d (const char *s, lua_Number *result) {
|
||||
char *endptr;
|
||||
Number res = lua_str2number(s, &endptr);
|
||||
lua_Number res = lua_str2number(s, &endptr);
|
||||
if (endptr == s) return 0; /* no conversion */
|
||||
while (isspace((unsigned char)*endptr)) endptr++;
|
||||
while (isspace((unsigned char)(*endptr))) endptr++;
|
||||
if (*endptr != '\0') return 0; /* invalid trailing characters? */
|
||||
*result = res;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* maximum length of a string format for `luaO_verror' */
|
||||
#define MAX_VERROR 280
|
||||
|
||||
/* this function needs to handle only '%d' and '%.XXs' formats */
|
||||
void luaO_verror (lua_State *L, const char *fmt, ...) {
|
||||
static void pushstr (lua_State *L, const char *str) {
|
||||
setsvalue2s(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;
|
||||
setsvalue2s(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, l_uacNumber));
|
||||
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->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;
|
||||
char buff[MAX_VERROR]; /* to hold formatted message */
|
||||
va_start(argp, fmt);
|
||||
vsprintf(buff, fmt, argp);
|
||||
msg = luaO_pushvfstring(L, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_error(L, buff);
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
@@ -95,31 +151,31 @@ void luaO_chunkid (char *out, const char *source, int bufflen) {
|
||||
strncpy(out, source+1, bufflen); /* remove first char */
|
||||
out[bufflen-1] = '\0'; /* ensures null termination */
|
||||
}
|
||||
else {
|
||||
else { /* out = "source", or "...source" */
|
||||
if (*source == '@') {
|
||||
int l;
|
||||
source++; /* skip the `@' */
|
||||
bufflen -= sizeof("file `...%s'");
|
||||
bufflen -= sizeof(" `...' ");
|
||||
l = strlen(source);
|
||||
strcpy(out, "");
|
||||
if (l>bufflen) {
|
||||
source += (l-bufflen); /* get last part of file name */
|
||||
sprintf(out, "file `...%.99s'", source);
|
||||
strcat(out, "...");
|
||||
}
|
||||
else
|
||||
sprintf(out, "file `%.99s'", source);
|
||||
strcat(out, source);
|
||||
}
|
||||
else {
|
||||
else { /* out = [string "string"] */
|
||||
int len = strcspn(source, "\n"); /* stop at first newline */
|
||||
bufflen -= sizeof("string \"%.*s...\"");
|
||||
bufflen -= sizeof(" [string \"...\"] ");
|
||||
if (len > bufflen) len = bufflen;
|
||||
strcpy(out, "[string \"");
|
||||
if (source[len] != '\0') { /* must truncate? */
|
||||
strcpy(out, "string \"");
|
||||
out += strlen(out);
|
||||
strncpy(out, source, len);
|
||||
strcpy(out+len, "...\"");
|
||||
strncat(out, source, len);
|
||||
strcat(out, "...");
|
||||
}
|
||||
else
|
||||
sprintf(out, "string \"%.99s\"", source);
|
||||
strcat(out, source);
|
||||
strcat(out, "\"]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
384
lobject.h
384
lobject.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 1.81 2000/10/30 16:29:59 roberto Exp roberto $
|
||||
** $Id: lobject.h,v 1.154 2002/11/14 11:51:50 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,115 +12,220 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifdef LUA_DEBUG
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
#define LUA_INTERNALERROR(s) assert(((void)s,0))
|
||||
#define LUA_ASSERT(c,s) assert(((void)s,(c)))
|
||||
#else
|
||||
#define LUA_INTERNALERROR(s) /* empty */
|
||||
#define LUA_ASSERT(c,s) /* empty */
|
||||
#endif
|
||||
/* tags for values visible from Lua */
|
||||
#define NUM_TAGS LUA_TTHREAD
|
||||
|
||||
|
||||
#ifdef LUA_DEBUG
|
||||
/* to avoid warnings, and make sure value is really unused */
|
||||
#define UNUSED(x) (x=0, (void)(x))
|
||||
#else
|
||||
#define UNUSED(x) ((void)(x)) /* to avoid warnings */
|
||||
#endif
|
||||
/*
|
||||
** Extra tags for non-values
|
||||
*/
|
||||
#define LUA_TPROTO (NUM_TAGS+1)
|
||||
#define LUA_TUPVAL (NUM_TAGS+2)
|
||||
|
||||
|
||||
/* mark for closures active in the stack */
|
||||
#define LUA_TMARK 6
|
||||
/*
|
||||
** Union of all collectable objects
|
||||
*/
|
||||
typedef union GCObject GCObject;
|
||||
|
||||
|
||||
/* tags for values visible from Lua == first user-created tag */
|
||||
#define NUM_TAGS 6
|
||||
/*
|
||||
** Common Header for all collectable objects (in macro form, to be
|
||||
** included in other objects)
|
||||
*/
|
||||
#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
|
||||
|
||||
|
||||
/* check whether `t' is a mark */
|
||||
#define is_T_MARK(t) ((t) == LUA_TMARK)
|
||||
/*
|
||||
** Common header in struct form
|
||||
*/
|
||||
typedef struct GCheader {
|
||||
CommonHeader;
|
||||
} GCheader;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Union of all Lua values
|
||||
*/
|
||||
typedef union {
|
||||
struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */
|
||||
struct Closure *cl; /* LUA_TFUNCTION */
|
||||
struct Hash *a; /* LUA_TTABLE */
|
||||
struct CallInfo *i; /* LUA_TLMARK */
|
||||
Number n; /* LUA_TNUMBER */
|
||||
GCObject *gc;
|
||||
void *p;
|
||||
lua_Number n;
|
||||
int b;
|
||||
} Value;
|
||||
|
||||
|
||||
/* Macros to access values */
|
||||
#define ttype(o) ((o)->ttype)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define clvalue(o) ((o)->value.cl)
|
||||
#define hvalue(o) ((o)->value.a)
|
||||
#define infovalue(o) ((o)->value.i)
|
||||
#define svalue(o) (tsvalue(o)->str)
|
||||
|
||||
|
||||
/*
|
||||
** Lua values (or `tagged objects')
|
||||
*/
|
||||
typedef struct lua_TObject {
|
||||
int ttype;
|
||||
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 ttisthread(o) (ttype(o) == LUA_TTHREAD)
|
||||
#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
|
||||
|
||||
/* Macros to access values */
|
||||
#define ttype(o) ((o)->tt)
|
||||
#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
|
||||
#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
|
||||
#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
|
||||
#define tsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
|
||||
#define uvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
|
||||
#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
|
||||
#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
|
||||
#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
|
||||
#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
|
||||
|
||||
#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.gc=cast(GCObject *, (x)); \
|
||||
lua_assert(i_o->value.gc->gch.tt == LUA_TSTRING); }
|
||||
|
||||
#define setuvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; \
|
||||
i_o->value.gc=cast(GCObject *, (x)); \
|
||||
lua_assert(i_o->value.gc->gch.tt == LUA_TUSERDATA); }
|
||||
|
||||
#define setthvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TTHREAD; \
|
||||
i_o->value.gc=cast(GCObject *, (x)); \
|
||||
lua_assert(i_o->value.gc->gch.tt == LUA_TTHREAD); }
|
||||
|
||||
#define setclvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; \
|
||||
i_o->value.gc=cast(GCObject *, (x)); \
|
||||
lua_assert(i_o->value.gc->gch.tt == LUA_TFUNCTION); }
|
||||
|
||||
#define sethvalue(obj,x) \
|
||||
{ TObject *i_o=(obj); i_o->tt=LUA_TTABLE; \
|
||||
i_o->value.gc=cast(GCObject *, (x)); \
|
||||
lua_assert(i_o->value.gc->gch.tt == LUA_TTABLE); }
|
||||
|
||||
#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** for internal debug only
|
||||
*/
|
||||
#define checkconsistency(obj) \
|
||||
lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
|
||||
|
||||
|
||||
#define setobj(obj1,obj2) \
|
||||
{ const TObject *o2=(obj2); TObject *o1=(obj1); \
|
||||
checkconsistency(o2); \
|
||||
o1->tt=o2->tt; o1->value = o2->value; }
|
||||
|
||||
|
||||
/*
|
||||
** different types of sets, according to destination
|
||||
*/
|
||||
|
||||
/* from stack to (same) stack */
|
||||
#define setobjs2s setobj
|
||||
/* to stack (not from same stack) */
|
||||
#define setobj2s setobj
|
||||
#define setsvalue2s setsvalue
|
||||
/* from table to same table */
|
||||
#define setobjt2t setobj
|
||||
/* to table */
|
||||
#define setobj2t setobj
|
||||
/* to new object */
|
||||
#define setobj2n setobj
|
||||
#define setsvalue2n setsvalue
|
||||
|
||||
#define setttype(obj, tt) (ttype(obj) = (tt))
|
||||
|
||||
|
||||
#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
|
||||
|
||||
|
||||
|
||||
typedef TObject *StkId; /* index to stack elements */
|
||||
|
||||
|
||||
/*
|
||||
** String headers for string table
|
||||
*/
|
||||
|
||||
/*
|
||||
** most `malloc' libraries allocate memory in blocks of 8 bytes. TSPACK
|
||||
** tries to make sizeof(TString) a multiple of this granularity, to reduce
|
||||
** waste of space.
|
||||
*/
|
||||
#define TSPACK ((int)sizeof(int))
|
||||
|
||||
typedef struct TString {
|
||||
union {
|
||||
struct { /* for strings */
|
||||
unsigned long hash;
|
||||
int constindex; /* hint to reuse constants */
|
||||
} s;
|
||||
struct { /* for userdata */
|
||||
int tag;
|
||||
void *value;
|
||||
} d;
|
||||
} u;
|
||||
size_t len;
|
||||
struct TString *nexthash; /* chain for hash table */
|
||||
int marked;
|
||||
char str[TSPACK]; /* variable length string!! must be the last field! */
|
||||
typedef union TString {
|
||||
L_Umaxalign dummy; /* ensures maximum alignment for strings */
|
||||
struct {
|
||||
CommonHeader;
|
||||
lu_byte reserved;
|
||||
lu_hash hash;
|
||||
size_t len;
|
||||
} tsv;
|
||||
} TString;
|
||||
|
||||
|
||||
#define getstr(ts) cast(const char *, (ts) + 1)
|
||||
#define svalue(o) getstr(tsvalue(o))
|
||||
|
||||
|
||||
|
||||
typedef union Udata {
|
||||
L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
|
||||
struct {
|
||||
CommonHeader;
|
||||
struct Table *metatable;
|
||||
size_t len;
|
||||
} uv;
|
||||
} Udata;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Function Prototypes
|
||||
*/
|
||||
typedef struct Proto {
|
||||
Number *knum; /* Number numbers used by the function */
|
||||
int nknum; /* size of `knum' */
|
||||
struct TString **kstr; /* strings used by the function */
|
||||
int nkstr; /* size of `kstr' */
|
||||
struct Proto **kproto; /* functions defined inside the function */
|
||||
int nkproto; /* size of `kproto' */
|
||||
CommonHeader;
|
||||
TObject *k; /* constants used by the function */
|
||||
Instruction *code;
|
||||
int ncode; /* size of `code'; when 0 means an incomplete `Proto' */
|
||||
short numparams;
|
||||
short is_vararg;
|
||||
short maxstacksize;
|
||||
short marked;
|
||||
struct Proto *next;
|
||||
/* debug information */
|
||||
struct Proto **p; /* functions defined inside the function */
|
||||
int *lineinfo; /* map from opcodes to source lines */
|
||||
int nlineinfo; /* size of `lineinfo' */
|
||||
int nlocvars;
|
||||
struct LocVar *locvars; /* information about local variables */
|
||||
int lineDefined;
|
||||
TString *source;
|
||||
int sizek; /* size of `k' */
|
||||
int sizecode;
|
||||
int sizelineinfo;
|
||||
int sizep; /* size of `p' */
|
||||
int sizelocvars;
|
||||
int lineDefined;
|
||||
GCObject *gclist;
|
||||
lu_byte nupvalues;
|
||||
lu_byte numparams;
|
||||
lu_byte is_vararg;
|
||||
lu_byte maxstacksize;
|
||||
} Proto;
|
||||
|
||||
|
||||
@@ -131,73 +236,98 @@ typedef struct LocVar {
|
||||
} LocVar;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Upvalues
|
||||
*/
|
||||
|
||||
typedef struct UpVal {
|
||||
CommonHeader;
|
||||
TObject *v; /* points to stack or to its own value */
|
||||
TObject value; /* the value (when closed) */
|
||||
} UpVal;
|
||||
|
||||
|
||||
/*
|
||||
** Closures
|
||||
*/
|
||||
typedef struct Closure {
|
||||
union {
|
||||
lua_CFunction c; /* C functions */
|
||||
struct Proto *l; /* Lua functions */
|
||||
} f;
|
||||
struct Closure *next;
|
||||
struct Closure *mark; /* marked closures (point to itself when not marked) */
|
||||
short isC; /* 0 for Lua functions, 1 for C functions */
|
||||
short nupvalues;
|
||||
|
||||
#define ClosureHeader \
|
||||
CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist
|
||||
|
||||
typedef struct CClosure {
|
||||
ClosureHeader;
|
||||
lua_CFunction f;
|
||||
TObject upvalue[1];
|
||||
} CClosure;
|
||||
|
||||
|
||||
typedef struct LClosure {
|
||||
ClosureHeader;
|
||||
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)->isC)
|
||||
|
||||
|
||||
typedef struct Node {
|
||||
TObject key;
|
||||
TObject val;
|
||||
struct Node *next; /* for chaining */
|
||||
} Node;
|
||||
|
||||
typedef struct Hash {
|
||||
Node *node;
|
||||
int htag;
|
||||
int size;
|
||||
Node *firstfree; /* this position is free; all positions after it are full */
|
||||
struct Hash *next;
|
||||
struct Hash *mark; /* marked tables (point to itself when not marked) */
|
||||
} Hash;
|
||||
|
||||
|
||||
/* unmarked tables and closures are represented by pointing `mark' to
|
||||
** themselves
|
||||
*/
|
||||
#define ismarked(x) ((x)->mark != (x))
|
||||
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
|
||||
#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
|
||||
|
||||
|
||||
/*
|
||||
** informations about a call (for debugging)
|
||||
** Tables
|
||||
*/
|
||||
typedef struct CallInfo {
|
||||
struct Closure *func; /* function being called */
|
||||
const Instruction **pc; /* current pc of called function */
|
||||
int lastpc; /* last pc traced */
|
||||
int line; /* current line */
|
||||
int refi; /* current index in `lineinfo' */
|
||||
} CallInfo;
|
||||
|
||||
typedef struct Node {
|
||||
TObject i_key;
|
||||
TObject i_val;
|
||||
struct Node *next; /* for chaining */
|
||||
} Node;
|
||||
|
||||
|
||||
typedef struct Table {
|
||||
CommonHeader;
|
||||
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
|
||||
lu_byte lsizenode; /* log2 of size of `node' array */
|
||||
struct Table *metatable;
|
||||
TObject *array; /* array part */
|
||||
Node *node;
|
||||
Node *firstfree; /* this position is free; all positions after it are full */
|
||||
GCObject *gclist;
|
||||
int sizearray; /* size of `array' array */
|
||||
} Table;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** `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;
|
||||
extern const char *const luaO_typenames[];
|
||||
|
||||
int luaO_log2 (unsigned int x);
|
||||
|
||||
|
||||
#define luaO_typename(o) (luaO_typenames[ttype(o)])
|
||||
int luaO_rawequalObj (const TObject *t1, const TObject *t2);
|
||||
int luaO_str2d (const char *s, lua_Number *result);
|
||||
|
||||
|
||||
lint32 luaO_power2 (lint32 n);
|
||||
char *luaO_openspace (lua_State *L, size_t n);
|
||||
|
||||
int luaO_equalObj (const TObject *t1, const TObject *t2);
|
||||
int luaO_str2d (const char *s, Number *result);
|
||||
|
||||
void luaO_verror (lua_State *L, const char *fmt, ...);
|
||||
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);
|
||||
|
||||
|
||||
|
||||
102
lopcodes.c
Normal file
102
lopcodes.c
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
** $Id: lopcodes.c,v 1.21 2002/08/20 20:03:05 roberto Exp roberto $
|
||||
** extracted automatically from lopcodes.h by mkprint.lua
|
||||
** DO NOT EDIT
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#define lopcodes_c
|
||||
|
||||
#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",
|
||||
"TEST",
|
||||
"CALL",
|
||||
"TAILCALL",
|
||||
"RETURN",
|
||||
"FORLOOP",
|
||||
"TFORLOOP",
|
||||
"TFORPREP",
|
||||
"SETLIST",
|
||||
"SETLISTO",
|
||||
"CLOSE",
|
||||
"CLOSURE"
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#define opmode(t,b,bk,ck,sa,k,m) (((t)<<OpModeT) | \
|
||||
((b)<<OpModeBreg) | ((bk)<<OpModeBrk) | ((ck)<<OpModeCrk) | \
|
||||
((sa)<<OpModesetA) | ((k)<<OpModeK) | (m))
|
||||
|
||||
|
||||
const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
/* T B Bk Ck sA K mode opcode */
|
||||
opmode(0, 1, 0, 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, 1, 0, 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, 1, 0, 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, 1, 0, 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, 1, 0, 0, 1, 0, iABC) /* OP_UNM */
|
||||
,opmode(0, 1, 0, 0, 1, 0, iABC) /* OP_NOT */
|
||||
,opmode(0, 1, 0, 1, 1, 0, iABC) /* OP_CONCAT */
|
||||
,opmode(0, 0, 0, 0, 0, 0, iAsBx) /* OP_JMP */
|
||||
,opmode(1, 0, 1, 1, 0, 0, iABC) /* OP_EQ */
|
||||
,opmode(1, 0, 1, 1, 0, 0, iABC) /* OP_LT */
|
||||
,opmode(1, 0, 1, 1, 0, 0, iABC) /* OP_LE */
|
||||
,opmode(1, 1, 0, 0, 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 */
|
||||
};
|
||||
|
||||
268
lopcodes.h
268
lopcodes.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.67 2000/08/29 14:48:16 roberto Exp roberto $
|
||||
** $Id: lopcodes.h,v 1.101 2002/08/20 20:03:05 roberto Exp roberto $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,29 +12,58 @@
|
||||
|
||||
/*===========================================================================
|
||||
We assume that instructions are unsigned numbers.
|
||||
All instructions have an opcode in the first 6 bits. Moreover,
|
||||
an instruction can have 0, 1, or 2 arguments. Instructions can
|
||||
have the following types:
|
||||
type 0: no arguments
|
||||
type 1: 1 unsigned argument in the higher bits (called `U')
|
||||
type 2: 1 signed argument in the higher bits (`S')
|
||||
type 3: 1st unsigned argument in the higher bits (`A')
|
||||
2nd unsigned argument in the middle bits (`B')
|
||||
All instructions have an opcode in the first 6 bits.
|
||||
Instructions can have the following fields:
|
||||
`A' : 8 bits
|
||||
`B' : 9 bits
|
||||
`C' : 9 bits
|
||||
`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.
|
||||
|
||||
The size of each argument is defined in `llimits.h'. The usual is an
|
||||
instruction with 32 bits, U arguments with 26 bits (32-6), B arguments
|
||||
with 9 bits, and A arguments with 17 bits (32-6-9). For small
|
||||
installations, the instruction size can be 16, so U has 10 bits,
|
||||
and A and B have 5 bits each.
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
|
||||
|
||||
|
||||
/*
|
||||
** size and position of opcode arguments.
|
||||
*/
|
||||
#define SIZE_C 9
|
||||
#define SIZE_B 9
|
||||
#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' */
|
||||
@@ -47,122 +76,163 @@
|
||||
** the following macros help to manipulate instructions
|
||||
*/
|
||||
|
||||
#define CREATE_0(o) ((Instruction)(o))
|
||||
#define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
|
||||
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
|
||||
#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 CREATE_U(o,u) ((Instruction)(o) | ((Instruction)(u)<<POS_U))
|
||||
#define GETARG_U(i) ((int)((i)>>POS_U))
|
||||
#define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
|
||||
((Instruction)(u)<<POS_U)))
|
||||
#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 CREATE_S(o,s) CREATE_U((o),(s)+MAXARG_S)
|
||||
#define GETARG_S(i) (GETARG_U(i)-MAXARG_S)
|
||||
#define SETARG_S(i,s) SETARG_U((i),(s)+MAXARG_S)
|
||||
|
||||
|
||||
#define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
|
||||
| ((Instruction)(b)<<POS_B))
|
||||
#define GETARG_A(i) ((int)((i)>>POS_A))
|
||||
#define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
|
||||
((Instruction)(a)<<POS_A)))
|
||||
#define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
|
||||
#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)) | \
|
||||
((Instruction)(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))
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** K = U argument used as index to `kstr'
|
||||
** J = S argument used as jump offset (relative to pc of next instruction)
|
||||
** L = unsigned argument used as index of local variable
|
||||
** N = U argument used as index to `knum'
|
||||
** invalid register that fits in 8 bits
|
||||
*/
|
||||
#define NO_REG MAXARG_A
|
||||
|
||||
|
||||
/*
|
||||
** R(x) - register
|
||||
** Kst(x) - constant (in constant table)
|
||||
** RK(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** grep "ORDER OP" if you change these enums
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
/*----------------------------------------------------------------------
|
||||
name args stack before stack after side effects
|
||||
name args description
|
||||
------------------------------------------------------------------------*/
|
||||
OP_END,/* - - (return) no results */
|
||||
OP_RETURN,/* U v_n-v_x(at u) (return) returns v_x-v_n */
|
||||
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_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
|
||||
OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
|
||||
OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */
|
||||
OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
|
||||
|
||||
OP_PUSHNIL,/* U - nil_1-nil_u */
|
||||
OP_POP,/* U a_u-a_1 - */
|
||||
OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */
|
||||
OP_SETUPVAL,/* A B UpValue[B] := R(A) */
|
||||
OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */
|
||||
|
||||
OP_PUSHINT,/* S - (Number)s */
|
||||
OP_PUSHSTRING,/* K - KSTR[k] */
|
||||
OP_PUSHNUM,/* N - KNUM[n] */
|
||||
OP_PUSHNEGNUM,/* N - -KNUM[n] */
|
||||
OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
|
||||
|
||||
OP_PUSHUPVALUE,/* U - Closure[u] */
|
||||
OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
|
||||
|
||||
OP_GETLOCAL,/* L - LOC[l] */
|
||||
OP_GETGLOBAL,/* K - VAR[KSTR[k]] */
|
||||
OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
|
||||
OP_SUB,/* A B C R(A) := RK(B) - RK(C) */
|
||||
OP_MUL,/* A B C R(A) := RK(B) * RK(C) */
|
||||
OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
|
||||
OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */
|
||||
OP_UNM,/* A B R(A) := -R(B) */
|
||||
OP_NOT,/* A B R(A) := not R(B) */
|
||||
|
||||
OP_GETTABLE,/* - i t t[i] */
|
||||
OP_GETDOTTED,/* K t t[KSTR[k]] */
|
||||
OP_GETINDEXED,/* L t t[LOC[l]] */
|
||||
OP_PUSHSELF,/* K t t t[KSTR[k]] */
|
||||
OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
|
||||
|
||||
OP_CREATETABLE,/* U - newarray(size = u) */
|
||||
OP_JMP,/* sBx PC += sBx */
|
||||
|
||||
OP_SETLOCAL,/* L x - LOC[l]=x */
|
||||
OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
|
||||
OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */
|
||||
OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
|
||||
OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
|
||||
OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
|
||||
|
||||
OP_SETLIST,/* A B v_b-v_1 t t t[i+a*FPF]=v_i */
|
||||
OP_SETMAP,/* U v_u k_u - v_1 k_1 t t t[k_i]=v_i */
|
||||
OP_TEST,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
|
||||
|
||||
OP_ADD,/* - y x x+y */
|
||||
OP_ADDI,/* S x x+s */
|
||||
OP_SUB,/* - y x x-y */
|
||||
OP_MULT,/* - y x x*y */
|
||||
OP_DIV,/* - y x x/y */
|
||||
OP_POW,/* - y x x^y */
|
||||
OP_CONCAT,/* U v_u-v_1 v1..-..v_u */
|
||||
OP_MINUS,/* - x -x */
|
||||
OP_NOT,/* - x (x==nil)? 1 : nil */
|
||||
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_JMPNE,/* J y x - (x~=y)? PC+=s */
|
||||
OP_JMPEQ,/* J y x - (x==y)? PC+=s */
|
||||
OP_JMPLT,/* J y x - (x<y)? PC+=s */
|
||||
OP_JMPLE,/* J y x - (x<y)? PC+=s */
|
||||
OP_JMPGT,/* J y x - (x>y)? PC+=s */
|
||||
OP_JMPGE,/* J y x - (x>=y)? PC+=s */
|
||||
OP_FORLOOP,/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then PC+= sBx */
|
||||
|
||||
OP_JMPT,/* J x - (x~=nil)? PC+=s */
|
||||
OP_JMPF,/* J x - (x==nil)? PC+=s */
|
||||
OP_JMPONT,/* J x (x~=nil)? x : - (x~=nil)? PC+=s */
|
||||
OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */
|
||||
OP_JMP,/* J - - PC+=s */
|
||||
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_PUSHNILJMP,/* - - nil PC++; */
|
||||
|
||||
OP_FORPREP,/* J */
|
||||
OP_FORLOOP,/* J */
|
||||
|
||||
OP_LFORPREP,/* J */
|
||||
OP_LFORLOOP,/* J */
|
||||
|
||||
OP_CLOSURE/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
|
||||
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 ((int)OP_CLOSURE+1)
|
||||
|
||||
|
||||
#define ISJUMP(o) (OP_JMPNE <= (o) && (o) <= OP_JMP)
|
||||
#define NUM_OPCODES (cast(int, OP_CLOSURE+1))
|
||||
|
||||
|
||||
|
||||
/* special code to fit a LUA_MULTRET inside an argB */
|
||||
#define MULT_RET 255 /* (<=MAXARG_B) */
|
||||
#if MULT_RET>MAXARG_B
|
||||
#undef MULT_RET
|
||||
#define MULT_RET MAXARG_B
|
||||
/*===========================================================================
|
||||
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 */
|
||||
OpModeBrk, /* B is a register/constant */
|
||||
OpModeCrk, /* 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
|
||||
|
||||
55
lparser.h
55
lparser.h
@@ -1,13 +1,15 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.25 2000/09/29 12:42:13 roberto Exp roberto $
|
||||
** LL(1) Parser and code generator for Lua
|
||||
** $Id: lparser.h,v 1.44 2002/05/14 17:52:22 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"
|
||||
|
||||
|
||||
@@ -16,45 +18,54 @@
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
VGLOBAL,
|
||||
VLOCAL,
|
||||
VINDEXED,
|
||||
VEXP
|
||||
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;
|
||||
union {
|
||||
int index; /* VGLOBAL: `kstr' index of global name; VLOCAL: stack index */
|
||||
struct {
|
||||
int t; /* patch list of `exit when true' */
|
||||
int f; /* patch list of `exit when false' */
|
||||
} l;
|
||||
} u;
|
||||
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 */
|
||||
int pc; /* next position to code */
|
||||
struct BlockCnt *bl; /* chain of current blocks */
|
||||
int pc; /* next position to code (equivalent to `ncode') */
|
||||
int lasttarget; /* `pc' of last `jump target' */
|
||||
int jlt; /* list of jumps to `lasttarget' */
|
||||
short stacklevel; /* number of values on activation register */
|
||||
short nactloc; /* number of active local variables */
|
||||
short nupvalues; /* number of upvalues */
|
||||
int lastline; /* line where last `lineinfo' was generated */
|
||||
struct Breaklabel *bl; /* chain of breakable blocks */
|
||||
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 actloc[MAXLOCALS]; /* local-variable stack (indices to locvars) */
|
||||
int actvar[MAXVARS]; /* declared-variable stack */
|
||||
} FuncState;
|
||||
|
||||
|
||||
Proto *luaY_parser (lua_State *L, ZIO *z);
|
||||
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
234
lstate.c
234
lstate.c
@@ -1,15 +1,19 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 1.47 2000/10/26 12:47:05 roberto Exp roberto $
|
||||
** $Id: lstate.c,v 1.116 2002/11/22 17:16:52 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define lstate_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
@@ -19,103 +23,183 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
#ifdef LUA_DEBUG
|
||||
static lua_State *lua_state = NULL;
|
||||
void luaB_opentests (lua_State *L);
|
||||
/*
|
||||
** macro to allow the inclusion of user information in Lua state
|
||||
*/
|
||||
#ifndef LUA_USERSTATE
|
||||
#define EXTRASPACE 0
|
||||
#else
|
||||
union UEXTRASPACE {L_Umaxalign a; LUA_USERSTATE b;};
|
||||
#define EXTRASPACE (sizeof(union UEXTRASPACE))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** built-in implementation for ERRORMESSAGE. In a "correct" environment
|
||||
** ERRORMESSAGE should have an external definition, and so this function
|
||||
** would not be used.
|
||||
** you can change this function through the official API:
|
||||
** call `lua_setpanicf'
|
||||
*/
|
||||
static int errormessage (lua_State *L) {
|
||||
const char *s = lua_tostring(L, 1);
|
||||
if (s == NULL) s = "(no message)";
|
||||
fprintf(stderr, "error: %s\n", s);
|
||||
static int default_panic (lua_State *L) {
|
||||
UNUSED(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static lua_State *mallocstate (lua_State *L) {
|
||||
lu_byte *block = (lu_byte *)luaM_malloc(L, sizeof(lua_State) + EXTRASPACE);
|
||||
if (block == NULL) return NULL;
|
||||
else {
|
||||
block += EXTRASPACE;
|
||||
return cast(lua_State *, block);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void freestate (lua_State *L, lua_State *L1) {
|
||||
luaM_free(L, cast(lu_byte *, L1) - EXTRASPACE,
|
||||
sizeof(lua_State) + EXTRASPACE);
|
||||
}
|
||||
|
||||
|
||||
static void stack_init (lua_State *L1, lua_State *L) {
|
||||
L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TObject);
|
||||
L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
|
||||
L1->top = L1->stack;
|
||||
L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
|
||||
L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
|
||||
L1->ci = L1->base_ci;
|
||||
L1->ci->state = CI_C; /* not a Lua function */
|
||||
setnilvalue(L1->top++); /* `function' entry for this `ci' */
|
||||
L1->base = L1->ci->base = L1->top;
|
||||
L1->ci->top = L1->top + LUA_MINSTACK;
|
||||
L1->size_ci = BASIC_CI_SIZE;
|
||||
L1->end_ci = L1->base_ci + L1->size_ci;
|
||||
}
|
||||
|
||||
|
||||
static void freestack (lua_State *L, lua_State *L1) {
|
||||
luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
|
||||
luaM_freearray(L, L1->stack, L1->stacksize, TObject);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** open parts that may cause memory-allocation errors
|
||||
*/
|
||||
static void f_luaopen (lua_State *L, void *ud) {
|
||||
int stacksize = *(int *)ud;
|
||||
if (stacksize == 0)
|
||||
stacksize = DEFAULT_STACK_SIZE;
|
||||
else
|
||||
stacksize += LUA_MINSTACK;
|
||||
L->gt = luaH_new(L, 10); /* table of globals */
|
||||
luaD_init(L, stacksize);
|
||||
luaS_init(L);
|
||||
luaX_init(L);
|
||||
/* create a new global state */
|
||||
global_State *g = luaM_new(NULL, global_State);
|
||||
UNUSED(ud);
|
||||
if (g == NULL) luaD_throw(L, LUA_ERRMEM);
|
||||
L->l_G = g;
|
||||
g->mainthread = L;
|
||||
g->GCthreshold = 0; /* mark it as unfinished state */
|
||||
g->strt.size = 0;
|
||||
g->strt.nuse = 0;
|
||||
g->strt.hash = NULL;
|
||||
setnilvalue(defaultmeta(L));
|
||||
setnilvalue(registry(L));
|
||||
luaZ_initbuffer(L, &g->buff);
|
||||
g->panic = &default_panic;
|
||||
g->rootgc = NULL;
|
||||
g->rootudata = NULL;
|
||||
g->tmudata = NULL;
|
||||
setnilvalue(key(g->dummynode));
|
||||
setnilvalue(val(g->dummynode));
|
||||
g->dummynode->next = NULL;
|
||||
g->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 */
|
||||
defaultmeta(L)->tt = LUA_TTABLE;
|
||||
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);
|
||||
lua_newtable(L);
|
||||
lua_ref(L, 1); /* create registry */
|
||||
lua_register(L, LUA_ERRORMESSAGE, errormessage);
|
||||
#ifdef LUA_DEBUG
|
||||
luaB_opentests(L);
|
||||
if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
|
||||
#endif
|
||||
LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack");
|
||||
luaX_init(L);
|
||||
luaS_fix(luaS_newliteral(L, MEMERRMSG));
|
||||
g->GCthreshold = 4*G(L)->nblocks;
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_State *lua_open (int stacksize) {
|
||||
lua_State *L = luaM_new(NULL, lua_State);
|
||||
if (L == NULL) return NULL; /* memory allocation error */
|
||||
static void preinit_state (lua_State *L) {
|
||||
L->stack = NULL;
|
||||
L->strt.size = L->udt.size = 0;
|
||||
L->strt.nuse = L->udt.nuse = 0;
|
||||
L->strt.hash = NULL;
|
||||
L->udt.hash = NULL;
|
||||
L->Mbuffer = NULL;
|
||||
L->Mbuffsize = 0;
|
||||
L->rootproto = NULL;
|
||||
L->rootcl = NULL;
|
||||
L->roottable = NULL;
|
||||
L->TMtable = NULL;
|
||||
L->last_tag = -1;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
L->refFree = NONEXT;
|
||||
L->nblocks = sizeof(lua_State);
|
||||
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
||||
L->callhook = NULL;
|
||||
L->linehook = NULL;
|
||||
L->allowhooks = 1;
|
||||
L->stacksize = 0;
|
||||
L->errorJmp = NULL;
|
||||
if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
|
||||
/* memory allocation error: free partial state */
|
||||
lua_close(L);
|
||||
return NULL;
|
||||
L->hook = NULL;
|
||||
L->hookmask = L->hookinit = 0;
|
||||
L->basehookcount = 0;
|
||||
L->allowhook = 1;
|
||||
resethookcount(L);
|
||||
L->openupval = NULL;
|
||||
L->size_ci = 0;
|
||||
L->nCcalls = 0;
|
||||
L->base_ci = L->ci = NULL;
|
||||
L->errfunc = 0;
|
||||
setnilvalue(gt(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_sweep(L, 1); /* collect all elements */
|
||||
lua_assert(G(L)->rootgc == NULL);
|
||||
lua_assert(G(L)->rootudata == NULL);
|
||||
luaS_freeall(L);
|
||||
luaZ_freebuffer(L, &G(L)->buff);
|
||||
}
|
||||
L->GCthreshold = 2*L->nblocks;
|
||||
freestack(L, L);
|
||||
if (G(L)) {
|
||||
lua_assert(G(L)->nblocks == sizeof(lua_State) + sizeof(global_State));
|
||||
luaM_freelem(NULL, G(L));
|
||||
}
|
||||
freestate(NULL, L);
|
||||
}
|
||||
|
||||
|
||||
lua_State *luaE_newthread (lua_State *L) {
|
||||
lua_State *L1 = mallocstate(L);
|
||||
luaC_link(L, valtogco(L1), LUA_TTHREAD);
|
||||
preinit_state(L1);
|
||||
L1->l_G = L->l_G;
|
||||
stack_init(L1, L); /* init stack */
|
||||
setobj2n(gt(L1), gt(L)); /* share table of globals */
|
||||
return L1;
|
||||
}
|
||||
|
||||
|
||||
void luaE_freethread (lua_State *L, lua_State *L1) {
|
||||
luaF_close(L1, L1->stack); /* close all upvalues for this thread */
|
||||
lua_assert(L1->openupval == NULL);
|
||||
freestack(L, L1);
|
||||
freestate(L, L1);
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_State *lua_open (void) {
|
||||
lua_State *L = mallocstate(NULL);
|
||||
if (L) { /* allocation OK? */
|
||||
L->tt = LUA_TTHREAD;
|
||||
preinit_state(L);
|
||||
L->l_G = NULL;
|
||||
if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
|
||||
/* memory allocation error: free partial state */
|
||||
close_state(L);
|
||||
L = NULL;
|
||||
}
|
||||
}
|
||||
lua_userstateopen(L);
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_close (lua_State *L) {
|
||||
LUA_ASSERT(L != lua_state || lua_gettop(L) == 0, "garbage in C stack");
|
||||
luaC_collect(L, 1); /* collect all elements */
|
||||
LUA_ASSERT(L->rootproto == NULL, "list should be empty");
|
||||
LUA_ASSERT(L->rootcl == NULL, "list should be empty");
|
||||
LUA_ASSERT(L->roottable == NULL, "list should be empty");
|
||||
luaS_freeall(L);
|
||||
if (L->stack)
|
||||
L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
|
||||
luaM_free(L, L->stack);
|
||||
L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
|
||||
luaM_free(L, L->TMtable);
|
||||
L->nblocks -= (L->refSize)*sizeof(struct Ref);
|
||||
luaM_free(L, L->refArray);
|
||||
L->nblocks -= (L->Mbuffsize)*sizeof(char);
|
||||
luaM_free(L, L->Mbuffer);
|
||||
LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks");
|
||||
luaM_free(L, L);
|
||||
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
|
||||
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
|
||||
lua_lock(L);
|
||||
L = G(L)->mainthread; /* only the main thread can be closed */
|
||||
luaC_callallgcTM(L); /* call GC tag methods for all udata */
|
||||
lua_assert(G(L)->tmudata == NULL);
|
||||
close_state(L);
|
||||
}
|
||||
|
||||
|
||||
205
lstate.h
205
lstate.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 1.40 2000/09/29 12:42:13 roberto Exp roberto $
|
||||
** $Id: lstate.h,v 1.107 2002/11/22 18:01:46 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,71 +7,188 @@
|
||||
#ifndef lstate_h
|
||||
#define lstate_h
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
|
||||
typedef TObject *StkId; /* index to stack elements */
|
||||
#include "lobject.h"
|
||||
#include "ltm.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
/*
|
||||
** marks for Reference array
|
||||
** macros for thread synchronization inside Lua core machine:
|
||||
** all accesses to the global state and to global objects are synchronized.
|
||||
** Because threads can read the stack of other threads
|
||||
** (when running garbage collection),
|
||||
** a thread must also synchronize any write-access to its own stack.
|
||||
** Unsynchronized accesses are allowed only when reading its own stack,
|
||||
** or when reading immutable fields from global objects
|
||||
** (such as string values and udata values).
|
||||
*/
|
||||
#define NONEXT -1 /* to end the free list */
|
||||
#define HOLD -2
|
||||
#define COLLECTED -3
|
||||
#define LOCK -4
|
||||
#ifndef lua_lock
|
||||
#define lua_lock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
#ifndef lua_unlock
|
||||
#define lua_unlock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
|
||||
struct Ref {
|
||||
TObject o;
|
||||
int st; /* can be LOCK, HOLD, COLLECTED, or next (for free list) */
|
||||
};
|
||||
#ifndef lua_userstateopen
|
||||
#define lua_userstateopen(l)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
struct lua_longjmp; /* defined in ldo.c */
|
||||
struct TM; /* defined in ltm.h */
|
||||
|
||||
|
||||
/* default meta table (both for tables and udata) */
|
||||
#define defaultmeta(L) (&G(L)->_defaultmeta)
|
||||
|
||||
/* table of globals */
|
||||
#define gt(L) (&L->_gt)
|
||||
|
||||
/* registry */
|
||||
#define registry(L) (&G(L)->_registry)
|
||||
|
||||
|
||||
/* 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 {
|
||||
GCObject **hash;
|
||||
ls_nstr nuse; /* number of elements */
|
||||
int size;
|
||||
lint32 nuse; /* number of elements */
|
||||
TString **hash;
|
||||
} 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' */
|
||||
} l;
|
||||
struct { /* for C functions */
|
||||
int dummy; /* just to avoid an empty struct */
|
||||
} c;
|
||||
} u;
|
||||
} CallInfo;
|
||||
|
||||
struct lua_State {
|
||||
/* thread-specific state */
|
||||
StkId top; /* first free slot in the stack */
|
||||
StkId stack; /* stack base */
|
||||
StkId stack_last; /* last free slot in the stack */
|
||||
int stacksize;
|
||||
StkId Cbase; /* base for current C function */
|
||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||
char *Mbuffer; /* global buffer */
|
||||
size_t Mbuffsize; /* size of Mbuffer */
|
||||
/* global state */
|
||||
Proto *rootproto; /* list of all prototypes */
|
||||
Closure *rootcl; /* list of all closures */
|
||||
Hash *roottable; /* list of all tables */
|
||||
|
||||
/*
|
||||
** bit fields for `CallInfo.state'
|
||||
*/
|
||||
#define CI_C (1<<0) /* 1 if function is a C function */
|
||||
/* 1 if (Lua) function has an active `luaV_execute' running it */
|
||||
#define CI_HASFRAME (1<<1)
|
||||
/* 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 (1<<2)
|
||||
#define CI_SAVEDPC (1<<3) /* 1 if `savedpc' is updated */
|
||||
#define CI_YIELD (1<<4) /* 1 if thread is suspended */
|
||||
|
||||
|
||||
#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 */
|
||||
stringtable udt; /* hash table for udata */
|
||||
Hash *gt; /* table for globals */
|
||||
struct TM *TMtable; /* table for tag methods */
|
||||
int last_tag; /* last used tag in TMtable */
|
||||
struct Ref *refArray; /* locked objects */
|
||||
int refSize; /* size of refArray */
|
||||
int refFree; /* list of free positions in refArray */
|
||||
unsigned long GCthreshold;
|
||||
unsigned long nblocks; /* number of `bytes' currently allocated */
|
||||
lua_Hook callhook;
|
||||
lua_Hook linehook;
|
||||
int allowhooks;
|
||||
GCObject *rootgc; /* list of (almost) all collectable objects */
|
||||
GCObject *rootudata; /* (separated) list of all userdata */
|
||||
GCObject *tmudata; /* list of userdata to be GC */
|
||||
Mbuffer buff; /* temporary buffer for string concatentation */
|
||||
lu_mem GCthreshold;
|
||||
lu_mem nblocks; /* number of `bytes' currently allocated */
|
||||
lua_CFunction panic; /* to be called in unprotected errors */
|
||||
TObject _registry;
|
||||
TObject _defaultmeta;
|
||||
struct lua_State *mainthread;
|
||||
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 {
|
||||
CommonHeader;
|
||||
StkId top; /* first free slot in the stack */
|
||||
StkId base; /* base of current function */
|
||||
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 */
|
||||
unsigned short size_ci; /* size of array `base_ci' */
|
||||
unsigned short nCcalls; /* number of nested C calls */
|
||||
lu_byte hookmask;
|
||||
lu_byte allowhook;
|
||||
lu_byte hookinit;
|
||||
int basehookcount;
|
||||
int hookcount;
|
||||
lua_Hook hook;
|
||||
TObject _gt; /* table of globals */
|
||||
GCObject *openupval; /* list of open upvalues in this stack */
|
||||
GCObject *gclist;
|
||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
||||
};
|
||||
|
||||
|
||||
#define G(L) (L->l_G)
|
||||
|
||||
|
||||
/*
|
||||
** Union of all collectable objects
|
||||
*/
|
||||
union GCObject {
|
||||
GCheader gch;
|
||||
union TString ts;
|
||||
union Udata u;
|
||||
union Closure cl;
|
||||
struct Table h;
|
||||
struct Proto p;
|
||||
struct UpVal uv;
|
||||
struct lua_State th; /* thread */
|
||||
};
|
||||
|
||||
|
||||
/* macros to convert a GCObject into a specific value */
|
||||
#define gcotots(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
|
||||
#define gcotou(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
|
||||
#define gcotocl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
|
||||
#define gcotoh(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
|
||||
#define gcotop(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
|
||||
#define gcotouv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
|
||||
#define ngcotouv(o) \
|
||||
check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
|
||||
#define gcototh(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
|
||||
|
||||
/* macro to convert any value into a GCObject */
|
||||
#define valtogco(v) (cast(GCObject *, (v)))
|
||||
|
||||
|
||||
lua_State *luaE_newthread (lua_State *L);
|
||||
void luaE_freethread (lua_State *L, lua_State *L1);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
163
lstring.c
163
lstring.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 1.44 2000/10/26 12:47:05 roberto Exp roberto $
|
||||
** $Id: lstring.c,v 1.77 2002/11/13 11:32:26 roberto Exp roberto $
|
||||
** String table (keeps all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define lstring_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lmem.h"
|
||||
@@ -15,141 +17,86 @@
|
||||
#include "lstring.h"
|
||||
|
||||
|
||||
/*
|
||||
** type equivalent to TString, but with maximum alignment requirements
|
||||
*/
|
||||
union L_UTString {
|
||||
TString ts;
|
||||
union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
|
||||
};
|
||||
|
||||
|
||||
|
||||
void luaS_init (lua_State *L) {
|
||||
L->strt.hash = luaM_newvector(L, 1, TString *);
|
||||
L->udt.hash = luaM_newvector(L, 1, TString *);
|
||||
L->nblocks += 2*sizeof(TString *);
|
||||
L->strt.size = L->udt.size = 1;
|
||||
L->strt.nuse = L->udt.nuse = 0;
|
||||
L->strt.hash[0] = L->udt.hash[0] = NULL;
|
||||
}
|
||||
|
||||
|
||||
void luaS_freeall (lua_State *L) {
|
||||
LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
|
||||
L->nblocks -= (L->strt.size + L->udt.size)*sizeof(TString *);
|
||||
luaM_free(L, L->strt.hash);
|
||||
LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
|
||||
luaM_free(L, L->udt.hash);
|
||||
lua_assert(G(L)->strt.nuse==0);
|
||||
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
|
||||
}
|
||||
|
||||
|
||||
static unsigned long hash_s (const char *s, size_t l) {
|
||||
unsigned long h = l; /* seed */
|
||||
size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */
|
||||
for (; l>=step; l-=step)
|
||||
h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
|
||||
TString **newhash = luaM_newvector(L, newsize, TString *);
|
||||
void luaS_resize (lua_State *L, int newsize) {
|
||||
GCObject **newhash = luaM_newvector(L, newsize, GCObject *);
|
||||
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];
|
||||
GCObject *p = tb->hash[i];
|
||||
while (p) { /* for each node in the list */
|
||||
TString *next = p->nexthash; /* save next */
|
||||
unsigned long h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value);
|
||||
int h1 = h&(newsize-1); /* new position */
|
||||
LUA_ASSERT(h%newsize == (h&(newsize-1)),
|
||||
"a&(x-1) == a%x, for x power of 2");
|
||||
p->nexthash = newhash[h1]; /* chain it in new position */
|
||||
GCObject *next = p->gch.next; /* save next */
|
||||
lu_hash h = gcotots(p)->tsv.hash;
|
||||
int h1 = lmod(h, newsize); /* new position */
|
||||
lua_assert(cast(int, h%newsize) == lmod(h, newsize));
|
||||
p->gch.next = newhash[h1]; /* chain it */
|
||||
newhash[h1] = p;
|
||||
p = next;
|
||||
}
|
||||
}
|
||||
luaM_free(L, tb->hash);
|
||||
L->nblocks += (newsize - tb->size)*sizeof(TString *);
|
||||
luaM_freearray(L, tb->hash, tb->size, TString *);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
|
||||
ts->nexthash = tb->hash[h]; /* chain new entry */
|
||||
tb->hash[h] = ts;
|
||||
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.len = l;
|
||||
ts->tsv.hash = h;
|
||||
ts->tsv.marked = 0;
|
||||
ts->tsv.tt = LUA_TSTRING;
|
||||
ts->tsv.reserved = 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.next = tb->hash[h]; /* chain new entry */
|
||||
tb->hash[h] = valtogco(ts);
|
||||
tb->nuse++;
|
||||
if (tb->nuse > (lint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */
|
||||
luaS_resize(L, tb, tb->size*2);
|
||||
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) {
|
||||
unsigned long h = hash_s(str, l);
|
||||
int h1 = h & (L->strt.size-1);
|
||||
TString *ts;
|
||||
for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) {
|
||||
if (ts->len == l && (memcmp(str, ts->str, l) == 0))
|
||||
GCObject *o;
|
||||
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 (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
|
||||
o != NULL;
|
||||
o = o->gch.next) {
|
||||
TString *ts = gcotots(o);
|
||||
if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
|
||||
return ts;
|
||||
}
|
||||
/* not found */
|
||||
ts = (TString *)luaM_malloc(L, sizestring(l));
|
||||
ts->marked = 0;
|
||||
ts->nexthash = NULL;
|
||||
ts->len = l;
|
||||
ts->u.s.hash = h;
|
||||
ts->u.s.constindex = 0;
|
||||
memcpy(ts->str, str, l);
|
||||
ts->str[l] = 0; /* ending 0 */
|
||||
L->nblocks += sizestring(l);
|
||||
newentry(L, &L->strt, ts, h1); /* insert it on table */
|
||||
return ts;
|
||||
return newlstr(L, str, l, h); /* not found */
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
|
||||
union L_UTString *uts = (union L_UTString *)luaM_malloc(L,
|
||||
(lint32)sizeof(union L_UTString)+s);
|
||||
TString *ts = &uts->ts;
|
||||
ts->marked = 0;
|
||||
ts->nexthash = NULL;
|
||||
ts->len = s;
|
||||
ts->u.d.tag = 0;
|
||||
ts->u.d.value = (udata == NULL) ? uts+1 : udata;
|
||||
L->nblocks += sizestring(s);
|
||||
/* insert it on table */
|
||||
newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1));
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_createudata (lua_State *L, void *udata, int tag) {
|
||||
int h1 = IntPoint(udata) & (L->udt.size-1);
|
||||
TString *ts;
|
||||
for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
|
||||
if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
|
||||
return ts;
|
||||
}
|
||||
/* not found */
|
||||
ts = luaS_newudata(L, 0, udata);
|
||||
if (tag != LUA_ANYTAG)
|
||||
ts->u.d.tag = tag;
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_new (lua_State *L, const char *str) {
|
||||
return luaS_newlstr(L, str, strlen(str));
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_newfixed (lua_State *L, const char *str) {
|
||||
TString *ts = luaS_new(L, str);
|
||||
if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
|
||||
return ts;
|
||||
Udata *luaS_newudata (lua_State *L, size_t s) {
|
||||
Udata *u;
|
||||
u = cast(Udata *, luaM_malloc(L, sizeudata(s)));
|
||||
u->uv.marked = (1<<1); /* is not finalized */
|
||||
u->uv.tt = LUA_TUSERDATA;
|
||||
u->uv.len = s;
|
||||
u->uv.metatable = hvalue(defaultmeta(L));
|
||||
/* chain it on udata list */
|
||||
u->uv.next = G(L)->rootudata;
|
||||
G(L)->rootudata = valtogco(u);
|
||||
return u;
|
||||
}
|
||||
|
||||
|
||||
26
lstring.h
26
lstring.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.23 2000/10/26 12:47:05 roberto Exp roberto $
|
||||
** $Id: lstring.h,v 1.36 2002/04/30 13:01:48 roberto Exp roberto $
|
||||
** String table (keep all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,26 +12,22 @@
|
||||
#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 sizestring(l) ((long)sizeof(TString) + \
|
||||
((long)(l+1)-TSPACK)*(long)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))
|
||||
|
||||
void luaS_init (lua_State *L);
|
||||
void luaS_resize (lua_State *L, stringtable *tb, int newsize);
|
||||
TString *luaS_newudata (lua_State *L, size_t s, void *udata);
|
||||
TString *luaS_createudata (lua_State *L, void *udata, int tag);
|
||||
#define luaS_fix(s) ((s)->tsv.marked |= (1<<4))
|
||||
|
||||
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);
|
||||
TString *luaS_new (lua_State *L, const char *str);
|
||||
TString *luaS_newfixed (lua_State *L, const char *str);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
585
lstrlib.c
585
lstrlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstrlib.c,v 1.55 2000/10/20 16:39:03 roberto Exp roberto $
|
||||
** $Id: lstrlib.c,v 1.91 2002/11/25 17:33:33 roberto Exp roberto $
|
||||
** Standard library for string operations and pattern-matching
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,37 +11,47 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lstrlib_c
|
||||
|
||||
#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);
|
||||
luaL_checklstring(L, 1, &l);
|
||||
lua_pushnumber(L, l);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static long posrelat (long pos, size_t len) {
|
||||
static sint32 posrelat (sint32 pos, size_t len) {
|
||||
/* relative string position: negative means back from end */
|
||||
return (pos>=0) ? pos : (long)len+pos+1;
|
||||
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);
|
||||
long start = posrelat(luaL_check_long(L, 2), l);
|
||||
long end = posrelat(luaL_opt_long(L, 3, -1), l);
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
sint32 start = posrelat(luaL_checklong(L, 2), l);
|
||||
sint32 end = posrelat(luaL_optlong(L, 3, -1), l);
|
||||
if (start < 1) start = 1;
|
||||
if (end > (long)l) end = l;
|
||||
if (end > (sint32)l) end = l;
|
||||
if (start <= end)
|
||||
lua_pushlstring(L, s+start-1, end-start+1);
|
||||
else lua_pushstring(L, "");
|
||||
else lua_pushliteral(L, "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -50,10 +60,10 @@ 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);
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=0; i<l; i++)
|
||||
luaL_putchar(&b, tolower((unsigned char)(s[i])));
|
||||
luaL_putchar(&b, tolower(uchar(s[i])));
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
@@ -63,10 +73,10 @@ 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);
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=0; i<l; i++)
|
||||
luaL_putchar(&b, toupper((unsigned char)(s[i])));
|
||||
luaL_putchar(&b, toupper(uchar(s[i])));
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
@@ -74,8 +84,8 @@ static int str_upper (lua_State *L) {
|
||||
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);
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
int n = luaL_checkint(L, 2);
|
||||
luaL_buffinit(L, &b);
|
||||
while (n-- > 0)
|
||||
luaL_addlstring(&b, s, l);
|
||||
@@ -86,10 +96,10 @@ static int str_rep (lua_State *L) {
|
||||
|
||||
static int str_byte (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
long 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, (unsigned char)s[pos-1]);
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
sint32 pos = posrelat(luaL_optlong(L, 2, 1), l);
|
||||
luaL_argcheck(L, 0 < pos && (size_t)(pos) <= l, 2, "out of range");
|
||||
lua_pushnumber(L, uchar(s[pos-1]));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -100,15 +110,33 @@ static int str_char (lua_State *L) {
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=1; i<=n; i++) {
|
||||
int c = luaL_check_int(L, i);
|
||||
luaL_arg_check(L, (unsigned char)c == c, i, "invalid value");
|
||||
luaL_putchar(&b, (unsigned char)c);
|
||||
int c = luaL_checkint(L, i);
|
||||
luaL_argcheck(L, uchar(c) == c, i, "invalid value");
|
||||
luaL_putchar(&b, uchar(c));
|
||||
}
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int writer (lua_State *L, const void* b, size_t size, void* B) {
|
||||
(void)L;
|
||||
luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int str_dump (lua_State *L) {
|
||||
luaL_Buffer b;
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
luaL_buffinit(L,&b);
|
||||
if (!lua_dump(L, writer, &b))
|
||||
luaL_error(L, "unable to dump given function");
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
@@ -121,51 +149,61 @@ static int str_char (lua_State *L) {
|
||||
#endif
|
||||
|
||||
|
||||
struct Capture {
|
||||
const char *src_end; /* end ('\0') of source string */
|
||||
#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;
|
||||
long len; /* -1 signals unfinished capture */
|
||||
sint32 len;
|
||||
} capture[MAX_CAPTURES];
|
||||
};
|
||||
} MatchState;
|
||||
|
||||
|
||||
#define ESC '%'
|
||||
#define SPECIALS "^$*+?.([%-"
|
||||
|
||||
|
||||
static int check_capture (lua_State *L, int l, struct Capture *cap) {
|
||||
static int check_capture (MatchState *ms, int l) {
|
||||
l -= '1';
|
||||
if (!(0 <= l && l < cap->level && cap->capture[l].len != -1))
|
||||
lua_error(L, "invalid capture index");
|
||||
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 (lua_State *L, struct Capture *cap) {
|
||||
int level = cap->level;
|
||||
static int capture_to_close (MatchState *ms) {
|
||||
int level = ms->level;
|
||||
for (level--; level>=0; level--)
|
||||
if (cap->capture[level].len == -1) return level;
|
||||
lua_error(L, "invalid pattern capture");
|
||||
return 0; /* to avoid warnings */
|
||||
if (ms->capture[level].len == CAP_UNFINISHED) return level;
|
||||
return luaL_error(ms->L, "invalid pattern capture");
|
||||
}
|
||||
|
||||
|
||||
const char *luaI_classend (lua_State *L, const char *p) {
|
||||
static const char *luaI_classend (MatchState *ms, const char *p) {
|
||||
switch (*p++) {
|
||||
case ESC:
|
||||
if (*p == '\0') lua_error(L, "malformed pattern (ends with `%')");
|
||||
case ESC: {
|
||||
if (*p == '\0')
|
||||
luaL_error(ms->L, "malformed pattern (ends with `%')");
|
||||
return p+1;
|
||||
case '[':
|
||||
}
|
||||
case '[': {
|
||||
if (*p == '^') p++;
|
||||
do { /* look for a ']' */
|
||||
if (*p == '\0') lua_error(L, "malformed pattern (missing `]')");
|
||||
if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */
|
||||
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:
|
||||
}
|
||||
default: {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,66 +220,59 @@ static int match_class (int c, int cl) {
|
||||
case 'u' : res = isupper(c); break;
|
||||
case 'w' : res = isalnum(c); break;
|
||||
case 'x' : res = isxdigit(c); break;
|
||||
case 'z' : res = (c == '\0'); 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 *endclass) {
|
||||
static int matchbracketclass (int c, const char *p, const char *ec) {
|
||||
int sig = 1;
|
||||
if (*(p+1) == '^') {
|
||||
sig = 0;
|
||||
p++; /* skip the '^' */
|
||||
p++; /* skip the `^' */
|
||||
}
|
||||
while (++p < endclass) {
|
||||
while (++p < ec) {
|
||||
if (*p == ESC) {
|
||||
p++;
|
||||
if (match_class(c, (unsigned char)*p))
|
||||
if (match_class(c, *p))
|
||||
return sig;
|
||||
}
|
||||
else if ((*(p+1) == '-') && (p+2 < endclass)) {
|
||||
else if ((*(p+1) == '-') && (p+2 < ec)) {
|
||||
p+=2;
|
||||
if ((int)(unsigned char)*(p-2) <= c && c <= (int)(unsigned char)*p)
|
||||
if (uchar(*(p-2)) <= c && c <= uchar(*p))
|
||||
return sig;
|
||||
}
|
||||
else if ((int)(unsigned char)*p == c) return sig;
|
||||
else if (uchar(*p) == c) return sig;
|
||||
}
|
||||
return !sig;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int luaI_singlematch (int c, const char *p, const char *ep) {
|
||||
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, (unsigned char)*(p+1));
|
||||
case '[':
|
||||
return matchbracketclass(c, p, ep-1);
|
||||
default:
|
||||
return ((unsigned char)*p == c);
|
||||
case '.': return 1; /* matches any char */
|
||||
case ESC: return match_class(c, *(p+1));
|
||||
case '[': return matchbracketclass(c, p, ep-1);
|
||||
default: return (uchar(*p) == c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *match (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap);
|
||||
static const char *match (MatchState *ms, const char *s, const char *p);
|
||||
|
||||
|
||||
static const char *matchbalance (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap) {
|
||||
static const char *matchbalance (MatchState *ms, const char *s,
|
||||
const char *p) {
|
||||
if (*p == 0 || *(p+1) == 0)
|
||||
lua_error(L, "unbalanced pattern");
|
||||
luaL_error(ms->L, "unbalanced pattern");
|
||||
if (*s != *p) return NULL;
|
||||
else {
|
||||
int b = *p;
|
||||
int e = *(p+1);
|
||||
int cont = 1;
|
||||
while (++s < cap->src_end) {
|
||||
while (++s < ms->src_end) {
|
||||
if (*s == e) {
|
||||
if (--cont == 0) return s+1;
|
||||
}
|
||||
@@ -252,14 +283,14 @@ static const char *matchbalance (lua_State *L, const char *s, const char *p,
|
||||
}
|
||||
|
||||
|
||||
static const char *max_expand (lua_State *L, const char *s, const char *p,
|
||||
const char *ep, struct Capture *cap) {
|
||||
long i = 0; /* counts maximum expand for item */
|
||||
while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
|
||||
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(L, (s+i), ep+1, cap);
|
||||
const char *res = match(ms, (s+i), ep+1);
|
||||
if (res) return res;
|
||||
i--; /* else didn't match; reduce 1 repetition to try again */
|
||||
}
|
||||
@@ -267,100 +298,126 @@ static const char *max_expand (lua_State *L, const char *s, const char *p,
|
||||
}
|
||||
|
||||
|
||||
static const char *min_expand (lua_State *L, const char *s, const char *p,
|
||||
const char *ep, struct Capture *cap) {
|
||||
static const char *min_expand (MatchState *ms, const char *s,
|
||||
const char *p, const char *ep) {
|
||||
for (;;) {
|
||||
const char *res = match(L, s, ep+1, cap);
|
||||
const char *res = match(ms, s, ep+1);
|
||||
if (res != NULL)
|
||||
return res;
|
||||
else if (s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep))
|
||||
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 (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap) {
|
||||
static const char *start_capture (MatchState *ms, const char *s,
|
||||
const char *p, int what) {
|
||||
const char *res;
|
||||
int level = cap->level;
|
||||
if (level >= MAX_CAPTURES) lua_error(L, "too many captures");
|
||||
cap->capture[level].init = s;
|
||||
cap->capture[level].len = -1;
|
||||
cap->level = level+1;
|
||||
if ((res=match(L, s, p+1, cap)) == NULL) /* match failed? */
|
||||
cap->level--; /* undo capture */
|
||||
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 (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap) {
|
||||
int l = capture_to_close(L, cap);
|
||||
static const char *end_capture (MatchState *ms, const char *s,
|
||||
const char *p) {
|
||||
int l = capture_to_close(ms);
|
||||
const char *res;
|
||||
cap->capture[l].len = s - cap->capture[l].init; /* close capture */
|
||||
if ((res = match(L, s, p+1, cap)) == NULL) /* match failed? */
|
||||
cap->capture[l].len = -1; /* undo capture */
|
||||
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 (lua_State *L, const char *s, int level,
|
||||
struct Capture *cap) {
|
||||
int l = check_capture(L, level, cap);
|
||||
size_t len = cap->capture[l].len;
|
||||
if ((size_t)(cap->src_end-s) >= len &&
|
||||
memcmp(cap->capture[l].init, s, len) == 0)
|
||||
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 (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap) {
|
||||
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 */
|
||||
return start_capture(L, s, p, cap);
|
||||
case ')': /* end capture */
|
||||
return end_capture(L, s, p, cap);
|
||||
case ESC: /* may be %[0-9] or %b */
|
||||
if (isdigit((unsigned char)(*(p+1)))) { /* capture? */
|
||||
s = match_capture(L, s, *(p+1), cap);
|
||||
if (s == NULL) return NULL;
|
||||
p+=2; goto init; /* else return match(L, s, p+2, cap) */
|
||||
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: {
|
||||
switch (*(p+1)) {
|
||||
case '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); */
|
||||
}
|
||||
case 'f': { /* frontier? */
|
||||
const char *ep; char previous;
|
||||
p += 2;
|
||||
if (*p != '[')
|
||||
luaL_error(ms->L, "missing `[' after `%%f' in pattern");
|
||||
ep = luaI_classend(ms, p); /* points to what is next */
|
||||
previous = (s == ms->src_init) ? '\0' : *(s-1);
|
||||
if (matchbracketclass(uchar(previous), p, ep-1) ||
|
||||
!matchbracketclass(uchar(*s), p, ep-1)) return NULL;
|
||||
p=ep; goto init; /* else return match(ms, s, ep); */
|
||||
}
|
||||
default: {
|
||||
if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
|
||||
s = match_capture(ms, s, *(p+1));
|
||||
if (s == NULL) return NULL;
|
||||
p+=2; goto init; /* else return match(ms, s, p+2) */
|
||||
}
|
||||
goto dflt; /* case default */
|
||||
}
|
||||
}
|
||||
else if (*(p+1) == 'b') { /* balanced string? */
|
||||
s = matchbalance(L, s, p+2, cap);
|
||||
if (s == NULL) return NULL;
|
||||
p+=4; goto init; /* else return match(L, s, p+4, cap); */
|
||||
}
|
||||
else goto dflt; /* case default */
|
||||
case '\0': /* end of pattern */
|
||||
}
|
||||
case '\0': { /* end of pattern */
|
||||
return s; /* match succeeded */
|
||||
case '$':
|
||||
if (*(p+1) == '\0') /* is the '$' the last char in pattern? */
|
||||
return (s == cap->src_end) ? s : NULL; /* check end of string */
|
||||
}
|
||||
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(L, p); /* points to what is next */
|
||||
int m = s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep);
|
||||
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(L, s+1, ep+1, cap)) != NULL))
|
||||
if (m && ((res=match(ms, s+1, ep+1)) != NULL))
|
||||
return res;
|
||||
p=ep+1; goto init; /* else return match(L, s, ep+1, cap); */
|
||||
p=ep+1; goto init; /* else return match(ms, s, ep+1); */
|
||||
}
|
||||
case '*': /* 0 or more repetitions */
|
||||
return max_expand(L, s, p, ep, cap);
|
||||
case '+': /* 1 or more repetitions */
|
||||
return (m ? max_expand(L, s+1, p, ep, cap) : NULL);
|
||||
case '-': /* 0 or more repetitions (minimum) */
|
||||
return min_expand(L, s, p, ep, cap);
|
||||
default:
|
||||
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(L, s+1, ep, cap); */
|
||||
s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -369,7 +426,7 @@ static const char *match (lua_State *L, const char *s, const char *p,
|
||||
|
||||
|
||||
static const char *lmemfind (const char *s1, size_t l1,
|
||||
const char *s2, size_t l2) {
|
||||
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 {
|
||||
@@ -390,27 +447,40 @@ static const char *lmemfind (const char *s1, size_t l1,
|
||||
}
|
||||
|
||||
|
||||
static int push_captures (lua_State *L, struct Capture *cap) {
|
||||
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_checkstack(L, cap->level, "too many captures");
|
||||
for (i=0; i<cap->level; i++) {
|
||||
int l = cap->capture[i].len;
|
||||
if (l == -1) lua_error(L, "unfinished capture");
|
||||
lua_pushlstring(L, cap->capture[i].init, l);
|
||||
luaL_checkstack(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 */
|
||||
}
|
||||
return cap->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);
|
||||
long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
|
||||
struct Capture cap;
|
||||
luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range");
|
||||
if (lua_gettop(L) > 3 || /* extra argument? */
|
||||
const char *s = luaL_checklstring(L, 1, &l1);
|
||||
const char *p = luaL_checklstring(L, 2, &l2);
|
||||
sint32 init = posrelat(luaL_optlong(L, 3, 1), l1) - 1;
|
||||
luaL_argcheck(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);
|
||||
@@ -419,25 +489,66 @@ static int str_find (lua_State *L) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
MatchState ms;
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
const char *s1=s+init;
|
||||
cap.src_end = s+l1;
|
||||
ms.L = L;
|
||||
ms.src_init = s;
|
||||
ms.src_end = s+l1;
|
||||
do {
|
||||
const char *res;
|
||||
cap.level = 0;
|
||||
if ((res=match(L, s1, p, &cap)) != NULL) {
|
||||
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(L, &cap) + 2;
|
||||
return push_captures(&ms, NULL, 0) + 2;
|
||||
}
|
||||
} while (s1++<cap.src_end && !anchor);
|
||||
} while (s1++<ms.src_end && !anchor);
|
||||
}
|
||||
lua_pushnil(L); /* not found */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||
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_checkstring(L, 1);
|
||||
luaL_checkstring(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);
|
||||
@@ -447,11 +558,12 @@ static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||
luaL_putchar(b, news[i]);
|
||||
else {
|
||||
i++; /* skip ESC */
|
||||
if (!isdigit((unsigned char)news[i]))
|
||||
if (!isdigit(uchar(news[i])))
|
||||
luaL_putchar(b, news[i]);
|
||||
else {
|
||||
int level = check_capture(L, news[i], cap);
|
||||
luaL_addlstring(b, cap->capture[level].init, cap->capture[level].len);
|
||||
int level = check_capture(ms, news[i]);
|
||||
push_onecapture(ms, level);
|
||||
luaL_addvalue(b); /* add capture to accumulated result */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -459,8 +571,8 @@ static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||
else { /* is a function */
|
||||
int n;
|
||||
lua_pushvalue(L, 3);
|
||||
n = push_captures(L, cap);
|
||||
lua_rawcall(L, n, 1);
|
||||
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
|
||||
@@ -471,34 +583,36 @@ static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||
|
||||
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);
|
||||
const char *src = luaL_checklstring(L, 1, &srcl);
|
||||
const char *p = luaL_checkstring(L, 2);
|
||||
int max_s = luaL_optint(L, 4, srcl+1);
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
int n = 0;
|
||||
struct Capture cap;
|
||||
MatchState ms;
|
||||
luaL_Buffer b;
|
||||
luaL_arg_check(L,
|
||||
luaL_argcheck(L,
|
||||
lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
|
||||
3, "string or function expected");
|
||||
luaL_buffinit(L, &b);
|
||||
cap.src_end = src+srcl;
|
||||
ms.L = L;
|
||||
ms.src_init = src;
|
||||
ms.src_end = src+srcl;
|
||||
while (n < max_s) {
|
||||
const char *e;
|
||||
cap.level = 0;
|
||||
e = match(L, src, p, &cap);
|
||||
ms.level = 0;
|
||||
e = match(&ms, src, p);
|
||||
if (e) {
|
||||
n++;
|
||||
add_s(L, &b, &cap);
|
||||
add_s(&ms, &b, src, e);
|
||||
}
|
||||
if (e && e>src) /* non empty match? */
|
||||
src = e; /* skip it */
|
||||
else if (src < cap.src_end)
|
||||
else if (src < ms.src_end)
|
||||
luaL_putchar(&b, *src++);
|
||||
else break;
|
||||
if (anchor) break;
|
||||
}
|
||||
luaL_addlstring(&b, src, cap.src_end-src);
|
||||
luaL_addlstring(&b, src, ms.src_end-src);
|
||||
luaL_pushresult(&b);
|
||||
lua_pushnumber(L, n); /* number of substitutions */
|
||||
return 2;
|
||||
@@ -507,88 +621,117 @@ static int str_gsub (lua_State *L) {
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/* 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);
|
||||
const char *s = luaL_checklstring(L, arg, &l);
|
||||
luaL_putchar(b, '"');
|
||||
while (l--) {
|
||||
switch (*s) {
|
||||
case '"': case '\\': case '\n':
|
||||
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);
|
||||
}
|
||||
case '\0': {
|
||||
luaL_addlstring(b, "\\000", 4);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
luaL_putchar(b, *s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
s++;
|
||||
}
|
||||
luaL_putchar(b, '"');
|
||||
}
|
||||
|
||||
/* 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 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;
|
||||
const char *strfrmt = luaL_check_string(L, arg);
|
||||
size_t sfl;
|
||||
const char *strfrmt = luaL_checklstring(L, arg, &sfl);
|
||||
const char *strfrmt_end = strfrmt+sfl;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
while (*strfrmt) {
|
||||
while (strfrmt < strfrmt_end) {
|
||||
if (*strfrmt != '%')
|
||||
luaL_putchar(&b, *strfrmt++);
|
||||
else if (*++strfrmt == '%')
|
||||
luaL_putchar(&b, *strfrmt++); /* %% */
|
||||
else { /* format item */
|
||||
struct Capture cap;
|
||||
char form[MAX_FORMAT]; /* to store the format ('%...') */
|
||||
char form[MAX_FORMAT]; /* to store the format (`%...') */
|
||||
char buff[MAX_ITEM]; /* to store the formatted item */
|
||||
const char *initf = strfrmt;
|
||||
form[0] = '%';
|
||||
if (isdigit((unsigned char)*initf) && *(initf+1) == '$') {
|
||||
arg = *initf - '0';
|
||||
initf += 2; /* skip the 'n$' */
|
||||
}
|
||||
int hasprecision = 0;
|
||||
if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == '$')
|
||||
return luaL_error(L, "obsolete `format' option (d$)");
|
||||
arg++;
|
||||
cap.src_end = strfrmt+strlen(strfrmt)+1;
|
||||
cap.level = 0;
|
||||
strfrmt = match(L, initf, "[-+ #0]*(%d*)%.?(%d*)", &cap);
|
||||
if (cap.capture[0].len > 2 || cap.capture[1].len > 2 || /* < 100? */
|
||||
strfrmt-initf > MAX_FORMAT-2)
|
||||
lua_error(L, "invalid format (width or precision too long)");
|
||||
strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
|
||||
form[strfrmt-initf+2] = 0;
|
||||
strfrmt = scanformat(L, strfrmt, form, &hasprecision);
|
||||
switch (*strfrmt++) {
|
||||
case 'c': case 'd': case 'i':
|
||||
sprintf(buff, form, luaL_check_int(L, arg));
|
||||
case 'c': case 'd': case 'i': {
|
||||
sprintf(buff, form, luaL_checkint(L, arg));
|
||||
break;
|
||||
case 'o': case 'u': case 'x': case 'X':
|
||||
sprintf(buff, form, (unsigned int)luaL_check_number(L, arg));
|
||||
}
|
||||
case 'o': case 'u': case 'x': case 'X': {
|
||||
sprintf(buff, form, (unsigned int)(luaL_checknumber(L, arg)));
|
||||
break;
|
||||
case 'e': case 'E': case 'f': case 'g': case 'G':
|
||||
sprintf(buff, form, luaL_check_number(L, arg));
|
||||
}
|
||||
case 'e': case 'E': case 'f':
|
||||
case 'g': case 'G': {
|
||||
sprintf(buff, form, luaL_checknumber(L, arg));
|
||||
break;
|
||||
case 'q':
|
||||
}
|
||||
case 'q': {
|
||||
luaI_addquoted(L, &b, arg);
|
||||
continue; /* skip the "addsize" at the end */
|
||||
continue; /* skip the `addsize' at the end */
|
||||
}
|
||||
case 's': {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, arg, &l);
|
||||
if (cap.capture[1].len == 0 && l >= 100) {
|
||||
const char *s = luaL_checklstring(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 */
|
||||
continue; /* skip the `addsize' at the end */
|
||||
}
|
||||
else {
|
||||
sprintf(buff, form, s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
default: /* also treat cases 'pnLlh' */
|
||||
lua_error(L, "invalid option in `format'");
|
||||
default: { /* also treat cases `pnLlh' */
|
||||
return luaL_error(L, "invalid option in `format'");
|
||||
}
|
||||
}
|
||||
luaL_addlstring(&b, buff, strlen(buff));
|
||||
}
|
||||
@@ -598,24 +741,28 @@ static int str_format (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_reg strlib[] = {
|
||||
{"strlen", str_len},
|
||||
{"strsub", str_sub},
|
||||
{"strlower", str_lower},
|
||||
{"strupper", str_upper},
|
||||
{"strchar", str_char},
|
||||
{"strrep", str_rep},
|
||||
{"ascii", str_byte}, /* for compatibility with 3.0 and earlier */
|
||||
{"strbyte", str_byte},
|
||||
{"format", str_format},
|
||||
{"strfind", str_find},
|
||||
{"gsub", str_gsub}
|
||||
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},
|
||||
{"dump", str_dump},
|
||||
{"find", str_find},
|
||||
{"gfind", gfind},
|
||||
{"gsub", str_gsub},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Open string library
|
||||
*/
|
||||
LUALIB_API void lua_strlibopen (lua_State *L) {
|
||||
luaL_openl(L, strlib);
|
||||
LUALIB_API int lua_strlibopen (lua_State *L) {
|
||||
luaL_openlib(L, LUA_STRLIBNAME, strlib, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
591
ltable.c
591
ltable.c
@@ -1,13 +1,17 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 1.57 2000/10/05 12:14:08 roberto Exp roberto $
|
||||
** $Id: ltable.c,v 1.125 2002/12/02 12:06:10 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** Implementation of tables (aka arrays, objects, or hash tables);
|
||||
** uses a mix of chained scatter table with Brent's variation.
|
||||
** 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.
|
||||
@@ -18,247 +22,347 @@
|
||||
*/
|
||||
|
||||
|
||||
#define ltable_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
|
||||
|
||||
#define gcsize(L, n) (sizeof(Hash)+(n)*sizeof(Node))
|
||||
/*
|
||||
** 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 TagDefault LUA_TTABLE
|
||||
#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 Hash *t, const TObject *key) {
|
||||
unsigned long h;
|
||||
Node *luaH_mainposition (const Table *t, const TObject *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TNUMBER:
|
||||
h = (unsigned long)(long)nvalue(key);
|
||||
break;
|
||||
case LUA_TNUMBER: {
|
||||
int ikey;
|
||||
lua_number2int(ikey, nvalue(key));
|
||||
return hashnum(t, ikey);
|
||||
}
|
||||
case LUA_TSTRING:
|
||||
h = tsvalue(key)->u.s.hash;
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
h = IntPoint(tsvalue(key));
|
||||
break;
|
||||
case LUA_TTABLE:
|
||||
h = IntPoint(hvalue(key));
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
h = IntPoint(clvalue(key));
|
||||
break;
|
||||
return hashstr(t, tsvalue(key));
|
||||
case LUA_TBOOLEAN:
|
||||
return hashboolean(t, bvalue(key));
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
return hashpointer(t, pvalue(key));
|
||||
default:
|
||||
return NULL; /* invalid key */
|
||||
return hashpointer(t, gcvalue(key));
|
||||
}
|
||||
LUA_ASSERT(h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
|
||||
"a&(x-1) == a%x, for x power of 2");
|
||||
return &t->node[h&(t->size-1)];
|
||||
}
|
||||
|
||||
|
||||
static const TObject *luaH_getany (lua_State *L, const Hash *t,
|
||||
const TObject *key) {
|
||||
Node *n = luaH_mainposition(t, key);
|
||||
if (!n)
|
||||
lua_error(L, "table index is nil");
|
||||
else do {
|
||||
if (luaO_equalObj(key, &n->key))
|
||||
return &n->val;
|
||||
n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject; /* key not found */
|
||||
}
|
||||
|
||||
|
||||
/* specialized version for numbers */
|
||||
const TObject *luaH_getnum (const Hash *t, Number key) {
|
||||
Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
|
||||
do {
|
||||
if (ttype(&n->key) == LUA_TNUMBER && nvalue(&n->key) == key)
|
||||
return &n->val;
|
||||
n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject; /* key not found */
|
||||
}
|
||||
|
||||
|
||||
/* specialized version for strings */
|
||||
const TObject *luaH_getstr (const Hash *t, TString *key) {
|
||||
Node *n = &t->node[key->u.s.hash&(t->size-1)];
|
||||
do {
|
||||
if (ttype(&n->key) == LUA_TSTRING && tsvalue(&n->key) == key)
|
||||
return &n->val;
|
||||
n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject; /* key not found */
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TNUMBER: return luaH_getnum(t, nvalue(key));
|
||||
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
|
||||
default: return luaH_getany(L, t, key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
|
||||
int i;
|
||||
if (ttype(key) == LUA_TNIL)
|
||||
i = 0; /* first iteration */
|
||||
else {
|
||||
const TObject *v = luaH_get(L, t, key);
|
||||
if (v == &luaO_nilobject)
|
||||
lua_error(L, "invalid key for `next'");
|
||||
i = (int)(((const char *)v -
|
||||
(const char *)(&t->node[0].val)) / sizeof(Node)) + 1;
|
||||
}
|
||||
for (; i<t->size; i++) {
|
||||
Node *n = node(t, i);
|
||||
if (ttype(val(n)) != LUA_TNIL)
|
||||
return n;
|
||||
}
|
||||
return NULL; /* no more elements */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** try to remove a key without value from a table. To avoid problems with
|
||||
** hash, change `key' for a number with the same hash.
|
||||
** returns the index for `key' if `key' is an appropriate key to live in
|
||||
** the array part of the table, -1 otherwise.
|
||||
*/
|
||||
void luaH_remove (Hash *t, TObject *key) {
|
||||
if (ttype(key) == LUA_TNUMBER ||
|
||||
(ttype(key) == LUA_TSTRING && tsvalue(key)->len <= 30))
|
||||
return; /* do not remove numbers nor small strings */
|
||||
else {
|
||||
/* try to find a number `n' with the same hash as `key' */
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
int n = mp - &t->node[0];
|
||||
/* make sure `n' is not in `t' */
|
||||
while (luaH_getnum(t, n) != &luaO_nilobject) {
|
||||
if (n >= MAX_INT - t->size)
|
||||
return; /* give up; (to avoid overflow) */
|
||||
n += t->size;
|
||||
}
|
||||
ttype(key) = LUA_TNUMBER;
|
||||
nvalue(key) = n;
|
||||
LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
|
||||
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 */
|
||||
}
|
||||
|
||||
|
||||
static void setnodevector (lua_State *L, Hash *t, lint32 size) {
|
||||
/*
|
||||
** 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, StkId key) {
|
||||
int i;
|
||||
if (size > MAX_INT)
|
||||
lua_error(L, "table overflow");
|
||||
t->node = luaM_newvector(L, size, Node);
|
||||
for (i=0; i<(int)size; i++) {
|
||||
ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL;
|
||||
t->node[i].next = NULL;
|
||||
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 */
|
||||
}
|
||||
L->nblocks += gcsize(L, size) - gcsize(L, t->size);
|
||||
t->size = size;
|
||||
t->firstfree = &t->node[size-1]; /* first free position to be used */
|
||||
}
|
||||
|
||||
|
||||
Hash *luaH_new (lua_State *L, int size) {
|
||||
Hash *t = luaM_new(L, Hash);
|
||||
t->htag = TagDefault;
|
||||
t->next = L->roottable;
|
||||
L->roottable = t;
|
||||
t->mark = t;
|
||||
t->size = 0;
|
||||
L->nblocks += gcsize(L, 0);
|
||||
int luaH_next (lua_State *L, Table *t, StkId 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);
|
||||
setobj2s(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? */
|
||||
setobj2s(key, key(node(t, i)));
|
||||
setobj2s(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]))
|
||||
setobjt2t(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)))
|
||||
setobjt2t(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);
|
||||
luaC_link(L, valtogco(t), LUA_TTABLE);
|
||||
t->metatable = hvalue(defaultmeta(L));
|
||||
t->flags = cast(lu_byte, ~0);
|
||||
/* temporary values (kept only if some malloc fails) */
|
||||
t->array = NULL;
|
||||
t->sizearray = 0;
|
||||
t->lsizenode = 0;
|
||||
t->node = NULL;
|
||||
setnodevector(L, t, luaO_power2(size));
|
||||
setarrayvector(L, t, narray);
|
||||
setnodevector(L, t, lnhash);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
void luaH_free (lua_State *L, Hash *t) {
|
||||
L->nblocks -= gcsize(L, t->size);
|
||||
luaM_free(L, t->node);
|
||||
luaM_free(L, 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);
|
||||
}
|
||||
|
||||
|
||||
static int numuse (const Hash *t) {
|
||||
Node *v = t->node;
|
||||
int size = t->size;
|
||||
int realuse = 0;
|
||||
int i;
|
||||
for (i=0; i<size; i++) {
|
||||
if (ttype(&v[i].val) != LUA_TNIL)
|
||||
realuse++;
|
||||
#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 */
|
||||
}
|
||||
return realuse;
|
||||
}
|
||||
|
||||
|
||||
static void rehash (lua_State *L, Hash *t) {
|
||||
int oldsize = t->size;
|
||||
Node *nold = t->node;
|
||||
int nelems = numuse(t);
|
||||
int i;
|
||||
LUA_ASSERT(nelems<=oldsize, "wrong count");
|
||||
if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
|
||||
setnodevector(L, t, (lint32)oldsize*2);
|
||||
else if (nelems <= oldsize/4 && /* less than 1/4? */
|
||||
oldsize > MINPOWER2)
|
||||
setnodevector(L, t, oldsize/2);
|
||||
else
|
||||
setnodevector(L, t, oldsize);
|
||||
for (i=0; i<oldsize; i++) {
|
||||
Node *old = nold+i;
|
||||
if (ttype(&old->val) != LUA_TNIL)
|
||||
*luaH_set(L, t, &old->key) = old->val;
|
||||
else {
|
||||
if (e->next != NULL) ??
|
||||
}
|
||||
luaM_free(L, nold); /* free old array */
|
||||
lua_assert(ttisnil(val(node)));
|
||||
setnilvalue(key(e)); /* clear node `e' */
|
||||
e->next = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** inserts a key into a hash table; first, check whether key is
|
||||
** already present; if not, 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.
|
||||
** 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.
|
||||
*/
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
||||
static TObject *newkey (lua_State *L, Table *t, const TObject *key) {
|
||||
TObject *val;
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
Node *n = mp;
|
||||
if (!mp)
|
||||
lua_error(L, "table index is nil");
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_equalObj(key, &n->key))
|
||||
return &n->val; /* that's all */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
/* `key' not found; must insert it */
|
||||
if (ttype(&mp->key) != LUA_TNIL) { /* main position is not free? */
|
||||
Node *othern; /* main position of colliding node */
|
||||
n = t->firstfree; /* get a free place */
|
||||
/* is colliding node out of its main position? (can only happens if
|
||||
its position is after "firstfree") */
|
||||
if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
|
||||
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 */
|
||||
@@ -267,37 +371,112 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
||||
mp = n;
|
||||
}
|
||||
}
|
||||
mp->key = *key;
|
||||
setobj2t(key(mp), key); /* write barrier */
|
||||
lua_assert(ttisnil(val(mp)));
|
||||
for (;;) { /* correct `firstfree' */
|
||||
if (ttype(&t->firstfree->key) == LUA_TNIL)
|
||||
return &mp->val; /* OK; table still has a free place */
|
||||
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)--;
|
||||
}
|
||||
rehash(L, t); /* no more free places */
|
||||
return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_setint (lua_State *L, Hash *t, int key) {
|
||||
TObject index;
|
||||
ttype(&index) = LUA_TNUMBER;
|
||||
nvalue(&index) = key;
|
||||
return luaH_set(L, t, &index);
|
||||
/*
|
||||
** 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) {
|
||||
TObject *value, index;
|
||||
ttype(&index) = LUA_TSTRING;
|
||||
tsvalue(&index) = key;
|
||||
value = luaH_set(L, t, &index);
|
||||
ttype(value) = LUA_TNUMBER;
|
||||
nvalue(value) = val;
|
||||
/*
|
||||
** 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaH_getglobal (lua_State *L, const char *name) {
|
||||
return luaH_getstr(L->gt, luaS_new(L, name));
|
||||
/*
|
||||
** 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
29
ltable.h
29
ltable.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 1.23 2000/06/06 16:31:41 roberto Exp roberto $
|
||||
** $Id: ltable.h,v 1.42 2002/05/27 20:35:40 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,24 +11,21 @@
|
||||
|
||||
|
||||
#define node(t,i) (&(t)->node[i])
|
||||
#define key(n) (&(n)->key)
|
||||
#define val(n) (&(n)->val)
|
||||
#define key(n) (&(n)->i_key)
|
||||
#define val(n) (&(n)->i_val)
|
||||
|
||||
Hash *luaH_new (lua_State *L, int nhash);
|
||||
void luaH_free (lua_State *L, Hash *t);
|
||||
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key);
|
||||
const TObject *luaH_getnum (const Hash *t, Number key);
|
||||
const TObject *luaH_getstr (const Hash *t, TString *key);
|
||||
void luaH_remove (Hash *t, TObject *key);
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key);
|
||||
Node * luaH_next (lua_State *L, const Hash *t, const TObject *r);
|
||||
TObject *luaH_setint (lua_State *L, Hash *t, int key);
|
||||
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val);
|
||||
unsigned long luaH_hash (lua_State *L, const TObject *key);
|
||||
const TObject *luaH_getglobal (lua_State *L, const char *name);
|
||||
|
||||
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, StkId key);
|
||||
|
||||
/* exported only for debugging */
|
||||
Node *luaH_mainposition (const Hash *t, const TObject *key);
|
||||
Node *luaH_mainposition (const Table *t, const TObject *key);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
301
ltablib.c
Normal file
301
ltablib.c
Normal file
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
** $Id: ltablib.c,v 1.16 2002/11/14 15:41:38 roberto Exp roberto $
|
||||
** Library for Table Manipulation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define ltablib_c
|
||||
|
||||
#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_checktype(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_checktype(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_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checktype(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_checktype(L, 1, LUA_TTABLE);
|
||||
aux_setn(L, 1, luaL_checkint(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_checkint(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_optint(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;
|
||||
size_t lsep;
|
||||
const char *sep = luaL_optlstring(L, 2, "", &lsep);
|
||||
int i = luaL_optint(L, 3, 1);
|
||||
int n = luaL_optint(L, 4, 0);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
if (n == 0) n = aux_getn(L, 1);
|
||||
luaL_buffinit(L, &b);
|
||||
for (; i <= n; i++) {
|
||||
lua_rawgeti(L, 1, i);
|
||||
luaL_argcheck(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_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
|
||||
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
lua_settop(L, 2); /* make sure there is two arguments */
|
||||
auxsort(L, 1, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static const luaL_reg 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_pushvalue(L, -1); /* `N' will be its own metatable */
|
||||
lua_setmetatable(L, -2);
|
||||
lua_pushliteral(L, "__mode");
|
||||
lua_pushliteral(L, "k");
|
||||
lua_rawset(L, -3); /* metatable(N).__mode = "k" */
|
||||
luaL_openlib(L, LUA_TABLIBNAME, tab_funcs, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
598
ltests.c
598
ltests.c
@@ -1,15 +1,17 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 1.53 2000/10/30 16:29:59 roberto Exp roberto $
|
||||
** $Id: ltests.c,v 1.147 2002/12/04 17:29:05 roberto Exp roberto $
|
||||
** Internal Module for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ltests_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
@@ -24,12 +26,9 @@
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
void luaB_opentests (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** The whole module only makes sense with LUA_DEBUG on
|
||||
@@ -37,6 +36,13 @@ void luaB_opentests (lua_State *L);
|
||||
#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);
|
||||
@@ -45,6 +51,99 @@ static void setnameval (lua_State *L, const char *name, int val) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================================
|
||||
** Controlled version for realloc.
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
||||
|
||||
#ifndef EXTERNMEMCHECK
|
||||
/* full memory check */
|
||||
#define HEADER (sizeof(L_Umaxalign)) /* ensures maximum alignment for HEADER */
|
||||
#define MARKSIZE 16 /* size of marks after each block */
|
||||
#define blockhead(b) (cast(char *, b) - HEADER)
|
||||
#define setsize(newblock, size) (*cast(size_t *, newblock) = size)
|
||||
#define checkblocksize(b, size) (size == (*cast(size_t *, blockhead(b))))
|
||||
#define fillmem(mem,size) memset(mem, -MARK, size)
|
||||
#else
|
||||
/* external memory check: don't do it twice */
|
||||
#define HEADER 0
|
||||
#define MARKSIZE 0
|
||||
#define blockhead(b) (b)
|
||||
#define setsize(newblock, size) /* empty */
|
||||
#define checkblocksize(b,size) (1)
|
||||
#define fillmem(mem,size) /* empty */
|
||||
#endif
|
||||
|
||||
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 size) {
|
||||
void *b = blockhead(block);
|
||||
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, size_t size) {
|
||||
if (block) {
|
||||
lua_assert(checkblocksize(block, size));
|
||||
block = checkblock(block, size);
|
||||
fillmem(block, 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 || checkblocksize(block, oldsize));
|
||||
/* ISO does not specify what realloc(NULL, 0) does */
|
||||
lua_assert(block != NULL || size > 0);
|
||||
if (size == 0) {
|
||||
freeblock(block, oldsize);
|
||||
return NULL;
|
||||
}
|
||||
else if (size > oldsize && memdebug_total+size-oldsize > memdebug_memlimit)
|
||||
return NULL; /* to test memory allocation errors */
|
||||
else {
|
||||
void *newblock;
|
||||
int i;
|
||||
size_t realsize = HEADER+size+MARKSIZE;
|
||||
size_t commonsize = (oldsize < size) ? oldsize : size;
|
||||
if (realsize < size) return NULL; /* overflow! */
|
||||
newblock = malloc(realsize); /* alloc a new block */
|
||||
if (newblock == NULL) return NULL;
|
||||
if (block) {
|
||||
memcpy(cast(char *, newblock)+HEADER, block, commonsize);
|
||||
freeblock(block, oldsize); /* erase (and check) old copy */
|
||||
}
|
||||
/* initialize new part of the block with something `weird' */
|
||||
fillmem(cast(char *, newblock)+HEADER+commonsize, size-commonsize);
|
||||
memdebug_total += size;
|
||||
if (memdebug_total > memdebug_maxmem)
|
||||
memdebug_maxmem = memdebug_total;
|
||||
memdebug_numblocks++;
|
||||
setsize(newblock, size);
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
*(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
|
||||
return cast(char *, newblock)+HEADER;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Disassembler
|
||||
@@ -52,73 +151,69 @@ static void setnameval (lua_State *L, const char *name, int val) {
|
||||
*/
|
||||
|
||||
|
||||
static const char *const instrname[NUM_OPCODES] = {
|
||||
"END", "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
|
||||
"PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
|
||||
"GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
|
||||
"CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
|
||||
"ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
|
||||
"JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
|
||||
"JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
|
||||
"LFORLOOP", "CLOSURE"
|
||||
};
|
||||
|
||||
|
||||
static int pushop (lua_State *L, Proto *p, int pc) {
|
||||
char buff[100];
|
||||
static char *buildop (Proto *p, int pc, char *buff) {
|
||||
Instruction i = p->code[pc];
|
||||
OpCode o = GET_OPCODE(i);
|
||||
const char *name = instrname[o];
|
||||
sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
|
||||
switch ((enum Mode)luaK_opproperties[o].mode) {
|
||||
case iO:
|
||||
sprintf(buff+8, "%-12s", name);
|
||||
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 iU:
|
||||
sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
|
||||
case iABx:
|
||||
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
|
||||
break;
|
||||
case iS:
|
||||
sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
|
||||
break;
|
||||
case iAB:
|
||||
sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
|
||||
case iAsBx:
|
||||
sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
|
||||
break;
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
return (o != OP_END);
|
||||
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;
|
||||
int res;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, "Lua function expected");
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
p = clvalue(index(L, 1))->l.p;
|
||||
lua_newtable(L);
|
||||
setnameval(L, "maxstack", p->maxstacksize);
|
||||
setnameval(L, "numparams", p->numparams);
|
||||
pc = 0;
|
||||
do {
|
||||
for (pc=0; pc<p->sizecode; pc++) {
|
||||
char buff[100];
|
||||
lua_pushnumber(L, pc+1);
|
||||
res = pushop(L, p, pc++);
|
||||
lua_pushstring(L, buildop(p, pc, buff));
|
||||
lua_settable(L, -3);
|
||||
} while (res);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int liststrings (lua_State *L) {
|
||||
static int listk (lua_State *L) {
|
||||
Proto *p;
|
||||
int i;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, "Lua function expected");
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
p = clvalue(index(L, 1))->l.p;
|
||||
lua_newtable(L);
|
||||
for (i=0; i<p->nkstr; i++) {
|
||||
for (i=0; i<p->sizek; i++) {
|
||||
lua_pushnumber(L, i+1);
|
||||
lua_pushstring(L, p->kstr[i]->str);
|
||||
luaA_pushobject(L, p->k+i);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
return 1;
|
||||
@@ -127,12 +222,12 @@ static int liststrings (lua_State *L) {
|
||||
|
||||
static int listlocals (lua_State *L) {
|
||||
Proto *p;
|
||||
int pc = luaL_check_int(L, 2) - 1;
|
||||
int pc = luaL_checkint(L, 2) - 1;
|
||||
int i = 0;
|
||||
const char *name;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, "Lua function expected");
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
p = clvalue(index(L, 1))->l.p;
|
||||
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
||||
lua_pushstring(L, name);
|
||||
return i-1;
|
||||
@@ -142,96 +237,107 @@ static int listlocals (lua_State *L) {
|
||||
|
||||
|
||||
|
||||
|
||||
static int get_limits (lua_State *L) {
|
||||
lua_newtable(L);
|
||||
setnameval(L, "BITS_INT", BITS_INT);
|
||||
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
||||
setnameval(L, "MAXARG_A", MAXARG_A);
|
||||
setnameval(L, "MAXARG_B", MAXARG_B);
|
||||
setnameval(L, "MAXARG_S", MAXARG_S);
|
||||
setnameval(L, "MAXARG_U", MAXARG_U);
|
||||
setnameval(L, "MAXLOCALS", MAXLOCALS);
|
||||
setnameval(L, "MAXVARS", MAXVARS);
|
||||
setnameval(L, "MAXPARAMS", MAXPARAMS);
|
||||
setnameval(L, "MAXSTACK", MAXSTACK);
|
||||
setnameval(L, "MAXUPVALUES", MAXUPVALUES);
|
||||
setnameval(L, "MAXVARSLH", MAXVARSLH);
|
||||
setnameval(L, "RFPF", RFIELDS_PER_FLUSH);
|
||||
setnameval(L, "SIZE_A", SIZE_A);
|
||||
setnameval(L, "SIZE_B", SIZE_B);
|
||||
setnameval(L, "SIZE_OP", SIZE_OP);
|
||||
setnameval(L, "SIZE_U", SIZE_U);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int mem_query (lua_State *L) {
|
||||
if (lua_isnull(L, 1)) {
|
||||
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);
|
||||
memdebug_memlimit = luaL_checkint(L, 1);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int hash_query (lua_State *L) {
|
||||
if (lua_isnull(L, 2)) {
|
||||
luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, "string expected");
|
||||
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
|
||||
if (lua_isnone(L, 2)) {
|
||||
luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
|
||||
lua_pushnumber(L, tsvalue(index(L, 1))->tsv.hash);
|
||||
}
|
||||
else {
|
||||
Hash *t;
|
||||
TObject *o = index(L, 1);
|
||||
Table *t;
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
t = hvalue(luaA_index(L, 2));
|
||||
lua_pushnumber(L, luaH_mainposition(t, luaA_index(L, 1)) - t->node);
|
||||
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 Hash *t;
|
||||
int i = luaL_opt_int(L, 2, -1);
|
||||
const Table *t;
|
||||
int i = luaL_optint(L, 2, -1);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
t = hvalue(luaA_index(L, 1));
|
||||
t = hvalue(index(L, 1));
|
||||
if (i == -1) {
|
||||
lua_pushnumber(L, t->size);
|
||||
lua_pushnumber(L, t->sizearray);
|
||||
lua_pushnumber(L, sizenode(t));
|
||||
lua_pushnumber(L, t->firstfree - t->node);
|
||||
return 2;
|
||||
}
|
||||
else if (i < t->size) {
|
||||
luaA_pushobject(L, &t->node[i].key);
|
||||
luaA_pushobject(L, &t->node[i].val);
|
||||
if (t->node[i].next) {
|
||||
lua_pushnumber(L, t->node[i].next - t->node);
|
||||
return 3;
|
||||
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
|
||||
return 2;
|
||||
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 0;
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
static int string_query (lua_State *L) {
|
||||
stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &L->strt : &L->udt;
|
||||
int s = luaL_opt_int(L, 2, 0) - 1;
|
||||
stringtable *tb = &G(L)->strt;
|
||||
int s = luaL_optint(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;
|
||||
GCObject *ts;
|
||||
int n = 0;
|
||||
for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
|
||||
ttype(L->top) = LUA_TSTRING;
|
||||
tsvalue(L->top) = ts;
|
||||
incr_top;
|
||||
for (ts = tb->hash[s]; ts; ts = ts->gch.next) {
|
||||
setsvalue2s(L->top, gcotots(ts));
|
||||
incr_top(L);
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
@@ -241,73 +347,124 @@ static int string_query (lua_State *L) {
|
||||
|
||||
|
||||
static int tref (lua_State *L) {
|
||||
int level = lua_gettop(L);
|
||||
int lock = luaL_optint(L, 2, 1);
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 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) {
|
||||
if (lua_getref(L, luaL_check_int(L, 1)))
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
int level = lua_gettop(L);
|
||||
lua_getref(L, luaL_checkint(L, 1));
|
||||
assert(lua_gettop(L) == level+1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int unref (lua_State *L) {
|
||||
lua_unref(L, luaL_check_int(L, 1));
|
||||
int level = lua_gettop(L);
|
||||
lua_unref(L, luaL_checkint(L, 1));
|
||||
assert(lua_gettop(L) == level);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int newuserdata (lua_State *L) {
|
||||
if (lua_isnumber(L, 2))
|
||||
lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
|
||||
else
|
||||
lua_newuserdata(L, luaL_check_int(L, 1));
|
||||
static int metatable (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
if (lua_isnone(L, 2)) {
|
||||
if (lua_getmetatable(L, 1) == 0)
|
||||
lua_pushnil(L);
|
||||
}
|
||||
else {
|
||||
lua_settop(L, 2);
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
lua_setmetatable(L, 1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int newuserdata (lua_State *L) {
|
||||
size_t size = luaL_checkint(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_checkint(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int udataval (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
lua_pushnumber(L, (int)lua_touserdata(L, 1));
|
||||
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_checklstring(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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int s2d (lua_State *L) {
|
||||
lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int d2s (lua_State *L) {
|
||||
double d = luaL_checknumber(L, 1);
|
||||
lua_pushlstring(L, cast(char *, &d), sizeof(d));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int newstate (lua_State *L) {
|
||||
lua_State *L1 = lua_open(luaL_check_int(L, 1));
|
||||
if (L1)
|
||||
lua_pushuserdata(L, L1);
|
||||
lua_State *L1 = lua_open();
|
||||
if (L1) {
|
||||
lua_userstateopen(L1); /* init lock */
|
||||
lua_pushnumber(L, (unsigned long)L1);
|
||||
}
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int loadlib (lua_State *L) {
|
||||
lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
|
||||
switch (*luaL_check_string(L, 2)) {
|
||||
case 'm': lua_mathlibopen(L1); break;
|
||||
case 's': lua_strlibopen(L1); break;
|
||||
case 'i': lua_iolibopen(L1); break;
|
||||
case 'd': lua_dblibopen(L1); break;
|
||||
case 'b': lua_baselibopen(L1); break;
|
||||
default: luaL_argerror(L, 2, "invalid option");
|
||||
}
|
||||
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(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) {
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
lua_close((lua_State *)lua_touserdata(L, 1));
|
||||
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
|
||||
lua_close(L1);
|
||||
lua_unlock(L); /* close cannot unlock that */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int doremote (lua_State *L) {
|
||||
lua_State *L1;
|
||||
const char *code = luaL_check_string(L, 2);
|
||||
lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
|
||||
size_t lcode;
|
||||
const char *code = luaL_checklstring(L, 2, &lcode);
|
||||
int status;
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
L1 = (lua_State *)lua_touserdata(L, 1);
|
||||
status = lua_dostring(L1, code);
|
||||
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);
|
||||
@@ -315,37 +472,34 @@ static int doremote (lua_State *L) {
|
||||
}
|
||||
else {
|
||||
int i = 0;
|
||||
while (!lua_isnull(L1, ++i))
|
||||
while (!lua_isnone(L1, ++i))
|
||||
lua_pushstring(L, lua_tostring(L1, i));
|
||||
lua_pop(L1, i-1);
|
||||
return i-1;
|
||||
}
|
||||
}
|
||||
|
||||
static int settagmethod (lua_State *L) {
|
||||
int tag = luaL_check_int(L, 1);
|
||||
const char *event = luaL_check_string(L, 2);
|
||||
luaL_checkany(L, 3);
|
||||
lua_gettagmethod(L, tag, event);
|
||||
lua_pushvalue(L, 3);
|
||||
lua_settagmethod(L, tag, event);
|
||||
|
||||
static int log2_aux (lua_State *L) {
|
||||
lua_pushnumber(L, luaO_log2(luaL_checkint(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int pushbool (lua_State *L, int b) {
|
||||
if (b) lua_pushnumber(L, 1);
|
||||
else lua_pushnil(L);
|
||||
return 1;
|
||||
|
||||
static int test_do (lua_State *L) {
|
||||
const char *p = luaL_checkstring(L, 1);
|
||||
if (*p == '@')
|
||||
lua_dofile(L, p+1);
|
||||
else
|
||||
lua_dostring(L, p);
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
static int equal (lua_State *L) {
|
||||
return pushbool(L, lua_equal(L, 1, 2));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** function to test the API with C. It interprets a kind of "assembler"
|
||||
** 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
|
||||
** =======================================================
|
||||
*/
|
||||
@@ -356,12 +510,12 @@ static void skip (const char **pc) {
|
||||
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
||||
}
|
||||
|
||||
static int getnum (lua_State *L, const char **pc) {
|
||||
static int getnum_aux (lua_State *L, const char **pc) {
|
||||
int res = 0;
|
||||
int sig = 1;
|
||||
skip(pc);
|
||||
if (**pc == '.') {
|
||||
res = (int)lua_tonumber(L, -1);
|
||||
res = cast(int, lua_tonumber(L, -1));
|
||||
lua_pop(L, 1);
|
||||
(*pc)++;
|
||||
return res;
|
||||
@@ -370,11 +524,11 @@ static int getnum (lua_State *L, const char **pc) {
|
||||
sig = -1;
|
||||
(*pc)++;
|
||||
}
|
||||
while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
|
||||
while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
|
||||
return sig*res;
|
||||
}
|
||||
|
||||
static const char *getname (char *buff, const char **pc) {
|
||||
static const char *getname_aux (char *buff, const char **pc) {
|
||||
int i = 0;
|
||||
skip(pc);
|
||||
while (**pc != '\0' && !strchr(delimits, **pc))
|
||||
@@ -386,13 +540,13 @@ static const char *getname (char *buff, const char **pc) {
|
||||
|
||||
#define EQ(s1) (strcmp(s1, inst) == 0)
|
||||
|
||||
#define getnum ((getnum)(L, &pc))
|
||||
#define getname ((getname)(buff, &pc))
|
||||
#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);
|
||||
const char *pc = luaL_checkstring(L, 1);
|
||||
for (;;) {
|
||||
const char *inst = getname;
|
||||
if EQ("") return 0;
|
||||
@@ -414,17 +568,21 @@ static int testC (lua_State *L) {
|
||||
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_isnull(L, getnum));
|
||||
lua_pushnumber(L, lua_isnone(L, getnum));
|
||||
}
|
||||
else if EQ("tonumber") {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
}
|
||||
else if EQ("tostring") {
|
||||
lua_pushstring(L, lua_tostring(L, getnum));
|
||||
const char *s = lua_tostring(L, getnum);
|
||||
lua_pushstring(L, s);
|
||||
}
|
||||
else if EQ("tonumber") {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
@@ -450,15 +608,33 @@ static int testC (lua_State *L) {
|
||||
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);
|
||||
}
|
||||
@@ -471,33 +647,43 @@ static int testC (lua_State *L) {
|
||||
else if EQ("concat") {
|
||||
lua_concat(L, getnum);
|
||||
}
|
||||
else if EQ("rawcall") {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_rawcall(L, narg, nres);
|
||||
else if EQ("lessthan") {
|
||||
int a = getnum;
|
||||
lua_pushboolean(L, lua_lessthan(L, a, getnum));
|
||||
}
|
||||
else if EQ("call") {
|
||||
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("dostring") {
|
||||
lua_dostring(L, luaL_check_string(L, getnum));
|
||||
else if EQ("call") {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_pcall(L, narg, nres, 0);
|
||||
}
|
||||
else if EQ("settagmethod") {
|
||||
int tag = getnum;
|
||||
const char *event = getname;
|
||||
lua_settagmethod(L, tag, event);
|
||||
else if EQ("loadstring") {
|
||||
size_t sl;
|
||||
const char *s = luaL_checklstring(L, getnum, &sl);
|
||||
luaL_loadbuffer(L, s, sl, s);
|
||||
}
|
||||
else if EQ("gettagmethod") {
|
||||
int tag = getnum;
|
||||
const char *event = getname;
|
||||
lua_gettagmethod(L, tag, event);
|
||||
else if EQ("loadfile") {
|
||||
luaL_loadfile(L, luaL_checkstring(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_verror(L, "unknown instruction %.30s", buff);
|
||||
else luaL_error(L, "unknown instruction %s", buff);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -505,39 +691,107 @@ static int testC (lua_State *L) {
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** tests for yield inside hooks
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void yieldf (lua_State *L, lua_Debug *ar) {
|
||||
lua_yield(L, 0);
|
||||
}
|
||||
|
||||
static int setyhook (lua_State *L) {
|
||||
if (lua_isnoneornil(L, 1))
|
||||
lua_sethook(L, NULL, 0, 0); /* turn off hooks */
|
||||
else {
|
||||
const char *smask = luaL_checkstring(L, 1);
|
||||
int count = luaL_optint(L, 2, 0);
|
||||
int mask = 0;
|
||||
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
|
||||
if (count > 0) mask |= LUA_MASKCOUNT;
|
||||
lua_sethook(L, yieldf, mask, count);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int coresume (lua_State *L) {
|
||||
int status;
|
||||
lua_State *co = lua_tothread(L, 1);
|
||||
luaL_argcheck(L, co, 1, "coroutine expected");
|
||||
status = lua_resume(co, 0);
|
||||
if (status != 0) {
|
||||
lua_pushboolean(L, 0);
|
||||
lua_insert(L, -2);
|
||||
return 2; /* return false + error message */
|
||||
}
|
||||
else {
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
static const struct luaL_reg tests_funcs[] = {
|
||||
{"hash", hash_query},
|
||||
{"limits", get_limits},
|
||||
{"listcode", listcode},
|
||||
{"liststrings", liststrings},
|
||||
{"listk", listk},
|
||||
{"listlocals", listlocals},
|
||||
{"loadlib", loadlib},
|
||||
{"stacklevel", stacklevel},
|
||||
{"querystr", string_query},
|
||||
{"querytab", table_query},
|
||||
{"doit", test_do},
|
||||
{"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},
|
||||
{"settagmethod", settagmethod},
|
||||
{"equal", equal},
|
||||
{"totalmem", mem_query}
|
||||
{"log2", log2_aux},
|
||||
{"totalmem", mem_query},
|
||||
{"resume", coresume},
|
||||
{"setyhook", setyhook},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
void luaB_opentests (lua_State *L) {
|
||||
lua_newtable(L);
|
||||
lua_getglobals(L);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_setglobals(L);
|
||||
luaL_openl(L, tests_funcs); /* open functions inside new table */
|
||||
lua_setglobals(L); /* restore old table of globals */
|
||||
lua_setglobal(L, "T"); /* set new table as global T */
|
||||
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) {
|
||||
lua_userstateopen(L); /* init lock */
|
||||
lua_state = L; /* keep first state to be opened */
|
||||
luaL_openlib(L, "T", tests_funcs, 0);
|
||||
atexit(fim);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#undef main
|
||||
int main (int argc, char *argv[]) {
|
||||
char *limit = getenv("MEMLIMIT");
|
||||
if (limit)
|
||||
memdebug_memlimit = strtoul(limit, NULL, 10);
|
||||
l_main(argc, argv);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
68
ltests.h
Normal file
68
ltests.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
** $Id: ltests.h,v 1.19 2002/11/19 17:42:32 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)
|
||||
#define l_free(b, os) debug_realloc(b, os, 0)
|
||||
|
||||
void *debug_realloc (void *block, size_t oldsize, size_t size);
|
||||
|
||||
|
||||
|
||||
/* test for lock/unlock */
|
||||
extern int islocked;
|
||||
#define LUA_USERSTATE int *
|
||||
#define getlock(l) (*(cast(LUA_USERSTATE *, l) - 1))
|
||||
#define lua_userstateopen(l) if (l != NULL) getlock(l) = &islocked;
|
||||
#define lua_lock(l) lua_assert((*getlock(l))++ == 0)
|
||||
#define lua_unlock(l) lua_assert(--(*getlock(l)) == 0)
|
||||
|
||||
|
||||
int luaB_opentests (lua_State *L);
|
||||
|
||||
#define LUA_EXTRALIBS { "tests", luaB_opentests },
|
||||
|
||||
|
||||
/* real main will be defined at `ltests.c' */
|
||||
int l_main (int argc, char *argv[]);
|
||||
#define main l_main
|
||||
|
||||
|
||||
|
||||
/* change some sizes to give some bugs a chance */
|
||||
|
||||
#define LUAL_BUFFERSIZE 27
|
||||
#define MINSTRTABSIZE 2
|
||||
|
||||
#endif
|
||||
171
ltm.c
171
ltm.c
@@ -1,163 +1,70 @@
|
||||
/*
|
||||
** $Id: ltm.c,v 1.55 2000/10/20 16:39:03 roberto Exp roberto $
|
||||
** $Id: ltm.c,v 1.104 2002/11/14 11:51:50 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ltm_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
const char *const luaT_eventname[] = { /* ORDER TM */
|
||||
"gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
|
||||
"mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
|
||||
"le", "gt", "ge", /* deprecated options!! */
|
||||
NULL
|
||||
|
||||
const char *const luaT_typenames[] = {
|
||||
"nil", "boolean", "userdata", "number",
|
||||
"string", "table", "function", "userdata", "thread"
|
||||
};
|
||||
|
||||
|
||||
static int findevent (const char *name) {
|
||||
int i;
|
||||
for (i=0; luaT_eventname[i]; i++)
|
||||
if (strcmp(luaT_eventname[i], name) == 0)
|
||||
return i;
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
|
||||
static int luaI_checkevent (lua_State *L, const char *name, int t) {
|
||||
int e = findevent(name);
|
||||
if (e >= TM_N)
|
||||
luaO_verror(L, "event `%.50s' is deprecated", name);
|
||||
if (e == TM_GC && t == LUA_TTABLE)
|
||||
luaO_verror(L, "event `gc' for tables is deprecated");
|
||||
if (e < 0)
|
||||
luaO_verror(L, "`%.50s' is not a valid event name", name);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* events in LUA_TNIL are all allowed, since this is used as a
|
||||
* 'placeholder' for "default" fallbacks
|
||||
*/
|
||||
/* ORDER LUA_T, ORDER TM */
|
||||
static const char luaT_validevents[NUM_TAGS][TM_N] = {
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_TNIL */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_TSTRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TTABLE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0} /* LUA_TFUNCTION */
|
||||
};
|
||||
|
||||
int luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
||||
return (t >= NUM_TAGS) ? 1 : luaT_validevents[t][e];
|
||||
}
|
||||
|
||||
|
||||
static void init_entry (lua_State *L, int tag) {
|
||||
int i;
|
||||
for (i=0; i<TM_N; i++)
|
||||
luaT_gettm(L, tag, i) = NULL;
|
||||
L->TMtable[tag].collected = NULL;
|
||||
}
|
||||
|
||||
|
||||
void luaT_init (lua_State *L) {
|
||||
int t;
|
||||
luaM_growvector(L, L->TMtable, 0, NUM_TAGS, struct TM, "", MAX_INT);
|
||||
L->nblocks += NUM_TAGS*sizeof(struct TM);
|
||||
L->last_tag = NUM_TAGS-1;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
init_entry(L, t);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_newtag (lua_State *L) {
|
||||
luaM_growvector(L, L->TMtable, L->last_tag, 1, struct TM,
|
||||
"tag table overflow", MAX_INT);
|
||||
L->nblocks += sizeof(struct TM);
|
||||
L->last_tag++;
|
||||
init_entry(L, L->last_tag);
|
||||
return L->last_tag;
|
||||
}
|
||||
|
||||
|
||||
static void checktag (lua_State *L, int tag) {
|
||||
if (!(0 <= tag && tag <= L->last_tag))
|
||||
luaO_verror(L, "%d is not a valid tag", tag);
|
||||
}
|
||||
|
||||
void luaT_realtag (lua_State *L, int tag) {
|
||||
if (!validtag(tag))
|
||||
luaO_verror(L, "tag %d was not created by `newtag'", tag);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
|
||||
int e;
|
||||
checktag(L, tagto);
|
||||
checktag(L, tagfrom);
|
||||
for (e=0; e<TM_N; e++) {
|
||||
if (luaT_validevent(tagto, e))
|
||||
luaT_gettm(L, tagto, e) = luaT_gettm(L, tagfrom, e);
|
||||
}
|
||||
return tagto;
|
||||
}
|
||||
|
||||
|
||||
int luaT_tag (const TObject *o) {
|
||||
int t = ttype(o);
|
||||
switch (t) {
|
||||
case LUA_TUSERDATA: return tsvalue(o)->u.d.tag;
|
||||
case LUA_TTABLE: return hvalue(o)->htag;
|
||||
default: return t;
|
||||
static const char *const luaT_eventname[] = { /* ORDER TM */
|
||||
"__index", "__newindex",
|
||||
"__gc", "__mode", "__eq",
|
||||
"__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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
|
||||
int e;
|
||||
e = luaI_checkevent(L, event, t);
|
||||
checktag(L, t);
|
||||
if (luaT_validevent(t, e) && luaT_gettm(L, t, e)) {
|
||||
clvalue(L->top) = luaT_gettm(L, t, e);
|
||||
ttype(L->top) = LUA_TFUNCTION;
|
||||
/*
|
||||
** 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
|
||||
ttype(L->top) = LUA_TNIL;
|
||||
incr_top;
|
||||
else return tm;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
|
||||
int e = luaI_checkevent(L, event, t);
|
||||
checktag(L, t);
|
||||
if (!luaT_validevent(t, e))
|
||||
luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
|
||||
luaT_eventname[e], luaO_typenames[t],
|
||||
(t == LUA_TTABLE || t == LUA_TUSERDATA) ?
|
||||
" with default tag" : "");
|
||||
switch (ttype(L->top - 1)) {
|
||||
case LUA_TNIL:
|
||||
luaT_gettm(L, t, e) = NULL;
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
luaT_gettm(L, t, e) = clvalue(L->top - 1);
|
||||
break;
|
||||
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:
|
||||
lua_error(L, "tag method must be a function (or nil)");
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
L->top--;
|
||||
}
|
||||
|
||||
|
||||
40
ltm.h
40
ltm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltm.h,v 1.17 2000/10/05 12:14:08 roberto Exp roberto $
|
||||
** $Id: ltm.h,v 1.40 2002/09/19 20:12:47 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,18 +9,18 @@
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER TM"
|
||||
*/
|
||||
typedef enum {
|
||||
TM_GETTABLE = 0,
|
||||
TM_SETTABLE,
|
||||
TM_INDEX,
|
||||
TM_GETGLOBAL,
|
||||
TM_SETGLOBAL,
|
||||
TM_NEWINDEX,
|
||||
TM_GC,
|
||||
TM_MODE,
|
||||
TM_EQ, /* last tag method with `fast' access */
|
||||
TM_ADD,
|
||||
TM_SUB,
|
||||
TM_MUL,
|
||||
@@ -28,32 +28,24 @@ typedef enum {
|
||||
TM_POW,
|
||||
TM_UNM,
|
||||
TM_LT,
|
||||
TM_LE,
|
||||
TM_CONCAT,
|
||||
TM_GC,
|
||||
TM_FUNCTION,
|
||||
TM_CALL,
|
||||
TM_N /* number of elements in the enum */
|
||||
} TMS;
|
||||
|
||||
|
||||
struct TM {
|
||||
Closure *method[TM_N];
|
||||
TString *collected; /* list of garbage-collected udata with this tag */
|
||||
};
|
||||
|
||||
|
||||
#define luaT_gettm(L,tag,event) (L->TMtable[tag].method[event])
|
||||
#define luaT_gettmbyObj(L,o,e) (luaT_gettm((L),luaT_tag(o),(e)))
|
||||
|
||||
|
||||
#define validtag(t) (NUM_TAGS <= (t) && (t) <= L->last_tag)
|
||||
|
||||
extern const char *const luaT_eventname[];
|
||||
|
||||
#define gfasttm(g,et,e) \
|
||||
(((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
|
||||
|
||||
#define fasttm(l,et,e) gfasttm(G(l), et, 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);
|
||||
void luaT_realtag (lua_State *L, int tag);
|
||||
int luaT_tag (const TObject *o);
|
||||
int luaT_validevent (int t, int e); /* used by compatibility module */
|
||||
|
||||
extern const char *const luaT_typenames[];
|
||||
|
||||
#endif
|
||||
|
||||
565
lua.c
565
lua.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.54 2000/10/17 13:36:24 roberto Exp roberto $
|
||||
** $Id: lua.c,v 1.112 2002/12/04 17:28:27 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,19 +10,22 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lua_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "luadebug.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
static lua_State *L = NULL;
|
||||
|
||||
|
||||
#ifndef PROMPT
|
||||
#define PROMPT "> "
|
||||
/*
|
||||
** generic extra include file
|
||||
*/
|
||||
#ifdef LUA_USERCONFIG
|
||||
#include LUA_USERCONFIG
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _POSIX_SOURCE
|
||||
#include <unistd.h>
|
||||
#else
|
||||
@@ -30,293 +33,401 @@ static int isatty (int x) { return x==0; } /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** global options
|
||||
*/
|
||||
struct Options {
|
||||
int toclose;
|
||||
int stacksize;
|
||||
|
||||
#ifndef PROMPT
|
||||
#define PROMPT "> "
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef PROMPT2
|
||||
#define PROMPT2 ">> "
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef lua_userinit
|
||||
#define lua_userinit(L) openstdlibs(L)
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef LUA_EXTRALIBS
|
||||
#define LUA_EXTRALIBS /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
static lua_State *L = NULL;
|
||||
|
||||
static const char *progname;
|
||||
|
||||
|
||||
|
||||
static const luaL_reg lualibs[] = {
|
||||
{"baselib", lua_baselibopen},
|
||||
{"tablib", lua_tablibopen},
|
||||
{"iolib", lua_iolibopen},
|
||||
{"strlib", lua_strlibopen},
|
||||
{"mathlib", lua_mathlibopen},
|
||||
{"dblib", lua_dblibopen},
|
||||
/* add your libraries here */
|
||||
LUA_EXTRALIBS
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
typedef void (*handler)(int); /* type for signal actions */
|
||||
|
||||
static void laction (int i);
|
||||
|
||||
|
||||
static lua_Hook old_linehook = NULL;
|
||||
static lua_Hook old_callhook = NULL;
|
||||
|
||||
|
||||
static void userinit (void) {
|
||||
lua_baselibopen(L);
|
||||
lua_iolibopen(L);
|
||||
lua_strlibopen(L);
|
||||
lua_mathlibopen(L);
|
||||
lua_dblibopen(L);
|
||||
/* add your libraries here */
|
||||
}
|
||||
|
||||
|
||||
static handler lreset (void) {
|
||||
return signal(SIGINT, laction);
|
||||
}
|
||||
|
||||
|
||||
static void lstop (void) {
|
||||
lua_setlinehook(L, old_linehook);
|
||||
lua_setcallhook(L, old_callhook);
|
||||
lreset();
|
||||
lua_error(L, "interrupted!");
|
||||
static void lstop (lua_State *l, lua_Debug *ar) {
|
||||
(void)ar; /* unused arg. */
|
||||
lua_sethook(l, NULL, 0, 0);
|
||||
luaL_error(l, "interrupted!");
|
||||
}
|
||||
|
||||
|
||||
static void laction (int i) {
|
||||
(void)i; /* to avoid warnings */
|
||||
signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
|
||||
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
|
||||
terminate process (default action) */
|
||||
old_linehook = lua_setlinehook(L, (lua_Hook)lstop);
|
||||
old_callhook = lua_setcallhook(L, (lua_Hook)lstop);
|
||||
lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
||||
}
|
||||
|
||||
|
||||
static int ldo (int (*f)(lua_State *l, const char *), const char *name) {
|
||||
int res;
|
||||
handler h = lreset();
|
||||
int top = lua_gettop(L);
|
||||
res = f(L, name); /* dostring | dofile */
|
||||
lua_settop(L, top); /* remove eventual results */
|
||||
signal(SIGINT, h); /* restore old action */
|
||||
/* Lua gives no message in such cases, so lua.c provides one */
|
||||
if (res == LUA_ERRMEM) {
|
||||
fprintf(stderr, "lua: memory allocation error\n");
|
||||
}
|
||||
else if (res == LUA_ERRERR)
|
||||
fprintf(stderr, "lua: error in error message\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static void print_message (void) {
|
||||
static void print_usage (void) {
|
||||
fprintf(stderr,
|
||||
"usage: lua [options]. Available options are:\n"
|
||||
"usage: %s [options] [script [args]].\n"
|
||||
"Available options are:\n"
|
||||
" - execute stdin as a file\n"
|
||||
" -c close Lua when exiting\n"
|
||||
" -e stat execute string `stat'\n"
|
||||
" -f name execute file `name' with remaining arguments in table `arg'\n"
|
||||
" -i enter interactive mode with prompt\n"
|
||||
" -q enter interactive mode without prompt\n"
|
||||
" -sNUM set stack size to NUM (must be the first option)\n"
|
||||
" -v print version information\n"
|
||||
" a=b set global `a' to string `b'\n"
|
||||
" name execute file `name'\n"
|
||||
);
|
||||
" -i enter interactive mode after executing `script'\n"
|
||||
" -l name load and run library `name'\n"
|
||||
" -v show version information\n"
|
||||
" -- stop handling options\n" ,
|
||||
progname);
|
||||
}
|
||||
|
||||
|
||||
static void l_message (const char *pname, const char *msg) {
|
||||
if (pname) fprintf(stderr, "%s: ", pname);
|
||||
fprintf(stderr, "%s\n", msg);
|
||||
}
|
||||
|
||||
|
||||
static int 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);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int lcall (int narg, int clear) {
|
||||
int status;
|
||||
int base = lua_gettop(L) - narg; /* function index */
|
||||
lua_pushliteral(L, "_TRACEBACK");
|
||||
lua_rawget(L, LUA_GLOBALSINDEX); /* get traceback function */
|
||||
lua_insert(L, base); /* put it under chunk and args */
|
||||
signal(SIGINT, laction);
|
||||
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
lua_remove(L, base); /* remove traceback function */
|
||||
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) {
|
||||
printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
|
||||
l_message(NULL, LUA_VERSION " " LUA_COPYRIGHT);
|
||||
}
|
||||
|
||||
|
||||
static void assign (char *arg) {
|
||||
char *eq = strchr(arg, '=');
|
||||
*eq = '\0'; /* spilt `arg' in two strings (name & value) */
|
||||
lua_pushstring(L, eq+1);
|
||||
lua_setglobal(L, arg);
|
||||
}
|
||||
|
||||
|
||||
static void getargs (char *argv[]) {
|
||||
static void getargs (char *argv[], int n) {
|
||||
int i;
|
||||
lua_newtable(L);
|
||||
for (i=0; argv[i]; i++) {
|
||||
/* arg[i] = argv[i] */
|
||||
lua_pushnumber(L, i);
|
||||
lua_pushnumber(L, i - n);
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_settable(L, -3);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
/* arg.n = maximum index in table `arg' */
|
||||
lua_pushstring(L, "n");
|
||||
lua_pushnumber(L, i-1);
|
||||
lua_settable(L, -3);
|
||||
lua_pushliteral(L, "n");
|
||||
lua_pushnumber(L, i-n-1);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static int l_getargs (lua_State *l) {
|
||||
char **argv = (char **)lua_touserdata(l, -1);
|
||||
getargs(argv);
|
||||
return 1;
|
||||
static int docall (int status) {
|
||||
if (status == 0) status = lcall(0, 1);
|
||||
return report(status);
|
||||
}
|
||||
|
||||
|
||||
static int file_input (const char *argv) {
|
||||
int result = ldo(lua_dofile, argv);
|
||||
if (result) {
|
||||
if (result == LUA_ERRFILE) {
|
||||
fprintf(stderr, "lua: cannot execute file ");
|
||||
perror(argv);
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
static int load_file (const char *name) {
|
||||
lua_pushliteral(L, "require");
|
||||
lua_rawget(L, LUA_GLOBALSINDEX);
|
||||
if (!lua_isfunction(L, -1)) { /* no `require' defined? */
|
||||
lua_pop(L, 1);
|
||||
return file_input(name);
|
||||
}
|
||||
else {
|
||||
lua_pushstring(L, name);
|
||||
return report(lcall(1, 1));
|
||||
}
|
||||
else
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/* maximum length of an input string */
|
||||
#ifndef MAXINPUT
|
||||
#define MAXINPUT BUFSIZ
|
||||
/*
|
||||
** 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
|
||||
|
||||
static void manual_input (int version, int prompt) {
|
||||
int cont = 1;
|
||||
if (version) print_version();
|
||||
while (cont) {
|
||||
char buffer[MAXINPUT];
|
||||
int i = 0;
|
||||
if (prompt) {
|
||||
const char *s;
|
||||
lua_getglobal(L, "_PROMPT");
|
||||
s = lua_tostring(L, -1);
|
||||
if (!s) s = PROMPT;
|
||||
fputs(s, stdout);
|
||||
lua_pop(L, 1); /* remove global */
|
||||
}
|
||||
for(;;) {
|
||||
int c = getchar();
|
||||
if (c == EOF) {
|
||||
cont = 0;
|
||||
break;
|
||||
}
|
||||
else if (c == '\n') {
|
||||
if (i>0 && buffer[i-1] == '\\')
|
||||
buffer[i-1] = '\n';
|
||||
else break;
|
||||
}
|
||||
else if (i >= MAXINPUT-1) {
|
||||
fprintf(stderr, "lua: input line too long\n");
|
||||
break;
|
||||
}
|
||||
else buffer[i++] = (char)c;
|
||||
}
|
||||
buffer[i] = '\0';
|
||||
ldo(lua_dostring, buffer);
|
||||
lua_settop(L, 0); /* remove eventual results */
|
||||
|
||||
/*
|
||||
** 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);
|
||||
}
|
||||
printf("\n");
|
||||
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_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
|
||||
lua_rawget(L, LUA_GLOBALSINDEX);
|
||||
p = lua_tostring(L, -1);
|
||||
if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
|
||||
lua_pop(L, 1); /* remove global */
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static int handle_argv (char *argv[], struct Options *opt) {
|
||||
if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */
|
||||
if (*argv == NULL) { /* no more arguments? */
|
||||
static int incomplete (int status) {
|
||||
if (status == LUA_ERRSYNTAX &&
|
||||
strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
|
||||
lua_pop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
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 */
|
||||
}
|
||||
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, 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)) {
|
||||
manual_input(1, 1);
|
||||
print_version();
|
||||
manual_input();
|
||||
}
|
||||
else
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
file_input(NULL); /* executes stdin as a file */
|
||||
}
|
||||
else { /* other arguments; loop over them */
|
||||
int i;
|
||||
for (i = 0; argv[i] != NULL; i++) {
|
||||
if (argv[i][0] != '-') { /* not an option? */
|
||||
if (strchr(argv[i], '='))
|
||||
assign(argv[i]);
|
||||
else
|
||||
if (file_input(argv[i]) != EXIT_SUCCESS)
|
||||
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 (load_file(filename))
|
||||
return EXIT_FAILURE; /* stop if file fails */
|
||||
break;
|
||||
}
|
||||
else switch (argv[i][1]) { /* option */
|
||||
case 0: {
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
break;
|
||||
}
|
||||
case 'i': {
|
||||
manual_input(0, 1);
|
||||
break;
|
||||
}
|
||||
case 'q': {
|
||||
manual_input(0, 0);
|
||||
break;
|
||||
}
|
||||
case 'c': {
|
||||
opt->toclose = 1;
|
||||
break;
|
||||
}
|
||||
case 'v': {
|
||||
print_version();
|
||||
break;
|
||||
}
|
||||
case 'e': {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (ldo(lua_dostring, argv[i]) != 0) {
|
||||
fprintf(stderr, "lua: error running argument `%.99s'\n", argv[i]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'f': {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
getargs(argv+i); /* collect remaining arguments */
|
||||
lua_setglobal(L, "arg");
|
||||
return file_input(argv[i]); /* stop scanning arguments */
|
||||
}
|
||||
case 's': {
|
||||
fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
default: {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
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 arguments */
|
||||
lua_setglobal(L, "arg");
|
||||
return file_input(filename); /* stop scanning arguments */
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void getstacksize (int argc, char *argv[], struct Options *opt) {
|
||||
if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 's') {
|
||||
int stacksize = atoi(&argv[1][2]);
|
||||
if (stacksize <= 0) {
|
||||
fprintf(stderr, "lua: invalid stack size ('%.20s')\n", &argv[1][2]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
opt->stacksize = stacksize;
|
||||
static void openstdlibs (lua_State *l) {
|
||||
const luaL_reg *lib = lualibs;
|
||||
for (; lib->name; lib++) {
|
||||
lib->func(l); /* open library */
|
||||
lua_settop(l, 0); /* discard any results */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
opt->stacksize = 0; /* no stack size */
|
||||
return dostring(init, "=LUA_INIT");
|
||||
}
|
||||
|
||||
|
||||
static void register_getargs (char *argv[]) {
|
||||
lua_pushuserdata(L, argv);
|
||||
lua_pushcclosure(L, l_getargs, 1);
|
||||
lua_setglobal(L, "getargs");
|
||||
struct Smain {
|
||||
int argc;
|
||||
char **argv;
|
||||
int status;
|
||||
};
|
||||
|
||||
|
||||
static int pmain (lua_State *l) {
|
||||
struct Smain *s = (struct Smain *)lua_touserdata(l, 1);
|
||||
int status;
|
||||
int interactive = 0;
|
||||
progname = s->argv[0];
|
||||
L = l;
|
||||
lua_userinit(l); /* open libraries */
|
||||
status = handle_luainit();
|
||||
if (status == 0) {
|
||||
status = handle_argv(s->argv, &interactive);
|
||||
if (status == 0 && interactive) manual_input();
|
||||
}
|
||||
s->status = status;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
struct Options opt;
|
||||
int status;
|
||||
opt.toclose = 0;
|
||||
getstacksize(argc, argv, &opt); /* handle option `-s' */
|
||||
L = lua_open(opt.stacksize); /* create state */
|
||||
userinit(); /* open libraries */
|
||||
register_getargs(argv); /* create `getargs' function */
|
||||
status = handle_argv(argv+1, &opt);
|
||||
if (opt.toclose)
|
||||
lua_close(L);
|
||||
return status;
|
||||
struct Smain s;
|
||||
lua_State *l = lua_open(); /* create state */
|
||||
if (l == NULL) {
|
||||
l_message(argv[0], "cannot create state: not enough memory");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
s.argc = argc;
|
||||
s.argv = argv;
|
||||
lua_atpanic(l, l_panic);
|
||||
status = lua_cpcall(l, &pmain, &s);
|
||||
report(status);
|
||||
lua_close(l);
|
||||
return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
397
lua.h
397
lua.h
@@ -1,9 +1,8 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.78 2000/10/30 12:38:50 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.168 2002/11/26 12:53:29 roberto Exp roberto $
|
||||
** Lua - An Extensible Extension Language
|
||||
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
|
||||
** e-mail: lua@tecgraf.puc-rio.br
|
||||
** www: http://www.tecgraf.puc-rio.br/lua/
|
||||
** 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
|
||||
*/
|
||||
|
||||
@@ -11,45 +10,29 @@
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
|
||||
/* definition of `size_t' */
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
/* mark for all API functions */
|
||||
#ifndef LUA_API
|
||||
#define LUA_API extern
|
||||
#endif
|
||||
#define LUA_VERSION "Lua 5.0 (beta)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2002 Tecgraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
|
||||
|
||||
|
||||
#define LUA_VERSION "Lua 4.0"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2000 TeCGraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
|
||||
|
||||
/* name of global variable with error handler */
|
||||
#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
|
||||
|
||||
|
||||
/* pre-defined references */
|
||||
#define LUA_NOREF (-2)
|
||||
#define LUA_REFNIL (-1)
|
||||
#define LUA_REFREGISTRY 0
|
||||
|
||||
/* pre-defined tags */
|
||||
#define LUA_ANYTAG (-1)
|
||||
#define LUA_NOTAG (-2)
|
||||
|
||||
|
||||
/* option for multiple returns in lua_call */
|
||||
/* option for multiple returns in `lua_pcall' and `lua_call' */
|
||||
#define LUA_MULTRET (-1)
|
||||
|
||||
|
||||
/* minimum stack available for a C function */
|
||||
#define LUA_MINSTACK 20
|
||||
/*
|
||||
** pseudo-indices
|
||||
*/
|
||||
#define LUA_REGISTRYINDEX (-10000)
|
||||
#define LUA_GLOBALSINDEX (-10001)
|
||||
#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
|
||||
|
||||
|
||||
/* error codes for lua_do* */
|
||||
/* error codes for `lua_load' and `lua_pcall' */
|
||||
#define LUA_ERRRUN 1
|
||||
#define LUA_ERRFILE 2
|
||||
#define LUA_ERRSYNTAX 3
|
||||
@@ -61,107 +44,163 @@ typedef struct lua_State lua_State;
|
||||
|
||||
typedef int (*lua_CFunction) (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** types returned by `lua_type'
|
||||
** functions that read/write blocks when loading/dumping Lua chunks
|
||||
*/
|
||||
typedef const char * (*lua_Chunkreader) (lua_State *L, void *ud, size_t *sz);
|
||||
|
||||
typedef int (*lua_Chunkwriter) (lua_State *L, const void* p,
|
||||
size_t sz, void* ud);
|
||||
|
||||
|
||||
/*
|
||||
** basic types
|
||||
*/
|
||||
#define LUA_TNONE (-1)
|
||||
|
||||
#define LUA_TUSERDATA 0
|
||||
#define LUA_TNIL 1
|
||||
#define LUA_TNUMBER 2
|
||||
#define LUA_TSTRING 3
|
||||
#define LUA_TTABLE 4
|
||||
#define LUA_TFUNCTION 5
|
||||
#define LUA_TNIL 0
|
||||
#define LUA_TBOOLEAN 1
|
||||
#define LUA_TLIGHTUSERDATA 2
|
||||
#define LUA_TNUMBER 3
|
||||
#define LUA_TSTRING 4
|
||||
#define LUA_TTABLE 5
|
||||
#define LUA_TFUNCTION 6
|
||||
#define LUA_TUSERDATA 7
|
||||
#define LUA_TTHREAD 8
|
||||
|
||||
|
||||
/* 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
|
||||
typedef double lua_Number;
|
||||
#else
|
||||
typedef LUA_NUMBER lua_Number;
|
||||
#endif
|
||||
|
||||
|
||||
/* mark for all API functions */
|
||||
#ifndef LUA_API
|
||||
#define LUA_API extern
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** state manipulation
|
||||
*/
|
||||
LUA_API lua_State *lua_open (int stacksize);
|
||||
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 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 int lua_stackspace (lua_State *L);
|
||||
LUA_API void lua_settop (lua_State *L, int idx);
|
||||
LUA_API void lua_pushvalue (lua_State *L, int idx);
|
||||
LUA_API void lua_remove (lua_State *L, int idx);
|
||||
LUA_API void lua_insert (lua_State *L, int idx);
|
||||
LUA_API void lua_replace (lua_State *L, int idx);
|
||||
LUA_API int lua_checkstack (lua_State *L, int sz);
|
||||
|
||||
LUA_API void lua_xmove (lua_State *from, lua_State *to, int n);
|
||||
|
||||
|
||||
/*
|
||||
** access functions (stack -> C)
|
||||
*/
|
||||
|
||||
LUA_API int lua_type (lua_State *L, int index);
|
||||
LUA_API const char *lua_typename (lua_State *L, int t);
|
||||
LUA_API int lua_isnumber (lua_State *L, int index);
|
||||
LUA_API int lua_isstring (lua_State *L, int index);
|
||||
LUA_API int lua_iscfunction (lua_State *L, int index);
|
||||
LUA_API int lua_tag (lua_State *L, int index);
|
||||
LUA_API int lua_isnumber (lua_State *L, int idx);
|
||||
LUA_API int lua_isstring (lua_State *L, int idx);
|
||||
LUA_API int lua_iscfunction (lua_State *L, int idx);
|
||||
LUA_API int lua_isuserdata (lua_State *L, int idx);
|
||||
LUA_API int lua_type (lua_State *L, int idx);
|
||||
LUA_API const char *lua_typename (lua_State *L, int tp);
|
||||
|
||||
LUA_API int lua_equal (lua_State *L, int index1, int index2);
|
||||
LUA_API int lua_lessthan (lua_State *L, int index1, int index2);
|
||||
LUA_API int lua_equal (lua_State *L, int idx1, int idx2);
|
||||
LUA_API int lua_rawequal (lua_State *L, int idx1, int idx2);
|
||||
LUA_API int lua_lessthan (lua_State *L, int idx1, int idx2);
|
||||
|
||||
LUA_API double lua_tonumber (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);
|
||||
LUA_API lua_Number lua_tonumber (lua_State *L, int idx);
|
||||
LUA_API int lua_toboolean (lua_State *L, int idx);
|
||||
LUA_API const char *lua_tostring (lua_State *L, int idx);
|
||||
LUA_API size_t lua_strlen (lua_State *L, int idx);
|
||||
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx);
|
||||
LUA_API void *lua_touserdata (lua_State *L, int idx);
|
||||
LUA_API lua_State *lua_tothread (lua_State *L, int idx);
|
||||
LUA_API const void *lua_topointer (lua_State *L, int idx);
|
||||
|
||||
|
||||
/*
|
||||
** push functions (C -> stack)
|
||||
*/
|
||||
LUA_API void lua_pushnil (lua_State *L);
|
||||
LUA_API void lua_pushnumber (lua_State *L, double n);
|
||||
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len);
|
||||
LUA_API void lua_pushnumber (lua_State *L, lua_Number n);
|
||||
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t l);
|
||||
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_pushusertag (lua_State *L, void *u, int tag);
|
||||
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_getglobal (lua_State *L, const char *name);
|
||||
LUA_API void lua_gettable (lua_State *L, int index);
|
||||
LUA_API void lua_rawget (lua_State *L, int index);
|
||||
LUA_API void lua_rawgeti (lua_State *L, int index, int n);
|
||||
LUA_API void lua_getglobals (lua_State *L);
|
||||
LUA_API void lua_gettagmethod (lua_State *L, int tag, const char *event);
|
||||
LUA_API int lua_getref (lua_State *L, int ref);
|
||||
LUA_API void lua_gettable (lua_State *L, int idx);
|
||||
LUA_API void lua_rawget (lua_State *L, int idx);
|
||||
LUA_API void lua_rawgeti (lua_State *L, int idx, int n);
|
||||
LUA_API void lua_newtable (lua_State *L);
|
||||
LUA_API int lua_getmetatable (lua_State *L, int objindex);
|
||||
LUA_API void lua_getglobals (lua_State *L, int idx);
|
||||
|
||||
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
LUA_API void lua_setglobal (lua_State *L, const char *name);
|
||||
LUA_API void lua_settable (lua_State *L, int index);
|
||||
LUA_API void lua_rawset (lua_State *L, int index);
|
||||
LUA_API void lua_rawseti (lua_State *L, int index, int n);
|
||||
LUA_API void lua_setglobals (lua_State *L);
|
||||
LUA_API void lua_settagmethod (lua_State *L, int tag, const char *event);
|
||||
LUA_API int lua_ref (lua_State *L, int lock);
|
||||
LUA_API void lua_settable (lua_State *L, int idx);
|
||||
LUA_API void lua_rawset (lua_State *L, int idx);
|
||||
LUA_API void lua_rawseti (lua_State *L, int idx, int n);
|
||||
LUA_API int lua_setmetatable (lua_State *L, int objindex);
|
||||
LUA_API int lua_setglobals (lua_State *L, int idx);
|
||||
|
||||
|
||||
/*
|
||||
** "do" functions (run Lua code)
|
||||
** `load' and `call' functions (load and run Lua code)
|
||||
*/
|
||||
LUA_API int lua_call (lua_State *L, int nargs, int nresults);
|
||||
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults);
|
||||
LUA_API int lua_dofile (lua_State *L, const char *filename);
|
||||
LUA_API int lua_dostring (lua_State *L, const char *str);
|
||||
LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name);
|
||||
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_cpcall (lua_State *L, lua_CFunction func, void *ud);
|
||||
LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *dt,
|
||||
const char *chunkname);
|
||||
|
||||
LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data);
|
||||
|
||||
|
||||
/*
|
||||
** Garbage-collection functions
|
||||
** coroutine functions
|
||||
*/
|
||||
LUA_API int lua_yield (lua_State *L, int nresults);
|
||||
LUA_API int lua_resume (lua_State *L, int narg);
|
||||
|
||||
/*
|
||||
** garbage-collection functions
|
||||
*/
|
||||
LUA_API int lua_getgcthreshold (lua_State *L);
|
||||
LUA_API int lua_getgccount (lua_State *L);
|
||||
@@ -170,20 +209,17 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
|
||||
/*
|
||||
** miscellaneous functions
|
||||
*/
|
||||
LUA_API int lua_newtag (lua_State *L);
|
||||
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
||||
LUA_API void lua_settag (lua_State *L, int tag);
|
||||
|
||||
LUA_API void lua_error (lua_State *L, const char *s);
|
||||
LUA_API const char *lua_version (void);
|
||||
|
||||
LUA_API void lua_unref (lua_State *L, int ref);
|
||||
LUA_API int lua_error (lua_State *L);
|
||||
|
||||
LUA_API int lua_next (lua_State *L, int index);
|
||||
LUA_API int lua_getn (lua_State *L, int index);
|
||||
LUA_API int lua_next (lua_State *L, int idx);
|
||||
|
||||
LUA_API void lua_concat (lua_State *L, int n);
|
||||
|
||||
LUA_API void *lua_newuserdata (lua_State *L, size_t size);
|
||||
LUA_API void *lua_newuserdata (lua_State *L, size_t sz);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@@ -192,57 +228,162 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size);
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#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_pushcfunction(L, f), lua_setglobal(L, n))
|
||||
#define lua_pushuserdata(L,u) lua_pushusertag(L, u, 0)
|
||||
#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_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
|
||||
|
||||
#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_isnull(L,n) (lua_type(L,n) == LUA_TNONE)
|
||||
#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_getregistry(L) lua_getref(L, LUA_REFREGISTRY)
|
||||
#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))
|
||||
|
||||
|
||||
/* 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 "%.14g"
|
||||
#endif
|
||||
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================================
|
||||
** Debug API
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** Event codes
|
||||
*/
|
||||
#define LUA_HOOKCALL 0
|
||||
#define LUA_HOOKRET 1
|
||||
#define LUA_HOOKLINE 2
|
||||
#define LUA_HOOKCOUNT 3
|
||||
|
||||
|
||||
/*
|
||||
** Event masks
|
||||
*/
|
||||
#define LUA_MASKCALL (1 << LUA_HOOKCALL)
|
||||
#define LUA_MASKRET (1 << LUA_HOOKRET)
|
||||
#define LUA_MASKLINE (1 << LUA_HOOKLINE)
|
||||
#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
|
||||
|
||||
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, int mask, int count);
|
||||
LUA_API lua_Hook lua_gethook (lua_State *L);
|
||||
LUA_API int lua_gethookmask (lua_State *L);
|
||||
LUA_API int lua_gethookcount (lua_State *L);
|
||||
|
||||
|
||||
#define LUA_IDSIZE 60
|
||||
|
||||
struct lua_Debug {
|
||||
int 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) 1994-2000 TeCGraf, PUC-Rio. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, without written agreement and without license
|
||||
* or royalty fees, to use, copy, modify, and distribute this software and its
|
||||
* documentation for any purpose, including commercial applications, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* - The above copyright notice and this permission notice shall appear in all
|
||||
* copies or substantial portions of this software.
|
||||
*
|
||||
* - The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software in a
|
||||
* product, an acknowledgment in the product documentation would be greatly
|
||||
* appreciated (but it is not required).
|
||||
*
|
||||
* - Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* The authors specifically disclaim any warranties, including, but not limited
|
||||
* to, the implied warranties of merchantability and fitness for a particular
|
||||
* purpose. The software provided hereunder is on an "as is" basis, and the
|
||||
* authors have no obligation to provide maintenance, support, updates,
|
||||
* enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the
|
||||
* authors be held liable to any party for direct, indirect, special,
|
||||
* incidental, or consequential damages arising out of the use of this software
|
||||
* and its documentation.
|
||||
*
|
||||
* The Lua language and this implementation have been entirely designed and
|
||||
* written by Waldemar Celes Filho, Roberto Ierusalimschy and
|
||||
* Luiz Henrique de Figueiredo at TeCGraf, PUC-Rio.
|
||||
* Copyright (C) 2002 Tecgraf, PUC-Rio. All rights reserved.
|
||||
*
|
||||
* This implementation contains no third-party code.
|
||||
* 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.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
46
luadebug.h
46
luadebug.h
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
** $Id: luadebug.h,v 1.16 2000/10/20 16:39:03 roberto Exp roberto $
|
||||
** Debugging API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef luadebug_h
|
||||
#define luadebug_h
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
typedef struct lua_Debug lua_Debug; /* activation record */
|
||||
typedef struct lua_Localvar lua_Localvar;
|
||||
|
||||
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 lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
|
||||
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
|
||||
|
||||
|
||||
#define LUA_IDSIZE 60
|
||||
|
||||
struct lua_Debug {
|
||||
const char *event; /* `call', `return' */
|
||||
int currentline; /* (l) */
|
||||
const char *name; /* (n) */
|
||||
const char *namewhat; /* (n) `global', `tag method', `local', `field' */
|
||||
int nups; /* (u) number of upvalues */
|
||||
int linedefined; /* (S) */
|
||||
const char *what; /* (S) `Lua' function, `C' function, Lua `main' */
|
||||
const char *source; /* (S) */
|
||||
char short_src[LUA_IDSIZE]; /* (S) */
|
||||
/* private part */
|
||||
struct lua_TObject *_func; /* active function */
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
34
lualib.h
34
lualib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lualib.h,v 1.13 2000/10/20 16:39:03 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
|
||||
*/
|
||||
@@ -16,19 +16,29 @@
|
||||
#endif
|
||||
|
||||
|
||||
#define LUA_ALERT "_ALERT"
|
||||
#define LUA_COLIBNAME "coroutine"
|
||||
LUALIB_API int lua_baselibopen (lua_State *L);
|
||||
|
||||
LUALIB_API void lua_baselibopen (lua_State *L);
|
||||
LUALIB_API void lua_iolibopen (lua_State *L);
|
||||
LUALIB_API void lua_strlibopen (lua_State *L);
|
||||
LUALIB_API void lua_mathlibopen (lua_State *L);
|
||||
LUALIB_API void lua_dblibopen (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);
|
||||
|
||||
|
||||
|
||||
/* Auxiliary functions (private) */
|
||||
|
||||
const char *luaI_classend (lua_State *L, const char *p);
|
||||
int luaI_singlematch (int c, const char *p, const char *ep);
|
||||
/* to help testing the libraries */
|
||||
#ifndef lua_assert
|
||||
#define lua_assert(c) /* empty */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
291
lundump.c
291
lundump.c
@@ -1,243 +1,272 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.33 2000/10/31 16:57:23 lhf Exp $
|
||||
** load bytecodes from files
|
||||
** $Id: lundump.c,v 1.57 2002/11/14 16:15:53 roberto Exp roberto $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#define lundump_c
|
||||
|
||||
#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 ezgetc
|
||||
#define LoadByte (lu_byte) ezgetc
|
||||
|
||||
static const char* ZNAME (ZIO* Z)
|
||||
typedef struct {
|
||||
lua_State* L;
|
||||
ZIO* Z;
|
||||
Mbuffer* b;
|
||||
int swap;
|
||||
const char* name;
|
||||
} LoadState;
|
||||
|
||||
static void unexpectedEOZ (LoadState* S)
|
||||
{
|
||||
const char* s=zname(Z);
|
||||
return (*s=='@') ? s+1 : s;
|
||||
luaG_runerror(S->L,"unexpected end of file in %s",S->name);
|
||||
}
|
||||
|
||||
static void unexpectedEOZ (lua_State* L, ZIO* Z)
|
||||
static int ezgetc (LoadState* S)
|
||||
{
|
||||
luaO_verror(L,"unexpected end of file in `%.99s'",ZNAME(Z));
|
||||
}
|
||||
|
||||
static int ezgetc (lua_State* L, ZIO* Z)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==EOZ) unexpectedEOZ(L,Z);
|
||||
int c=zgetc(S->Z);
|
||||
if (c==EOZ) unexpectedEOZ(S);
|
||||
return c;
|
||||
}
|
||||
|
||||
static void ezread (lua_State* L, ZIO* Z, void* b, int n)
|
||||
static void ezread (LoadState* S, void* b, int n)
|
||||
{
|
||||
int r=zread(Z,b,n);
|
||||
if (r!=0) unexpectedEOZ(L,Z);
|
||||
int r=luaZ_read(S->Z,b,n);
|
||||
if (r!=0) unexpectedEOZ(S);
|
||||
}
|
||||
|
||||
static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
|
||||
static void LoadBlock (LoadState* S, void* b, size_t size)
|
||||
{
|
||||
if (swap)
|
||||
if (S->swap)
|
||||
{
|
||||
char *p=(char *) b+size-1;
|
||||
char* p=(char*) b+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(L,Z);
|
||||
while (n--) *p--=(char)ezgetc(S);
|
||||
}
|
||||
else
|
||||
ezread(L,Z,b,size);
|
||||
ezread(S,b,size);
|
||||
}
|
||||
|
||||
static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
|
||||
static void LoadVector (LoadState* S, void* b, int m, size_t size)
|
||||
{
|
||||
if (swap)
|
||||
if (S->swap)
|
||||
{
|
||||
char *q=(char *) b;
|
||||
char* q=(char*) b;
|
||||
while (m--)
|
||||
{
|
||||
char *p=q+size-1;
|
||||
char* p=q+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(L,Z);
|
||||
while (n--) *p--=(char)ezgetc(S);
|
||||
q+=size;
|
||||
}
|
||||
}
|
||||
else
|
||||
ezread(L,Z,b,m*size);
|
||||
ezread(S,b,m*size);
|
||||
}
|
||||
|
||||
static int LoadInt (lua_State* L, ZIO* Z, int swap)
|
||||
static int LoadInt (LoadState* S)
|
||||
{
|
||||
int x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
LoadBlock(S,&x,sizeof(x));
|
||||
if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name);
|
||||
return x;
|
||||
}
|
||||
|
||||
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
|
||||
static size_t LoadSize (LoadState* S)
|
||||
{
|
||||
size_t x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
LoadBlock(S,&x,sizeof(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
|
||||
static lua_Number LoadNumber (LoadState* S)
|
||||
{
|
||||
Number x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
lua_Number x;
|
||||
LoadBlock(S,&x,sizeof(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static TString* LoadString (lua_State* L, ZIO* Z, int swap)
|
||||
static TString* LoadString (LoadState* S)
|
||||
{
|
||||
size_t size=LoadSize(L,Z,swap);
|
||||
size_t size=LoadSize(S);
|
||||
if (size==0)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
char* s=luaO_openspace(L,size);
|
||||
LoadBlock(L,s,size,Z,0);
|
||||
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
|
||||
char* s=luaZ_openspace(S->L,S->b,size);
|
||||
ezread(S,s,size);
|
||||
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
static void LoadCode (LoadState* S, Proto* f)
|
||||
{
|
||||
int size=LoadInt(L,Z,swap);
|
||||
tf->code=luaM_newvector(L,size,Instruction);
|
||||
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
|
||||
if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.99s'",ZNAME(Z));
|
||||
luaF_protook(L,tf,size);
|
||||
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 (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
static void LoadLocals (LoadState* S, Proto* f)
|
||||
{
|
||||
int i,n;
|
||||
tf->nlocvars=n=LoadInt(L,Z,swap);
|
||||
tf->locvars=luaM_newvector(L,n,LocVar);
|
||||
n=LoadInt(S);
|
||||
f->locvars=luaM_newvector(S->L,n,LocVar);
|
||||
f->sizelocvars=n;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->locvars[i].varname=LoadString(L,Z,swap);
|
||||
tf->locvars[i].startpc=LoadInt(L,Z,swap);
|
||||
tf->locvars[i].endpc=LoadInt(L,Z,swap);
|
||||
f->locvars[i].varname=LoadString(S);
|
||||
f->locvars[i].startpc=LoadInt(S);
|
||||
f->locvars[i].endpc=LoadInt(S);
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
static void LoadLines (LoadState* S, Proto* f)
|
||||
{
|
||||
int n;
|
||||
tf->nlineinfo=n=LoadInt(L,Z,swap);
|
||||
tf->lineinfo=luaM_newvector(L,n,int);
|
||||
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
|
||||
int size=LoadInt(S);
|
||||
f->lineinfo=luaM_newvector(S->L,size,int);
|
||||
f->sizelineinfo=size;
|
||||
LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo));
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
|
||||
static Proto* LoadFunction (LoadState* S, TString* p);
|
||||
|
||||
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
static void LoadConstants (LoadState* S, Proto* f)
|
||||
{
|
||||
int i,n;
|
||||
tf->nkstr=n=LoadInt(L,Z,swap);
|
||||
tf->kstr=luaM_newvector(L,n,TString*);
|
||||
n=LoadInt(S);
|
||||
f->k=luaM_newvector(S->L,n,TObject);
|
||||
f->sizek=n;
|
||||
for (i=0; i<n; i++)
|
||||
tf->kstr[i]=LoadString(L,Z,swap);
|
||||
tf->nknum=n=LoadInt(L,Z,swap);
|
||||
tf->knum=luaM_newvector(L,n,Number);
|
||||
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
|
||||
tf->nkproto=n=LoadInt(L,Z,swap);
|
||||
tf->kproto=luaM_newvector(L,n,Proto*);
|
||||
for (i=0; i<n; i++)
|
||||
tf->kproto[i]=LoadFunction(L,Z,swap);
|
||||
{
|
||||
TObject* o=&f->k[i];
|
||||
int t=LoadByte(S);
|
||||
switch (t)
|
||||
{
|
||||
case LUA_TNUMBER:
|
||||
setnvalue(o,LoadNumber(S));
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
setsvalue2n(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 (lua_State* L, ZIO* Z, int swap)
|
||||
static Proto* LoadFunction (LoadState* S, TString* p)
|
||||
{
|
||||
Proto* tf=luaF_newproto(L);
|
||||
tf->source=LoadString(L,Z,swap);
|
||||
tf->lineDefined=LoadInt(L,Z,swap);
|
||||
tf->numparams=LoadInt(L,Z,swap);
|
||||
tf->is_vararg=LoadByte(L,Z);
|
||||
tf->maxstacksize=LoadInt(L,Z,swap);
|
||||
LoadLocals(L,tf,Z,swap);
|
||||
LoadLines(L,tf,Z,swap);
|
||||
LoadConstants(L,tf,Z,swap);
|
||||
LoadCode(L,tf,Z,swap);
|
||||
return tf;
|
||||
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 (lua_State* L, ZIO* Z)
|
||||
static void LoadSignature (LoadState* S)
|
||||
{
|
||||
const char* s=SIGNATURE;
|
||||
while (*s!=0 && ezgetc(L,Z)==*s)
|
||||
const char* s=LUA_SIGNATURE;
|
||||
while (*s!=0 && ezgetc(S)==*s)
|
||||
++s;
|
||||
if (*s!=0) luaO_verror(L,"bad signature in `%.99s'",ZNAME(Z));
|
||||
if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name);
|
||||
}
|
||||
|
||||
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
|
||||
static void TestSize (LoadState* S, int s, const char* what)
|
||||
{
|
||||
int r=ezgetc(L,Z);
|
||||
int r=LoadByte(S);
|
||||
if (r!=s)
|
||||
luaO_verror(L,"virtual machine mismatch in `%.99s':\n"
|
||||
" %.20s is %d but read %d",ZNAME(Z),what,s,r);
|
||||
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) TestSize(L,s,#s,Z)
|
||||
#define V(v) v/16,v%16
|
||||
#define TESTSIZE(s,w) TestSize(S,s,w)
|
||||
#define V(v) v/16,v%16
|
||||
|
||||
static int LoadHeader (lua_State* L, ZIO* Z)
|
||||
static void LoadHeader (LoadState* S)
|
||||
{
|
||||
int version,swap;
|
||||
Number f=0,tf=TEST_NUMBER;
|
||||
LoadSignature(L,Z);
|
||||
version=ezgetc(L,Z);
|
||||
int version;
|
||||
lua_Number x=0,tx=TEST_NUMBER;
|
||||
LoadSignature(S);
|
||||
version=LoadByte(S);
|
||||
if (version>VERSION)
|
||||
luaO_verror(L,"`%.99s' too new:\n"
|
||||
" read version %d.%d; expected at most %d.%d",
|
||||
ZNAME(Z),V(version),V(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 */
|
||||
luaO_verror(L,"`%.99s' too old:\n"
|
||||
" read version %d.%d; expected at least %d.%d",
|
||||
ZNAME(Z),V(version),V(VERSION));
|
||||
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
|
||||
TESTSIZE(sizeof(int));
|
||||
TESTSIZE(sizeof(size_t));
|
||||
TESTSIZE(sizeof(Instruction));
|
||||
TESTSIZE(SIZE_INSTRUCTION);
|
||||
TESTSIZE(SIZE_OP);
|
||||
TESTSIZE(SIZE_B);
|
||||
TESTSIZE(sizeof(Number));
|
||||
f=LoadNumber(L,Z,swap);
|
||||
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
||||
luaO_verror(L,"unknown number format in `%.99s':\n"
|
||||
" read " NUMBER_FMT "; expected " NUMBER_FMT, ZNAME(Z),f,tf);
|
||||
return swap;
|
||||
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",S->name);
|
||||
}
|
||||
|
||||
static Proto* LoadChunk (lua_State* L, ZIO* Z)
|
||||
static Proto* LoadChunk (LoadState* S)
|
||||
{
|
||||
return LoadFunction(L,Z,LoadHeader(L,Z));
|
||||
LoadHeader(S);
|
||||
return LoadFunction(S,NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
** load one chunk from a file or buffer
|
||||
** return main if ok and NULL at EOF
|
||||
** load precompiled chunk
|
||||
*/
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z)
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff)
|
||||
{
|
||||
Proto* tf=NULL;
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_CHUNK)
|
||||
tf=LoadChunk(L,Z);
|
||||
c=zgetc(Z);
|
||||
if (c!=EOZ)
|
||||
luaO_verror(L,"`%.99s' apparently contains more than one chunk",ZNAME(Z));
|
||||
return tf;
|
||||
LoadState S;
|
||||
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;
|
||||
S.b=buff;
|
||||
return LoadChunk(&S);
|
||||
}
|
||||
|
||||
/*
|
||||
** find byte order
|
||||
*/
|
||||
int luaU_endianess (void)
|
||||
int luaU_endianness (void)
|
||||
{
|
||||
int x=1;
|
||||
return *(char*)&x;
|
||||
|
||||
29
lundump.h
29
lundump.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.21 2000/10/31 16:57:23 lhf Exp $
|
||||
** $Id: lundump.h,v 1.28 2002/10/09 13:42:01 roberto Exp roberto $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,23 +10,22 @@
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
/* load one chunk */
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z);
|
||||
/* load one chunk; from lundump.c */
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer *buff);
|
||||
|
||||
/* find byte order */
|
||||
int luaU_endianess (void);
|
||||
/* find byte order; from lundump.c */
|
||||
int luaU_endianness (void);
|
||||
|
||||
/* dump one chunk; from dump.c */
|
||||
void luaU_dump (lua_State *L, const Proto* Main, lua_Chunkwriter w, void* data);
|
||||
|
||||
/* print one chunk; from print.c */
|
||||
void luaU_print (const Proto* Main);
|
||||
|
||||
/* definitions for headers of binary files */
|
||||
#define VERSION 0x40 /* last format change was in 4.0 */
|
||||
#define VERSION0 0x40 /* last major change was in 4.0 */
|
||||
#define ID_CHUNK 27 /* binary files start with ESC... */
|
||||
#define SIGNATURE "Lua" /* ...followed by this signature */
|
||||
|
||||
/* formats for error messages */
|
||||
#define SOURCE_FMT "<%d:%.99s>"
|
||||
#define SOURCE tf->lineDefined,tf->source->str
|
||||
#define IN_FMT " in %p " SOURCE_FMT
|
||||
#define IN tf,SOURCE
|
||||
#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 */
|
||||
|
||||
31
lvm.h
31
lvm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 1.26 2000/09/05 19:33:32 roberto Exp roberto $
|
||||
** $Id: lvm.h,v 1.46 2002/08/07 19:22:39 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,20 +13,23 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
#define tonumber(o) ((ttype(o) != LUA_TNUMBER) && (luaV_tonumber(o) != 0))
|
||||
#define tostring(L,o) ((ttype(o) != LUA_TSTRING) && (luaV_tostring(L, o) != 0))
|
||||
#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_tonumber (TObject *obj);
|
||||
int luaV_tostring (lua_State *L, TObject *obj);
|
||||
const TObject *luaV_gettable (lua_State *L, StkId t);
|
||||
void luaV_settable (lua_State *L, StkId t, StkId key);
|
||||
const TObject *luaV_getglobal (lua_State *L, TString *s);
|
||||
void luaV_setglobal (lua_State *L, TString *s);
|
||||
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base);
|
||||
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems);
|
||||
void luaV_Lclosure (lua_State *L, Proto *l, int nelems);
|
||||
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top);
|
||||
void luaV_strconc (lua_State *L, int total, StkId top);
|
||||
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, StkId 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
|
||||
|
||||
93
lzio.c
93
lzio.c
@@ -1,77 +1,62 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.12 2000/05/24 13:54:49 roberto Exp roberto $
|
||||
** $Id: lzio.c,v 1.22 2002/10/08 18:46:08 roberto Exp roberto $
|
||||
** a generic input stream interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lzio_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lmem.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
/* ----------------------------------------------------- memory buffers --- */
|
||||
|
||||
static int zmfilbuf (ZIO* z) {
|
||||
(void)z; /* to avoid warnings */
|
||||
return EOZ;
|
||||
}
|
||||
|
||||
|
||||
ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name) {
|
||||
if (b==NULL) return NULL;
|
||||
z->n = size;
|
||||
z->p = (const unsigned char *)b;
|
||||
z->filbuf = zmfilbuf;
|
||||
z->u = NULL;
|
||||
z->name = name;
|
||||
return z;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ strings --- */
|
||||
|
||||
ZIO* zsopen (ZIO* z, const char* s, const char *name) {
|
||||
if (s==NULL) return NULL;
|
||||
return zmopen(z, s, strlen(s), name);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------- FILEs --- */
|
||||
|
||||
static int zffilbuf (ZIO* z) {
|
||||
size_t n;
|
||||
if (feof((FILE *)z->u)) return EOZ;
|
||||
n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u);
|
||||
if (n==0) return EOZ;
|
||||
z->n = n-1;
|
||||
z->p = z->buffer;
|
||||
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++);
|
||||
}
|
||||
|
||||
|
||||
ZIO* zFopen (ZIO* z, FILE* f, const char *name) {
|
||||
if (f==NULL) return NULL;
|
||||
z->n = 0;
|
||||
z->p = z->buffer;
|
||||
z->filbuf = zffilbuf;
|
||||
z->u = f;
|
||||
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;
|
||||
return z;
|
||||
z->n = 0;
|
||||
z->p = NULL;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- read --- */
|
||||
size_t zread (ZIO *z, void *b, size_t n) {
|
||||
size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
||||
while (n) {
|
||||
size_t m;
|
||||
if (z->n == 0) {
|
||||
if (z->filbuf(z) == EOZ)
|
||||
if (luaZ_fill(z) == EOZ)
|
||||
return n; /* return number of missing bytes */
|
||||
zungetc(z); /* put result from `filbuf' in the buffer */
|
||||
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);
|
||||
@@ -82,3 +67,15 @@ size_t zread (ZIO *z, void *b, size_t n) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
|
||||
if (n > buff->buffsize) {
|
||||
if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
|
||||
luaM_reallocvector(L, buff->buffer, buff->buffsize, n, char);
|
||||
buff->buffsize = n;
|
||||
}
|
||||
return buff->buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
64
lzio.h
64
lzio.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.h,v 1.6 2000/05/24 13:54:49 roberto Exp roberto $
|
||||
** $Id: lzio.h,v 1.13 2002/08/05 18:45:02 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,46 +8,56 @@
|
||||
#ifndef lzio_h
|
||||
#define lzio_h
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
|
||||
/* For Lua only */
|
||||
#define zFopen luaZ_Fopen
|
||||
#define zsopen luaZ_sopen
|
||||
#define zmopen luaZ_mopen
|
||||
#define zread luaZ_read
|
||||
|
||||
#define EOZ (-1) /* end of stream */
|
||||
|
||||
typedef struct zio ZIO;
|
||||
typedef struct Zio ZIO;
|
||||
|
||||
ZIO* zFopen (ZIO* z, FILE* f, const char *name); /* open FILEs */
|
||||
ZIO* zsopen (ZIO* z, const char* s, const char *name); /* string */
|
||||
ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name); /* memory */
|
||||
#define zgetc(z) (((z)->n--)>0 ? \
|
||||
cast(int, cast(unsigned char, *(z)->p++)) : \
|
||||
luaZ_fill(z))
|
||||
|
||||
size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
|
||||
|
||||
#define zgetc(z) (((z)->n--)>0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||
#define zungetc(z) (++(z)->n,--(z)->p)
|
||||
#define 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);
|
||||
|
||||
|
||||
|
||||
typedef struct Mbuffer {
|
||||
char *buffer;
|
||||
size_t buffsize;
|
||||
} Mbuffer;
|
||||
|
||||
|
||||
char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
|
||||
|
||||
#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
|
||||
|
||||
#define luaZ_sizebuffer(buff) ((buff)->buffsize)
|
||||
#define luaZ_buffer(buff) ((buff)->buffer)
|
||||
|
||||
#define luaZ_resizebuffer(L, buff, size) \
|
||||
(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
|
||||
(buff)->buffsize = size)
|
||||
|
||||
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
|
||||
|
||||
|
||||
/* --------- Private Part ------------------ */
|
||||
|
||||
#ifndef ZBSIZE
|
||||
#define ZBSIZE 256 /* buffer size */
|
||||
#endif
|
||||
|
||||
struct zio {
|
||||
size_t n; /* bytes still unread */
|
||||
const unsigned char* p; /* current position in buffer */
|
||||
int (*filbuf)(ZIO* z);
|
||||
void* u; /* additional data */
|
||||
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;
|
||||
unsigned char buffer[ZBSIZE]; /* buffer */
|
||||
};
|
||||
|
||||
|
||||
int luaZ_fill (ZIO *z);
|
||||
|
||||
#endif
|
||||
|
||||
130
makefile
130
makefile
@@ -1,5 +1,5 @@
|
||||
#
|
||||
## $Id: makefile,v 1.29 2000/10/30 17:50:00 roberto Exp roberto $
|
||||
## $Id: makefile,v 1.38 2002/10/25 21:38:17 roberto Exp $
|
||||
## Makefile
|
||||
## See Copyright Notice in lua.h
|
||||
#
|
||||
@@ -7,43 +7,39 @@
|
||||
|
||||
#CONFIGURATION
|
||||
|
||||
# define (undefine) POPEN if your system (does not) support piped I/O
|
||||
# define (undefine) USE_POPEN if your system (does not) support piped I/O
|
||||
#
|
||||
# define (undefine) _POSIX_SOURCE if your system is (not) POSIX compliant
|
||||
#
|
||||
# define (undefine) OLD_ANSI if your system does NOT have some new ANSI
|
||||
# facilities (e.g. strerror, locale.h, memmove). SunOS does not comply;
|
||||
# so, add "-DOLD_ANSI" on SunOS
|
||||
#
|
||||
# define LUA_NUM_TYPE if you need numbers to be different from double
|
||||
# (for instance, -DLUA_NUM_TYPE=float)
|
||||
# you may need to adapat the code, too.
|
||||
#
|
||||
# define LUA_COMPAT_READPATTERN if you need read patterns
|
||||
# (only for compatibility with previous versions)
|
||||
# define LUA_COMPAT_ARGRET for compatibility in the way function results
|
||||
# are passed as arguments
|
||||
# define LUA_DEPRECATEDFUNCS to include obsolete functions
|
||||
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||
#CONFIG = -DOLD_ANSI -DLUA_DEBUG -DLUA_COMPAT_READPATTERN -DLUA_COMPAT_ARGRET
|
||||
# -DLUA_DEPRECATEDFUNCS
|
||||
|
||||
# DEBUG = -g -DLUA_USER_H='"ltests.h"' # -DHARDSTACKTESTS -DEXTERNMEMCHECK
|
||||
OPTIMIZE = -O2 \
|
||||
# -D'lua_number2int(i,d)=__asm__("fldl %1\nfistpl %0":"=m"(i):"m"(d))' \
|
||||
# -fomit-frame-pointer
|
||||
|
||||
|
||||
CONFIG = -D_POSIX_SOURCE $(DEBUG) $(OPTIMIZE) # -DUSE_POPEN
|
||||
|
||||
|
||||
# Compilation parameters
|
||||
CC = gcc
|
||||
CWARNS = -Wall -W -pedantic \
|
||||
CWARNS = -Wall -pedantic \
|
||||
-Waggregate-return \
|
||||
-Wcast-align \
|
||||
-Wmissing-prototypes \
|
||||
-Wstrict-prototypes \
|
||||
-Wnested-externs \
|
||||
-Wpointer-arith \
|
||||
-Wshadow \
|
||||
-Wwrite-strings
|
||||
-Wwrite-strings \
|
||||
# -Wtraditional \
|
||||
# -Wcast-qual
|
||||
# -Wtraditional
|
||||
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O2
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi
|
||||
|
||||
|
||||
# To make early versions
|
||||
@@ -70,16 +66,19 @@ LUAOBJS = \
|
||||
lcode.o \
|
||||
lparser.o \
|
||||
llex.o \
|
||||
lopcodes.o \
|
||||
lundump.o \
|
||||
ldump.o \
|
||||
lzio.o \
|
||||
ldebug.o \
|
||||
ltests.o
|
||||
ltests.o
|
||||
|
||||
LIBOBJS = \
|
||||
lauxlib.o \
|
||||
lbaselib.o \
|
||||
liolib.o \
|
||||
ltablib.o \
|
||||
lmathlib.o \
|
||||
liolib.o \
|
||||
lstrlib.o \
|
||||
ldblib.o
|
||||
|
||||
@@ -111,50 +110,53 @@ clear :
|
||||
co $(CO_OPTIONS) $@
|
||||
|
||||
|
||||
lapi.o: lapi.c lua.h lapi.h lobject.h llimits.h ldo.h lstate.h \
|
||||
luadebug.h lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h lvm.h
|
||||
lauxlib.o: lauxlib.c lua.h lauxlib.h luadebug.h
|
||||
lbaselib.o: lbaselib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
lcode.o: lcode.c /usr/include/stdlib.h lua.h lcode.h llex.h lobject.h \
|
||||
llimits.h lzio.h lopcodes.h lparser.h ldo.h lstate.h luadebug.h \
|
||||
lmem.h
|
||||
ldblib.o: ldblib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
ldebug.o: ldebug.c lua.h lapi.h lobject.h llimits.h lcode.h llex.h \
|
||||
lzio.h lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h \
|
||||
lfunc.h lstring.h ltable.h ltm.h
|
||||
ldo.o: ldo.c lua.h ldebug.h lstate.h lobject.h llimits.h luadebug.h \
|
||||
ldo.h lgc.h lmem.h lparser.h lzio.h lstring.h ltable.h ltm.h \
|
||||
lundump.h lvm.h
|
||||
lfunc.o: lfunc.c lua.h lfunc.h lobject.h llimits.h lmem.h lstate.h \
|
||||
luadebug.h
|
||||
lgc.o: lgc.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h
|
||||
liolib.o: liolib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
llex.o: llex.c lua.h llex.h lobject.h llimits.h lzio.h lmem.h \
|
||||
lparser.h lstate.h luadebug.h lstring.h ltable.h
|
||||
lapi.o: lapi.c lua.h lapi.h lobject.h llimits.h ldebug.h lstate.h ltm.h \
|
||||
lzio.h ldo.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 lzio.h \
|
||||
ldo.h lfunc.h lgc.h lmem.h lopcodes.h lparser.h ltable.h lstring.h \
|
||||
lundump.h lvm.h
|
||||
ldump.o: ldump.c lua.h lobject.h llimits.h lopcodes.h lstate.h ltm.h \
|
||||
lzio.h lundump.h
|
||||
lfunc.o: lfunc.c lua.h lfunc.h lobject.h llimits.h lgc.h lmem.h lstate.h \
|
||||
ltm.h lzio.h
|
||||
lgc.o: lgc.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h lzio.h \
|
||||
ldo.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 ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h
|
||||
lobject.o: lobject.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h
|
||||
lmem.o: lmem.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h lzio.h \
|
||||
ldo.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 lfunc.h lmem.h lstate.h luadebug.h lstring.h
|
||||
lstate.o: lstate.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lgc.h llex.h lzio.h lmem.h lstring.h ltable.h ltm.h
|
||||
lstring.o: lstring.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h lstring.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 \
|
||||
lzio.h ldo.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 \
|
||||
lzio.h lstring.h
|
||||
lstrlib.o: lstrlib.c lua.h lauxlib.h lualib.h
|
||||
ltable.o: ltable.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h lstring.h ltable.h
|
||||
ltable.o: ltable.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
||||
lzio.h ldo.h lgc.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 ldebug.h lstate.h luadebug.h ldo.h \
|
||||
lfunc.h lmem.h lstring.h ltable.h lualib.h
|
||||
ltm.o: ltm.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h ltm.h
|
||||
lua.o: lua.c lua.h luadebug.h lualib.h
|
||||
lundump.o: lundump.c lfunc.h lobject.h llimits.h lua.h lmem.h \
|
||||
lopcodes.h lstring.h lstate.h luadebug.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lua.h lapi.h lobject.h llimits.h ldebug.h lstate.h \
|
||||
luadebug.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h ltm.h \
|
||||
lvm.h
|
||||
lzio.o: lzio.c lua.h lzio.h
|
||||
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 lzio.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 \
|
||||
lzio.h lfunc.h lmem.h lopcodes.h lstring.h lundump.h
|
||||
lvm.o: lvm.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h lzio.h \
|
||||
ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
|
||||
lzio.o: lzio.c lua.h llimits.h lmem.h lzio.h
|
||||
|
||||
5520
manual.tex
5520
manual.tex
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user