mirror of
https://github.com/lua/lua.git
synced 2026-07-26 16:09:07 +00:00
Compare commits
1760 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98194db429 | ||
|
|
fad7582c9a | ||
|
|
a62fca1ebb | ||
|
|
2b5c1f99e5 | ||
|
|
e2dc5f5d15 | ||
|
|
ed64346b9d | ||
|
|
475b0ecbf1 | ||
|
|
414359b2f1 | ||
|
|
0079efc479 | ||
|
|
ad0765b4f0 | ||
|
|
20f4bbdc3a | ||
|
|
c408158047 | ||
|
|
55e323190e | ||
|
|
fe8f4c06f1 | ||
|
|
c351392940 | ||
|
|
18330b6091 | ||
|
|
8487913697 | ||
|
|
672bb67ee6 | ||
|
|
90df6b7a54 | ||
|
|
0238a0b01e | ||
|
|
1ae0b6c0bf | ||
|
|
e1dda047b2 | ||
|
|
2c8206d448 | ||
|
|
ae76307847 | ||
|
|
3d61c31e5a | ||
|
|
44a9bd6a8c | ||
|
|
69ea087dff | ||
|
|
c05aaf3bf0 | ||
|
|
298ae7e230 | ||
|
|
6316a866a3 | ||
|
|
ea2cc2bc47 | ||
|
|
64205e91a3 | ||
|
|
8173688542 | ||
|
|
3ef5a6797f | ||
|
|
9e6e43984d | ||
|
|
baffc37f5c | ||
|
|
440113bbe8 | ||
|
|
73ebc5d8f6 | ||
|
|
a666752b1d | ||
|
|
f8b7a5581e | ||
|
|
ffb798e1e2 | ||
|
|
fa936f8fa9 | ||
|
|
dd1221582b | ||
|
|
bfdcbbcd76 | ||
|
|
30eebb2d1c | ||
|
|
0fd6466957 | ||
|
|
6a516878e9 | ||
|
|
a486090a5c | ||
|
|
0160591998 | ||
|
|
c505f341d6 | ||
|
|
428325baec | ||
|
|
0561f71f0f | ||
|
|
9fbefdf69c | ||
|
|
43c61fc113 | ||
|
|
ea6b1b42c7 | ||
|
|
87024e257d | ||
|
|
6cd461633d | ||
|
|
16fd4abaf6 | ||
|
|
f26b85c5b7 | ||
|
|
3592c08a2c | ||
|
|
db724e14e0 | ||
|
|
1702953293 | ||
|
|
33b4dec1cb | ||
|
|
7dd10a1d7e | ||
|
|
521a452f28 | ||
|
|
2753134a38 | ||
|
|
cbbde11a8a | ||
|
|
7b89cf3aeb | ||
|
|
d2811e8097 | ||
|
|
e43e95553f | ||
|
|
930018e273 | ||
|
|
a160266c3d | ||
|
|
d4c99b2673 | ||
|
|
88f9843eee | ||
|
|
9b5ae6faf9 | ||
|
|
caf3cfc6ac | ||
|
|
ff7638b0ed | ||
|
|
8e48ebb638 | ||
|
|
c2bd68d2c3 | ||
|
|
201288b4df | ||
|
|
62367dda44 | ||
|
|
056b6a8ef4 | ||
|
|
502d8f9a06 | ||
|
|
7f376e6ea8 | ||
|
|
053e873145 | ||
|
|
9f4211310f | ||
|
|
515d556832 | ||
|
|
ed7039024d | ||
|
|
5511bf6b9d | ||
|
|
86704cffe9 | ||
|
|
fa41fafa6e | ||
|
|
ebf646efb5 | ||
|
|
b3f81f1d1d | ||
|
|
a24eb3ef17 | ||
|
|
4385e2ada4 | ||
|
|
e9b06c506f | ||
|
|
cd12ab2597 | ||
|
|
e251e84e0f | ||
|
|
e6bfbc38b5 | ||
|
|
d31c04c049 | ||
|
|
522481a788 | ||
|
|
37b49aa451 | ||
|
|
0fa9b16148 | ||
|
|
e41fb9a604 | ||
|
|
5fc92cce12 | ||
|
|
6dd0c6ccbc | ||
|
|
a77413acd0 | ||
|
|
5dbb870815 | ||
|
|
ede19c9138 | ||
|
|
9e88b336b4 | ||
|
|
0fae476ed4 | ||
|
|
361a9adba7 | ||
|
|
d8f1cca16e | ||
|
|
bf96d3fdd9 | ||
|
|
c8c2e06899 | ||
|
|
8dcc6bc532 | ||
|
|
38f585d271 | ||
|
|
6887f91799 | ||
|
|
7fbbf1adc6 | ||
|
|
6dd3a73ca2 | ||
|
|
1e6a559b29 | ||
|
|
2d2f955763 | ||
|
|
669f6306be | ||
|
|
2bcbd3c725 | ||
|
|
5fa6604f00 | ||
|
|
ce67a9be31 | ||
|
|
506c89cef8 | ||
|
|
8ead2ec358 | ||
|
|
c3cc4de3fd | ||
|
|
351f7dad6b | ||
|
|
3b19af44b0 | ||
|
|
3390f9a35a | ||
|
|
9273fbd131 | ||
|
|
be666a662b | ||
|
|
39cdbce23e | ||
|
|
8974b59e40 | ||
|
|
4ff1e5b4e1 | ||
|
|
98296f6b45 | ||
|
|
23b79c5945 | ||
|
|
6fcd334ca0 | ||
|
|
43ad0637ca | ||
|
|
027e9e99ca | ||
|
|
3b828d9e48 | ||
|
|
68548a02d3 | ||
|
|
2f2b4a42a9 | ||
|
|
074352911f | ||
|
|
5e8a9e324c | ||
|
|
16ddf86168 | ||
|
|
e4d369c9b7 | ||
|
|
42a662334a | ||
|
|
39c0f391c6 | ||
|
|
c7b4f44772 | ||
|
|
44b19075c0 | ||
|
|
ab3dfa5594 | ||
|
|
62f786e08c | ||
|
|
bbfd28959d | ||
|
|
894cd31c52 | ||
|
|
f01a95d4a0 | ||
|
|
680fc88cc2 | ||
|
|
d96f088a26 | ||
|
|
d8be9a6326 | ||
|
|
95f3eefa5b | ||
|
|
e82bdb24f3 | ||
|
|
1a343814d8 | ||
|
|
280f7becb8 | ||
|
|
d6ff5d9f46 | ||
|
|
a05190fa3b | ||
|
|
a569099b70 | ||
|
|
20131e54d3 | ||
|
|
80ac5e83fd | ||
|
|
289b5dc67a | ||
|
|
c2f065f405 | ||
|
|
b261cd110f | ||
|
|
e4324f54b9 | ||
|
|
3d08aa591d | ||
|
|
0f30d00c87 | ||
|
|
e86dab2dae | ||
|
|
64b57db377 | ||
|
|
575637319e | ||
|
|
87be01598b | ||
|
|
e84b11a494 | ||
|
|
bbf2cea9a1 | ||
|
|
b3f7a43019 | ||
|
|
7f97ec286e | ||
|
|
e82b41aac2 | ||
|
|
ee1edd5734 | ||
|
|
8efb44b5e0 | ||
|
|
f7720bebe3 | ||
|
|
69cc0a12fe | ||
|
|
2f0955b99b | ||
|
|
999d55d887 | ||
|
|
1f9f97e3cf | ||
|
|
34aac33c15 | ||
|
|
3c2cf9db67 | ||
|
|
0b71bfd6af | ||
|
|
0d1685d751 | ||
|
|
bf2b342ac1 | ||
|
|
5fa2bec1f0 | ||
|
|
746a1d612b | ||
|
|
3ad03b331d | ||
|
|
86add535d8 | ||
|
|
d987cf1f81 | ||
|
|
eca9fa02d2 | ||
|
|
e33d7bae45 | ||
|
|
d628795940 | ||
|
|
05348d834b | ||
|
|
90de38bf1f | ||
|
|
e8a7ecb982 | ||
|
|
38da9d568a | ||
|
|
f21e9c172f | ||
|
|
67578ec51f | ||
|
|
c2bb9abcec | ||
|
|
da32450c3d | ||
|
|
a2b78aad49 | ||
|
|
c6293a76cc | ||
|
|
1ab2b93462 | ||
|
|
6cf85dcc90 | ||
|
|
65f4a0f636 | ||
|
|
12acca7bd8 | ||
|
|
d5fd44d747 | ||
|
|
b0abc2ca03 | ||
|
|
b320d37a80 | ||
|
|
9b8d136e1c | ||
|
|
9832f6b186 | ||
|
|
8e696dd14a | ||
|
|
c3939f5786 | ||
|
|
bcc4089a7d | ||
|
|
cf5d1bdd76 | ||
|
|
8718fda9b2 | ||
|
|
8c3c3e7691 | ||
|
|
04f657c7f8 | ||
|
|
2873d4efff | ||
|
|
a7ea0c71e8 | ||
|
|
b1957b1e8e | ||
|
|
a17e1fe124 | ||
|
|
fabf5db237 | ||
|
|
de0bfe33b7 | ||
|
|
9768e2f681 | ||
|
|
71eb824036 | ||
|
|
7b6c3b463e | ||
|
|
0316308c0d | ||
|
|
409ee99900 | ||
|
|
445eda7ead | ||
|
|
092dc95b06 | ||
|
|
e6e543a534 | ||
|
|
1c9c886974 | ||
|
|
ade585bdf9 | ||
|
|
04c41444e2 | ||
|
|
03fb3c39d4 | ||
|
|
8af1ca02e0 | ||
|
|
f1eb79fdf4 | ||
|
|
408cb71795 | ||
|
|
390256edf7 | ||
|
|
ad24cff0f1 | ||
|
|
6d475731ca | ||
|
|
f41fc0eb0e | ||
|
|
d872090248 | ||
|
|
fb0f95a2b7 | ||
|
|
d3a6d95b9a | ||
|
|
47984a0cc2 | ||
|
|
086d6b09ce | ||
|
|
bfe2905618 | ||
|
|
2cbd9eecfb | ||
|
|
4494094031 | ||
|
|
04bbd01171 | ||
|
|
9ffae705ee | ||
|
|
6bfef60e77 | ||
|
|
370d31a559 | ||
|
|
f8df21bd20 | ||
|
|
d3902cfa81 | ||
|
|
b5b230d95c | ||
|
|
7d57ea70bc | ||
|
|
2d5b923759 | ||
|
|
192d185492 | ||
|
|
e010ef8948 | ||
|
|
1e48b20ce0 | ||
|
|
d1ed6e4c07 | ||
|
|
b6a7983b65 | ||
|
|
49f90ffdeb | ||
|
|
dcabf721f8 | ||
|
|
5eeff89f32 | ||
|
|
d6143b1d7b | ||
|
|
d55bb795fa | ||
|
|
d84cc9d2db | ||
|
|
7d45a5f48f | ||
|
|
73d7640244 | ||
|
|
a9af455b32 | ||
|
|
678d2fb2ac | ||
|
|
af39352632 | ||
|
|
4df8800a01 | ||
|
|
0e60572606 | ||
|
|
621ef9f767 | ||
|
|
d89a56f869 | ||
|
|
334ba8132b | ||
|
|
ac71a0891d | ||
|
|
8e346d875a | ||
|
|
5be517602e | ||
|
|
d2bda8046c | ||
|
|
a72fbf794d | ||
|
|
8ddfe3df29 | ||
|
|
6eb68ba57a | ||
|
|
a27497aa03 | ||
|
|
0480ea0886 | ||
|
|
162b66624d | ||
|
|
e791f025c0 | ||
|
|
f61d435a7d | ||
|
|
071b2ae0e1 | ||
|
|
e2498e079e | ||
|
|
65726f3e2e | ||
|
|
9fe2d0266a | ||
|
|
e22cdf7752 | ||
|
|
a5eb48a3e6 | ||
|
|
49fe79e9fd | ||
|
|
96727c61b8 | ||
|
|
d5ebc3ff6d | ||
|
|
3d19aee78f | ||
|
|
4194de70e7 | ||
|
|
c6254dceff | ||
|
|
a56d889f72 | ||
|
|
2ec4fc4295 | ||
|
|
70751dd27c | ||
|
|
39a8082f50 | ||
|
|
531874f6ce | ||
|
|
61a888518f | ||
|
|
c78940f21a | ||
|
|
8b239eeba1 | ||
|
|
a5330b77f3 | ||
|
|
e1c2fb6eed | ||
|
|
12dacd3c0e | ||
|
|
23b38176a7 | ||
|
|
97e2dab1fb | ||
|
|
0ed8519127 | ||
|
|
0e002005b1 | ||
|
|
1d99a7360b | ||
|
|
2f82bf6fe9 | ||
|
|
087df82a61 | ||
|
|
10e48b9160 | ||
|
|
6f1ea817f5 | ||
|
|
cdcb236747 | ||
|
|
9be5844713 | ||
|
|
497f042fad | ||
|
|
2ed07ea8c1 | ||
|
|
cfd7bc478f | ||
|
|
737ec947d3 | ||
|
|
45d566f676 | ||
|
|
cfb79b1751 | ||
|
|
3e1a1f2836 | ||
|
|
b32e9a46d2 | ||
|
|
e5cfa7a367 | ||
|
|
aae1612747 | ||
|
|
8050e75f9d | ||
|
|
3bec76abe3 | ||
|
|
5cb6037d49 | ||
|
|
f1c2dd3aed | ||
|
|
bd38017ddf | ||
|
|
652f885c30 | ||
|
|
f7e9465a0c | ||
|
|
5ee87acd6b | ||
|
|
5d834bdf57 | ||
|
|
67391a35da | ||
|
|
36541dec9b | ||
|
|
cf2a194edc | ||
|
|
66845f415f | ||
|
|
b2820f39a2 | ||
|
|
8b5bb6056b | ||
|
|
4973682294 | ||
|
|
6acfb91c8d | ||
|
|
2419f2bf02 | ||
|
|
0e54d2be36 | ||
|
|
226c57fec0 | ||
|
|
0de2065f4e | ||
|
|
ee165043ef | ||
|
|
3c394b12f9 | ||
|
|
79fc7dea43 | ||
|
|
9c34e23214 | ||
|
|
2aba14b623 | ||
|
|
1dd8c9b6b6 | ||
|
|
7d309480dd | ||
|
|
bcb2cb59ac | ||
|
|
0b06241483 | ||
|
|
857253cdfc | ||
|
|
5dcd908add | ||
|
|
32d4f304db | ||
|
|
4b12eff801 | ||
|
|
271e05917f | ||
|
|
fe8c365281 | ||
|
|
2d8b099274 | ||
|
|
ff4f8fe59a | ||
|
|
a7c74c06b9 | ||
|
|
c7dda42c2f | ||
|
|
26ae992129 | ||
|
|
f87057690b | ||
|
|
9e6aa878c9 | ||
|
|
c0fdaf5842 | ||
|
|
2430d3cb28 | ||
|
|
6bddbbde7a | ||
|
|
31e48f26ed | ||
|
|
0d9007b642 | ||
|
|
9609236fbb | ||
|
|
942496d767 | ||
|
|
a535ad6fee | ||
|
|
4206d7ed60 | ||
|
|
7a796a0682 | ||
|
|
76d8b8db06 | ||
|
|
ce455481ab | ||
|
|
85f1d70e68 | ||
|
|
31f6540fba | ||
|
|
eab1965c05 | ||
|
|
3d5187f6e5 | ||
|
|
80ec81926c | ||
|
|
96917ff42a | ||
|
|
a6179adf12 | ||
|
|
02a2c01ccd | ||
|
|
23e08a68f5 | ||
|
|
0f5a497ed9 | ||
|
|
753625c3f3 | ||
|
|
42b74ccf1d | ||
|
|
0c49857c1a | ||
|
|
6b41e84da5 | ||
|
|
359840c2f8 | ||
|
|
dba17070ac | ||
|
|
569eefbf73 | ||
|
|
5e4ea9a831 | ||
|
|
6311e3109f | ||
|
|
9b854e6dbc | ||
|
|
2b2d8ecd7a | ||
|
|
75057dfced | ||
|
|
345379b5ff | ||
|
|
118e471fa0 | ||
|
|
ba9878fcb8 | ||
|
|
fa38421cea | ||
|
|
e8d4fe7236 | ||
|
|
b8691f13a8 | ||
|
|
f4718544de | ||
|
|
50f9cd201e | ||
|
|
9d8426bab4 | ||
|
|
557f0009c4 | ||
|
|
0dfd04eb60 | ||
|
|
1e0aaf2156 | ||
|
|
616438fe9a | ||
|
|
47eda6ebd8 | ||
|
|
0f49b3e62d | ||
|
|
0733c83b8c | ||
|
|
e9844a4f1d | ||
|
|
c61bb89dba | ||
|
|
0bda88e6cd | ||
|
|
7966a4acae | ||
|
|
37cd986532 | ||
|
|
a17dd24b34 | ||
|
|
b072e4ea0b | ||
|
|
7e41612eb2 | ||
|
|
b0f341ee52 | ||
|
|
1c459b881f | ||
|
|
9629508a98 | ||
|
|
763b8fba1b | ||
|
|
b4cd38ba6c | ||
|
|
079facab40 | ||
|
|
4c5d7b2ddd | ||
|
|
d7cb622866 | ||
|
|
26a9b249db | ||
|
|
227bddafe8 | ||
|
|
5008e3d7d1 | ||
|
|
21bc05c920 | ||
|
|
29a13b4128 | ||
|
|
ef8c0d7d90 | ||
|
|
178246062c | ||
|
|
24f2d1183c | ||
|
|
8e1f25e3f8 | ||
|
|
48a8d78102 | ||
|
|
a4e1230f95 | ||
|
|
9804467eeb | ||
|
|
a003e89125 | ||
|
|
b876ec61c0 | ||
|
|
898e8a6794 | ||
|
|
1edea34666 | ||
|
|
2aaf7394ad | ||
|
|
b3ce450529 | ||
|
|
24434b59f7 | ||
|
|
beb2aa5a46 | ||
|
|
47fc57a252 | ||
|
|
4d5fe1f54b | ||
|
|
df429f163a | ||
|
|
fe595a45c2 | ||
|
|
9db1942bac | ||
|
|
c6eac44a94 | ||
|
|
8878554b85 | ||
|
|
af850484a9 | ||
|
|
1d10acb355 | ||
|
|
da61624756 | ||
|
|
8b97b072cd | ||
|
|
5ee63257f9 | ||
|
|
8bc6c68021 | ||
|
|
57b6ed6815 | ||
|
|
9b9cdfee8b | ||
|
|
366e4af3c9 | ||
|
|
53c7f86194 | ||
|
|
ab7d9bfd0c | ||
|
|
921b1723e2 | ||
|
|
fa198197a0 | ||
|
|
1e944de6cb | ||
|
|
275c150b17 | ||
|
|
d6d896b953 | ||
|
|
07201066bd | ||
|
|
97b838d9ab | ||
|
|
c51bcf4796 | ||
|
|
03bab90303 | ||
|
|
8502331f69 | ||
|
|
9914e1bcdd | ||
|
|
10de467c79 | ||
|
|
533737f26e | ||
|
|
a41d60e1d1 | ||
|
|
f04fe526cd | ||
|
|
21947deddc | ||
|
|
3087636c76 | ||
|
|
7b8504840f | ||
|
|
3a508b8bd7 | ||
|
|
6becdb3e70 | ||
|
|
b97fb932ec | ||
|
|
c7cf92e6f3 | ||
|
|
a62d39aa1c | ||
|
|
5d4bf35ec9 | ||
|
|
6063c5c61f | ||
|
|
2a70107581 | ||
|
|
7dae7899b1 | ||
|
|
30e51f09b9 | ||
|
|
994a37c8e8 | ||
|
|
0ff1596476 | ||
|
|
b27664e0db | ||
|
|
beb896b082 | ||
|
|
8332d5c8a5 | ||
|
|
885961be1d | ||
|
|
b114142799 | ||
|
|
4b2e71ddb6 | ||
|
|
9fcc485176 | ||
|
|
64066359dd | ||
|
|
97af24ea32 | ||
|
|
433cb1d13a | ||
|
|
d66198719d | ||
|
|
91bf77534c | ||
|
|
a0a384a6b1 | ||
|
|
e89945aaa1 | ||
|
|
d407d3fe0e | ||
|
|
522407e23a | ||
|
|
fa26d294ae | ||
|
|
5a761e3a15 | ||
|
|
265530478b | ||
|
|
00180bb133 | ||
|
|
66ed154c89 | ||
|
|
f52f357a55 | ||
|
|
79fee99024 | ||
|
|
a73ded2153 | ||
|
|
0ec4c473df | ||
|
|
9e05503ffa | ||
|
|
17f695157c | ||
|
|
b93f67f2ce | ||
|
|
60aa01d8f7 | ||
|
|
ecf5730c0c | ||
|
|
b10bfd4934 | ||
|
|
176cd7d1a3 | ||
|
|
859c7f7ce0 | ||
|
|
56c5e749cc | ||
|
|
9ec9a012e7 | ||
|
|
dae99205cf | ||
|
|
9eb0f25a29 | ||
|
|
f93051155c | ||
|
|
524ae9fc3d | ||
|
|
0ddedaee92 | ||
|
|
5cc448386a | ||
|
|
c116dcb92b | ||
|
|
6d268b0b00 | ||
|
|
c767747191 | ||
|
|
f966404ed6 | ||
|
|
ee07ad346d | ||
|
|
fee9e473f2 | ||
|
|
82dfacbcf7 | ||
|
|
26097b6c4b | ||
|
|
f1f8f0ca22 | ||
|
|
bcc5f1406b | ||
|
|
b1b752fb2b | ||
|
|
572a69df78 | ||
|
|
943c82b376 | ||
|
|
17149b7d43 | ||
|
|
01b303c87e | ||
|
|
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 | ||
|
|
b9c98cd4d9 | ||
|
|
0e8855e171 | ||
|
|
2ae9c856cf | ||
|
|
c4409f69ab | ||
|
|
89a9c36281 | ||
|
|
8f2fba5877 | ||
|
|
62824137d6 | ||
|
|
71219ccc39 | ||
|
|
870f61d299 | ||
|
|
c845ec777a | ||
|
|
e408cf61a2 | ||
|
|
4e90768635 | ||
|
|
c1666a13e3 | ||
|
|
e02750ec60 | ||
|
|
0b56646baf | ||
|
|
634c3d57e9 | ||
|
|
4aa9ad6514 | ||
|
|
d76b1a0eef | ||
|
|
e7c8393682 | ||
|
|
ceaa97ff5b | ||
|
|
2bb77cdaed | ||
|
|
f9cf402fbd | ||
|
|
0c3fe2c44b | ||
|
|
0433e42023 | ||
|
|
0810bc707f | ||
|
|
c3b73cbeb8 | ||
|
|
1eabd0549e | ||
|
|
54dd5cc7fd | ||
|
|
031978798c | ||
|
|
14251c5a56 | ||
|
|
42130ed712 | ||
|
|
d615e78e08 | ||
|
|
c6965ce551 | ||
|
|
22329e4cdf | ||
|
|
9f734094f9 | ||
|
|
4e7e9e8de5 | ||
|
|
3e45496295 | ||
|
|
e30327728c | ||
|
|
36e1390631 | ||
|
|
8f0f54ec38 | ||
|
|
556a89e537 | ||
|
|
e2c60eda16 | ||
|
|
a69356e9e0 | ||
|
|
b53dc0c485 | ||
|
|
63e230a16b | ||
|
|
102a1be615 | ||
|
|
4c03196697 | ||
|
|
54633d7c7d | ||
|
|
13578517c7 | ||
|
|
213e9febc8 | ||
|
|
47b4bf5964 | ||
|
|
415ee1a24f | ||
|
|
5f3c5ace95 | ||
|
|
441469f803 | ||
|
|
1157c2a7e4 | ||
|
|
374cc888f4 | ||
|
|
bb9605bbc9 | ||
|
|
6759299880 | ||
|
|
da7eb34cd6 | ||
|
|
2c8e28d75a | ||
|
|
4fb8e93c36 | ||
|
|
7f77431950 | ||
|
|
87367e97f3 | ||
|
|
06f08f5634 | ||
|
|
3860c5934e | ||
|
|
f0b697e01c | ||
|
|
73aa465a8e | ||
|
|
3d0577f4b9 | ||
|
|
977b8f65f0 | ||
|
|
e3cf93ddb6 | ||
|
|
88b306f495 | ||
|
|
563de491be | ||
|
|
29e0193425 | ||
|
|
61f97a8ace | ||
|
|
d1ea38580a | ||
|
|
3c9d999424 | ||
|
|
f7840a3e0b | ||
|
|
1780e2c977 | ||
|
|
fa14cbf8e8 | ||
|
|
4d4e6f07c0 | ||
|
|
5cecf0ea9f | ||
|
|
3bc925138e | ||
|
|
39e1f079bd | ||
|
|
075da266e5 | ||
|
|
228a62f799 | ||
|
|
7c2fb15bec | ||
|
|
1f01998516 | ||
|
|
c929376d59 | ||
|
|
3afe85b2ce | ||
|
|
52aad0ab59 | ||
|
|
74f1c3d025 | ||
|
|
1f691a4fcd | ||
|
|
d6b9f49aaf | ||
|
|
d83c2a8455 | ||
|
|
d11e5adf55 | ||
|
|
99e340b2ba | ||
|
|
9744255ae9 | ||
|
|
512b15b601 | ||
|
|
33d3504889 | ||
|
|
2877bad4c2 | ||
|
|
27163f032e | ||
|
|
eab57ed6fd | ||
|
|
ea45f3eb28 | ||
|
|
46ec57cbc6 | ||
|
|
62787f1b1f | ||
|
|
dcb7bb514e | ||
|
|
b3aaa048b0 | ||
|
|
aee07c6599 | ||
|
|
bcdbdaccc3 | ||
|
|
5cafe5af02 | ||
|
|
607012ece1 | ||
|
|
0652906e7a | ||
|
|
4d1b815b60 | ||
|
|
4ebc183e56 | ||
|
|
298d0abff7 | ||
|
|
4fbe775154 | ||
|
|
aabefe9d68 | ||
|
|
fb60283974 | ||
|
|
acdb0b741e | ||
|
|
5b08fcd5a1 | ||
|
|
b1b0c219f5 | ||
|
|
be3212de78 | ||
|
|
2eaf9a00ae | ||
|
|
6c79a0a80d | ||
|
|
3daeabb606 | ||
|
|
cfdebfbc53 | ||
|
|
d61d5bef9d | ||
|
|
4450efc97e | ||
|
|
de65253f2d | ||
|
|
12b45c2df2 | ||
|
|
1b15206cf9 | ||
|
|
e6d56cd2d8 | ||
|
|
c16c63cc59 | ||
|
|
dea54a7c71 | ||
|
|
544eeb1f9c | ||
|
|
b3fe203c36 | ||
|
|
9c142740c1 | ||
|
|
7621cc2aa5 | ||
|
|
ba1f504970 | ||
|
|
617be66015 | ||
|
|
f356eb010b | ||
|
|
3e38fd2463 | ||
|
|
533beedad2 | ||
|
|
968ad49da6 | ||
|
|
1fdb445e7d | ||
|
|
c7119a93f0 | ||
|
|
3c9e29f56d | ||
|
|
72afb6debb | ||
|
|
b097076678 | ||
|
|
8223ff473f | ||
|
|
fe237ad808 | ||
|
|
3181dfefee | ||
|
|
764e095d7f | ||
|
|
2b3ea61342 | ||
|
|
49af08e3e7 | ||
|
|
c5fa0895e9 | ||
|
|
d015f1fc02 | ||
|
|
790690a223 | ||
|
|
33b8a01032 | ||
|
|
d29ce75737 | ||
|
|
878ef96127 | ||
|
|
e5743adb21 | ||
|
|
514783de9d | ||
|
|
7d1499ba88 | ||
|
|
29ede6aa13 | ||
|
|
951897c093 | ||
|
|
a34677e4f4 | ||
|
|
573372665a | ||
|
|
a6755e2f1a | ||
|
|
a4628a02cf | ||
|
|
0c725b2492 | ||
|
|
d915cf4f9d | ||
|
|
53fb65d394 | ||
|
|
a7fa7bafc8 | ||
|
|
cde179b369 | ||
|
|
80b39d83c3 | ||
|
|
5a48255c9f | ||
|
|
bbab974717 | ||
|
|
910836fb53 | ||
|
|
8e7451512f | ||
|
|
4e9f2d13d5 | ||
|
|
b6ebbb2fee | ||
|
|
c5fee7615e | ||
|
|
cca78b5c71 | ||
|
|
d435b7acd8 | ||
|
|
63ccf42397 | ||
|
|
1942b58609 | ||
|
|
4343420d4d | ||
|
|
1f7103e05d | ||
|
|
dcc10accea | ||
|
|
e2fc5aa684 | ||
|
|
d53e2d2386 | ||
|
|
cf9a22396c | ||
|
|
17374d2daa | ||
|
|
04265655a8 | ||
|
|
e10788b2ff | ||
|
|
7eb2f18621 | ||
|
|
a8a75fb7ab | ||
|
|
ae3ecc2d4a | ||
|
|
2e13cd77ab | ||
|
|
ff9c0da783 | ||
|
|
f7fe04d4b7 | ||
|
|
82699d0c4f | ||
|
|
88866208f0 | ||
|
|
7d365a5c7a | ||
|
|
52a206366d | ||
|
|
b2c89ed2d2 | ||
|
|
2a03170ebd | ||
|
|
05f55cc062 | ||
|
|
c787dccd9b | ||
|
|
b44e35b773 | ||
|
|
21843f022a | ||
|
|
900257e814 | ||
|
|
a82c8185bc | ||
|
|
4bbe0679a8 | ||
|
|
438598aeea | ||
|
|
e9a670695a |
911
bugs
911
bugs
@@ -1,3 +1,4 @@
|
||||
--[=[
|
||||
** lua.stx / llex.c
|
||||
Tue Dec 2 10:45:48 EDT 1997
|
||||
>> BUG: "lastline" was not reset on function entry, so debug information
|
||||
@@ -5,6 +6,7 @@ Tue Dec 2 10:45:48 EDT 1997
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 3.1 alpha
|
||||
|
||||
** lua.c
|
||||
@@ -44,6 +46,7 @@ Mon May 18 19:20:00 EST 1998
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 3.1
|
||||
|
||||
** liolib.c / lauxlib.c
|
||||
@@ -83,6 +86,7 @@ Thu Mar 4 11:49:37 EST 1999
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 3.2 (beta)
|
||||
|
||||
** lstrlib.c
|
||||
@@ -102,3 +106,910 @@ Wed Jun 16 10:32:46 EST 1999
|
||||
the number of returns of a function.
|
||||
(since 3.1)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 3.2
|
||||
|
||||
** lmathlib.c
|
||||
Wed Aug 18 11:28:38 EST 1999
|
||||
>> random(0) and random(x,0) are wrong (0 is read as no argument!).
|
||||
(by Dave Bollinger; since 3.1)
|
||||
|
||||
** lparser.c
|
||||
Thu Sep 2 10:07:20 EST 1999
|
||||
>> in the (old) expression << ls->fs->f->consts[checkname(ls)] >>, checkname
|
||||
could realloc f->consts.
|
||||
(by Supratik Champati; since 3.2 beta)
|
||||
|
||||
** lobject.c / lbuiltin.c
|
||||
Wed Sep 8 17:41:54 EST 1999
|
||||
>> tonumber'e1' and tonumber(' ', x), for x!=10, gave 0 instead of nil.
|
||||
(since 3.1)
|
||||
|
||||
** lstrlib.c
|
||||
Thu Nov 11 14:36:30 EDT 1999
|
||||
>> `strfind' does not handle \0 in plain search.
|
||||
(by Jon Kleiser; since 3.1)
|
||||
|
||||
** lparser.c
|
||||
Wed Dec 29 16:05:43 EDT 1999
|
||||
>> return gives wrong line in debug information
|
||||
(by lhf; since 3.2 [at least])
|
||||
|
||||
** ldo.c
|
||||
Thu Dec 30 16:39:33 EDT 1999
|
||||
>> cannot reopen stdin (for binary mode)
|
||||
(by lhf & roberto; since 3.1)
|
||||
|
||||
** lapi.c
|
||||
Thu Mar 2 09:41:53 EST 2000
|
||||
>> lua_settable should check stack space (it could call a T.M.)
|
||||
(by lhf & celes; since 3.2; it was already fixed by fixed stack)
|
||||
|
||||
** lparser.c
|
||||
Mon Apr 3 09:59:06 EST 2000
|
||||
>> '%' should be in expfollow
|
||||
(by Edgar Toernig; since 3.1; it was already fixed)
|
||||
|
||||
** lbuiltin.c
|
||||
Mon Apr 3 10:05:05 EST 2000
|
||||
>> tostring() without arguments gives seg. fault.
|
||||
(by Edgar Toernig; since 3.0)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0 alpha
|
||||
|
||||
Tested with full test suites (as locked in Mon Apr 24 14:23:11 EST 2000)
|
||||
in the following platforms:
|
||||
* Linux - gcc, g++
|
||||
* AIX - gcc
|
||||
* Solaris - gcc, cc
|
||||
* IRIX - cc, cc-purify
|
||||
* Windows - Visual C++ (.c e .cpp, warning level=4)
|
||||
|
||||
|
||||
** lstrlib.c
|
||||
Tue May 2 15:27:58 EST 2000
|
||||
>> `strfind' gets wrong subject length when there is an offset
|
||||
(by Jon Kleiser; since 4.0a)
|
||||
|
||||
** lparser.c
|
||||
Fri May 12 15:11:12 EST 2000
|
||||
>> first element in a list constructor is not adjusted to one value
|
||||
>> (e.g. «a = {gsub('a','a','')}»)
|
||||
(by Tomas; since 4.0a)
|
||||
|
||||
** lparser.c
|
||||
Wed May 24 14:50:16 EST 2000
|
||||
>> record-constructor starting with an upvalue name gets an error
|
||||
>> (e.g. «local a; function f() x = {a=1} end»)
|
||||
(by Edgar Toernig; since 3.1)
|
||||
|
||||
** lparser.c
|
||||
Tue Aug 29 15:56:05 EST 2000
|
||||
>> error message for `for' uses `while'
|
||||
(since 4.0a; already corrected)
|
||||
|
||||
** lgc.c
|
||||
Tue Aug 29 15:57:41 EST 2000
|
||||
>> gc tag method for nil could call line hook
|
||||
(by ry; since ?)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0 Beta
|
||||
|
||||
** liolib.c
|
||||
Fri Sep 22 15:12:37 EST 2000
|
||||
>> `read("*w")' should return nil at EOF
|
||||
(by roberto; since 4.0b)
|
||||
|
||||
** lvm.c
|
||||
Mon Sep 25 11:47:48 EST 2000
|
||||
>> lua_gettable does not get key from stack top
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lgc.c
|
||||
Mon Sep 25 11:50:48 EST 2000
|
||||
>> GC may crash when checking locked C closures
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lapi.c
|
||||
Wed Sep 27 09:50:19 EST 2000
|
||||
>> lua_tag should return LUA_NOTAG for non-valid indices
|
||||
(by Paul Hankin; since 4.0b)
|
||||
|
||||
** llex.h / llex.c / lparser.c
|
||||
Wed Sep 27 13:39:45 EST 2000
|
||||
>> parser overwrites semantic information when looking ahead
|
||||
>> (e.g. «a = {print'foo'}»)
|
||||
(by Edgar Toernig; since 4.0b, deriving from previous bug)
|
||||
|
||||
** liolib.c
|
||||
Thu Oct 26 10:50:46 EDT 2000
|
||||
>> in function `read_file', realloc() doesn't free the buffer if it can't
|
||||
>> allocate new memory
|
||||
(by Mauro Vezzosi; since 4.0b)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0
|
||||
|
||||
** lparser.c
|
||||
Wed Nov 29 09:51:44 EDT 2000
|
||||
>> parser does not accept a `;' after a `return'
|
||||
(by lhf; since 4.0b)
|
||||
|
||||
** liolib.c
|
||||
Fri Dec 22 15:30:42 EDT 2000
|
||||
>> when `read' fails it must return nil (and not no value)
|
||||
(by cassino; since at least 3.1)
|
||||
|
||||
** lstring.c/lapi.c
|
||||
Thu Feb 1 11:55:45 EDT 2001
|
||||
>> lua_pushuserdata(L, NULL) is buggy
|
||||
(by Edgar Toernig; since 4.0)
|
||||
|
||||
** ldo.c
|
||||
Fri Feb 2 14:06:40 EDT 2001
|
||||
>> «while 1 dostring[[print('hello\n')]] end» never reclaims memory
|
||||
(by Andrew Paton; since 4.0b)
|
||||
|
||||
** lbaselib.c
|
||||
Tue Feb 6 11:57:13 EDT 2001
|
||||
>> ESC (which starts precompiled code) in C is \33, not \27
|
||||
(by Edgar Toernig and lhf; since 4.0b)
|
||||
|
||||
** lparser.c
|
||||
Tue Jul 10 16:59:18 EST 2001
|
||||
>> error message for `%a' gave wrong line number
|
||||
(by Leonardo Constantino; since 4.0)
|
||||
|
||||
** lbaselib.c
|
||||
Fri Dec 21 15:21:05 EDT 2001
|
||||
>> seg. fault when rawget/rawset get extra arguments
|
||||
(by Eric Mauger; since 4.0b)
|
||||
|
||||
** lvm.c
|
||||
Wed Jun 19 13:28:20 EST 2002
|
||||
>> line hook gets wrong `ar'
|
||||
(by Daniel C. Sinclair; since 4.0.b)
|
||||
|
||||
** ldo.c
|
||||
Wed Jun 19 13:31:49 EST 2002
|
||||
>> `protectedparser' may run GC, and then collect `filename'
|
||||
>> (in function `parse_file')
|
||||
(by Alex Bilyk; since 4.0)
|
||||
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- 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)
|
||||
|
||||
|
||||
|
||||
--]=]
|
||||
-----------------------------------------------------------------
|
||||
-- Lua 5.0 (final)
|
||||
|
||||
Bug{
|
||||
what = [[lua_closethread exists only in the manual]],
|
||||
report = [[by Nguyen Binh, 28/04/2003]],
|
||||
patch = [[no patch; the manual is wrong]],
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[attempt to resume a running coroutine crashes Lua]],
|
||||
example = [[
|
||||
function co_func (current_co)
|
||||
coroutine.resume(co)
|
||||
end
|
||||
co = coroutine.create(co_func)
|
||||
coroutine.resume(co)
|
||||
coroutine.resume(co) --> seg. fault
|
||||
]],
|
||||
report = [[by Alex Bilyk, 09/05/2003]],
|
||||
patch = [[
|
||||
* ldo.c:
|
||||
325,326c325
|
||||
< if (nargs >= L->top - L->base)
|
||||
< luaG_runerror(L, "cannot resume dead coroutine");
|
||||
---
|
||||
> lua_assert(nargs < L->top - L->base);
|
||||
329c328,329
|
||||
< else if (ci->state & CI_YIELD) { /* inside a yield? */
|
||||
---
|
||||
> else { /* inside a yield */
|
||||
> lua_assert(ci->state & CI_YIELD);
|
||||
344,345d343
|
||||
< else
|
||||
< luaG_runerror(L, "cannot resume non-suspended coroutine");
|
||||
351a350,358
|
||||
> static int resume_error (lua_State *L, const char *msg) {
|
||||
> L->top = L->ci->base;
|
||||
> setsvalue2s(L->top, luaS_new(L, msg));
|
||||
> incr_top(L);
|
||||
> lua_unlock(L);
|
||||
> return LUA_ERRRUN;
|
||||
> }
|
||||
>
|
||||
>
|
||||
355a363,368
|
||||
> if (L->ci == L->base_ci) {
|
||||
> if (nargs >= L->top - L->base)
|
||||
> return resume_error(L, "cannot resume dead coroutine");
|
||||
> }
|
||||
> else if (!(L->ci->state & CI_YIELD)) /* not inside a yield? */
|
||||
> return resume_error(L, "cannot resume non-suspended coroutine");
|
||||
]],
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[file:close cannot be called without a file. (results in seg fault)]],
|
||||
example = [[
|
||||
> io.stdin.close() -- correct call shold be io.stdin:close()
|
||||
]],
|
||||
report = [[by Tuomo Valkonen, 27/05/2003]],
|
||||
patch = [[
|
||||
* liolib.c:
|
||||
161c161
|
||||
< if (lua_isnone(L, 1)) {
|
||||
---
|
||||
> if (lua_isnone(L, 1) && lua_type(L, lua_upvalueindex(1)) == LUA_TTABLE) {
|
||||
]], --}}
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[C functions also may have stacks larger than current top]],
|
||||
example = [[
|
||||
Must recompile lua with a change in lua.c and with lua_assert defined:
|
||||
* lua.c:
|
||||
381a382
|
||||
> lua_checkstack(l, 1000);
|
||||
]],
|
||||
report = [[Alex Bilyk, 09/06/2003]],
|
||||
patch = [[
|
||||
* lgc.c:
|
||||
247c247
|
||||
< if (!(ci->state & CI_C) && lim < ci->top)
|
||||
---
|
||||
> if (lim < ci->top)
|
||||
]],
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[`pc' address is invalidated when a coroutine is suspended]],
|
||||
example = [[
|
||||
function g(x)
|
||||
coroutine.yield(x)
|
||||
end
|
||||
|
||||
function f (i)
|
||||
debug.sethook(print, "l")
|
||||
for j=1,1000 do
|
||||
g(i+j)
|
||||
end
|
||||
end
|
||||
|
||||
co = coroutine.wrap(f)
|
||||
co(10)
|
||||
pcall(co)
|
||||
pcall(co)
|
||||
]],
|
||||
report = [[Nick Trout, 07/07/2003]],
|
||||
patch = [[
|
||||
* lvm.c:
|
||||
402,403c402,403
|
||||
< L->ci->u.l.pc = &pc;
|
||||
< if (L->hookmask & LUA_MASKCALL)
|
||||
---
|
||||
> if (L->hookmask & LUA_MASKCALL) {
|
||||
> L->ci->u.l.pc = &pc;
|
||||
404a405
|
||||
> }
|
||||
405a407
|
||||
> L->ci->u.l.pc = &pc;
|
||||
676,678c678
|
||||
< lua_assert(ci->u.l.pc == &pc &&
|
||||
< ttisfunction(ci->base - 1) &&
|
||||
< (ci->state & CI_SAVEDPC));
|
||||
---
|
||||
> lua_assert(ttisfunction(ci->base - 1) && (ci->state & CI_SAVEDPC));
|
||||
]]
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[userdata to be collected still counts into new GC threshold,
|
||||
increasing memory consumption]],
|
||||
report = [[Roberto, 25/07/2003]],
|
||||
example = [[
|
||||
a = newproxy(true)
|
||||
getmetatable(a).__gc = function () end
|
||||
for i=1,10000000 do
|
||||
newproxy(a)
|
||||
if math.mod(i, 10000) == 0 then print(gcinfo()) end
|
||||
end
|
||||
]],
|
||||
patch = [[
|
||||
* lgc.h:
|
||||
18c18
|
||||
< void luaC_separateudata (lua_State *L);
|
||||
---
|
||||
> size_t luaC_separateudata (lua_State *L);
|
||||
|
||||
* lgc.c:
|
||||
113c113,114
|
||||
< void luaC_separateudata (lua_State *L) {
|
||||
---
|
||||
> size_t luaC_separateudata (lua_State *L) {
|
||||
> size_t deadmem = 0;
|
||||
127a129
|
||||
> deadmem += sizeudata(gcotou(curr)->uv.len);
|
||||
136a139
|
||||
> return deadmem;
|
||||
390c393
|
||||
< static void checkSizes (lua_State *L) {
|
||||
---
|
||||
> static void checkSizes (lua_State *L, size_t deadmem) {
|
||||
400c403
|
||||
< G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
|
||||
---
|
||||
> G(L)->GCthreshold = 2*G(L)->nblocks - deadmem; /* new threshold */
|
||||
454c457,458
|
||||
< static void mark (lua_State *L) {
|
||||
---
|
||||
> static size_t mark (lua_State *L) {
|
||||
> size_t deadmem;
|
||||
467c471
|
||||
< luaC_separateudata(L); /* separate userdata to be preserved */
|
||||
---
|
||||
> deadmem = luaC_separateudata(L); /* separate userdata to be preserved */
|
||||
475a480
|
||||
> return deadmem;
|
||||
480c485
|
||||
< mark(L);
|
||||
---
|
||||
> size_t deadmem = mark(L);
|
||||
482c487
|
||||
< checkSizes(L);
|
||||
---
|
||||
> checkSizes(L, deadmem);
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what=[[IBM AS400 (OS400) has sizeof(void *)==16, and a `%p' may generate
|
||||
up to 60 characters in a `printf'. That causes a buffer overflow in
|
||||
`tostring'.]],
|
||||
|
||||
report = [[David Burgess, 25/08/2003]],
|
||||
|
||||
example = [[print{}; (in an AS400 machine)]],
|
||||
|
||||
patch = [[
|
||||
* liolib.c:
|
||||
178c178
|
||||
< char buff[32];
|
||||
---
|
||||
> char buff[128];
|
||||
|
||||
* lbaselib.c:
|
||||
327c327
|
||||
< char buff[64];
|
||||
---
|
||||
> char buff[128];
|
||||
]]
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[syntax `local function' does not increment stack size]],
|
||||
|
||||
report = [[Rici Lake, 26/09/2003]],
|
||||
|
||||
example = [[
|
||||
-- must run this with precompiled code
|
||||
local a,b,c
|
||||
local function d () end
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lparser.c:
|
||||
1143a1144
|
||||
> FuncState *fs = ls->fs;
|
||||
1145c1146,1147
|
||||
< init_exp(&v, VLOCAL, ls->fs->freereg++);
|
||||
---
|
||||
> init_exp(&v, VLOCAL, fs->freereg);
|
||||
> luaK_reserveregs(fs, 1);
|
||||
1148c1150,1152
|
||||
< luaK_storevar(ls->fs, &v, &b);
|
||||
---
|
||||
> luaK_storevar(fs, &v, &b);
|
||||
> /* debug information will only see the variable after this point! */
|
||||
> getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
|
||||
what = [[count hook may be called without being set]],
|
||||
|
||||
report = [[Andreas Stenius, 06/10/2003]],
|
||||
|
||||
example = [[
|
||||
set your hooks with
|
||||
|
||||
lua_sethook(L, my_hook, LUA_MASKLINE | LUA_MASKRET, 1);
|
||||
|
||||
(It is weird to use a count > 0 without setting the count hook,
|
||||
but it is not wrong.)
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lvm.c:
|
||||
69c69
|
||||
< if (mask > LUA_MASKLINE) { /* instruction-hook set? */
|
||||
---
|
||||
> if (mask & LUA_MASKCOUNT) { /* instruction-hook set? */
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
|
||||
what = [[`dofile' eats one return value when called without arguments]],
|
||||
|
||||
report = [[Frederico Abraham, 15/01/2004]],
|
||||
|
||||
example = [[
|
||||
a,b = dofile() --< here you enter `return 1,2,3 <eof>'
|
||||
print(a,b) --> 2 3 (should be 1 and 2)
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lbaselib.c:
|
||||
313a314
|
||||
> int n = lua_gettop(L);
|
||||
317c318
|
||||
< return lua_gettop(L) - 1;
|
||||
---
|
||||
> return lua_gettop(L) - n;
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
-----------------------------------------------------------------
|
||||
-- Lua 5.0.2
|
||||
|
||||
Bug{
|
||||
what = [[string concatenation may cause arithmetic overflow, leading
|
||||
to a buffer overflow]],
|
||||
|
||||
report = [[Rici Lake, 20/05/2004]],
|
||||
|
||||
example = [[
|
||||
longs = string.rep("\0", 2^25)
|
||||
function catter(i)
|
||||
return assert(loadstring(
|
||||
string.format("return function(a) return a%s end",
|
||||
string.rep("..a", i-1))))()
|
||||
end
|
||||
rep129 = catter(129)
|
||||
rep129(longs)
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lvm.c:
|
||||
@@ -321,15 +321,15 @@
|
||||
luaG_concaterror(L, top-2, top-1);
|
||||
} else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */
|
||||
/* at least two string values; get as many as possible */
|
||||
- lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) +
|
||||
- cast(lu_mem, tsvalue(top-2)->tsv.len);
|
||||
+ size_t tl = tsvalue(top-1)->tsv.len;
|
||||
char *buffer;
|
||||
int i;
|
||||
- while (n < total && tostring(L, top-n-1)) { /* collect total length */
|
||||
- tl += tsvalue(top-n-1)->tsv.len;
|
||||
- n++;
|
||||
+ /* collect total length */
|
||||
+ for (n = 1; n < total && tostring(L, top-n-1); n++) {
|
||||
+ size_t l = tsvalue(top-n-1)->tsv.len;
|
||||
+ if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
|
||||
+ tl += l;
|
||||
}
|
||||
- if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow");
|
||||
buffer = luaZ_openspace(L, &G(L)->buff, tl);
|
||||
tl = 0;
|
||||
for (i=n; i>0; i--) { /* concat all strings */
|
||||
]]
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[lua_getupvalue and setupvalue do not check for index too small]],
|
||||
|
||||
report = [[Mike Pall, ?/2004]],
|
||||
|
||||
example = [[debug.getupvalue(function() end, 0)]],
|
||||
|
||||
patch = [[
|
||||
* lapi.c
|
||||
941c941
|
||||
< if (n > f->c.nupvalues) return NULL;
|
||||
---
|
||||
> if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
|
||||
947c947
|
||||
< if (n > p->sizeupvalues) return NULL;
|
||||
---
|
||||
> if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
|
||||
]]
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[values holded in open upvalues of suspended threads may be
|
||||
incorrectly collected]],
|
||||
|
||||
report = [[Spencer Schumann, 31/12/2004]],
|
||||
|
||||
example = [[
|
||||
local thread_id = 0
|
||||
local threads = {}
|
||||
|
||||
function fn(thread)
|
||||
thread_id = thread_id + 1
|
||||
threads[thread_id] = function()
|
||||
thread = nil
|
||||
end
|
||||
coroutine.yield()
|
||||
end
|
||||
|
||||
while true do
|
||||
local thread = coroutine.create(fn)
|
||||
coroutine.resume(thread, thread)
|
||||
end
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lgc.c:
|
||||
221,224c221,222
|
||||
< if (!u->marked) {
|
||||
< markobject(st, &u->value);
|
||||
< u->marked = 1;
|
||||
< }
|
||||
---
|
||||
> markobject(st, u->v);
|
||||
> u->marked = 1;
|
||||
]],
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[rawset/rawget do not ignore extra arguments]],
|
||||
|
||||
report = [[Romulo Bahiense, 11/03/2005]],
|
||||
|
||||
example = [[
|
||||
a = {}
|
||||
rawset(a, 1, 2, 3)
|
||||
print(a[1], a[2]) -- should be 2 and nil
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lbaselib.c:
|
||||
175a176
|
||||
> lua_settop(L, 2);
|
||||
183a185
|
||||
> lua_settop(L, 3);
|
||||
]],
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[weak tables that survive one collection are never collected]],
|
||||
|
||||
report = [[Chromix, 02/01/2006]],
|
||||
|
||||
example = [[
|
||||
a = {}
|
||||
print(gcinfo())
|
||||
for i = 1, 10000 do
|
||||
a[i] = setmetatable({}, {__mode = "v"})
|
||||
end
|
||||
collectgarbage()
|
||||
a = nil
|
||||
collectgarbage()
|
||||
print(gcinfo())
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lgc.c
|
||||
@@ -366,7 +366,7 @@
|
||||
GCObject *curr;
|
||||
int count = 0; /* number of collected items */
|
||||
while ((curr = *p) != NULL) {
|
||||
- if (curr->gch.marked > limit) {
|
||||
+ if ((curr->gch.marked & ~(KEYWEAK | VALUEWEAK)) > limit) {
|
||||
unmark(curr);
|
||||
p = &curr->gch.next;
|
||||
}
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
-----------------------------------------------------------------
|
||||
-- Lua 5.1
|
||||
|
||||
Bug{
|
||||
what = [[In 16-bit machines, expressions and/or with numeric constants as the
|
||||
right operand may result in weird values]],
|
||||
|
||||
report = [[Andreas Stenius/Kein-Hong Man, 15/03/2006]],
|
||||
|
||||
example = [[
|
||||
print(false or 0) -- on 16-bit machines
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lcode.c:
|
||||
@@ -731,17 +731,15 @@
|
||||
case OPR_AND: {
|
||||
lua_assert(e1->t == NO_JUMP); /* list must be closed */
|
||||
luaK_dischargevars(fs, e2);
|
||||
- luaK_concat(fs, &e1->f, e2->f);
|
||||
- e1->k = e2->k; e1->u.s.info = e2->u.s.info;
|
||||
- e1->u.s.aux = e2->u.s.aux; e1->t = e2->t;
|
||||
+ luaK_concat(fs, &e2->f, e1->f);
|
||||
+ *e1 = *e2;
|
||||
break;
|
||||
}
|
||||
case OPR_OR: {
|
||||
lua_assert(e1->f == NO_JUMP); /* list must be closed */
|
||||
luaK_dischargevars(fs, e2);
|
||||
- luaK_concat(fs, &e1->t, e2->t);
|
||||
- e1->k = e2->k; e1->u.s.info = e2->u.s.info;
|
||||
- e1->u.s.aux = e2->u.s.aux; e1->f = e2->f;
|
||||
+ luaK_concat(fs, &e2->t, e1->t);
|
||||
+ *e1 = *e2;
|
||||
break;
|
||||
}
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[luaL_checkudata may produce wrong error message]],
|
||||
|
||||
report = [[Greg Falcon, 21/03/2006]],
|
||||
|
||||
example = [[
|
||||
getmetatable(io.stdin).__gc()
|
||||
--> bad argument #1 to '__gc' (FILE* expected, got table)
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lauxlib.c:
|
||||
@@ -123,11 +123,17 @@
|
||||
|
||||
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
|
||||
void *p = lua_touserdata(L, ud);
|
||||
- lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
|
||||
- if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2))
|
||||
- luaL_typerror(L, ud, tname);
|
||||
- lua_pop(L, 2); /* remove both metatables */
|
||||
- return p;
|
||||
+ if (p != NULL) { /* value is a userdata? */
|
||||
+ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
|
||||
+ lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
|
||||
+ if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
|
||||
+ lua_pop(L, 2); /* remove both metatables */
|
||||
+ return p;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ luaL_typerror(L, ud, tname); /* else error */
|
||||
+ return NULL; /* to avoid warnings */
|
||||
}
|
||||
]]
|
||||
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[
|
||||
In Windows,
|
||||
when Lua is used in an application that also uses DirectX,
|
||||
it may present an erractic behavior.
|
||||
THIS IS NOT A LUA BUG!
|
||||
The problem is that DirectX violates an ABI that Lua depends on.]],
|
||||
|
||||
patch = [[
|
||||
The simplest solution is to use DirectX with
|
||||
the D3DCREATE_FPU_PRESERVE flag.
|
||||
|
||||
Otherwise, you can change the definition of lua_number2int,
|
||||
in luaconf.h, to this one:
|
||||
#define lua_number2int(i,d) __asm fld d __asm fistp i
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[option '%q' in string.format does not handle '\r' correctly.]],
|
||||
|
||||
example = [[
|
||||
local s = "a string with \r and \n and \r\n and \n\r"
|
||||
local c = string.format("return %q", s)
|
||||
assert(assert(loadstring(c))() == s)
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* lstrlib.c:
|
||||
@@ -703,6 +703,10 @@
|
||||
luaL_addchar(b, *s);
|
||||
break;
|
||||
}
|
||||
+ case '\r': {
|
||||
+ luaL_addlstring(b, "\\r", 2);
|
||||
+ break;
|
||||
+ }
|
||||
case '\0': {
|
||||
luaL_addlstring(b, "\\000", 4);
|
||||
break;
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
what = [[lua_dostring/lua_dofile should return any values returned
|
||||
by the chunk]],
|
||||
|
||||
patch = [[
|
||||
* lauxlib.h:
|
||||
@@ -108,9 +108,11 @@
|
||||
|
||||
#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
|
||||
|
||||
-#define luaL_dofile(L, fn) (luaL_loadfile(L, fn) || lua_pcall(L, 0, 0, 0))
|
||||
+#define luaL_dofile(L, fn) \
|
||||
+ (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
|
||||
|
||||
-#define luaL_dostring(L, s) (luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0))+#define luaL_dostring(L, s) \
|
||||
+ (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
|
||||
|
||||
#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
|
||||
Bug{
|
||||
|
||||
what = [[garbage collector does not compensate enough for finalizers]],
|
||||
|
||||
patch = [[
|
||||
lgc.c:
|
||||
@@ -322,4 +322,6 @@
|
||||
|
||||
-static void propagateall (global_State *g) {
|
||||
- while (g->gray) propagatemark(g);
|
||||
+static size_t propagateall (global_State *g) {
|
||||
+ size_t m = 0;
|
||||
+ while (g->gray) m += propagatemark(g);
|
||||
+ return m;
|
||||
}
|
||||
@@ -542,3 +544,3 @@
|
||||
marktmu(g); /* mark `preserved' userdata */
|
||||
- propagateall(g); /* remark, to propagate `preserveness' */
|
||||
+ udsize += propagateall(g); /* remark, to propagate `preserveness' */
|
||||
cleartable(g->weak); /* remove collected objects from weak tables */
|
||||
@@ -592,2 +594,4 @@
|
||||
GCTM(L);
|
||||
+ if (g->estimate > GCFINALIZECOST)
|
||||
+ g->estimate -= GCFINALIZECOST;
|
||||
]]
|
||||
}
|
||||
|
||||
|
||||
But{
|
||||
|
||||
what = [[debug hooks may get wrong when mixed with coroutines]],
|
||||
|
||||
report = [[by Ivko Stanilov, 03/06/2006]],
|
||||
|
||||
example = [[
|
||||
co = coroutine.create(function (a,b)
|
||||
coroutine.yield(a, b)
|
||||
return b, "end"
|
||||
end)
|
||||
|
||||
debug.sethook(co, function() end, "lcr")
|
||||
coroutine.resume(co, 100, 2000)
|
||||
coroutine.resume(co, 100, 2000)
|
||||
]],
|
||||
|
||||
patch = [[
|
||||
* ldo.c:
|
||||
@@ -389,6 +389,7 @@
|
||||
return;
|
||||
}
|
||||
else { /* resuming from previous yield */
|
||||
+ L->status = 0;
|
||||
if (!f_isLua(ci)) { /* `common' yield? */
|
||||
/* finish interrupted execution of `OP_CALL' */
|
||||
lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
|
||||
@@ -399,7 +400,6 @@
|
||||
else /* yielded inside a hook: just continue its execution */
|
||||
L->base = L->ci->base;
|
||||
}
|
||||
- L->status = 0;
|
||||
luaV_execute(L, cast_int(L->ci - L->base_ci));
|
||||
}
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
10
lapi.h
10
lapi.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.h,v 1.3 1999/02/22 19:13:12 roberto Exp roberto $
|
||||
** $Id: lapi.h,v 2.1 2003/12/10 12:13:36 roberto Exp roberto $
|
||||
** Auxiliary functions from Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,15 +8,9 @@
|
||||
#define lapi_h
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
TObject *luaA_Address (lua_Object o);
|
||||
void luaA_pushobject (TObject *o);
|
||||
void luaA_packresults (void);
|
||||
int luaA_passresults (void);
|
||||
TaggedString *luaA_nextvar (TaggedString *g);
|
||||
int luaA_next (Hash *t, int i);
|
||||
LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);
|
||||
|
||||
#endif
|
||||
|
||||
708
lauxlib.c
708
lauxlib.c
@@ -1,133 +1,653 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.16 1999/03/10 14:19:41 roberto Exp roberto $
|
||||
** $Id: lauxlib.c,v 1.158 2006/01/16 12:42:21 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 <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Please Notice: This file uses only the official API of Lua
|
||||
|
||||
/* This file uses only the official API of Lua.
|
||||
** Any function declared here could be written as an application function.
|
||||
** With care, these functions can be used by other libraries.
|
||||
*/
|
||||
|
||||
#include "lauxlib.h"
|
||||
#define lauxlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
|
||||
|
||||
#define FREELIST_REF 0 /* free list of references */
|
||||
|
||||
int luaL_findstring (char *name, char *list[]) {
|
||||
int i;
|
||||
for (i=0; list[i]; i++)
|
||||
if (strcmp(list[i], name) == 0)
|
||||
return i;
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
void luaL_argerror (int numarg, char *extramsg) {
|
||||
lua_Function f = lua_stackedfunction(0);
|
||||
char *funcname;
|
||||
lua_getobjname(f, &funcname);
|
||||
numarg -= lua_nups(f);
|
||||
if (funcname == NULL)
|
||||
funcname = "?";
|
||||
if (extramsg == NULL)
|
||||
luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
|
||||
else
|
||||
luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
|
||||
numarg, funcname, extramsg);
|
||||
}
|
||||
/* convert a stack index to positive */
|
||||
#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
|
||||
lua_gettop(L) + (i) + 1)
|
||||
|
||||
char *luaL_check_lstr (int numArg, long *len)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(lua_isstring(o), numArg, "string expected");
|
||||
if (len) *len = lua_strlen(o);
|
||||
return lua_getstring(o);
|
||||
}
|
||||
|
||||
char *luaL_opt_lstr (int numArg, char *def, long *len)
|
||||
{
|
||||
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||
luaL_check_lstr(numArg, len);
|
||||
}
|
||||
/*
|
||||
** {======================================================
|
||||
** Error-report functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
double luaL_check_number (int numArg)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(lua_isnumber(o), numArg, "number expected");
|
||||
return lua_getnumber(o);
|
||||
|
||||
LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
|
||||
lua_Debug ar;
|
||||
if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
|
||||
return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
|
||||
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 " LUA_QS " on bad self (%s)",
|
||||
ar.name, extramsg);
|
||||
}
|
||||
if (ar.name == NULL)
|
||||
ar.name = "?";
|
||||
return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
|
||||
narg, ar.name, extramsg);
|
||||
}
|
||||
|
||||
|
||||
double luaL_opt_number (int numArg, double def)
|
||||
{
|
||||
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||
luaL_check_number(numArg);
|
||||
}
|
||||
|
||||
|
||||
lua_Object luaL_tablearg (int arg)
|
||||
{
|
||||
lua_Object o = lua_getparam(arg);
|
||||
luaL_arg_check(lua_istable(o), arg, "table expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
lua_Object luaL_functionarg (int arg)
|
||||
{
|
||||
lua_Object o = lua_getparam(arg);
|
||||
luaL_arg_check(lua_isfunction(o), arg, "function expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
lua_Object luaL_nonnullarg (int numArg)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
void luaL_openlib (struct luaL_reg *l, int n)
|
||||
{
|
||||
int i;
|
||||
lua_open(); /* make sure lua is already open */
|
||||
for (i=0; i<n; i++)
|
||||
lua_register(l[i].name, l[i].func);
|
||||
LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
|
||||
const char *msg = lua_pushfstring(L, "%s expected, got %s",
|
||||
tname, luaL_typename(L, narg));
|
||||
return luaL_argerror(L, narg, msg);
|
||||
}
|
||||
|
||||
|
||||
void luaL_verror (char *fmt, ...)
|
||||
{
|
||||
char buff[500];
|
||||
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, "Sl", &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);
|
||||
vsprintf(buff, fmt, argp);
|
||||
luaL_where(L, 1);
|
||||
lua_pushvfstring(L, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_error(buff);
|
||||
lua_concat(L, 2);
|
||||
return lua_error(L);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
|
||||
const char *const lst[]) {
|
||||
const char *name = (def) ? luaL_optstring(L, narg, def) :
|
||||
luaL_checkstring(L, narg);
|
||||
int i;
|
||||
for (i=0; lst[i]; i++)
|
||||
if (strcmp(lst[i], name) == 0)
|
||||
return i;
|
||||
return luaL_argerror(L, narg,
|
||||
lua_pushfstring(L, "invalid option " LUA_QS, name));
|
||||
}
|
||||
|
||||
|
||||
void luaL_chunkid (char *out, char *source, int len) {
|
||||
len -= 13; /* 13 = strlen("string ''...\0") */
|
||||
if (*source == '@')
|
||||
sprintf(out, "file `%.*s'", len, source+1);
|
||||
else if (*source == '(')
|
||||
strcpy(out, "(C code)");
|
||||
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, tname); /* 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_pushvalue(L, -1);
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
|
||||
void *p = lua_touserdata(L, ud);
|
||||
if (p != NULL) { /* value is a userdata? */
|
||||
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
|
||||
if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
|
||||
lua_pop(L, 2); /* remove both metatables */
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
luaL_typerror(L, ud, tname); /* else error */
|
||||
return NULL; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
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_tolstring(L, narg, len);
|
||||
if (!s) tag_error(L, narg, LUA_TSTRING);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
|
||||
const char *def, size_t *len) {
|
||||
if (lua_isnoneornil(L, narg)) {
|
||||
if (len)
|
||||
*len = (def ? strlen(def) : 0);
|
||||
return def;
|
||||
}
|
||||
else return luaL_checklstring(L, narg, len);
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
return luaL_opt(L, luaL_checknumber, narg, def);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
|
||||
lua_Integer d = lua_tointeger(L, narg);
|
||||
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
|
||||
tag_error(L, narg, LUA_TNUMBER);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
|
||||
lua_Integer def) {
|
||||
return luaL_opt(L, luaL_checkinteger, narg, def);
|
||||
}
|
||||
|
||||
|
||||
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 {
|
||||
char *b = strchr(source , '\n'); /* stop string at first new line */
|
||||
int lim = (b && (b-source)<len) ? b-source : len;
|
||||
sprintf(out, "string `%.*s'", lim, source);
|
||||
strcpy(out+lim+(13-5), "...'"); /* 5 = strlen("...'\0") */
|
||||
lua_remove(L, -2); /* remove only metatable */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaL_filesource (char *out, char *filename, int len) {
|
||||
if (filename == NULL) filename = "(stdin)";
|
||||
sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
|
||||
const luaL_Reg *l) {
|
||||
luaI_openlib(L, libname, l, 0);
|
||||
}
|
||||
|
||||
|
||||
static int libsize (const luaL_Reg *l) {
|
||||
int size = 0;
|
||||
for (; l->name; l++) size++;
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
|
||||
const luaL_Reg *l, int nup) {
|
||||
if (libname) {
|
||||
int size = libsize(l);
|
||||
/* check whether lib already exists */
|
||||
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", size);
|
||||
lua_getfield(L, -1, libname); /* get _LOADED[libname] */
|
||||
if (!lua_istable(L, -1)) { /* not found? */
|
||||
lua_pop(L, 1); /* remove previous result */
|
||||
/* try global variable (and create one if it does not exist) */
|
||||
if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
|
||||
luaL_error(L, "name conflict for module " LUA_QS, libname);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
|
||||
}
|
||||
lua_remove(L, -2); /* remove _LOADED table */
|
||||
lua_insert(L, -(nup+1)); /* move library table to below upvalues */
|
||||
}
|
||||
for (; l->name; l++) {
|
||||
int i;
|
||||
for (i=0; i<nup; i++) /* copy upvalues to the top */
|
||||
lua_pushvalue(L, -nup);
|
||||
lua_pushcclosure(L, l->func, nup);
|
||||
lua_setfield(L, -(nup+2), l->name);
|
||||
}
|
||||
lua_pop(L, nup); /* remove upvalues */
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** getn-setn: size for arrays
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#if defined(LUA_COMPAT_GETN)
|
||||
|
||||
static int checkint (lua_State *L, int topop) {
|
||||
int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
|
||||
lua_pop(L, topop);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
static void getsizes (lua_State *L) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
|
||||
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, "kv");
|
||||
lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API 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_pushinteger(L, n);
|
||||
lua_rawset(L, t);
|
||||
}
|
||||
else { /* use `sizes' */
|
||||
getsizes(L);
|
||||
lua_pushvalue(L, t);
|
||||
lua_pushinteger(L, n);
|
||||
lua_rawset(L, -3); /* sizes[t] = n */
|
||||
lua_pop(L, 1); /* remove `sizes' */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API 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;
|
||||
return (int)lua_objlen(L, t);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
|
||||
const char *r) {
|
||||
const char *wild;
|
||||
size_t l = strlen(p);
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
while ((wild = strstr(s, p)) != NULL) {
|
||||
luaL_addlstring(&b, s, wild - s); /* push prefix */
|
||||
luaL_addstring(&b, r); /* push replacement in place of pattern */
|
||||
s = wild + l; /* continue after `p' */
|
||||
}
|
||||
luaL_addstring(&b, s); /* push last suffix */
|
||||
luaL_pushresult(&b);
|
||||
return lua_tostring(L, -1);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
|
||||
const char *fname, int szhint) {
|
||||
const char *e;
|
||||
lua_pushvalue(L, idx);
|
||||
do {
|
||||
e = strchr(fname, '.');
|
||||
if (e == NULL) e = fname + strlen(fname);
|
||||
lua_pushlstring(L, fname, e - fname);
|
||||
lua_rawget(L, -2);
|
||||
if (lua_isnil(L, -1)) { /* no such field? */
|
||||
lua_pop(L, 1); /* remove this nil */
|
||||
lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
|
||||
lua_pushlstring(L, fname, e - fname);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, -4); /* set new table into field */
|
||||
}
|
||||
else if (!lua_istable(L, -1)) { /* field has a non-table value? */
|
||||
lua_pop(L, 2); /* remove table and value */
|
||||
return fname; /* return problematic part of the name */
|
||||
}
|
||||
lua_remove(L, -2); /* remove previous table */
|
||||
fname = e + 1;
|
||||
} while (*e == '.');
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** 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_addchar(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;
|
||||
const char *s = lua_tolstring(L, -1, &vl);
|
||||
if (vl <= bufffree(B)) { /* fit into buffer? */
|
||||
memcpy(B->p, s, vl); /* put it there */
|
||||
B->p += vl;
|
||||
lua_pop(L, 1); /* remove from stack */
|
||||
}
|
||||
else {
|
||||
if (emptybuffer(B))
|
||||
lua_insert(L, -2); /* put buffer before new value */
|
||||
B->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_tointeger(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 = (int)lua_objlen(L, t);
|
||||
ref++; /* create new reference */
|
||||
}
|
||||
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_pushinteger(L, ref);
|
||||
lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Load functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
typedef struct LoadF {
|
||||
int extraline;
|
||||
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 (lf->extraline) {
|
||||
lf->extraline = 0;
|
||||
*size = 1;
|
||||
return "\n";
|
||||
}
|
||||
if (feof(lf->f)) return NULL;
|
||||
*size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
|
||||
return (*size > 0) ? lf->buff : NULL;
|
||||
}
|
||||
|
||||
|
||||
static int errfile (lua_State *L, const char *what, int fnameindex) {
|
||||
const char *serr = strerror(errno);
|
||||
const char *filename = lua_tostring(L, fnameindex) + 1;
|
||||
lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
|
||||
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 */
|
||||
lf.extraline = 0;
|
||||
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, "open", fnameindex);
|
||||
}
|
||||
c = getc(lf.f);
|
||||
if (c == '#') { /* Unix exec. file? */
|
||||
lf.extraline = 1;
|
||||
while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
|
||||
if (c == '\n') c = getc(lf.f);
|
||||
}
|
||||
if (c == LUA_SIGNATURE[0] && lf.f != stdin) { /* binary file? */
|
||||
fclose(lf.f);
|
||||
lf.f = fopen(filename, "rb"); /* reopen in binary mode */
|
||||
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
|
||||
/* skip eventual `#!...' */
|
||||
while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
|
||||
lf.extraline = 0;
|
||||
}
|
||||
ungetc(c, lf.f);
|
||||
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, "read", 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);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
|
||||
return luaL_loadbuffer(L, s, strlen(s), s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
(void)ud;
|
||||
(void)osize;
|
||||
if (nsize == 0) {
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
return realloc(ptr, nsize);
|
||||
}
|
||||
|
||||
|
||||
static int panic (lua_State *L) {
|
||||
(void)L; /* to avoid warnings */
|
||||
fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
|
||||
lua_tostring(L, -1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_State *luaL_newstate (void) {
|
||||
lua_State *L = lua_newstate(l_alloc, NULL);
|
||||
if (L) lua_atpanic(L, &panic);
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
191
lauxlib.h
191
lauxlib.h
@@ -1,53 +1,174 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.11 1999/03/04 21:17:26 roberto Exp roberto $
|
||||
** $Id: lauxlib.h,v 1.87 2005/12/29 15:32:11 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef auxlib_h
|
||||
#define auxlib_h
|
||||
#ifndef lauxlib_h
|
||||
#define lauxlib_h
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
struct luaL_reg {
|
||||
char *name;
|
||||
#if defined(LUA_COMPAT_GETN)
|
||||
LUALIB_API int (luaL_getn) (lua_State *L, int t);
|
||||
LUALIB_API void (luaL_setn) (lua_State *L, int t, int n);
|
||||
#else
|
||||
#define luaL_getn(L,i) ((int)lua_objlen(L, i))
|
||||
#define luaL_setn(L,i,j) ((void)0) /* no op! */
|
||||
#endif
|
||||
|
||||
#if defined(LUA_COMPAT_OPENLIB)
|
||||
#define luaI_openlib luaL_openlib
|
||||
#endif
|
||||
|
||||
|
||||
/* extra error code for `luaL_load' */
|
||||
#define LUA_ERRFILE (LUA_ERRERR+1)
|
||||
|
||||
|
||||
typedef struct luaL_Reg {
|
||||
const char *name;
|
||||
lua_CFunction func;
|
||||
};
|
||||
} luaL_Reg;
|
||||
|
||||
|
||||
#define luaL_arg_check(cond,numarg,extramsg) if (!(cond)) \
|
||||
luaL_argerror(numarg,extramsg)
|
||||
|
||||
void luaL_openlib (struct luaL_reg *l, int n);
|
||||
void luaL_argerror (int numarg, char *extramsg);
|
||||
#define luaL_check_string(n) (luaL_check_lstr((n), NULL))
|
||||
char *luaL_check_lstr (int numArg, long *len);
|
||||
#define luaL_opt_string(n, d) (luaL_opt_lstr((n), (d), NULL))
|
||||
char *luaL_opt_lstr (int numArg, char *def, long *len);
|
||||
double luaL_check_number (int numArg);
|
||||
#define luaL_check_int(n) ((int)luaL_check_number(n))
|
||||
#define luaL_check_long(n) ((long)luaL_check_number(n))
|
||||
double luaL_opt_number (int numArg, double def);
|
||||
#define luaL_opt_int(n,d) ((int)luaL_opt_number(n,d))
|
||||
#define luaL_opt_long(n,d) ((long)luaL_opt_number(n,d))
|
||||
lua_Object luaL_functionarg (int arg);
|
||||
lua_Object luaL_tablearg (int arg);
|
||||
lua_Object luaL_nonnullarg (int numArg);
|
||||
void luaL_verror (char *fmt, ...);
|
||||
char *luaL_openspace (int size);
|
||||
void luaL_resetbuffer (void);
|
||||
void luaL_addchar (int c);
|
||||
int luaL_getsize (void);
|
||||
void luaL_addsize (int n);
|
||||
int luaL_newbuffer (int size);
|
||||
void luaL_oldbuffer (int old);
|
||||
char *luaL_buffer (void);
|
||||
int luaL_findstring (char *name, char *list[]);
|
||||
void luaL_chunkid (char *out, char *source, int len);
|
||||
void luaL_filesource (char *out, char *filename, int len);
|
||||
LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname,
|
||||
const luaL_Reg *l, int nup);
|
||||
LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
|
||||
const luaL_Reg *l);
|
||||
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 lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
|
||||
LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
|
||||
lua_Integer 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_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_checkoption) (lua_State *L, int narg, const char *def,
|
||||
const char *const lst[]);
|
||||
|
||||
LUALIB_API int (luaL_ref) (lua_State *L, int t);
|
||||
LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
|
||||
|
||||
LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
|
||||
LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
|
||||
const char *name);
|
||||
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
|
||||
|
||||
LUALIB_API lua_State *(luaL_newstate) (void);
|
||||
|
||||
|
||||
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
|
||||
const char *r);
|
||||
|
||||
LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
|
||||
const char *fname, int szhint);
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** some useful macros
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#define luaL_argcheck(L, cond,numarg,extramsg) \
|
||||
((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
|
||||
#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_checkinteger(L, (n)))
|
||||
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
|
||||
#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
|
||||
#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
|
||||
|
||||
#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
|
||||
|
||||
#define luaL_dofile(L, fn) \
|
||||
(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
|
||||
|
||||
#define luaL_dostring(L, s) \
|
||||
(luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
|
||||
|
||||
#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
|
||||
|
||||
#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
|
||||
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_addchar(B,c) \
|
||||
((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
|
||||
(*(B)->p++ = (char)(c)))
|
||||
|
||||
/* compatibility only */
|
||||
#define luaL_putchar(B,c) luaL_addchar(B,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 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))
|
||||
|
||||
|
||||
#define luaL_reg luaL_Reg
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
643
lbaselib.c
Normal file
643
lbaselib.c
Normal file
@@ -0,0 +1,643 @@
|
||||
/*
|
||||
** $Id: lbaselib.c,v 1.190 2006/05/31 16:50:40 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
|
||||
#define LUA_LIB
|
||||
|
||||
#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, LUA_QL("tostring") " must return a string to "
|
||||
LUA_QL("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);
|
||||
lua_settop(L, 1);
|
||||
if (lua_isstring(L, 1) && level > 0) { /* 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 luaB_getfenv (lua_State *L) {
|
||||
getfunc(L);
|
||||
if (lua_iscfunction(L, -1)) /* is a C function? */
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
|
||||
else
|
||||
lua_getfenv(L, -1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_setfenv (lua_State *L) {
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
getfunc(L);
|
||||
lua_pushvalue(L, 2);
|
||||
if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
|
||||
/* change environment of current thread */
|
||||
lua_pushthread(L);
|
||||
lua_insert(L, -2);
|
||||
lua_setfenv(L, -2);
|
||||
return 0;
|
||||
}
|
||||
else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
|
||||
luaL_error(L,
|
||||
LUA_QL("setfenv") " cannot change environment of given object");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
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_settop(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_settop(L, 3);
|
||||
lua_rawset(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_gcinfo (lua_State *L) {
|
||||
lua_pushinteger(L, lua_getgccount(L));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_collectgarbage (lua_State *L) {
|
||||
static const char *const opts[] = {"stop", "restart", "collect",
|
||||
"count", "step", "setpause", "setstepmul", NULL};
|
||||
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
|
||||
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL};
|
||||
int o = luaL_checkoption(L, 1, "collect", opts);
|
||||
int ex = luaL_optint(L, 2, 0);
|
||||
int res = lua_gc(L, optsnum[o], ex);
|
||||
switch (optsnum[o]) {
|
||||
case LUA_GCCOUNT: {
|
||||
int b = lua_gc(L, LUA_GCCOUNTB, 0);
|
||||
lua_pushnumber(L, res + ((lua_Number)b/1024));
|
||||
return 1;
|
||||
}
|
||||
case LUA_GCSTEP: {
|
||||
lua_pushboolean(L, res);
|
||||
return 1;
|
||||
}
|
||||
default: {
|
||||
lua_pushnumber(L, res);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_type (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushstring(L, luaL_typename(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_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushnil(L); /* and initial value */
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
static int ipairsaux (lua_State *L) {
|
||||
int i = luaL_checkint(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
i++; /* next value */
|
||||
lua_pushinteger(L, i);
|
||||
lua_rawgeti(L, 1, i);
|
||||
return (lua_isnil(L, -1)) ? 0 : 2;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_ipairs (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushinteger(L, 0); /* and initial value */
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Reader for generic `load' function: `lua_load' uses the
|
||||
** stack for internal stuff, so the reader cannot change the
|
||||
** stack top. Instead, it keeps its resulting string in a
|
||||
** reserved slot inside the stack.
|
||||
*/
|
||||
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
|
||||
(void)ud; /* to avoid warnings */
|
||||
luaL_checkstack(L, 2, "too many nested functions");
|
||||
lua_pushvalue(L, 1); /* get function */
|
||||
lua_call(L, 0, 1); /* call it */
|
||||
if (lua_isnil(L, -1)) {
|
||||
*size = 0;
|
||||
return NULL;
|
||||
}
|
||||
else if (lua_isstring(L, -1)) {
|
||||
lua_replace(L, 3); /* save string in a reserved stack slot */
|
||||
return lua_tolstring(L, 3, size);
|
||||
}
|
||||
else luaL_error(L, "reader function must return a string");
|
||||
return NULL; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
static int luaB_load (lua_State *L) {
|
||||
int status;
|
||||
const char *cname = luaL_optstring(L, 2, "=(load)");
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
|
||||
status = lua_load(L, generic_reader, NULL, cname);
|
||||
return load_aux(L, status);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_dofile (lua_State *L) {
|
||||
const char *fname = luaL_optstring(L, 1, NULL);
|
||||
int n = lua_gettop(L);
|
||||
if (luaL_loadfile(L, fname) != 0) lua_error(L);
|
||||
lua_call(L, 0, LUA_MULTRET);
|
||||
return lua_gettop(L) - n;
|
||||
}
|
||||
|
||||
|
||||
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!"));
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_unpack (lua_State *L) {
|
||||
int i, e, n;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
i = luaL_optint(L, 2, 1);
|
||||
e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
|
||||
n = e - i + 1; /* number of elements */
|
||||
if (n <= 0) return 0; /* empty range */
|
||||
luaL_checkstack(L, n, "table too big to unpack");
|
||||
for (; i<=e; i++) /* push arg[i...e] */
|
||||
lua_rawgeti(L, 1, i);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_select (lua_State *L) {
|
||||
int n = lua_gettop(L);
|
||||
if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
|
||||
lua_pushinteger(L, n-1);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int i = luaL_checkint(L, 1);
|
||||
if (i < 0) i = n + i;
|
||||
else if (i > n) i = n;
|
||||
luaL_argcheck(L, 1 <= i, 1, "index out of range");
|
||||
return n - i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
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));
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
lua_pushvalue(L, 1);
|
||||
break;
|
||||
case LUA_TBOOLEAN:
|
||||
lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
|
||||
break;
|
||||
case LUA_TNIL:
|
||||
lua_pushliteral(L, "nil");
|
||||
break;
|
||||
default:
|
||||
lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_Reg base_funcs[] = {
|
||||
{"assert", luaB_assert},
|
||||
{"collectgarbage", luaB_collectgarbage},
|
||||
{"dofile", luaB_dofile},
|
||||
{"error", luaB_error},
|
||||
{"gcinfo", luaB_gcinfo},
|
||||
{"getfenv", luaB_getfenv},
|
||||
{"getmetatable", luaB_getmetatable},
|
||||
{"loadfile", luaB_loadfile},
|
||||
{"load", luaB_load},
|
||||
{"loadstring", luaB_loadstring},
|
||||
{"next", luaB_next},
|
||||
{"pcall", luaB_pcall},
|
||||
{"print", luaB_print},
|
||||
{"rawequal", luaB_rawequal},
|
||||
{"rawget", luaB_rawget},
|
||||
{"rawset", luaB_rawset},
|
||||
{"select", luaB_select},
|
||||
{"setfenv", luaB_setfenv},
|
||||
{"setmetatable", luaB_setmetatable},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
{"type", luaB_type},
|
||||
{"unpack", luaB_unpack},
|
||||
{"xpcall", luaB_xpcall},
|
||||
{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");
|
||||
if (lua_status(co) == 0 && lua_gettop(co) == 0) {
|
||||
lua_pushliteral(L, "cannot resume dead coroutine");
|
||||
return -1; /* error flag */
|
||||
}
|
||||
lua_xmove(L, co, narg);
|
||||
status = lua_resume(co, narg);
|
||||
if (status == 0 || status == LUA_YIELD) {
|
||||
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 {
|
||||
switch (lua_status(co)) {
|
||||
case LUA_YIELD:
|
||||
lua_pushliteral(L, "suspended");
|
||||
break;
|
||||
case 0: {
|
||||
lua_Debug ar;
|
||||
if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
|
||||
lua_pushliteral(L, "normal"); /* it is running */
|
||||
else if (lua_gettop(co) == 0)
|
||||
lua_pushliteral(L, "dead");
|
||||
else
|
||||
lua_pushliteral(L, "suspended"); /* initial state */
|
||||
break;
|
||||
}
|
||||
default: /* some error occured */
|
||||
lua_pushliteral(L, "dead");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_corunning (lua_State *L) {
|
||||
if (lua_pushthread(L))
|
||||
return 0; /* main thread is not a coroutine */
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_Reg co_funcs[] = {
|
||||
{"create", luaB_cocreate},
|
||||
{"resume", luaB_coresume},
|
||||
{"running", luaB_corunning},
|
||||
{"status", luaB_costatus},
|
||||
{"wrap", luaB_cowrap},
|
||||
{"yield", luaB_yield},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static void auxopen (lua_State *L, const char *name,
|
||||
lua_CFunction f, lua_CFunction u) {
|
||||
lua_pushcfunction(L, u);
|
||||
lua_pushcclosure(L, f, 1);
|
||||
lua_setfield(L, -2, name);
|
||||
}
|
||||
|
||||
|
||||
static void base_open (lua_State *L) {
|
||||
/* set global _G */
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
lua_setglobal(L, "_G");
|
||||
/* open lib into global table */
|
||||
luaL_register(L, "_G", base_funcs);
|
||||
lua_pushliteral(L, LUA_VERSION);
|
||||
lua_setglobal(L, "_VERSION"); /* set global _VERSION */
|
||||
/* `ipairs' and `pairs' need auxiliary functions as upvalues */
|
||||
auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
|
||||
auxopen(L, "pairs", luaB_pairs, luaB_next);
|
||||
/* `newproxy' needs a weaktable as upvalue */
|
||||
lua_createtable(L, 0, 1); /* new table `w' */
|
||||
lua_pushvalue(L, -1); /* `w' will be its own metatable */
|
||||
lua_setmetatable(L, -2);
|
||||
lua_pushliteral(L, "kv");
|
||||
lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
|
||||
lua_pushcclosure(L, luaB_newproxy, 1);
|
||||
lua_setglobal(L, "newproxy"); /* set global `newproxy' */
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaopen_base (lua_State *L) {
|
||||
base_open(L);
|
||||
luaL_register(L, LUA_COLIBNAME, co_funcs);
|
||||
return 2;
|
||||
}
|
||||
|
||||
75
lbuffer.c
75
lbuffer.c
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuffer.c,v 1.8 1999/02/25 19:20:40 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*-------------------------------------------------------
|
||||
** Auxiliary buffer
|
||||
-------------------------------------------------------*/
|
||||
|
||||
|
||||
#define EXTRABUFF 32
|
||||
|
||||
|
||||
#define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size)
|
||||
|
||||
static void Openspace (int size) {
|
||||
lua_State *l = L; /* to optimize */
|
||||
size += EXTRABUFF;
|
||||
l->Mbuffsize = l->Mbuffnext+size;
|
||||
luaM_growvector(l->Mbuffer, l->Mbuffnext, size, char, arrEM, MAX_INT);
|
||||
}
|
||||
|
||||
|
||||
char *luaL_openspace (int size) {
|
||||
openspace(size);
|
||||
return L->Mbuffer+L->Mbuffnext;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addchar (int c) {
|
||||
openspace(1);
|
||||
L->Mbuffer[L->Mbuffnext++] = (char)c;
|
||||
}
|
||||
|
||||
|
||||
void luaL_resetbuffer (void) {
|
||||
L->Mbuffnext = L->Mbuffbase;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addsize (int n) {
|
||||
L->Mbuffnext += n;
|
||||
}
|
||||
|
||||
int luaL_getsize (void) {
|
||||
return L->Mbuffnext-L->Mbuffbase;
|
||||
}
|
||||
|
||||
int luaL_newbuffer (int size) {
|
||||
int old = L->Mbuffbase;
|
||||
openspace(size);
|
||||
L->Mbuffbase = L->Mbuffnext;
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
void luaL_oldbuffer (int old) {
|
||||
L->Mbuffnext = L->Mbuffbase;
|
||||
L->Mbuffbase = old;
|
||||
}
|
||||
|
||||
|
||||
char *luaL_buffer (void) {
|
||||
return L->Mbuffer+L->Mbuffbase;
|
||||
}
|
||||
|
||||
736
lbuiltin.c
736
lbuiltin.c
@@ -1,736 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuiltin.c,v 1.58 1999/05/27 20:21:03 roberto Exp roberto $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lbuiltin.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Auxiliary functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static void pushtagstring (TaggedString *s) {
|
||||
TObject o;
|
||||
o.ttype = LUA_T_STRING;
|
||||
o.value.ts = s;
|
||||
luaA_pushobject(&o);
|
||||
}
|
||||
|
||||
|
||||
static real getsize (Hash *h) {
|
||||
real max = 0;
|
||||
int i;
|
||||
for (i = 0; i<nhash(h); i++) {
|
||||
Node *n = h->node+i;
|
||||
if (ttype(ref(n)) == LUA_T_NUMBER &&
|
||||
ttype(val(n)) != LUA_T_NIL &&
|
||||
nvalue(ref(n)) > max)
|
||||
max = nvalue(ref(n));
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
|
||||
static real getnarg (Hash *a) {
|
||||
TObject index;
|
||||
TObject *value;
|
||||
/* value = table.n */
|
||||
ttype(&index) = LUA_T_STRING;
|
||||
tsvalue(&index) = luaS_new("n");
|
||||
value = luaH_get(a, &index);
|
||||
return (ttype(value) == LUA_T_NUMBER) ? nvalue(value) : getsize(a);
|
||||
}
|
||||
|
||||
|
||||
static Hash *gethash (int arg) {
|
||||
return avalue(luaA_Address(luaL_tablearg(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.
|
||||
*/
|
||||
static void luaB_alert (void) {
|
||||
fputs(luaL_check_string(1), stderr);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Standard implementation of _ERRORMESSAGE.
|
||||
** The library "iolib" redefines _ERRORMESSAGE for better error information.
|
||||
*/
|
||||
static void error_message (void) {
|
||||
lua_Object al = lua_rawgetglobal("_ALERT");
|
||||
if (lua_isfunction(al)) { /* avoid error loop if _ALERT is not defined */
|
||||
char buff[600];
|
||||
sprintf(buff, "lua error: %.500s\n", luaL_check_string(1));
|
||||
lua_pushstring(buff);
|
||||
lua_callfunction(al);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support "stdout", 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).
|
||||
*/
|
||||
#ifndef MAXPRINT
|
||||
#define MAXPRINT 40 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
static void luaB_print (void) {
|
||||
lua_Object args[MAXPRINT];
|
||||
lua_Object obj;
|
||||
int n = 0;
|
||||
int i;
|
||||
while ((obj = lua_getparam(n+1)) != LUA_NOOBJECT) {
|
||||
luaL_arg_check(n < MAXPRINT, n+1, "too many arguments");
|
||||
args[n++] = obj;
|
||||
}
|
||||
for (i=0; i<n; i++) {
|
||||
lua_pushobject(args[i]);
|
||||
if (lua_call("tostring"))
|
||||
lua_error("error in `tostring' called by `print'");
|
||||
obj = lua_getresult(1);
|
||||
if (!lua_isstring(obj))
|
||||
lua_error("`tostring' must return a string to `print'");
|
||||
if (i>0) fputs("\t", stdout);
|
||||
fputs(lua_getstring(obj), stdout);
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
}
|
||||
|
||||
|
||||
static void luaB_tonumber (void) {
|
||||
int base = luaL_opt_int(2, 10);
|
||||
if (base == 10) { /* standard conversion */
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isnumber(o)) lua_pushnumber(lua_getnumber(o));
|
||||
else lua_pushnil(); /* not a number */
|
||||
}
|
||||
else {
|
||||
char *s = luaL_check_string(1);
|
||||
long n;
|
||||
luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
|
||||
n = strtol(s, &s, base);
|
||||
while (isspace((unsigned char)*s)) s++; /* skip trailing spaces */
|
||||
if (*s) lua_pushnil(); /* invalid format: return nil */
|
||||
else lua_pushnumber(n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaB_error (void) {
|
||||
lua_error(lua_getstring(lua_getparam(1)));
|
||||
}
|
||||
|
||||
static void luaB_setglobal (void) {
|
||||
char *n = luaL_check_string(1);
|
||||
lua_Object value = luaL_nonnullarg(2);
|
||||
lua_pushobject(value);
|
||||
lua_setglobal(n);
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
static void luaB_rawsetglobal (void) {
|
||||
char *n = luaL_check_string(1);
|
||||
lua_Object value = luaL_nonnullarg(2);
|
||||
lua_pushobject(value);
|
||||
lua_rawsetglobal(n);
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
static void luaB_getglobal (void) {
|
||||
lua_pushobject(lua_getglobal(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
static void luaB_rawgetglobal (void) {
|
||||
lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
static void luaB_luatag (void) {
|
||||
lua_pushnumber(lua_tag(lua_getparam(1)));
|
||||
}
|
||||
|
||||
static void luaB_settag (void) {
|
||||
lua_Object o = luaL_tablearg(1);
|
||||
lua_pushobject(o);
|
||||
lua_settag(luaL_check_int(2));
|
||||
lua_pushobject(o); /* return first argument */
|
||||
}
|
||||
|
||||
static void luaB_newtag (void) {
|
||||
lua_pushnumber(lua_newtag());
|
||||
}
|
||||
|
||||
static void luaB_copytagmethods (void) {
|
||||
lua_pushnumber(lua_copytagmethods(luaL_check_int(1),
|
||||
luaL_check_int(2)));
|
||||
}
|
||||
|
||||
static void luaB_rawgettable (void) {
|
||||
lua_pushobject(luaL_nonnullarg(1));
|
||||
lua_pushobject(luaL_nonnullarg(2));
|
||||
lua_pushobject(lua_rawgettable());
|
||||
}
|
||||
|
||||
static void luaB_rawsettable (void) {
|
||||
lua_pushobject(luaL_nonnullarg(1));
|
||||
lua_pushobject(luaL_nonnullarg(2));
|
||||
lua_pushobject(luaL_nonnullarg(3));
|
||||
lua_rawsettable();
|
||||
}
|
||||
|
||||
static void luaB_settagmethod (void) {
|
||||
lua_Object nf = luaL_nonnullarg(3);
|
||||
lua_pushobject(nf);
|
||||
lua_pushobject(lua_settagmethod(luaL_check_int(1), luaL_check_string(2)));
|
||||
}
|
||||
|
||||
static void luaB_gettagmethod (void) {
|
||||
lua_pushobject(lua_gettagmethod(luaL_check_int(1), luaL_check_string(2)));
|
||||
}
|
||||
|
||||
static void luaB_seterrormethod (void) {
|
||||
lua_Object nf = luaL_functionarg(1);
|
||||
lua_pushobject(nf);
|
||||
lua_pushobject(lua_seterrormethod());
|
||||
}
|
||||
|
||||
static void luaB_collectgarbage (void) {
|
||||
lua_pushnumber(lua_collectgarbage(luaL_opt_int(1, 0)));
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Functions that could use only the official API but
|
||||
** do not, for efficiency.
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void luaB_dostring (void) {
|
||||
long l;
|
||||
char *s = luaL_check_lstr(1, &l);
|
||||
if (*s == ID_CHUNK)
|
||||
lua_error("`dostring' cannot run pre-compiled code");
|
||||
if (lua_dobuffer(s, l, luaL_opt_string(2, s)) == 0)
|
||||
if (luaA_passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
|
||||
static void luaB_dofile (void) {
|
||||
char *fname = luaL_opt_string(1, NULL);
|
||||
if (lua_dofile(fname) == 0)
|
||||
if (luaA_passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
|
||||
static void luaB_call (void) {
|
||||
lua_Object f = luaL_nonnullarg(1);
|
||||
Hash *arg = gethash(2);
|
||||
char *options = luaL_opt_string(3, "");
|
||||
lua_Object err = lua_getparam(4);
|
||||
int narg = (int)getnarg(arg);
|
||||
int i, status;
|
||||
if (err != LUA_NOOBJECT) { /* set new error method */
|
||||
lua_pushobject(err);
|
||||
err = lua_seterrormethod();
|
||||
}
|
||||
/* push arg[1...n] */
|
||||
luaD_checkstack(narg);
|
||||
for (i=0; i<narg; i++)
|
||||
*(L->stack.top++) = *luaH_getint(arg, i+1);
|
||||
status = lua_callfunction(f);
|
||||
if (err != LUA_NOOBJECT) { /* restore old error method */
|
||||
lua_pushobject(err);
|
||||
lua_seterrormethod();
|
||||
}
|
||||
if (status != 0) { /* error in call? */
|
||||
if (strchr(options, 'x')) {
|
||||
lua_pushnil();
|
||||
return; /* return nil to signal the error */
|
||||
}
|
||||
else
|
||||
lua_error(NULL);
|
||||
}
|
||||
else { /* no errors */
|
||||
if (strchr(options, 'p'))
|
||||
luaA_packresults();
|
||||
else
|
||||
luaA_passresults();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaB_nextvar (void) {
|
||||
TObject *o = luaA_Address(luaL_nonnullarg(1));
|
||||
TaggedString *g;
|
||||
if (ttype(o) == LUA_T_NIL)
|
||||
g = NULL;
|
||||
else {
|
||||
luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
|
||||
g = tsvalue(o);
|
||||
}
|
||||
if (!luaA_nextvar(g))
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
|
||||
static void luaB_next (void) {
|
||||
Hash *a = gethash(1);
|
||||
TObject *k = luaA_Address(luaL_nonnullarg(2));
|
||||
int i = (ttype(k) == LUA_T_NIL) ? 0 : luaH_pos(a, k)+1;
|
||||
if (luaA_next(a, i) == 0)
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
|
||||
static void luaB_tostring (void) {
|
||||
lua_Object obj = lua_getparam(1);
|
||||
TObject *o = luaA_Address(obj);
|
||||
char buff[64];
|
||||
switch (ttype(o)) {
|
||||
case LUA_T_NUMBER:
|
||||
lua_pushstring(lua_getstring(obj));
|
||||
return;
|
||||
case LUA_T_STRING:
|
||||
lua_pushobject(obj);
|
||||
return;
|
||||
case LUA_T_ARRAY:
|
||||
sprintf(buff, "table: %p", (void *)o->value.a);
|
||||
break;
|
||||
case LUA_T_CLOSURE:
|
||||
sprintf(buff, "function: %p", (void *)o->value.cl);
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
sprintf(buff, "function: %p", (void *)o->value.tf);
|
||||
break;
|
||||
case LUA_T_CPROTO:
|
||||
sprintf(buff, "function: %p", (void *)o->value.f);
|
||||
break;
|
||||
case LUA_T_USERDATA:
|
||||
sprintf(buff, "userdata: %p", o->value.ts->u.d.v);
|
||||
break;
|
||||
case LUA_T_NIL:
|
||||
lua_pushstring("nil");
|
||||
return;
|
||||
default:
|
||||
LUA_INTERNALERROR("invalid type");
|
||||
}
|
||||
lua_pushstring(buff);
|
||||
}
|
||||
|
||||
|
||||
static void luaB_type (void) {
|
||||
lua_Object o = luaL_nonnullarg(1);
|
||||
lua_pushstring(luaO_typename(luaA_Address(o)));
|
||||
lua_pushnumber(lua_tag(o));
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** "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".
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void luaB_assert (void) {
|
||||
lua_Object p = lua_getparam(1);
|
||||
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||
luaL_verror("assertion failed! %.100s", luaL_opt_string(2, ""));
|
||||
}
|
||||
|
||||
|
||||
static void luaB_foreachi (void) {
|
||||
Hash *t = gethash(1);
|
||||
int i;
|
||||
int n = (int)getnarg(t);
|
||||
TObject f;
|
||||
/* 'f' cannot be a pointer to TObject, because it is on the stack, and the
|
||||
stack may be reallocated by the call. Moreover, some C compilers do not
|
||||
initialize structs, so we must do the assignment after the declaration */
|
||||
f = *luaA_Address(luaL_functionarg(2));
|
||||
luaD_checkstack(3); /* for f, ref, and val */
|
||||
for (i=1; i<=n; i++) {
|
||||
*(L->stack.top++) = f;
|
||||
ttype(L->stack.top) = LUA_T_NUMBER; nvalue(L->stack.top++) = i;
|
||||
*(L->stack.top++) = *luaH_getint(t, i);
|
||||
luaD_calln(2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL)
|
||||
return;
|
||||
L->stack.top--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaB_foreach (void) {
|
||||
Hash *a = gethash(1);
|
||||
int i;
|
||||
TObject f; /* see comment in 'foreachi' */
|
||||
f = *luaA_Address(luaL_functionarg(2));
|
||||
luaD_checkstack(3); /* for f, ref, and val */
|
||||
for (i=0; i<a->nhash; i++) {
|
||||
Node *nd = &(a->node[i]);
|
||||
if (ttype(val(nd)) != LUA_T_NIL) {
|
||||
*(L->stack.top++) = f;
|
||||
*(L->stack.top++) = *ref(nd);
|
||||
*(L->stack.top++) = *val(nd);
|
||||
luaD_calln(2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL)
|
||||
return;
|
||||
L->stack.top--; /* remove result */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaB_foreachvar (void) {
|
||||
GCnode *g;
|
||||
TObject f; /* see comment in 'foreachi' */
|
||||
f = *luaA_Address(luaL_functionarg(1));
|
||||
luaD_checkstack(4); /* for extra var name, f, var name, and globalval */
|
||||
for (g = L->rootglobal.next; g; g = g->next) {
|
||||
TaggedString *s = (TaggedString *)g;
|
||||
if (s->u.s.globalval.ttype != LUA_T_NIL) {
|
||||
pushtagstring(s); /* keep (extra) s on stack to avoid GC */
|
||||
*(L->stack.top++) = f;
|
||||
pushtagstring(s);
|
||||
*(L->stack.top++) = s->u.s.globalval;
|
||||
luaD_calln(2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL) {
|
||||
L->stack.top--;
|
||||
*(L->stack.top-1) = *L->stack.top; /* remove extra s */
|
||||
return;
|
||||
}
|
||||
L->stack.top-=2; /* remove result and extra s */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaB_getn (void) {
|
||||
lua_pushnumber(getnarg(gethash(1)));
|
||||
}
|
||||
|
||||
|
||||
static void luaB_tinsert (void) {
|
||||
Hash *a = gethash(1);
|
||||
lua_Object v = lua_getparam(3);
|
||||
int n = (int)getnarg(a);
|
||||
int pos;
|
||||
if (v != LUA_NOOBJECT)
|
||||
pos = luaL_check_int(2);
|
||||
else { /* called with only 2 arguments */
|
||||
v = luaL_nonnullarg(2);
|
||||
pos = n+1;
|
||||
}
|
||||
luaV_setn(a, n+1); /* a.n = n+1 */
|
||||
for ( ;n>=pos; n--)
|
||||
luaH_move(a, n, n+1); /* a[n+1] = a[n] */
|
||||
luaH_setint(a, pos, luaA_Address(v)); /* a[pos] = v */
|
||||
}
|
||||
|
||||
|
||||
static void luaB_tremove (void) {
|
||||
Hash *a = gethash(1);
|
||||
int n = (int)getnarg(a);
|
||||
int pos = luaL_opt_int(2, n);
|
||||
if (n <= 0) return; /* table is "empty" */
|
||||
luaA_pushobject(luaH_getint(a, pos)); /* result = a[pos] */
|
||||
for ( ;pos<n; pos++)
|
||||
luaH_move(a, pos+1, pos); /* a[pos] = a[pos+1] */
|
||||
luaV_setn(a, n-1); /* a.n = n-1 */
|
||||
luaH_setint(a, n, &luaO_nilobject); /* a[n] = nil */
|
||||
}
|
||||
|
||||
|
||||
/* {
|
||||
** Quicksort
|
||||
*/
|
||||
|
||||
static void swap (Hash *a, int i, int j) {
|
||||
TObject temp;
|
||||
temp = *luaH_getint(a, i);
|
||||
luaH_move(a, j, i);
|
||||
luaH_setint(a, j, &temp);
|
||||
}
|
||||
|
||||
static int sort_comp (lua_Object f, TObject *a, TObject *b) {
|
||||
/* notice: the caller (auxsort) must check stack space */
|
||||
if (f != LUA_NOOBJECT) {
|
||||
*(L->stack.top) = *luaA_Address(f);
|
||||
*(L->stack.top+1) = *a;
|
||||
*(L->stack.top+2) = *b;
|
||||
L->stack.top += 3;
|
||||
luaD_calln(2, 1);
|
||||
}
|
||||
else { /* a < b? */
|
||||
*(L->stack.top) = *a;
|
||||
*(L->stack.top+1) = *b;
|
||||
L->stack.top += 2;
|
||||
luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
|
||||
}
|
||||
return ttype(--(L->stack.top)) != LUA_T_NIL;
|
||||
}
|
||||
|
||||
static void auxsort (Hash *a, int l, int u, lua_Object f) {
|
||||
while (l < u) { /* for tail recursion */
|
||||
TObject *P;
|
||||
int i, j;
|
||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||
if (sort_comp(f, luaH_getint(a, u), luaH_getint(a, l))) /* a[l]>a[u]? */
|
||||
swap(a, l, u);
|
||||
if (u-l == 1) break; /* only 2 elements */
|
||||
i = (l+u)/2;
|
||||
P = luaH_getint(a, i);
|
||||
if (sort_comp(f, P, luaH_getint(a, l))) /* a[l]>a[i]? */
|
||||
swap(a, l, i);
|
||||
else if (sort_comp(f, luaH_getint(a, u), P)) /* a[i]>a[u]? */
|
||||
swap(a, i, u);
|
||||
if (u-l == 2) break; /* only 3 elements */
|
||||
P = L->stack.top++;
|
||||
*P = *luaH_getint(a, i); /* save pivot on stack (for GC) */
|
||||
swap(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] */
|
||||
while (sort_comp(f, luaH_getint(a, ++i), P)) /* stop when a[i] >= P */
|
||||
if (i>u) lua_error("invalid order function for sorting");
|
||||
while (sort_comp(f, P, luaH_getint(a, --j))) /* stop when a[j] <= P */
|
||||
if (j<l) lua_error("invalid order function for sorting");
|
||||
if (j<i) break;
|
||||
swap(a, i, j);
|
||||
}
|
||||
swap(a, u-1, i); /* swap pivot (a[u-1]) with a[i] */
|
||||
L->stack.top--; /* remove pivot from stack */
|
||||
/* 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(a, j, i, f); /* call recursively the smaller one */
|
||||
} /* repeat the routine for the larger one */
|
||||
}
|
||||
|
||||
static void luaB_sort (void) {
|
||||
lua_Object t = lua_getparam(1);
|
||||
Hash *a = gethash(1);
|
||||
int n = (int)getnarg(a);
|
||||
lua_Object func = lua_getparam(2);
|
||||
luaL_arg_check(func == LUA_NOOBJECT || lua_isfunction(func), 2,
|
||||
"function expected");
|
||||
luaD_checkstack(4); /* for Pivot, f, a, b (sort_comp) */
|
||||
auxsort(a, 1, n, func);
|
||||
lua_pushobject(t);
|
||||
}
|
||||
|
||||
/* }}===================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** ====================================================== */
|
||||
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
/*
|
||||
** {======================================================
|
||||
** some DEBUG functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void mem_query (void) {
|
||||
lua_pushnumber(totalmem);
|
||||
lua_pushnumber(numblocks);
|
||||
}
|
||||
|
||||
|
||||
static void query_strings (void) {
|
||||
lua_pushnumber(L->string_root[luaL_check_int(1)].nuse);
|
||||
}
|
||||
|
||||
|
||||
static void countlist (void) {
|
||||
char *s = luaL_check_string(1);
|
||||
GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next :
|
||||
(s[0]=='p') ? L->rootproto.next : L->rootglobal.next;
|
||||
int i=0;
|
||||
while (l) {
|
||||
i++;
|
||||
l = l->next;
|
||||
}
|
||||
lua_pushnumber(i);
|
||||
}
|
||||
|
||||
|
||||
static void testC (void) {
|
||||
#define getnum(s) ((*s++) - '0')
|
||||
#define getname(s) (nome[0] = *s++, nome)
|
||||
|
||||
static int locks[10];
|
||||
lua_Object reg[10];
|
||||
char nome[2];
|
||||
char *s = luaL_check_string(1);
|
||||
nome[1] = 0;
|
||||
for (;;) {
|
||||
switch (*s++) {
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
lua_pushnumber(*(s-1) - '0');
|
||||
break;
|
||||
|
||||
case 'c': reg[getnum(s)] = lua_createtable(); break;
|
||||
case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
|
||||
lua_pushcclosure(f, getnum(s));
|
||||
break;
|
||||
}
|
||||
case 'P': reg[getnum(s)] = lua_pop(); break;
|
||||
case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
|
||||
case 'G': { int n = getnum(s);
|
||||
reg[n] = lua_rawgetglobal(getname(s));
|
||||
break;
|
||||
}
|
||||
case 'l': locks[getnum(s)] = lua_ref(1); break;
|
||||
case 'L': locks[getnum(s)] = lua_ref(0); break;
|
||||
case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; }
|
||||
case 'u': lua_unref(locks[getnum(s)]); break;
|
||||
case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
|
||||
case '=': lua_setglobal(getname(s)); break;
|
||||
case 's': lua_pushstring(getname(s)); break;
|
||||
case 'o': lua_pushobject(reg[getnum(s)]); break;
|
||||
case 'f': lua_call(getname(s)); break;
|
||||
case 'i': reg[getnum(s)] = lua_gettable(); break;
|
||||
case 'I': reg[getnum(s)] = lua_rawgettable(); break;
|
||||
case 't': lua_settable(); break;
|
||||
case 'T': lua_rawsettable(); break;
|
||||
case 'N' : lua_pushstring(lua_nextvar(lua_getstring(reg[getnum(s)])));
|
||||
break;
|
||||
case 'n' : { int n=getnum(s);
|
||||
n=lua_next(reg[n], (int)lua_getnumber(reg[getnum(s)]));
|
||||
lua_pushnumber(n); break;
|
||||
}
|
||||
default: luaL_verror("unknown command in `testC': %c", *(s-1));
|
||||
}
|
||||
if (*s == 0) return;
|
||||
if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
|
||||
}
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
static struct luaL_reg builtin_funcs[] = {
|
||||
#ifdef LUA_COMPAT2_5
|
||||
{"setfallback", luaT_setfallback},
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
{"testC", testC},
|
||||
{"totalmem", mem_query},
|
||||
{"count", countlist},
|
||||
{"querystr", query_strings},
|
||||
#endif
|
||||
{"_ALERT", luaB_alert},
|
||||
{"_ERRORMESSAGE", error_message},
|
||||
{"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},
|
||||
{"seterrormethod", luaB_seterrormethod},
|
||||
{"setglobal", luaB_setglobal},
|
||||
{"settag", luaB_settag},
|
||||
{"settagmethod", luaB_settagmethod},
|
||||
{"tag", luaB_luatag},
|
||||
{"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}
|
||||
};
|
||||
|
||||
|
||||
#define INTFUNCSIZE (sizeof(builtin_funcs)/sizeof(builtin_funcs[0]))
|
||||
|
||||
|
||||
void luaB_predefine (void) {
|
||||
/* pre-register mem error messages, to avoid loop when error arises */
|
||||
luaS_newfixedstring(tableEM);
|
||||
luaS_newfixedstring(memEM);
|
||||
luaL_openlib(builtin_funcs, (sizeof(builtin_funcs)/sizeof(builtin_funcs[0])));
|
||||
lua_pushstring(LUA_VERSION);
|
||||
lua_setglobal("_VERSION");
|
||||
}
|
||||
|
||||
14
lbuiltin.h
14
lbuiltin.h
@@ -1,14 +0,0 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lbuiltin_h
|
||||
#define lbuiltin_h
|
||||
|
||||
|
||||
void luaB_predefine (void);
|
||||
|
||||
|
||||
#endif
|
||||
823
lcode.c
Normal file
823
lcode.c
Normal file
@@ -0,0 +1,823 @@
|
||||
/*
|
||||
** $Id: lcode.c,v 2.24 2005/12/22 16:19:56 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define lcode_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lcode.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lgc.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
#include "ltable.h"
|
||||
|
||||
|
||||
#define hasjumps(e) ((e)->t != (e)->f)
|
||||
|
||||
|
||||
static int isnumeral(expdesc *e) {
|
||||
return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
|
||||
}
|
||||
|
||||
|
||||
void luaK_nil (FuncState *fs, int from, int n) {
|
||||
Instruction *previous;
|
||||
if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
|
||||
if (fs->pc == 0) /* function start? */
|
||||
return; /* positions are already clean */
|
||||
if (GET_OPCODE(*(previous = &fs->f->code[fs->pc-1])) == OP_LOADNIL) {
|
||||
int pfrom = GETARG_A(*previous);
|
||||
int pto = GETARG_B(*previous);
|
||||
if (pfrom <= from && from <= pto+1) { /* can connect both? */
|
||||
if (from+n-1 > pto)
|
||||
SETARG_B(*previous, from+n-1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
|
||||
}
|
||||
|
||||
|
||||
int luaK_jump (FuncState *fs) {
|
||||
int jpc = fs->jpc; /* save list of jumps to here */
|
||||
int j;
|
||||
fs->jpc = NO_JUMP;
|
||||
j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
|
||||
luaK_concat(fs, &j, jpc); /* keep them on hold */
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
void luaK_ret (FuncState *fs, int first, int nret) {
|
||||
luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
|
||||
}
|
||||
|
||||
|
||||
static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
|
||||
luaK_codeABC(fs, op, A, B, C);
|
||||
return luaK_jump(fs);
|
||||
}
|
||||
|
||||
|
||||
static void fixjump (FuncState *fs, int pc, int dest) {
|
||||
Instruction *jmp = &fs->f->code[pc];
|
||||
int offset = dest-(pc+1);
|
||||
lua_assert(dest != NO_JUMP);
|
||||
if (abs(offset) > MAXARG_sBx)
|
||||
luaX_syntaxerror(fs->ls, "control structure too long");
|
||||
SETARG_sBx(*jmp, offset);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns current `pc' and marks it as a jump target (to avoid wrong
|
||||
** optimizations with consecutive instructions not in the same basic block).
|
||||
*/
|
||||
int luaK_getlabel (FuncState *fs) {
|
||||
fs->lasttarget = fs->pc;
|
||||
return fs->pc;
|
||||
}
|
||||
|
||||
|
||||
static int getjump (FuncState *fs, int pc) {
|
||||
int offset = GETARG_sBx(fs->f->code[pc]);
|
||||
if (offset == NO_JUMP) /* point to itself represents end of list */
|
||||
return NO_JUMP; /* end of list */
|
||||
else
|
||||
return (pc+1)+offset; /* turn offset into absolute position */
|
||||
}
|
||||
|
||||
|
||||
static Instruction *getjumpcontrol (FuncState *fs, int pc) {
|
||||
Instruction *pi = &fs->f->code[pc];
|
||||
if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
|
||||
return pi-1;
|
||||
else
|
||||
return pi;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** check whether list has any jump that do not produce a value
|
||||
** (or produce an inverted value)
|
||||
*/
|
||||
static int need_value (FuncState *fs, int list) {
|
||||
for (; list != NO_JUMP; list = getjump(fs, list)) {
|
||||
Instruction i = *getjumpcontrol(fs, list);
|
||||
if (GET_OPCODE(i) != OP_TESTSET) return 1;
|
||||
}
|
||||
return 0; /* not found */
|
||||
}
|
||||
|
||||
|
||||
static int patchtestreg (FuncState *fs, int node, int reg) {
|
||||
Instruction *i = getjumpcontrol(fs, node);
|
||||
if (GET_OPCODE(*i) != OP_TESTSET)
|
||||
return 0; /* cannot patch other instructions */
|
||||
if (reg != NO_REG && reg != GETARG_B(*i))
|
||||
SETARG_A(*i, reg);
|
||||
else /* no register to put value or register already has the value */
|
||||
*i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void removevalues (FuncState *fs, int list) {
|
||||
for (; list != NO_JUMP; list = getjump(fs, list))
|
||||
patchtestreg(fs, list, NO_REG);
|
||||
}
|
||||
|
||||
|
||||
static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
|
||||
int dtarget) {
|
||||
while (list != NO_JUMP) {
|
||||
int next = getjump(fs, list);
|
||||
if (patchtestreg(fs, list, reg))
|
||||
fixjump(fs, list, vtarget);
|
||||
else
|
||||
fixjump(fs, list, dtarget); /* jump to default target */
|
||||
list = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void dischargejpc (FuncState *fs) {
|
||||
patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
|
||||
fs->jpc = NO_JUMP;
|
||||
}
|
||||
|
||||
|
||||
void luaK_patchlist (FuncState *fs, int list, int target) {
|
||||
if (target == fs->pc)
|
||||
luaK_patchtohere(fs, list);
|
||||
else {
|
||||
lua_assert(target < fs->pc);
|
||||
patchlistaux(fs, list, target, NO_REG, target);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_patchtohere (FuncState *fs, int list) {
|
||||
luaK_getlabel(fs);
|
||||
luaK_concat(fs, &fs->jpc, list);
|
||||
}
|
||||
|
||||
|
||||
void luaK_concat (FuncState *fs, int *l1, int l2) {
|
||||
if (l2 == NO_JUMP) return;
|
||||
else if (*l1 == NO_JUMP)
|
||||
*l1 = l2;
|
||||
else {
|
||||
int list = *l1;
|
||||
int next;
|
||||
while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
|
||||
list = next;
|
||||
fixjump(fs, list, l2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_checkstack (FuncState *fs, int n) {
|
||||
int newstack = fs->freereg + n;
|
||||
if (newstack > fs->f->maxstacksize) {
|
||||
if (newstack >= MAXSTACK)
|
||||
luaX_syntaxerror(fs->ls, "function or expression too complex");
|
||||
fs->f->maxstacksize = cast_byte(newstack);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_reserveregs (FuncState *fs, int n) {
|
||||
luaK_checkstack(fs, n);
|
||||
fs->freereg += n;
|
||||
}
|
||||
|
||||
|
||||
static void freereg (FuncState *fs, int reg) {
|
||||
if (!ISK(reg) && reg >= fs->nactvar) {
|
||||
fs->freereg--;
|
||||
lua_assert(reg == fs->freereg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void freeexp (FuncState *fs, expdesc *e) {
|
||||
if (e->k == VNONRELOC)
|
||||
freereg(fs, e->u.s.info);
|
||||
}
|
||||
|
||||
|
||||
static int addk (FuncState *fs, TValue *k, TValue *v) {
|
||||
lua_State *L = fs->L;
|
||||
TValue *idx = luaH_set(L, fs->h, k);
|
||||
Proto *f = fs->f;
|
||||
int oldsize = f->sizek;
|
||||
if (ttisnumber(idx)) {
|
||||
lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
|
||||
return cast_int(nvalue(idx));
|
||||
}
|
||||
else { /* constant not found; create a new entry */
|
||||
setnvalue(idx, cast_num(fs->nk));
|
||||
luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
|
||||
MAXARG_Bx, "constant table overflow");
|
||||
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
|
||||
setobj(L, &f->k[fs->nk], v);
|
||||
luaC_barrier(L, f, v);
|
||||
return fs->nk++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaK_stringK (FuncState *fs, TString *s) {
|
||||
TValue o;
|
||||
setsvalue(fs->L, &o, s);
|
||||
return addk(fs, &o, &o);
|
||||
}
|
||||
|
||||
|
||||
int luaK_numberK (FuncState *fs, lua_Number r) {
|
||||
TValue o;
|
||||
setnvalue(&o, r);
|
||||
return addk(fs, &o, &o);
|
||||
}
|
||||
|
||||
|
||||
static int boolK (FuncState *fs, int b) {
|
||||
TValue o;
|
||||
setbvalue(&o, b);
|
||||
return addk(fs, &o, &o);
|
||||
}
|
||||
|
||||
|
||||
static int nilK (FuncState *fs) {
|
||||
TValue k, v;
|
||||
setnilvalue(&v);
|
||||
/* cannot use nil as key; instead use table itself to represent nil */
|
||||
sethvalue(fs->L, &k, fs->h);
|
||||
return addk(fs, &k, &v);
|
||||
}
|
||||
|
||||
|
||||
void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
|
||||
if (e->k == VCALL) { /* expression is an open function call? */
|
||||
SETARG_C(getcode(fs, e), nresults+1);
|
||||
}
|
||||
else if (e->k == VVARARG) {
|
||||
SETARG_B(getcode(fs, e), nresults+1);
|
||||
SETARG_A(getcode(fs, e), fs->freereg);
|
||||
luaK_reserveregs(fs, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_setoneret (FuncState *fs, expdesc *e) {
|
||||
if (e->k == VCALL) { /* expression is an open function call? */
|
||||
e->k = VNONRELOC;
|
||||
e->u.s.info = GETARG_A(getcode(fs, e));
|
||||
}
|
||||
else if (e->k == VVARARG) {
|
||||
SETARG_B(getcode(fs, e), 2);
|
||||
e->k = VRELOCABLE; /* can relocate its simple result */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
||||
switch (e->k) {
|
||||
case VLOCAL: {
|
||||
e->k = VNONRELOC;
|
||||
break;
|
||||
}
|
||||
case VUPVAL: {
|
||||
e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0);
|
||||
e->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
case VGLOBAL: {
|
||||
e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);
|
||||
e->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
case VINDEXED: {
|
||||
freereg(fs, e->u.s.aux);
|
||||
freereg(fs, e->u.s.info);
|
||||
e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux);
|
||||
e->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
case VVARARG:
|
||||
case VCALL: {
|
||||
luaK_setoneret(fs, e);
|
||||
break;
|
||||
}
|
||||
default: break; /* there is one value available (somewhere) */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int code_label (FuncState *fs, int A, int b, int jump) {
|
||||
luaK_getlabel(fs); /* those instructions may be jump targets */
|
||||
return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
|
||||
}
|
||||
|
||||
|
||||
static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: {
|
||||
luaK_nil(fs, reg, 1);
|
||||
break;
|
||||
}
|
||||
case VFALSE: case VTRUE: {
|
||||
luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
|
||||
break;
|
||||
}
|
||||
case VK: {
|
||||
luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);
|
||||
break;
|
||||
}
|
||||
case VKNUM: {
|
||||
luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
|
||||
break;
|
||||
}
|
||||
case VRELOCABLE: {
|
||||
Instruction *pc = &getcode(fs, e);
|
||||
SETARG_A(*pc, reg);
|
||||
break;
|
||||
}
|
||||
case VNONRELOC: {
|
||||
if (reg != e->u.s.info)
|
||||
luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
lua_assert(e->k == VVOID || e->k == VJMP);
|
||||
return; /* nothing to do... */
|
||||
}
|
||||
}
|
||||
e->u.s.info = reg;
|
||||
e->k = VNONRELOC;
|
||||
}
|
||||
|
||||
|
||||
static void discharge2anyreg (FuncState *fs, expdesc *e) {
|
||||
if (e->k != VNONRELOC) {
|
||||
luaK_reserveregs(fs, 1);
|
||||
discharge2reg(fs, e, fs->freereg-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void exp2reg (FuncState *fs, expdesc *e, int reg) {
|
||||
discharge2reg(fs, e, reg);
|
||||
if (e->k == VJMP)
|
||||
luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */
|
||||
if (hasjumps(e)) {
|
||||
int final; /* position after whole expression */
|
||||
int p_f = NO_JUMP; /* position of an eventual LOAD false */
|
||||
int p_t = NO_JUMP; /* position of an eventual LOAD true */
|
||||
if (need_value(fs, e->t) || need_value(fs, e->f)) {
|
||||
int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
|
||||
p_f = code_label(fs, reg, 0, 1);
|
||||
p_t = code_label(fs, reg, 1, 0);
|
||||
luaK_patchtohere(fs, fj);
|
||||
}
|
||||
final = luaK_getlabel(fs);
|
||||
patchlistaux(fs, e->f, final, reg, p_f);
|
||||
patchlistaux(fs, e->t, final, reg, p_t);
|
||||
}
|
||||
e->f = e->t = NO_JUMP;
|
||||
e->u.s.info = reg;
|
||||
e->k = VNONRELOC;
|
||||
}
|
||||
|
||||
|
||||
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
freeexp(fs, e);
|
||||
luaK_reserveregs(fs, 1);
|
||||
exp2reg(fs, e, fs->freereg - 1);
|
||||
}
|
||||
|
||||
|
||||
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
if (e->k == VNONRELOC) {
|
||||
if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */
|
||||
if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */
|
||||
exp2reg(fs, e, e->u.s.info); /* put value on it */
|
||||
return e->u.s.info;
|
||||
}
|
||||
}
|
||||
luaK_exp2nextreg(fs, e); /* default */
|
||||
return e->u.s.info;
|
||||
}
|
||||
|
||||
|
||||
void luaK_exp2val (FuncState *fs, expdesc *e) {
|
||||
if (hasjumps(e))
|
||||
luaK_exp2anyreg(fs, e);
|
||||
else
|
||||
luaK_dischargevars(fs, e);
|
||||
}
|
||||
|
||||
|
||||
int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
||||
luaK_exp2val(fs, e);
|
||||
switch (e->k) {
|
||||
case VKNUM:
|
||||
case VTRUE:
|
||||
case VFALSE:
|
||||
case VNIL: {
|
||||
if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
|
||||
e->u.s.info = (e->k == VNIL) ? nilK(fs) :
|
||||
(e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
|
||||
boolK(fs, (e->k == VTRUE));
|
||||
e->k = VK;
|
||||
return RKASK(e->u.s.info);
|
||||
}
|
||||
else break;
|
||||
}
|
||||
case VK: {
|
||||
if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */
|
||||
return RKASK(e->u.s.info);
|
||||
else break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
/* not a constant in the right range: put it in a register */
|
||||
return luaK_exp2anyreg(fs, e);
|
||||
}
|
||||
|
||||
|
||||
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
|
||||
switch (var->k) {
|
||||
case VLOCAL: {
|
||||
freeexp(fs, ex);
|
||||
exp2reg(fs, ex, var->u.s.info);
|
||||
return;
|
||||
}
|
||||
case VUPVAL: {
|
||||
int e = luaK_exp2anyreg(fs, ex);
|
||||
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);
|
||||
break;
|
||||
}
|
||||
case VGLOBAL: {
|
||||
int e = luaK_exp2anyreg(fs, ex);
|
||||
luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);
|
||||
break;
|
||||
}
|
||||
case VINDEXED: {
|
||||
int e = luaK_exp2RK(fs, ex);
|
||||
luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
lua_assert(0); /* invalid var kind to store */
|
||||
break;
|
||||
}
|
||||
}
|
||||
freeexp(fs, ex);
|
||||
}
|
||||
|
||||
|
||||
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
|
||||
int func;
|
||||
luaK_exp2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
func = fs->freereg;
|
||||
luaK_reserveregs(fs, 2);
|
||||
luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
|
||||
freeexp(fs, key);
|
||||
e->u.s.info = func;
|
||||
e->k = VNONRELOC;
|
||||
}
|
||||
|
||||
|
||||
static void invertjump (FuncState *fs, expdesc *e) {
|
||||
Instruction *pc = getjumpcontrol(fs, e->u.s.info);
|
||||
lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
|
||||
GET_OPCODE(*pc) != OP_TEST);
|
||||
SETARG_A(*pc, !(GETARG_A(*pc)));
|
||||
}
|
||||
|
||||
|
||||
static int jumponcond (FuncState *fs, expdesc *e, int cond) {
|
||||
if (e->k == VRELOCABLE) {
|
||||
Instruction ie = getcode(fs, e);
|
||||
if (GET_OPCODE(ie) == OP_NOT) {
|
||||
fs->pc--; /* remove previous OP_NOT */
|
||||
return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
|
||||
}
|
||||
/* else go through */
|
||||
}
|
||||
discharge2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond);
|
||||
}
|
||||
|
||||
|
||||
void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
||||
int pc; /* pc of last jump */
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VK: case VKNUM: case VTRUE: {
|
||||
pc = NO_JUMP; /* always true; do nothing */
|
||||
break;
|
||||
}
|
||||
case VFALSE: {
|
||||
pc = luaK_jump(fs); /* always jump */
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
invertjump(fs, e);
|
||||
pc = e->u.s.info;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
pc = jumponcond(fs, e, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
|
||||
luaK_patchtohere(fs, e->t);
|
||||
e->t = NO_JUMP;
|
||||
}
|
||||
|
||||
|
||||
static void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
||||
int pc; /* pc of last jump */
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: case VFALSE: {
|
||||
pc = NO_JUMP; /* always false; do nothing */
|
||||
break;
|
||||
}
|
||||
case VTRUE: {
|
||||
pc = luaK_jump(fs); /* always jump */
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
pc = e->u.s.info;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
pc = jumponcond(fs, e, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
|
||||
luaK_patchtohere(fs, e->f);
|
||||
e->f = NO_JUMP;
|
||||
}
|
||||
|
||||
|
||||
static void codenot (FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: case VFALSE: {
|
||||
e->k = VTRUE;
|
||||
break;
|
||||
}
|
||||
case VK: case VKNUM: case VTRUE: {
|
||||
e->k = VFALSE;
|
||||
break;
|
||||
}
|
||||
case VJMP: {
|
||||
invertjump(fs, e);
|
||||
break;
|
||||
}
|
||||
case VRELOCABLE:
|
||||
case VNONRELOC: {
|
||||
discharge2anyreg(fs, e);
|
||||
freeexp(fs, e);
|
||||
e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0);
|
||||
e->k = VRELOCABLE;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
lua_assert(0); /* cannot happen */
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* interchange true and false lists */
|
||||
{ int temp = e->f; e->f = e->t; e->t = temp; }
|
||||
removevalues(fs, e->f);
|
||||
removevalues(fs, e->t);
|
||||
}
|
||||
|
||||
|
||||
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
||||
t->u.s.aux = luaK_exp2RK(fs, k);
|
||||
t->k = VINDEXED;
|
||||
}
|
||||
|
||||
|
||||
static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
|
||||
lua_Number v1, v2, r;
|
||||
if (!isnumeral(e1) || !isnumeral(e2)) return 0;
|
||||
v1 = e1->u.nval;
|
||||
v2 = e2->u.nval;
|
||||
switch (op) {
|
||||
case OP_ADD: r = luai_numadd(v1, v2); break;
|
||||
case OP_SUB: r = luai_numsub(v1, v2); break;
|
||||
case OP_MUL: r = luai_nummul(v1, v2); break;
|
||||
case OP_DIV:
|
||||
if (v2 == 0) return 0; /* do not attempt to divide by 0 */
|
||||
r = luai_numdiv(v1, v2); break;
|
||||
case OP_MOD:
|
||||
if (v2 == 0) return 0; /* do not attempt to divide by 0 */
|
||||
r = luai_nummod(v1, v2); break;
|
||||
case OP_POW: r = luai_numpow(v1, v2); break;
|
||||
case OP_UNM: r = luai_numunm(v1); break;
|
||||
case OP_LEN: return 0; /* no constant folding for 'len' */
|
||||
default: lua_assert(0); r = 0; break;
|
||||
}
|
||||
if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
|
||||
e1->u.nval = r;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
|
||||
if (constfolding(op, e1, e2))
|
||||
return;
|
||||
else {
|
||||
int o1 = luaK_exp2RK(fs, e1);
|
||||
int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
|
||||
freeexp(fs, e2);
|
||||
freeexp(fs, e1);
|
||||
e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);
|
||||
e1->k = VRELOCABLE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
|
||||
expdesc *e2) {
|
||||
int o1 = luaK_exp2RK(fs, e1);
|
||||
int o2 = luaK_exp2RK(fs, e2);
|
||||
freeexp(fs, e2);
|
||||
freeexp(fs, e1);
|
||||
if (cond == 0 && op != OP_EQ) {
|
||||
int temp; /* exchange args to replace by `<' or `<=' */
|
||||
temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
|
||||
cond = 1;
|
||||
}
|
||||
e1->u.s.info = condjump(fs, op, cond, o1, o2);
|
||||
e1->k = VJMP;
|
||||
}
|
||||
|
||||
|
||||
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
|
||||
expdesc e2;
|
||||
e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
|
||||
switch (op) {
|
||||
case OPR_MINUS: {
|
||||
if (e->k == VK)
|
||||
luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */
|
||||
codearith(fs, OP_UNM, e, &e2);
|
||||
break;
|
||||
}
|
||||
case OPR_NOT: codenot(fs, e); break;
|
||||
case OPR_LEN: {
|
||||
luaK_exp2anyreg(fs, e); /* cannot operate on constants */
|
||||
codearith(fs, OP_LEN, e, &e2);
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
||||
switch (op) {
|
||||
case OPR_AND: {
|
||||
luaK_goiftrue(fs, v);
|
||||
break;
|
||||
}
|
||||
case OPR_OR: {
|
||||
luaK_goiffalse(fs, v);
|
||||
break;
|
||||
}
|
||||
case OPR_CONCAT: {
|
||||
luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
if (!isnumeral(v)) luaK_exp2RK(fs, v);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
|
||||
switch (op) {
|
||||
case OPR_AND: {
|
||||
lua_assert(e1->t == NO_JUMP); /* list must be closed */
|
||||
luaK_dischargevars(fs, e2);
|
||||
luaK_concat(fs, &e2->f, e1->f);
|
||||
*e1 = *e2;
|
||||
break;
|
||||
}
|
||||
case OPR_OR: {
|
||||
lua_assert(e1->f == NO_JUMP); /* list must be closed */
|
||||
luaK_dischargevars(fs, e2);
|
||||
luaK_concat(fs, &e2->t, e1->t);
|
||||
*e1 = *e2;
|
||||
break;
|
||||
}
|
||||
case OPR_CONCAT: {
|
||||
luaK_exp2val(fs, e2);
|
||||
if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
|
||||
lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1);
|
||||
freeexp(fs, e1);
|
||||
SETARG_B(getcode(fs, e2), e1->u.s.info);
|
||||
e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info;
|
||||
}
|
||||
else {
|
||||
luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
|
||||
codearith(fs, OP_CONCAT, e1, e2);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;
|
||||
case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;
|
||||
case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;
|
||||
case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
|
||||
case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;
|
||||
case OPR_POW: codearith(fs, OP_POW, e1, e2); break;
|
||||
case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;
|
||||
case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;
|
||||
case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;
|
||||
case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break;
|
||||
case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break;
|
||||
case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break;
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_fixline (FuncState *fs, int line) {
|
||||
fs->f->lineinfo[fs->pc - 1] = line;
|
||||
}
|
||||
|
||||
|
||||
static int luaK_code (FuncState *fs, Instruction i, int line) {
|
||||
Proto *f = fs->f;
|
||||
dischargejpc(fs); /* `pc' will change */
|
||||
/* put new instruction in code array */
|
||||
luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
|
||||
MAX_INT, "code size overflow");
|
||||
f->code[fs->pc] = i;
|
||||
/* save corresponding line information */
|
||||
luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
|
||||
MAX_INT, "code size overflow");
|
||||
f->lineinfo[fs->pc] = line;
|
||||
return fs->pc++;
|
||||
}
|
||||
|
||||
|
||||
int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
|
||||
lua_assert(getOpMode(o) == iABC);
|
||||
lua_assert(getBMode(o) != OpArgN || b == 0);
|
||||
lua_assert(getCMode(o) != OpArgN || c == 0);
|
||||
return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
|
||||
}
|
||||
|
||||
|
||||
int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
|
||||
lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
|
||||
lua_assert(getCMode(o) == OpArgN);
|
||||
return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
|
||||
}
|
||||
|
||||
|
||||
void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
|
||||
int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
|
||||
int b = (tostore == LUA_MULTRET) ? 0 : tostore;
|
||||
lua_assert(tostore != 0);
|
||||
if (c <= MAXARG_C)
|
||||
luaK_codeABC(fs, OP_SETLIST, base, b, c);
|
||||
else {
|
||||
luaK_codeABC(fs, OP_SETLIST, base, b, 0);
|
||||
luaK_code(fs, cast(Instruction, c), fs->ls->lastline);
|
||||
}
|
||||
fs->freereg = base + 1; /* free registers with list values */
|
||||
}
|
||||
|
||||
76
lcode.h
Normal file
76
lcode.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
** $Id: lcode.h,v 1.47 2005/11/08 19:44:31 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lcode_h
|
||||
#define lcode_h
|
||||
|
||||
#include "llex.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
|
||||
|
||||
/*
|
||||
** Marks the end of a patch list. It is an invalid value both as an absolute
|
||||
** address, and as a list link (would link an element to itself).
|
||||
*/
|
||||
#define NO_JUMP (-1)
|
||||
|
||||
|
||||
/*
|
||||
** grep "ORDER OPR" if you change these enums
|
||||
*/
|
||||
typedef enum BinOpr {
|
||||
OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,
|
||||
OPR_CONCAT,
|
||||
OPR_NE, OPR_EQ,
|
||||
OPR_LT, OPR_LE, OPR_GT, OPR_GE,
|
||||
OPR_AND, OPR_OR,
|
||||
OPR_NOBINOPR
|
||||
} BinOpr;
|
||||
|
||||
|
||||
typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
|
||||
|
||||
|
||||
#define getcode(fs,e) ((fs)->f->code[(e)->u.s.info])
|
||||
|
||||
#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
|
||||
|
||||
#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET)
|
||||
|
||||
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
|
||||
LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
|
||||
LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
|
||||
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
|
||||
LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
|
||||
LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
|
||||
LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s);
|
||||
LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r);
|
||||
LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
|
||||
LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
|
||||
LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
|
||||
LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
|
||||
LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC int luaK_jump (FuncState *fs);
|
||||
LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
|
||||
LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
|
||||
LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
|
||||
LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
|
||||
LUAI_FUNC int luaK_getlabel (FuncState *fs);
|
||||
LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v);
|
||||
LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
|
||||
LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
|
||||
LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
|
||||
|
||||
|
||||
#endif
|
||||
508
ldblib.c
508
ldblib.c
@@ -1,217 +1,397 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.4 1999/02/04 17:47:59 roberto Exp roberto $
|
||||
** $Id: ldblib.c,v 1.103 2005/11/01 16:08:32 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#define ldblib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static void settabss (lua_Object t, char *i, char *v) {
|
||||
lua_pushobject(t);
|
||||
lua_pushstring(i);
|
||||
lua_pushstring(v);
|
||||
lua_settable();
|
||||
static int db_getregistry (lua_State *L) {
|
||||
lua_pushvalue(L, LUA_REGISTRYINDEX);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void settabsi (lua_Object t, char *i, int v) {
|
||||
lua_pushobject(t);
|
||||
lua_pushstring(i);
|
||||
lua_pushnumber(v);
|
||||
lua_settable();
|
||||
}
|
||||
|
||||
|
||||
static lua_Object getfuncinfo (lua_Object func) {
|
||||
lua_Object result = lua_createtable();
|
||||
char *str;
|
||||
int line;
|
||||
lua_funcinfo(func, &str, &line);
|
||||
if (line == -1) /* C function? */
|
||||
settabss(result, "kind", "C");
|
||||
else if (line == 0) { /* "main"? */
|
||||
settabss(result, "kind", "chunk");
|
||||
settabss(result, "source", str);
|
||||
}
|
||||
else { /* Lua function */
|
||||
settabss(result, "kind", "Lua");
|
||||
settabsi(result, "def_line", line);
|
||||
settabss(result, "source", str);
|
||||
static int db_getmetatable (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
if (!lua_getmetatable(L, 1)) {
|
||||
lua_pushnil(L); /* no metatable */
|
||||
}
|
||||
if (line != 0) { /* is it not a "main"? */
|
||||
char *kind = lua_getobjname(func, &str);
|
||||
if (*kind) {
|
||||
settabss(result, "name", str);
|
||||
settabss(result, "where", kind);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void getstack (void) {
|
||||
lua_Object func = lua_stackedfunction(luaL_check_int(1));
|
||||
if (func == LUA_NOOBJECT) /* level out of range? */
|
||||
return;
|
||||
else {
|
||||
lua_Object result = getfuncinfo(func);
|
||||
int currline = lua_currentline(func);
|
||||
if (currline > 0)
|
||||
settabsi(result, "current", currline);
|
||||
lua_pushobject(result);
|
||||
lua_pushstring("func");
|
||||
lua_pushobject(func);
|
||||
lua_settable(); /* result.func = func */
|
||||
lua_pushobject(result);
|
||||
}
|
||||
static int db_setmetatable (lua_State *L) {
|
||||
int t = lua_type(L, 2);
|
||||
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
|
||||
"nil or table expected");
|
||||
lua_settop(L, 2);
|
||||
lua_pushboolean(L, lua_setmetatable(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void funcinfo (void) {
|
||||
lua_pushobject(getfuncinfo(luaL_functionarg(1)));
|
||||
static int db_getfenv (lua_State *L) {
|
||||
lua_getfenv(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int findlocal (lua_Object func, int arg) {
|
||||
lua_Object v = lua_getparam(arg);
|
||||
if (lua_isnumber(v))
|
||||
return (int)lua_getnumber(v);
|
||||
else {
|
||||
char *name = luaL_check_string(arg);
|
||||
int i = 0;
|
||||
int result = -1;
|
||||
char *vname;
|
||||
while (lua_getlocal(func, ++i, &vname) != LUA_NOOBJECT) {
|
||||
if (strcmp(name, vname) == 0)
|
||||
result = i; /* keep looping to get the last var with this name */
|
||||
}
|
||||
if (result == -1)
|
||||
luaL_verror("no local variable `%.50s' at given level", name);
|
||||
return result;
|
||||
}
|
||||
static int db_setfenv (lua_State *L) {
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
lua_settop(L, 2);
|
||||
if (lua_setfenv(L, 1) == 0)
|
||||
luaL_error(L, LUA_QL("setfenv")
|
||||
" cannot change environment of given object");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void getlocal (void) {
|
||||
lua_Object func = lua_stackedfunction(luaL_check_int(1));
|
||||
lua_Object val;
|
||||
char *name;
|
||||
if (func == LUA_NOOBJECT) /* level out of range? */
|
||||
return; /* return nil */
|
||||
else if (lua_getparam(2) != LUA_NOOBJECT) { /* 2nd argument? */
|
||||
if ((val = lua_getlocal(func, findlocal(func, 2), &name)) != LUA_NOOBJECT) {
|
||||
lua_pushobject(val);
|
||||
lua_pushstring(name);
|
||||
}
|
||||
/* else return nil */
|
||||
}
|
||||
else { /* collect all locals in a table */
|
||||
lua_Object result = lua_createtable();
|
||||
int i;
|
||||
for (i=1; ;i++) {
|
||||
if ((val = lua_getlocal(func, i, &name)) == LUA_NOOBJECT)
|
||||
break;
|
||||
lua_pushobject(result);
|
||||
lua_pushstring(name);
|
||||
lua_pushobject(val);
|
||||
lua_settable(); /* result[name] = value */
|
||||
}
|
||||
lua_pushobject(result);
|
||||
}
|
||||
static void settabss (lua_State *L, const char *i, const char *v) {
|
||||
lua_pushstring(L, v);
|
||||
lua_setfield(L, -2, i);
|
||||
}
|
||||
|
||||
|
||||
static void setlocal (void) {
|
||||
lua_Object func = lua_stackedfunction(luaL_check_int(1));
|
||||
int numvar;
|
||||
luaL_arg_check(func != LUA_NOOBJECT, 1, "level out of range");
|
||||
numvar = findlocal(func, 2);
|
||||
lua_pushobject(luaL_nonnullarg(3));
|
||||
if (!lua_setlocal(func, numvar))
|
||||
lua_error("no such local variable");
|
||||
static void settabsi (lua_State *L, const char *i, int v) {
|
||||
lua_pushinteger(L, v);
|
||||
lua_setfield(L, -2, i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int linehook = -1; /* Lua reference to line hook function */
|
||||
static int callhook = -1; /* Lua reference to call hook function */
|
||||
|
||||
|
||||
static void dohook (int ref) {
|
||||
lua_LHFunction oldlinehook = lua_setlinehook(NULL);
|
||||
lua_CHFunction oldcallhook = lua_setcallhook(NULL);
|
||||
lua_callfunction(lua_getref(ref));
|
||||
lua_setlinehook(oldlinehook);
|
||||
lua_setcallhook(oldcallhook);
|
||||
}
|
||||
|
||||
|
||||
static void linef (int line) {
|
||||
lua_pushnumber(line);
|
||||
dohook(linehook);
|
||||
}
|
||||
|
||||
|
||||
static void callf (lua_Function func, char *file, int line) {
|
||||
if (func != LUA_NOOBJECT) {
|
||||
lua_pushobject(func);
|
||||
lua_pushstring(file);
|
||||
lua_pushnumber(line);
|
||||
}
|
||||
dohook(callhook);
|
||||
}
|
||||
|
||||
|
||||
static void setcallhook (void) {
|
||||
lua_Object f = lua_getparam(1);
|
||||
lua_unref(callhook);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
callhook = -1;
|
||||
lua_setcallhook(NULL);
|
||||
static lua_State *getthread (lua_State *L, int *arg) {
|
||||
if (lua_isthread(L, 1)) {
|
||||
*arg = 1;
|
||||
return lua_tothread(L, 1);
|
||||
}
|
||||
else {
|
||||
lua_pushobject(f);
|
||||
callhook = lua_ref(1);
|
||||
lua_setcallhook(callf);
|
||||
*arg = 0;
|
||||
return L;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void setlinehook (void) {
|
||||
lua_Object f = lua_getparam(1);
|
||||
lua_unref(linehook);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
linehook = -1;
|
||||
lua_setlinehook(NULL);
|
||||
static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
|
||||
if (L == L1) {
|
||||
lua_pushvalue(L, -2);
|
||||
lua_remove(L, -3);
|
||||
}
|
||||
else
|
||||
lua_xmove(L1, L, 1);
|
||||
lua_setfield(L, -2, fname);
|
||||
}
|
||||
|
||||
|
||||
static int db_getinfo (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
int arg;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
const char *options = luaL_optstring(L, arg+2, "flnSu");
|
||||
if (lua_isnumber(L, arg+1)) {
|
||||
if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
|
||||
lua_pushnil(L); /* level out of range */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (lua_isfunction(L, arg+1)) {
|
||||
lua_pushfstring(L, ">%s", options);
|
||||
options = lua_tostring(L, -1);
|
||||
lua_pushvalue(L, arg+1);
|
||||
lua_xmove(L, L1, 1);
|
||||
}
|
||||
else
|
||||
return luaL_argerror(L, arg+1, "function or level expected");
|
||||
if (!lua_getinfo(L1, options, &ar))
|
||||
return luaL_argerror(L, arg+2, "invalid option");
|
||||
lua_createtable(L, 0, 2);
|
||||
if (strchr(options, 'S')) {
|
||||
settabss(L, "source", ar.source);
|
||||
settabss(L, "short_src", ar.short_src);
|
||||
settabsi(L, "linedefined", ar.linedefined);
|
||||
settabsi(L, "lastlinedefined", ar.lastlinedefined);
|
||||
settabss(L, "what", ar.what);
|
||||
}
|
||||
if (strchr(options, 'l'))
|
||||
settabsi(L, "currentline", ar.currentline);
|
||||
if (strchr(options, 'u'))
|
||||
settabsi(L, "nups", ar.nups);
|
||||
if (strchr(options, 'n')) {
|
||||
settabss(L, "name", ar.name);
|
||||
settabss(L, "namewhat", ar.namewhat);
|
||||
}
|
||||
if (strchr(options, 'L'))
|
||||
treatstackoption(L, L1, "activelines");
|
||||
if (strchr(options, 'f'))
|
||||
treatstackoption(L, L1, "func");
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
|
||||
static int db_getlocal (lua_State *L) {
|
||||
int arg;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
lua_Debug ar;
|
||||
const char *name;
|
||||
if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
|
||||
return luaL_argerror(L, arg+1, "level out of range");
|
||||
name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
|
||||
if (name) {
|
||||
lua_xmove(L1, L, 1);
|
||||
lua_pushstring(L, name);
|
||||
lua_pushvalue(L, -2);
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
lua_pushobject(f);
|
||||
linehook = lua_ref(1);
|
||||
lua_setlinehook(linef);
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static struct luaL_reg dblib[] = {
|
||||
{"funcinfo", funcinfo},
|
||||
{"getlocal", getlocal},
|
||||
{"getstack", getstack},
|
||||
{"setcallhook", setcallhook},
|
||||
{"setlinehook", setlinehook},
|
||||
{"setlocal", setlocal}
|
||||
static int db_setlocal (lua_State *L) {
|
||||
int arg;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
lua_Debug ar;
|
||||
if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
|
||||
return luaL_argerror(L, arg+1, "level out of range");
|
||||
luaL_checkany(L, arg+3);
|
||||
lua_settop(L, arg+3);
|
||||
lua_xmove(L, L1, 1);
|
||||
lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+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 db_getupvalue (lua_State *L) {
|
||||
return auxupvalue(L, 1);
|
||||
}
|
||||
|
||||
|
||||
static int db_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);
|
||||
lua_pushlightuserdata(L, L);
|
||||
lua_rawget(L, -2);
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushstring(L, hooknames[(int)ar->event]);
|
||||
if (ar->currentline >= 0)
|
||||
lua_pushinteger(L, ar->currentline);
|
||||
else lua_pushnil(L);
|
||||
lua_assert(lua_getinfo(L, "lS", ar));
|
||||
lua_call(L, 2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 void gethooktable (lua_State *L) {
|
||||
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
if (!lua_istable(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
lua_createtable(L, 0, 1);
|
||||
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int db_sethook (lua_State *L) {
|
||||
int arg;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
if (lua_isnoneornil(L, arg+1)) {
|
||||
lua_settop(L, arg+1);
|
||||
lua_sethook(L1, NULL, 0, 0); /* turn off hooks */
|
||||
}
|
||||
else {
|
||||
const char *smask = luaL_checkstring(L, arg+2);
|
||||
int count = luaL_optint(L, arg+3, 0);
|
||||
luaL_checktype(L, arg+1, LUA_TFUNCTION);
|
||||
lua_sethook(L1, hookf, makemask(smask, count), count);
|
||||
}
|
||||
gethooktable(L1);
|
||||
lua_pushlightuserdata(L1, L1);
|
||||
lua_pushvalue(L, arg+1);
|
||||
lua_xmove(L, L1, 1);
|
||||
lua_rawset(L1, -3); /* set new hook */
|
||||
lua_pop(L1, 1); /* remove hook table */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int db_gethook (lua_State *L) {
|
||||
int arg;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
char buff[5];
|
||||
int mask = lua_gethookmask(L1);
|
||||
lua_Hook hook = lua_gethook(L1);
|
||||
if (hook != NULL && hook != hookf) /* external hook? */
|
||||
lua_pushliteral(L, "external hook");
|
||||
else {
|
||||
gethooktable(L1);
|
||||
lua_pushlightuserdata(L1, L1);
|
||||
lua_rawget(L1, -2); /* get hook */
|
||||
lua_remove(L1, -2); /* remove hook table */
|
||||
lua_xmove(L1, L, 1);
|
||||
}
|
||||
lua_pushstring(L, unmakemask(mask, buff));
|
||||
lua_pushinteger(L, lua_gethookcount(L1));
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
static int db_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;
|
||||
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
||||
lua_pcall(L, 0, 0, 0)) {
|
||||
fputs(lua_tostring(L, -1), stderr);
|
||||
fputs("\n", stderr);
|
||||
}
|
||||
lua_settop(L, 0); /* remove eventual returns */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define LEVELS1 12 /* size of the first part of the stack */
|
||||
#define LEVELS2 10 /* size of the second part of the stack */
|
||||
|
||||
static int db_errorfb (lua_State *L) {
|
||||
int level;
|
||||
int firstpart = 1; /* still before eventual `...' */
|
||||
int arg;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
lua_Debug ar;
|
||||
if (lua_isnumber(L, arg+2)) {
|
||||
level = (int)lua_tointeger(L, arg+2);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
else
|
||||
level = (L == L1) ? 1 : 0; /* level 0 may be this own function */
|
||||
if (lua_gettop(L) == arg)
|
||||
lua_pushliteral(L, "");
|
||||
else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
|
||||
else lua_pushliteral(L, "\n");
|
||||
lua_pushliteral(L, "stack traceback:");
|
||||
while (lua_getstack(L1, level++, &ar)) {
|
||||
if (level > LEVELS1 && firstpart) {
|
||||
/* no more than `LEVELS2' more levels? */
|
||||
if (!lua_getstack(L1, level+LEVELS2, &ar))
|
||||
level--; /* keep going */
|
||||
else {
|
||||
lua_pushliteral(L, "\n\t..."); /* too many levels */
|
||||
while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
|
||||
level++;
|
||||
}
|
||||
firstpart = 0;
|
||||
continue;
|
||||
}
|
||||
lua_pushliteral(L, "\n\t");
|
||||
lua_getinfo(L1, "Snl", &ar);
|
||||
lua_pushfstring(L, "%s:", ar.short_src);
|
||||
if (ar.currentline > 0)
|
||||
lua_pushfstring(L, "%d:", ar.currentline);
|
||||
if (*ar.namewhat != '\0') /* is there a name? */
|
||||
lua_pushfstring(L, " in function " LUA_QS, ar.name);
|
||||
else {
|
||||
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) - arg);
|
||||
}
|
||||
lua_concat(L, lua_gettop(L) - arg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_Reg dblib[] = {
|
||||
{"debug", db_debug},
|
||||
{"getfenv", db_getfenv},
|
||||
{"gethook", db_gethook},
|
||||
{"getinfo", db_getinfo},
|
||||
{"getlocal", db_getlocal},
|
||||
{"getregistry", db_getregistry},
|
||||
{"getmetatable", db_getmetatable},
|
||||
{"getupvalue", db_getupvalue},
|
||||
{"setfenv", db_setfenv},
|
||||
{"sethook", db_sethook},
|
||||
{"setlocal", db_setlocal},
|
||||
{"setmetatable", db_setmetatable},
|
||||
{"setupvalue", db_setupvalue},
|
||||
{"traceback", db_errorfb},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
void lua_dblibopen (void) {
|
||||
luaL_openlib(dblib, (sizeof(dblib)/sizeof(dblib[0])));
|
||||
LUALIB_API int luaopen_debug (lua_State *L) {
|
||||
luaL_register(L, LUA_DBLIBNAME, dblib);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
620
ldebug.c
Normal file
620
ldebug.c
Normal file
@@ -0,0 +1,620 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 2.28 2005/11/01 16:08:52 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define ldebug_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lcode.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
|
||||
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
|
||||
|
||||
|
||||
static int currentpc (lua_State *L, CallInfo *ci) {
|
||||
if (!isLua(ci)) return -1; /* function is not a Lua function? */
|
||||
if (ci == L->ci)
|
||||
ci->savedpc = L->savedpc;
|
||||
return pcRel(ci->savedpc, ci_func(ci)->l.p);
|
||||
}
|
||||
|
||||
|
||||
static int currentline (lua_State *L, CallInfo *ci) {
|
||||
int pc = currentpc(L, ci);
|
||||
if (pc < 0)
|
||||
return -1; /* only active lua functions have current-line information */
|
||||
else
|
||||
return getline(ci_func(ci)->l.p, pc);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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_byte(mask);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_Hook lua_gethook (lua_State *L) {
|
||||
return L->hook;
|
||||
}
|
||||
|
||||
|
||||
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 (f_isLua(ci)) /* Lua function? */
|
||||
level -= ci->tailcalls; /* skip lost tail calls */
|
||||
}
|
||||
if (level == 0 && ci > L->base_ci) { /* level found? */
|
||||
status = 1;
|
||||
ar->i_ci = cast_int(ci - L->base_ci);
|
||||
}
|
||||
else if (level < 0) { /* level is of a lost tail call? */
|
||||
status = 1;
|
||||
ar->i_ci = 0;
|
||||
}
|
||||
else status = 0; /* no such level */
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static Proto *getluaproto (CallInfo *ci) {
|
||||
return (isLua(ci) ? ci_func(ci)->l.p : NULL);
|
||||
}
|
||||
|
||||
|
||||
static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
|
||||
const char *name;
|
||||
Proto *fp = getluaproto(ci);
|
||||
if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
|
||||
return name; /* is a local variable in a Lua function */
|
||||
else {
|
||||
StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
|
||||
if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */
|
||||
return "(*temporary)";
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
CallInfo *ci = L->base_ci + ar->i_ci;
|
||||
const char *name = findlocal(L, ci, n);
|
||||
lua_lock(L);
|
||||
if (name)
|
||||
luaA_pushobject(L, ci->base + (n - 1));
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
CallInfo *ci = L->base_ci + ar->i_ci;
|
||||
const char *name = findlocal(L, ci, n);
|
||||
lua_lock(L);
|
||||
if (name)
|
||||
setobjs2s(L, ci->base + (n - 1), L->top - 1);
|
||||
L->top--; /* pop value */
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
static void funcinfo (lua_Debug *ar, Closure *cl) {
|
||||
if (cl->c.isC) {
|
||||
ar->source = "=[C]";
|
||||
ar->linedefined = -1;
|
||||
ar->lastlinedefined = -1;
|
||||
ar->what = "C";
|
||||
}
|
||||
else {
|
||||
ar->source = getstr(cl->l.p->source);
|
||||
ar->linedefined = cl->l.p->linedefined;
|
||||
ar->lastlinedefined = cl->l.p->lastlinedefined;
|
||||
ar->what = (ar->linedefined == 0) ? "main" : "Lua";
|
||||
}
|
||||
luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
|
||||
}
|
||||
|
||||
|
||||
static void info_tailcall (lua_Debug *ar) {
|
||||
ar->name = ar->namewhat = "";
|
||||
ar->what = "tail";
|
||||
ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
|
||||
ar->source = "=(tail call)";
|
||||
luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
|
||||
ar->nups = 0;
|
||||
}
|
||||
|
||||
|
||||
static void collectvalidlines (lua_State *L, Closure *f) {
|
||||
if (f == NULL || f->c.isC) {
|
||||
setnilvalue(L->top);
|
||||
}
|
||||
else {
|
||||
Table *t = luaH_new(L, 0, 0);
|
||||
int *lineinfo = f->l.p->lineinfo;
|
||||
int i;
|
||||
for (i=0; i<f->l.p->sizelineinfo; i++)
|
||||
setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
|
||||
sethvalue(L, L->top, t);
|
||||
}
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
|
||||
Closure *f, CallInfo *ci) {
|
||||
int status = 1;
|
||||
if (f == NULL) {
|
||||
info_tailcall(ar);
|
||||
return status;
|
||||
}
|
||||
for (; *what; what++) {
|
||||
switch (*what) {
|
||||
case 'S': {
|
||||
funcinfo(ar, f);
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
ar->currentline = (ci) ? currentline(L, ci) : -1;
|
||||
break;
|
||||
}
|
||||
case 'u': {
|
||||
ar->nups = f->c.nupvalues;
|
||||
break;
|
||||
}
|
||||
case 'n': {
|
||||
ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
|
||||
if (ar->namewhat == NULL) {
|
||||
ar->namewhat = ""; /* not found */
|
||||
ar->name = NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'L':
|
||||
case 'f': /* handled by lua_getinfo */
|
||||
break;
|
||||
default: status = 0; /* invalid option */
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
int status;
|
||||
Closure *f = NULL;
|
||||
CallInfo *ci = NULL;
|
||||
lua_lock(L);
|
||||
if (*what == '>') {
|
||||
StkId func = L->top - 1;
|
||||
luai_apicheck(L, ttisfunction(func));
|
||||
what++; /* skip the '>' */
|
||||
f = clvalue(func);
|
||||
L->top--; /* pop function */
|
||||
}
|
||||
else if (ar->i_ci != 0) { /* no tail call? */
|
||||
ci = L->base_ci + ar->i_ci;
|
||||
lua_assert(ttisfunction(ci->func));
|
||||
f = clvalue(ci->func);
|
||||
}
|
||||
status = auxgetinfo(L, what, ar, f, ci);
|
||||
if (strchr(what, 'f')) {
|
||||
if (f == NULL) setnilvalue(L->top);
|
||||
else setclvalue(L, L->top, f);
|
||||
incr_top(L);
|
||||
}
|
||||
if (strchr(what, 'L'))
|
||||
collectvalidlines(L, f);
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Symbolic Execution and code checker
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define check(x) if (!(x)) return 0;
|
||||
|
||||
#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
|
||||
|
||||
#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
|
||||
|
||||
|
||||
|
||||
static int precheck (const Proto *pt) {
|
||||
check(pt->maxstacksize <= MAXSTACK);
|
||||
lua_assert(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
|
||||
lua_assert(!(pt->is_vararg & VARARG_NEEDSARG) ||
|
||||
(pt->is_vararg & VARARG_HASARG));
|
||||
check(pt->sizeupvalues <= pt->nups);
|
||||
check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
|
||||
check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
|
||||
|
||||
int luaG_checkopenop (Instruction i) {
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_CALL:
|
||||
case OP_TAILCALL:
|
||||
case OP_RETURN:
|
||||
case OP_SETLIST: {
|
||||
check(GETARG_B(i) == 0);
|
||||
return 1;
|
||||
}
|
||||
default: return 0; /* invalid instruction after an open call */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
|
||||
switch (mode) {
|
||||
case OpArgN: check(r == 0); break;
|
||||
case OpArgU: break;
|
||||
case OpArgR: checkreg(pt, r); break;
|
||||
case OpArgK:
|
||||
check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static Instruction 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++) {
|
||||
Instruction i = pt->code[pc];
|
||||
OpCode op = GET_OPCODE(i);
|
||||
int a = GETARG_A(i);
|
||||
int b = 0;
|
||||
int c = 0;
|
||||
check(op < NUM_OPCODES);
|
||||
checkreg(pt, a);
|
||||
switch (getOpMode(op)) {
|
||||
case iABC: {
|
||||
b = GETARG_B(i);
|
||||
c = GETARG_C(i);
|
||||
check(checkArgMode(pt, b, getBMode(op)));
|
||||
check(checkArgMode(pt, c, getCMode(op)));
|
||||
break;
|
||||
}
|
||||
case iABx: {
|
||||
b = GETARG_Bx(i);
|
||||
if (getBMode(op) == OpArgK) check(b < pt->sizek);
|
||||
break;
|
||||
}
|
||||
case iAsBx: {
|
||||
b = GETARG_sBx(i);
|
||||
if (getBMode(op) == OpArgR) {
|
||||
int dest = pc+1+b;
|
||||
check(0 <= dest && dest < pt->sizecode);
|
||||
if (dest > 0) {
|
||||
/* cannot jump to a setlist count */
|
||||
Instruction d = pt->code[dest-1];
|
||||
check(!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (testAMode(op)) {
|
||||
if (a == reg) last = pc; /* change register `a' */
|
||||
}
|
||||
if (testTMode(op)) {
|
||||
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: {
|
||||
check(b < c); /* at least two operands */
|
||||
break;
|
||||
}
|
||||
case OP_TFORLOOP: {
|
||||
check(c >= 1); /* at least one result (control variable) */
|
||||
checkreg(pt, a+2+c); /* space for results */
|
||||
if (reg >= a+2) last = pc; /* affect all regs above its base */
|
||||
break;
|
||||
}
|
||||
case OP_FORLOOP:
|
||||
case OP_FORPREP:
|
||||
checkreg(pt, a+3);
|
||||
/* go through */
|
||||
case OP_JMP: {
|
||||
int dest = pc+1+b;
|
||||
/* 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: {
|
||||
if (b > 0) checkreg(pt, a + b);
|
||||
if (c == 0) pc++;
|
||||
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;
|
||||
}
|
||||
case OP_VARARG: {
|
||||
check((pt->is_vararg & VARARG_ISVARARG) &&
|
||||
!(pt->is_vararg & VARARG_NEEDSARG));
|
||||
b--;
|
||||
if (b == LUA_MULTRET) check(checkopenop(pt, pc));
|
||||
checkreg(pt, a+b-1);
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return pt->code[last];
|
||||
}
|
||||
|
||||
#undef check
|
||||
#undef checkjump
|
||||
#undef checkreg
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
int luaG_checkcode (const Proto *pt) {
|
||||
return (symbexec(pt, pt->sizecode, NO_REG) != 0);
|
||||
}
|
||||
|
||||
|
||||
static const char *kname (Proto *p, int c) {
|
||||
if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
|
||||
return svalue(&p->k[INDEXK(c)]);
|
||||
else
|
||||
return "?";
|
||||
}
|
||||
|
||||
|
||||
static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
|
||||
const char **name) {
|
||||
if (isLua(ci)) { /* a Lua function? */
|
||||
Proto *p = ci_func(ci)->l.p;
|
||||
int pc = currentpc(L, ci);
|
||||
Instruction i;
|
||||
*name = luaF_getlocalname(p, stackpos+1, pc);
|
||||
if (*name) /* is a local? */
|
||||
return "local";
|
||||
i = 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(L, 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_GETUPVAL: {
|
||||
int u = GETARG_B(i); /* upvalue index */
|
||||
*name = p->upvalues ? getstr(p->upvalues[u]) : "?";
|
||||
return "upvalue";
|
||||
}
|
||||
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 (lua_State *L, CallInfo *ci, const char **name) {
|
||||
Instruction i;
|
||||
if ((isLua(ci) && ci->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(L, ci)];
|
||||
if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
|
||||
GET_OPCODE(i) == OP_TFORLOOP)
|
||||
return getobjname(L, 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 TValue *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 TValue *o, const char *op) {
|
||||
const char *name = NULL;
|
||||
const char *t = luaT_typenames[ttype(o)];
|
||||
const char *kind = (isinstack(L->ci, o)) ?
|
||||
getobjname(L, L->ci, cast_int(o - L->base), &name) :
|
||||
NULL;
|
||||
if (kind)
|
||||
luaG_runerror(L, "attempt to %s %s " LUA_QS " (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 TValue *p1, const TValue *p2) {
|
||||
TValue 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 TValue *p1, const TValue *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(L, 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, L->top, L->top - 1); /* move argument */
|
||||
setobjs2s(L, L->top - 1, errfunc); /* push function */
|
||||
incr_top(L);
|
||||
luaD_call(L, L->top - 2, 1); /* call it */
|
||||
}
|
||||
luaD_throw(L, LUA_ERRRUN);
|
||||
}
|
||||
|
||||
|
||||
void luaG_runerror (lua_State *L, const char *fmt, ...) {
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
addinfo(L, luaO_pushvfstring(L, fmt, argp));
|
||||
va_end(argp);
|
||||
luaG_errormsg(L);
|
||||
}
|
||||
|
||||
33
ldebug.h
Normal file
33
ldebug.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
** $Id: ldebug.h,v 2.2 2004/06/02 19:07:55 roberto Exp roberto $
|
||||
** Auxiliary functions from Debug Interface module
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ldebug_h
|
||||
#define ldebug_h
|
||||
|
||||
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
|
||||
|
||||
#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0)
|
||||
|
||||
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
||||
|
||||
|
||||
LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o,
|
||||
const char *opname);
|
||||
LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2);
|
||||
LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...);
|
||||
LUAI_FUNC void luaG_errormsg (lua_State *L);
|
||||
LUAI_FUNC int luaG_checkcode (const Proto *pt);
|
||||
LUAI_FUNC int luaG_checkopenop (Instruction i);
|
||||
|
||||
#endif
|
||||
757
ldo.c
757
ldo.c
@@ -1,385 +1,516 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 1.44 1999/06/17 17:04:03 roberto Exp roberto $
|
||||
** $Id: ldo.c,v 2.37 2005/12/22 16:19:56 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>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#define ldo_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
#ifndef STACK_LIMIT
|
||||
#define STACK_LIMIT 6000 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Error-recovery functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#define STACK_UNIT 128
|
||||
/* chain list of long jump buffers */
|
||||
struct lua_longjmp {
|
||||
struct lua_longjmp *previous;
|
||||
luai_jmpbuf b;
|
||||
volatile int status; /* error code */
|
||||
};
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
#undef STACK_UNIT
|
||||
#define STACK_UNIT 2
|
||||
#endif
|
||||
|
||||
|
||||
void luaD_init (void) {
|
||||
L->stack.stack = luaM_newvector(STACK_UNIT, TObject);
|
||||
L->stack.top = L->stack.stack;
|
||||
L->stack.last = L->stack.stack+(STACK_UNIT-1);
|
||||
void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
|
||||
switch (errcode) {
|
||||
case LUA_ERRMEM: {
|
||||
setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));
|
||||
break;
|
||||
}
|
||||
case LUA_ERRERR: {
|
||||
setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
|
||||
break;
|
||||
}
|
||||
case LUA_ERRSYNTAX:
|
||||
case LUA_ERRRUN: {
|
||||
setobjs2s(L, oldtop, L->top - 1); /* error message on current top */
|
||||
break;
|
||||
}
|
||||
}
|
||||
L->top = oldtop + 1;
|
||||
}
|
||||
|
||||
|
||||
void luaD_checkstack (int n) {
|
||||
struct Stack *S = &L->stack;
|
||||
if (S->last-S->top <= n) {
|
||||
StkId top = S->top-S->stack;
|
||||
int stacksize = (S->last-S->stack)+STACK_UNIT+n;
|
||||
luaM_reallocvector(S->stack, stacksize, TObject);
|
||||
S->last = S->stack+(stacksize-1);
|
||||
S->top = S->stack + top;
|
||||
if (stacksize >= STACK_LIMIT) { /* stack overflow? */
|
||||
if (lua_stackedfunction(100) == LUA_NOOBJECT) /* 100 funcs on stack? */
|
||||
lua_error("Lua2C - C2Lua overflow"); /* doesn't look like a rec. loop */
|
||||
else
|
||||
lua_error("stack size overflow");
|
||||
}
|
||||
static void restore_stack_limit (lua_State *L) {
|
||||
lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
|
||||
if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */
|
||||
int inuse = cast_int(L->ci - L->base_ci);
|
||||
if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */
|
||||
luaD_reallocCI(L, LUAI_MAXCALLS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Adjust stack. Set top to the given value, pushing NILs if needed.
|
||||
*/
|
||||
void luaD_adjusttop (StkId newtop) {
|
||||
int diff = newtop-(L->stack.top-L->stack.stack);
|
||||
if (diff <= 0)
|
||||
L->stack.top += diff;
|
||||
static void resetstack (lua_State *L, int status) {
|
||||
L->ci = L->base_ci;
|
||||
L->base = L->ci->base;
|
||||
luaF_close(L, L->base); /* close eventual pending closures */
|
||||
luaD_seterrorobj(L, status, L->base);
|
||||
L->nCcalls = 0;
|
||||
L->allowhook = 1;
|
||||
restore_stack_limit(L);
|
||||
L->errfunc = 0;
|
||||
L->errorJmp = NULL;
|
||||
}
|
||||
|
||||
|
||||
void luaD_throw (lua_State *L, int errcode) {
|
||||
if (L->errorJmp) {
|
||||
L->errorJmp->status = errcode;
|
||||
LUAI_THROW(L, L->errorJmp);
|
||||
}
|
||||
else {
|
||||
luaD_checkstack(diff);
|
||||
while (diff--)
|
||||
ttype(L->stack.top++) = LUA_T_NIL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a hole below "nelems" from the L->stack.top.
|
||||
*/
|
||||
void luaD_openstack (int nelems) {
|
||||
luaO_memup(L->stack.top-nelems+1, L->stack.top-nelems,
|
||||
nelems*sizeof(TObject));
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
void luaD_lineHook (int line) {
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
|
||||
L->Cstack.num = 0;
|
||||
(*L->linehook)(line);
|
||||
L->stack.top = L->stack.stack+old_top;
|
||||
L->Cstack = oldCLS;
|
||||
}
|
||||
|
||||
|
||||
void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn) {
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
|
||||
L->Cstack.num = 0;
|
||||
if (isreturn)
|
||||
(*L->callhook)(LUA_NOOBJECT, "(return)", 0);
|
||||
else {
|
||||
TObject *f = L->stack.stack+base-1;
|
||||
if (tf)
|
||||
(*L->callhook)(Ref(f), tf->source->str, tf->lineDefined);
|
||||
else
|
||||
(*L->callhook)(Ref(f), "(C)", -1);
|
||||
}
|
||||
L->stack.top = L->stack.stack+old_top;
|
||||
L->Cstack = oldCLS;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call a C function.
|
||||
** Cstack.num is the number of arguments; Cstack.lua2C points to the
|
||||
** first argument. Returns an index to the first result from C.
|
||||
*/
|
||||
static StkId callC (lua_CFunction f, StkId base) {
|
||||
struct C_Lua_Stack *cls = &L->Cstack;
|
||||
struct C_Lua_Stack oldCLS = *cls;
|
||||
StkId firstResult;
|
||||
int numarg = (L->stack.top-L->stack.stack) - base;
|
||||
cls->num = numarg;
|
||||
cls->lua2C = base;
|
||||
cls->base = base+numarg; /* == top-stack */
|
||||
if (L->callhook)
|
||||
luaD_callHook(base, NULL, 0);
|
||||
(*f)(); /* do the actual call */
|
||||
if (L->callhook) /* func may have changed callhook */
|
||||
luaD_callHook(base, NULL, 1);
|
||||
firstResult = cls->base;
|
||||
*cls = oldCLS;
|
||||
return firstResult;
|
||||
}
|
||||
|
||||
|
||||
static StkId callCclosure (struct Closure *cl, lua_CFunction f, StkId base) {
|
||||
TObject *pbase;
|
||||
int nup = cl->nelems; /* number of upvalues */
|
||||
luaD_checkstack(nup);
|
||||
pbase = L->stack.stack+base; /* care: previous call may change this */
|
||||
/* open space for upvalues as extra arguments */
|
||||
luaO_memup(pbase+nup, pbase, (L->stack.top-pbase)*sizeof(TObject));
|
||||
/* copy upvalues into stack */
|
||||
memcpy(pbase, cl->consts+1, nup*sizeof(TObject));
|
||||
L->stack.top += nup;
|
||||
return callC(f, base);
|
||||
}
|
||||
|
||||
|
||||
void luaD_callTM (TObject *f, int nParams, int nResults) {
|
||||
luaD_openstack(nParams);
|
||||
*(L->stack.top-nParams-1) = *f;
|
||||
luaD_calln(nParams, nResults);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call a function (C or Lua). The parameters must be on the stack,
|
||||
** between [top-nArgs,top). The function to be called is right below the
|
||||
** arguments.
|
||||
** When returns, the results are on the stack, between [top-nArgs-1,top).
|
||||
** The number of results is nResults, unless nResults=MULT_RET.
|
||||
*/
|
||||
void luaD_calln (int nArgs, int nResults) {
|
||||
struct Stack *S = &L->stack; /* to optimize */
|
||||
StkId base = (S->top-S->stack)-nArgs;
|
||||
TObject *func = S->stack+base-1;
|
||||
StkId firstResult;
|
||||
int i;
|
||||
switch (ttype(func)) {
|
||||
case LUA_T_CPROTO:
|
||||
ttype(func) = LUA_T_CMARK;
|
||||
firstResult = callC(fvalue(func), base);
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
ttype(func) = LUA_T_PMARK;
|
||||
firstResult = luaV_execute(NULL, tfvalue(func), base);
|
||||
break;
|
||||
case LUA_T_CLOSURE: {
|
||||
Closure *c = clvalue(func);
|
||||
TObject *proto = &(c->consts[0]);
|
||||
ttype(func) = LUA_T_CLMARK;
|
||||
firstResult = (ttype(proto) == LUA_T_CPROTO) ?
|
||||
callCclosure(c, fvalue(proto), base) :
|
||||
luaV_execute(c, tfvalue(proto), base);
|
||||
break;
|
||||
}
|
||||
default: { /* func is not a function */
|
||||
/* Check the tag method for invalid functions */
|
||||
TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error("call expression not a function");
|
||||
luaD_callTM(im, (S->top-S->stack)-(base-1), nResults);
|
||||
return;
|
||||
L->status = cast_byte(errcode);
|
||||
if (G(L)->panic) {
|
||||
resetstack(L, errcode);
|
||||
lua_unlock(L);
|
||||
G(L)->panic(L);
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/* adjust the number of results */
|
||||
if (nResults == MULT_RET)
|
||||
nResults = (S->top-S->stack)-firstResult;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
LUAI_TRY(L, &lj,
|
||||
(*f)(L, ud);
|
||||
);
|
||||
L->errorJmp = lj.previous; /* restore old error handler */
|
||||
return lj.status;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static void correctstack (lua_State *L, TValue *oldstack) {
|
||||
CallInfo *ci;
|
||||
GCObject *up;
|
||||
L->top = (L->top - oldstack) + L->stack;
|
||||
for (up = L->openupval; up != NULL; up = up->gch.next)
|
||||
gco2uv(up)->v = (gco2uv(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;
|
||||
ci->func = (ci->func - oldstack) + L->stack;
|
||||
}
|
||||
L->base = (L->base - oldstack) + L->stack;
|
||||
}
|
||||
|
||||
|
||||
void luaD_reallocstack (lua_State *L, int newsize) {
|
||||
TValue *oldstack = L->stack;
|
||||
int realsize = newsize + 1 + EXTRA_STACK;
|
||||
lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
|
||||
luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
|
||||
L->stacksize = realsize;
|
||||
L->stack_last = L->stack+newsize;
|
||||
correctstack(L, oldstack);
|
||||
}
|
||||
|
||||
|
||||
void luaD_reallocCI (lua_State *L, int newsize) {
|
||||
CallInfo *oldci = L->base_ci;
|
||||
luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
|
||||
L->size_ci = newsize;
|
||||
L->ci = (L->ci - oldci) + L->base_ci;
|
||||
L->end_ci = L->base_ci + L->size_ci - 1;
|
||||
}
|
||||
|
||||
|
||||
void luaD_growstack (lua_State *L, int n) {
|
||||
if (n <= L->stacksize) /* double size is enough? */
|
||||
luaD_reallocstack(L, 2*L->stacksize);
|
||||
else
|
||||
luaD_adjusttop(firstResult+nResults);
|
||||
/* move results to base-1 (to erase parameters and function) */
|
||||
base--;
|
||||
for (i=0; i<nResults; i++)
|
||||
*(S->stack+base+i) = *(S->stack+firstResult+i);
|
||||
S->top -= firstResult-base;
|
||||
luaD_reallocstack(L, L->stacksize + n);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Traverse all objects on L->stack.stack
|
||||
*/
|
||||
void luaD_travstack (int (*fn)(TObject *))
|
||||
{
|
||||
StkId i;
|
||||
for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
|
||||
fn(L->stack.stack+i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void message (char *s) {
|
||||
TObject *em = &(luaS_new("_ERRORMESSAGE")->u.s.globalval);
|
||||
if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO ||
|
||||
ttype(em) == LUA_T_CLOSURE) {
|
||||
*L->stack.top = *em;
|
||||
incr_top;
|
||||
lua_pushstring(s);
|
||||
luaD_calln(1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Reports an error, and jumps up to the available recover label
|
||||
*/
|
||||
void lua_error (char *s) {
|
||||
if (s) message(s);
|
||||
if (L->errorJmp)
|
||||
longjmp(L->errorJmp->b, 1);
|
||||
static CallInfo *growCI (lua_State *L) {
|
||||
if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */
|
||||
luaD_throw(L, LUA_ERRERR);
|
||||
else {
|
||||
message("exit(1). Unable to recover.\n");
|
||||
exit(1);
|
||||
luaD_reallocCI(L, 2*L->size_ci);
|
||||
if (L->size_ci > LUAI_MAXCALLS)
|
||||
luaG_runerror(L, "stack overflow");
|
||||
}
|
||||
return ++L->ci;
|
||||
}
|
||||
|
||||
|
||||
void luaD_callhook (lua_State *L, int event, int line) {
|
||||
lua_Hook hook = L->hook;
|
||||
if (hook && L->allowhook) {
|
||||
ptrdiff_t top = savestack(L, L->top);
|
||||
ptrdiff_t ci_top = savestack(L, L->ci->top);
|
||||
lua_Debug ar;
|
||||
ar.event = event;
|
||||
ar.currentline = line;
|
||||
if (event == LUA_HOOKTAILRET)
|
||||
ar.i_ci = 0; /* tail call; no debug information about it */
|
||||
else
|
||||
ar.i_ci = cast_int(L->ci - L->base_ci);
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
L->ci->top = L->top + LUA_MINSTACK;
|
||||
lua_assert(L->ci->top <= L->stack_last);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected call. Assumes that function is at L->Cstack.base and
|
||||
** parameters are on top of it. Leave nResults on the stack.
|
||||
*/
|
||||
int luaD_protectedrun (void) {
|
||||
volatile struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
struct lua_longjmp myErrorJmp;
|
||||
volatile int status;
|
||||
struct lua_longjmp *volatile oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp.b) == 0) {
|
||||
StkId base = L->Cstack.base;
|
||||
luaD_calln((L->stack.top-L->stack.stack)-base-1, MULT_RET);
|
||||
L->Cstack.lua2C = base; /* position of the new results */
|
||||
L->Cstack.num = (L->stack.top-L->stack.stack) - base;
|
||||
L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
|
||||
status = 0;
|
||||
static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
|
||||
int i;
|
||||
int nfixargs = p->numparams;
|
||||
Table *htab = NULL;
|
||||
StkId base, fixed;
|
||||
for (; actual < nfixargs; ++actual)
|
||||
setnilvalue(L->top++);
|
||||
#if defined(LUA_COMPAT_VARARG)
|
||||
if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
|
||||
int nvar = actual - nfixargs; /* number of extra arguments */
|
||||
lua_assert(p->is_vararg & VARARG_HASARG);
|
||||
luaC_checkGC(L);
|
||||
htab = luaH_new(L, nvar, 1); /* create `arg' table */
|
||||
for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
|
||||
setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
|
||||
/* store counter in field `n' */
|
||||
setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
|
||||
}
|
||||
else { /* an error occurred: restore L->Cstack and L->stack.top */
|
||||
L->Cstack = oldCLS;
|
||||
L->stack.top = L->stack.stack+L->Cstack.base;
|
||||
status = 1;
|
||||
#endif
|
||||
/* move fixed parameters to final position */
|
||||
fixed = L->top - actual; /* first fixed argument */
|
||||
base = L->top; /* final position of first argument */
|
||||
for (i=0; i<nfixargs; i++) {
|
||||
setobjs2s(L, L->top++, fixed+i);
|
||||
setnilvalue(fixed+i);
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
return status;
|
||||
/* add `arg' parameter */
|
||||
if (htab) {
|
||||
sethvalue(L, L->top++, htab);
|
||||
lua_assert(iswhite(obj2gco(htab)));
|
||||
}
|
||||
return base;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
|
||||
*/
|
||||
static int protectedparser (ZIO *z, int bin) {
|
||||
volatile struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
struct lua_longjmp myErrorJmp;
|
||||
volatile int status;
|
||||
TProtoFunc *volatile tf;
|
||||
struct lua_longjmp *volatile oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp.b) == 0) {
|
||||
tf = bin ? luaU_undump1(z) : luaY_parser(z);
|
||||
status = 0;
|
||||
}
|
||||
else { /* an error occurred: restore L->Cstack and L->stack.top */
|
||||
L->Cstack = oldCLS;
|
||||
L->stack.top = L->stack.stack+L->Cstack.base;
|
||||
tf = NULL;
|
||||
status = 1;
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
if (status) return 1; /* error code */
|
||||
if (tf == NULL) return 2; /* 'natural' end */
|
||||
luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */
|
||||
L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
|
||||
L->stack.stack[L->Cstack.base].value.tf = tf;
|
||||
luaV_closure(0);
|
||||
return 0;
|
||||
static StkId tryfuncTM (lua_State *L, StkId func) {
|
||||
const TValue *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(L, p, p-1);
|
||||
incr_top(L);
|
||||
func = restorestack(L, funcr); /* previous call may change stack */
|
||||
setobj2s(L, func, tm); /* tag method is the new function to be called */
|
||||
return func;
|
||||
}
|
||||
|
||||
|
||||
static int do_main (ZIO *z, int bin) {
|
||||
int status;
|
||||
int debug = L->debug; /* save debug status */
|
||||
do {
|
||||
long old_blocks = (luaC_checkGC(), L->nblocks);
|
||||
status = protectedparser(z, bin);
|
||||
if (status == 1) return 1; /* error */
|
||||
else if (status == 2) return 0; /* 'natural' end */
|
||||
else {
|
||||
unsigned long newelems2 = 2*(L->nblocks-old_blocks);
|
||||
L->GCthreshold += newelems2;
|
||||
status = luaD_protectedrun();
|
||||
L->GCthreshold -= newelems2;
|
||||
|
||||
#define inc_ci(L) \
|
||||
((L->ci == L->end_ci) ? growCI(L) : \
|
||||
(condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
|
||||
|
||||
|
||||
int luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||
LClosure *cl;
|
||||
ptrdiff_t funcr;
|
||||
if (!ttisfunction(func)) /* `func' is not a function? */
|
||||
func = tryfuncTM(L, func); /* check the `function' tag method */
|
||||
funcr = savestack(L, func);
|
||||
cl = &clvalue(func)->l;
|
||||
L->ci->savedpc = L->savedpc;
|
||||
if (!cl->isC) { /* Lua function? prepare its call */
|
||||
CallInfo *ci;
|
||||
StkId st, base;
|
||||
Proto *p = cl->p;
|
||||
luaD_checkstack(L, p->maxstacksize);
|
||||
func = restorestack(L, funcr);
|
||||
if (!p->is_vararg) { /* no varargs? */
|
||||
base = func + 1;
|
||||
if (L->top > base + p->numparams)
|
||||
L->top = base + p->numparams;
|
||||
}
|
||||
else { /* vararg function */
|
||||
int nargs = cast_int(L->top - func) - 1;
|
||||
base = adjust_varargs(L, p, nargs);
|
||||
func = restorestack(L, funcr); /* previous call may change the stack */
|
||||
}
|
||||
ci = inc_ci(L); /* now `enter' new function */
|
||||
ci->func = func;
|
||||
L->base = ci->base = base;
|
||||
ci->top = L->base + p->maxstacksize;
|
||||
lua_assert(ci->top <= L->stack_last);
|
||||
L->savedpc = p->code; /* starting point */
|
||||
ci->tailcalls = 0;
|
||||
ci->nresults = nresults;
|
||||
for (st = L->top; st < ci->top; st++)
|
||||
setnilvalue(st);
|
||||
L->top = ci->top;
|
||||
if (L->hookmask & LUA_MASKCALL) {
|
||||
L->savedpc++; /* hooks assume 'pc' is already incremented */
|
||||
luaD_callhook(L, LUA_HOOKCALL, -1);
|
||||
L->savedpc--; /* correct 'pc' */
|
||||
}
|
||||
return PCRLUA;
|
||||
}
|
||||
else { /* if is a C function, call it */
|
||||
CallInfo *ci;
|
||||
int n;
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
ci = inc_ci(L); /* now `enter' new function */
|
||||
ci->func = restorestack(L, funcr);
|
||||
L->base = ci->base = ci->func + 1;
|
||||
ci->top = L->top + LUA_MINSTACK;
|
||||
lua_assert(ci->top <= L->stack_last);
|
||||
ci->nresults = nresults;
|
||||
if (L->hookmask & LUA_MASKCALL)
|
||||
luaD_callhook(L, LUA_HOOKCALL, -1);
|
||||
lua_unlock(L);
|
||||
n = (*curr_func(L)->c.f)(L); /* do the actual call */
|
||||
lua_lock(L);
|
||||
if (n < 0) /* yielding? */
|
||||
return PCRYIELD;
|
||||
else {
|
||||
luaD_poscall(L, L->top - n);
|
||||
return PCRC;
|
||||
}
|
||||
} while (bin && status == 0);
|
||||
L->debug = debug; /* restore debug status */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
void luaD_gcIM (TObject *o)
|
||||
{
|
||||
TObject *im = luaT_getimbyObj(o, IM_GC);
|
||||
if (ttype(im) != LUA_T_NIL) {
|
||||
*L->stack.top = *o;
|
||||
incr_top;
|
||||
luaD_callTM(im, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define MAXFILENAME 260 /* maximum part of a file name kept */
|
||||
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 (f_isLua(L->ci)) { /* Lua function? */
|
||||
while (L->ci->tailcalls--) /* call hook for eventual tail calls */
|
||||
luaD_callhook(L, LUA_HOOKTAILRET, -1);
|
||||
}
|
||||
return restorestack(L, fr);
|
||||
}
|
||||
|
||||
int lua_dofile (char *filename) {
|
||||
ZIO z;
|
||||
|
||||
int luaD_poscall (lua_State *L, StkId firstResult) {
|
||||
StkId res;
|
||||
int wanted, i;
|
||||
CallInfo *ci;
|
||||
if (L->hookmask & LUA_MASKRET)
|
||||
firstResult = callrethooks(L, firstResult);
|
||||
ci = L->ci--;
|
||||
res = ci->func; /* res == final position of 1st result */
|
||||
wanted = ci->nresults;
|
||||
L->base = (ci - 1)->base; /* restore base */
|
||||
L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
|
||||
/* move results to correct place */
|
||||
for (i = wanted; i != 0 && firstResult < L->top; i--)
|
||||
setobjs2s(L, res++, firstResult++);
|
||||
while (i-- > 0)
|
||||
setnilvalue(res++);
|
||||
L->top = res;
|
||||
return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call a function (C or Lua). The function to be called is at *func.
|
||||
** The arguments are on the stack, right after the function.
|
||||
** When returns, all the results are on the stack, starting at the original
|
||||
** function position.
|
||||
*/
|
||||
void luaD_call (lua_State *L, StkId func, int nResults) {
|
||||
if (++L->nCcalls >= LUAI_MAXCCALLS) {
|
||||
if (L->nCcalls == LUAI_MAXCCALLS)
|
||||
luaG_runerror(L, "C stack overflow");
|
||||
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
|
||||
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
|
||||
}
|
||||
if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
|
||||
luaV_execute(L, 1); /* call it */
|
||||
L->nCcalls--;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
|
||||
static void resume (lua_State *L, void *ud) {
|
||||
StkId firstArg = cast(StkId, ud);
|
||||
CallInfo *ci = L->ci;
|
||||
if (L->status == 0) { /* start coroutine? */
|
||||
lua_assert(ci == L->base_ci && firstArg > L->base);
|
||||
if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
|
||||
return;
|
||||
}
|
||||
else { /* resuming from previous yield */
|
||||
lua_assert(L->status == LUA_YIELD);
|
||||
L->status = 0;
|
||||
if (!f_isLua(ci)) { /* `common' yield? */
|
||||
/* finish interrupted execution of `OP_CALL' */
|
||||
lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
|
||||
GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
|
||||
if (luaD_poscall(L, firstArg)) /* complete it... */
|
||||
L->top = L->ci->top; /* and correct top if not multiple results */
|
||||
}
|
||||
else /* yielded inside a hook: just continue its execution */
|
||||
L->base = L->ci->base;
|
||||
}
|
||||
luaV_execute(L, cast_int(L->ci - L->base_ci));
|
||||
}
|
||||
|
||||
|
||||
static int resume_error (lua_State *L, const char *msg) {
|
||||
L->top = L->ci->base;
|
||||
setsvalue2s(L, L->top, luaS_new(L, msg));
|
||||
incr_top(L);
|
||||
lua_unlock(L);
|
||||
return LUA_ERRRUN;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_resume (lua_State *L, int nargs) {
|
||||
int status;
|
||||
int c;
|
||||
int bin;
|
||||
char source[MAXFILENAME];
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL)
|
||||
return 2;
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
bin = (c == ID_CHUNK);
|
||||
if (bin)
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
luaL_filesource(source, filename, sizeof(source));
|
||||
luaZ_Fopen(&z, f, source);
|
||||
status = do_main(&z, bin);
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
lua_lock(L);
|
||||
if (L->status != LUA_YIELD) {
|
||||
if (L->status != 0)
|
||||
return resume_error(L, "cannot resume dead coroutine");
|
||||
else if (L->ci != L->base_ci)
|
||||
return resume_error(L, "cannot resume non-suspended coroutine");
|
||||
}
|
||||
luai_userstateresume(L, nargs);
|
||||
lua_assert(L->errfunc == 0 && L->nCcalls == 0);
|
||||
status = luaD_rawrunprotected(L, resume, L->top - nargs);
|
||||
if (status != 0) { /* error? */
|
||||
L->status = cast_byte(status); /* mark thread as `dead' */
|
||||
luaD_seterrorobj(L, status, L->top);
|
||||
L->ci->top = L->top;
|
||||
}
|
||||
else
|
||||
status = L->status;
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int lua_dostring (char *str) {
|
||||
return lua_dobuffer(str, strlen(str), str);
|
||||
LUA_API int lua_yield (lua_State *L, int nresults) {
|
||||
luai_userstateyield(L, nresults);
|
||||
lua_lock(L);
|
||||
if (L->nCcalls > 0)
|
||||
luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
|
||||
L->base = L->top - nresults; /* protect stack slots below */
|
||||
L->status = LUA_YIELD;
|
||||
lua_unlock(L);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int lua_dobuffer (char *buff, int size, char *name) {
|
||||
ZIO z;
|
||||
if (!name) name = "?";
|
||||
luaZ_mopen(&z, buff, size, name);
|
||||
return do_main(&z, buff[0]==ID_CHUNK);
|
||||
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 */
|
||||
luaD_seterrorobj(L, status, oldtop);
|
||||
L->nCcalls = oldnCcalls;
|
||||
L->ci = restoreci(L, old_ci);
|
||||
L->base = L->ci->base;
|
||||
L->savedpc = L->ci->savedpc;
|
||||
L->allowhook = old_allowhooks;
|
||||
restore_stack_limit(L);
|
||||
}
|
||||
L->errfunc = old_errfunc;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected parser.
|
||||
*/
|
||||
struct SParser { /* data to `f_parser' */
|
||||
ZIO *z;
|
||||
Mbuffer buff; /* buffer to be used by the scanner */
|
||||
const char *name;
|
||||
};
|
||||
|
||||
static void f_parser (lua_State *L, void *ud) {
|
||||
int i;
|
||||
Proto *tf;
|
||||
Closure *cl;
|
||||
struct SParser *p = cast(struct SParser *, ud);
|
||||
int c = luaZ_lookahead(p->z);
|
||||
luaC_checkGC(L);
|
||||
tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
|
||||
&p->buff, p->name);
|
||||
cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
|
||||
cl->l.p = tf;
|
||||
for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */
|
||||
cl->l.upvals[i] = luaF_newupval(L);
|
||||
setclvalue(L, L->top, cl);
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
|
||||
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
|
||||
struct SParser p;
|
||||
int status;
|
||||
p.z = z; p.name = name;
|
||||
luaZ_initbuffer(L, &p.buff);
|
||||
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
|
||||
luaZ_freebuffer(L, &p.buff);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
57
ldo.h
57
ldo.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.h,v 1.5 1998/07/12 16:14:34 roberto Exp roberto $
|
||||
** $Id: ldo.h,v 2.6 2005/08/22 19:58:29 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,37 +10,48 @@
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
#define MULT_RET 255
|
||||
#define luaD_checkstack(L,n) \
|
||||
if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \
|
||||
luaD_growstack(L, n); \
|
||||
else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1));
|
||||
|
||||
|
||||
#define incr_top(L) {luaD_checkstack(L,1); L->top++;}
|
||||
|
||||
/*
|
||||
** macro to increment stack top.
|
||||
** There must be always an empty slot at the L->stack.top
|
||||
*/
|
||||
#define incr_top { if (L->stack.top >= L->stack.last) luaD_checkstack(1); \
|
||||
L->stack.top++; }
|
||||
#define savestack(L,p) ((char *)(p) - (char *)L->stack)
|
||||
#define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
|
||||
|
||||
#define saveci(L,p) ((char *)(p) - (char *)L->base_ci)
|
||||
#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n)))
|
||||
|
||||
|
||||
/* macros to convert from lua_Object to (TObject *) and back */
|
||||
|
||||
#define Address(lo) ((lo)+L->stack.stack-1)
|
||||
#define Ref(st) ((st)-L->stack.stack+1)
|
||||
/* results from luaD_precall */
|
||||
#define PCRLUA 0 /* initiated a call to a Lua function */
|
||||
#define PCRC 1 /* did a call to a C function */
|
||||
#define PCRYIELD 2 /* C funtion yielded */
|
||||
|
||||
|
||||
void luaD_init (void);
|
||||
void luaD_adjusttop (StkId newtop);
|
||||
void luaD_openstack (int nelems);
|
||||
void luaD_lineHook (int line);
|
||||
void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn);
|
||||
void luaD_calln (int nArgs, int nResults);
|
||||
void luaD_callTM (TObject *f, int nParams, int nResults);
|
||||
int luaD_protectedrun (void);
|
||||
void luaD_gcIM (TObject *o);
|
||||
void luaD_travstack (int (*fn)(TObject *));
|
||||
void luaD_checkstack (int n);
|
||||
/* type of protected functions, to be ran by `runprotected' */
|
||||
typedef void (*Pfunc) (lua_State *L, void *ud);
|
||||
|
||||
LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name);
|
||||
LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line);
|
||||
LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
|
||||
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
|
||||
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||
ptrdiff_t oldtop, ptrdiff_t ef);
|
||||
LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult);
|
||||
LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize);
|
||||
LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
|
||||
LUAI_FUNC void luaD_growstack (lua_State *L, int n);
|
||||
|
||||
LUAI_FUNC void luaD_throw (lua_State *L, int errcode);
|
||||
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
||||
|
||||
LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
164
ldump.c
Normal file
164
ldump.c
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
** $Id: ldump.c,v 1.15 2006/02/16 15:53:49 lhf Exp $
|
||||
** save precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define ldump_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lundump.h"
|
||||
|
||||
typedef struct {
|
||||
lua_State* L;
|
||||
lua_Writer writer;
|
||||
void* data;
|
||||
int strip;
|
||||
int status;
|
||||
} DumpState;
|
||||
|
||||
#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
|
||||
#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
|
||||
|
||||
static void DumpBlock(const void* b, size_t size, DumpState* D)
|
||||
{
|
||||
if (D->status==0)
|
||||
{
|
||||
lua_unlock(D->L);
|
||||
D->status=(*D->writer)(D->L,b,size,D->data);
|
||||
lua_lock(D->L);
|
||||
}
|
||||
}
|
||||
|
||||
static void DumpChar(int y, DumpState* D)
|
||||
{
|
||||
char x=(char)y;
|
||||
DumpVar(x,D);
|
||||
}
|
||||
|
||||
static void DumpInt(int x, DumpState* D)
|
||||
{
|
||||
DumpVar(x,D);
|
||||
}
|
||||
|
||||
static void DumpNumber(lua_Number x, DumpState* D)
|
||||
{
|
||||
DumpVar(x,D);
|
||||
}
|
||||
|
||||
static void DumpVector(const void* b, int n, size_t size, DumpState* D)
|
||||
{
|
||||
DumpInt(n,D);
|
||||
DumpMem(b,n,size,D);
|
||||
}
|
||||
|
||||
static void DumpString(const TString* s, DumpState* D)
|
||||
{
|
||||
if (s==NULL || getstr(s)==NULL)
|
||||
{
|
||||
size_t size=0;
|
||||
DumpVar(size,D);
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t size=s->tsv.len+1; /* include trailing '\0' */
|
||||
DumpVar(size,D);
|
||||
DumpBlock(getstr(s),size,D);
|
||||
}
|
||||
}
|
||||
|
||||
#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
|
||||
|
||||
static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
|
||||
|
||||
static void DumpConstants(const Proto* f, DumpState* D)
|
||||
{
|
||||
int i,n=f->sizek;
|
||||
DumpInt(n,D);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
const TValue* o=&f->k[i];
|
||||
DumpChar(ttype(o),D);
|
||||
switch (ttype(o))
|
||||
{
|
||||
case LUA_TNIL:
|
||||
break;
|
||||
case LUA_TBOOLEAN:
|
||||
DumpChar(bvalue(o),D);
|
||||
break;
|
||||
case LUA_TNUMBER:
|
||||
DumpNumber(nvalue(o),D);
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
DumpString(rawtsvalue(o),D);
|
||||
break;
|
||||
default:
|
||||
lua_assert(0); /* cannot happen */
|
||||
break;
|
||||
}
|
||||
}
|
||||
n=f->sizep;
|
||||
DumpInt(n,D);
|
||||
for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
|
||||
}
|
||||
|
||||
static void DumpDebug(const Proto* f, DumpState* D)
|
||||
{
|
||||
int i,n;
|
||||
n= (D->strip) ? 0 : f->sizelineinfo;
|
||||
DumpVector(f->lineinfo,n,sizeof(int),D);
|
||||
n= (D->strip) ? 0 : 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);
|
||||
}
|
||||
n= (D->strip) ? 0 : 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)
|
||||
{
|
||||
DumpString((f->source==p || D->strip) ? NULL : f->source,D);
|
||||
DumpInt(f->linedefined,D);
|
||||
DumpInt(f->lastlinedefined,D);
|
||||
DumpChar(f->nups,D);
|
||||
DumpChar(f->numparams,D);
|
||||
DumpChar(f->is_vararg,D);
|
||||
DumpChar(f->maxstacksize,D);
|
||||
DumpCode(f,D);
|
||||
DumpConstants(f,D);
|
||||
DumpDebug(f,D);
|
||||
}
|
||||
|
||||
static void DumpHeader(DumpState* D)
|
||||
{
|
||||
char h[LUAC_HEADERSIZE];
|
||||
luaU_header(h);
|
||||
DumpBlock(h,LUAC_HEADERSIZE,D);
|
||||
}
|
||||
|
||||
/*
|
||||
** dump Lua function as precompiled chunk
|
||||
*/
|
||||
int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
|
||||
{
|
||||
DumpState D;
|
||||
D.L=L;
|
||||
D.writer=w;
|
||||
D.data=data;
|
||||
D.strip=strip;
|
||||
D.status=0;
|
||||
DumpHeader(&D);
|
||||
DumpFunction(f,NULL,&D);
|
||||
return D.status;
|
||||
}
|
||||
200
lfunc.c
200
lfunc.c
@@ -1,98 +1,174 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 1.9 1998/06/19 16:14:09 roberto Exp roberto $
|
||||
** $Id: lfunc.c,v 2.11 2005/05/05 20:47:02 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define lfunc_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
#define gcsizeproto(p) 5 /* approximate "weight" for a prototype */
|
||||
#define gcsizeclosure(c) 1 /* approximate "weight" for a closure */
|
||||
|
||||
|
||||
|
||||
Closure *luaF_newclosure (int nelems)
|
||||
{
|
||||
Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
|
||||
luaO_insertlist(&(L->rootcl), (GCnode *)c);
|
||||
L->nblocks += gcsizeclosure(c);
|
||||
c->nelems = nelems;
|
||||
Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) {
|
||||
Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
|
||||
luaC_link(L, obj2gco(c), LUA_TFUNCTION);
|
||||
c->c.isC = 1;
|
||||
c->c.env = e;
|
||||
c->c.nupvalues = cast_byte(nelems);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
TProtoFunc *luaF_newproto (void)
|
||||
{
|
||||
TProtoFunc *f = luaM_new(TProtoFunc);
|
||||
Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) {
|
||||
Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
|
||||
luaC_link(L, obj2gco(c), LUA_TFUNCTION);
|
||||
c->l.isC = 0;
|
||||
c->l.env = e;
|
||||
c->l.nupvalues = cast_byte(nelems);
|
||||
while (nelems--) c->l.upvals[nelems] = NULL;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
UpVal *luaF_newupval (lua_State *L) {
|
||||
UpVal *uv = luaM_new(L, UpVal);
|
||||
luaC_link(L, obj2gco(uv), LUA_TUPVAL);
|
||||
uv->v = &uv->u.value;
|
||||
setnilvalue(uv->v);
|
||||
return uv;
|
||||
}
|
||||
|
||||
|
||||
UpVal *luaF_findupval (lua_State *L, StkId level) {
|
||||
global_State *g = G(L);
|
||||
GCObject **pp = &L->openupval;
|
||||
UpVal *p;
|
||||
UpVal *uv;
|
||||
while ((p = ngcotouv(*pp)) != NULL && p->v >= level) {
|
||||
lua_assert(p->v != &p->u.value);
|
||||
if (p->v == level) { /* found a corresponding upvalue? */
|
||||
if (isdead(g, obj2gco(p))) /* is it dead? */
|
||||
changewhite(obj2gco(p)); /* ressurect it */
|
||||
return p;
|
||||
}
|
||||
pp = &p->next;
|
||||
}
|
||||
uv = luaM_new(L, UpVal); /* not found: create a new one */
|
||||
uv->tt = LUA_TUPVAL;
|
||||
uv->marked = luaC_white(g);
|
||||
uv->v = level; /* current value lives in the stack */
|
||||
uv->next = *pp; /* chain it in the proper position */
|
||||
*pp = obj2gco(uv);
|
||||
uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */
|
||||
uv->u.l.next = g->uvhead.u.l.next;
|
||||
uv->u.l.next->u.l.prev = uv;
|
||||
g->uvhead.u.l.next = uv;
|
||||
lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
|
||||
return uv;
|
||||
}
|
||||
|
||||
|
||||
static void unlinkupval (UpVal *uv) {
|
||||
lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
|
||||
uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */
|
||||
uv->u.l.prev->u.l.next = uv->u.l.next;
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeupval (lua_State *L, UpVal *uv) {
|
||||
if (uv->v != &uv->u.value) /* is it open? */
|
||||
unlinkupval(uv); /* remove from open list */
|
||||
luaM_free(L, uv); /* free upvalue */
|
||||
}
|
||||
|
||||
|
||||
void luaF_close (lua_State *L, StkId level) {
|
||||
UpVal *uv;
|
||||
global_State *g = G(L);
|
||||
while ((uv = ngcotouv(L->openupval)) != NULL && uv->v >= level) {
|
||||
GCObject *o = obj2gco(uv);
|
||||
lua_assert(!isblack(o) && uv->v != &uv->u.value);
|
||||
L->openupval = uv->next; /* remove from `open' list */
|
||||
if (isdead(g, o))
|
||||
luaF_freeupval(L, uv); /* free upvalue */
|
||||
else {
|
||||
unlinkupval(uv);
|
||||
setobj(L, &uv->u.value, uv->v);
|
||||
uv->v = &uv->u.value; /* now current value lives here */
|
||||
luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Proto *luaF_newproto (lua_State *L) {
|
||||
Proto *f = luaM_new(L, Proto);
|
||||
luaC_link(L, obj2gco(f), LUA_TPROTO);
|
||||
f->k = NULL;
|
||||
f->sizek = 0;
|
||||
f->p = NULL;
|
||||
f->sizep = 0;
|
||||
f->code = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->source = NULL;
|
||||
f->consts = NULL;
|
||||
f->nconsts = 0;
|
||||
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;
|
||||
luaO_insertlist(&(L->rootproto), (GCnode *)f);
|
||||
L->nblocks += gcsizeproto(f);
|
||||
f->linedefined = 0;
|
||||
f->lastlinedefined = 0;
|
||||
f->source = NULL;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void freefunc (TProtoFunc *f)
|
||||
{
|
||||
luaM_free(f->code);
|
||||
luaM_free(f->locvars);
|
||||
luaM_free(f->consts);
|
||||
luaM_free(f);
|
||||
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||
luaM_freearray(L, f->code, f->sizecode, Instruction);
|
||||
luaM_freearray(L, f->p, f->sizep, Proto *);
|
||||
luaM_freearray(L, f->k, f->sizek, TValue);
|
||||
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_free(L, f);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeproto (TProtoFunc *l)
|
||||
{
|
||||
while (l) {
|
||||
TProtoFunc *next = (TProtoFunc *)l->head.next;
|
||||
L->nblocks -= gcsizeproto(l);
|
||||
freefunc(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeclosure (Closure *l)
|
||||
{
|
||||
while (l) {
|
||||
Closure *next = (Closure *)l->head.next;
|
||||
L->nblocks -= gcsizeclosure(l);
|
||||
luaM_free(l);
|
||||
l = next;
|
||||
}
|
||||
void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
|
||||
sizeLclosure(c->l.nupvalues);
|
||||
luaM_freemem(L, c, size);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Look for n-th local variable at line "line" in function "func".
|
||||
** Look for n-th local variable at line `line' in function `func'.
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
char *luaF_getlocalname (TProtoFunc *func, int local_number, int line)
|
||||
{
|
||||
int count = 0;
|
||||
char *varname = NULL;
|
||||
LocVar *lv = func->locvars;
|
||||
if (lv == NULL)
|
||||
return NULL;
|
||||
for (; lv->line != -1 && lv->line < line; lv++) {
|
||||
if (lv->varname) { /* register */
|
||||
if (++count == local_number)
|
||||
varname = lv->varname->str;
|
||||
const 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 */
|
||||
}
|
||||
|
||||
|
||||
25
lfunc.h
25
lfunc.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: lfunc.h,v 1.4 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** Lua Function structures
|
||||
** $Id: lfunc.h,v 2.3 2005/02/18 12:40:02 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -11,13 +11,24 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
|
||||
cast(int, sizeof(TValue)*((n)-1)))
|
||||
|
||||
TProtoFunc *luaF_newproto (void);
|
||||
Closure *luaF_newclosure (int nelems);
|
||||
void luaF_freeproto (TProtoFunc *l);
|
||||
void luaF_freeclosure (Closure *l);
|
||||
#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
|
||||
cast(int, sizeof(TValue *)*((n)-1)))
|
||||
|
||||
char *luaF_getlocalname (TProtoFunc *func, int local_number, int line);
|
||||
|
||||
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
|
||||
LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e);
|
||||
LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e);
|
||||
LUAI_FUNC UpVal *luaF_newupval (lua_State *L);
|
||||
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
|
||||
LUAI_FUNC void luaF_close (lua_State *L, StkId level);
|
||||
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
|
||||
LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c);
|
||||
LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv);
|
||||
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
|
||||
int pc);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
900
lgc.c
900
lgc.c
@@ -1,10 +1,17 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 1.22 1999/02/26 15:48:55 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 2.37 2005/12/22 16:19:56 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define lgc_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
@@ -14,262 +21,691 @@
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define GCSTEPSIZE 1024u
|
||||
#define GCSWEEPMAX 40
|
||||
#define GCSWEEPCOST 10
|
||||
#define GCFINALIZECOST 100
|
||||
|
||||
|
||||
#define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS))
|
||||
|
||||
#define makewhite(g,x) \
|
||||
((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g)))
|
||||
|
||||
#define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
|
||||
#define black2gray(x) resetbit((x)->gch.marked, BLACKBIT)
|
||||
|
||||
#define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)
|
||||
|
||||
|
||||
#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT)
|
||||
#define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT)
|
||||
|
||||
|
||||
#define KEYWEAK bitmask(KEYWEAKBIT)
|
||||
#define VALUEWEAK bitmask(VALUEWEAKBIT)
|
||||
|
||||
|
||||
|
||||
static int markobject (TObject *o);
|
||||
#define markvalue(g,o) { checkconsistency(o); \
|
||||
if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); }
|
||||
|
||||
#define markobject(g,t) { if (iswhite(obj2gco(t))) \
|
||||
reallymarkobject(g, obj2gco(t)); }
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** REF mechanism
|
||||
** =======================================================
|
||||
*/
|
||||
#define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause)
|
||||
|
||||
|
||||
int luaC_ref (TObject *o, int lock) {
|
||||
int ref;
|
||||
if (ttype(o) == LUA_T_NIL)
|
||||
ref = -1; /* special ref for nil */
|
||||
else {
|
||||
for (ref=0; ref<L->refSize; ref++)
|
||||
if (L->refArray[ref].status == FREE)
|
||||
break;
|
||||
if (ref == L->refSize) { /* no more empty spaces? */
|
||||
luaM_growvector(L->refArray, L->refSize, 1, struct ref, refEM, MAX_INT);
|
||||
L->refSize++;
|
||||
static void removeentry (Node *n) {
|
||||
lua_assert(ttisnil(gval(n)));
|
||||
if (iscollectable(gkey(n)))
|
||||
setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */
|
||||
}
|
||||
|
||||
|
||||
static void reallymarkobject (global_State *g, GCObject *o) {
|
||||
lua_assert(iswhite(o) && !isdead(g, o));
|
||||
white2gray(o);
|
||||
switch (o->gch.tt) {
|
||||
case LUA_TSTRING: {
|
||||
return;
|
||||
}
|
||||
L->refArray[ref].o = *o;
|
||||
L->refArray[ref].status = lock ? LOCK : HOLD;
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
void lua_unref (int ref)
|
||||
{
|
||||
if (ref >= 0 && ref < L->refSize)
|
||||
L->refArray[ref].status = FREE;
|
||||
}
|
||||
|
||||
|
||||
TObject* luaC_getref (int ref)
|
||||
{
|
||||
if (ref == -1)
|
||||
return &luaO_nilobject;
|
||||
if (ref >= 0 && ref < L->refSize &&
|
||||
(L->refArray[ref].status == LOCK || L->refArray[ref].status == HOLD))
|
||||
return &L->refArray[ref].o;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static void travlock (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++)
|
||||
if (L->refArray[i].status == LOCK)
|
||||
markobject(&L->refArray[i].o);
|
||||
}
|
||||
|
||||
|
||||
static int ismarked (TObject *o)
|
||||
{
|
||||
/* valid only for locked objects */
|
||||
switch (o->ttype) {
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
return o->value.ts->head.marked;
|
||||
case LUA_T_ARRAY:
|
||||
return o->value.a->head.marked;
|
||||
case LUA_T_CLOSURE:
|
||||
return o->value.cl->head.marked;
|
||||
case LUA_T_PROTO:
|
||||
return o->value.tf->head.marked;
|
||||
#ifdef DEBUG
|
||||
case LUA_T_LINE: case LUA_T_CLMARK:
|
||||
case LUA_T_CMARK: case LUA_T_PMARK:
|
||||
LUA_INTERNALERROR("invalid type");
|
||||
#endif
|
||||
default: /* nil, number or cproto */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void invalidaterefs (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++)
|
||||
if (L->refArray[i].status == HOLD && !ismarked(&L->refArray[i].o))
|
||||
L->refArray[i].status = COLLECTED;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void luaC_hashcallIM (Hash *l)
|
||||
{
|
||||
TObject t;
|
||||
ttype(&t) = LUA_T_ARRAY;
|
||||
for (; l; l=(Hash *)l->head.next) {
|
||||
avalue(&t) = l;
|
||||
luaD_gcIM(&t);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_strcallIM (TaggedString *l)
|
||||
{
|
||||
TObject o;
|
||||
ttype(&o) = LUA_T_USERDATA;
|
||||
for (; l; l=(TaggedString *)l->head.next)
|
||||
if (l->constindex == -1) { /* is userdata? */
|
||||
tsvalue(&o) = l;
|
||||
luaD_gcIM(&o);
|
||||
case LUA_TUSERDATA: {
|
||||
Table *mt = gco2u(o)->metatable;
|
||||
gray2black(o); /* udata are never gray */
|
||||
if (mt) markobject(g, mt);
|
||||
markobject(g, gco2u(o)->env);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static GCnode *listcollect (GCnode *l)
|
||||
{
|
||||
GCnode *frees = NULL;
|
||||
while (l) {
|
||||
GCnode *next = l->next;
|
||||
l->marked = 0;
|
||||
while (next && !next->marked) {
|
||||
l->next = next->next;
|
||||
next->next = frees;
|
||||
frees = next;
|
||||
next = l->next;
|
||||
case LUA_TUPVAL: {
|
||||
UpVal *uv = gco2uv(o);
|
||||
markvalue(g, uv->v);
|
||||
if (uv->v == &uv->u.value) /* closed? */
|
||||
gray2black(o); /* open upvalues are never black */
|
||||
return;
|
||||
}
|
||||
l = next;
|
||||
}
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
static void strmark (TaggedString *s)
|
||||
{
|
||||
if (!s->head.marked)
|
||||
s->head.marked = 1;
|
||||
}
|
||||
|
||||
|
||||
static void protomark (TProtoFunc *f) {
|
||||
if (!f->head.marked) {
|
||||
int i;
|
||||
f->head.marked = 1;
|
||||
strmark(f->source);
|
||||
for (i=0; i<f->nconsts; i++)
|
||||
markobject(&f->consts[i]);
|
||||
case LUA_TFUNCTION: {
|
||||
gco2cl(o)->c.gclist = g->gray;
|
||||
g->gray = o;
|
||||
break;
|
||||
}
|
||||
case LUA_TTABLE: {
|
||||
gco2h(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
break;
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
gco2th(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
break;
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
gco2p(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void closuremark (Closure *f)
|
||||
{
|
||||
if (!f->head.marked) {
|
||||
int i;
|
||||
f->head.marked = 1;
|
||||
for (i=f->nelems; i>=0; i--)
|
||||
markobject(&f->consts[i]);
|
||||
static void marktmu (global_State *g) {
|
||||
GCObject *u = g->tmudata;
|
||||
if (u) {
|
||||
do {
|
||||
u = u->gch.next;
|
||||
makewhite(g, u); /* may be marked, if left from previous GC */
|
||||
reallymarkobject(g, u);
|
||||
} while (u != g->tmudata);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void hashmark (Hash *h)
|
||||
{
|
||||
if (!h->head.marked) {
|
||||
int i;
|
||||
h->head.marked = 1;
|
||||
for (i=0; i<nhash(h); i++) {
|
||||
Node *n = node(h,i);
|
||||
if (ttype(ref(n)) != LUA_T_NIL) {
|
||||
markobject(&n->ref);
|
||||
markobject(&n->val);
|
||||
/* move `dead' udata that need finalization to list `tmudata' */
|
||||
size_t luaC_separateudata (lua_State *L, int all) {
|
||||
global_State *g = G(L);
|
||||
size_t deadmem = 0;
|
||||
GCObject **p = &g->mainthread->next;
|
||||
GCObject *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (!(iswhite(curr) || all) || isfinalized(gco2u(curr)))
|
||||
p = &curr->gch.next; /* don't bother with them */
|
||||
else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) {
|
||||
markfinalized(gco2u(curr)); /* don't need finalization */
|
||||
p = &curr->gch.next;
|
||||
}
|
||||
else { /* must call its gc method */
|
||||
deadmem += sizeudata(gco2u(curr));
|
||||
markfinalized(gco2u(curr));
|
||||
*p = curr->gch.next;
|
||||
/* link `curr' at the end of `tmudata' list */
|
||||
if (g->tmudata == NULL) /* list is empty? */
|
||||
g->tmudata = curr->gch.next = curr; /* creates a circular list */
|
||||
else {
|
||||
curr->gch.next = g->tmudata->gch.next;
|
||||
g->tmudata->gch.next = curr;
|
||||
g->tmudata = curr;
|
||||
}
|
||||
}
|
||||
}
|
||||
return deadmem;
|
||||
}
|
||||
|
||||
|
||||
static void globalmark (void)
|
||||
{
|
||||
TaggedString *g;
|
||||
for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next){
|
||||
LUA_ASSERT(g->constindex >= 0, "userdata in global list");
|
||||
if (g->u.s.globalval.ttype != LUA_T_NIL) {
|
||||
markobject(&g->u.s.globalval);
|
||||
strmark(g); /* cannot collect non nil global variables */
|
||||
static int traversetable (global_State *g, Table *h) {
|
||||
int i;
|
||||
int weakkey = 0;
|
||||
int weakvalue = 0;
|
||||
const TValue *mode;
|
||||
if (h->metatable)
|
||||
markobject(g, h->metatable);
|
||||
mode = gfasttm(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? */
|
||||
h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */
|
||||
h->marked |= cast_byte((weakkey << KEYWEAKBIT) |
|
||||
(weakvalue << VALUEWEAKBIT));
|
||||
h->gclist = g->weak; /* must be cleared after GC, ... */
|
||||
g->weak = obj2gco(h); /* ... so put in the appropriate list */
|
||||
}
|
||||
}
|
||||
if (weakkey && weakvalue) return 1;
|
||||
if (!weakvalue) {
|
||||
i = h->sizearray;
|
||||
while (i--)
|
||||
markvalue(g, &h->array[i]);
|
||||
}
|
||||
i = sizenode(h);
|
||||
while (i--) {
|
||||
Node *n = gnode(h, i);
|
||||
lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));
|
||||
if (ttisnil(gval(n)))
|
||||
removeentry(n); /* remove empty entries */
|
||||
else {
|
||||
lua_assert(!ttisnil(gkey(n)));
|
||||
if (!weakkey) markvalue(g, gkey(n));
|
||||
if (!weakvalue) markvalue(g, gval(n));
|
||||
}
|
||||
}
|
||||
return weakkey || weakvalue;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** All marks are conditional because a GC may happen while the
|
||||
** prototype is still being created
|
||||
*/
|
||||
static void traverseproto (global_State *g, Proto *f) {
|
||||
int i;
|
||||
if (f->source) stringmark(f->source);
|
||||
for (i=0; i<f->sizek; i++) /* mark literals */
|
||||
markvalue(g, &f->k[i]);
|
||||
for (i=0; i<f->sizeupvalues; i++) { /* mark upvalue names */
|
||||
if (f->upvalues[i])
|
||||
stringmark(f->upvalues[i]);
|
||||
}
|
||||
for (i=0; i<f->sizep; i++) { /* mark nested protos */
|
||||
if (f->p[i])
|
||||
markobject(g, f->p[i]);
|
||||
}
|
||||
for (i=0; i<f->sizelocvars; i++) { /* mark local-variable names */
|
||||
if (f->locvars[i].varname)
|
||||
stringmark(f->locvars[i].varname);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void traverseclosure (global_State *g, Closure *cl) {
|
||||
markobject(g, cl->c.env);
|
||||
if (cl->c.isC) {
|
||||
int i;
|
||||
for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
|
||||
markvalue(g, &cl->c.upvalue[i]);
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
lua_assert(cl->l.nupvalues == cl->l.p->nups);
|
||||
markobject(g, cl->l.p);
|
||||
for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */
|
||||
markobject(g, cl->l.upvals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void checkstacksizes (lua_State *L, StkId max) {
|
||||
int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */
|
||||
int s_used = cast_int(max - L->stack); /* part of stack in use */
|
||||
if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */
|
||||
return; /* do not touch the stacks */
|
||||
if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
|
||||
luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
|
||||
condhardstacktests(luaD_reallocCI(L, ci_used + 1));
|
||||
if (4*s_used < L->stacksize &&
|
||||
2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
|
||||
luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
|
||||
condhardstacktests(luaD_reallocstack(L, s_used));
|
||||
}
|
||||
|
||||
|
||||
static void traversestack (global_State *g, lua_State *l) {
|
||||
StkId o, lim;
|
||||
CallInfo *ci;
|
||||
markvalue(g, gt(l));
|
||||
lim = l->top;
|
||||
for (ci = l->base_ci; ci <= l->ci; ci++) {
|
||||
lua_assert(ci->top <= l->stack_last);
|
||||
if (lim < ci->top) lim = ci->top;
|
||||
}
|
||||
for (o = l->stack; o < l->top; o++)
|
||||
markvalue(g, o);
|
||||
for (; o <= lim; o++)
|
||||
setnilvalue(o);
|
||||
checkstacksizes(l, lim);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** traverse one gray object, turning it to black.
|
||||
** Returns `quantity' traversed.
|
||||
*/
|
||||
static l_mem propagatemark (global_State *g) {
|
||||
GCObject *o = g->gray;
|
||||
lua_assert(isgray(o));
|
||||
gray2black(o);
|
||||
switch (o->gch.tt) {
|
||||
case LUA_TTABLE: {
|
||||
Table *h = gco2h(o);
|
||||
g->gray = h->gclist;
|
||||
if (traversetable(g, h)) /* table is weak? */
|
||||
black2gray(o); /* keep it gray */
|
||||
return sizeof(Table) + sizeof(TValue) * h->sizearray +
|
||||
sizeof(Node) * sizenode(h);
|
||||
}
|
||||
case LUA_TFUNCTION: {
|
||||
Closure *cl = gco2cl(o);
|
||||
g->gray = cl->c.gclist;
|
||||
traverseclosure(g, cl);
|
||||
return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :
|
||||
sizeLclosure(cl->l.nupvalues);
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
lua_State *th = gco2th(o);
|
||||
g->gray = th->gclist;
|
||||
th->gclist = g->grayagain;
|
||||
g->grayagain = o;
|
||||
black2gray(o);
|
||||
traversestack(g, th);
|
||||
return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
|
||||
sizeof(CallInfo) * th->size_ci;
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
Proto *p = gco2p(o);
|
||||
g->gray = p->gclist;
|
||||
traverseproto(g, p);
|
||||
return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
|
||||
sizeof(Proto *) * p->sizep +
|
||||
sizeof(TValue) * p->sizek +
|
||||
sizeof(int) * p->sizelineinfo +
|
||||
sizeof(LocVar) * p->sizelocvars +
|
||||
sizeof(TString *) * p->sizeupvalues;
|
||||
}
|
||||
default: lua_assert(0); return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static size_t propagateall (global_State *g) {
|
||||
size_t m = 0;
|
||||
while (g->gray) m += propagatemark(g);
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** The next function tells whether a key or value can be cleared from
|
||||
** a weak table. Non-collectable objects are never removed from weak
|
||||
** tables. Strings behave as `values', so are never removed too. for
|
||||
** other objects: if really collected, cannot keep them; for userdata
|
||||
** being finalized, keep them in keys, but not in values
|
||||
*/
|
||||
static int iscleared (const TValue *o, int iskey) {
|
||||
if (!iscollectable(o)) return 0;
|
||||
if (ttisstring(o)) {
|
||||
stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */
|
||||
return 0;
|
||||
}
|
||||
return iswhite(gcvalue(o)) ||
|
||||
(ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** clear collected entries from weaktables
|
||||
*/
|
||||
static void cleartable (GCObject *l) {
|
||||
while (l) {
|
||||
Table *h = gco2h(l);
|
||||
int i = h->sizearray;
|
||||
lua_assert(testbit(h->marked, VALUEWEAKBIT) ||
|
||||
testbit(h->marked, KEYWEAKBIT));
|
||||
if (testbit(h->marked, VALUEWEAKBIT)) {
|
||||
while (i--) {
|
||||
TValue *o = &h->array[i];
|
||||
if (iscleared(o, 0)) /* value was collected? */
|
||||
setnilvalue(o); /* remove value */
|
||||
}
|
||||
}
|
||||
i = sizenode(h);
|
||||
while (i--) {
|
||||
Node *n = gnode(h, i);
|
||||
if (!ttisnil(gval(n)) && /* non-empty entry? */
|
||||
(iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) {
|
||||
setnilvalue(gval(n)); /* remove value ... */
|
||||
removeentry(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, gco2p(o)); break;
|
||||
case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break;
|
||||
case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
|
||||
case LUA_TTABLE: luaH_free(L, gco2h(o)); break;
|
||||
case LUA_TTHREAD: {
|
||||
lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread);
|
||||
luaE_freethread(L, gco2th(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TSTRING: {
|
||||
G(L)->strt.nuse--;
|
||||
luaM_freemem(L, o, sizestring(gco2ts(o)));
|
||||
break;
|
||||
}
|
||||
case LUA_TUSERDATA: {
|
||||
luaM_freemem(L, o, sizeudata(gco2u(o)));
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM)
|
||||
|
||||
|
||||
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
||||
GCObject *curr;
|
||||
global_State *g = G(L);
|
||||
int deadmask = otherwhite(g);
|
||||
while ((curr = *p) != NULL && count-- > 0) {
|
||||
if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */
|
||||
sweepwholelist(L, &gco2th(curr)->openupval);
|
||||
if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */
|
||||
lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT));
|
||||
makewhite(g, curr); /* make it white (for next cycle) */
|
||||
p = &curr->gch.next;
|
||||
}
|
||||
else { /* must erase `curr' */
|
||||
lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT));
|
||||
*p = curr->gch.next;
|
||||
if (curr == g->rootgc) /* is the first element of the list? */
|
||||
g->rootgc = curr->gch.next; /* adjust first */
|
||||
freeobj(L, curr);
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static void checkSizes (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
/* check size of string hash */
|
||||
if (g->strt.nuse < cast(lu_int32, g->strt.size/4) &&
|
||||
g->strt.size > MINSTRTABSIZE*2)
|
||||
luaS_resize(L, g->strt.size/2); /* table is too big */
|
||||
/* check size of buffer */
|
||||
if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */
|
||||
size_t newsize = luaZ_sizebuffer(&g->buff) / 2;
|
||||
luaZ_resizebuffer(L, &g->buff, newsize);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void GCTM (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
GCObject *o = g->tmudata->gch.next; /* get first element */
|
||||
Udata *udata = rawgco2u(o);
|
||||
const TValue *tm;
|
||||
/* remove udata from `tmudata' */
|
||||
if (o == g->tmudata) /* last element? */
|
||||
g->tmudata = NULL;
|
||||
else
|
||||
g->tmudata->gch.next = udata->uv.next;
|
||||
udata->uv.next = g->mainthread->next; /* return it to `root' list */
|
||||
g->mainthread->next = o;
|
||||
makewhite(g, o);
|
||||
tm = fasttm(L, udata->uv.metatable, TM_GC);
|
||||
if (tm != NULL) {
|
||||
lu_byte oldah = L->allowhook;
|
||||
lu_mem oldt = g->GCthreshold;
|
||||
L->allowhook = 0; /* stop debug hooks during GC tag method */
|
||||
g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */
|
||||
setobj2s(L, L->top, tm);
|
||||
setuvalue(L, L->top+1, udata);
|
||||
L->top += 2;
|
||||
luaD_call(L, L->top - 2, 0);
|
||||
L->allowhook = oldah; /* restore hooks */
|
||||
g->GCthreshold = oldt; /* restore threshold */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call all GC tag methods
|
||||
*/
|
||||
void luaC_callGCTM (lua_State *L) {
|
||||
while (G(L)->tmudata)
|
||||
GCTM(L);
|
||||
}
|
||||
|
||||
|
||||
void luaC_freeall (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
int i;
|
||||
g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */
|
||||
sweepwholelist(L, &g->rootgc);
|
||||
for (i = 0; i < g->strt.size; i++) /* free all string lists */
|
||||
sweepwholelist(L, &g->strt.hash[i]);
|
||||
}
|
||||
|
||||
|
||||
static void markmt (global_State *g) {
|
||||
int i;
|
||||
for (i=0; i<NUM_TAGS; i++)
|
||||
if (g->mt[i]) markobject(g, g->mt[i]);
|
||||
}
|
||||
|
||||
|
||||
/* mark root set */
|
||||
static void markroot (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
g->gray = NULL;
|
||||
g->grayagain = NULL;
|
||||
g->weak = NULL;
|
||||
markobject(g, g->mainthread);
|
||||
/* make global table be traversed before main stack */
|
||||
markvalue(g, gt(g->mainthread));
|
||||
markvalue(g, registry(L));
|
||||
markmt(g);
|
||||
g->gcstate = GCSpropagate;
|
||||
}
|
||||
|
||||
|
||||
static void remarkupvals (global_State *g) {
|
||||
UpVal *uv;
|
||||
for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
|
||||
lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
|
||||
if (isgray(obj2gco(uv)))
|
||||
markvalue(g, uv->v);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void atomic (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
size_t udsize; /* total size of userdata to be finalized */
|
||||
/* remark occasional upvalues of (maybe) dead threads */
|
||||
remarkupvals(g);
|
||||
/* traverse objects cautch by write barrier and by 'remarkupvals' */
|
||||
propagateall(g);
|
||||
/* remark weak tables */
|
||||
g->gray = g->weak;
|
||||
g->weak = NULL;
|
||||
lua_assert(!iswhite(obj2gco(g->mainthread)));
|
||||
markobject(g, L); /* mark running thread */
|
||||
markmt(g); /* mark basic metatables (again) */
|
||||
propagateall(g);
|
||||
/* remark gray again */
|
||||
g->gray = g->grayagain;
|
||||
g->grayagain = NULL;
|
||||
propagateall(g);
|
||||
udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */
|
||||
marktmu(g); /* mark `preserved' userdata */
|
||||
udsize += propagateall(g); /* remark, to propagate `preserveness' */
|
||||
cleartable(g->weak); /* remove collected objects from weak tables */
|
||||
/* flip current white */
|
||||
g->currentwhite = cast_byte(otherwhite(g));
|
||||
g->sweepstrgc = 0;
|
||||
g->sweepgc = &g->rootgc;
|
||||
g->gcstate = GCSsweepstring;
|
||||
g->estimate = g->totalbytes - udsize; /* first estimate */
|
||||
}
|
||||
|
||||
|
||||
static l_mem singlestep (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
/*lua_checkmemory(L);*/
|
||||
switch (g->gcstate) {
|
||||
case GCSpause: {
|
||||
markroot(L); /* start a new collection */
|
||||
return 0;
|
||||
}
|
||||
case GCSpropagate: {
|
||||
if (g->gray)
|
||||
return propagatemark(g);
|
||||
else { /* no more `gray' objects */
|
||||
atomic(L); /* finish mark phase */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
case GCSsweepstring: {
|
||||
lu_mem old = g->totalbytes;
|
||||
sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]);
|
||||
if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */
|
||||
g->gcstate = GCSsweep; /* end sweep-string phase */
|
||||
lua_assert(old >= g->totalbytes);
|
||||
g->estimate -= old - g->totalbytes;
|
||||
return GCSWEEPCOST;
|
||||
}
|
||||
case GCSsweep: {
|
||||
lu_mem old = g->totalbytes;
|
||||
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
|
||||
if (*g->sweepgc == NULL) { /* nothing more to sweep? */
|
||||
checkSizes(L);
|
||||
g->gcstate = GCSfinalize; /* end sweep phase */
|
||||
}
|
||||
lua_assert(old >= g->totalbytes);
|
||||
g->estimate -= old - g->totalbytes;
|
||||
return GCSWEEPMAX*GCSWEEPCOST;
|
||||
}
|
||||
case GCSfinalize: {
|
||||
if (g->tmudata) {
|
||||
GCTM(L);
|
||||
if (g->estimate > GCFINALIZECOST)
|
||||
g->estimate -= GCFINALIZECOST;
|
||||
return GCFINALIZECOST;
|
||||
}
|
||||
else {
|
||||
g->gcstate = GCSpause; /* end collection */
|
||||
g->gcdept = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
default: lua_assert(0); return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_step (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul;
|
||||
if (lim == 0)
|
||||
lim = (MAX_LUMEM-1)/2; /* no limit */
|
||||
g->gcdept += g->totalbytes - g->GCthreshold;
|
||||
do {
|
||||
lim -= singlestep(L);
|
||||
if (g->gcstate == GCSpause)
|
||||
break;
|
||||
} while (lim > 0);
|
||||
if (g->gcstate != GCSpause) {
|
||||
if (g->gcdept < GCSTEPSIZE)
|
||||
g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/
|
||||
else {
|
||||
g->gcdept -= GCSTEPSIZE;
|
||||
g->GCthreshold = g->totalbytes;
|
||||
}
|
||||
}
|
||||
else {
|
||||
lua_assert(g->totalbytes >= g->estimate);
|
||||
setthreshold(g);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_fullgc (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
if (g->gcstate <= GCSpropagate) {
|
||||
/* reset sweep marks to sweep all elements (returning them to white) */
|
||||
g->sweepstrgc = 0;
|
||||
g->sweepgc = &g->rootgc;
|
||||
/* reset other collector lists */
|
||||
g->gray = NULL;
|
||||
g->grayagain = NULL;
|
||||
g->weak = NULL;
|
||||
g->gcstate = GCSsweepstring;
|
||||
}
|
||||
lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate);
|
||||
/* finish any pending sweep phase */
|
||||
while (g->gcstate != GCSfinalize) {
|
||||
lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);
|
||||
singlestep(L);
|
||||
}
|
||||
markroot(L);
|
||||
while (g->gcstate != GCSpause) {
|
||||
singlestep(L);
|
||||
}
|
||||
setthreshold(g);
|
||||
}
|
||||
|
||||
|
||||
void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {
|
||||
global_State *g = G(L);
|
||||
lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
|
||||
lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
|
||||
lua_assert(ttype(&o->gch) != LUA_TTABLE);
|
||||
/* must keep invariant? */
|
||||
if (g->gcstate == GCSpropagate)
|
||||
reallymarkobject(g, v); /* restore invariant */
|
||||
else /* don't mind */
|
||||
makewhite(g, o); /* mark as white just to avoid other barriers */
|
||||
}
|
||||
|
||||
|
||||
void luaC_barrierback (lua_State *L, Table *t) {
|
||||
global_State *g = G(L);
|
||||
GCObject *o = obj2gco(t);
|
||||
lua_assert(isblack(o) && !isdead(g, o));
|
||||
lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
|
||||
black2gray(o); /* make table gray (again) */
|
||||
t->gclist = g->grayagain;
|
||||
g->grayagain = o;
|
||||
}
|
||||
|
||||
|
||||
void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
|
||||
global_State *g = G(L);
|
||||
o->gch.next = g->rootgc;
|
||||
g->rootgc = o;
|
||||
o->gch.marked = luaC_white(g);
|
||||
o->gch.tt = tt;
|
||||
}
|
||||
|
||||
|
||||
void luaC_linkupval (lua_State *L, UpVal *uv) {
|
||||
global_State *g = G(L);
|
||||
GCObject *o = obj2gco(uv);
|
||||
o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */
|
||||
g->rootgc = o;
|
||||
if (isgray(o)) {
|
||||
if (g->gcstate == GCSpropagate) {
|
||||
gray2black(o); /* closed upvalues need barrier */
|
||||
luaC_barrier(L, uv, uv->v);
|
||||
}
|
||||
else { /* sweep phase: sweep it (turning it into white) */
|
||||
makewhite(g, o);
|
||||
lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int markobject (TObject *o)
|
||||
{
|
||||
switch (ttype(o)) {
|
||||
case LUA_T_USERDATA: case LUA_T_STRING:
|
||||
strmark(tsvalue(o));
|
||||
break;
|
||||
case LUA_T_ARRAY:
|
||||
hashmark(avalue(o));
|
||||
break;
|
||||
case LUA_T_CLOSURE: case LUA_T_CLMARK:
|
||||
closuremark(o->value.cl);
|
||||
break;
|
||||
case LUA_T_PROTO: case LUA_T_PMARK:
|
||||
protomark(o->value.tf);
|
||||
break;
|
||||
default: break; /* numbers, cprotos, etc */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void markall (void)
|
||||
{
|
||||
luaD_travstack(markobject); /* mark stack objects */
|
||||
globalmark(); /* mark global variable values and names */
|
||||
travlock(); /* mark locked objects */
|
||||
luaT_travtagmethods(markobject); /* mark fallbacks */
|
||||
}
|
||||
|
||||
|
||||
long lua_collectgarbage (long limit)
|
||||
{
|
||||
unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */
|
||||
Hash *freetable;
|
||||
TaggedString *freestr;
|
||||
TProtoFunc *freefunc;
|
||||
Closure *freeclos;
|
||||
markall();
|
||||
invalidaterefs();
|
||||
freestr = luaS_collector();
|
||||
freetable = (Hash *)listcollect(&(L->roottable));
|
||||
freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
|
||||
freeclos = (Closure *)listcollect(&(L->rootcl));
|
||||
L->GCthreshold *= 4; /* to avoid GC during GC */
|
||||
luaC_hashcallIM(freetable); /* GC tag methods for tables */
|
||||
luaC_strcallIM(freestr); /* GC tag methods for userdata */
|
||||
luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
|
||||
luaH_free(freetable);
|
||||
luaS_free(freestr);
|
||||
luaF_freeproto(freefunc);
|
||||
luaF_freeclosure(freeclos);
|
||||
recovered = recovered-L->nblocks;
|
||||
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
||||
return recovered;
|
||||
}
|
||||
|
||||
|
||||
void luaC_checkGC (void)
|
||||
{
|
||||
if (L->nblocks >= L->GCthreshold)
|
||||
lua_collectgarbage(0);
|
||||
}
|
||||
|
||||
|
||||
101
lgc.h
101
lgc.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lgc.h,v 1.3 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** $Id: lgc.h,v 2.14 2005/06/07 18:53:45 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,11 +11,100 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaC_checkGC (void);
|
||||
TObject* luaC_getref (int ref);
|
||||
int luaC_ref (TObject *o, int lock);
|
||||
void luaC_hashcallIM (Hash *l);
|
||||
void luaC_strcallIM (TaggedString *l);
|
||||
/*
|
||||
** Possible states of the Garbage Collector
|
||||
*/
|
||||
#define GCSpause 0
|
||||
#define GCSpropagate 1
|
||||
#define GCSsweepstring 2
|
||||
#define GCSsweep 3
|
||||
#define GCSfinalize 4
|
||||
|
||||
|
||||
/*
|
||||
** some userful bit tricks
|
||||
*/
|
||||
#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
|
||||
#define setbits(x,m) ((x) |= (m))
|
||||
#define testbits(x,m) ((x) & (m))
|
||||
#define bitmask(b) (1<<(b))
|
||||
#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
|
||||
#define l_setbit(x,b) setbits(x, bitmask(b))
|
||||
#define resetbit(x,b) resetbits(x, bitmask(b))
|
||||
#define testbit(x,b) testbits(x, bitmask(b))
|
||||
#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
|
||||
#define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2)))
|
||||
#define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2)))
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Layout for bit use in `marked' field:
|
||||
** bit 0 - object is white (type 0)
|
||||
** bit 1 - object is white (type 1)
|
||||
** bit 2 - object is black
|
||||
** bit 3 - for userdata: has been finalized
|
||||
** bit 3 - for tables: has weak keys
|
||||
** bit 4 - for tables: has weak values
|
||||
** bit 5 - object is fixed (should not be collected)
|
||||
** bit 6 - object is "super" fixed (only the main thread)
|
||||
*/
|
||||
|
||||
|
||||
#define WHITE0BIT 0
|
||||
#define WHITE1BIT 1
|
||||
#define BLACKBIT 2
|
||||
#define FINALIZEDBIT 3
|
||||
#define KEYWEAKBIT 3
|
||||
#define VALUEWEAKBIT 4
|
||||
#define FIXEDBIT 5
|
||||
#define SFIXEDBIT 6
|
||||
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
|
||||
|
||||
|
||||
#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
|
||||
#define isblack(x) testbit((x)->gch.marked, BLACKBIT)
|
||||
#define isgray(x) (!isblack(x) && !iswhite(x))
|
||||
|
||||
#define otherwhite(g) (g->currentwhite ^ WHITEBITS)
|
||||
#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS)
|
||||
|
||||
#define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
|
||||
#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
|
||||
|
||||
#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
|
||||
|
||||
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
|
||||
|
||||
|
||||
#define luaC_checkGC(L) { \
|
||||
condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \
|
||||
if (G(L)->totalbytes >= G(L)->GCthreshold) \
|
||||
luaC_step(L); }
|
||||
|
||||
|
||||
#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
|
||||
luaC_barrierf(L,obj2gco(p),gcvalue(v)); }
|
||||
|
||||
#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \
|
||||
luaC_barrierback(L,t); }
|
||||
|
||||
#define luaC_objbarrier(L,p,o) \
|
||||
{ if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
|
||||
luaC_barrierf(L,obj2gco(p),obj2gco(o)); }
|
||||
|
||||
#define luaC_objbarriert(L,t,o) \
|
||||
{ if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); }
|
||||
|
||||
LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all);
|
||||
LUAI_FUNC void luaC_callGCTM (lua_State *L);
|
||||
LUAI_FUNC void luaC_freeall (lua_State *L);
|
||||
LUAI_FUNC void luaC_step (lua_State *L);
|
||||
LUAI_FUNC void luaC_fullgc (lua_State *L);
|
||||
LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
|
||||
LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv);
|
||||
LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);
|
||||
LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
33
linit.c
33
linit.c
@@ -1,17 +1,38 @@
|
||||
/*
|
||||
** $Id: linit.c,v 1.1 1999/01/08 16:47:44 roberto Exp $
|
||||
** $Id: linit.c,v 1.13 2005/08/26 17:36:32 roberto Exp roberto $
|
||||
** Initialization of libraries for lua.c
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#define linit_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lualib.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
|
||||
void lua_userinit (void) {
|
||||
lua_iolibopen();
|
||||
lua_strlibopen();
|
||||
lua_mathlibopen();
|
||||
lua_dblibopen();
|
||||
static const luaL_Reg lualibs[] = {
|
||||
{"", luaopen_base},
|
||||
{LUA_LOADLIBNAME, luaopen_package},
|
||||
{LUA_TABLIBNAME, luaopen_table},
|
||||
{LUA_IOLIBNAME, luaopen_io},
|
||||
{LUA_OSLIBNAME, luaopen_os},
|
||||
{LUA_STRLIBNAME, luaopen_string},
|
||||
{LUA_MATHLIBNAME, luaopen_math},
|
||||
{LUA_DBLIBNAME, luaopen_debug},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlibs (lua_State *L) {
|
||||
const luaL_Reg *lib = lualibs;
|
||||
for (; lib->func; lib++) {
|
||||
lua_pushcfunction(L, lib->func);
|
||||
lua_pushstring(L, lib->name);
|
||||
lua_call(L, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
730
llex.c
730
llex.c
@@ -1,437 +1,461 @@
|
||||
/*
|
||||
** $Id: llex.c,v 1.35 1999/05/14 12:24:04 roberto Exp roberto $
|
||||
** $Id: llex.c,v 2.19 2006/02/06 18:28:16 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <locale.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#define llex_c
|
||||
#define LUA_CORE
|
||||
|
||||
#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 "ltable.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
#define next(LS) (LS->current = zgetc(LS->lex_z))
|
||||
#define next(ls) (ls->current = zgetc(ls->z))
|
||||
|
||||
|
||||
#define save(c) luaL_addchar(c)
|
||||
#define save_and_next(LS) (save(LS->current), next(LS))
|
||||
|
||||
|
||||
char *reserved [] = {"and", "do", "else", "elseif", "end", "function",
|
||||
"if", "local", "nil", "not", "or", "repeat", "return", "then",
|
||||
"until", "while"};
|
||||
#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
|
||||
|
||||
|
||||
void luaX_init (void) {
|
||||
/* ORDER RESERVED */
|
||||
const char *const luaX_tokens [] = {
|
||||
"and", "break", "do", "else", "elseif",
|
||||
"end", "false", "for", "function", "if",
|
||||
"in", "local", "nil", "not", "or", "repeat",
|
||||
"return", "then", "true", "until", "while",
|
||||
"..", "...", "==", ">=", "<=", "~=",
|
||||
"<number>", "<name>", "<string>", "<eof>",
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
#define save_and_next(ls) (save(ls, ls->current), next(ls))
|
||||
|
||||
|
||||
static void save (LexState *ls, int c) {
|
||||
Mbuffer *b = ls->buff;
|
||||
if (b->n + 1 > b->buffsize) {
|
||||
size_t newsize;
|
||||
if (b->buffsize >= MAX_SIZET/2)
|
||||
luaX_lexerror(ls, "lexical element too long", 0);
|
||||
newsize = b->buffsize * 2;
|
||||
luaZ_resizebuffer(ls->L, b, newsize);
|
||||
}
|
||||
b->buffer[b->n++] = cast(char, c);
|
||||
}
|
||||
|
||||
|
||||
void luaX_init (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
|
||||
TaggedString *ts = luaS_new(reserved[i]);
|
||||
ts->head.marked = FIRST_RESERVED+i; /* reserved word (always > 255) */
|
||||
for (i=0; i<NUM_RESERVED; i++) {
|
||||
TString *ts = luaS_new(L, luaX_tokens[i]);
|
||||
luaS_fix(ts); /* reserved words are never collected */
|
||||
lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
|
||||
ts->tsv.reserved = cast_byte(i+1); /* reserved word */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define MAXSRC 80
|
||||
|
||||
void luaX_syntaxerror (LexState *ls, char *s, char *token) {
|
||||
char buff[MAXSRC];
|
||||
luaL_chunkid(buff, zname(ls->lex_z), sizeof(buff));
|
||||
if (token[0] == '\0')
|
||||
token = "<eof>";
|
||||
luaL_verror("%.100s;\n last token read: `%.50s' at line %d in %.80s",
|
||||
s, token, ls->linenumber, buff);
|
||||
}
|
||||
|
||||
|
||||
void luaX_error (LexState *ls, char *s) {
|
||||
save('\0');
|
||||
luaX_syntaxerror(ls, s, luaL_buffer());
|
||||
}
|
||||
|
||||
|
||||
void luaX_token2str (int token, char *s) {
|
||||
if (token < 255) {
|
||||
s[0] = (char)token;
|
||||
s[1] = '\0';
|
||||
const char *luaX_token2str (LexState *ls, int token) {
|
||||
if (token < FIRST_RESERVED) {
|
||||
lua_assert(token == cast(unsigned char, token));
|
||||
return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) :
|
||||
luaO_pushfstring(ls->L, "%c", token);
|
||||
}
|
||||
else
|
||||
strcpy(s, reserved[token-FIRST_RESERVED]);
|
||||
return luaX_tokens[token-FIRST_RESERVED];
|
||||
}
|
||||
|
||||
|
||||
static void luaX_invalidchar (LexState *ls, int c) {
|
||||
char buff[8];
|
||||
sprintf(buff, "0x%02X", c);
|
||||
luaX_syntaxerror(ls, "invalid control char", buff);
|
||||
static const char *txtToken (LexState *ls, int token) {
|
||||
switch (token) {
|
||||
case TK_NAME:
|
||||
case TK_STRING:
|
||||
case TK_NUMBER:
|
||||
save(ls, '\0');
|
||||
return luaZ_buffer(ls->buff);
|
||||
default:
|
||||
return luaX_token2str(ls, token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void firstline (LexState *LS)
|
||||
{
|
||||
int c = zgetc(LS->lex_z);
|
||||
if (c == '#')
|
||||
while ((c=zgetc(LS->lex_z)) != '\n' && c != EOZ) /* skip first line */;
|
||||
zungetc(LS->lex_z);
|
||||
void luaX_lexerror (LexState *ls, const char *msg, int token) {
|
||||
char buff[MAXSRC];
|
||||
luaO_chunkid(buff, getstr(ls->source), MAXSRC);
|
||||
msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
|
||||
if (token)
|
||||
luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token));
|
||||
luaD_throw(ls->L, LUA_ERRSYNTAX);
|
||||
}
|
||||
|
||||
|
||||
void luaX_setinput (LexState *LS, ZIO *z)
|
||||
{
|
||||
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->lex_z = z;
|
||||
LS->fs = NULL;
|
||||
firstline(LS);
|
||||
luaL_resetbuffer();
|
||||
void luaX_syntaxerror (LexState *ls, const char *msg) {
|
||||
luaX_lexerror(ls, msg, ls->t.token);
|
||||
}
|
||||
|
||||
|
||||
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||
lua_State *L = ls->L;
|
||||
TString *ts = luaS_newlstr(L, str, l);
|
||||
TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */
|
||||
if (ttisnil(o))
|
||||
setbvalue(o, 1); /* make sure `str' will not be collected */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *ls) {
|
||||
int old = ls->current;
|
||||
lua_assert(currIsNewline(ls));
|
||||
next(ls); /* skip `\n' or `\r' */
|
||||
if (currIsNewline(ls) && ls->current != old)
|
||||
next(ls); /* skip `\n\r' or `\r\n' */
|
||||
if (++ls->linenumber >= MAX_INT)
|
||||
luaX_syntaxerror(ls, "chunk has too many lines");
|
||||
}
|
||||
|
||||
|
||||
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
|
||||
ls->decpoint = '.';
|
||||
ls->L = L;
|
||||
ls->lookahead.token = TK_EOS; /* no look-ahead token */
|
||||
ls->z = z;
|
||||
ls->fs = NULL;
|
||||
ls->linenumber = 1;
|
||||
ls->lastline = 1;
|
||||
ls->source = source;
|
||||
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
|
||||
next(ls); /* read first char */
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** PRAGMAS
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#ifndef PRAGMASIZE
|
||||
#define PRAGMASIZE 80 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
static void skipspace (LexState *LS) {
|
||||
while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
|
||||
next(LS);
|
||||
}
|
||||
|
||||
|
||||
static int checkcond (LexState *LS, char *buff) {
|
||||
static char *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(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 (LexState *LS);
|
||||
|
||||
|
||||
static void ifskip (LexState *LS) {
|
||||
while (LS->ifstate[LS->iflevel].skip) {
|
||||
if (LS->current == '\n')
|
||||
inclinenumber(LS);
|
||||
else if (LS->current == EOZ)
|
||||
luaX_error(LS, "input ends inside a $if");
|
||||
else next(LS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *LS) {
|
||||
static char *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(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(LS);
|
||||
else if (LS->current != EOZ) /* or eof */
|
||||
luaX_syntaxerror(LS, "invalid pragma format", buff);
|
||||
ifskip(LS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** LEXICAL ANALIZER
|
||||
** LEXICAL ANALYZER
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
|
||||
static int read_long_string (LexState *LS) {
|
||||
static int check_next (LexState *ls, const char *set) {
|
||||
if (!strchr(set, ls->current))
|
||||
return 0;
|
||||
save_and_next(ls);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void buffreplace (LexState *ls, char from, char to) {
|
||||
size_t n = luaZ_bufflen(ls->buff);
|
||||
char *p = luaZ_buffer(ls->buff);
|
||||
while (n--)
|
||||
if (p[n] == from) p[n] = to;
|
||||
}
|
||||
|
||||
|
||||
static void trydecpoint (LexState *ls, SemInfo *seminfo) {
|
||||
/* format error: try to update decimal point separator */
|
||||
struct lconv *cv = localeconv();
|
||||
char old = ls->decpoint;
|
||||
ls->decpoint = (cv ? cv->decimal_point[0] : '.');
|
||||
buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
|
||||
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
|
||||
/* format error with correct decimal point: no more options */
|
||||
buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
|
||||
luaX_lexerror(ls, "malformed number", TK_NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* LUA_NUMBER */
|
||||
static void read_numeral (LexState *ls, SemInfo *seminfo) {
|
||||
lua_assert(isdigit(ls->current));
|
||||
do {
|
||||
save_and_next(ls);
|
||||
} while (isdigit(ls->current) || ls->current == '.');
|
||||
if (check_next(ls, "Ee")) /* `E'? */
|
||||
check_next(ls, "+-"); /* optional exponent sign */
|
||||
while (isalnum(ls->current) || ls->current == '_')
|
||||
save_and_next(ls);
|
||||
save(ls, '\0');
|
||||
buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
|
||||
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
|
||||
trydecpoint(ls, seminfo); /* try to update decimal point separator */
|
||||
}
|
||||
|
||||
|
||||
static int skip_sep (LexState *ls) {
|
||||
int count = 0;
|
||||
int s = ls->current;
|
||||
lua_assert(s == '[' || s == ']');
|
||||
save_and_next(ls);
|
||||
while (ls->current == '=') {
|
||||
save_and_next(ls);
|
||||
count++;
|
||||
}
|
||||
return (ls->current == s) ? count : (-count) - 1;
|
||||
}
|
||||
|
||||
|
||||
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
||||
int cont = 0;
|
||||
(void)(cont); /* avoid warnings when `cont' is not used */
|
||||
save_and_next(ls); /* skip 2nd `[' */
|
||||
if (currIsNewline(ls)) /* string starts with a newline? */
|
||||
inclinenumber(ls); /* skip it */
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
switch (ls->current) {
|
||||
case EOZ:
|
||||
luaX_error(LS, "unfinished long string");
|
||||
return EOS; /* to avoid warnings */
|
||||
case '[':
|
||||
save_and_next(LS);
|
||||
if (LS->current == '[') {
|
||||
luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
|
||||
"unfinished long comment", TK_EOS);
|
||||
break; /* to avoid warnings */
|
||||
#if defined(LUA_COMPAT_LSTR)
|
||||
case '[': {
|
||||
if (skip_sep(ls) == sep) {
|
||||
save_and_next(ls); /* skip 2nd `[' */
|
||||
cont++;
|
||||
save_and_next(LS);
|
||||
#if LUA_COMPAT_LSTR == 1
|
||||
if (sep == 0)
|
||||
luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
|
||||
#endif
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next(LS);
|
||||
if (LS->current == ']') {
|
||||
if (cont == 0) goto endloop;
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
case ']': {
|
||||
if (skip_sep(ls) == sep) {
|
||||
save_and_next(ls); /* skip 2nd `]' */
|
||||
#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
|
||||
cont--;
|
||||
save_and_next(LS);
|
||||
if (sep == 0 && cont >= 0) break;
|
||||
#endif
|
||||
goto endloop;
|
||||
}
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
case '\n':
|
||||
save('\n');
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
default:
|
||||
save_and_next(LS);
|
||||
case '\r': {
|
||||
save(ls, '\n');
|
||||
inclinenumber(ls);
|
||||
if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
if (seminfo) save_and_next(ls);
|
||||
else next(ls);
|
||||
}
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(LS); /* skip the second ']' */
|
||||
LS->seminfo.ts = luaS_newlstr(L->Mbuffer+(L->Mbuffbase+2),
|
||||
L->Mbuffnext-L->Mbuffbase-4);
|
||||
return STRING;
|
||||
if (seminfo)
|
||||
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
|
||||
luaZ_bufflen(ls->buff) - 2*(2 + sep));
|
||||
}
|
||||
|
||||
|
||||
int luaX_lex (LexState *LS) {
|
||||
luaL_resetbuffer();
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
|
||||
case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
|
||||
next(LS);
|
||||
continue;
|
||||
|
||||
static void read_string (LexState *ls, int del, SemInfo *seminfo) {
|
||||
save_and_next(ls);
|
||||
while (ls->current != del) {
|
||||
switch (ls->current) {
|
||||
case EOZ:
|
||||
luaX_lexerror(ls, "unfinished string", TK_EOS);
|
||||
continue; /* to avoid warnings */
|
||||
case '\n':
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
|
||||
case '-':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '-') return '-';
|
||||
do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
|
||||
luaL_resetbuffer();
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '[') return '[';
|
||||
else {
|
||||
save_and_next(LS); /* pass the second '[' */
|
||||
return read_long_string(LS);
|
||||
case '\r':
|
||||
luaX_lexerror(ls, "unfinished string", TK_STRING);
|
||||
continue; /* to avoid warnings */
|
||||
case '\\': {
|
||||
int c;
|
||||
next(ls); /* do not save the `\' */
|
||||
switch (ls->current) {
|
||||
case 'a': c = '\a'; break;
|
||||
case 'b': c = '\b'; break;
|
||||
case 'f': c = '\f'; break;
|
||||
case 'n': c = '\n'; break;
|
||||
case 'r': c = '\r'; break;
|
||||
case 't': c = '\t'; break;
|
||||
case 'v': c = '\v'; break;
|
||||
case '\n': /* go through */
|
||||
case '\r': save(ls, '\n'); inclinenumber(ls); continue;
|
||||
case EOZ: continue; /* will raise an error next loop */
|
||||
default: {
|
||||
if (!isdigit(ls->current))
|
||||
save_and_next(ls); /* handles \\, \", \', and \? */
|
||||
else { /* \xxx */
|
||||
int i = 0;
|
||||
c = 0;
|
||||
do {
|
||||
c = 10*c + (ls->current-'0');
|
||||
next(ls);
|
||||
} while (++i<3 && isdigit(ls->current));
|
||||
if (c > UCHAR_MAX)
|
||||
luaX_lexerror(ls, "escape sequence too large", TK_STRING);
|
||||
save(ls, c);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
save(ls, c);
|
||||
next(ls);
|
||||
continue;
|
||||
}
|
||||
default:
|
||||
save_and_next(ls);
|
||||
}
|
||||
}
|
||||
save_and_next(ls); /* skip delimiter */
|
||||
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
|
||||
luaZ_bufflen(ls->buff) - 2);
|
||||
}
|
||||
|
||||
case '=':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '=';
|
||||
else { save_and_next(LS); return EQ; }
|
||||
|
||||
case '<':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '<';
|
||||
else { save_and_next(LS); return LE; }
|
||||
|
||||
case '>':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '>';
|
||||
else { save_and_next(LS); return GE; }
|
||||
|
||||
case '~':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '~';
|
||||
else { save_and_next(LS); return NE; }
|
||||
|
||||
static int llex (LexState *ls, SemInfo *seminfo) {
|
||||
luaZ_resetbuffer(ls->buff);
|
||||
for (;;) {
|
||||
switch (ls->current) {
|
||||
case '\n':
|
||||
case '\r': {
|
||||
inclinenumber(ls);
|
||||
continue;
|
||||
}
|
||||
case '-': {
|
||||
next(ls);
|
||||
if (ls->current != '-') return '-';
|
||||
/* else is a comment */
|
||||
next(ls);
|
||||
if (ls->current == '[') {
|
||||
int sep = skip_sep(ls);
|
||||
luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
|
||||
if (sep >= 0) {
|
||||
read_long_string(ls, NULL, sep); /* long comment */
|
||||
luaZ_resetbuffer(ls->buff);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* else short comment */
|
||||
while (!currIsNewline(ls) && ls->current != EOZ)
|
||||
next(ls);
|
||||
continue;
|
||||
}
|
||||
case '[': {
|
||||
int sep = skip_sep(ls);
|
||||
if (sep >= 0) {
|
||||
read_long_string(ls, seminfo, sep);
|
||||
return TK_STRING;
|
||||
}
|
||||
else if (sep == -1) return '[';
|
||||
else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING);
|
||||
}
|
||||
case '=': {
|
||||
next(ls);
|
||||
if (ls->current != '=') return '=';
|
||||
else { next(ls); return TK_EQ; }
|
||||
}
|
||||
case '<': {
|
||||
next(ls);
|
||||
if (ls->current != '=') return '<';
|
||||
else { next(ls); return TK_LE; }
|
||||
}
|
||||
case '>': {
|
||||
next(ls);
|
||||
if (ls->current != '=') return '>';
|
||||
else { next(ls); return TK_GE; }
|
||||
}
|
||||
case '~': {
|
||||
next(ls);
|
||||
if (ls->current != '=') return '~';
|
||||
else { next(ls); return TK_NE; }
|
||||
}
|
||||
case '"':
|
||||
case '\'': {
|
||||
int del = LS->current;
|
||||
save_and_next(LS);
|
||||
while (LS->current != del) {
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
case '\n':
|
||||
luaX_error(LS, "unfinished string");
|
||||
return EOS; /* to avoid warnings */
|
||||
case '\\':
|
||||
next(LS); /* do not save the '\' */
|
||||
switch (LS->current) {
|
||||
case 'a': save('\a'); next(LS); break;
|
||||
case 'b': save('\b'); next(LS); break;
|
||||
case 'f': save('\f'); next(LS); break;
|
||||
case 'n': save('\n'); next(LS); break;
|
||||
case 'r': save('\r'); next(LS); break;
|
||||
case 't': save('\t'); next(LS); break;
|
||||
case 'v': save('\v'); next(LS); break;
|
||||
case '\n': save('\n'); inclinenumber(LS); break;
|
||||
default : {
|
||||
if (isdigit(LS->current)) {
|
||||
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");
|
||||
save(c);
|
||||
}
|
||||
else { /* handles \, ", ', and ? */
|
||||
save(LS->current);
|
||||
next(LS);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next(LS);
|
||||
}
|
||||
}
|
||||
save_and_next(LS); /* skip delimiter */
|
||||
LS->seminfo.ts = luaS_newlstr(L->Mbuffer+(L->Mbuffbase+1),
|
||||
L->Mbuffnext-L->Mbuffbase-2);
|
||||
return STRING;
|
||||
read_string(ls, ls->current, seminfo);
|
||||
return TK_STRING;
|
||||
}
|
||||
|
||||
case '.':
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.')
|
||||
{
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.')
|
||||
{
|
||||
save_and_next(LS);
|
||||
return DOTS; /* ... */
|
||||
}
|
||||
else return CONC; /* .. */
|
||||
case '.': {
|
||||
save_and_next(ls);
|
||||
if (check_next(ls, ".")) {
|
||||
if (check_next(ls, "."))
|
||||
return TK_DOTS; /* ... */
|
||||
else return TK_CONCAT; /* .. */
|
||||
}
|
||||
else if (!isdigit(LS->current)) return '.';
|
||||
goto fraction; /* LS->current is a digit: goes through to number */
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
do {
|
||||
save_and_next(LS);
|
||||
} while (isdigit(LS->current));
|
||||
if (LS->current == '.') {
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.') {
|
||||
save('.');
|
||||
luaX_error(LS,
|
||||
"ambiguous syntax (decimal point x string concatenation)");
|
||||
}
|
||||
else if (!isdigit(ls->current)) return '.';
|
||||
else {
|
||||
read_numeral(ls, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
fraction:
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(LS);
|
||||
if (toupper(LS->current) == 'E') {
|
||||
save_and_next(LS); /* read 'E' */
|
||||
save_and_next(LS); /* read '+', '-' or first digit */
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(LS);
|
||||
}
|
||||
case EOZ: {
|
||||
return TK_EOS;
|
||||
}
|
||||
default: {
|
||||
if (isspace(ls->current)) {
|
||||
lua_assert(!currIsNewline(ls));
|
||||
next(ls);
|
||||
continue;
|
||||
}
|
||||
save('\0');
|
||||
LS->seminfo.r = luaO_str2d(L->Mbuffer+L->Mbuffbase);
|
||||
if (LS->seminfo.r < 0)
|
||||
luaX_error(LS, "invalid numeric format");
|
||||
return NUMBER;
|
||||
|
||||
case EOZ:
|
||||
if (LS->iflevel > 0)
|
||||
luaX_error(LS, "input ends inside a $if");
|
||||
return EOS;
|
||||
|
||||
default:
|
||||
if (LS->current != '_' && !isalpha(LS->current)) {
|
||||
int c = LS->current;
|
||||
if (iscntrl(c))
|
||||
luaX_invalidchar(LS, c);
|
||||
save_and_next(LS);
|
||||
return c;
|
||||
else if (isdigit(ls->current)) {
|
||||
read_numeral(ls, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
else { /* identifier or reserved word */
|
||||
TaggedString *ts;
|
||||
else if (isalpha(ls->current) || ls->current == '_') {
|
||||
/* identifier or reserved word */
|
||||
TString *ts;
|
||||
do {
|
||||
save_and_next(LS);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save('\0');
|
||||
ts = luaS_new(L->Mbuffer+L->Mbuffbase);
|
||||
if (ts->head.marked >= FIRST_RESERVED)
|
||||
return ts->head.marked; /* reserved word */
|
||||
LS->seminfo.ts = ts;
|
||||
return NAME;
|
||||
save_and_next(ls);
|
||||
} while (isalnum(ls->current) || ls->current == '_');
|
||||
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
|
||||
luaZ_bufflen(ls->buff));
|
||||
if (ts->tsv.reserved > 0) /* reserved word? */
|
||||
return ts->tsv.reserved - 1 + FIRST_RESERVED;
|
||||
else {
|
||||
seminfo->ts = ts;
|
||||
return TK_NAME;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int c = ls->current;
|
||||
next(ls);
|
||||
return c; /* single-char tokens (+ - / ...) */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaX_next (LexState *ls) {
|
||||
ls->lastline = ls->linenumber;
|
||||
if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
|
||||
ls->t = ls->lookahead; /* use this one */
|
||||
ls->lookahead.token = TK_EOS; /* and discharge it */
|
||||
}
|
||||
else
|
||||
ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
|
||||
}
|
||||
|
||||
|
||||
void luaX_lookahead (LexState *ls) {
|
||||
lua_assert(ls->lookahead.token == TK_EOS);
|
||||
ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
|
||||
}
|
||||
|
||||
|
||||
89
llex.h
89
llex.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.11 1999/02/25 19:13:56 roberto Exp roberto $
|
||||
** $Id: llex.h,v 1.57 2005/12/07 15:43:05 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,54 +11,71 @@
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
#define FIRST_RESERVED 260
|
||||
#define FIRST_RESERVED 257
|
||||
|
||||
/* maximum length of a reserved word (+1 for terminal 0) */
|
||||
#define TOKEN_LEN 15
|
||||
/* maximum length of a reserved word */
|
||||
#define TOKEN_LEN (sizeof("function")/sizeof(char))
|
||||
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER RESERVED"
|
||||
*/
|
||||
enum RESERVED {
|
||||
/* terminal symbols denoted by reserved words */
|
||||
AND = FIRST_RESERVED,
|
||||
DO, ELSE, ELSEIF, END, FUNCTION, IF, LOCAL, NIL, NOT, OR,
|
||||
REPEAT, RETURN, THEN, UNTIL, WHILE,
|
||||
TK_AND = FIRST_RESERVED, TK_BREAK,
|
||||
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 */
|
||||
NAME, CONC, DOTS, EQ, GE, LE, NE, NUMBER, STRING, EOS};
|
||||
|
||||
|
||||
#ifndef MAX_IFS
|
||||
#define MAX_IFS 5 /* arbitrary limit */
|
||||
#endif
|
||||
|
||||
/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
|
||||
|
||||
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 */
|
||||
TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
|
||||
TK_NAME, TK_STRING, TK_EOS
|
||||
};
|
||||
|
||||
/* number of reserved words */
|
||||
#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
|
||||
|
||||
|
||||
/* array with token `names' */
|
||||
LUAI_DATA const char *const luaX_tokens [];
|
||||
|
||||
|
||||
typedef union {
|
||||
lua_Number r;
|
||||
TString *ts;
|
||||
} SemInfo; /* semantics information */
|
||||
|
||||
|
||||
typedef struct Token {
|
||||
int token;
|
||||
SemInfo seminfo;
|
||||
} Token;
|
||||
|
||||
|
||||
typedef struct LexState {
|
||||
int current; /* look ahead character */
|
||||
int token; /* look ahead token */
|
||||
struct FuncState *fs; /* 'FuncState' is private for the parser */
|
||||
union {
|
||||
real r;
|
||||
TaggedString *ts;
|
||||
} seminfo; /* semantics information */
|
||||
struct zio *lex_z; /* input stream */
|
||||
int current; /* current character (charint) */
|
||||
int linenumber; /* input line counter */
|
||||
int iflevel; /* level of nested $if's (for lexical analysis) */
|
||||
struct ifState ifstate[MAX_IFS];
|
||||
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;
|
||||
ZIO *z; /* input stream */
|
||||
Mbuffer *buff; /* buffer for tokens */
|
||||
TString *source; /* current source name */
|
||||
char decpoint; /* locale decimal point */
|
||||
} LexState;
|
||||
|
||||
|
||||
void luaX_init (void);
|
||||
void luaX_setinput (LexState *LS, ZIO *z);
|
||||
int luaX_lex (LexState *LS);
|
||||
void luaX_syntaxerror (LexState *ls, char *s, char *token);
|
||||
void luaX_error (LexState *ls, char *s);
|
||||
void luaX_token2str (int token, char *s);
|
||||
LUAI_FUNC void luaX_init (lua_State *L);
|
||||
LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
|
||||
TString *source);
|
||||
LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
|
||||
LUAI_FUNC void luaX_next (LexState *ls);
|
||||
LUAI_FUNC void luaX_lookahead (LexState *ls);
|
||||
LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token);
|
||||
LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s);
|
||||
LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
128
llimits.h
Normal file
128
llimits.h
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.68 2005/12/22 16:19:56 roberto Exp roberto $
|
||||
** Limits, basic types, and some other `installation-dependent' definitions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef llimits_h
|
||||
#define llimits_h
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
typedef LUAI_UINT32 lu_int32;
|
||||
|
||||
typedef LUAI_UMEM lu_mem;
|
||||
|
||||
typedef LUAI_MEM l_mem;
|
||||
|
||||
|
||||
|
||||
/* 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_LUMEM ((lu_mem)(~(lu_mem)0)-2)
|
||||
|
||||
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
|
||||
|
||||
/*
|
||||
** conversion of pointer to integer
|
||||
** this is for hashing only; there is no problem if the integer
|
||||
** cannot hold the whole pointer value
|
||||
*/
|
||||
#define IntPoint(p) ((unsigned int)(lu_mem)(p))
|
||||
|
||||
|
||||
|
||||
/* type to ensure maximum alignment */
|
||||
typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
|
||||
|
||||
|
||||
/* result of a `usual argument conversion' over lua_Number */
|
||||
typedef LUAI_UACNUMBER l_uacNumber;
|
||||
|
||||
|
||||
/* internal assertions for in-house debugging */
|
||||
#ifdef lua_assert
|
||||
|
||||
#define check_exp(c,e) (lua_assert(c), (e))
|
||||
#define api_check(l,e) lua_assert(e)
|
||||
|
||||
#else
|
||||
|
||||
#define lua_assert(c) ((void)0)
|
||||
#define check_exp(c,e) (e)
|
||||
#define api_check luai_apicheck
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef UNUSED
|
||||
#define UNUSED(x) ((void)(x)) /* to avoid warnings */
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef cast
|
||||
#define cast(t, exp) ((t)(exp))
|
||||
#endif
|
||||
|
||||
#define cast_byte(i) cast(lu_byte, (i))
|
||||
#define cast_num(i) cast(lua_Number, (i))
|
||||
#define cast_int(i) cast(int, (i))
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** type for virtual-machine instructions
|
||||
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||
*/
|
||||
typedef lu_int32 Instruction;
|
||||
|
||||
|
||||
|
||||
/* maximum stack for a Lua function */
|
||||
#define MAXSTACK 250
|
||||
|
||||
|
||||
|
||||
/* minimum size for the string table (must be power of 2) */
|
||||
#ifndef MINSTRTABSIZE
|
||||
#define MINSTRTABSIZE 32
|
||||
#endif
|
||||
|
||||
|
||||
/* minimum size for string buffer */
|
||||
#ifndef LUA_MINBUFFER
|
||||
#define LUA_MINBUFFER 32
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef lua_lock
|
||||
#define lua_lock(L) ((void) 0)
|
||||
#define lua_unlock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
#ifndef luai_threadyield
|
||||
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** macro to control inclusion of some hard tests on stack reallocation
|
||||
*/
|
||||
#ifndef HARDSTACKTESTS
|
||||
#define condhardstacktests(x) ((void)0)
|
||||
#else
|
||||
#define condhardstacktests(x) x
|
||||
#endif
|
||||
|
||||
#endif
|
||||
294
lmathlib.c
294
lmathlib.c
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.16 1999/02/19 17:33:35 roberto Exp roberto $
|
||||
** Lua standard mathematical library
|
||||
** $Id: lmathlib.c,v 1.66 2005/08/15 14:12:32 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -8,196 +8,256 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#define lmathlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#undef PI
|
||||
#define PI (3.14159265358979323846)
|
||||
#define RADIANS_PER_DEGREE (PI/180.0)
|
||||
#define PI (3.14159265358979323846)
|
||||
#define RADIANS_PER_DEGREE (PI/180.0)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** If you want Lua to operate in radians (instead of degrees),
|
||||
** define RADIANS
|
||||
*/
|
||||
#ifdef RADIANS
|
||||
#define FROMRAD(a) (a)
|
||||
#define TORAD(a) (a)
|
||||
#else
|
||||
#define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
|
||||
#define TORAD(a) ((a)*RADIANS_PER_DEGREE)
|
||||
#endif
|
||||
|
||||
|
||||
static void math_abs (void) {
|
||||
lua_pushnumber(fabs(luaL_check_number(1)));
|
||||
static int math_abs (lua_State *L) {
|
||||
lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_sin (void) {
|
||||
lua_pushnumber(sin(TORAD(luaL_check_number(1))));
|
||||
static int math_sin (lua_State *L) {
|
||||
lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_cos (void) {
|
||||
lua_pushnumber(cos(TORAD(luaL_check_number(1))));
|
||||
static int math_sinh (lua_State *L) {
|
||||
lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_tan (void) {
|
||||
lua_pushnumber(tan(TORAD(luaL_check_number(1))));
|
||||
static int math_cos (lua_State *L) {
|
||||
lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_asin (void) {
|
||||
lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
|
||||
static int math_cosh (lua_State *L) {
|
||||
lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_acos (void) {
|
||||
lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
|
||||
static int math_tan (lua_State *L) {
|
||||
lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_atan (void) {
|
||||
lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
|
||||
static int math_tanh (lua_State *L) {
|
||||
lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_atan2 (void) {
|
||||
lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2))));
|
||||
static int math_asin (lua_State *L) {
|
||||
lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_ceil (void) {
|
||||
lua_pushnumber(ceil(luaL_check_number(1)));
|
||||
static int math_acos (lua_State *L) {
|
||||
lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_floor (void) {
|
||||
lua_pushnumber(floor(luaL_check_number(1)));
|
||||
static int math_atan (lua_State *L) {
|
||||
lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_mod (void) {
|
||||
lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
|
||||
static int math_atan2 (lua_State *L) {
|
||||
lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_sqrt (void) {
|
||||
lua_pushnumber(sqrt(luaL_check_number(1)));
|
||||
static int math_ceil (lua_State *L) {
|
||||
lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_pow (void) {
|
||||
lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
|
||||
static int math_floor (lua_State *L) {
|
||||
lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_log (void) {
|
||||
lua_pushnumber(log(luaL_check_number(1)));
|
||||
static int math_fmod (lua_State *L) {
|
||||
lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_log10 (void) {
|
||||
lua_pushnumber(log10(luaL_check_number(1)));
|
||||
static int math_modf (lua_State *L) {
|
||||
double ip;
|
||||
double fp = modf(luaL_checknumber(L, 1), &ip);
|
||||
lua_pushnumber(L, ip);
|
||||
lua_pushnumber(L, fp);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static void math_exp (void) {
|
||||
lua_pushnumber(exp(luaL_check_number(1)));
|
||||
static int math_sqrt (lua_State *L) {
|
||||
lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_deg (void) {
|
||||
lua_pushnumber(luaL_check_number(1)/RADIANS_PER_DEGREE);
|
||||
static int math_pow (lua_State *L) {
|
||||
lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_rad (void) {
|
||||
lua_pushnumber(luaL_check_number(1)*RADIANS_PER_DEGREE);
|
||||
static int math_log (lua_State *L) {
|
||||
lua_pushnumber(L, log(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void math_frexp (void) {
|
||||
static int math_log10 (lua_State *L) {
|
||||
lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_exp (lua_State *L) {
|
||||
lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_deg (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_rad (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_frexp (lua_State *L) {
|
||||
int e;
|
||||
lua_pushnumber(frexp(luaL_check_number(1), &e));
|
||||
lua_pushnumber(e);
|
||||
lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
|
||||
lua_pushinteger(L, e);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static void math_ldexp (void) {
|
||||
lua_pushnumber(ldexp(luaL_check_number(1), luaL_check_int(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 (void) {
|
||||
int i = 1;
|
||||
double dmin = luaL_check_number(i);
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT) {
|
||||
double d = luaL_check_number(i);
|
||||
static int math_min (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
lua_Number dmin = luaL_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(dmin);
|
||||
lua_pushnumber(L, dmin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_max (void) {
|
||||
int i = 1;
|
||||
double dmax = luaL_check_number(i);
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT) {
|
||||
double d = luaL_check_number(i);
|
||||
static int math_max (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
lua_Number dmax = luaL_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(dmax);
|
||||
lua_pushnumber(L, dmax);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_random (void) {
|
||||
/* the '%' avoids the (rare) case of r==1, and is needed also because on
|
||||
some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */
|
||||
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
||||
int l = luaL_opt_int(1, 0);
|
||||
if (l == 0)
|
||||
lua_pushnumber(r);
|
||||
else {
|
||||
int u = luaL_opt_int(2, 0);
|
||||
if (u == 0) {
|
||||
u = l;
|
||||
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;
|
||||
}
|
||||
luaL_arg_check(l<=u, 1, "interval is empty");
|
||||
lua_pushnumber((int)(r*(u-l+1))+l);
|
||||
case 1: { /* only upper limit */
|
||||
int u = luaL_checkint(L, 1);
|
||||
luaL_argcheck(L, 1<=u, 1, "interval is empty");
|
||||
lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
|
||||
break;
|
||||
}
|
||||
case 2: { /* lower and upper limits */
|
||||
int l = luaL_checkint(L, 1);
|
||||
int u = luaL_checkint(L, 2);
|
||||
luaL_argcheck(L, l<=u, 2, "interval is empty");
|
||||
lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
|
||||
break;
|
||||
}
|
||||
default: return luaL_error(L, "wrong number of arguments");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void math_randomseed (void) {
|
||||
srand(luaL_check_int(1));
|
||||
static int math_randomseed (lua_State *L) {
|
||||
srand(luaL_checkint(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static struct luaL_reg mathlib[] = {
|
||||
{"abs", math_abs},
|
||||
{"sin", math_sin},
|
||||
{"cos", math_cos},
|
||||
{"tan", math_tan},
|
||||
{"asin", math_asin},
|
||||
{"acos", math_acos},
|
||||
{"atan", math_atan},
|
||||
{"atan2", math_atan2},
|
||||
{"ceil", math_ceil},
|
||||
{"floor", math_floor},
|
||||
{"mod", math_mod},
|
||||
{"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},
|
||||
{"acos", math_acos},
|
||||
{"asin", math_asin},
|
||||
{"atan2", math_atan2},
|
||||
{"atan", math_atan},
|
||||
{"ceil", math_ceil},
|
||||
{"cosh", math_cosh},
|
||||
{"cos", math_cos},
|
||||
{"deg", math_deg},
|
||||
{"exp", math_exp},
|
||||
{"floor", math_floor},
|
||||
{"fmod", math_fmod},
|
||||
{"frexp", math_frexp},
|
||||
{"ldexp", math_ldexp},
|
||||
{"log10", math_log10},
|
||||
{"log", math_log},
|
||||
{"max", math_max},
|
||||
{"min", math_min},
|
||||
{"modf", math_modf},
|
||||
{"pow", math_pow},
|
||||
{"rad", math_rad},
|
||||
{"random", math_random},
|
||||
{"randomseed", math_randomseed},
|
||||
{"sinh", math_sinh},
|
||||
{"sin", math_sin},
|
||||
{"sqrt", math_sqrt},
|
||||
{"tanh", math_tanh},
|
||||
{"tan", math_tan},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Open math library
|
||||
*/
|
||||
void lua_mathlibopen (void) {
|
||||
luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
|
||||
lua_pushcfunction(math_pow);
|
||||
lua_pushnumber(0); /* to get its tag */
|
||||
lua_settagmethod(lua_tag(lua_pop()), "pow");
|
||||
lua_pushnumber(PI); lua_setglobal("PI");
|
||||
LUALIB_API int luaopen_math (lua_State *L) {
|
||||
luaL_register(L, LUA_MATHLIBNAME, mathlib);
|
||||
lua_pushnumber(L, PI);
|
||||
lua_setfield(L, -2, "pi");
|
||||
lua_pushnumber(L, HUGE_VAL);
|
||||
lua_setfield(L, -2, "huge");
|
||||
#if defined(LUA_COMPAT_MOD)
|
||||
lua_getfield(L, -1, "fmod");
|
||||
lua_setfield(L, -2, "mod");
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
168
lmem.c
168
lmem.c
@@ -1,140 +1,86 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.16 1999/05/20 20:43:06 roberto Exp roberto $
|
||||
** $Id: lmem.c,v 1.69 2005/02/23 17:30:22 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define lmem_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** real ANSI systems do not need these tests;
|
||||
** but some systems (Sun OS) are not that ANSI...
|
||||
** About the realloc function:
|
||||
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
||||
** (`osize' is the old size, `nsize' is the new size)
|
||||
**
|
||||
** Lua ensures that (ptr == NULL) iff (osize == 0).
|
||||
**
|
||||
** * frealloc(ud, NULL, 0, x) creates a new block of size `x'
|
||||
**
|
||||
** * frealloc(ud, p, x, 0) frees the block `p'
|
||||
** (in this specific case, frealloc must return NULL).
|
||||
** particularly, frealloc(ud, NULL, 0, 0) does nothing
|
||||
** (which is equivalent to free(NULL) in ANSI C)
|
||||
**
|
||||
** frealloc returns NULL if it cannot create or reallocate the area
|
||||
** (any reallocation to an equal or smaller size cannot fail!)
|
||||
*/
|
||||
#ifdef OLD_ANSI
|
||||
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
||||
#define free(b) if (b) (free)(b)
|
||||
#endif
|
||||
|
||||
|
||||
#define MINSIZE 8 /* minimum size for "growing" vectors */
|
||||
|
||||
|
||||
|
||||
#define MINSIZEARRAY 4
|
||||
|
||||
static unsigned long power2 (unsigned long n) {
|
||||
unsigned long p = MINSIZE;
|
||||
while (p<=n) p<<=1;
|
||||
return p;
|
||||
|
||||
void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
|
||||
int limit, const char *errormsg) {
|
||||
void *newblock;
|
||||
int newsize;
|
||||
if (*size >= limit/2) { /* cannot double it? */
|
||||
if (*size >= limit) /* cannot grow even a little? */
|
||||
luaG_runerror(L, errormsg);
|
||||
newsize = limit; /* still have at least one free place */
|
||||
}
|
||||
else {
|
||||
newsize = (*size)*2;
|
||||
if (newsize < MINSIZEARRAY)
|
||||
newsize = MINSIZEARRAY; /* minimum size */
|
||||
}
|
||||
newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
|
||||
*size = newsize; /* update only when everything else is OK */
|
||||
return newblock;
|
||||
}
|
||||
|
||||
|
||||
void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
|
||||
char *errormsg, unsigned long limit) {
|
||||
unsigned long newn = nelems+inc;
|
||||
if (newn >= limit) lua_error(errormsg);
|
||||
if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */
|
||||
(nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */
|
||||
return block; /* do not need to reallocate */
|
||||
else /* it crossed a power of 2 boundary; grow to next power */
|
||||
return luaM_realloc(block, power2(newn)*size);
|
||||
void *luaM_toobig (lua_State *L) {
|
||||
luaG_runerror(L, "memory allocation error: block too big");
|
||||
return NULL; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
#ifndef DEBUG
|
||||
|
||||
/*
|
||||
** generic allocation routine.
|
||||
*/
|
||||
void *luaM_realloc (void *block, unsigned long size) {
|
||||
size_t s = (size_t)size;
|
||||
if (s != size)
|
||||
lua_error("memory allocation error: block too big");
|
||||
if (size == 0) {
|
||||
free(block); /* block may be NULL, that is OK for free */
|
||||
return NULL;
|
||||
}
|
||||
block = realloc(block, s);
|
||||
if (block == NULL)
|
||||
lua_error(memEM);
|
||||
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
||||
global_State *g = G(L);
|
||||
lua_assert((osize == 0) == (block == NULL));
|
||||
block = (*g->frealloc)(g->ud, block, osize, nsize);
|
||||
if (block == NULL && nsize > 0)
|
||||
luaD_throw(L, LUA_ERRMEM);
|
||||
lua_assert((nsize == 0) == (block == NULL));
|
||||
g->totalbytes = (g->totalbytes - osize) + nsize;
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
/* DEBUG */
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define HEADER (sizeof(double))
|
||||
#define MARKSIZE 16
|
||||
|
||||
#define MARK 55
|
||||
|
||||
|
||||
#define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
|
||||
|
||||
unsigned long numblocks = 0;
|
||||
unsigned long totalmem = 0;
|
||||
|
||||
|
||||
static void *checkblock (void *block) {
|
||||
if (block == NULL)
|
||||
return NULL;
|
||||
else {
|
||||
unsigned long *b = blocksize(block);
|
||||
unsigned long size = *b;
|
||||
int i;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
LUA_ASSERT(*(((char *)b)+HEADER+size+i) == MARK+i, "corrupted block");
|
||||
numblocks--;
|
||||
totalmem -= size;
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void freeblock (void *block) {
|
||||
if (block)
|
||||
memset(block, -1, *blocksize(block)); /* erase block */
|
||||
free(checkblock(block));
|
||||
}
|
||||
|
||||
|
||||
void *luaM_realloc (void *block, unsigned long size) {
|
||||
unsigned long realsize = HEADER+size+MARKSIZE;
|
||||
if (realsize != (size_t)realsize)
|
||||
lua_error("memory allocation error: block too big");
|
||||
if (size == 0) {
|
||||
freeblock(block);
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
char *newblock = malloc(realsize);
|
||||
int i;
|
||||
if (block) {
|
||||
unsigned long oldsize = *blocksize(block);
|
||||
if (oldsize > size) oldsize = size;
|
||||
memcpy(newblock+HEADER, block, oldsize);
|
||||
freeblock(block); /* erase (and check) old copy */
|
||||
}
|
||||
if (newblock == NULL)
|
||||
lua_error(memEM);
|
||||
totalmem += size;
|
||||
numblocks++;
|
||||
*(unsigned long *)newblock = size;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
*(newblock+HEADER+size+i) = MARK+i;
|
||||
return newblock+HEADER;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
56
lmem.h
56
lmem.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.7 1999/02/25 15:16:26 roberto Exp roberto $
|
||||
** $Id: lmem.h,v 1.30 2005/03/18 16:38:02 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,34 +8,42 @@
|
||||
#define lmem_h
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.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 bigger than `int' limit"
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
void *luaM_realloc (void *oldblock, unsigned long size);
|
||||
void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
|
||||
char *errormsg, unsigned long limit);
|
||||
|
||||
#define luaM_free(b) luaM_realloc((b), 0)
|
||||
#define luaM_malloc(t) luaM_realloc(NULL, (t))
|
||||
#define luaM_new(t) ((t *)luaM_malloc(sizeof(t)))
|
||||
#define luaM_newvector(n,t) ((t *)luaM_malloc((n)*sizeof(t)))
|
||||
#define luaM_growvector(v,nelems,inc,t,e,l) \
|
||||
((v)=(t *)luaM_growaux(v,nelems,inc,sizeof(t),e,l))
|
||||
#define luaM_reallocvector(v,n,t) ((v)=(t *)luaM_realloc(v,(n)*sizeof(t)))
|
||||
#define MEMERRMSG "not enough memory"
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
extern unsigned long numblocks;
|
||||
extern unsigned long totalmem;
|
||||
#endif
|
||||
#define luaM_reallocv(L,b,on,n,e) \
|
||||
((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \
|
||||
luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \
|
||||
luaM_toobig(L))
|
||||
|
||||
#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
|
||||
#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
|
||||
#define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t))
|
||||
|
||||
#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_reallocv(L, NULL, 0, n, 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_reallocv(L, v, oldn, n, sizeof(t))))
|
||||
|
||||
|
||||
LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
|
||||
size_t size);
|
||||
LUAI_FUNC void *luaM_toobig (lua_State *L);
|
||||
LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
|
||||
size_t size_elem, int limit,
|
||||
const char *errormsg);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
663
loadlib.c
Normal file
663
loadlib.c
Normal file
@@ -0,0 +1,663 @@
|
||||
/*
|
||||
** $Id: loadlib.c,v 1.51 2005/12/29 15:32:11 roberto Exp roberto $
|
||||
** Dynamic library loader for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
**
|
||||
** This module contains an implementation of loadlib for Unix systems
|
||||
** that have dlfcn, an implementation for Darwin (Mac OS X), an
|
||||
** implementation for Windows, and a stub for other systems.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define loadlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
/* prefix for open functions in C libraries */
|
||||
#define LUA_POF "luaopen_"
|
||||
|
||||
/* separator for open functions in C libraries */
|
||||
#define LUA_OFSEP "_"
|
||||
|
||||
|
||||
#define LIBPREFIX "LOADLIB: "
|
||||
|
||||
#define POF LUA_POF
|
||||
#define LIB_FAIL "open"
|
||||
|
||||
|
||||
/* error codes for ll_loadfunc */
|
||||
#define ERRLIB 1
|
||||
#define ERRFUNC 2
|
||||
|
||||
#define setprogdir(L) ((void)0)
|
||||
|
||||
|
||||
static void ll_unloadlib (void *lib);
|
||||
static void *ll_load (lua_State *L, const char *path);
|
||||
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
|
||||
|
||||
|
||||
|
||||
#if defined(LUA_DL_DLOPEN)
|
||||
/*
|
||||
** {========================================================================
|
||||
** 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 void ll_unloadlib (void *lib) {
|
||||
dlclose(lib);
|
||||
}
|
||||
|
||||
|
||||
static void *ll_load (lua_State *L, const char *path) {
|
||||
void *lib = dlopen(path, RTLD_NOW);
|
||||
if (lib == NULL) lua_pushstring(L, dlerror());
|
||||
return lib;
|
||||
}
|
||||
|
||||
|
||||
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
|
||||
lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
|
||||
if (f == NULL) lua_pushstring(L, dlerror());
|
||||
return f;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
#elif defined(LUA_DL_DLL)
|
||||
/*
|
||||
** {======================================================================
|
||||
** This is an implementation of loadlib for Windows using native functions.
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
#undef setprogdir
|
||||
|
||||
static void setprogdir (lua_State *L) {
|
||||
char buff[MAX_PATH + 1];
|
||||
char *lb;
|
||||
DWORD nsize = sizeof(buff)/sizeof(char);
|
||||
DWORD n = GetModuleFileName(NULL, buff, nsize);
|
||||
if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
|
||||
luaL_error(L, "unable to get ModuleFileName");
|
||||
else {
|
||||
*lb = '\0';
|
||||
luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
|
||||
lua_remove(L, -2); /* remove original string */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void pusherror (lua_State *L) {
|
||||
int error = GetLastError();
|
||||
char buffer[128];
|
||||
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, error, 0, buffer, sizeof(buffer), NULL))
|
||||
lua_pushstring(L, buffer);
|
||||
else
|
||||
lua_pushfstring(L, "system error %d\n", error);
|
||||
}
|
||||
|
||||
static void ll_unloadlib (void *lib) {
|
||||
FreeLibrary((HINSTANCE)lib);
|
||||
}
|
||||
|
||||
|
||||
static void *ll_load (lua_State *L, const char *path) {
|
||||
HINSTANCE lib = LoadLibrary(path);
|
||||
if (lib == NULL) pusherror(L);
|
||||
return lib;
|
||||
}
|
||||
|
||||
|
||||
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
|
||||
lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);
|
||||
if (f == NULL) pusherror(L);
|
||||
return f;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
#elif defined(LUA_DL_DYLD)
|
||||
/*
|
||||
** {======================================================================
|
||||
** Native Mac OS X / Darwin Implementation
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
#include <mach-o/dyld.h>
|
||||
|
||||
|
||||
/* Mac appends a `_' before C function names */
|
||||
#undef POF
|
||||
#define POF "_" LUA_POF
|
||||
|
||||
|
||||
static void pusherror (lua_State *L) {
|
||||
const char *err_str;
|
||||
const char *err_file;
|
||||
NSLinkEditErrors err;
|
||||
int err_num;
|
||||
NSLinkEditError(&err, &err_num, &err_file, &err_str);
|
||||
lua_pushstring(L, err_str);
|
||||
}
|
||||
|
||||
|
||||
static const char *errorfromcode (NSObjectFileImageReturnCode ret) {
|
||||
switch (ret) {
|
||||
case NSObjectFileImageInappropriateFile:
|
||||
return "file is not a bundle";
|
||||
case NSObjectFileImageArch:
|
||||
return "library is for wrong CPU type";
|
||||
case NSObjectFileImageFormat:
|
||||
return "bad format";
|
||||
case NSObjectFileImageAccess:
|
||||
return "cannot access file";
|
||||
case NSObjectFileImageFailure:
|
||||
default:
|
||||
return "unable to load library";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void ll_unloadlib (void *lib) {
|
||||
NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
|
||||
}
|
||||
|
||||
|
||||
static void *ll_load (lua_State *L, const char *path) {
|
||||
NSObjectFileImage img;
|
||||
NSObjectFileImageReturnCode ret;
|
||||
/* this would be a rare case, but prevents crashing if it happens */
|
||||
if(!_dyld_present()) {
|
||||
lua_pushliteral(L, "dyld not present");
|
||||
return NULL;
|
||||
}
|
||||
ret = NSCreateObjectFileImageFromFile(path, &img);
|
||||
if (ret == NSObjectFileImageSuccess) {
|
||||
NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE |
|
||||
NSLINKMODULE_OPTION_RETURN_ON_ERROR);
|
||||
NSDestroyObjectFileImage(img);
|
||||
if (mod == NULL) pusherror(L);
|
||||
return mod;
|
||||
}
|
||||
lua_pushstring(L, errorfromcode(ret));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
|
||||
NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym);
|
||||
if (nss == NULL) {
|
||||
lua_pushfstring(L, "symbol " LUA_QS " not found", sym);
|
||||
return NULL;
|
||||
}
|
||||
return (lua_CFunction)NSAddressOfSymbol(nss);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
#else
|
||||
/*
|
||||
** {======================================================
|
||||
** Fallback for other systems
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#undef LIB_FAIL
|
||||
#define LIB_FAIL "absent"
|
||||
|
||||
|
||||
#define DLMSG "dynamic libraries not enabled; check your Lua installation"
|
||||
|
||||
|
||||
static void ll_unloadlib (void *lib) {
|
||||
(void)lib; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
static void *ll_load (lua_State *L, const char *path) {
|
||||
(void)path; /* to avoid warnings */
|
||||
lua_pushliteral(L, DLMSG);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
|
||||
(void)lib; (void)sym; /* to avoid warnings */
|
||||
lua_pushliteral(L, DLMSG);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
static void **ll_register (lua_State *L, const char *path) {
|
||||
void **plib;
|
||||
lua_pushfstring(L, "%s%s", LIBPREFIX, path);
|
||||
lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */
|
||||
if (!lua_isnil(L, -1)) /* is there an entry? */
|
||||
plib = (void **)lua_touserdata(L, -1);
|
||||
else { /* no entry yet; create one */
|
||||
lua_pop(L, 1);
|
||||
plib = (void **)lua_newuserdata(L, sizeof(const void *));
|
||||
*plib = NULL;
|
||||
luaL_getmetatable(L, "_LOADLIB");
|
||||
lua_setmetatable(L, -2);
|
||||
lua_pushfstring(L, "%s%s", LIBPREFIX, path);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, LUA_REGISTRYINDEX);
|
||||
}
|
||||
return plib;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** __gc tag method: calls library's `ll_unloadlib' function with the lib
|
||||
** handle
|
||||
*/
|
||||
static int gctm (lua_State *L) {
|
||||
void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
|
||||
if (*lib) ll_unloadlib(*lib);
|
||||
*lib = NULL; /* mark library as closed */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
|
||||
void **reg = ll_register(L, path);
|
||||
if (*reg == NULL) *reg = ll_load(L, path);
|
||||
if (*reg == NULL)
|
||||
return ERRLIB; /* unable to load library */
|
||||
else {
|
||||
lua_CFunction f = ll_sym(L, *reg, sym);
|
||||
if (f == NULL)
|
||||
return ERRFUNC; /* unable to find function */
|
||||
lua_pushcfunction(L, f);
|
||||
return 0; /* return function */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int ll_loadlib (lua_State *L) {
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
const char *init = luaL_checkstring(L, 2);
|
||||
int stat = ll_loadfunc(L, path, init);
|
||||
if (stat == 0) /* no errors? */
|
||||
return 1; /* return the loaded function */
|
||||
else { /* error; error message is on stack top */
|
||||
lua_pushnil(L);
|
||||
lua_insert(L, -2);
|
||||
lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
|
||||
return 3; /* return nil, error message, and where */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** 'require' function
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static int readable (const char *filename) {
|
||||
FILE *f = fopen(filename, "r"); /* try to open file */
|
||||
if (f == NULL) return 0; /* open failed */
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const char *pushnexttemplate (lua_State *L, const char *path) {
|
||||
const char *l;
|
||||
while (*path == *LUA_PATHSEP) path++; /* skip separators */
|
||||
if (*path == '\0') return NULL; /* no more templates */
|
||||
l = strchr(path, *LUA_PATHSEP); /* find next separator */
|
||||
if (l == NULL) l = path + strlen(path);
|
||||
lua_pushlstring(L, path, l - path); /* template */
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
static const char *findfile (lua_State *L, const char *name,
|
||||
const char *pname) {
|
||||
const char *path;
|
||||
name = luaL_gsub(L, name, ".", LUA_DIRSEP);
|
||||
lua_getfield(L, LUA_ENVIRONINDEX, pname);
|
||||
path = lua_tostring(L, -1);
|
||||
if (path == NULL)
|
||||
luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
|
||||
lua_pushstring(L, ""); /* error accumulator */
|
||||
while ((path = pushnexttemplate(L, path)) != NULL) {
|
||||
const char *filename;
|
||||
filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
|
||||
if (readable(filename)) /* does file exist and is readable? */
|
||||
return filename; /* return that file name */
|
||||
lua_pop(L, 2); /* remove path template and file name */
|
||||
luaO_pushfstring(L, "\n\tno file " LUA_QS, filename);
|
||||
lua_concat(L, 2);
|
||||
}
|
||||
return NULL; /* not found */
|
||||
}
|
||||
|
||||
|
||||
static void loaderror (lua_State *L, const char *filename) {
|
||||
luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
|
||||
lua_tostring(L, 1), filename, lua_tostring(L, -1));
|
||||
}
|
||||
|
||||
|
||||
static int loader_Lua (lua_State *L) {
|
||||
const char *filename;
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
filename = findfile(L, name, "path");
|
||||
if (filename == NULL) return 1; /* library not found in this path */
|
||||
if (luaL_loadfile(L, filename) != 0)
|
||||
loaderror(L, filename);
|
||||
return 1; /* library loaded successfully */
|
||||
}
|
||||
|
||||
|
||||
static const char *mkfuncname (lua_State *L, const char *modname) {
|
||||
const char *funcname;
|
||||
const char *mark = strchr(modname, *LUA_IGMARK);
|
||||
if (mark) modname = mark + 1;
|
||||
funcname = luaL_gsub(L, modname, ".", LUA_OFSEP);
|
||||
funcname = lua_pushfstring(L, POF"%s", funcname);
|
||||
lua_remove(L, -2); /* remove 'gsub' result */
|
||||
return funcname;
|
||||
}
|
||||
|
||||
|
||||
static int loader_C (lua_State *L) {
|
||||
const char *funcname;
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
const char *filename = findfile(L, name, "cpath");
|
||||
if (filename == NULL) return 1; /* library not found in this path */
|
||||
funcname = mkfuncname(L, name);
|
||||
if (ll_loadfunc(L, filename, funcname) != 0)
|
||||
loaderror(L, filename);
|
||||
return 1; /* library loaded successfully */
|
||||
}
|
||||
|
||||
|
||||
static int loader_Croot (lua_State *L) {
|
||||
const char *funcname;
|
||||
const char *filename;
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
const char *p = strchr(name, '.');
|
||||
int stat;
|
||||
if (p == NULL) return 0; /* is root */
|
||||
lua_pushlstring(L, name, p - name);
|
||||
filename = findfile(L, lua_tostring(L, -1), "cpath");
|
||||
if (filename == NULL) return 1; /* root not found */
|
||||
funcname = mkfuncname(L, name);
|
||||
if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
|
||||
if (stat != ERRFUNC) loaderror(L, filename); /* real error */
|
||||
luaO_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
|
||||
name, filename);
|
||||
return 1; /* function not found */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int loader_preload (lua_State *L) {
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
lua_getfield(L, LUA_ENVIRONINDEX, "preload");
|
||||
if (!lua_istable(L, -1))
|
||||
luaL_error(L, LUA_QL("package.preload") " must be a table");
|
||||
lua_getfield(L, -1, name);
|
||||
if (lua_isnil(L, -1)) /* not found? */
|
||||
luaO_pushfstring(L, "\n\tno field package.preload['%s']", name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const int sentinel_ = 0;
|
||||
#define sentinel ((void *)&sentinel_)
|
||||
|
||||
|
||||
static int ll_require (lua_State *L) {
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
int i;
|
||||
lua_settop(L, 1); /* _LOADED table will be at index 2 */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
lua_getfield(L, 2, name);
|
||||
if (lua_toboolean(L, -1)) { /* is it there? */
|
||||
if (lua_touserdata(L, -1) == sentinel) /* check loops */
|
||||
luaL_error(L, "loop or previous error loading module " LUA_QS, name);
|
||||
return 1; /* package is already loaded */
|
||||
}
|
||||
/* else must load it; iterate over available loaders */
|
||||
lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
|
||||
if (!lua_istable(L, -1))
|
||||
luaL_error(L, LUA_QL("package.loaders") " must be a table");
|
||||
lua_pushstring(L, ""); /* error message accumulator */
|
||||
for (i=1; ; i++) {
|
||||
lua_rawgeti(L, -2, i); /* get a loader */
|
||||
if (lua_isnil(L, -1))
|
||||
luaL_error(L, "module " LUA_QS " not found:%s",
|
||||
name, lua_tostring(L, -2));
|
||||
lua_pushstring(L, name);
|
||||
lua_call(L, 1, 1); /* call it */
|
||||
if (lua_isfunction(L, -1)) /* did it find module? */
|
||||
break; /* module loaded successfully */
|
||||
else if (lua_isstring(L, -1)) /* loader returned error message? */
|
||||
lua_concat(L, 2); /* accumulate it */
|
||||
else
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_pushlightuserdata(L, sentinel);
|
||||
lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
|
||||
lua_pushstring(L, name); /* pass name as argument to module */
|
||||
lua_call(L, 1, 1); /* run loaded module */
|
||||
if (!lua_isnil(L, -1)) /* non-nil return? */
|
||||
lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
|
||||
lua_getfield(L, 2, name);
|
||||
if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
|
||||
lua_pushboolean(L, 1); /* use true as result */
|
||||
lua_pushvalue(L, -1); /* extra copy to be returned */
|
||||
lua_setfield(L, 2, name); /* _LOADED[name] = true */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** 'module' function
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static void setfenv (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
lua_getstack(L, 1, &ar);
|
||||
lua_getinfo(L, "f", &ar);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_setfenv(L, -2);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
|
||||
static void dooptions (lua_State *L, int n) {
|
||||
int i;
|
||||
for (i = 2; i <= n; i++) {
|
||||
lua_pushvalue(L, i); /* get option (a function) */
|
||||
lua_pushvalue(L, -2); /* module */
|
||||
lua_call(L, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void modinit (lua_State *L, const char *modname) {
|
||||
const char *dot;
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -2, "_M"); /* module._M = module */
|
||||
lua_pushstring(L, modname);
|
||||
lua_setfield(L, -2, "_NAME");
|
||||
dot = strrchr(modname, '.'); /* look for last dot in module name */
|
||||
if (dot == NULL) dot = modname;
|
||||
else dot++;
|
||||
/* set _PACKAGE as package name (full module name minus last part) */
|
||||
lua_pushlstring(L, modname, dot - modname);
|
||||
lua_setfield(L, -2, "_PACKAGE");
|
||||
}
|
||||
|
||||
|
||||
static int ll_module (lua_State *L) {
|
||||
const char *modname = luaL_checkstring(L, 1);
|
||||
int loaded = lua_gettop(L) + 1; /* index of _LOADED table */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
lua_getfield(L, loaded, modname); /* get _LOADED[modname] */
|
||||
if (!lua_istable(L, -1)) { /* not found? */
|
||||
lua_pop(L, 1); /* remove previous result */
|
||||
/* try global variable (and create one if it does not exist) */
|
||||
if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
|
||||
return luaL_error(L, "name conflict for module " LUA_QS, modname);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */
|
||||
}
|
||||
/* check whether table already has a _NAME field */
|
||||
lua_getfield(L, -1, "_NAME");
|
||||
if (!lua_isnil(L, -1)) /* is table an initialized module? */
|
||||
lua_pop(L, 1);
|
||||
else { /* no; initialize it */
|
||||
lua_pop(L, 1);
|
||||
modinit(L, modname);
|
||||
}
|
||||
lua_pushvalue(L, -1);
|
||||
setfenv(L);
|
||||
dooptions(L, loaded - 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int ll_seeall (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
if (!lua_getmetatable(L, 1)) {
|
||||
lua_createtable(L, 0, 1); /* create new metatable */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setmetatable(L, 1);
|
||||
}
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
lua_setfield(L, -2, "__index"); /* mt.__index = _G */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/* auxiliary mark (for internal use) */
|
||||
#define AUXMARK "\1"
|
||||
|
||||
static void setpath (lua_State *L, const char *fieldname, const char *envname,
|
||||
const char *def) {
|
||||
const char *path = getenv(envname);
|
||||
if (path == NULL) /* no environment variable? */
|
||||
lua_pushstring(L, def); /* use default */
|
||||
else {
|
||||
/* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
|
||||
path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
|
||||
LUA_PATHSEP AUXMARK LUA_PATHSEP);
|
||||
luaL_gsub(L, path, AUXMARK, def);
|
||||
lua_remove(L, -2);
|
||||
}
|
||||
setprogdir(L);
|
||||
lua_setfield(L, -2, fieldname);
|
||||
}
|
||||
|
||||
|
||||
static const luaL_Reg pk_funcs[] = {
|
||||
{"loadlib", ll_loadlib},
|
||||
{"seeall", ll_seeall},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
static const luaL_Reg ll_funcs[] = {
|
||||
{"module", ll_module},
|
||||
{"require", ll_require},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
static const lua_CFunction loaders[] =
|
||||
{loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
|
||||
|
||||
|
||||
LUALIB_API int luaopen_package (lua_State *L) {
|
||||
int i;
|
||||
/* create new type _LOADLIB */
|
||||
luaL_newmetatable(L, "_LOADLIB");
|
||||
lua_pushcfunction(L, gctm);
|
||||
lua_setfield(L, -2, "__gc");
|
||||
/* create `package' table */
|
||||
luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
|
||||
#if defined(LUA_COMPAT_LOADLIB)
|
||||
lua_getfield(L, -1, "loadlib");
|
||||
lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
|
||||
#endif
|
||||
lua_pushvalue(L, -1);
|
||||
lua_replace(L, LUA_ENVIRONINDEX);
|
||||
/* create `loaders' table */
|
||||
lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1);
|
||||
/* fill it with pre-defined loaders */
|
||||
for (i=0; loaders[i] != NULL; i++) {
|
||||
lua_pushcfunction(L, loaders[i]);
|
||||
lua_rawseti(L, -2, i+1);
|
||||
}
|
||||
lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
|
||||
setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */
|
||||
setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
|
||||
/* store config information */
|
||||
lua_pushstring(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
|
||||
LUA_EXECDIR "\n" LUA_IGMARK);
|
||||
lua_setfield(L, -2, "config");
|
||||
/* set field `loaded' */
|
||||
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
|
||||
lua_setfield(L, -2, "loaded");
|
||||
/* set field `preload' */
|
||||
lua_newtable(L);
|
||||
lua_setfield(L, -2, "preload");
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
luaL_register(L, NULL, ll_funcs); /* open lib into global table */
|
||||
lua_pop(L, 1);
|
||||
return 1; /* return 'package' table */
|
||||
}
|
||||
|
||||
267
lobject.c
267
lobject.c
@@ -1,129 +1,214 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 1.18 1999/02/26 15:48:30 roberto Exp roberto $
|
||||
** $Id: lobject.c,v 2.21 2006/01/10 12:50:00 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lobject_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
char *luaO_typenames[] = { /* ORDER LUA_T */
|
||||
"userdata", "number", "string", "table", "function", "function",
|
||||
"nil", "function", "mark", "mark", "mark", "line", NULL
|
||||
};
|
||||
|
||||
|
||||
TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
|
||||
/* hash dimensions values */
|
||||
static long dimensions[] =
|
||||
{5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
|
||||
12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
|
||||
1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
|
||||
const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
|
||||
|
||||
|
||||
int luaO_redimension (int oldsize)
|
||||
{
|
||||
int i;
|
||||
for (i=0; dimensions[i]<MAX_INT; i++) {
|
||||
if (dimensions[i] > oldsize)
|
||||
return dimensions[i];
|
||||
/*
|
||||
** converts an integer to a "floating point byte", represented as
|
||||
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
|
||||
** eeeee != 0 and (xxx) otherwise.
|
||||
*/
|
||||
int luaO_int2fb (unsigned int x) {
|
||||
int e = 0; /* expoent */
|
||||
while (x >= 16) {
|
||||
x = (x+1) >> 1;
|
||||
e++;
|
||||
}
|
||||
lua_error("tableEM");
|
||||
return 0; /* to avoid warnings */
|
||||
if (x < 8) return x;
|
||||
else return ((e+1) << 3) | (cast_int(x) - 8);
|
||||
}
|
||||
|
||||
|
||||
int luaO_equalval (TObject *t1, TObject *t2) {
|
||||
switch (ttype(t1)) {
|
||||
case LUA_T_NIL: return 1;
|
||||
case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
|
||||
case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2);
|
||||
case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
|
||||
case LUA_T_PROTO: return tfvalue(t1) == tfvalue(t2);
|
||||
case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2);
|
||||
case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl;
|
||||
/* converts back */
|
||||
int luaO_fb2int (int x) {
|
||||
int e = (x >> 3) & 31;
|
||||
if (e == 0) return x;
|
||||
else return ((x & 7)+8) << (e - 1);
|
||||
}
|
||||
|
||||
|
||||
int luaO_log2 (unsigned int x) {
|
||||
static const lu_byte log_2[256] = {
|
||||
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,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,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
||||
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
||||
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
||||
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
|
||||
};
|
||||
int l = -1;
|
||||
while (x >= 256) { l += 8; x >>= 8; }
|
||||
return l + log_2[x];
|
||||
|
||||
}
|
||||
|
||||
|
||||
int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
|
||||
if (ttype(t1) != ttype(t2)) return 0;
|
||||
else switch (ttype(t1)) {
|
||||
case LUA_TNIL:
|
||||
return 1;
|
||||
case LUA_TNUMBER:
|
||||
return luai_numeq(nvalue(t1), nvalue(t2));
|
||||
case LUA_TBOOLEAN:
|
||||
return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
return pvalue(t1) == pvalue(t2);
|
||||
default:
|
||||
LUA_INTERNALERROR("invalid type");
|
||||
return 0; /* UNREACHABLE */
|
||||
lua_assert(iscollectable(t1));
|
||||
return gcvalue(t1) == gcvalue(t2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaO_insertlist (GCnode *root, GCnode *node)
|
||||
{
|
||||
node->next = root->next;
|
||||
root->next = node;
|
||||
node->marked = 0;
|
||||
int luaO_str2d (const char *s, lua_Number *result) {
|
||||
char *endptr;
|
||||
*result = lua_str2number(s, &endptr);
|
||||
if (endptr == s) return 0; /* conversion failed */
|
||||
if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
|
||||
*result = cast_num(strtoul(s, &endptr, 16));
|
||||
if (*endptr == '\0') return 1; /* most common case */
|
||||
while (isspace(cast(unsigned char, *endptr))) endptr++;
|
||||
if (*endptr != '\0') return 0; /* invalid trailing characters? */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#ifdef OLD_ANSI
|
||||
void luaO_memup (void *dest, void *src, int size) {
|
||||
while (size--)
|
||||
((char *)dest)[size]=((char *)src)[size];
|
||||
|
||||
static void pushstr (lua_State *L, const char *str) {
|
||||
setsvalue2s(L, L->top, luaS_new(L, str));
|
||||
incr_top(L);
|
||||
}
|
||||
|
||||
void luaO_memdown (void *dest, void *src, int size) {
|
||||
int i;
|
||||
for (i=0; i<size; i++)
|
||||
((char *)dest)[i]=((char *)src)[i];
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
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;
|
||||
/* this function handles only `%d', `%c', %f, %p, 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, L->top, luaS_newlstr(L, fmt, e-fmt));
|
||||
incr_top(L);
|
||||
switch (*(e+1)) {
|
||||
case 's': {
|
||||
const char *s = va_arg(argp, char *);
|
||||
if (s == NULL) s = "(null)";
|
||||
pushstr(L, s);
|
||||
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_num(va_arg(argp, int)));
|
||||
incr_top(L);
|
||||
break;
|
||||
}
|
||||
case 'f': {
|
||||
setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
|
||||
incr_top(L);
|
||||
break;
|
||||
}
|
||||
case 'p': {
|
||||
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
|
||||
sprintf(buff, "%p", va_arg(argp, void *));
|
||||
pushstr(L, buff);
|
||||
break;
|
||||
}
|
||||
case '%': {
|
||||
pushstr(L, "%");
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
char buff[3];
|
||||
buff[0] = '%';
|
||||
buff[1] = *(e+1);
|
||||
buff[2] = '\0';
|
||||
pushstr(L, buff);
|
||||
break;
|
||||
}
|
||||
}
|
||||
n += 2;
|
||||
fmt = e+2;
|
||||
}
|
||||
return res;
|
||||
pushstr(L, fmt);
|
||||
luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
|
||||
L->top -= n;
|
||||
return svalue(L->top - 1);
|
||||
}
|
||||
|
||||
|
||||
double luaO_str2d (char *s) { /* LUA_NUMBER */
|
||||
double a = 0.0;
|
||||
int point = 0;
|
||||
while (isdigit((unsigned char)*s)) {
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
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, size_t bufflen) {
|
||||
if (*source == '=') {
|
||||
strncpy(out, source+1, bufflen); /* remove first char */
|
||||
out[bufflen-1] = '\0'; /* ensures null termination */
|
||||
}
|
||||
if (*s == '.') {
|
||||
s++;
|
||||
while (isdigit((unsigned char)*s)) {
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
point++;
|
||||
else { /* out = "source", or "...source" */
|
||||
if (*source == '@') {
|
||||
size_t 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"] */
|
||||
size_t len = strcspn(source, "\n\r"); /* 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, "\"]");
|
||||
}
|
||||
}
|
||||
if (toupper((unsigned char)*s) == 'E') {
|
||||
int e = 0;
|
||||
int sig = 1;
|
||||
s++;
|
||||
if (*s == '-') {
|
||||
s++;
|
||||
sig = -1;
|
||||
}
|
||||
else if (*s == '+') s++;
|
||||
if (!isdigit((unsigned char)*s)) return -1; /* no digit in the exponent? */
|
||||
do {
|
||||
e = 10*e + (*(s++)-'0');
|
||||
} while (isdigit((unsigned char)*s));
|
||||
point -= sig*e;
|
||||
}
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
if (*s != '\0') return -1; /* invalid trailing characters? */
|
||||
if (point > 0)
|
||||
a /= expten(point);
|
||||
else if (point < 0)
|
||||
a *= expten(-point);
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
439
lobject.h
439
lobject.h
@@ -1,119 +1,226 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 1.27 1999/03/04 21:17:26 roberto Exp roberto $
|
||||
** $Id: lobject.h,v 2.19 2006/01/10 12:51:53 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lobject_h
|
||||
#define lobject_h
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
#include "lauxlib.h"
|
||||
#define LUA_INTERNALERROR(s) \
|
||||
luaL_verror("INTERNAL ERROR - %s [%s:%d]",(s),__FILE__,__LINE__)
|
||||
#define LUA_ASSERT(c,s) { if (!(c)) LUA_INTERNALERROR(s); }
|
||||
#else
|
||||
#define LUA_INTERNALERROR(s) /* empty */
|
||||
#define LUA_ASSERT(c,s) /* empty */
|
||||
#endif
|
||||
/* tags for values visible from Lua */
|
||||
#define LAST_TAG LUA_TTHREAD
|
||||
|
||||
#define NUM_TAGS (LAST_TAG+1)
|
||||
|
||||
|
||||
/*
|
||||
** "real" is the type "number" of Lua
|
||||
** GREP LUA_NUMBER to change that
|
||||
** Extra tags for non-values
|
||||
*/
|
||||
#ifndef LUA_NUM_TYPE
|
||||
#define LUA_NUM_TYPE double
|
||||
#endif
|
||||
|
||||
|
||||
typedef LUA_NUM_TYPE real;
|
||||
|
||||
#define Byte lua_Byte /* some systems have Byte as a predefined type */
|
||||
typedef unsigned char Byte; /* unsigned 8 bits */
|
||||
|
||||
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
|
||||
|
||||
typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
|
||||
#define LUA_TPROTO (LAST_TAG+1)
|
||||
#define LUA_TUPVAL (LAST_TAG+2)
|
||||
#define LUA_TDEADKEY (LAST_TAG+3)
|
||||
|
||||
|
||||
/*
|
||||
** Lua TYPES
|
||||
** WARNING: if you change the order of this enumeration,
|
||||
** grep "ORDER LUA_T"
|
||||
** Union of all collectable objects
|
||||
*/
|
||||
typedef enum {
|
||||
LUA_T_USERDATA = 0, /* tag default for userdata */
|
||||
LUA_T_NUMBER = -1, /* fixed tag for numbers */
|
||||
LUA_T_STRING = -2, /* fixed tag for strings */
|
||||
LUA_T_ARRAY = -3, /* tag default for tables (or arrays) */
|
||||
LUA_T_PROTO = -4, /* fixed tag for functions */
|
||||
LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */
|
||||
LUA_T_NIL = -6, /* last "pre-defined" tag */
|
||||
LUA_T_CLOSURE = -7,
|
||||
LUA_T_CLMARK = -8, /* mark for closures */
|
||||
LUA_T_PMARK = -9, /* mark for Lua prototypes */
|
||||
LUA_T_CMARK = -10, /* mark for C prototypes */
|
||||
LUA_T_LINE = -11
|
||||
} lua_Type;
|
||||
|
||||
#define NUM_TAGS 7
|
||||
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 {
|
||||
lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
|
||||
real n; /* LUA_T_NUMBER */
|
||||
struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
|
||||
struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
|
||||
struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
|
||||
struct Hash *a; /* LUA_T_ARRAY */
|
||||
int i; /* LUA_T_LINE */
|
||||
GCObject *gc;
|
||||
void *p;
|
||||
lua_Number n;
|
||||
int b;
|
||||
} Value;
|
||||
|
||||
|
||||
typedef struct TObject {
|
||||
lua_Type ttype;
|
||||
Value value;
|
||||
} TObject;
|
||||
/*
|
||||
** Tagged Values
|
||||
*/
|
||||
|
||||
#define TValuefields Value value; int tt
|
||||
|
||||
typedef struct lua_TValue {
|
||||
TValuefields;
|
||||
} TValue;
|
||||
|
||||
|
||||
/* 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 rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
|
||||
#define tsvalue(o) (&rawtsvalue(o)->tsv)
|
||||
#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
|
||||
#define uvalue(o) (&rawuvalue(o)->uv)
|
||||
#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))
|
||||
|
||||
/*
|
||||
** for internal debug only
|
||||
*/
|
||||
#define checkconsistency(obj) \
|
||||
lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
|
||||
|
||||
#define checkliveness(g,obj) \
|
||||
lua_assert(!iscollectable(obj) || \
|
||||
((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
|
||||
|
||||
|
||||
/* Macros to set values */
|
||||
#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
|
||||
|
||||
#define setnvalue(obj,x) \
|
||||
{ TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
|
||||
|
||||
#define setpvalue(obj,x) \
|
||||
{ TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
|
||||
|
||||
#define setbvalue(obj,x) \
|
||||
{ TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
|
||||
|
||||
#define setsvalue(L,obj,x) \
|
||||
{ TValue *i_o=(obj); \
|
||||
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
|
||||
checkliveness(G(L),i_o); }
|
||||
|
||||
#define setuvalue(L,obj,x) \
|
||||
{ TValue *i_o=(obj); \
|
||||
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
|
||||
checkliveness(G(L),i_o); }
|
||||
|
||||
#define setthvalue(L,obj,x) \
|
||||
{ TValue *i_o=(obj); \
|
||||
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
|
||||
checkliveness(G(L),i_o); }
|
||||
|
||||
#define setclvalue(L,obj,x) \
|
||||
{ TValue *i_o=(obj); \
|
||||
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
|
||||
checkliveness(G(L),i_o); }
|
||||
|
||||
#define sethvalue(L,obj,x) \
|
||||
{ TValue *i_o=(obj); \
|
||||
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
|
||||
checkliveness(G(L),i_o); }
|
||||
|
||||
#define setptvalue(L,obj,x) \
|
||||
{ TValue *i_o=(obj); \
|
||||
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
|
||||
checkliveness(G(L),i_o); }
|
||||
|
||||
|
||||
|
||||
|
||||
#define setobj(L,obj1,obj2) \
|
||||
{ const TValue *o2=(obj2); TValue *o1=(obj1); \
|
||||
o1->value = o2->value; o1->tt=o2->tt; \
|
||||
checkliveness(G(L),o1); }
|
||||
|
||||
|
||||
/*
|
||||
** generic header for garbage collector lists
|
||||
** different types of sets, according to destination
|
||||
*/
|
||||
typedef struct GCnode {
|
||||
struct GCnode *next;
|
||||
int marked;
|
||||
} GCnode;
|
||||
|
||||
/* from stack to (same) stack */
|
||||
#define setobjs2s setobj
|
||||
/* to stack (not from same stack) */
|
||||
#define setobj2s setobj
|
||||
#define setsvalue2s setsvalue
|
||||
#define sethvalue2s sethvalue
|
||||
#define setptvalue2s setptvalue
|
||||
/* 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 TValue *StkId; /* index to stack elements */
|
||||
|
||||
|
||||
/*
|
||||
** String headers for string table
|
||||
*/
|
||||
typedef union TString {
|
||||
L_Umaxalign dummy; /* ensures maximum alignment for strings */
|
||||
struct {
|
||||
CommonHeader;
|
||||
lu_byte reserved;
|
||||
unsigned int hash;
|
||||
size_t len;
|
||||
} tsv;
|
||||
} TString;
|
||||
|
||||
typedef struct TaggedString {
|
||||
GCnode head;
|
||||
unsigned long hash;
|
||||
int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
|
||||
union {
|
||||
struct {
|
||||
TObject globalval;
|
||||
long len; /* if this is a string, here is its length */
|
||||
} s;
|
||||
struct {
|
||||
int tag;
|
||||
void *v; /* if this is a userdata, here is its value */
|
||||
} d;
|
||||
} u;
|
||||
char str[1]; /* \0 byte already reserved */
|
||||
} TaggedString;
|
||||
|
||||
#define getstr(ts) 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;
|
||||
struct Table *env;
|
||||
size_t len;
|
||||
} uv;
|
||||
} Udata;
|
||||
|
||||
|
||||
|
||||
@@ -121,84 +228,154 @@ typedef struct TaggedString {
|
||||
/*
|
||||
** Function Prototypes
|
||||
*/
|
||||
typedef struct TProtoFunc {
|
||||
GCnode head;
|
||||
struct TObject *consts;
|
||||
int nconsts;
|
||||
Byte *code; /* ends with opcode ENDCODE */
|
||||
int lineDefined;
|
||||
TaggedString *source;
|
||||
struct LocVar *locvars; /* ends with line = -1 */
|
||||
} TProtoFunc;
|
||||
typedef struct Proto {
|
||||
CommonHeader;
|
||||
TValue *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 sizeupvalues;
|
||||
int sizek; /* size of `k' */
|
||||
int sizecode;
|
||||
int sizelineinfo;
|
||||
int sizep; /* size of `p' */
|
||||
int sizelocvars;
|
||||
int linedefined;
|
||||
int lastlinedefined;
|
||||
GCObject *gclist;
|
||||
lu_byte nups; /* number of upvalues */
|
||||
lu_byte numparams;
|
||||
lu_byte is_vararg;
|
||||
lu_byte maxstacksize;
|
||||
} Proto;
|
||||
|
||||
|
||||
/* masks for new-style vararg */
|
||||
#define VARARG_HASARG 1
|
||||
#define VARARG_ISVARARG 2
|
||||
#define VARARG_NEEDSARG 4
|
||||
|
||||
|
||||
typedef struct LocVar {
|
||||
TaggedString *varname; /* NULL signals end of scope */
|
||||
int line;
|
||||
TString *varname;
|
||||
int startpc; /* first point where variable is active */
|
||||
int endpc; /* first point where variable is dead */
|
||||
} LocVar;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Upvalues
|
||||
*/
|
||||
|
||||
|
||||
/* Macros to access structure members */
|
||||
#define ttype(o) ((o)->ttype)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define svalue(o) ((o)->value.ts->str)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define clvalue(o) ((o)->value.cl)
|
||||
#define avalue(o) ((o)->value.a)
|
||||
#define fvalue(o) ((o)->value.f)
|
||||
#define tfvalue(o) ((o)->value.tf)
|
||||
|
||||
#define protovalue(o) ((o)->value.cl->consts)
|
||||
typedef struct UpVal {
|
||||
CommonHeader;
|
||||
TValue *v; /* points to stack or to its own value */
|
||||
union {
|
||||
TValue value; /* the value (when closed) */
|
||||
struct { /* double linked list (when open) */
|
||||
struct UpVal *prev;
|
||||
struct UpVal *next;
|
||||
} l;
|
||||
} u;
|
||||
} UpVal;
|
||||
|
||||
|
||||
/*
|
||||
** Closures
|
||||
*/
|
||||
typedef struct Closure {
|
||||
GCnode head;
|
||||
int nelems; /* not included the first one (always the prototype) */
|
||||
TObject consts[1]; /* at least one for prototype */
|
||||
|
||||
#define ClosureHeader \
|
||||
CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
|
||||
struct Table *env
|
||||
|
||||
typedef struct CClosure {
|
||||
ClosureHeader;
|
||||
lua_CFunction f;
|
||||
TValue upvalue[1];
|
||||
} CClosure;
|
||||
|
||||
|
||||
typedef struct LClosure {
|
||||
ClosureHeader;
|
||||
struct Proto *p;
|
||||
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)
|
||||
|
||||
typedef struct node {
|
||||
TObject ref;
|
||||
TObject val;
|
||||
|
||||
/*
|
||||
** Tables
|
||||
*/
|
||||
|
||||
typedef union TKey {
|
||||
struct {
|
||||
TValuefields;
|
||||
struct Node *next; /* for chaining */
|
||||
} nk;
|
||||
TValue tvk;
|
||||
} TKey;
|
||||
|
||||
|
||||
typedef struct Node {
|
||||
TValue i_val;
|
||||
TKey i_key;
|
||||
} Node;
|
||||
|
||||
typedef struct Hash {
|
||||
GCnode head;
|
||||
|
||||
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;
|
||||
TValue *array; /* array part */
|
||||
Node *node;
|
||||
int nhash;
|
||||
int nuse;
|
||||
int htag;
|
||||
} Hash;
|
||||
Node *lastfree; /* any free position is before this position */
|
||||
GCObject *gclist;
|
||||
int sizearray; /* size of `array' array */
|
||||
} Table;
|
||||
|
||||
|
||||
extern char *luaO_typenames[];
|
||||
|
||||
#define luaO_typename(o) luaO_typenames[-ttype(o)]
|
||||
/*
|
||||
** `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)))))
|
||||
|
||||
|
||||
extern TObject luaO_nilobject;
|
||||
#define twoto(x) (1<<(x))
|
||||
#define sizenode(t) (twoto((t)->lsizenode))
|
||||
|
||||
|
||||
#define luaO_nilobject (&luaO_nilobject_)
|
||||
|
||||
LUAI_DATA const TValue luaO_nilobject_;
|
||||
|
||||
#define ceillog2(x) (luaO_log2((x)-1) + 1)
|
||||
|
||||
LUAI_FUNC int luaO_log2 (unsigned int x);
|
||||
LUAI_FUNC int luaO_int2fb (unsigned int x);
|
||||
LUAI_FUNC int luaO_fb2int (int x);
|
||||
LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
|
||||
LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
|
||||
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
|
||||
va_list argp);
|
||||
LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
|
||||
LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
|
||||
|
||||
#define luaO_equalObj(t1,t2) ((ttype(t1) != ttype(t2)) ? 0 \
|
||||
: luaO_equalval(t1,t2))
|
||||
int luaO_equalval (TObject *t1, TObject *t2);
|
||||
int luaO_redimension (int oldsize);
|
||||
void luaO_insertlist (GCnode *root, GCnode *node);
|
||||
double luaO_str2d (char *s);
|
||||
|
||||
#ifdef OLD_ANSI
|
||||
void luaO_memup (void *dest, void *src, int size);
|
||||
void luaO_memdown (void *dest, void *src, int size);
|
||||
#else
|
||||
#include <string.h>
|
||||
#define luaO_memup(d,s,n) memmove(d,s,n)
|
||||
#define luaO_memdown(d,s,n) memmove(d,s,n)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
102
lopcodes.c
Normal file
102
lopcodes.c
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
** $Id: lopcodes.c,v 1.36 2005/10/13 12:22:05 roberto Exp roberto $
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#define lopcodes_c
|
||||
#define LUA_CORE
|
||||
|
||||
|
||||
#include "lopcodes.h"
|
||||
|
||||
|
||||
/* ORDER OP */
|
||||
|
||||
const char *const luaP_opnames[NUM_OPCODES+1] = {
|
||||
"MOVE",
|
||||
"LOADK",
|
||||
"LOADBOOL",
|
||||
"LOADNIL",
|
||||
"GETUPVAL",
|
||||
"GETGLOBAL",
|
||||
"GETTABLE",
|
||||
"SETGLOBAL",
|
||||
"SETUPVAL",
|
||||
"SETTABLE",
|
||||
"NEWTABLE",
|
||||
"SELF",
|
||||
"ADD",
|
||||
"SUB",
|
||||
"MUL",
|
||||
"DIV",
|
||||
"MOD",
|
||||
"POW",
|
||||
"UNM",
|
||||
"NOT",
|
||||
"LEN",
|
||||
"CONCAT",
|
||||
"JMP",
|
||||
"EQ",
|
||||
"LT",
|
||||
"LE",
|
||||
"TEST",
|
||||
"TESTSET",
|
||||
"CALL",
|
||||
"TAILCALL",
|
||||
"RETURN",
|
||||
"FORLOOP",
|
||||
"FORPREP",
|
||||
"TFORLOOP",
|
||||
"SETLIST",
|
||||
"CLOSE",
|
||||
"CLOSURE",
|
||||
"VARARG",
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m))
|
||||
|
||||
const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
/* T A B C mode opcode */
|
||||
opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */
|
||||
,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */
|
||||
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
|
||||
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */
|
||||
,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
|
||||
,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */
|
||||
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
|
||||
,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */
|
||||
,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */
|
||||
,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */
|
||||
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */
|
||||
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */
|
||||
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */
|
||||
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */
|
||||
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */
|
||||
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */
|
||||
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */
|
||||
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */
|
||||
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
|
||||
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */
|
||||
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */
|
||||
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */
|
||||
,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */
|
||||
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
|
||||
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */
|
||||
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */
|
||||
,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */
|
||||
,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */
|
||||
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */
|
||||
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */
|
||||
,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */
|
||||
,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */
|
||||
,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */
|
||||
,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */
|
||||
,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */
|
||||
,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */
|
||||
,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */
|
||||
,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */
|
||||
};
|
||||
|
||||
328
lopcodes.h
328
lopcodes.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.32 1999/03/10 14:09:45 roberto Exp roberto $
|
||||
** $Id: lopcodes.h,v 1.124 2005/12/02 18:42:08 roberto Exp roberto $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,132 +7,262 @@
|
||||
#ifndef lopcodes_h
|
||||
#define lopcodes_h
|
||||
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*===========================================================================
|
||||
We assume that instructions are unsigned numbers.
|
||||
All instructions have an opcode in the first 6 bits.
|
||||
Instructions can have the following fields:
|
||||
`A' : 8 bits
|
||||
`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.
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
|
||||
|
||||
|
||||
/*
|
||||
** NOTICE: variants of the same opcode must be consecutive: First, those
|
||||
** with word parameter, then with byte parameter.
|
||||
** 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_OP 0
|
||||
#define POS_A (POS_OP + SIZE_OP)
|
||||
#define POS_C (POS_A + SIZE_A)
|
||||
#define POS_B (POS_C + SIZE_C)
|
||||
#define POS_Bx POS_C
|
||||
|
||||
|
||||
/*
|
||||
** limits for opcode arguments.
|
||||
** we use (signed) int to manipulate most arguments,
|
||||
** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
|
||||
*/
|
||||
#if SIZE_Bx < LUAI_BITSINT-1
|
||||
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
|
||||
#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */
|
||||
#else
|
||||
#define MAXARG_Bx MAX_INT
|
||||
#define MAXARG_sBx MAX_INT
|
||||
#endif
|
||||
|
||||
|
||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||
#define MAXARG_C ((1<<SIZE_C)-1)
|
||||
|
||||
|
||||
/* creates a mask with `n' 1 bits at position `p' */
|
||||
#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
|
||||
|
||||
/* creates a mask with `n' 0 bits at position `p' */
|
||||
#define MASK0(n,p) (~MASK1(n,p))
|
||||
|
||||
/*
|
||||
** the following macros help to manipulate instructions
|
||||
*/
|
||||
|
||||
#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
|
||||
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
|
||||
((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
|
||||
|
||||
#define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0)))
|
||||
#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
|
||||
((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))
|
||||
|
||||
#define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))
|
||||
#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
|
||||
((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))
|
||||
|
||||
#define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))
|
||||
#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
|
||||
((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))
|
||||
|
||||
#define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))
|
||||
#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \
|
||||
((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))
|
||||
|
||||
#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
|
||||
#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
|
||||
|
||||
|
||||
#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \
|
||||
| (cast(Instruction, a)<<POS_A) \
|
||||
| (cast(Instruction, b)<<POS_B) \
|
||||
| (cast(Instruction, c)<<POS_C))
|
||||
|
||||
#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
|
||||
| (cast(Instruction, a)<<POS_A) \
|
||||
| (cast(Instruction, bc)<<POS_Bx))
|
||||
|
||||
|
||||
/*
|
||||
** Macros to operate RK indices
|
||||
*/
|
||||
|
||||
/* this bit 1 means constant (0 means register) */
|
||||
#define BITRK (1 << (SIZE_B - 1))
|
||||
|
||||
/* test whether value is a constant */
|
||||
#define ISK(x) ((x) & BITRK)
|
||||
|
||||
/* gets the index of the constant */
|
||||
#define INDEXK(r) ((int)(r) & ~BITRK)
|
||||
|
||||
#define MAXINDEXRK (BITRK - 1)
|
||||
|
||||
/* code a constant index as a RK value */
|
||||
#define RKASK(x) ((x) | BITRK)
|
||||
|
||||
|
||||
/*
|
||||
** invalid register that fits in 8 bits
|
||||
*/
|
||||
#define NO_REG MAXARG_A
|
||||
|
||||
|
||||
/*
|
||||
** R(x) - register
|
||||
** Kst(x) - constant (in constant table)
|
||||
** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** grep "ORDER OP" if you change these enums
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
/* name parm before after side effect
|
||||
-----------------------------------------------------------------------------*/
|
||||
ENDCODE,/* - - (return) */
|
||||
RETCODE,/* b - (return) */
|
||||
/*----------------------------------------------------------------------
|
||||
name args description
|
||||
------------------------------------------------------------------------*/
|
||||
OP_MOVE,/* A B R(A) := R(B) */
|
||||
OP_LOADK,/* A Bx R(A) := Kst(Bx) */
|
||||
OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
|
||||
OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
|
||||
OP_GETUPVAL,/* A B R(A) := UpValue[B] */
|
||||
|
||||
CALL,/* b c v_c...v_1 f r_b...r_1 f(v1,...,v_c) */
|
||||
OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */
|
||||
OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
|
||||
|
||||
TAILCALL,/* b c v_c...v_1 f (return) f(v1,...,v_c) */
|
||||
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) */
|
||||
|
||||
PUSHNIL,/* b - nil_0...nil_b */
|
||||
POP,/* b a_b...a_1 - */
|
||||
OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
|
||||
|
||||
PUSHNUMBERW,/* w - (float)w */
|
||||
PUSHNUMBER,/* b - (float)b */
|
||||
OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
|
||||
|
||||
PUSHNUMBERNEGW,/* w - (float)-w */
|
||||
PUSHNUMBERNEG,/* b - (float)-b */
|
||||
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_MOD,/* 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_LEN,/* A B R(A) := length of R(B) */
|
||||
|
||||
PUSHCONSTANTW,/*w - CNST[w] */
|
||||
PUSHCONSTANT,/* b - CNST[b] */
|
||||
OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
|
||||
|
||||
PUSHUPVALUE,/* b - Closure[b] */
|
||||
OP_JMP,/* sBx pc+=sBx */
|
||||
|
||||
PUSHLOCAL,/* b - LOC[b] */
|
||||
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++ */
|
||||
|
||||
GETGLOBALW,/* w - VAR[CNST[w]] */
|
||||
GETGLOBAL,/* b - VAR[CNST[b]] */
|
||||
OP_TEST,/* A C if not (R(A) <=> C) then pc++ */
|
||||
OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
|
||||
|
||||
GETTABLE,/* - i t t[i] */
|
||||
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) */
|
||||
|
||||
GETDOTTEDW,/* w t t[CNST[w]] */
|
||||
GETDOTTED,/* b t t[CNST[b]] */
|
||||
OP_FORLOOP,/* A sBx R(A)+=R(A+2);
|
||||
if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
|
||||
OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */
|
||||
|
||||
PUSHSELFW,/* w t t t[CNST[w]] */
|
||||
PUSHSELF,/* b t t t[CNST[b]] */
|
||||
OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
|
||||
if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */
|
||||
OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
|
||||
|
||||
CREATEARRAYW,/* w - newarray(size = w) */
|
||||
CREATEARRAY,/* b - newarray(size = b) */
|
||||
|
||||
SETLOCAL,/* b x - LOC[b]=x */
|
||||
|
||||
SETGLOBALW,/* w x - VAR[CNST[w]]=x */
|
||||
SETGLOBAL,/* b x - VAR[CNST[b]]=x */
|
||||
|
||||
SETTABLEPOP,/* - v i t - t[i]=v */
|
||||
|
||||
SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
|
||||
|
||||
SETLISTW,/* w c v_c...v_1 t t t[i+w*FPF]=v_i */
|
||||
SETLIST,/* b c v_c...v_1 t t t[i+b*FPF]=v_i */
|
||||
|
||||
SETMAP,/* b v_b k_b ...v_0 k_0 t t t[k_i]=v_i */
|
||||
|
||||
NEQOP,/* - y x (x~=y)? 1 : nil */
|
||||
EQOP,/* - y x (x==y)? 1 : nil */
|
||||
LTOP,/* - y x (x<y)? 1 : nil */
|
||||
LEOP,/* - y x (x<y)? 1 : nil */
|
||||
GTOP,/* - y x (x>y)? 1 : nil */
|
||||
GEOP,/* - y x (x>=y)? 1 : nil */
|
||||
ADDOP,/* - y x x+y */
|
||||
SUBOP,/* - y x x-y */
|
||||
MULTOP,/* - y x x*y */
|
||||
DIVOP,/* - y x x/y */
|
||||
POWOP,/* - y x x^y */
|
||||
CONCOP,/* - y x x..y */
|
||||
MINUSOP,/* - x -x */
|
||||
NOTOP,/* - x (x==nil)? 1 : nil */
|
||||
|
||||
ONTJMPW,/* w x (x!=nil)? x : - (x!=nil)? PC+=w */
|
||||
ONTJMP,/* b x (x!=nil)? x : - (x!=nil)? PC+=b */
|
||||
ONFJMPW,/* w x (x==nil)? x : - (x==nil)? PC+=w */
|
||||
ONFJMP,/* b x (x==nil)? x : - (x==nil)? PC+=b */
|
||||
JMPW,/* w - - PC+=w */
|
||||
JMP,/* b - - PC+=b */
|
||||
IFFJMPW,/* w x - (x==nil)? PC+=w */
|
||||
IFFJMP,/* b x - (x==nil)? PC+=b */
|
||||
IFTUPJMPW,/* w x - (x!=nil)? PC-=w */
|
||||
IFTUPJMP,/* b x - (x!=nil)? PC-=b */
|
||||
IFFUPJMPW,/* w x - (x==nil)? PC-=w */
|
||||
IFFUPJMP,/* b x - (x==nil)? PC-=b */
|
||||
|
||||
CLOSUREW,/* w c v_c...v_1 closure(CNST[w], v_c...v_1) */
|
||||
CLOSURE,/* b c v_c...v_1 closure(CNST[b], v_c...v_1) */
|
||||
|
||||
SETLINEW,/* w - - LINE=w */
|
||||
SETLINE,/* b - - LINE=b */
|
||||
|
||||
LONGARGW,/* w (add w*(1<<16) to arg of next instruction) */
|
||||
LONGARG,/* b (add b*(1<<16) to arg of next instruction) */
|
||||
|
||||
CHECKSTACK /* b (assert #temporaries == b; only for internal debuging!) */
|
||||
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)) */
|
||||
|
||||
OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */
|
||||
} OpCode;
|
||||
|
||||
|
||||
#define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
|
||||
#define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) */
|
||||
|
||||
#define ZEROVARARG 128
|
||||
#define NUM_OPCODES (cast(int, OP_VARARG) + 1)
|
||||
|
||||
|
||||
/* maximum value of an arg of 3 bytes; must fit in an "int" */
|
||||
#if MAX_INT < (1<<24)
|
||||
#define MAX_ARG MAX_INT
|
||||
#else
|
||||
#define MAX_ARG ((1<<24)-1)
|
||||
#endif
|
||||
|
||||
/* maximum value of a word of 2 bytes; cannot be bigger than MAX_ARG */
|
||||
#if MAX_ARG < (1<<16)
|
||||
#define MAX_WORD MAX_ARG
|
||||
#else
|
||||
#define MAX_WORD ((1<<16)-1)
|
||||
#endif
|
||||
/*===========================================================================
|
||||
Notes:
|
||||
(*) 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'.
|
||||
|
||||
(*) In OP_VARARG, if (B == 0) then use actual number of varargs and
|
||||
set top (like in OP_CALL with C == 0).
|
||||
|
||||
(*) In OP_RETURN, if (B == 0) then return up to `top'
|
||||
|
||||
(*) In OP_SETLIST, if (B == 0) then B = `top';
|
||||
if (C == 0) then next `instruction' is real C
|
||||
|
||||
(*) For comparisons, A specifies what condition the test should accept
|
||||
(true or false).
|
||||
|
||||
(*) All `skips' (pc++) assume that next instruction is a jump
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
/* maximum value of a byte */
|
||||
#define MAX_BYTE ((1<<8)-1)
|
||||
/*
|
||||
** masks for instruction properties. The format is:
|
||||
** bits 0-1: op mode
|
||||
** bits 2-3: C arg mode
|
||||
** bits 4-5: B arg mode
|
||||
** bit 6: instruction set register A
|
||||
** bit 7: operator is a test
|
||||
*/
|
||||
|
||||
enum OpArgMask {
|
||||
OpArgN, /* argument is not used */
|
||||
OpArgU, /* argument is used */
|
||||
OpArgR, /* argument is a register or a jump offset */
|
||||
OpArgK /* argument is a constant or register/constant */
|
||||
};
|
||||
|
||||
LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];
|
||||
|
||||
#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))
|
||||
#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
|
||||
#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
|
||||
#define testAMode(m) (luaP_opmodes[m] & (1 << 6))
|
||||
#define testTMode(m) (luaP_opmodes[m] & (1 << 7))
|
||||
|
||||
|
||||
LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */
|
||||
|
||||
|
||||
/* number of list items to accumulate before a SETLIST instruction */
|
||||
#define LFIELDS_PER_FLUSH 50
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
233
loslib.c
Normal file
233
loslib.c
Normal file
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
** $Id: loslib.c,v 1.18 2006/03/09 18:08:22 roberto Exp roberto $
|
||||
** Standard Operating System library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <locale.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define loslib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
static int os_pushresult (lua_State *L, int i, const char *filename) {
|
||||
int en = errno; /* calls to Lua API may change this value */
|
||||
if (i) {
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
lua_pushfstring(L, "%s: %s", filename, strerror(en));
|
||||
lua_pushinteger(L, en);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int os_execute (lua_State *L) {
|
||||
lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int os_remove (lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
return os_pushresult(L, remove(filename) == 0, filename);
|
||||
}
|
||||
|
||||
|
||||
static int os_rename (lua_State *L) {
|
||||
const char *fromname = luaL_checkstring(L, 1);
|
||||
const char *toname = luaL_checkstring(L, 2);
|
||||
return os_pushresult(L, rename(fromname, toname) == 0, fromname);
|
||||
}
|
||||
|
||||
|
||||
static int os_tmpname (lua_State *L) {
|
||||
char buff[LUA_TMPNAMBUFSIZE];
|
||||
int err;
|
||||
lua_tmpnam(buff, err);
|
||||
if (err)
|
||||
return luaL_error(L, "unable to generate a unique filename");
|
||||
lua_pushstring(L, buff);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int os_getenv (lua_State *L) {
|
||||
lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int os_clock (lua_State *L) {
|
||||
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Time/Date operations
|
||||
** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
|
||||
** wday=%w+1, yday=%j, isdst=? }
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void setfield (lua_State *L, const char *key, int value) {
|
||||
lua_pushinteger(L, value);
|
||||
lua_setfield(L, -2, key);
|
||||
}
|
||||
|
||||
static void setboolfield (lua_State *L, const char *key, int value) {
|
||||
if (value < 0) /* undefined? */
|
||||
return; /* does not set field */
|
||||
lua_pushboolean(L, value);
|
||||
lua_setfield(L, -2, key);
|
||||
}
|
||||
|
||||
static int getboolfield (lua_State *L, const char *key) {
|
||||
int res;
|
||||
lua_getfield(L, -1, key);
|
||||
res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static int getfield (lua_State *L, const char *key, int d) {
|
||||
int res;
|
||||
lua_getfield(L, -1, key);
|
||||
if (lua_isnumber(L, -1))
|
||||
res = (int)lua_tointeger(L, -1);
|
||||
else {
|
||||
if (d < 0)
|
||||
return luaL_error(L, "field " LUA_QS " missing in date table", key);
|
||||
res = d;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static int os_date (lua_State *L) {
|
||||
const char *s = luaL_optstring(L, 1, "%c");
|
||||
time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
|
||||
struct tm *stm;
|
||||
if (*s == '!') { /* UTC? */
|
||||
stm = gmtime(&t);
|
||||
s++; /* skip `!' */
|
||||
}
|
||||
else
|
||||
stm = localtime(&t);
|
||||
if (stm == NULL) /* invalid date? */
|
||||
lua_pushnil(L);
|
||||
else if (strcmp(s, "*t") == 0) {
|
||||
lua_createtable(L, 0, 9); /* 9 = number of fields */
|
||||
setfield(L, "sec", stm->tm_sec);
|
||||
setfield(L, "min", stm->tm_min);
|
||||
setfield(L, "hour", stm->tm_hour);
|
||||
setfield(L, "day", stm->tm_mday);
|
||||
setfield(L, "month", stm->tm_mon+1);
|
||||
setfield(L, "year", stm->tm_year+1900);
|
||||
setfield(L, "wday", stm->tm_wday+1);
|
||||
setfield(L, "yday", stm->tm_yday+1);
|
||||
setboolfield(L, "isdst", stm->tm_isdst);
|
||||
}
|
||||
else {
|
||||
char b[256];
|
||||
if (strftime(b, sizeof(b), s, stm))
|
||||
lua_pushstring(L, b);
|
||||
else
|
||||
return luaL_error(L, LUA_QL("date") " format too long");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int os_time (lua_State *L) {
|
||||
time_t t;
|
||||
if (lua_isnoneornil(L, 1)) /* called without args? */
|
||||
t = time(NULL); /* get current time */
|
||||
else {
|
||||
struct tm ts;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_settop(L, 1); /* make sure table is at the top */
|
||||
ts.tm_sec = getfield(L, "sec", 0);
|
||||
ts.tm_min = getfield(L, "min", 0);
|
||||
ts.tm_hour = getfield(L, "hour", 12);
|
||||
ts.tm_mday = getfield(L, "day", -1);
|
||||
ts.tm_mon = getfield(L, "month", -1) - 1;
|
||||
ts.tm_year = getfield(L, "year", -1) - 1900;
|
||||
ts.tm_isdst = getboolfield(L, "isdst");
|
||||
t = mktime(&ts);
|
||||
}
|
||||
if (t == (time_t)(-1))
|
||||
lua_pushnil(L);
|
||||
else
|
||||
lua_pushnumber(L, (lua_Number)t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int os_difftime (lua_State *L) {
|
||||
lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
|
||||
(time_t)(luaL_optnumber(L, 2, 0))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static int os_setlocale (lua_State *L) {
|
||||
static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
|
||||
LC_NUMERIC, LC_TIME};
|
||||
static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
|
||||
"numeric", "time", NULL};
|
||||
const char *l = luaL_optstring(L, 1, NULL);
|
||||
int op = luaL_checkoption(L, 2, "all", catnames);
|
||||
lua_pushstring(L, setlocale(cat[op], l));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int os_exit (lua_State *L) {
|
||||
exit(luaL_optint(L, 1, EXIT_SUCCESS));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
static const luaL_Reg syslib[] = {
|
||||
{"clock", os_clock},
|
||||
{"date", os_date},
|
||||
{"difftime", os_difftime},
|
||||
{"execute", os_execute},
|
||||
{"exit", os_exit},
|
||||
{"getenv", os_getenv},
|
||||
{"remove", os_remove},
|
||||
{"rename", os_rename},
|
||||
{"setlocale", os_setlocale},
|
||||
{"time", os_time},
|
||||
{"tmpname", os_tmpname},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
LUALIB_API int luaopen_os (lua_State *L) {
|
||||
luaL_register(L, LUA_OSLIBNAME, syslib);
|
||||
return 1;
|
||||
}
|
||||
|
||||
74
lparser.h
74
lparser.h
@@ -1,20 +1,82 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.2 1997/12/22 20:57:18 roberto Exp roberto $
|
||||
** LL(1) Parser and code generator for Lua
|
||||
** $Id: lparser.h,v 1.56 2005/10/03 14:02:40 roberto Exp roberto $
|
||||
** Lua Parser
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lparser_h
|
||||
#define lparser_h
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
void luaY_codedebugline (int line);
|
||||
TProtoFunc *luaY_parser (ZIO *z);
|
||||
void luaY_error (char *s);
|
||||
void luaY_syntaxerror (char *s, char *token);
|
||||
/*
|
||||
** Expression descriptor
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
VVOID, /* no value */
|
||||
VNIL,
|
||||
VTRUE,
|
||||
VFALSE,
|
||||
VK, /* info = index of constant in `k' */
|
||||
VKNUM, /* nval = numerical value */
|
||||
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 = instruction pc */
|
||||
VVARARG /* info = instruction pc */
|
||||
} expkind;
|
||||
|
||||
typedef struct expdesc {
|
||||
expkind k;
|
||||
union {
|
||||
struct { int info, aux; } s;
|
||||
lua_Number nval;
|
||||
} u;
|
||||
int t; /* patch list of `exit when true' */
|
||||
int f; /* patch list of `exit when false' */
|
||||
} expdesc;
|
||||
|
||||
|
||||
typedef struct upvaldesc {
|
||||
lu_byte k;
|
||||
lu_byte info;
|
||||
} upvaldesc;
|
||||
|
||||
|
||||
struct BlockCnt; /* defined in lparser.c */
|
||||
|
||||
|
||||
/* state needed to generate code for a given function */
|
||||
typedef struct FuncState {
|
||||
Proto *f; /* current function header */
|
||||
Table *h; /* table to find (and reuse) elements in `k' */
|
||||
struct FuncState *prev; /* enclosing function */
|
||||
struct LexState *ls; /* lexical state */
|
||||
struct lua_State *L; /* copy of the Lua state */
|
||||
struct BlockCnt *bl; /* chain of current blocks */
|
||||
int pc; /* next position to code (equivalent to `ncode') */
|
||||
int lasttarget; /* `pc' of last `jump target' */
|
||||
int jpc; /* list of pending jumps to `pc' */
|
||||
int freereg; /* first free register */
|
||||
int nk; /* number of elements in `k' */
|
||||
int np; /* number of elements in `p' */
|
||||
short nlocvars; /* number of elements in `locvars' */
|
||||
lu_byte nactvar; /* number of active local variables */
|
||||
upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */
|
||||
unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */
|
||||
} FuncState;
|
||||
|
||||
|
||||
LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
const char *name);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
248
lstate.c
248
lstate.c
@@ -1,11 +1,18 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 1.11 1999/05/11 14:19:32 roberto Exp roberto $
|
||||
** $Id: lstate.c,v 2.35 2005/10/06 20:46:25 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include "lbuiltin.h"
|
||||
#include <stddef.h>
|
||||
|
||||
#define lstate_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
@@ -17,70 +24,191 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
lua_State *lua_state = NULL;
|
||||
#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
|
||||
#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
|
||||
#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
|
||||
|
||||
|
||||
void lua_open (void)
|
||||
{
|
||||
if (lua_state) return;
|
||||
lua_state = luaM_new(lua_State);
|
||||
L->Cstack.base = 0;
|
||||
L->Cstack.lua2C = 0;
|
||||
L->Cstack.num = 0;
|
||||
/*
|
||||
** Main thread combines a thread state and the global state
|
||||
*/
|
||||
typedef struct LG {
|
||||
lua_State l;
|
||||
global_State g;
|
||||
} LG;
|
||||
|
||||
|
||||
|
||||
static void stack_init (lua_State *L1, lua_State *L) {
|
||||
/* initialize CallInfo array */
|
||||
L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
|
||||
L1->ci = L1->base_ci;
|
||||
L1->size_ci = BASIC_CI_SIZE;
|
||||
L1->end_ci = L1->base_ci + L1->size_ci - 1;
|
||||
/* initialize stack array */
|
||||
L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
|
||||
L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
|
||||
L1->top = L1->stack;
|
||||
L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
|
||||
/* initialize first ci */
|
||||
L1->ci->func = L1->top;
|
||||
setnilvalue(L1->top++); /* `function' entry for this `ci' */
|
||||
L1->base = L1->ci->base = L1->top;
|
||||
L1->ci->top = L1->top + LUA_MINSTACK;
|
||||
}
|
||||
|
||||
|
||||
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, TValue);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** open parts that may cause memory-allocation errors
|
||||
*/
|
||||
static void f_luaopen (lua_State *L, void *ud) {
|
||||
global_State *g = G(L);
|
||||
UNUSED(ud);
|
||||
stack_init(L, L); /* init stack */
|
||||
sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */
|
||||
sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */
|
||||
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
|
||||
luaT_init(L);
|
||||
luaX_init(L);
|
||||
luaS_fix(luaS_newliteral(L, MEMERRMSG));
|
||||
g->GCthreshold = 4*g->totalbytes;
|
||||
}
|
||||
|
||||
|
||||
static void preinit_state (lua_State *L, global_State *g) {
|
||||
G(L) = g;
|
||||
L->stack = NULL;
|
||||
L->stacksize = 0;
|
||||
L->errorJmp = NULL;
|
||||
L->Mbuffer = NULL;
|
||||
L->Mbuffbase = 0;
|
||||
L->Mbuffsize = 0;
|
||||
L->Mbuffnext = 0;
|
||||
L->Cblocks = NULL;
|
||||
L->numCblocks = 0;
|
||||
L->debug = 0;
|
||||
L->callhook = NULL;
|
||||
L->linehook = NULL;
|
||||
L->rootproto.next = NULL;
|
||||
L->rootproto.marked = 0;
|
||||
L->rootcl.next = NULL;
|
||||
L->rootcl.marked = 0;
|
||||
L->rootglobal.next = NULL;
|
||||
L->rootglobal.marked = 0;
|
||||
L->roottable.next = NULL;
|
||||
L->roottable.marked = 0;
|
||||
L->IMtable = NULL;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
L->GCthreshold = GARBAGE_BLOCK;
|
||||
L->nblocks = 0;
|
||||
luaD_init();
|
||||
luaS_init();
|
||||
luaX_init();
|
||||
luaT_init();
|
||||
luaB_predefine();
|
||||
L->hook = NULL;
|
||||
L->hookmask = 0;
|
||||
L->basehookcount = 0;
|
||||
L->allowhook = 1;
|
||||
resethookcount(L);
|
||||
L->openupval = NULL;
|
||||
L->size_ci = 0;
|
||||
L->nCcalls = 0;
|
||||
L->status = 0;
|
||||
L->base_ci = L->ci = NULL;
|
||||
L->savedpc = NULL;
|
||||
L->errfunc = 0;
|
||||
setnilvalue(gt(L));
|
||||
}
|
||||
|
||||
|
||||
void lua_close (void)
|
||||
{
|
||||
TaggedString *alludata = luaS_collectudata();
|
||||
L->GCthreshold = MAX_INT; /* to avoid GC during GC */
|
||||
luaC_hashcallIM((Hash *)L->roottable.next); /* GC t.methods for tables */
|
||||
luaC_strcallIM(alludata); /* GC tag methods for userdata */
|
||||
luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
|
||||
luaH_free((Hash *)L->roottable.next);
|
||||
luaF_freeproto((TProtoFunc *)L->rootproto.next);
|
||||
luaF_freeclosure((Closure *)L->rootcl.next);
|
||||
luaS_free(alludata);
|
||||
luaS_freeall();
|
||||
luaM_free(L->stack.stack);
|
||||
luaM_free(L->IMtable);
|
||||
luaM_free(L->refArray);
|
||||
luaM_free(L->Mbuffer);
|
||||
luaM_free(L->Cblocks);
|
||||
luaM_free(L);
|
||||
L = NULL;
|
||||
#ifdef DEBUG
|
||||
printf("total de blocos: %ld\n", numblocks);
|
||||
printf("total de memoria: %ld\n", totalmem);
|
||||
#endif
|
||||
static void close_state (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
luaF_close(L, L->stack); /* close all upvalues for this thread */
|
||||
luaC_freeall(L); /* collect all objects */
|
||||
lua_assert(g->rootgc == obj2gco(L));
|
||||
lua_assert(g->strt.nuse == 0);
|
||||
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
|
||||
luaZ_freebuffer(L, &g->buff);
|
||||
freestack(L, L);
|
||||
lua_assert(g->totalbytes == sizeof(LG));
|
||||
(*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
|
||||
}
|
||||
|
||||
|
||||
lua_State *luaE_newthread (lua_State *L) {
|
||||
lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
|
||||
luaC_link(L, obj2gco(L1), LUA_TTHREAD);
|
||||
preinit_state(L1, G(L));
|
||||
stack_init(L1, L); /* init stack */
|
||||
setobj2n(L, gt(L1), gt(L)); /* share table of globals */
|
||||
L1->hookmask = L->hookmask;
|
||||
L1->basehookcount = L->basehookcount;
|
||||
L1->hook = L->hook;
|
||||
resethookcount(L1);
|
||||
lua_assert(iswhite(obj2gco(L1)));
|
||||
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);
|
||||
luai_userstatefree(L1);
|
||||
freestack(L, L1);
|
||||
luaM_freemem(L, fromstate(L1), state_size(lua_State));
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
int i;
|
||||
lua_State *L;
|
||||
global_State *g;
|
||||
void *l = (*f)(ud, NULL, 0, state_size(LG));
|
||||
if (l == NULL) return NULL;
|
||||
L = tostate(l);
|
||||
g = &((LG *)L)->g;
|
||||
L->next = NULL;
|
||||
L->tt = LUA_TTHREAD;
|
||||
g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
|
||||
L->marked = luaC_white(g);
|
||||
set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
|
||||
preinit_state(L, g);
|
||||
g->frealloc = f;
|
||||
g->ud = ud;
|
||||
g->mainthread = L;
|
||||
g->uvhead.u.l.prev = &g->uvhead;
|
||||
g->uvhead.u.l.next = &g->uvhead;
|
||||
g->GCthreshold = 0; /* mark it as unfinished state */
|
||||
g->strt.size = 0;
|
||||
g->strt.nuse = 0;
|
||||
g->strt.hash = NULL;
|
||||
setnilvalue(registry(L));
|
||||
luaZ_initbuffer(L, &g->buff);
|
||||
g->panic = NULL;
|
||||
g->gcstate = GCSpause;
|
||||
g->rootgc = obj2gco(L);
|
||||
g->sweepstrgc = 0;
|
||||
g->sweepgc = &g->rootgc;
|
||||
g->gray = NULL;
|
||||
g->grayagain = NULL;
|
||||
g->weak = NULL;
|
||||
g->tmudata = NULL;
|
||||
g->totalbytes = sizeof(LG);
|
||||
g->gcpause = LUAI_GCPAUSE;
|
||||
g->gcstepmul = LUAI_GCMUL;
|
||||
g->gcdept = 0;
|
||||
for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
|
||||
if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
|
||||
/* memory allocation error: free partial state */
|
||||
close_state(L);
|
||||
L = NULL;
|
||||
}
|
||||
else
|
||||
luai_userstateopen(L);
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
static void callallgcTM (lua_State *L, void *ud) {
|
||||
UNUSED(ud);
|
||||
luaC_callGCTM(L); /* call GC metamethods for all udata */
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_close (lua_State *L) {
|
||||
L = G(L)->mainthread; /* only the main thread can be closed */
|
||||
lua_lock(L);
|
||||
luaF_close(L, L->stack); /* close all upvalues for this thread */
|
||||
luaC_separateudata(L, 1); /* 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);
|
||||
luai_userstateclose(L);
|
||||
close_state(L);
|
||||
}
|
||||
|
||||
|
||||
188
lstate.h
188
lstate.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 1.18 1999/05/11 14:19:32 roberto Exp roberto $
|
||||
** $Id: lstate.h,v 2.23 2005/07/09 13:22:34 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,88 +7,162 @@
|
||||
#ifndef lstate_h
|
||||
#define lstate_h
|
||||
|
||||
#include <setjmp.h>
|
||||
#include "lua.h"
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "ltm.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
#define GARBAGE_BLOCK 150
|
||||
|
||||
struct lua_longjmp; /* defined in ldo.c */
|
||||
|
||||
|
||||
typedef int StkId; /* index to stack elements */
|
||||
/* table of globals */
|
||||
#define gt(L) (&L->l_gt)
|
||||
|
||||
/* registry */
|
||||
#define registry(L) (&G(L)->l_registry)
|
||||
|
||||
|
||||
/*
|
||||
** "jmp_buf" may be an array, so it is better to make sure it has an
|
||||
** address (and not that it *is* an address...)
|
||||
*/
|
||||
struct lua_longjmp {
|
||||
jmp_buf b;
|
||||
};
|
||||
/* extra stack space to handle TM calls and some other extras */
|
||||
#define EXTRA_STACK 5
|
||||
|
||||
|
||||
struct Stack {
|
||||
TObject *top;
|
||||
TObject *stack;
|
||||
TObject *last;
|
||||
};
|
||||
#define BASIC_CI_SIZE 8
|
||||
|
||||
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
|
||||
|
||||
struct C_Lua_Stack {
|
||||
StkId base; /* when Lua calls C or C calls Lua, points to */
|
||||
/* the first slot after the last parameter. */
|
||||
StkId lua2C; /* points to first element of "array" lua2C */
|
||||
int num; /* size of "array" lua2C */
|
||||
};
|
||||
|
||||
|
||||
typedef struct stringtable {
|
||||
GCObject **hash;
|
||||
lu_int32 nuse; /* number of elements */
|
||||
int size;
|
||||
int nuse; /* number of elements (including EMPTYs) */
|
||||
TaggedString **hash;
|
||||
} stringtable;
|
||||
|
||||
|
||||
enum Status {LOCK, HOLD, FREE, COLLECTED};
|
||||
|
||||
struct ref {
|
||||
TObject o;
|
||||
enum Status status;
|
||||
};
|
||||
/*
|
||||
** informations about a call
|
||||
*/
|
||||
typedef struct CallInfo {
|
||||
StkId base; /* base for this function */
|
||||
StkId func; /* function index in the stack */
|
||||
StkId top; /* top for this function */
|
||||
const Instruction *savedpc;
|
||||
int nresults; /* expected number of results from this function */
|
||||
int tailcalls; /* number of tail calls lost under this entry */
|
||||
} CallInfo;
|
||||
|
||||
|
||||
|
||||
#define curr_func(L) (clvalue(L->ci->func))
|
||||
#define ci_func(ci) (clvalue((ci)->func))
|
||||
#define f_isLua(ci) (!ci_func(ci)->c.isC)
|
||||
#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci))
|
||||
|
||||
|
||||
/*
|
||||
** `global state', shared by all threads of this state
|
||||
*/
|
||||
typedef struct global_State {
|
||||
stringtable strt; /* hash table for strings */
|
||||
lua_Alloc frealloc; /* function to reallocate memory */
|
||||
void *ud; /* auxiliary data to `frealloc' */
|
||||
lu_byte currentwhite;
|
||||
lu_byte gcstate; /* state of garbage collector */
|
||||
int sweepstrgc; /* position of sweep in `strt' */
|
||||
GCObject *rootgc; /* list of all collectable objects */
|
||||
GCObject **sweepgc; /* position of sweep in `rootgc' */
|
||||
GCObject *gray; /* list of gray objects */
|
||||
GCObject *grayagain; /* list of objects to be traversed atomically */
|
||||
GCObject *weak; /* list of weak tables (to be cleared) */
|
||||
GCObject *tmudata; /* last element of list of userdata to be GC */
|
||||
Mbuffer buff; /* temporary buffer for string concatentation */
|
||||
lu_mem GCthreshold;
|
||||
lu_mem totalbytes; /* number of bytes currently allocated */
|
||||
lu_mem estimate; /* an estimate of number of bytes actually in use */
|
||||
lu_mem gcdept; /* how much GC is `behind schedule' */
|
||||
int gcpause; /* size of pause between successive GCs */
|
||||
int gcstepmul; /* GC `granularity' */
|
||||
lua_CFunction panic; /* to be called in unprotected errors */
|
||||
TValue l_registry;
|
||||
struct lua_State *mainthread;
|
||||
UpVal uvhead; /* head of double-linked list of all open upvalues */
|
||||
struct Table *mt[NUM_TAGS]; /* metatables for basic types */
|
||||
TString *tmname[TM_N]; /* array with tag-method names */
|
||||
} global_State;
|
||||
|
||||
|
||||
/*
|
||||
** `per thread' state
|
||||
*/
|
||||
struct lua_State {
|
||||
/* thread-specific state */
|
||||
struct Stack stack; /* Lua stack */
|
||||
struct C_Lua_Stack Cstack; /* C2lua struct */
|
||||
CommonHeader;
|
||||
lu_byte status;
|
||||
StkId top; /* first free slot in the stack */
|
||||
StkId base; /* base of current function */
|
||||
global_State *l_G;
|
||||
CallInfo *ci; /* call info for current function */
|
||||
const Instruction *savedpc; /* `savedpc' of current function */
|
||||
StkId stack_last; /* last free slot in the stack */
|
||||
StkId stack; /* stack base */
|
||||
CallInfo *end_ci; /* points after end of ci array*/
|
||||
CallInfo *base_ci; /* array of CallInfo's */
|
||||
int stacksize;
|
||||
int size_ci; /* size of array `base_ci' */
|
||||
unsigned short nCcalls; /* number of nested C calls */
|
||||
lu_byte hookmask;
|
||||
lu_byte allowhook;
|
||||
int basehookcount;
|
||||
int hookcount;
|
||||
lua_Hook hook;
|
||||
TValue l_gt; /* table of globals */
|
||||
TValue env; /* temporary place for environments */
|
||||
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 */
|
||||
int debug;
|
||||
lua_CHFunction callhook;
|
||||
lua_LHFunction linehook;
|
||||
/* global state */
|
||||
GCnode rootproto; /* list of all prototypes */
|
||||
GCnode rootcl; /* list of all closures */
|
||||
GCnode roottable; /* list of all tables */
|
||||
GCnode rootglobal; /* list of strings with global values */
|
||||
stringtable *string_root; /* array of hash tables for strings and udata */
|
||||
struct IM *IMtable; /* table for tag methods */
|
||||
int last_tag; /* last used tag in IMtable */
|
||||
struct ref *refArray; /* locked objects */
|
||||
int refSize; /* size of refArray */
|
||||
unsigned long GCthreshold;
|
||||
unsigned long nblocks; /* number of 'blocks' currently allocated */
|
||||
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
||||
};
|
||||
|
||||
|
||||
#define L lua_state
|
||||
#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 rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
|
||||
#define gco2ts(o) (&rawgco2ts(o)->tsv)
|
||||
#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
|
||||
#define gco2u(o) (&rawgco2u(o)->uv)
|
||||
#define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
|
||||
#define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
|
||||
#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
|
||||
#define gco2uv(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 gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
|
||||
|
||||
/* macro to convert any Lua object into a GCObject */
|
||||
#define obj2gco(v) (cast(GCObject *, (v)))
|
||||
|
||||
|
||||
LUAI_FUNC lua_State *luaE_newthread (lua_State *L);
|
||||
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
337
lstring.c
337
lstring.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 1.18 1999/02/08 16:28:48 roberto Exp roberto $
|
||||
** $Id: lstring.c,v 2.7 2005/02/18 12:40:02 roberto Exp roberto $
|
||||
** String table (keeps all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,292 +7,105 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define lstring_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define NUM_HASHSTR 31
|
||||
#define NUM_HASHUDATA 31
|
||||
#define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA)
|
||||
|
||||
|
||||
#define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */
|
||||
|
||||
|
||||
|
||||
static TaggedString EMPTY = {{NULL, 2}, 0L, 0,
|
||||
{{{LUA_T_NIL, {NULL}}, 0L}}, {0}};
|
||||
|
||||
|
||||
void luaS_init (void) {
|
||||
void luaS_resize (lua_State *L, int newsize) {
|
||||
GCObject **newhash;
|
||||
stringtable *tb;
|
||||
int i;
|
||||
L->string_root = luaM_newvector(NUM_HASHS, stringtable);
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
L->string_root[i].size = 0;
|
||||
L->string_root[i].nuse = 0;
|
||||
L->string_root[i].hash = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static unsigned long hash_s (char *s, long l) {
|
||||
unsigned long h = 0; /* seed */
|
||||
while (l--)
|
||||
h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
|
||||
return h;
|
||||
}
|
||||
|
||||
static int newsize (stringtable *tb) {
|
||||
int size = tb->size;
|
||||
int realuse = 0;
|
||||
int i;
|
||||
/* count how many entries are really in use */
|
||||
for (i=0; i<size; i++)
|
||||
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
|
||||
realuse++;
|
||||
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
||||
}
|
||||
|
||||
|
||||
static void grow (stringtable *tb) {
|
||||
int ns = newsize(tb);
|
||||
TaggedString **newhash = luaM_newvector(ns, TaggedString *);
|
||||
int i;
|
||||
for (i=0; i<ns; i++)
|
||||
newhash[i] = NULL;
|
||||
if (G(L)->gcstate == GCSsweepstring)
|
||||
return; /* cannot resize during GC traverse */
|
||||
newhash = luaM_newvector(L, newsize, GCObject *);
|
||||
tb = &G(L)->strt;
|
||||
for (i=0; i<newsize; i++) newhash[i] = NULL;
|
||||
/* rehash */
|
||||
tb->nuse = 0;
|
||||
for (i=0; i<tb->size; i++) {
|
||||
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
|
||||
unsigned long h = tb->hash[i]->hash;
|
||||
int h1 = h%ns;
|
||||
while (newhash[h1]) {
|
||||
h1 += (h&(ns-2)) + 1; /* double hashing */
|
||||
if (h1 >= ns) h1 -= ns;
|
||||
}
|
||||
newhash[h1] = tb->hash[i];
|
||||
tb->nuse++;
|
||||
GCObject *p = tb->hash[i];
|
||||
while (p) { /* for each node in the list */
|
||||
GCObject *next = p->gch.next; /* save next */
|
||||
unsigned int h = gco2ts(p)->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(tb->hash);
|
||||
tb->size = ns;
|
||||
luaM_freearray(L, tb->hash, tb->size, TString *);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static TaggedString *newone_s (char *str, long l, unsigned long h) {
|
||||
TaggedString *ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
|
||||
memcpy(ts->str, str, l);
|
||||
ts->str[l] = 0; /* ending 0 */
|
||||
ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */
|
||||
ts->u.s.len = l;
|
||||
ts->constindex = 0;
|
||||
L->nblocks += gcsizestring(l);
|
||||
ts->head.marked = 0;
|
||||
ts->head.next = (GCnode *)ts; /* signal it is in no list */
|
||||
ts->hash = h;
|
||||
static TString *newlstr (lua_State *L, const char *str, size_t l,
|
||||
unsigned int h) {
|
||||
TString *ts;
|
||||
stringtable *tb;
|
||||
if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
|
||||
luaM_toobig(L);
|
||||
ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
|
||||
ts->tsv.len = l;
|
||||
ts->tsv.hash = h;
|
||||
ts->tsv.marked = luaC_white(G(L));
|
||||
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] = obj2gco(ts);
|
||||
tb->nuse++;
|
||||
if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
|
||||
luaS_resize(L, tb->size*2); /* too crowded */
|
||||
return ts;
|
||||
}
|
||||
|
||||
static TaggedString *newone_u (char *buff, int tag, unsigned long h) {
|
||||
TaggedString *ts = luaM_new(TaggedString);
|
||||
ts->u.d.v = buff;
|
||||
ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
|
||||
ts->constindex = -1; /* tag -> this is a userdata */
|
||||
L->nblocks++;
|
||||
ts->head.marked = 0;
|
||||
ts->head.next = (GCnode *)ts; /* signal it is in no list */
|
||||
ts->hash = h;
|
||||
return ts;
|
||||
}
|
||||
|
||||
static TaggedString *insert_s (char *str, long l, stringtable *tb) {
|
||||
TaggedString *ts;
|
||||
unsigned long h = hash_s(str, l);
|
||||
int size = tb->size;
|
||||
int j = -1;
|
||||
int h1;
|
||||
if ((long)tb->nuse*3 >= (long)size*2) {
|
||||
grow(tb);
|
||||
size = tb->size;
|
||||
}
|
||||
h1 = h%size;
|
||||
while ((ts = tb->hash[h1]) != NULL) {
|
||||
if (ts == &EMPTY)
|
||||
j = h1;
|
||||
else 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;
|
||||
unsigned int h = cast(unsigned int, 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)+cast(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 = rawgco2ts(o);
|
||||
if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
|
||||
/* string may be dead */
|
||||
if (isdead(G(L), o)) changewhite(o);
|
||||
return ts;
|
||||
h1 += (h&(size-2)) + 1; /* double hashing */
|
||||
if (h1 >= size) h1 -= size;
|
||||
}
|
||||
/* not found */
|
||||
if (j != -1) /* is there an EMPTY space? */
|
||||
h1 = j;
|
||||
else
|
||||
tb->nuse++;
|
||||
ts = tb->hash[h1] = newone_s(str, l, h);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static TaggedString *insert_u (void *buff, int tag, stringtable *tb) {
|
||||
TaggedString *ts;
|
||||
unsigned long h = (unsigned long)buff;
|
||||
int size = tb->size;
|
||||
int j = -1;
|
||||
int h1;
|
||||
if ((long)tb->nuse*3 >= (long)size*2) {
|
||||
grow(tb);
|
||||
size = tb->size;
|
||||
}
|
||||
h1 = h%size;
|
||||
while ((ts = tb->hash[h1]) != NULL) {
|
||||
if (ts == &EMPTY)
|
||||
j = h1;
|
||||
else if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v)
|
||||
return ts;
|
||||
h1 += (h&(size-2)) + 1; /* double hashing */
|
||||
if (h1 >= size) h1 -= size;
|
||||
}
|
||||
/* not found */
|
||||
if (j != -1) /* is there an EMPTY space? */
|
||||
h1 = j;
|
||||
else
|
||||
tb->nuse++;
|
||||
ts = tb->hash[h1] = newone_u(buff, tag, h);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
TaggedString *luaS_createudata (void *udata, int tag) {
|
||||
int t = ((unsigned)udata%NUM_HASHUDATA)+NUM_HASHSTR;
|
||||
return insert_u(udata, tag, &L->string_root[t]);
|
||||
}
|
||||
|
||||
TaggedString *luaS_newlstr (char *str, long l) {
|
||||
int t = (l==0) ? 0 : ((int)((unsigned char)str[0]*l))%NUM_HASHSTR;
|
||||
return insert_s(str, l, &L->string_root[t]);
|
||||
}
|
||||
|
||||
TaggedString *luaS_new (char *str) {
|
||||
return luaS_newlstr(str, strlen(str));
|
||||
}
|
||||
|
||||
TaggedString *luaS_newfixedstring (char *str) {
|
||||
TaggedString *ts = luaS_new(str);
|
||||
if (ts->head.marked == 0)
|
||||
ts->head.marked = 2; /* avoid GC */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
void luaS_free (TaggedString *l) {
|
||||
while (l) {
|
||||
TaggedString *next = (TaggedString *)l->head.next;
|
||||
L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(l->u.s.len);
|
||||
luaM_free(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage collection functions.
|
||||
*/
|
||||
|
||||
static void remove_from_list (GCnode *l) {
|
||||
while (l) {
|
||||
GCnode *next = l->next;
|
||||
while (next && !next->marked)
|
||||
next = l->next = next->next;
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TaggedString *luaS_collector (void) {
|
||||
TaggedString *frees = NULL;
|
||||
int i;
|
||||
remove_from_list(&(L->rootglobal));
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t == NULL) continue;
|
||||
if (t->head.marked == 1)
|
||||
t->head.marked = 0;
|
||||
else if (!t->head.marked) {
|
||||
t->head.next = (GCnode *)frees;
|
||||
frees = t;
|
||||
tb->hash[j] = &EMPTY;
|
||||
}
|
||||
}
|
||||
}
|
||||
return frees;
|
||||
return newlstr(L, str, l, h); /* not found */
|
||||
}
|
||||
|
||||
|
||||
TaggedString *luaS_collectudata (void) {
|
||||
TaggedString *frees = NULL;
|
||||
int i;
|
||||
L->rootglobal.next = NULL; /* empty list of globals */
|
||||
for (i=NUM_HASHSTR; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t == NULL || t == &EMPTY)
|
||||
continue;
|
||||
LUA_ASSERT(t->constindex == -1, "must be userdata");
|
||||
t->head.next = (GCnode *)frees;
|
||||
frees = t;
|
||||
tb->hash[j] = &EMPTY;
|
||||
}
|
||||
}
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
void luaS_freeall (void) {
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t == &EMPTY) continue;
|
||||
luaM_free(t);
|
||||
}
|
||||
luaM_free(tb->hash);
|
||||
}
|
||||
luaM_free(L->string_root);
|
||||
}
|
||||
|
||||
|
||||
void luaS_rawsetglobal (TaggedString *ts, TObject *newval) {
|
||||
ts->u.s.globalval = *newval;
|
||||
if (ts->head.next == (GCnode *)ts) { /* is not in list? */
|
||||
ts->head.next = L->rootglobal.next;
|
||||
L->rootglobal.next = (GCnode *)ts;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char *luaS_travsymbol (int (*fn)(TObject *)) {
|
||||
TaggedString *g;
|
||||
for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
|
||||
if (fn(&g->u.s.globalval))
|
||||
return g->str;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int luaS_globaldefined (char *name) {
|
||||
TaggedString *ts = luaS_new(name);
|
||||
return ts->u.s.globalval.ttype != LUA_T_NIL;
|
||||
Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
|
||||
Udata *u;
|
||||
if (s > MAX_SIZET - sizeof(Udata))
|
||||
luaM_toobig(L);
|
||||
u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
|
||||
u->uv.marked = luaC_white(G(L)); /* is not finalized */
|
||||
u->uv.tt = LUA_TUSERDATA;
|
||||
u->uv.len = s;
|
||||
u->uv.metatable = NULL;
|
||||
u->uv.env = e;
|
||||
/* chain it on udata list (after main thread) */
|
||||
u->uv.next = G(L)->mainthread->next;
|
||||
G(L)->mainthread->next = obj2gco(u);
|
||||
return u;
|
||||
}
|
||||
|
||||
|
||||
29
lstring.h
29
lstring.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.6 1997/12/01 20:31:25 roberto Exp roberto $
|
||||
** $Id: lstring.h,v 1.42 2005/02/23 17:30:22 roberto Exp roberto $
|
||||
** String table (keep all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,21 +8,24 @@
|
||||
#define lstring_h
|
||||
|
||||
|
||||
#include "lgc.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
void luaS_init (void);
|
||||
TaggedString *luaS_createudata (void *udata, int tag);
|
||||
TaggedString *luaS_collector (void);
|
||||
void luaS_free (TaggedString *l);
|
||||
TaggedString *luaS_newlstr (char *str, long l);
|
||||
TaggedString *luaS_new (char *str);
|
||||
TaggedString *luaS_newfixedstring (char *str);
|
||||
void luaS_rawsetglobal (TaggedString *ts, TObject *newval);
|
||||
char *luaS_travsymbol (int (*fn)(TObject *));
|
||||
int luaS_globaldefined (char *name);
|
||||
TaggedString *luaS_collectudata (void);
|
||||
void luaS_freeall (void);
|
||||
#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char))
|
||||
|
||||
#define sizeudata(u) (sizeof(union Udata)+(u)->len)
|
||||
|
||||
#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s)))
|
||||
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
|
||||
(sizeof(s)/sizeof(char))-1))
|
||||
|
||||
#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT)
|
||||
|
||||
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
||||
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e);
|
||||
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
667
ltable.c
667
ltable.c
@@ -1,177 +1,588 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 1.21 1999/02/23 14:57:28 roberto Exp roberto $
|
||||
** $Id: ltable.c,v 2.31 2006/01/10 13:13:06 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
/*
|
||||
** Implementation of tables (aka arrays, objects, or hash tables).
|
||||
** 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.
|
||||
** Hence even when the load factor reaches 100%, performance remains good.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ltable_c
|
||||
#define LUA_CORE
|
||||
|
||||
#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(n) (1+(n/16))
|
||||
/*
|
||||
** max size of array part is 2^MAXBITS
|
||||
*/
|
||||
#if LUAI_BITSINT > 26
|
||||
#define MAXBITS 26
|
||||
#else
|
||||
#define MAXBITS (LUAI_BITSINT-2)
|
||||
#endif
|
||||
|
||||
#define nuse(t) ((t)->nuse)
|
||||
#define nodevector(t) ((t)->node)
|
||||
#define MAXASIZE (1 << MAXBITS)
|
||||
|
||||
|
||||
#define TagDefault LUA_T_ARRAY;
|
||||
#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))
|
||||
|
||||
|
||||
|
||||
static long int hashindex (TObject *ref) {
|
||||
long int h;
|
||||
switch (ttype(ref)) {
|
||||
case LUA_T_NUMBER:
|
||||
h = (long int)nvalue(ref);
|
||||
break;
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
h = (IntPoint)tsvalue(ref);
|
||||
break;
|
||||
case LUA_T_ARRAY:
|
||||
h = (IntPoint)avalue(ref);
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
h = (IntPoint)tfvalue(ref);
|
||||
break;
|
||||
case LUA_T_CPROTO:
|
||||
h = (IntPoint)fvalue(ref);
|
||||
break;
|
||||
case LUA_T_CLOSURE:
|
||||
h = (IntPoint)clvalue(ref);
|
||||
break;
|
||||
default:
|
||||
lua_error("unexpected type to index table");
|
||||
h = 0; /* to avoid warnings */
|
||||
}
|
||||
return (h >= 0 ? h : -(h+1));
|
||||
}
|
||||
#define dummynode (&dummynode_)
|
||||
|
||||
static const Node dummynode_ = {
|
||||
{{NULL}, LUA_TNIL}, /* value */
|
||||
{{{NULL}, LUA_TNIL, NULL}} /* key */
|
||||
};
|
||||
|
||||
|
||||
Node *luaH_present (Hash *t, TObject *key) {
|
||||
int tsize = nhash(t);
|
||||
long int h = hashindex(key);
|
||||
int h1 = h%tsize;
|
||||
Node *n = node(t, h1);
|
||||
/* keep looking until an entry with "ref" equal to key or nil */
|
||||
while ((ttype(ref(n)) == ttype(key)) ? !luaO_equalval(key, ref(n))
|
||||
: ttype(ref(n)) != LUA_T_NIL) {
|
||||
h1 += (h&(tsize-2)) + 1; /* double hashing */
|
||||
if (h1 >= tsize) h1 -= tsize;
|
||||
n = node(t, h1);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
void luaH_free (Hash *frees) {
|
||||
while (frees) {
|
||||
Hash *next = (Hash *)frees->head.next;
|
||||
L->nblocks -= gcsize(frees->nhash);
|
||||
luaM_free(nodevector(frees));
|
||||
luaM_free(frees);
|
||||
frees = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static Node *hashnodecreate (int nhash) {
|
||||
Node *v = luaM_newvector(nhash, Node);
|
||||
/*
|
||||
** hash for lua_Numbers
|
||||
*/
|
||||
static Node *hashnum (const Table *t, lua_Number n) {
|
||||
unsigned int a[numints];
|
||||
int i;
|
||||
for (i=0; i<nhash; i++)
|
||||
ttype(ref(&v[i])) = ttype(val(&v[i])) = LUA_T_NIL;
|
||||
return v;
|
||||
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, a[0]);
|
||||
}
|
||||
|
||||
|
||||
Hash *luaH_new (int nhash) {
|
||||
Hash *t = luaM_new(Hash);
|
||||
nhash = luaO_redimension(nhash*3/2);
|
||||
nodevector(t) = hashnodecreate(nhash);
|
||||
nhash(t) = nhash;
|
||||
nuse(t) = 0;
|
||||
t->htag = TagDefault;
|
||||
luaO_insertlist(&(L->roottable), (GCnode *)t);
|
||||
L->nblocks += gcsize(nhash);
|
||||
|
||||
/*
|
||||
** returns the `main' position of an element in a table (that is, the index
|
||||
** of its hash value)
|
||||
*/
|
||||
static Node *mainposition (const Table *t, const TValue *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TNUMBER:
|
||||
return hashnum(t, nvalue(key));
|
||||
case LUA_TSTRING:
|
||||
return hashstr(t, rawtsvalue(key));
|
||||
case LUA_TBOOLEAN:
|
||||
return hashboolean(t, bvalue(key));
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
return hashpointer(t, pvalue(key));
|
||||
default:
|
||||
return hashpointer(t, gcvalue(key));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns the index for `key' if `key' is an appropriate key to live in
|
||||
** the array part of the table, -1 otherwise.
|
||||
*/
|
||||
static int arrayindex (const TValue *key) {
|
||||
if (ttisnumber(key)) {
|
||||
lua_Number n = nvalue(key);
|
||||
int k;
|
||||
lua_number2int(k, n);
|
||||
if (luai_numeq(cast_num(k), n))
|
||||
return k;
|
||||
}
|
||||
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 of a traversal is signalled by -1.
|
||||
*/
|
||||
static int findindex (lua_State *L, Table *t, StkId key) {
|
||||
int i;
|
||||
if (ttisnil(key)) return -1; /* first iteration */
|
||||
i = arrayindex(key);
|
||||
if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
|
||||
return i-1; /* yes; that's the index (corrected to C) */
|
||||
else {
|
||||
Node *n = mainposition(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
/* key may be dead already, but it is ok to use it in `next' */
|
||||
if (luaO_rawequalObj(key2tval(n), key) ||
|
||||
(ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
|
||||
gcvalue(gkey(n)) == gcvalue(key))) {
|
||||
i = cast_int(n - gnode(t, 0)); /* key index in hash table */
|
||||
/* hash elements are numbered after array ones */
|
||||
return i + t->sizearray;
|
||||
}
|
||||
else n = gnext(n);
|
||||
} while (n);
|
||||
luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaH_next (lua_State *L, Table *t, StkId key) {
|
||||
int i = findindex(L, t, key); /* find original element */
|
||||
for (i++; i < t->sizearray; i++) { /* try first array part */
|
||||
if (!ttisnil(&t->array[i])) { /* a non-nil value? */
|
||||
setnvalue(key, cast_num(i+1));
|
||||
setobj2s(L, key+1, &t->array[i]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
|
||||
if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
|
||||
setobj2s(L, key, key2tval(gnode(t, i)));
|
||||
setobj2s(L, key+1, gval(gnode(t, i)));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0; /* no more elements */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {=============================================================
|
||||
** Rehash
|
||||
** ==============================================================
|
||||
*/
|
||||
|
||||
|
||||
static int computesizes (int nums[], int *narray) {
|
||||
int i;
|
||||
int twotoi; /* 2^i */
|
||||
int a = 0; /* number of elements smaller than 2^i */
|
||||
int na = 0; /* number of elements to go to array part */
|
||||
int n = 0; /* optimal size for array part */
|
||||
for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
|
||||
if (nums[i] > 0) {
|
||||
a += nums[i];
|
||||
if (a > twotoi/2) { /* more than half elements present? */
|
||||
n = twotoi; /* optimal size (till now) */
|
||||
na = a; /* all elements smaller than n will go to array part */
|
||||
}
|
||||
}
|
||||
if (a == *narray) break; /* all elements already counted */
|
||||
}
|
||||
*narray = n;
|
||||
lua_assert(*narray/2 <= na && na <= *narray);
|
||||
return na;
|
||||
}
|
||||
|
||||
|
||||
static int countint (const TValue *key, int *nums) {
|
||||
int k = arrayindex(key);
|
||||
if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
|
||||
nums[ceillog2(k)]++; /* count as such */
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int numusearray (const Table *t, int *nums) {
|
||||
int lg;
|
||||
int ttlg; /* 2^lg */
|
||||
int ause = 0; /* summation of `nums' */
|
||||
int i = 1; /* count to traverse all array keys */
|
||||
for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
|
||||
int lc = 0; /* counter */
|
||||
int lim = ttlg;
|
||||
if (lim > t->sizearray) {
|
||||
lim = t->sizearray; /* adjust upper limit */
|
||||
if (i > lim)
|
||||
break; /* no more elements to count */
|
||||
}
|
||||
/* count elements in range (2^(lg-1), 2^lg] */
|
||||
for (; i <= lim; i++) {
|
||||
if (!ttisnil(&t->array[i-1]))
|
||||
lc++;
|
||||
}
|
||||
nums[lg] += lc;
|
||||
ause += lc;
|
||||
}
|
||||
return ause;
|
||||
}
|
||||
|
||||
|
||||
static int numusehash (const Table *t, int *nums, int *pnasize) {
|
||||
int totaluse = 0; /* total number of elements */
|
||||
int ause = 0; /* summation of `nums' */
|
||||
int i = sizenode(t);
|
||||
while (i--) {
|
||||
Node *n = &t->node[i];
|
||||
if (!ttisnil(gval(n))) {
|
||||
ause += countint(key2tval(n), nums);
|
||||
totaluse++;
|
||||
}
|
||||
}
|
||||
*pnasize += ause;
|
||||
return totaluse;
|
||||
}
|
||||
|
||||
|
||||
static void setarrayvector (lua_State *L, Table *t, int size) {
|
||||
int i;
|
||||
luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
|
||||
for (i=t->sizearray; i<size; i++)
|
||||
setnilvalue(&t->array[i]);
|
||||
t->sizearray = size;
|
||||
}
|
||||
|
||||
|
||||
static void setnodevector (lua_State *L, Table *t, int size) {
|
||||
int lsize;
|
||||
if (size == 0) { /* no elements to hash part? */
|
||||
t->node = cast(Node *, dummynode); /* use common `dummynode' */
|
||||
lsize = 0;
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
lsize = ceillog2(size);
|
||||
if (lsize > MAXBITS)
|
||||
luaG_runerror(L, "table overflow");
|
||||
size = twoto(lsize);
|
||||
t->node = luaM_newvector(L, size, Node);
|
||||
for (i=0; i<size; i++) {
|
||||
Node *n = gnode(t, i);
|
||||
gnext(n) = NULL;
|
||||
setnilvalue(gkey(n));
|
||||
setnilvalue(gval(n));
|
||||
}
|
||||
}
|
||||
t->lsizenode = cast_byte(lsize);
|
||||
t->lastfree = gnode(t, size); /* all positions are free */
|
||||
}
|
||||
|
||||
|
||||
static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
||||
int i;
|
||||
int oldasize = t->sizearray;
|
||||
int oldhsize = t->lsizenode;
|
||||
Node *nold = t->node; /* save old hash ... */
|
||||
if (nasize > oldasize) /* array part must grow? */
|
||||
setarrayvector(L, t, nasize);
|
||||
/* create new hash part with appropriate size */
|
||||
setnodevector(L, t, nhsize);
|
||||
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(L, luaH_setnum(L, t, i+1), &t->array[i]);
|
||||
}
|
||||
/* shrink array */
|
||||
luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
|
||||
}
|
||||
/* re-insert elements from hash part */
|
||||
for (i = twoto(oldhsize) - 1; i >= 0; i--) {
|
||||
Node *old = nold+i;
|
||||
if (!ttisnil(gval(old)))
|
||||
setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
|
||||
}
|
||||
if (nold != dummynode)
|
||||
luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
|
||||
}
|
||||
|
||||
|
||||
void luaH_resizearray (lua_State *L, Table *t, int nasize) {
|
||||
int nsize = (t->node == dummynode) ? 0 : sizenode(t);
|
||||
resize(L, t, nasize, nsize);
|
||||
}
|
||||
|
||||
|
||||
static void rehash (lua_State *L, Table *t, const TValue *ek) {
|
||||
int nasize, na;
|
||||
int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
|
||||
int i;
|
||||
int totaluse;
|
||||
for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
|
||||
nasize = numusearray(t, nums); /* count keys in array part */
|
||||
totaluse = nasize; /* all those keys are integer keys */
|
||||
totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
|
||||
/* count extra key */
|
||||
nasize += countint(ek, nums);
|
||||
totaluse++;
|
||||
/* compute new size for array part */
|
||||
na = computesizes(nums, &nasize);
|
||||
/* resize the table to new computed sizes */
|
||||
resize(L, t, nasize, totaluse - na);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** }=============================================================
|
||||
*/
|
||||
|
||||
|
||||
Table *luaH_new (lua_State *L, int narray, int nhash) {
|
||||
Table *t = luaM_new(L, Table);
|
||||
luaC_link(L, obj2gco(t), LUA_TTABLE);
|
||||
t->metatable = NULL;
|
||||
t->flags = cast_byte(~0);
|
||||
/* temporary values (kept only if some malloc fails) */
|
||||
t->array = NULL;
|
||||
t->sizearray = 0;
|
||||
t->lsizenode = 0;
|
||||
t->node = cast(Node *, dummynode);
|
||||
setarrayvector(L, t, narray);
|
||||
setnodevector(L, t, nhash);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
static int newsize (Hash *t) {
|
||||
Node *v = t->node;
|
||||
int size = nhash(t);
|
||||
int realuse = 0;
|
||||
int i;
|
||||
for (i=0; i<size; i++) {
|
||||
if (ttype(val(v+i)) != LUA_T_NIL)
|
||||
realuse++;
|
||||
}
|
||||
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
||||
void luaH_free (lua_State *L, Table *t) {
|
||||
if (t->node != dummynode)
|
||||
luaM_freearray(L, t->node, sizenode(t), Node);
|
||||
luaM_freearray(L, t->array, t->sizearray, TValue);
|
||||
luaM_free(L, t);
|
||||
}
|
||||
|
||||
|
||||
static void rehash (Hash *t) {
|
||||
int nold = nhash(t);
|
||||
Node *vold = nodevector(t);
|
||||
int nnew = newsize(t);
|
||||
int i;
|
||||
nodevector(t) = hashnodecreate(nnew);
|
||||
nhash(t) = nnew;
|
||||
nuse(t) = 0;
|
||||
for (i=0; i<nold; i++) {
|
||||
Node *n = vold+i;
|
||||
if (ttype(val(n)) != LUA_T_NIL) {
|
||||
*luaH_present(t, ref(n)) = *n; /* copy old node to new hash */
|
||||
nuse(t)++;
|
||||
static Node *getfreepos (Table *t) {
|
||||
while (t->lastfree-- > t->node) {
|
||||
if (ttisnil(gkey(t->lastfree)))
|
||||
return t->lastfree;
|
||||
}
|
||||
return NULL; /* could not find a free place */
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** 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 TValue *newkey (lua_State *L, Table *t, const TValue *key) {
|
||||
Node *mp = mainposition(t, key);
|
||||
if (!ttisnil(gval(mp)) || mp == dummynode) {
|
||||
Node *othern;
|
||||
Node *n = getfreepos(t); /* get a free place */
|
||||
if (n == NULL) { /* cannot find a free place? */
|
||||
rehash(L, t, key); /* grow table */
|
||||
return luaH_set(L, t, key); /* re-insert key into grown table */
|
||||
}
|
||||
lua_assert(n != dummynode);
|
||||
othern = mainposition(t, key2tval(mp));
|
||||
if (othern != mp) { /* is colliding node out of its main position? */
|
||||
/* yes; move colliding node into free position */
|
||||
while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
|
||||
gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
|
||||
*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
|
||||
gnext(mp) = NULL; /* now `mp' is free */
|
||||
setnilvalue(gval(mp));
|
||||
}
|
||||
else { /* colliding node is in its own main position */
|
||||
/* new node will go into free position */
|
||||
gnext(n) = gnext(mp); /* chain new position */
|
||||
gnext(mp) = n;
|
||||
mp = n;
|
||||
}
|
||||
}
|
||||
L->nblocks += gcsize(nnew)-gcsize(nold);
|
||||
luaM_free(vold);
|
||||
gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
|
||||
luaC_barriert(L, t, key);
|
||||
lua_assert(ttisnil(gval(mp)));
|
||||
return gval(mp);
|
||||
}
|
||||
|
||||
|
||||
void luaH_set (Hash *t, TObject *ref, TObject *val) {
|
||||
Node *n = luaH_present(t, ref);
|
||||
if (ttype(ref(n)) != LUA_T_NIL)
|
||||
*val(n) = *val;
|
||||
/*
|
||||
** search function for integers
|
||||
*/
|
||||
const TValue *luaH_getnum (Table *t, int key) {
|
||||
/* (1 <= key && key <= t->sizearray) */
|
||||
if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
|
||||
return &t->array[key-1];
|
||||
else {
|
||||
TObject buff;
|
||||
buff = *val; /* rehash may invalidate this address */
|
||||
if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
|
||||
rehash(t);
|
||||
n = luaH_present(t, ref);
|
||||
}
|
||||
nuse(t)++;
|
||||
*ref(n) = *ref;
|
||||
*val(n) = buff;
|
||||
lua_Number nk = cast_num(key);
|
||||
Node *n = hashnum(t, nk);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
|
||||
return gval(n); /* that's it */
|
||||
else n = gnext(n);
|
||||
} while (n);
|
||||
return luaO_nilobject;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaH_pos (Hash *t, TObject *r) {
|
||||
Node *n = luaH_present(t, r);
|
||||
luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found");
|
||||
return n-(t->node);
|
||||
/*
|
||||
** search function for strings
|
||||
*/
|
||||
const TValue *luaH_getstr (Table *t, TString *key) {
|
||||
Node *n = hashstr(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
|
||||
return gval(n); /* that's it */
|
||||
else n = gnext(n);
|
||||
} while (n);
|
||||
return luaO_nilobject;
|
||||
}
|
||||
|
||||
|
||||
void luaH_setint (Hash *t, int ref, TObject *val) {
|
||||
TObject index;
|
||||
ttype(&index) = LUA_T_NUMBER;
|
||||
nvalue(&index) = ref;
|
||||
luaH_set(t, &index, val);
|
||||
/*
|
||||
** main search function
|
||||
*/
|
||||
const TValue *luaH_get (Table *t, const TValue *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TNIL: return luaO_nilobject;
|
||||
case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
|
||||
case LUA_TNUMBER: {
|
||||
int k;
|
||||
lua_Number n = nvalue(key);
|
||||
lua_number2int(k, n);
|
||||
if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
|
||||
return luaH_getnum(t, k); /* use specialized version */
|
||||
/* else go through */
|
||||
}
|
||||
default: {
|
||||
Node *n = mainposition(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_rawequalObj(key2tval(n), key))
|
||||
return gval(n); /* that's it */
|
||||
else n = gnext(n);
|
||||
} while (n);
|
||||
return luaO_nilobject;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_getint (Hash *t, int ref) {
|
||||
TObject index;
|
||||
ttype(&index) = LUA_T_NUMBER;
|
||||
nvalue(&index) = ref;
|
||||
return luaH_get(t, &index);
|
||||
TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
|
||||
const TValue *p = luaH_get(t, key);
|
||||
t->flags = 0;
|
||||
if (p != luaO_nilobject)
|
||||
return cast(TValue *, p);
|
||||
else {
|
||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||
else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
|
||||
luaG_runerror(L, "table index is NaN");
|
||||
return newkey(L, t, key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TValue *luaH_setnum (lua_State *L, Table *t, int key) {
|
||||
const TValue *p = luaH_getnum(t, key);
|
||||
if (p != luaO_nilobject)
|
||||
return cast(TValue *, p);
|
||||
else {
|
||||
TValue k;
|
||||
setnvalue(&k, cast_num(key));
|
||||
return newkey(L, t, &k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
|
||||
const TValue *p = luaH_getstr(t, key);
|
||||
if (p != luaO_nilobject)
|
||||
return cast(TValue *, p);
|
||||
else {
|
||||
TValue k;
|
||||
setsvalue(L, &k, key);
|
||||
return newkey(L, t, &k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int unbound_search (Table *t, unsigned int j) {
|
||||
unsigned int i = j; /* i is zero or a present index */
|
||||
j++;
|
||||
/* find `i' and `j' such that i is present and j is not */
|
||||
while (!ttisnil(luaH_getnum(t, j))) {
|
||||
i = j;
|
||||
j *= 2;
|
||||
if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
|
||||
/* table was built with bad purposes: resort to linear search */
|
||||
i = 1;
|
||||
while (!ttisnil(luaH_getnum(t, i))) i++;
|
||||
return i - 1;
|
||||
}
|
||||
}
|
||||
/* now do a binary search between them */
|
||||
while (j - i > 1) {
|
||||
unsigned int m = (i+j)/2;
|
||||
if (ttisnil(luaH_getnum(t, m))) j = m;
|
||||
else i = m;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Try to find a boundary in table `t'. A `boundary' is an integer index
|
||||
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
|
||||
*/
|
||||
int luaH_getn (Table *t) {
|
||||
unsigned int j = t->sizearray;
|
||||
if (j > 0 && ttisnil(&t->array[j - 1])) {
|
||||
/* there is a boundary in the array part: (binary) search for it */
|
||||
unsigned int i = 0;
|
||||
while (j - i > 1) {
|
||||
unsigned int m = (i+j)/2;
|
||||
if (ttisnil(&t->array[m - 1])) j = m;
|
||||
else i = m;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
/* else must find a boundary in hash part */
|
||||
else if (t->node == dummynode) /* hash part is empty? */
|
||||
return j; /* that is easy... */
|
||||
else return unbound_search(t, j);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if defined(LUA_DEBUG)
|
||||
|
||||
Node *luaH_mainposition (const Table *t, const TValue *key) {
|
||||
return mainposition(t, key);
|
||||
}
|
||||
|
||||
int luaH_isdummy (Node *n) { return n == dummynode; }
|
||||
|
||||
#endif
|
||||
|
||||
38
ltable.h
38
ltable.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 1.10 1999/01/25 17:40:10 roberto Exp roberto $
|
||||
** $Id: ltable.h,v 2.9 2006/01/10 12:51:53 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,21 +10,31 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define node(t,i) (&(t)->node[i])
|
||||
#define ref(n) (&(n)->ref)
|
||||
#define val(n) (&(n)->val)
|
||||
#define nhash(t) ((t)->nhash)
|
||||
#define gnode(t,i) (&(t)->node[i])
|
||||
#define gkey(n) (&(n)->i_key.nk)
|
||||
#define gval(n) (&(n)->i_val)
|
||||
#define gnext(n) ((n)->i_key.nk.next)
|
||||
|
||||
#define luaH_get(t,ref) (val(luaH_present((t), (ref))))
|
||||
#define luaH_move(t,from,to) (luaH_setint(t, to, luaH_getint(t, from)))
|
||||
#define key2tval(n) (&(n)->i_key.tvk)
|
||||
|
||||
Hash *luaH_new (int nhash);
|
||||
void luaH_free (Hash *frees);
|
||||
Node *luaH_present (Hash *t, TObject *key);
|
||||
void luaH_set (Hash *t, TObject *ref, TObject *val);
|
||||
int luaH_pos (Hash *t, TObject *r);
|
||||
void luaH_setint (Hash *t, int ref, TObject *val);
|
||||
TObject *luaH_getint (Hash *t, int ref);
|
||||
|
||||
LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
|
||||
LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
|
||||
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
|
||||
LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
|
||||
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
|
||||
LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
|
||||
LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash);
|
||||
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);
|
||||
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
|
||||
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
|
||||
LUAI_FUNC int luaH_getn (Table *t);
|
||||
|
||||
|
||||
#if defined(LUA_DEBUG)
|
||||
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
|
||||
LUAI_FUNC int luaH_isdummy (Node *n);
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
278
ltablib.c
Normal file
278
ltablib.c
Normal file
@@ -0,0 +1,278 @@
|
||||
/*
|
||||
** $Id: ltablib.c,v 1.37 2005/10/21 13:47:42 roberto Exp roberto $
|
||||
** Library for Table Manipulation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define ltablib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#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 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_pushinteger(L, i); /* 1st argument */
|
||||
lua_rawgeti(L, 1, i); /* 2nd argument */
|
||||
lua_call(L, 2, 1);
|
||||
if (!lua_isnil(L, -1))
|
||||
return 1;
|
||||
lua_pop(L, 1); /* remove nil result */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int foreach (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
lua_pushnil(L); /* first key */
|
||||
while (lua_next(L, 1)) {
|
||||
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 */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int maxn (lua_State *L) {
|
||||
lua_Number max = 0;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushnil(L); /* first key */
|
||||
while (lua_next(L, 1)) {
|
||||
lua_pop(L, 1); /* remove value */
|
||||
if (lua_type(L, -1) == LUA_TNUMBER) {
|
||||
lua_Number v = lua_tonumber(L, -1);
|
||||
if (v > max) max = v;
|
||||
}
|
||||
}
|
||||
lua_pushnumber(L, max);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int getn (lua_State *L) {
|
||||
lua_pushinteger(L, aux_getn(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int setn (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
#ifndef luaL_setn
|
||||
luaL_setn(L, 1, luaL_checkint(L, 2));
|
||||
#else
|
||||
luaL_error(L, LUA_QL("setn") " is obsolete");
|
||||
#endif
|
||||
lua_pushvalue(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int tinsert (lua_State *L) {
|
||||
int e = aux_getn(L, 1) + 1; /* first empty element */
|
||||
int pos; /* where to insert new element */
|
||||
switch (lua_gettop(L)) {
|
||||
case 2: { /* called with only 2 arguments */
|
||||
pos = e; /* insert new element at the end */
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
int i;
|
||||
pos = luaL_checkint(L, 2); /* 2nd argument is the position */
|
||||
if (pos > e) e = pos; /* `grow' array if necessary */
|
||||
for (i = e; i > pos; i--) { /* move up elements */
|
||||
lua_rawgeti(L, 1, i-1);
|
||||
lua_rawseti(L, 1, i); /* t[i] = t[i-1] */
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
|
||||
}
|
||||
}
|
||||
luaL_setn(L, 1, e); /* new size */
|
||||
lua_rawseti(L, 1, pos); /* t[pos] = v */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int tremove (lua_State *L) {
|
||||
int e = aux_getn(L, 1);
|
||||
int pos = luaL_optint(L, 2, e);
|
||||
if (e == 0) return 0; /* table is `empty' */
|
||||
luaL_setn(L, 1, e - 1); /* t.n = n-1 */
|
||||
lua_rawgeti(L, 1, pos); /* result = t[pos] */
|
||||
for ( ;pos<e; pos++) {
|
||||
lua_rawgeti(L, 1, pos+1);
|
||||
lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
|
||||
}
|
||||
lua_pushnil(L);
|
||||
lua_rawseti(L, 1, e); /* t[e] = nil */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int tconcat (lua_State *L) {
|
||||
luaL_Buffer b;
|
||||
size_t lsep;
|
||||
int i, last;
|
||||
const char *sep = luaL_optlstring(L, 2, "", &lsep);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
i = luaL_optint(L, 3, 1);
|
||||
last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
|
||||
luaL_buffinit(L, &b);
|
||||
for (; i <= last; i++) {
|
||||
lua_rawgeti(L, 1, i);
|
||||
luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings");
|
||||
luaL_addvalue(&b);
|
||||
if (i != last)
|
||||
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 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", tconcat},
|
||||
{"foreach", foreach},
|
||||
{"foreachi", foreachi},
|
||||
{"getn", getn},
|
||||
{"maxn", maxn},
|
||||
{"insert", tinsert},
|
||||
{"remove", tremove},
|
||||
{"setn", setn},
|
||||
{"sort", sort},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API int luaopen_table (lua_State *L) {
|
||||
luaL_register(L, LUA_TABLIBNAME, tab_funcs);
|
||||
return 1;
|
||||
}
|
||||
|
||||
86
ltests.h
Normal file
86
ltests.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
** $Id: ltests.h,v 2.17 2005/12/27 17:12:00 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
|
||||
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
#define lua_assert(c) assert(c)
|
||||
|
||||
|
||||
/* to avoid warnings, and to make sure value is really unused */
|
||||
#define UNUSED(x) (x=0, (void)(x))
|
||||
|
||||
|
||||
/* memory allocator control variables */
|
||||
typedef struct Memcontrol {
|
||||
unsigned long numblocks;
|
||||
unsigned long total;
|
||||
unsigned long maxmem;
|
||||
unsigned long memlimit;
|
||||
} Memcontrol;
|
||||
|
||||
LUAI_DATA Memcontrol memcontrol;
|
||||
|
||||
|
||||
/*
|
||||
** generic variable for debug tricks
|
||||
*/
|
||||
LUAI_DATA int Trick;
|
||||
|
||||
|
||||
void *debug_realloc (void *ud, void *block, size_t osize, size_t nsize);
|
||||
|
||||
#ifdef lua_c
|
||||
#define luaL_newstate() lua_newstate(debug_realloc, &memcontrol)
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct CallInfo *pCallInfo;
|
||||
|
||||
int lua_checkmemory (lua_State *L);
|
||||
int lua_checkpc (lua_State *L, pCallInfo ci);
|
||||
|
||||
|
||||
/* test for lock/unlock */
|
||||
#undef luai_userstateopen
|
||||
#undef luai_userstatethread
|
||||
#undef lua_lock
|
||||
#undef lua_unlock
|
||||
#undef LUAI_EXTRASPACE
|
||||
|
||||
struct L_EXTRA { int lock; int *plock; };
|
||||
#define LUAI_EXTRASPACE sizeof(struct L_EXTRA)
|
||||
#define getlock(l) (cast(struct L_EXTRA *, l) - 1)
|
||||
#define luai_userstateopen(l) \
|
||||
(getlock(l)->lock = 0, getlock(l)->plock = &(getlock(l)->lock))
|
||||
#define luai_userstatethread(l,l1) (getlock(l1)->plock = getlock(l)->plock)
|
||||
#define lua_lock(l) lua_assert((*getlock(l)->plock)++ == 0)
|
||||
#define lua_unlock(l) lua_assert(--(*getlock(l)->plock) == 0)
|
||||
|
||||
|
||||
int luaB_opentests (lua_State *L);
|
||||
|
||||
#ifdef lua_c
|
||||
#define luaL_openlibs(L) { (luaL_openlibs)(L); luaB_opentests(L); }
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* change some sizes to give some bugs a chance */
|
||||
|
||||
#undef LUAL_BUFFERSIZE
|
||||
#define LUAL_BUFFERSIZE 27
|
||||
#define MINSTRTABSIZE 2
|
||||
|
||||
#endif
|
||||
271
ltm.c
271
ltm.c
@@ -1,248 +1,75 @@
|
||||
/*
|
||||
** $Id: ltm.c,v 1.24 1999/02/26 15:48:55 roberto Exp roberto $
|
||||
** $Id: ltm.c,v 2.7 2005/12/22 16:19:56 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#define ltm_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
char *luaT_eventname[] = { /* ORDER IM */
|
||||
"gettable", "settable", "index", "getglobal", "setglobal", "add",
|
||||
"sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
|
||||
"concat", "gc", "function", NULL
|
||||
|
||||
const char *const luaT_typenames[] = {
|
||||
"nil", "boolean", "userdata", "number",
|
||||
"string", "table", "function", "userdata", "thread",
|
||||
"proto", "upval"
|
||||
};
|
||||
|
||||
|
||||
static int luaI_checkevent (char *name, char *list[]) {
|
||||
int e = luaL_findstring(name, list);
|
||||
if (e < 0)
|
||||
luaL_verror("`%.50s' is not a valid event name", name);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* events in LUA_T_NIL are all allowed, since this is used as a
|
||||
* 'placeholder' for "default" fallbacks
|
||||
*/
|
||||
static char luaT_validevents[NUM_TAGS][IM_N] = { /* ORDER LUA_T, ORDER IM */
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_PROTO */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CPROTO */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
|
||||
};
|
||||
|
||||
static int luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
||||
return (t < LUA_T_NIL) ? 1 : luaT_validevents[-t][e];
|
||||
}
|
||||
|
||||
|
||||
static void init_entry (int tag) {
|
||||
void luaT_init (lua_State *L) {
|
||||
static const char *const luaT_eventname[] = { /* ORDER TM */
|
||||
"__index", "__newindex",
|
||||
"__gc", "__mode", "__eq",
|
||||
"__add", "__sub", "__mul", "__div", "__mod",
|
||||
"__pow", "__unm", "__len", "__lt", "__le",
|
||||
"__concat", "__call"
|
||||
};
|
||||
int i;
|
||||
for (i=0; i<IM_N; i++)
|
||||
ttype(luaT_getim(tag, i)) = LUA_T_NIL;
|
||||
}
|
||||
|
||||
|
||||
void luaT_init (void) {
|
||||
int t;
|
||||
L->last_tag = -(NUM_TAGS-1);
|
||||
luaM_growvector(L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
|
||||
for (t=L->last_tag; t<=0; t++)
|
||||
init_entry(t);
|
||||
}
|
||||
|
||||
|
||||
int lua_newtag (void) {
|
||||
--L->last_tag;
|
||||
luaM_growvector(L->IMtable, -(L->last_tag), 1, struct IM, arrEM, MAX_INT);
|
||||
init_entry(L->last_tag);
|
||||
return L->last_tag;
|
||||
}
|
||||
|
||||
|
||||
static void checktag (int tag) {
|
||||
if (!(L->last_tag <= tag && tag <= 0))
|
||||
luaL_verror("%d is not a valid tag", tag);
|
||||
}
|
||||
|
||||
void luaT_realtag (int tag) {
|
||||
if (!(L->last_tag <= tag && tag < LUA_T_NIL))
|
||||
luaL_verror("tag %d was not created by `newtag'", tag);
|
||||
}
|
||||
|
||||
|
||||
int lua_copytagmethods (int tagto, int tagfrom) {
|
||||
int e;
|
||||
checktag(tagto);
|
||||
checktag(tagfrom);
|
||||
for (e=0; e<IM_N; e++) {
|
||||
if (luaT_validevent(tagto, e))
|
||||
*luaT_getim(tagto, e) = *luaT_getim(tagfrom, e);
|
||||
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 */
|
||||
}
|
||||
return tagto;
|
||||
}
|
||||
|
||||
|
||||
int luaT_effectivetag (TObject *o) {
|
||||
int t;
|
||||
switch (t = ttype(o)) {
|
||||
case LUA_T_ARRAY:
|
||||
return o->value.a->htag;
|
||||
case LUA_T_USERDATA: {
|
||||
int tag = o->value.ts->u.d.tag;
|
||||
return (tag >= 0) ? LUA_T_USERDATA : tag;
|
||||
}
|
||||
case LUA_T_CLOSURE:
|
||||
return o->value.cl->consts[0].ttype;
|
||||
#ifdef DEBUG
|
||||
case LUA_T_PMARK: case LUA_T_CMARK:
|
||||
case LUA_T_CLMARK: case LUA_T_LINE:
|
||||
LUA_INTERNALERROR("invalid type");
|
||||
#endif
|
||||
default:
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject *luaT_gettagmethod (int t, char *event) {
|
||||
int e = luaI_checkevent(event, luaT_eventname);
|
||||
checktag(t);
|
||||
if (luaT_validevent(t, e))
|
||||
return luaT_getim(t,e);
|
||||
else
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
|
||||
|
||||
void luaT_settagmethod (int t, char *event, TObject *func) {
|
||||
TObject temp;
|
||||
int e = luaI_checkevent(event, luaT_eventname);
|
||||
checktag(t);
|
||||
if (!luaT_validevent(t, e))
|
||||
luaL_verror("cannot change tag method `%.20s' for type `%.20s'%.20s",
|
||||
luaT_eventname[e], luaO_typenames[-t],
|
||||
(t == LUA_T_ARRAY || t == LUA_T_USERDATA) ? " with default tag"
|
||||
: "");
|
||||
temp = *func;
|
||||
*func = *luaT_getim(t,e);
|
||||
*luaT_getim(t, e) = temp;
|
||||
}
|
||||
|
||||
|
||||
char *luaT_travtagmethods (int (*fn)(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(luaT_getim(t,e)))
|
||||
return luaT_eventname[e];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ===================================================================
|
||||
* compatibility with old fallback system
|
||||
** function to be used with macro "fasttm": optimized for absence of
|
||||
** tag methods
|
||||
*/
|
||||
#ifdef LUA_COMPAT2_5
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lstring.h"
|
||||
|
||||
static void errorFB (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isstring(o))
|
||||
fprintf(stderr, "lua: %s\n", lua_getstring(o));
|
||||
else
|
||||
fprintf(stderr, "lua: unknown error\n");
|
||||
}
|
||||
|
||||
|
||||
static void nilFB (void) { }
|
||||
|
||||
|
||||
static void typeFB (void) {
|
||||
lua_error("unexpected type");
|
||||
}
|
||||
|
||||
|
||||
static void fillvalids (IMS e, TObject *func) {
|
||||
int t;
|
||||
for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
|
||||
if (luaT_validevent(t, e))
|
||||
*luaT_getim(t, e) = *func;
|
||||
}
|
||||
|
||||
|
||||
void luaT_setfallback (void) {
|
||||
static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
|
||||
TObject oldfunc;
|
||||
lua_CFunction replace;
|
||||
char *name = luaL_check_string(1);
|
||||
lua_Object func = lua_getparam(2);
|
||||
luaL_arg_check(lua_isfunction(func), 2, "function expected");
|
||||
switch (luaL_findstring(name, oldnames)) {
|
||||
case 0: { /* old error fallback */
|
||||
TObject *em = &(luaS_new("_ERRORMESSAGE")->u.s.globalval);
|
||||
oldfunc = *em;
|
||||
*em = *luaA_Address(func);
|
||||
replace = errorFB;
|
||||
break;
|
||||
}
|
||||
case 1: /* old getglobal fallback */
|
||||
oldfunc = *luaT_getim(LUA_T_NIL, IM_GETGLOBAL);
|
||||
*luaT_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaA_Address(func);
|
||||
replace = nilFB;
|
||||
break;
|
||||
case 2: { /* old arith fallback */
|
||||
int i;
|
||||
oldfunc = *luaT_getim(LUA_T_NUMBER, IM_POW);
|
||||
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
|
||||
fillvalids(i, luaA_Address(func));
|
||||
replace = typeFB;
|
||||
break;
|
||||
}
|
||||
case 3: { /* old order fallback */
|
||||
int i;
|
||||
oldfunc = *luaT_getim(LUA_T_NIL, IM_LT);
|
||||
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
|
||||
fillvalids(i, luaA_Address(func));
|
||||
replace = typeFB;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
int e;
|
||||
if ((e = luaL_findstring(name, luaT_eventname)) >= 0) {
|
||||
oldfunc = *luaT_getim(LUA_T_NIL, e);
|
||||
fillvalids(e, luaA_Address(func));
|
||||
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
|
||||
}
|
||||
else {
|
||||
luaL_verror("`%.50s' is not a valid fallback name", name);
|
||||
replace = NULL; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
||||
const TValue *tm = luaH_getstr(events, ename);
|
||||
lua_assert(event <= TM_EQ);
|
||||
if (ttisnil(tm)) { /* no tag method? */
|
||||
events->flags |= cast_byte(1u<<event); /* cache this fact */
|
||||
return NULL;
|
||||
}
|
||||
if (oldfunc.ttype != LUA_T_NIL)
|
||||
luaA_pushobject(&oldfunc);
|
||||
else
|
||||
lua_pushcfunction(replace);
|
||||
else return tm;
|
||||
}
|
||||
|
||||
|
||||
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
||||
Table *mt;
|
||||
switch (ttype(o)) {
|
||||
case LUA_TTABLE:
|
||||
mt = hvalue(o)->metatable;
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
mt = uvalue(o)->metatable;
|
||||
break;
|
||||
default:
|
||||
mt = G(L)->mt[ttype(o)];
|
||||
}
|
||||
return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
74
ltm.h
74
ltm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltm.h,v 1.4 1997/11/26 18:53:45 roberto Exp roberto $
|
||||
** $Id: ltm.h,v 2.5 2005/05/20 15:53:42 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,54 +9,46 @@
|
||||
|
||||
|
||||
#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_LE,
|
||||
IM_GT,
|
||||
IM_GE,
|
||||
IM_CONCAT,
|
||||
IM_GC,
|
||||
IM_FUNCTION
|
||||
} IMS;
|
||||
|
||||
#define IM_N 18
|
||||
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_MOD,
|
||||
TM_POW,
|
||||
TM_UNM,
|
||||
TM_LEN,
|
||||
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 gfasttm(g,et,e) ((et) == NULL ? NULL : \
|
||||
((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
|
||||
|
||||
#define fasttm(l,et,e) gfasttm(G(l), et, e)
|
||||
|
||||
LUAI_DATA const char *const luaT_typenames[];
|
||||
|
||||
|
||||
#define luaT_getim(tag,event) (&L->IMtable[-(tag)].int_method[event])
|
||||
#define luaT_getimbyObj(o,e) (luaT_getim(luaT_effectivetag(o),(e)))
|
||||
|
||||
extern char *luaT_eventname[];
|
||||
|
||||
|
||||
void luaT_init (void);
|
||||
void luaT_realtag (int tag);
|
||||
int luaT_effectivetag (TObject *o);
|
||||
void luaT_settagmethod (int t, char *event, TObject *func);
|
||||
TObject *luaT_gettagmethod (int t, char *event);
|
||||
char *luaT_travtagmethods (int (*fn)(TObject *));
|
||||
|
||||
void luaT_setfallback (void); /* only if LUA_COMPAT2_5 */
|
||||
LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
|
||||
LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
|
||||
TMS event);
|
||||
LUAI_FUNC void luaT_init (lua_State *L);
|
||||
|
||||
#endif
|
||||
|
||||
484
lua.c
484
lua.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.20 1999/06/24 19:42:02 roberto Exp roberto $
|
||||
** $Id: lua.c,v 1.159 2006/05/24 14:16:39 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,179 +10,381 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lua_c
|
||||
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#ifdef _POSIX_SOURCE
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#define isatty(x) (x==0) /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
static lua_State *globalL = NULL;
|
||||
|
||||
static const char *progname = LUA_PROGNAME;
|
||||
|
||||
|
||||
typedef void (*handler)(int); /* type for signal actions */
|
||||
|
||||
static void laction (int i);
|
||||
|
||||
|
||||
static lua_LHFunction old_linehook = NULL;
|
||||
static lua_CHFunction old_callhook = NULL;
|
||||
|
||||
|
||||
static handler lreset (void) {
|
||||
return signal(SIGINT, laction);
|
||||
}
|
||||
|
||||
|
||||
static void lstop (void) {
|
||||
lua_setlinehook(old_linehook);
|
||||
lua_setcallhook(old_callhook);
|
||||
lreset();
|
||||
lua_error("interrupted!");
|
||||
static void lstop (lua_State *L, lua_Debug *ar) {
|
||||
(void)ar; /* unused arg. */
|
||||
lua_sethook(L, NULL, 0, 0);
|
||||
luaL_error(L, "interrupted!");
|
||||
}
|
||||
|
||||
|
||||
static void laction (int i) {
|
||||
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_LHFunction)lstop);
|
||||
old_callhook = lua_setcallhook((lua_CHFunction)lstop);
|
||||
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
||||
}
|
||||
|
||||
|
||||
static int ldo (int (*f)(char *), char *name) {
|
||||
int res;
|
||||
handler h = lreset();
|
||||
res = f(name); /* dostring | dofile */
|
||||
signal(SIGINT, h); /* restore old action */
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static void print_message (void) {
|
||||
static void print_usage (void) {
|
||||
fprintf(stderr,
|
||||
"Lua: command line options:\n"
|
||||
" -v print version information\n"
|
||||
" -d turn debug on\n"
|
||||
" -e stat dostring `stat'\n"
|
||||
" -q interactive mode without prompt\n"
|
||||
" -i interactive mode with prompt\n"
|
||||
" - executes stdin as a file\n"
|
||||
" a=b sets global `a' with string `b'\n"
|
||||
" name dofile `name'\n\n");
|
||||
"usage: %s [options] [script [args]].\n"
|
||||
"Available options are:\n"
|
||||
" -e stat execute string " LUA_QL("stat") "\n"
|
||||
" -l name require library " LUA_QL("name") "\n"
|
||||
" -i enter interactive mode after executing " LUA_QL("script") "\n"
|
||||
" -v show version information\n"
|
||||
" -- stop handling options\n"
|
||||
" - execute stdin and stop handling options\n"
|
||||
,
|
||||
progname);
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
|
||||
static void assign (char *arg) {
|
||||
if (strlen(arg) >= 500)
|
||||
fprintf(stderr, "lua: shell argument too long");
|
||||
else {
|
||||
char buffer[500];
|
||||
char *eq = strchr(arg, '=');
|
||||
lua_pushstring(eq+1);
|
||||
strncpy(buffer, arg, eq-arg);
|
||||
buffer[eq-arg] = 0;
|
||||
lua_setglobal(buffer);
|
||||
static void l_message (const char *pname, const char *msg) {
|
||||
if (pname) fprintf(stderr, "%s: ", pname);
|
||||
fprintf(stderr, "%s\n", msg);
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
|
||||
static int report (lua_State *L, int status) {
|
||||
if (status && !lua_isnil(L, -1)) {
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL) msg = "(error object is not a string)";
|
||||
l_message(progname, msg);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static void manual_input (int prompt) {
|
||||
int cont = 1;
|
||||
while (cont) {
|
||||
char buffer[BUFSIZ];
|
||||
int i = 0;
|
||||
lua_beginblock();
|
||||
if (prompt)
|
||||
printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
|
||||
for(;;) {
|
||||
int c = getchar();
|
||||
if (c == EOF) {
|
||||
cont = 0;
|
||||
break;
|
||||
}
|
||||
else if (c == '\n') {
|
||||
if (i>0 && buffer[i-1] == '\\')
|
||||
buffer[i-1] = '\n';
|
||||
else break;
|
||||
}
|
||||
else if (i >= BUFSIZ-1) {
|
||||
fprintf(stderr, "lua: argument line too long\n");
|
||||
break;
|
||||
}
|
||||
else buffer[i++] = (char)c;
|
||||
}
|
||||
buffer[i] = '\0';
|
||||
ldo(lua_dostring, buffer);
|
||||
lua_endblock();
|
||||
static int traceback (lua_State *L) {
|
||||
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
|
||||
if (!lua_istable(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
printf("\n");
|
||||
lua_getfield(L, -1, "traceback");
|
||||
if (!lua_isfunction(L, -1)) {
|
||||
lua_pop(L, 2);
|
||||
return 1;
|
||||
}
|
||||
lua_pushvalue(L, 1); /* pass error message */
|
||||
lua_pushinteger(L, 2); /* skip this function and traceback */
|
||||
lua_call(L, 2, 1); /* call debug.traceback */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
static int docall (lua_State *L, int narg, int clear) {
|
||||
int status;
|
||||
int base = lua_gettop(L) - narg; /* function index */
|
||||
lua_pushcfunction(L, traceback); /* push 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 */
|
||||
/* force a complete garbage collection in case of errors */
|
||||
if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static void print_version (void) {
|
||||
l_message(NULL, LUA_RELEASE " " LUA_COPYRIGHT);
|
||||
}
|
||||
|
||||
|
||||
static int getargs (lua_State *L, char **argv, int n) {
|
||||
int narg;
|
||||
int i;
|
||||
lua_open();
|
||||
lua_pushstring("> "); lua_setglobal("_PROMPT");
|
||||
lua_userinit();
|
||||
if (argc < 2) { /* no arguments? */
|
||||
if (isatty(0)) {
|
||||
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
|
||||
manual_input(1);
|
||||
}
|
||||
else
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
int argc = 0;
|
||||
while (argv[argc]) argc++; /* count total number of arguments */
|
||||
narg = argc - (n + 1); /* number of arguments to the script */
|
||||
luaL_checkstack(L, narg + 3, "too many arguments to script");
|
||||
for (i=n+1; i < argc; i++)
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_createtable(L, narg, n + 1);
|
||||
for (i=0; i < argc; i++) {
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_rawseti(L, -2, i - n);
|
||||
}
|
||||
else for (i=1; 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(1);
|
||||
break;
|
||||
case 'q':
|
||||
manual_input(0);
|
||||
break;
|
||||
case 'd':
|
||||
lua_setdebug(1);
|
||||
break;
|
||||
case 'v':
|
||||
printf("%s %s\n(written by %s)\n\n",
|
||||
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
|
||||
break;
|
||||
case 'e':
|
||||
return narg;
|
||||
}
|
||||
|
||||
|
||||
static int dofile (lua_State *L, const char *name) {
|
||||
int status = luaL_loadfile(L, name) || docall(L, 0, 1);
|
||||
return report(L, status);
|
||||
}
|
||||
|
||||
|
||||
static int dostring (lua_State *L, const char *s, const char *name) {
|
||||
int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
|
||||
return report(L, status);
|
||||
}
|
||||
|
||||
|
||||
static int dolibrary (lua_State *L, const char *name) {
|
||||
lua_getglobal(L, "require");
|
||||
lua_pushstring(L, name);
|
||||
return report(L, lua_pcall(L, 1, 0, 0));
|
||||
}
|
||||
|
||||
|
||||
static const char *get_prompt (lua_State *L, int firstline) {
|
||||
const char *p;
|
||||
lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
|
||||
p = lua_tostring(L, -1);
|
||||
if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
|
||||
lua_pop(L, 1); /* remove global */
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static int incomplete (lua_State *L, int status) {
|
||||
if (status == LUA_ERRSYNTAX) {
|
||||
size_t lmsg;
|
||||
const char *msg = lua_tolstring(L, -1, &lmsg);
|
||||
const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
|
||||
if (strstr(msg, LUA_QL("<eof>")) == tp) {
|
||||
lua_pop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0; /* else... */
|
||||
}
|
||||
|
||||
|
||||
static int pushline (lua_State *L, int firstline) {
|
||||
char buffer[LUA_MAXINPUT];
|
||||
char *b = buffer;
|
||||
size_t l;
|
||||
const char *prmt = get_prompt(L, firstline);
|
||||
if (lua_readline(L, b, prmt) == 0)
|
||||
return 0; /* no input */
|
||||
l = strlen(b);
|
||||
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
|
||||
b[l-1] = '\0'; /* remove it */
|
||||
if (firstline && b[0] == '=') /* first line starts with `=' ? */
|
||||
lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
|
||||
else
|
||||
lua_pushstring(L, b);
|
||||
lua_freeline(L, b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int loadline (lua_State *L) {
|
||||
int status;
|
||||
lua_settop(L, 0);
|
||||
if (!pushline(L, 1))
|
||||
return -1; /* no input */
|
||||
for (;;) { /* repeat until gets a complete line */
|
||||
status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
|
||||
if (!incomplete(L, status)) break; /* cannot try to add lines? */
|
||||
if (!pushline(L, 0)) /* no more input? */
|
||||
return -1;
|
||||
lua_pushliteral(L, "\n"); /* add a new line... */
|
||||
lua_insert(L, -2); /* ...between the two lines */
|
||||
lua_concat(L, 3); /* join them */
|
||||
}
|
||||
lua_saveline(L, 1);
|
||||
lua_remove(L, 1); /* remove line */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static void dotty (lua_State *L) {
|
||||
int status;
|
||||
const char *oldprogname = progname;
|
||||
progname = NULL;
|
||||
while ((status = loadline(L)) != -1) {
|
||||
if (status == 0) status = docall(L, 0, 0);
|
||||
report(L, 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 " LUA_QL("print") " (%s)",
|
||||
lua_tostring(L, -1)));
|
||||
}
|
||||
}
|
||||
lua_settop(L, 0); /* clear stack */
|
||||
fputs("\n", stdout);
|
||||
fflush(stdout);
|
||||
progname = oldprogname;
|
||||
}
|
||||
|
||||
|
||||
static int handle_script (lua_State *L, char **argv, int n) {
|
||||
int status;
|
||||
const char *fname;
|
||||
int narg = getargs(L, argv, n); /* collect arguments */
|
||||
lua_setglobal(L, "arg");
|
||||
fname = argv[n];
|
||||
if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
|
||||
fname = NULL; /* stdin */
|
||||
status = luaL_loadfile(L, fname);
|
||||
lua_insert(L, -(narg+1));
|
||||
if (status == 0)
|
||||
status = docall(L, narg, 0);
|
||||
else
|
||||
lua_pop(L, narg);
|
||||
return report(L, status);
|
||||
}
|
||||
|
||||
|
||||
/* check that argument has no extra characters at the end */
|
||||
#define notail(x) {if ((x)[2] != '\0') return -1;}
|
||||
|
||||
|
||||
static int collectargs (char **argv, int *pi, int *pv, int *pe) {
|
||||
int i;
|
||||
for (i = 1; argv[i] != NULL; i++) {
|
||||
if (argv[i][0] != '-') /* not an option? */
|
||||
return i;
|
||||
switch (argv[i][1]) { /* option */
|
||||
case '-':
|
||||
notail(argv[i]);
|
||||
return (argv[i+1] != NULL ? i+1 : 0);
|
||||
case '\0':
|
||||
return i;
|
||||
case 'i':
|
||||
notail(argv[i]);
|
||||
*pi = 1; /* go through */
|
||||
case 'v':
|
||||
notail(argv[i]);
|
||||
*pv = 1;
|
||||
break;
|
||||
case 'e':
|
||||
*pe = 1; /* go through */
|
||||
case 'l':
|
||||
if (argv[i][2] == '\0') {
|
||||
i++;
|
||||
if (ldo(lua_dostring, argv[i]) != 0) {
|
||||
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
print_message();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else if (strchr(argv[i], '='))
|
||||
assign(argv[i]);
|
||||
else {
|
||||
int result = ldo(lua_dofile, argv[i]);
|
||||
if (result) {
|
||||
if (result == 2) {
|
||||
fprintf(stderr, "lua: cannot execute file ");
|
||||
perror(argv[i]);
|
||||
if (argv[i] == NULL) return -1;
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
default: return -1; /* invalid option */
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG
|
||||
lua_close();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int runargs (lua_State *L, char **argv, int n) {
|
||||
int i;
|
||||
for (i = 1; i < n; i++) {
|
||||
if (argv[i] == NULL) continue;
|
||||
lua_assert(argv[i][0] == '-');
|
||||
switch (argv[i][1]) { /* option */
|
||||
case 'e': {
|
||||
const char *chunk = argv[i] + 2;
|
||||
if (*chunk == '\0') chunk = argv[++i];
|
||||
lua_assert(chunk != NULL);
|
||||
if (dostring(L, chunk, "=(command line)") != 0)
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
const char *filename = argv[i] + 2;
|
||||
if (*filename == '\0') filename = argv[++i];
|
||||
lua_assert(filename != NULL);
|
||||
if (dolibrary(L, filename))
|
||||
return 1; /* stop if file fails */
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int handle_luainit (lua_State *L) {
|
||||
const char *init = getenv(LUA_INIT);
|
||||
if (init == NULL) return 0; /* status OK */
|
||||
else if (init[0] == '@')
|
||||
return dofile(L, init+1);
|
||||
else
|
||||
return dostring(L, 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);
|
||||
char **argv = s->argv;
|
||||
int script;
|
||||
int has_i = 0, has_v = 0, has_e = 0;
|
||||
globalL = L;
|
||||
if (argv[0] && argv[0][0]) progname = argv[0];
|
||||
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
|
||||
luaL_openlibs(L); /* open libraries */
|
||||
lua_gc(L, LUA_GCRESTART, 0);
|
||||
s->status = handle_luainit(L);
|
||||
if (s->status != 0) return 0;
|
||||
script = collectargs(argv, &has_i, &has_v, &has_e);
|
||||
if (script < 0) { /* invalid args? */
|
||||
print_usage();
|
||||
s->status = 1;
|
||||
return 0;
|
||||
}
|
||||
if (has_v) print_version();
|
||||
s->status = runargs(L, argv, (script > 0) ? script : s->argc);
|
||||
if (s->status != 0) return 0;
|
||||
if (script)
|
||||
s->status = handle_script(L, argv, script);
|
||||
if (s->status != 0) return 0;
|
||||
if (has_i)
|
||||
dotty(L);
|
||||
else if (script == 0 && !has_e && !has_v) {
|
||||
if (lua_stdin_is_tty()) {
|
||||
print_version();
|
||||
dotty(L);
|
||||
}
|
||||
else dofile(L, NULL); /* executes stdin as a file */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
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;
|
||||
}
|
||||
s.argc = argc;
|
||||
s.argv = argv;
|
||||
status = lua_cpcall(L, &pmain, &s);
|
||||
report(L, status);
|
||||
lua_close(L);
|
||||
return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
482
lua.h
482
lua.h
@@ -1,9 +1,7 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.31 1999/04/15 12:33:19 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.217 2006/05/31 16:50:40 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/
|
||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||
** See Copyright Notice at the end of this file
|
||||
*/
|
||||
|
||||
@@ -11,183 +9,377 @@
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
#define LUA_VERSION "Lua 3.2"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-1999 TeCGraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#define LUA_NOOBJECT 0
|
||||
#include "luaconf.h"
|
||||
|
||||
|
||||
#define LUA_VERSION "Lua 5.1"
|
||||
#define LUA_RELEASE "Lua 5.1.1"
|
||||
#define LUA_VERSION_NUM 501
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2006 Lua.org, PUC-Rio"
|
||||
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
|
||||
|
||||
|
||||
/* mark for precompiled code (`<esc>Lua') */
|
||||
#define LUA_SIGNATURE "\033Lua"
|
||||
|
||||
/* option for multiple returns in `lua_pcall' and `lua_call' */
|
||||
#define LUA_MULTRET (-1)
|
||||
|
||||
|
||||
/*
|
||||
** pseudo-indices
|
||||
*/
|
||||
#define LUA_REGISTRYINDEX (-10000)
|
||||
#define LUA_ENVIRONINDEX (-10001)
|
||||
#define LUA_GLOBALSINDEX (-10002)
|
||||
#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
|
||||
|
||||
|
||||
/* thread status; 0 is OK */
|
||||
#define LUA_YIELD 1
|
||||
#define LUA_ERRRUN 2
|
||||
#define LUA_ERRSYNTAX 3
|
||||
#define LUA_ERRMEM 4
|
||||
#define LUA_ERRERR 5
|
||||
|
||||
#define LUA_ANYTAG (-1)
|
||||
|
||||
typedef struct lua_State lua_State;
|
||||
extern lua_State *lua_state;
|
||||
|
||||
typedef void (*lua_CFunction) (void);
|
||||
typedef unsigned int lua_Object;
|
||||
|
||||
void lua_open (void);
|
||||
void lua_close (void);
|
||||
lua_State *lua_setstate (lua_State *st);
|
||||
|
||||
lua_Object lua_settagmethod (int tag, char *event); /* In: new method */
|
||||
lua_Object lua_gettagmethod (int tag, char *event);
|
||||
|
||||
int lua_newtag (void);
|
||||
int lua_copytagmethods (int tagto, int tagfrom);
|
||||
void lua_settag (int tag); /* In: object */
|
||||
|
||||
void lua_error (char *s);
|
||||
int lua_dofile (char *filename); /* Out: returns */
|
||||
int lua_dostring (char *string); /* Out: returns */
|
||||
int lua_dobuffer (char *buff, int size, char *name);
|
||||
/* Out: returns */
|
||||
int lua_callfunction (lua_Object f);
|
||||
/* In: parameters; Out: returns */
|
||||
|
||||
void lua_beginblock (void);
|
||||
void lua_endblock (void);
|
||||
|
||||
lua_Object lua_lua2C (int number);
|
||||
#define lua_getparam(_) lua_lua2C(_)
|
||||
#define lua_getresult(_) lua_lua2C(_)
|
||||
|
||||
int lua_isnil (lua_Object object);
|
||||
int lua_istable (lua_Object object);
|
||||
int lua_isuserdata (lua_Object object);
|
||||
int lua_iscfunction (lua_Object object);
|
||||
int lua_isnumber (lua_Object object);
|
||||
int lua_isstring (lua_Object object);
|
||||
int lua_isfunction (lua_Object object);
|
||||
|
||||
double lua_getnumber (lua_Object object);
|
||||
char *lua_getstring (lua_Object object);
|
||||
long lua_strlen (lua_Object object);
|
||||
lua_CFunction lua_getcfunction (lua_Object object);
|
||||
void *lua_getuserdata (lua_Object object);
|
||||
typedef int (*lua_CFunction) (lua_State *L);
|
||||
|
||||
|
||||
void lua_pushnil (void);
|
||||
void lua_pushnumber (double n);
|
||||
void lua_pushlstring (char *s, long len);
|
||||
void lua_pushstring (char *s);
|
||||
void lua_pushcclosure (lua_CFunction fn, int n);
|
||||
void lua_pushusertag (void *u, int tag);
|
||||
void lua_pushobject (lua_Object object);
|
||||
/*
|
||||
** functions that read/write blocks when loading/dumping Lua chunks
|
||||
*/
|
||||
typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
|
||||
|
||||
lua_Object lua_pop (void);
|
||||
|
||||
lua_Object lua_getglobal (char *name);
|
||||
lua_Object lua_rawgetglobal (char *name);
|
||||
void lua_setglobal (char *name); /* In: value */
|
||||
void lua_rawsetglobal (char *name); /* In: value */
|
||||
|
||||
void lua_settable (void); /* In: table, index, value */
|
||||
void lua_rawsettable (void); /* In: table, index, value */
|
||||
lua_Object lua_gettable (void); /* In: table, index */
|
||||
lua_Object lua_rawgettable (void); /* In: table, index */
|
||||
|
||||
int lua_tag (lua_Object object);
|
||||
|
||||
char *lua_nextvar (char *varname); /* Out: value */
|
||||
int lua_next (lua_Object o, int i);
|
||||
/* Out: ref, value */
|
||||
|
||||
int lua_ref (int lock); /* In: value */
|
||||
lua_Object lua_getref (int ref);
|
||||
void lua_unref (int ref);
|
||||
|
||||
lua_Object lua_createtable (void);
|
||||
|
||||
long lua_collectgarbage (long limit);
|
||||
typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);
|
||||
|
||||
|
||||
/* =============================================================== */
|
||||
/* some useful macros/functions */
|
||||
/*
|
||||
** prototype for memory-allocation functions
|
||||
*/
|
||||
typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
|
||||
|
||||
#define lua_call(name) lua_callfunction(lua_getglobal(name))
|
||||
|
||||
#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
|
||||
/*
|
||||
** basic types
|
||||
*/
|
||||
#define LUA_TNONE (-1)
|
||||
|
||||
#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(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
|
||||
|
||||
#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)
|
||||
/* minimum Lua stack available to a C function */
|
||||
#define LUA_MINSTACK 20
|
||||
|
||||
#define lua_clonetag(t) lua_copytagmethods(lua_newtag(), (t))
|
||||
|
||||
lua_Object lua_seterrormethod (void); /* In: new method */
|
||||
/*
|
||||
** generic extra include file
|
||||
*/
|
||||
#if defined(LUA_USER_H)
|
||||
#include LUA_USER_H
|
||||
#endif
|
||||
|
||||
/* ==========================================================================
|
||||
** for compatibility with old versions. Avoid using these macros/functions
|
||||
** If your program does need any of these, define LUA_COMPAT2_5
|
||||
|
||||
/* type of numbers in Lua */
|
||||
typedef LUA_NUMBER lua_Number;
|
||||
|
||||
|
||||
/* type for integer functions */
|
||||
typedef LUA_INTEGER lua_Integer;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** state manipulation
|
||||
*/
|
||||
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
|
||||
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 lua_Integer (lua_tointeger) (lua_State *L, int idx);
|
||||
LUA_API int (lua_toboolean) (lua_State *L, int idx);
|
||||
LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
|
||||
LUA_API size_t (lua_objlen) (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_pushinteger) (lua_State *L, lua_Integer 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);
|
||||
LUA_API int (lua_pushthread) (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
LUA_API void (lua_gettable) (lua_State *L, int idx);
|
||||
LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k);
|
||||
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_createtable) (lua_State *L, int narr, int nrec);
|
||||
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_setfield) (lua_State *L, int idx, const char *k);
|
||||
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_Reader reader, void *dt,
|
||||
const char *chunkname);
|
||||
|
||||
LUA_API int (lua_dump) (lua_State *L, lua_Writer 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);
|
||||
LUA_API int (lua_status) (lua_State *L);
|
||||
|
||||
/*
|
||||
** garbage-collection function and options
|
||||
*/
|
||||
|
||||
#define LUA_GCSTOP 0
|
||||
#define LUA_GCRESTART 1
|
||||
#define LUA_GCCOLLECT 2
|
||||
#define LUA_GCCOUNT 3
|
||||
#define LUA_GCCOUNTB 4
|
||||
#define LUA_GCSTEP 5
|
||||
#define LUA_GCSETPAUSE 6
|
||||
#define LUA_GCSETSTEPMUL 7
|
||||
|
||||
LUA_API int (lua_gc) (lua_State *L, int what, int data);
|
||||
|
||||
|
||||
/*
|
||||
** miscellaneous functions
|
||||
*/
|
||||
|
||||
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);
|
||||
|
||||
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
|
||||
LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** ===============================================================
|
||||
** some useful macros
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#define lua_pop(L,n) lua_settop(L, -(n)-1)
|
||||
|
||||
#define lua_newtable(L) lua_createtable(L, 0, 0)
|
||||
|
||||
#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
|
||||
|
||||
#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
|
||||
|
||||
#define lua_strlen(L,i) lua_objlen(L, (i))
|
||||
|
||||
#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_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
|
||||
#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
|
||||
#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
|
||||
|
||||
#define lua_pushliteral(L, s) \
|
||||
lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
|
||||
|
||||
#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s))
|
||||
#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s))
|
||||
|
||||
#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** compatibility macros and functions
|
||||
*/
|
||||
|
||||
#define lua_open() luaL_newstate()
|
||||
|
||||
#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
|
||||
|
||||
#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0)
|
||||
|
||||
#define lua_Chunkreader lua_Reader
|
||||
#define lua_Chunkwriter lua_Writer
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================================
|
||||
** Debug API
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LUA_COMPAT2_5
|
||||
/*
|
||||
** Event codes
|
||||
*/
|
||||
#define LUA_HOOKCALL 0
|
||||
#define LUA_HOOKRET 1
|
||||
#define LUA_HOOKLINE 2
|
||||
#define LUA_HOOKCOUNT 3
|
||||
#define LUA_HOOKTAILRET 4
|
||||
|
||||
|
||||
lua_Object lua_setfallback (char *event, lua_CFunction fallback);
|
||||
/*
|
||||
** 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)
|
||||
|
||||
#define lua_storeglobal lua_setglobal
|
||||
#define lua_type lua_tag
|
||||
typedef struct lua_Debug lua_Debug; /* activation record */
|
||||
|
||||
#define lua_lockobject(o) lua_refobject(o,1)
|
||||
#define lua_lock() lua_ref(1)
|
||||
#define lua_getlocked lua_getref
|
||||
#define lua_pushlocked lua_pushref
|
||||
#define lua_unlock lua_unref
|
||||
|
||||
#define lua_pushliteral(o) lua_pushstring(o)
|
||||
/* Functions to be called by the debuger in specific events */
|
||||
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
||||
|
||||
#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_gettable())
|
||||
#define lua_getfield(o,f) (lua_pushobject(o), lua_pushstring(f), lua_gettable())
|
||||
|
||||
#define lua_copystring(o) (strdup(lua_getstring(o)))
|
||||
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);
|
||||
|
||||
#define lua_getsubscript lua_gettable
|
||||
#define lua_storesubscript lua_settable
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
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) */
|
||||
int lastlinedefined; /* (S) */
|
||||
char short_src[LUA_IDSIZE]; /* (S) */
|
||||
/* private part */
|
||||
int i_ci; /* active function */
|
||||
};
|
||||
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (c) 1994-1999 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-2006 Lua.org, 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
|
||||
|
||||
762
luaconf.h
Normal file
762
luaconf.h
Normal file
@@ -0,0 +1,762 @@
|
||||
/*
|
||||
** $Id: luaconf.h,v 1.81 2006/02/10 17:44:06 roberto Exp roberto $
|
||||
** Configuration file for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lconfig_h
|
||||
#define lconfig_h
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
/*
|
||||
** ==================================================================
|
||||
** Search for "@@" to find all configurable definitions.
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_ANSI controls the use of non-ansi features.
|
||||
** CHANGE it (define it) if you want Lua to avoid the use of any
|
||||
** non-ansi feature or library.
|
||||
*/
|
||||
#if defined(__STRICT_ANSI__)
|
||||
#define LUA_ANSI
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(LUA_ANSI) && defined(_WIN32)
|
||||
#define LUA_WIN
|
||||
#endif
|
||||
|
||||
#if defined(LUA_USE_LINUX)
|
||||
#define LUA_USE_POSIX
|
||||
#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
|
||||
#define LUA_USE_READLINE /* needs some extra libraries */
|
||||
#endif
|
||||
|
||||
#if defined(LUA_USE_MACOSX)
|
||||
#define LUA_USE_POSIX
|
||||
#define LUA_DL_DYLD /* does not need extra library */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_USE_POSIX includes all functionallity listed as X/Open System
|
||||
@* Interfaces Extension (XSI).
|
||||
** CHANGE it (define it) if your system is XSI compatible.
|
||||
*/
|
||||
#if defined(LUA_USE_POSIX)
|
||||
#define LUA_USE_MKSTEMP
|
||||
#define LUA_USE_ISATTY
|
||||
#define LUA_USE_POPEN
|
||||
#define LUA_USE_ULONGJMP
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_PATH and LUA_CPATH are the names of the environment variables that
|
||||
@* Lua check to set its paths.
|
||||
@@ LUA_INIT is the name of the environment variable that Lua
|
||||
@* checks for initialization code.
|
||||
** CHANGE them if you want different names.
|
||||
*/
|
||||
#define LUA_PATH "LUA_PATH"
|
||||
#define LUA_CPATH "LUA_CPATH"
|
||||
#define LUA_INIT "LUA_INIT"
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
|
||||
@* Lua libraries.
|
||||
@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
|
||||
@* C libraries.
|
||||
** CHANGE them if your machine has a non-conventional directory
|
||||
** hierarchy or if you want to install your libraries in
|
||||
** non-conventional directories.
|
||||
*/
|
||||
#if defined(_WIN32)
|
||||
/*
|
||||
** In Windows, any exclamation mark ('!') in the path is replaced by the
|
||||
** path of the directory of the executable file of the current process.
|
||||
*/
|
||||
#define LUA_LDIR "!\\lua\\"
|
||||
#define LUA_CDIR "!\\"
|
||||
#define LUA_PATH_DEFAULT \
|
||||
".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
|
||||
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua"
|
||||
#define LUA_CPATH_DEFAULT \
|
||||
".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
|
||||
|
||||
#else
|
||||
#define LUA_ROOT "/usr/local/"
|
||||
#define LUA_LDIR LUA_ROOT "share/lua/5.1/"
|
||||
#define LUA_CDIR LUA_ROOT "lib/lua/5.1/"
|
||||
#define LUA_PATH_DEFAULT \
|
||||
"./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
|
||||
LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua"
|
||||
#define LUA_CPATH_DEFAULT \
|
||||
"./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_DIRSEP is the directory separator (for submodules).
|
||||
** CHANGE it if your machine does not use "/" as the directory separator
|
||||
** and is not Windows. (On Windows Lua automatically uses "\".)
|
||||
*/
|
||||
#if defined(_WIN32)
|
||||
#define LUA_DIRSEP "\\"
|
||||
#else
|
||||
#define LUA_DIRSEP "/"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_PATHSEP is the character that separates templates in a path.
|
||||
@@ LUA_PATH_MARK is the string that marks the substitution points in a
|
||||
@* template.
|
||||
@@ LUA_EXECDIR in a Windows path is replaced by the executable's
|
||||
@* directory.
|
||||
@@ LUA_IGMARK is a mark to ignore all before it when bulding the
|
||||
@* luaopen_ function name.
|
||||
** CHANGE them if for some reason your system cannot use those
|
||||
** characters. (E.g., if one of those characters is a common character
|
||||
** in file/directory names.) Probably you do not need to change them.
|
||||
*/
|
||||
#define LUA_PATHSEP ";"
|
||||
#define LUA_PATH_MARK "?"
|
||||
#define LUA_EXECDIR "!"
|
||||
#define LUA_IGMARK "-"
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
|
||||
** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
|
||||
** machines, ptrdiff_t gives a good choice between int or long.)
|
||||
*/
|
||||
#define LUA_INTEGER ptrdiff_t
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_API is a mark for all core API functions.
|
||||
@@ LUALIB_API is a mark for all standard library functions.
|
||||
** CHANGE them if you need to define those functions in some special way.
|
||||
** For instance, if you want to create one Windows DLL with the core and
|
||||
** the libraries, you may want to use the following definition (define
|
||||
** LUA_BUILD_AS_DLL to get it).
|
||||
*/
|
||||
#if defined(LUA_BUILD_AS_DLL)
|
||||
|
||||
#if defined(LUA_CORE) || defined(LUA_LIB)
|
||||
#define LUA_API __declspec(dllexport)
|
||||
#else
|
||||
#define LUA_API __declspec(dllimport)
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#define LUA_API extern
|
||||
|
||||
#endif
|
||||
|
||||
/* more often than not the libs go together with the core */
|
||||
#define LUALIB_API LUA_API
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_FUNC is a mark for all extern functions that are not to be
|
||||
@* exported to outside modules.
|
||||
@@ LUAI_DATA is a mark for all extern (const) variables that are not to
|
||||
@* be exported to outside modules.
|
||||
** CHANGE them if you need to mark them in some special way. Elf/gcc
|
||||
** (versions 3.2 and later) mark them as "hidden" to optimize access
|
||||
** when Lua is compiled as a shared library.
|
||||
*/
|
||||
#if defined(luaall_c)
|
||||
#define LUAI_FUNC static
|
||||
#define LUAI_DATA /* empty */
|
||||
|
||||
#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
||||
defined(__ELF__)
|
||||
#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
|
||||
#define LUAI_DATA LUAI_FUNC
|
||||
|
||||
#else
|
||||
#define LUAI_FUNC extern
|
||||
#define LUAI_DATA extern
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_QL describes how error messages quote program elements.
|
||||
** CHANGE it if you want a different appearance.
|
||||
*/
|
||||
#define LUA_QL(x) "'" x "'"
|
||||
#define LUA_QS LUA_QL("%s")
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_IDSIZE gives the maximum size for the description of the source
|
||||
@* of a function in debug information.
|
||||
** CHANGE it if you want a different size.
|
||||
*/
|
||||
#define LUA_IDSIZE 60
|
||||
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
** Stand-alone configuration
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#if defined(lua_c) || defined(luaall_c)
|
||||
|
||||
/*
|
||||
@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that
|
||||
@* is, whether we're running lua interactively).
|
||||
** CHANGE it if you have a better definition for non-POSIX/non-Windows
|
||||
** systems.
|
||||
*/
|
||||
#if defined(LUA_USE_ISATTY)
|
||||
#include <unistd.h>
|
||||
#define lua_stdin_is_tty() isatty(0)
|
||||
#elif defined(LUA_WIN)
|
||||
#include <io.h>
|
||||
#include <stdio.h>
|
||||
#define lua_stdin_is_tty() _isatty(_fileno(stdin))
|
||||
#else
|
||||
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_PROMPT is the default prompt used by stand-alone Lua.
|
||||
@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.
|
||||
** CHANGE them if you want different prompts. (You can also change the
|
||||
** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)
|
||||
*/
|
||||
#define LUA_PROMPT "> "
|
||||
#define LUA_PROMPT2 ">> "
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_PROGNAME is the default name for the stand-alone Lua program.
|
||||
** CHANGE it if your stand-alone interpreter has a different name and
|
||||
** your system is not able to detect that name automatically.
|
||||
*/
|
||||
#define LUA_PROGNAME "lua"
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_MAXINPUT is the maximum length for an input line in the
|
||||
@* stand-alone interpreter.
|
||||
** CHANGE it if you need longer lines.
|
||||
*/
|
||||
#define LUA_MAXINPUT 512
|
||||
|
||||
|
||||
/*
|
||||
@@ lua_readline defines how to show a prompt and then read a line from
|
||||
@* the standard input.
|
||||
@@ lua_saveline defines how to "save" a read line in a "history".
|
||||
@@ lua_freeline defines how to free a line read by lua_readline.
|
||||
** CHANGE them if you want to improve this functionality (e.g., by using
|
||||
** GNU readline and history facilities).
|
||||
*/
|
||||
#if defined(LUA_USE_READLINE)
|
||||
#include <stdio.h>
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
|
||||
#define lua_saveline(L,idx) \
|
||||
if (lua_strlen(L,idx) > 0) /* non-empty line? */ \
|
||||
add_history(lua_tostring(L, idx)); /* add it to history */
|
||||
#define lua_freeline(L,b) ((void)L, free(b))
|
||||
#else
|
||||
#define lua_readline(L,b,p) \
|
||||
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
|
||||
fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
|
||||
#define lua_saveline(L,idx) { (void)L; (void)idx; }
|
||||
#define lua_freeline(L,b) { (void)L; (void)b; }
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles
|
||||
@* as a percentage.
|
||||
** CHANGE it if you want the GC to run faster or slower (higher values
|
||||
** mean larger pauses which mean slower collection.) You can also change
|
||||
** this value dynamically.
|
||||
*/
|
||||
#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_GCMUL defines the default speed of garbage collection relative to
|
||||
@* memory allocation as a percentage.
|
||||
** CHANGE it if you want to change the granularity of the garbage
|
||||
** collection. (Higher values mean coarser collections. 0 represents
|
||||
** infinity, where each step performs a full collection.) You can also
|
||||
** change this value dynamically.
|
||||
*/
|
||||
#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_GETN controls compatibility with old getn behavior.
|
||||
** CHANGE it (define it) if you want exact compatibility with the
|
||||
** behavior of setn/getn in Lua 5.0.
|
||||
*/
|
||||
#undef LUA_COMPAT_GETN
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib.
|
||||
** CHANGE it to undefined as soon as you do not need a global 'loadlib'
|
||||
** function (the function is still available as 'package.loadlib').
|
||||
*/
|
||||
#undef LUA_COMPAT_LOADLIB
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature.
|
||||
** CHANGE it to undefined as soon as your programs use only '...' to
|
||||
** access vararg parameters (instead of the old 'arg' table).
|
||||
*/
|
||||
#define LUA_COMPAT_VARARG
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_MOD controls compatibility with old math.mod function.
|
||||
** CHANGE it to undefined as soon as your programs use 'math.fmod' or
|
||||
** the new '%' operator instead of 'math.mod'.
|
||||
*/
|
||||
#define LUA_COMPAT_MOD
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting
|
||||
@* facility.
|
||||
** CHANGE it to 2 if you want the old behaviour, or undefine it to turn
|
||||
** off the advisory error when nesting [[...]].
|
||||
*/
|
||||
#define LUA_COMPAT_LSTR 1
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name.
|
||||
** CHANGE it to undefined as soon as you rename 'string.gfind' to
|
||||
** 'string.gmatch'.
|
||||
*/
|
||||
#define LUA_COMPAT_GFIND
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib'
|
||||
@* behavior.
|
||||
** CHANGE it to undefined as soon as you replace to 'luaL_registry'
|
||||
** your uses of 'luaL_openlib'
|
||||
*/
|
||||
#define LUA_COMPAT_OPENLIB
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@@ luai_apicheck is the assert macro used by the Lua-C API.
|
||||
** CHANGE luai_apicheck if you want Lua to perform some checks in the
|
||||
** parameters it gets from API calls. This may slow down the interpreter
|
||||
** a bit, but may be quite useful when debugging C code that interfaces
|
||||
** with Lua. A useful redefinition is to use assert.h.
|
||||
*/
|
||||
#if defined(LUA_USE_APICHECK)
|
||||
#include <assert.h>
|
||||
#define luai_apicheck(L,o) { (void)L; assert(o); }
|
||||
#else
|
||||
#define luai_apicheck(L,o) { (void)L; }
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_BITSINT defines the number of bits in an int.
|
||||
** CHANGE here if Lua cannot automatically detect the number of bits of
|
||||
** your machine. Probably you do not need to change this.
|
||||
*/
|
||||
/* avoid overflows in comparison */
|
||||
#if INT_MAX-20 < 32760
|
||||
#define LUAI_BITSINT 16
|
||||
#elif INT_MAX > 2147483640L
|
||||
/* int has at least 32 bits */
|
||||
#define LUAI_BITSINT 32
|
||||
#else
|
||||
#error "you must define LUA_BITSINT with number of bits in an integer"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
|
||||
@@ LUAI_INT32 is an signed integer with at least 32 bits.
|
||||
@@ LUAI_UMEM is an unsigned integer big enough to count the total
|
||||
@* memory used by Lua.
|
||||
@@ LUAI_MEM is a signed integer big enough to count the total memory
|
||||
@* used by Lua.
|
||||
** CHANGE here if for some weird reason the default definitions are not
|
||||
** good enough for your machine. (The definitions in the 'else'
|
||||
** part always works, but may waste space on machines with 64-bit
|
||||
** longs.) Probably you do not need to change this.
|
||||
*/
|
||||
#if LUAI_BITSINT >= 32
|
||||
#define LUAI_UINT32 unsigned int
|
||||
#define LUAI_INT32 int
|
||||
#define LUAI_MAXINT32 INT_MAX
|
||||
#define LUAI_UMEM size_t
|
||||
#define LUAI_MEM ptrdiff_t
|
||||
#else
|
||||
/* 16-bit ints */
|
||||
#define LUAI_UINT32 unsigned long
|
||||
#define LUAI_INT32 long
|
||||
#define LUAI_MAXINT32 LONG_MAX
|
||||
#define LUAI_UMEM unsigned long
|
||||
#define LUAI_MEM long
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_MAXCALLS limits the number of nested calls.
|
||||
** CHANGE it if you need really deep recursive calls. This limit is
|
||||
** arbitrary; its only purpose is to stop infinite recursion before
|
||||
** exhausting memory.
|
||||
*/
|
||||
#define LUAI_MAXCALLS 20000
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function
|
||||
@* can use.
|
||||
** CHANGE it if you need lots of (Lua) stack space for your C
|
||||
** functions. This limit is arbitrary; its only purpose is to stop C
|
||||
** functions to consume unlimited stack space.
|
||||
*/
|
||||
#define LUAI_MAXCSTACK 2048
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
** CHANGE (to smaller values) the following definitions if your system
|
||||
** has a small C stack. (Or you may want to change them to larger
|
||||
** values if your system has a large C stack and these limits are
|
||||
** too rigid for you.) Some of these constants control the size of
|
||||
** stack-allocated arrays used by the compiler or the interpreter, while
|
||||
** others limit the maximum number of recursive calls that the compiler
|
||||
** or the interpreter can perform. Values too large may cause a C stack
|
||||
** overflow for some forms of deep constructs.
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and
|
||||
@* syntactical nested non-terminals in a program.
|
||||
*/
|
||||
#define LUAI_MAXCCALLS 200
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_MAXVARS is the maximum number of local variables per function
|
||||
@* (must be smaller than 250).
|
||||
*/
|
||||
#define LUAI_MAXVARS 200
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function
|
||||
@* (must be smaller than 250).
|
||||
*/
|
||||
#define LUAI_MAXUPVALUES 60
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
|
||||
*/
|
||||
#define LUAL_BUFFERSIZE BUFSIZ
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
@@ LUA_NUMBER is the type of numbers in Lua.
|
||||
** CHANGE the following definitions only if you want to build Lua
|
||||
** with a number type different from double. You may also need to
|
||||
** change lua_number2int & lua_number2integer.
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#define LUA_NUMBER_DOUBLE
|
||||
#define LUA_NUMBER double
|
||||
|
||||
/*
|
||||
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
|
||||
@* over a number.
|
||||
*/
|
||||
#define LUAI_UACNUMBER double
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_NUMBER_SCAN is the format for reading numbers.
|
||||
@@ LUA_NUMBER_FMT is the format for writing numbers.
|
||||
@@ lua_number2str converts a number to a string.
|
||||
@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
|
||||
@@ lua_str2number converts a string to a number.
|
||||
*/
|
||||
#define LUA_NUMBER_SCAN "%lf"
|
||||
#define LUA_NUMBER_FMT "%.14g"
|
||||
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
|
||||
#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
|
||||
#define lua_str2number(s,p) strtod((s), (p))
|
||||
|
||||
|
||||
/*
|
||||
@@ The luai_num* macros define the primitive operations over numbers.
|
||||
*/
|
||||
#if defined(LUA_CORE)
|
||||
#include <math.h>
|
||||
#define luai_numadd(a,b) ((a)+(b))
|
||||
#define luai_numsub(a,b) ((a)-(b))
|
||||
#define luai_nummul(a,b) ((a)*(b))
|
||||
#define luai_numdiv(a,b) ((a)/(b))
|
||||
#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
|
||||
#define luai_numpow(a,b) (pow(a,b))
|
||||
#define luai_numunm(a) (-(a))
|
||||
#define luai_numeq(a,b) ((a)==(b))
|
||||
#define luai_numlt(a,b) ((a)<(b))
|
||||
#define luai_numle(a,b) ((a)<=(b))
|
||||
#define luai_numisnan(a) (!luai_numeq((a), (a)))
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ lua_number2int is a macro to convert lua_Number to int.
|
||||
@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
|
||||
** CHANGE them if you know a faster way to convert a lua_Number to
|
||||
** int (with any rounding method and without throwing errors) in your
|
||||
** system. In Pentium machines, a naive typecast from double to int
|
||||
** in C is extremely slow, so any alternative is worth trying.
|
||||
*/
|
||||
|
||||
/* On a Pentium, resort to a trick */
|
||||
#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
|
||||
(defined(__i386) || defined (_M_IX86) || defined(__i386__))
|
||||
|
||||
/* On a Microsoft compiler, use assembler */
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
#define lua_number2int(i,d) __asm fld d __asm fistp i
|
||||
#define lua_number2integer(i,n) lua_number2int(i, n)
|
||||
|
||||
/* the next trick should work on any Pentium, but sometimes clashes
|
||||
with a DirectX idiosyncrasy */
|
||||
#else
|
||||
|
||||
union luai_Cast { double l_d; long l_l; };
|
||||
#define lua_number2int(i,d) \
|
||||
{ volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
|
||||
#define lua_number2integer(i,n) lua_number2int(i, n)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* this option always works, but may be slow */
|
||||
#else
|
||||
#define lua_number2int(i,d) ((i)=(int)(d))
|
||||
#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
|
||||
|
||||
#endif
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
|
||||
** CHANGE it if your system requires alignments larger than double. (For
|
||||
** instance, if your system supports long doubles and they must be
|
||||
** aligned in 16-byte boundaries, then you should add long double in the
|
||||
** union.) Probably you do not need to change this.
|
||||
*/
|
||||
#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; }
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling.
|
||||
** CHANGE them if you prefer to use longjmp/setjmp even with C++
|
||||
** or if want/don't to use _longjmp/_setjmp instead of regular
|
||||
** longjmp/setjmp. By default, Lua handles errors with exceptions when
|
||||
** compiling as C++ code, with _longjmp/_setjmp when asked to use them,
|
||||
** and with longjmp/setjmp otherwise.
|
||||
*/
|
||||
#if defined(__cplusplus)
|
||||
/* C++ exceptions */
|
||||
#define LUAI_THROW(L,c) throw(c)
|
||||
#define LUAI_TRY(L,c,a) try { a } catch(...) \
|
||||
{ if ((c)->status == 0) (c)->status = -1; }
|
||||
#define luai_jmpbuf int /* dummy variable */
|
||||
|
||||
#elif defined(LUA_USE_ULONGJMP)
|
||||
/* in Unix, try _longjmp/_setjmp (more efficient) */
|
||||
#define LUAI_THROW(L,c) _longjmp((c)->b, 1)
|
||||
#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
|
||||
#define luai_jmpbuf jmp_buf
|
||||
|
||||
#else
|
||||
/* default handling with long jumps */
|
||||
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
|
||||
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
|
||||
#define luai_jmpbuf jmp_buf
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern
|
||||
@* can do during pattern-matching.
|
||||
** CHANGE it if you need more captures. This limit is arbitrary.
|
||||
*/
|
||||
#define LUA_MAXCAPTURES 32
|
||||
|
||||
|
||||
/*
|
||||
@@ lua_tmpnam is the function that the OS library uses to create a
|
||||
@* temporary name.
|
||||
@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam.
|
||||
** CHANGE them if you have an alternative to tmpnam (which is considered
|
||||
** insecure) or if you want the original tmpnam anyway. By default, Lua
|
||||
** uses tmpnam except when POSIX is available, where it uses mkstemp.
|
||||
*/
|
||||
#if defined(loslib_c) || defined(luaall_c)
|
||||
|
||||
#if defined(LUA_USE_MKSTEMP)
|
||||
#include <unistd.h>
|
||||
#define LUA_TMPNAMBUFSIZE 32
|
||||
#define lua_tmpnam(b,e) { \
|
||||
strcpy(b, "/tmp/lua_XXXXXX"); \
|
||||
e = mkstemp(b); \
|
||||
if (e != -1) close(e); \
|
||||
e = (e == -1); }
|
||||
|
||||
#else
|
||||
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
||||
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ lua_popen spawns a new process connected to the current one through
|
||||
@* the file streams.
|
||||
** CHANGE it if you have a way to implement it in your system.
|
||||
*/
|
||||
#if defined(LUA_USE_POPEN)
|
||||
|
||||
#define lua_popen(L,c,m) ((void)L, popen(c,m))
|
||||
#define lua_pclose(L,file) ((void)L, (pclose(file) != -1))
|
||||
|
||||
#elif defined(LUA_WIN)
|
||||
|
||||
#define lua_popen(L,c,m) ((void)L, _popen(c,m))
|
||||
#define lua_pclose(L,file) ((void)L, (_pclose(file) != -1))
|
||||
|
||||
#else
|
||||
|
||||
#define lua_popen(L,c,m) ((void)((void)c, m), \
|
||||
luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
|
||||
#define lua_pclose(L,file) ((void)((void)L, file), 0)
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
@@ LUA_DL_* define which dynamic-library system Lua should use.
|
||||
** CHANGE here if Lua has problems choosing the appropriate
|
||||
** dynamic-library system for your platform (either Windows' DLL, Mac's
|
||||
** dyld, or Unix's dlopen). If your system is some kind of Unix, there
|
||||
** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for
|
||||
** it. To use dlopen you also need to adapt the src/Makefile (probably
|
||||
** adding -ldl to the linker options), so Lua does not select it
|
||||
** automatically. (When you change the makefile to add -ldl, you must
|
||||
** also add -DLUA_USE_DLOPEN.)
|
||||
** If you do not want any kind of dynamic library, undefine all these
|
||||
** options.
|
||||
** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD.
|
||||
*/
|
||||
#if defined(LUA_USE_DLOPEN)
|
||||
#define LUA_DL_DLOPEN
|
||||
#endif
|
||||
|
||||
#if defined(LUA_WIN)
|
||||
#define LUA_DL_DLL
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State
|
||||
@* (the data goes just *before* the lua_State pointer).
|
||||
** CHANGE (define) this if you really need that. This value must be
|
||||
** a multiple of the maximum alignment required for your machine.
|
||||
*/
|
||||
#define LUAI_EXTRASPACE 0
|
||||
|
||||
|
||||
/*
|
||||
@@ luai_userstate* allow user-specific actions on threads.
|
||||
** CHANGE them if you defined LUAI_EXTRASPACE and need to do something
|
||||
** extra when a thread is created/deleted/resumed/yielded.
|
||||
*/
|
||||
#define luai_userstateopen(L) ((void)L)
|
||||
#define luai_userstateclose(L) ((void)L)
|
||||
#define luai_userstatethread(L,L1) ((void)L)
|
||||
#define luai_userstatefree(L) ((void)L)
|
||||
#define luai_userstateresume(L,n) ((void)L)
|
||||
#define luai_userstateyield(L,n) ((void)L)
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_INTFRMLEN is the length modifier for integer conversions
|
||||
@* in 'string.format'.
|
||||
@@ LUA_INTFRM_T is the integer type correspoding to the previous length
|
||||
@* modifier.
|
||||
** CHANGE them if your system supports long long or does not support long.
|
||||
*/
|
||||
|
||||
#if defined(LUA_USELONGLONG)
|
||||
|
||||
#define LUA_INTFRMLEN "ll"
|
||||
#define LUA_INTFRM_T long long
|
||||
|
||||
#else
|
||||
|
||||
#define LUA_INTFRMLEN "l"
|
||||
#define LUA_INTFRM_T long
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* =================================================================== */
|
||||
|
||||
/*
|
||||
** Local configuration. You can use this space to add your redefinitions
|
||||
** without modifying the main part of the file.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
34
luadebug.h
34
luadebug.h
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
** $Id: luadebug.h,v 1.5 1999/02/04 17:47:59 roberto Exp roberto $
|
||||
** Debugging API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef luadebug_h
|
||||
#define luadebug_h
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
typedef lua_Object lua_Function;
|
||||
|
||||
typedef void (*lua_LHFunction) (int line);
|
||||
typedef void (*lua_CHFunction) (lua_Function func, char *file, int line);
|
||||
|
||||
lua_Function lua_stackedfunction (int level);
|
||||
void lua_funcinfo (lua_Object func, char **source, int *linedefined);
|
||||
int lua_currentline (lua_Function func);
|
||||
char *lua_getobjname (lua_Object o, char **name);
|
||||
|
||||
lua_Object lua_getlocal (lua_Function func, int local_number, char **name);
|
||||
int lua_setlocal (lua_Function func, int local_number);
|
||||
|
||||
int lua_nups (lua_Function func);
|
||||
|
||||
lua_LHFunction lua_setlinehook (lua_LHFunction func);
|
||||
lua_CHFunction lua_setcallhook (lua_CHFunction func);
|
||||
int lua_setdebug (int debug);
|
||||
|
||||
|
||||
#endif
|
||||
49
lualib.h
49
lualib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lualib.h,v 1.5 1999/01/08 16:47:44 roberto Exp roberto $
|
||||
** $Id: lualib.h,v 1.35 2005/08/10 18:06:58 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,27 +10,44 @@
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
void lua_iolibopen (void);
|
||||
void lua_strlibopen (void);
|
||||
void lua_mathlibopen (void);
|
||||
void lua_dblibopen (void);
|
||||
|
||||
/* Key to file-handle type */
|
||||
#define LUA_FILEHANDLE "FILE*"
|
||||
|
||||
|
||||
void lua_userinit (void);
|
||||
#define LUA_COLIBNAME "coroutine"
|
||||
LUALIB_API int (luaopen_base) (lua_State *L);
|
||||
|
||||
#define LUA_TABLIBNAME "table"
|
||||
LUALIB_API int (luaopen_table) (lua_State *L);
|
||||
|
||||
#define LUA_IOLIBNAME "io"
|
||||
LUALIB_API int (luaopen_io) (lua_State *L);
|
||||
|
||||
#define LUA_OSLIBNAME "os"
|
||||
LUALIB_API int (luaopen_os) (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);
|
||||
|
||||
#define LUA_LOADLIBNAME "package"
|
||||
LUALIB_API int (luaopen_package) (lua_State *L);
|
||||
|
||||
|
||||
/* To keep compatibility with old versions */
|
||||
|
||||
#define iolib_open lua_iolibopen
|
||||
#define strlib_open lua_strlibopen
|
||||
#define mathlib_open lua_mathlibopen
|
||||
/* open all previous libraries */
|
||||
LUALIB_API void (luaL_openlibs) (lua_State *L);
|
||||
|
||||
|
||||
|
||||
/* Auxiliary functions (private) */
|
||||
|
||||
char *luaI_classend (char *p);
|
||||
int luaI_singlematch (int c, char *p, char *ep);
|
||||
|
||||
#ifndef lua_assert
|
||||
#define lua_assert(x) ((void)0)
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
332
lundump.c
332
lundump.c
@@ -1,239 +1,223 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.21 1999/07/02 19:34:26 lhf Exp $
|
||||
** load bytecodes from files
|
||||
** $Id: lundump.c,v 1.60 2006/02/16 15:53:49 lhf Exp $
|
||||
** load precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "lauxlib.h"
|
||||
|
||||
#define lundump_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lobject.h"
|
||||
#include "lstring.h"
|
||||
#include "lundump.h"
|
||||
#include "lzio.h"
|
||||
|
||||
#define LoadBlock(b,size,Z) ezread(Z,b,size)
|
||||
typedef struct {
|
||||
lua_State* L;
|
||||
ZIO* Z;
|
||||
Mbuffer* b;
|
||||
const char* name;
|
||||
} LoadState;
|
||||
|
||||
static void unexpectedEOZ (ZIO* Z)
|
||||
#ifdef LUAC_TRUST_BINARIES
|
||||
#define IF(c,s)
|
||||
#else
|
||||
#define IF(c,s) if (c) error(S,s)
|
||||
|
||||
static void error(LoadState* S, const char* why)
|
||||
{
|
||||
luaL_verror("unexpected end of file in %s",zname(Z));
|
||||
luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
|
||||
luaD_throw(S->L,LUA_ERRSYNTAX);
|
||||
}
|
||||
#endif
|
||||
|
||||
#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
|
||||
#define LoadByte(S) (lu_byte)LoadChar(S)
|
||||
#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
|
||||
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
|
||||
|
||||
static void LoadBlock(LoadState* S, void* b, size_t size)
|
||||
{
|
||||
size_t r=luaZ_read(S->Z,b,size);
|
||||
IF (r!=0, "unexpected end");
|
||||
}
|
||||
|
||||
static int ezgetc (ZIO* Z)
|
||||
static int LoadChar(LoadState* S)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==EOZ) unexpectedEOZ(Z);
|
||||
return c;
|
||||
char x;
|
||||
LoadVar(S,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
static void ezread (ZIO* Z, void* b, int n)
|
||||
static int LoadInt(LoadState* S)
|
||||
{
|
||||
int r=zread(Z,b,n);
|
||||
if (r!=0) unexpectedEOZ(Z);
|
||||
int x;
|
||||
LoadVar(S,x);
|
||||
IF (x<0, "bad integer");
|
||||
return x;
|
||||
}
|
||||
|
||||
static unsigned int LoadWord (ZIO* Z)
|
||||
static lua_Number LoadNumber(LoadState* S)
|
||||
{
|
||||
unsigned int hi=ezgetc(Z);
|
||||
unsigned int lo=ezgetc(Z);
|
||||
return (hi<<8)|lo;
|
||||
lua_Number x;
|
||||
LoadVar(S,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
static unsigned long LoadLong (ZIO* Z)
|
||||
static TString* LoadString(LoadState* S)
|
||||
{
|
||||
unsigned long hi=LoadWord(Z);
|
||||
unsigned long lo=LoadWord(Z);
|
||||
return (hi<<16)|lo;
|
||||
}
|
||||
|
||||
/*
|
||||
* convert number from text
|
||||
*/
|
||||
double luaU_str2d (char* b, char* where)
|
||||
{
|
||||
int negative=(b[0]=='-');
|
||||
double x=luaO_str2d(b+negative);
|
||||
if (x<0) luaL_verror("cannot convert number '%s' in %s",b,where);
|
||||
return negative ? -x : x;
|
||||
}
|
||||
|
||||
static real LoadNumber (ZIO* Z, int native)
|
||||
{
|
||||
real x;
|
||||
if (native)
|
||||
{
|
||||
LoadBlock(&x,sizeof(x),Z);
|
||||
return x;
|
||||
}
|
||||
else
|
||||
{
|
||||
char b[256];
|
||||
int size=ezgetc(Z);
|
||||
LoadBlock(b,size,Z);
|
||||
b[size]=0;
|
||||
return luaU_str2d(b,zname(Z));
|
||||
}
|
||||
}
|
||||
|
||||
static int LoadInt (ZIO* Z, char* message)
|
||||
{
|
||||
unsigned long l=LoadLong(Z);
|
||||
unsigned int i=l;
|
||||
if (i!=l) luaL_verror(message,l,zname(Z));
|
||||
return i;
|
||||
}
|
||||
|
||||
#define PAD 5 /* two word operands plus opcode */
|
||||
|
||||
static Byte* LoadCode (ZIO* Z)
|
||||
{
|
||||
int size=LoadInt(Z,"code too long (%ld bytes) in %s");
|
||||
Byte* b=luaM_malloc(size+PAD);
|
||||
LoadBlock(b,size,Z);
|
||||
if (b[size-1]!=ENDCODE) luaL_verror("bad code in %s",zname(Z));
|
||||
memset(b+size,ENDCODE,PAD); /* pad code for safety */
|
||||
return b;
|
||||
}
|
||||
|
||||
static TaggedString* LoadTString (ZIO* Z)
|
||||
{
|
||||
long size=LoadLong(Z);
|
||||
size_t size;
|
||||
LoadVar(S,size);
|
||||
if (size==0)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
char* s=luaL_openspace(size);
|
||||
LoadBlock(s,size,Z);
|
||||
return luaS_newlstr(s,size-1);
|
||||
char* s=luaZ_openspace(S->L,S->b,size);
|
||||
LoadBlock(S,s,size);
|
||||
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadLocals (TProtoFunc* tf, ZIO* Z)
|
||||
static void LoadCode(LoadState* S, Proto* f)
|
||||
{
|
||||
int i,n=LoadInt(Z,"too many locals (%ld) in %s");
|
||||
if (n==0) return;
|
||||
tf->locvars=luaM_newvector(n+1,LocVar);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->locvars[i].line=LoadInt(Z,"too many lines (%ld) in %s");
|
||||
tf->locvars[i].varname=LoadTString(Z);
|
||||
}
|
||||
tf->locvars[i].line=-1; /* flag end of vector */
|
||||
tf->locvars[i].varname=NULL;
|
||||
int n=LoadInt(S);
|
||||
f->code=luaM_newvector(S->L,n,Instruction);
|
||||
f->sizecode=n;
|
||||
LoadVector(S,f->code,n,sizeof(Instruction));
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadFunction (ZIO* Z, int native);
|
||||
static Proto* LoadFunction(LoadState* S, TString* p);
|
||||
|
||||
static void LoadConstants (TProtoFunc* tf, ZIO* Z, int native)
|
||||
static void LoadConstants(LoadState* S, Proto* f)
|
||||
{
|
||||
int i,n=LoadInt(Z,"too many constants (%ld) in %s");
|
||||
tf->nconsts=n;
|
||||
if (n==0) return;
|
||||
tf->consts=luaM_newvector(n,TObject);
|
||||
int i,n;
|
||||
n=LoadInt(S);
|
||||
f->k=luaM_newvector(S->L,n,TValue);
|
||||
f->sizek=n;
|
||||
for (i=0; i<n; i++) setnilvalue(&f->k[i]);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
TObject* o=tf->consts+i;
|
||||
ttype(o)=-ezgetc(Z); /* ttype(o) is negative - ORDER LUA_T */
|
||||
switch (ttype(o))
|
||||
TValue* o=&f->k[i];
|
||||
int t=LoadChar(S);
|
||||
switch (t)
|
||||
{
|
||||
case LUA_T_NUMBER:
|
||||
nvalue(o)=LoadNumber(Z,native);
|
||||
case LUA_TNIL:
|
||||
setnilvalue(o);
|
||||
break;
|
||||
case LUA_T_STRING:
|
||||
tsvalue(o)=LoadTString(Z);
|
||||
case LUA_TBOOLEAN:
|
||||
setbvalue(o,LoadChar(S));
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
tfvalue(o)=LoadFunction(Z,native);
|
||||
case LUA_TNUMBER:
|
||||
setnvalue(o,LoadNumber(S));
|
||||
break;
|
||||
case LUA_T_NIL:
|
||||
case LUA_TSTRING:
|
||||
setsvalue2n(S->L,o,LoadString(S));
|
||||
break;
|
||||
default: /* cannot happen */
|
||||
luaU_badconstant("load",i,o,tf);
|
||||
default:
|
||||
IF (1, "bad constant");
|
||||
break;
|
||||
}
|
||||
}
|
||||
n=LoadInt(S);
|
||||
f->p=luaM_newvector(S->L,n,Proto*);
|
||||
f->sizep=n;
|
||||
for (i=0; i<n; i++) f->p[i]=NULL;
|
||||
for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadFunction (ZIO* Z, int native)
|
||||
static void LoadDebug(LoadState* S, Proto* f)
|
||||
{
|
||||
TProtoFunc* tf=luaF_newproto();
|
||||
tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s");
|
||||
tf->source=LoadTString(Z);
|
||||
if (tf->source==NULL) tf->source=luaS_new(zname(Z));
|
||||
tf->code=LoadCode(Z);
|
||||
LoadLocals(tf,Z);
|
||||
LoadConstants(tf,Z,native);
|
||||
return tf;
|
||||
}
|
||||
|
||||
static void LoadSignature (ZIO* Z)
|
||||
{
|
||||
char* s=SIGNATURE;
|
||||
while (*s!=0 && ezgetc(Z)==*s)
|
||||
++s;
|
||||
if (*s!=0) luaL_verror("bad signature in %s",zname(Z));
|
||||
}
|
||||
|
||||
static int LoadHeader (ZIO* Z)
|
||||
{
|
||||
int version,sizeofR;
|
||||
int native;
|
||||
LoadSignature(Z);
|
||||
version=ezgetc(Z);
|
||||
if (version>VERSION)
|
||||
luaL_verror(
|
||||
"%s too new: version=0x%02x; expected at most 0x%02x",
|
||||
zname(Z),version,VERSION);
|
||||
if (version<VERSION0) /* check last major change */
|
||||
luaL_verror(
|
||||
"%s too old: version=0x%02x; expected at least 0x%02x",
|
||||
zname(Z),version,VERSION0);
|
||||
sizeofR=ezgetc(Z);
|
||||
native=(sizeofR!=0);
|
||||
if (native) /* test number representation */
|
||||
int i,n;
|
||||
n=LoadInt(S);
|
||||
f->lineinfo=luaM_newvector(S->L,n,int);
|
||||
f->sizelineinfo=n;
|
||||
LoadVector(S,f->lineinfo,n,sizeof(int));
|
||||
n=LoadInt(S);
|
||||
f->locvars=luaM_newvector(S->L,n,LocVar);
|
||||
f->sizelocvars=n;
|
||||
for (i=0; i<n; i++) f->locvars[i].varname=NULL;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
if (sizeofR!=sizeof(real))
|
||||
luaL_verror("unknown number size in %s: read %d; expected %d",
|
||||
zname(Z),sizeofR,sizeof(real));
|
||||
else
|
||||
{
|
||||
real tf=TEST_NUMBER;
|
||||
real f=LoadNumber(Z,native);
|
||||
if ((long)f!=(long)tf)
|
||||
luaL_verror("unknown number format in %s: "
|
||||
"read " NUMBER_FMT "; expected " NUMBER_FMT,
|
||||
zname(Z),f,tf);
|
||||
}
|
||||
f->locvars[i].varname=LoadString(S);
|
||||
f->locvars[i].startpc=LoadInt(S);
|
||||
f->locvars[i].endpc=LoadInt(S);
|
||||
}
|
||||
return native;
|
||||
n=LoadInt(S);
|
||||
f->upvalues=luaM_newvector(S->L,n,TString*);
|
||||
f->sizeupvalues=n;
|
||||
for (i=0; i<n; i++) f->upvalues[i]=NULL;
|
||||
for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadChunk (ZIO* Z)
|
||||
static Proto* LoadFunction(LoadState* S, TString* p)
|
||||
{
|
||||
return LoadFunction(Z,LoadHeader(Z));
|
||||
Proto* f=luaF_newproto(S->L);
|
||||
setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
|
||||
f->source=LoadString(S); if (f->source==NULL) f->source=p;
|
||||
f->linedefined=LoadInt(S);
|
||||
f->lastlinedefined=LoadInt(S);
|
||||
f->nups=LoadByte(S);
|
||||
f->numparams=LoadByte(S);
|
||||
f->is_vararg=LoadByte(S);
|
||||
f->maxstacksize=LoadByte(S);
|
||||
LoadCode(S,f);
|
||||
LoadConstants(S,f);
|
||||
LoadDebug(S,f);
|
||||
IF (!luaG_checkcode(f), "bad code");
|
||||
S->L->top--;
|
||||
return f;
|
||||
}
|
||||
|
||||
static void LoadHeader(LoadState* S)
|
||||
{
|
||||
char h[LUAC_HEADERSIZE];
|
||||
char s[LUAC_HEADERSIZE];
|
||||
luaU_header(h);
|
||||
LoadBlock(S,s,LUAC_HEADERSIZE);
|
||||
IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
|
||||
}
|
||||
|
||||
/*
|
||||
** load one chunk from a file or buffer
|
||||
** return main if ok and NULL at EOF
|
||||
** load precompiled chunk
|
||||
*/
|
||||
TProtoFunc* luaU_undump1 (ZIO* Z)
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_CHUNK)
|
||||
return LoadChunk(Z);
|
||||
else if (c!=EOZ)
|
||||
luaL_verror("%s is not a Lua binary file",zname(Z));
|
||||
return NULL;
|
||||
LoadState S;
|
||||
if (*name=='@' || *name=='=')
|
||||
S.name=name+1;
|
||||
else if (*name==LUA_SIGNATURE[0])
|
||||
S.name="binary string";
|
||||
else
|
||||
S.name=name;
|
||||
S.L=L;
|
||||
S.Z=Z;
|
||||
S.b=buff;
|
||||
LoadHeader(&S);
|
||||
return LoadFunction(&S,luaS_newliteral(L,"=?"));
|
||||
}
|
||||
|
||||
/*
|
||||
* handle constants that cannot happen
|
||||
* make header
|
||||
*/
|
||||
void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf)
|
||||
void luaU_header (char* h)
|
||||
{
|
||||
int t=ttype(o);
|
||||
char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t];
|
||||
luaL_verror("cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
|
||||
int x=1;
|
||||
memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
|
||||
h+=sizeof(LUA_SIGNATURE)-1;
|
||||
*h++=(char)LUAC_VERSION;
|
||||
*h++=(char)LUAC_FORMAT;
|
||||
*h++=(char)*(char*)&x; /* endianness */
|
||||
*h++=(char)sizeof(int);
|
||||
*h++=(char)sizeof(size_t);
|
||||
*h++=(char)sizeof(Instruction);
|
||||
*h++=(char)sizeof(lua_Number);
|
||||
*h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
|
||||
}
|
||||
|
||||
41
lundump.h
41
lundump.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.15 1999/07/02 19:34:26 lhf Exp $
|
||||
** load pre-compiled Lua chunks
|
||||
** $Id: lundump.h,v 1.39 2005/11/01 17:04:55 lhf Exp lhf $
|
||||
** load precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -10,30 +10,27 @@
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
TProtoFunc* luaU_undump1 (ZIO* Z); /* load one chunk */
|
||||
void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf);
|
||||
/* handle cases that cannot happen */
|
||||
double luaU_str2d (char* b, char* where);
|
||||
/* convert number from text */
|
||||
/* load one chunk; from lundump.c */
|
||||
LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);
|
||||
|
||||
/* definitions for headers of binary files */
|
||||
#define VERSION 0x32 /* last format change was in 3.2 */
|
||||
#define VERSION0 0x32 /* last major change was in 3.2 */
|
||||
#define ID_CHUNK 27 /* binary files start with ESC... */
|
||||
#define SIGNATURE "Lua" /* ...followed by this signature */
|
||||
/* make header; from lundump.c */
|
||||
LUAI_FUNC void luaU_header (char* h);
|
||||
|
||||
/* formats for error messages */
|
||||
#define SOURCE "<%s:%d>"
|
||||
#define IN " in %p " SOURCE
|
||||
#define INLOC tf,tf->source->str,tf->lineDefined
|
||||
/* dump one chunk; from ldump.c */
|
||||
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip);
|
||||
|
||||
/* format for numbers in listings and error messages */
|
||||
#ifndef NUMBER_FMT
|
||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||
#ifdef luac_c
|
||||
/* print one chunk; from print.c */
|
||||
LUAI_FUNC void luaU_print (const Proto* f, int full);
|
||||
#endif
|
||||
|
||||
/* a multiple of PI for testing native format */
|
||||
/* multiplying by 1E8 gives non-trivial integer values */
|
||||
#define TEST_NUMBER 3.14159265358979323846E8
|
||||
/* for header of binary files -- this is Lua 5.1 */
|
||||
#define LUAC_VERSION 0x51
|
||||
|
||||
/* for header of binary files -- this is the official format */
|
||||
#define LUAC_FORMAT 0
|
||||
|
||||
/* size of header of binary files */
|
||||
#define LUAC_HEADERSIZE 12
|
||||
|
||||
#endif
|
||||
|
||||
34
lvm.h
34
lvm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 1.7 1998/12/30 17:26:49 roberto Exp roberto $
|
||||
** $Id: lvm.h,v 2.4 2005/04/25 19:24:10 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,22 +13,24 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (luaV_tonumber(o) != 0))
|
||||
#define tostring(o) ((ttype(o) != LUA_T_STRING) && (luaV_tostring(o) != 0))
|
||||
#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
|
||||
|
||||
#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 (StkId firstel, int nvararg, TObject *tab);
|
||||
int luaV_tonumber (TObject *obj);
|
||||
int luaV_tostring (TObject *obj);
|
||||
void luaV_setn (Hash *t, int val);
|
||||
void luaV_gettable (void);
|
||||
void luaV_settable (TObject *t);
|
||||
void luaV_rawsettable (TObject *t);
|
||||
void luaV_getglobal (TaggedString *ts);
|
||||
void luaV_setglobal (TaggedString *ts);
|
||||
StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base);
|
||||
void luaV_closure (int nelems);
|
||||
void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal,
|
||||
lua_Type ttype_great, IMS op);
|
||||
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
|
||||
LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2);
|
||||
LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n);
|
||||
LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
|
||||
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
|
||||
StkId val);
|
||||
LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
|
||||
StkId val);
|
||||
LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);
|
||||
LUAI_FUNC void luaV_concat (lua_State *L, int total, int last);
|
||||
|
||||
#endif
|
||||
|
||||
104
lzio.c
104
lzio.c
@@ -1,78 +1,65 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.6 1999/03/04 14:49:18 roberto Exp roberto $
|
||||
** $Id: lzio.c,v 1.30 2005/05/17 19:49:15 roberto Exp roberto $
|
||||
** a generic input stream interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lzio_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
/* ----------------------------------------------------- memory buffers --- */
|
||||
|
||||
static int zmfilbuf (ZIO* z) {
|
||||
return EOZ;
|
||||
int luaZ_fill (ZIO *z) {
|
||||
size_t size;
|
||||
lua_State *L = z->L;
|
||||
const char *buff;
|
||||
lua_unlock(L);
|
||||
buff = z->reader(L, z->data, &size);
|
||||
lua_lock(L);
|
||||
if (buff == NULL || size == 0) return EOZ;
|
||||
z->n = size - 1;
|
||||
z->p = buff;
|
||||
return char2int(*(z->p++));
|
||||
}
|
||||
|
||||
|
||||
ZIO* zmopen (ZIO* z, char* b, int size, char *name)
|
||||
{
|
||||
if (b==NULL) return NULL;
|
||||
z->n=size;
|
||||
z->p= (unsigned char *)b;
|
||||
z->filbuf=zmfilbuf;
|
||||
z->u=NULL;
|
||||
z->name=name;
|
||||
return z;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ strings --- */
|
||||
|
||||
ZIO* zsopen (ZIO* z, char* s, 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,z->u);
|
||||
if (n==0) return EOZ;
|
||||
z->n=n-1;
|
||||
z->p=z->buffer;
|
||||
return *(z->p++);
|
||||
int luaZ_lookahead (ZIO *z) {
|
||||
if (z->n == 0) {
|
||||
if (luaZ_fill(z) == EOZ)
|
||||
return EOZ;
|
||||
else {
|
||||
z->n++; /* luaZ_fill removed first byte; put back it */
|
||||
z->p--;
|
||||
}
|
||||
}
|
||||
return char2int(*z->p);
|
||||
}
|
||||
|
||||
|
||||
ZIO* zFopen (ZIO* z, FILE* f, char *name)
|
||||
{
|
||||
if (f==NULL) return NULL;
|
||||
z->n=0;
|
||||
z->p=z->buffer;
|
||||
z->filbuf=zffilbuf;
|
||||
z->u=f;
|
||||
z->name=name;
|
||||
return z;
|
||||
void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
|
||||
z->L = L;
|
||||
z->reader = reader;
|
||||
z->data = data;
|
||||
z->n = 0;
|
||||
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;
|
||||
if (z->n == 0) {
|
||||
if (z->filbuf(z) == EOZ)
|
||||
return n; /* return number of missing bytes */
|
||||
zungetc(z); /* put result from 'filbuf' in the buffer */
|
||||
}
|
||||
size_t m;
|
||||
if (luaZ_lookahead(z) == EOZ)
|
||||
return n; /* return number of missing bytes */
|
||||
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
||||
memcpy(b, z->p, m);
|
||||
z->n -= m;
|
||||
@@ -82,3 +69,14 @@ 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;
|
||||
luaZ_resizebuffer(L, buff, n);
|
||||
}
|
||||
return buff->buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
67
lzio.h
67
lzio.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.h,v 1.3 1997/12/22 20:57:18 roberto Exp roberto $
|
||||
** $Id: lzio.h,v 1.20 2005/04/25 19:24:10 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,43 +8,60 @@
|
||||
#ifndef lzio_h
|
||||
#define lzio_h
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lua.h"
|
||||
|
||||
#include "lmem.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, char *name); /* open FILEs */
|
||||
ZIO* zsopen (ZIO* z, char* s, char *name); /* string */
|
||||
ZIO* zmopen (ZIO* z, char* b, int size, char *name); /* memory */
|
||||
#define char2int(c) cast(int, cast(unsigned char, (c)))
|
||||
|
||||
int zread (ZIO* z, void* b, int n); /* read next n bytes */
|
||||
#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
|
||||
|
||||
typedef struct Mbuffer {
|
||||
char *buffer;
|
||||
size_t n;
|
||||
size_t buffsize;
|
||||
} Mbuffer;
|
||||
|
||||
#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
|
||||
|
||||
#define luaZ_buffer(buff) ((buff)->buffer)
|
||||
#define luaZ_sizebuffer(buff) ((buff)->buffsize)
|
||||
#define luaZ_bufflen(buff) ((buff)->n)
|
||||
|
||||
#define luaZ_resetbuffer(buff) ((buff)->n = 0)
|
||||
|
||||
|
||||
#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)
|
||||
|
||||
|
||||
LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
|
||||
LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
|
||||
void *data);
|
||||
LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
|
||||
LUAI_FUNC int luaZ_lookahead (ZIO *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)
|
||||
|
||||
|
||||
/* --------- Private Part ------------------ */
|
||||
|
||||
#define ZBSIZE 256 /* buffer size */
|
||||
|
||||
struct zio {
|
||||
int n; /* bytes still unread */
|
||||
unsigned char* p; /* current position in buffer */
|
||||
int (*filbuf)(ZIO* z);
|
||||
void* u; /* additional data */
|
||||
char *name;
|
||||
unsigned char buffer[ZBSIZE]; /* buffer */
|
||||
struct Zio {
|
||||
size_t n; /* bytes still unread */
|
||||
const char *p; /* current position in buffer */
|
||||
lua_Reader reader;
|
||||
void* data; /* additional data */
|
||||
lua_State *L; /* Lua state (for reader) */
|
||||
};
|
||||
|
||||
|
||||
LUAI_FUNC int luaZ_fill (ZIO *z);
|
||||
|
||||
#endif
|
||||
|
||||
247
makefile
247
makefile
@@ -1,140 +1,155 @@
|
||||
#
|
||||
## $Id: makefile,v 1.18 1999/02/23 15:01:29 roberto Exp roberto $
|
||||
## Makefile
|
||||
## See Copyright Notice in lua.h
|
||||
#
|
||||
# makefile for building Lua
|
||||
# see INSTALL for installation instructions
|
||||
# see ../Makefile and luaconf.h for further customization
|
||||
|
||||
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
|
||||
|
||||
CWARNS= -pedantic -Waggregate-return -Wcast-align \
|
||||
-Wmissing-prototypes -Wpointer-arith -Wshadow \
|
||||
-Wsign-compare -Wstrict-prototypes -Wundef -Wwrite-strings
|
||||
# -Wcast-qual
|
||||
|
||||
# -DEXTERNMEMCHECK -DHARDSTACKTESTS
|
||||
# -g -DLUA_USER_H='"ltests.h"'
|
||||
# -fomit-frame-pointer #-pg -malign-double
|
||||
TESTS= -g -DLUA_USER_H='"ltests.h"'
|
||||
|
||||
LOCAL = $(TESTS) $(CWARNS)
|
||||
|
||||
|
||||
#CONFIGURATION
|
||||
CC= gcc
|
||||
CFLAGS= -Wall $(MYCFLAGS) -O2
|
||||
AR= ar rcu
|
||||
RANLIB= ranlib
|
||||
RM= rm -f
|
||||
|
||||
# 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_COMPAT2_5 if yous system does need to be compatible with
|
||||
# version 2.5 (or older)
|
||||
#
|
||||
# define LUA_NUM_TYPE if you need numbers to be different from double
|
||||
# (for instance, -DLUA_NUM_TYPE=float)
|
||||
#
|
||||
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||
#CONFIG = -DLUA_COMPAT2_5 -DOLD_ANSI -DDEBUG
|
||||
MYCFLAGS= $(LOCAL)
|
||||
MYLDFLAGS=
|
||||
MYLIBS=
|
||||
|
||||
|
||||
# Compilation parameters
|
||||
CC = gcc
|
||||
CWARNS = -Wall -Wmissing-prototypes -Wshadow -pedantic -Wpointer-arith -Wcast-align -Waggregate-return
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O2
|
||||
# enable Linux goodies
|
||||
MYCFLAGS= $(LOCAL) -DLUA_USE_LINUX
|
||||
MYLDFLAGS= -Wl,-E
|
||||
MYLIBS= -ldl -lreadline -lhistory -lncurses
|
||||
|
||||
|
||||
# To make early versions
|
||||
CO_OPTIONS =
|
||||
|
||||
# == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE =========
|
||||
|
||||
|
||||
AR = ar
|
||||
ARFLAGS = rvl
|
||||
LIBS = -lm
|
||||
|
||||
CORE_T= liblua.a
|
||||
CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
|
||||
lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
|
||||
lundump.o lvm.o lzio.o ltests.o
|
||||
AUX_O= lauxlib.o
|
||||
LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \
|
||||
loadlib.o linit.o
|
||||
|
||||
# Aplication modules
|
||||
LUAOBJS = \
|
||||
lapi.o \
|
||||
lauxlib.o \
|
||||
lbuffer.o \
|
||||
lbuiltin.o \
|
||||
ldo.o \
|
||||
lfunc.o \
|
||||
lgc.o \
|
||||
llex.o \
|
||||
lmem.o \
|
||||
lobject.o \
|
||||
lparser.o \
|
||||
lstate.o \
|
||||
lstring.o \
|
||||
ltable.o \
|
||||
ltm.o \
|
||||
lvm.o \
|
||||
lundump.o \
|
||||
lzio.o
|
||||
LUA_T= lua
|
||||
LUA_O= lua.o
|
||||
|
||||
LIBOBJS = \
|
||||
liolib.o \
|
||||
lmathlib.o \
|
||||
lstrlib.o \
|
||||
ldblib.o \
|
||||
linit.o
|
||||
LUAC_T= luac
|
||||
LUAC_O= luac.o print.o
|
||||
|
||||
ALL_T= $(CORE_T) $(LUA_T) $(LUAC_T)
|
||||
ALL_O= $(CORE_O) $(LUA_O) $(LUAC_O) $(AUX_O) $(LIB_O)
|
||||
ALL_A= $(CORE_T)
|
||||
|
||||
lua : lua.o liblua.a liblualib.a
|
||||
$(CC) $(CFLAGS) -o $@ lua.o -L. -llua -llualib -lm
|
||||
all: $(ALL_T)
|
||||
|
||||
liblua.a : $(LUAOBJS)
|
||||
$(AR) $(ARFLAGS) $@ $?
|
||||
ranlib $@
|
||||
o: $(ALL_O)
|
||||
|
||||
liblualib.a : $(LIBOBJS)
|
||||
$(AR) $(ARFLAGS) $@ $?
|
||||
ranlib $@
|
||||
a: $(ALL_A)
|
||||
|
||||
liblua.so.1.0 : lua.o
|
||||
ld -o liblua.so.1.0 $(LUAOBJS)
|
||||
$(CORE_T): $(CORE_O) $(AUX_O) $(LIB_O)
|
||||
$(AR) $@ $?
|
||||
$(RANLIB) $@
|
||||
|
||||
$(LUA_T): $(LUA_O) $(CORE_T)
|
||||
$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL)
|
||||
|
||||
clear :
|
||||
rcsclean
|
||||
rm -f *.o
|
||||
rm -f
|
||||
co $(CO_OPTIONS) lua.h lualib.h luadebug.h
|
||||
$(LUAC_T): $(LUAC_O) $(CORE_T)
|
||||
$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(CORE_T) $(LIBS) $(MYLIBS)
|
||||
|
||||
clean:
|
||||
rcsclean -u
|
||||
$(RM) $(ALL_T) $(ALL_O)
|
||||
|
||||
%.h : RCS/%.h,v
|
||||
co $(CO_OPTIONS) $@
|
||||
depend:
|
||||
@$(CC) $(CFLAGS) -MM *.c
|
||||
|
||||
%.c : RCS/%.c,v
|
||||
co $(CO_OPTIONS) $@
|
||||
echo:
|
||||
@echo "CC = $(CC)"
|
||||
@echo "CFLAGS = $(CFLAGS)"
|
||||
@echo "AR = $(AR)"
|
||||
@echo "RANLIB = $(RANLIB)"
|
||||
@echo "RM = $(RM)"
|
||||
@echo "MYCFLAGS = $(MYCFLAGS)"
|
||||
@echo "MYLDFLAGS = $(MYLDFLAGS)"
|
||||
@echo "MYLIBS = $(MYLIBS)"
|
||||
@echo "DL = $(DL)"
|
||||
|
||||
# DO NOT DELETE
|
||||
|
||||
lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lstate.h \
|
||||
luadebug.h lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h lvm.h
|
||||
lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h
|
||||
lbuffer.o: lbuffer.c lauxlib.h lua.h lmem.h lstate.h lobject.h \
|
||||
luadebug.h
|
||||
lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.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
|
||||
ldblib.o: ldblib.c lauxlib.h lua.h luadebug.h lualib.h
|
||||
ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h luadebug.h lfunc.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 lua.h lmem.h lstate.h luadebug.h
|
||||
lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h luadebug.h lfunc.h lgc.h \
|
||||
lmem.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 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 lstate.h lobject.h lua.h luadebug.h
|
||||
lobject.o: lobject.c lobject.h lua.h
|
||||
lparser.o: lparser.c lauxlib.h lua.h ldo.h lobject.h lstate.h \
|
||||
luadebug.h lfunc.h llex.h lzio.h lmem.h lopcodes.h lparser.h \
|
||||
lstring.h
|
||||
lstate.o: lstate.c lbuiltin.h ldo.h lobject.h lua.h lstate.h \
|
||||
luadebug.h lfunc.h lgc.h llex.h lzio.h lmem.h lstring.h ltable.h \
|
||||
ltm.h
|
||||
lstring.o: lstring.c lmem.h lobject.h lua.h lstate.h 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 lstate.h \
|
||||
luadebug.h ltable.h
|
||||
ltm.o: ltm.c lauxlib.h lua.h lmem.h lobject.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 lmem.h \
|
||||
lstring.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lstate.h luadebug.h \
|
||||
lfunc.h lgc.h lmem.h lopcodes.h lstring.h ltable.h ltm.h lvm.h
|
||||
lzio.o: lzio.c lzio.h
|
||||
lapi.o: lapi.c lua.h luaconf.h lapi.h lobject.h llimits.h ldebug.h \
|
||||
lstate.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h \
|
||||
lundump.h lvm.h
|
||||
lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h
|
||||
lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
|
||||
lzio.h lmem.h lopcodes.h lparser.h ltable.h ldebug.h lstate.h ltm.h \
|
||||
ldo.h lgc.h
|
||||
ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
ldebug.o: ldebug.c lua.h luaconf.h lapi.h lobject.h llimits.h lcode.h \
|
||||
llex.h lzio.h lmem.h lopcodes.h lparser.h ltable.h ldebug.h lstate.h \
|
||||
ltm.h ldo.h lfunc.h lstring.h lgc.h lvm.h
|
||||
ldo.o: ldo.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
||||
lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h ltable.h \
|
||||
lstring.h lundump.h lvm.h
|
||||
ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lopcodes.h lstate.h \
|
||||
ltm.h lzio.h lmem.h lundump.h
|
||||
lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h lmem.h \
|
||||
lstate.h ltm.h lzio.h
|
||||
lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
||||
lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
|
||||
linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h
|
||||
liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
llex.o: llex.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h ltm.h \
|
||||
lzio.h lmem.h llex.h lparser.h ltable.h lstring.h lgc.h
|
||||
lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
|
||||
ltm.h lzio.h lmem.h ldo.h
|
||||
loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \
|
||||
ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h
|
||||
lopcodes.o: lopcodes.c lua.h luaconf.h lobject.h llimits.h lopcodes.h
|
||||
loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
|
||||
lzio.h lmem.h lopcodes.h lparser.h ltable.h ldebug.h lstate.h ltm.h \
|
||||
ldo.h lfunc.h lstring.h lgc.h
|
||||
lstate.o: lstate.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
|
||||
ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h ltable.h
|
||||
lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \
|
||||
ltm.h lzio.h lstring.h lgc.h
|
||||
lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
|
||||
ltm.h lzio.h lmem.h ldo.h lgc.h ltable.h
|
||||
ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
ltests.o: ltests.c lua.h luaconf.h lapi.h lobject.h llimits.h lauxlib.h \
|
||||
lcode.h llex.h lzio.h lmem.h lopcodes.h lparser.h ltable.h ldebug.h \
|
||||
lstate.h ltm.h ldo.h lfunc.h lstring.h lgc.h lualib.h
|
||||
ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \
|
||||
lmem.h lstring.h lgc.h ltable.h
|
||||
lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lopcodes.h lstring.h lgc.h \
|
||||
lundump.h
|
||||
lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
||||
lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
|
||||
lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \
|
||||
lzio.h
|
||||
|
||||
# (end of Makefile)
|
||||
|
||||
3294
manual.tex
3294
manual.tex
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user