mirror of
https://github.com/lua/lua.git
synced 2026-07-27 08:29:08 +00:00
Compare commits
960 Commits
v4.0-alpha
...
v5.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
762c737037 | ||
|
|
de57dc2653 | ||
|
|
d1df829f8d | ||
|
|
2ad947fe03 | ||
|
|
f0cc013afa | ||
|
|
0d88545b82 | ||
|
|
f84c5a5fc6 | ||
|
|
05d35d8f3c | ||
|
|
3a6230ef7c | ||
|
|
a424a112b5 | ||
|
|
b858161fbc | ||
|
|
80bac182db | ||
|
|
30a411ab3c | ||
|
|
5bb5a1831d | ||
|
|
b518d14071 | ||
|
|
15dbb53461 | ||
|
|
abc304f9da | ||
|
|
1a553f9199 | ||
|
|
0ca225d33a | ||
|
|
70e2b7ab86 | ||
|
|
5a144e891a | ||
|
|
b12776f29c | ||
|
|
ab9e062928 | ||
|
|
81cdbabf09 | ||
|
|
40cfb0691e | ||
|
|
9b7af7e45b | ||
|
|
2e66248f8a | ||
|
|
79db75101e | ||
|
|
b0c40e2405 | ||
|
|
4734e2de05 | ||
|
|
7ea180af6e | ||
|
|
05d567d52e | ||
|
|
d8a3a00d84 | ||
|
|
c9ea94ec92 | ||
|
|
fa4b4c1100 | ||
|
|
4039bf457d | ||
|
|
034de1fe73 | ||
|
|
430d6db928 | ||
|
|
7c3857cded | ||
|
|
8ce6f0b418 | ||
|
|
572bddc0b0 | ||
|
|
3684f67771 | ||
|
|
69dd9461e5 | ||
|
|
6b6bc532a4 | ||
|
|
ea16ee41a8 | ||
|
|
60d11ec316 | ||
|
|
e1a424e8a3 | ||
|
|
59ce42dbd5 | ||
|
|
92f6e0c1bf | ||
|
|
5cd99b82b7 | ||
|
|
07e210e655 | ||
|
|
feb724c122 | ||
|
|
67f9a2a98f | ||
|
|
c4f9c887fc | ||
|
|
9866fb0ef1 | ||
|
|
60c83ded30 | ||
|
|
07948c3181 | ||
|
|
dce9a49a08 | ||
|
|
15db58a2f5 | ||
|
|
d6826dbc80 | ||
|
|
e75a6ae9ee | ||
|
|
2866fb2dd0 | ||
|
|
4b6f436d67 | ||
|
|
8f105d6b59 | ||
|
|
2fef8c772b | ||
|
|
7285fa393b | ||
|
|
5085f09ce5 | ||
|
|
d674626392 | ||
|
|
7a40cdbda0 | ||
|
|
3cdeacbbfb | ||
|
|
6f207b15fb | ||
|
|
3184314bf3 | ||
|
|
7964f7cbbd | ||
|
|
383a6e88f9 | ||
|
|
871166d7db | ||
|
|
592cf2334e | ||
|
|
0175f8d5d1 | ||
|
|
e79bf02f33 | ||
|
|
41ed3c4771 | ||
|
|
635b7c707d | ||
|
|
83f0d6ef1a | ||
|
|
3088ebd95b | ||
|
|
797d964df9 | ||
|
|
e44e579dc1 | ||
|
|
a153cafd8d | ||
|
|
3b5158f2a1 | ||
|
|
60a8b94fd0 | ||
|
|
c499442e5f | ||
|
|
9a5bab82ff | ||
|
|
32fd039bb5 | ||
|
|
75f73172c4 | ||
|
|
754c630754 | ||
|
|
dd8edecae1 | ||
|
|
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 | ||
|
|
523c5d8e1c | ||
|
|
40a4c76773 | ||
|
|
1385d81d20 | ||
|
|
906d5dcc41 | ||
|
|
93a5649d40 | ||
|
|
df416661cc | ||
|
|
67c1afff59 | ||
|
|
03770ecfc9 | ||
|
|
7409678b5d | ||
|
|
d1c689af40 | ||
|
|
37e9c2e744 | ||
|
|
e42a219eeb | ||
|
|
b3959d58ff | ||
|
|
f379d06e24 | ||
|
|
728ff20701 | ||
|
|
2cbbf3933a | ||
|
|
e2b6b7de1b | ||
|
|
563b1f5704 | ||
|
|
4670476584 | ||
|
|
89f98c0995 | ||
|
|
b892f0a877 | ||
|
|
aadc35449e | ||
|
|
cdc8139e29 | ||
|
|
e833bd47c9 | ||
|
|
b7ffb128cb | ||
|
|
64eecc0b82 | ||
|
|
8b88ab07f7 | ||
|
|
2779ceeb12 | ||
|
|
e93c4547fe | ||
|
|
0ffc676ce7 | ||
|
|
18fb3ddb89 | ||
|
|
2bddbe6603 | ||
|
|
e323338fd0 | ||
|
|
46b543ebef | ||
|
|
79909a92e1 | ||
|
|
d6232a0b2e | ||
|
|
ae63a0e692 | ||
|
|
cd3d446957 | ||
|
|
f01e6c6f1d | ||
|
|
ad3816d0d1 | ||
|
|
046a3d6173 | ||
|
|
001f2bdd0e | ||
|
|
cd2ddaded9 | ||
|
|
d68209e822 | ||
|
|
1088cde03c | ||
|
|
6759f3ec5e | ||
|
|
f6834f4393 | ||
|
|
78bc8e553d | ||
|
|
dad808a73a | ||
|
|
ca7fd50a4e | ||
|
|
282ab366f4 | ||
|
|
444d6a106b | ||
|
|
13635f7de7 | ||
|
|
d8a442206d | ||
|
|
c9c6f9747c | ||
|
|
c2aa7bd72d | ||
|
|
f9dd50cefc | ||
|
|
dbde9f8817 | ||
|
|
857a1204d8 | ||
|
|
d742b88fa1 | ||
|
|
eb822c314a | ||
|
|
ab7aceb980 | ||
|
|
72b12e0956 | ||
|
|
5ed3bcd4ea | ||
|
|
7508fed673 | ||
|
|
aab4fde468 | ||
|
|
6ec4a0ef76 | ||
|
|
42ca3105ac | ||
|
|
6030d5fed4 | ||
|
|
91c003dcc2 | ||
|
|
620d22f2a0 | ||
|
|
f45cba42b5 | ||
|
|
2628a42480 | ||
|
|
0b3b6850c9 | ||
|
|
5d033f33eb | ||
|
|
e143fed484 | ||
|
|
01ce1ce48c | ||
|
|
024ed4218f | ||
|
|
c1f725ba4a | ||
|
|
5d69960734 | ||
|
|
d1c351857d | ||
|
|
4ef14ba280 | ||
|
|
b8d57092eb | ||
|
|
0f59e3d7a7 | ||
|
|
55c3bc2bcb | ||
|
|
8060193702 | ||
|
|
2779e81fbb | ||
|
|
30f6e658d2 | ||
|
|
787a78f83e | ||
|
|
70c8a31092 | ||
|
|
2018380e9f | ||
|
|
6e80c1cde1 | ||
|
|
f67f324377 | ||
|
|
eebc9729e4 | ||
|
|
6990b06467 | ||
|
|
cad91499dd | ||
|
|
ebdd4e8451 | ||
|
|
9a21e81907 | ||
|
|
f0b3cd1d6f | ||
|
|
fb5e6d5ac4 | ||
|
|
d63afba9d3 | ||
|
|
100bfec39a | ||
|
|
a290b84c67 | ||
|
|
1022b3c85e | ||
|
|
a1d3e001b9 | ||
|
|
10b4799ce5 | ||
|
|
a0de89d62a | ||
|
|
21dc77b2af | ||
|
|
2d3ebba537 | ||
|
|
a97f29f154 | ||
|
|
4135f4f586 | ||
|
|
808525e417 | ||
|
|
48a968e6b5 | ||
|
|
58adbde216 | ||
|
|
9e74ca62d7 | ||
|
|
d396562b5e | ||
|
|
9d60598260 | ||
|
|
4e56c0d514 | ||
|
|
ac12f4db4b | ||
|
|
b691d4344b | ||
|
|
9fdf73bc9a | ||
|
|
f1fd9b5c2c | ||
|
|
3723d790ae | ||
|
|
b800c38b69 | ||
|
|
5c0e79847c | ||
|
|
e662e0f1cd | ||
|
|
c85162be27 | ||
|
|
bd39db46ed | ||
|
|
90fb2e18e8 | ||
|
|
4365c31c89 | ||
|
|
44eb7d91ac | ||
|
|
ddc8d94a08 | ||
|
|
5d9cbdadfb | ||
|
|
58453dc1e1 | ||
|
|
f555e493f0 | ||
|
|
d1ffd1a1c5 | ||
|
|
9e84bf18db | ||
|
|
daf09c476f | ||
|
|
e238efc536 | ||
|
|
fdd04e7a7f | ||
|
|
fb55c96706 | ||
|
|
c39bec81dc | ||
|
|
5f22f8961c | ||
|
|
a7c1390ffa | ||
|
|
0802a9df9e | ||
|
|
f90bc248b3 | ||
|
|
d9e61e8cea | ||
|
|
397905ef86 | ||
|
|
435f587ed0 | ||
|
|
ae55f3eead | ||
|
|
cfba572076 | ||
|
|
aa01d2568d | ||
|
|
014a09c509 | ||
|
|
b622282973 | ||
|
|
7f867eb0b9 | ||
|
|
5362426ffa | ||
|
|
906434011f | ||
|
|
c07cebbbf6 | ||
|
|
afef009fce | ||
|
|
b69e712713 | ||
|
|
f517759507 | ||
|
|
52062684e1 | ||
|
|
83e3ec0117 | ||
|
|
feb2083730 | ||
|
|
8d2baf719c | ||
|
|
88657809ac | ||
|
|
2e1b32d873 | ||
|
|
89a3ec08f3 | ||
|
|
e3ea307434 | ||
|
|
18b0e8270d | ||
|
|
094880ef9e | ||
|
|
1de5587184 | ||
|
|
8ca9534d04 | ||
|
|
8bcf622876 | ||
|
|
dbbf6c073b | ||
|
|
292c953018 | ||
|
|
c542aac0b9 | ||
|
|
dbfb810267 | ||
|
|
9c14b5e416 | ||
|
|
6b9bf49265 | ||
|
|
989ad7232a | ||
|
|
190c3be739 | ||
|
|
7e30900def | ||
|
|
f63d7753b8 | ||
|
|
50a82ec1b9 | ||
|
|
ed1751bc32 | ||
|
|
8e617985fa | ||
|
|
c39345fba3 | ||
|
|
ed94a85f0c | ||
|
|
58fbdc76d5 | ||
|
|
a301304612 | ||
|
|
677313da32 | ||
|
|
ef62b340e0 | ||
|
|
5c2dd7a9e0 | ||
|
|
93d93a0bfb | ||
|
|
9e1f94fc1c | ||
|
|
8714cc02d6 | ||
|
|
69b45bb4e9 | ||
|
|
843f84f4ce | ||
|
|
b803c0600e | ||
|
|
8ac0bbf64b | ||
|
|
dc1e4f5073 | ||
|
|
330e51bed3 | ||
|
|
44b71ca816 | ||
|
|
bad6365540 | ||
|
|
91f34fb05c | ||
|
|
11a7022067 | ||
|
|
35a6ed2838 | ||
|
|
cfe6ca819b | ||
|
|
fe5c37ae95 | ||
|
|
c31f02948f | ||
|
|
c9f91c6fe3 | ||
|
|
66d620674c | ||
|
|
84790bb953 | ||
|
|
534c3a64d3 |
164
bugs
164
bugs
@@ -170,3 +170,167 @@ in the following platforms:
|
||||
* IRIX - cc, cc-purify
|
||||
* Windows - Visual C++ (.c e .cpp, warning level=4)
|
||||
|
||||
|
||||
** lstrlib.c
|
||||
Tue May 2 15:27:58 EST 2000
|
||||
>> `strfind' gets wrong subject length when there is an offset
|
||||
(by Jon Kleiser; since 4.0a)
|
||||
|
||||
** lparser.c
|
||||
Fri May 12 15:11:12 EST 2000
|
||||
>> first element in a list constructor is not adjusted to one value
|
||||
>> (e.g. «a = {gsub('a','a','')}»)
|
||||
(by Tomas; since 4.0a)
|
||||
|
||||
** lparser.c
|
||||
Wed May 24 14:50:16 EST 2000
|
||||
>> record-constructor starting with an upvalue name gets an error
|
||||
>> (e.g. «local a; function f() x = {a=1} end»)
|
||||
(by Edgar Toernig; since 3.1)
|
||||
|
||||
** lparser.c
|
||||
Tue Aug 29 15:56:05 EST 2000
|
||||
>> error message for `for' uses `while'
|
||||
(since 4.0a; already corrected)
|
||||
|
||||
** lgc.c
|
||||
Tue Aug 29 15:57:41 EST 2000
|
||||
>> gc tag method for nil could call line hook
|
||||
(by ry; since ?)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0 Beta
|
||||
|
||||
** liolib.c
|
||||
Fri Sep 22 15:12:37 EST 2000
|
||||
>> `read("*w")' should return nil at EOF
|
||||
(by roberto; since 4.0b)
|
||||
|
||||
** lvm.c
|
||||
Mon Sep 25 11:47:48 EST 2000
|
||||
>> lua_gettable does not get key from stack top
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lgc.c
|
||||
Mon Sep 25 11:50:48 EST 2000
|
||||
>> GC may crash when checking locked C closures
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lapi.c
|
||||
Wed Sep 27 09:50:19 EST 2000
|
||||
>> lua_tag should return LUA_NOTAG for non-valid indices
|
||||
(by Paul Hankin; since 4.0b)
|
||||
|
||||
** llex.h / llex.c / lparser.c
|
||||
Wed Sep 27 13:39:45 EST 2000
|
||||
>> parser overwrites semantic information when looking ahead
|
||||
>> (e.g. «a = {print'foo'}»)
|
||||
(by Edgar Toernig; since 4.0b, deriving from previous bug)
|
||||
|
||||
** liolib.c
|
||||
Thu Oct 26 10:50:46 EDT 2000
|
||||
>> in function `read_file', realloc() doesn't free the buffer if it can't
|
||||
>> allocate new memory
|
||||
(by Mauro Vezzosi; since 4.0b)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0
|
||||
|
||||
** lparser.c
|
||||
Wed Nov 29 09:51:44 EDT 2000
|
||||
>> parser does not accept a `;' after a `return'
|
||||
(by lhf; since 4.0b)
|
||||
|
||||
** liolib.c
|
||||
Fri Dec 22 15:30:42 EDT 2000
|
||||
>> when `read' fails it must return nil (and not no value)
|
||||
(by cassino; since at least 3.1)
|
||||
|
||||
** lstring.c/lapi.c
|
||||
Thu Feb 1 11:55:45 EDT 2001
|
||||
>> lua_pushuserdata(L, NULL) is buggy
|
||||
(by Edgar Toernig; since 4.0)
|
||||
|
||||
** ldo.c
|
||||
Fri Feb 2 14:06:40 EDT 2001
|
||||
>> «while 1 dostring[[print('hello\n')]] end» never reclaims memory
|
||||
(by Andrew Paton; since 4.0b)
|
||||
|
||||
** lbaselib.c
|
||||
Tue Feb 6 11:57:13 EDT 2001
|
||||
>> ESC (which starts precompiled code) in C is \33, not \27
|
||||
(by Edgar Toernig and lhf; since 4.0b)
|
||||
|
||||
** lparser.c
|
||||
Tue Jul 10 16:59:18 EST 2001
|
||||
>> error message for `%a' gave wrong line number
|
||||
(by Leonardo Constantino; since 4.0)
|
||||
|
||||
** lbaselib.c
|
||||
Fri Dec 21 15:21:05 EDT 2001
|
||||
>> seg. fault when rawget/rawset get extra arguments
|
||||
(by Eric Mauger; since 4.0b)
|
||||
|
||||
** lvm.c
|
||||
Wed Jun 19 13:28:20 EST 2002
|
||||
>> line hook gets wrong `ar'
|
||||
(by Daniel C. Sinclair; since 4.0.b)
|
||||
|
||||
** ldo.c
|
||||
Wed Jun 19 13:31:49 EST 2002
|
||||
>> `protectedparser' may run GC, and then collect `filename'
|
||||
>> (in function `parse_file')
|
||||
(by Alex Bilyk; since 4.0)
|
||||
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- 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)
|
||||
|
||||
** lapi.c
|
||||
Thu Nov 21 11:00:00 EST 2002
|
||||
>> ULONG_MAX>>10 may not fit into an int
|
||||
(by Jeff Petkau; since 4.0)
|
||||
|
||||
** lparser.c
|
||||
Fri Dec 6 17:06:40 UTC 2002
|
||||
>> scope of generic for variables is not sound
|
||||
(by Gavin Wraith; since 5.0a)
|
||||
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 5.0 beta
|
||||
** lbaselib.c
|
||||
Fri Dec 20 09:53:19 UTC 2002
|
||||
>> `resume' was checking the wrong value for stack overflow
|
||||
(by Maik Zimmermann; since 5.0b)
|
||||
|
||||
** ldo.c
|
||||
Thu Jan 23 11:29:06 UTC 2003
|
||||
>> error during garbage collection in luaD_protectedparser is not being
|
||||
>> protected
|
||||
(by Benoit Germain; since 5.0a)
|
||||
|
||||
** ldo.c (and others)
|
||||
Fri Feb 28 14:20:33 EST 2003
|
||||
>> GC metamethod calls could mess C/Lua stack syncronization
|
||||
(by Roberto; since 5.0b)
|
||||
|
||||
** lzio.h/zlio.c
|
||||
Thu Mar 20 11:40:12 EST 2003
|
||||
>> zio mixes a 255 as first char in a buffer with EOZ
|
||||
(by lhf; since 5.0a)
|
||||
|
||||
|
||||
7
lapi.h
7
lapi.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.h,v 1.15 2000/03/10 18:37:44 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,11 +11,6 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaA_checkCargs (lua_State *L, int nargs);
|
||||
void luaA_pushobject (lua_State *L, const TObject *o);
|
||||
GlobalVar *luaA_nextvar (lua_State *L, TString *g);
|
||||
int luaA_next (lua_State *L, const Hash *t, int i);
|
||||
lua_Object luaA_putluaObject (lua_State *L, const TObject *o);
|
||||
lua_Object luaA_putObjectOnTop (lua_State *L);
|
||||
|
||||
#endif
|
||||
|
||||
620
lauxlib.c
620
lauxlib.c
@@ -1,28 +1,103 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.26 2000/02/08 16:34:31 roberto Exp roberto $
|
||||
** $Id: lauxlib.c,v 1.99 2003/04/03 13:35:34 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 LUA_REENTRANT
|
||||
#define lauxlib_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
/* number of prereserved references (for internal use) */
|
||||
#define RESERVED_REFS 2
|
||||
|
||||
int luaL_findstring (const char *name, const char *const list[]) {
|
||||
/* reserved references */
|
||||
#define FREELIST_REF 1 /* free list of references */
|
||||
#define ARRAYSIZE_REF 2 /* array sizes */
|
||||
|
||||
|
||||
/* convert a stack index to positive */
|
||||
#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
|
||||
lua_gettop(L) + (i) + 1)
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** 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 (%s)", ar.name, extramsg);
|
||||
}
|
||||
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;
|
||||
for (i=0; list[i]; i++)
|
||||
if (strcmp(list[i], name) == 0)
|
||||
@@ -30,122 +105,487 @@ int luaL_findstring (const char *name, const char *const list[]) {
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
|
||||
lua_Debug ar;
|
||||
lua_getstack(L, 0, &ar);
|
||||
lua_getinfo(L, "nu", &ar);
|
||||
narg -= ar.nups;
|
||||
if (ar.name == NULL)
|
||||
ar.name = "?";
|
||||
luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)",
|
||||
narg, ar.name, extramsg);
|
||||
|
||||
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
|
||||
lua_pushstring(L, tname);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX); /* get registry.name */
|
||||
if (!lua_isnil(L, -1)) /* name already in use? */
|
||||
return 0; /* leave previous value on top, but return 0 */
|
||||
lua_pop(L, 1);
|
||||
lua_newtable(L); /* create metatable */
|
||||
lua_pushstring(L, tname);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX); /* registry.name = metatable */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_pushstring(L, tname);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void type_error (lua_State *L, int narg, const char *type_name,
|
||||
lua_Object o) {
|
||||
char buff[100];
|
||||
const char *otype = (o == LUA_NOOBJECT) ? "no value" : lua_type(L, o);
|
||||
sprintf(buff, "%.10s expected, got %.10s", type_name, otype);
|
||||
luaL_argerror(L, narg, buff);
|
||||
LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) {
|
||||
lua_pushstring(L, tname);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
}
|
||||
|
||||
|
||||
static const char *checkstr (lua_State *L, lua_Object o, int narg, long *len) {
|
||||
const char *s = lua_getstring(L, o);
|
||||
if (!s) type_error(L, narg, "string", o);
|
||||
if (len) *len = lua_strlen(L, o);
|
||||
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
|
||||
const char *tn;
|
||||
if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */
|
||||
lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */
|
||||
tn = lua_tostring(L, -1);
|
||||
if (tn && (strcmp(tn, tname) == 0)) {
|
||||
lua_pop(L, 1);
|
||||
return lua_touserdata(L, ud);
|
||||
}
|
||||
else {
|
||||
lua_pop(L, 1);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *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) {
|
||||
if (lua_type(L, narg) != t)
|
||||
tag_error(L, narg, t);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkany (lua_State *L, int narg) {
|
||||
if (lua_type(L, narg) == LUA_TNONE)
|
||||
luaL_argerror(L, narg, "value expected");
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
|
||||
const char *s = lua_tostring(L, narg);
|
||||
if (!s) tag_error(L, narg, LUA_TSTRING);
|
||||
if (len) *len = lua_strlen(L, narg);
|
||||
return s;
|
||||
}
|
||||
|
||||
const char *luaL_check_lstr (lua_State *L, int narg, long *len) {
|
||||
return checkstr(L, lua_getparam(L, narg), narg, len);
|
||||
}
|
||||
|
||||
const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, long *len) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (o == LUA_NOOBJECT) {
|
||||
if (len) *len = def ? strlen(def) : 0;
|
||||
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 checkstr(L, o, narg, len);
|
||||
}
|
||||
|
||||
double luaL_check_number (lua_State *L, int narg) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
|
||||
return lua_getnumber(L, o);
|
||||
else return luaL_checklstring(L, narg, len);
|
||||
}
|
||||
|
||||
|
||||
double luaL_opt_number (lua_State *L, int narg, double def) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (o == LUA_NOOBJECT) return def;
|
||||
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 */
|
||||
tag_error(L, narg, LUA_TNUMBER);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
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 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;
|
||||
}
|
||||
else {
|
||||
if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
|
||||
return lua_getnumber(L, o);
|
||||
lua_remove(L, -2); /* remove only metatable */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
lua_Object luaL_tablearg (lua_State *L, int narg) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (!lua_istable(L, o)) type_error(L, narg, "table", o);
|
||||
return o;
|
||||
}
|
||||
|
||||
lua_Object luaL_functionarg (lua_State *L, int narg) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
if (!lua_isfunction(L, o)) type_error(L, narg, "function", o);
|
||||
return o;
|
||||
}
|
||||
|
||||
lua_Object luaL_nonnullarg (lua_State *L, int narg) {
|
||||
lua_Object o = lua_getparam(L, narg);
|
||||
luaL_arg_check(L, o != LUA_NOOBJECT, narg, "value expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
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_callmeta (lua_State *L, int obj, const char *event) {
|
||||
obj = abs_index(L, obj);
|
||||
if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
|
||||
return 0;
|
||||
lua_pushvalue(L, obj);
|
||||
lua_call(L, 1, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
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 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_pushstring(L, libname);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, LUA_GLOBALSINDEX); /* register it with given name */
|
||||
}
|
||||
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 */
|
||||
}
|
||||
|
||||
|
||||
#define EXTRALEN sizeof("string \"...\"0")
|
||||
|
||||
void luaL_chunkid (char *out, const char *source, int len) {
|
||||
if (*source == '(') {
|
||||
strncpy(out, source+1, len-1); /* remove first char */
|
||||
out[len-1] = '\0'; /* make sure `out' has an end */
|
||||
out[strlen(out)-1] = '\0'; /* remove last char */
|
||||
/*
|
||||
** {======================================================
|
||||
** getn-setn: size for arrays
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static int checkint (lua_State *L, int topop) {
|
||||
int n = (int)lua_tonumber(L, -1);
|
||||
if (n == 0 && !lua_isnumber(L, -1)) n = -1;
|
||||
lua_pop(L, topop);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
static void getsizes (lua_State *L) {
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF);
|
||||
if (lua_isnil(L, -1)) { /* no `size' table? */
|
||||
lua_pop(L, 1); /* remove nil */
|
||||
lua_newtable(L); /* create it */
|
||||
lua_pushvalue(L, -1); /* `size' 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" */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_rawseti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF); /* store in register */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaL_setn (lua_State *L, int t, int n) {
|
||||
t = abs_index(L, t);
|
||||
lua_pushliteral(L, "n");
|
||||
lua_rawget(L, t);
|
||||
if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
|
||||
lua_pushliteral(L, "n"); /* use it */
|
||||
lua_pushnumber(L, (lua_Number)n);
|
||||
lua_rawset(L, t);
|
||||
}
|
||||
else { /* use `sizes' */
|
||||
getsizes(L);
|
||||
lua_pushvalue(L, t);
|
||||
lua_pushnumber(L, (lua_Number)n);
|
||||
lua_rawset(L, -3); /* sizes[t] = n */
|
||||
lua_pop(L, 1); /* remove `sizes' */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaL_getn (lua_State *L, int t) {
|
||||
int n;
|
||||
t = abs_index(L, t);
|
||||
lua_pushliteral(L, "n"); /* try t.n */
|
||||
lua_rawget(L, t);
|
||||
if ((n = checkint(L, 1)) >= 0) return n;
|
||||
getsizes(L); /* else try sizes[t] */
|
||||
lua_pushvalue(L, t);
|
||||
lua_rawget(L, -2);
|
||||
if ((n = checkint(L, 2)) >= 0) return n;
|
||||
for (n = 1; ; n++) { /* else must count elements */
|
||||
lua_rawgeti(L, t, n);
|
||||
if (lua_isnil(L, -1)) break;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
return n - 1;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
#define bufflen(B) ((B)->p - (B)->buffer)
|
||||
#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
|
||||
|
||||
#define LIMIT (LUA_MINSTACK/2)
|
||||
|
||||
|
||||
static int emptybuffer (luaL_Buffer *B) {
|
||||
size_t l = bufflen(B);
|
||||
if (l == 0) return 0; /* put nothing on stack */
|
||||
else {
|
||||
lua_pushlstring(B->L, B->buffer, l);
|
||||
B->p = B->buffer;
|
||||
B->lvl++;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjuststack (luaL_Buffer *B) {
|
||||
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->lvl - toget + 1 >= LIMIT || toplen > l) {
|
||||
toplen += l;
|
||||
toget++;
|
||||
}
|
||||
else break;
|
||||
} while (toget < B->lvl);
|
||||
lua_concat(L, toget);
|
||||
B->lvl = B->lvl - toget + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
|
||||
if (emptybuffer(B))
|
||||
adjuststack(B);
|
||||
return B->buffer;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
|
||||
while (l--)
|
||||
luaL_putchar(B, *s++);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
|
||||
luaL_addlstring(B, s, strlen(s));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
||||
emptybuffer(B);
|
||||
lua_concat(B->L, B->lvl);
|
||||
B->lvl = 1;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
|
||||
lua_State *L = B->L;
|
||||
size_t vl = lua_strlen(L, -1);
|
||||
if (vl <= bufffree(B)) { /* fit into buffer? */
|
||||
memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
|
||||
B->p += vl;
|
||||
lua_pop(L, 1); /* remove from stack */
|
||||
}
|
||||
else {
|
||||
len -= EXTRALEN;
|
||||
if (*source == '@')
|
||||
sprintf(out, "file `%.*s'", len, source+1);
|
||||
else {
|
||||
const char *b = strchr(source , '\n'); /* stop at first new line */
|
||||
int lim = (b && (b-source)<len) ? b-source : len;
|
||||
sprintf(out, "string \"%.*s\"", lim, source);
|
||||
strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
|
||||
if (emptybuffer(B))
|
||||
lua_insert(L, -2); /* put buffer before new value */
|
||||
B->lvl++; /* add new value into B stack */
|
||||
adjuststack(B);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
|
||||
B->L = L;
|
||||
B->p = B->buffer;
|
||||
B->lvl = 0;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
int ref;
|
||||
t = abs_index(L, t);
|
||||
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, FREELIST_REF); /* get first free element */
|
||||
ref = (int)lua_tonumber(L, -1); /* ref = t[FREELIST_REF] */
|
||||
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, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
|
||||
}
|
||||
else { /* no free elements */
|
||||
ref = luaL_getn(L, t);
|
||||
if (ref < RESERVED_REFS)
|
||||
ref = RESERVED_REFS; /* skip reserved references */
|
||||
ref++; /* create new reference */
|
||||
luaL_setn(L, t, ref);
|
||||
}
|
||||
lua_rawseti(L, t, ref);
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
||||
if (ref >= 0) {
|
||||
t = abs_index(L, t);
|
||||
lua_rawgeti(L, t, FREELIST_REF);
|
||||
lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
|
||||
lua_pushnumber(L, (lua_Number)ref);
|
||||
lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = 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 */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaL_filesource (char *out, const char *filename, int len) {
|
||||
if (filename == NULL) filename = "(stdin)";
|
||||
sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */
|
||||
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);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
174
lauxlib.h
174
lauxlib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.16 1999/12/03 11:26:23 roberto Exp roberto $
|
||||
** $Id: lauxlib.h,v 1.59 2003/03/18 12:25:32 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,39 +9,60 @@
|
||||
#define lauxlib_h
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
struct luaL_reg {
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API LUA_API
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
typedef struct luaL_reg {
|
||||
const char *name;
|
||||
lua_CFunction func;
|
||||
};
|
||||
} luaL_reg;
|
||||
|
||||
|
||||
void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
|
||||
void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
|
||||
const char *luaL_check_lstr (lua_State *L, int numArg, long *len);
|
||||
const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def, long *len);
|
||||
double luaL_check_number (lua_State *L, int numArg);
|
||||
double luaL_opt_number (lua_State *L, int numArg, double def);
|
||||
lua_Object luaL_functionarg (lua_State *L, int arg);
|
||||
lua_Object luaL_tablearg (lua_State *L, int arg);
|
||||
lua_Object luaL_nonnullarg (lua_State *L, int numArg);
|
||||
void luaL_verror (lua_State *L, const char *fmt, ...);
|
||||
char *luaL_openspace (lua_State *L, int size);
|
||||
void luaL_resetbuffer (lua_State *L);
|
||||
void luaL_addchar (lua_State *L, int c);
|
||||
int luaL_getsize (lua_State *L);
|
||||
void luaL_addsize (lua_State *L, int n);
|
||||
int luaL_newbuffer (lua_State *L, int size);
|
||||
void luaL_oldbuffer (lua_State *L, int old);
|
||||
char *luaL_buffer (lua_State *L);
|
||||
int luaL_findstring (const char *name, const char *const list[]);
|
||||
void luaL_chunkid (char *out, const char *source, int len);
|
||||
void luaL_filesource (char *out, const char *filename, int len);
|
||||
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 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 int luaL_newmetatable (lua_State *L, const char *tname);
|
||||
LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname);
|
||||
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname);
|
||||
|
||||
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_getn (lua_State *L, int t);
|
||||
LUALIB_API void luaL_setn (lua_State *L, int t, int n);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
#ifdef LUA_REENTRANT
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
@@ -49,57 +70,76 @@ void luaL_filesource (char *out, const char *filename, int len);
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#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])))
|
||||
|
||||
#else
|
||||
#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,(lua_Number)(d)))
|
||||
#define luaL_optlong(L,n,d) ((long)luaL_optnumber(L, n,(lua_Number)(d)))
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** Macros for single-state use
|
||||
** ===============================================================
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define luaL_arg_check(cond,numarg,extramsg) if (!(cond)) \
|
||||
luaL_argerror(numarg,extramsg)
|
||||
#define luaL_check_string(n) (luaL_check_lstr((n), NULL))
|
||||
#define luaL_opt_string(n, d) (luaL_opt_lstr((n), (d), NULL))
|
||||
#define luaL_check_int(n) ((int)luaL_check_number(n))
|
||||
#define luaL_check_long(n) ((long)luaL_check_number(n))
|
||||
#define luaL_opt_int(n,d) ((int)luaL_opt_number(n,d))
|
||||
#define luaL_opt_long(n,d) ((long)luaL_opt_number(n,d))
|
||||
#define luaL_openl(a) luaL_openlib(a, (sizeof(a)/sizeof(a[0])))
|
||||
|
||||
#ifndef LUAL_BUFFERSIZE
|
||||
#define LUAL_BUFFERSIZE BUFSIZ
|
||||
#endif
|
||||
|
||||
|
||||
#define luaL_openlib(l,n) (luaL_openlib)(lua_state,l,n)
|
||||
#define luaL_argerror(numarg,extramsg) \
|
||||
(luaL_argerror)(lua_state,numarg,extramsg)
|
||||
#define luaL_check_lstr(numArg,len) (luaL_check_lstr)(lua_state,numArg,len)
|
||||
#define luaL_opt_lstr(numArg,def,len) \
|
||||
(luaL_opt_lstr)(lua_state,numArg,def,len)
|
||||
#define luaL_check_number(numArg) (luaL_check_number)(lua_state,numArg)
|
||||
#define luaL_opt_number(numArg,def) (luaL_opt_number)(lua_state,numArg,def)
|
||||
#define luaL_functionarg(arg) (luaL_functionarg)(lua_state,arg)
|
||||
#define luaL_tablearg(arg) (luaL_tablearg)(lua_state,arg)
|
||||
#define luaL_nonnullarg(numArg) (luaL_nonnullarg)(lua_state,numArg)
|
||||
#define luaL_openspace(size) (luaL_openspace)(lua_state,size)
|
||||
#define luaL_resetbuffer() (luaL_resetbuffer)(lua_state)
|
||||
#define luaL_addchar(c) (luaL_addchar)(lua_state,c)
|
||||
#define luaL_getsize() (luaL_getsize)(lua_state)
|
||||
#define luaL_addsize(n) (luaL_addsize)(lua_state,n)
|
||||
#define luaL_newbuffer(size) (luaL_newbuffer)(lua_state,size)
|
||||
#define luaL_oldbuffer(old) (luaL_oldbuffer)(lua_state,old)
|
||||
#define luaL_buffer() (luaL_buffer)(lua_state)
|
||||
typedef struct luaL_Buffer {
|
||||
char *p; /* current position in buffer */
|
||||
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)), \
|
||||
(*(B)->p++ = (char)(c)))
|
||||
|
||||
#define luaL_addsize(B,n) ((B)->p += (n))
|
||||
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B);
|
||||
LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B);
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
|
||||
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s);
|
||||
LUALIB_API void luaL_addvalue (luaL_Buffer *B);
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B);
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Compatibility macros and functions
|
||||
*/
|
||||
|
||||
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
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
674
lbaselib.c
Normal file
674
lbaselib.c
Normal file
@@ -0,0 +1,674 @@
|
||||
/*
|
||||
** $Id: lbaselib.c,v 1.129 2003/03/19 21:14:34 roberto Exp roberto $
|
||||
** Basic library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lbaselib_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stdout', you can just remove this function.
|
||||
** If you need, you can define your own `print' function, following this
|
||||
** model but changing `fputs' to put the strings at a proper place
|
||||
** (a console window or a log file, for instance).
|
||||
*/
|
||||
static int luaB_print (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
int i;
|
||||
lua_getglobal(L, "tostring");
|
||||
for (i=1; i<=n; i++) {
|
||||
const char *s;
|
||||
lua_pushvalue(L, -1); /* function to be called */
|
||||
lua_pushvalue(L, i); /* value to print */
|
||||
lua_call(L, 1, 1);
|
||||
s = lua_tostring(L, -1); /* get result */
|
||||
if (s == NULL)
|
||||
return luaL_error(L, "`tostring' must return a string to `print'");
|
||||
if (i>1) fputs("\t", stdout);
|
||||
fputs(s, stdout);
|
||||
lua_pop(L, 1); /* pop result */
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tonumber (lua_State *L) {
|
||||
int base = luaL_optint(L, 2, 10);
|
||||
if (base == 10) { /* standard conversion */
|
||||
luaL_checkany(L, 1);
|
||||
if (lua_isnumber(L, 1)) {
|
||||
lua_pushnumber(L, lua_tonumber(L, 1));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const char *s1 = luaL_checkstring(L, 1);
|
||||
char *s2;
|
||||
unsigned long n;
|
||||
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
n = strtoul(s1, &s2, base);
|
||||
if (s1 != s2) { /* at least one valid digit? */
|
||||
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
|
||||
if (*s2 == '\0') { /* no invalid trailing characters? */
|
||||
lua_pushnumber(L, (lua_Number)n);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_pushnil(L); /* else not a number */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_error (lua_State *L) {
|
||||
int level = luaL_optint(L, 2, 1);
|
||||
luaL_checkany(L, 1);
|
||||
if (!lua_isstring(L, 1) || level == 0)
|
||||
lua_pushvalue(L, 1); /* propagate error message without changes */
|
||||
else { /* add extra information */
|
||||
luaL_where(L, level);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_concat(L, 2);
|
||||
}
|
||||
return lua_error(L);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getmetatable (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
if (!lua_getmetatable(L, 1)) {
|
||||
lua_pushnil(L);
|
||||
return 1; /* no metatable */
|
||||
}
|
||||
luaL_getmetafield(L, 1, "__metatable");
|
||||
return 1; /* returns either __metatable field (if present) or metatable */
|
||||
}
|
||||
|
||||
|
||||
static int luaB_setmetatable (lua_State *L) {
|
||||
int t = lua_type(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
|
||||
"nil or table expected");
|
||||
if (luaL_getmetafield(L, 1, "__metatable"))
|
||||
luaL_error(L, "cannot change a protected metatable");
|
||||
lua_settop(L, 2);
|
||||
lua_setmetatable(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void getfunc (lua_State *L) {
|
||||
if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
|
||||
else {
|
||||
lua_Debug ar;
|
||||
int level = luaL_optint(L, 1, 1);
|
||||
luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
|
||||
if (lua_getstack(L, level, &ar) == 0)
|
||||
luaL_argerror(L, 1, "invalid level");
|
||||
lua_getinfo(L, "f", &ar);
|
||||
if (lua_isnil(L, -1))
|
||||
luaL_error(L, "no function environment for tail call at level %d",
|
||||
level);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int aux_getfenv (lua_State *L) {
|
||||
lua_getfenv(L, -1);
|
||||
lua_pushliteral(L, "__fenv");
|
||||
lua_rawget(L, -2);
|
||||
return !lua_isnil(L, -1);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getfenv (lua_State *L) {
|
||||
getfunc(L);
|
||||
if (!aux_getfenv(L)) /* __fenv not defined? */
|
||||
lua_pop(L, 1); /* remove it, to return real environment */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_setfenv (lua_State *L) {
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
getfunc(L);
|
||||
if (aux_getfenv(L)) /* __fenv defined? */
|
||||
luaL_error(L, "`setfenv' cannot change a protected environment");
|
||||
else
|
||||
lua_pop(L, 2); /* remove __fenv and real environment table */
|
||||
lua_pushvalue(L, 2);
|
||||
if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0)
|
||||
lua_replace(L, LUA_GLOBALSINDEX);
|
||||
else if (lua_setfenv(L, -2) == 0)
|
||||
luaL_error(L, "`setfenv' cannot change environment of given function");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_rawequal (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
luaL_checkany(L, 2);
|
||||
lua_pushboolean(L, lua_rawequal(L, 1, 2));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_rawget (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checkany(L, 2);
|
||||
lua_rawget(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_rawset (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checkany(L, 2);
|
||||
luaL_checkany(L, 3);
|
||||
lua_rawset(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_gcinfo (lua_State *L) {
|
||||
lua_pushnumber(L, (lua_Number)lua_getgccount(L));
|
||||
lua_pushnumber(L, (lua_Number)lua_getgcthreshold(L));
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_collectgarbage (lua_State *L) {
|
||||
lua_setgcthreshold(L, luaL_optint(L, 1, 0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_type (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_next (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
|
||||
if (lua_next(L, 1))
|
||||
return 2;
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_pairs (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushliteral(L, "next");
|
||||
lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushnil(L); /* and initial value */
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_ipairs (lua_State *L) {
|
||||
lua_Number i = lua_tonumber(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
if (i == 0 && lua_isnone(L, 2)) { /* `for' start? */
|
||||
lua_pushliteral(L, "ipairs");
|
||||
lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushnumber(L, 0); /* and initial value */
|
||||
return 3;
|
||||
}
|
||||
else { /* `for' step */
|
||||
i++; /* next value */
|
||||
lua_pushnumber(L, i);
|
||||
lua_rawgeti(L, 1, (int)i);
|
||||
return (lua_isnil(L, -1)) ? 0 : 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int load_aux (lua_State *L, int status) {
|
||||
if (status == 0) /* OK? */
|
||||
return 1;
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
lua_insert(L, -2); /* put before error message */
|
||||
return 2; /* return nil plus error message */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadstring (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
const char *chunkname = luaL_optstring(L, 2, s);
|
||||
return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadfile (lua_State *L) {
|
||||
const char *fname = luaL_optstring(L, 1, NULL);
|
||||
return load_aux(L, luaL_loadfile(L, fname));
|
||||
}
|
||||
|
||||
|
||||
static int luaB_dofile (lua_State *L) {
|
||||
const char *fname = luaL_optstring(L, 1, NULL);
|
||||
int status = luaL_loadfile(L, fname);
|
||||
if (status != 0) lua_error(L);
|
||||
lua_call(L, 0, LUA_MULTRET);
|
||||
return lua_gettop(L) - 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_assert (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
if (!lua_toboolean(L, 1))
|
||||
return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
|
||||
lua_settop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_unpack (lua_State *L) {
|
||||
int n, i;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = luaL_getn(L, 1);
|
||||
luaL_checkstack(L, n, "table too big to unpack");
|
||||
for (i=1; i<=n; i++) /* push arg[1...n] */
|
||||
lua_rawgeti(L, 1, i);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_pcall (lua_State *L) {
|
||||
int status;
|
||||
luaL_checkany(L, 1);
|
||||
status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
|
||||
lua_pushboolean(L, (status == 0));
|
||||
lua_insert(L, 1);
|
||||
return lua_gettop(L); /* return status + all results */
|
||||
}
|
||||
|
||||
|
||||
static int luaB_xpcall (lua_State *L) {
|
||||
int status;
|
||||
luaL_checkany(L, 2);
|
||||
lua_settop(L, 2);
|
||||
lua_insert(L, 1); /* put error function under function to be called */
|
||||
status = lua_pcall(L, 0, LUA_MULTRET, 1);
|
||||
lua_pushboolean(L, (status == 0));
|
||||
lua_replace(L, 1);
|
||||
return lua_gettop(L); /* return status + all results */
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tostring (lua_State *L) {
|
||||
char buff[64];
|
||||
luaL_checkany(L, 1);
|
||||
if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
|
||||
return 1; /* use its value */
|
||||
switch (lua_type(L, 1)) {
|
||||
case LUA_TNUMBER:
|
||||
lua_pushstring(L, lua_tostring(L, 1));
|
||||
return 1;
|
||||
case LUA_TSTRING:
|
||||
lua_pushvalue(L, 1);
|
||||
return 1;
|
||||
case LUA_TBOOLEAN:
|
||||
lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
|
||||
return 1;
|
||||
case LUA_TTABLE:
|
||||
sprintf(buff, "table: %p", lua_topointer(L, 1));
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
sprintf(buff, "function: %p", lua_topointer(L, 1));
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
|
||||
break;
|
||||
case LUA_TTHREAD:
|
||||
sprintf(buff, "thread: %p", (void *)lua_tothread(L, 1));
|
||||
break;
|
||||
case LUA_TNIL:
|
||||
lua_pushliteral(L, "nil");
|
||||
return 1;
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_newproxy (lua_State *L) {
|
||||
lua_settop(L, 1);
|
||||
lua_newuserdata(L, 0); /* create proxy */
|
||||
if (lua_toboolean(L, 1) == 0)
|
||||
return 1; /* no metatable */
|
||||
else if (lua_isboolean(L, 1)) {
|
||||
lua_newtable(L); /* create a new metatable `m' ... */
|
||||
lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
|
||||
lua_pushboolean(L, 1);
|
||||
lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
|
||||
}
|
||||
else {
|
||||
int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
|
||||
if (lua_getmetatable(L, 1)) {
|
||||
lua_rawget(L, lua_upvalueindex(1));
|
||||
validproxy = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1); /* remove value */
|
||||
}
|
||||
luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
|
||||
lua_getmetatable(L, 1); /* metatable is valid; get it */
|
||||
}
|
||||
lua_setmetatable(L, 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** `require' function
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/* name of global that holds table with loaded packages */
|
||||
#define REQTAB "_LOADED"
|
||||
|
||||
/* name of global that holds the search path for packages */
|
||||
#define LUA_PATH "LUA_PATH"
|
||||
|
||||
#ifndef LUA_PATH_SEP
|
||||
#define LUA_PATH_SEP ';'
|
||||
#endif
|
||||
|
||||
#ifndef LUA_PATH_MARK
|
||||
#define LUA_PATH_MARK '?'
|
||||
#endif
|
||||
|
||||
#ifndef LUA_PATH_DEFAULT
|
||||
#define LUA_PATH_DEFAULT "?;?.lua"
|
||||
#endif
|
||||
|
||||
|
||||
static const char *getpath (lua_State *L) {
|
||||
const char *path;
|
||||
lua_getglobal(L, LUA_PATH); /* try global variable */
|
||||
path = lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
if (path) return path;
|
||||
path = getenv(LUA_PATH); /* else try environment variable */
|
||||
if (path) return path;
|
||||
return LUA_PATH_DEFAULT; /* else use default */
|
||||
}
|
||||
|
||||
|
||||
static const char *pushnextpath (lua_State *L, const char *path) {
|
||||
const char *l;
|
||||
if (*path == '\0') return NULL; /* no more paths */
|
||||
if (*path == LUA_PATH_SEP) path++; /* skip separator */
|
||||
l = strchr(path, LUA_PATH_SEP); /* find next separator */
|
||||
if (l == NULL) l = path+strlen(path);
|
||||
lua_pushlstring(L, path, l - path); /* directory name */
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
static void pushcomposename (lua_State *L) {
|
||||
const char *path = lua_tostring(L, -1);
|
||||
const char *wild;
|
||||
int n = 1;
|
||||
while ((wild = strchr(path, LUA_PATH_MARK)) != NULL) {
|
||||
/* is there stack space for prefix, name, and eventual last sufix? */
|
||||
luaL_checkstack(L, 3, "too many marks in a path component");
|
||||
lua_pushlstring(L, path, wild - path); /* push prefix */
|
||||
lua_pushvalue(L, 1); /* push package name (in place of MARK) */
|
||||
path = wild + 1; /* continue after MARK */
|
||||
n += 2;
|
||||
}
|
||||
lua_pushstring(L, path); /* push last sufix (`n' already includes this) */
|
||||
lua_concat(L, n);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_require (lua_State *L) {
|
||||
const char *path;
|
||||
int status = LUA_ERRFILE; /* not found (yet) */
|
||||
luaL_checkstring(L, 1);
|
||||
lua_settop(L, 1);
|
||||
lua_getglobal(L, REQTAB);
|
||||
if (!lua_istable(L, 2)) return luaL_error(L, "`" REQTAB "' is not a table");
|
||||
path = getpath(L);
|
||||
lua_pushvalue(L, 1); /* check package's name in book-keeping table */
|
||||
lua_rawget(L, 2);
|
||||
if (lua_toboolean(L, -1)) /* is it there? */
|
||||
return 1; /* package is already loaded; return its result */
|
||||
else { /* must load it */
|
||||
while (status == LUA_ERRFILE) {
|
||||
lua_settop(L, 3); /* reset stack position */
|
||||
if ((path = pushnextpath(L, path)) == NULL) break;
|
||||
pushcomposename(L);
|
||||
status = luaL_loadfile(L, lua_tostring(L, -1)); /* try to load it */
|
||||
}
|
||||
}
|
||||
switch (status) {
|
||||
case 0: {
|
||||
lua_getglobal(L, "_REQUIREDNAME"); /* save previous name */
|
||||
lua_insert(L, -2); /* put it below function */
|
||||
lua_pushvalue(L, 1);
|
||||
lua_setglobal(L, "_REQUIREDNAME"); /* set new name */
|
||||
lua_call(L, 0, 1); /* run loaded module */
|
||||
lua_insert(L, -2); /* put result below previous name */
|
||||
lua_setglobal(L, "_REQUIREDNAME"); /* reset to previous name */
|
||||
if (lua_isnil(L, -1)) { /* no/nil return? */
|
||||
lua_pushboolean(L, 1);
|
||||
lua_replace(L, -2); /* replace to true */
|
||||
}
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_rawset(L, 2); /* mark it as loaded */
|
||||
return 1; /* return value */
|
||||
}
|
||||
case LUA_ERRFILE: { /* file not found */
|
||||
return luaL_error(L, "could not load package `%s' from path `%s'",
|
||||
lua_tostring(L, 1), getpath(L));
|
||||
}
|
||||
default: {
|
||||
return luaL_error(L, "error loading package `%s' (%s)",
|
||||
lua_tostring(L, 1), lua_tostring(L, -1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static const luaL_reg base_funcs[] = {
|
||||
{"error", luaB_error},
|
||||
{"getmetatable", luaB_getmetatable},
|
||||
{"setmetatable", luaB_setmetatable},
|
||||
{"getfenv", luaB_getfenv},
|
||||
{"setfenv", luaB_setfenv},
|
||||
{"next", luaB_next},
|
||||
{"ipairs", luaB_ipairs},
|
||||
{"pairs", luaB_pairs},
|
||||
{"print", luaB_print},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
{"type", luaB_type},
|
||||
{"assert", luaB_assert},
|
||||
{"unpack", luaB_unpack},
|
||||
{"rawequal", luaB_rawequal},
|
||||
{"rawget", luaB_rawget},
|
||||
{"rawset", luaB_rawset},
|
||||
{"pcall", luaB_pcall},
|
||||
{"xpcall", luaB_xpcall},
|
||||
{"collectgarbage", luaB_collectgarbage},
|
||||
{"gcinfo", luaB_gcinfo},
|
||||
{"loadfile", luaB_loadfile},
|
||||
{"dofile", luaB_dofile},
|
||||
{"loadstring", luaB_loadstring},
|
||||
{"require", luaB_require},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Coroutine library
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static int auxresume (lua_State *L, lua_State *co, int narg) {
|
||||
int status;
|
||||
if (!lua_checkstack(co, narg))
|
||||
luaL_error(L, "too many arguments to resume");
|
||||
lua_xmove(L, co, narg);
|
||||
status = lua_resume(co, narg);
|
||||
if (status == 0) {
|
||||
int nres = lua_gettop(co);
|
||||
if (!lua_checkstack(L, nres))
|
||||
luaL_error(L, "too many results to resume");
|
||||
lua_xmove(co, L, nres); /* move yielded values */
|
||||
return nres;
|
||||
}
|
||||
else {
|
||||
lua_xmove(co, L, 1); /* move error message */
|
||||
return -1; /* error flag */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_coresume (lua_State *L) {
|
||||
lua_State *co = lua_tothread(L, 1);
|
||||
int r;
|
||||
luaL_argcheck(L, co, 1, "coroutine expected");
|
||||
r = auxresume(L, co, lua_gettop(L) - 1);
|
||||
if (r < 0) {
|
||||
lua_pushboolean(L, 0);
|
||||
lua_insert(L, -2);
|
||||
return 2; /* return false + error message */
|
||||
}
|
||||
else {
|
||||
lua_pushboolean(L, 1);
|
||||
lua_insert(L, -(r + 1));
|
||||
return r + 1; /* return true + `resume' returns */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_auxwrap (lua_State *L) {
|
||||
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
|
||||
int r = auxresume(L, co, lua_gettop(L));
|
||||
if (r < 0) {
|
||||
if (lua_isstring(L, -1)) { /* error object is a string? */
|
||||
luaL_where(L, 1); /* add extra info */
|
||||
lua_insert(L, -2);
|
||||
lua_concat(L, 2);
|
||||
}
|
||||
lua_error(L); /* propagate error */
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_cocreate (lua_State *L) {
|
||||
lua_State *NL = lua_newthread(L);
|
||||
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
|
||||
"Lua function expected");
|
||||
lua_pushvalue(L, 1); /* move function to top */
|
||||
lua_xmove(L, NL, 1); /* move function from L to NL */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_cowrap (lua_State *L) {
|
||||
luaB_cocreate(L);
|
||||
lua_pushcclosure(L, luaB_auxwrap, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_yield (lua_State *L) {
|
||||
return lua_yield(L, lua_gettop(L));
|
||||
}
|
||||
|
||||
|
||||
static int luaB_costatus (lua_State *L) {
|
||||
lua_State *co = lua_tothread(L, 1);
|
||||
luaL_argcheck(L, co, 1, "coroutine expected");
|
||||
if (L == co) lua_pushliteral(L, "running");
|
||||
else {
|
||||
lua_Debug ar;
|
||||
if (lua_getstack(co, 0, &ar) == 0 && lua_gettop(co) == 0)
|
||||
lua_pushliteral(L, "dead");
|
||||
else
|
||||
lua_pushliteral(L, "suspended");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_reg co_funcs[] = {
|
||||
{"create", luaB_cocreate},
|
||||
{"wrap", luaB_cowrap},
|
||||
{"resume", luaB_coresume},
|
||||
{"yield", luaB_yield},
|
||||
{"status", luaB_costatus},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
static void base_open (lua_State *L) {
|
||||
lua_pushliteral(L, "_G");
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
|
||||
lua_pushliteral(L, "_VERSION");
|
||||
lua_pushliteral(L, LUA_VERSION);
|
||||
lua_rawset(L, -3); /* set global _VERSION */
|
||||
/* `newproxy' needs a weaktable as upvalue */
|
||||
lua_pushliteral(L, "newproxy");
|
||||
lua_newtable(L); /* new table `w' */
|
||||
lua_pushvalue(L, -1); /* `w' will be its own metatable */
|
||||
lua_setmetatable(L, -2);
|
||||
lua_pushliteral(L, "__mode");
|
||||
lua_pushliteral(L, "k");
|
||||
lua_rawset(L, -3); /* metatable(w).__mode = "k" */
|
||||
lua_pushcclosure(L, luaB_newproxy, 1);
|
||||
lua_rawset(L, -3); /* set global `newproxy' */
|
||||
lua_rawset(L, -1); /* set global _G */
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaopen_base (lua_State *L) {
|
||||
base_open(L);
|
||||
luaL_openlib(L, LUA_COLIBNAME, co_funcs, 0);
|
||||
lua_newtable(L);
|
||||
lua_setglobal(L, REQTAB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
79
lbuffer.c
79
lbuffer.c
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuffer.c,v 1.11 1999/11/22 13:12:07 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*-------------------------------------------------------
|
||||
** Auxiliary buffer
|
||||
-------------------------------------------------------*/
|
||||
|
||||
|
||||
/*
|
||||
** amount of extra space (pre)allocated when buffer is reallocated
|
||||
*/
|
||||
#define EXTRABUFF 32
|
||||
|
||||
|
||||
#define openspace(L, size) if (L->Mbuffnext+(size) > L->Mbuffsize) \
|
||||
Openspace(L, size)
|
||||
|
||||
static void Openspace (lua_State *L, int size) {
|
||||
L->Mbuffsize = (L->Mbuffnext+size+EXTRABUFF)*2;
|
||||
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
|
||||
}
|
||||
|
||||
|
||||
char *luaL_openspace (lua_State *L, int size) {
|
||||
openspace(L, size);
|
||||
return L->Mbuffer+L->Mbuffnext;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addchar (lua_State *L, int c) {
|
||||
openspace(L, 1);
|
||||
L->Mbuffer[L->Mbuffnext++] = (char)c;
|
||||
}
|
||||
|
||||
|
||||
void luaL_resetbuffer (lua_State *L) {
|
||||
L->Mbuffnext = L->Mbuffbase;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addsize (lua_State *L, int n) {
|
||||
L->Mbuffnext += n;
|
||||
}
|
||||
|
||||
int luaL_getsize (lua_State *L) {
|
||||
return L->Mbuffnext-L->Mbuffbase;
|
||||
}
|
||||
|
||||
int luaL_newbuffer (lua_State *L, int size) {
|
||||
int old = L->Mbuffbase;
|
||||
openspace(L, size);
|
||||
L->Mbuffbase = L->Mbuffnext;
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
void luaL_oldbuffer (lua_State *L, int old) {
|
||||
L->Mbuffnext = L->Mbuffbase;
|
||||
L->Mbuffbase = old;
|
||||
}
|
||||
|
||||
|
||||
char *luaL_buffer (lua_State *L) {
|
||||
return L->Mbuffer+L->Mbuffbase;
|
||||
}
|
||||
|
||||
662
lbuiltin.c
662
lbuiltin.c
@@ -1,662 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuiltin.c,v 1.105 2000/04/14 17:44:20 roberto Exp roberto $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** =========================================================================
|
||||
** All built-in functions are public (i.e. not static) and are named luaB_f,
|
||||
** where f is the function name in Lua. So, if you do not need all these
|
||||
** functions, you may register manually only the ones that you need.
|
||||
** =========================================================================
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lbuiltin.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** function defined in ltests.c, to open some internal-test functions
|
||||
*/
|
||||
void luaB_opentests (lua_State *L);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Auxiliary functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static void pushtagstring (lua_State *L, TString *s) {
|
||||
ttype(L->top) = TAG_STRING;
|
||||
tsvalue(L->top) = s;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
static Number getsize (const Hash *h) {
|
||||
Number max = 0;
|
||||
int i = h->size;
|
||||
Node *n = h->node;
|
||||
while (i--) {
|
||||
if (ttype(key(n)) == TAG_NUMBER &&
|
||||
ttype(val(n)) != TAG_NIL &&
|
||||
nvalue(key(n)) > max)
|
||||
max = nvalue(key(n));
|
||||
n++;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
|
||||
static Number getnarg (lua_State *L, const Hash *a) {
|
||||
TObject index;
|
||||
const TObject *value;
|
||||
/* value = table.n */
|
||||
ttype(&index) = TAG_STRING;
|
||||
tsvalue(&index) = luaS_new(L, "n");
|
||||
value = luaH_get(L, a, &index);
|
||||
return (ttype(value) == TAG_NUMBER) ? nvalue(value) : getsize(a);
|
||||
}
|
||||
|
||||
|
||||
static Hash *gettable (lua_State *L, int arg) {
|
||||
return avalue(luaL_tablearg(L, arg));
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Functions that use only the official API
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stderr', redefine this function, or
|
||||
** redefine _ERRORMESSAGE so that it won't need _ALERT.
|
||||
*/
|
||||
void luaB__ALERT (lua_State *L) {
|
||||
fputs(luaL_check_string(L, 1), stderr);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Standard implementation of _ERRORMESSAGE.
|
||||
** The library `liolib' redefines _ERRORMESSAGE for better error information.
|
||||
*/
|
||||
void luaB__ERRORMESSAGE (lua_State *L) {
|
||||
lua_Object al = lua_rawgetglobal(L, "_ALERT");
|
||||
if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */
|
||||
const char *s = luaL_check_string(L, 1);
|
||||
char *buff = luaL_openspace(L, strlen(s)+sizeof("error: \n"));
|
||||
strcpy(buff, "error: "); strcat(buff, s); strcat(buff, "\n");
|
||||
lua_pushstring(L, buff);
|
||||
lua_callfunction(L, al);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stdout', you can just remove this function.
|
||||
** If you need, you can define your own `print' function, following this
|
||||
** model but changing `fputs' to put the strings at a proper place
|
||||
** (a console window or a log file, for instance).
|
||||
*/
|
||||
void luaB_print (lua_State *L) {
|
||||
lua_Object args[MAXPRINT];
|
||||
lua_Object obj;
|
||||
int n = 0;
|
||||
int i;
|
||||
while ((obj = lua_getparam(L, n+1)) != LUA_NOOBJECT) {
|
||||
luaL_arg_check(L, n < MAXPRINT, n+1, "too many arguments");
|
||||
args[n++] = obj;
|
||||
}
|
||||
for (i=0; i<n; i++) {
|
||||
lua_pushobject(L, args[i]);
|
||||
if (lua_call(L, "tostring"))
|
||||
lua_error(L, "error in `tostring' called by `print'");
|
||||
obj = lua_getresult(L, 1);
|
||||
if (!lua_isstring(L, obj))
|
||||
lua_error(L, "`tostring' must return a string to `print'");
|
||||
if (i>0) fputs("\t", stdout);
|
||||
fputs(lua_getstring(L, obj), stdout);
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
}
|
||||
|
||||
|
||||
void luaB_tonumber (lua_State *L) {
|
||||
int base = luaL_opt_int(L, 2, 10);
|
||||
if (base == 10) { /* standard conversion */
|
||||
lua_Object o = luaL_nonnullarg(L, 1);
|
||||
if (lua_isnumber(L, o)) {
|
||||
lua_pushnumber(L, lua_getnumber(L, o));
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const char *s1 = luaL_check_string(L, 1);
|
||||
char *s2;
|
||||
Number n;
|
||||
luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
n = strtoul(s1, &s2, base);
|
||||
if (s1 != s2) { /* at least one valid digit? */
|
||||
while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
|
||||
if (*s2 == '\0') { /* no invalid trailing characters? */
|
||||
lua_pushnumber(L, n);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_pushnil(L); /* else not a number */
|
||||
}
|
||||
|
||||
|
||||
void luaB_error (lua_State *L) {
|
||||
lua_error(L, luaL_opt_string(L, 1, NULL));
|
||||
}
|
||||
|
||||
void luaB_setglobal (lua_State *L) {
|
||||
const char *name = luaL_check_string(L, 1);
|
||||
lua_Object value = luaL_nonnullarg(L, 2);
|
||||
lua_pushobject(L, value);
|
||||
lua_setglobal(L, name);
|
||||
}
|
||||
|
||||
void luaB_rawsetglobal (lua_State *L) {
|
||||
const char *name = luaL_check_string(L, 1);
|
||||
lua_Object value = luaL_nonnullarg(L, 2);
|
||||
lua_pushobject(L, value);
|
||||
lua_rawsetglobal(L, name);
|
||||
}
|
||||
|
||||
void luaB_getglobal (lua_State *L) {
|
||||
lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1)));
|
||||
}
|
||||
|
||||
void luaB_rawgetglobal (lua_State *L) {
|
||||
lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1)));
|
||||
}
|
||||
|
||||
void luaB_tag (lua_State *L) {
|
||||
lua_pushnumber(L, lua_tag(L, luaL_nonnullarg(L, 1)));
|
||||
}
|
||||
|
||||
void luaB_settag (lua_State *L) {
|
||||
lua_Object o = luaL_tablearg(L, 1);
|
||||
lua_pushobject(L, o);
|
||||
lua_settag(L, luaL_check_int(L, 2));
|
||||
lua_pushobject(L, o); /* return first argument */
|
||||
}
|
||||
|
||||
void luaB_newtag (lua_State *L) {
|
||||
lua_pushnumber(L, lua_newtag(L));
|
||||
}
|
||||
|
||||
void luaB_copytagmethods (lua_State *L) {
|
||||
lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
|
||||
luaL_check_int(L, 2)));
|
||||
}
|
||||
|
||||
void luaB_rawgettable (lua_State *L) {
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 1));
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 2));
|
||||
lua_pushobject(L, lua_rawgettable(L));
|
||||
}
|
||||
|
||||
void luaB_rawsettable (lua_State *L) {
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 1));
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 2));
|
||||
lua_pushobject(L, luaL_nonnullarg(L, 3));
|
||||
lua_rawsettable(L);
|
||||
}
|
||||
|
||||
void luaB_settagmethod (lua_State *L) {
|
||||
int tag = luaL_check_int(L, 1);
|
||||
const char *event = luaL_check_string(L, 2);
|
||||
lua_Object nf = luaL_nonnullarg(L, 3);
|
||||
luaL_arg_check(L, lua_isnil(L, nf) || lua_isfunction(L, nf), 3,
|
||||
"function or nil expected");
|
||||
if (strcmp(event, "gc") == 0 && tag != TAG_NIL)
|
||||
lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
|
||||
lua_pushobject(L, nf);
|
||||
lua_pushobject(L, lua_settagmethod(L, tag, event));
|
||||
}
|
||||
|
||||
void luaB_gettagmethod (lua_State *L) {
|
||||
lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1),
|
||||
luaL_check_string(L, 2)));
|
||||
}
|
||||
|
||||
|
||||
void luaB_collectgarbage (lua_State *L) {
|
||||
lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
|
||||
}
|
||||
|
||||
|
||||
void luaB_type (lua_State *L) {
|
||||
lua_Object o = luaL_nonnullarg(L, 1);
|
||||
lua_pushstring(L, lua_type(L, o));
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Functions that could use only the official API but
|
||||
** do not, for efficiency.
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static void passresults (lua_State *L) {
|
||||
L->Cstack.base = L->Cstack.lua2C; /* position of first result */
|
||||
if (L->Cstack.num == 0)
|
||||
lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
void luaB_dostring (lua_State *L) {
|
||||
long l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
if (*s == ID_CHUNK)
|
||||
lua_error(L, "`dostring' cannot run pre-compiled code");
|
||||
if (lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)) == 0)
|
||||
passresults(L);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
void luaB_dofile (lua_State *L) {
|
||||
const char *fname = luaL_opt_string(L, 1, NULL);
|
||||
if (lua_dofile(L, fname) == 0)
|
||||
passresults(L);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
void luaB_call (lua_State *L) {
|
||||
lua_Object f = luaL_nonnullarg(L, 1);
|
||||
const Hash *arg = gettable(L, 2);
|
||||
const char *options = luaL_opt_string(L, 3, "");
|
||||
lua_Object err = lua_getparam(L, 4);
|
||||
int narg = (int)getnarg(L, arg);
|
||||
int i, status;
|
||||
if (err != LUA_NOOBJECT) { /* set new error method */
|
||||
lua_Object oldem = lua_getglobal(L, "_ERRORMESSAGE");
|
||||
lua_pushobject(L, err);
|
||||
lua_setglobal(L, "_ERRORMESSAGE");
|
||||
err = oldem;
|
||||
}
|
||||
/* push arg[1...n] */
|
||||
luaD_checkstack(L, narg);
|
||||
for (i=0; i<narg; i++)
|
||||
*(L->top++) = *luaH_getint(L, arg, i+1);
|
||||
status = lua_callfunction(L, f);
|
||||
if (err != LUA_NOOBJECT) { /* restore old error method */
|
||||
lua_pushobject(L, err);
|
||||
lua_setglobal(L, "_ERRORMESSAGE");
|
||||
}
|
||||
if (status != 0) { /* error in call? */
|
||||
if (strchr(options, 'x')) {
|
||||
lua_pushnil(L);
|
||||
return; /* return nil to signal the error */
|
||||
}
|
||||
else
|
||||
lua_error(L, NULL); /* propagate error without additional messages */
|
||||
}
|
||||
else { /* no errors */
|
||||
if (strchr(options, 'p')) { /* pack results? */
|
||||
luaV_pack(L, L->Cstack.lua2C, L->Cstack.num, L->top);
|
||||
incr_top;
|
||||
}
|
||||
else
|
||||
L->Cstack.base = L->Cstack.lua2C; /* position of first result */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaB_nextvar (lua_State *L) {
|
||||
lua_Object o = lua_getparam(L, 1);
|
||||
TString *name;
|
||||
if (o == LUA_NOOBJECT || ttype(o) == TAG_NIL)
|
||||
name = NULL;
|
||||
else {
|
||||
luaL_arg_check(L, ttype(o) == TAG_STRING, 1, "variable name expected");
|
||||
name = tsvalue(o);
|
||||
}
|
||||
if (!luaA_nextvar(L, name))
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
void luaB_next (lua_State *L) {
|
||||
const Hash *a = gettable(L, 1);
|
||||
lua_Object k = lua_getparam(L, 2);
|
||||
int i; /* `luaA_next' gets first element after `i' */
|
||||
if (k == LUA_NOOBJECT || ttype(k) == TAG_NIL)
|
||||
i = 0; /* get first */
|
||||
else {
|
||||
i = luaH_pos(L, a, k)+1;
|
||||
luaL_arg_check(L, i != 0, 2, "key not found");
|
||||
}
|
||||
if (luaA_next(L, a, i) == 0)
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
void luaB_tostring (lua_State *L) {
|
||||
lua_Object o = luaL_nonnullarg(L, 1);
|
||||
char buff[64];
|
||||
switch (ttype(o)) {
|
||||
case TAG_NUMBER:
|
||||
lua_pushstring(L, lua_getstring(L, o));
|
||||
return;
|
||||
case TAG_STRING:
|
||||
lua_pushobject(L, o);
|
||||
return;
|
||||
case TAG_TABLE:
|
||||
sprintf(buff, "table: %p", o->value.a);
|
||||
break;
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
sprintf(buff, "function: %p", o->value.cl);
|
||||
break;
|
||||
case TAG_USERDATA:
|
||||
sprintf(buff, "userdata: %p(%d)", o->value.ts->u.d.value,
|
||||
o->value.ts->u.d.tag);
|
||||
break;
|
||||
case TAG_NIL:
|
||||
lua_pushstring(L, "nil");
|
||||
return;
|
||||
default:
|
||||
LUA_INTERNALERROR(L, "invalid type");
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** "Extra" functions
|
||||
** These functions can be written in Lua, so you can
|
||||
** delete them if you need a tiny Lua implementation.
|
||||
** If you delete them, remove their entries in array
|
||||
** "builtin_funcs".
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
void luaB_assert (lua_State *L) {
|
||||
lua_Object p = luaL_nonnullarg(L, 1);
|
||||
if (lua_isnil(L, p))
|
||||
luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
|
||||
}
|
||||
|
||||
|
||||
void luaB_foreachi (lua_State *L) {
|
||||
const Hash *t = gettable(L, 1);
|
||||
int n = (int)getnarg(L, t);
|
||||
int i;
|
||||
lua_Object f = luaL_functionarg(L, 2);
|
||||
luaD_checkstack(L, 3); /* for f, key, and val */
|
||||
for (i=1; i<=n; i++) {
|
||||
*(L->top++) = *f;
|
||||
ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i;
|
||||
*(L->top++) = *luaH_getint(L, t, i);
|
||||
luaD_call(L, L->top-3, 1);
|
||||
if (ttype(L->top-1) != TAG_NIL)
|
||||
return;
|
||||
L->top--; /* remove nil result */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaB_foreach (lua_State *L) {
|
||||
const Hash *a = gettable(L, 1);
|
||||
lua_Object f = luaL_functionarg(L, 2);
|
||||
int i;
|
||||
luaD_checkstack(L, 3); /* for f, key, and val */
|
||||
for (i=0; i<a->size; i++) {
|
||||
const Node *nd = &(a->node[i]);
|
||||
if (ttype(val(nd)) != TAG_NIL) {
|
||||
*(L->top++) = *f;
|
||||
*(L->top++) = *key(nd);
|
||||
*(L->top++) = *val(nd);
|
||||
luaD_call(L, L->top-3, 1);
|
||||
if (ttype(L->top-1) != TAG_NIL)
|
||||
return;
|
||||
L->top--; /* remove result */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaB_foreachvar (lua_State *L) {
|
||||
lua_Object f = luaL_functionarg(L, 1);
|
||||
GlobalVar *gv;
|
||||
luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */
|
||||
for (gv = L->rootglobal; gv; gv = gv->next) {
|
||||
if (gv->value.ttype != TAG_NIL) {
|
||||
pushtagstring(L, gv->name); /* keep (extra) name on stack to avoid GC */
|
||||
*(L->top++) = *f;
|
||||
pushtagstring(L, gv->name);
|
||||
*(L->top++) = gv->value;
|
||||
luaD_call(L, L->top-3, 1);
|
||||
if (ttype(L->top-1) != TAG_NIL) {
|
||||
*(L->top-2) = *(L->top-1); /* remove extra name */
|
||||
L->top--;
|
||||
return;
|
||||
}
|
||||
L->top-=2; /* remove result and extra name */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaB_getn (lua_State *L) {
|
||||
lua_pushnumber(L, getnarg(L, gettable(L, 1)));
|
||||
}
|
||||
|
||||
|
||||
void luaB_tinsert (lua_State *L) {
|
||||
Hash *a = gettable(L, 1);
|
||||
lua_Object v = lua_getparam(L, 3);
|
||||
int n = (int)getnarg(L, a);
|
||||
int pos;
|
||||
if (v != LUA_NOOBJECT)
|
||||
pos = luaL_check_int(L, 2);
|
||||
else { /* called with only 2 arguments */
|
||||
v = luaL_nonnullarg(L, 2);
|
||||
pos = n+1;
|
||||
}
|
||||
luaV_setn(L, a, n+1); /* a.n = n+1 */
|
||||
for (; n>=pos; n--)
|
||||
luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */
|
||||
luaH_setint(L, a, pos, v); /* a[pos] = v */
|
||||
}
|
||||
|
||||
|
||||
void luaB_tremove (lua_State *L) {
|
||||
Hash *a = gettable(L, 1);
|
||||
int n = (int)getnarg(L, a);
|
||||
int pos = luaL_opt_int(L, 2, n);
|
||||
if (n <= 0) return; /* table is "empty" */
|
||||
luaA_pushobject(L, luaH_getint(L, a, pos)); /* result = a[pos] */
|
||||
for ( ;pos<n; pos++)
|
||||
luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */
|
||||
luaV_setn(L, a, n-1); /* a.n = n-1 */
|
||||
luaH_setint(L, a, n, &luaO_nilobject); /* a[n] = nil */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Quicksort
|
||||
** (based on `Algorithms in MODULA-3', Robert Sedgewick; Addison-Wesley, 1993.)
|
||||
*/
|
||||
|
||||
static void swap (lua_State *L, Hash *a, int i, int j) {
|
||||
TObject temp;
|
||||
temp = *luaH_getint(L, a, i);
|
||||
luaH_move(L, a, j, i);
|
||||
luaH_setint(L, a, j, &temp);
|
||||
}
|
||||
|
||||
static int sort_comp (lua_State *L, lua_Object f, const TObject *a,
|
||||
const TObject *b) {
|
||||
/* WARNING: the caller (auxsort) must ensure stack space */
|
||||
if (f != LUA_NOOBJECT) {
|
||||
*(L->top) = *f;
|
||||
*(L->top+1) = *a;
|
||||
*(L->top+2) = *b;
|
||||
L->top += 3;
|
||||
luaD_call(L, L->top-3, 1);
|
||||
L->top--;
|
||||
return (ttype(L->top) != TAG_NIL);
|
||||
}
|
||||
else /* a < b? */
|
||||
return luaV_lessthan(L, a, b, L->top);
|
||||
}
|
||||
|
||||
static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) {
|
||||
StkId P = L->top++; /* temporary place for pivot */
|
||||
ttype(P) = TAG_NIL;
|
||||
while (l < u) { /* for tail recursion */
|
||||
int i, j;
|
||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||
if (sort_comp(L, f, luaH_getint(L, a, u), luaH_getint(L, a, l)))
|
||||
swap(L, a, l, u); /* a[u]<a[l] */
|
||||
if (u-l == 1) break; /* only 2 elements */
|
||||
i = (l+u)/2;
|
||||
*P = *luaH_getint(L, a, i); /* P = a[i] */
|
||||
if (sort_comp(L, f, P, luaH_getint(L, a, l))) /* a[i]<a[l]? */
|
||||
swap(L, a, l, i);
|
||||
else if (sort_comp(L, f, luaH_getint(L, a, u), P)) /* a[u]<a[i]? */
|
||||
swap(L, a, i, u);
|
||||
if (u-l == 2) break; /* only 3 elements */
|
||||
*P = *luaH_getint(L, a, i); /* save pivot on stack (GC) */
|
||||
swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */
|
||||
/* a[l] <= P == a[u-1] <= a[u], only needs 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 (sort_comp(L, f, luaH_getint(L, a, ++i), P))
|
||||
if (i>u) lua_error(L, "invalid order function for sorting");
|
||||
/* repeat j-- until a[j] <= P */
|
||||
while (sort_comp(L, f, P, luaH_getint(L, a, --j)))
|
||||
if (j<l) lua_error(L, "invalid order function for sorting");
|
||||
if (j<i) break;
|
||||
swap(L, a, i, j);
|
||||
}
|
||||
swap(L, a, 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, a, j, i, f); /* call recursively the smaller one */
|
||||
} /* repeat the routine for the larger one */
|
||||
L->top--; /* remove pivot from stack */
|
||||
}
|
||||
|
||||
void luaB_sort (lua_State *L) {
|
||||
Hash *a = gettable(L, 1);
|
||||
int n = (int)getnarg(L, a);
|
||||
lua_Object func = lua_getparam(L, 2);
|
||||
luaL_arg_check(L, func == LUA_NOOBJECT || lua_isfunction(L, func), 2,
|
||||
"function expected");
|
||||
luaD_checkstack(L, 4); /* for pivot, f, a, b (sort_comp) */
|
||||
auxsort(L, a, 1, n, func);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static const struct luaL_reg builtin_funcs[] = {
|
||||
{"_ALERT", luaB__ALERT},
|
||||
{"_ERRORMESSAGE", luaB__ERRORMESSAGE},
|
||||
{"call", luaB_call},
|
||||
{"collectgarbage", luaB_collectgarbage},
|
||||
{"copytagmethods", luaB_copytagmethods},
|
||||
{"dofile", luaB_dofile},
|
||||
{"dostring", luaB_dostring},
|
||||
{"error", luaB_error},
|
||||
{"getglobal", luaB_getglobal},
|
||||
{"gettagmethod", luaB_gettagmethod},
|
||||
{"newtag", luaB_newtag},
|
||||
{"next", luaB_next},
|
||||
{"nextvar", luaB_nextvar},
|
||||
{"print", luaB_print},
|
||||
{"rawgetglobal", luaB_rawgetglobal},
|
||||
{"rawgettable", luaB_rawgettable},
|
||||
{"rawsetglobal", luaB_rawsetglobal},
|
||||
{"rawsettable", luaB_rawsettable},
|
||||
{"setglobal", luaB_setglobal},
|
||||
{"settag", luaB_settag},
|
||||
{"settagmethod", luaB_settagmethod},
|
||||
{"tag", luaB_tag},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
{"type", luaB_type},
|
||||
/* "Extra" functions */
|
||||
{"assert", luaB_assert},
|
||||
{"foreach", luaB_foreach},
|
||||
{"foreachi", luaB_foreachi},
|
||||
{"foreachvar", luaB_foreachvar},
|
||||
{"getn", luaB_getn},
|
||||
{"sort", luaB_sort},
|
||||
{"tinsert", luaB_tinsert},
|
||||
{"tremove", luaB_tremove}
|
||||
};
|
||||
|
||||
|
||||
void luaB_predefine (lua_State *L) {
|
||||
/* pre-register mem error messages, to avoid loop when error arises */
|
||||
luaS_newfixed(L, tableEM);
|
||||
luaS_newfixed(L, memEM);
|
||||
luaL_openl(L, builtin_funcs);
|
||||
#ifdef DEBUG
|
||||
luaB_opentests(L); /* internal test functions */
|
||||
#endif
|
||||
lua_pushstring(L, LUA_VERSION);
|
||||
lua_setglobal(L, "_VERSION");
|
||||
}
|
||||
|
||||
49
lbuiltin.h
49
lbuiltin.h
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuiltin.h,v 1.6 2000/03/03 14:58:26 roberto Exp roberto $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lbuiltin_h
|
||||
#define lbuiltin_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
void luaB__ALERT (lua_State *L);
|
||||
void luaB__ERRORMESSAGE (lua_State *L);
|
||||
void luaB_assert (lua_State *L);
|
||||
void luaB_call (lua_State *L);
|
||||
void luaB_collectgarbage (lua_State *L);
|
||||
void luaB_copytagmethods (lua_State *L);
|
||||
void luaB_dofile (lua_State *L);
|
||||
void luaB_dostring (lua_State *L);
|
||||
void luaB_error (lua_State *L);
|
||||
void luaB_foreach (lua_State *L);
|
||||
void luaB_foreachi (lua_State *L);
|
||||
void luaB_foreachvar (lua_State *L);
|
||||
void luaB_getglobal (lua_State *L);
|
||||
void luaB_getn (lua_State *L);
|
||||
void luaB_gettagmethod (lua_State *L);
|
||||
void luaB_newtag (lua_State *L);
|
||||
void luaB_next (lua_State *L);
|
||||
void luaB_nextvar (lua_State *L);
|
||||
void luaB_print (lua_State *L);
|
||||
void luaB_rawgetglobal (lua_State *L);
|
||||
void luaB_rawgettable (lua_State *L);
|
||||
void luaB_rawsetglobal (lua_State *L);
|
||||
void luaB_rawsettable (lua_State *L);
|
||||
void luaB_setglobal (lua_State *L);
|
||||
void luaB_settag (lua_State *L);
|
||||
void luaB_settagmethod (lua_State *L);
|
||||
void luaB_sort (lua_State *L);
|
||||
void luaB_tag (lua_State *L);
|
||||
void luaB_tinsert (lua_State *L);
|
||||
void luaB_tonumber (lua_State *L);
|
||||
void luaB_tostring (lua_State *L);
|
||||
void luaB_tremove (lua_State *L);
|
||||
void luaB_type (lua_State *L);
|
||||
|
||||
void luaB_predefine (lua_State *L);
|
||||
|
||||
|
||||
#endif
|
||||
64
lcode.h
64
lcode.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcode.h,v 1.11 2000/04/07 19:35:20 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
|
||||
*/
|
||||
@@ -20,27 +20,55 @@
|
||||
#define NO_JUMP (-1)
|
||||
|
||||
|
||||
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);
|
||||
/*
|
||||
** grep "ORDER OPR" if you change these enums
|
||||
*/
|
||||
typedef enum BinOpr {
|
||||
OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_POW,
|
||||
OPR_CONCAT,
|
||||
OPR_NE, OPR_EQ,
|
||||
OPR_LT, OPR_LE, OPR_GT, OPR_GE,
|
||||
OPR_AND, OPR_OR,
|
||||
OPR_NOBINOPR
|
||||
} BinOpr;
|
||||
|
||||
#define binopistest(op) ((op) >= OPR_NE)
|
||||
|
||||
typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr;
|
||||
|
||||
|
||||
#define getcode(fs,e) ((fs)->f->code[(e)->info])
|
||||
|
||||
#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
|
||||
|
||||
int luaK_code (FuncState *fs, Instruction i, int line);
|
||||
int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
|
||||
int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
|
||||
void luaK_fixline (FuncState *fs, int line);
|
||||
void luaK_nil (FuncState *fs, int from, int n);
|
||||
void luaK_reserveregs (FuncState *fs, int n);
|
||||
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);
|
||||
void luaK_goiffalse (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, int op, expdesc *v);
|
||||
void luaK_infix (LexState *ls, int op, expdesc *v);
|
||||
void luaK_posfix (LexState *ls, int 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
|
||||
|
||||
377
ldblib.c
377
ldblib.c
@@ -1,176 +1,299 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.12 2000/03/30 17:19:48 roberto Exp roberto $
|
||||
** $Id: ldblib.c,v 1.79 2003/03/11 12:24:34 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define ldblib_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static void settabss (lua_State *L, lua_Object t, const char *i, const char *v) {
|
||||
lua_pushobject(L, t);
|
||||
static void settabss (lua_State *L, const char *i, const char *v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushstring(L, v);
|
||||
lua_settable(L);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void settabsi (lua_State *L, lua_Object t, const char *i, int v) {
|
||||
lua_pushobject(L, t);
|
||||
static void settabsi (lua_State *L, const char *i, int v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushnumber(L, v);
|
||||
lua_settable(L);
|
||||
lua_pushnumber(L, (lua_Number)v);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void settabso (lua_State *L, lua_Object t, const char *i, lua_Object v) {
|
||||
lua_pushobject(L, t);
|
||||
lua_pushstring(L, i);
|
||||
lua_pushobject(L, v);
|
||||
lua_settable(L);
|
||||
}
|
||||
|
||||
|
||||
static void getstack (lua_State *L) {
|
||||
static int getinfo (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
return;
|
||||
else {
|
||||
const char *options = luaL_check_string(L, 2);
|
||||
lua_Object res = lua_createtable(L);
|
||||
if (!lua_getinfo(L, options, &ar))
|
||||
luaL_argerror(L, 2, "invalid option");
|
||||
for (; *options; options++) {
|
||||
switch (*options) {
|
||||
case 'S':
|
||||
settabss(L, res, "source", ar.source);
|
||||
settabsi(L, res, "linedefined", ar.linedefined);
|
||||
settabss(L, res, "what", ar.what);
|
||||
break;
|
||||
case 'l':
|
||||
settabsi(L, res, "currentline", ar.currentline);
|
||||
break;
|
||||
case 'u':
|
||||
settabsi(L, res, "nups", ar.nups);
|
||||
break;
|
||||
case 'n':
|
||||
settabss(L, res, "name", ar.name);
|
||||
settabss(L, res, "namewhat", ar.namewhat);
|
||||
break;
|
||||
case 'f':
|
||||
settabso(L, res, "func", ar.func);
|
||||
break;
|
||||
|
||||
}
|
||||
const char *options = luaL_optstring(L, 2, "flnSu");
|
||||
if (lua_isnumber(L, 1)) {
|
||||
if (!lua_getstack(L, (int)(lua_tonumber(L, 1)), &ar)) {
|
||||
lua_pushnil(L); /* level out of range */
|
||||
return 1;
|
||||
}
|
||||
lua_pushobject(L, res);
|
||||
}
|
||||
else if (lua_isfunction(L, 1)) {
|
||||
lua_pushfstring(L, ">%s", options);
|
||||
options = lua_tostring(L, -1);
|
||||
lua_pushvalue(L, 1);
|
||||
}
|
||||
else
|
||||
return luaL_argerror(L, 1, "function or level expected");
|
||||
if (!lua_getinfo(L, options, &ar))
|
||||
return luaL_argerror(L, 2, "invalid option");
|
||||
lua_newtable(L);
|
||||
for (; *options; options++) {
|
||||
switch (*options) {
|
||||
case 'S':
|
||||
settabss(L, "source", ar.source);
|
||||
settabss(L, "short_src", ar.short_src);
|
||||
settabsi(L, "linedefined", ar.linedefined);
|
||||
settabss(L, "what", ar.what);
|
||||
break;
|
||||
case 'l':
|
||||
settabsi(L, "currentline", ar.currentline);
|
||||
break;
|
||||
case 'u':
|
||||
settabsi(L, "nups", ar.nups);
|
||||
break;
|
||||
case 'n':
|
||||
settabss(L, "name", ar.name);
|
||||
settabss(L, "namewhat", ar.namewhat);
|
||||
break;
|
||||
case 'f':
|
||||
lua_pushliteral(L, "func");
|
||||
lua_pushvalue(L, -3);
|
||||
lua_rawset(L, -3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
|
||||
static void getlocal (lua_State *L) {
|
||||
static int getlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
lua_Localvar lvar;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
luaL_argerror(L, 1, "level out of range");
|
||||
lvar.index = luaL_check_int(L, 2);
|
||||
if (lua_getlocal(L, &ar, &lvar)) {
|
||||
lua_pushstring(L, lvar.name);
|
||||
lua_pushobject(L, lvar.value);
|
||||
}
|
||||
else lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
static void setlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
lua_Localvar lvar;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
luaL_argerror(L, 1, "level out of range");
|
||||
lvar.index = luaL_check_int(L, 2);
|
||||
lvar.value = luaL_nonnullarg(L, 3);
|
||||
if (lua_setlocal(L, &ar, &lvar))
|
||||
lua_pushstring(L, lvar.name);
|
||||
else lua_pushnil(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** because of these variables, this module is not reentrant, and should
|
||||
** not be used in multiple states
|
||||
*/
|
||||
|
||||
static int linehook = LUA_NOREF; /* Lua reference to line hook function */
|
||||
static int callhook = LUA_NOREF; /* Lua reference to call hook function */
|
||||
|
||||
|
||||
|
||||
static void linef (lua_State *L, lua_Debug *ar) {
|
||||
if (linehook != LUA_NOREF) {
|
||||
lua_pushnumber(L, ar->currentline);
|
||||
lua_callfunction(L, lua_getref(L, linehook));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void callf (lua_State *L, lua_Debug *ar) {
|
||||
if (callhook != LUA_NOREF) {
|
||||
lua_pushstring(L, ar->event);
|
||||
lua_callfunction(L, lua_getref(L, callhook));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void setcallhook (lua_State *L) {
|
||||
lua_Object f = lua_getparam(L, 1);
|
||||
lua_unref(L, callhook);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
callhook = LUA_NOREF;
|
||||
lua_setcallhook(L, NULL);
|
||||
const char *name;
|
||||
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);
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
lua_pushobject(L, f);
|
||||
callhook = lua_ref(L, 1);
|
||||
lua_setcallhook(L, callf);
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void setlinehook (lua_State *L) {
|
||||
lua_Object f = lua_getparam(L, 1);
|
||||
lua_unref(L, linehook);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
linehook = LUA_NOREF;
|
||||
lua_setlinehook(L, NULL);
|
||||
static int setlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
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_checkint(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int auxupvalue (lua_State *L, int get) {
|
||||
const char *name;
|
||||
int n = luaL_checkint(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */
|
||||
name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
|
||||
if (name == NULL) return 0;
|
||||
lua_pushstring(L, name);
|
||||
lua_insert(L, -(get+1));
|
||||
return get + 1;
|
||||
}
|
||||
|
||||
|
||||
static int getupvalue (lua_State *L) {
|
||||
return auxupvalue(L, 1);
|
||||
}
|
||||
|
||||
|
||||
static int setupvalue (lua_State *L) {
|
||||
luaL_checkany(L, 3);
|
||||
return auxupvalue(L, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static const char KEY_HOOK = 'h';
|
||||
|
||||
|
||||
static void hookf (lua_State *L, lua_Debug *ar) {
|
||||
static const char *const hooknames[] =
|
||||
{"call", "return", "line", "count", "tail return"};
|
||||
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushstring(L, hooknames[(int)ar->event]);
|
||||
if (ar->currentline >= 0)
|
||||
lua_pushnumber(L, (lua_Number)ar->currentline);
|
||||
else lua_pushnil(L);
|
||||
lua_assert(lua_getinfo(L, "lS", ar));
|
||||
lua_call(L, 2, 0);
|
||||
}
|
||||
else
|
||||
lua_pop(L, 1); /* pop result from gettable */
|
||||
}
|
||||
|
||||
|
||||
static 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 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 int sethook (lua_State *L) {
|
||||
if (lua_isnoneornil(L, 1)) {
|
||||
lua_settop(L, 1);
|
||||
lua_sethook(L, NULL, 0, 0); /* turn off hooks */
|
||||
}
|
||||
else {
|
||||
lua_pushobject(L, f);
|
||||
linehook = lua_ref(L, 1);
|
||||
lua_setlinehook(L, linef);
|
||||
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_rawset(L, LUA_REGISTRYINDEX); /* set new hook */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
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_Number)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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_reg dblib[] = {
|
||||
#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' || *ar.what == 't')
|
||||
lua_pushliteral(L, " ?"); /* C function or tail call */
|
||||
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 const luaL_reg dblib[] = {
|
||||
{"getlocal", getlocal},
|
||||
{"getstack", getstack},
|
||||
{"setcallhook", setcallhook},
|
||||
{"setlinehook", setlinehook},
|
||||
{"setlocal", setlocal}
|
||||
{"getinfo", getinfo},
|
||||
{"gethook", gethook},
|
||||
{"getupvalue", getupvalue},
|
||||
{"sethook", sethook},
|
||||
{"setlocal", setlocal},
|
||||
{"setupvalue", setupvalue},
|
||||
{"debug", debug},
|
||||
{"traceback", errorfb},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
void lua_dblibopen (lua_State *L) {
|
||||
luaL_openl(L, dblib);
|
||||
LUALIB_API int luaopen_debug (lua_State *L) {
|
||||
luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
|
||||
lua_pushliteral(L, "_TRACEBACK");
|
||||
lua_pushcfunction(L, errorfb);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
678
ldebug.c
678
ldebug.c
@@ -1,221 +1,585 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 1.15 2000/03/30 17:19:48 roberto Exp roberto $
|
||||
** $Id: ldebug.c,v 1.149 2003/03/18 12:50:04 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define ldebug_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lcode.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
static const lua_Type normtype[] = { /* ORDER LUA_T */
|
||||
TAG_USERDATA, TAG_NUMBER, TAG_STRING, TAG_TABLE,
|
||||
TAG_LCLOSURE, TAG_CCLOSURE, TAG_NIL,
|
||||
TAG_LCLOSURE, TAG_CCLOSURE /* TAG_LMARK, TAG_CMARK */
|
||||
};
|
||||
|
||||
static const char *getfuncname (CallInfo *ci, const char **name);
|
||||
|
||||
|
||||
static void setnormalized (TObject *d, const TObject *s) {
|
||||
d->value = s->value;
|
||||
d->ttype = normtype[ttype(s)];
|
||||
#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 hasdebuginfo (lua_State *L, StkId f) {
|
||||
return (f+1 < L->top && (f+1)->ttype == TAG_LINE);
|
||||
static int currentline (CallInfo *ci) {
|
||||
int pc = currentpc(ci);
|
||||
if (pc < 0)
|
||||
return -1; /* only active lua functions have current-line information */
|
||||
else
|
||||
return getline(ci_func(ci)->l.p, pc);
|
||||
}
|
||||
|
||||
|
||||
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook = L->callhook;
|
||||
L->callhook = func;
|
||||
return oldhook;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook = L->linehook;
|
||||
L->linehook = func;
|
||||
return oldhook;
|
||||
/*
|
||||
** 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;
|
||||
}
|
||||
L->hook = func;
|
||||
L->basehookcount = count;
|
||||
resethookcount(L);
|
||||
L->hookmask = cast(lu_byte, mask);
|
||||
L->hookinit = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int lua_setdebug (lua_State *L, int debug) {
|
||||
int old = L->debug;
|
||||
L->debug = debug;
|
||||
return old;
|
||||
LUA_API lua_Hook lua_gethook (lua_State *L) {
|
||||
return L->hook;
|
||||
}
|
||||
|
||||
|
||||
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--;
|
||||
}
|
||||
LUA_API int lua_gethookmask (lua_State *L) {
|
||||
return L->hookmask;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_gethookcount (lua_State *L) {
|
||||
return L->basehookcount;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
int status;
|
||||
CallInfo *ci;
|
||||
lua_lock(L);
|
||||
for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
|
||||
level--;
|
||||
if (!(ci->state & CI_C)) /* Lua function? */
|
||||
level -= ci->u.l.tailcalls; /* skip lost tail calls */
|
||||
}
|
||||
if (level > 0 || ci == L->base_ci) status = 0; /* there is no such level */
|
||||
else if (level < 0) { /* level is of a lost tail call */
|
||||
status = 1;
|
||||
ar->i_ci = 0;
|
||||
}
|
||||
else {
|
||||
status = 1;
|
||||
ar->i_ci = ci - L->base_ci;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static Proto *getluaproto (CallInfo *ci) {
|
||||
return (isLua(ci) ? ci_func(ci)->l.p : NULL);
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const char *name;
|
||||
CallInfo *ci;
|
||||
Proto *fp;
|
||||
lua_lock(L);
|
||||
name = NULL;
|
||||
ci = L->base_ci + ar->i_ci;
|
||||
fp = getluaproto(ci);
|
||||
if (fp) { /* is a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(ci));
|
||||
if (name)
|
||||
luaA_pushobject(L, ci->base+(n-1)); /* push value */
|
||||
}
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const char *name;
|
||||
CallInfo *ci;
|
||||
Proto *fp;
|
||||
lua_lock(L);
|
||||
name = NULL;
|
||||
ci = L->base_ci + ar->i_ci;
|
||||
fp = getluaproto(ci);
|
||||
L->top--; /* pop new value */
|
||||
if (fp) { /* is a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(ci));
|
||||
if (!name || name[0] == '(') /* `(' starts private locals */
|
||||
name = NULL;
|
||||
else
|
||||
setobjs2s(ci->base+(n-1), L->top);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
static void funcinfo (lua_Debug *ar, StkId func) {
|
||||
Closure *cl = clvalue(func);
|
||||
if (cl->c.isC) {
|
||||
ar->source = "=[C]";
|
||||
ar->linedefined = -1;
|
||||
ar->what = "C";
|
||||
}
|
||||
else {
|
||||
ar->source = getstr(cl->l.p->source);
|
||||
ar->linedefined = cl->l.p->lineDefined;
|
||||
ar->what = (ar->linedefined == 0) ? "main" : "Lua";
|
||||
}
|
||||
luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
|
||||
}
|
||||
|
||||
|
||||
static const char *travglobals (lua_State *L, const TObject *o) {
|
||||
Table *g = hvalue(gt(L));
|
||||
int i = sizenode(g);
|
||||
while (i--) {
|
||||
Node *n = gnode(g, i);
|
||||
if (luaO_rawequalObj(o, gval(n)) && ttisstring(gkey(n)))
|
||||
return getstr(tsvalue(gkey(n)));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
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 */
|
||||
else {
|
||||
ar->_func = f;
|
||||
return 1;
|
||||
}
|
||||
static void info_tailcall (lua_State *L, lua_Debug *ar) {
|
||||
ar->name = ar->namewhat = "";
|
||||
ar->what = "tail";
|
||||
ar->linedefined = ar->currentline = -1;
|
||||
ar->source = "=(tail call)";
|
||||
luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
|
||||
ar->nups = 0;
|
||||
setnilvalue(L->top);
|
||||
}
|
||||
|
||||
|
||||
static int lua_nups (StkId f) {
|
||||
switch (ttype(f)) {
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
case TAG_LMARK: case TAG_CMARK:
|
||||
return f->value.cl->nelems;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int lua_currentline (lua_State *L, StkId f) {
|
||||
return hasdebuginfo(L, f) ? (f+1)->value.i : -1;
|
||||
}
|
||||
|
||||
|
||||
static Proto *getluaproto (StkId f) {
|
||||
return (ttype(f) == TAG_LMARK) ? clvalue(f)->f.l : NULL;
|
||||
}
|
||||
|
||||
|
||||
int lua_getlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
|
||||
StkId f = ar->_func;
|
||||
Proto *fp = getluaproto(f);
|
||||
if (!fp) return 0; /* `f' is not a Lua function? */
|
||||
v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
|
||||
if (!v->name) return 0;
|
||||
/* if `name', there must be a TAG_LINE */
|
||||
/* therefore, f+2 points to function base */
|
||||
LUA_ASSERT(L, ttype(f+1) == TAG_LINE, "");
|
||||
v->value = luaA_putluaObject(L, (f+2)+(v->index-1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int lua_setlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
|
||||
StkId f = ar->_func;
|
||||
Proto *fp = getluaproto(f);
|
||||
if (!fp) return 0; /* `f' is not a Lua function? */
|
||||
v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
|
||||
if (!v->name) return 0;
|
||||
LUA_ASSERT(L, ttype(f+1) == TAG_LINE, "");
|
||||
*((f+2)+(v->index-1)) = *v->value;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void lua_funcinfo (lua_Debug *ar) {
|
||||
StkId func = ar->_func;
|
||||
switch (ttype(func)) {
|
||||
case TAG_LCLOSURE: case TAG_LMARK:
|
||||
ar->source = clvalue(func)->f.l->source->str;
|
||||
ar->linedefined = clvalue(func)->f.l->lineDefined;
|
||||
ar->what = "Lua";
|
||||
break;
|
||||
case TAG_CCLOSURE: case TAG_CMARK:
|
||||
ar->source = "(C)";
|
||||
ar->linedefined = -1;
|
||||
ar->what = "C";
|
||||
break;
|
||||
default:
|
||||
LUA_INTERNALERROR(L, "invalid `func' value");
|
||||
}
|
||||
if (ar->linedefined == 0)
|
||||
ar->what = "main";
|
||||
}
|
||||
|
||||
|
||||
static int checkfunc (lua_State *L, TObject *o) {
|
||||
return luaO_equalObj(o, L->top);
|
||||
}
|
||||
|
||||
|
||||
static void lua_getobjname (lua_State *L, StkId f, lua_Debug *ar) {
|
||||
GlobalVar *g;
|
||||
/* try to find a name for given function */
|
||||
setnormalized(L->top, f); /* to be used by `checkfunc' */
|
||||
for (g=L->rootglobal; g; g=g->next) {
|
||||
if (checkfunc(L, &g->value)) {
|
||||
ar->name = g->name->str;
|
||||
ar->namewhat = "global";
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* not found: try tag methods */
|
||||
if ((ar->name = luaT_travtagmethods(L, checkfunc)) != NULL)
|
||||
ar->namewhat = "tag-method";
|
||||
else ar->namewhat = ""; /* not found at all */
|
||||
}
|
||||
|
||||
|
||||
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
StkId func = ar->_func;
|
||||
LUA_ASSERT(L, is_T_MARK(ttype(func)), "invalid activation record");
|
||||
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
|
||||
StkId f, CallInfo *ci) {
|
||||
int status = 1;
|
||||
for (; *what; what++) {
|
||||
switch (*what) {
|
||||
case 'S':
|
||||
lua_funcinfo(ar);
|
||||
case 'S': {
|
||||
funcinfo(ar, f);
|
||||
break;
|
||||
case 'l':
|
||||
ar->currentline = lua_currentline(L, func);
|
||||
}
|
||||
case 'l': {
|
||||
ar->currentline = (ci) ? currentline(ci) : -1;
|
||||
break;
|
||||
case 'u':
|
||||
ar->nups = lua_nups(func);
|
||||
}
|
||||
case 'u': {
|
||||
ar->nups = clvalue(f)->c.nupvalues;
|
||||
break;
|
||||
case 'n':
|
||||
lua_getobjname(L, func, ar);
|
||||
}
|
||||
case 'n': {
|
||||
ar->namewhat = (ci) ? getfuncname(ci, &ar->name) : NULL;
|
||||
if (ar->namewhat == NULL) {
|
||||
/* try to find a global name */
|
||||
if ((ar->name = travglobals(L, f)) != NULL)
|
||||
ar->namewhat = "global";
|
||||
else ar->namewhat = ""; /* not found */
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
setnormalized(L->top, func);
|
||||
incr_top;
|
||||
ar->func = luaA_putObjectOnTop(L);
|
||||
}
|
||||
case 'f': {
|
||||
setobj2s(L->top, f);
|
||||
break;
|
||||
default: return 0; /* invalid option */
|
||||
}
|
||||
default: status = 0; /* invalid option */
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
int status = 1;
|
||||
lua_lock(L);
|
||||
if (*what == '>') {
|
||||
StkId f = L->top - 1;
|
||||
if (!ttisfunction(f))
|
||||
luaG_runerror(L, "value for `lua_getinfo' is not a function");
|
||||
status = auxgetinfo(L, what + 1, ar, f, NULL);
|
||||
L->top--; /* pop function */
|
||||
}
|
||||
else if (ar->i_ci != 0) { /* no tail call? */
|
||||
CallInfo *ci = L->base_ci + ar->i_ci;
|
||||
lua_assert(ttisfunction(ci->base - 1));
|
||||
status = auxgetinfo(L, what, ar, ci->base - 1, ci);
|
||||
}
|
||||
else
|
||||
info_tailcall(L, ar);
|
||||
if (strchr(what, 'f')) incr_top(L);
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Symbolic Execution and code checker
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define check(x) if (!(x)) return 0;
|
||||
|
||||
#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
|
||||
|
||||
#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
|
||||
|
||||
|
||||
|
||||
static int precheck (const Proto *pt) {
|
||||
check(pt->maxstacksize <= MAXSTACK);
|
||||
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 void call_index_error (lua_State *L, TObject *o, const char *v) {
|
||||
luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o));
|
||||
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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaG_callerror (lua_State *L, TObject *func) {
|
||||
call_index_error(L, func, "call");
|
||||
static int checkRK (const Proto *pt, int r) {
|
||||
return (r < pt->maxstacksize || (r >= MAXSTACK && r-MAXSTACK < pt->sizek));
|
||||
}
|
||||
|
||||
|
||||
void luaG_indexerror (lua_State *L, TObject *t) {
|
||||
call_index_error(L, t, "index");
|
||||
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 iABx: {
|
||||
b = GETARG_Bx(i);
|
||||
if (testOpMode(op, OpModeK)) check(b < pt->sizek);
|
||||
break;
|
||||
}
|
||||
case iAsBx: {
|
||||
b = GETARG_sBx(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (testOpMode(op, OpModesetA)) {
|
||||
if (a == reg) last = pc; /* change register `a' */
|
||||
}
|
||||
if (testOpMode(op, OpModeT)) {
|
||||
check(pc+2 < pt->sizecode); /* check skip */
|
||||
check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
|
||||
}
|
||||
switch (op) {
|
||||
case OP_LOADBOOL: {
|
||||
check(c == 0 || pc+2 < pt->sizecode); /* check its jump */
|
||||
break;
|
||||
}
|
||||
case OP_LOADNIL: {
|
||||
if (a <= reg && reg <= b)
|
||||
last = pc; /* set registers from `a' to `b' */
|
||||
break;
|
||||
}
|
||||
case OP_GETUPVAL:
|
||||
case OP_SETUPVAL: {
|
||||
check(b < pt->nups);
|
||||
break;
|
||||
}
|
||||
case OP_GETGLOBAL:
|
||||
case OP_SETGLOBAL: {
|
||||
check(ttisstring(&pt->k[b]));
|
||||
break;
|
||||
}
|
||||
case OP_SELF: {
|
||||
checkreg(pt, a+1);
|
||||
if (reg == a+1) last = pc;
|
||||
break;
|
||||
}
|
||||
case OP_CONCAT: {
|
||||
/* `c' is a register, and at least two operands */
|
||||
check(c < MAXSTACK && b < c);
|
||||
break;
|
||||
}
|
||||
case OP_TFORLOOP:
|
||||
checkreg(pt, a+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: {
|
||||
int nup;
|
||||
check(b < pt->sizep);
|
||||
nup = pt->p[b]->nups;
|
||||
check(pc + nup < pt->sizecode);
|
||||
for (; nup>0; nup--) {
|
||||
OpCode op1 = GET_OPCODE(pt->code[pc+nup]);
|
||||
check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return pt->code[last];
|
||||
}
|
||||
|
||||
#undef check
|
||||
#undef checkjump
|
||||
#undef checkreg
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
int luaG_checkcode (const Proto *pt) {
|
||||
return luaG_symbexec(pt, pt->sizecode, NO_REG);
|
||||
}
|
||||
|
||||
|
||||
static const char *kname (Proto *p, int c) {
|
||||
c = c - MAXSTACK;
|
||||
if (c >= 0 && ttisstring(&p->k[c]))
|
||||
return svalue(&p->k[c]);
|
||||
else
|
||||
return "?";
|
||||
}
|
||||
|
||||
|
||||
static const char *getobjname (CallInfo *ci, int stackpos, const char **name) {
|
||||
if (isLua(ci)) { /* a Lua function? */
|
||||
Proto *p = ci_func(ci)->l.p;
|
||||
int pc = currentpc(ci);
|
||||
Instruction i;
|
||||
*name = luaF_getlocalname(p, stackpos+1, pc);
|
||||
if (*name) /* is a local? */
|
||||
return "local";
|
||||
i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
|
||||
lua_assert(pc != -1);
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_GETGLOBAL: {
|
||||
int g = GETARG_Bx(i); /* global index */
|
||||
lua_assert(ttisstring(&p->k[g]));
|
||||
*name = svalue(&p->k[g]);
|
||||
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: {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
*name = kname(p, k);
|
||||
return "field";
|
||||
}
|
||||
case OP_SELF: {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
*name = kname(p, k);
|
||||
return "method";
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return NULL; /* no useful name found */
|
||||
}
|
||||
|
||||
|
||||
static const char *getfuncname (CallInfo *ci, const char **name) {
|
||||
Instruction i;
|
||||
if ((isLua(ci) && ci->u.l.tailcalls > 0) || !isLua(ci - 1))
|
||||
return NULL; /* calling function is not Lua (or is unknown) */
|
||||
ci--; /* calling function */
|
||||
i = ci_func(ci)->l.p->code[currentpc(ci)];
|
||||
if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL)
|
||||
return getobjname(ci, GETARG_A(i), name);
|
||||
else
|
||||
return NULL; /* no useful name can be 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)
|
||||
luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
|
||||
op, kind, name, t);
|
||||
else
|
||||
luaG_runerror(L, "attempt to %s a %s value", op, t);
|
||||
}
|
||||
|
||||
|
||||
void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
|
||||
if (ttisstring(p1)) p1 = p2;
|
||||
lua_assert(!ttisstring(p1));
|
||||
luaG_typeerror(L, p1, "concatenate");
|
||||
}
|
||||
|
||||
|
||||
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])
|
||||
luaG_runerror(L, "attempt to compare two %s values", t1);
|
||||
else
|
||||
luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void addinfo (lua_State *L, const char *msg) {
|
||||
CallInfo *ci = L->ci;
|
||||
if (isLua(ci)) { /* 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: $
|
||||
** $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
|
||||
*/
|
||||
@@ -8,12 +8,24 @@
|
||||
#define ldebug_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "luadebug.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
void luaG_callerror (lua_State *L, TObject *func);
|
||||
void luaG_indexerror (lua_State *L, TObject *t);
|
||||
#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
|
||||
|
||||
644
ldo.c
644
ldo.c
@@ -1,366 +1,458 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 1.72 2000/03/30 20:55:50 roberto Exp roberto $
|
||||
** $Id: ldo.c,v 1.216 2003/02/28 19:45:15 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 LUA_REENTRANT
|
||||
#define ldo_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
#define EXTRA_STACK 32 /* space to handle stack overflow errors */
|
||||
|
||||
|
||||
/*
|
||||
** typical numer of stack slots used by a (big) function
|
||||
** (this constant is used only for choosing error messages)
|
||||
** {======================================================
|
||||
** Error-recovery functions (based on long jumps)
|
||||
** =======================================================
|
||||
*/
|
||||
#define SLOTS_PER_F 20
|
||||
|
||||
|
||||
void luaD_init (lua_State *L, int stacksize) {
|
||||
L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
|
||||
L->stack_last = L->stack+(stacksize-1);
|
||||
L->stacksize = stacksize;
|
||||
L->Cstack.base = L->Cstack.lua2C = L->top = L->stack;
|
||||
L->Cstack.num = 0;
|
||||
/* 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: do what?? */
|
||||
L->top -= EXTRA_STACK;
|
||||
lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
|
||||
}
|
||||
else {
|
||||
lua_Debug dummy;
|
||||
L->stack_last += EXTRA_STACK; /* to be used by error message */
|
||||
if (lua_getstack(L, L->stacksize/SLOTS_PER_F, &dummy) == 0) {
|
||||
/* too few funcs on stack: doesn't look like a recursion loop */
|
||||
lua_error(L, "Lua2C - C2Lua overflow");
|
||||
}
|
||||
else
|
||||
lua_error(L, "stack overflow; possible recursion loop");
|
||||
}
|
||||
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++) = TAG_NIL;
|
||||
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'
|
||||
*/
|
||||
void luaD_openstack (lua_State *L, StkId pos) {
|
||||
int i = L->top-pos;
|
||||
while (i--) pos[i+1] = pos[i];
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line) {
|
||||
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;
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
|
||||
L->Cstack.num = 0;
|
||||
ar._func = func;
|
||||
ar.event = "line";
|
||||
ar.currentline = line;
|
||||
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||
(*L->linehook)(L, &ar);
|
||||
L->allowhooks = 1;
|
||||
L->top = old_top;
|
||||
L->Cstack = oldCLS;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
|
||||
const char *event) {
|
||||
if (L->allowhooks) {
|
||||
lua_Debug ar;
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
|
||||
L->Cstack.num = 0;
|
||||
ar._func = func;
|
||||
ar.event = event;
|
||||
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||
callhook(L, &ar);
|
||||
L->allowhooks = 1;
|
||||
L->top = old_top;
|
||||
L->Cstack = oldCLS;
|
||||
ar.currentline = line;
|
||||
if (event == LUA_HOOKTAILRET)
|
||||
ar.i_ci = 0; /* tail call; no debug information about it */
|
||||
else
|
||||
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->nelems; /* number of upvalues */
|
||||
int numarg = L->top-base;
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId firstResult;
|
||||
if (nup > 0) {
|
||||
int n = numarg;
|
||||
luaD_checkstack(L, nup);
|
||||
/* open space for upvalues as extra arguments */
|
||||
while (n--) *(base+nup+n) = *(base+n);
|
||||
L->top += nup;
|
||||
numarg += nup;
|
||||
/* copy upvalues into stack */
|
||||
while (nup--) *(base+nup) = cl->consts[nup];
|
||||
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++);
|
||||
}
|
||||
L->Cstack.num = numarg;
|
||||
L->Cstack.lua2C = base;
|
||||
L->Cstack.base = L->top;
|
||||
(*cl->f.c)(L); /* do the actual call */
|
||||
firstResult = L->Cstack.base;
|
||||
L->Cstack = oldCLS;
|
||||
return firstResult;
|
||||
actual -= nfixargs; /* number of extra arguments */
|
||||
htab = luaH_new(L, actual, 1); /* 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), cast(lua_Number, actual));
|
||||
L->top -= actual; /* remove extra elements from the stack */
|
||||
sethvalue(L->top, htab);
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
|
||||
StkId base = L->top - nParams;
|
||||
luaD_openstack(L, base);
|
||||
*base = *f;
|
||||
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->u.l.tailcalls = 0;
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static StkId callrethooks (lua_State *L, StkId firstResult) {
|
||||
ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
|
||||
luaD_callhook(L, LUA_HOOKRET, -1);
|
||||
if (!(L->ci->state & CI_C)) { /* Lua function? */
|
||||
while (L->ci->u.l.tailcalls--) /* call hook for eventual tail calls */
|
||||
luaD_callhook(L, LUA_HOOKTAILRET, -1);
|
||||
}
|
||||
return restorestack(L, fr);
|
||||
}
|
||||
|
||||
|
||||
void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
|
||||
StkId res;
|
||||
if (L->hookmask & LUA_MASKRET)
|
||||
firstResult = callrethooks(L, firstResult);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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=MULT_RET.
|
||||
*/
|
||||
void luaD_call (lua_State *L, StkId func, int nResults) {
|
||||
StkId firstResult;
|
||||
lua_Hook callhook = L->callhook;
|
||||
retry: /* for `function' tag method */
|
||||
switch (ttype(func)) {
|
||||
case TAG_LCLOSURE: {
|
||||
ttype(func) = TAG_LMARK;
|
||||
if (callhook)
|
||||
luaD_callHook(L, func, callhook, "call");
|
||||
firstResult = luaV_execute(L, clvalue(func), func+1);
|
||||
break;
|
||||
lua_assert(!(L->ci->state & CI_CALLING));
|
||||
if (++L->nCcalls >= LUA_MAXCCALLS) {
|
||||
if (L->nCcalls == LUA_MAXCCALLS)
|
||||
luaG_runerror(L, "C stack overflow");
|
||||
else if (L->nCcalls >= (LUA_MAXCCALLS + (LUA_MAXCCALLS>>3)))
|
||||
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
|
||||
}
|
||||
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--;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
|
||||
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 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;
|
||||
}
|
||||
case TAG_CCLOSURE: {
|
||||
ttype(func) = TAG_CMARK;
|
||||
if (callhook)
|
||||
luaD_callHook(L, func, callhook, "call");
|
||||
firstResult = callCclosure(L, clvalue(func), func+1);
|
||||
break;
|
||||
}
|
||||
default: { /* `func' is not a function; check the `function' tag method */
|
||||
const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
|
||||
if (ttype(im) == TAG_NIL)
|
||||
luaG_callerror(L, func);
|
||||
luaD_openstack(L, func);
|
||||
*func = *im; /* tag method is the new function to be called */
|
||||
goto retry; /* retry the call */
|
||||
else { /* yielded inside a hook: just continue its execution */
|
||||
ci->state &= ~CI_YIELD;
|
||||
}
|
||||
}
|
||||
if (callhook) /* same hook that was active at entry */
|
||||
luaD_callHook(L, func, callhook, "return");
|
||||
/* adjust the number of results */
|
||||
if (nResults == MULT_RET)
|
||||
nResults = L->top - firstResult;
|
||||
else
|
||||
luaD_adjusttop(L, firstResult, nResults);
|
||||
/* move results to `func' (to erase parameters and function) */
|
||||
while (nResults) {
|
||||
*func++ = *(L->top - nResults);
|
||||
nResults--;
|
||||
}
|
||||
L->top = func;
|
||||
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 */
|
||||
}
|
||||
|
||||
|
||||
static void message (lua_State *L, const char *s) {
|
||||
const TObject *em = &(luaS_assertglobalbyname(L, "_ERRORMESSAGE")->value);
|
||||
if (*luaO_typename(em) == 'f') {
|
||||
*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
|
||||
*/
|
||||
void lua_error (lua_State *L, const char *s) {
|
||||
if (s) message(L, s);
|
||||
if (L->errorJmp)
|
||||
longjmp(L->errorJmp->b, 1);
|
||||
else {
|
||||
message(L, "unable to recover; exiting\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected call. Assumes that function is at Cstack.base and
|
||||
** parameters are on top of it.
|
||||
*/
|
||||
int luaD_protectedrun (lua_State *L) {
|
||||
struct lua_longjmp myErrorJmp;
|
||||
StkId base = L->Cstack.base;
|
||||
int numCblocks = L->numCblocks;
|
||||
LUA_API int lua_resume (lua_State *L, int nargs) {
|
||||
int status;
|
||||
struct lua_longjmp *volatile oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp.b) == 0) {
|
||||
luaD_call(L, base, MULT_RET);
|
||||
L->Cstack.lua2C = base; /* position of the new results */
|
||||
L->Cstack.num = L->top - base;
|
||||
L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
|
||||
status = 0;
|
||||
}
|
||||
else { /* an error occurred: restore the stack */
|
||||
L->Cstack.num = 0; /* no results */
|
||||
L->top = L->Cstack.base = L->Cstack.lua2C = base;
|
||||
L->numCblocks = numCblocks;
|
||||
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);
|
||||
status = 1;
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
|
||||
** Execute a protected parser.
|
||||
*/
|
||||
static int protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||
struct lua_longjmp myErrorJmp;
|
||||
StkId base = L->Cstack.base;
|
||||
int numCblocks = L->numCblocks;
|
||||
int status;
|
||||
Proto *volatile tf;
|
||||
struct lua_longjmp *volatile oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
L->top = base; /* clear C2Lua */
|
||||
if (setjmp(myErrorJmp.b) == 0) {
|
||||
tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
|
||||
status = 0;
|
||||
}
|
||||
else { /* an error occurred: restore Cstack and top */
|
||||
L->Cstack.num = 0; /* no results */
|
||||
L->top = L->Cstack.base = L->Cstack.lua2C = base;
|
||||
L->numCblocks = numCblocks;
|
||||
tf = NULL;
|
||||
status = 1;
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
if (status) return 1; /* error code */
|
||||
if (tf == NULL) return 2; /* `natural' end */
|
||||
luaV_Lclosure(L, tf, 0);
|
||||
return 0;
|
||||
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 SParser *p;
|
||||
Proto *tf;
|
||||
Closure *cl;
|
||||
luaC_checkGC(L);
|
||||
p = cast(struct SParser *, ud);
|
||||
tf = p->bin ? luaU_undump(L, p->z, &p->buff) : luaY_parser(L, p->z, &p->buff);
|
||||
cl = luaF_newLclosure(L, 0, gt(L));
|
||||
cl->l.p = tf;
|
||||
setclvalue(L->top, cl);
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
static int do_main (lua_State *L, ZIO *z, int bin) {
|
||||
int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||
struct SParser p;
|
||||
int status;
|
||||
int debug = L->debug; /* save debug status */
|
||||
do {
|
||||
long old_blocks = (luaC_checkGC(L), L->nblocks);
|
||||
status = protectedparser(L, z, bin);
|
||||
if (status == 1) return 1; /* error */
|
||||
else if (status == 2) return 0; /* `natural' end */
|
||||
else {
|
||||
unsigned long newelems2 = 2*(L->nblocks-old_blocks);
|
||||
L->GCthreshold += newelems2;
|
||||
status = luaD_protectedrun(L);
|
||||
L->GCthreshold -= newelems2;
|
||||
}
|
||||
} while (bin && status == 0);
|
||||
L->debug = debug; /* restore debug status */
|
||||
ptrdiff_t oldtopr = savestack(L, L->top); /* save current top */
|
||||
p.z = z; p.bin = bin;
|
||||
luaZ_initbuffer(L, &p.buff);
|
||||
status = luaD_rawrunprotected(L, f_parser, &p);
|
||||
luaZ_freebuffer(L, &p.buff);
|
||||
if (status != 0) { /* error? */
|
||||
StkId oldtop = restorestack(L, oldtopr);
|
||||
seterrorobj(L, status, oldtop);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
#define MAXFILENAME 260 /* maximum part of a file name kept */
|
||||
|
||||
int lua_dofile (lua_State *L, const char *filename) {
|
||||
ZIO z;
|
||||
int status;
|
||||
int bin; /* flag for file mode */
|
||||
int c; /* look ahead char */
|
||||
char source[MAXFILENAME];
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL) return 2; /* unable to open file */
|
||||
luaL_filesource(source, filename, sizeof(source));
|
||||
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 2; /* unable to reopen file */
|
||||
}
|
||||
luaZ_Fopen(&z, f, source);
|
||||
status = do_main(L, &z, bin);
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int lua_dostring (lua_State *L, const char *str) {
|
||||
return lua_dobuffer(L, str, strlen(str), str);
|
||||
}
|
||||
|
||||
|
||||
int lua_dobuffer (lua_State *L, const char *buff, int size, const char *name) {
|
||||
ZIO z;
|
||||
if (!name) name = "?";
|
||||
luaZ_mopen(&z, buff, size, name);
|
||||
return do_main(L, &z, buff[0]==ID_CHUNK);
|
||||
}
|
||||
|
||||
|
||||
52
ldo.h
52
ldo.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.h,v 1.19 2000/03/29 20:19:20 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,23 +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_openstack (lua_State *L, StkId pos);
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line);
|
||||
#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, const TObject *f, int nParams, int nResults);
|
||||
int luaD_protectedrun (lua_State *L);
|
||||
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_throw (lua_State *L, int errcode);
|
||||
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
170
ldump.c
Normal file
170
ldump.c
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
** $Id: ldump.c,v 1.4 2003/02/11 23:52:12 lhf Exp $
|
||||
** 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 DumpUpvalues(const Proto* f, DumpState* D)
|
||||
{
|
||||
int i,n=f->sizeupvalues;
|
||||
DumpInt(n,D);
|
||||
for (i=0; i<n; i++) DumpString(f->upvalues[i],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->nups,D);
|
||||
DumpByte(f->numparams,D);
|
||||
DumpByte(f->is_vararg,D);
|
||||
DumpByte(f->maxstacksize,D);
|
||||
DumpLines(f,D);
|
||||
DumpLocals(f,D);
|
||||
DumpUpvalues(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);
|
||||
}
|
||||
|
||||
/*
|
||||
** dump function as precompiled chunk
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
137
lfunc.c
137
lfunc.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 1.20 2000/03/10 18:37:44 roberto Exp roberto $
|
||||
** $Id: lfunc.c,v 1.66 2003/02/11 10:46:24 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,63 +7,113 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define lfunc_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
#define gcsizeproto(L, p) numblocks(L, 0, sizeof(Proto))
|
||||
#define gcsizeclosure(L, c) numblocks(L, c->nelems, sizeof(Closure))
|
||||
|
||||
#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) {
|
||||
Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
|
||||
sizeof(TObject)*(nelems-1));
|
||||
c->next = L->rootcl;
|
||||
L->rootcl = c;
|
||||
c->marked = 0;
|
||||
c->nelems = nelems;
|
||||
L->nblocks += gcsizeclosure(L, c);
|
||||
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 *e) {
|
||||
Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
|
||||
luaC_link(L, valtogco(c), LUA_TFUNCTION);
|
||||
c->l.isC = 0;
|
||||
c->l.g = *e;
|
||||
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);
|
||||
luaC_link(L, valtogco(f), LUA_TPROTO);
|
||||
f->k = NULL;
|
||||
f->sizek = 0;
|
||||
f->p = NULL;
|
||||
f->sizep = 0;
|
||||
f->code = NULL;
|
||||
f->sizecode = 0;
|
||||
f->sizelineinfo = 0;
|
||||
f->sizeupvalues = 0;
|
||||
f->nups = 0;
|
||||
f->upvalues = NULL;
|
||||
f->numparams = 0;
|
||||
f->is_vararg = 0;
|
||||
f->maxstacksize = 0;
|
||||
f->lineinfo = NULL;
|
||||
f->sizelocvars = 0;
|
||||
f->locvars = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->source = NULL;
|
||||
f->kstr = NULL;
|
||||
f->nkstr = 0;
|
||||
f->knum = NULL;
|
||||
f->nknum = 0;
|
||||
f->kproto = NULL;
|
||||
f->nkproto = 0;
|
||||
f->locvars = NULL;
|
||||
f->next = L->rootproto;
|
||||
L->rootproto = f;
|
||||
f->marked = 0;
|
||||
L->nblocks += gcsizeproto(L, f);
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||
L->nblocks -= gcsizeproto(L, 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);
|
||||
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_freearray(L, f->upvalues, f->sizeupvalues, TString *);
|
||||
luaM_freelem(L, f);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
L->nblocks -= gcsizeclosure(L, c);
|
||||
luaM_free(L, c);
|
||||
int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
|
||||
sizeLclosure(c->l.nupvalues);
|
||||
luaM_free(L, c, size);
|
||||
}
|
||||
|
||||
|
||||
@@ -71,22 +121,15 @@ void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
** Look for n-th local variable at line `line' in function `func'.
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
const char *luaF_getlocalname (const Proto *func,
|
||||
int local_number, int line) {
|
||||
int count = 0;
|
||||
const char *varname = NULL;
|
||||
LocVar *lv = func->locvars;
|
||||
if (lv == NULL)
|
||||
return NULL;
|
||||
for (; lv->line != -1 && lv->line < line; lv++) {
|
||||
if (lv->varname) { /* register */
|
||||
if (++count == local_number)
|
||||
varname = lv->varname->str;
|
||||
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
||||
int i;
|
||||
for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
|
||||
if (pc < f->locvars[i].endpc) { /* is variable active? */
|
||||
local_number--;
|
||||
if (local_number == 0)
|
||||
return getstr(f->locvars[i].varname);
|
||||
}
|
||||
else /* unregister */
|
||||
if (--count < local_number)
|
||||
varname = NULL;
|
||||
}
|
||||
return varname;
|
||||
return NULL; /* not found */
|
||||
}
|
||||
|
||||
|
||||
11
lfunc.h
11
lfunc.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lfunc.h,v 1.10 1999/12/27 17:33:22 roberto Exp roberto $
|
||||
** $Id: lfunc.h,v 1.20 2002/06/20 20:41:46 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,14 +11,15 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
|
||||
Proto *luaF_newproto (lua_State *L);
|
||||
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 *e);
|
||||
UpVal *luaF_findupval (lua_State *L, StkId level);
|
||||
void luaF_close (lua_State *L, StkId level);
|
||||
void luaF_freeproto (lua_State *L, Proto *f);
|
||||
void luaF_freeclosure (lua_State *L, Closure *c);
|
||||
|
||||
const char *luaF_getlocalname (const Proto *func,
|
||||
int local_number, int line);
|
||||
const char *luaF_getlocalname (const Proto *func, int local_number, int pc);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
648
lgc.c
648
lgc.c
@@ -1,269 +1,493 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 1.46 2000/03/30 20:55:50 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 1.170 2003/03/18 12:50:04 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include <string.h>
|
||||
|
||||
#define lgc_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lref.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
typedef struct GCState {
|
||||
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)))
|
||||
|
||||
#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)
|
||||
|
||||
|
||||
#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 luaD_gcTM (lua_State *L, const TObject *o) {
|
||||
const TObject *im = luaT_getimbyObj(L, o, IM_GC);
|
||||
if (ttype(im) != TAG_NIL) {
|
||||
luaD_checkstack(L, 2);
|
||||
*(L->top++) = *im;
|
||||
*(L->top++) = *o;
|
||||
luaD_call(L, L->top-2, 0);
|
||||
#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 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;
|
||||
}
|
||||
case LUA_TFUNCTION: {
|
||||
gcotocl(o)->c.gclist = st->tmark;
|
||||
st->tmark = o;
|
||||
break;
|
||||
}
|
||||
case LUA_TTABLE: {
|
||||
gcotoh(o)->gclist = st->tmark;
|
||||
st->tmark = o;
|
||||
break;
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
gcototh(o)->gclist = st->tmark;
|
||||
st->tmark = o;
|
||||
break;
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
gcotop(o)->gclist = st->tmark;
|
||||
st->tmark = o;
|
||||
break;
|
||||
}
|
||||
default: lua_assert(o->gch.tt == LUA_TSTRING);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int markobject (lua_State *L, TObject *o);
|
||||
|
||||
|
||||
/* mark a string; marks larger than 1 cannot be changed */
|
||||
#define strmark(L, s) {if ((s)->marked == 0) (s)->marked = 1;}
|
||||
|
||||
|
||||
|
||||
static void protomark (lua_State *L, Proto *f) {
|
||||
if (!f->marked) {
|
||||
int i;
|
||||
f->marked = 1;
|
||||
strmark(L, f->source);
|
||||
for (i=f->nkstr-1; i>=0; i--)
|
||||
strmark(L, f->kstr[i]);
|
||||
for (i=f->nkproto-1; i>=0; i--)
|
||||
protomark(L, f->kproto[i]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void closuremark (lua_State *L, Closure *f) {
|
||||
if (!f->marked) {
|
||||
int i = f->nelems;
|
||||
f->marked = 1;
|
||||
/* move `dead' udata that need finalization to list `tmudata' */
|
||||
void luaC_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 */
|
||||
|
||||
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(gval(n)); /* remove corresponding value ... */
|
||||
if (iscollectable(gkey(n)))
|
||||
setttype(gkey(n), LUA_TNONE); /* dead key; remove it */
|
||||
}
|
||||
|
||||
|
||||
static void traversetable (GCState *st, Table *h) {
|
||||
int i;
|
||||
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 |= cast(lu_byte, (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 */
|
||||
}
|
||||
}
|
||||
if (!weakvalue) {
|
||||
i = h->sizearray;
|
||||
while (i--)
|
||||
markobject(L, &f->consts[i]);
|
||||
markobject(st, &h->array[i]);
|
||||
}
|
||||
i = sizenode(h);
|
||||
while (i--) {
|
||||
Node *n = gnode(h, i);
|
||||
if (!ttisnil(gval(n))) {
|
||||
lua_assert(!ttisnil(gkey(n)));
|
||||
condmarkobject(st, gkey(n), !weakkey);
|
||||
condmarkobject(st, gval(n), !weakvalue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void hashmark (lua_State *L, Hash *h) {
|
||||
if (!h->marked) {
|
||||
static void traverseproto (GCState *st, Proto *f) {
|
||||
int i;
|
||||
stringmark(f->source);
|
||||
for (i=0; i<f->sizek; i++) { /* mark literal strings */
|
||||
if (ttisstring(f->k+i))
|
||||
stringmark(tsvalue(f->k+i));
|
||||
}
|
||||
for (i=0; i<f->sizeupvalues; i++) /* mark upvalue names */
|
||||
stringmark(f->upvalues[i]);
|
||||
for (i=0; i<f->sizep; i++) /* mark nested protos */
|
||||
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;
|
||||
h->marked = 1;
|
||||
for (i=h->size-1; i>=0; i--) {
|
||||
Node *n = node(h,i);
|
||||
if (ttype(key(n)) != TAG_NIL) {
|
||||
markobject(L, &n->key);
|
||||
markobject(L, &n->val);
|
||||
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->nups);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void travglobal (lua_State *L) {
|
||||
GlobalVar *gv;
|
||||
for (gv=L->rootglobal; gv; gv=gv->next) {
|
||||
LUA_ASSERT(L, gv->name->u.s.gv == gv, "inconsistent global name");
|
||||
if (gv->value.ttype != TAG_NIL) {
|
||||
strmark(L, gv->name); /* cannot collect non nil global variables */
|
||||
markobject(L, &gv->value);
|
||||
}
|
||||
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 travstack (lua_State *L) {
|
||||
int i;
|
||||
for (i = (L->top-1)-L->stack; i>=0; i--)
|
||||
markobject(L, L->stack+i);
|
||||
}
|
||||
|
||||
|
||||
static void travlock (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++) {
|
||||
if (L->refArray[i].st == LOCK)
|
||||
markobject(L, &L->refArray[i].o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int markobject (lua_State *L, TObject *o) {
|
||||
switch (ttype(o)) {
|
||||
case TAG_USERDATA: case TAG_STRING:
|
||||
strmark(L, tsvalue(o));
|
||||
break;
|
||||
case TAG_TABLE:
|
||||
hashmark(L, avalue(o));
|
||||
break;
|
||||
case TAG_LCLOSURE: case TAG_LMARK:
|
||||
protomark(L, clvalue(o)->f.l);
|
||||
/* go trhough */
|
||||
case TAG_CCLOSURE: case TAG_CMARK:
|
||||
closuremark(L, clvalue(o));
|
||||
break;
|
||||
default: break; /* numbers, etc */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
else {
|
||||
*p = next->next;
|
||||
luaF_freeproto(L, next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collectclosure (lua_State *L) {
|
||||
Closure **p = &L->rootcl;
|
||||
Closure *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->marked) {
|
||||
next->marked = 0;
|
||||
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 (next->marked) {
|
||||
next->marked = 0;
|
||||
p = &next->next;
|
||||
}
|
||||
else {
|
||||
*p = next->next;
|
||||
luaH_free(L, next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** remove from the global list globals whose names will be collected
|
||||
** (the global itself is freed when its name is freed)
|
||||
*/
|
||||
static void clear_global_list (lua_State *L, int limit) {
|
||||
GlobalVar **p = &L->rootglobal;
|
||||
GlobalVar *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->name->marked >= limit) p = &next->next;
|
||||
else *p = next->next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** collect all elements with `marked' < `limit'.
|
||||
** with limit=1, that means all unmarked elements;
|
||||
** with limit=MAX_INT, that means all elements.
|
||||
*/
|
||||
static void collectstring (lua_State *L, int limit) {
|
||||
TObject o; /* to call userdata `gc' tag method */
|
||||
int i;
|
||||
ttype(&o) = TAG_USERDATA;
|
||||
clear_global_list(L, limit);
|
||||
for (i=0; i<NUM_HASHS; i++) { /* for each hash table */
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) { /* for each list */
|
||||
TString **p = &tb->hash[j];
|
||||
TString *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->marked >= limit) {
|
||||
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
||||
next->marked = 0;
|
||||
p = &next->nexthash;
|
||||
}
|
||||
else { /* collect */
|
||||
if (next->constindex == -1) { /* is userdata? */
|
||||
tsvalue(&o) = next;
|
||||
luaD_gcTM(L, &o);
|
||||
}
|
||||
*p = next->nexthash;
|
||||
luaS_free(L, next);
|
||||
tb->nuse--;
|
||||
}
|
||||
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);
|
||||
}
|
||||
if ((tb->nuse+1)*6 < tb->size)
|
||||
luaS_resize(L, tb, tb->size/2); /* table is too big */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markall (lua_State *L) {
|
||||
travstack(L); /* mark stack objects */
|
||||
travglobal(L); /* mark global variable values and names */
|
||||
travlock(L); /* mark locked objects */
|
||||
luaT_travtagmethods(L, markobject); /* mark tag methods */
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
void luaC_collect (lua_State *L, int all) {
|
||||
int oldah = L->allowhooks;
|
||||
L->allowhooks = 0; /* stop debug hooks during GC */
|
||||
L->GCthreshold *= 4; /* to avoid GC during GC */
|
||||
collecttable(L);
|
||||
collectstring(L, all?MAX_INT:1);
|
||||
collectproto(L);
|
||||
collectclosure(L);
|
||||
L->allowhooks = oldah; /* restore hooks */
|
||||
}
|
||||
|
||||
|
||||
long lua_collectgarbage (lua_State *L, long limit) {
|
||||
unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
|
||||
markall(L);
|
||||
luaR_invalidaterefs(L);
|
||||
luaC_collect(L, 0);
|
||||
recovered = recovered - L->nblocks;
|
||||
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
||||
if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */
|
||||
L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */
|
||||
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
|
||||
/*
|
||||
** 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 = gnode(h, i);
|
||||
if (!valismarked(gkey(n))) /* key was collected? */
|
||||
removekey(n); /* remove entry from table */
|
||||
}
|
||||
l = h->gclist;
|
||||
}
|
||||
luaD_gcTM(L, &luaO_nilobject);
|
||||
return recovered;
|
||||
}
|
||||
|
||||
|
||||
void luaC_checkGC (lua_State *L) {
|
||||
if (L->nblocks >= L->GCthreshold)
|
||||
lua_collectgarbage(L, 0);
|
||||
/*
|
||||
** clear collected values from weaktables
|
||||
*/
|
||||
static void cleartablevalues (GCObject *l) {
|
||||
while (l) {
|
||||
Table *h = gcotoh(l);
|
||||
int i = h->sizearray;
|
||||
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 = gnode(h, i);
|
||||
if (!valismarked(gval(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) {
|
||||
setobj2s(L->top, tm);
|
||||
setuvalue(L->top+1, udata);
|
||||
L->top += 2;
|
||||
luaD_call(L, L->top - 2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_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_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);
|
||||
}
|
||||
|
||||
|
||||
/* 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;
|
||||
luaC_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);
|
||||
luaC_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;
|
||||
}
|
||||
|
||||
|
||||
13
lgc.h
13
lgc.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lgc.h,v 1.6 1999/10/04 17:51:04 roberto Exp roberto $
|
||||
** $Id: lgc.h,v 1.18 2003/02/10 17:32:50 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,8 +11,15 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaC_checkGC (lua_State *L);
|
||||
void luaC_collect (lua_State *L, int all);
|
||||
#define luaC_checkGC(L) { lua_assert(!(L->ci->state & CI_CALLING)); \
|
||||
if (G(L)->nblocks >= G(L)->GCthreshold) luaC_collectgarbage(L); }
|
||||
|
||||
|
||||
void luaC_separateudata (lua_State *L);
|
||||
void luaC_callGCTM (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
|
||||
|
||||
17
linit.c
17
linit.c
@@ -1,21 +1,8 @@
|
||||
/*
|
||||
** $Id: linit.c,v 1.2 1999/11/22 13:12:07 roberto Exp roberto $
|
||||
** $Id: linit.c,v 1.5 2000/06/16 17:22:43 roberto Exp roberto $
|
||||
** Initialization of libraries for lua.c
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
void lua_userinit (lua_State *L) {
|
||||
lua_beginblock(L);
|
||||
lua_iolibopen(L);
|
||||
lua_strlibopen(L);
|
||||
lua_mathlibopen(L);
|
||||
lua_dblibopen(L);
|
||||
lua_endblock(L);
|
||||
}
|
||||
>>>>>>> This module is now obsolete, and is not part of Lua. <<<<<<<<<
|
||||
|
||||
|
||||
561
llex.c
561
llex.c
@@ -1,24 +1,23 @@
|
||||
/*
|
||||
** $Id: llex.c,v 1.56 2000/04/07 13:11:49 roberto Exp roberto $
|
||||
** $Id: llex.c,v 1.118 2003/02/28 17:19:47 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define llex_c
|
||||
|
||||
#include "lauxlib.h"
|
||||
#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 "luadebug.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
@@ -26,199 +25,110 @@
|
||||
#define next(LS) (LS->current = zgetc(LS->z))
|
||||
|
||||
|
||||
#define save(L, c) luaL_addchar(L, c)
|
||||
#define save_and_next(L, LS) (save(L, LS->current), next(LS))
|
||||
|
||||
|
||||
/* 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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define MAXSRC 80
|
||||
|
||||
void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
|
||||
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
|
||||
if (val > limit) {
|
||||
msg = luaO_pushfstring(ls->L, "too many %s (limit=%d)", msg, limit);
|
||||
luaX_syntaxerror(ls, msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaX_errorline (LexState *ls, const char *s, const char *token, int line) {
|
||||
lua_State *L = ls->L;
|
||||
char buff[MAXSRC];
|
||||
luaL_chunkid(buff, zname(ls->z), sizeof(buff));
|
||||
luaL_verror(ls->L, "%.100s;\n last token read: `%.50s' 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, line, 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') {
|
||||
save(ls->L, '\0');
|
||||
luaX_syntaxerror(ls, s, luaL_buffer(ls->L));
|
||||
static void luaX_error (LexState *ls, const char *s, const char *token) {
|
||||
luaX_errorline(ls, s, token, ls->linenumber);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
const char *luaX_token2str (LexState *ls, int token) {
|
||||
if (token < FIRST_RESERVED) {
|
||||
lua_assert(token == (unsigned char)token);
|
||||
return luaO_pushfstring(ls->L, "%c", token);
|
||||
}
|
||||
else
|
||||
luaX_syntaxerror(ls, s, buff);
|
||||
return token2string[token-FIRST_RESERVED];
|
||||
}
|
||||
|
||||
|
||||
void luaX_token2str (int token, char *s) {
|
||||
if (token < 256) {
|
||||
s[0] = (char)token;
|
||||
s[1] = '\0';
|
||||
}
|
||||
static void luaX_lexerror (LexState *ls, const char *s, int token) {
|
||||
if (token == TK_EOS)
|
||||
luaX_error(ls, s, luaX_token2str(ls, token));
|
||||
else
|
||||
strcpy(s, token2string[token-FIRST_RESERVED]);
|
||||
luaX_error(ls, s, luaZ_buffer(ls->buff));
|
||||
}
|
||||
|
||||
|
||||
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 inclinenumber (LexState *LS) {
|
||||
next(LS); /* skip `\n' */
|
||||
++LS->linenumber;
|
||||
luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
|
||||
}
|
||||
|
||||
|
||||
static void firstline (LexState *LS)
|
||||
{
|
||||
int c = zgetc(LS->z);
|
||||
if (c == '#')
|
||||
while ((c=zgetc(LS->z)) != '\n' && c != EOZ) /* skip first line */;
|
||||
zungetc(LS->z);
|
||||
}
|
||||
|
||||
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z) {
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
|
||||
LS->L = L;
|
||||
LS->current = '\n';
|
||||
LS->linenumber = 0;
|
||||
LS->iflevel = 0;
|
||||
LS->ifstate[0].skip = 0;
|
||||
LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
|
||||
LS->lookahead.token = TK_EOS; /* no look-ahead token */
|
||||
LS->z = z;
|
||||
LS->fs = NULL;
|
||||
firstline(LS);
|
||||
luaL_resetbuffer(L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** PRAGMAS
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void skipspace (LexState *LS) {
|
||||
while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
|
||||
next(LS);
|
||||
}
|
||||
|
||||
|
||||
static int checkcond (lua_State *L, LexState *LS, const char *buff) {
|
||||
static const char *const opts[] = {"nil", "1", NULL};
|
||||
int i = luaL_findstring(buff, opts);
|
||||
if (i >= 0) return i;
|
||||
else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
|
||||
return luaS_globaldefined(L, buff);
|
||||
else {
|
||||
luaX_syntaxerror(LS, "invalid $if condition", buff);
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void readname (LexState *LS, char *buff) {
|
||||
int i = 0;
|
||||
skipspace(LS);
|
||||
while (isalnum(LS->current) || LS->current == '_') {
|
||||
if (i >= PRAGMASIZE) {
|
||||
buff[PRAGMASIZE] = 0;
|
||||
luaX_syntaxerror(LS, "pragma too long", buff);
|
||||
}
|
||||
buff[i++] = (char)LS->current;
|
||||
next(LS);
|
||||
}
|
||||
buff[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (lua_State *L, LexState *LS);
|
||||
|
||||
|
||||
static void ifskip (lua_State *L, LexState *LS) {
|
||||
while (LS->ifstate[LS->iflevel].skip) {
|
||||
if (LS->current == '\n')
|
||||
inclinenumber(L, LS);
|
||||
else if (LS->current == EOZ)
|
||||
luaX_error(LS, "input ends inside a $if", TK_EOS);
|
||||
else next(LS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (lua_State *L, LexState *LS) {
|
||||
static const char *const pragmas [] =
|
||||
{"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
|
||||
next(LS); /* skip '\n' */
|
||||
++LS->linenumber;
|
||||
if (LS->current == '$') { /* is a pragma? */
|
||||
char buff[PRAGMASIZE+1];
|
||||
int ifnot = 0;
|
||||
int skip = LS->ifstate[LS->iflevel].skip;
|
||||
next(LS); /* skip $ */
|
||||
readname(LS, buff);
|
||||
switch (luaL_findstring(buff, pragmas)) {
|
||||
case 0: /* debug */
|
||||
if (!skip) L->debug = 1;
|
||||
break;
|
||||
case 1: /* nodebug */
|
||||
if (!skip) L->debug = 0;
|
||||
break;
|
||||
case 2: /* endinput */
|
||||
if (!skip) {
|
||||
LS->current = EOZ;
|
||||
LS->iflevel = 0; /* to allow $endinput inside a $if */
|
||||
}
|
||||
break;
|
||||
case 3: /* end */
|
||||
if (LS->iflevel-- == 0)
|
||||
luaX_syntaxerror(LS, "unmatched $end", "$end");
|
||||
break;
|
||||
case 4: /* ifnot */
|
||||
ifnot = 1;
|
||||
/* go through */
|
||||
case 5: /* if */
|
||||
if (LS->iflevel == MAX_IFS-1)
|
||||
luaX_syntaxerror(LS, "too many nested $ifs", "$if");
|
||||
readname(LS, buff);
|
||||
LS->iflevel++;
|
||||
LS->ifstate[LS->iflevel].elsepart = 0;
|
||||
LS->ifstate[LS->iflevel].condition = checkcond(L, LS, buff) ? !ifnot : ifnot;
|
||||
LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
|
||||
break;
|
||||
case 6: /* else */
|
||||
if (LS->ifstate[LS->iflevel].elsepart)
|
||||
luaX_syntaxerror(LS, "unmatched $else", "$else");
|
||||
LS->ifstate[LS->iflevel].elsepart = 1;
|
||||
LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip ||
|
||||
LS->ifstate[LS->iflevel].condition;
|
||||
break;
|
||||
default:
|
||||
luaX_syntaxerror(LS, "unknown pragma", buff);
|
||||
}
|
||||
skipspace(LS);
|
||||
if (LS->current == '\n') /* pragma must end with a '\n' ... */
|
||||
inclinenumber(L, LS);
|
||||
else if (LS->current != EOZ) /* or eof */
|
||||
luaX_syntaxerror(LS, "invalid pragma format", buff);
|
||||
ifskip(L, LS);
|
||||
LS->linenumber = 1;
|
||||
LS->lastline = 1;
|
||||
LS->source = source;
|
||||
next(LS); /* read first char */
|
||||
if (LS->current == '#') {
|
||||
do { /* skip first line */
|
||||
next(LS);
|
||||
} while (LS->current != '\n' && LS->current != EOZ);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,147 +141,231 @@ static void inclinenumber (lua_State *L, LexState *LS) {
|
||||
*/
|
||||
|
||||
|
||||
/* use buffer to store names, literal strings and numbers */
|
||||
|
||||
static void read_long_string (lua_State *L, LexState *LS) {
|
||||
/* extra space to allocate when growing buffer */
|
||||
#define EXTRABUFF 32
|
||||
|
||||
/* 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 size_t readname (LexState *LS) {
|
||||
size_t l = 0;
|
||||
checkbuffer(LS, l);
|
||||
do {
|
||||
checkbuffer(LS, l);
|
||||
save_and_next(LS, l);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(LS, '\0', l);
|
||||
return l-1;
|
||||
}
|
||||
|
||||
|
||||
/* LUA_NUMBER */
|
||||
static void read_numeral (LexState *LS, int comma, SemInfo *seminfo) {
|
||||
size_t l = 0;
|
||||
checkbuffer(LS, l);
|
||||
if (comma) save(LS, '.', l);
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
if (LS->current == '.') {
|
||||
save_and_next(LS, l);
|
||||
if (LS->current == '.') {
|
||||
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(LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
if (LS->current == 'e' || LS->current == 'E') {
|
||||
save_and_next(LS, l); /* read `E' */
|
||||
if (LS->current == '+' || LS->current == '-')
|
||||
save_and_next(LS, l); /* optional exponent sign */
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(LS, l);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
}
|
||||
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) {
|
||||
int cont = 0;
|
||||
size_t l = 0;
|
||||
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(LS, l);
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
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);
|
||||
save_and_next(LS, l);
|
||||
if (LS->current == '[') {
|
||||
cont++;
|
||||
save_and_next(L, LS);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next(L, LS);
|
||||
save_and_next(LS, l);
|
||||
if (LS->current == ']') {
|
||||
if (cont == 0) goto endloop;
|
||||
cont--;
|
||||
save_and_next(L, LS);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
save(L, '\n');
|
||||
inclinenumber(L, LS);
|
||||
save(LS, '\n', l);
|
||||
inclinenumber(LS);
|
||||
if (!seminfo) l = 0; /* reset buffer to avoid wasting space */
|
||||
continue;
|
||||
default:
|
||||
save_and_next(L, LS);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(L, LS); /* skip the second ']' */
|
||||
LS->seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+2),
|
||||
L->Mbuffnext-L->Mbuffbase-4);
|
||||
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 (lua_State *L, LexState *LS, int del) {
|
||||
save_and_next(L, LS);
|
||||
static void read_string (LexState *LS, int del, SemInfo *seminfo) {
|
||||
size_t l = 0;
|
||||
checkbuffer(LS, l);
|
||||
save_and_next(LS, l);
|
||||
while (LS->current != del) {
|
||||
checkbuffer(LS, l);
|
||||
switch (LS->current) {
|
||||
case EOZ: case '\n':
|
||||
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'); next(LS); break;
|
||||
case 'b': save(L, '\b'); next(LS); break;
|
||||
case 'f': save(L, '\f'); next(LS); break;
|
||||
case 'n': save(L, '\n'); next(LS); break;
|
||||
case 'r': save(L, '\r'); next(LS); break;
|
||||
case 't': save(L, '\t'); next(LS); break;
|
||||
case 'v': save(L, '\v'); next(LS); break;
|
||||
case '\n': save(L, '\n'); inclinenumber(L, 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)
|
||||
luaX_error(LS, "escape sequence too large", TK_STRING);
|
||||
save(L, c);
|
||||
break;
|
||||
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);
|
||||
}
|
||||
}
|
||||
default: /* handles \\, \", \', and \? */
|
||||
save(L, LS->current);
|
||||
next(LS);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next(L, LS);
|
||||
save_and_next(LS, l);
|
||||
}
|
||||
}
|
||||
save_and_next(L, LS); /* skip delimiter */
|
||||
LS->seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+1),
|
||||
L->Mbuffnext-L->Mbuffbase-2);
|
||||
save_and_next(LS, l); /* skip delimiter */
|
||||
save(LS, '\0', l);
|
||||
seminfo->ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff) + 1, l - 3);
|
||||
}
|
||||
|
||||
|
||||
int luaX_lex (LexState *LS) {
|
||||
lua_State *L = LS->L;
|
||||
int luaX_lex (LexState *LS, SemInfo *seminfo) {
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
|
||||
case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
|
||||
next(LS);
|
||||
case '\n': {
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
|
||||
case '\n':
|
||||
inclinenumber(L, LS);
|
||||
continue;
|
||||
|
||||
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 '[':
|
||||
luaL_resetbuffer(L);
|
||||
save_and_next(L, LS);
|
||||
}
|
||||
case '[': {
|
||||
next(LS);
|
||||
if (LS->current != '[') return '[';
|
||||
else {
|
||||
save_and_next(L, LS); /* pass the second '[' */
|
||||
read_long_string(L, LS);
|
||||
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 '\'':
|
||||
luaL_resetbuffer(L);
|
||||
read_string(L, LS, LS->current);
|
||||
case '\'': {
|
||||
read_string(LS, LS->current, seminfo);
|
||||
return TK_STRING;
|
||||
|
||||
case '.':
|
||||
luaL_resetbuffer(L);
|
||||
save_and_next(L, LS);
|
||||
}
|
||||
case '.': {
|
||||
next(LS);
|
||||
if (LS->current == '.') {
|
||||
next(LS);
|
||||
if (LS->current == '.') {
|
||||
@@ -381,66 +375,43 @@ int luaX_lex (LexState *LS) {
|
||||
else return TK_CONCAT; /* .. */
|
||||
}
|
||||
else if (!isdigit(LS->current)) return '.';
|
||||
else goto fraction; /* LS->current is a digit */
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
luaL_resetbuffer(L);
|
||||
do {
|
||||
save_and_next(L, LS);
|
||||
} while (isdigit(LS->current));
|
||||
if (LS->current == '.') {
|
||||
save_and_next(L, LS);
|
||||
if (LS->current == '.') {
|
||||
save(L, '.');
|
||||
luaX_error(LS, "ambiguous syntax"
|
||||
" (decimal point x string concatenation)", TK_NUMBER);
|
||||
}
|
||||
else {
|
||||
read_numeral(LS, 1, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
fraction: /* LUA_NUMBER */
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(L, LS);
|
||||
if (LS->current == 'e' || LS->current == 'E') {
|
||||
save_and_next(L, LS); /* read 'E' */
|
||||
if (LS->current == '+' || LS->current == '-')
|
||||
save_and_next(L, LS); /* optional exponent sign */
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(L, LS);
|
||||
}
|
||||
save(L, '\0');
|
||||
if (!luaO_str2d(L->Mbuffer+L->Mbuffbase, &LS->seminfo.r))
|
||||
luaX_error(LS, "malformed number", TK_NUMBER);
|
||||
return TK_NUMBER;
|
||||
|
||||
case EOZ:
|
||||
if (LS->iflevel > 0)
|
||||
luaX_error(LS, "input ends inside a $if", TK_EOS);
|
||||
}
|
||||
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;
|
||||
luaL_resetbuffer(L);
|
||||
do {
|
||||
save_and_next(L, LS);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(L, '\0');
|
||||
ts = luaS_new(L, L->Mbuffer+L->Mbuffbase);
|
||||
if (ts->marked >= RESERVEDMARK) /* reserved word? */
|
||||
return ts->marked-RESERVEDMARK+FIRST_RESERVED;
|
||||
LS->seminfo.ts = ts;
|
||||
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
|
||||
|
||||
58
llex.h
58
llex.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.23 2000/04/07 13:11:49 roberto Exp roberto $
|
||||
** $Id: llex.h,v 1.46 2002/11/22 16:35:20 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,48 +24,52 @@
|
||||
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))
|
||||
|
||||
|
||||
/* `ifState' keeps the state of each nested $if the lexical is dealing with. */
|
||||
typedef union {
|
||||
lua_Number r;
|
||||
TString *ts;
|
||||
} SemInfo; /* semantics information */
|
||||
|
||||
struct ifState {
|
||||
int elsepart; /* true if it's in the $else part */
|
||||
int condition; /* true if $if condition is true */
|
||||
int skip; /* true if part must be skipped */
|
||||
};
|
||||
|
||||
typedef struct Token {
|
||||
int token;
|
||||
SemInfo seminfo;
|
||||
} Token;
|
||||
|
||||
|
||||
typedef struct LexState {
|
||||
int current; /* look ahead character */
|
||||
int token; /* look ahead token */
|
||||
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;
|
||||
union {
|
||||
Number r;
|
||||
TString *ts;
|
||||
} seminfo; /* semantics information */
|
||||
struct zio *z; /* input stream */
|
||||
int linenumber; /* input line counter */
|
||||
int iflevel; /* level of nested $if's (for lexical analysis) */
|
||||
struct ifState ifstate[MAX_IFS];
|
||||
ZIO *z; /* input stream */
|
||||
Mbuffer *buff; /* buffer for tokens */
|
||||
TString *source; /* current source name */
|
||||
int nestlevel; /* level of nested non-terminals */
|
||||
} LexState;
|
||||
|
||||
|
||||
void luaX_init (lua_State *L);
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z);
|
||||
int luaX_lex (LexState *LS);
|
||||
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_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);
|
||||
void luaX_errorline (LexState *ls, const char *s, const char *token, int line);
|
||||
const char *luaX_token2str (LexState *ls, int token);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
251
llimits.h
251
llimits.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.4 2000/03/31 16:28:45 roberto Exp roberto $
|
||||
** Limits, basic types, and some other "instalation-dependent" definitions
|
||||
** $Id: llimits.h,v 1.51 2002/11/25 17:47:13 roberto Exp roberto $
|
||||
** Limits, basic types, and some other `installation-dependent' definitions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -9,139 +9,149 @@
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/*
|
||||
** Define the type `number' of Lua
|
||||
** GREP LUA_NUMBER to change that
|
||||
** try to find number of bits in an integer
|
||||
*/
|
||||
#ifndef LUA_NUM_TYPE
|
||||
#define LUA_NUM_TYPE double
|
||||
#ifndef BITS_INT
|
||||
/* avoid overflows in comparison */
|
||||
#if INT_MAX-20 < 32760
|
||||
#define BITS_INT 16
|
||||
#else
|
||||
#if INT_MAX > 2147483640L
|
||||
/* machine has at least 32 bits */
|
||||
#define BITS_INT 32
|
||||
#else
|
||||
#error "you must define BITS_INT with number of bits in an integer"
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef LUA_NUM_TYPE Number;
|
||||
|
||||
/*
|
||||
** the following types define integer types for values that may not
|
||||
** fit in a `small int' (16 bits), but may waste space in a
|
||||
** `large long' (64 bits). The current definitions should work in
|
||||
** any machine, but may not be optimal.
|
||||
*/
|
||||
|
||||
/* an unsigned integer to hold hash values */
|
||||
typedef unsigned int lu_hash;
|
||||
/* its signed equivalent */
|
||||
typedef int ls_hash;
|
||||
|
||||
/* an unsigned integer big enough to count the total memory used by Lua; */
|
||||
/* it should be at least as large as size_t */
|
||||
typedef unsigned long lu_mem;
|
||||
|
||||
#define MAX_LUMEM ULONG_MAX
|
||||
|
||||
|
||||
/* an integer big enough to count the number of strings in use */
|
||||
typedef long ls_nstr;
|
||||
|
||||
/* chars used as small naturals (so that `char' is reserved for characters) */
|
||||
typedef unsigned char lu_byte;
|
||||
|
||||
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0)-2)
|
||||
|
||||
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
|
||||
|
||||
/*
|
||||
** conversion of pointer to 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(L, p) (((unsigned long)(p)) >> 3)
|
||||
|
||||
|
||||
/*
|
||||
** number of `blocks' for garbage collection: each reference to other
|
||||
** objects count 1, and each 32 bytes of `raw' memory count 1; we add
|
||||
** 2 to the total as a minimum (and also to count the overhead of malloc)
|
||||
*/
|
||||
#define numblocks(L, o,b) ((o)+((b)>>5)+2)
|
||||
|
||||
|
||||
#define MINPOWER2 4 /* minimum size for "growing" vectors */
|
||||
#define IntPoint(p) ((lu_hash)(p))
|
||||
|
||||
|
||||
|
||||
#ifndef DEFAULT_STACK_SIZE
|
||||
#define DEFAULT_STACK_SIZE 1024
|
||||
/* type to ensure maximum alignment */
|
||||
#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 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)
|
||||
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||
*/
|
||||
typedef unsigned long Instruction;
|
||||
|
||||
|
||||
/*
|
||||
** limits for opcode arguments.
|
||||
** For an instruction with 2 bytes, size is 16, and size_b can be 5
|
||||
*/
|
||||
#define SIZE_INSTRUCTION 32
|
||||
#define SIZE_OP 6
|
||||
#define SIZE_B 9
|
||||
|
||||
#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)
|
||||
|
||||
#define MAXARG_U ((1<<SIZE_U)-1)
|
||||
#define MAXARG_S (MAXARG_U>>1) /* `S' is signed */
|
||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||
/* maximum depth for calls (unsigned short) */
|
||||
#ifndef LUA_MAXCALLS
|
||||
#define LUA_MAXCALLS 4096
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** we use int to manipulate most arguments, so they must fit
|
||||
** maximum depth for C calls (unsigned short): Not too big, or may
|
||||
** overflow the C stack...
|
||||
*/
|
||||
#if MAXARG_U > MAX_INT
|
||||
#undef MAXARG_U
|
||||
#define MAXARG_U MAX_INT
|
||||
#endif
|
||||
|
||||
#if MAXARG_S > MAX_INT
|
||||
#undef MAXARG_S
|
||||
#define MAXARG_S MAX_INT
|
||||
#endif
|
||||
|
||||
#if MAXARG_A > MAX_INT
|
||||
#undef MAXARG_A
|
||||
#define MAXARG_A MAX_INT
|
||||
#endif
|
||||
|
||||
#if MAXARG_B > MAX_INT
|
||||
#undef MAXARG_B
|
||||
#define MAXARG_B MAX_INT
|
||||
#ifndef LUA_MAXCCALLS
|
||||
#define LUA_MAXCCALLS 200
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum stack size in a function */
|
||||
#define MAXSTACK MAXARG_B
|
||||
|
||||
|
||||
/* maximum number of local variables */
|
||||
#ifndef MAXLOCALS
|
||||
#define MAXLOCALS 200 /* arbitrary limit (<MAXSTACK) */
|
||||
#endif
|
||||
#if MAXLOCALS>=MAXSTACK
|
||||
#undef MAXLOCALS
|
||||
#define MAXLOCALS (MAXSTACK-1)
|
||||
/* maximum size for the C stack */
|
||||
#ifndef LUA_MAXCSTACK
|
||||
#define LUA_MAXCSTACK 2048
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of upvalues */
|
||||
/* maximum stack for a Lua function */
|
||||
#define MAXSTACK 250
|
||||
|
||||
|
||||
/* maximum number of variables declared in a function */
|
||||
#ifndef MAXVARS
|
||||
#define MAXVARS 200 /* arbitrary limit (<MAXSTACK) */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of upvalues per function */
|
||||
#ifndef MAXUPVALUES
|
||||
#define MAXUPVALUES 32 /* arbitrary limit (<=MAXARG_B) */
|
||||
#endif
|
||||
#if MAXUPVALUES>MAXARG_B
|
||||
#undef MAXUPVALUES
|
||||
#define MAXUPVALUES MAXARG_B
|
||||
#endif
|
||||
|
||||
|
||||
/* special code for multiple returns */
|
||||
#define MULT_RET 255 /* (<=MAXARG_B) */
|
||||
#if MULT_RET>MAXARG_B
|
||||
#undef MULT_RET
|
||||
#define MULT_RET 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
|
||||
|
||||
|
||||
@@ -149,45 +159,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)
|
||||
#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 number of values printed in one call to `print' */
|
||||
#ifndef MAXPRINT
|
||||
#define MAXPRINT 40 /* arbitrary limit */
|
||||
/* minimum size for string buffer */
|
||||
#ifndef LUA_MINBUFFER
|
||||
#define LUA_MINBUFFER 32
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum depth of nested $ifs */
|
||||
#ifndef MAX_IFS
|
||||
#define MAX_IFS 5 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum size of a pragma line */
|
||||
#ifndef PRAGMASIZE
|
||||
#define PRAGMASIZE 80 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
|
||||
/* 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
|
||||
|
||||
|
||||
|
||||
250
lmathlib.c
250
lmathlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.23 1999/12/27 17:33:22 roberto Exp roberto $
|
||||
** $Id: lmathlib.c,v 1.55 2003/03/11 12:24:34 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,10 +8,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define lmathlib_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
@@ -22,187 +23,224 @@
|
||||
|
||||
|
||||
/*
|
||||
** 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 void math_abs (lua_State *L) {
|
||||
lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
|
||||
static int math_abs (lua_State *L) {
|
||||
lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_sin (lua_State *L) {
|
||||
lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
|
||||
static int math_sin (lua_State *L) {
|
||||
lua_pushnumber(L, sin(TORAD(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_cos (lua_State *L) {
|
||||
lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
|
||||
static int math_cos (lua_State *L) {
|
||||
lua_pushnumber(L, cos(TORAD(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_tan (lua_State *L) {
|
||||
lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
|
||||
static int math_tan (lua_State *L) {
|
||||
lua_pushnumber(L, tan(TORAD(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_asin (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
|
||||
static int math_asin (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(asin(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_acos (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
|
||||
static int math_acos (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(acos(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_atan (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
|
||||
static int math_atan (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan(luaL_checknumber(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_atan2 (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
|
||||
static int math_atan2 (lua_State *L) {
|
||||
lua_pushnumber(L, FROMRAD(atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_ceil (lua_State *L) {
|
||||
lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
|
||||
static int math_ceil (lua_State *L) {
|
||||
lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_floor (lua_State *L) {
|
||||
lua_pushnumber(L, floor(luaL_check_number(L, 1)));
|
||||
static int math_floor (lua_State *L) {
|
||||
lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_mod (lua_State *L) {
|
||||
lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||
static int math_mod (lua_State *L) {
|
||||
lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_sqrt (lua_State *L) {
|
||||
lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
|
||||
static int math_sqrt (lua_State *L) {
|
||||
lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_pow (lua_State *L) {
|
||||
lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||
static int math_pow (lua_State *L) {
|
||||
lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_log (lua_State *L) {
|
||||
lua_pushnumber(L, log(luaL_check_number(L, 1)));
|
||||
static int math_log (lua_State *L) {
|
||||
lua_pushnumber(L, log(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_log10 (lua_State *L) {
|
||||
lua_pushnumber(L, log10(luaL_check_number(L, 1)));
|
||||
static int math_log10 (lua_State *L) {
|
||||
lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_exp (lua_State *L) {
|
||||
lua_pushnumber(L, exp(luaL_check_number(L, 1)));
|
||||
static int math_exp (lua_State *L) {
|
||||
lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_deg (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
|
||||
static int math_deg (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_rad (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
|
||||
static int math_rad (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_frexp (lua_State *L) {
|
||||
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 void math_ldexp (lua_State *L) {
|
||||
lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
|
||||
static int math_ldexp (lua_State *L) {
|
||||
lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_min (lua_State *L) {
|
||||
int i = 1;
|
||||
double dmin = luaL_check_number(L, i);
|
||||
while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
|
||||
double d = luaL_check_number(L, i);
|
||||
static int math_min (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
lua_Number dmin = luaL_checknumber(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
lua_Number d = luaL_checknumber(L, i);
|
||||
if (d < dmin)
|
||||
dmin = d;
|
||||
}
|
||||
lua_pushnumber(L, dmin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_max (lua_State *L) {
|
||||
int i = 1;
|
||||
double dmax = luaL_check_number(L, i);
|
||||
while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
|
||||
double d = luaL_check_number(L, i);
|
||||
static int math_max (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
lua_Number dmax = luaL_checknumber(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
lua_Number d = luaL_checknumber(L, i);
|
||||
if (d > dmax)
|
||||
dmax = d;
|
||||
}
|
||||
lua_pushnumber(L, dmax);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void 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;
|
||||
if (lua_getparam(L, 1) == LUA_NOOBJECT) /* no arguments? */
|
||||
lua_pushnumber(L, r); /* Number between 0 and 1 */
|
||||
else {
|
||||
int l, u; /* lower & upper limits */
|
||||
if (lua_getparam(L, 2) == LUA_NOOBJECT) { /* only one argument? */
|
||||
l = 1;
|
||||
u = luaL_check_int(L, 1);
|
||||
static int math_random (lua_State *L) {
|
||||
/* the `%' avoids the (rare) case of r==1, and is needed also because on
|
||||
some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
|
||||
lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
|
||||
switch (lua_gettop(L)) { /* check number of arguments */
|
||||
case 0: { /* no arguments */
|
||||
lua_pushnumber(L, r); /* Number between 0 and 1 */
|
||||
break;
|
||||
}
|
||||
else { /* two arguments */
|
||||
l = luaL_check_int(L, 1);
|
||||
u = luaL_check_int(L, 2);
|
||||
case 1: { /* only upper limit */
|
||||
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;
|
||||
}
|
||||
luaL_arg_check(L, l<=u, 1, "interval is empty");
|
||||
lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
|
||||
case 2: { /* lower and upper limits */
|
||||
int l = luaL_checkint(L, 1);
|
||||
int u = luaL_checkint(L, 2);
|
||||
luaL_argcheck(L, l<=u, 2, "interval is empty");
|
||||
lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */
|
||||
break;
|
||||
}
|
||||
default: return luaL_error(L, "wrong number of arguments");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_randomseed (lua_State *L) {
|
||||
srand(luaL_check_int(L, 1));
|
||||
static int math_randomseed (lua_State *L) {
|
||||
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
|
||||
*/
|
||||
void lua_mathlibopen (lua_State *L) {
|
||||
luaL_openl(L, mathlib);
|
||||
LUALIB_API int luaopen_math (lua_State *L) {
|
||||
luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
|
||||
lua_pushliteral(L, "pi");
|
||||
lua_pushnumber(L, PI);
|
||||
lua_settable(L, -3);
|
||||
lua_pushliteral(L, "__pow");
|
||||
lua_pushcfunction(L, math_pow);
|
||||
lua_pushnumber(L, 0); /* to get its tag */
|
||||
lua_settagmethod(L, lua_tag(L, lua_pop(L)), "pow");
|
||||
lua_pushnumber(L, PI); lua_setglobal(L, "PI");
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
163
lmem.c
163
lmem.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.28 2000/03/10 18:37:44 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,136 +7,85 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define lmem_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Number ANSI systems do not need these tests;
|
||||
** but some systems (Sun OS) are not that ANSI...
|
||||
** 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.)
|
||||
*/
|
||||
#ifdef OLD_ANSI
|
||||
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
||||
#define free(b) if (b) (free)(b)
|
||||
#ifndef l_realloc
|
||||
#define l_realloc(b,os,s) realloc(b,s)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
/*
|
||||
** {======================================================================
|
||||
** Controlled version for realloc.
|
||||
** =======================================================================
|
||||
** definition for free function. (`os' is the old block size; some
|
||||
** allocators may use that.)
|
||||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define realloc(b, s) debug_realloc(b, s)
|
||||
#define malloc(b) debug_realloc(NULL, 0)
|
||||
#define free(b) debug_realloc(b, 0)
|
||||
|
||||
|
||||
/* ensures maximum alignment for HEADER */
|
||||
#define HEADER (sizeof(double)>sizeof(long) ? sizeof(double) : sizeof(long))
|
||||
|
||||
#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;
|
||||
|
||||
|
||||
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 {
|
||||
size_t realsize = HEADER+size+MARKSIZE;
|
||||
char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
|
||||
int i;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================================== */
|
||||
#ifndef l_free
|
||||
#define l_free(b,os) free(b)
|
||||
#endif
|
||||
|
||||
|
||||
#define MINSIZEARRAY 4
|
||||
|
||||
void *luaM_growaux (lua_State *L, void *block, unsigned long nelems,
|
||||
int inc, int size, const char *errormsg, unsigned long limit) {
|
||||
unsigned long newn = nelems+inc;
|
||||
if (newn >= limit) 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, unsigned long 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)
|
||||
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;
|
||||
}
|
||||
else if ((size_t)size != size)
|
||||
lua_error(L, "memory allocation error: block too big");
|
||||
block = realloc(block, size);
|
||||
if (block == NULL)
|
||||
lua_error(L, memEM);
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
52
lmem.h
52
lmem.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.12 2000/01/13 16:30:47 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
|
||||
*/
|
||||
@@ -8,36 +8,36 @@
|
||||
#define lmem_h
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
/* memory error messages */
|
||||
#define codeEM "code size overflow"
|
||||
#define constantEM "constant table overflow"
|
||||
#define refEM "reference table overflow"
|
||||
#define tableEM "table overflow"
|
||||
#define memEM "not enough memory"
|
||||
#define arrEM "internal array larger than `int' limit"
|
||||
|
||||
void *luaM_realloc (lua_State *L, void *oldblock, unsigned long size);
|
||||
void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, int inc, int size,
|
||||
const char *errormsg, unsigned long 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)*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)*sizeof(t)))
|
||||
#define MEMERRMSG "not enough memory"
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
extern unsigned long memdebug_numblocks;
|
||||
extern unsigned long memdebug_total;
|
||||
extern unsigned long memdebug_maxmem;
|
||||
#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
|
||||
|
||||
205
loadlib.c
Normal file
205
loadlib.c
Normal file
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
** $Id: loadlib.c,v 1.3 2003/04/02 13:09:14 roberto Exp roberto $
|
||||
** Dynamic library loader for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*
|
||||
* This Lua library exports a single function, called loadlib, which is
|
||||
* called from Lua as loadlib(lib,init), where lib is the full name of the
|
||||
* library to be loaded (including the complete path) and init is the name
|
||||
* of a function to be called after the library is loaded. Typically, this
|
||||
* function will register other functions, thus making the complete library
|
||||
* available to Lua. The init function is *not* automatically called by
|
||||
* loadlib. Instead, loadlib returns the init function as a Lua function
|
||||
* that the client can call when it thinks is appropriate. In the case of
|
||||
* errors, loadlib returns nil and two strings describing the error.
|
||||
* The first string is supplied by the operating system; it should be
|
||||
* informative and useful for error messages. The second string is "open",
|
||||
* "init", or "absent" to identify the error and is meant to be used for
|
||||
* making decisions without having to look into the first string (whose
|
||||
* format is system-dependent).
|
||||
*
|
||||
* This module contains an implementation of loadlib for Unix systems that
|
||||
* have dlfcn, an implementation for Windows, and a stub for other systems.
|
||||
* See the list at the end of this file for some links to available
|
||||
* implementations of dlfcn and interfaces to other native dynamic loaders
|
||||
* on top of which loadlib could be implemented.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#undef LOADLIB
|
||||
|
||||
|
||||
#ifdef USE_DLOPEN
|
||||
#define LOADLIB
|
||||
/*
|
||||
* This is an implementation of loadlib based on the dlfcn interface.
|
||||
* The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
|
||||
* NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
|
||||
* as an emulation layer on top of native functions.
|
||||
*/
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
static int loadlib(lua_State *L)
|
||||
{
|
||||
const char *path=luaL_checkstring(L,1);
|
||||
const char *init=luaL_checkstring(L,2);
|
||||
void *lib=dlopen(path,RTLD_NOW);
|
||||
if (lib!=NULL)
|
||||
{
|
||||
lua_CFunction f=(lua_CFunction) dlsym(lib,init);
|
||||
if (f!=NULL)
|
||||
{
|
||||
lua_pushlightuserdata(L,lib);
|
||||
lua_pushcclosure(L,f,1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* else return appropriate error messages */
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L,dlerror());
|
||||
lua_pushstring(L,(lib!=NULL) ? "init" : "open");
|
||||
if (lib!=NULL) dlclose(lib);
|
||||
return 3;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** In Windows, default is to use dll; otherwise, default is not to use dll
|
||||
*/
|
||||
#ifndef USE_DLL
|
||||
#ifdef _WIN32
|
||||
#define USE_DLL 1
|
||||
#else
|
||||
#define USE_DLL 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#if USE_DLL
|
||||
#define LOADLIB
|
||||
/*
|
||||
* This is an implementation of loadlib for Windows using native functions.
|
||||
*/
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
static void pusherror(lua_State *L)
|
||||
{
|
||||
int error=GetLastError();
|
||||
char buffer[128];
|
||||
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
0, error, 0, buffer, sizeof(buffer), 0))
|
||||
lua_pushstring(L,buffer);
|
||||
else
|
||||
lua_pushfstring(L,"system error %d\n",error);
|
||||
}
|
||||
|
||||
static int loadlib(lua_State *L)
|
||||
{
|
||||
const char *path=luaL_checkstring(L,1);
|
||||
const char *init=luaL_checkstring(L,2);
|
||||
HINSTANCE lib=LoadLibrary(path);
|
||||
if (lib!=NULL)
|
||||
{
|
||||
lua_CFunction f=(lua_CFunction) GetProcAddress(lib,init);
|
||||
if (f!=NULL)
|
||||
{
|
||||
lua_pushlightuserdata(L,lib);
|
||||
lua_pushcclosure(L,f,1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
lua_pushnil(L);
|
||||
pusherror(L);
|
||||
lua_pushstring(L,(lib!=NULL) ? "init" : "open");
|
||||
if (lib!=NULL) FreeLibrary(lib);
|
||||
return 3;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef LOADLIB
|
||||
/* Fallback for other systems */
|
||||
|
||||
/*
|
||||
** Those systems support dlopen, so they should have defined USE_DLOPEN.
|
||||
** The default (no)implementation gives them a special error message.
|
||||
*/
|
||||
#ifdef linux
|
||||
#define LOADLIB
|
||||
#endif
|
||||
|
||||
#ifdef sun
|
||||
#define LOADLIB
|
||||
#endif
|
||||
|
||||
#ifdef sgi
|
||||
#define LOADLIB
|
||||
#endif
|
||||
|
||||
#ifdef BSD
|
||||
#define LOADLIB
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#define LOADLIB
|
||||
#endif
|
||||
|
||||
#ifdef LOADLIB
|
||||
#undef LOADLIB
|
||||
#define LOADLIB "`loadlib' not installed (check your Lua configuration)"
|
||||
#else
|
||||
#define LOADLIB "`loadlib' not supported"
|
||||
#endif
|
||||
|
||||
static int loadlib(lua_State *L)
|
||||
{
|
||||
lua_pushnil(L);
|
||||
lua_pushliteral(L,LOADLIB);
|
||||
lua_pushliteral(L,"absent");
|
||||
return 3;
|
||||
}
|
||||
#endif
|
||||
|
||||
LUALIB_API int luaopen_loadlib (lua_State *L)
|
||||
{
|
||||
lua_register(L,"loadlib",loadlib);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Here are some links to available implementations of dlfcn and
|
||||
* interfaces to other native dynamic loaders on top of which loadlib
|
||||
* could be implemented. Please send contributions and corrections to us.
|
||||
*
|
||||
* AIX
|
||||
* Starting with AIX 4.2, dlfcn is included in the base OS.
|
||||
* There is also an emulation package available.
|
||||
* http://www.faqs.org/faqs/aix-faq/part4/section-21.html
|
||||
*
|
||||
* HPUX
|
||||
* HPUX 11 has dlfcn. For HPUX 10 use shl_*.
|
||||
* http://www.geda.seul.org/mailinglist/geda-dev37/msg00094.html
|
||||
* http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
|
||||
*
|
||||
* Macintosh, Windows
|
||||
* http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html
|
||||
*
|
||||
* Mac OS X/Darwin
|
||||
* http://www.opendarwin.org/projects/dlcompat/
|
||||
*
|
||||
* GLIB has wrapper code for BeOS, OS2, Unix and Windows
|
||||
* http://cvs.gnome.org/lxr/source/glib/gmodule/
|
||||
*
|
||||
*/
|
||||
247
lobject.c
247
lobject.c
@@ -1,110 +1,195 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 1.35 2000/03/29 20:19:20 roberto Exp roberto $
|
||||
** $Id: lobject.c,v 1.96 2003/02/18 16:02:56 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define lobject_c
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
const char *const luaO_typenames[] = { /* ORDER LUA_T */
|
||||
"userdata", "number", "string", "table", "function", "function", "nil",
|
||||
"function", "function", "line"
|
||||
};
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
const TObject luaO_nilobject = {TAG_NIL, {NULL}};
|
||||
/* 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}};
|
||||
|
||||
|
||||
/*
|
||||
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
||||
** converts an integer to a "floating point byte", represented as
|
||||
** (mmmmmxxx), where the real value is (xxx) * 2^(mmmmm)
|
||||
*/
|
||||
unsigned long luaO_power2 (unsigned long n) {
|
||||
unsigned long p = MINPOWER2;
|
||||
while (p<=n) p<<=1;
|
||||
return p;
|
||||
int luaO_int2fb (unsigned int x) {
|
||||
int m = 0; /* mantissa */
|
||||
while (x >= (1<<3)) {
|
||||
x = (x+1) >> 1;
|
||||
m++;
|
||||
}
|
||||
return (m << 3) | cast(int, x);
|
||||
}
|
||||
|
||||
|
||||
int luaO_equalval (const TObject *t1, const TObject *t2) {
|
||||
switch (ttype(t1)) {
|
||||
case TAG_NUMBER:
|
||||
int luaO_log2 (unsigned int x) {
|
||||
static const lu_byte log_8[255] = {
|
||||
0,
|
||||
1,1,
|
||||
2,2,2,2,
|
||||
3,3,3,3,3,3,3,3,
|
||||
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
|
||||
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
|
||||
};
|
||||
if (x >= 0x00010000) {
|
||||
if (x >= 0x01000000) return log_8[((x>>24) & 0xff) - 1]+24;
|
||||
else return log_8[((x>>16) & 0xff) - 1]+16;
|
||||
}
|
||||
else {
|
||||
if (x >= 0x00000100) return log_8[((x>>8) & 0xff) - 1]+8;
|
||||
else if (x) return log_8[(x & 0xff) - 1];
|
||||
return -1; /* special `log' for 0 */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaO_rawequalObj (const TObject *t1, const TObject *t2) {
|
||||
if (ttype(t1) != ttype(t2)) return 0;
|
||||
else switch (ttype(t1)) {
|
||||
case LUA_TNIL:
|
||||
return 1;
|
||||
case LUA_TNUMBER:
|
||||
return nvalue(t1) == nvalue(t2);
|
||||
case TAG_STRING: case TAG_USERDATA:
|
||||
return svalue(t1) == svalue(t2);
|
||||
case TAG_TABLE:
|
||||
return avalue(t1) == avalue(t2);
|
||||
case TAG_CCLOSURE: case TAG_LCLOSURE:
|
||||
return clvalue(t1) == clvalue(t2);
|
||||
case LUA_TBOOLEAN:
|
||||
return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
return pvalue(t1) == pvalue(t2);
|
||||
default:
|
||||
LUA_ASSERT(L, ttype(t1) == TAG_NIL, "invalid type");
|
||||
return 1; /* TAG_NIL */
|
||||
lua_assert(iscollectable(t1));
|
||||
return gcvalue(t1) == gcvalue(t2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static double expten (unsigned int e) {
|
||||
double exp = 10.0;
|
||||
double res = 1.0;
|
||||
for (; e; e>>=1) {
|
||||
if (e & 1) res *= exp;
|
||||
exp *= exp;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
|
||||
double a = 0.0;
|
||||
int point = 0; /* number of decimal digits */
|
||||
int sig;
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
sig = 0;
|
||||
switch (*s) {
|
||||
case '-': sig = 1; /* go through */
|
||||
case '+': s++;
|
||||
}
|
||||
if (! (isdigit((unsigned char)*s) ||
|
||||
(*s == '.' && isdigit((unsigned char)*(s+1)))))
|
||||
return 0; /* not (at least one digit before or after the point) */
|
||||
while (isdigit((unsigned char)*s))
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
if (*s == '.') {
|
||||
s++;
|
||||
while (isdigit((unsigned char)*s)) {
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
point++;
|
||||
}
|
||||
}
|
||||
if (sig) a = -a;
|
||||
if (*s == 'e' || *s == 'E') {
|
||||
int e = 0;
|
||||
s++;
|
||||
sig = 0;
|
||||
switch (*s) {
|
||||
case '-': sig = 1; /* go through */
|
||||
case '+': s++;
|
||||
}
|
||||
if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
|
||||
do {
|
||||
e = 10*e + (*(s++)-'0');
|
||||
} while (isdigit((unsigned char)*s));
|
||||
if (sig) e = -e;
|
||||
point -= e;
|
||||
}
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
if (*s != '\0') return 0; /* invalid trailing characters? */
|
||||
if (point != 0) {
|
||||
if (point > 0) a /= expten(point);
|
||||
else a *= expten(-point);
|
||||
}
|
||||
*result = a;
|
||||
int luaO_str2d (const char *s, lua_Number *result) {
|
||||
char *endptr;
|
||||
lua_Number res = lua_str2number(s, &endptr);
|
||||
if (endptr == s) return 0; /* no conversion */
|
||||
while (isspace((unsigned char)(*endptr))) endptr++;
|
||||
if (*endptr != '\0') return 0; /* invalid trailing characters? */
|
||||
*result = res;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void pushstr (lua_State *L, const char *str) {
|
||||
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, cast(lua_Number, va_arg(argp, int)));
|
||||
incr_top(L);
|
||||
break;
|
||||
case 'f':
|
||||
setnvalue(L->top, cast(lua_Number, 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;
|
||||
va_start(argp, fmt);
|
||||
msg = luaO_pushvfstring(L, fmt, argp);
|
||||
va_end(argp);
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
void luaO_chunkid (char *out, const char *source, int bufflen) {
|
||||
if (*source == '=') {
|
||||
strncpy(out, source+1, bufflen); /* remove first char */
|
||||
out[bufflen-1] = '\0'; /* ensures null termination */
|
||||
}
|
||||
else { /* out = "source", or "...source" */
|
||||
if (*source == '@') {
|
||||
int l;
|
||||
source++; /* skip the `@' */
|
||||
bufflen -= sizeof(" `...' ");
|
||||
l = strlen(source);
|
||||
strcpy(out, "");
|
||||
if (l>bufflen) {
|
||||
source += (l-bufflen); /* get last part of file name */
|
||||
strcat(out, "...");
|
||||
}
|
||||
strcat(out, source);
|
||||
}
|
||||
else { /* out = [string "string"] */
|
||||
int len = strcspn(source, "\n"); /* stop at first newline */
|
||||
bufflen -= sizeof(" [string \"...\"] ");
|
||||
if (len > bufflen) len = bufflen;
|
||||
strcpy(out, "[string \"");
|
||||
if (source[len] != '\0') { /* must truncate? */
|
||||
strncat(out, source, len);
|
||||
strcat(out, "...");
|
||||
}
|
||||
else
|
||||
strcat(out, source);
|
||||
strcat(out, "\"]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
381
lobject.h
381
lobject.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 1.59 2000/03/31 16:28:45 roberto Exp roberto $
|
||||
** $Id: lobject.h,v 1.158 2003/02/18 16:02:56 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,178 +12,325 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
#define LUA_INTERNALERROR(L,s) assert(((void)s,0))
|
||||
#define LUA_ASSERT(L,c,s) assert(((void)s,(c)))
|
||||
#else
|
||||
#define LUA_INTERNALERROR(L,s) /* empty */
|
||||
#define LUA_ASSERT(L,c,s) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef 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
|
||||
/* tags for values visible from Lua */
|
||||
#define NUM_TAGS LUA_TTHREAD
|
||||
|
||||
|
||||
/*
|
||||
** Lua TYPES
|
||||
** WARNING: if you change the order of this enumeration,
|
||||
** grep "ORDER LUA_T"
|
||||
** Extra tags for non-values
|
||||
*/
|
||||
typedef enum {
|
||||
TAG_USERDATA = 0, /* default tag for userdata */
|
||||
TAG_NUMBER, /* fixed tag for numbers */
|
||||
TAG_STRING, /* fixed tag for strings */
|
||||
TAG_TABLE, /* default tag for tables */
|
||||
TAG_LCLOSURE, /* fixed tag for Lua closures */
|
||||
TAG_CCLOSURE, /* fixed tag for C closures */
|
||||
TAG_NIL, /* last "pre-defined" tag */
|
||||
|
||||
TAG_LMARK, /* mark for Lua closures */
|
||||
TAG_CMARK, /* mark for C closures */
|
||||
|
||||
TAG_LINE
|
||||
} lua_Type;
|
||||
|
||||
/* tags for values visible from Lua == first user-created tag */
|
||||
#define NUM_TAGS 7
|
||||
#define LUA_TPROTO (NUM_TAGS+1)
|
||||
#define LUA_TUPVAL (NUM_TAGS+2)
|
||||
|
||||
|
||||
/*
|
||||
** check whether `t' is a mark
|
||||
** Union of all collectable objects
|
||||
*/
|
||||
#define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK)
|
||||
typedef union GCObject GCObject;
|
||||
|
||||
|
||||
/*
|
||||
** 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
|
||||
|
||||
|
||||
/*
|
||||
** Common header in struct form
|
||||
*/
|
||||
typedef struct GCheader {
|
||||
CommonHeader;
|
||||
} GCheader;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Union of all Lua values
|
||||
*/
|
||||
typedef union {
|
||||
struct TString *ts; /* TAG_STRING, TAG_USERDATA */
|
||||
struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_[CL]MARK */
|
||||
struct Hash *a; /* TAG_TABLE */
|
||||
Number n; /* TAG_NUMBER */
|
||||
int i; /* TAG_LINE */
|
||||
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 svalue(o) ((o)->value.ts->str)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define clvalue(o) ((o)->value.cl)
|
||||
#define avalue(o) ((o)->value.a)
|
||||
|
||||
|
||||
typedef struct TObject {
|
||||
lua_Type ttype;
|
||||
/*
|
||||
** Lua values (or `tagged objects')
|
||||
*/
|
||||
typedef struct lua_TObject {
|
||||
int tt;
|
||||
Value value;
|
||||
} TObject;
|
||||
|
||||
|
||||
typedef struct GlobalVar {
|
||||
TObject value;
|
||||
struct GlobalVar *next;
|
||||
struct TString *name;
|
||||
} GlobalVar;
|
||||
/* 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
|
||||
*/
|
||||
typedef struct TString {
|
||||
union {
|
||||
struct { /* for strings */
|
||||
GlobalVar *gv; /* eventual global value with this name */
|
||||
long len;
|
||||
} s;
|
||||
struct { /* for userdata */
|
||||
int tag;
|
||||
void *value;
|
||||
} d;
|
||||
} u;
|
||||
struct TString *nexthash; /* chain for hash table */
|
||||
unsigned long hash;
|
||||
int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
|
||||
unsigned char marked;
|
||||
char str[1]; /* 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 {
|
||||
struct Proto *next;
|
||||
int marked;
|
||||
struct TString **kstr; /* strings used by the function */
|
||||
int nkstr; /* size of `kstr' */
|
||||
Number *knum; /* Number numbers used by the function */
|
||||
int nknum; /* size of `knum' */
|
||||
struct Proto **kproto; /* functions defined inside the function */
|
||||
int nkproto; /* size of `kproto' */
|
||||
Instruction *code; /* ends with opcode ENDCODE */
|
||||
int lineDefined;
|
||||
CommonHeader;
|
||||
TObject *k; /* constants used by the function */
|
||||
Instruction *code;
|
||||
struct Proto **p; /* functions defined inside the function */
|
||||
int *lineinfo; /* map from opcodes to source lines */
|
||||
struct LocVar *locvars; /* information about local variables */
|
||||
TString **upvalues; /* upvalue names */
|
||||
TString *source;
|
||||
int numparams;
|
||||
int is_vararg;
|
||||
int maxstacksize;
|
||||
struct LocVar *locvars; /* ends with line = -1 */
|
||||
int sizeupvalues;
|
||||
int sizek; /* size of `k' */
|
||||
int sizecode;
|
||||
int sizelineinfo;
|
||||
int sizep; /* size of `p' */
|
||||
int sizelocvars;
|
||||
int lineDefined;
|
||||
GCObject *gclist;
|
||||
lu_byte nups; /* number of upvalues */
|
||||
lu_byte numparams;
|
||||
lu_byte is_vararg;
|
||||
lu_byte maxstacksize;
|
||||
} Proto;
|
||||
|
||||
|
||||
typedef struct LocVar {
|
||||
TString *varname; /* NULL signals end of scope */
|
||||
int line;
|
||||
TString *varname;
|
||||
int startpc; /* first point where variable is active */
|
||||
int endpc; /* first point where variable is dead */
|
||||
} LocVar;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** 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 {
|
||||
struct Closure *next;
|
||||
int marked;
|
||||
union {
|
||||
lua_CFunction c; /* C functions */
|
||||
struct Proto *l; /* Lua functions */
|
||||
} f;
|
||||
int nelems;
|
||||
TObject consts[1];
|
||||
|
||||
#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)->c.isC)
|
||||
#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
|
||||
|
||||
|
||||
/*
|
||||
** Tables
|
||||
*/
|
||||
|
||||
typedef struct Node {
|
||||
TObject key;
|
||||
TObject val;
|
||||
TObject i_key;
|
||||
TObject i_val;
|
||||
struct Node *next; /* for chaining */
|
||||
} Node;
|
||||
|
||||
typedef struct Hash {
|
||||
int htag;
|
||||
|
||||
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;
|
||||
int size;
|
||||
Node *firstfree; /* this position is free; all positions after it are full */
|
||||
struct Hash *next;
|
||||
int marked;
|
||||
} Hash;
|
||||
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))
|
||||
|
||||
|
||||
|
||||
extern const char *const luaO_typenames[];
|
||||
extern const TObject luaO_nilobject;
|
||||
|
||||
#define luaO_typename(o) luaO_typenames[ttype(o)]
|
||||
int luaO_log2 (unsigned int x);
|
||||
int luaO_int2fb (unsigned int x);
|
||||
#define fb2int(x) (((x) & 7) << ((x) >> 3))
|
||||
|
||||
unsigned long luaO_power2 (unsigned long n);
|
||||
int luaO_rawequalObj (const TObject *t1, const TObject *t2);
|
||||
int luaO_str2d (const char *s, lua_Number *result);
|
||||
|
||||
#define luaO_equalObj(t1,t2) (ttype(t1) == ttype(t2) && luaO_equalval(t1,t2))
|
||||
int luaO_equalval (const TObject *t1, const TObject *t2);
|
||||
int luaO_redimension (lua_State *L, int oldsize);
|
||||
int luaO_str2d (const char *s, Number *result);
|
||||
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp);
|
||||
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
|
||||
void luaO_chunkid (char *out, const char *source, int len);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
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 */
|
||||
};
|
||||
|
||||
263
lopcodes.h
263
lopcodes.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.58 2000/04/13 16:51:01 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
|
||||
instalations, 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,111 +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_0 t t t[i+a*FPF]=v_i */
|
||||
OP_SETMAP,/* U v_u k_u - v_0 k_0 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_CLOSURE,/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
|
||||
|
||||
OP_SETLINE/* U - - LINE=u */
|
||||
OP_SETLIST,/* A Bx R(A)[Bx-Bx%FPF+i] := R(A+i), 1 <= i <= Bx%FPF+1 */
|
||||
OP_SETLISTO,/* A Bx */
|
||||
|
||||
OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
|
||||
OP_CLOSURE/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */
|
||||
} OpCode;
|
||||
|
||||
|
||||
#define NUM_OPCODES (cast(int, OP_CLOSURE+1))
|
||||
|
||||
#define ISJUMP(o) (OP_JMPNE <= (o) && (o) <= OP_JMP)
|
||||
|
||||
|
||||
/*===========================================================================
|
||||
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
|
||||
|
||||
56
lparser.h
56
lparser.h
@@ -1,13 +1,15 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.15 2000/04/05 17:51:58 roberto Exp roberto $
|
||||
** LL(1) Parser and code generator for Lua
|
||||
** $Id: lparser.h,v 1.46 2002/12/19 11:11:55 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,46 +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 `lasttarged' */
|
||||
int stacklevel; /* number of values on activation register */
|
||||
int nlocalvar; /* number of active local variables */
|
||||
int nupvalues; /* number of upvalues */
|
||||
int nvars; /* number of entries in f->locvars (-1 if no debug information) */
|
||||
int lastsetline; /* line where last SETLINE was issued */
|
||||
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 */
|
||||
TString *localvar[MAXLOCALS]; /* store local variable names */
|
||||
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
|
||||
|
||||
115
lref.c
115
lref.c
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
** $Id: lref.c,v 1.10 2000/03/27 20:10:21 roberto Exp roberto $
|
||||
** reference mechanism
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#define LUA_REENTRANT
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lmem.h"
|
||||
#include "lref.h"
|
||||
#include "lstate.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
int lua_ref (lua_State *L, int lock) {
|
||||
int ref;
|
||||
luaA_checkCargs(L, 1);
|
||||
if (ttype(L->top-1) == TAG_NIL)
|
||||
ref = LUA_REFNIL;
|
||||
else {
|
||||
if (L->refFree != NONEXT) { /* is there a free place? */
|
||||
ref = L->refFree;
|
||||
L->refFree = L->refArray[ref].st;
|
||||
}
|
||||
else { /* no more free places */
|
||||
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref, refEM, MAX_INT);
|
||||
ref = L->refSize++;
|
||||
}
|
||||
L->refArray[ref].o = *(L->top-1);
|
||||
L->refArray[ref].st = lock ? LOCK : HOLD;
|
||||
}
|
||||
L->top--;
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
void lua_unref (lua_State *L, int ref) {
|
||||
if (ref >= 0) {
|
||||
if (ref >= L->refSize || L->refArray[ref].st >= 0)
|
||||
lua_error(L, "Lua API error - "
|
||||
"invalid argument for function `lua_unref'");
|
||||
L->refArray[ref].st = L->refFree;
|
||||
L->refFree = ref;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_getref (lua_State *L, int ref) {
|
||||
if (ref == LUA_REFNIL)
|
||||
return luaA_putluaObject(L, &luaO_nilobject);
|
||||
else if (0 <= ref && ref < L->refSize &&
|
||||
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
|
||||
return luaA_putluaObject(L, &L->refArray[ref].o);
|
||||
else
|
||||
return LUA_NOOBJECT;
|
||||
}
|
||||
|
||||
|
||||
void lua_beginblock (lua_State *L) {
|
||||
luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
|
||||
"too many nested blocks", L->stacksize);
|
||||
L->Cblocks[L->numCblocks] = L->Cstack;
|
||||
L->numCblocks++;
|
||||
}
|
||||
|
||||
|
||||
void lua_endblock (lua_State *L) {
|
||||
if (L->numCblocks <= 0)
|
||||
lua_error(L, "Lua API error - no block to end");
|
||||
--L->numCblocks;
|
||||
L->Cstack = L->Cblocks[L->numCblocks];
|
||||
L->top = L->Cstack.base;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static int ismarked (const TObject *o) {
|
||||
/* valid only for locked objects */
|
||||
switch (o->ttype) {
|
||||
case TAG_STRING: case TAG_USERDATA:
|
||||
return o->value.ts->marked;
|
||||
case TAG_TABLE:
|
||||
return o->value.a->marked;
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
return o->value.cl->marked;
|
||||
default: /* number */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* for internal debugging only; check if a link of free refs is valid */
|
||||
#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
|
||||
|
||||
void luaR_invalidaterefs (lua_State *L) {
|
||||
int n = L->refSize;
|
||||
int i;
|
||||
for (i=0; i<n; i++) {
|
||||
struct Ref *r = &L->refArray[i];
|
||||
if (r->st == HOLD && !ismarked(&r->o))
|
||||
r->st = COLLECTED;
|
||||
LUA_ASSERT(L, (r->st == LOCK && ismarked(&r->o)) ||
|
||||
r->st == COLLECTED ||
|
||||
r->st == NONEXT ||
|
||||
(r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
|
||||
"inconsistent ref table");
|
||||
}
|
||||
LUA_ASSERT(L, VALIDLINK(L, L->refFree, n), "inconsistent ref table");
|
||||
}
|
||||
|
||||
27
lref.h
27
lref.h
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
** $Id: lref.h,v 1.5 1999/12/27 17:33:22 roberto Exp roberto $
|
||||
** reference mechanism
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lref_h
|
||||
#define lref_h
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define NONEXT -1 /* to end the free list */
|
||||
#define HOLD -2
|
||||
#define COLLECTED -3
|
||||
#define LOCK -4
|
||||
|
||||
|
||||
struct Ref {
|
||||
TObject o;
|
||||
int st; /* can be LOCK, HOLD, COLLECTED, or next (for free list) */
|
||||
};
|
||||
|
||||
|
||||
void luaR_invalidaterefs (lua_State *L);
|
||||
|
||||
#endif
|
||||
270
lstate.c
270
lstate.c
@@ -1,108 +1,220 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 1.24 2000/01/13 16:30:47 roberto Exp roberto $
|
||||
** $Id: lstate.c,v 1.122 2003/03/18 12:50:04 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define lstate_c
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lbuiltin.h"
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lref.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
lua_State *lua_state = NULL;
|
||||
/*
|
||||
** 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
|
||||
|
||||
|
||||
static lua_State *newstate_aux (int stacksize, int put_builtin) {
|
||||
lua_State *L = luaM_new(NULL, lua_State);
|
||||
L->errorJmp = NULL;
|
||||
L->Mbuffer = NULL;
|
||||
L->Mbuffbase = 0;
|
||||
L->Mbuffsize = 0;
|
||||
L->Mbuffnext = 0;
|
||||
L->Cblocks = NULL;
|
||||
L->numCblocks = 0;
|
||||
L->rootproto = NULL;
|
||||
L->rootcl = NULL;
|
||||
L->rootglobal = NULL;
|
||||
L->roottable = NULL;
|
||||
L->IMtable = NULL;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
L->refFree = NONEXT;
|
||||
L->nblocks = 0;
|
||||
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
||||
L->debug = 0;
|
||||
L->callhook = NULL;
|
||||
L->linehook = NULL;
|
||||
L->allowhooks = 1;
|
||||
luaD_init(L, stacksize);
|
||||
luaS_init(L);
|
||||
luaX_init(L);
|
||||
|
||||
/*
|
||||
** you can change this function through the official API:
|
||||
** call `lua_setpanicf'
|
||||
*/
|
||||
static int default_panic (lua_State *L) {
|
||||
UNUSED(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static 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) {
|
||||
/* 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(gkey(g->dummynode));
|
||||
setnilvalue(gval(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, 0));
|
||||
hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L));
|
||||
sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */
|
||||
sethvalue(registry(L), luaH_new(L, 4, 4)); /* registry */
|
||||
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
|
||||
luaT_init(L);
|
||||
if (put_builtin)
|
||||
luaB_predefine(L);
|
||||
L->GCthreshold = L->nblocks*4;
|
||||
luaX_init(L);
|
||||
luaS_fix(luaS_newliteral(L, MEMERRMSG));
|
||||
g->GCthreshold = 4*G(L)->nblocks;
|
||||
}
|
||||
|
||||
|
||||
static void preinit_state (lua_State *L) {
|
||||
L->stack = NULL;
|
||||
L->stacksize = 0;
|
||||
L->errorJmp = NULL;
|
||||
L->hook = NULL;
|
||||
L->hookmask = 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);
|
||||
}
|
||||
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;
|
||||
L->marked = 0;
|
||||
L->next = L->gclist = NULL;
|
||||
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_State *lua_newstate (const char *s, ...) {
|
||||
static const char *const ops[] = {"stack", "builtin", NULL};
|
||||
va_list ap;
|
||||
int stacksize = DEFAULT_STACK_SIZE;
|
||||
int put_builtin = 1;
|
||||
va_start(ap, s);
|
||||
while (s) {
|
||||
switch (luaL_findstring(s, ops)) {
|
||||
case 0: /* stack */
|
||||
stacksize = va_arg(ap, int);
|
||||
break;
|
||||
case 1: /* builtin */
|
||||
put_builtin = va_arg(ap, int);
|
||||
break;
|
||||
default: /* invalid argument */
|
||||
va_end(ap);
|
||||
return NULL;
|
||||
}
|
||||
s = va_arg(ap, const char *);
|
||||
}
|
||||
va_end(ap);
|
||||
return newstate_aux(stacksize, put_builtin);
|
||||
static void callallgcTM (lua_State *L, void *ud) {
|
||||
UNUSED(ud);
|
||||
luaC_callGCTM(L); /* call GC metamethods for all udata */
|
||||
}
|
||||
|
||||
|
||||
void lua_close (lua_State *L) {
|
||||
luaC_collect(L, 1); /* collect all elements */
|
||||
LUA_ASSERT(L, L->rootproto == NULL, "list should be empty");
|
||||
LUA_ASSERT(L, L->rootcl == NULL, "list should be empty");
|
||||
LUA_ASSERT(L, L->rootglobal == NULL, "list should be empty");
|
||||
LUA_ASSERT(L, L->roottable == NULL, "list should be empty");
|
||||
luaS_freeall(L);
|
||||
luaM_free(L, L->stack);
|
||||
luaM_free(L, L->IMtable);
|
||||
luaM_free(L, L->refArray);
|
||||
luaM_free(L, L->Mbuffer);
|
||||
luaM_free(L, L->Cblocks);
|
||||
LUA_ASSERT(L, L->numCblocks == 0, "Cblocks still open");
|
||||
LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks");
|
||||
LUA_ASSERT(L, L->Cstack.base == L->top, "C2Lua not empty");
|
||||
luaM_free(L, L);
|
||||
if (L == lua_state) {
|
||||
LUA_ASSERT(L, memdebug_numblocks == 0, "memory leak!");
|
||||
LUA_ASSERT(L, memdebug_total == 0,"memory leak!");
|
||||
lua_state = NULL;
|
||||
}
|
||||
LUA_API void lua_close (lua_State *L) {
|
||||
lua_lock(L);
|
||||
L = G(L)->mainthread; /* only the main thread can be closed */
|
||||
luaF_close(L, L->stack); /* close all upvalues for this thread */
|
||||
luaC_separateudata(L); /* separate udata that have GC metamethods */
|
||||
L->errfunc = 0; /* no error function during GC metamethods */
|
||||
do { /* repeat until no more errors */
|
||||
L->ci = L->base_ci;
|
||||
L->base = L->top = L->ci->base;
|
||||
L->nCcalls = 0;
|
||||
} while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
|
||||
lua_assert(G(L)->tmudata == NULL);
|
||||
close_state(L);
|
||||
}
|
||||
|
||||
|
||||
210
lstate.h
210
lstate.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 1.30 2000/03/10 18:37:44 roberto Exp roberto $
|
||||
** $Id: lstate.h,v 1.108 2002/11/25 17:47:13 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,83 +7,189 @@
|
||||
#ifndef lstate_h
|
||||
#define lstate_h
|
||||
|
||||
#include <setjmp.h>
|
||||
#include "lua.h"
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
|
||||
typedef TObject *StkId; /* index to stack elements */
|
||||
#include "ltm.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
/*
|
||||
** `jmp_buf' may be an array, so it is better to make sure it has an
|
||||
** address (and not that it *is* an address...)
|
||||
** 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).
|
||||
*/
|
||||
struct lua_longjmp {
|
||||
jmp_buf b;
|
||||
};
|
||||
#ifndef lua_lock
|
||||
#define lua_lock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
#ifndef lua_unlock
|
||||
#define lua_unlock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** stack layout for C point of view:
|
||||
** [lua2C, lua2C+num) - `array' lua2C
|
||||
** [lua2C+num, base) - space for extra lua_Objects (limbo)
|
||||
** [base, L->top) - `stack' C2Lua
|
||||
*/
|
||||
struct C_Lua_Stack {
|
||||
StkId base;
|
||||
StkId lua2C;
|
||||
int num;
|
||||
};
|
||||
#ifndef lua_userstateopen
|
||||
#define lua_userstateopen(l)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
struct lua_longjmp; /* defined in ldo.c */
|
||||
|
||||
|
||||
/* 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;
|
||||
int nuse; /* number of elements (including EMPTYs) */
|
||||
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' */
|
||||
int tailcalls; /* number of tail calls lost under this entry */
|
||||
} l;
|
||||
struct { /* for C functions */
|
||||
int dummy; /* just to avoid an empty struct */
|
||||
} c;
|
||||
} u;
|
||||
} CallInfo;
|
||||
|
||||
|
||||
/*
|
||||
** 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 */
|
||||
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 {
|
||||
/* thread-specific state */
|
||||
CommonHeader;
|
||||
StkId top; /* first free slot in the stack */
|
||||
StkId stack; /* stack base */
|
||||
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;
|
||||
struct C_Lua_Stack Cstack; /* C2lua struct */
|
||||
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 */
|
||||
char *Mbuffer; /* global buffer */
|
||||
int Mbuffbase; /* current first position of Mbuffer */
|
||||
int Mbuffsize; /* size of Mbuffer */
|
||||
int Mbuffnext; /* next position to fill in Mbuffer */
|
||||
struct C_Lua_Stack *Cblocks;
|
||||
int numCblocks; /* number of nested Cblocks */
|
||||
/* global state */
|
||||
Proto *rootproto; /* list of all prototypes */
|
||||
Closure *rootcl; /* list of all closures */
|
||||
Hash *roottable; /* list of all tables */
|
||||
GlobalVar *rootglobal; /* list of global variables */
|
||||
stringtable *string_root; /* array of hash tables for strings and udata */
|
||||
struct IM *IMtable; /* table for tag methods */
|
||||
int last_tag; /* last used tag in IMtable */
|
||||
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 `blocks' currently allocated */
|
||||
int debug;
|
||||
lua_Hook callhook;
|
||||
lua_Hook linehook;
|
||||
int allowhooks;
|
||||
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
|
||||
|
||||
|
||||
219
lstring.c
219
lstring.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 1.33 2000/03/10 14:38:10 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,199 +7,96 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define lstring_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
|
||||
#define gcsizestring(L, l) numblocks(L, 0, sizeof(TString)+l)
|
||||
#define gcsizeudata gcsizestring(L, 0)
|
||||
|
||||
|
||||
|
||||
void luaS_init (lua_State *L) {
|
||||
int i;
|
||||
L->string_root = luaM_newvector(L, NUM_HASHS, stringtable);
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
L->string_root[i].size = 1;
|
||||
L->string_root[i].nuse = 0;
|
||||
L->string_root[i].hash = luaM_newvector(L, 1, TString *);;
|
||||
L->string_root[i].hash[0] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaS_freeall (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
LUA_ASSERT(L, L->string_root[i].nuse==0, "non-empty string table");
|
||||
luaM_free(L, L->string_root[i].hash);
|
||||
}
|
||||
luaM_free(L, L->string_root);
|
||||
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, long l) {
|
||||
unsigned long h = l; /* seed */
|
||||
long step = (l>>6)+1; /* if string is too long, don't hash all its chars */
|
||||
for (; l>0; 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 */
|
||||
int h = p->hash&(newsize-1); /* new position */
|
||||
LUA_ASSERT(L, p->hash%newsize == (p->hash&(newsize-1)),
|
||||
"a&(x-1) == a%x, for x power of 2");
|
||||
p->nexthash = newhash[h]; /* chain it in new position */
|
||||
newhash[h] = p;
|
||||
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);
|
||||
luaM_freearray(L, tb->hash, tb->size, TString *);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static TString *newone (lua_State *L, long l, unsigned long h) {
|
||||
TString *ts = (TString *)luaM_malloc(L,
|
||||
sizeof(TString)+l*sizeof(char));
|
||||
ts->marked = 0;
|
||||
ts->nexthash = NULL;
|
||||
ts->hash = h;
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static TString *newone_s (lua_State *L, const char *str,
|
||||
long l, unsigned long h) {
|
||||
TString *ts = newone(L, l, h);
|
||||
memcpy(ts->str, str, l);
|
||||
ts->str[l] = 0; /* ending 0 */
|
||||
ts->u.s.gv = NULL; /* no global value */
|
||||
ts->u.s.len = l;
|
||||
ts->constindex = 0;
|
||||
L->nblocks += gcsizestring(L, l);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static TString *newone_u (lua_State *L, void *buff,
|
||||
int tag, unsigned long h) {
|
||||
TString *ts = newone(L, 0, h);
|
||||
ts->u.d.value = buff;
|
||||
ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
|
||||
ts->constindex = -1; /* tag -> this is a userdata */
|
||||
L->nblocks += gcsizeudata;
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
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 > tb->size) /* 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, long l) {
|
||||
unsigned long h = hash_s(str, l);
|
||||
stringtable *tb = &L->string_root[(l==0) ? 0 :
|
||||
((unsigned int)(str[0]+str[l-1]))&(NUM_HASHSTR-1)];
|
||||
int h1 = h&(tb->size-1);
|
||||
TString *ts;
|
||||
for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
|
||||
if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||
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 = newone_s(L, str, l, h); /* create new entry */
|
||||
newentry(L, tb, ts, h1); /* insert it on table */
|
||||
return ts;
|
||||
return newlstr(L, str, l, h); /* not found */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** uses '%' for one hashing with userdata because addresses are too regular,
|
||||
** so two '&' operations would be highly correlated
|
||||
*/
|
||||
TString *luaS_createudata (lua_State *L, void *udata, int tag) {
|
||||
unsigned long h = IntPoint(L, udata);
|
||||
stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
|
||||
int h1 = h&(tb->size-1);
|
||||
TString *ts;
|
||||
for (ts = tb->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 = newone_u(L, udata, tag, h);
|
||||
newentry(L, tb, ts, h1);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
void luaS_free (lua_State *L, TString *t) {
|
||||
if (t->constindex == -1) /* is userdata? */
|
||||
L->nblocks -= gcsizeudata;
|
||||
else { /* is string */
|
||||
L->nblocks -= gcsizestring(L, t->u.s.len);
|
||||
luaM_free(L, t->u.s.gv);
|
||||
}
|
||||
luaM_free(L, t);
|
||||
}
|
||||
|
||||
|
||||
GlobalVar *luaS_assertglobal (lua_State *L, TString *ts) {
|
||||
GlobalVar *gv = ts->u.s.gv;
|
||||
if (!gv) { /* no global value yet? */
|
||||
gv = luaM_new(L, GlobalVar);
|
||||
gv->value.ttype = TAG_NIL; /* initial value */
|
||||
gv->name = ts;
|
||||
gv->next = L->rootglobal; /* chain in global list */
|
||||
L->rootglobal = gv;
|
||||
ts->u.s.gv = gv;
|
||||
}
|
||||
return gv;
|
||||
}
|
||||
|
||||
|
||||
GlobalVar *luaS_assertglobalbyname (lua_State *L, const char *name) {
|
||||
return luaS_assertglobal(L, luaS_new(L, name));
|
||||
}
|
||||
|
||||
|
||||
int luaS_globaldefined (lua_State *L, const char *name) {
|
||||
TString *ts = luaS_new(L, name);
|
||||
return ts->u.s.gv && ts->u.s.gv->value.ttype != TAG_NIL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
32
lstring.h
32
lstring.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.17 2000/03/10 14:38:10 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,30 +12,22 @@
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
#define NUM_HASHSTR 32
|
||||
#define NUM_HASHUDATA 31
|
||||
#define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA)
|
||||
|
||||
#define sizestring(l) (cast(lu_mem, sizeof(union TString))+ \
|
||||
(cast(lu_mem, l)+1)*sizeof(char))
|
||||
|
||||
/*
|
||||
** any taggedstring with mark>=FIXMARK is never collected.
|
||||
** Marks>=RESERVEDMARK are used to identify reserved words.
|
||||
*/
|
||||
#define FIXMARK 2
|
||||
#define RESERVEDMARK 3
|
||||
#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_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);
|
||||
void luaS_free (lua_State *L, TString *ts);
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, long l);
|
||||
TString *luaS_new (lua_State *L, const char *str);
|
||||
TString *luaS_newfixed (lua_State *L, const char *str);
|
||||
GlobalVar *luaS_assertglobal (lua_State *L, TString *ts);
|
||||
GlobalVar *luaS_assertglobalbyname (lua_State *L, const char *name);
|
||||
int luaS_globaldefined (lua_State *L, const char *name);
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
599
ltable.c
599
ltable.c
@@ -1,13 +1,17 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 1.38 2000/03/29 20:19:20 roberto Exp roberto $
|
||||
** $Id: ltable.c,v 1.131 2003/03/24 14:18:42 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.
|
||||
@@ -17,22 +21,74 @@
|
||||
** performance penalties.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define ltable_c
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "ltable.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define gcsize(L, n) numblocks(L, n*2, sizeof(Hash))
|
||||
/*
|
||||
** 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 TAG_TABLE
|
||||
|
||||
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
|
||||
|
||||
#define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
|
||||
#define hashboolean(t,p) hashpow2(t, p)
|
||||
|
||||
|
||||
/*
|
||||
** for some types, it is better to avoid modulus by power of 2, as
|
||||
** they tend to have many 2 factors.
|
||||
*/
|
||||
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
|
||||
|
||||
|
||||
#define hashpointer(t,p) hashmod(t, IntPoint(p))
|
||||
|
||||
|
||||
/*
|
||||
** number of ints inside a lua_Number
|
||||
*/
|
||||
#define numints cast(int, sizeof(lua_Number)/sizeof(int))
|
||||
|
||||
|
||||
/*
|
||||
** hash for lua_Numbers
|
||||
*/
|
||||
static Node *hashnum (const Table *t, lua_Number n) {
|
||||
unsigned int a[numints];
|
||||
int i;
|
||||
n += 1; /* normalize number (avoid -0) */
|
||||
lua_assert(sizeof(a) <= sizeof(n));
|
||||
memcpy(a, &n, sizeof(a));
|
||||
for (i = 1; i < numints; i++) a[0] += a[i];
|
||||
return hashmod(t, cast(lu_hash, a[0]));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -40,153 +96,299 @@
|
||||
** 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 TAG_NUMBER:
|
||||
h = (unsigned long)(long)nvalue(key);
|
||||
break;
|
||||
case TAG_STRING: case TAG_USERDATA:
|
||||
h = tsvalue(key)->hash;
|
||||
break;
|
||||
case TAG_TABLE:
|
||||
h = IntPoint(L, avalue(key));
|
||||
break;
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
h = IntPoint(L, clvalue(key));
|
||||
break;
|
||||
case LUA_TNUMBER:
|
||||
return hashnum(t, nvalue(key));
|
||||
case LUA_TSTRING:
|
||||
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(L, 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&((unsigned int)t->size-1)];
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
||||
Node *n = luaH_mainposition(t, key);
|
||||
if (!n)
|
||||
lua_error(L, "unexpected type to index table");
|
||||
else do {
|
||||
if (luaO_equalObj(key, &n->key))
|
||||
return &n->val;
|
||||
n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject; /* key not found */
|
||||
}
|
||||
|
||||
|
||||
int luaH_pos (lua_State *L, const Hash *t, const TObject *key) {
|
||||
const TObject *v = luaH_get(L, t, key);
|
||||
return (v == &luaO_nilobject) ? -1 : /* key not found */
|
||||
(int)(((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static Node *hashnodecreate (lua_State *L, int nhash) {
|
||||
Node *v = luaM_newvector(L, nhash, Node);
|
||||
int i;
|
||||
for (i=0; i<nhash; i++) {
|
||||
ttype(&v[i].key) = ttype(&v[i].val) = TAG_NIL;
|
||||
v[i].next = NULL;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
static void setnodevector (lua_State *L, Hash *t, int size) {
|
||||
t->node = hashnodecreate(L, size);
|
||||
t->size = size;
|
||||
t->firstfree = &t->node[size-1]; /* first free position to be used */
|
||||
L->nblocks += gcsize(L, size);
|
||||
}
|
||||
|
||||
|
||||
Hash *luaH_new (lua_State *L, int size) {
|
||||
Hash *t = luaM_new(L, Hash);
|
||||
setnodevector(L, t, luaO_power2(size));
|
||||
t->htag = TagDefault;
|
||||
t->next = L->roottable;
|
||||
L->roottable = t;
|
||||
t->marked = 0;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
static int newsize (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) != TAG_NIL)
|
||||
realuse++;
|
||||
}
|
||||
return luaO_power2(realuse+realuse/4+1);
|
||||
}
|
||||
|
||||
|
||||
static void rehash (lua_State *L, Hash *t) {
|
||||
int oldsize = t->size;
|
||||
Node *nold = t->node;
|
||||
int i;
|
||||
L->nblocks -= gcsize(L, oldsize);
|
||||
setnodevector(L, t, newsize(t)); /* create new array of nodes */
|
||||
for (i=0; i<oldsize; i++) {
|
||||
Node *old = nold+i;
|
||||
if (ttype(&old->val) != TAG_NIL)
|
||||
luaH_set(L, t, &old->key, &old->val);
|
||||
}
|
||||
luaM_free(L, nold); /* free old array */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** sets a pair key-value in 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 pair
|
||||
** in its main position; otherwise (colliding node is in its main position),
|
||||
** new pair goes to an empty position.
|
||||
** Tricky point: the only place where an old element is moved is when
|
||||
** we move the colliding node to an empty place; nevertheless, its old
|
||||
** value is still in that position until we set the value for the new
|
||||
** pair; therefore, even when `val' points to an element of this table
|
||||
** (this happens when we use `luaH_move'), there is no problem.
|
||||
** returns the index for `key' if `key' is an appropriate key to live in
|
||||
** the array part of the table, -1 otherwise.
|
||||
*/
|
||||
void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
Node *n = mp;
|
||||
if (!mp)
|
||||
lua_error(L, "unexpected type to index table");
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_equalObj(key, &n->key)) {
|
||||
n->val = *val; /* update value */
|
||||
return; /* that's all */
|
||||
static int arrayindex (const TObject *key) {
|
||||
if (ttisnumber(key)) {
|
||||
int k;
|
||||
lua_number2int(k, (nvalue(key)));
|
||||
if (cast(lua_Number, k) == nvalue(key) && k >= 1 && !toobig(k))
|
||||
return k;
|
||||
}
|
||||
return -1; /* `key' did not match some condition */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns the index of a `key' for table traversals. First goes all
|
||||
** elements in the array part, then elements in the hash part. The
|
||||
** beginning and end of a traversal are signalled by -1.
|
||||
*/
|
||||
static int luaH_index (lua_State *L, Table *t, StkId key) {
|
||||
int i;
|
||||
if (ttisnil(key)) return -1; /* first iteration */
|
||||
i = arrayindex(key);
|
||||
if (0 <= i && i <= t->sizearray) { /* is `key' inside array part? */
|
||||
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 *, gval(gnode(t, 0)))) / sizeof(Node));
|
||||
return i + t->sizearray; /* hash elements are numbered after array ones */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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, cast(lua_Number, i+1));
|
||||
setobj2s(key+1, &t->array[i]);
|
||||
return 1;
|
||||
}
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
/* `key' not found; must insert it */
|
||||
if (ttype(&mp->key) != TAG_NIL) { /* 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 if after "firstfree") */
|
||||
if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
|
||||
}
|
||||
for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
|
||||
if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
|
||||
setobj2s(key, gkey(gnode(t, i)));
|
||||
setobj2s(key+1, gval(gnode(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; a < *narray && *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, lg;
|
||||
int totaluse = 0;
|
||||
/* count elements in array part */
|
||||
for (i=0, lg=0; lg<=MAXBITS; lg++) { /* for each slice [2^(lg-1) to 2^lg) */
|
||||
int ttlg = twoto(lg); /* 2^lg */
|
||||
if (ttlg > t->sizearray) {
|
||||
ttlg = t->sizearray;
|
||||
if (i >= ttlg) break;
|
||||
}
|
||||
nums[lg] = 0;
|
||||
for (; i<ttlg; i++) {
|
||||
if (!ttisnil(&t->array[i])) {
|
||||
nums[lg]++;
|
||||
totaluse++;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (; lg<=MAXBITS; lg++) nums[lg] = 0; /* reset other counts */
|
||||
*narray = totaluse; /* all previous uses were in array part */
|
||||
/* count elements in hash part */
|
||||
i = sizenode(t);
|
||||
while (i--) {
|
||||
Node *n = &t->node[i];
|
||||
if (!ttisnil(gval(n))) {
|
||||
int k = arrayindex(gkey(n));
|
||||
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(gkey(t->node))); /* assert invariants: */
|
||||
lua_assert(ttisnil(gval(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(gkey(gnode(t, i)));
|
||||
setnilvalue(gval(gnode(t, i)));
|
||||
}
|
||||
}
|
||||
t->lsizenode = cast(lu_byte, lsize);
|
||||
t->firstfree = gnode(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(gkey(G(L)->dummynode)); /* restate invariant */
|
||||
setnilvalue(gval(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(gval(old)))
|
||||
setobjt2t(luaH_set(L, t, gkey(old)), gval(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;
|
||||
setarrayvector(L, t, narray);
|
||||
setnodevector(L, t, lnhash);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
void luaH_free (lua_State *L, Table *t) {
|
||||
if (t->lsizenode)
|
||||
luaM_freearray(L, t->node, sizenode(t), Node);
|
||||
luaM_freearray(L, t->array, t->sizearray, TObject);
|
||||
luaM_freelem(L, t);
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
/*
|
||||
** try to remove an element from a hash table; cannot move any element
|
||||
** (because gc can call `remove' during a table traversal)
|
||||
*/
|
||||
void luaH_remove (Table *t, Node *e) {
|
||||
Node *mp = luaH_mainposition(t, gkey(e));
|
||||
if (e != mp) { /* element not in its main position? */
|
||||
while (mp->next != e) mp = mp->next; /* find previous */
|
||||
mp->next = e->next; /* remove `e' from its list */
|
||||
}
|
||||
else {
|
||||
if (e->next != NULL) ??
|
||||
}
|
||||
lua_assert(ttisnil(gval(node)));
|
||||
setnilvalue(gkey(e)); /* clear node `e' */
|
||||
e->next = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** inserts a new key into a hash table; first, check whether key's main
|
||||
** position is free. If not, check whether colliding node is in its main
|
||||
** position or not: if it is not, move colliding node to an empty place and
|
||||
** put new key in its main position; otherwise (colliding node is in its main
|
||||
** position), new key goes to an empty position.
|
||||
*/
|
||||
static TObject *newkey (lua_State *L, Table *t, const TObject *key) {
|
||||
TObject *val;
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
if (!ttisnil(gval(mp))) { /* main position is not free? */
|
||||
Node *othern = luaH_mainposition(t, gkey(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(gval(mp));
|
||||
}
|
||||
else { /* colliding node is in its own main position */
|
||||
/* new node will go into free position */
|
||||
@@ -195,30 +397,113 @@ void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
|
||||
mp = n;
|
||||
}
|
||||
}
|
||||
mp->key = *key;
|
||||
mp->val = *val;
|
||||
for (;;) { /* check free places */
|
||||
if (ttype(&t->firstfree->key) == TAG_NIL)
|
||||
return; /* OK; table still has a free place */
|
||||
setobj2t(gkey(mp), key); /* write barrier */
|
||||
lua_assert(ttisnil(gval(mp)));
|
||||
for (;;) { /* correct `firstfree' */
|
||||
if (ttisnil(gkey(t->firstfree)))
|
||||
return gval(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 */
|
||||
/* no more free places; must create one */
|
||||
setbvalue(gval(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;
|
||||
}
|
||||
|
||||
|
||||
void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) {
|
||||
TObject index;
|
||||
ttype(&index) = TAG_NUMBER;
|
||||
nvalue(&index) = key;
|
||||
luaH_set(L, t, &index, val);
|
||||
/*
|
||||
** generic search function
|
||||
*/
|
||||
static const TObject *luaH_getany (Table *t, const TObject *key) {
|
||||
if (ttisnil(key)) return &luaO_nilobject;
|
||||
else {
|
||||
Node *n = luaH_mainposition(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_rawequalObj(gkey(n), key)) return gval(n); /* that's it */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaH_getint (lua_State *L, const Hash *t, int key) {
|
||||
TObject index;
|
||||
ttype(&index) = TAG_NUMBER;
|
||||
nvalue(&index) = key;
|
||||
return luaH_get(L, t, &index);
|
||||
/*
|
||||
** search function for integers
|
||||
*/
|
||||
const TObject *luaH_getnum (Table *t, int key) {
|
||||
if (1 <= key && key <= t->sizearray)
|
||||
return &t->array[key-1];
|
||||
else {
|
||||
lua_Number nk = cast(lua_Number, key);
|
||||
Node *n = hashnum(t, nk);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttisnumber(gkey(n)) && nvalue(gkey(n)) == nk)
|
||||
return gval(n); /* that's it */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** search function for strings
|
||||
*/
|
||||
const TObject *luaH_getstr (Table *t, TString *key) {
|
||||
Node *n = hashstr(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttisstring(gkey(n)) && tsvalue(gkey(n)) == key)
|
||||
return gval(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, cast(lua_Number, key));
|
||||
return newkey(L, t, &k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
27
ltable.h
27
ltable.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 1.17 1999/11/23 13:58:02 roberto Exp roberto $
|
||||
** $Id: ltable.h,v 1.43 2002/11/07 16:03:33 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,23 +10,22 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define node(t,i) (&(t)->node[i])
|
||||
#define key(n) (&(n)->key)
|
||||
#define val(n) (&(n)->val)
|
||||
#define gnode(t,i) (&(t)->node[i])
|
||||
#define gkey(n) (&(n)->i_key)
|
||||
#define gval(n) (&(n)->i_val)
|
||||
|
||||
#define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getint(L, t, from)))
|
||||
|
||||
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);
|
||||
void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val);
|
||||
int luaH_pos (lua_State *L, const Hash *t, const TObject *r);
|
||||
void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val);
|
||||
const TObject *luaH_getint (lua_State *L, const Hash *t, int key);
|
||||
unsigned long luaH_hash (lua_State *L, const TObject *key);
|
||||
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
|
||||
|
||||
250
ltablib.c
Normal file
250
ltablib.c
Normal file
@@ -0,0 +1,250 @@
|
||||
/*
|
||||
** $Id: ltablib.c,v 1.20 2003/03/11 12:24:34 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"
|
||||
|
||||
|
||||
#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n))
|
||||
|
||||
|
||||
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, (lua_Number)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, (lua_Number)aux_getn(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_setn (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_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 */
|
||||
}
|
||||
luaL_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' */
|
||||
luaL_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 = luaL_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 luaopen_table (lua_State *L) {
|
||||
luaL_openlib(L, LUA_TABLIBNAME, tab_funcs, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
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
|
||||
178
ltm.c
178
ltm.c
@@ -1,158 +1,70 @@
|
||||
/*
|
||||
** $Id: ltm.c,v 1.38 2000/03/29 20:19:20 roberto Exp roberto $
|
||||
** $Id: ltm.c,v 1.105 2002/12/04 17:38:31 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#define ltm_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.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 IM */
|
||||
"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 luaI_checkevent (lua_State *L, const char *name, int t) {
|
||||
int e = luaL_findstring(name, luaT_eventname);
|
||||
if (e >= IM_N)
|
||||
luaL_verror(L, "event `%.50s' is deprecated", name);
|
||||
if (e == IM_GC && t == TAG_TABLE)
|
||||
luaL_verror(L, "event `gc' for tables is deprecated");
|
||||
if (e < 0)
|
||||
luaL_verror(L, "`%.50s' is not a valid event name", name);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* events in TAG_NIL are all allowed, since this is used as a
|
||||
* 'placeholder' for "default" fallbacks
|
||||
*/
|
||||
/* ORDER LUA_T, ORDER IM */
|
||||
static const char luaT_validevents[NUM_TAGS][IM_N] = {
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_USERDATA */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CCLOSURE */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
|
||||
};
|
||||
|
||||
int luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
||||
return (t > TAG_NIL) ? 1 : luaT_validevents[t][e];
|
||||
}
|
||||
|
||||
|
||||
static void init_entry (lua_State *L, int tag) {
|
||||
int i;
|
||||
for (i=0; i<IM_N; i++)
|
||||
ttype(luaT_getim(L, tag, i)) = TAG_NIL;
|
||||
}
|
||||
|
||||
|
||||
void luaT_init (lua_State *L) {
|
||||
int t;
|
||||
L->last_tag = NUM_TAGS-1;
|
||||
luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
init_entry(L, t);
|
||||
}
|
||||
|
||||
|
||||
int lua_newtag (lua_State *L) {
|
||||
++L->last_tag;
|
||||
luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM, arrEM, MAX_INT);
|
||||
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))
|
||||
luaL_verror(L, "%d is not a valid tag", tag);
|
||||
}
|
||||
|
||||
void luaT_realtag (lua_State *L, int tag) {
|
||||
if (!(NUM_TAGS <= tag && tag <= L->last_tag))
|
||||
luaL_verror(L, "tag %d was not created by `newtag'", tag);
|
||||
}
|
||||
|
||||
|
||||
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
|
||||
int e;
|
||||
checktag(L, tagto);
|
||||
checktag(L, tagfrom);
|
||||
for (e=0; e<IM_N; e++) {
|
||||
if (luaT_validevent(tagto, e))
|
||||
*luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
|
||||
}
|
||||
return tagto;
|
||||
}
|
||||
|
||||
|
||||
int luaT_effectivetag (lua_State *L, const TObject *o) {
|
||||
lua_Type t = ttype(o);
|
||||
switch (t) {
|
||||
case TAG_USERDATA: {
|
||||
int tag = o->value.ts->u.d.tag;
|
||||
return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
|
||||
}
|
||||
case TAG_TABLE: return o->value.a->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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaT_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))
|
||||
return luaT_getim(L, t,e);
|
||||
else
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
|
||||
|
||||
void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
|
||||
TObject temp;
|
||||
int e;
|
||||
e = luaI_checkevent(L, event, t);
|
||||
checktag(L, t);
|
||||
if (!luaT_validevent(t, e))
|
||||
luaL_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
|
||||
luaT_eventname[e], luaO_typenames[t],
|
||||
(t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
|
||||
: "");
|
||||
temp = *func;
|
||||
*func = *luaT_getim(L, t,e);
|
||||
*luaT_getim(L, t, e) = temp;
|
||||
}
|
||||
|
||||
|
||||
const char *luaT_travtagmethods (lua_State *L,
|
||||
int (*fn)(lua_State *, TObject *)) { /* ORDER IM */
|
||||
int e;
|
||||
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
if (fn(L, luaT_getim(L, t,e)))
|
||||
return luaT_eventname[e];
|
||||
/*
|
||||
** 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 |= cast(lu_byte, 1u<<event); /* cache this fact */
|
||||
return NULL;
|
||||
}
|
||||
else return tm;
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) {
|
||||
TString *ename = G(L)->tmname[event];
|
||||
switch (ttype(o)) {
|
||||
case LUA_TTABLE:
|
||||
return luaH_getstr(hvalue(o)->metatable, ename);
|
||||
case LUA_TUSERDATA:
|
||||
return luaH_getstr(uvalue(o)->uv.metatable, ename);
|
||||
default:
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
63
ltm.h
63
ltm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltm.h,v 1.11 2000/03/20 19:14:54 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,50 +9,43 @@
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER IM"
|
||||
* grep "ORDER TM"
|
||||
*/
|
||||
typedef enum {
|
||||
IM_GETTABLE = 0,
|
||||
IM_SETTABLE,
|
||||
IM_INDEX,
|
||||
IM_GETGLOBAL,
|
||||
IM_SETGLOBAL,
|
||||
IM_ADD,
|
||||
IM_SUB,
|
||||
IM_MUL,
|
||||
IM_DIV,
|
||||
IM_POW,
|
||||
IM_UNM,
|
||||
IM_LT,
|
||||
IM_CONCAT,
|
||||
IM_GC,
|
||||
IM_FUNCTION,
|
||||
IM_N /* number of elements in the enum */
|
||||
} IMS;
|
||||
TM_INDEX,
|
||||
TM_NEWINDEX,
|
||||
TM_GC,
|
||||
TM_MODE,
|
||||
TM_EQ, /* last tag method with `fast' access */
|
||||
TM_ADD,
|
||||
TM_SUB,
|
||||
TM_MUL,
|
||||
TM_DIV,
|
||||
TM_POW,
|
||||
TM_UNM,
|
||||
TM_LT,
|
||||
TM_LE,
|
||||
TM_CONCAT,
|
||||
TM_CALL,
|
||||
TM_N /* number of elements in the enum */
|
||||
} TMS;
|
||||
|
||||
|
||||
struct IM {
|
||||
TObject int_method[IM_N];
|
||||
};
|
||||
|
||||
|
||||
#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event])
|
||||
#define luaT_getimbyObj(L,o,e) (luaT_getim(L, luaT_effectivetag(L, o),(e)))
|
||||
|
||||
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_effectivetag (lua_State *L, const TObject *o);
|
||||
void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func);
|
||||
const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event);
|
||||
const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *));
|
||||
int luaT_validevent (int t, int e); /* used by compatibility module */
|
||||
|
||||
extern const char *const luaT_typenames[];
|
||||
|
||||
#endif
|
||||
|
||||
556
lua.c
556
lua.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.36 2000/03/30 17:19:48 roberto Exp roberto $
|
||||
** $Id: lua.c,v 1.121 2003/03/24 12:40:01 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,245 +10,429 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lua_c
|
||||
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
/*
|
||||
** generic extra include file
|
||||
*/
|
||||
#ifdef LUA_USERCONFIG
|
||||
#include LUA_USERCONFIG
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** definition of `isatty'
|
||||
*/
|
||||
#ifdef _POSIX_C_SOURCE
|
||||
#include <unistd.h>
|
||||
#define stdin_is_tty() isatty(0)
|
||||
#else
|
||||
#define stdin_is_tty() 1 /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef PROMPT
|
||||
#define PROMPT "> "
|
||||
#endif
|
||||
|
||||
#ifdef _POSIX_SOURCE
|
||||
#include <unistd.h>
|
||||
#else
|
||||
static int isatty (int x) { return x==0; } /* assume stdin is a tty */
|
||||
|
||||
#ifndef PROMPT2
|
||||
#define PROMPT2 ">> "
|
||||
#endif
|
||||
|
||||
typedef void (*handler)(int); /* type for signal actions */
|
||||
#ifndef PROGNAME
|
||||
#define PROGNAME "lua"
|
||||
#endif
|
||||
|
||||
static void laction (int i);
|
||||
#ifndef lua_userinit
|
||||
#define lua_userinit(L) openstdlibs(L)
|
||||
#endif
|
||||
|
||||
|
||||
static lua_Hook old_linehook = NULL;
|
||||
static lua_Hook old_callhook = NULL;
|
||||
#ifndef LUA_EXTRALIBS
|
||||
#define LUA_EXTRALIBS /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
static handler lreset (void) {
|
||||
return signal(SIGINT, laction);
|
||||
}
|
||||
static lua_State *L = NULL;
|
||||
|
||||
static const char *progname = PROGNAME;
|
||||
|
||||
|
||||
static void lstop (void) {
|
||||
lua_setlinehook(lua_state, old_linehook);
|
||||
lua_setcallhook(lua_state, old_callhook);
|
||||
lreset();
|
||||
lua_error("interrupted!");
|
||||
|
||||
static const luaL_reg lualibs[] = {
|
||||
{"base", luaopen_base},
|
||||
{"table", luaopen_table},
|
||||
{"io", luaopen_io},
|
||||
{"string", luaopen_string},
|
||||
{"math", luaopen_math},
|
||||
{"debug", luaopen_debug},
|
||||
{"loadlib", luaopen_loadlib},
|
||||
/* add your libraries here */
|
||||
LUA_EXTRALIBS
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
|
||||
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(lua_state, (lua_Hook)lstop);
|
||||
old_callhook = lua_setcallhook(lua_state, (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();
|
||||
res = f(lua_state, name); /* dostring | dofile */
|
||||
signal(SIGINT, h); /* restore old action */
|
||||
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"
|
||||
" -d turn debug on\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 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 void print_version (void) {
|
||||
printf("%s %s\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(eq+1);
|
||||
lua_setglobal(arg);
|
||||
}
|
||||
|
||||
|
||||
static lua_Object getargs (char *argv[]) {
|
||||
lua_Object args = lua_createtable();
|
||||
static void getargs (char *argv[], int n) {
|
||||
int i;
|
||||
lua_newtable(L);
|
||||
for (i=0; argv[i]; i++) {
|
||||
/* arg[i] = argv[i] */
|
||||
lua_pushobject(args); lua_pushnumber(i);
|
||||
lua_pushstring(argv[i]); lua_settable();
|
||||
lua_pushnumber(L, i - n);
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
/* arg.n = maximum index in table `arg' */
|
||||
lua_pushobject(args); lua_pushstring("n");
|
||||
lua_pushnumber(i-1); lua_settable();
|
||||
return args;
|
||||
lua_pushliteral(L, "n");
|
||||
lua_pushnumber(L, i-n-1);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void l_getargs (void) {
|
||||
char **argv = (char **)lua_getuserdata(lua_getparam(1));
|
||||
lua_pushobject(getargs(argv));
|
||||
static int docall (int status) {
|
||||
if (status == 0) status = lcall(0, 1);
|
||||
return report(status);
|
||||
}
|
||||
|
||||
|
||||
static void file_input (const char *argv) {
|
||||
int result = ldo(lua_dofile, argv);
|
||||
if (result) {
|
||||
if (result == 2) {
|
||||
fprintf(stderr, "lua: cannot execute file ");
|
||||
perror(argv);
|
||||
}
|
||||
exit(1);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void manual_input (int version, int prompt) {
|
||||
int cont = 1;
|
||||
if (version) print_version();
|
||||
while (cont) {
|
||||
char buffer[BUFSIZ];
|
||||
int i = 0;
|
||||
lua_beginblock();
|
||||
if (prompt) {
|
||||
const char *s = lua_getstring(lua_getglobal("_PROMPT"));
|
||||
if (!s) s = PROMPT;
|
||||
fputs(s, stdout);
|
||||
}
|
||||
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 >= BUFSIZ-1) {
|
||||
fprintf(stderr, "lua: input line too long\n");
|
||||
break;
|
||||
}
|
||||
else buffer[i++] = (char)c;
|
||||
}
|
||||
buffer[i] = '\0';
|
||||
ldo(lua_dostring, buffer);
|
||||
lua_endblock();
|
||||
/*
|
||||
** this macro can be used by some `history' system to save lines
|
||||
** read in manual input
|
||||
*/
|
||||
#ifndef lua_saveline
|
||||
#define lua_saveline(L,line) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** this macro defines a function to show the prompt and reads the
|
||||
** next line for manual input
|
||||
*/
|
||||
#ifndef lua_readline
|
||||
#define lua_readline(L,prompt) readline(L,prompt)
|
||||
|
||||
/* maximum length of an input line */
|
||||
#ifndef MAXINPUT
|
||||
#define MAXINPUT 512
|
||||
#endif
|
||||
|
||||
|
||||
static int readline (lua_State *l, const char *prompt) {
|
||||
static char buffer[MAXINPUT];
|
||||
if (prompt) {
|
||||
fputs(prompt, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
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 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, lua_pushfstring(L, "error calling `print' (%s)",
|
||||
lua_tostring(L, -1)));
|
||||
}
|
||||
}
|
||||
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 (stdin_is_tty()) {
|
||||
print_version();
|
||||
manual_input();
|
||||
}
|
||||
else
|
||||
file_input(NULL); /* executes stdin as a file */
|
||||
}
|
||||
else { /* other arguments; loop over them */
|
||||
int i;
|
||||
for (i = 1; argv[i] != NULL; i++) {
|
||||
if (argv[i][0] != '-') break; /* not an option? */
|
||||
switch (argv[i][1]) { /* option */
|
||||
case '-': { /* `--' */
|
||||
if (argv[i][2] != '\0') {
|
||||
print_usage();
|
||||
return 1;
|
||||
}
|
||||
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 1;
|
||||
}
|
||||
if (dostring(chunk, "=<command line>") != 0)
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
const char *filename = argv[i] + 2;
|
||||
if (*filename == '\0') filename = argv[++i];
|
||||
if (filename == NULL) {
|
||||
print_usage();
|
||||
return 1;
|
||||
}
|
||||
if (load_file(filename))
|
||||
return 1; /* stop if file fails */
|
||||
break;
|
||||
}
|
||||
case 'c': {
|
||||
l_message(progname, "option `-c' is deprecated");
|
||||
break;
|
||||
}
|
||||
case 's': {
|
||||
l_message(progname, "option `-s' is deprecated");
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
print_usage();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
} 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 0;
|
||||
}
|
||||
|
||||
|
||||
static void openstdlibs (lua_State *l) {
|
||||
const luaL_reg *lib = lualibs;
|
||||
for (; lib->func; 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
|
||||
return dostring(init, "=LUA_INIT");
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
if (s->argv[0] && s->argv[0][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[]) {
|
||||
int i = 1;
|
||||
if (i < argc && argv[1][0] == '-' && argv[1][1] == 's') {
|
||||
int stacksize = atoi(&argv[1][2]);
|
||||
if (stacksize == 0) {
|
||||
fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]);
|
||||
exit(1);
|
||||
}
|
||||
i++;
|
||||
lua_state = lua_newstate("stack", stacksize, NULL);
|
||||
int 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;
|
||||
}
|
||||
else
|
||||
lua_state = lua_newstate(NULL);
|
||||
lua_userinit();
|
||||
lua_pushuserdata(argv);
|
||||
lua_pushcclosure(l_getargs, 1);
|
||||
lua_setglobal("getargs");
|
||||
if (i >= argc) { /* no other arguments? */
|
||||
if (isatty(0)) {
|
||||
manual_input(1, 1);
|
||||
}
|
||||
else
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
}
|
||||
else for (; i<argc; i++) {
|
||||
if (argv[i][0] == '-') { /* option? */
|
||||
switch (argv[i][1]) {
|
||||
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 'd':
|
||||
lua_setdebug(lua_state, 1);
|
||||
if (i+1 >= argc) { /* last argument? */
|
||||
manual_input(1, 1);
|
||||
}
|
||||
break;
|
||||
case 'v':
|
||||
print_version();
|
||||
break;
|
||||
case 'e':
|
||||
i++;
|
||||
if (i >= argc) {
|
||||
print_message();
|
||||
exit(1);
|
||||
}
|
||||
if (ldo(lua_dostring, argv[i]) != 0) {
|
||||
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
i++;
|
||||
if (i >= argc) {
|
||||
print_message();
|
||||
exit(1);
|
||||
}
|
||||
lua_pushobject(getargs(argv+i)); /* collect remaining arguments */
|
||||
lua_setglobal("arg");
|
||||
file_input(argv[i]);
|
||||
goto endloop; /* stop scanning arguments */
|
||||
break;
|
||||
case 's':
|
||||
fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
|
||||
exit(1);
|
||||
default:
|
||||
print_message();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else if (strchr(argv[i], '='))
|
||||
assign(argv[i]);
|
||||
else
|
||||
file_input(argv[i]);
|
||||
}
|
||||
endloop:
|
||||
#ifdef DEBUG
|
||||
lua_close();
|
||||
#endif
|
||||
return 0;
|
||||
s.argc = argc;
|
||||
s.argv = argv;
|
||||
status = lua_cpcall(l, &pmain, &s);
|
||||
report(status);
|
||||
lua_close(l);
|
||||
return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
533
lua.h
533
lua.h
@@ -1,9 +1,8 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.47 2000/04/14 17:48:20 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.174 2003/02/27 11:52:30 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,105 +10,214 @@
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
#define LUA_VERSION "Lua 4.0 (alpha)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2000 TeCGraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#define LUA_NOREF (-2)
|
||||
#define LUA_REFNIL (-1)
|
||||
#define LUA_VERSION "Lua 5.0"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2003 Tecgraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
|
||||
|
||||
|
||||
|
||||
/* option for multiple returns in `lua_pcall' and `lua_call' */
|
||||
#define LUA_MULTRET (-1)
|
||||
|
||||
|
||||
/*
|
||||
** pseudo-indices
|
||||
*/
|
||||
#define LUA_REGISTRYINDEX (-10000)
|
||||
#define LUA_GLOBALSINDEX (-10001)
|
||||
#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
|
||||
|
||||
|
||||
/* error codes for `lua_load' and `lua_pcall' */
|
||||
#define LUA_ERRRUN 1
|
||||
#define LUA_ERRFILE 2
|
||||
#define LUA_ERRSYNTAX 3
|
||||
#define LUA_ERRMEM 4
|
||||
#define LUA_ERRERR 5
|
||||
|
||||
#define LUA_ANYTAG (-1)
|
||||
|
||||
typedef struct lua_State lua_State;
|
||||
|
||||
typedef void (*lua_CFunction) (lua_State *L);
|
||||
|
||||
typedef struct TObject *lua_Object;
|
||||
|
||||
#define LUA_NOOBJECT ((lua_Object)0)
|
||||
typedef int (*lua_CFunction) (lua_State *L);
|
||||
|
||||
|
||||
lua_State *lua_newstate (const char *s, ...);
|
||||
void lua_close (lua_State *L);
|
||||
/*
|
||||
** functions that read/write blocks when loading/dumping Lua chunks
|
||||
*/
|
||||
typedef const char * (*lua_Chunkreader) (lua_State *L, void *ud, size_t *sz);
|
||||
|
||||
lua_Object lua_settagmethod (lua_State *L, int tag, const char *event);
|
||||
/* In: new method */
|
||||
lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event);
|
||||
|
||||
int lua_newtag (lua_State *L);
|
||||
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
||||
void lua_settag (lua_State *L, int tag); /* In: object */
|
||||
|
||||
void lua_error (lua_State *L, const char *s);
|
||||
int lua_dofile (lua_State *L, const char *filename);
|
||||
/* Out: returns */
|
||||
int lua_dostring (lua_State *L, const char *str);
|
||||
/* Out: returns */
|
||||
int lua_dobuffer (lua_State *L, const char *buff, int size,
|
||||
const char *name); /* Out: returns */
|
||||
int lua_callfunction (lua_State *L, lua_Object f);
|
||||
/* In: parameters; Out: returns */
|
||||
|
||||
void lua_beginblock (lua_State *L);
|
||||
void lua_endblock (lua_State *L);
|
||||
|
||||
lua_Object lua_lua2C (lua_State *L, int number);
|
||||
#define lua_getparam lua_lua2C
|
||||
#define lua_getresult lua_lua2C
|
||||
|
||||
const char *lua_type (lua_State *L, lua_Object obj);
|
||||
|
||||
int lua_isnil (lua_State *L, lua_Object obj);
|
||||
int lua_istable (lua_State *L, lua_Object obj);
|
||||
int lua_isuserdata (lua_State *L, lua_Object obj);
|
||||
int lua_iscfunction (lua_State *L, lua_Object obj);
|
||||
int lua_isnumber (lua_State *L, lua_Object obj);
|
||||
int lua_isstring (lua_State *L, lua_Object obj);
|
||||
int lua_isfunction (lua_State *L, lua_Object obj);
|
||||
|
||||
int lua_equal (lua_State *L, lua_Object o1, lua_Object o2);
|
||||
|
||||
double lua_getnumber (lua_State *L, lua_Object obj);
|
||||
const char *lua_getstring (lua_State *L, lua_Object obj);
|
||||
long lua_strlen (lua_State *L, lua_Object obj);
|
||||
lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj);
|
||||
void *lua_getuserdata (lua_State *L, lua_Object obj);
|
||||
typedef int (*lua_Chunkwriter) (lua_State *L, const void* p,
|
||||
size_t sz, void* ud);
|
||||
|
||||
|
||||
void lua_pushnil (lua_State *L);
|
||||
void lua_pushnumber (lua_State *L, double n);
|
||||
void lua_pushlstring (lua_State *L, const char *s, long len);
|
||||
void lua_pushstring (lua_State *L, const char *s);
|
||||
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
|
||||
void lua_pushusertag (lua_State *L, void *u, int tag);
|
||||
void lua_pushobject (lua_State *L, lua_Object obj);
|
||||
/*
|
||||
** basic types
|
||||
*/
|
||||
#define LUA_TNONE (-1)
|
||||
|
||||
lua_Object lua_pop (lua_State *L);
|
||||
#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
|
||||
|
||||
lua_Object lua_getglobal (lua_State *L, const char *name);
|
||||
lua_Object lua_rawgetglobal (lua_State *L, const char *name);
|
||||
void lua_setglobal (lua_State *L, const char *name); /* In: value */
|
||||
void lua_rawsetglobal (lua_State *L, const char *name);/* In: value */
|
||||
|
||||
void lua_settable (lua_State *L); /* In: table, index, value */
|
||||
void lua_rawsettable (lua_State *L); /* In: table, index, value */
|
||||
lua_Object lua_gettable (lua_State *L); /* In: table, index */
|
||||
lua_Object lua_rawgettable (lua_State *L); /* In: table, index */
|
||||
/* minimum Lua stack available to a C function */
|
||||
#define LUA_MINSTACK 20
|
||||
|
||||
int lua_tag (lua_State *L, lua_Object obj);
|
||||
|
||||
const char *lua_nextvar (lua_State *L, const char *varname); /* Out: value */
|
||||
int lua_next (lua_State *L, lua_Object o, int i);
|
||||
/* Out: ref, value */
|
||||
/*
|
||||
** generic extra include file
|
||||
*/
|
||||
#ifdef LUA_USER_H
|
||||
#include LUA_USER_H
|
||||
#endif
|
||||
|
||||
int lua_ref (lua_State *L, int lock); /* In: value */
|
||||
lua_Object lua_getref (lua_State *L, int ref);
|
||||
void lua_unref (lua_State *L, int ref);
|
||||
|
||||
lua_Object lua_createtable (lua_State *L);
|
||||
/* type of numbers in Lua */
|
||||
#ifndef LUA_NUMBER
|
||||
typedef double lua_Number;
|
||||
#else
|
||||
typedef LUA_NUMBER lua_Number;
|
||||
#endif
|
||||
|
||||
long lua_collectgarbage (lua_State *L, long limit);
|
||||
|
||||
/* mark for all API functions */
|
||||
#ifndef LUA_API
|
||||
#define LUA_API extern
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** state manipulation
|
||||
*/
|
||||
LUA_API lua_State *lua_open (void);
|
||||
LUA_API void lua_close (lua_State *L);
|
||||
LUA_API lua_State *lua_newthread (lua_State *L);
|
||||
|
||||
LUA_API 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 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_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 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 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, 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_pushboolean (lua_State *L, int b);
|
||||
LUA_API void lua_pushlightuserdata (lua_State *L, void *p);
|
||||
|
||||
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
LUA_API void lua_gettable (lua_State *L, int 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 void *lua_newuserdata (lua_State *L, size_t sz);
|
||||
LUA_API int lua_getmetatable (lua_State *L, int objindex);
|
||||
LUA_API void lua_getfenv (lua_State *L, int idx);
|
||||
|
||||
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
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_setfenv (lua_State *L, int idx);
|
||||
|
||||
|
||||
/*
|
||||
** `load' and `call' functions (load and run Lua code)
|
||||
*/
|
||||
LUA_API void lua_call (lua_State *L, int nargs, int nresults);
|
||||
LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
|
||||
LUA_API int lua_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);
|
||||
|
||||
|
||||
/*
|
||||
** 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);
|
||||
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
|
||||
|
||||
/*
|
||||
** miscellaneous functions
|
||||
*/
|
||||
|
||||
LUA_API const char *lua_version (void);
|
||||
|
||||
LUA_API int lua_error (lua_State *L);
|
||||
|
||||
LUA_API int lua_next (lua_State *L, int idx);
|
||||
|
||||
LUA_API void lua_concat (lua_State *L, int n);
|
||||
|
||||
|
||||
|
||||
@@ -119,138 +227,165 @@ long lua_collectgarbage (lua_State *L, long limit);
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#ifdef LUA_REENTRANT
|
||||
#define lua_boxpointer(L,u) \
|
||||
(*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
|
||||
|
||||
#define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i)))
|
||||
|
||||
#define lua_pop(L,n) lua_settop(L, -(n)-1)
|
||||
|
||||
#define lua_register(L,n,f) \
|
||||
(lua_pushstring(L, n), \
|
||||
lua_pushcfunction(L, f), \
|
||||
lua_settable(L, LUA_GLOBALSINDEX))
|
||||
|
||||
#define lua_call(L,name) lua_callfunction(L, lua_getglobal(L, name))
|
||||
#define lua_pushref(L,ref) lua_pushobject(L, lua_getref(L, ref))
|
||||
#define lua_refobject(L,o,l) (lua_pushobject(L, o), lua_ref(L, l))
|
||||
#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_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
|
||||
#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
|
||||
|
||||
#else
|
||||
#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
|
||||
#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
|
||||
#define lua_islightuserdata(L,n) (lua_type(L,n) == LUA_TLIGHTUSERDATA)
|
||||
#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
|
||||
#define lua_isboolean(L,n) (lua_type(L,n) == LUA_TBOOLEAN)
|
||||
#define lua_isnone(L,n) (lua_type(L,n) == LUA_TNONE)
|
||||
#define lua_isnoneornil(L, n) (lua_type(L,n) <= 0)
|
||||
|
||||
#define lua_call(name) lua_callfunction(lua_getglobal(name))
|
||||
#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
|
||||
#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))
|
||||
#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n))
|
||||
#define lua_pushuserdata(u) lua_pushusertag(u, 0)
|
||||
#define lua_pushcfunction(f) lua_pushcclosure(f, 0)
|
||||
#define lua_clonetag(t) lua_copytagmethods(lua_newtag(), (t))
|
||||
|
||||
#endif
|
||||
#define lua_pushliteral(L, s) \
|
||||
lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
|
||||
|
||||
|
||||
|
||||
#ifndef LUA_REENTRANT
|
||||
/*
|
||||
** ===============================================================
|
||||
** Macros for single-state use
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
extern lua_State *lua_state;
|
||||
|
||||
#define lua_open() ((void)(lua_state?0:(lua_state=lua_newstate(0))))
|
||||
|
||||
#define lua_close() (lua_close)(lua_state)
|
||||
#define lua_settagmethod(tag,event) (lua_settagmethod)(lua_state, tag,event)
|
||||
#define lua_gettagmethod(tag,event) (lua_gettagmethod)(lua_state, tag,event)
|
||||
#define lua_newtag() (lua_newtag)(lua_state)
|
||||
#define lua_copytagmethods(tagto,tagfrom) \
|
||||
(lua_copytagmethods)(lua_state, tagto,tagfrom)
|
||||
#define lua_settag(tag) (lua_settag)(lua_state, tag)
|
||||
#define lua_error(s) (lua_error)(lua_state, s)
|
||||
#define lua_dofile(filename) (lua_dofile)(lua_state, filename)
|
||||
#define lua_dostring(str) (lua_dostring)(lua_state, str)
|
||||
#define lua_callfunction(f) (lua_callfunction)(lua_state, f)
|
||||
#define lua_beginblock() (lua_beginblock)(lua_state)
|
||||
#define lua_endblock() (lua_endblock)(lua_state)
|
||||
#define lua_lua2C(number) (lua_lua2C)(lua_state, number)
|
||||
#define lua_type(obj) (lua_type)(lua_state, obj)
|
||||
#define lua_isnil(obj) (lua_isnil)(lua_state, obj)
|
||||
#define lua_istable(obj) (lua_istable)(lua_state, obj)
|
||||
#define lua_isuserdata(obj) (lua_isuserdata)(lua_state, obj)
|
||||
#define lua_iscfunction(obj) (lua_iscfunction)(lua_state, obj)
|
||||
#define lua_isnumber(obj) (lua_isnumber)(lua_state, obj)
|
||||
#define lua_isstring(obj) (lua_isstring)(lua_state, obj)
|
||||
#define lua_isfunction(obj) (lua_isfunction)(lua_state, obj)
|
||||
#define lua_equal(o1,o2) (lua_equal)(lua_state, o1,o2)
|
||||
#define lua_getnumber(obj) (lua_getnumber)(lua_state, obj)
|
||||
#define lua_getstring(obj) (lua_getstring)(lua_state, obj)
|
||||
#define lua_strlen(obj) (lua_strlen)(lua_state, obj)
|
||||
#define lua_getcfunction(obj) (lua_getcfunction)(lua_state, obj)
|
||||
#define lua_getuserdata(obj) (lua_getuserdata)(lua_state, obj)
|
||||
#define lua_pushnil() (lua_pushnil)(lua_state)
|
||||
#define lua_pushnumber(n) (lua_pushnumber)(lua_state, n)
|
||||
#define lua_pushlstring(s,len) (lua_pushlstring)(lua_state, s,len)
|
||||
#define lua_pushstring(s) (lua_pushstring)(lua_state, s)
|
||||
#define lua_pushusertag(u,tag) (lua_pushusertag)(lua_state, u,tag)
|
||||
#define lua_pushobject(obj) (lua_pushobject)(lua_state, obj)
|
||||
#define lua_pop() (lua_pop)(lua_state)
|
||||
#define lua_getglobal(name) (lua_getglobal)(lua_state, name)
|
||||
#define lua_rawgetglobal(name) (lua_rawgetglobal)(lua_state, name)
|
||||
#define lua_setglobal(name) (lua_setglobal)(lua_state, name)
|
||||
#define lua_rawsetglobal(name) (lua_rawsetglobal)(lua_state, name)
|
||||
#define lua_settable() (lua_settable)(lua_state)
|
||||
#define lua_rawsettable() (lua_rawsettable)(lua_state)
|
||||
#define lua_gettable() (lua_gettable)(lua_state)
|
||||
#define lua_rawgettable() (lua_rawgettable)(lua_state)
|
||||
#define lua_tag(obj) (lua_tag)(lua_state, obj)
|
||||
#define lua_nextvar(varname) (lua_nextvar)(lua_state, varname)
|
||||
#define lua_next(o,i) (lua_next)(lua_state, o,i)
|
||||
#define lua_ref(lock) (lua_ref)(lua_state, lock)
|
||||
#define lua_getref(ref) (lua_getref)(lua_state, ref)
|
||||
#define lua_unref(ref) (lua_unref)(lua_state, ref)
|
||||
#define lua_createtable() (lua_createtable)(lua_state)
|
||||
#define lua_collectgarbage(limit) (lua_collectgarbage)(lua_state, limit)
|
||||
/*
|
||||
** the following typecast is a little dirty, but we know of no other
|
||||
** way to keep compatibility with old definition of `lua_CFunction'
|
||||
** compatibility macros and functions
|
||||
*/
|
||||
#define lua_pushcclosure(fn,n) \
|
||||
(lua_pushcclosure)(lua_state, (lua_CFunction)(fn), n)
|
||||
|
||||
|
||||
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
|
||||
#define LUA_HOOKTAILRET 4
|
||||
|
||||
|
||||
/*
|
||||
** 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 const char *lua_getupvalue (lua_State *L, int funcindex, int n);
|
||||
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, 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', `C', `main', `tail' */
|
||||
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) 1994-2003 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
|
||||
|
||||
54
luadebug.h
54
luadebug.h
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
** $Id: luadebug.h,v 1.9 2000/01/19 12:00:45 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);
|
||||
|
||||
|
||||
int lua_getstack (lua_State *L, int level, lua_Debug *ar);
|
||||
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
|
||||
int lua_getlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v);
|
||||
int lua_setlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v);
|
||||
|
||||
int lua_setdebug (lua_State *L, int debug);
|
||||
|
||||
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
|
||||
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
|
||||
|
||||
|
||||
|
||||
struct lua_Debug {
|
||||
const char *event; /* `call', `return' */
|
||||
const char *source; /* (S) */
|
||||
int linedefined; /* (S) */
|
||||
const char *what; /* (S) `Lua' function, `C' function, Lua `main' */
|
||||
int currentline; /* (l) */
|
||||
const char *name; /* (n) */
|
||||
const char *namewhat; /* (n) global, tag method, local, field */
|
||||
int nups; /* (u) number of upvalues */
|
||||
lua_Object func; /* (f) function being executed */
|
||||
/* private part */
|
||||
lua_Object _func; /* active function */
|
||||
};
|
||||
|
||||
|
||||
struct lua_Localvar {
|
||||
int index;
|
||||
const char *name;
|
||||
lua_Object value;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
63
lualib.h
63
lualib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lualib.h,v 1.7 1999/08/16 20:52:00 roberto Exp roberto $
|
||||
** $Id: lualib.h,v 1.27 2003/03/17 13:04:58 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,36 +10,47 @@
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
void lua_iolibopen (lua_State *L);
|
||||
void lua_strlibopen (lua_State *L);
|
||||
void lua_mathlibopen (lua_State *L);
|
||||
void lua_dblibopen (lua_State *L);
|
||||
|
||||
|
||||
void lua_userinit (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** Macros for single-state use
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#ifndef LUA_REENTRANT
|
||||
|
||||
#define lua_iolibopen() (lua_iolibopen)(lua_state)
|
||||
#define lua_strlibopen() (lua_strlibopen)(lua_state)
|
||||
#define lua_mathlibopen() (lua_mathlibopen)(lua_state)
|
||||
#define lua_dblibopen() (lua_dblibopen)(lua_state)
|
||||
#define lua_userinit() (lua_userinit)(lua_state)
|
||||
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API LUA_API
|
||||
#endif
|
||||
|
||||
|
||||
#define LUA_COLIBNAME "coroutine"
|
||||
LUALIB_API int luaopen_base (lua_State *L);
|
||||
|
||||
/* Auxiliary functions (private) */
|
||||
#define LUA_TABLIBNAME "table"
|
||||
LUALIB_API int luaopen_table (lua_State *L);
|
||||
|
||||
const char *luaI_classend (lua_State *L, const char *p);
|
||||
int luaI_singlematch (int c, const char *p, const char *ep);
|
||||
#define LUA_IOLIBNAME "io"
|
||||
#define LUA_OSLIBNAME "os"
|
||||
LUALIB_API int luaopen_io (lua_State *L);
|
||||
|
||||
#define LUA_STRLIBNAME "string"
|
||||
LUALIB_API int luaopen_string (lua_State *L);
|
||||
|
||||
#define LUA_MATHLIBNAME "math"
|
||||
LUALIB_API int luaopen_math (lua_State *L);
|
||||
|
||||
#define LUA_DBLIBNAME "debug"
|
||||
LUALIB_API int luaopen_debug (lua_State *L);
|
||||
|
||||
|
||||
LUALIB_API int luaopen_loadlib (lua_State *L);
|
||||
|
||||
|
||||
/* to help testing the libraries */
|
||||
#ifndef lua_assert
|
||||
#define lua_assert(c) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
/* compatibility code */
|
||||
#define lua_baselibopen luaopen_base
|
||||
#define lua_tablibopen luaopen_table
|
||||
#define lua_iolibopen luaopen_io
|
||||
#define lua_strlibopen luaopen_string
|
||||
#define lua_mathlibopen luaopen_math
|
||||
#define lua_dblibopen luaopen_debug
|
||||
|
||||
#endif
|
||||
|
||||
410
lundump.c
410
lundump.c
@@ -1,278 +1,286 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.28 2000/04/24 19:32:58 lhf Exp $
|
||||
** load bytecodes from files
|
||||
** $Id: lundump.c,v 1.49 2003/04/07 20:34:20 lhf Exp $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#define lundump_c
|
||||
|
||||
#define LUA_REENTRANT
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "ldebug.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstring.h"
|
||||
#include "lundump.h"
|
||||
#include "lzio.h"
|
||||
|
||||
#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
|
||||
#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)
|
||||
{
|
||||
luaL_verror(L,"unexpected end of file in `%.255s'",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 unsigned int LoadWord (lua_State* L, ZIO* Z)
|
||||
static void LoadBlock (LoadState* S, void* b, size_t size)
|
||||
{
|
||||
unsigned int hi=ezgetc(L,Z);
|
||||
unsigned int lo=ezgetc(L,Z);
|
||||
return (hi<<8)|lo;
|
||||
if (S->swap)
|
||||
{
|
||||
char* p=(char*) b+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(S);
|
||||
}
|
||||
else
|
||||
ezread(S,b,size);
|
||||
}
|
||||
|
||||
static unsigned long LoadLong (lua_State* L, ZIO* Z)
|
||||
static void LoadVector (LoadState* S, void* b, int m, size_t size)
|
||||
{
|
||||
unsigned long hi=LoadWord(L,Z);
|
||||
unsigned long lo=LoadWord(L,Z);
|
||||
return (hi<<16)|lo;
|
||||
if (S->swap)
|
||||
{
|
||||
char* q=(char*) b;
|
||||
while (m--)
|
||||
{
|
||||
char* p=q+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(S);
|
||||
q+=size;
|
||||
}
|
||||
}
|
||||
else
|
||||
ezread(S,b,m*size);
|
||||
}
|
||||
|
||||
static Number LoadNumber (lua_State* L, ZIO* Z)
|
||||
static int LoadInt (LoadState* S)
|
||||
{
|
||||
char b[256];
|
||||
int size=ezgetc(L,Z);
|
||||
LoadBlock(L,b,size,Z);
|
||||
b[size]=0;
|
||||
return luaU_str2d(L,b,ZNAME(Z));
|
||||
int x;
|
||||
LoadBlock(S,&x,sizeof(x));
|
||||
if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name);
|
||||
return x;
|
||||
}
|
||||
|
||||
static int LoadInt (lua_State* L, ZIO* Z, const char* message)
|
||||
static size_t LoadSize (LoadState* S)
|
||||
{
|
||||
unsigned long l=LoadLong(L,Z);
|
||||
unsigned int i=l;
|
||||
if (i!=l) luaL_verror(L,"%s in `%.255s';\n"
|
||||
" read %lu; expected %u",message,ZNAME(Z),l,-1);
|
||||
return i;
|
||||
size_t x;
|
||||
LoadBlock(S,&x,sizeof(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static TString* LoadString (lua_State* L, ZIO* Z)
|
||||
static lua_Number LoadNumber (LoadState* S)
|
||||
{
|
||||
long size=LoadLong(L,Z);
|
||||
lua_Number x;
|
||||
LoadBlock(S,&x,sizeof(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static TString* LoadString (LoadState* S)
|
||||
{
|
||||
size_t size=LoadSize(S);
|
||||
if (size==0)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
char* s=luaL_openspace(L,size);
|
||||
LoadBlock(L,s,size,Z);
|
||||
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 SwapCode (lua_State* L, Instruction* code, int size, ZIO* Z)
|
||||
static void LoadCode (LoadState* S, Proto* f)
|
||||
{
|
||||
unsigned char* p;
|
||||
int c;
|
||||
if (sizeof(Instruction)==4)
|
||||
while (size--)
|
||||
{
|
||||
p=(unsigned char *) code++;
|
||||
c=p[0]; p[0]=p[3]; p[3]=c;
|
||||
c=p[1]; p[1]=p[2]; p[2]=c;
|
||||
}
|
||||
else if (sizeof(Instruction)==2)
|
||||
while (size--)
|
||||
{
|
||||
p=(unsigned char *) code++;
|
||||
c=p[0]; p[0]=p[1]; p[1]=c;
|
||||
}
|
||||
else
|
||||
luaL_verror(L,"cannot swap code with %d-byte instructions in `%.255s'",
|
||||
(int)sizeof(Instruction),ZNAME(Z));
|
||||
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 LoadCode (lua_State* L, Proto* tf, ZIO* Z)
|
||||
{
|
||||
int size=LoadInt(L,Z,"code too long");
|
||||
Instruction t=0,tt=TEST_CODE;
|
||||
tf->code=luaM_newvector(L,size,Instruction);
|
||||
LoadBlock(L,tf->code,size*sizeof(*tf->code),Z);
|
||||
if (tf->code[size-1]!=OP_END) luaL_verror(L,"bad code in `%.255s'",ZNAME(Z));
|
||||
LoadBlock(L,&t,sizeof(t),Z);
|
||||
if (t!=tt)
|
||||
{
|
||||
Instruction ot=t;
|
||||
SwapCode(L,&t,1,Z);
|
||||
if (t!=tt) luaL_verror(L,"cannot swap code in `%.255s';\n"
|
||||
" read 0x%08X; swapped to 0x%08X; expected 0x%08X",
|
||||
ZNAME(Z),(unsigned long)ot,(unsigned long)t,(unsigned long)tt);
|
||||
SwapCode(L,tf->code,size,Z);
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z)
|
||||
{
|
||||
int i,n=LoadInt(L,Z,"too many locals");
|
||||
if (n==0) return;
|
||||
tf->locvars=luaM_newvector(L,n+1,LocVar);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->locvars[i].line=LoadInt(L,Z,"too many lines");
|
||||
tf->locvars[i].varname=LoadString(L,Z);
|
||||
}
|
||||
tf->locvars[i].line=-1; /* flag end of vector */
|
||||
tf->locvars[i].varname=NULL;
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int native);
|
||||
|
||||
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int native)
|
||||
static void LoadLocals (LoadState* S, Proto* f)
|
||||
{
|
||||
int i,n;
|
||||
tf->nkstr=n=LoadInt(L,Z,"too many strings");
|
||||
if (n>0)
|
||||
n=LoadInt(S);
|
||||
f->locvars=luaM_newvector(S->L,n,LocVar);
|
||||
f->sizelocvars=n;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->kstr=luaM_newvector(L,n,TString*);
|
||||
for (i=0; i<n; i++)
|
||||
f->locvars[i].varname=LoadString(S);
|
||||
f->locvars[i].startpc=LoadInt(S);
|
||||
f->locvars[i].endpc=LoadInt(S);
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadLines (LoadState* S, Proto* f)
|
||||
{
|
||||
int size=LoadInt(S);
|
||||
f->lineinfo=luaM_newvector(S->L,size,int);
|
||||
f->sizelineinfo=size;
|
||||
LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo));
|
||||
}
|
||||
|
||||
static void LoadUpvalues (LoadState* S, Proto* f)
|
||||
{
|
||||
int i,n;
|
||||
n=LoadInt(S);
|
||||
if (n!=0 && n!=f->nups)
|
||||
luaG_runerror(S->L,"bad nupvalues in %s: read %d; expected %d",
|
||||
S->name,n,f->nups);
|
||||
f->upvalues=luaM_newvector(S->L,n,TString*);
|
||||
f->sizeupvalues=n;
|
||||
for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (LoadState* S, TString* p);
|
||||
|
||||
static void LoadConstants (LoadState* S, Proto* f)
|
||||
{
|
||||
int i,n;
|
||||
n=LoadInt(S);
|
||||
f->k=luaM_newvector(S->L,n,TObject);
|
||||
f->sizek=n;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
TObject* o=&f->k[i];
|
||||
int t=LoadByte(S);
|
||||
switch (t)
|
||||
{
|
||||
TString* s=LoadString(L,Z);
|
||||
int isglobal=LoadByte(L,Z);
|
||||
if (isglobal) luaS_assertglobal(L,s);
|
||||
tf->kstr[i]=s;
|
||||
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;
|
||||
}
|
||||
}
|
||||
tf->nknum=n=LoadInt(L,Z,"too many numbers");
|
||||
if (n>0)
|
||||
{
|
||||
tf->knum=luaM_newvector(L,n,Number);
|
||||
if (native)
|
||||
LoadBlock(L,tf->knum,n*sizeof(*tf->knum),Z);
|
||||
else
|
||||
for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z);
|
||||
}
|
||||
tf->nkproto=n=LoadInt(L,Z,"too many functions");
|
||||
if (n>0)
|
||||
{
|
||||
tf->kproto=luaM_newvector(L,n,Proto*);
|
||||
for (i=0; i<n; i++) tf->kproto[i]=LoadFunction(L,Z,native);
|
||||
}
|
||||
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 native)
|
||||
static Proto* LoadFunction (LoadState* S, TString* p)
|
||||
{
|
||||
Proto* tf=luaF_newproto(L);
|
||||
tf->source=LoadString(L,Z);
|
||||
if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
|
||||
tf->lineDefined=LoadInt(L,Z,"lineDefined too large");
|
||||
tf->numparams=LoadInt(L,Z,"too many parameters");
|
||||
tf->is_vararg=LoadByte(L,Z);
|
||||
tf->maxstacksize=LoadInt(L,Z,"too much stack needed");
|
||||
LoadCode(L,tf,Z);
|
||||
LoadLocals(L,tf,Z);
|
||||
LoadConstants(L,tf,Z,native);
|
||||
return tf;
|
||||
Proto* f=luaF_newproto(S->L);
|
||||
f->source=LoadString(S); if (f->source==NULL) f->source=p;
|
||||
f->lineDefined=LoadInt(S);
|
||||
f->nups=LoadByte(S);
|
||||
f->numparams=LoadByte(S);
|
||||
f->is_vararg=LoadByte(S);
|
||||
f->maxstacksize=LoadByte(S);
|
||||
LoadLines(S,f);
|
||||
LoadLocals(S,f);
|
||||
LoadUpvalues(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) luaL_verror(L,"bad signature in `%.255s'",ZNAME(Z));
|
||||
if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name);
|
||||
}
|
||||
|
||||
#define V(v) v/16,v%16
|
||||
|
||||
static int LoadHeader (lua_State* L, ZIO* Z)
|
||||
static void TestSize (LoadState* S, int s, const char* what)
|
||||
{
|
||||
int version,sizeofR,native;
|
||||
LoadSignature(L,Z);
|
||||
version=ezgetc(L,Z);
|
||||
int r=LoadByte(S);
|
||||
if (r!=s)
|
||||
luaG_runerror(S->L,"virtual machine mismatch in %s: "
|
||||
"size of %s is %d but read %d",S->name,what,s,r);
|
||||
}
|
||||
|
||||
#define TESTSIZE(s,w) TestSize(S,s,w)
|
||||
#define V(v) v/16,v%16
|
||||
|
||||
static void LoadHeader (LoadState* S)
|
||||
{
|
||||
int version;
|
||||
lua_Number x,tx=TEST_NUMBER;
|
||||
LoadSignature(S);
|
||||
version=LoadByte(S);
|
||||
if (version>VERSION)
|
||||
luaL_verror(L,"`%.255s' too new:\n"
|
||||
" read version %d.%d; expected at most %d.%d",
|
||||
ZNAME(Z),V(version),V(VERSION));
|
||||
if (version<VERSION0) /* check last major change */
|
||||
luaL_verror(L,"`%.255s' too old:\n"
|
||||
" read version %d.%d; expected at least %d.%d",
|
||||
ZNAME(Z),V(version),V(VERSION));
|
||||
{
|
||||
int I=ezgetc(L,Z);
|
||||
int i=ezgetc(L,Z);
|
||||
int op=ezgetc(L,Z);
|
||||
int b=ezgetc(L,Z);
|
||||
if (I!=sizeof(Instruction) || i!=SIZE_INSTRUCTION || op!=SIZE_OP || b!=SIZE_B)
|
||||
luaL_verror(L,"virtual machine mismatch in `%.255s':\n"
|
||||
" read %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments;\n"
|
||||
" expected %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments",
|
||||
ZNAME(Z),i,I,op,b,SIZE_INSTRUCTION,(int)sizeof(Instruction),SIZE_OP,SIZE_B);
|
||||
}
|
||||
sizeofR=ezgetc(L,Z);
|
||||
native=(sizeofR!=0);
|
||||
if (native) /* test number representation */
|
||||
{
|
||||
if (sizeofR!=sizeof(Number))
|
||||
luaL_verror(L,"native number size mismatch in `%.255s':\n"
|
||||
" read %d; expected %d",
|
||||
ZNAME(Z),sizeofR,(int)sizeof(Number));
|
||||
else
|
||||
{
|
||||
Number f=0,tf=TEST_NUMBER;
|
||||
LoadBlock(L,&f,sizeof(f),Z);
|
||||
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
||||
luaL_verror(L,"unknown number format in `%.255s':\n"
|
||||
" read " NUMBER_FMT "; expected " NUMBER_FMT,
|
||||
ZNAME(Z),f,tf);
|
||||
}
|
||||
}
|
||||
return native;
|
||||
luaG_runerror(S->L,"%s too new: "
|
||||
"read version %d.%d; expected at most %d.%d",
|
||||
S->name,V(version),V(VERSION));
|
||||
if (version<VERSION0) /* check last major change */
|
||||
luaG_runerror(S->L,"%s too old: "
|
||||
"read version %d.%d; expected at least %d.%d",
|
||||
S->name,V(version),V(VERSION0));
|
||||
S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */
|
||||
TESTSIZE(sizeof(int),"int");
|
||||
TESTSIZE(sizeof(size_t), "size_t");
|
||||
TESTSIZE(sizeof(Instruction), "Instruction");
|
||||
TESTSIZE(SIZE_OP, "OP");
|
||||
TESTSIZE(SIZE_A, "A");
|
||||
TESTSIZE(SIZE_B, "B");
|
||||
TESTSIZE(SIZE_C, "C");
|
||||
TESTSIZE(sizeof(lua_Number), "number");
|
||||
x=LoadNumber(S);
|
||||
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
|
||||
luaG_runerror(S->L,"unknown number format in %s",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_undump1 (lua_State* L, ZIO* Z)
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_CHUNK)
|
||||
return LoadChunk(L,Z);
|
||||
else if (c!=EOZ)
|
||||
luaL_verror(L,"`%.255s' is not a Lua binary file",ZNAME(Z));
|
||||
return NULL;
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
* convert number from text
|
||||
** find byte order
|
||||
*/
|
||||
double luaU_str2d (lua_State* L, const char* b, const char* where)
|
||||
int luaU_endianness (void)
|
||||
{
|
||||
double x;
|
||||
if (!luaO_str2d(b,&x))
|
||||
luaL_verror(L,"cannot convert number `%.255s' in `%.255s'",b,where);
|
||||
return x;
|
||||
int x=1;
|
||||
return *(char*)&x;
|
||||
}
|
||||
|
||||
41
lundump.h
41
lundump.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.19 2000/04/24 17:32:29 lhf Exp $
|
||||
** $Id: lundump.h,v 1.30 2003/04/07 20:34:20 lhf Exp $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,34 +10,25 @@
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
/* load one chunk */
|
||||
Proto* luaU_undump1 (lua_State* L, ZIO* Z);
|
||||
/* load one chunk; from lundump.c */
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff);
|
||||
|
||||
/* convert number from text */
|
||||
double luaU_str2d (lua_State* L, const char* b, const char* where);
|
||||
/* find byte order; from lundump.c */
|
||||
int luaU_endianness (void);
|
||||
|
||||
/* dump one chunk; from ldump.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 xSOURCE "<%d:%.255s>"
|
||||
#define SOURCE "<%.255s:%d>"
|
||||
#define IN " in %p " SOURCE
|
||||
#define INLOC tf,tf->source->str,tf->lineDefined
|
||||
|
||||
/* format for numbers in listings and error messages */
|
||||
#ifndef NUMBER_FMT
|
||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||
#endif
|
||||
#define LUA_SIGNATURE "\033Lua" /* binary files start with "<esc>Lua" */
|
||||
#define VERSION 0x50 /* last format change was in 5.0 */
|
||||
#define VERSION0 0x50 /* last major change was in 5.0 */
|
||||
|
||||
/* a multiple of PI for testing native format */
|
||||
/* multiplying by 1E8 gives non-trivial integer values */
|
||||
#define TEST_NUMBER 3.14159265358979323846E8
|
||||
|
||||
/* something for testing byte order in instructions */
|
||||
#define TEST_CODE 0x01020304
|
||||
/* multiplying by 1E7 gives non-trivial integer values */
|
||||
#define TEST_NUMBER ((lua_Number)3.14159265358979323846E7)
|
||||
|
||||
#endif
|
||||
|
||||
33
lvm.h
33
lvm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 1.20 2000/03/29 20:19:20 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,22 +13,23 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
#define tonumber(o) ((ttype(o) != TAG_NUMBER) && (luaV_tonumber(o) != 0))
|
||||
#define tostring(L,o) ((ttype(o) != TAG_STRING) && (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))
|
||||
|
||||
|
||||
void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab);
|
||||
int luaV_tonumber (TObject *obj);
|
||||
int luaV_tostring (lua_State *L, TObject *obj);
|
||||
void luaV_setn (lua_State *L, Hash *t, int val);
|
||||
void luaV_gettable (lua_State *L, StkId top);
|
||||
void luaV_settable (lua_State *L, StkId t, StkId top);
|
||||
void luaV_rawsettable (lua_State *L, StkId t);
|
||||
void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top);
|
||||
void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top);
|
||||
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);
|
||||
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
|
||||
|
||||
97
lzio.c
97
lzio.c
@@ -1,75 +1,62 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.10 2000/02/08 16:39:42 roberto Exp roberto $
|
||||
** $Id: lzio.c,v 1.23 2002/12/04 17:38:31 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;
|
||||
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 char2int(*(z->p++));
|
||||
}
|
||||
|
||||
|
||||
ZIO* zmopen (ZIO* z, const char* b, int size, const char *name) {
|
||||
if (b==NULL) return NULL;
|
||||
z->n = size;
|
||||
z->p = (const unsigned char *)b;
|
||||
z->filbuf = zmfilbuf;
|
||||
z->u = NULL;
|
||||
int luaZ_lookahead (ZIO *z) {
|
||||
if (z->n == 0) {
|
||||
int c = luaZ_fill(z);
|
||||
if (c == EOZ) return c;
|
||||
z->n++;
|
||||
z->p--;
|
||||
}
|
||||
return char2int(*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;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ 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) {
|
||||
int n;
|
||||
if (feof((FILE *)z->u)) return EOZ;
|
||||
n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u);
|
||||
if (n==0) return EOZ;
|
||||
z->n = n-1;
|
||||
z->p = z->buffer;
|
||||
return *(z->p++);
|
||||
}
|
||||
|
||||
|
||||
ZIO* zFopen (ZIO* z, FILE* f, const char *name) {
|
||||
if (f==NULL) return NULL;
|
||||
z->n = 0;
|
||||
z->p = z->buffer;
|
||||
z->filbuf = zffilbuf;
|
||||
z->u = f;
|
||||
z->name = name;
|
||||
return z;
|
||||
z->p = NULL;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- read --- */
|
||||
int zread (ZIO *z, void *b, int n) {
|
||||
size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
||||
while (n) {
|
||||
int m;
|
||||
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);
|
||||
@@ -80,3 +67,15 @@ int zread (ZIO *z, void *b, int 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
62
lzio.h
62
lzio.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.h,v 1.4 1998/01/09 14:57:43 roberto Exp roberto $
|
||||
** $Id: lzio.h,v 1.14 2002/10/08 18:46:08 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,43 +8,57 @@
|
||||
#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, int size, const char *name); /* memory */
|
||||
|
||||
int zread (ZIO* z, void* b, int n); /* read next n bytes */
|
||||
#define char2int(c) cast(int, cast(unsigned char, (c)))
|
||||
|
||||
#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
|
||||
|
||||
#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 ------------------ */
|
||||
|
||||
#define ZBSIZE 256 /* buffer size */
|
||||
|
||||
struct zio {
|
||||
int 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
|
||||
|
||||
160
makefile
160
makefile
@@ -1,5 +1,5 @@
|
||||
#
|
||||
## $Id: makefile,v 1.24 2000/04/14 17:52:09 roberto Exp roberto $
|
||||
## $Id: makefile,v 1.40 2003/03/19 21:27:30 roberto Exp roberto $
|
||||
## Makefile
|
||||
## See Copyright Notice in lua.h
|
||||
#
|
||||
@@ -7,28 +7,31 @@
|
||||
|
||||
#CONFIGURATION
|
||||
|
||||
# define (undefine) 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)
|
||||
#
|
||||
# define LUA_COMPAT_READPATTERN if you need read patterns
|
||||
# (only for compatibility with previous versions)
|
||||
# -DEXTERNMEMCHECK -DHARDSTACKTESTS
|
||||
# DEBUG = -g -DLUA_USER_H='"ltests.h"'
|
||||
OPTIMIZE = -O2 \
|
||||
-D'lua_number2int(i,d)=__asm__("fldl %1\nfistpl %0":"=m"(i):"m"(d))' \
|
||||
# -fomit-frame-pointer
|
||||
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||
#CONFIG = -DOLD_ANSI -DDEBUG -DLUA_COMPAT_READPATTERN
|
||||
|
||||
CONFIG = $(DEBUG) $(OPTIMIZE) -DLUA_COMPATUPSYNTAX -DUSE_TMPNAME -DUSE_DLOPEN
|
||||
|
||||
|
||||
# Compilation parameters
|
||||
CC = gcc
|
||||
CWARNS = -Wall -W -Wmissing-prototypes -Wshadow -pedantic -Wpointer-arith -Wcast-align -Waggregate-return -Wcast-qual -Wnested-externs -Wwrite-strings
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O2
|
||||
CWARNS = -Wall -pedantic \
|
||||
-Waggregate-return \
|
||||
-Wcast-align \
|
||||
-Wmissing-prototypes \
|
||||
-Wstrict-prototypes \
|
||||
-Wnested-externs \
|
||||
-Wpointer-arith \
|
||||
-Wshadow \
|
||||
-Wwrite-strings \
|
||||
# -Wtraditional \
|
||||
# -Wcast-qual
|
||||
|
||||
CFLAGS = $(CONFIG) $(CWARNS) # -ansi
|
||||
|
||||
|
||||
# To make early versions
|
||||
@@ -42,10 +45,7 @@ ARFLAGS = rvl
|
||||
# Aplication modules
|
||||
LUAOBJS = \
|
||||
lstate.o \
|
||||
lref.o \
|
||||
lapi.o \
|
||||
lauxlib.o \
|
||||
lbuiltin.o \
|
||||
lmem.o \
|
||||
lstring.o \
|
||||
ltable.o \
|
||||
@@ -53,27 +53,31 @@ LUAOBJS = \
|
||||
lvm.o \
|
||||
ldo.o \
|
||||
lobject.o \
|
||||
lbuffer.o \
|
||||
lfunc.o \
|
||||
lgc.o \
|
||||
lcode.o \
|
||||
lparser.o \
|
||||
llex.o \
|
||||
lopcodes.o \
|
||||
lundump.o \
|
||||
ldump.o \
|
||||
lzio.o \
|
||||
ldebug.o \
|
||||
ltests.o
|
||||
ltests.o
|
||||
|
||||
LIBOBJS = \
|
||||
liolib.o \
|
||||
lauxlib.o \
|
||||
lbaselib.o \
|
||||
ltablib.o \
|
||||
lmathlib.o \
|
||||
liolib.o \
|
||||
lstrlib.o \
|
||||
ldblib.o \
|
||||
linit.o
|
||||
loadlib.o
|
||||
|
||||
|
||||
lua : lua.o liblua.a liblualib.a
|
||||
$(CC) $(CFLAGS) -o $@ lua.o -L. -llua -llualib -lm
|
||||
$(CC) $(CFLAGS) -o $@ lua.o -Wl,-E -L. -llua -llualib -lm -ldl
|
||||
|
||||
liblua.a : $(LUAOBJS)
|
||||
$(AR) $(ARFLAGS) $@ $?
|
||||
@@ -99,56 +103,54 @@ clear :
|
||||
co $(CO_OPTIONS) $@
|
||||
|
||||
|
||||
lapi.o: lapi.c lapi.h lobject.h llimits.h lua.h lauxlib.h ldo.h \
|
||||
lstate.h luadebug.h lfunc.h lgc.h lmem.h lref.h lstring.h ltable.h \
|
||||
ltm.h lvm.h
|
||||
lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h
|
||||
lbuffer.o: lbuffer.c lauxlib.h lua.h lmem.h lstate.h lobject.h \
|
||||
llimits.h luadebug.h
|
||||
lbuiltin.o: lbuiltin.c lapi.h lobject.h llimits.h lua.h lauxlib.h \
|
||||
lbuiltin.h ldo.h lstate.h luadebug.h lfunc.h lmem.h lstring.h \
|
||||
ltable.h ltm.h lundump.h lzio.h lvm.h
|
||||
lcode.o: lcode.c /usr/include/stdlib.h lcode.h llex.h lobject.h \
|
||||
llimits.h lua.h lzio.h lopcodes.h lparser.h ldo.h lstate.h luadebug.h \
|
||||
lmem.h lstring.h
|
||||
ldblib.o: ldblib.c lauxlib.h lua.h luadebug.h lualib.h
|
||||
ldebug.o: ldebug.c lapi.h lobject.h llimits.h lua.h lauxlib.h ldebug.h \
|
||||
luadebug.h ldo.h lstate.h lfunc.h ltable.h ltm.h
|
||||
ldo.o: ldo.c lauxlib.h lua.h ldebug.h lobject.h llimits.h luadebug.h \
|
||||
ldo.h lstate.h lgc.h lmem.h lparser.h lzio.h lstring.h ltm.h \
|
||||
lundump.h lvm.h
|
||||
lfunc.o: lfunc.c lfunc.h lobject.h llimits.h lua.h lmem.h lstate.h \
|
||||
luadebug.h
|
||||
lgc.o: lgc.c ldo.h lobject.h llimits.h lua.h lstate.h luadebug.h \
|
||||
lfunc.h lgc.h lmem.h lref.h lstring.h ltable.h ltm.h
|
||||
linit.o: linit.c lua.h lualib.h
|
||||
liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h
|
||||
llex.o: llex.c lauxlib.h lua.h llex.h lobject.h llimits.h lzio.h \
|
||||
lmem.h lparser.h lstate.h luadebug.h lstring.h
|
||||
lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h
|
||||
lmem.o: lmem.c lmem.h lua.h lobject.h llimits.h lstate.h luadebug.h
|
||||
lobject.o: lobject.c lobject.h llimits.h lua.h
|
||||
lparser.o: lparser.c lcode.h llex.h lobject.h llimits.h lua.h lzio.h \
|
||||
lopcodes.h lparser.h ldo.h lstate.h luadebug.h lfunc.h lmem.h \
|
||||
lstring.h
|
||||
lref.o: lref.c lapi.h lobject.h llimits.h lua.h lmem.h lref.h lstate.h \
|
||||
luadebug.h
|
||||
lstate.o: lstate.c lauxlib.h lua.h lbuiltin.h ldo.h lobject.h \
|
||||
llimits.h lstate.h luadebug.h lgc.h llex.h lzio.h lmem.h lref.h \
|
||||
lstring.h ltm.h
|
||||
lstring.o: lstring.c lmem.h lua.h lobject.h llimits.h lstate.h \
|
||||
luadebug.h lstring.h
|
||||
lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h
|
||||
ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h llimits.h lstate.h \
|
||||
luadebug.h ltable.h
|
||||
ltests.o: ltests.c lapi.h lobject.h llimits.h lua.h lauxlib.h lmem.h \
|
||||
lopcodes.h lstate.h luadebug.h lstring.h ltable.h
|
||||
ltm.o: ltm.c lauxlib.h lua.h lmem.h lobject.h llimits.h lstate.h \
|
||||
luadebug.h ltm.h
|
||||
lua.o: lua.c lua.h luadebug.h lualib.h
|
||||
lundump.o: lundump.c lauxlib.h lua.h lfunc.h lobject.h llimits.h \
|
||||
lmem.h lopcodes.h lstring.h lstate.h luadebug.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lauxlib.h lua.h ldebug.h lobject.h llimits.h luadebug.h \
|
||||
ldo.h lstate.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h ltm.h \
|
||||
lvm.h
|
||||
lzio.o: lzio.c lzio.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 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
|
||||
loadlib.o: loadlib.c lua.h lauxlib.h lualib.h
|
||||
lopcodes.o: lopcodes.c lua.h lobject.h llimits.h lopcodes.h
|
||||
lparser.o: lparser.c lua.h lcode.h llex.h lobject.h llimits.h lzio.h \
|
||||
lopcodes.h lparser.h ltable.h ldebug.h lstate.h ltm.h lfunc.h lmem.h \
|
||||
lstring.h
|
||||
lstate.o: lstate.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
||||
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 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 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
|
||||
|
||||
3502
manual.tex
3502
manual.tex
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user