mirror of
https://github.com/lua/lua.git
synced 2026-07-27 16:39:06 +00:00
Compare commits
316 Commits
v5.2-alpha
...
v5.2.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78d9865900 | ||
|
|
cc2a60ecb7 | ||
|
|
43bfb60ac8 | ||
|
|
c510d94549 | ||
|
|
4dd0622d0c | ||
|
|
ab2c3d5cde | ||
|
|
01e5f358bd | ||
|
|
69c775de21 | ||
|
|
6fcdfc6d4f | ||
|
|
b77a90681e | ||
|
|
6efffb9acd | ||
|
|
51e8f08e60 | ||
|
|
5adb5a4812 | ||
|
|
e29f3a5751 | ||
|
|
21ed264a38 | ||
|
|
e131eb0d9a | ||
|
|
054c20cd5b | ||
|
|
8e7149f496 | ||
|
|
086da10dac | ||
|
|
b36b4b521f | ||
|
|
398811a313 | ||
|
|
2a66b34f72 | ||
|
|
8d0e1ed52f | ||
|
|
3ac595da8a | ||
|
|
ea980fdeef | ||
|
|
5654763797 | ||
|
|
edfa9e007d | ||
|
|
31829ad177 | ||
|
|
c2361e0b61 | ||
|
|
77cbd817d1 | ||
|
|
4ec7d6de95 | ||
|
|
f6a12948ca | ||
|
|
3cadc37f47 | ||
|
|
bb1146dc39 | ||
|
|
fc24a3a6c4 | ||
|
|
a94ce68e0a | ||
|
|
ec22fc963a | ||
|
|
c54f5f64c9 | ||
|
|
4cca1a436d | ||
|
|
8df0198178 | ||
|
|
048be9cf27 | ||
|
|
1485ea2ee7 | ||
|
|
b10dbe5c72 | ||
|
|
462375ba47 | ||
|
|
cfbe2333a4 | ||
|
|
afc3fcf952 | ||
|
|
99bd1a000f | ||
|
|
3e66d3b4be | ||
|
|
29a28693e5 | ||
|
|
678c1255c9 | ||
|
|
a4b96ce9a3 | ||
|
|
291f564485 | ||
|
|
9f1a8dbdd3 | ||
|
|
d19f1da6ef | ||
|
|
f88806a1fb | ||
|
|
fb6eee2805 | ||
|
|
0010ec68b1 | ||
|
|
28f8ed9113 | ||
|
|
fd22ccd6d0 | ||
|
|
76eab106df | ||
|
|
81ed85ecfb | ||
|
|
7dcc02b165 | ||
|
|
e2fc2ce8df | ||
|
|
a241b6cb3c | ||
|
|
f0d143f30f | ||
|
|
c82087b8dd | ||
|
|
138be5b843 | ||
|
|
e320b5b672 | ||
|
|
ca9c405fef | ||
|
|
e2feb886d6 | ||
|
|
5999e14ad9 | ||
|
|
01747f2c24 | ||
|
|
7377fbdebc | ||
|
|
f1d2ac3a98 | ||
|
|
c5da4f4cd0 | ||
|
|
7bcb2462e4 | ||
|
|
0bd99b327b | ||
|
|
6d8b672094 | ||
|
|
e21b26a964 | ||
|
|
0f388193b3 | ||
|
|
7948b8568e | ||
|
|
61b1d9a466 | ||
|
|
3617e04e97 | ||
|
|
8c62bde36f | ||
|
|
51650eac78 | ||
|
|
aea68e5c54 | ||
|
|
ece042fef0 | ||
|
|
217b65e6d9 | ||
|
|
8119374e74 | ||
|
|
3a997eefb5 | ||
|
|
5184ff2096 | ||
|
|
b0cbe77816 | ||
|
|
6e899850b9 | ||
|
|
fded7bef19 | ||
|
|
b5d7255034 | ||
|
|
6cce5c0601 | ||
|
|
eb70f58279 | ||
|
|
ef900f224c | ||
|
|
3105febf9a | ||
|
|
7133e20c94 | ||
|
|
888d39ea75 | ||
|
|
eef22fa4ce | ||
|
|
eda87f1156 | ||
|
|
e0e406ead3 | ||
|
|
30cea671a7 | ||
|
|
a716ae1f50 | ||
|
|
ea382b0be1 | ||
|
|
6d8d282e0f | ||
|
|
00c8a17a2d | ||
|
|
166ce15512 | ||
|
|
87cf2bf711 | ||
|
|
fd8a849621 | ||
|
|
5a568a3b01 | ||
|
|
475e6c5352 | ||
|
|
af00a0772c | ||
|
|
6819c2a98a | ||
|
|
1dd8af67b6 | ||
|
|
1350a2bcb5 | ||
|
|
217e67cb22 | ||
|
|
9bbfe9f3fd | ||
|
|
788a0ecd27 | ||
|
|
6d86b937e4 | ||
|
|
bc90cf4b83 | ||
|
|
d79190e27f | ||
|
|
9caf5b4f79 | ||
|
|
50cddefeeb | ||
|
|
e24f1ee9ff | ||
|
|
f3b0eb3ecf | ||
|
|
162ce8256e | ||
|
|
ad90447eb6 | ||
|
|
87912786af | ||
|
|
5645a84368 | ||
|
|
95ed6c1127 | ||
|
|
12ab78aca6 | ||
|
|
45c346645c | ||
|
|
68ce878292 | ||
|
|
500336efd0 | ||
|
|
5ea8f108e1 | ||
|
|
2275030b86 | ||
|
|
05de314701 | ||
|
|
d281d23f8d | ||
|
|
ec431ce5db | ||
|
|
c8f47c2934 | ||
|
|
ca458f1f52 | ||
|
|
121dc8af66 | ||
|
|
a8675966ec | ||
|
|
3dc5475e23 | ||
|
|
8a008a2057 | ||
|
|
89b59eee73 | ||
|
|
166dd0261a | ||
|
|
51471ba748 | ||
|
|
0b65cb74cd | ||
|
|
ea46b750dc | ||
|
|
92afcf2823 | ||
|
|
b5bf7d9ef4 | ||
|
|
6a5d89b39f | ||
|
|
ae14adc272 | ||
|
|
ff98f17d27 | ||
|
|
59bcd137ae | ||
|
|
5ab6d36d99 | ||
|
|
43c873895f | ||
|
|
03a078493e | ||
|
|
bae57ea088 | ||
|
|
2557107e91 | ||
|
|
32a12e2f3f | ||
|
|
559bb554c9 | ||
|
|
fd5e810e08 | ||
|
|
067f761739 | ||
|
|
7978a8d8b2 | ||
|
|
32e09c2a16 | ||
|
|
df19931ddc | ||
|
|
b5084fdafe | ||
|
|
d8200d5f3c | ||
|
|
79cbc3468c | ||
|
|
9e89a55a4f | ||
|
|
b707e81c3d | ||
|
|
ee37ee50d6 | ||
|
|
ad1a54b5c0 | ||
|
|
88c29c0cd5 | ||
|
|
d93df22bf6 | ||
|
|
5017cc6ba2 | ||
|
|
2f125ebc33 | ||
|
|
49a01c0991 | ||
|
|
17526ed2fe | ||
|
|
f180822fa6 | ||
|
|
594220c624 | ||
|
|
3dd68624dc | ||
|
|
f9eea453b7 | ||
|
|
374773748b | ||
|
|
29644366fc | ||
|
|
6eadedbfa1 | ||
|
|
1978094b3a | ||
|
|
5acc5b0c1e | ||
|
|
59547c6cda | ||
|
|
3d3355b4f3 | ||
|
|
801f43f09a | ||
|
|
719c01359f | ||
|
|
43f13729a2 | ||
|
|
136f8e148d | ||
|
|
f90a130b07 | ||
|
|
cb4950a6d8 | ||
|
|
fdede85419 | ||
|
|
beab626061 | ||
|
|
817f8674af | ||
|
|
cf0562e1e7 | ||
|
|
22e1c5ebad | ||
|
|
470dd56a89 | ||
|
|
20d30bcd33 | ||
|
|
44b6f496b5 | ||
|
|
fd80e63468 | ||
|
|
f62565abea | ||
|
|
c9d1d9f9c6 | ||
|
|
35391d9b1a | ||
|
|
99b1b8e918 | ||
|
|
3b44821334 | ||
|
|
0214dab989 | ||
|
|
821bd7025e | ||
|
|
9b7dddad7d | ||
|
|
3f04a9f2c0 | ||
|
|
2a59a999d4 | ||
|
|
ee7d0c2649 | ||
|
|
aa13c591f5 | ||
|
|
6c8a32217a | ||
|
|
cb09f4fef4 | ||
|
|
3cf1729a02 | ||
|
|
74b0bb3a45 | ||
|
|
e4076fe6d9 | ||
|
|
81952e6884 | ||
|
|
25b4e35ee8 | ||
|
|
7e1facaa4e | ||
|
|
ad2531a0ee | ||
|
|
bc1c718cc0 | ||
|
|
c9ce754e38 | ||
|
|
d34271fe42 | ||
|
|
d120ec29ca | ||
|
|
2aff901c93 | ||
|
|
bad231c481 | ||
|
|
a2b1c279f1 | ||
|
|
2f128c5130 | ||
|
|
4758113043 | ||
|
|
a4e644add2 | ||
|
|
05ca63642b | ||
|
|
f17e3624ef | ||
|
|
2e9b8476ca | ||
|
|
c31f4946e9 | ||
|
|
c86586e866 | ||
|
|
19fbdf6cae | ||
|
|
427ee519db | ||
|
|
a958b711f5 | ||
|
|
1ffdd0bc7a | ||
|
|
5286650894 | ||
|
|
0fb1644c60 | ||
|
|
119d5e46d5 | ||
|
|
83abbac946 | ||
|
|
a25416a8d5 | ||
|
|
682cce3813 | ||
|
|
d806710ab5 | ||
|
|
e049abb69a | ||
|
|
98816d0ce5 | ||
|
|
7482e8f914 | ||
|
|
03b769053a | ||
|
|
3c710f056b | ||
|
|
c0a865fa54 | ||
|
|
0009ac1f3a | ||
|
|
7b968bb514 | ||
|
|
bf8b08295a | ||
|
|
3f5b56c48b | ||
|
|
e7192dfdbb | ||
|
|
0539f48661 | ||
|
|
e7a9c45a48 | ||
|
|
fd6c1f4898 | ||
|
|
f079749287 | ||
|
|
f8d677f94c | ||
|
|
094a7d0290 | ||
|
|
7cc0e63d8a | ||
|
|
a4a8914c20 | ||
|
|
06c7efb4a9 | ||
|
|
dd547c55c8 | ||
|
|
f6bd8b1147 | ||
|
|
a7faa557fa | ||
|
|
026a7e92be | ||
|
|
c4ea0c3b29 | ||
|
|
7106c491dd | ||
|
|
a10d495b18 | ||
|
|
c2e3cc4c89 | ||
|
|
67feed49f1 | ||
|
|
b63b0928cf | ||
|
|
511679313f | ||
|
|
868ff40339 | ||
|
|
aa6faa6331 | ||
|
|
0b3f4e254e | ||
|
|
551b076f1c | ||
|
|
737f119187 | ||
|
|
8980c630bf | ||
|
|
8d579c5bc5 | ||
|
|
d51743b0c7 | ||
|
|
1a92c2b354 | ||
|
|
fe0d54d37e | ||
|
|
c9af0768fc | ||
|
|
7e0caa7d61 | ||
|
|
a40768e5ea | ||
|
|
4a83fe569c | ||
|
|
b83b6ba015 | ||
|
|
af119c8b55 | ||
|
|
1aead7f553 | ||
|
|
75d8470f0f | ||
|
|
81646af13b | ||
|
|
233b71c092 | ||
|
|
ccc4fc9cf0 | ||
|
|
c79b4a97aa | ||
|
|
ac3afc7cd4 | ||
|
|
350cc4bcb6 | ||
|
|
35931bbed4 | ||
|
|
12779b2b71 | ||
|
|
7ca1bd639f | ||
|
|
9b7a12c46d |
384
bugs
384
bugs
@@ -1880,8 +1880,8 @@ patch = [[
|
||||
+++ lundump.c 2008/04/04 19:51:41 2.7.1.4
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
-** $Id: bugs,v 1.107 2010/04/19 18:04:58 roberto Exp roberto $
|
||||
+** $Id: bugs,v 1.107 2010/04/19 18:04:58 roberto Exp roberto $
|
||||
-** $Id: bugs,v 1.114 2012/05/11 14:10:28 roberto Exp roberto $
|
||||
+** $Id: bugs,v 1.114 2012/05/11 14:10:28 roberto Exp roberto $
|
||||
** load precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -2316,3 +2316,383 @@ patch = [[
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what = [[wrong code generation for some particular boolean expressions]],
|
||||
report = [[Thierry Van Elsuwe, 2011/01/20]],
|
||||
since = [[5.0]],
|
||||
example = [[
|
||||
print((('hi' or true) and true) or true)
|
||||
--> hi (should be true)
|
||||
print(((nil and nil) or false) and true)
|
||||
--> nil (should be false)
|
||||
]],
|
||||
patch = [[
|
||||
--- lcode.c 2009/06/15 14:12:25 2.25.1.4
|
||||
+++ lcode.c 2011/01/31 14:44:25
|
||||
@@ -549,13 +549,6 @@
|
||||
pc = e->u.s.info;
|
||||
break;
|
||||
}
|
||||
- case VFALSE: {
|
||||
- if (!hasjumps(e)) {
|
||||
- pc = luaK_jump(fs); /* always jump */
|
||||
- break;
|
||||
- }
|
||||
- /* else go through */
|
||||
- }
|
||||
default: {
|
||||
pc = jumponcond(fs, e, 0);
|
||||
break;
|
||||
@@ -579,13 +572,6 @@
|
||||
pc = e->u.s.info;
|
||||
break;
|
||||
}
|
||||
- case VTRUE: {
|
||||
- if (!hasjumps(e)) {
|
||||
- pc = luaK_jump(fs); /* always jump */
|
||||
- break;
|
||||
- }
|
||||
- /* else go through */
|
||||
- }
|
||||
default: {
|
||||
pc = jumponcond(fs, e, 1);
|
||||
break;
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what = [[__newindex metamethod may not work if metatable is its own
|
||||
metatable]],
|
||||
report = [[Cuero Bugot, 2011/08/09]],
|
||||
since = [[5.1]],
|
||||
example = [[
|
||||
meta={}
|
||||
setmetatable(meta, meta)
|
||||
meta.__newindex = function(t, key, value) print("set") end
|
||||
o = setmetatable({}, meta)
|
||||
o.x = 10 -- should print 'set'
|
||||
]],
|
||||
patch = [[
|
||||
--- lvm.c 2009/07/01 21:10:33 2.63.1.4
|
||||
+++ lvm.c 2011/08/17 20:36:28
|
||||
@@ -142,6 +142,7 @@
|
||||
if (!ttisnil(oldval) || /* result is no nil? */
|
||||
(tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
|
||||
setobj2t(L, oldval, val);
|
||||
+ h->flags = 0;
|
||||
luaC_barriert(L, h, val);
|
||||
return;
|
||||
}
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what = [[parser may collect a prototype while building it]],
|
||||
report = [[Ingo van Lil, 2011/10/13]],
|
||||
since = [[5.1.4 (caused by patch 5.1.4-6)]],
|
||||
example = nil,
|
||||
patch = [[
|
||||
--- lparser.c 2007/12/28 15:32:23 2.42.1.3
|
||||
+++ lparser.c 2011/10/17 13:10:43
|
||||
@@ -374,9 +374,9 @@
|
||||
lua_assert(luaG_checkcode(f));
|
||||
lua_assert(fs->bl == NULL);
|
||||
ls->fs = fs->prev;
|
||||
- L->top -= 2; /* remove table and prototype from the stack */
|
||||
/* last token read was anchored in defunct function; must reanchor it */
|
||||
if (fs) anchor_token(ls);
|
||||
+ L->top -= 2; /* remove table and prototype from the stack */
|
||||
}
|
||||
|
||||
|
||||
]]
|
||||
}
|
||||
|
||||
|
||||
-----------------------------------------------------------------
|
||||
-- Lua 5.2.0
|
||||
|
||||
Bug{
|
||||
what = [[memory hoarding when creating Lua hooks for coroutines]],
|
||||
report = [[Arseny Vakhrushev, 2012/01/16]],
|
||||
since = [[5.1]],
|
||||
example = [[
|
||||
collectgarbage(); print(collectgarbage'count' * 1024)
|
||||
|
||||
for i = 1, 100 do
|
||||
local co = coroutine.create(function () end)
|
||||
local x = {}
|
||||
for j=1,1000 do x[j] = j end
|
||||
debug.sethook(co, function () return x end, 'l')
|
||||
end
|
||||
|
||||
collectgarbage(); print(collectgarbage'count' * 1024)
|
||||
-- value should back to near the original level
|
||||
]],
|
||||
patch = [[
|
||||
-- For 5.2
|
||||
|
||||
--- ldblib.c 2011/10/24 14:54:05 1.131
|
||||
+++ ldblib.c 2012/01/18 02:36:59
|
||||
@@ -253,14 +253,15 @@
|
||||
}
|
||||
|
||||
|
||||
-#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY);
|
||||
+#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
|
||||
|
||||
|
||||
static void hookf (lua_State *L, lua_Debug *ar) {
|
||||
static const char *const hooknames[] =
|
||||
{"call", "return", "line", "count", "tail call"};
|
||||
gethooktable(L);
|
||||
- lua_rawgetp(L, -1, L);
|
||||
+ lua_pushthread(L);
|
||||
+ lua_rawget(L, -2);
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushstring(L, hooknames[(int)ar->event]);
|
||||
if (ar->currentline >= 0)
|
||||
@@ -306,10 +307,15 @@
|
||||
count = luaL_optint(L, arg+3, 0);
|
||||
func = hookf; mask = makemask(smask, count);
|
||||
}
|
||||
- gethooktable(L);
|
||||
+ if (gethooktable(L) == 0) { /* creating hook table? */
|
||||
+ lua_pushstring(L, "k");
|
||||
+ lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
||||
+ lua_pushvalue(L, -1);
|
||||
+ lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
|
||||
+ }
|
||||
+ lua_pushthread(L1); lua_xmove(L1, L, 1);
|
||||
lua_pushvalue(L, arg+1);
|
||||
- lua_rawsetp(L, -2, L1); /* set new hook */
|
||||
- lua_pop(L, 1); /* remove hook table */
|
||||
+ lua_rawset(L, -3); /* set new hook */
|
||||
lua_sethook(L1, func, mask, count); /* set hooks */
|
||||
return 0;
|
||||
}
|
||||
@@ -325,7 +331,8 @@
|
||||
lua_pushliteral(L, "external hook");
|
||||
else {
|
||||
gethooktable(L);
|
||||
- lua_rawgetp(L, -1, L1); /* get hook */
|
||||
+ lua_pushthread(L1); lua_xmove(L1, L, 1);
|
||||
+ lua_rawget(L, -2); /* get hook */
|
||||
lua_remove(L, -2); /* remove hook table */
|
||||
}
|
||||
lua_pushstring(L, unmakemask(mask, buff));
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what = [[Lexical gets confused with some combination of arithmetic
|
||||
operators and hexadecimal numbers]],
|
||||
report = [[Alexandra Barros, 2012/01/17]],
|
||||
since = [[5.2.0]],
|
||||
example = [[print(0xE+1)]],
|
||||
patch = [[
|
||||
--- llex.c 2011/11/30 12:43:51 2.59
|
||||
+++ llex.c 2012/01/20 18:22:50
|
||||
@@ -223,12 +223,19 @@
|
||||
|
||||
/* LUA_NUMBER */
|
||||
static void read_numeral (LexState *ls, SemInfo *seminfo) {
|
||||
+ const char *expo = "Ee";
|
||||
+ int first = ls->current;
|
||||
lua_assert(lisdigit(ls->current));
|
||||
- do {
|
||||
- save_and_next(ls);
|
||||
- if (check_next(ls, "EePp")) /* exponent part? */
|
||||
+ save_and_next(ls);
|
||||
+ if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */
|
||||
+ expo = "Pp";
|
||||
+ for (;;) {
|
||||
+ if (check_next(ls, expo)) /* exponent part? */
|
||||
check_next(ls, "+-"); /* optional exponent sign */
|
||||
- } while (lislalnum(ls->current) || ls->current == '.');
|
||||
+ if (lisxdigit(ls->current) || ls->current == '.')
|
||||
+ save_and_next(ls);
|
||||
+ else break;
|
||||
+ }
|
||||
save(ls, '\0');
|
||||
buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
|
||||
if (!buff2d(ls->buff, &seminfo->r)) /* format error? */
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what = [[Finalizers may call functions from a dynamic library after
|
||||
the library has been unloaded]],
|
||||
report = [[Josh Haberman, 2012/04/08]],
|
||||
since = [[5.1]],
|
||||
example = [[
|
||||
local u = setmetatable({}, {__gc = function () foo() end})
|
||||
local m = require 'mod' -- 'mod' may be any dynamic library written in C
|
||||
foo = m.foo -- 'foo' may be any function from 'mod'
|
||||
-- end program; it crashes
|
||||
]],
|
||||
patch = [[
|
||||
loadlib.c:
|
||||
95c95
|
||||
< #define LIBPREFIX "LOADLIB: "
|
||||
---
|
||||
> #define CLIBS "_CLIBS"
|
||||
251,266c251,256
|
||||
<
|
||||
< 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); /* remove result from gettable */
|
||||
< plib = (void **)lua_newuserdata(L, sizeof(const void *));
|
||||
< *plib = NULL;
|
||||
< luaL_setmetatable(L, "_LOADLIB");
|
||||
< lua_pushfstring(L, "%s%s", LIBPREFIX, path);
|
||||
< lua_pushvalue(L, -2);
|
||||
< lua_settable(L, LUA_REGISTRYINDEX);
|
||||
< }
|
||||
---
|
||||
> static void *ll_checkclib (lua_State *L, const char *path) {
|
||||
> void *plib;
|
||||
> lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
> lua_getfield(L, -1, path);
|
||||
> plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
||||
> lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
||||
270a261,270
|
||||
> static void ll_addtoclib (lua_State *L, const char *path, void *plib) {
|
||||
> lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
> lua_pushlightuserdata(L, plib);
|
||||
> lua_pushvalue(L, -1);
|
||||
> lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
||||
> lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
|
||||
> lua_pop(L, 1); /* pop CLIBS table */
|
||||
> }
|
||||
>
|
||||
>
|
||||
272,273c272,273
|
||||
< ** __gc tag method: calls library's `ll_unloadlib' function with the lib
|
||||
< ** handle
|
||||
---
|
||||
> ** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib
|
||||
> ** handles in list CLIBS
|
||||
276,278c276,281
|
||||
< void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
|
||||
< if (*lib) ll_unloadlib(*lib);
|
||||
< *lib = NULL; /* mark library as closed */
|
||||
---
|
||||
> int n = luaL_len(L, 1);
|
||||
> for (; n >= 1; n--) { /* for each handle, in reverse order */
|
||||
> lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
|
||||
> ll_unloadlib(lua_touserdata(L, -1));
|
||||
> lua_pop(L, 1); /* pop handle */
|
||||
> }
|
||||
284,286c287,292
|
||||
< void **reg = ll_register(L, path);
|
||||
< if (*reg == NULL) *reg = ll_load(L, path, *sym == '*');
|
||||
< if (*reg == NULL) return ERRLIB; /* unable to load library */
|
||||
---
|
||||
> void *reg = ll_checkclib(L, path); /* check loaded C libraries */
|
||||
> if (reg == NULL) { /* must load library? */
|
||||
> reg = ll_load(L, path, *sym == '*');
|
||||
> if (reg == NULL) return ERRLIB; /* unable to load library */
|
||||
> ll_addtoclib(L, path, reg);
|
||||
> }
|
||||
292c298
|
||||
< lua_CFunction f = ll_sym(L, *reg, sym);
|
||||
---
|
||||
> lua_CFunction f = ll_sym(L, reg, sym);
|
||||
675,676c681,683
|
||||
< /* create new type _LOADLIB */
|
||||
< luaL_newmetatable(L, "_LOADLIB");
|
||||
---
|
||||
> /* create table CLIBS to keep track of loaded C libraries */
|
||||
> luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
> lua_createtable(L, 0, 1); /* metatable for CLIBS */
|
||||
678a686
|
||||
> lua_setmetatable(L, -2);
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what = [[wrong handling of 'nCcalls' in coroutines]],
|
||||
report = [[Alexander Gavrilov, 2012/04/18]],
|
||||
since = [[5.2.0]],
|
||||
example = [[
|
||||
coroutine.wrap(function()
|
||||
print(pcall(pcall,pcall,pcall,pcall,pcall,error,3))
|
||||
end)()
|
||||
]],
|
||||
patch = [[
|
||||
--- ldo.c 2011/11/29 15:55:08 2.102
|
||||
+++ ldo.c 2012/04/26 20:38:32
|
||||
@@ -402,8 +402,6 @@
|
||||
int n;
|
||||
lua_assert(ci->u.c.k != NULL); /* must have a continuation */
|
||||
lua_assert(L->nny == 0);
|
||||
- /* finish 'luaD_call' */
|
||||
- L->nCcalls--;
|
||||
/* finish 'lua_callk' */
|
||||
adjustresults(L, ci->nresults);
|
||||
/* call continuation function */
|
||||
@@ -513,7 +511,6 @@
|
||||
api_checknelems(L, n);
|
||||
firstArg = L->top - n; /* yield results come from continuation */
|
||||
}
|
||||
- L->nCcalls--; /* finish 'luaD_call' */
|
||||
luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
|
||||
}
|
||||
unroll(L, NULL);
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what = [[Internal Lua values may escape through the debug API]],
|
||||
report = [[Dan Tull, 2012/04/20]],
|
||||
since = [[5.1]],
|
||||
example = [[
|
||||
-- for Lua 5.1
|
||||
local firsttime = true
|
||||
local function foo ()
|
||||
if firsttime then
|
||||
firsttime = false
|
||||
return "a = 1"
|
||||
else
|
||||
for i = 1, 10 do
|
||||
print(debug.getlocal(2, i))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
print(load(foo)) -- prints some lines and then seg. fault.
|
||||
]],
|
||||
patch = [[
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what = [[Problems when yielding from debug hooks]],
|
||||
report = [[Erik Cassel, 2012/06/05]],
|
||||
since = [[5.2.0]],
|
||||
example = [[
|
||||
Set, in C, a line hook that simply yields,
|
||||
and then call any Lua function.
|
||||
You get an infinite loop of yields.
|
||||
]],
|
||||
patch = [[
|
||||
]]
|
||||
}
|
||||
|
||||
|
||||
--[=[
|
||||
Bug{
|
||||
what = [[ ]],
|
||||
report = [[ ]],
|
||||
since = [[ ]],
|
||||
example = [[ ]],
|
||||
patch = [[
|
||||
]]
|
||||
}
|
||||
]=]
|
||||
|
||||
289
lapi.c
289
lapi.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 2.139 2010/10/25 20:31:11 roberto Exp roberto $
|
||||
** $Id: lapi.c,v 2.163 2012/05/23 15:42:27 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -34,9 +34,13 @@ const char lua_ident[] =
|
||||
"$LuaAuthors: " LUA_AUTHORS " $";
|
||||
|
||||
|
||||
/* value at a non-valid index */
|
||||
#define NONVALIDVALUE cast(TValue *, luaO_nilobject)
|
||||
|
||||
#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject, \
|
||||
"invalid index")
|
||||
/* corresponding test */
|
||||
#define isvalid(o) ((o) != luaO_nilobject)
|
||||
|
||||
#define api_checkvalidindex(L, i) api_check(L, isvalid(i), "invalid index")
|
||||
|
||||
|
||||
static TValue *index2addr (lua_State *L, int idx) {
|
||||
@@ -44,7 +48,7 @@ static TValue *index2addr (lua_State *L, int idx) {
|
||||
if (idx > 0) {
|
||||
TValue *o = ci->func + idx;
|
||||
api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
|
||||
if (o >= L->top) return cast(TValue *, luaO_nilobject);
|
||||
if (o >= L->top) return NONVALIDVALUE;
|
||||
else return o;
|
||||
}
|
||||
else if (idx > LUA_REGISTRYINDEX) {
|
||||
@@ -57,12 +61,10 @@ static TValue *index2addr (lua_State *L, int idx) {
|
||||
idx = LUA_REGISTRYINDEX - idx;
|
||||
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
|
||||
if (ttislcf(ci->func)) /* light C function? */
|
||||
return cast(TValue *, luaO_nilobject); /* it has no upvalues */
|
||||
return NONVALIDVALUE; /* it has no upvalues */
|
||||
else {
|
||||
Closure *func = clvalue(ci->func);
|
||||
return (idx <= func->c.nupvalues)
|
||||
? &func->c.upvalue[idx-1]
|
||||
: cast(TValue *, luaO_nilobject);
|
||||
CClosure *func = clCvalue(ci->func);
|
||||
return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -195,10 +197,8 @@ static void moveto (lua_State *L, TValue *fr, int idx) {
|
||||
TValue *to = index2addr(L, idx);
|
||||
api_checkvalidindex(L, to);
|
||||
setobj(L, to, fr);
|
||||
if (idx < LUA_REGISTRYINDEX) { /* function upvalue? */
|
||||
lua_assert(ttisclosure(L->ci->func));
|
||||
luaC_barrier(L, clvalue(L->ci->func), fr);
|
||||
}
|
||||
if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
|
||||
luaC_barrier(L, clCvalue(L->ci->func), fr);
|
||||
/* LUA_REGISTRYINDEX does not need gc barrier
|
||||
(collector revisits it before finishing collection) */
|
||||
}
|
||||
@@ -239,7 +239,7 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) {
|
||||
|
||||
LUA_API int lua_type (lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
return (o == luaO_nilobject) ? LUA_TNONE : ttypenv(o);
|
||||
return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
|
||||
}
|
||||
|
||||
|
||||
@@ -251,7 +251,7 @@ LUA_API const char *lua_typename (lua_State *L, int t) {
|
||||
|
||||
LUA_API int lua_iscfunction (lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
return (ttislcf(o) || (ttisclosure(o) && clvalue(o)->c.isC));
|
||||
return (ttislcf(o) || (ttisCclosure(o)));
|
||||
}
|
||||
|
||||
|
||||
@@ -277,21 +277,28 @@ LUA_API int lua_isuserdata (lua_State *L, int idx) {
|
||||
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
|
||||
StkId o1 = index2addr(L, index1);
|
||||
StkId o2 = index2addr(L, index2);
|
||||
return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
|
||||
: luaO_rawequalObj(o1, o2);
|
||||
return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_arith (lua_State *L, int op) {
|
||||
StkId o1; /* 1st operand */
|
||||
StkId o2; /* 2nd operand */
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
if (ttisnumber(L->top - 2) && ttisnumber(L->top - 1)) {
|
||||
changenvalue(L->top - 2,
|
||||
luaO_arith(op, nvalue(L->top - 2), nvalue(L->top - 1)));
|
||||
if (op != LUA_OPUNM) /* all other operations expect two operands */
|
||||
api_checknelems(L, 2);
|
||||
else { /* for unary minus, add fake 2nd operand */
|
||||
api_checknelems(L, 1);
|
||||
setobjs2s(L, L->top, L->top - 1);
|
||||
L->top++;
|
||||
}
|
||||
o1 = L->top - 2;
|
||||
o2 = L->top - 1;
|
||||
if (ttisnumber(o1) && ttisnumber(o2)) {
|
||||
changenvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
|
||||
}
|
||||
else
|
||||
luaV_arith(L, L->top - 2, L->top - 2, L->top - 1,
|
||||
cast(TMS, op - LUA_OPADD + TM_ADD));
|
||||
luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
@@ -299,17 +306,17 @@ LUA_API void lua_arith (lua_State *L, int op) {
|
||||
|
||||
LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
|
||||
StkId o1, o2;
|
||||
int i;
|
||||
int i = 0;
|
||||
lua_lock(L); /* may call tag method */
|
||||
o1 = index2addr(L, index1);
|
||||
o2 = index2addr(L, index2);
|
||||
if (o1 == luaO_nilobject || o2 == luaO_nilobject)
|
||||
i = 0;
|
||||
else switch (op) {
|
||||
case LUA_OPEQ: i = equalobj(L, o1, o2); break;
|
||||
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
|
||||
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
|
||||
default: api_check(L, 0, "invalid option"); i = 0;
|
||||
if (isvalid(o1) && isvalid(o2)) {
|
||||
switch (op) {
|
||||
case LUA_OPEQ: i = equalobj(L, o1, o2); break;
|
||||
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
|
||||
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
|
||||
default: api_check(L, 0, "invalid option");
|
||||
}
|
||||
}
|
||||
lua_unlock(L);
|
||||
return i;
|
||||
@@ -390,7 +397,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
|
||||
LUA_API size_t lua_rawlen (lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
switch (ttype(o)) {
|
||||
switch (ttypenv(o)) {
|
||||
case LUA_TSTRING: return tsvalue(o)->len;
|
||||
case LUA_TUSERDATA: return uvalue(o)->len;
|
||||
case LUA_TTABLE: return luaH_getn(hvalue(o));
|
||||
@@ -402,15 +409,15 @@ LUA_API size_t lua_rawlen (lua_State *L, int idx) {
|
||||
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
if (ttislcf(o)) return fvalue(o);
|
||||
else if (ttisclosure(o) && clvalue(o)->c.isC)
|
||||
return clvalue(o)->c.f;
|
||||
else if (ttisCclosure(o))
|
||||
return clCvalue(o)->f;
|
||||
else return NULL; /* not a C function */
|
||||
}
|
||||
|
||||
|
||||
LUA_API void *lua_touserdata (lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
switch (ttype(o)) {
|
||||
switch (ttypenv(o)) {
|
||||
case LUA_TUSERDATA: return (rawuvalue(o) + 1);
|
||||
case LUA_TLIGHTUSERDATA: return pvalue(o);
|
||||
default: return NULL;
|
||||
@@ -428,7 +435,8 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
switch (ttype(o)) {
|
||||
case LUA_TTABLE: return hvalue(o);
|
||||
case LUA_TFUNCTION: return clvalue(o);
|
||||
case LUA_TLCL: return clLvalue(o);
|
||||
case LUA_TCCL: return clCvalue(o);
|
||||
case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
|
||||
case LUA_TTHREAD: return thvalue(o);
|
||||
case LUA_TUSERDATA:
|
||||
@@ -456,6 +464,8 @@ LUA_API void lua_pushnil (lua_State *L) {
|
||||
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
||||
lua_lock(L);
|
||||
setnvalue(L->top, n);
|
||||
luai_checknum(L, L->top,
|
||||
luaG_runerror(L, "C API - attempt to push a signaling NaN"));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
@@ -548,7 +558,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
L->top -= n;
|
||||
while (n--)
|
||||
setobj2n(L, &cl->c.upvalue[n], L->top + n);
|
||||
setclvalue(L, L->top, cl);
|
||||
setclCvalue(L, L->top, cl);
|
||||
}
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
@@ -586,6 +596,17 @@ LUA_API int lua_pushthread (lua_State *L) {
|
||||
*/
|
||||
|
||||
|
||||
LUA_API void lua_getglobal (lua_State *L, const char *var) {
|
||||
Table *reg = hvalue(&G(L)->l_registry);
|
||||
const TValue *gt; /* global table */
|
||||
lua_lock(L);
|
||||
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
||||
setsvalue2s(L, L->top++, luaS_new(L, var));
|
||||
luaV_gettable(L, gt, L->top - 1, L->top - 1);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_gettable (lua_State *L, int idx) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
@@ -619,11 +640,24 @@ LUA_API void lua_rawget (lua_State *L, int idx) {
|
||||
|
||||
|
||||
LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
|
||||
StkId o;
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
o = index2addr(L, idx);
|
||||
api_check(L, ttistable(o), "table expected");
|
||||
setobj2s(L, L->top, luaH_getint(hvalue(o), n));
|
||||
t = index2addr(L, idx);
|
||||
api_check(L, ttistable(t), "table expected");
|
||||
setobj2s(L, L->top, luaH_getint(hvalue(t), n));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
|
||||
StkId t;
|
||||
TValue k;
|
||||
lua_lock(L);
|
||||
t = index2addr(L, idx);
|
||||
api_check(L, ttistable(t), "table expected");
|
||||
setpvalue(&k, cast(void *, p));
|
||||
setobj2s(L, L->top, luaH_get(hvalue(t), &k));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
@@ -648,7 +682,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
||||
int res;
|
||||
lua_lock(L);
|
||||
obj = index2addr(L, objindex);
|
||||
switch (ttype(obj)) {
|
||||
switch (ttypenv(obj)) {
|
||||
case LUA_TTABLE:
|
||||
mt = hvalue(obj)->metatable;
|
||||
break;
|
||||
@@ -691,6 +725,19 @@ LUA_API void lua_getuservalue (lua_State *L, int idx) {
|
||||
*/
|
||||
|
||||
|
||||
LUA_API void lua_setglobal (lua_State *L, const char *var) {
|
||||
Table *reg = hvalue(&G(L)->l_registry);
|
||||
const TValue *gt; /* global table */
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
||||
setsvalue2s(L, L->top++, luaS_new(L, var));
|
||||
luaV_settable(L, gt, L->top - 1, L->top - 2);
|
||||
L->top -= 2; /* pop value and key */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_settable (lua_State *L, int idx) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
@@ -723,6 +770,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) {
|
||||
t = index2addr(L, idx);
|
||||
api_check(L, ttistable(t), "table expected");
|
||||
setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
|
||||
invalidateTMcache(hvalue(t));
|
||||
luaC_barrierback(L, gcvalue(t), L->top-1);
|
||||
L->top -= 2;
|
||||
lua_unlock(L);
|
||||
@@ -730,13 +778,28 @@ LUA_API void lua_rawset (lua_State *L, int idx) {
|
||||
|
||||
|
||||
LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
|
||||
StkId o;
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
o = index2addr(L, idx);
|
||||
api_check(L, ttistable(o), "table expected");
|
||||
setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
|
||||
luaC_barrierback(L, gcvalue(o), L->top-1);
|
||||
t = index2addr(L, idx);
|
||||
api_check(L, ttistable(t), "table expected");
|
||||
luaH_setint(L, hvalue(t), n, L->top - 1);
|
||||
luaC_barrierback(L, gcvalue(t), L->top-1);
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
||||
StkId t;
|
||||
TValue k;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
t = index2addr(L, idx);
|
||||
api_check(L, ttistable(t), "table expected");
|
||||
setpvalue(&k, cast(void *, p));
|
||||
setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
|
||||
luaC_barrierback(L, gcvalue(t), L->top - 1);
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
@@ -755,18 +818,19 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
||||
api_check(L, ttistable(L->top - 1), "table expected");
|
||||
mt = hvalue(L->top - 1);
|
||||
}
|
||||
switch (ttype(obj)) {
|
||||
switch (ttypenv(obj)) {
|
||||
case LUA_TTABLE: {
|
||||
hvalue(obj)->metatable = mt;
|
||||
if (mt)
|
||||
luaC_objbarrierback(L, gcvalue(obj), mt);
|
||||
luaC_checkfinalizer(L, gcvalue(obj), mt);
|
||||
break;
|
||||
}
|
||||
case LUA_TUSERDATA: {
|
||||
uvalue(obj)->metatable = mt;
|
||||
if (mt) {
|
||||
luaC_objbarrier(L, rawuvalue(obj), mt);
|
||||
luaC_checkfinalizer(L, rawuvalue(obj));
|
||||
luaC_checkfinalizer(L, gcvalue(obj), mt);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -886,7 +950,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
||||
ci->u.c.k = k; /* save continuation */
|
||||
ci->u.c.ctx = ctx; /* save context */
|
||||
/* save information for error recovery */
|
||||
ci->u.c.extra = savestack(L, c.func);
|
||||
ci->extra = savestack(L, c.func);
|
||||
ci->u.c.old_allowhook = L->allowhook;
|
||||
ci->u.c.old_errfunc = L->errfunc;
|
||||
L->errfunc = func;
|
||||
@@ -904,23 +968,22 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
||||
|
||||
|
||||
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
||||
const char *chunkname) {
|
||||
const char *chunkname, const char *mode) {
|
||||
ZIO z;
|
||||
int status;
|
||||
lua_lock(L);
|
||||
if (!chunkname) chunkname = "?";
|
||||
luaZ_init(L, &z, reader, data);
|
||||
status = luaD_protectedparser(L, &z, chunkname);
|
||||
status = luaD_protectedparser(L, &z, chunkname, mode);
|
||||
if (status == LUA_OK) { /* no errors? */
|
||||
Closure *f = clvalue(L->top - 1); /* get newly created function */
|
||||
lua_assert(!f->c.isC);
|
||||
if (f->l.nupvalues == 1) { /* does it have one upvalue? */
|
||||
LClosure *f = clLvalue(L->top - 1); /* get newly created function */
|
||||
if (f->nupvalues == 1) { /* does it have one upvalue? */
|
||||
/* get global table from registry */
|
||||
Table *reg = hvalue(&G(L)->l_registry);
|
||||
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
||||
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
|
||||
setobj(L, f->l.upvals[0]->v, gt);
|
||||
luaC_barrier(L, f->l.upvals[0], gt);
|
||||
setobj(L, f->upvals[0]->v, gt);
|
||||
luaC_barrier(L, f->upvals[0], gt);
|
||||
}
|
||||
}
|
||||
lua_unlock(L);
|
||||
@@ -959,11 +1022,12 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
|
||||
g = G(L);
|
||||
switch (what) {
|
||||
case LUA_GCSTOP: {
|
||||
stopgc(g);
|
||||
g->gcrunning = 0;
|
||||
break;
|
||||
}
|
||||
case LUA_GCRESTART: {
|
||||
g->GCdebt = 0;
|
||||
luaE_setdebt(g, 0);
|
||||
g->gcrunning = 1;
|
||||
break;
|
||||
}
|
||||
case LUA_GCCOLLECT: {
|
||||
@@ -972,30 +1036,27 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
|
||||
}
|
||||
case LUA_GCCOUNT: {
|
||||
/* GC values are expressed in Kbytes: #bytes/2^10 */
|
||||
res = cast_int(g->totalbytes >> 10);
|
||||
res = cast_int(gettotalbytes(g) >> 10);
|
||||
break;
|
||||
}
|
||||
case LUA_GCCOUNTB: {
|
||||
res = cast_int(g->totalbytes & 0x3ff);
|
||||
res = cast_int(gettotalbytes(g) & 0x3ff);
|
||||
break;
|
||||
}
|
||||
case LUA_GCSTEP: {
|
||||
int stopped = gcstopped(g);
|
||||
if (g->gckind == KGC_GEN) { /* generational mode? */
|
||||
res = (g->lastmajormem == 0); /* 1 if will do major collection */
|
||||
luaC_step(L); /* do a single step */
|
||||
res = (g->GCestimate == 0); /* true if it will do major collection */
|
||||
luaC_forcestep(L); /* do a single step */
|
||||
}
|
||||
else {
|
||||
while (data-- >= 0) {
|
||||
luaC_step(L);
|
||||
if (g->gcstate == GCSpause) { /* end of cycle? */
|
||||
res = 1; /* signal it */
|
||||
break;
|
||||
}
|
||||
}
|
||||
lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
|
||||
if (g->gcrunning)
|
||||
debt += g->GCdebt; /* include current debt */
|
||||
luaE_setdebt(g, debt);
|
||||
luaC_forcestep(L);
|
||||
if (g->gcstate == GCSpause) /* end of cycle? */
|
||||
res = 1; /* signal it */
|
||||
}
|
||||
if (stopped) /* collector was stopped? */
|
||||
stopgc(g); /* keep it that way */
|
||||
break;
|
||||
}
|
||||
case LUA_GCSETPAUSE: {
|
||||
@@ -1014,7 +1075,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
|
||||
break;
|
||||
}
|
||||
case LUA_GCISRUNNING: {
|
||||
res = !gcstopped(g);
|
||||
res = g->gcrunning;
|
||||
break;
|
||||
}
|
||||
case LUA_GCGEN: { /* change collector to generational mode */
|
||||
@@ -1121,35 +1182,34 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
|
||||
|
||||
|
||||
|
||||
|
||||
static const char *aux_upvalue (StkId fi, int n, TValue **val,
|
||||
GCObject **owner) {
|
||||
Closure *f;
|
||||
if (!ttisclosure(fi)) return NULL;
|
||||
f = clvalue(fi);
|
||||
if (f->c.isC) {
|
||||
if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
|
||||
*val = &f->c.upvalue[n-1];
|
||||
if (owner) *owner = obj2gco(f);
|
||||
return "";
|
||||
}
|
||||
else {
|
||||
const char *name;
|
||||
Proto *p = f->l.p;
|
||||
if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
|
||||
*val = f->l.upvals[n-1]->v;
|
||||
if (owner) *owner = obj2gco(f->l.upvals[n - 1]);
|
||||
name = getstr(p->upvalues[n-1].name);
|
||||
if (name == NULL) /* no debug information? */
|
||||
name = "";
|
||||
return name;
|
||||
switch (ttype(fi)) {
|
||||
case LUA_TCCL: { /* C closure */
|
||||
CClosure *f = clCvalue(fi);
|
||||
if (!(1 <= n && n <= f->nupvalues)) return NULL;
|
||||
*val = &f->upvalue[n-1];
|
||||
if (owner) *owner = obj2gco(f);
|
||||
return "";
|
||||
}
|
||||
case LUA_TLCL: { /* Lua closure */
|
||||
LClosure *f = clLvalue(fi);
|
||||
TString *name;
|
||||
Proto *p = f->p;
|
||||
if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
|
||||
*val = f->upvals[n-1]->v;
|
||||
if (owner) *owner = obj2gco(f->upvals[n - 1]);
|
||||
name = p->upvalues[n-1].name;
|
||||
return (name == NULL) ? "" : getstr(name);
|
||||
}
|
||||
default: return NULL; /* not a closure */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
|
||||
const char *name;
|
||||
TValue *val;
|
||||
TValue *val = NULL; /* to avoid warnings */
|
||||
lua_lock(L);
|
||||
name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
|
||||
if (name) {
|
||||
@@ -1163,8 +1223,8 @@ 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) {
|
||||
const char *name;
|
||||
TValue *val;
|
||||
GCObject *owner;
|
||||
TValue *val = NULL; /* to avoid warnings */
|
||||
GCObject *owner = NULL; /* to avoid warnings */
|
||||
StkId fi;
|
||||
lua_lock(L);
|
||||
fi = index2addr(L, funcindex);
|
||||
@@ -1180,34 +1240,39 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
|
||||
}
|
||||
|
||||
|
||||
static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
|
||||
Closure *f;
|
||||
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
|
||||
LClosure *f;
|
||||
StkId fi = index2addr(L, fidx);
|
||||
api_check(L, ttisclosure(fi), "Lua function expected");
|
||||
f = clvalue(fi);
|
||||
api_check(L, !f->c.isC, "Lua function expected");
|
||||
api_check(L, (1 <= n && n <= f->l.p->sizeupvalues), "invalid upvalue index");
|
||||
api_check(L, ttisLclosure(fi), "Lua function expected");
|
||||
f = clLvalue(fi);
|
||||
api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
|
||||
if (pf) *pf = f;
|
||||
return &f->l.upvals[n - 1]; /* get its upvalue pointer */
|
||||
return &f->upvals[n - 1]; /* get its upvalue pointer */
|
||||
}
|
||||
|
||||
|
||||
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
|
||||
Closure *f;
|
||||
StkId fi = index2addr(L, fidx);
|
||||
api_check(L, ttisclosure(fi), "function expected");
|
||||
f = clvalue(fi);
|
||||
if (f->c.isC) {
|
||||
api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
|
||||
return &f->c.upvalue[n - 1];
|
||||
switch (ttype(fi)) {
|
||||
case LUA_TLCL: { /* lua closure */
|
||||
return *getupvalref(L, fidx, n, NULL);
|
||||
}
|
||||
case LUA_TCCL: { /* C closure */
|
||||
CClosure *f = clCvalue(fi);
|
||||
api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
|
||||
return &f->upvalue[n - 1];
|
||||
}
|
||||
default: {
|
||||
api_check(L, 0, "closure expected");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else return *getupvalref(L, fidx, n, NULL);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
|
||||
int fidx2, int n2) {
|
||||
Closure *f1;
|
||||
LClosure *f1;
|
||||
UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
|
||||
UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
|
||||
*up1 = *up2;
|
||||
|
||||
139
lauxlib.c
139
lauxlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.226 2010/11/10 17:38:10 roberto Exp roberto $
|
||||
** $Id: lauxlib.c,v 1.243 2012/04/20 17:05:17 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -41,11 +41,10 @@
|
||||
** return 1 + string at top if find a good name.
|
||||
*/
|
||||
static int findfield (lua_State *L, int objidx, int level) {
|
||||
int found = 0;
|
||||
if (level == 0 || !lua_istable(L, -1))
|
||||
return 0; /* not found */
|
||||
lua_pushnil(L); /* start 'next' loop */
|
||||
while (!found && lua_next(L, -2)) { /* for each pair in table */
|
||||
while (lua_next(L, -2)) { /* for each pair in table */
|
||||
if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
|
||||
if (lua_rawequal(L, objidx, -1)) { /* found object? */
|
||||
lua_pop(L, 1); /* remove value (but keep name) */
|
||||
@@ -86,7 +85,7 @@ static void pushfuncname (lua_State *L, lua_Debug *ar) {
|
||||
lua_pushfstring(L, "function " LUA_QS, ar->name);
|
||||
else if (*ar->what == 'm') /* main? */
|
||||
lua_pushfstring(L, "main chunk");
|
||||
else if (*ar->what == 'C' || *ar->what == 't') {
|
||||
else if (*ar->what == 'C') {
|
||||
if (pushglobalfuncname(L, ar)) {
|
||||
lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
|
||||
lua_remove(L, -2); /* remove name */
|
||||
@@ -203,6 +202,63 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
|
||||
return lua_error(L);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
|
||||
int en = errno; /* calls to Lua API may change this value */
|
||||
if (stat) {
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
if (fname)
|
||||
lua_pushfstring(L, "%s: %s", fname, strerror(en));
|
||||
else
|
||||
lua_pushfstring(L, "%s", strerror(en));
|
||||
lua_pushinteger(L, en);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if !defined(inspectstat) /* { */
|
||||
|
||||
#if defined(LUA_USE_POSIX)
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
/*
|
||||
** use appropriate macros to interpret 'pclose' return status
|
||||
*/
|
||||
#define inspectstat(stat,what) \
|
||||
if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
|
||||
else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
|
||||
|
||||
#else
|
||||
|
||||
#define inspectstat(stat,what) /* no op */
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* } */
|
||||
|
||||
|
||||
LUALIB_API int luaL_execresult (lua_State *L, int stat) {
|
||||
const char *what = "exit"; /* type of termination */
|
||||
if (stat == -1) /* error? */
|
||||
return luaL_fileresult(L, 0, NULL);
|
||||
else {
|
||||
inspectstat(stat, what); /* interpret result */
|
||||
if (*what == 'e' && stat == 0) /* successful termination? */
|
||||
lua_pushboolean(L, 1);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, what);
|
||||
lua_pushinteger(L, stat);
|
||||
return 3; /* return true/nil,what,code */
|
||||
}
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
@@ -213,7 +269,7 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
|
||||
*/
|
||||
|
||||
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
|
||||
luaL_getmetatable(L, tname); /* try to get metatable */
|
||||
if (!lua_isnil(L, -1)) /* name already in use? */
|
||||
return 0; /* leave previous value on top, but return 0 */
|
||||
lua_pop(L, 1);
|
||||
@@ -234,7 +290,7 @@ LUALIB_API void *luaL_testudata (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 */
|
||||
luaL_getmetatable(L, tname); /* get correct metatable */
|
||||
if (!lua_rawequal(L, -1, -2)) /* not the same? */
|
||||
p = NULL; /* value is a userdata with wrong metatable */
|
||||
lua_pop(L, 2); /* remove both metatables */
|
||||
@@ -274,7 +330,9 @@ LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
|
||||
if (!lua_checkstack(L, space)) {
|
||||
/* keep some extra space to run error routines, if needed */
|
||||
const int extra = LUA_MINSTACK;
|
||||
if (!lua_checkstack(L, space + extra)) {
|
||||
if (msg)
|
||||
luaL_error(L, "stack overflow (%s)", msg);
|
||||
else
|
||||
@@ -384,8 +442,10 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
|
||||
newsize = B->n + sz;
|
||||
if (newsize < B->n || newsize - B->n < sz)
|
||||
luaL_error(L, "buffer too large");
|
||||
newbuff = (char *)lua_newuserdata(L, newsize); /* create larger buffer */
|
||||
memcpy(newbuff, B->b, B->n); /* move content to new buffer */
|
||||
/* create larger buffer */
|
||||
newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));
|
||||
/* move content to new buffer */
|
||||
memcpy(newbuff, B->b, B->n * sizeof(char));
|
||||
if (buffonstack(B))
|
||||
lua_remove(L, -2); /* remove old buffer */
|
||||
B->b = newbuff;
|
||||
@@ -397,7 +457,7 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
|
||||
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
|
||||
char *b = luaL_prepbuffsize(B, l);
|
||||
memcpy(b, s, l);
|
||||
memcpy(b, s, l * sizeof(char));
|
||||
luaL_addsize(B, l);
|
||||
}
|
||||
|
||||
@@ -460,11 +520,11 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
|
||||
|
||||
LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
int ref;
|
||||
t = lua_absindex(L, t);
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1); /* remove from stack */
|
||||
return LUA_REFNIL; /* `nil' has a unique fixed reference */
|
||||
}
|
||||
t = lua_absindex(L, t);
|
||||
lua_rawgeti(L, t, freelist); /* get first free element */
|
||||
ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
|
||||
lua_pop(L, 1); /* remove it from stack */
|
||||
@@ -507,7 +567,7 @@ typedef struct LoadF {
|
||||
|
||||
static const char *getF (lua_State *L, void *ud, size_t *size) {
|
||||
LoadF *lf = (LoadF *)ud;
|
||||
(void)L;
|
||||
(void)L; /* not used */
|
||||
if (lf->n > 0) { /* are there pre-read characters to be read? */
|
||||
*size = lf->n; /* return them (chars already in buffer) */
|
||||
lf->n = 0; /* no more pre-read characters */
|
||||
@@ -556,15 +616,18 @@ static int skipBOM (LoadF *lf) {
|
||||
static int skipcomment (LoadF *lf, int *cp) {
|
||||
int c = *cp = skipBOM(lf);
|
||||
if (c == '#') { /* first line is a comment (Unix exec. file)? */
|
||||
while ((c = getc(lf->f)) != EOF && c != '\n') ; /* skip first line */
|
||||
*cp = getc(lf->f); /* skip end-of-line */
|
||||
do { /* skip first line */
|
||||
c = getc(lf->f);
|
||||
} while (c != EOF && c != '\n') ;
|
||||
*cp = getc(lf->f); /* skip end-of-line, if present */
|
||||
return 1; /* there was a comment */
|
||||
}
|
||||
else return 0; /* no comment */
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
|
||||
LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
|
||||
const char *mode) {
|
||||
LoadF lf;
|
||||
int status, readstatus;
|
||||
int c;
|
||||
@@ -587,7 +650,7 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
|
||||
}
|
||||
if (c != EOF)
|
||||
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
|
||||
status = lua_load(L, getF, &lf, lua_tostring(L, -1));
|
||||
status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
|
||||
readstatus = ferror(lf.f);
|
||||
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
||||
if (readstatus) {
|
||||
@@ -607,7 +670,7 @@ typedef struct LoadS {
|
||||
|
||||
static const char *getS (lua_State *L, void *ud, size_t *size) {
|
||||
LoadS *ls = (LoadS *)ud;
|
||||
(void)L;
|
||||
(void)L; /* not used */
|
||||
if (ls->size == 0) return NULL;
|
||||
*size = ls->size;
|
||||
ls->size = 0;
|
||||
@@ -615,12 +678,12 @@ static const char *getS (lua_State *L, void *ud, size_t *size) {
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name) {
|
||||
LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
|
||||
const char *name, const char *mode) {
|
||||
LoadS ls;
|
||||
ls.s = buff;
|
||||
ls.size = size;
|
||||
return lua_load(L, getS, &ls, name);
|
||||
return lua_load(L, getS, &ls, name, mode);
|
||||
}
|
||||
|
||||
|
||||
@@ -700,8 +763,8 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
*/
|
||||
#if defined(LUA_COMPAT_MODULE)
|
||||
|
||||
static const char *luaL_findtablex (lua_State *L, int idx,
|
||||
const char *fname, int szhint) {
|
||||
static const char *luaL_findtable (lua_State *L, int idx,
|
||||
const char *fname, int szhint) {
|
||||
const char *e;
|
||||
if (idx) lua_pushvalue(L, idx);
|
||||
do {
|
||||
@@ -745,13 +808,13 @@ static int libsize (const luaL_Reg *l) {
|
||||
*/
|
||||
LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
|
||||
int sizehint) {
|
||||
luaL_findtablex(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
|
||||
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
|
||||
lua_getfield(L, -1, 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) */
|
||||
lua_pushglobaltable(L);
|
||||
if (luaL_findtablex(L, 0, modname, sizehint) != NULL)
|
||||
if (luaL_findtable(L, 0, modname, sizehint) != NULL)
|
||||
luaL_error(L, "name conflict for module " LUA_QS, modname);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
|
||||
@@ -767,7 +830,10 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
|
||||
luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
|
||||
lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
|
||||
}
|
||||
luaL_setfuncs(L, l, nup);
|
||||
if (l)
|
||||
luaL_setfuncs(L, l, nup);
|
||||
else
|
||||
lua_pop(L, nup); /* remove upvalues */
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -779,8 +845,9 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
|
||||
** Returns with only the table at the stack.
|
||||
*/
|
||||
LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
||||
luaL_checkversion(L);
|
||||
luaL_checkstack(L, nup, "too many upvalues");
|
||||
for (; l && l->name; l++) { /* fill the table with given functions */
|
||||
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
||||
int i;
|
||||
for (i = 0; i < nup; i++) /* copy upvalues to the top */
|
||||
lua_pushvalue(L, -nup);
|
||||
@@ -795,15 +862,16 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
||||
** ensure that stack[idx][fname] has a table and push that table
|
||||
** into the stack
|
||||
*/
|
||||
LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) {
|
||||
LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
|
||||
lua_getfield(L, idx, fname);
|
||||
if (lua_istable(L, -1)) return; /* table already there */
|
||||
if (lua_istable(L, -1)) return 1; /* table already there */
|
||||
else {
|
||||
idx = lua_absindex(L, idx);
|
||||
lua_pop(L, 1); /* remove previous result */
|
||||
idx = lua_absindex(L, idx);
|
||||
lua_newtable(L);
|
||||
lua_pushvalue(L, -1); /* copy to be left at top */
|
||||
lua_setfield(L, idx, fname); /* assign new table to field */
|
||||
return 0; /* false, because did not find table there */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -819,15 +887,13 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
|
||||
lua_pushcfunction(L, openf);
|
||||
lua_pushstring(L, modname); /* argument to open function */
|
||||
lua_call(L, 1, 1); /* open module */
|
||||
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
lua_pushvalue(L, -2); /* make copy of module (call result) */
|
||||
lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
|
||||
lua_pop(L, 1); /* remove _LOADED table */
|
||||
if (glb) {
|
||||
lua_pushglobaltable(L);
|
||||
lua_pushvalue(L, -2); /* copy of 'mod' */
|
||||
lua_setfield(L, -2, modname); /* _G[modname] = module */
|
||||
lua_pop(L, 1); /* remove _G table */
|
||||
lua_pushvalue(L, -1); /* copy of 'mod' */
|
||||
lua_setglobal(L, modname); /* _G[modname] = module */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -850,8 +916,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
|
||||
|
||||
|
||||
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
(void)ud;
|
||||
(void)osize;
|
||||
(void)ud; (void)osize; /* not used */
|
||||
if (nsize == 0) {
|
||||
free(ptr);
|
||||
return NULL;
|
||||
@@ -880,7 +945,7 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) {
|
||||
if (v != lua_version(NULL))
|
||||
luaL_error(L, "multiple Lua VMs detected");
|
||||
else if (*v != ver)
|
||||
luaL_error(L, "version mismatch: app. needs %d, Lua core provides %f",
|
||||
luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
|
||||
ver, *v);
|
||||
/* check conversions number -> integer types */
|
||||
lua_pushnumber(L, -(lua_Number)0x1234);
|
||||
|
||||
47
lauxlib.h
47
lauxlib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.112 2010/11/16 17:43:29 roberto Exp roberto $
|
||||
** $Id: lauxlib.h,v 1.119 2011/11/14 17:10:24 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -62,6 +62,9 @@ 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_fileresult) (lua_State *L, int stat, const char *fname);
|
||||
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
|
||||
|
||||
/* pre-defined references */
|
||||
#define LUA_NOREF (-2)
|
||||
#define LUA_REFNIL (-1)
|
||||
@@ -69,9 +72,13 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
|
||||
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_loadfilex) (lua_State *L, const char *filename,
|
||||
const char *mode);
|
||||
|
||||
#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
|
||||
|
||||
LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
|
||||
const char *name, const char *mode);
|
||||
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
|
||||
|
||||
LUALIB_API lua_State *(luaL_newstate) (void);
|
||||
@@ -83,7 +90,7 @@ LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
|
||||
|
||||
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
|
||||
|
||||
LUALIB_API void (luaL_findtable) (lua_State *L, int idx, const char *fname);
|
||||
LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname);
|
||||
|
||||
LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
|
||||
const char *msg, int level);
|
||||
@@ -124,6 +131,8 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
|
||||
|
||||
#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
|
||||
|
||||
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
@@ -160,7 +169,33 @@ LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz);
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** File handles for IO library
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and
|
||||
** initial structure 'luaL_Stream' (it may contain other fields
|
||||
** after that initial structure).
|
||||
*/
|
||||
|
||||
#define LUA_FILEHANDLE "FILE*"
|
||||
|
||||
|
||||
typedef struct luaL_Stream {
|
||||
FILE *f; /* stream (NULL for incompletely created streams) */
|
||||
lua_CFunction closef; /* to close stream (NULL for closed streams) */
|
||||
} luaL_Stream;
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/* compatibility with old module system */
|
||||
#if defined(LUA_COMPAT_MODULE)
|
||||
|
||||
LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname,
|
||||
int sizehint);
|
||||
@@ -169,6 +204,8 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
|
||||
|
||||
#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0))
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
271
lbaselib.c
271
lbaselib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lbaselib.c,v 1.250 2010/09/07 19:38:36 roberto Exp roberto $
|
||||
** $Id: lbaselib.c,v 1.273 2011/11/30 13:03:24 roberto Exp roberto $
|
||||
** Basic library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -32,44 +32,56 @@ static int luaB_print (lua_State *L) {
|
||||
lua_call(L, 1, 1);
|
||||
s = lua_tolstring(L, -1, &l); /* get result */
|
||||
if (s == NULL)
|
||||
return luaL_error(L, LUA_QL("tostring") " must return a string to "
|
||||
LUA_QL("print"));
|
||||
return luaL_error(L,
|
||||
LUA_QL("tostring") " must return a string to " LUA_QL("print"));
|
||||
if (i>1) luai_writestring("\t", 1);
|
||||
luai_writestring(s, l);
|
||||
lua_pop(L, 1); /* pop result */
|
||||
}
|
||||
luai_writestring("\n", 1);
|
||||
luai_writeline();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#define SPACECHARS " \f\n\r\t\v"
|
||||
|
||||
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));
|
||||
if (lua_isnoneornil(L, 2)) { /* standard conversion */
|
||||
int isnum;
|
||||
lua_Number n = lua_tonumberx(L, 1, &isnum);
|
||||
if (isnum) {
|
||||
lua_pushnumber(L, n);
|
||||
return 1;
|
||||
}
|
||||
} /* else not a number; must be something */
|
||||
luaL_checkany(L, 1);
|
||||
}
|
||||
else {
|
||||
const char *s1 = luaL_checkstring(L, 1);
|
||||
char *s2;
|
||||
unsigned long n;
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
const char *e = s + l; /* end point for 's' */
|
||||
int base = luaL_checkint(L, 2);
|
||||
int neg = 0;
|
||||
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
while (isspace((unsigned char)(*s1))) s1++; /* skip initial spaces */
|
||||
if (*s1 == '-') { s1++; neg = 1; }
|
||||
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, (neg) ? -(lua_Number)n : (lua_Number)n);
|
||||
s += strspn(s, SPACECHARS); /* skip initial spaces */
|
||||
if (*s == '-') { s++; neg = 1; } /* handle signal */
|
||||
else if (*s == '+') s++;
|
||||
if (isalnum((unsigned char)*s)) {
|
||||
lua_Number n = 0;
|
||||
do {
|
||||
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
|
||||
: toupper((unsigned char)*s) - 'A' + 10;
|
||||
if (digit >= base) break; /* invalid numeral; force a fail */
|
||||
n = n * (lua_Number)base + (lua_Number)digit;
|
||||
s++;
|
||||
} while (isalnum((unsigned char)*s));
|
||||
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
||||
if (s == e) { /* no invalid trailing characters? */
|
||||
lua_pushnumber(L, (neg) ? -n : n);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
} /* else not a number */
|
||||
} /* else not a number */
|
||||
}
|
||||
lua_pushnil(L); /* else not a number */
|
||||
lua_pushnil(L); /* not a number */
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -110,13 +122,6 @@ static int luaB_setmetatable (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getfenv (lua_State *L) {
|
||||
return luaL_error(L, "getfenv/setfenv deprecated");
|
||||
}
|
||||
|
||||
#define luaB_setfenv luaB_getfenv
|
||||
|
||||
|
||||
static int luaB_rawequal (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
luaL_checkany(L, 2);
|
||||
@@ -125,6 +130,15 @@ static int luaB_rawequal (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int luaB_rawlen (lua_State *L) {
|
||||
int t = lua_type(L, 1);
|
||||
luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
|
||||
"table or string expected");
|
||||
lua_pushinteger(L, lua_rawlen(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_rawget (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checkany(L, 2);
|
||||
@@ -146,7 +160,7 @@ static int luaB_rawset (lua_State *L) {
|
||||
static int luaB_collectgarbage (lua_State *L) {
|
||||
static const char *const opts[] = {"stop", "restart", "collect",
|
||||
"count", "step", "setpause", "setstepmul",
|
||||
"setmajorinc", "isrunning", "gen", "inc", NULL};
|
||||
"setmajorinc", "isrunning", "generational", "incremental", NULL};
|
||||
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
|
||||
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
|
||||
LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
|
||||
@@ -241,7 +255,14 @@ static int load_aux (lua_State *L, int status) {
|
||||
|
||||
static int luaB_loadfile (lua_State *L) {
|
||||
const char *fname = luaL_optstring(L, 1, NULL);
|
||||
return load_aux(L, luaL_loadfile(L, fname));
|
||||
const char *mode = luaL_optstring(L, 2, NULL);
|
||||
int env = !lua_isnone(L, 3); /* 'env' parameter? */
|
||||
int status = luaL_loadfilex(L, fname, mode);
|
||||
if (status == LUA_OK && env) { /* 'env' parameter? */
|
||||
lua_pushvalue(L, 3);
|
||||
lua_setupvalue(L, -2, 1); /* set it as 1st upvalue of loaded chunk */
|
||||
}
|
||||
return load_aux(L, status);
|
||||
}
|
||||
|
||||
|
||||
@@ -251,20 +272,13 @@ static int luaB_loadfile (lua_State *L) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static const char *checkrights (lua_State *L, const char *mode, const char *s) {
|
||||
if (strchr(mode, 'b') == NULL && *s == LUA_SIGNATURE[0])
|
||||
return lua_pushstring(L, "attempt to load a binary chunk");
|
||||
if (strchr(mode, 't') == NULL && *s != LUA_SIGNATURE[0])
|
||||
return lua_pushstring(L, "attempt to load a text chunk");
|
||||
return NULL; /* chunk in allowed format */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** reserves a slot, above all arguments, to hold a copy of the returned
|
||||
** string to avoid it being collected while parsed
|
||||
** reserved slot, above all arguments, to hold a copy of the returned
|
||||
** string to avoid it being collected while parsed. 'load' has four
|
||||
** optional arguments (chunk, source name, mode, and environment).
|
||||
*/
|
||||
#define RESERVEDSLOT 4
|
||||
#define RESERVEDSLOT 5
|
||||
|
||||
|
||||
/*
|
||||
@@ -273,83 +287,46 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) {
|
||||
** stack top. Instead, it keeps its resulting string in a
|
||||
** reserved slot inside the stack.
|
||||
*/
|
||||
typedef struct { /* reader state */
|
||||
int f; /* position of reader function on stack */
|
||||
const char *mode; /* allowed modes (binary/text) */
|
||||
} Readstat;
|
||||
|
||||
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
|
||||
const char *s;
|
||||
Readstat *stat = (Readstat *)ud;
|
||||
(void)(ud); /* not used */
|
||||
luaL_checkstack(L, 2, "too many nested functions");
|
||||
lua_pushvalue(L, stat->f); /* get function */
|
||||
lua_pushvalue(L, 1); /* get function */
|
||||
lua_call(L, 0, 1); /* call it */
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1); /* pop result */
|
||||
*size = 0;
|
||||
return NULL;
|
||||
}
|
||||
else if ((s = lua_tostring(L, -1)) != NULL) {
|
||||
if (stat->mode != NULL) { /* first time? */
|
||||
s = checkrights(L, stat->mode, s); /* check mode */
|
||||
stat->mode = NULL; /* to avoid further checks */
|
||||
if (s) luaL_error(L, s);
|
||||
}
|
||||
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
|
||||
return lua_tolstring(L, RESERVEDSLOT, size);
|
||||
}
|
||||
else {
|
||||
else if (!lua_isstring(L, -1))
|
||||
luaL_error(L, "reader function must return a string");
|
||||
return NULL; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_load_aux (lua_State *L, int farg) {
|
||||
int status;
|
||||
Readstat stat;
|
||||
size_t l;
|
||||
const char *s = lua_tolstring(L, farg, &l);
|
||||
stat.mode = luaL_optstring(L, farg + 2, "bt");
|
||||
if (s != NULL) { /* loading a string? */
|
||||
const char *chunkname = luaL_optstring(L, farg + 1, s);
|
||||
status = (checkrights(L, stat.mode, s) != NULL)
|
||||
|| luaL_loadbuffer(L, s, l, chunkname);
|
||||
}
|
||||
else { /* loading from a reader function */
|
||||
const char *chunkname = luaL_optstring(L, farg + 1, "=(load)");
|
||||
luaL_checktype(L, farg, LUA_TFUNCTION);
|
||||
stat.f = farg;
|
||||
lua_settop(L, RESERVEDSLOT); /* create reserved slot */
|
||||
status = lua_load(L, generic_reader, &stat, chunkname);
|
||||
}
|
||||
return load_aux(L, status);
|
||||
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
|
||||
return lua_tolstring(L, RESERVEDSLOT, size);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_load (lua_State *L) {
|
||||
return luaB_load_aux(L, 1);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadin (lua_State *L) {
|
||||
int n;
|
||||
luaL_checkany(L, 1);
|
||||
n = luaB_load_aux(L, 2);
|
||||
if (n == 1) { /* success? */
|
||||
lua_pushvalue(L, 1); /* environment for loaded function */
|
||||
if (lua_setupvalue(L, -2, 1) == NULL)
|
||||
luaL_error(L, "loaded chunk does not have an upvalue");
|
||||
int status;
|
||||
size_t l;
|
||||
int top = lua_gettop(L);
|
||||
const char *s = lua_tolstring(L, 1, &l);
|
||||
const char *mode = luaL_optstring(L, 3, "bt");
|
||||
if (s != NULL) { /* loading a string? */
|
||||
const char *chunkname = luaL_optstring(L, 2, s);
|
||||
status = luaL_loadbufferx(L, s, l, chunkname, mode);
|
||||
}
|
||||
return n;
|
||||
else { /* loading from a reader function */
|
||||
const char *chunkname = luaL_optstring(L, 2, "=(load)");
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
lua_settop(L, RESERVEDSLOT); /* create reserved slot */
|
||||
status = lua_load(L, generic_reader, NULL, chunkname, mode);
|
||||
}
|
||||
if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */
|
||||
lua_pushvalue(L, 4); /* environment for loaded function */
|
||||
lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */
|
||||
}
|
||||
return load_aux(L, status);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadstring (lua_State *L) {
|
||||
lua_settop(L, 2);
|
||||
lua_pushliteral(L, "tb");
|
||||
return luaB_load(L); /* dostring(s, n) == load(s, n, "tb") */
|
||||
|
||||
}
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
@@ -390,27 +367,32 @@ static int luaB_select (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int pcallcont (lua_State *L) {
|
||||
int errfunc; /* call has an error function in bottom of the stack */
|
||||
int status = lua_getctx(L, &errfunc);
|
||||
lua_assert(status != LUA_OK);
|
||||
lua_pushboolean(L, (status == LUA_YIELD)); /* first result (status) */
|
||||
if (errfunc) /* came from xpcall? */
|
||||
lua_replace(L, 1); /* put first result in place of error function */
|
||||
else /* came from pcall */
|
||||
lua_insert(L, 1); /* open space for first result */
|
||||
static int finishpcall (lua_State *L, int status) {
|
||||
if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */
|
||||
lua_settop(L, 0); /* create space for return values */
|
||||
lua_pushboolean(L, 0);
|
||||
lua_pushstring(L, "stack overflow");
|
||||
return 2; /* return false, msg */
|
||||
}
|
||||
lua_pushboolean(L, status); /* first result (status) */
|
||||
lua_replace(L, 1); /* put first result in first slot */
|
||||
return lua_gettop(L);
|
||||
}
|
||||
|
||||
|
||||
static int pcallcont (lua_State *L) {
|
||||
int status = lua_getctx(L, NULL);
|
||||
return finishpcall(L, (status == LUA_YIELD));
|
||||
}
|
||||
|
||||
|
||||
static int luaB_pcall (lua_State *L) {
|
||||
int status;
|
||||
luaL_checkany(L, 1);
|
||||
status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, pcallcont);
|
||||
luaL_checkstack(L, 1, NULL);
|
||||
lua_pushboolean(L, (status == LUA_OK));
|
||||
lua_insert(L, 1);
|
||||
return lua_gettop(L); /* return status + all results */
|
||||
lua_pushnil(L);
|
||||
lua_insert(L, 1); /* create space for status result */
|
||||
status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont);
|
||||
return finishpcall(L, (status == LUA_OK));
|
||||
}
|
||||
|
||||
|
||||
@@ -421,11 +403,8 @@ static int luaB_xpcall (lua_State *L) {
|
||||
lua_pushvalue(L, 1); /* exchange function... */
|
||||
lua_copy(L, 2, 1); /* ...and error handler */
|
||||
lua_replace(L, 2);
|
||||
status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 1, pcallcont);
|
||||
luaL_checkstack(L, 1, NULL);
|
||||
lua_pushboolean(L, (status == LUA_OK));
|
||||
lua_replace(L, 1);
|
||||
return lua_gettop(L); /* return status + all results */
|
||||
status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont);
|
||||
return finishpcall(L, (status == LUA_OK));
|
||||
}
|
||||
|
||||
|
||||
@@ -436,55 +415,27 @@ static int luaB_tostring (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
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_createtable(L, 0, 1); /* create a new metatable `m' ... */
|
||||
lua_pushboolean(L, 1);
|
||||
lua_setfield(L, -2, "__gc"); /* ... m.__gc = false (HACK!!)... */
|
||||
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},
|
||||
{"getfenv", luaB_getfenv},
|
||||
{"getmetatable", luaB_getmetatable},
|
||||
{"ipairs", luaB_ipairs},
|
||||
{"loadfile", luaB_loadfile},
|
||||
{"load", luaB_load},
|
||||
{"loadin", luaB_loadin},
|
||||
{"loadstring", luaB_loadstring},
|
||||
#if defined(LUA_COMPAT_LOADSTRING)
|
||||
{"loadstring", luaB_load},
|
||||
#endif
|
||||
{"next", luaB_next},
|
||||
{"pairs", luaB_pairs},
|
||||
{"pcall", luaB_pcall},
|
||||
{"print", luaB_print},
|
||||
{"rawequal", luaB_rawequal},
|
||||
{"rawlen", luaB_rawlen},
|
||||
{"rawget", luaB_rawget},
|
||||
{"rawset", luaB_rawset},
|
||||
{"select", luaB_select},
|
||||
{"setfenv", luaB_setfenv},
|
||||
{"setmetatable", luaB_setmetatable},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
@@ -503,14 +454,6 @@ LUAMOD_API int luaopen_base (lua_State *L) {
|
||||
luaL_setfuncs(L, base_funcs, 0);
|
||||
lua_pushliteral(L, LUA_VERSION);
|
||||
lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
|
||||
/* `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_setfield(L, -2, "newproxy"); /* set global `newproxy' */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
88
lbitlib.c
88
lbitlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lbitlib.c,v 1.12 2010/11/22 16:39:20 roberto Exp roberto $
|
||||
** $Id: lbitlib.c,v 1.15 2010/12/17 13:26:38 roberto Exp roberto $
|
||||
** Standard library for bitwise operations
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -14,25 +14,30 @@
|
||||
|
||||
|
||||
/* number of bits to consider in a number */
|
||||
#define NBITS 32
|
||||
#if !defined(LUA_NBITS)
|
||||
#define LUA_NBITS 32
|
||||
#endif
|
||||
|
||||
#define ALLONES (~(((~(lua_Unsigned)0) << (NBITS - 1)) << 1))
|
||||
|
||||
/* mask to trim extra bits */
|
||||
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
|
||||
|
||||
/* macro to trim extra bits */
|
||||
#define trim(x) ((x) & ALLONES)
|
||||
|
||||
|
||||
/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
|
||||
#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
|
||||
|
||||
|
||||
typedef lua_Unsigned b_uint;
|
||||
|
||||
|
||||
#define getuintarg(L,arg) luaL_checkunsigned(L,arg)
|
||||
|
||||
|
||||
static b_uint andaux (lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = ~(b_uint)0;
|
||||
for (i = 1; i <= n; i++)
|
||||
r &= getuintarg(L, i);
|
||||
r &= luaL_checkunsigned(L, i);
|
||||
return trim(r);
|
||||
}
|
||||
|
||||
@@ -55,7 +60,7 @@ static int b_or (lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = 0;
|
||||
for (i = 1; i <= n; i++)
|
||||
r |= getuintarg(L, i);
|
||||
r |= luaL_checkunsigned(L, i);
|
||||
lua_pushunsigned(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
@@ -65,14 +70,14 @@ static int b_xor (lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = 0;
|
||||
for (i = 1; i <= n; i++)
|
||||
r ^= getuintarg(L, i);
|
||||
r ^= luaL_checkunsigned(L, i);
|
||||
lua_pushunsigned(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_not (lua_State *L) {
|
||||
b_uint r = ~getuintarg(L, 1);
|
||||
b_uint r = ~luaL_checkunsigned(L, 1);
|
||||
lua_pushunsigned(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
@@ -82,11 +87,11 @@ static int b_shift (lua_State *L, b_uint r, int i) {
|
||||
if (i < 0) { /* shift right? */
|
||||
i = -i;
|
||||
r = trim(r);
|
||||
if (i >= NBITS) r = 0;
|
||||
if (i >= LUA_NBITS) r = 0;
|
||||
else r >>= i;
|
||||
}
|
||||
else { /* shift left */
|
||||
if (i >= NBITS) r = 0;
|
||||
if (i >= LUA_NBITS) r = 0;
|
||||
else r <<= i;
|
||||
r = trim(r);
|
||||
}
|
||||
@@ -96,22 +101,22 @@ static int b_shift (lua_State *L, b_uint r, int i) {
|
||||
|
||||
|
||||
static int b_lshift (lua_State *L) {
|
||||
return b_shift(L, getuintarg(L, 1), luaL_checkint(L, 2));
|
||||
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_rshift (lua_State *L) {
|
||||
return b_shift(L, getuintarg(L, 1), -luaL_checkint(L, 2));
|
||||
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_arshift (lua_State *L) {
|
||||
b_uint r = getuintarg(L, 1);
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
int i = luaL_checkint(L, 2);
|
||||
if (i < 0 || !(r & ((b_uint)1 << (NBITS - 1))))
|
||||
if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
|
||||
return b_shift(L, r, -i);
|
||||
else { /* arithmetic shift for 'negative' number */
|
||||
if (i >= NBITS) r = ALLONES;
|
||||
if (i >= LUA_NBITS) r = ALLONES;
|
||||
else
|
||||
r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
|
||||
lua_pushunsigned(L, r);
|
||||
@@ -121,10 +126,10 @@ static int b_arshift (lua_State *L) {
|
||||
|
||||
|
||||
static int b_rot (lua_State *L, int i) {
|
||||
b_uint r = getuintarg(L, 1);
|
||||
i &= (NBITS - 1); /* i = i % NBITS */
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
i &= (LUA_NBITS - 1); /* i = i % NBITS */
|
||||
r = trim(r);
|
||||
r = (r << i) | (r >> (NBITS - i));
|
||||
r = (r << i) | (r >> (LUA_NBITS - i));
|
||||
lua_pushunsigned(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
@@ -140,17 +145,58 @@ static int b_rrot (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** get field and width arguments for field-manipulation functions,
|
||||
** checking whether they are valid
|
||||
*/
|
||||
static int fieldargs (lua_State *L, int farg, int *width) {
|
||||
int f = luaL_checkint(L, farg);
|
||||
int w = luaL_optint(L, farg + 1, 1);
|
||||
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
|
||||
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
|
||||
if (f + w > LUA_NBITS)
|
||||
luaL_error(L, "trying to access non-existent bits");
|
||||
*width = w;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
static int b_extract (lua_State *L) {
|
||||
int w;
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
int f = fieldargs(L, 2, &w);
|
||||
r = (r >> f) & mask(w);
|
||||
lua_pushunsigned(L, r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_replace (lua_State *L) {
|
||||
int w;
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
b_uint v = luaL_checkunsigned(L, 2);
|
||||
int f = fieldargs(L, 3, &w);
|
||||
int m = mask(w);
|
||||
v &= m; /* erase bits outside given width */
|
||||
r = (r & ~(m << f)) | (v << f);
|
||||
lua_pushunsigned(L, r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_Reg bitlib[] = {
|
||||
{"arshift", b_arshift},
|
||||
{"band", b_and},
|
||||
{"bnot", b_not},
|
||||
{"bor", b_or},
|
||||
{"bxor", b_xor},
|
||||
{"btest", b_test},
|
||||
{"extract", b_extract},
|
||||
{"lrotate", b_lrot},
|
||||
{"lshift", b_lshift},
|
||||
{"replace", b_replace},
|
||||
{"rrotate", b_rrot},
|
||||
{"rshift", b_rshift},
|
||||
{"btest", b_test},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
96
lcode.c
96
lcode.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcode.c,v 2.48 2010/07/02 20:42:40 roberto Exp roberto $
|
||||
** $Id: lcode.c,v 2.59 2011/08/15 19:41:58 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "lparser.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
#define hasjumps(e) ((e)->t != (e)->f)
|
||||
@@ -35,19 +36,23 @@ static int isnumeral(expdesc *e) {
|
||||
|
||||
void luaK_nil (FuncState *fs, int from, int n) {
|
||||
Instruction *previous;
|
||||
int l = from + n - 1; /* last register to set nil */
|
||||
if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
|
||||
previous = &fs->f->code[fs->pc-1];
|
||||
if (GET_OPCODE(*previous) == OP_LOADNIL) {
|
||||
int pfrom = GETARG_A(*previous);
|
||||
int pto = GETARG_B(*previous);
|
||||
if (pfrom <= from && from <= pto+1) { /* can connect both? */
|
||||
if (from+n-1 > pto)
|
||||
SETARG_B(*previous, from+n-1);
|
||||
int pl = pfrom + GETARG_B(*previous);
|
||||
if ((pfrom <= from && from <= pl + 1) ||
|
||||
(from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
|
||||
if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
|
||||
if (pl > l) l = pl; /* l = max(l, pl) */
|
||||
SETARG_A(*previous, from);
|
||||
SETARG_B(*previous, l - from);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} /* else go through */
|
||||
}
|
||||
luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
|
||||
luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
|
||||
}
|
||||
|
||||
|
||||
@@ -171,6 +176,19 @@ void luaK_patchlist (FuncState *fs, int list, int target) {
|
||||
}
|
||||
|
||||
|
||||
LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
|
||||
level++; /* argument is +1 to reserve 0 as non-op */
|
||||
while (list != NO_JUMP) {
|
||||
int next = getjump(fs, list);
|
||||
lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
|
||||
(GETARG_A(fs->f->code[list]) == 0 ||
|
||||
GETARG_A(fs->f->code[list]) >= level));
|
||||
SETARG_A(fs->f->code[list], level);
|
||||
list = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaK_patchtohere (FuncState *fs, int list) {
|
||||
luaK_getlabel(fs);
|
||||
luaK_concat(fs, &fs->jpc, list);
|
||||
@@ -195,11 +213,11 @@ static int luaK_code (FuncState *fs, Instruction i) {
|
||||
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,
|
||||
luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
|
||||
MAX_INT, "opcodes");
|
||||
f->code[fs->pc] = i;
|
||||
/* save corresponding line information */
|
||||
luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
|
||||
luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
|
||||
MAX_INT, "opcodes");
|
||||
f->lineinfo[fs->pc] = fs->ls->lastline;
|
||||
return fs->pc++;
|
||||
@@ -229,11 +247,11 @@ static int codeextraarg (FuncState *fs, int a) {
|
||||
}
|
||||
|
||||
|
||||
int luaK_codeABxX (FuncState *fs, OpCode o, int reg, int k) {
|
||||
if (k < MAXARG_Bx)
|
||||
return luaK_codeABx(fs, o, reg, k + 1);
|
||||
int luaK_codek (FuncState *fs, int reg, int k) {
|
||||
if (k <= MAXARG_Bx)
|
||||
return luaK_codeABx(fs, OP_LOADK, reg, k);
|
||||
else {
|
||||
int p = luaK_codeABx(fs, o, reg, 0);
|
||||
int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
|
||||
codeextraarg(fs, k);
|
||||
return p;
|
||||
}
|
||||
@@ -271,14 +289,14 @@ static void freeexp (FuncState *fs, expdesc *e) {
|
||||
|
||||
|
||||
static int addk (FuncState *fs, TValue *key, TValue *v) {
|
||||
lua_State *L = fs->L;
|
||||
lua_State *L = fs->ls->L;
|
||||
TValue *idx = luaH_set(L, fs->h, key);
|
||||
Proto *f = fs->f;
|
||||
int k, oldsize;
|
||||
if (ttisnumber(idx)) {
|
||||
lua_Number n = nvalue(idx);
|
||||
lua_number2int(k, n);
|
||||
if (luaO_rawequalObj(&f->k[k], v))
|
||||
if (luaV_rawequalobj(&f->k[k], v))
|
||||
return k;
|
||||
/* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
|
||||
go through and create a new entry for this value */
|
||||
@@ -286,6 +304,8 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
|
||||
/* constant not found; create a new entry */
|
||||
oldsize = f->sizek;
|
||||
k = fs->nk;
|
||||
/* numerical value does not need GC barrier;
|
||||
table has no metatable, so it does not need to invalidate cache */
|
||||
setnvalue(idx, cast_num(k));
|
||||
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
|
||||
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
|
||||
@@ -298,14 +318,14 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
|
||||
|
||||
int luaK_stringK (FuncState *fs, TString *s) {
|
||||
TValue o;
|
||||
setsvalue(fs->L, &o, s);
|
||||
setsvalue(fs->ls->L, &o, s);
|
||||
return addk(fs, &o, &o);
|
||||
}
|
||||
|
||||
|
||||
int luaK_numberK (FuncState *fs, lua_Number r) {
|
||||
int n;
|
||||
lua_State *L = fs->L;
|
||||
lua_State *L = fs->ls->L;
|
||||
TValue o;
|
||||
setnvalue(&o, r);
|
||||
if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */
|
||||
@@ -332,7 +352,7 @@ 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);
|
||||
sethvalue(fs->ls->L, &k, fs->h);
|
||||
return addk(fs, &k, &v);
|
||||
}
|
||||
|
||||
@@ -564,15 +584,15 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
|
||||
|
||||
|
||||
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
|
||||
int func;
|
||||
int ereg;
|
||||
luaK_exp2anyreg(fs, e);
|
||||
ereg = e->u.info; /* register where 'e' was placed */
|
||||
freeexp(fs, e);
|
||||
func = fs->freereg;
|
||||
luaK_codeABC(fs, OP_SELF, func, e->u.info, luaK_exp2RK(fs, key));
|
||||
freeexp(fs, key);
|
||||
luaK_reserveregs(fs, 2);
|
||||
e->u.info = func;
|
||||
e->u.info = fs->freereg; /* base register for op_self */
|
||||
e->k = VNONRELOC;
|
||||
luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
|
||||
luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
|
||||
freeexp(fs, key);
|
||||
}
|
||||
|
||||
|
||||
@@ -603,21 +623,14 @@ 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 VJMP: {
|
||||
invertjump(fs, e);
|
||||
pc = e->u.info;
|
||||
break;
|
||||
}
|
||||
case VFALSE: {
|
||||
if (!hasjumps(e)) {
|
||||
pc = luaK_jump(fs); /* always jump */
|
||||
break;
|
||||
}
|
||||
/* else go through */
|
||||
case VK: case VKNUM: case VTRUE: {
|
||||
pc = NO_JUMP; /* always true; do nothing */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
pc = jumponcond(fs, e, 0);
|
||||
@@ -630,24 +643,17 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
||||
}
|
||||
|
||||
|
||||
static void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
||||
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 VJMP: {
|
||||
pc = e->u.info;
|
||||
break;
|
||||
}
|
||||
case VTRUE: {
|
||||
if (!hasjumps(e)) {
|
||||
pc = luaK_jump(fs); /* always jump */
|
||||
break;
|
||||
}
|
||||
/* else go through */
|
||||
case VNIL: case VFALSE: {
|
||||
pc = NO_JUMP; /* always false; do nothing */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
pc = jumponcond(fs, e, 1);
|
||||
|
||||
8
lcode.h
8
lcode.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcode.h,v 1.54 2010/04/17 12:46:32 roberto Exp roberto $
|
||||
** $Id: lcode.h,v 1.57 2011/04/07 18:14:12 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -44,11 +44,9 @@ typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
|
||||
|
||||
#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
|
||||
|
||||
#define luaK_codek(fs,reg,k) luaK_codeABxX(fs, OP_LOADK, reg, k)
|
||||
|
||||
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 int luaK_codeABxX (FuncState *fs, OpCode o, int reg, int k);
|
||||
LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k);
|
||||
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);
|
||||
@@ -64,6 +62,7 @@ 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_goiffalse (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);
|
||||
@@ -71,6 +70,7 @@ 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_patchclose (FuncState *fs, int list, int level);
|
||||
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, int line);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcorolib.c,v 1.1 2010/06/10 21:30:26 roberto Exp roberto $
|
||||
** $Id: lcorolib.c,v 1.3 2011/08/23 17:24:34 roberto Exp roberto $
|
||||
** Coroutine Library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -28,7 +28,7 @@ static int auxresume (lua_State *L, lua_State *co, int narg) {
|
||||
return -1; /* error flag */
|
||||
}
|
||||
lua_xmove(L, co, narg);
|
||||
status = lua_resume(co, narg);
|
||||
status = lua_resume(co, L, narg);
|
||||
if (status == LUA_OK || status == LUA_YIELD) {
|
||||
int nres = lua_gettop(co);
|
||||
if (!lua_checkstack(L, nres + 1)) {
|
||||
@@ -80,8 +80,9 @@ static int luaB_auxwrap (lua_State *L) {
|
||||
|
||||
|
||||
static int luaB_cocreate (lua_State *L) {
|
||||
lua_State *NL = lua_newthread(L);
|
||||
lua_State *NL;
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
NL = lua_newthread(L);
|
||||
lua_pushvalue(L, 1); /* move function to top */
|
||||
lua_xmove(L, NL, 1); /* move function from L to NL */
|
||||
return 1;
|
||||
|
||||
45
lctype.c
45
lctype.c
@@ -1,45 +1,52 @@
|
||||
/*
|
||||
** $Id: lctype.c,v 1.7 2009/05/27 16:51:15 roberto Exp roberto $
|
||||
** $Id: lctype.c,v 1.10 2011/06/24 12:25:33 roberto Exp roberto $
|
||||
** 'ctype' functions for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
#define lctype_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lctype.h"
|
||||
|
||||
#if !LUA_USE_CTYPE /* { */
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
||||
0x00, /* EOZ */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
|
||||
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
||||
0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */
|
||||
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
||||
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16,
|
||||
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */
|
||||
0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
||||
0x04, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x25,
|
||||
0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25,
|
||||
0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25,
|
||||
0x25, 0x25, 0x25, 0x04, 0x04, 0x04, 0x04, 0x05,
|
||||
0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05,
|
||||
0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */
|
||||
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05,
|
||||
0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
|
||||
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
#endif /* } */
|
||||
|
||||
57
lctype.h
57
lctype.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lctype.h,v 1.7 2009/05/27 16:51:15 roberto Exp roberto $
|
||||
** $Id: lctype.h,v 1.11 2011/06/27 18:22:46 roberto Exp roberto $
|
||||
** 'ctype' functions for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,11 +7,32 @@
|
||||
#ifndef lctype_h
|
||||
#define lctype_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/*
|
||||
** WARNING: the functions defined here do not necessarily correspond
|
||||
** to the similar functions in the standard C ctype.h. They are
|
||||
** optimized for the specific needs of Lua
|
||||
*/
|
||||
|
||||
#if !defined(LUA_USE_CTYPE)
|
||||
|
||||
#if 'A' == 65 && '0' == 48
|
||||
/* ASCII case: can use its own tables; faster and fixed */
|
||||
#define LUA_USE_CTYPE 0
|
||||
#else
|
||||
/* must use standard C ctype */
|
||||
#define LUA_USE_CTYPE 1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if !LUA_USE_CTYPE /* { */
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
@@ -20,7 +41,6 @@
|
||||
#define PRINTBIT 2
|
||||
#define SPACEBIT 3
|
||||
#define XDIGITBIT 4
|
||||
#define UPPERBIT 5
|
||||
|
||||
|
||||
#define MASK(B) (1 << (B))
|
||||
@@ -36,15 +56,40 @@
|
||||
*/
|
||||
#define lislalpha(c) testprop(c, MASK(ALPHABIT))
|
||||
#define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
|
||||
#define lisupper(c) testprop(c, MASK(UPPERBIT))
|
||||
#define lisdigit(c) testprop(c, MASK(DIGITBIT))
|
||||
#define lisspace(c) testprop(c, MASK(SPACEBIT))
|
||||
#define lisprint(c) testprop(c, MASK(PRINTBIT))
|
||||
#define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
|
||||
|
||||
/*
|
||||
** this 'ltolower' only works for alphabetic characters
|
||||
*/
|
||||
#define ltolower(c) ((c) | ('A' ^ 'a'))
|
||||
|
||||
/* one more entry for 0 and one more for -1 (EOZ) */
|
||||
|
||||
/* two more entries for 0 and -1 (EOZ) */
|
||||
LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];
|
||||
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/*
|
||||
** use standard C ctypes
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
#define lislalpha(c) (isalpha(c) || (c) == '_')
|
||||
#define lislalnum(c) (isalnum(c) || (c) == '_')
|
||||
#define lisdigit(c) (isdigit(c))
|
||||
#define lisspace(c) (isspace(c))
|
||||
#define lisprint(c) (isprint(c))
|
||||
#define lisxdigit(c) (isxdigit(c))
|
||||
|
||||
#define ltolower(c) (tolower(c))
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
41
ldblib.c
41
ldblib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.125 2010/11/10 18:06:10 roberto Exp roberto $
|
||||
** $Id: ldblib.c,v 1.131 2011/10/24 14:54:05 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -18,6 +18,9 @@
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#define HOOKKEY "_HKEY"
|
||||
|
||||
|
||||
|
||||
static int db_getregistry (lua_State *L) {
|
||||
lua_pushvalue(L, LUA_REGISTRYINDEX);
|
||||
@@ -39,8 +42,8 @@ static int db_setmetatable (lua_State *L) {
|
||||
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;
|
||||
lua_setmetatable(L, 1);
|
||||
return 1; /* return 1st argument */
|
||||
}
|
||||
|
||||
|
||||
@@ -250,15 +253,14 @@ static int db_upvaluejoin (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static const char KEY_HOOK = 'h';
|
||||
#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
|
||||
|
||||
|
||||
static void hookf (lua_State *L, lua_Debug *ar) {
|
||||
static const char *const hooknames[] =
|
||||
{"call", "return", "line", "count", "tail call"};
|
||||
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
lua_pushlightuserdata(L, L);
|
||||
gethooktable(L);
|
||||
lua_pushthread(L);
|
||||
lua_rawget(L, -2);
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushstring(L, hooknames[(int)ar->event]);
|
||||
@@ -291,19 +293,6 @@ static char *unmakemask (int mask, char *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, mask, count;
|
||||
lua_Hook func;
|
||||
@@ -318,11 +307,15 @@ static int db_sethook (lua_State *L) {
|
||||
count = luaL_optint(L, arg+3, 0);
|
||||
func = hookf; mask = makemask(smask, count);
|
||||
}
|
||||
gethooktable(L);
|
||||
lua_pushlightuserdata(L, L1);
|
||||
if (gethooktable(L) == 0) { /* creating hook table? */
|
||||
lua_pushstring(L, "k");
|
||||
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
|
||||
}
|
||||
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
||||
lua_pushvalue(L, arg+1);
|
||||
lua_rawset(L, -3); /* set new hook */
|
||||
lua_pop(L, 1); /* remove hook table */
|
||||
lua_sethook(L1, func, mask, count); /* set hooks */
|
||||
return 0;
|
||||
}
|
||||
@@ -338,7 +331,7 @@ static int db_gethook (lua_State *L) {
|
||||
lua_pushliteral(L, "external hook");
|
||||
else {
|
||||
gethooktable(L);
|
||||
lua_pushlightuserdata(L, L1);
|
||||
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
||||
lua_rawget(L, -2); /* get hook */
|
||||
lua_remove(L, -2); /* remove hook table */
|
||||
}
|
||||
|
||||
289
ldebug.c
289
ldebug.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 2.73 2010/09/07 19:21:39 roberto Exp roberto $
|
||||
** $Id: ldebug.c,v 2.88 2011/11/30 12:43:51 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -30,17 +30,20 @@
|
||||
|
||||
|
||||
|
||||
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
|
||||
|
||||
|
||||
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
|
||||
|
||||
|
||||
static int currentpc (CallInfo *ci) {
|
||||
lua_assert(isLua(ci));
|
||||
return pcRel(ci->u.l.savedpc, ci_func(ci)->l.p);
|
||||
return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
|
||||
}
|
||||
|
||||
|
||||
static int currentline (CallInfo *ci) {
|
||||
return getfuncline(ci_func(ci)->l.p, currentpc(ci));
|
||||
return getfuncline(ci_func(ci)->p, currentpc(ci));
|
||||
}
|
||||
|
||||
|
||||
@@ -94,13 +97,35 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
}
|
||||
|
||||
|
||||
static const char *upvalname (Proto *p, int uv) {
|
||||
TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
|
||||
if (s == NULL) return "?";
|
||||
else return getstr(s);
|
||||
}
|
||||
|
||||
|
||||
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
|
||||
int nparams = clLvalue(ci->func)->p->numparams;
|
||||
if (n >= ci->u.l.base - ci->func - nparams)
|
||||
return NULL; /* no such vararg */
|
||||
else {
|
||||
*pos = ci->func + nparams + n;
|
||||
return "(*vararg)"; /* generic name for any vararg */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *findlocal (lua_State *L, CallInfo *ci, int n,
|
||||
StkId *pos) {
|
||||
const char *name = NULL;
|
||||
StkId base;
|
||||
if (isLua(ci)) {
|
||||
base = ci->u.l.base;
|
||||
name = luaF_getlocalname(ci_func(ci)->l.p, n, currentpc(ci));
|
||||
if (n < 0) /* access to vararg values? */
|
||||
return findvararg(ci, -n, pos);
|
||||
else {
|
||||
base = ci->u.l.base;
|
||||
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
|
||||
}
|
||||
}
|
||||
else
|
||||
base = ci->func + 1;
|
||||
@@ -108,10 +133,8 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n,
|
||||
StkId limit = (ci == L->ci) ? L->top : ci->next->func;
|
||||
if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
|
||||
name = "(*temporary)"; /* generic name for any valid slot */
|
||||
else {
|
||||
*pos = base; /* to avoid warnings */
|
||||
else
|
||||
return NULL; /* no name */
|
||||
}
|
||||
}
|
||||
*pos = base + (n - 1);
|
||||
return name;
|
||||
@@ -125,10 +148,10 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
if (!isLfunction(L->top - 1)) /* not a Lua function? */
|
||||
name = NULL;
|
||||
else /* consider live variables at function start (parameters) */
|
||||
name = luaF_getlocalname(clvalue(L->top - 1)->l.p, n, 0);
|
||||
name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
|
||||
}
|
||||
else { /* active function; get information through 'ar' */
|
||||
StkId pos;
|
||||
StkId pos = 0; /* to avoid warnings */
|
||||
name = findlocal(L, ar->i_ci, n, &pos);
|
||||
if (name) {
|
||||
setobj2s(L, L->top, pos);
|
||||
@@ -141,11 +164,11 @@ 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) {
|
||||
StkId pos;
|
||||
StkId pos = 0; /* to avoid warnings */
|
||||
const char *name = findlocal(L, ar->i_ci, n, &pos);
|
||||
lua_lock(L);
|
||||
if (name)
|
||||
setobjs2s(L, pos, L->top - 1);
|
||||
setobjs2s(L, pos, L->top - 1);
|
||||
L->top--; /* pop value */
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
@@ -153,7 +176,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
|
||||
|
||||
static void funcinfo (lua_Debug *ar, Closure *cl) {
|
||||
if (cl == NULL || cl->c.isC) {
|
||||
if (noLuaClosure(cl)) {
|
||||
ar->source = "=[C]";
|
||||
ar->linedefined = -1;
|
||||
ar->lastlinedefined = -1;
|
||||
@@ -171,24 +194,26 @@ static void funcinfo (lua_Debug *ar, Closure *cl) {
|
||||
|
||||
|
||||
static void collectvalidlines (lua_State *L, Closure *f) {
|
||||
if (f == NULL || f->c.isC) {
|
||||
if (noLuaClosure(f)) {
|
||||
setnilvalue(L->top);
|
||||
incr_top(L);
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
TValue v;
|
||||
int *lineinfo = f->l.p->lineinfo;
|
||||
Table *t = luaH_new(L);
|
||||
sethvalue(L, L->top, t);
|
||||
Table *t = luaH_new(L); /* new table to store active lines */
|
||||
sethvalue(L, L->top, t); /* push it on stack */
|
||||
incr_top(L);
|
||||
for (i=0; i<f->l.p->sizelineinfo; i++)
|
||||
setbvalue(luaH_setint(L, t, lineinfo[i]), 1);
|
||||
setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
|
||||
for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
|
||||
luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
|
||||
Closure *f, CallInfo *ci) {
|
||||
Closure *f, CallInfo *ci) {
|
||||
int status = 1;
|
||||
for (; *what; what++) {
|
||||
switch (*what) {
|
||||
@@ -202,7 +227,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
|
||||
}
|
||||
case 'u': {
|
||||
ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
|
||||
if (f == NULL || f->c.isC) {
|
||||
if (noLuaClosure(f)) {
|
||||
ar->isvararg = 1;
|
||||
ar->nparams = 0;
|
||||
}
|
||||
@@ -217,7 +242,11 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
|
||||
break;
|
||||
}
|
||||
case 'n': {
|
||||
ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
|
||||
/* calling function is a known Lua function? */
|
||||
if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
|
||||
ar->namewhat = getfuncname(L, ci->previous, &ar->name);
|
||||
else
|
||||
ar->namewhat = NULL;
|
||||
if (ar->namewhat == NULL) {
|
||||
ar->namewhat = ""; /* not found */
|
||||
ar->name = NULL;
|
||||
@@ -243,7 +272,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
if (*what == '>') {
|
||||
ci = NULL;
|
||||
func = L->top - 1;
|
||||
luai_apicheck(L, ttisfunction(func));
|
||||
api_check(L, ttisfunction(func), "function expected");
|
||||
what++; /* skip the '>' */
|
||||
L->top--; /* pop function */
|
||||
}
|
||||
@@ -271,96 +300,57 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static const char *getobjname (Proto *p, int lastpc, int reg,
|
||||
const char **name);
|
||||
|
||||
static void kname (Proto *p, int c, int reg, const char *what,
|
||||
const char **name) {
|
||||
if (c == reg && what && *what == 'c')
|
||||
return; /* index is a constant; name already correct */
|
||||
else if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
|
||||
*name = svalue(&p->k[INDEXK(c)]);
|
||||
else
|
||||
*name = "?";
|
||||
|
||||
/*
|
||||
** find a "name" for the RK value 'c'
|
||||
*/
|
||||
static void kname (Proto *p, int pc, int c, const char **name) {
|
||||
if (ISK(c)) { /* is 'c' a constant? */
|
||||
TValue *kvalue = &p->k[INDEXK(c)];
|
||||
if (ttisstring(kvalue)) { /* literal constant? */
|
||||
*name = svalue(kvalue); /* it is its own name */
|
||||
return;
|
||||
}
|
||||
/* else no reasonable name found */
|
||||
}
|
||||
else { /* 'c' is a register */
|
||||
const char *what = getobjname(p, pc, c, name); /* search for 'c' */
|
||||
if (what && *what == 'c') { /* found a constant name? */
|
||||
return; /* 'name' already filled */
|
||||
}
|
||||
/* else no reasonable name found */
|
||||
}
|
||||
*name = "?"; /* no reasonable name found */
|
||||
}
|
||||
|
||||
|
||||
static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
|
||||
const char **name) {
|
||||
Proto *p = ci_func(ci)->l.p;
|
||||
const char *what = NULL;
|
||||
int lastpc = currentpc(ci);
|
||||
/*
|
||||
** try to find last instruction before 'lastpc' that modified register 'reg'
|
||||
*/
|
||||
static int findsetreg (Proto *p, int lastpc, int reg) {
|
||||
int pc;
|
||||
*name = luaF_getlocalname(p, reg + 1, lastpc);
|
||||
if (*name) /* is a local? */
|
||||
return "local";
|
||||
/* else try symbolic execution */
|
||||
int setreg = -1; /* keep last instruction that changed 'reg' */
|
||||
for (pc = 0; pc < lastpc; pc++) {
|
||||
Instruction i = p->code[pc];
|
||||
OpCode op = GET_OPCODE(i);
|
||||
int a = GETARG_A(i);
|
||||
switch (op) {
|
||||
case OP_MOVE: {
|
||||
if (reg == a) {
|
||||
int b = GETARG_B(i); /* move from 'b' to 'a' */
|
||||
if (b < a)
|
||||
what = getobjname(L, ci, b, name); /* get name for 'b' */
|
||||
else what = NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_GETTABUP:
|
||||
case OP_GETTABLE: {
|
||||
if (reg == a) {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
int t = GETARG_B(i);
|
||||
const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
|
||||
? luaF_getlocalname(p, t + 1, pc)
|
||||
: getstr(p->upvalues[t].name);
|
||||
kname(p, k, a, what, name);
|
||||
what = (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_GETUPVAL: {
|
||||
if (reg == a) {
|
||||
int u = GETARG_B(i); /* upvalue index */
|
||||
TString *tn = p->upvalues[u].name;
|
||||
*name = tn ? getstr(tn) : "?";
|
||||
what = "upvalue";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_LOADK: {
|
||||
if (reg == a) {
|
||||
int b = GETARG_Bx(i);
|
||||
b = (b > 0) ? b - 1 : GETARG_Ax(p->code[pc + 1]);
|
||||
if (ttisstring(&p->k[b])) {
|
||||
what = "constant";
|
||||
*name = svalue(&p->k[b]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_LOADNIL: {
|
||||
int b = GETARG_B(i); /* move from 'b' to 'a' */
|
||||
if (a <= reg && reg <= b) /* set registers from 'a' to 'b' */
|
||||
what = NULL;
|
||||
break;
|
||||
}
|
||||
case OP_SELF: {
|
||||
if (reg == a) {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
kname(p, k, a, what, name);
|
||||
what = "method";
|
||||
}
|
||||
int b = GETARG_B(i);
|
||||
if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
|
||||
setreg = pc;
|
||||
break;
|
||||
}
|
||||
case OP_TFORCALL: {
|
||||
if (reg >= a + 2) what = NULL; /* affect all regs above its base */
|
||||
if (reg >= a + 2) setreg = pc; /* affect all regs above its base */
|
||||
break;
|
||||
}
|
||||
case OP_CALL:
|
||||
case OP_TAILCALL: {
|
||||
if (reg >= a) what = NULL; /* affect all registers above base */
|
||||
if (reg >= a) setreg = pc; /* affect all registers above base */
|
||||
break;
|
||||
}
|
||||
case OP_JMP: {
|
||||
@@ -371,32 +361,88 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
|
||||
pc += b; /* do the jump */
|
||||
break;
|
||||
}
|
||||
case OP_TEST: {
|
||||
if (reg == a) setreg = pc; /* jumped code can change 'a' */
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if (testAMode(op) && reg == a) what = NULL;
|
||||
if (testAMode(op) && reg == a) /* any instruction that set A */
|
||||
setreg = pc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return what;
|
||||
return setreg;
|
||||
}
|
||||
|
||||
|
||||
static const char *getobjname (Proto *p, int lastpc, int reg,
|
||||
const char **name) {
|
||||
int pc;
|
||||
*name = luaF_getlocalname(p, reg + 1, lastpc);
|
||||
if (*name) /* is a local? */
|
||||
return "local";
|
||||
/* else try symbolic execution */
|
||||
pc = findsetreg(p, lastpc, reg);
|
||||
if (pc != -1) { /* could find instruction? */
|
||||
Instruction i = p->code[pc];
|
||||
OpCode op = GET_OPCODE(i);
|
||||
switch (op) {
|
||||
case OP_MOVE: {
|
||||
int b = GETARG_B(i); /* move from 'b' to 'a' */
|
||||
if (b < GETARG_A(i))
|
||||
return getobjname(p, pc, b, name); /* get name for 'b' */
|
||||
break;
|
||||
}
|
||||
case OP_GETTABUP:
|
||||
case OP_GETTABLE: {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
int t = GETARG_B(i); /* table index */
|
||||
const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
|
||||
? luaF_getlocalname(p, t + 1, pc)
|
||||
: upvalname(p, t);
|
||||
kname(p, pc, k, name);
|
||||
return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
|
||||
}
|
||||
case OP_GETUPVAL: {
|
||||
*name = upvalname(p, GETARG_B(i));
|
||||
return "upvalue";
|
||||
}
|
||||
case OP_LOADK:
|
||||
case OP_LOADKX: {
|
||||
int b = (op == OP_LOADK) ? GETARG_Bx(i)
|
||||
: GETARG_Ax(p->code[pc + 1]);
|
||||
if (ttisstring(&p->k[b])) {
|
||||
*name = svalue(&p->k[b]);
|
||||
return "constant";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_SELF: {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
kname(p, pc, k, name);
|
||||
return "method";
|
||||
}
|
||||
default: break; /* go through to return NULL */
|
||||
}
|
||||
}
|
||||
return NULL; /* could not find reasonable name */
|
||||
}
|
||||
|
||||
|
||||
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
||||
TMS tm;
|
||||
Instruction i;
|
||||
if ((ci->callstatus & CIST_TAIL) || !isLua(ci->previous))
|
||||
return NULL; /* calling function is not Lua (or is unknown) */
|
||||
ci = ci->previous; /* calling function */
|
||||
i = ci_func(ci)->l.p->code[currentpc(ci)];
|
||||
if (GET_OPCODE(i) == OP_EXTRAARG) /* extra argument? */
|
||||
i = ci_func(ci)->l.p->code[currentpc(ci) - 1]; /* get 'real' instruction */
|
||||
Proto *p = ci_func(ci)->p; /* calling function */
|
||||
int pc = currentpc(ci); /* calling instruction index */
|
||||
Instruction i = p->code[pc]; /* calling instruction */
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_CALL:
|
||||
case OP_TAILCALL:
|
||||
return getobjname(L, ci, GETARG_A(i), name);
|
||||
case OP_TFORCALL: {
|
||||
case OP_TAILCALL: /* get function name */
|
||||
return getobjname(p, pc, GETARG_A(i), name);
|
||||
case OP_TFORCALL: { /* for iterator */
|
||||
*name = "for iterator";
|
||||
return "for iterator";
|
||||
}
|
||||
/* all other instructions can call only through metamethods */
|
||||
case OP_SELF:
|
||||
case OP_GETTABUP:
|
||||
case OP_GETTABLE: tm = TM_INDEX; break;
|
||||
@@ -425,7 +471,10 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
||||
|
||||
|
||||
|
||||
/* only ANSI way to check whether a pointer points to an array */
|
||||
/*
|
||||
** only ANSI way to check whether a pointer points to an array
|
||||
** (used only for error messages, so efficiency is not a big concern)
|
||||
*/
|
||||
static int isinstack (CallInfo *ci, const TValue *o) {
|
||||
StkId p;
|
||||
for (p = ci->u.l.base; p < ci->top; p++)
|
||||
@@ -435,12 +484,12 @@ static int isinstack (CallInfo *ci, const TValue *o) {
|
||||
|
||||
|
||||
static const char *getupvalname (CallInfo *ci, const TValue *o,
|
||||
const char **name) {
|
||||
LClosure *c = &ci_func(ci)->l;
|
||||
const char **name) {
|
||||
LClosure *c = ci_func(ci);
|
||||
int i;
|
||||
for (i = 0; i < c->nupvalues; i++) {
|
||||
if (c->upvals[i]->v == o) {
|
||||
*name = getstr(c->p->upvalues[i].name);
|
||||
*name = upvalname(c->p, i);
|
||||
return "upvalue";
|
||||
}
|
||||
}
|
||||
@@ -448,15 +497,16 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
|
||||
}
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
|
||||
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
|
||||
CallInfo *ci = L->ci;
|
||||
const char *name = NULL;
|
||||
const char *t = objtypename(o);
|
||||
const char *kind = NULL;
|
||||
if (isLua(ci)) {
|
||||
kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
|
||||
if (!kind && isinstack(ci, o)) /* no? try a register */
|
||||
kind = getobjname(L, ci, cast_int(o - ci->u.l.base), &name);
|
||||
if (!kind && isinstack(ci, o)) /* no? try a register */
|
||||
kind = getobjname(ci_func(ci)->p, currentpc(ci),
|
||||
cast_int(o - ci->u.l.base), &name);
|
||||
}
|
||||
if (kind)
|
||||
luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
|
||||
@@ -466,14 +516,14 @@ void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
|
||||
}
|
||||
|
||||
|
||||
void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
|
||||
l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
|
||||
if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
|
||||
lua_assert(!ttisstring(p1) && !ttisnumber(p2));
|
||||
luaG_typeerror(L, p1, "concatenate");
|
||||
}
|
||||
|
||||
|
||||
void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
l_noret 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 */
|
||||
@@ -481,14 +531,13 @@ void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
}
|
||||
|
||||
|
||||
int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
const char *t1 = objtypename(p1);
|
||||
const char *t2 = objtypename(p2);
|
||||
if (t1 == t2)
|
||||
luaG_runerror(L, "attempt to compare two %s values", t1);
|
||||
else
|
||||
luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -497,7 +546,7 @@ static void addinfo (lua_State *L, const char *msg) {
|
||||
if (isLua(ci)) { /* is Lua code? */
|
||||
char buff[LUA_IDSIZE]; /* add file:line information */
|
||||
int line = currentline(ci);
|
||||
TString *src = ci_func(ci)->l.p->source;
|
||||
TString *src = ci_func(ci)->p->source;
|
||||
if (src)
|
||||
luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
|
||||
else { /* no source available; use "?" instead */
|
||||
@@ -508,7 +557,7 @@ static void addinfo (lua_State *L, const char *msg) {
|
||||
}
|
||||
|
||||
|
||||
void luaG_errormsg (lua_State *L) {
|
||||
l_noret 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);
|
||||
@@ -521,7 +570,7 @@ void luaG_errormsg (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
void luaG_runerror (lua_State *L, const char *fmt, ...) {
|
||||
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
addinfo(L, luaO_pushvfstring(L, fmt, argp));
|
||||
|
||||
23
ldebug.h
23
ldebug.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldebug.h,v 2.4 2009/04/30 17:42:21 roberto Exp roberto $
|
||||
** $Id: ldebug.h,v 2.6 2011/06/02 19:31:40 roberto Exp roberto $
|
||||
** Auxiliary functions from Debug Interface module
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -17,15 +17,18 @@
|
||||
|
||||
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
||||
|
||||
/* Active Lua function (given call info) */
|
||||
#define ci_func(ci) (clLvalue((ci)->func))
|
||||
|
||||
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 l_noret luaG_typeerror (lua_State *L, const TValue *o,
|
||||
const char *opname);
|
||||
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2);
|
||||
LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
|
||||
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
||||
|
||||
#endif
|
||||
|
||||
211
ldo.c
211
ldo.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 2.89 2010/09/30 17:21:31 roberto Exp roberto $
|
||||
** $Id: ldo.c,v 2.104 2012/05/08 13:53:33 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -100,7 +100,7 @@ static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
|
||||
}
|
||||
|
||||
|
||||
void luaD_throw (lua_State *L, int errcode) {
|
||||
l_noret luaD_throw (lua_State *L, int errcode) {
|
||||
if (L->errorJmp) { /* thread has an error handler? */
|
||||
L->errorJmp->status = errcode; /* set status */
|
||||
LUAI_THROW(L, L->errorJmp); /* jump to it */
|
||||
@@ -123,7 +123,7 @@ void luaD_throw (lua_State *L, int errcode) {
|
||||
|
||||
|
||||
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
||||
unsigned short oldnCcalls = G(L)->nCcalls;
|
||||
unsigned short oldnCcalls = L->nCcalls;
|
||||
struct lua_longjmp lj;
|
||||
lj.status = LUA_OK;
|
||||
lj.previous = L->errorJmp; /* chain new error handler */
|
||||
@@ -132,7 +132,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
||||
(*f)(L, ud);
|
||||
);
|
||||
L->errorJmp = lj.previous; /* restore old error handler */
|
||||
G(L)->nCcalls = oldnCcalls;
|
||||
L->nCcalls = oldnCcalls;
|
||||
return lj.status;
|
||||
}
|
||||
|
||||
@@ -293,65 +293,59 @@ static StkId tryfuncTM (lua_State *L, StkId func) {
|
||||
** returns true if function has been executed (C function)
|
||||
*/
|
||||
int luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||
Closure *cl;
|
||||
lua_CFunction f;
|
||||
ptrdiff_t funcr;
|
||||
if (!ttisfunction(func)) /* `func' is not a function? */
|
||||
func = tryfuncTM(L, func); /* check the `function' tag method */
|
||||
funcr = savestack(L, func);
|
||||
if (ttislcf(func)) { /* light C function? */
|
||||
f = fvalue(func); /* get it */
|
||||
goto isCfunc; /* go to call it */
|
||||
}
|
||||
cl = clvalue(func);
|
||||
if (cl->c.isC) { /* C closure? */
|
||||
CallInfo *ci;
|
||||
int n;
|
||||
f = cl->c.f;
|
||||
isCfunc: /* call C function 'f' */
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
ci = next_ci(L); /* now 'enter' new function */
|
||||
ci->nresults = nresults;
|
||||
ci->func = restorestack(L, funcr);
|
||||
ci->top = L->top + LUA_MINSTACK;
|
||||
lua_assert(ci->top <= L->stack_last);
|
||||
ci->callstatus = 0;
|
||||
if (L->hookmask & LUA_MASKCALL)
|
||||
luaD_hook(L, LUA_HOOKCALL, -1);
|
||||
lua_unlock(L);
|
||||
n = (*f)(L); /* do the actual call */
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
luaD_poscall(L, L->top - n);
|
||||
return 1;
|
||||
}
|
||||
else { /* Lua function: prepare its call */
|
||||
CallInfo *ci;
|
||||
int nparams, nargs;
|
||||
StkId base;
|
||||
Proto *p = cl->l.p;
|
||||
luaD_checkstack(L, p->maxstacksize);
|
||||
func = restorestack(L, funcr);
|
||||
nargs = cast_int(L->top - func) - 1; /* number of real arguments */
|
||||
nparams = p->numparams; /* number of expected parameters */
|
||||
for (; nargs < nparams; nargs++)
|
||||
setnilvalue(L->top++); /* complete missing arguments */
|
||||
if (!p->is_vararg) /* no varargs? */
|
||||
base = func + 1;
|
||||
else /* vararg function */
|
||||
base = adjust_varargs(L, p, nargs);
|
||||
ci = next_ci(L); /* now 'enter' new function */
|
||||
ci->nresults = nresults;
|
||||
ci->func = func;
|
||||
ci->u.l.base = base;
|
||||
ci->top = base + p->maxstacksize;
|
||||
lua_assert(ci->top <= L->stack_last);
|
||||
ci->u.l.savedpc = p->code; /* starting point */
|
||||
ci->callstatus = CIST_LUA;
|
||||
L->top = ci->top;
|
||||
if (L->hookmask & LUA_MASKCALL)
|
||||
callhook(L, ci);
|
||||
return 0;
|
||||
CallInfo *ci;
|
||||
int n; /* number of arguments (Lua) or returns (C) */
|
||||
ptrdiff_t funcr = savestack(L, func);
|
||||
switch (ttype(func)) {
|
||||
case LUA_TLCF: /* light C function */
|
||||
f = fvalue(func);
|
||||
goto Cfunc;
|
||||
case LUA_TCCL: { /* C closure */
|
||||
f = clCvalue(func)->f;
|
||||
Cfunc:
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
ci = next_ci(L); /* now 'enter' new function */
|
||||
ci->nresults = nresults;
|
||||
ci->func = restorestack(L, funcr);
|
||||
ci->top = L->top + LUA_MINSTACK;
|
||||
lua_assert(ci->top <= L->stack_last);
|
||||
ci->callstatus = 0;
|
||||
if (L->hookmask & LUA_MASKCALL)
|
||||
luaD_hook(L, LUA_HOOKCALL, -1);
|
||||
lua_unlock(L);
|
||||
n = (*f)(L); /* do the actual call */
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
luaD_poscall(L, L->top - n);
|
||||
return 1;
|
||||
}
|
||||
case LUA_TLCL: { /* Lua function: prepare its call */
|
||||
StkId base;
|
||||
Proto *p = clLvalue(func)->p;
|
||||
luaD_checkstack(L, p->maxstacksize);
|
||||
func = restorestack(L, funcr);
|
||||
n = cast_int(L->top - func) - 1; /* number of real arguments */
|
||||
for (; n < p->numparams; n++)
|
||||
setnilvalue(L->top++); /* complete missing arguments */
|
||||
base = (!p->is_vararg) ? func + 1 : adjust_varargs(L, p, n);
|
||||
ci = next_ci(L); /* now 'enter' new function */
|
||||
ci->nresults = nresults;
|
||||
ci->func = func;
|
||||
ci->u.l.base = base;
|
||||
ci->top = base + p->maxstacksize;
|
||||
lua_assert(ci->top <= L->stack_last);
|
||||
ci->u.l.savedpc = p->code; /* starting point */
|
||||
ci->callstatus = CIST_LUA;
|
||||
L->top = ci->top;
|
||||
if (L->hookmask & LUA_MASKCALL)
|
||||
callhook(L, ci);
|
||||
return 0;
|
||||
}
|
||||
default: { /* not a function */
|
||||
func = tryfuncTM(L, func); /* retry with 'function' tag method */
|
||||
return luaD_precall(L, func, nresults); /* now it must be a function */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -388,18 +382,17 @@ int luaD_poscall (lua_State *L, StkId firstResult) {
|
||||
** function position.
|
||||
*/
|
||||
void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
|
||||
global_State *g = G(L);
|
||||
if (++g->nCcalls >= LUAI_MAXCCALLS) {
|
||||
if (g->nCcalls == LUAI_MAXCCALLS)
|
||||
if (++L->nCcalls >= LUAI_MAXCCALLS) {
|
||||
if (L->nCcalls == LUAI_MAXCCALLS)
|
||||
luaG_runerror(L, "C stack overflow");
|
||||
else if (g->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
|
||||
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
|
||||
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
|
||||
}
|
||||
if (!allowyield) L->nny++;
|
||||
if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
|
||||
luaV_execute(L); /* call it */
|
||||
if (!allowyield) L->nny--;
|
||||
g->nCcalls--;
|
||||
L->nCcalls--;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
@@ -409,8 +402,6 @@ static void finishCcall (lua_State *L) {
|
||||
int n;
|
||||
lua_assert(ci->u.c.k != NULL); /* must have a continuation */
|
||||
lua_assert(L->nny == 0);
|
||||
/* finish 'luaD_call' */
|
||||
G(L)->nCcalls--;
|
||||
/* finish 'lua_callk' */
|
||||
adjustresults(L, ci->nresults);
|
||||
/* call continuation function */
|
||||
@@ -460,7 +451,7 @@ static int recover (lua_State *L, int status) {
|
||||
CallInfo *ci = findpcall(L);
|
||||
if (ci == NULL) return 0; /* no recovery point */
|
||||
/* "finish" luaD_pcall */
|
||||
oldtop = restorestack(L, ci->u.c.extra);
|
||||
oldtop = restorestack(L, ci->extra);
|
||||
luaF_close(L, oldtop);
|
||||
seterrorobj(L, status, oldtop);
|
||||
L->ci = ci;
|
||||
@@ -479,7 +470,7 @@ static int recover (lua_State *L, int status) {
|
||||
** coroutine itself. (Such errors should not be handled by any coroutine
|
||||
** error handler and should not kill the coroutine.)
|
||||
*/
|
||||
static void resume_error (lua_State *L, const char *msg, StkId firstArg) {
|
||||
static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
|
||||
L->top = firstArg; /* remove args from the stack */
|
||||
setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */
|
||||
incr_top(L);
|
||||
@@ -491,9 +482,10 @@ static void resume_error (lua_State *L, const char *msg, StkId firstArg) {
|
||||
** do the work for 'lua_resume' in protected mode
|
||||
*/
|
||||
static void resume (lua_State *L, void *ud) {
|
||||
int nCcalls = L->nCcalls;
|
||||
StkId firstArg = cast(StkId, ud);
|
||||
CallInfo *ci = L->ci;
|
||||
if (G(L)->nCcalls >= LUAI_MAXCCALLS)
|
||||
if (nCcalls >= LUAI_MAXCCALLS)
|
||||
resume_error(L, "C stack overflow", firstArg);
|
||||
if (L->status == LUA_OK) { /* may be starting a coroutine */
|
||||
if (ci != &L->base_ci) /* not in base level? */
|
||||
@@ -506,10 +498,10 @@ static void resume (lua_State *L, void *ud) {
|
||||
resume_error(L, "cannot resume dead coroutine", firstArg);
|
||||
else { /* resuming from previous yield */
|
||||
L->status = LUA_OK;
|
||||
ci->func = restorestack(L, ci->extra);
|
||||
if (isLua(ci)) /* yielded inside a hook? */
|
||||
luaV_execute(L); /* just continue running Lua code */
|
||||
else { /* 'common' yield */
|
||||
ci->func = restorestack(L, ci->u.c.extra);
|
||||
if (ci->u.c.k != NULL) { /* does it have a continuation? */
|
||||
int n;
|
||||
ci->u.c.status = LUA_YIELD; /* 'default' status */
|
||||
@@ -520,19 +512,19 @@ static void resume (lua_State *L, void *ud) {
|
||||
api_checknelems(L, n);
|
||||
firstArg = L->top - n; /* yield results come from continuation */
|
||||
}
|
||||
G(L)->nCcalls--; /* finish 'luaD_call' */
|
||||
luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
|
||||
}
|
||||
unroll(L, NULL);
|
||||
}
|
||||
lua_assert(nCcalls == L->nCcalls);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_resume (lua_State *L, int nargs) {
|
||||
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
|
||||
int status;
|
||||
lua_lock(L);
|
||||
luai_userstateresume(L, nargs);
|
||||
++G(L)->nCcalls; /* count resume */
|
||||
L->nCcalls = (from) ? from->nCcalls + 1 : 1;
|
||||
L->nny = 0; /* allow yields */
|
||||
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
|
||||
status = luaD_rawrunprotected(L, resume, L->top - nargs);
|
||||
@@ -552,7 +544,8 @@ LUA_API int lua_resume (lua_State *L, int nargs) {
|
||||
lua_assert(status == L->status);
|
||||
}
|
||||
L->nny = 1; /* do not allow yields */
|
||||
--G(L)->nCcalls;
|
||||
L->nCcalls--;
|
||||
lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
@@ -570,13 +563,13 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
|
||||
luaG_runerror(L, "attempt to yield from outside a coroutine");
|
||||
}
|
||||
L->status = LUA_YIELD;
|
||||
ci->extra = savestack(L, ci->func); /* save current 'func' */
|
||||
if (isLua(ci)) { /* inside a hook? */
|
||||
api_check(L, k == NULL, "hooks cannot continue after yielding");
|
||||
}
|
||||
else {
|
||||
if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
|
||||
ci->u.c.ctx = ctx; /* save context */
|
||||
ci->u.c.extra = savestack(L, ci->func); /* save current 'func' */
|
||||
ci->func = L->top - nresults - 1; /* protect stack below results */
|
||||
luaD_throw(L, LUA_YIELD);
|
||||
}
|
||||
@@ -615,39 +608,59 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||
*/
|
||||
struct SParser { /* data to `f_parser' */
|
||||
ZIO *z;
|
||||
Mbuffer buff; /* buffer to be used by the scanner */
|
||||
Varlist varl; /* list of local variables (to be used by the parser) */
|
||||
Mbuffer buff; /* dynamic structure used by the scanner */
|
||||
Dyndata dyd; /* dynamic structures used by the parser */
|
||||
const char *mode;
|
||||
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);
|
||||
tf = (c == LUA_SIGNATURE[0])
|
||||
? luaU_undump(L, p->z, &p->buff, p->name)
|
||||
: luaY_parser(L, p->z, &p->buff, &p->varl, p->name);
|
||||
setptvalue2s(L, L->top, tf);
|
||||
incr_top(L);
|
||||
cl = luaF_newLclosure(L, tf);
|
||||
setclvalue(L, L->top - 1, cl);
|
||||
for (i = 0; i < tf->sizeupvalues; i++) /* initialize upvalues */
|
||||
cl->l.upvals[i] = luaF_newupval(L);
|
||||
|
||||
static void checkmode (lua_State *L, const char *mode, const char *x) {
|
||||
if (mode && strchr(mode, x[0]) == NULL) {
|
||||
luaO_pushfstring(L,
|
||||
"attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
|
||||
luaD_throw(L, LUA_ERRSYNTAX);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
|
||||
static void f_parser (lua_State *L, void *ud) {
|
||||
int i;
|
||||
Closure *cl;
|
||||
struct SParser *p = cast(struct SParser *, ud);
|
||||
int c = zgetc(p->z); /* read first character */
|
||||
if (c == LUA_SIGNATURE[0]) {
|
||||
checkmode(L, p->mode, "binary");
|
||||
cl = luaU_undump(L, p->z, &p->buff, p->name);
|
||||
}
|
||||
else {
|
||||
checkmode(L, p->mode, "text");
|
||||
cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
|
||||
}
|
||||
lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
|
||||
for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */
|
||||
UpVal *up = luaF_newupval(L);
|
||||
cl->l.upvals[i] = up;
|
||||
luaC_objbarrier(L, cl, up);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||
const char *mode) {
|
||||
struct SParser p;
|
||||
int status;
|
||||
L->nny++; /* cannot yield during parsing */
|
||||
p.z = z; p.name = name;
|
||||
p.varl.actvar = NULL; p.varl.nactvar = p.varl.actvarsize = 0;
|
||||
p.z = z; p.name = name; p.mode = mode;
|
||||
p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
|
||||
p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
|
||||
p.dyd.label.arr = NULL; p.dyd.label.size = 0;
|
||||
luaZ_initbuffer(L, &p.buff);
|
||||
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
|
||||
luaZ_freebuffer(L, &p.buff);
|
||||
luaM_freearray(L, p.varl.actvar, p.varl.actvarsize);
|
||||
luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
|
||||
luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
|
||||
luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
|
||||
L->nny--;
|
||||
return status;
|
||||
}
|
||||
|
||||
7
ldo.h
7
ldo.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.h,v 2.17 2009/11/25 15:27:51 roberto Exp roberto $
|
||||
** $Id: ldo.h,v 2.19 2011/10/07 20:45:19 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -26,7 +26,8 @@
|
||||
/* 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 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||
const char *mode);
|
||||
LUAI_FUNC void luaD_hook (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,
|
||||
@@ -38,7 +39,7 @@ LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
|
||||
LUAI_FUNC void luaD_growstack (lua_State *L, int n);
|
||||
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
|
||||
|
||||
LUAI_FUNC void luaD_throw (lua_State *L, int errcode);
|
||||
LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
|
||||
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
||||
|
||||
#endif
|
||||
|
||||
15
ldump.c
15
ldump.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldump.c,v 1.17 2010/10/13 21:04:52 lhf Exp $
|
||||
** $Id: ldump.c,v 2.16 2011/11/24 13:25:41 roberto Exp roberto $
|
||||
** save precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -69,7 +69,7 @@ static void DumpString(const TString* s, DumpState* D)
|
||||
{
|
||||
size_t size=s->tsv.len+1; /* include trailing '\0' */
|
||||
DumpVar(size,D);
|
||||
DumpBlock(getstr(s),size,D);
|
||||
DumpBlock(getstr(s),size*sizeof(char),D);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,8 +84,8 @@ static void DumpConstants(const Proto* f, DumpState* D)
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
const TValue* o=&f->k[i];
|
||||
DumpChar(ttype(o),D);
|
||||
switch (ttype(o))
|
||||
DumpChar(ttypenv(o),D);
|
||||
switch (ttypenv(o))
|
||||
{
|
||||
case LUA_TNIL:
|
||||
break;
|
||||
@@ -98,6 +98,7 @@ static void DumpConstants(const Proto* f, DumpState* D)
|
||||
case LUA_TSTRING:
|
||||
DumpString(rawtsvalue(o),D);
|
||||
break;
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
n=f->sizep;
|
||||
@@ -111,8 +112,8 @@ static void DumpUpvalues(const Proto* f, DumpState* D)
|
||||
DumpInt(n,D);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
DumpChar(f->upvalues[i].instack, D);
|
||||
DumpChar(f->upvalues[i].idx, D);
|
||||
DumpChar(f->upvalues[i].instack,D);
|
||||
DumpChar(f->upvalues[i].idx,D);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +151,7 @@ static void DumpFunction(const Proto* f, DumpState* D)
|
||||
|
||||
static void DumpHeader(DumpState* D)
|
||||
{
|
||||
char h[LUAC_HEADERSIZE];
|
||||
lu_byte h[LUAC_HEADERSIZE];
|
||||
luaU_header(h);
|
||||
DumpBlock(h,LUAC_HEADERSIZE,D);
|
||||
}
|
||||
|
||||
20
lfunc.c
20
lfunc.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 2.26 2010/06/10 21:27:09 roberto Exp roberto $
|
||||
** $Id: lfunc.c,v 2.28 2012/01/20 22:05:50 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -21,18 +21,15 @@
|
||||
|
||||
|
||||
Closure *luaF_newCclosure (lua_State *L, int n) {
|
||||
Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeCclosure(n), NULL, 0)->cl;
|
||||
c->c.isC = 1;
|
||||
Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl;
|
||||
c->c.nupvalues = cast_byte(n);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
Closure *luaF_newLclosure (lua_State *L, Proto *p) {
|
||||
int n = p->sizeupvalues;
|
||||
Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeLclosure(n), NULL, 0)->cl;
|
||||
c->l.isC = 0;
|
||||
c->l.p = p;
|
||||
Closure *luaF_newLclosure (lua_State *L, int n) {
|
||||
Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl;
|
||||
c->l.p = NULL;
|
||||
c->l.nupvalues = cast_byte(n);
|
||||
while (n--) c->l.upvals[n] = NULL;
|
||||
return c;
|
||||
@@ -146,13 +143,6 @@ void luaF_freeproto (lua_State *L, Proto *f) {
|
||||
}
|
||||
|
||||
|
||||
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'.
|
||||
** Returns NULL if not found.
|
||||
|
||||
5
lfunc.h
5
lfunc.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lfunc.h,v 2.5 2010/03/26 20:58:11 roberto Exp roberto $
|
||||
** $Id: lfunc.h,v 2.7 2012/01/20 22:05:50 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -20,12 +20,11 @@
|
||||
|
||||
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
|
||||
LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems);
|
||||
LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, Proto *p);
|
||||
LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems);
|
||||
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);
|
||||
|
||||
29
lgc.h
29
lgc.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lgc.h,v 2.43 2010/06/07 16:55:34 roberto Exp roberto $
|
||||
** $Id: lgc.h,v 2.55 2012/05/21 13:18:10 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -25,6 +25,14 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/* how much to allocate before next GC step */
|
||||
#if !defined(GCSTEPSIZE)
|
||||
/* ~100 small strings */
|
||||
#define GCSTEPSIZE (cast_int(100 * sizeof(TString)))
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Possible states of the Garbage Collector
|
||||
*/
|
||||
@@ -52,10 +60,6 @@
|
||||
#define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic)
|
||||
|
||||
|
||||
#define gcstopped(g) ((g)->GCdebt == MIN_LMEM)
|
||||
#define stopgc(g) ((g)->GCdebt = MIN_LMEM)
|
||||
|
||||
|
||||
/*
|
||||
** some useful bit tricks
|
||||
*/
|
||||
@@ -67,17 +71,14 @@
|
||||
#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)))
|
||||
|
||||
|
||||
|
||||
/* Layout for bit use in `marked' field: */
|
||||
#define WHITE0BIT 0 /* object is white (type 0) */
|
||||
#define WHITE1BIT 1 /* object is white (type 1) */
|
||||
#define BLACKBIT 2 /* object is black */
|
||||
#define FINALIZEDBIT 3 /* for userdata: has been finalized */
|
||||
#define SEPARATED 4 /* " ": it's in 'udgc' list or in 'tobefnz' */
|
||||
#define FINALIZEDBIT 3 /* object has been separated for finalization */
|
||||
#define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */
|
||||
#define FIXEDBIT 5 /* object is fixed (should not be collected) */
|
||||
#define OLDBIT 6 /* object is old (only in generational mode) */
|
||||
/* bit 7 is currently used by tests (luaL_checkmemory) */
|
||||
@@ -108,7 +109,9 @@
|
||||
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
|
||||
|
||||
|
||||
#define luaC_checkGC(L) {condchangemem(L); if (G(L)->GCdebt > 0) luaC_step(L);}
|
||||
#define luaC_condGC(L,c) \
|
||||
{if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
|
||||
#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
|
||||
|
||||
|
||||
#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
|
||||
@@ -127,9 +130,9 @@
|
||||
#define luaC_barrierproto(L,p,c) \
|
||||
{ if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); }
|
||||
|
||||
LUAI_FUNC void luaC_separateudata (lua_State *L, int all);
|
||||
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
|
||||
LUAI_FUNC void luaC_step (lua_State *L);
|
||||
LUAI_FUNC void luaC_forcestep (lua_State *L);
|
||||
LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
|
||||
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
|
||||
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
|
||||
@@ -137,7 +140,7 @@ LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
|
||||
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
|
||||
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
|
||||
LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c);
|
||||
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, Udata *u);
|
||||
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
|
||||
LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv);
|
||||
LUAI_FUNC void luaC_changemode (lua_State *L, int mode);
|
||||
|
||||
|
||||
10
linit.c
10
linit.c
@@ -1,16 +1,16 @@
|
||||
/*
|
||||
** $Id: linit.c,v 1.29 2010/10/25 14:32:36 roberto Exp roberto $
|
||||
** Initialization of libraries for lua.c and other clients
|
||||
** $Id: linit.c,v 1.31 2011/01/26 16:30:02 roberto Exp roberto $
|
||||
** Initialization of libraries for lua.c and other clients
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
/*
|
||||
** If you embed Lua in your program and need to open the standard
|
||||
** libraries, call luaL_openlibs in your program. If you need a
|
||||
** different set of libraries, copy this file to your project and edit
|
||||
** it to suit your needs.
|
||||
*/
|
||||
*/
|
||||
|
||||
|
||||
#define linit_c
|
||||
@@ -57,7 +57,7 @@ LUALIB_API void luaL_openlibs (lua_State *L) {
|
||||
lua_pop(L, 1); /* remove lib */
|
||||
}
|
||||
/* add open functions from 'preloadedlibs' into 'package.preload' table */
|
||||
luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
for (lib = preloadedlibs; lib->func; lib++) {
|
||||
lua_pushcfunction(L, lib->func);
|
||||
lua_setfield(L, -2, lib->name);
|
||||
|
||||
408
liolib.c
408
liolib.c
@@ -1,10 +1,20 @@
|
||||
/*
|
||||
** $Id: liolib.c,v 2.94 2010/11/09 16:57:49 roberto Exp roberto $
|
||||
** $Id: liolib.c,v 2.107 2011/11/14 16:55:35 roberto Exp roberto $
|
||||
** Standard I/O (and system) library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** POSIX idiosyncrasy!
|
||||
** This definition must come before the inclusion of 'stdio.h'; it
|
||||
** should not affect non-POSIX systems
|
||||
*/
|
||||
#if !defined(_FILE_OFFSET_BITS)
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
#endif
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -19,78 +29,96 @@
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#define MAX_SIZE_T (~(size_t)0)
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** lua_popen spawns a new process connected to the current
|
||||
** one through the file streams.
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#if !defined(lua_popen) /* { */
|
||||
|
||||
#if defined(LUA_USE_POPEN) /* { */
|
||||
|
||||
#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
|
||||
#define lua_pclose(L,file) ((void)L, pclose(file))
|
||||
|
||||
#elif defined(LUA_WIN) /* }{ */
|
||||
|
||||
#define lua_popen(L,c,m) ((void)L, _popen(c,m))
|
||||
#define lua_pclose(L,file) ((void)L, _pclose(file))
|
||||
|
||||
|
||||
#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), -1)
|
||||
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif /* } */
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** lua_popen spawns a new process connected to the current one through
|
||||
** the file streams.
|
||||
** {======================================================
|
||||
** lua_fseek/lua_ftell: configuration for longer offsets
|
||||
** =======================================================
|
||||
*/
|
||||
#if !defined(lua_popen)
|
||||
|
||||
#if defined(LUA_USE_POPEN)
|
||||
#if !defined(lua_fseek) /* { */
|
||||
|
||||
#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
|
||||
#define lua_pclose(L,file) ((void)L, pclose(file))
|
||||
#if defined(LUA_USE_POSIX)
|
||||
|
||||
#elif defined(LUA_WIN)
|
||||
#define l_fseek(f,o,w) fseeko(f,o,w)
|
||||
#define l_ftell(f) ftello(f)
|
||||
#define l_seeknum off_t
|
||||
|
||||
#define lua_popen(L,c,m) ((void)L, _popen(c,m))
|
||||
#define lua_pclose(L,file) ((void)L, _pclose(file))
|
||||
#elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \
|
||||
&& defined(_MSC_VER) && (_MSC_VER >= 1400)
|
||||
/* Windows (but not DDK) and Visual C++ 2005 or higher */
|
||||
|
||||
#define l_fseek(f,o,w) _fseeki64(f,o,w)
|
||||
#define l_ftell(f) _ftelli64(f)
|
||||
#define l_seeknum __int64
|
||||
|
||||
#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), -1)
|
||||
#define l_fseek(f,o,w) fseek(f,o,w)
|
||||
#define l_ftell(f) ftell(f)
|
||||
#define l_seeknum long
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif /* } */
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
#define IO_INPUT 1
|
||||
#define IO_OUTPUT 2
|
||||
#define IO_PREFIX "_IO_"
|
||||
#define IO_INPUT (IO_PREFIX "input")
|
||||
#define IO_OUTPUT (IO_PREFIX "output")
|
||||
|
||||
|
||||
static const char *const fnames[] = {"input", "output"};
|
||||
typedef luaL_Stream LStream;
|
||||
|
||||
|
||||
static int 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);
|
||||
if (filename)
|
||||
lua_pushfstring(L, "%s: %s", filename, strerror(en));
|
||||
else
|
||||
lua_pushfstring(L, "%s", strerror(en));
|
||||
lua_pushinteger(L, en);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
#define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
|
||||
|
||||
|
||||
static void fileerror (lua_State *L, int arg, const char *filename) {
|
||||
lua_pushfstring(L, "%s: %s", filename, strerror(errno));
|
||||
luaL_argerror(L, arg, lua_tostring(L, -1));
|
||||
}
|
||||
|
||||
|
||||
#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
|
||||
#define isclosed(p) ((p)->closef == NULL)
|
||||
|
||||
|
||||
static int io_type (lua_State *L) {
|
||||
void *ud;
|
||||
LStream *p;
|
||||
luaL_checkany(L, 1);
|
||||
ud = luaL_testudata(L, 1, LUA_FILEHANDLE);
|
||||
if (ud == NULL)
|
||||
p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
|
||||
if (p == NULL)
|
||||
lua_pushnil(L); /* not a file */
|
||||
else if (*((FILE **)ud) == NULL)
|
||||
else if (isclosed(p))
|
||||
lua_pushliteral(L, "closed file");
|
||||
else
|
||||
lua_pushliteral(L, "file");
|
||||
@@ -98,11 +126,22 @@ static int io_type (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int f_tostring (lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
if (isclosed(p))
|
||||
lua_pushliteral(L, "file (closed)");
|
||||
else
|
||||
lua_pushfstring(L, "file (%p)", p->f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static FILE *tofile (lua_State *L) {
|
||||
FILE **f = tofilep(L);
|
||||
if (*f == NULL)
|
||||
LStream *p = tolstream(L);
|
||||
if (isclosed(p))
|
||||
luaL_error(L, "attempt to use a closed file");
|
||||
return *f;
|
||||
lua_assert(p->f);
|
||||
return p->f;
|
||||
}
|
||||
|
||||
|
||||
@@ -111,45 +150,35 @@ static FILE *tofile (lua_State *L) {
|
||||
** before opening the actual file; so, if there is a memory error, the
|
||||
** file is not left opened.
|
||||
*/
|
||||
static FILE **newprefile (lua_State *L) {
|
||||
FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
|
||||
*pf = NULL; /* file handle is currently `closed' */
|
||||
static LStream *newprefile (lua_State *L) {
|
||||
LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
|
||||
p->closef = NULL; /* mark file handle as 'closed' */
|
||||
luaL_setmetatable(L, LUA_FILEHANDLE);
|
||||
return pf;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static FILE **newfile (lua_State *L) {
|
||||
FILE **pf = newprefile(L);
|
||||
lua_pushvalue(L, lua_upvalueindex(1)); /* set upvalue... */
|
||||
lua_setuservalue(L, -2); /* ... as environment for new file */
|
||||
return pf;
|
||||
static int aux_close (lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
lua_CFunction cf = p->closef;
|
||||
p->closef = NULL; /* mark stream as closed */
|
||||
return (*cf)(L); /* close it */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** function to (not) close the standard files stdin, stdout, and stderr
|
||||
*/
|
||||
static int io_noclose (lua_State *L) {
|
||||
lua_pushnil(L);
|
||||
lua_pushliteral(L, "cannot close standard file");
|
||||
return 2;
|
||||
static int io_close (lua_State *L) {
|
||||
if (lua_isnone(L, 1)) /* no argument? */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
|
||||
tofile(L); /* make sure argument is an open stream */
|
||||
return aux_close(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** function to close 'popen' files
|
||||
*/
|
||||
static int io_pclose (lua_State *L) {
|
||||
FILE **p = tofilep(L);
|
||||
int stat = lua_pclose(L, *p);
|
||||
*p = NULL;
|
||||
if (stat == -1) /* error? */
|
||||
return pushresult(L, 0, NULL);
|
||||
else {
|
||||
lua_pushinteger(L, stat);
|
||||
return 1; /* return status */
|
||||
}
|
||||
static int f_gc (lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
if (!isclosed(p) && p->f != NULL)
|
||||
aux_close(L); /* ignore closed and incompletely open files */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -157,112 +186,94 @@ static int io_pclose (lua_State *L) {
|
||||
** function to close regular files
|
||||
*/
|
||||
static int io_fclose (lua_State *L) {
|
||||
FILE **p = tofilep(L);
|
||||
int ok = (fclose(*p) == 0);
|
||||
*p = NULL;
|
||||
return pushresult(L, ok, NULL);
|
||||
LStream *p = tolstream(L);
|
||||
int res = fclose(p->f);
|
||||
return luaL_fileresult(L, (res == 0), NULL);
|
||||
}
|
||||
|
||||
|
||||
static int aux_close (lua_State *L) {
|
||||
lua_getuservalue(L, 1);
|
||||
lua_getfield(L, -1, "__close");
|
||||
return (lua_tocfunction(L, -1))(L);
|
||||
static LStream *newfile (lua_State *L) {
|
||||
LStream *p = newprefile(L);
|
||||
p->f = NULL;
|
||||
p->closef = &io_fclose;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static int io_close (lua_State *L) {
|
||||
if (lua_isnone(L, 1))
|
||||
lua_rawgeti(L, lua_upvalueindex(1), IO_OUTPUT);
|
||||
tofile(L); /* make sure argument is a file */
|
||||
return aux_close(L);
|
||||
}
|
||||
|
||||
|
||||
static int io_gc (lua_State *L) {
|
||||
FILE *f = *tofilep(L);
|
||||
/* ignore closed files */
|
||||
if (f != NULL)
|
||||
aux_close(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int io_tostring (lua_State *L) {
|
||||
FILE *f = *tofilep(L);
|
||||
if (f == NULL)
|
||||
lua_pushliteral(L, "file (closed)");
|
||||
else
|
||||
lua_pushfstring(L, "file (%p)", f);
|
||||
return 1;
|
||||
static void opencheck (lua_State *L, const char *fname, const char *mode) {
|
||||
LStream *p = newfile(L);
|
||||
p->f = fopen(fname, mode);
|
||||
if (p->f == NULL)
|
||||
luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno));
|
||||
}
|
||||
|
||||
|
||||
static int io_open (lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
FILE **pf;
|
||||
LStream *p = newfile(L);
|
||||
int i = 0;
|
||||
/* check whether 'mode' matches '[rwa]%+?b?' */
|
||||
if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL &&
|
||||
(mode[i] != '+' || ++i) && /* skip if char is '+' */
|
||||
(mode[i] != 'b' || ++i) && /* skip if char is 'b' */
|
||||
(mode[i] != '+' || ++i) && /* skip if char is '+' */
|
||||
(mode[i] != 'b' || ++i) && /* skip if char is 'b' */
|
||||
(mode[i] == '\0')))
|
||||
luaL_error(L, "invalid mode " LUA_QL("%s")
|
||||
" (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
|
||||
pf = newfile(L);
|
||||
*pf = fopen(filename, mode);
|
||||
return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
|
||||
return luaL_error(L, "invalid mode " LUA_QS
|
||||
" (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
|
||||
p->f = fopen(filename, mode);
|
||||
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** this function has a separated environment, which defines the
|
||||
** correct __close for 'popen' files
|
||||
** function to close 'popen' files
|
||||
*/
|
||||
static int io_pclose (lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
return luaL_execresult(L, lua_pclose(L, p->f));
|
||||
}
|
||||
|
||||
|
||||
static int io_popen (lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
FILE **pf = newfile(L);
|
||||
*pf = lua_popen(L, filename, mode);
|
||||
return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
|
||||
LStream *p = newprefile(L);
|
||||
p->f = lua_popen(L, filename, mode);
|
||||
p->closef = &io_pclose;
|
||||
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
||||
}
|
||||
|
||||
|
||||
static int io_tmpfile (lua_State *L) {
|
||||
FILE **pf = newfile(L);
|
||||
*pf = tmpfile();
|
||||
return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
|
||||
LStream *p = newfile(L);
|
||||
p->f = tmpfile();
|
||||
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
|
||||
}
|
||||
|
||||
|
||||
static FILE *getiofile (lua_State *L, int findex) {
|
||||
FILE *f;
|
||||
lua_rawgeti(L, lua_upvalueindex(1), findex);
|
||||
f = *(FILE **)lua_touserdata(L, -1);
|
||||
if (f == NULL)
|
||||
luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
|
||||
return f;
|
||||
static FILE *getiofile (lua_State *L, const char *findex) {
|
||||
LStream *p;
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, findex);
|
||||
p = (LStream *)lua_touserdata(L, -1);
|
||||
if (isclosed(p))
|
||||
luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX));
|
||||
return p->f;
|
||||
}
|
||||
|
||||
|
||||
static int g_iofile (lua_State *L, int f, const char *mode) {
|
||||
static int g_iofile (lua_State *L, const char *f, const char *mode) {
|
||||
if (!lua_isnoneornil(L, 1)) {
|
||||
const char *filename = lua_tostring(L, 1);
|
||||
if (filename) {
|
||||
FILE **pf = newfile(L);
|
||||
*pf = fopen(filename, mode);
|
||||
if (*pf == NULL)
|
||||
fileerror(L, 1, filename);
|
||||
}
|
||||
if (filename)
|
||||
opencheck(L, filename, mode);
|
||||
else {
|
||||
tofile(L); /* check that it's a valid file handle */
|
||||
lua_pushvalue(L, 1);
|
||||
}
|
||||
lua_rawseti(L, lua_upvalueindex(1), f);
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, f);
|
||||
}
|
||||
/* return current value */
|
||||
lua_rawgeti(L, lua_upvalueindex(1), f);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -304,18 +315,15 @@ static int io_lines (lua_State *L) {
|
||||
int toclose;
|
||||
if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
|
||||
if (lua_isnil(L, 1)) { /* no file name? */
|
||||
lua_rawgeti(L, lua_upvalueindex(1), IO_INPUT); /* get default input */
|
||||
lua_replace(L, 1); /* put it at index 1 */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */
|
||||
lua_replace(L, 1); /* put it at index 1 */
|
||||
tofile(L); /* check that it's a valid file handle */
|
||||
toclose = 0; /* do not close it after iteration */
|
||||
}
|
||||
else { /* open a new file */
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
FILE **pf = newfile(L);
|
||||
*pf = fopen(filename, "r");
|
||||
if (*pf == NULL)
|
||||
fileerror(L, 1, filename);
|
||||
lua_replace(L, 1); /* put file at index 1 */
|
||||
opencheck(L, filename, "r");
|
||||
lua_replace(L, 1); /* put file at index 1 */
|
||||
toclose = 1; /* close it after iteration */
|
||||
}
|
||||
aux_lines(L, toclose);
|
||||
@@ -373,6 +381,8 @@ static int read_line (lua_State *L, FILE *f, int chop) {
|
||||
}
|
||||
|
||||
|
||||
#define MAX_SIZE_T (~(size_t)0)
|
||||
|
||||
static void read_all (lua_State *L, FILE *f) {
|
||||
size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */
|
||||
luaL_Buffer b;
|
||||
@@ -443,7 +453,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
|
||||
}
|
||||
}
|
||||
if (ferror(f))
|
||||
return pushresult(L, 0, NULL);
|
||||
return luaL_fileresult(L, 0, NULL);
|
||||
if (!success) {
|
||||
lua_pop(L, 1); /* remove last result */
|
||||
lua_pushnil(L); /* push nil instead */
|
||||
@@ -463,22 +473,23 @@ static int f_read (lua_State *L) {
|
||||
|
||||
|
||||
static int io_readline (lua_State *L) {
|
||||
FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
|
||||
LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1));
|
||||
int i;
|
||||
int n = (int)lua_tointeger(L, lua_upvalueindex(2));
|
||||
if (f == NULL) /* file is already closed? */
|
||||
luaL_error(L, "file is already closed");
|
||||
if (isclosed(p)) /* file is already closed? */
|
||||
return luaL_error(L, "file is already closed");
|
||||
lua_settop(L , 1);
|
||||
for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
|
||||
lua_pushvalue(L, lua_upvalueindex(3 + i));
|
||||
n = g_read(L, f, 2); /* 'n' is number of results */
|
||||
n = g_read(L, p->f, 2); /* 'n' is number of results */
|
||||
lua_assert(n > 0); /* should return at least a nil */
|
||||
if (!lua_isnil(L, -n)) /* read at least one value? */
|
||||
return n; /* return them */
|
||||
else { /* first result is nil: EOF or error */
|
||||
if (!lua_isnil(L, -1)) /* is there error information? */
|
||||
return luaL_error(L, "%s", lua_tostring(L, -1)); /* error */
|
||||
/* else EOF */
|
||||
if (n > 1) { /* is there error information? */
|
||||
/* 2nd result is error message */
|
||||
return luaL_error(L, "%s", lua_tostring(L, -n + 1));
|
||||
}
|
||||
if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
|
||||
lua_settop(L, 0);
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
@@ -507,7 +518,7 @@ static int g_write (lua_State *L, FILE *f, int arg) {
|
||||
}
|
||||
}
|
||||
if (status) return 1; /* file handle already on stack top */
|
||||
else return pushresult(L, status, NULL);
|
||||
else return luaL_fileresult(L, status, NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -517,7 +528,7 @@ static int io_write (lua_State *L) {
|
||||
|
||||
|
||||
static int f_write (lua_State *L) {
|
||||
FILE * f = tofile(L);
|
||||
FILE *f = tofile(L);
|
||||
lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
|
||||
return g_write(L, f, 2);
|
||||
}
|
||||
@@ -528,12 +539,15 @@ static int f_seek (lua_State *L) {
|
||||
static const char *const modenames[] = {"set", "cur", "end", NULL};
|
||||
FILE *f = tofile(L);
|
||||
int op = luaL_checkoption(L, 2, "cur", modenames);
|
||||
long offset = luaL_optlong(L, 3, 0);
|
||||
op = fseek(f, offset, mode[op]);
|
||||
lua_Number p3 = luaL_optnumber(L, 3, 0);
|
||||
l_seeknum offset = (l_seeknum)p3;
|
||||
luaL_argcheck(L, (lua_Number)offset == p3, 3,
|
||||
"not an integer in proper range");
|
||||
op = l_fseek(f, offset, mode[op]);
|
||||
if (op)
|
||||
return pushresult(L, 0, NULL); /* error */
|
||||
return luaL_fileresult(L, 0, NULL); /* error */
|
||||
else {
|
||||
lua_pushinteger(L, ftell(f));
|
||||
lua_pushnumber(L, (lua_Number)l_ftell(f));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -546,21 +560,24 @@ static int f_setvbuf (lua_State *L) {
|
||||
int op = luaL_checkoption(L, 2, NULL, modenames);
|
||||
lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
|
||||
int res = setvbuf(f, NULL, mode[op], sz);
|
||||
return pushresult(L, res == 0, NULL);
|
||||
return luaL_fileresult(L, res == 0, NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int io_flush (lua_State *L) {
|
||||
return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
||||
return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
||||
}
|
||||
|
||||
|
||||
static int f_flush (lua_State *L) {
|
||||
return pushresult(L, fflush(tofile(L)) == 0, NULL);
|
||||
return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** functions for 'io' library
|
||||
*/
|
||||
static const luaL_Reg iolib[] = {
|
||||
{"close", io_close},
|
||||
{"flush", io_flush},
|
||||
@@ -577,6 +594,9 @@ static const luaL_Reg iolib[] = {
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** methods for file handles
|
||||
*/
|
||||
static const luaL_Reg flib[] = {
|
||||
{"close", io_close},
|
||||
{"flush", f_flush},
|
||||
@@ -585,8 +605,8 @@ static const luaL_Reg flib[] = {
|
||||
{"seek", f_seek},
|
||||
{"setvbuf", f_setvbuf},
|
||||
{"write", f_write},
|
||||
{"__gc", io_gc},
|
||||
{"__tostring", io_tostring},
|
||||
{"__gc", f_gc},
|
||||
{"__tostring", f_tostring},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -600,46 +620,38 @@ static void createmeta (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
|
||||
*newprefile(L) = f;
|
||||
if (k > 0) {
|
||||
lua_pushvalue(L, -1); /* copy new file */
|
||||
lua_rawseti(L, 1, k); /* add it to common upvalue */
|
||||
}
|
||||
lua_pushvalue(L, 3); /* get environment for default files */
|
||||
lua_setuservalue(L, -2); /* set it as environment for file */
|
||||
lua_setfield(L, 2, fname); /* add file to module */
|
||||
/*
|
||||
** function to (not) close the standard files stdin, stdout, and stderr
|
||||
*/
|
||||
static int io_noclose (lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
p->closef = &io_noclose; /* keep file opened */
|
||||
lua_pushnil(L);
|
||||
lua_pushliteral(L, "cannot close standard file");
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** pushes a new table with {__close = cls}
|
||||
*/
|
||||
static void newenv (lua_State *L, lua_CFunction cls) {
|
||||
lua_createtable(L, 0, 1);
|
||||
lua_pushcfunction(L, cls);
|
||||
lua_setfield(L, -2, "__close");
|
||||
static void createstdfile (lua_State *L, FILE *f, const char *k,
|
||||
const char *fname) {
|
||||
LStream *p = newprefile(L);
|
||||
p->f = f;
|
||||
p->closef = &io_noclose;
|
||||
if (k != NULL) {
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */
|
||||
}
|
||||
lua_setfield(L, -2, fname); /* add file to module */
|
||||
}
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_io (lua_State *L) {
|
||||
lua_settop(L, 0);
|
||||
luaL_newlib(L, iolib); /* new module */
|
||||
createmeta(L);
|
||||
/* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
|
||||
newenv(L, io_fclose); /* upvalue for all io functions at index 1 */
|
||||
luaL_newlibtable(L, iolib); /* new module at index 2 */
|
||||
lua_pushvalue(L, 1); /* copy of env to be consumed by 'setfuncs' */
|
||||
luaL_setfuncs(L, iolib, 1);
|
||||
/* create (and set) default files */
|
||||
newenv(L, io_noclose); /* environment for default files at index 3 */
|
||||
createstdfile(L, stdin, IO_INPUT, "stdin");
|
||||
createstdfile(L, stdout, IO_OUTPUT, "stdout");
|
||||
createstdfile(L, stderr, 0, "stderr");
|
||||
lua_pop(L, 1); /* pop environment for default files */
|
||||
lua_getfield(L, 2, "popen");
|
||||
newenv(L, io_pclose); /* create environment for 'popen' streams */
|
||||
lua_setupvalue(L, -2, 1); /* set it as upvalue for 'popen' */
|
||||
lua_pop(L, 1); /* pop 'popen' */
|
||||
createstdfile(L, stderr, NULL, "stderr");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
161
llex.c
161
llex.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.c,v 2.40 2010/10/25 12:24:36 roberto Exp roberto $
|
||||
** $Id: llex.c,v 2.60 2012/01/20 18:35:36 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -35,10 +35,10 @@
|
||||
/* ORDER RESERVED */
|
||||
static const char *const luaX_tokens [] = {
|
||||
"and", "break", "do", "else", "elseif",
|
||||
"end", "false", "for", "function", "if",
|
||||
"end", "false", "for", "function", "goto", "if",
|
||||
"in", "local", "nil", "not", "or", "repeat",
|
||||
"return", "then", "true", "until", "while",
|
||||
"..", "...", "==", ">=", "<=", "~=", "<eof>",
|
||||
"..", "...", "==", ">=", "<=", "~=", "::", "<eof>",
|
||||
"<number>", "<name>", "<string>"
|
||||
};
|
||||
|
||||
@@ -46,7 +46,7 @@ static const char *const luaX_tokens [] = {
|
||||
#define save_and_next(ls) (save(ls, ls->current), next(ls))
|
||||
|
||||
|
||||
static void lexerror (LexState *ls, const char *msg, int token);
|
||||
static l_noret lexerror (LexState *ls, const char *msg, int token);
|
||||
|
||||
|
||||
static void save (LexState *ls, int c) {
|
||||
@@ -67,8 +67,7 @@ void luaX_init (lua_State *L) {
|
||||
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 */
|
||||
ts->tsv.extra = cast_byte(i+1); /* reserved word */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +101,7 @@ static const char *txtToken (LexState *ls, int token) {
|
||||
}
|
||||
|
||||
|
||||
static void lexerror (LexState *ls, const char *msg, int token) {
|
||||
static l_noret lexerror (LexState *ls, const char *msg, int token) {
|
||||
char buff[LUA_IDSIZE];
|
||||
luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE);
|
||||
msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
|
||||
@@ -112,7 +111,7 @@ static void lexerror (LexState *ls, const char *msg, int token) {
|
||||
}
|
||||
|
||||
|
||||
void luaX_syntaxerror (LexState *ls, const char *msg) {
|
||||
l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
|
||||
lexerror(ls, msg, ls->t.token);
|
||||
}
|
||||
|
||||
@@ -127,8 +126,10 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||
TValue *o; /* entry for `str' */
|
||||
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
||||
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
|
||||
o = luaH_setstr(L, ls->fs->h, ts);
|
||||
if (ttisnil(o)) {
|
||||
o = luaH_set(L, ls->fs->h, L->top - 1);
|
||||
if (ttisnil(o)) { /* not in use yet? (see 'addK') */
|
||||
/* boolean value does not need GC barrier;
|
||||
table has no metatable, so it does not need to invalidate cache */
|
||||
setbvalue(o, 1); /* t[string] = true */
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
@@ -152,9 +153,11 @@ static void inclinenumber (LexState *ls) {
|
||||
}
|
||||
|
||||
|
||||
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
|
||||
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
||||
int firstchar) {
|
||||
ls->decpoint = '.';
|
||||
ls->L = L;
|
||||
ls->current = firstchar;
|
||||
ls->lookahead.token = TK_EOS; /* no look-ahead token */
|
||||
ls->z = z;
|
||||
ls->fs = NULL;
|
||||
@@ -164,7 +167,6 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
|
||||
ls->envn = luaS_new(L, LUA_ENV); /* create env name */
|
||||
luaS_fix(ls->envn); /* never collect this name */
|
||||
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
|
||||
next(ls); /* read first char */
|
||||
}
|
||||
|
||||
|
||||
@@ -185,7 +187,7 @@ static int check_next (LexState *ls, const char *set) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
/*
|
||||
** change all characters 'from' in buffer to 'to'
|
||||
*/
|
||||
static void buffreplace (LexState *ls, char from, char to) {
|
||||
@@ -200,6 +202,9 @@ static void buffreplace (LexState *ls, char from, char to) {
|
||||
#define getlocaledecpoint() (localeconv()->decimal_point[0])
|
||||
#endif
|
||||
|
||||
|
||||
#define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e)
|
||||
|
||||
/*
|
||||
** in case of format error, try to change decimal point separator to
|
||||
** the one defined in the current locale and check again
|
||||
@@ -208,7 +213,7 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) {
|
||||
char old = ls->decpoint;
|
||||
ls->decpoint = getlocaledecpoint();
|
||||
buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
|
||||
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
|
||||
if (!buff2d(ls->buff, &seminfo->r)) {
|
||||
/* format error with correct decimal point: no more options */
|
||||
buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
|
||||
lexerror(ls, "malformed number", TK_NUMBER);
|
||||
@@ -217,16 +222,27 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) {
|
||||
|
||||
|
||||
/* LUA_NUMBER */
|
||||
/*
|
||||
** this function is quite liberal in what it accepts, as 'luaO_str2d'
|
||||
** will reject ill-formed numerals.
|
||||
*/
|
||||
static void read_numeral (LexState *ls, SemInfo *seminfo) {
|
||||
const char *expo = "Ee";
|
||||
int first = ls->current;
|
||||
lua_assert(lisdigit(ls->current));
|
||||
do {
|
||||
save_and_next(ls);
|
||||
if (check_next(ls, "EePp")) /* exponent part? */
|
||||
save_and_next(ls);
|
||||
if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */
|
||||
expo = "Pp";
|
||||
for (;;) {
|
||||
if (check_next(ls, expo)) /* exponent part? */
|
||||
check_next(ls, "+-"); /* optional exponent sign */
|
||||
} while (lislalnum(ls->current) || ls->current == '.');
|
||||
if (lisxdigit(ls->current) || ls->current == '.')
|
||||
save_and_next(ls);
|
||||
else break;
|
||||
}
|
||||
save(ls, '\0');
|
||||
buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
|
||||
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
|
||||
if (!buff2d(ls->buff, &seminfo->r)) /* format error? */
|
||||
trydecpoint(ls, seminfo); /* try to update decimal point separator */
|
||||
}
|
||||
|
||||
@@ -283,45 +299,41 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
||||
}
|
||||
|
||||
|
||||
static int hexavalue (int c) {
|
||||
if (lisdigit(c)) return c - '0';
|
||||
else if (lisupper(c)) return c - 'A' + 10;
|
||||
else return c - 'a' + 10;
|
||||
static void escerror (LexState *ls, int *c, int n, const char *msg) {
|
||||
int i;
|
||||
luaZ_resetbuffer(ls->buff); /* prepare error message */
|
||||
save(ls, '\\');
|
||||
for (i = 0; i < n && c[i] != EOZ; i++)
|
||||
save(ls, c[i]);
|
||||
lexerror(ls, msg, TK_STRING);
|
||||
}
|
||||
|
||||
|
||||
static int readhexaesc (LexState *ls) {
|
||||
int c1, c2 = EOZ;
|
||||
if (!lisxdigit(c1 = next(ls)) || !lisxdigit(c2 = next(ls))) {
|
||||
luaZ_resetbuffer(ls->buff); /* prepare error message */
|
||||
save(ls, '\\'); save(ls, 'x');
|
||||
if (c1 != EOZ) save(ls, c1);
|
||||
if (c2 != EOZ) save(ls, c2);
|
||||
lexerror(ls, "hexadecimal digit expected", TK_STRING);
|
||||
int c[3], i; /* keep input for error message */
|
||||
int r = 0; /* result accumulator */
|
||||
c[0] = 'x'; /* for error message */
|
||||
for (i = 1; i < 3; i++) { /* read two hexa digits */
|
||||
c[i] = next(ls);
|
||||
if (!lisxdigit(c[i]))
|
||||
escerror(ls, c, i + 1, "hexadecimal digit expected");
|
||||
r = (r << 4) + luaO_hexavalue(c[i]);
|
||||
}
|
||||
return (hexavalue(c1) << 4) + hexavalue(c2);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
static int readdecesc (LexState *ls) {
|
||||
int c1 = ls->current, c2, c3;
|
||||
int c = c1 - '0';
|
||||
if (lisdigit(c2 = next(ls))) {
|
||||
c = 10*c + c2 - '0';
|
||||
if (lisdigit(c3 = next(ls))) {
|
||||
c = 10*c + c3 - '0';
|
||||
if (c > UCHAR_MAX) {
|
||||
luaZ_resetbuffer(ls->buff); /* prepare error message */
|
||||
save(ls, '\\');
|
||||
save(ls, c1); save(ls, c2); save(ls, c3);
|
||||
lexerror(ls, "decimal escape too large", TK_STRING);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
int c[3], i;
|
||||
int r = 0; /* result accumulator */
|
||||
for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */
|
||||
c[i] = ls->current;
|
||||
r = 10*r + c[i] - '0';
|
||||
next(ls);
|
||||
}
|
||||
/* else, has read one character that was not a digit */
|
||||
zungetc(ls->z); /* return it to input stream */
|
||||
return c;
|
||||
if (r > UCHAR_MAX)
|
||||
escerror(ls, c, i, "decimal escape too large");
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -340,36 +352,38 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
|
||||
int c; /* final character to be saved */
|
||||
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 'x': c = readhexaesc(ls); break;
|
||||
case '\n':
|
||||
case '\r': save(ls, '\n'); inclinenumber(ls); continue;
|
||||
case EOZ: continue; /* will raise an error next loop */
|
||||
case '*': { /* skip following span of spaces */
|
||||
next(ls); /* skip the '*' */
|
||||
case 'a': c = '\a'; goto read_save;
|
||||
case 'b': c = '\b'; goto read_save;
|
||||
case 'f': c = '\f'; goto read_save;
|
||||
case 'n': c = '\n'; goto read_save;
|
||||
case 'r': c = '\r'; goto read_save;
|
||||
case 't': c = '\t'; goto read_save;
|
||||
case 'v': c = '\v'; goto read_save;
|
||||
case 'x': c = readhexaesc(ls); goto read_save;
|
||||
case '\n': case '\r':
|
||||
inclinenumber(ls); c = '\n'; goto only_save;
|
||||
case '\\': case '\"': case '\'':
|
||||
c = ls->current; goto read_save;
|
||||
case EOZ: goto no_save; /* will raise an error next loop */
|
||||
case 'z': { /* zap following span of spaces */
|
||||
next(ls); /* skip the 'z' */
|
||||
while (lisspace(ls->current)) {
|
||||
if (currIsNewline(ls)) inclinenumber(ls);
|
||||
else next(ls);
|
||||
}
|
||||
continue; /* do not save 'c' */
|
||||
goto no_save;
|
||||
}
|
||||
default: {
|
||||
if (!lisdigit(ls->current))
|
||||
c = ls->current; /* handles \\, \", \', and \? */
|
||||
else /* digital escape \ddd */
|
||||
c = readdecesc(ls);
|
||||
break;
|
||||
escerror(ls, &ls->current, 1, "invalid escape sequence");
|
||||
/* digital escape \ddd */
|
||||
c = readdecesc(ls);
|
||||
goto only_save;
|
||||
}
|
||||
}
|
||||
next(ls);
|
||||
save(ls, c);
|
||||
break;
|
||||
read_save: next(ls); /* read next character */
|
||||
only_save: save(ls, c); /* save 'c' */
|
||||
no_save: break;
|
||||
}
|
||||
default:
|
||||
save_and_next(ls);
|
||||
@@ -441,6 +455,11 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
||||
if (ls->current != '=') return '~';
|
||||
else { next(ls); return TK_NE; }
|
||||
}
|
||||
case ':': {
|
||||
next(ls);
|
||||
if (ls->current != ':') return ':';
|
||||
else { next(ls); return TK_DBCOLON; }
|
||||
}
|
||||
case '"': case '\'': { /* short literal strings */
|
||||
read_string(ls, ls->current, seminfo);
|
||||
return TK_STRING;
|
||||
@@ -472,8 +491,8 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
||||
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
|
||||
luaZ_bufflen(ls->buff));
|
||||
seminfo->ts = ts;
|
||||
if (ts->tsv.reserved > 0) /* reserved word? */
|
||||
return ts->tsv.reserved - 1 + FIRST_RESERVED;
|
||||
if (isreserved(ts)) /* reserved word? */
|
||||
return ts->tsv.extra - 1 + FIRST_RESERVED;
|
||||
else {
|
||||
return TK_NAME;
|
||||
}
|
||||
|
||||
18
llex.h
18
llex.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.64 2010/03/13 15:55:42 roberto Exp roberto $
|
||||
** $Id: llex.h,v 1.71 2011/06/20 16:52:48 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,8 +13,6 @@
|
||||
|
||||
#define FIRST_RESERVED 257
|
||||
|
||||
/* maximum length of a reserved word */
|
||||
#define TOKEN_LEN (sizeof("function")/sizeof(char))
|
||||
|
||||
|
||||
/*
|
||||
@@ -25,10 +23,10 @@ enum RESERVED {
|
||||
/* terminal symbols denoted by reserved words */
|
||||
TK_AND = FIRST_RESERVED, TK_BREAK,
|
||||
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
|
||||
TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
|
||||
TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
|
||||
TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
|
||||
/* other terminal symbols */
|
||||
TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_EOS,
|
||||
TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS,
|
||||
TK_NUMBER, TK_NAME, TK_STRING
|
||||
};
|
||||
|
||||
@@ -48,17 +46,19 @@ typedef struct Token {
|
||||
} Token;
|
||||
|
||||
|
||||
/* state of the lexer plus state of the parser when shared by all
|
||||
functions */
|
||||
typedef struct LexState {
|
||||
int current; /* current character (charint) */
|
||||
int linenumber; /* input line counter */
|
||||
int lastline; /* line of last token `consumed' */
|
||||
Token t; /* current token */
|
||||
Token lookahead; /* look ahead token */
|
||||
struct FuncState *fs; /* `FuncState' is private to the parser */
|
||||
struct FuncState *fs; /* current function (parser) */
|
||||
struct lua_State *L;
|
||||
ZIO *z; /* input stream */
|
||||
Mbuffer *buff; /* buffer for tokens */
|
||||
struct Varlist *varl; /* list of all active local variables */
|
||||
struct Dyndata *dyd; /* dynamic structures used by the parser */
|
||||
TString *source; /* current source name */
|
||||
TString *envn; /* environment variable name */
|
||||
char decpoint; /* locale decimal point */
|
||||
@@ -67,11 +67,11 @@ typedef struct LexState {
|
||||
|
||||
LUAI_FUNC void luaX_init (lua_State *L);
|
||||
LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
|
||||
TString *source);
|
||||
TString *source, int firstchar);
|
||||
LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
|
||||
LUAI_FUNC void luaX_next (LexState *ls);
|
||||
LUAI_FUNC int luaX_lookahead (LexState *ls);
|
||||
LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s);
|
||||
LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s);
|
||||
LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);
|
||||
|
||||
|
||||
|
||||
64
llimits.h
64
llimits.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.83 2010/11/03 15:16:17 roberto Exp roberto $
|
||||
** $Id: llimits.h,v 1.98 2012/05/11 14:10:50 roberto Exp roberto $
|
||||
** Limits, basic types, and some other `installation-dependent' definitions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -30,7 +30,8 @@ typedef unsigned char lu_byte;
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0)-2)
|
||||
|
||||
#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
|
||||
#define MIN_LMEM ((l_mem)~((~(lu_mem)0)>>1))
|
||||
|
||||
#define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2))
|
||||
|
||||
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
|
||||
@@ -59,21 +60,28 @@ typedef LUAI_UACNUMBER l_uacNumber;
|
||||
/* internal assertions for in-house debugging */
|
||||
#if defined(lua_assert)
|
||||
#define check_exp(c,e) (lua_assert(c), (e))
|
||||
/* to avoid problems with conditions too long */
|
||||
#define lua_longassert(c) { if (!(c)) lua_assert(0); }
|
||||
#else
|
||||
#define lua_assert(c) /* empty */
|
||||
#define lua_assert(c) ((void)0)
|
||||
#define check_exp(c,e) (e)
|
||||
#define lua_longassert(c) ((void)0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
** assertion for checking API calls
|
||||
*/
|
||||
#if !defined(luai_apicheck)
|
||||
|
||||
#if defined(LUA_USE_APICHECK)
|
||||
#include <assert.h>
|
||||
#define luai_apicheck(L,e) { (void)L; assert(e); }
|
||||
#elif !defined(luai_apicheck)
|
||||
#define luai_apicheck(L,e) assert(e)
|
||||
#else
|
||||
#define luai_apicheck(L,e) lua_assert(e)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
|
||||
|
||||
|
||||
@@ -87,6 +95,20 @@ typedef LUAI_UACNUMBER l_uacNumber;
|
||||
#define cast_byte(i) cast(lu_byte, (i))
|
||||
#define cast_num(i) cast(lua_Number, (i))
|
||||
#define cast_int(i) cast(int, (i))
|
||||
#define cast_uchar(i) cast(unsigned char, (i))
|
||||
|
||||
|
||||
/*
|
||||
** non-return type
|
||||
*/
|
||||
#if defined(__GNUC__)
|
||||
#define l_noret void __attribute__((noreturn))
|
||||
#elif defined(_MSC_VER)
|
||||
#define l_noret void __declspec(noreturn)
|
||||
#else
|
||||
#define l_noret void
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@@ -173,6 +195,9 @@ typedef lu_int32 Instruction;
|
||||
** lua_number2integer is a macro to convert lua_Number to lua_Integer.
|
||||
** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
|
||||
** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
|
||||
** luai_hashnum is a macro to hash a lua_Number value into an integer.
|
||||
** The hash must be deterministic and give reasonable values for
|
||||
** both small and large values (outside the range of integers).
|
||||
*/
|
||||
|
||||
#if defined(MS_ASMTRICK) /* { */
|
||||
@@ -186,27 +211,36 @@ typedef lu_int32 Instruction;
|
||||
|
||||
#elif defined(LUA_IEEE754TRICK) /* }{ */
|
||||
/* the next trick should work on any machine using IEEE754 with
|
||||
a 32-bit integer type */
|
||||
a 32-bit int type */
|
||||
|
||||
union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
|
||||
|
||||
#if !defined(LUA_IEEEENDIAN) /* { */
|
||||
#define LUAI_EXTRAIEEE \
|
||||
static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)};
|
||||
#define LUA_IEEEENDIAN (ieeeendian.l_p[1] == 33)
|
||||
#define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33)
|
||||
#else
|
||||
#define LUA_IEEEENDIANLOC LUA_IEEEENDIAN
|
||||
#define LUAI_EXTRAIEEE /* empty */
|
||||
#endif /* } */
|
||||
|
||||
#define lua_number2int32(i,n,t) \
|
||||
{ LUAI_EXTRAIEEE \
|
||||
volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
|
||||
(i) = (t)u.l_p[LUA_IEEEENDIAN]; }
|
||||
(i) = (t)u.l_p[LUA_IEEEENDIANLOC]; }
|
||||
|
||||
#define luai_hashnum(i,n) \
|
||||
{ volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \
|
||||
(i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */
|
||||
|
||||
#define lua_number2int(i,n) lua_number2int32(i, n, int)
|
||||
#define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer)
|
||||
#define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
|
||||
|
||||
/* the trick can be expanded to lua_Integer when it is a 32-bit value */
|
||||
#if defined(LUA_IEEELL)
|
||||
#define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer)
|
||||
#endif
|
||||
|
||||
#endif /* } */
|
||||
|
||||
|
||||
@@ -241,14 +275,8 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** luai_hashnum is a macro do hash a lua_Number value into an integer.
|
||||
** The hash must be deterministic and give reasonable values for
|
||||
** both small and large values (outside the range of integers).
|
||||
** It is used only in ltable.c.
|
||||
*/
|
||||
|
||||
#if !defined(luai_hashnum) /* { */
|
||||
#if defined(ltable_c) && !defined(luai_hashnum)
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
@@ -257,7 +285,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
|
||||
n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
|
||||
lua_number2int(i, n); i += e; }
|
||||
|
||||
#endif /* } */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -275,7 +303,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
|
||||
#define condchangemem(L) condmovestack(L)
|
||||
#else
|
||||
#define condchangemem(L) \
|
||||
((void)(gcstopped(G(L)) || (luaC_fullgc(L, 0), 1)))
|
||||
((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
21
lmathlib.c
21
lmathlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.78 2010/11/12 15:47:34 roberto Exp roberto $
|
||||
** $Id: lmathlib.c,v 1.80 2011/07/05 12:49:35 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -17,17 +17,17 @@
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#undef PI
|
||||
#define PI (3.14159265358979323846)
|
||||
#define RADIANS_PER_DEGREE (PI/180.0)
|
||||
|
||||
|
||||
/* macro 'l_tg' allows the addition of an 'l' or 'f' to all math operations */
|
||||
#if !defined(l_tg)
|
||||
#define l_tg(x) (x)
|
||||
#endif
|
||||
|
||||
|
||||
#undef PI
|
||||
#define PI (l_tg(3.1415926535897932384626433832795))
|
||||
#define RADIANS_PER_DEGREE (PI/180.0)
|
||||
|
||||
|
||||
|
||||
static int math_abs (lua_State *L) {
|
||||
lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1)));
|
||||
@@ -134,15 +134,12 @@ static int math_log (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if defined(LUA_COMPAT_LOG10)
|
||||
static int math_log10 (lua_State *L) {
|
||||
#if !defined(LUA_COMPAT_LOG10)
|
||||
return luaL_error(L, "function " LUA_QL("log10")
|
||||
" is deprecated; use log(x, 10) instead");
|
||||
#else
|
||||
lua_pushnumber(L, l_tg(log10)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
static int math_exp (lua_State *L) {
|
||||
lua_pushnumber(L, l_tg(exp)(luaL_checknumber(L, 1)));
|
||||
@@ -252,7 +249,9 @@ static const luaL_Reg mathlib[] = {
|
||||
{"fmod", math_fmod},
|
||||
{"frexp", math_frexp},
|
||||
{"ldexp", math_ldexp},
|
||||
#if defined(LUA_COMPAT_LOG10)
|
||||
{"log10", math_log10},
|
||||
#endif
|
||||
{"log", math_log},
|
||||
{"max", math_max},
|
||||
{"min", math_min},
|
||||
|
||||
31
lmem.c
31
lmem.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.78 2010/05/04 18:10:02 roberto Exp roberto $
|
||||
** $Id: lmem.c,v 1.83 2011/11/30 12:42:49 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -63,9 +63,8 @@ void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
|
||||
}
|
||||
|
||||
|
||||
void *luaM_toobig (lua_State *L) {
|
||||
l_noret luaM_toobig (lua_State *L) {
|
||||
luaG_runerror(L, "memory allocation error: block too big");
|
||||
return NULL; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
@@ -79,14 +78,14 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
||||
size_t realosize = (block) ? osize : 0;
|
||||
lua_assert((realosize == 0) == (block == NULL));
|
||||
#if defined(HARDMEMTESTS)
|
||||
if (nsize > realosize && !gcstopped(g))
|
||||
if (nsize > realosize && g->gcrunning)
|
||||
luaC_fullgc(L, 1); /* force a GC whenever possible */
|
||||
#endif
|
||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
||||
if (newblock == NULL && nsize > 0) {
|
||||
api_check(L, nsize > realosize,
|
||||
"realloc cannot fail when shrinking a block");
|
||||
if (!gcstopped(g)) {
|
||||
if (g->gcrunning) {
|
||||
luaC_fullgc(L, 1); /* try to free some memory... */
|
||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
||||
}
|
||||
@@ -94,27 +93,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
||||
luaD_throw(L, LUA_ERRMEM);
|
||||
}
|
||||
lua_assert((nsize == 0) == (newblock == NULL));
|
||||
g->totalbytes = (g->totalbytes - realosize) + nsize;
|
||||
if (!gcstopped(g))
|
||||
g->GCdebt += nsize; /* give some credit to garbage collector */
|
||||
#if defined(TRACEMEM)
|
||||
{ /* auxiliary patch to monitor garbage collection.
|
||||
** To plot, gnuplot with following command:
|
||||
** plot TRACEMEM using 1:2 with lines, TRACEMEM using 1:3 with lines
|
||||
*/
|
||||
static unsigned long total = 0; /* our "time" */
|
||||
static FILE *f = NULL; /* output file */
|
||||
total++; /* "time" always grows */
|
||||
if ((total % 200) == 0) {
|
||||
if (f == NULL) f = fopen(TRACEMEM, "w");
|
||||
fprintf(f, "%lu %u %d %d\n", total,
|
||||
g->totalbytes,
|
||||
gcstopped(g) ? 0 : g->GCdebt,
|
||||
g->gcstate * 1000);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
g->GCdebt = (g->GCdebt + nsize) - realosize;
|
||||
return newblock;
|
||||
}
|
||||
|
||||
|
||||
10
lmem.h
10
lmem.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.35 2009/12/16 16:42:58 roberto Exp roberto $
|
||||
** $Id: lmem.h,v 1.37 2011/11/30 12:42:49 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -15,9 +15,9 @@
|
||||
|
||||
|
||||
#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))
|
||||
((cast(size_t, (n)+1) > MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \
|
||||
(luaM_toobig(L), (void *)0) : \
|
||||
luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
|
||||
|
||||
#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
|
||||
#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
|
||||
@@ -37,7 +37,7 @@
|
||||
#define luaM_reallocvector(L, v,oldn,n,t) \
|
||||
((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
|
||||
|
||||
LUAI_FUNC void *luaM_toobig (lua_State *L);
|
||||
LUAI_FUNC l_noret luaM_toobig (lua_State *L);
|
||||
|
||||
/* not to be called directly */
|
||||
LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
|
||||
|
||||
318
loadlib.c
318
loadlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: loadlib.c,v 1.93 2010/11/10 18:05:36 roberto Exp roberto $
|
||||
** $Id: loadlib.c,v 1.110 2012/04/26 19:38:52 roberto Exp roberto $
|
||||
** Dynamic library loader for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
**
|
||||
@@ -9,6 +9,14 @@
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** if needed, includes windows header before everything else
|
||||
*/
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -62,6 +70,20 @@
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** LUA_CSUBSEP is the character that replaces dots in submodule names
|
||||
** when searching for a C loader.
|
||||
** LUA_LSUBSEP is the character that replaces dots in submodule names
|
||||
** when searching for a Lua loader.
|
||||
*/
|
||||
#if !defined(LUA_CSUBSEP)
|
||||
#define LUA_CSUBSEP LUA_DIRSEP
|
||||
#endif
|
||||
|
||||
#if !defined(LUA_LSUBSEP)
|
||||
#define LUA_LSUBSEP LUA_DIRSEP
|
||||
#endif
|
||||
|
||||
|
||||
/* prefix for open functions in C libraries */
|
||||
#define LUA_POF "luaopen_"
|
||||
@@ -70,9 +92,9 @@
|
||||
#define LUA_OFSEP "_"
|
||||
|
||||
|
||||
#define LIBPREFIX "LOADLIB: "
|
||||
/* table (in the registry) that keeps handles for all loaded C libraries */
|
||||
#define CLIBS "_CLIBS"
|
||||
|
||||
#define POF LUA_POF
|
||||
#define LIB_FAIL "open"
|
||||
|
||||
|
||||
@@ -110,7 +132,7 @@ static void ll_unloadlib (void *lib) {
|
||||
|
||||
|
||||
static void *ll_load (lua_State *L, const char *path, int seeglb) {
|
||||
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : 0));
|
||||
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
|
||||
if (lib == NULL) lua_pushstring(L, dlerror());
|
||||
return lib;
|
||||
}
|
||||
@@ -133,8 +155,6 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#undef setprogdir
|
||||
|
||||
/*
|
||||
@@ -164,7 +184,7 @@ static void pusherror (lua_State *L) {
|
||||
int error = GetLastError();
|
||||
char buffer[128];
|
||||
if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, error, 0, buffer, sizeof(buffer), NULL))
|
||||
NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))
|
||||
lua_pushstring(L, buffer);
|
||||
else
|
||||
lua_pushfstring(L, "system error %d\n", error);
|
||||
@@ -177,7 +197,7 @@ static void ll_unloadlib (void *lib) {
|
||||
|
||||
static void *ll_load (lua_State *L, const char *path, int seeglb) {
|
||||
HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
|
||||
(void)(seeglb); /* symbols are 'global' by default */
|
||||
(void)(seeglb); /* not used: symbols are 'global' by default */
|
||||
if (lib == NULL) pusherror(L);
|
||||
return lib;
|
||||
}
|
||||
@@ -207,19 +227,19 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
|
||||
|
||||
|
||||
static void ll_unloadlib (void *lib) {
|
||||
(void)(lib); /* to avoid warnings */
|
||||
(void)(lib); /* not used */
|
||||
}
|
||||
|
||||
|
||||
static void *ll_load (lua_State *L, const char *path, int seeglb) {
|
||||
(void)(path); (void)(seeglb); /* to avoid warnings */
|
||||
(void)(path); (void)(seeglb); /* not used */
|
||||
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 */
|
||||
(void)(lib); (void)(sym); /* not used */
|
||||
lua_pushliteral(L, DLMSG);
|
||||
return NULL;
|
||||
}
|
||||
@@ -228,48 +248,54 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
|
||||
#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); /* remove result from gettable */
|
||||
plib = (void **)lua_newuserdata(L, sizeof(const void *));
|
||||
*plib = NULL;
|
||||
luaL_setmetatable(L, "_LOADLIB");
|
||||
lua_pushfstring(L, "%s%s", LIBPREFIX, path);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, LUA_REGISTRYINDEX);
|
||||
}
|
||||
static void *ll_checkclib (lua_State *L, const char *path) {
|
||||
void *plib;
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_getfield(L, -1, path);
|
||||
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
||||
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
||||
return plib;
|
||||
}
|
||||
|
||||
|
||||
static void ll_addtoclib (lua_State *L, const char *path, void *plib) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_pushlightuserdata(L, plib);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
||||
lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
|
||||
lua_pop(L, 1); /* pop CLIBS table */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** __gc tag method: calls library's `ll_unloadlib' function with the lib
|
||||
** handle
|
||||
** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib
|
||||
** handles in list CLIBS
|
||||
*/
|
||||
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 */
|
||||
int n = luaL_len(L, 1);
|
||||
for (; n >= 1; n--) { /* for each handle, in reverse order */
|
||||
lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
|
||||
ll_unloadlib(lua_touserdata(L, -1));
|
||||
lua_pop(L, 1); /* pop handle */
|
||||
}
|
||||
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, *sym == '*');
|
||||
if (*reg == NULL) return ERRLIB; /* unable to load library */
|
||||
void *reg = ll_checkclib(L, path); /* check loaded C libraries */
|
||||
if (reg == NULL) { /* must load library? */
|
||||
reg = ll_load(L, path, *sym == '*');
|
||||
if (reg == NULL) return ERRLIB; /* unable to load library */
|
||||
ll_addtoclib(L, path, reg);
|
||||
}
|
||||
if (*sym == '*') { /* loading only library (no function)? */
|
||||
lua_pushboolean(L, 1); /* return 'true' */
|
||||
return 0; /* no errors */
|
||||
}
|
||||
else {
|
||||
lua_CFunction f = ll_sym(L, *reg, sym);
|
||||
lua_CFunction f = ll_sym(L, reg, sym);
|
||||
if (f == NULL)
|
||||
return ERRFUNC; /* unable to find function */
|
||||
lua_pushcfunction(L, f); /* else create new function */
|
||||
@@ -321,9 +347,13 @@ static const char *pushnexttemplate (lua_State *L, const char *path) {
|
||||
|
||||
|
||||
static const char *searchpath (lua_State *L, const char *name,
|
||||
const char *path) {
|
||||
name = luaL_gsub(L, name, ".", LUA_DIRSEP);
|
||||
lua_pushliteral(L, ""); /* error accumulator */
|
||||
const char *path,
|
||||
const char *sep,
|
||||
const char *dirsep) {
|
||||
luaL_Buffer msg; /* to build error message */
|
||||
luaL_buffinit(L, &msg);
|
||||
if (*sep != '\0') /* non-empty separator? */
|
||||
name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
|
||||
while ((path = pushnexttemplate(L, path)) != NULL) {
|
||||
const char *filename = luaL_gsub(L, lua_tostring(L, -1),
|
||||
LUA_PATH_MARK, name);
|
||||
@@ -332,14 +362,18 @@ static const char *searchpath (lua_State *L, const char *name,
|
||||
return filename; /* return that file name */
|
||||
lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
|
||||
lua_remove(L, -2); /* remove file name */
|
||||
lua_concat(L, 2); /* add entry to possible error message */
|
||||
luaL_addvalue(&msg); /* concatenate error msg. entry */
|
||||
}
|
||||
luaL_pushresult(&msg); /* create error message */
|
||||
return NULL; /* not found */
|
||||
}
|
||||
|
||||
|
||||
static int ll_searchpath (lua_State *L) {
|
||||
const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2));
|
||||
const char *f = searchpath(L, luaL_checkstring(L, 1),
|
||||
luaL_checkstring(L, 2),
|
||||
luaL_optstring(L, 3, "."),
|
||||
luaL_optstring(L, 4, LUA_DIRSEP));
|
||||
if (f != NULL) return 1;
|
||||
else { /* error message is on top of the stack */
|
||||
lua_pushnil(L);
|
||||
@@ -350,30 +384,35 @@ static int ll_searchpath (lua_State *L) {
|
||||
|
||||
|
||||
static const char *findfile (lua_State *L, const char *name,
|
||||
const char *pname) {
|
||||
const char *pname,
|
||||
const char *dirsep) {
|
||||
const char *path;
|
||||
lua_getfield(L, lua_upvalueindex(1), pname);
|
||||
path = lua_tostring(L, -1);
|
||||
if (path == NULL)
|
||||
luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
|
||||
return searchpath(L, name, path);
|
||||
return searchpath(L, name, path, ".", dirsep);
|
||||
}
|
||||
|
||||
|
||||
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 checkload (lua_State *L, int stat, const char *filename) {
|
||||
if (stat) { /* module loaded successfully? */
|
||||
lua_pushstring(L, filename); /* will be 2nd argument to module */
|
||||
return 2; /* return open function and file name */
|
||||
}
|
||||
else
|
||||
return 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) {
|
||||
static int searcher_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) != LUA_OK)
|
||||
loaderror(L, filename);
|
||||
return 1; /* library loaded successfully */
|
||||
filename = findfile(L, name, "path", LUA_LSUBSEP);
|
||||
if (filename == NULL) return 1; /* module not found in this path */
|
||||
return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
|
||||
}
|
||||
|
||||
|
||||
@@ -385,46 +424,48 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) {
|
||||
if (mark) {
|
||||
int stat;
|
||||
funcname = lua_pushlstring(L, modname, mark - modname);
|
||||
funcname = lua_pushfstring(L, POF"%s", funcname);
|
||||
funcname = lua_pushfstring(L, LUA_POF"%s", funcname);
|
||||
stat = ll_loadfunc(L, filename, funcname);
|
||||
if (stat != ERRFUNC) return stat;
|
||||
modname = mark + 1; /* else go ahead and try old-style name */
|
||||
}
|
||||
funcname = lua_pushfstring(L, POF"%s", modname);
|
||||
funcname = lua_pushfstring(L, LUA_POF"%s", modname);
|
||||
return ll_loadfunc(L, filename, funcname);
|
||||
}
|
||||
|
||||
|
||||
static int loader_C (lua_State *L) {
|
||||
static int searcher_C (lua_State *L) {
|
||||
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 */
|
||||
if (loadfunc(L, filename, name) != 0)
|
||||
loaderror(L, filename);
|
||||
return 1; /* library loaded successfully */
|
||||
const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
|
||||
if (filename == NULL) return 1; /* module not found in this path */
|
||||
return checkload(L, (loadfunc(L, filename, name) == 0), filename);
|
||||
}
|
||||
|
||||
|
||||
static int loader_Croot (lua_State *L) {
|
||||
static int searcher_Croot (lua_State *L) {
|
||||
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");
|
||||
filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
|
||||
if (filename == NULL) return 1; /* root not found */
|
||||
if ((stat = loadfunc(L, filename, name)) != 0) {
|
||||
if (stat != ERRFUNC) loaderror(L, filename); /* real error */
|
||||
lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
|
||||
name, filename);
|
||||
return 1; /* function not found */
|
||||
if (stat != ERRFUNC)
|
||||
return checkload(L, 0, filename); /* real error */
|
||||
else { /* open function not found */
|
||||
lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
|
||||
name, filename);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
lua_pushstring(L, filename); /* will be 2nd argument to module */
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
static int loader_preload (lua_State *L) {
|
||||
static int searcher_preload (lua_State *L) {
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
lua_getfield(L, -1, name);
|
||||
@@ -434,35 +475,49 @@ static int loader_preload (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static void findloader (lua_State *L, const char *name) {
|
||||
int i;
|
||||
luaL_Buffer msg; /* to build error message */
|
||||
luaL_buffinit(L, &msg);
|
||||
lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */
|
||||
if (!lua_istable(L, 3))
|
||||
luaL_error(L, LUA_QL("package.searchers") " must be a table");
|
||||
/* iterate over available searchers to find a loader */
|
||||
for (i = 1; ; i++) {
|
||||
lua_rawgeti(L, 3, i); /* get a searcher */
|
||||
if (lua_isnil(L, -1)) { /* no more searchers? */
|
||||
lua_pop(L, 1); /* remove nil */
|
||||
luaL_pushresult(&msg); /* create error message */
|
||||
luaL_error(L, "module " LUA_QS " not found:%s",
|
||||
name, lua_tostring(L, -1));
|
||||
}
|
||||
lua_pushstring(L, name);
|
||||
lua_call(L, 1, 2); /* call it */
|
||||
if (lua_isfunction(L, -2)) /* did it find a loader? */
|
||||
return; /* module loader found */
|
||||
else if (lua_isstring(L, -2)) { /* searcher returned error message? */
|
||||
lua_pop(L, 1); /* remove extra return */
|
||||
luaL_addvalue(&msg); /* concatenate error message */
|
||||
}
|
||||
else
|
||||
lua_pop(L, 2); /* remove both returns */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
lua_getfield(L, 2, name); /* _LOADED[name] */
|
||||
if (lua_toboolean(L, -1)) /* is it there? */
|
||||
return 1; /* package is already loaded */
|
||||
/* else must load it; iterate over available loaders */
|
||||
lua_getfield(L, lua_upvalueindex(1), "loaders");
|
||||
if (!lua_istable(L, -1))
|
||||
luaL_error(L, LUA_QL("package.loaders") " must be a table");
|
||||
lua_pushliteral(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_pushstring(L, name); /* pass name as argument to module */
|
||||
lua_call(L, 1, 1); /* run loaded module */
|
||||
/* else must load package */
|
||||
lua_pop(L, 1); /* remove 'getfield' result */
|
||||
findloader(L, name);
|
||||
lua_pushstring(L, name); /* pass name as argument to module loader */
|
||||
lua_insert(L, -2); /* name is 1st argument (before search data) */
|
||||
lua_call(L, 2, 1); /* run loader to load module */
|
||||
if (!lua_isnil(L, -1)) /* non-nil return? */
|
||||
lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
|
||||
lua_getfield(L, 2, name);
|
||||
@@ -503,9 +558,11 @@ static void set_env (lua_State *L) {
|
||||
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);
|
||||
if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */
|
||||
lua_pushvalue(L, i); /* get option (a function) */
|
||||
lua_pushvalue(L, -2); /* module */
|
||||
lua_call(L, 1, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -556,17 +613,6 @@ static int ll_seeall (lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
||||
static int ll_seeall (lua_State *L) {
|
||||
return luaL_error(L, "deprecated function");
|
||||
}
|
||||
|
||||
static int ll_module (lua_State *L) {
|
||||
return luaL_error(L, "deprecated function");
|
||||
}
|
||||
|
||||
#endif
|
||||
/* }====================================================== */
|
||||
|
||||
@@ -575,12 +621,25 @@ static int ll_module (lua_State *L) {
|
||||
/* auxiliary mark (for internal use) */
|
||||
#define AUXMARK "\1"
|
||||
|
||||
|
||||
/*
|
||||
** return registry.LUA_NOENV as a boolean
|
||||
*/
|
||||
static int noenv (lua_State *L) {
|
||||
int b;
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
|
||||
b = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1); /* remove value */
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
static void setpath (lua_State *L, const char *fieldname, const char *envname1,
|
||||
const char *envname2, const char *def) {
|
||||
const char *path = getenv(envname1);
|
||||
if (path == NULL) /* no environment variable? */
|
||||
path = getenv(envname2); /* try alternative name */
|
||||
if (path == NULL) /* no environment variable? */
|
||||
if (path == NULL || noenv(L)) /* no environment variable? */
|
||||
lua_pushstring(L, def); /* use default */
|
||||
else {
|
||||
/* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
|
||||
@@ -597,39 +656,52 @@ static void setpath (lua_State *L, const char *fieldname, const char *envname1,
|
||||
static const luaL_Reg pk_funcs[] = {
|
||||
{"loadlib", ll_loadlib},
|
||||
{"searchpath", ll_searchpath},
|
||||
#if defined(LUA_COMPAT_MODULE)
|
||||
{"seeall", ll_seeall},
|
||||
#endif
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
static const luaL_Reg ll_funcs[] = {
|
||||
#if defined(LUA_COMPAT_MODULE)
|
||||
{"module", ll_module},
|
||||
#endif
|
||||
{"require", ll_require},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
static const lua_CFunction loaders[] =
|
||||
{loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
|
||||
static void createsearcherstable (lua_State *L) {
|
||||
static const lua_CFunction searchers[] =
|
||||
{searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
|
||||
int i;
|
||||
/* create 'searchers' table */
|
||||
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
|
||||
/* fill it with pre-defined searchers */
|
||||
for (i=0; searchers[i] != NULL; i++) {
|
||||
lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
|
||||
lua_pushcclosure(L, searchers[i], 1);
|
||||
lua_rawseti(L, -2, i+1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_package (lua_State *L) {
|
||||
int i;
|
||||
/* create new type _LOADLIB */
|
||||
luaL_newmetatable(L, "_LOADLIB");
|
||||
/* create table CLIBS to keep track of loaded C libraries */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_createtable(L, 0, 1); /* metatable for CLIBS */
|
||||
lua_pushcfunction(L, gctm);
|
||||
lua_setfield(L, -2, "__gc");
|
||||
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
||||
lua_setmetatable(L, -2);
|
||||
/* create `package' table */
|
||||
luaL_newlib(L, pk_funcs);
|
||||
/* create `loaders' table */
|
||||
lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
|
||||
/* fill it with pre-defined loaders */
|
||||
for (i=0; loaders[i] != NULL; i++) {
|
||||
lua_pushvalue(L, -2); /* set 'package' as upvalue for all loaders */
|
||||
lua_pushcclosure(L, loaders[i], 1);
|
||||
lua_rawseti(L, -2, i+1);
|
||||
}
|
||||
lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
|
||||
createsearcherstable(L);
|
||||
#if defined(LUA_COMPAT_LOADERS)
|
||||
lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
|
||||
lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */
|
||||
#endif
|
||||
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
|
||||
/* set field 'path' */
|
||||
setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT);
|
||||
/* set field 'cpath' */
|
||||
@@ -639,10 +711,10 @@ LUAMOD_API int luaopen_package (lua_State *L) {
|
||||
LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
|
||||
lua_setfield(L, -2, "config");
|
||||
/* set field `loaded' */
|
||||
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
lua_setfield(L, -2, "loaded");
|
||||
/* set field `preload' */
|
||||
luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
lua_setfield(L, -2, "preload");
|
||||
lua_pushglobaltable(L);
|
||||
lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
|
||||
|
||||
128
lobject.c
128
lobject.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 2.42 2010/10/29 11:13:14 roberto Exp roberto $
|
||||
** $Id: lobject.c,v 2.54 2011/11/30 12:44:26 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -33,7 +33,7 @@ LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
|
||||
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
|
||||
** eeeee != 0 and (xxx) otherwise.
|
||||
*/
|
||||
int luaO_int2fb (lu_int32 x) {
|
||||
int luaO_int2fb (unsigned int x) {
|
||||
int e = 0; /* exponent */
|
||||
if (x < 8) return x;
|
||||
while (x >= 0x10) {
|
||||
@@ -70,28 +70,6 @@ int luaO_ceillog2 (unsigned int 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);
|
||||
case LUA_TSTRING:
|
||||
return rawtsvalue(t1) == rawtsvalue(t2);
|
||||
case LUA_TLCF:
|
||||
return fvalue(t1) == fvalue(t2);
|
||||
default:
|
||||
lua_assert(iscollectable(t1));
|
||||
return gcvalue(t1) == gcvalue(t2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
|
||||
switch (op) {
|
||||
case LUA_OPADD: return luai_numadd(NULL, v1, v2);
|
||||
@@ -106,19 +84,88 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
|
||||
}
|
||||
|
||||
|
||||
static int checkend (const char *s, const char *endptr) {
|
||||
if (endptr == s) return 0; /* no characters converted */
|
||||
while (lisspace(cast(unsigned char, *endptr))) endptr++;
|
||||
return (*endptr == '\0'); /* OK if no trailing characters */
|
||||
int luaO_hexavalue (int c) {
|
||||
if (lisdigit(c)) return c - '0';
|
||||
else return ltolower(c) - 'a' + 10;
|
||||
}
|
||||
|
||||
|
||||
int luaO_str2d (const char *s, lua_Number *result) {
|
||||
#if !defined(lua_strx2number)
|
||||
|
||||
#include <math.h>
|
||||
|
||||
|
||||
static int isneg (const char **s) {
|
||||
if (**s == '-') { (*s)++; return 1; }
|
||||
else if (**s == '+') (*s)++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static lua_Number readhexa (const char **s, lua_Number r, int *count) {
|
||||
for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
|
||||
r = (r * 16.0) + cast_num(luaO_hexavalue(cast_uchar(**s)));
|
||||
(*count)++;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** convert an hexadecimal numeric string to a number, following
|
||||
** C99 specification for 'strtod'
|
||||
*/
|
||||
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
||||
lua_Number r = 0.0;
|
||||
int e = 0, i = 0;
|
||||
int neg = 0; /* 1 if number is negative */
|
||||
*endptr = cast(char *, s); /* nothing is valid yet */
|
||||
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
||||
neg = isneg(&s); /* check signal */
|
||||
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
|
||||
return 0.0; /* invalid format (no '0x') */
|
||||
s += 2; /* skip '0x' */
|
||||
r = readhexa(&s, r, &i); /* read integer part */
|
||||
if (*s == '.') {
|
||||
s++; /* skip dot */
|
||||
r = readhexa(&s, r, &e); /* read fractional part */
|
||||
}
|
||||
if (i == 0 && e == 0)
|
||||
return 0.0; /* invalid format (no digit) */
|
||||
e *= -4; /* each fractional digit divides value by 2^-4 */
|
||||
*endptr = cast(char *, s); /* valid up to here */
|
||||
if (*s == 'p' || *s == 'P') { /* exponent part? */
|
||||
int exp1 = 0;
|
||||
int neg1;
|
||||
s++; /* skip 'p' */
|
||||
neg1 = isneg(&s); /* signal */
|
||||
if (!lisdigit(cast_uchar(*s)))
|
||||
goto ret; /* must have at least one digit */
|
||||
while (lisdigit(cast_uchar(*s))) /* read exponent */
|
||||
exp1 = exp1 * 10 + *(s++) - '0';
|
||||
if (neg1) exp1 = -exp1;
|
||||
e += exp1;
|
||||
}
|
||||
*endptr = cast(char *, s); /* valid up to here */
|
||||
ret:
|
||||
if (neg) r = -r;
|
||||
return ldexp(r, e);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
int luaO_str2d (const char *s, size_t len, lua_Number *result) {
|
||||
char *endptr;
|
||||
*result = lua_str2number(s, &endptr);
|
||||
if (checkend(s, endptr)) return 1; /* conversion OK? */
|
||||
*result = cast_num(strtoul(s, &endptr, 0)); /* try hexadecimal */
|
||||
return checkend(s, endptr);
|
||||
if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
|
||||
return 0;
|
||||
else if (strpbrk(s, "xX")) /* hexa? */
|
||||
*result = lua_strx2number(s, &endptr);
|
||||
else
|
||||
*result = lua_str2number(s, &endptr);
|
||||
if (endptr == s) return 0; /* nothing recognized */
|
||||
while (lisspace(cast_uchar(*endptr))) endptr++;
|
||||
return (endptr == s + len); /* OK if no trailing characters */
|
||||
}
|
||||
|
||||
|
||||
@@ -174,7 +221,6 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
luaG_runerror(L,
|
||||
"invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
|
||||
*(e + 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
n += 2;
|
||||
@@ -196,19 +242,20 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
|
||||
}
|
||||
|
||||
|
||||
/* number of chars of a literal string without the ending \0 */
|
||||
#define LL(x) (sizeof(x)/sizeof(char) - 1)
|
||||
|
||||
#define LL(x) (sizeof(x) - 1)
|
||||
#define RETS "..."
|
||||
#define PRE "[string \""
|
||||
#define POS "\"]"
|
||||
|
||||
#define addstr(a,b,l) ( memcpy(a,b,l), a += (l) )
|
||||
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
|
||||
|
||||
void luaO_chunkid (char *out, const char *source, size_t bufflen) {
|
||||
size_t l = strlen(source);
|
||||
if (*source == '=') { /* 'literal' source */
|
||||
if (l <= bufflen) /* small enough? */
|
||||
memcpy(out, source + 1, l);
|
||||
memcpy(out, source + 1, l * sizeof(char));
|
||||
else { /* truncate it */
|
||||
addstr(out, source + 1, bufflen - 1);
|
||||
*out = '\0';
|
||||
@@ -216,11 +263,11 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) {
|
||||
}
|
||||
else if (*source == '@') { /* file name */
|
||||
if (l <= bufflen) /* small enough? */
|
||||
memcpy(out, source + 1, l);
|
||||
memcpy(out, source + 1, l * sizeof(char));
|
||||
else { /* add '...' before rest of name */
|
||||
addstr(out, RETS, LL(RETS));
|
||||
bufflen -= LL(RETS);
|
||||
memcpy(out, source + 1 + l - bufflen, bufflen);
|
||||
memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
|
||||
}
|
||||
}
|
||||
else { /* string; format as [string "source"] */
|
||||
@@ -236,6 +283,7 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) {
|
||||
addstr(out, source, l);
|
||||
addstr(out, RETS, LL(RETS));
|
||||
}
|
||||
memcpy(out, POS, LL(POS) + 1);
|
||||
memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
377
lobject.h
377
lobject.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 2.41 2010/06/04 13:25:10 roberto Exp roberto $
|
||||
** $Id: lobject.h,v 2.69 2012/05/08 13:53:33 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -23,12 +23,47 @@
|
||||
#define LUA_TUPVAL (LUA_NUMTAGS+1)
|
||||
#define LUA_TDEADKEY (LUA_NUMTAGS+2)
|
||||
|
||||
/*
|
||||
** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
|
||||
*/
|
||||
#define LUA_TOTALTAGS (LUA_TUPVAL+2)
|
||||
|
||||
|
||||
/*
|
||||
** Variant tag for light C functions (negative to be considered
|
||||
** non collectable by 'iscollectable')
|
||||
** tags for Tagged Values have the following use of bits:
|
||||
** bits 0-3: actual tag (a LUA_T* value)
|
||||
** bits 4-5: variant bits
|
||||
** bit 6: whether value is collectable
|
||||
*/
|
||||
#define LUA_TLCF (~0x0F | LUA_TFUNCTION)
|
||||
|
||||
#define VARBITS (3 << 4)
|
||||
|
||||
|
||||
/*
|
||||
** LUA_TFUNCTION variants:
|
||||
** 0 - Lua function
|
||||
** 1 - light C function
|
||||
** 2 - regular C function (closure)
|
||||
*/
|
||||
|
||||
/* Variant tags for functions */
|
||||
#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
|
||||
#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
|
||||
#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
|
||||
|
||||
|
||||
/*
|
||||
** LUA_TSTRING variants */
|
||||
#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
|
||||
#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
|
||||
|
||||
|
||||
/* Bit mark for collectable types */
|
||||
#define BIT_ISCOLLECTABLE (1 << 6)
|
||||
|
||||
/* mark a tag as collectable */
|
||||
#define ctb(t) ((t) | BIT_ISCOLLECTABLE)
|
||||
|
||||
|
||||
/*
|
||||
** Union of all collectable objects
|
||||
@@ -55,13 +90,10 @@ typedef struct GCheader {
|
||||
/*
|
||||
** Union of all Lua values
|
||||
*/
|
||||
typedef union {
|
||||
GCObject *gc; /* collectable objects */
|
||||
void *p; /* light userdata */
|
||||
lua_Number n; /* numbers */
|
||||
int b; /* booleans */
|
||||
lua_CFunction f; /* light C functions */
|
||||
} Value;
|
||||
typedef union Value Value;
|
||||
|
||||
|
||||
#define numfield lua_Number n; /* numbers */
|
||||
|
||||
|
||||
|
||||
@@ -72,125 +104,146 @@ typedef union {
|
||||
|
||||
#define TValuefields Value value_; int tt_
|
||||
|
||||
typedef struct lua_TValue {
|
||||
TValuefields;
|
||||
} TValue;
|
||||
typedef struct lua_TValue TValue;
|
||||
|
||||
|
||||
/* macro defining a nil value */
|
||||
#define NILCONSTANT {NULL}, LUA_TNIL
|
||||
#define NILCONSTANT {NULL}, LUA_TNIL
|
||||
|
||||
|
||||
/*
|
||||
** type tag of a TValue
|
||||
*/
|
||||
#define ttype(o) ((o)->tt_)
|
||||
#define val_(o) ((o)->value_)
|
||||
#define num_(o) (val_(o).n)
|
||||
|
||||
|
||||
/*
|
||||
** type tag of a TValue with no variants
|
||||
*/
|
||||
#define ttypenv(o) (ttype(o) & 0x0F)
|
||||
/* raw type tag of a TValue */
|
||||
#define rttype(o) ((o)->tt_)
|
||||
|
||||
/* tag with no variants (bits 0-3) */
|
||||
#define novariant(x) ((x) & 0x0F)
|
||||
|
||||
/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
|
||||
#define ttype(o) (rttype(o) & 0x3F)
|
||||
|
||||
/* type tag of a TValue with no variants (bits 0-3) */
|
||||
#define ttypenv(o) (novariant(rttype(o)))
|
||||
|
||||
|
||||
/* 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) (ttypenv(o) == LUA_TFUNCTION)
|
||||
#define ttisclosure(o) (ttype(o) == LUA_TFUNCTION)
|
||||
#define ttislcf(o) (ttype(o) == LUA_TLCF)
|
||||
#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)
|
||||
#define ttisdeadkey(o) (ttype(o) == LUA_TDEADKEY)
|
||||
#define checktag(o,t) (rttype(o) == (t))
|
||||
#define checktype(o,t) (ttypenv(o) == (t))
|
||||
#define ttisnumber(o) checktag((o), LUA_TNUMBER)
|
||||
#define ttisnil(o) checktag((o), LUA_TNIL)
|
||||
#define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
|
||||
#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
|
||||
#define ttisstring(o) checktype((o), LUA_TSTRING)
|
||||
#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
|
||||
#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
|
||||
#define ttistable(o) checktag((o), ctb(LUA_TTABLE))
|
||||
#define ttisfunction(o) checktype(o, LUA_TFUNCTION)
|
||||
#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
|
||||
#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
|
||||
#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
|
||||
#define ttislcf(o) checktag((o), LUA_TLCF)
|
||||
#define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))
|
||||
#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
|
||||
#define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
|
||||
|
||||
#define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
|
||||
|
||||
/* Macros to access values */
|
||||
#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 nvalue(o) check_exp(ttisnumber(o), num_(o))
|
||||
#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
|
||||
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
|
||||
#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
|
||||
#define tsvalue(o) (&rawtsvalue(o)->tsv)
|
||||
#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value_.gc->u)
|
||||
#define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)
|
||||
#define uvalue(o) (&rawuvalue(o)->uv)
|
||||
#define clvalue(o) check_exp(ttisclosure(o), &(o)->value_.gc->cl)
|
||||
#define fvalue(o) check_exp(ttislcf(o), (o)->value_.f)
|
||||
#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 clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
|
||||
#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
|
||||
#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
|
||||
#define fvalue(o) check_exp(ttislcf(o), val_(o).f)
|
||||
#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
|
||||
#define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
|
||||
#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
|
||||
/* a dead value may get the 'gc' field, but cannot access its contents */
|
||||
#define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
|
||||
|
||||
#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
|
||||
|
||||
|
||||
#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
|
||||
#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
|
||||
|
||||
|
||||
/* Macros for internal tests */
|
||||
#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)
|
||||
|
||||
#define checkconsistency(obj) lua_assert(!iscollectable(obj) || righttt(obj))
|
||||
|
||||
#define checkliveness(g,obj) \
|
||||
lua_assert(!iscollectable(obj) || (righttt(obj) && !isdead(g,gcvalue(obj))))
|
||||
lua_longassert(!iscollectable(obj) || \
|
||||
(righttt(obj) && !isdead(g,gcvalue(obj))))
|
||||
|
||||
|
||||
/* Macros to set values */
|
||||
#define setnilvalue(obj) ((obj)->tt_=LUA_TNIL)
|
||||
#define settt_(o,t) ((o)->tt_=(t))
|
||||
|
||||
#define setnvalue(obj,x) \
|
||||
{ TValue *i_o=(obj); i_o->value_.n=(x); i_o->tt_=LUA_TNUMBER; }
|
||||
{ TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
|
||||
|
||||
#define changenvalue(o,x) check_exp(ttisnumber(o), num_(o)=(x))
|
||||
|
||||
#define setnilvalue(obj) settt_(obj, LUA_TNIL)
|
||||
|
||||
#define setfvalue(obj,x) \
|
||||
{ TValue *i_o=(obj); i_o->value_.f=(x); i_o->tt_=LUA_TLCF; }
|
||||
|
||||
#define changenvalue(o,x) check_exp((o)->tt_==LUA_TNUMBER, (o)->value_.n=(x))
|
||||
{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
|
||||
|
||||
#define setpvalue(obj,x) \
|
||||
{ TValue *i_o=(obj); i_o->value_.p=(x); i_o->tt_=LUA_TLIGHTUSERDATA; }
|
||||
{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
|
||||
|
||||
#define setbvalue(obj,x) \
|
||||
{ TValue *i_o=(obj); i_o->value_.b=(x); i_o->tt_=LUA_TBOOLEAN; }
|
||||
{ TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
|
||||
|
||||
#define setgcovalue(L,obj,x) \
|
||||
{ TValue *io=(obj); GCObject *i_g=(x); \
|
||||
val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
|
||||
|
||||
#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); }
|
||||
{ TValue *io=(obj); \
|
||||
TString *x_ = (x); \
|
||||
val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
|
||||
checkliveness(G(L),io); }
|
||||
|
||||
#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); }
|
||||
{ TValue *io=(obj); \
|
||||
val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
|
||||
checkliveness(G(L),io); }
|
||||
|
||||
#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); }
|
||||
{ TValue *io=(obj); \
|
||||
val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
|
||||
checkliveness(G(L),io); }
|
||||
|
||||
#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 setclLvalue(L,obj,x) \
|
||||
{ TValue *io=(obj); \
|
||||
val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
|
||||
checkliveness(G(L),io); }
|
||||
|
||||
#define setclCvalue(L,obj,x) \
|
||||
{ TValue *io=(obj); \
|
||||
val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
|
||||
checkliveness(G(L),io); }
|
||||
|
||||
#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); }
|
||||
{ TValue *io=(obj); \
|
||||
val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
|
||||
checkliveness(G(L),io); }
|
||||
|
||||
#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 setdeadvalue(obj) ((obj)->tt_=LUA_TDEADKEY)
|
||||
#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
|
||||
|
||||
|
||||
|
||||
#define setobj(L,obj1,obj2) \
|
||||
{ const TValue *o2=(obj2); TValue *o1=(obj1); \
|
||||
o1->value_ = o2->value_; o1->tt_=o2->tt_; \
|
||||
checkliveness(G(L),o1); }
|
||||
{ const TValue *io2=(obj2); TValue *io1=(obj1); \
|
||||
io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
|
||||
checkliveness(G(L),io1); }
|
||||
|
||||
|
||||
/*
|
||||
@@ -213,10 +266,147 @@ typedef struct lua_TValue {
|
||||
#define setsvalue2n setsvalue
|
||||
|
||||
|
||||
/* check whether a number is valid (useful only for NaN trick) */
|
||||
#define luai_checknum(L,o,c) { /* empty */ }
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** NaN Trick
|
||||
** =======================================================
|
||||
*/
|
||||
#if defined(LUA_NANTRICK)
|
||||
|
||||
/*
|
||||
** numbers are represented in the 'd_' field. All other values have the
|
||||
** value (NNMARK | tag) in 'tt__'. A number with such pattern would be
|
||||
** a "signaled NaN", which is never generated by regular operations by
|
||||
** the CPU (nor by 'strtod')
|
||||
*/
|
||||
|
||||
/* allows for external implementation for part of the trick */
|
||||
#if !defined(NNMARK) /* { */
|
||||
|
||||
|
||||
#if !defined(LUA_IEEEENDIAN)
|
||||
#error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN'
|
||||
#endif
|
||||
|
||||
|
||||
#define NNMARK 0x7FF7A500
|
||||
#define NNMASK 0x7FFFFF00
|
||||
|
||||
#undef TValuefields
|
||||
#undef NILCONSTANT
|
||||
|
||||
#if (LUA_IEEEENDIAN == 0) /* { */
|
||||
|
||||
/* little endian */
|
||||
#define TValuefields \
|
||||
union { struct { Value v__; int tt__; } i; double d__; } u
|
||||
#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
|
||||
/* field-access macros */
|
||||
#define v_(o) ((o)->u.i.v__)
|
||||
#define d_(o) ((o)->u.d__)
|
||||
#define tt_(o) ((o)->u.i.tt__)
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/* big endian */
|
||||
#define TValuefields \
|
||||
union { struct { int tt__; Value v__; } i; double d__; } u
|
||||
#define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}
|
||||
/* field-access macros */
|
||||
#define v_(o) ((o)->u.i.v__)
|
||||
#define d_(o) ((o)->u.d__)
|
||||
#define tt_(o) ((o)->u.i.tt__)
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif /* } */
|
||||
|
||||
|
||||
/* correspondence with standard representation */
|
||||
#undef val_
|
||||
#define val_(o) v_(o)
|
||||
#undef num_
|
||||
#define num_(o) d_(o)
|
||||
|
||||
|
||||
#undef numfield
|
||||
#define numfield /* no such field; numbers are the entire struct */
|
||||
|
||||
/* basic check to distinguish numbers from non-numbers */
|
||||
#undef ttisnumber
|
||||
#define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK)
|
||||
|
||||
#define tag2tt(t) (NNMARK | (t))
|
||||
|
||||
#undef rttype
|
||||
#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
|
||||
|
||||
#undef settt_
|
||||
#define settt_(o,t) (tt_(o) = tag2tt(t))
|
||||
|
||||
#undef setnvalue
|
||||
#define setnvalue(obj,x) \
|
||||
{ TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
|
||||
|
||||
#undef setobj
|
||||
#define setobj(L,obj1,obj2) \
|
||||
{ const TValue *o2_=(obj2); TValue *o1_=(obj1); \
|
||||
o1_->u = o2_->u; \
|
||||
checkliveness(G(L),o1_); }
|
||||
|
||||
|
||||
/*
|
||||
** these redefinitions are not mandatory, but these forms are more efficient
|
||||
*/
|
||||
|
||||
#undef checktag
|
||||
#undef checktype
|
||||
#define checktag(o,t) (tt_(o) == tag2tt(t))
|
||||
#define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS))
|
||||
|
||||
#undef ttisequal
|
||||
#define ttisequal(o1,o2) \
|
||||
(ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
|
||||
|
||||
|
||||
#undef luai_checknum
|
||||
#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
|
||||
|
||||
#endif
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** types and prototypes
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
union Value {
|
||||
GCObject *gc; /* collectable objects */
|
||||
void *p; /* light userdata */
|
||||
int b; /* booleans */
|
||||
lua_CFunction f; /* light C functions */
|
||||
numfield /* numbers */
|
||||
};
|
||||
|
||||
|
||||
struct lua_TValue {
|
||||
TValuefields;
|
||||
};
|
||||
|
||||
|
||||
typedef TValue *StkId; /* index to stack elements */
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Header for string value; string bytes follow the end of this structure
|
||||
*/
|
||||
@@ -224,9 +414,9 @@ typedef union TString {
|
||||
L_Umaxalign dummy; /* ensures maximum alignment for strings */
|
||||
struct {
|
||||
CommonHeader;
|
||||
lu_byte reserved;
|
||||
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
|
||||
unsigned int hash;
|
||||
size_t len;
|
||||
size_t len; /* number of characters in string */
|
||||
} tsv;
|
||||
} TString;
|
||||
|
||||
@@ -247,7 +437,7 @@ typedef union Udata {
|
||||
CommonHeader;
|
||||
struct Table *metatable;
|
||||
struct Table *env;
|
||||
size_t len;
|
||||
size_t len; /* number of bytes */
|
||||
} uv;
|
||||
} Udata;
|
||||
|
||||
@@ -282,11 +472,11 @@ typedef struct Proto {
|
||||
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 */
|
||||
LocVar *locvars; /* information about local variables */
|
||||
int *lineinfo; /* map from opcodes to source lines (debug information) */
|
||||
LocVar *locvars; /* information about local variables (debug information) */
|
||||
Upvaldesc *upvalues; /* upvalue information */
|
||||
union Closure *cache; /* last created closure with this prototype */
|
||||
TString *source;
|
||||
TString *source; /* used for debug information */
|
||||
int sizeupvalues; /* size of 'upvalues' */
|
||||
int sizek; /* size of `k' */
|
||||
int sizecode;
|
||||
@@ -324,7 +514,7 @@ typedef struct UpVal {
|
||||
*/
|
||||
|
||||
#define ClosureHeader \
|
||||
CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist
|
||||
CommonHeader; lu_byte nupvalues; GCObject *gclist
|
||||
|
||||
typedef struct CClosure {
|
||||
ClosureHeader;
|
||||
@@ -346,9 +536,9 @@ typedef union Closure {
|
||||
} Closure;
|
||||
|
||||
|
||||
#define isLfunction(o) (ttisclosure(o) && !clvalue(o)->c.isC)
|
||||
#define isLfunction(o) ttisLclosure(o)
|
||||
|
||||
#define getproto(o) (clvalue(o)->l.p)
|
||||
#define getproto(o) (clLvalue(o)->p)
|
||||
|
||||
|
||||
/*
|
||||
@@ -403,12 +593,13 @@ typedef struct Table {
|
||||
|
||||
LUAI_DDEC const TValue luaO_nilobject_;
|
||||
|
||||
|
||||
LUAI_FUNC int luaO_int2fb (unsigned int x);
|
||||
LUAI_FUNC int luaO_fb2int (int x);
|
||||
LUAI_FUNC int luaO_ceillog2 (lu_int32 x);
|
||||
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
|
||||
LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
|
||||
LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
|
||||
LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
|
||||
LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
|
||||
LUAI_FUNC int luaO_hexavalue (int c);
|
||||
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, ...);
|
||||
|
||||
11
lopcodes.c
11
lopcodes.c
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
** $Id: lopcodes.c,v 1.43 2010/03/12 19:14:06 roberto Exp roberto $
|
||||
** $Id: lopcodes.c,v 1.48 2011/04/19 16:22:13 roberto Exp roberto $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -16,6 +17,7 @@
|
||||
LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
|
||||
"MOVE",
|
||||
"LOADK",
|
||||
"LOADKX",
|
||||
"LOADBOOL",
|
||||
"LOADNIL",
|
||||
"GETUPVAL",
|
||||
@@ -50,7 +52,6 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
|
||||
"TFORCALL",
|
||||
"TFORLOOP",
|
||||
"SETLIST",
|
||||
"CLOSE",
|
||||
"CLOSURE",
|
||||
"VARARG",
|
||||
"EXTRAARG",
|
||||
@@ -64,8 +65,9 @@ LUAI_DDEF 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, OpArgN, OpArgN, iABx) /* OP_LOADKX */
|
||||
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
|
||||
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */
|
||||
,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */
|
||||
,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
|
||||
,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */
|
||||
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
|
||||
@@ -88,7 +90,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
,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, 0, OpArgN, 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 */
|
||||
@@ -98,7 +100,6 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */
|
||||
,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* 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 */
|
||||
,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */
|
||||
|
||||
13
lopcodes.h
13
lopcodes.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.136 2010/10/13 16:45:54 roberto Exp roberto $
|
||||
** $Id: lopcodes.h,v 1.141 2011/04/19 16:22:13 roberto Exp roberto $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -167,9 +167,10 @@ typedef enum {
|
||||
name args description
|
||||
------------------------------------------------------------------------*/
|
||||
OP_MOVE,/* A B R(A) := R(B) */
|
||||
OP_LOADK,/* A Bx R(A) := Kst(Bx - 1) */
|
||||
OP_LOADK,/* A Bx R(A) := Kst(Bx) */
|
||||
OP_LOADKX,/* A R(A) := Kst(extra arg) */
|
||||
OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
|
||||
OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
|
||||
OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */
|
||||
OP_GETUPVAL,/* A B R(A) := UpValue[B] */
|
||||
|
||||
OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */
|
||||
@@ -195,8 +196,7 @@ OP_LEN,/* A B R(A) := length of R(B) */
|
||||
|
||||
OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
|
||||
|
||||
OP_JMP,/* sBx pc+=sBx */
|
||||
|
||||
OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) + 1 */
|
||||
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++ */
|
||||
@@ -217,7 +217,6 @@ OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
|
||||
|
||||
OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
|
||||
|
||||
OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
|
||||
OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
|
||||
|
||||
OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
|
||||
@@ -244,7 +243,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
(*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next
|
||||
'instruction' is EXTRAARG(real C).
|
||||
|
||||
(*) In OP_LOADK, if (Bx == 0) then next 'instruction' is EXTRAARG(real Bx).
|
||||
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
|
||||
|
||||
(*) For comparisons, A specifies what condition the test should accept
|
||||
(true or false).
|
||||
|
||||
74
loslib.c
74
loslib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: loslib.c,v 1.31 2010/07/02 12:01:53 roberto Exp roberto $
|
||||
** $Id: loslib.c,v 1.38 2011/11/30 12:35:05 roberto Exp roberto $
|
||||
** Standard Operating System library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -21,16 +21,17 @@
|
||||
|
||||
|
||||
/*
|
||||
** list of valid conversion specifiers @* for the 'strftime' function
|
||||
** list of valid conversion specifiers for the 'strftime' function
|
||||
*/
|
||||
#if !defined(LUA_STRFTIMEOPTIONS)
|
||||
|
||||
#if !defined(LUA_USE_POSIX)
|
||||
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
|
||||
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
|
||||
#else
|
||||
#define LUA_STRFTIMEOPTIONS { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
|
||||
"E", "cCxXyY", \
|
||||
"O", "deHImMSuUVwWy" }
|
||||
#define LUA_STRFTIMEOPTIONS \
|
||||
{ "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \
|
||||
"", "E", "cCxXyY", \
|
||||
"O", "deHImMSuUVwWy" }
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -43,7 +44,7 @@
|
||||
*/
|
||||
#if defined(LUA_USE_MKSTEMP)
|
||||
#include <unistd.h>
|
||||
#define LUA_TMPNAMBUFSIZE 32
|
||||
#define LUA_TMPNAMBUFSIZE 32
|
||||
#define lua_tmpnam(b,e) { \
|
||||
strcpy(b, "/tmp/lua_XXXXXX"); \
|
||||
e = mkstemp(b); \
|
||||
@@ -52,44 +53,52 @@
|
||||
|
||||
#elif !defined(lua_tmpnam)
|
||||
|
||||
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
||||
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
||||
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
||||
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** By default, Lua uses gmtime/localtime, except when POSIX is available,
|
||||
** where it uses gmtime_r/localtime_r
|
||||
*/
|
||||
#if defined(LUA_USE_GMTIME_R)
|
||||
|
||||
#define l_gmtime(t,r) gmtime_r(t,r)
|
||||
#define l_localtime(t,r) localtime_r(t,r)
|
||||
|
||||
#elif !defined(l_gmtime)
|
||||
|
||||
#define l_gmtime(t,r) ((void)r, gmtime(t))
|
||||
#define l_localtime(t,r) ((void)r, localtime(t))
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
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);
|
||||
static int os_execute (lua_State *L) {
|
||||
const char *cmd = luaL_optstring(L, 1, NULL);
|
||||
int stat = system(cmd);
|
||||
if (cmd != NULL)
|
||||
return luaL_execresult(L, stat);
|
||||
else {
|
||||
lua_pushboolean(L, stat); /* true if there is a shell */
|
||||
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);
|
||||
return luaL_fileresult(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);
|
||||
return luaL_fileresult(L, rename(fromname, toname) == 0, fromname);
|
||||
}
|
||||
|
||||
|
||||
@@ -182,17 +191,17 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) {
|
||||
return conv; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
struct tm tmr, *stm;
|
||||
if (*s == '!') { /* UTC? */
|
||||
stm = gmtime(&t);
|
||||
stm = l_gmtime(&t, &tmr);
|
||||
s++; /* skip `!' */
|
||||
}
|
||||
else
|
||||
stm = localtime(&t);
|
||||
stm = l_localtime(&t, &tmr);
|
||||
if (stm == NULL) /* invalid date? */
|
||||
lua_pushnil(L);
|
||||
else if (strcmp(s, "*t") == 0) {
|
||||
@@ -283,7 +292,8 @@ static int os_exit (lua_State *L) {
|
||||
status = luaL_optint(L, 1, EXIT_SUCCESS);
|
||||
if (lua_toboolean(L, 2))
|
||||
lua_close(L);
|
||||
exit(status);
|
||||
if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
56
lparser.h
56
lparser.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.64 2010/07/02 20:42:40 roberto Exp roberto $
|
||||
** $Id: lparser.h,v 1.69 2011/07/27 18:09:01 roberto Exp roberto $
|
||||
** Lua Parser
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -53,19 +53,42 @@ typedef struct expdesc {
|
||||
} expdesc;
|
||||
|
||||
|
||||
typedef struct vardesc {
|
||||
unsigned short idx;
|
||||
} vardesc;
|
||||
/* description of active local variable */
|
||||
typedef struct Vardesc {
|
||||
short idx; /* variable index in stack */
|
||||
} Vardesc;
|
||||
|
||||
|
||||
/* list of all active local variables */
|
||||
typedef struct Varlist {
|
||||
vardesc *actvar;
|
||||
int nactvar;
|
||||
int actvarsize;
|
||||
} Varlist;
|
||||
/* description of pending goto statements and label statements */
|
||||
typedef struct Labeldesc {
|
||||
TString *name; /* label identifier */
|
||||
int pc; /* position in code */
|
||||
int line; /* line where it appeared */
|
||||
lu_byte nactvar; /* local level where it appears in current block */
|
||||
} Labeldesc;
|
||||
|
||||
|
||||
/* list of labels or gotos */
|
||||
typedef struct Labellist {
|
||||
Labeldesc *arr; /* array */
|
||||
int n; /* number of entries in use */
|
||||
int size; /* array size */
|
||||
} Labellist;
|
||||
|
||||
|
||||
/* dynamic structures used by the parser */
|
||||
typedef struct Dyndata {
|
||||
struct { /* list of active local variables */
|
||||
Vardesc *arr;
|
||||
int n;
|
||||
int size;
|
||||
} actvar;
|
||||
Labellist gt; /* list of pending gotos */
|
||||
Labellist label; /* list of active labels */
|
||||
} Dyndata;
|
||||
|
||||
|
||||
/* control of blocks */
|
||||
struct BlockCnt; /* defined in lparser.c */
|
||||
|
||||
|
||||
@@ -75,23 +98,22 @@ typedef struct FuncState {
|
||||
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 lasttarget; /* 'label' of last 'jump label' */
|
||||
int jpc; /* list of pending jumps to `pc' */
|
||||
int freereg; /* first free register */
|
||||
int nk; /* number of elements in `k' */
|
||||
int np; /* number of elements in `p' */
|
||||
int firstlocal; /* index of first local var of this function */
|
||||
short nlocvars; /* number of elements in `locvars' */
|
||||
int firstlocal; /* index of first local var (in Dyndata array) */
|
||||
short nlocvars; /* number of elements in 'f->locvars' */
|
||||
lu_byte nactvar; /* number of active local variables */
|
||||
lu_byte nups; /* number of upvalues */
|
||||
lu_byte freereg; /* first free register */
|
||||
} FuncState;
|
||||
|
||||
|
||||
LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
Varlist *varl, const char *name);
|
||||
LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
Dyndata *dyd, const char *name, int firstchar);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
69
lstate.c
69
lstate.c
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 2.85 2010/04/30 18:36:22 roberto Exp roberto $
|
||||
** $Id: lstate.c,v 2.97 2012/05/23 15:37:09 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lstate_c
|
||||
#define LUA_CORE
|
||||
@@ -38,7 +39,18 @@
|
||||
#endif
|
||||
|
||||
|
||||
#define MEMERRMSG "not enough memory"
|
||||
#define MEMERRMSG "not enough memory"
|
||||
|
||||
|
||||
/*
|
||||
** a macro to help the creation of a unique random seed when a state is
|
||||
** created; the seed is used to randomize hashes.
|
||||
*/
|
||||
#if !defined(luai_makeseed)
|
||||
#include <time.h>
|
||||
#define luai_makeseed() cast(size_t, time(NULL))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@@ -65,11 +77,36 @@ typedef struct LG {
|
||||
#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
|
||||
|
||||
|
||||
/*
|
||||
** Compute an initial seed as random as possible. In ANSI, rely on
|
||||
** Address Space Layout Randomization (if present) to increase
|
||||
** randomness..
|
||||
*/
|
||||
#define addbuff(b,p,e) \
|
||||
{ size_t t = cast(size_t, e); \
|
||||
memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
|
||||
|
||||
static unsigned int makeseed (lua_State *L) {
|
||||
char buff[4 * sizeof(size_t)];
|
||||
unsigned int h = luai_makeseed();
|
||||
int p = 0;
|
||||
addbuff(buff, p, L); /* heap variable */
|
||||
addbuff(buff, p, &h); /* local variable */
|
||||
addbuff(buff, p, luaO_nilobject); /* global variable */
|
||||
addbuff(buff, p, &lua_newstate); /* public function */
|
||||
lua_assert(p == sizeof(buff));
|
||||
return luaS_hash(buff, p, h);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** maximum number of nested calls made by error-handling function
|
||||
** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
|
||||
** invariant
|
||||
*/
|
||||
#define LUAI_EXTRACALLS 10
|
||||
void luaE_setdebt (global_State *g, l_mem debt) {
|
||||
g->totalbytes -= (debt - g->GCdebt);
|
||||
g->GCdebt = debt;
|
||||
}
|
||||
|
||||
|
||||
CallInfo *luaE_extendCI (lua_State *L) {
|
||||
@@ -133,10 +170,10 @@ static void init_registry (lua_State *L, global_State *g) {
|
||||
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
||||
/* registry[LUA_RIDX_MAINTHREAD] = L */
|
||||
setthvalue(L, &mt, L);
|
||||
setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt);
|
||||
luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
|
||||
/* registry[LUA_RIDX_GLOBALS] = table of globals */
|
||||
sethvalue(L, &mt, luaH_new(L));
|
||||
setobj2t(L, luaH_setint(L, registry, LUA_RIDX_GLOBALS), &mt);
|
||||
luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
|
||||
}
|
||||
|
||||
|
||||
@@ -154,7 +191,7 @@ static void f_luaopen (lua_State *L, void *ud) {
|
||||
/* pre-create memory-error message */
|
||||
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
|
||||
luaS_fix(g->memerrmsg); /* it should never be collected */
|
||||
g->GCdebt = 0;
|
||||
g->gcrunning = 1; /* allow gc */
|
||||
}
|
||||
|
||||
|
||||
@@ -168,6 +205,7 @@ static void preinit_state (lua_State *L, global_State *g) {
|
||||
L->ci = NULL;
|
||||
L->stacksize = 0;
|
||||
L->errorJmp = NULL;
|
||||
L->nCcalls = 0;
|
||||
L->hook = NULL;
|
||||
L->hookmask = 0;
|
||||
L->basehookcount = 0;
|
||||
@@ -187,8 +225,8 @@ static void close_state (lua_State *L) {
|
||||
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
|
||||
luaZ_freebuffer(L, &g->buff);
|
||||
freestack(L);
|
||||
lua_assert(g->totalbytes == sizeof(LG));
|
||||
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);
|
||||
lua_assert(gettotalbytes(g) == sizeof(LG));
|
||||
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
|
||||
}
|
||||
|
||||
|
||||
@@ -234,15 +272,15 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
|
||||
L->marked = luaC_white(g);
|
||||
g->gckind = KGC_NORMAL;
|
||||
g->nCcalls = 0;
|
||||
preinit_state(L, g);
|
||||
g->frealloc = f;
|
||||
g->ud = ud;
|
||||
g->mainthread = L;
|
||||
g->seed = makeseed(L);
|
||||
g->uvhead.u.l.prev = &g->uvhead;
|
||||
g->uvhead.u.l.next = &g->uvhead;
|
||||
stopgc(g); /* no GC while building state */
|
||||
g->lastmajormem = 0;
|
||||
g->gcrunning = 0; /* no GC while building state */
|
||||
g->GCestimate = 0;
|
||||
g->strt.size = 0;
|
||||
g->strt.nuse = 0;
|
||||
g->strt.hash = NULL;
|
||||
@@ -252,11 +290,13 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
g->version = lua_version(NULL);
|
||||
g->gcstate = GCSpause;
|
||||
g->allgc = NULL;
|
||||
g->udgc = NULL;
|
||||
g->finobj = NULL;
|
||||
g->tobefnz = NULL;
|
||||
g->sweepgc = g->sweepfin = NULL;
|
||||
g->gray = g->grayagain = NULL;
|
||||
g->weak = g->ephemeron = g->allweak = NULL;
|
||||
g->totalbytes = sizeof(LG);
|
||||
g->GCdebt = 0;
|
||||
g->gcpause = LUAI_GCPAUSE;
|
||||
g->gcmajorinc = LUAI_GCMAJOR;
|
||||
g->gcstepmul = LUAI_GCMUL;
|
||||
@@ -275,9 +315,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
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 all udata with GC metamethods */
|
||||
lua_assert(L->next == NULL);
|
||||
luai_userstateclose(L);
|
||||
close_state(L);
|
||||
}
|
||||
|
||||
40
lstate.h
40
lstate.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 2.67 2010/09/30 17:21:31 roberto Exp roberto $
|
||||
** $Id: lstate.h,v 2.80 2012/05/22 17:50:39 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -32,9 +32,9 @@
|
||||
** when traversing the respective threads, but the thread may already be
|
||||
** dead, while the upvalue is still accessible through closures.)
|
||||
**
|
||||
** Userdata with finalizers are kept in the list g->udgc.
|
||||
** Objects with finalizers are kept in the list g->finobj.
|
||||
**
|
||||
** The list g->tobefnz links all userdata being finalized.
|
||||
** The list g->tobefnz links all objects being finalized.
|
||||
|
||||
*/
|
||||
|
||||
@@ -47,8 +47,6 @@ struct lua_longjmp; /* defined in ldo.c */
|
||||
#define EXTRA_STACK 5
|
||||
|
||||
|
||||
#define BASIC_CI_SIZE 8
|
||||
|
||||
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
|
||||
|
||||
|
||||
@@ -74,6 +72,7 @@ typedef struct CallInfo {
|
||||
struct CallInfo *previous, *next; /* dynamic call link */
|
||||
short nresults; /* expected number of results from this function */
|
||||
lu_byte callstatus;
|
||||
ptrdiff_t extra;
|
||||
union {
|
||||
struct { /* only for Lua functions */
|
||||
StkId base; /* base for this function */
|
||||
@@ -83,7 +82,6 @@ typedef struct CallInfo {
|
||||
int ctx; /* context info. in case of yields */
|
||||
lua_CFunction k; /* continuation in case of yields */
|
||||
ptrdiff_t old_errfunc;
|
||||
ptrdiff_t extra;
|
||||
lu_byte old_allowhook;
|
||||
lu_byte status;
|
||||
} c;
|
||||
@@ -102,9 +100,9 @@ typedef struct CallInfo {
|
||||
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
|
||||
#define CIST_STAT (1<<5) /* call has an error status (pcall) */
|
||||
#define CIST_TAIL (1<<6) /* call was tail called */
|
||||
#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */
|
||||
|
||||
|
||||
#define ci_func(ci) (clvalue((ci)->func))
|
||||
#define isLua(ci) ((ci)->callstatus & CIST_LUA)
|
||||
|
||||
|
||||
@@ -114,19 +112,22 @@ typedef struct CallInfo {
|
||||
typedef struct global_State {
|
||||
lua_Alloc frealloc; /* function to reallocate memory */
|
||||
void *ud; /* auxiliary data to `frealloc' */
|
||||
lu_mem totalbytes; /* number of bytes currently allocated */
|
||||
l_mem GCdebt; /* when positive, run a GC step */
|
||||
lu_mem lastmajormem; /* memory in use after last major collection */
|
||||
lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
||||
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
||||
lu_mem GCmemtrav; /* memory traversed by the GC */
|
||||
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
|
||||
stringtable strt; /* hash table for strings */
|
||||
TValue l_registry;
|
||||
unsigned short nCcalls; /* number of nested C calls */
|
||||
unsigned int seed; /* randomized seed for hashes */
|
||||
lu_byte currentwhite;
|
||||
lu_byte gcstate; /* state of garbage collector */
|
||||
lu_byte gckind; /* kind of GC running */
|
||||
lu_byte gcrunning; /* true if GC is running */
|
||||
int sweepstrgc; /* position of sweep in `strt' */
|
||||
GCObject *allgc; /* list of all collectable objects */
|
||||
GCObject *udgc; /* list of collectable userdata with finalizers */
|
||||
GCObject **sweepgc; /* current position of sweep */
|
||||
GCObject *finobj; /* list of collectable objects with finalizers */
|
||||
GCObject **sweepgc; /* current position of sweep in list 'allgc' */
|
||||
GCObject **sweepfin; /* current position of sweep in list 'finobj' */
|
||||
GCObject *gray; /* list of gray objects */
|
||||
GCObject *grayagain; /* list of objects to be traversed atomically */
|
||||
GCObject *weak; /* list of tables with weak values */
|
||||
@@ -161,6 +162,7 @@ struct lua_State {
|
||||
StkId stack; /* stack base */
|
||||
int stacksize;
|
||||
unsigned short nny; /* number of non-yieldable calls in stack */
|
||||
unsigned short nCcalls; /* number of nested C calls */
|
||||
lu_byte hookmask;
|
||||
lu_byte allowhook;
|
||||
int basehookcount;
|
||||
@@ -195,11 +197,15 @@ union GCObject {
|
||||
#define gch(o) (&(o)->gch)
|
||||
|
||||
/* macros to convert a GCObject into a specific value */
|
||||
#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
|
||||
#define rawgco2ts(o) \
|
||||
check_exp(novariant((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 gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l))
|
||||
#define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c))
|
||||
#define gco2cl(o) \
|
||||
check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl))
|
||||
#define gco2t(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))
|
||||
@@ -209,6 +215,10 @@ union GCObject {
|
||||
#define obj2gco(v) (cast(GCObject *, (v)))
|
||||
|
||||
|
||||
/* actual number of total bytes allocated */
|
||||
#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt)
|
||||
|
||||
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
|
||||
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
|
||||
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
|
||||
LUAI_FUNC void luaE_freeCI (lua_State *L);
|
||||
|
||||
120
lstring.c
120
lstring.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 2.17 2010/04/03 20:24:18 roberto Exp roberto $
|
||||
** $Id: lstring.c,v 2.23 2012/03/28 18:28:25 roberto Exp roberto $
|
||||
** String table (keeps all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -18,7 +18,49 @@
|
||||
#include "lstring.h"
|
||||
|
||||
|
||||
/*
|
||||
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
|
||||
** compute its hash
|
||||
*/
|
||||
#if !defined(LUAI_HASHLIMIT)
|
||||
#define LUAI_HASHLIMIT 5
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** equality for long strings
|
||||
*/
|
||||
int luaS_eqlngstr (TString *a, TString *b) {
|
||||
size_t len = a->tsv.len;
|
||||
lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
|
||||
return (a == b) || /* same instance or... */
|
||||
((len == b->tsv.len) && /* equal length and ... */
|
||||
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** equality for strings
|
||||
*/
|
||||
int luaS_eqstr (TString *a, TString *b) {
|
||||
return (a->tsv.tt == b->tsv.tt) &&
|
||||
(a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b));
|
||||
}
|
||||
|
||||
|
||||
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
|
||||
unsigned int h = seed ^ l;
|
||||
size_t l1;
|
||||
size_t step = (l >> LUAI_HASHLIMIT) + 1;
|
||||
for (l1 = l; l1 >= step; l1 -= step)
|
||||
h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** resizes the string table
|
||||
*/
|
||||
void luaS_resize (lua_State *L, int newsize) {
|
||||
int i;
|
||||
stringtable *tb = &G(L)->strt;
|
||||
@@ -50,51 +92,81 @@ void luaS_resize (lua_State *L, int newsize) {
|
||||
}
|
||||
|
||||
|
||||
static TString *newlstr (lua_State *L, const char *str, size_t l,
|
||||
unsigned int h) {
|
||||
size_t totalsize; /* total size of TString object */
|
||||
GCObject **list; /* (pointer to) list where it will be inserted */
|
||||
/*
|
||||
** creates a new string object
|
||||
*/
|
||||
static TString *createstrobj (lua_State *L, const char *str, size_t l,
|
||||
int tag, unsigned int h, GCObject **list) {
|
||||
TString *ts;
|
||||
stringtable *tb = &G(L)->strt;
|
||||
if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
|
||||
luaM_toobig(L);
|
||||
if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
|
||||
luaS_resize(L, tb->size*2); /* too crowded */
|
||||
size_t totalsize; /* total size of TString object */
|
||||
totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
|
||||
list = &tb->hash[lmod(h, tb->size)];
|
||||
ts = &luaC_newobj(L, LUA_TSTRING, totalsize, list, 0)->ts;
|
||||
ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts;
|
||||
ts->tsv.len = l;
|
||||
ts->tsv.hash = h;
|
||||
ts->tsv.reserved = 0;
|
||||
ts->tsv.extra = 0;
|
||||
memcpy(ts+1, str, l*sizeof(char));
|
||||
((char *)(ts+1))[l] = '\0'; /* ending 0 */
|
||||
tb->nuse++;
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||
/*
|
||||
** creates a new short string, inserting it into string table
|
||||
*/
|
||||
static TString *newshrstr (lua_State *L, const char *str, size_t l,
|
||||
unsigned int h) {
|
||||
GCObject **list; /* (pointer to) list where it will be inserted */
|
||||
stringtable *tb = &G(L)->strt;
|
||||
TString *s;
|
||||
if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
|
||||
luaS_resize(L, tb->size*2); /* too crowded */
|
||||
list = &tb->hash[lmod(h, tb->size)];
|
||||
s = createstrobj(L, str, l, LUA_TSHRSTR, h, list);
|
||||
tb->nuse++;
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** checks whether short string exists and reuses it or creates a new one
|
||||
*/
|
||||
static TString *internshrstr (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)];
|
||||
global_State *g = G(L);
|
||||
unsigned int h = luaS_hash(str, l, g->seed);
|
||||
for (o = g->strt.hash[lmod(h, g->strt.size)];
|
||||
o != NULL;
|
||||
o = gch(o)->next) {
|
||||
TString *ts = rawgco2ts(o);
|
||||
if (h == ts->tsv.hash && ts->tsv.len == l &&
|
||||
(memcmp(str, getstr(ts), l) == 0)) {
|
||||
if (h == ts->tsv.hash &&
|
||||
ts->tsv.len == l &&
|
||||
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
|
||||
if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
|
||||
changewhite(o); /* resurrect it */
|
||||
return ts;
|
||||
}
|
||||
}
|
||||
return newlstr(L, str, l, h); /* not found; create a new string */
|
||||
return newshrstr(L, str, l, h); /* not found; create a new string */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** new string (with explicit length)
|
||||
*/
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||
if (l <= LUAI_MAXSHORTLEN) /* short string? */
|
||||
return internshrstr(L, str, l);
|
||||
else {
|
||||
if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
|
||||
luaM_toobig(L);
|
||||
return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** new zero-terminated string
|
||||
*/
|
||||
TString *luaS_new (lua_State *L, const char *str) {
|
||||
return luaS_newlstr(L, str, strlen(str));
|
||||
}
|
||||
|
||||
17
lstring.h
17
lstring.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.45 2010/04/03 20:24:18 roberto Exp roberto $
|
||||
** $Id: lstring.h,v 1.48 2012/01/25 21:05:40 roberto Exp roberto $
|
||||
** String table (keep all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -23,11 +23,20 @@
|
||||
|
||||
|
||||
/*
|
||||
** as all string are internalized, string equality becomes
|
||||
** pointer equality
|
||||
** test whether a string is a reserved word
|
||||
*/
|
||||
#define eqstr(a,b) ((a) == (b))
|
||||
#define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0)
|
||||
|
||||
|
||||
/*
|
||||
** equality for short strings, which are always internalized
|
||||
*/
|
||||
#define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b))
|
||||
|
||||
|
||||
LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
|
||||
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
|
||||
LUAI_FUNC int luaS_eqstr (TString *a, TString *b);
|
||||
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);
|
||||
|
||||
98
lstrlib.c
98
lstrlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstrlib.c,v 1.158 2010/11/16 20:39:41 roberto Exp roberto $
|
||||
** $Id: lstrlib.c,v 1.175 2012/04/20 13:16:48 roberto Exp roberto $
|
||||
** Standard library for string operations and pattern-matching
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
|
||||
/* macro to `unsign' a character */
|
||||
#define uchar(c) ((unsigned char)(c))
|
||||
#define uchar(c) ((unsigned char)(c))
|
||||
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ static int str_len (lua_State *L) {
|
||||
/* translate a relative string position: negative means back from end */
|
||||
static size_t posrelat (ptrdiff_t pos, size_t len) {
|
||||
if (pos >= 0) return (size_t)pos;
|
||||
else if (pos == -pos || (size_t)-pos > len) return 0;
|
||||
else if (0u - (size_t)pos > len) return 0;
|
||||
else return len - ((size_t)-pos) + 1;
|
||||
}
|
||||
|
||||
@@ -101,15 +101,31 @@ static int str_upper (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* reasonable limit to avoid arithmetic overflow */
|
||||
#define MAXSIZE ((~(size_t)0) >> 1)
|
||||
|
||||
static int str_rep (lua_State *L) {
|
||||
size_t l;
|
||||
luaL_Buffer b;
|
||||
size_t l, lsep;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
int n = luaL_checkint(L, 2);
|
||||
luaL_buffinit(L, &b);
|
||||
while (n-- > 0)
|
||||
luaL_addlstring(&b, s, l);
|
||||
luaL_pushresult(&b);
|
||||
const char *sep = luaL_optlstring(L, 3, "", &lsep);
|
||||
if (n <= 0) lua_pushliteral(L, "");
|
||||
else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */
|
||||
return luaL_error(L, "resulting string too large");
|
||||
else {
|
||||
size_t totallen = n * l + (n - 1) * lsep;
|
||||
luaL_Buffer b;
|
||||
char *p = luaL_buffinitsize(L, &b, totallen);
|
||||
while (n-- > 1) { /* first n-1 copies (followed by separator) */
|
||||
memcpy(p, s, l * sizeof(char)); p += l;
|
||||
if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */
|
||||
memcpy(p, sep, lsep * sizeof(char)); p += lsep;
|
||||
}
|
||||
}
|
||||
memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
|
||||
luaL_pushresultsize(&b, totallen);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -124,8 +140,8 @@ static int str_byte (lua_State *L) {
|
||||
if (pose > l) pose = l;
|
||||
if (posi > pose) return 0; /* empty interval; return no values */
|
||||
n = (int)(pose - posi + 1);
|
||||
if (posi + n <= pose) /* overflow? */
|
||||
luaL_error(L, "string slice too long");
|
||||
if (posi + n <= pose) /* (size_t -> int) overflow? */
|
||||
return luaL_error(L, "string slice too long");
|
||||
luaL_checkstack(L, n, "string slice too long");
|
||||
for (i=0; i<n; i++)
|
||||
lua_pushinteger(L, uchar(s[posi+i-1]));
|
||||
@@ -140,7 +156,7 @@ static int str_char (lua_State *L) {
|
||||
char *p = luaL_buffinitsize(L, &b, n);
|
||||
for (i=1; i<=n; i++) {
|
||||
int c = luaL_checkint(L, i);
|
||||
luaL_argcheck(L, uchar(c) == c, i, "invalid value");
|
||||
luaL_argcheck(L, uchar(c) == c, i, "value out of range");
|
||||
p[i - 1] = uchar(c);
|
||||
}
|
||||
luaL_pushresultsize(&b, n);
|
||||
@@ -161,7 +177,7 @@ static int str_dump (lua_State *L) {
|
||||
lua_settop(L, 1);
|
||||
luaL_buffinit(L,&b);
|
||||
if (lua_dump(L, writer, &b) != 0)
|
||||
luaL_error(L, "unable to dump given function");
|
||||
return luaL_error(L, "unable to dump given function");
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
@@ -510,7 +526,7 @@ static int nospecials (const char *p, size_t l) {
|
||||
size_t upto = 0;
|
||||
do {
|
||||
if (strpbrk(p + upto, SPECIALS))
|
||||
return 0; /* pattern has a special character */
|
||||
return 0; /* pattern has a special character */
|
||||
upto += strlen(p + upto) + 1; /* may have more after \0 */
|
||||
} while (upto <= l);
|
||||
return 1; /* no special chars found */
|
||||
@@ -729,15 +745,15 @@ static int str_gsub (lua_State *L) {
|
||||
** the previous length
|
||||
*/
|
||||
#if !defined(LUA_INTFRMLEN) /* { */
|
||||
#if defined(LUA_USELONGLONG)
|
||||
#if defined(LUA_USE_LONGLONG)
|
||||
|
||||
#define LUA_INTFRMLEN "ll"
|
||||
#define LUA_INTFRM_T long long
|
||||
#define LUA_INTFRMLEN "ll"
|
||||
#define LUA_INTFRM_T long long
|
||||
|
||||
#else
|
||||
|
||||
#define LUA_INTFRMLEN "l"
|
||||
#define LUA_INTFRM_T long
|
||||
#define LUA_INTFRMLEN "l"
|
||||
#define LUA_INTFRM_T long
|
||||
|
||||
#endif
|
||||
#endif /* } */
|
||||
@@ -750,8 +766,8 @@ static int str_gsub (lua_State *L) {
|
||||
*/
|
||||
#if !defined(LUA_FLTFRMLEN)
|
||||
|
||||
#define LUA_FLTFRMLEN ""
|
||||
#define LUA_FLTFRM_T double
|
||||
#define LUA_FLTFRMLEN ""
|
||||
#define LUA_FLTFRM_T double
|
||||
|
||||
#endif
|
||||
|
||||
@@ -794,7 +810,7 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
||||
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
|
||||
const char *p = strfrmt;
|
||||
while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
|
||||
if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
|
||||
if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
|
||||
luaL_error(L, "invalid format (repeated flags)");
|
||||
if (isdigit(uchar(*p))) p++; /* skip width */
|
||||
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
|
||||
@@ -806,7 +822,7 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
|
||||
if (isdigit(uchar(*p)))
|
||||
luaL_error(L, "invalid format (width or precision too long)");
|
||||
*(form++) = '%';
|
||||
memcpy(form, strfrmt, p - strfrmt + 1);
|
||||
memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
|
||||
form += p - strfrmt + 1;
|
||||
*form = '\0';
|
||||
return p;
|
||||
@@ -851,16 +867,30 @@ static int str_format (lua_State *L) {
|
||||
nb = sprintf(buff, form, luaL_checkint(L, arg));
|
||||
break;
|
||||
}
|
||||
case 'd': case 'i':
|
||||
case 'd': case 'i': {
|
||||
lua_Number n = luaL_checknumber(L, arg);
|
||||
LUA_INTFRM_T ni = (LUA_INTFRM_T)n;
|
||||
lua_Number diff = n - (lua_Number)ni;
|
||||
luaL_argcheck(L, -1 < diff && diff < 1, arg,
|
||||
"not a number in proper range");
|
||||
addlenmod(form, LUA_INTFRMLEN);
|
||||
nb = sprintf(buff, form, ni);
|
||||
break;
|
||||
}
|
||||
case 'o': case 'u': case 'x': case 'X': {
|
||||
lua_Number n = luaL_checknumber(L, arg);
|
||||
LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n :
|
||||
(LUA_INTFRM_T)(unsigned LUA_INTFRM_T)n;
|
||||
unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n;
|
||||
lua_Number diff = n - (lua_Number)ni;
|
||||
luaL_argcheck(L, -1 < diff && diff < 1, arg,
|
||||
"not a non-negative number in proper range");
|
||||
addlenmod(form, LUA_INTFRMLEN);
|
||||
nb = sprintf(buff, form, r);
|
||||
nb = sprintf(buff, form, ni);
|
||||
break;
|
||||
}
|
||||
case 'e': case 'E': case 'f':
|
||||
#if defined(LUA_USE_AFORMAT)
|
||||
case 'a': case 'A':
|
||||
#endif
|
||||
case 'g': case 'G': {
|
||||
addlenmod(form, LUA_FLTFRMLEN);
|
||||
nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg));
|
||||
@@ -872,16 +902,16 @@ static int str_format (lua_State *L) {
|
||||
}
|
||||
case 's': {
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, arg, &l);
|
||||
const char *s = luaL_tolstring(L, arg, &l);
|
||||
if (!strchr(form, '.') && l >= 100) {
|
||||
/* no precision and string is too long to be formatted;
|
||||
keep original string */
|
||||
lua_pushvalue(L, arg);
|
||||
luaL_addvalue(&b);
|
||||
break;
|
||||
}
|
||||
else {
|
||||
nb = sprintf(buff, form, s);
|
||||
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -920,13 +950,13 @@ static const luaL_Reg strlib[] = {
|
||||
|
||||
|
||||
static void createmetatable (lua_State *L) {
|
||||
lua_createtable(L, 0, 1); /* create metatable for strings */
|
||||
lua_createtable(L, 0, 1); /* table to be metatable for strings */
|
||||
lua_pushliteral(L, ""); /* dummy string */
|
||||
lua_pushvalue(L, -2);
|
||||
lua_setmetatable(L, -2); /* set string metatable */
|
||||
lua_pushvalue(L, -2); /* copy table */
|
||||
lua_setmetatable(L, -2); /* set table as metatable for strings */
|
||||
lua_pop(L, 1); /* pop dummy string */
|
||||
lua_pushvalue(L, -2); /* string library... */
|
||||
lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */
|
||||
lua_pushvalue(L, -2); /* get string library */
|
||||
lua_setfield(L, -2, "__index"); /* metatable.__index = string */
|
||||
lua_pop(L, 1); /* pop metatable */
|
||||
}
|
||||
|
||||
|
||||
110
ltable.c
110
ltable.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 2.52 2010/06/25 12:18:10 roberto Exp roberto $
|
||||
** $Id: ltable.c,v 2.70 2012/02/01 21:57:15 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
/*
|
||||
@@ -47,10 +48,10 @@
|
||||
#define MAXASIZE (1 << MAXBITS)
|
||||
|
||||
|
||||
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
|
||||
#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)
|
||||
#define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
|
||||
#define hashboolean(t,p) hashpow2(t, p)
|
||||
|
||||
|
||||
/*
|
||||
@@ -63,13 +64,6 @@
|
||||
#define hashpointer(t,p) hashmod(t, IntPoint(p))
|
||||
|
||||
|
||||
/*
|
||||
** number of ints inside a lua_Number
|
||||
*/
|
||||
#define numints cast_int(sizeof(lua_Number)/sizeof(int))
|
||||
|
||||
|
||||
|
||||
#define dummynode (&dummynode_)
|
||||
|
||||
#define isdummy(n) ((n) == dummynode)
|
||||
@@ -87,8 +81,9 @@ static Node *hashnum (const Table *t, lua_Number n) {
|
||||
int i;
|
||||
luai_hashnum(i, n);
|
||||
if (i < 0) {
|
||||
i = -i; /* must be a positive value */
|
||||
if (i < 0) i = 0; /* handle INT_MIN */
|
||||
if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */
|
||||
i = 0; /* handle INT_MIN */
|
||||
i = -i; /* must be a positive value */
|
||||
}
|
||||
return hashmod(t, i);
|
||||
}
|
||||
@@ -103,7 +98,15 @@ static Node *mainposition (const Table *t, const TValue *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TNUMBER:
|
||||
return hashnum(t, nvalue(key));
|
||||
case LUA_TSTRING:
|
||||
case LUA_TLNGSTR: {
|
||||
TString *s = rawtsvalue(key);
|
||||
if (s->tsv.extra == 0) { /* no hash? */
|
||||
s->tsv.hash = luaS_hash(getstr(s), s->tsv.len, s->tsv.hash);
|
||||
s->tsv.extra = 1; /* now it has its hash */
|
||||
}
|
||||
return hashstr(t, rawtsvalue(key));
|
||||
}
|
||||
case LUA_TSHRSTR:
|
||||
return hashstr(t, rawtsvalue(key));
|
||||
case LUA_TBOOLEAN:
|
||||
return hashboolean(t, bvalue(key));
|
||||
@@ -146,19 +149,19 @@ static int findindex (lua_State *L, Table *t, StkId key) {
|
||||
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 */
|
||||
for (;;) { /* 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(gkey(n), key) ||
|
||||
(ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
|
||||
gcvalue(gkey(n)) == gcvalue(key))) {
|
||||
if (luaV_rawequalobj(gkey(n), key) ||
|
||||
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
|
||||
deadvalue(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 */
|
||||
if (n == NULL)
|
||||
luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,7 +315,7 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
||||
/* re-insert elements from vanishing slice */
|
||||
for (i=nasize; i<oldasize; i++) {
|
||||
if (!ttisnil(&t->array[i]))
|
||||
setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]);
|
||||
luaH_setint(L, t, i + 1, &t->array[i]);
|
||||
}
|
||||
/* shrink array */
|
||||
luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
|
||||
@@ -320,11 +323,14 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
||||
/* re-insert elements from hash part */
|
||||
for (i = twoto(oldhsize) - 1; i >= 0; i--) {
|
||||
Node *old = nold+i;
|
||||
if (!ttisnil(gval(old)))
|
||||
if (!ttisnil(gval(old))) {
|
||||
/* doesn't need barrier/invalidate cache, as entry was
|
||||
already present in the table */
|
||||
setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
|
||||
}
|
||||
}
|
||||
if (!isdummy(nold))
|
||||
luaM_freearray(L, nold, twoto(oldhsize)); /* free old array */
|
||||
luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */
|
||||
}
|
||||
|
||||
|
||||
@@ -336,7 +342,7 @@ void luaH_resizearray (lua_State *L, Table *t, int nasize) {
|
||||
|
||||
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 nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */
|
||||
int i;
|
||||
int totaluse;
|
||||
for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
|
||||
@@ -372,7 +378,7 @@ Table *luaH_new (lua_State *L) {
|
||||
|
||||
void luaH_free (lua_State *L, Table *t) {
|
||||
if (!isdummy(t->node))
|
||||
luaM_freearray(L, t->node, sizenode(t));
|
||||
luaM_freearray(L, t->node, cast(size_t, sizenode(t)));
|
||||
luaM_freearray(L, t->array, t->sizearray);
|
||||
luaM_free(L, t);
|
||||
}
|
||||
@@ -396,14 +402,19 @@ static Node *getfreepos (Table *t) {
|
||||
** 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);
|
||||
TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
||||
Node *mp;
|
||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||
else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
|
||||
luaG_runerror(L, "table index is NaN");
|
||||
mp = mainposition(t, key);
|
||||
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
|
||||
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 */
|
||||
/* whatever called 'newkey' take care of TM cache and GC barrier */
|
||||
return luaH_set(L, t, key); /* insert key into grown table */
|
||||
}
|
||||
lua_assert(!isdummy(n));
|
||||
othern = mainposition(t, gkey(mp));
|
||||
@@ -450,12 +461,13 @@ const TValue *luaH_getint (Table *t, int key) {
|
||||
|
||||
|
||||
/*
|
||||
** search function for strings
|
||||
** search function for short strings
|
||||
*/
|
||||
const TValue *luaH_getstr (Table *t, TString *key) {
|
||||
Node *n = hashstr(t, key);
|
||||
lua_assert(key->tsv.tt == LUA_TSHRSTR);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttisstring(gkey(n)) && eqstr(rawtsvalue(gkey(n)), key))
|
||||
if (ttisshrstring(gkey(n)) && eqshrstr(rawtsvalue(gkey(n)), key))
|
||||
return gval(n); /* that's it */
|
||||
else n = gnext(n);
|
||||
} while (n);
|
||||
@@ -469,7 +481,7 @@ const TValue *luaH_getstr (Table *t, TString *key) {
|
||||
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_TSHRSTR: return luaH_getstr(t, rawtsvalue(key));
|
||||
case LUA_TNUMBER: {
|
||||
int k;
|
||||
lua_Number n = nvalue(key);
|
||||
@@ -481,7 +493,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
|
||||
default: {
|
||||
Node *n = mainposition(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_rawequalObj(gkey(n), key))
|
||||
if (luaV_rawequalobj(gkey(n), key))
|
||||
return gval(n); /* that's it */
|
||||
else n = gnext(n);
|
||||
} while (n);
|
||||
@@ -491,41 +503,29 @@ const TValue *luaH_get (Table *t, const TValue *key) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** beware: when using this function you probably need to check a GC
|
||||
** barrier and invalidate the TM cache.
|
||||
*/
|
||||
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(L, nvalue(key)))
|
||||
luaG_runerror(L, "table index is NaN");
|
||||
return newkey(L, t, key);
|
||||
}
|
||||
else return luaH_newkey(L, t, key);
|
||||
}
|
||||
|
||||
|
||||
TValue *luaH_setint (lua_State *L, Table *t, int key) {
|
||||
void luaH_setint (lua_State *L, Table *t, int key, TValue *value) {
|
||||
const TValue *p = luaH_getint(t, key);
|
||||
TValue *cell;
|
||||
if (p != luaO_nilobject)
|
||||
return cast(TValue *, p);
|
||||
cell = 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);
|
||||
cell = luaH_newkey(L, t, &k);
|
||||
}
|
||||
setobj2t(L, cell, value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
8
ltable.h
8
ltable.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 2.13 2009/11/06 17:07:48 roberto Exp roberto $
|
||||
** $Id: ltable.h,v 2.15 2011/08/09 20:58:29 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -15,12 +15,14 @@
|
||||
#define gval(n) (&(n)->i_val)
|
||||
#define gnext(n) ((n)->i_key.nk.next)
|
||||
|
||||
#define invalidateTMcache(t) ((t)->flags = 0)
|
||||
|
||||
|
||||
LUAI_FUNC const TValue *luaH_getint (Table *t, int key);
|
||||
LUAI_FUNC TValue *luaH_setint (lua_State *L, Table *t, int key);
|
||||
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value);
|
||||
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_newkey (lua_State *L, 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);
|
||||
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize);
|
||||
|
||||
39
ltablib.c
39
ltablib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltablib.c,v 1.57 2010/10/25 19:01:37 roberto Exp roberto $
|
||||
** $Id: ltablib.c,v 1.62 2011/09/30 12:45:45 roberto Exp roberto $
|
||||
** Library for Table Manipulation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -17,12 +17,7 @@
|
||||
|
||||
|
||||
#define aux_getn(L,n) \
|
||||
(luaL_checktype(L, n, LUA_TTABLE), (int)lua_rawlen(L, n))
|
||||
|
||||
|
||||
static int deprecatedfunc (lua_State *L) {
|
||||
return luaL_error(L, "deprecated function");
|
||||
}
|
||||
(luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
|
||||
|
||||
|
||||
#if defined(LUA_COMPAT_MAXN)
|
||||
@@ -40,8 +35,6 @@ static int maxn (lua_State *L) {
|
||||
lua_pushnumber(L, max);
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
#define maxn deprecatedfunc
|
||||
#endif
|
||||
|
||||
|
||||
@@ -93,7 +86,7 @@ static void addfield (lua_State *L, luaL_Buffer *b, int i) {
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_error(L, "invalid value (%s) at index %d in table for "
|
||||
LUA_QL("concat"), luaL_typename(L, -1), i);
|
||||
luaL_addvalue(b);
|
||||
luaL_addvalue(b);
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +97,7 @@ static int tconcat (lua_State *L) {
|
||||
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, (int)lua_rawlen(L, 1));
|
||||
last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1));
|
||||
luaL_buffinit(L, &b);
|
||||
for (; i < last; i++) {
|
||||
addfield(L, &b, i);
|
||||
@@ -124,18 +117,19 @@ static int tconcat (lua_State *L) {
|
||||
*/
|
||||
|
||||
static int pack (lua_State *L) {
|
||||
int top = lua_gettop(L);
|
||||
lua_createtable(L, top, 1); /* create result table */
|
||||
lua_pushinteger(L, top); /* number of elements */
|
||||
int n = lua_gettop(L); /* number of elements to pack */
|
||||
lua_createtable(L, n, 1); /* create result table */
|
||||
lua_pushinteger(L, n);
|
||||
lua_setfield(L, -2, "n"); /* t.n = number of elements */
|
||||
if (top > 0) { /* at least one element? */
|
||||
if (n > 0) { /* at least one element? */
|
||||
int i;
|
||||
lua_pushvalue(L, 1);
|
||||
lua_rawseti(L, -2, 1); /* insert first element */
|
||||
lua_replace(L, 1); /* move table into its position (index 1) */
|
||||
for (; top >= 2; top--) /* assign other elements */
|
||||
lua_rawseti(L, 1, top);
|
||||
lua_replace(L, 1); /* move table into index 1 */
|
||||
for (i = n; i >= 2; i--) /* assign other elements */
|
||||
lua_rawseti(L, 1, i);
|
||||
}
|
||||
return 1;
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
|
||||
@@ -143,7 +137,7 @@ static int 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, (int)lua_rawlen(L, 1));
|
||||
e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
|
||||
if (i > e) return 0; /* empty range */
|
||||
n = e - i + 1; /* number of elements */
|
||||
if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
|
||||
@@ -265,10 +259,9 @@ static int sort (lua_State *L) {
|
||||
|
||||
static const luaL_Reg tab_funcs[] = {
|
||||
{"concat", tconcat},
|
||||
{"foreach", deprecatedfunc},
|
||||
{"foreachi", deprecatedfunc},
|
||||
{"getn", deprecatedfunc},
|
||||
#if defined(LUA_COMPAT_MAXN)
|
||||
{"maxn", maxn},
|
||||
#endif
|
||||
{"insert", tinsert},
|
||||
{"pack", pack},
|
||||
{"unpack", unpack},
|
||||
|
||||
129
ltests.c
129
ltests.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 2.112 2010/07/28 15:51:59 roberto Exp roberto $
|
||||
** $Id: ltests.c,v 2.129 2012/05/31 20:25:42 roberto Exp $
|
||||
** Internal Module for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -83,7 +83,7 @@ typedef union Header {
|
||||
} Header;
|
||||
|
||||
|
||||
#ifndef EXTERNMEMCHECK
|
||||
#if !defined(EXTERNMEMCHECK)
|
||||
|
||||
/* full memory check */
|
||||
#define MARKSIZE 16 /* size of marks after each block */
|
||||
@@ -189,11 +189,16 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
|
||||
|
||||
|
||||
static void printobj (global_State *g, GCObject *o) {
|
||||
int i = 0;
|
||||
int i = 1;
|
||||
GCObject *p;
|
||||
for (p = g->allgc; p != o && p != NULL; p = gch(p)->next) i++;
|
||||
if (p == NULL) i = -1;
|
||||
printf("%d:%s(%p)-%c(%02X)", i, ttypename(gch(o)->tt), (void *)o,
|
||||
if (p == NULL) {
|
||||
i = 1;
|
||||
for (p = g->finobj; p != o && p != NULL; p = gch(p)->next) i++;
|
||||
if (p == NULL) i = 0; /* zero means 'not found' */
|
||||
else i = -i; /* negative means 'found in findobj list */
|
||||
}
|
||||
printf("||%d:%s(%p)-%c(%02X)||", i, ttypename(gch(o)->tt), (void *)o,
|
||||
isdead(g,o)?'d':isblack(o)?'b':iswhite(o)?'w':'g', gch(o)->marked);
|
||||
}
|
||||
|
||||
@@ -215,7 +220,8 @@ static int testobjref (global_State *g, GCObject *f, GCObject *t) {
|
||||
|
||||
static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
|
||||
if (iscollectable(t)) {
|
||||
lua_assert(righttt(t) && testobjref(g, f, gcvalue(t)));
|
||||
lua_assert(righttt(t));
|
||||
lua_assert(testobjref(g, f, gcvalue(t)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,22 +272,22 @@ static void checkproto (global_State *g, Proto *f) {
|
||||
|
||||
|
||||
|
||||
static void checkclosure (global_State *g, Closure *cl) {
|
||||
static void checkCclosure (global_State *g, CClosure *cl) {
|
||||
GCObject *clgc = obj2gco(cl);
|
||||
if (cl->c.isC) {
|
||||
int i;
|
||||
for (i=0; i<cl->c.nupvalues; i++)
|
||||
checkvalref(g, clgc, &cl->c.upvalue[i]);
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
|
||||
checkobjref(g, clgc, cl->l.p);
|
||||
for (i=0; i<cl->l.nupvalues; i++) {
|
||||
if (cl->l.upvals[i]) {
|
||||
lua_assert(cl->l.upvals[i]->tt == LUA_TUPVAL);
|
||||
checkobjref(g, clgc, cl->l.upvals[i]);
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < cl->nupvalues; i++)
|
||||
checkvalref(g, clgc, &cl->upvalue[i]);
|
||||
}
|
||||
|
||||
|
||||
static void checkLclosure (global_State *g, LClosure *cl) {
|
||||
GCObject *clgc = obj2gco(cl);
|
||||
int i;
|
||||
if (cl->p) checkobjref(g, clgc, cl->p);
|
||||
for (i=0; i<cl->nupvalues; i++) {
|
||||
if (cl->upvals[i]) {
|
||||
lua_assert(cl->upvals[i]->tt == LUA_TUPVAL);
|
||||
checkobjref(g, clgc, cl->upvals[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -290,7 +296,7 @@ static void checkclosure (global_State *g, Closure *cl) {
|
||||
static int lua_checkpc (pCallInfo ci) {
|
||||
if (!isLua(ci)) return 1;
|
||||
else {
|
||||
Proto *p = ci_func(ci)->l.p;
|
||||
Proto *p = ci_func(ci)->p;
|
||||
return p->code <= ci->u.l.savedpc &&
|
||||
ci->u.l.savedpc <= p->code + p->sizecode;
|
||||
}
|
||||
@@ -319,9 +325,9 @@ static void checkstack (global_State *g, lua_State *L1) {
|
||||
}
|
||||
|
||||
|
||||
static void checkobject (global_State *g, GCObject *o) {
|
||||
static void checkobject (global_State *g, GCObject *o, int maybedead) {
|
||||
if (isdead(g, o))
|
||||
lua_assert(issweepphase(g));
|
||||
lua_assert(maybedead);
|
||||
else {
|
||||
if (g->gcstate == GCSpause && !isgenerational(g))
|
||||
lua_assert(iswhite(o));
|
||||
@@ -346,15 +352,20 @@ static void checkobject (global_State *g, GCObject *o) {
|
||||
checkstack(g, gco2th(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TFUNCTION: {
|
||||
checkclosure(g, gco2cl(o));
|
||||
case LUA_TLCL: {
|
||||
checkLclosure(g, gco2lcl(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TCCL: {
|
||||
checkCclosure(g, gco2ccl(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
checkproto(g, gco2p(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TSTRING: break;
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_TLNGSTR: break;
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
@@ -367,10 +378,11 @@ static void checkgraylist (GCObject *l) {
|
||||
while (l) {
|
||||
lua_assert(isgray(l));
|
||||
lua_assert(!testbit(l->gch.marked, TESTGRAYBIT));
|
||||
l->gch.marked = l_setbit(l->gch.marked, TESTGRAYBIT);
|
||||
l_setbit(l->gch.marked, TESTGRAYBIT);
|
||||
switch (gch(l)->tt) {
|
||||
case LUA_TTABLE: l = gco2t(l)->gclist; break;
|
||||
case LUA_TFUNCTION: l = gco2cl(l)->c.gclist; break;
|
||||
case LUA_TLCL: l = gco2lcl(l)->gclist; break;
|
||||
case LUA_TCCL: l = gco2ccl(l)->gclist; break;
|
||||
case LUA_TTHREAD: l = gco2th(l)->gclist; break;
|
||||
case LUA_TPROTO: l = gco2p(l)->gclist; break;
|
||||
default: lua_assert(0); /* other objects cannot be gray */
|
||||
@@ -404,7 +416,7 @@ static void checkold (global_State *g, GCObject *o) {
|
||||
else lua_assert(!isold); /* non-old object cannot be after an old one */
|
||||
if (isgray(o)) {
|
||||
lua_assert(!keepinvariant(g) || testbit(o->gch.marked, TESTGRAYBIT));
|
||||
o->gch.marked = resetbit(o->gch.marked, TESTGRAYBIT);
|
||||
resetbit(o->gch.marked, TESTGRAYBIT);
|
||||
}
|
||||
lua_assert(!testbit(o->gch.marked, TESTGRAYBIT));
|
||||
}
|
||||
@@ -415,34 +427,40 @@ int lua_checkmemory (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
GCObject *o;
|
||||
UpVal *uv;
|
||||
int maybedead;
|
||||
if (keepinvariant(g)) {
|
||||
lua_assert(!iswhite(obj2gco(g->mainthread)));
|
||||
lua_assert(!iswhite(gcvalue(&g->l_registry)));
|
||||
}
|
||||
lua_assert(!isdead(g, gcvalue(&g->l_registry)));
|
||||
checkstack(g, g->mainthread);
|
||||
g->mainthread->marked = resetbit(g->mainthread->marked, TESTGRAYBIT);
|
||||
resetbit(g->mainthread->marked, TESTGRAYBIT);
|
||||
/* check 'allgc' list */
|
||||
markgrays(g);
|
||||
checkold(g, g->allgc);
|
||||
lua_assert(g->sweepgc == NULL || issweepphase(g));
|
||||
maybedead = 0;
|
||||
for (o = g->allgc; o != NULL; o = gch(o)->next) {
|
||||
checkobject(g, o);
|
||||
if (g->sweepgc && o == *g->sweepgc)
|
||||
maybedead = 1; /* part of the list not yet sweeped */
|
||||
checkobject(g, o, maybedead);
|
||||
lua_assert(!testbit(o->gch.marked, SEPARATED));
|
||||
}
|
||||
/* check 'udgc' list */
|
||||
checkold(g, g->udgc);
|
||||
for (o = g->udgc; o != NULL; o = gch(o)->next) {
|
||||
lua_assert(gch(o)->tt == LUA_TUSERDATA &&
|
||||
!isdead(g, o) &&
|
||||
testbit(o->gch.marked, SEPARATED));
|
||||
checkobject(g, o);
|
||||
/* check 'finobj' list */
|
||||
checkold(g, g->finobj);
|
||||
for (o = g->finobj; o != NULL; o = gch(o)->next) {
|
||||
lua_assert(testbit(o->gch.marked, SEPARATED));
|
||||
lua_assert(gch(o)->tt == LUA_TUSERDATA ||
|
||||
gch(o)->tt == LUA_TTABLE);
|
||||
checkobject(g, o, 0);
|
||||
}
|
||||
/* check 'tobefnz' list */
|
||||
checkold(g, g->tobefnz);
|
||||
for (o = g->tobefnz; o != NULL; o = gch(o)->next) {
|
||||
lua_assert(gch(o)->tt == LUA_TUSERDATA);
|
||||
lua_assert(isblack(o));
|
||||
lua_assert(testbit(o->gch.marked, SEPARATED));
|
||||
lua_assert(!iswhite(o));
|
||||
lua_assert(!isdead(g, o) && testbit(o->gch.marked, SEPARATED));
|
||||
lua_assert(gch(o)->tt == LUA_TUSERDATA ||
|
||||
gch(o)->tt == LUA_TTABLE);
|
||||
}
|
||||
/* check 'uvhead' list */
|
||||
for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
|
||||
@@ -502,6 +520,12 @@ void luaI_printcode (Proto *pt, int size) {
|
||||
}
|
||||
printf("-------\n");
|
||||
}
|
||||
|
||||
|
||||
void luaI_printinst (Proto *pt, int pc) {
|
||||
char buff[100];
|
||||
printf("%s\n", buildop(pt, pc, buff));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -593,7 +617,10 @@ static int mem_query (lua_State *L) {
|
||||
|
||||
|
||||
static int settrick (lua_State *L) {
|
||||
l_Trick = gcvalue(obj_at(L, 1));
|
||||
if (ttisnil(obj_at(L, 1)))
|
||||
l_Trick = NULL;
|
||||
else
|
||||
l_Trick = gcvalue(obj_at(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -775,7 +802,7 @@ static int newuserdata (lua_State *L) {
|
||||
|
||||
|
||||
static int pushuserdata (lua_State *L) {
|
||||
lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
|
||||
lua_pushlightuserdata(L, cast(void *, luaL_checkinteger(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -844,6 +871,7 @@ static int loadlib (lua_State *L) {
|
||||
{"coroutine", luaopen_coroutine},
|
||||
{"debug", luaopen_debug},
|
||||
{"io", luaopen_io},
|
||||
{"os", luaopen_os},
|
||||
{"math", luaopen_math},
|
||||
{"string", luaopen_string},
|
||||
{"table", luaopen_table},
|
||||
@@ -852,7 +880,7 @@ static int loadlib (lua_State *L) {
|
||||
lua_State *L1 = getstate(L);
|
||||
int i;
|
||||
luaL_requiref(L1, "package", luaopen_package, 1);
|
||||
luaL_findtable(L1, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
luaL_getsubtable(L1, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
for (i = 0; libs[i].name; i++) {
|
||||
lua_pushcfunction(L1, libs[i].func);
|
||||
lua_setfield(L1, -2, libs[i].name);
|
||||
@@ -936,9 +964,9 @@ static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
|
||||
sig = -1;
|
||||
(*pc)++;
|
||||
}
|
||||
if (!lisdigit(cast(unsigned char, **pc)))
|
||||
if (!lisdigit(cast_uchar(**pc)))
|
||||
luaL_error(L, "number expected (%s)", *pc);
|
||||
while (lisdigit(cast(unsigned char, **pc))) res = res*10 + (*(*pc)++) - '0';
|
||||
while (lisdigit(cast_uchar(**pc))) res = res*10 + (*(*pc)++) - '0';
|
||||
return sig*res;
|
||||
}
|
||||
|
||||
@@ -1208,7 +1236,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
}
|
||||
else if EQ("resume") {
|
||||
int i = getindex;
|
||||
status = lua_resume(lua_tothread(L1, i), getnum);
|
||||
status = lua_resume(lua_tothread(L1, i), L, getnum);
|
||||
}
|
||||
else if EQ("pushstatus") {
|
||||
pushcode(L1, status);
|
||||
@@ -1252,7 +1280,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
lua_pushinteger(L1, i);
|
||||
}
|
||||
else if EQ("checkstack") {
|
||||
luaL_checkstack(L1, getnum, NULL);
|
||||
int sz = getnum;
|
||||
luaL_checkstack(L1, sz, getstring);
|
||||
}
|
||||
else if EQ("newmetatable") {
|
||||
lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
|
||||
@@ -1395,7 +1424,7 @@ static int coresume (lua_State *L) {
|
||||
int status;
|
||||
lua_State *co = lua_tothread(L, 1);
|
||||
luaL_argcheck(L, co, 1, "coroutine expected");
|
||||
status = lua_resume(co, 0);
|
||||
status = lua_resume(co, L, 0);
|
||||
if (status != LUA_OK && status != LUA_YIELD) {
|
||||
lua_pushboolean(L, 0);
|
||||
lua_insert(L, -2);
|
||||
|
||||
8
ltm.c
8
ltm.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltm.c,v 2.11 2010/01/13 16:18:25 roberto Exp roberto $
|
||||
** $Id: ltm.c,v 2.13 2011/02/28 17:32:10 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -21,11 +21,11 @@
|
||||
|
||||
static const char udatatypename[] = "userdata";
|
||||
|
||||
LUAI_DDEF const char *const luaT_typenames_[] = {
|
||||
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
|
||||
"no value",
|
||||
"nil", "boolean", udatatypename, "number",
|
||||
"string", "table", "function", udatatypename, "thread",
|
||||
"proto", "upval"
|
||||
"proto", "upval" /* these last two cases are used for tests only */
|
||||
};
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
||||
|
||||
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
||||
Table *mt;
|
||||
switch (ttype(o)) {
|
||||
switch (ttypenv(o)) {
|
||||
case LUA_TTABLE:
|
||||
mt = hvalue(o)->metatable;
|
||||
break;
|
||||
|
||||
4
ltm.h
4
ltm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltm.h,v 2.9 2010/01/13 16:18:25 roberto Exp roberto $
|
||||
** $Id: ltm.h,v 2.10 2010/04/13 20:48:12 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -46,7 +46,7 @@ typedef enum {
|
||||
#define ttypename(x) luaT_typenames_[(x) + 1]
|
||||
#define objtypename(x) ttypename(ttypenv(x))
|
||||
|
||||
LUAI_DDEC const char *const luaT_typenames_[];
|
||||
LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS];
|
||||
|
||||
|
||||
LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
|
||||
|
||||
90
lua.c
90
lua.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.193 2010/10/18 16:06:33 roberto Exp roberto $
|
||||
** $Id: lua.c,v 1.204 2012/04/20 17:05:17 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -45,13 +45,13 @@
|
||||
*/
|
||||
#if defined(LUA_USE_ISATTY)
|
||||
#include <unistd.h>
|
||||
#define lua_stdin_is_tty() isatty(0)
|
||||
#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))
|
||||
#define lua_stdin_is_tty() _isatty(_fileno(stdin))
|
||||
#else
|
||||
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
||||
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
|
||||
@@ -66,19 +66,19 @@
|
||||
#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_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
|
||||
#define lua_saveline(L,idx) \
|
||||
if (lua_rawlen(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))
|
||||
#define lua_freeline(L,b) ((void)L, free(b))
|
||||
|
||||
#elif !defined(lua_readline)
|
||||
|
||||
#define lua_readline(L,b,p) \
|
||||
#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; }
|
||||
#define lua_saveline(L,idx) { (void)L; (void)idx; }
|
||||
#define lua_freeline(L,b) { (void)L; (void)b; }
|
||||
|
||||
#endif
|
||||
|
||||
@@ -106,13 +106,11 @@ static void laction (int i) {
|
||||
|
||||
|
||||
static void print_usage (const char *badoption) {
|
||||
if (badoption[1] == 'e' || badoption[1] == 'l') {
|
||||
luai_writestringerror("%s: ", progname);
|
||||
luai_writestringerror("%s: ", progname);
|
||||
if (badoption[1] == 'e' || badoption[1] == 'l')
|
||||
luai_writestringerror("'%s' needs argument\n", badoption);
|
||||
} else {
|
||||
luai_writestringerror("%s: ", progname);
|
||||
else
|
||||
luai_writestringerror("unrecognized option '%s'\n", badoption);
|
||||
}
|
||||
luai_writestringerror(
|
||||
"usage: %s [options] [script [args]]\n"
|
||||
"Available options are:\n"
|
||||
@@ -120,6 +118,7 @@ static void print_usage (const char *badoption) {
|
||||
" -i enter interactive mode after executing " LUA_QL("script") "\n"
|
||||
" -l name require library " LUA_QL("name") "\n"
|
||||
" -v show version information\n"
|
||||
" -E ignore environment variables\n"
|
||||
" -- stop handling options\n"
|
||||
" - stop handling options and execute stdin\n"
|
||||
,
|
||||
@@ -185,7 +184,8 @@ static int docall (lua_State *L, int narg, int nres) {
|
||||
|
||||
|
||||
static void print_version (void) {
|
||||
printf("%s\n", LUA_COPYRIGHT);
|
||||
luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
|
||||
luai_writeline();
|
||||
}
|
||||
|
||||
|
||||
@@ -223,16 +223,11 @@ static int dostring (lua_State *L, const char *s, const char *name) {
|
||||
|
||||
static int dolibrary (lua_State *L, const char *name) {
|
||||
int status;
|
||||
lua_pushglobaltable(L);
|
||||
lua_getfield(L, -1, "require");
|
||||
lua_getglobal(L, "require");
|
||||
lua_pushstring(L, name);
|
||||
status = docall(L, 1, 1);
|
||||
if (status == LUA_OK) {
|
||||
lua_setfield(L, -2, name); /* global[name] = require return */
|
||||
lua_pop(L, 1); /* remove global table */
|
||||
}
|
||||
else
|
||||
lua_remove(L, -2); /* remove global table (below error msg.) */
|
||||
status = docall(L, 1, 1); /* call 'require(name)' */
|
||||
if (status == LUA_OK)
|
||||
lua_setglobal(L, name); /* global[name] = require return */
|
||||
return report(L, status);
|
||||
}
|
||||
|
||||
@@ -247,14 +242,14 @@ static const char *get_prompt (lua_State *L, int firstline) {
|
||||
}
|
||||
|
||||
/* mark in error messages for incomplete statements */
|
||||
#define mark "<eof>"
|
||||
#define marklen (sizeof(mark) - 1)
|
||||
#define EOFMARK "<eof>"
|
||||
#define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
|
||||
|
||||
static int incomplete (lua_State *L, int status) {
|
||||
if (status == LUA_ERRSYNTAX) {
|
||||
size_t lmsg;
|
||||
const char *msg = lua_tolstring(L, -1, &lmsg);
|
||||
if (lmsg >= marklen && strcmp(msg + lmsg - marklen, mark) == 0) {
|
||||
if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
|
||||
lua_pop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
@@ -322,7 +317,7 @@ static void dotty (lua_State *L) {
|
||||
}
|
||||
}
|
||||
lua_settop(L, 0); /* clear stack */
|
||||
luai_writestring("\n", 1);
|
||||
luai_writeline();
|
||||
progname = oldprogname;
|
||||
}
|
||||
|
||||
@@ -349,7 +344,16 @@ static int handle_script (lua_State *L, char **argv, int n) {
|
||||
#define noextrachars(x) {if ((x)[2] != '\0') return -1;}
|
||||
|
||||
|
||||
static int collectargs (char **argv, int *pi, int *pv, int *pe) {
|
||||
/* indices of various argument indicators in array args */
|
||||
#define has_i 0 /* -i */
|
||||
#define has_v 1 /* -v */
|
||||
#define has_e 2 /* -e */
|
||||
#define has_E 3 /* -E */
|
||||
|
||||
#define num_has 4 /* number of 'has_*' */
|
||||
|
||||
|
||||
static int collectargs (char **argv, int *args) {
|
||||
int i;
|
||||
for (i = 1; argv[i] != NULL; i++) {
|
||||
if (argv[i][0] != '-') /* not an option? */
|
||||
@@ -360,15 +364,18 @@ static int collectargs (char **argv, int *pi, int *pv, int *pe) {
|
||||
return (argv[i+1] != NULL ? i+1 : 0);
|
||||
case '\0':
|
||||
return i;
|
||||
case 'E':
|
||||
args[has_E] = 1;
|
||||
break;
|
||||
case 'i':
|
||||
noextrachars(argv[i]);
|
||||
*pi = 1; /* go through */
|
||||
args[has_i] = 1; /* go through */
|
||||
case 'v':
|
||||
noextrachars(argv[i]);
|
||||
*pv = 1;
|
||||
args[has_v] = 1;
|
||||
break;
|
||||
case 'e':
|
||||
*pe = 1; /* go through */
|
||||
args[has_e] = 1; /* go through */
|
||||
case 'l': /* both options need an argument */
|
||||
if (argv[i][2] == '\0') { /* no concatenated argument? */
|
||||
i++; /* try next 'argv' */
|
||||
@@ -431,28 +438,33 @@ static int pmain (lua_State *L) {
|
||||
int argc = (int)lua_tointeger(L, 1);
|
||||
char **argv = (char **)lua_touserdata(L, 2);
|
||||
int script;
|
||||
int has_i = 0, has_v = 0, has_e = 0;
|
||||
int args[num_has];
|
||||
args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0;
|
||||
if (argv[0] && argv[0][0]) progname = argv[0];
|
||||
script = collectargs(argv, &has_i, &has_v, &has_e);
|
||||
script = collectargs(argv, args);
|
||||
if (script < 0) { /* invalid arg? */
|
||||
print_usage(argv[-script]);
|
||||
return 0;
|
||||
}
|
||||
if (has_v) print_version();
|
||||
if (args[has_v]) print_version();
|
||||
if (args[has_E]) { /* option '-E'? */
|
||||
lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
|
||||
}
|
||||
/* open standard libraries */
|
||||
luaL_checkversion(L);
|
||||
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
|
||||
luaL_openlibs(L); /* open libraries */
|
||||
lua_gc(L, LUA_GCRESTART, 0);
|
||||
/* run LUA_INIT */
|
||||
if (handle_luainit(L) != LUA_OK) return 0;
|
||||
if (!args[has_E] && handle_luainit(L) != LUA_OK)
|
||||
return 0; /* error running LUA_INIT */
|
||||
/* execute arguments -e and -l */
|
||||
if (!runargs(L, argv, (script > 0) ? script : argc)) return 0;
|
||||
/* execute main script (if there is one) */
|
||||
if (script && handle_script(L, argv, script) != LUA_OK) return 0;
|
||||
if (has_i) /* -i option? */
|
||||
if (args[has_i]) /* -i option? */
|
||||
dotty(L);
|
||||
else if (script == 0 && !has_e && !has_v) { /* no arguments? */
|
||||
else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */
|
||||
if (lua_stdin_is_tty()) {
|
||||
print_version();
|
||||
dotty(L);
|
||||
|
||||
26
lua.h
26
lua.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.275 2010/10/25 20:31:11 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.282 2011/11/29 15:55:08 roberto Exp roberto $
|
||||
** Lua - A Scripting Language
|
||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||
** See Copyright Notice at the end of this file
|
||||
@@ -18,12 +18,12 @@
|
||||
|
||||
#define LUA_VERSION_MAJOR "5"
|
||||
#define LUA_VERSION_MINOR "2"
|
||||
#define LUA_VERSION_RELEASE "0"
|
||||
#define LUA_VERSION_NUM 502
|
||||
#define LUA_VERSION_RELEASE "1"
|
||||
|
||||
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
||||
#define LUA_VERSION_NUM 502
|
||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2010 Lua.org, PUC-Rio"
|
||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2012 Lua.org, PUC-Rio"
|
||||
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
|
||||
|
||||
|
||||
@@ -215,10 +215,12 @@ LUA_API int (lua_pushthread) (lua_State *L);
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
LUA_API void (lua_getglobal) (lua_State *L, const char *var);
|
||||
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_rawgetp) (lua_State *L, int idx, const void *p);
|
||||
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);
|
||||
@@ -228,10 +230,12 @@ LUA_API void (lua_getuservalue) (lua_State *L, int idx);
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
LUA_API void (lua_setglobal) (lua_State *L, const char *var);
|
||||
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 void (lua_rawsetp) (lua_State *L, int idx, const void *p);
|
||||
LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
|
||||
LUA_API void (lua_setuservalue) (lua_State *L, int idx);
|
||||
|
||||
@@ -250,7 +254,8 @@ LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
|
||||
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
|
||||
|
||||
LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
|
||||
const char *chunkname);
|
||||
const char *chunkname,
|
||||
const char *mode);
|
||||
|
||||
LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);
|
||||
|
||||
@@ -261,7 +266,7 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);
|
||||
LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx,
|
||||
lua_CFunction k);
|
||||
#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
|
||||
LUA_API int (lua_resume) (lua_State *L, int narg);
|
||||
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
|
||||
LUA_API int (lua_status) (lua_State *L);
|
||||
|
||||
/*
|
||||
@@ -314,13 +319,6 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
||||
|
||||
#define lua_newtable(L) lua_createtable(L, 0, 0)
|
||||
|
||||
#define lua_setglobal(L,s) \
|
||||
(lua_pushglobaltable(L), lua_pushvalue(L, -2), \
|
||||
lua_setfield(L, -2, (s)), lua_pop(L, 2))
|
||||
|
||||
#define lua_getglobal(L,s) \
|
||||
(lua_pushglobaltable(L), lua_getfield(L, -1, (s)), lua_remove(L, -2))
|
||||
|
||||
#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
|
||||
|
||||
#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
|
||||
@@ -415,7 +413,7 @@ struct lua_Debug {
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (C) 1994-2010 Lua.org, PUC-Rio. All rights reserved.
|
||||
* Copyright (C) 1994-2012 Lua.org, PUC-Rio.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
|
||||
155
luaconf.h
155
luaconf.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: luaconf.h,v 1.150 2010/11/10 17:38:10 roberto Exp roberto $
|
||||
** $Id: luaconf.h,v 1.171 2012/05/11 14:10:50 roberto Exp roberto $
|
||||
** Configuration file for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -29,12 +29,13 @@
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(LUA_ANSI) && defined(_WIN32)
|
||||
#define LUA_WIN
|
||||
#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE)
|
||||
#define LUA_WIN /* enable goodies for regular Windows platforms */
|
||||
#endif
|
||||
|
||||
#if defined(LUA_WIN)
|
||||
#define LUA_DL_DLL
|
||||
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
|
||||
#endif
|
||||
|
||||
|
||||
@@ -43,12 +44,18 @@
|
||||
#define LUA_USE_POSIX
|
||||
#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
|
||||
#define LUA_USE_READLINE /* needs some extra libraries */
|
||||
#define LUA_USE_STRTODHEX /* assume 'strtod' handles hexa formats */
|
||||
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
|
||||
#define LUA_USE_LONGLONG /* assume support for long long */
|
||||
#endif
|
||||
|
||||
#if defined(LUA_USE_MACOSX)
|
||||
#define LUA_USE_POSIX
|
||||
#define LUA_USE_DLOPEN /* does not need -ldl */
|
||||
#define LUA_USE_READLINE /* needs an extra library: -lreadline */
|
||||
#define LUA_USE_STRTODHEX /* assume 'strtod' handles hexa formats */
|
||||
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
|
||||
#define LUA_USE_LONGLONG /* assume support for long long */
|
||||
#endif
|
||||
|
||||
|
||||
@@ -63,6 +70,7 @@
|
||||
#define LUA_USE_ISATTY
|
||||
#define LUA_USE_POPEN
|
||||
#define LUA_USE_ULONGJMP
|
||||
#define LUA_USE_GMTIME_R
|
||||
#endif
|
||||
|
||||
|
||||
@@ -166,13 +174,8 @@
|
||||
** give a warning about it. To avoid these warnings, change to the
|
||||
** default definition.
|
||||
*/
|
||||
#if defined(luaall_c) /* { */
|
||||
#define LUAI_FUNC static
|
||||
#define LUAI_DDEC static
|
||||
#define LUAI_DDEF static
|
||||
|
||||
#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
||||
defined(__ELF__)
|
||||
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
||||
defined(__ELF__) /* { */
|
||||
#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
|
||||
#define LUAI_DDEC LUAI_FUNC
|
||||
#define LUAI_DDEF /* empty */
|
||||
@@ -202,10 +205,15 @@
|
||||
|
||||
|
||||
/*
|
||||
@@ luai_writestring defines how 'print' prints its results.
|
||||
@@ luai_writestring/luai_writeline define how 'print' prints its results.
|
||||
** They are only used in libraries and the stand-alone program. (The #if
|
||||
** avoids including 'stdio.h' everywhere.)
|
||||
*/
|
||||
#if defined(LUA_LIB) || defined(lua_c)
|
||||
#include <stdio.h>
|
||||
#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
||||
#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout))
|
||||
#endif
|
||||
|
||||
/*
|
||||
@@ luai_writestringerror defines how to print error messages.
|
||||
@@ -215,6 +223,13 @@
|
||||
(fprintf(stderr, (s), (p)), fflush(stderr))
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_MAXSHORTLEN is the maximum length for short strings, that is,
|
||||
** strings that are internalized. (Cannot be smaller than reserved words
|
||||
** or tags for metamethods, as these strings must be internalized;
|
||||
** #("function") = 8, #("__newindex") = 10.)
|
||||
*/
|
||||
#define LUAI_MAXSHORTLEN 40
|
||||
|
||||
|
||||
|
||||
@@ -237,6 +252,12 @@
|
||||
*/
|
||||
#define LUA_COMPAT_UNPACK
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'.
|
||||
** You can replace it with 'package.searchers'.
|
||||
*/
|
||||
#define LUA_COMPAT_LOADERS
|
||||
|
||||
/*
|
||||
@@ macro 'lua_cpcall' emulates deprecated function lua_cpcall.
|
||||
** You can call your C function directly (with light C functions).
|
||||
@@ -253,6 +274,12 @@
|
||||
*/
|
||||
#define LUA_COMPAT_LOG10
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base
|
||||
** library. You can rewrite 'loadstring(s)' as 'load(s)'.
|
||||
*/
|
||||
#define LUA_COMPAT_LOADSTRING
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library.
|
||||
*/
|
||||
@@ -371,21 +398,34 @@
|
||||
@@ 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 */
|
||||
|
||||
|
||||
/*
|
||||
@@ lua_str2number converts a decimal numeric string to a number.
|
||||
@@ lua_strx2number converts an hexadecimal numeric string to a number.
|
||||
** In C99, 'strtod' do both conversions. C89, however, has no function
|
||||
** to convert floating hexadecimal strings to numbers. For these
|
||||
** systems, you can leave 'lua_strx2number' undefined and Lua will
|
||||
** provide its own implementation.
|
||||
*/
|
||||
#define lua_str2number(s,p) strtod((s), (p))
|
||||
|
||||
#if defined(LUA_USE_STRTODHEX)
|
||||
#define lua_strx2number(s,p) strtod((s), (p))
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ The luai_num* macros define the primitive operations over numbers.
|
||||
*/
|
||||
|
||||
/* the following operations need the math library */
|
||||
#if defined(lobject_c) || defined(lvm_c) || defined(luaall_c)
|
||||
#if defined(lobject_c) || defined(lvm_c)
|
||||
#include <math.h>
|
||||
#define luai_nummod(L,a,b) ((a) - floor((a)/(b))*(b))
|
||||
#define luai_numpow(L,a,b) (pow(a,b))
|
||||
@@ -420,40 +460,73 @@
|
||||
#define LUA_UNSIGNED unsigned LUA_INT32
|
||||
|
||||
|
||||
#if defined(LUA_CORE) /* { */
|
||||
|
||||
#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */
|
||||
|
||||
/* On a Microsoft compiler on a Pentium, use assembler to avoid clashes
|
||||
with a DirectX idiosyncrasy */
|
||||
#if defined(_MSC_VER) && defined(M_IX86) /* { */
|
||||
|
||||
#define MS_ASMTRICK
|
||||
|
||||
#else /* }{ */
|
||||
/* the next definition uses a trick that should work on any machine
|
||||
using IEEE754 with a 32-bit integer type */
|
||||
|
||||
#define LUA_IEEE754TRICK
|
||||
|
||||
/*
|
||||
@@ LUA_IEEEENDIAN is the endianness of doubles in your machine
|
||||
@@ (0 for little endian, 1 for big endian); if not defined, Lua will
|
||||
@@ check it dynamically.
|
||||
** Some tricks with doubles
|
||||
*/
|
||||
/* check for known architectures */
|
||||
#if defined(__i386__) || defined(__i386) || defined(i386) || \
|
||||
defined (__x86_64)
|
||||
#define LUA_IEEEENDIAN 0
|
||||
#elif defined(__POWERPC__) || defined(__ppc__)
|
||||
#define LUA_IEEEENDIAN 1
|
||||
#endif
|
||||
|
||||
#endif /* } */
|
||||
#if defined(LUA_CORE) && \
|
||||
defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */
|
||||
/*
|
||||
** The next definitions activate some tricks to speed up the
|
||||
** conversion from doubles to integer types, mainly to LUA_UNSIGNED.
|
||||
**
|
||||
@@ MS_ASMTRICK uses Microsoft assembler to avoid clashes with a
|
||||
** DirectX idiosyncrasy.
|
||||
**
|
||||
@@ LUA_IEEE754TRICK uses a trick that should work on any machine
|
||||
** using IEEE754 with a 32-bit integer type.
|
||||
**
|
||||
@@ LUA_IEEELL extends the trick to LUA_INTEGER; should only be
|
||||
** defined when LUA_INTEGER is a 32-bit integer.
|
||||
**
|
||||
@@ LUA_IEEEENDIAN is the endianness of doubles in your machine
|
||||
** (0 for little endian, 1 for big endian); if not defined, Lua will
|
||||
** check it dynamically for LUA_IEEE754TRICK (but not for LUA_NANTRICK).
|
||||
**
|
||||
@@ LUA_NANTRICK controls the use of a trick to pack all types into
|
||||
** a single double value, using NaN values to represent non-number
|
||||
** values. The trick only works on 32-bit machines (ints and pointers
|
||||
** are 32-bit values) with numbers represented as IEEE 754-2008 doubles
|
||||
** with conventional endianess (12345678 or 87654321), in CPUs that do
|
||||
** not produce signaling NaN values (all NaNs are quiet).
|
||||
*/
|
||||
|
||||
#endif /* } */
|
||||
/* Microsoft compiler on a Pentium (32 bit) ? */
|
||||
#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */
|
||||
|
||||
#endif /* } */
|
||||
#define MS_ASMTRICK
|
||||
#define LUA_IEEEENDIAN 0
|
||||
#define LUA_NANTRICK
|
||||
|
||||
|
||||
/* pentium 32 bits? */
|
||||
#elif defined(__i386__) || defined(__i386) || defined(__X86__) /* }{ */
|
||||
|
||||
#define LUA_IEEE754TRICK
|
||||
#define LUA_IEEELL
|
||||
#define LUA_IEEEENDIAN 0
|
||||
#define LUA_NANTRICK
|
||||
|
||||
/* pentium 64 bits? */
|
||||
#elif defined(__x86_64) /* }{ */
|
||||
|
||||
#define LUA_IEEE754TRICK
|
||||
#define LUA_IEEEENDIAN 0
|
||||
|
||||
#elif defined(__POWERPC__) || defined(__ppc__) /* }{ */
|
||||
|
||||
#define LUA_IEEE754TRICK
|
||||
#define LUA_IEEEENDIAN 1
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/* assume IEEE754 and a 32-bit integer type */
|
||||
#define LUA_IEEE754TRICK
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif /* } */
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
7
lualib.h
7
lualib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lualib.h,v 1.40 2010/06/10 21:29:47 roberto Exp roberto $
|
||||
** $Id: lualib.h,v 1.42 2011/05/25 14:12:28 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,9 +11,6 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/* Key to file-handle type */
|
||||
#define LUA_FILEHANDLE "FILE*"
|
||||
|
||||
|
||||
LUAMOD_API int (luaopen_base) (lua_State *L);
|
||||
|
||||
@@ -50,7 +47,7 @@ LUALIB_API void (luaL_openlibs) (lua_State *L);
|
||||
|
||||
|
||||
|
||||
#ifndef lua_assert
|
||||
#if !defined(lua_assert)
|
||||
#define lua_assert(x) ((void)0)
|
||||
#endif
|
||||
|
||||
|
||||
100
lundump.c
100
lundump.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.67 2010/10/13 21:04:52 lhf Exp $
|
||||
** $Id: lundump.c,v 2.21 2012/03/19 22:58:09 roberto Exp roberto $
|
||||
** load precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -27,7 +27,7 @@ typedef struct {
|
||||
const char* name;
|
||||
} LoadState;
|
||||
|
||||
static void error(LoadState* S, const char* why)
|
||||
static l_noret error(LoadState* S, const char* why)
|
||||
{
|
||||
luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
|
||||
luaD_throw(S->L,LUA_ERRSYNTAX);
|
||||
@@ -38,9 +38,13 @@ static void error(LoadState* S, const char* why)
|
||||
#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
|
||||
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
|
||||
|
||||
#if !defined(luai_verifycode)
|
||||
#define luai_verifycode(L,b,f) /* empty */
|
||||
#endif
|
||||
|
||||
static void LoadBlock(LoadState* S, void* b, size_t size)
|
||||
{
|
||||
if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted");
|
||||
if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
|
||||
}
|
||||
|
||||
static int LoadChar(LoadState* S)
|
||||
@@ -54,6 +58,7 @@ static int LoadInt(LoadState* S)
|
||||
{
|
||||
int x;
|
||||
LoadVar(S,x);
|
||||
if (x<0) error(S,"corrupted");
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -73,7 +78,7 @@ static TString* LoadString(LoadState* S)
|
||||
else
|
||||
{
|
||||
char* s=luaZ_openspace(S->L,S->b,size);
|
||||
LoadBlock(S,s,size);
|
||||
LoadBlock(S,s,size*sizeof(char));
|
||||
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
|
||||
}
|
||||
}
|
||||
@@ -86,7 +91,7 @@ static void LoadCode(LoadState* S, Proto* f)
|
||||
LoadVector(S,f->code,n,sizeof(Instruction));
|
||||
}
|
||||
|
||||
static Proto* LoadFunction(LoadState* S);
|
||||
static void LoadFunction(LoadState* S, Proto* f);
|
||||
|
||||
static void LoadConstants(LoadState* S, Proto* f)
|
||||
{
|
||||
@@ -113,13 +118,18 @@ static void LoadConstants(LoadState* S, Proto* f)
|
||||
case LUA_TSTRING:
|
||||
setsvalue2n(S->L,o,LoadString(S));
|
||||
break;
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
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);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
f->p[i]=luaF_newproto(S->L);
|
||||
LoadFunction(S,f->p[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadUpvalues(LoadState* S, Proto* f)
|
||||
@@ -131,8 +141,8 @@ static void LoadUpvalues(LoadState* S, Proto* f)
|
||||
for (i=0; i<n; i++) f->upvalues[i].name=NULL;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
f->upvalues[i].instack=LoadChar(S);
|
||||
f->upvalues[i].idx=LoadChar(S);
|
||||
f->upvalues[i].instack=LoadByte(S);
|
||||
f->upvalues[i].idx=LoadByte(S);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,10 +168,8 @@ static void LoadDebug(LoadState* S, Proto* f)
|
||||
for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
|
||||
}
|
||||
|
||||
static Proto* LoadFunction(LoadState* S)
|
||||
static void LoadFunction(LoadState* S, Proto* f)
|
||||
{
|
||||
Proto* f=luaF_newproto(S->L);
|
||||
setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
|
||||
f->linedefined=LoadInt(S);
|
||||
f->lastlinedefined=LoadInt(S);
|
||||
f->numparams=LoadByte(S);
|
||||
@@ -171,25 +179,34 @@ static Proto* LoadFunction(LoadState* S)
|
||||
LoadConstants(S,f);
|
||||
LoadUpvalues(S,f);
|
||||
LoadDebug(S,f);
|
||||
S->L->top--;
|
||||
return f;
|
||||
}
|
||||
|
||||
/* the code below must be consistent with the code in luaU_header */
|
||||
#define N0 LUAC_HEADERSIZE
|
||||
#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
|
||||
#define N2 N1+2
|
||||
#define N3 N2+6
|
||||
|
||||
static void LoadHeader(LoadState* S)
|
||||
{
|
||||
char h[LUAC_HEADERSIZE];
|
||||
char s[LUAC_HEADERSIZE];
|
||||
lu_byte h[LUAC_HEADERSIZE];
|
||||
lu_byte s[LUAC_HEADERSIZE];
|
||||
luaU_header(h);
|
||||
LoadBlock(S,s,LUAC_HEADERSIZE);
|
||||
if (memcmp(h,s,LUAC_HEADERSIZE)!=0) error(S,"incompatible");
|
||||
memcpy(s,h,sizeof(char)); /* first char already read */
|
||||
LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
|
||||
if (memcmp(h,s,N0)==0) return;
|
||||
if (memcmp(h,s,N1)!=0) error(S,"not a");
|
||||
if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
|
||||
if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
|
||||
}
|
||||
|
||||
/*
|
||||
** load precompiled chunk
|
||||
*/
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
|
||||
Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
|
||||
{
|
||||
LoadState S;
|
||||
Closure* cl;
|
||||
if (*name=='@' || *name=='=')
|
||||
S.name=name+1;
|
||||
else if (*name==LUA_SIGNATURE[0])
|
||||
@@ -200,25 +217,42 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
|
||||
S.Z=Z;
|
||||
S.b=buff;
|
||||
LoadHeader(&S);
|
||||
return LoadFunction(&S);
|
||||
cl=luaF_newLclosure(L,1);
|
||||
setclLvalue(L,L->top,cl); incr_top(L);
|
||||
cl->l.p=luaF_newproto(L);
|
||||
LoadFunction(&S,cl->l.p);
|
||||
if (cl->l.p->sizeupvalues != 1)
|
||||
{
|
||||
Proto* p=cl->l.p;
|
||||
cl=luaF_newLclosure(L,cl->l.p->sizeupvalues);
|
||||
cl->l.p=p;
|
||||
setclLvalue(L,L->top-1,cl);
|
||||
}
|
||||
luai_verifycode(L,buff,cl->l.p);
|
||||
return cl;
|
||||
}
|
||||
|
||||
#define MYINT(s) (s[0]-'0')
|
||||
#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
|
||||
#define FORMAT 0 /* this is the official format */
|
||||
|
||||
/*
|
||||
* make header
|
||||
* if you make any changes in the header or in LUA_SIGNATURE,
|
||||
* be sure to update LUAC_HEADERSIZE accordingly in lundump.h.
|
||||
* make header for precompiled chunks
|
||||
* if you change the code below be sure to update LoadHeader and FORMAT above
|
||||
* and LUAC_HEADERSIZE in lundump.h
|
||||
*/
|
||||
void luaU_header (char* h)
|
||||
void luaU_header (lu_byte* h)
|
||||
{
|
||||
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? */
|
||||
memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
|
||||
h+=sizeof(LUA_SIGNATURE)-sizeof(char);
|
||||
*h++=cast_byte(VERSION);
|
||||
*h++=cast_byte(FORMAT);
|
||||
*h++=cast_byte(*(char*)&x); /* endianness */
|
||||
*h++=cast_byte(sizeof(int));
|
||||
*h++=cast_byte(sizeof(size_t));
|
||||
*h++=cast_byte(sizeof(Instruction));
|
||||
*h++=cast_byte(sizeof(lua_Number));
|
||||
*h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
|
||||
memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
|
||||
}
|
||||
|
||||
22
lundump.h
22
lundump.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.39 2005/11/01 17:04:55 lhf Exp lhf $
|
||||
** $Id: lundump.h,v 1.38 2011/05/17 12:42:43 roberto Exp roberto $
|
||||
** load precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,26 +11,18 @@
|
||||
#include "lzio.h"
|
||||
|
||||
/* load one chunk; from lundump.c */
|
||||
LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);
|
||||
LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);
|
||||
|
||||
/* make header; from lundump.c */
|
||||
LUAI_FUNC void luaU_header (char* h);
|
||||
LUAI_FUNC void luaU_header (lu_byte* h);
|
||||
|
||||
/* dump one chunk; from ldump.c */
|
||||
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip);
|
||||
|
||||
#ifdef luac_c
|
||||
/* print one chunk; from print.c */
|
||||
LUAI_FUNC void luaU_print (const Proto* f, int full);
|
||||
#endif
|
||||
/* data to catch conversion errors */
|
||||
#define LUAC_TAIL "\x19\x93\r\n\x1a\n"
|
||||
|
||||
/* 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
|
||||
/* size in bytes of header of binary files */
|
||||
#define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char))
|
||||
|
||||
#endif
|
||||
|
||||
222
lvm.c
222
lvm.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.c,v 2.124 2010/10/25 19:01:37 roberto Exp roberto $
|
||||
** $Id: lvm.c,v 2.151 2012/05/14 17:50:49 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -35,7 +35,7 @@
|
||||
const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
|
||||
lua_Number num;
|
||||
if (ttisnumber(obj)) return obj;
|
||||
if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
|
||||
if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) {
|
||||
setnvalue(n, num);
|
||||
return n;
|
||||
}
|
||||
@@ -60,22 +60,31 @@ int luaV_tostring (lua_State *L, StkId obj) {
|
||||
static void traceexec (lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
lu_byte mask = L->hookmask;
|
||||
if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
|
||||
resethookcount(L);
|
||||
luaD_hook(L, LUA_HOOKCOUNT, -1);
|
||||
int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0);
|
||||
if (counthook)
|
||||
resethookcount(L); /* reset count */
|
||||
if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
|
||||
ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
|
||||
return; /* do not call hook again (VM yielded, so it did not move) */
|
||||
}
|
||||
if (counthook)
|
||||
luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
|
||||
if (mask & LUA_MASKLINE) {
|
||||
Proto *p = ci_func(ci)->l.p;
|
||||
Proto *p = ci_func(ci)->p;
|
||||
int npc = pcRel(ci->u.l.savedpc, p);
|
||||
int newline = getfuncline(p, npc);
|
||||
if (npc == 0 || /* call linehook when enter a new function, */
|
||||
ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
|
||||
newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */
|
||||
luaD_hook(L, LUA_HOOKLINE, newline);
|
||||
luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
|
||||
}
|
||||
L->oldpc = ci->u.l.savedpc;
|
||||
if (L->status == LUA_YIELD) { /* did hook yield? */
|
||||
if (counthook)
|
||||
L->hookcount = 1; /* undo decrement to zero */
|
||||
ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
|
||||
ci->callstatus |= CIST_HOOKYIELD; /* mark that it yieled */
|
||||
ci->func = L->top - 1; /* protect stack below results */
|
||||
luaD_throw(L, LUA_YIELD);
|
||||
}
|
||||
}
|
||||
@@ -127,29 +136,38 @@ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
|
||||
|
||||
void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
|
||||
int loop;
|
||||
TValue temp;
|
||||
for (loop = 0; loop < MAXTAGLOOP; loop++) {
|
||||
const TValue *tm;
|
||||
if (ttistable(t)) { /* `t' is a table? */
|
||||
Table *h = hvalue(t);
|
||||
TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
|
||||
if (!ttisnil(oldval) || /* result is not nil? */
|
||||
(tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
|
||||
setobj2t(L, oldval, val);
|
||||
TValue *oldval = cast(TValue *, luaH_get(h, key));
|
||||
/* if previous value is not nil, there must be a previous entry
|
||||
in the table; moreover, a metamethod has no relevance */
|
||||
if (!ttisnil(oldval) ||
|
||||
/* previous value is nil; must check the metamethod */
|
||||
((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
|
||||
/* no metamethod; is there a previous entry in the table? */
|
||||
(oldval != luaO_nilobject ||
|
||||
/* no previous entry; must create one. (The next test is
|
||||
always true; we only need the assignment.) */
|
||||
(oldval = luaH_newkey(L, h, key), 1)))) {
|
||||
/* no metamethod and (now) there is an entry with given key */
|
||||
setobj2t(L, oldval, val); /* assign new value to that entry */
|
||||
invalidateTMcache(h);
|
||||
luaC_barrierback(L, obj2gco(h), val);
|
||||
return;
|
||||
}
|
||||
/* else will try the tag method */
|
||||
/* else will try the metamethod */
|
||||
}
|
||||
else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
|
||||
luaG_typeerror(L, t, "index");
|
||||
else /* not a table; check metamethod */
|
||||
if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
|
||||
luaG_typeerror(L, t, "index");
|
||||
/* there is a metamethod */
|
||||
if (ttisfunction(tm)) {
|
||||
callTM(L, tm, t, key, val, 0);
|
||||
return;
|
||||
}
|
||||
/* else repeat with 'tm' */
|
||||
setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */
|
||||
t = &temp;
|
||||
t = tm; /* else repeat with 'tm' */
|
||||
}
|
||||
luaG_runerror(L, "loop in settable");
|
||||
}
|
||||
@@ -161,7 +179,6 @@ static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
if (ttisnil(tm))
|
||||
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
|
||||
if (ttisnil(tm)) return 0;
|
||||
if (event == TM_UNM) p2 = luaO_nilobject;
|
||||
callTM(L, tm, p1, p2, res, 1);
|
||||
return 1;
|
||||
}
|
||||
@@ -175,7 +192,7 @@ static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2,
|
||||
if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
|
||||
tm2 = fasttm(L, mt2, event);
|
||||
if (tm2 == NULL) return NULL; /* no metamethod */
|
||||
if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
|
||||
if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */
|
||||
return tm1;
|
||||
return NULL;
|
||||
}
|
||||
@@ -218,9 +235,9 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
|
||||
return luai_numlt(L, nvalue(l), nvalue(r));
|
||||
else if (ttisstring(l) && ttisstring(r))
|
||||
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
|
||||
else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
|
||||
return res;
|
||||
return luaG_ordererror(L, l, r);
|
||||
else if ((res = call_orderTM(L, l, r, TM_LT)) < 0)
|
||||
luaG_ordererror(L, l, r);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@@ -230,35 +247,43 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
|
||||
return luai_numle(L, nvalue(l), nvalue(r));
|
||||
else if (ttisstring(l) && ttisstring(r))
|
||||
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
|
||||
else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
|
||||
else if ((res = call_orderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */
|
||||
return res;
|
||||
else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
|
||||
return !res;
|
||||
return luaG_ordererror(L, l, r);
|
||||
else if ((res = call_orderTM(L, r, l, TM_LT)) < 0) /* else try `lt' */
|
||||
luaG_ordererror(L, l, r);
|
||||
return !res;
|
||||
}
|
||||
|
||||
|
||||
int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2) {
|
||||
/*
|
||||
** equality of Lua values. L == NULL means raw equality (no metamethods)
|
||||
*/
|
||||
int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) {
|
||||
const TValue *tm;
|
||||
lua_assert(ttype(t1) == ttype(t2));
|
||||
lua_assert(ttisequal(t1, t2));
|
||||
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); /* true must be 1 !! */
|
||||
case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
|
||||
case LUA_TLCF: return fvalue(t1) == fvalue(t2);
|
||||
case LUA_TSTRING: return eqstr(rawtsvalue(t1), rawtsvalue(t2));
|
||||
case LUA_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2));
|
||||
case LUA_TLNGSTR: return luaS_eqlngstr(rawtsvalue(t1), rawtsvalue(t2));
|
||||
case LUA_TUSERDATA: {
|
||||
if (uvalue(t1) == uvalue(t2)) return 1;
|
||||
else if (L == NULL) return 0;
|
||||
tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ);
|
||||
break; /* will try TM */
|
||||
}
|
||||
case LUA_TTABLE: {
|
||||
if (hvalue(t1) == hvalue(t2)) return 1;
|
||||
else if (L == NULL) return 0;
|
||||
tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
|
||||
break; /* will try TM */
|
||||
}
|
||||
default: return gcvalue(t1) == gcvalue(t2);
|
||||
default:
|
||||
lua_assert(iscollectable(t1));
|
||||
return gcvalue(t1) == gcvalue(t2);
|
||||
}
|
||||
if (tm == NULL) return 0; /* no TM? */
|
||||
callTM(L, tm, t1, t2, L->top, 1); /* call TM */
|
||||
@@ -278,7 +303,7 @@ void luaV_concat (lua_State *L, int total) {
|
||||
else if (tsvalue(top-1)->len == 0) /* second operand is empty? */
|
||||
(void)tostring(L, top - 2); /* result is first operand */
|
||||
else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
|
||||
setsvalue2s(L, top-2, rawtsvalue(top-1)); /* result is second op. */
|
||||
setobjs2s(L, top - 2, top - 1); /* result is second op. */
|
||||
}
|
||||
else {
|
||||
/* at least two non-empty string values; get as many as possible */
|
||||
@@ -286,18 +311,20 @@ void luaV_concat (lua_State *L, int total) {
|
||||
char *buffer;
|
||||
int i;
|
||||
/* collect total length */
|
||||
for (n = 1; n < total && tostring(L, top-n-1); n++) {
|
||||
size_t l = tsvalue(top-n-1)->len;
|
||||
if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
|
||||
for (i = 1; i < total && tostring(L, top-i-1); i++) {
|
||||
size_t l = tsvalue(top-i-1)->len;
|
||||
if (l >= (MAX_SIZET/sizeof(char)) - tl)
|
||||
luaG_runerror(L, "string length overflow");
|
||||
tl += l;
|
||||
}
|
||||
buffer = luaZ_openspace(L, &G(L)->buff, tl);
|
||||
tl = 0;
|
||||
for (i=n; i>0; i--) { /* concat all strings */
|
||||
n = i;
|
||||
do { /* concat all strings */
|
||||
size_t l = tsvalue(top-i)->len;
|
||||
memcpy(buffer+tl, svalue(top-i), l);
|
||||
memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
|
||||
tl += l;
|
||||
}
|
||||
} while (--i > 0);
|
||||
setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
|
||||
}
|
||||
total -= n-1; /* got 'n' strings to create 1 new */
|
||||
@@ -308,7 +335,7 @@ void luaV_concat (lua_State *L, int total) {
|
||||
|
||||
void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
|
||||
const TValue *tm;
|
||||
switch (ttype(rb)) {
|
||||
switch (ttypenv(rb)) {
|
||||
case LUA_TTABLE: {
|
||||
Table *h = hvalue(rb);
|
||||
tm = fasttm(L, h->metatable, TM_LEN);
|
||||
@@ -327,7 +354,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
callTM(L, tm, rb, luaO_nilobject, ra, 1);
|
||||
callTM(L, tm, rb, rb, ra, 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -377,8 +404,9 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
|
||||
int nup = p->sizeupvalues;
|
||||
Upvaldesc *uv = p->upvalues;
|
||||
int i;
|
||||
Closure *ncl = luaF_newLclosure(L, p);
|
||||
setclvalue(L, ra, ncl); /* anchor new closure in stack */
|
||||
Closure *ncl = luaF_newLclosure(L, nup);
|
||||
ncl->l.p = p;
|
||||
setclLvalue(L, ra, ncl); /* anchor new closure in stack */
|
||||
for (i = 0; i < nup; i++) { /* fill in its upvalues */
|
||||
if (uv[i].instack) /* upvalue refers to local variable? */
|
||||
ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
|
||||
@@ -454,6 +482,11 @@ void luaV_finishOp (lua_State *L) {
|
||||
** some macros for common tasks in `luaV_execute'
|
||||
*/
|
||||
|
||||
#if !defined luai_runtimecheck
|
||||
#define luai_runtimecheck(L, c) /* void */
|
||||
#endif
|
||||
|
||||
|
||||
#define RA(i) (base+GETARG_A(i))
|
||||
/* to be used after possible stack reallocation */
|
||||
#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
|
||||
@@ -466,12 +499,23 @@ void luaV_finishOp (lua_State *L) {
|
||||
(k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
|
||||
|
||||
|
||||
#define dojump(i) (ci->u.l.savedpc += (i))
|
||||
/* execute a jump instruction */
|
||||
#define dojump(ci,i,e) \
|
||||
{ int a = GETARG_A(i); \
|
||||
if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
|
||||
ci->u.l.savedpc += GETARG_sBx(i) + e; }
|
||||
|
||||
/* for test instructions, execute the jump instruction that follows it */
|
||||
#define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
|
||||
|
||||
|
||||
#define Protect(x) { {x;}; base = ci->u.l.base; }
|
||||
|
||||
#define checkGC(L) Protect(luaC_checkGC(L); luai_threadyield(L);)
|
||||
#define checkGC(L,c) \
|
||||
Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
|
||||
luaC_step(L); \
|
||||
L->top = ci->top;}) /* restore top */ \
|
||||
luai_threadyield(L); )
|
||||
|
||||
|
||||
#define arith_op(op,tm) { \
|
||||
@@ -486,6 +530,7 @@ void luaV_finishOp (lua_State *L) {
|
||||
|
||||
#define vmdispatch(o) switch(o)
|
||||
#define vmcase(l,b) case l: {b} break;
|
||||
#define vmcasenb(l,b) case l: {b} /* nb = no break */
|
||||
|
||||
void luaV_execute (lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
@@ -493,9 +538,8 @@ void luaV_execute (lua_State *L) {
|
||||
TValue *k;
|
||||
StkId base;
|
||||
newframe: /* reentry point when frame changes (call/return) */
|
||||
lua_assert(isLua(ci));
|
||||
lua_assert(ci == L->ci);
|
||||
cl = &clvalue(ci->func)->l;
|
||||
cl = clLvalue(ci->func);
|
||||
k = cl->p->k;
|
||||
base = ci->u.l.base;
|
||||
/* main loop of interpreter */
|
||||
@@ -506,7 +550,7 @@ void luaV_execute (lua_State *L) {
|
||||
(--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
|
||||
Protect(traceexec(L));
|
||||
}
|
||||
/* warning!! several calls may realloc the stack and invalidate `ra' */
|
||||
/* WARNING: several calls may realloc the stack and invalidate `ra' */
|
||||
ra = RA(i);
|
||||
lua_assert(base == ci->u.l.base);
|
||||
lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
|
||||
@@ -515,7 +559,13 @@ void luaV_execute (lua_State *L) {
|
||||
setobjs2s(L, ra, RB(i));
|
||||
)
|
||||
vmcase(OP_LOADK,
|
||||
TValue *rb = KBx(i);
|
||||
TValue *rb = k + GETARG_Bx(i);
|
||||
setobj2s(L, ra, rb);
|
||||
)
|
||||
vmcase(OP_LOADKX,
|
||||
TValue *rb;
|
||||
lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
|
||||
rb = k + GETARG_Ax(*ci->u.l.savedpc++);
|
||||
setobj2s(L, ra, rb);
|
||||
)
|
||||
vmcase(OP_LOADBOOL,
|
||||
@@ -523,10 +573,10 @@ void luaV_execute (lua_State *L) {
|
||||
if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
|
||||
)
|
||||
vmcase(OP_LOADNIL,
|
||||
TValue *rb = RB(i);
|
||||
int b = GETARG_B(i);
|
||||
do {
|
||||
setnilvalue(rb--);
|
||||
} while (rb >= ra);
|
||||
setnilvalue(ra++);
|
||||
} while (b--);
|
||||
)
|
||||
vmcase(OP_GETUPVAL,
|
||||
int b = GETARG_B(i);
|
||||
@@ -558,7 +608,7 @@ void luaV_execute (lua_State *L) {
|
||||
sethvalue(L, ra, t);
|
||||
if (b != 0 || c != 0)
|
||||
luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
|
||||
checkGC(L);
|
||||
checkGC(L, ra + 1);
|
||||
)
|
||||
vmcase(OP_SELF,
|
||||
StkId rb = RB(i);
|
||||
@@ -594,7 +644,8 @@ void luaV_execute (lua_State *L) {
|
||||
}
|
||||
)
|
||||
vmcase(OP_NOT,
|
||||
int res = l_isfalse(RB(i)); /* next assignment may change this value */
|
||||
TValue *rb = RB(i);
|
||||
int res = l_isfalse(rb); /* next assignment may change this value */
|
||||
setbvalue(ra, res);
|
||||
)
|
||||
vmcase(OP_LEN,
|
||||
@@ -603,49 +654,58 @@ void luaV_execute (lua_State *L) {
|
||||
vmcase(OP_CONCAT,
|
||||
int b = GETARG_B(i);
|
||||
int c = GETARG_C(i);
|
||||
StkId rb;
|
||||
L->top = base + c + 1; /* mark the end of concat operands */
|
||||
Protect(luaV_concat(L, c-b+1); checkGC(L);)
|
||||
Protect(luaV_concat(L, c - b + 1));
|
||||
ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
|
||||
rb = b + base;
|
||||
setobjs2s(L, ra, rb);
|
||||
checkGC(L, (ra >= rb ? ra + 1 : rb));
|
||||
L->top = ci->top; /* restore top */
|
||||
setobjs2s(L, RA(i), base+b);
|
||||
)
|
||||
vmcase(OP_JMP,
|
||||
dojump(GETARG_sBx(i));
|
||||
dojump(ci, i, 0);
|
||||
)
|
||||
vmcase(OP_EQ,
|
||||
TValue *rb = RKB(i);
|
||||
TValue *rc = RKC(i);
|
||||
Protect(
|
||||
if (equalobj(L, rb, rc) == GETARG_A(i))
|
||||
dojump(GETARG_sBx(*ci->u.l.savedpc));
|
||||
if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i))
|
||||
ci->u.l.savedpc++;
|
||||
else
|
||||
donextjump(ci);
|
||||
)
|
||||
ci->u.l.savedpc++;
|
||||
)
|
||||
vmcase(OP_LT,
|
||||
Protect(
|
||||
if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
|
||||
dojump(GETARG_sBx(*ci->u.l.savedpc));
|
||||
if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
|
||||
ci->u.l.savedpc++;
|
||||
else
|
||||
donextjump(ci);
|
||||
)
|
||||
ci->u.l.savedpc++;
|
||||
)
|
||||
vmcase(OP_LE,
|
||||
Protect(
|
||||
if (luaV_lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
|
||||
dojump(GETARG_sBx(*ci->u.l.savedpc));
|
||||
if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
|
||||
ci->u.l.savedpc++;
|
||||
else
|
||||
donextjump(ci);
|
||||
)
|
||||
ci->u.l.savedpc++;
|
||||
)
|
||||
vmcase(OP_TEST,
|
||||
if (GETARG_C(i) ? !l_isfalse(ra) : l_isfalse(ra))
|
||||
dojump(GETARG_sBx(*ci->u.l.savedpc));
|
||||
ci->u.l.savedpc++;
|
||||
if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
|
||||
ci->u.l.savedpc++;
|
||||
else
|
||||
donextjump(ci);
|
||||
)
|
||||
vmcase(OP_TESTSET,
|
||||
TValue *rb = RB(i);
|
||||
if (GETARG_C(i) ? !l_isfalse(rb) : l_isfalse(rb)) {
|
||||
if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
|
||||
ci->u.l.savedpc++;
|
||||
else {
|
||||
setobjs2s(L, ra, rb);
|
||||
dojump(GETARG_sBx(*ci->u.l.savedpc));
|
||||
donextjump(ci);
|
||||
}
|
||||
ci->u.l.savedpc++;
|
||||
)
|
||||
vmcase(OP_CALL,
|
||||
int b = GETARG_B(i);
|
||||
@@ -690,7 +750,7 @@ void luaV_execute (lua_State *L) {
|
||||
goto newframe; /* restart luaV_execute over new Lua function */
|
||||
}
|
||||
)
|
||||
vmcase(OP_RETURN,
|
||||
vmcasenb(OP_RETURN,
|
||||
int b = GETARG_B(i);
|
||||
if (b != 0) L->top = ra+b-1;
|
||||
if (cl->p->sizep > 0) luaF_close(L, base);
|
||||
@@ -711,7 +771,7 @@ void luaV_execute (lua_State *L) {
|
||||
lua_Number limit = nvalue(ra+1);
|
||||
if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
|
||||
: luai_numle(L, limit, idx)) {
|
||||
dojump(GETARG_sBx(i)); /* jump back */
|
||||
ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
|
||||
setnvalue(ra, idx); /* update internal index... */
|
||||
setnvalue(ra+3, idx); /* ...and external index */
|
||||
}
|
||||
@@ -727,9 +787,9 @@ void luaV_execute (lua_State *L) {
|
||||
else if (!tonumber(pstep, ra+2))
|
||||
luaG_runerror(L, LUA_QL("for") " step must be a number");
|
||||
setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
|
||||
dojump(GETARG_sBx(i));
|
||||
ci->u.l.savedpc += GETARG_sBx(i);
|
||||
)
|
||||
vmcase(OP_TFORCALL,
|
||||
vmcasenb(OP_TFORCALL,
|
||||
StkId cb = ra + 3; /* call base */
|
||||
setobjs2s(L, cb+2, ra+2);
|
||||
setobjs2s(L, cb+1, ra+1);
|
||||
@@ -746,7 +806,7 @@ void luaV_execute (lua_State *L) {
|
||||
l_tforloop:
|
||||
if (!ttisnil(ra + 1)) { /* continue loop? */
|
||||
setobjs2s(L, ra, ra + 1); /* save control variable */
|
||||
dojump(GETARG_sBx(i)); /* jump back */
|
||||
ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
|
||||
}
|
||||
)
|
||||
vmcase(OP_SETLIST,
|
||||
@@ -759,28 +819,26 @@ void luaV_execute (lua_State *L) {
|
||||
lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
|
||||
c = GETARG_Ax(*ci->u.l.savedpc++);
|
||||
}
|
||||
luai_runtimecheck(L, ttistable(ra));
|
||||
h = hvalue(ra);
|
||||
last = ((c-1)*LFIELDS_PER_FLUSH) + n;
|
||||
if (last > h->sizearray) /* needs more space? */
|
||||
luaH_resizearray(L, h, last); /* pre-allocate it at once */
|
||||
for (; n > 0; n--) {
|
||||
TValue *val = ra+n;
|
||||
setobj2t(L, luaH_setint(L, h, last--), val);
|
||||
luaH_setint(L, h, last--, val);
|
||||
luaC_barrierback(L, obj2gco(h), val);
|
||||
}
|
||||
L->top = ci->top; /* correct top (in case of previous open call) */
|
||||
)
|
||||
vmcase(OP_CLOSE,
|
||||
luaF_close(L, ra);
|
||||
)
|
||||
vmcase(OP_CLOSURE,
|
||||
Proto *p = cl->p->p[GETARG_Bx(i)];
|
||||
Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */
|
||||
if (ncl == NULL) /* no match? */
|
||||
pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
|
||||
else
|
||||
setclvalue(L, ra, ncl); /* push cashed closure */
|
||||
checkGC(L);
|
||||
setclLvalue(L, ra, ncl); /* push cashed closure */
|
||||
checkGC(L, ra + 1);
|
||||
)
|
||||
vmcase(OP_VARARG,
|
||||
int b = GETARG_B(i) - 1;
|
||||
|
||||
11
lvm.h
11
lvm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 2.13 2009/11/19 19:04:58 roberto Exp roberto $
|
||||
** $Id: lvm.h,v 2.16 2011/05/31 18:24:36 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -17,12 +17,15 @@
|
||||
|
||||
#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
|
||||
|
||||
#define equalobj(L,o1,o2) \
|
||||
(ttype(o1) == ttype(o2) && luaV_equalval_(L, o1, o2))
|
||||
#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2))
|
||||
|
||||
#define luaV_rawequalobj(t1,t2) \
|
||||
(ttisequal(t1,t2) && luaV_equalobj_(NULL,t1,t2))
|
||||
|
||||
|
||||
/* not to called directly */
|
||||
LUAI_FUNC int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2);
|
||||
LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2);
|
||||
|
||||
|
||||
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
|
||||
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
|
||||
|
||||
34
lzio.c
34
lzio.c
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.30 2005/05/17 19:49:15 roberto Exp roberto $
|
||||
** a generic input stream interface
|
||||
** $Id: lzio.c,v 1.34 2011/07/15 12:35:32 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -25,23 +25,11 @@ int luaZ_fill (ZIO *z) {
|
||||
lua_unlock(L);
|
||||
buff = z->reader(L, z->data, &size);
|
||||
lua_lock(L);
|
||||
if (buff == NULL || size == 0) return EOZ;
|
||||
z->n = size - 1;
|
||||
if (buff == NULL || size == 0)
|
||||
return EOZ;
|
||||
z->n = size - 1; /* discount char being returned */
|
||||
z->p = buff;
|
||||
return char2int(*(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);
|
||||
return cast_uchar(*(z->p++));
|
||||
}
|
||||
|
||||
|
||||
@@ -58,8 +46,14 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
|
||||
size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
||||
while (n) {
|
||||
size_t m;
|
||||
if (luaZ_lookahead(z) == EOZ)
|
||||
return n; /* return number of missing bytes */
|
||||
if (z->n == 0) { /* no bytes in buffer? */
|
||||
if (luaZ_fill(z) == EOZ) /* try to read more */
|
||||
return n; /* no more input; return number of missing bytes */
|
||||
else {
|
||||
z->n++; /* luaZ_fill consumed first byte; put it back */
|
||||
z->p--;
|
||||
}
|
||||
}
|
||||
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
||||
memcpy(b, z->p, m);
|
||||
z->n -= m;
|
||||
|
||||
11
lzio.h
11
lzio.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.h,v 1.21 2005/05/17 19:49:15 roberto Exp roberto $
|
||||
** $Id: lzio.h,v 1.25 2011/07/15 12:35:32 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -17,11 +17,7 @@
|
||||
|
||||
typedef struct Zio ZIO;
|
||||
|
||||
#define char2int(c) cast(int, cast(unsigned char, (c)))
|
||||
|
||||
#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
|
||||
|
||||
#define zungetc(z) ((z)->n++, (z)->p--)
|
||||
#define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z))
|
||||
|
||||
|
||||
typedef struct Mbuffer {
|
||||
@@ -50,7 +46,6 @@ 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);
|
||||
|
||||
|
||||
|
||||
@@ -59,7 +54,7 @@ LUAI_FUNC int luaZ_lookahead (ZIO *z);
|
||||
struct Zio {
|
||||
size_t n; /* bytes still unread */
|
||||
const char *p; /* current position in buffer */
|
||||
lua_Reader reader;
|
||||
lua_Reader reader; /* reader function */
|
||||
void* data; /* additional data */
|
||||
lua_State *L; /* Lua state (for reader) */
|
||||
};
|
||||
|
||||
16
makefile
16
makefile
@@ -18,9 +18,9 @@ CWARNSC=-pedantic -Wextra \
|
||||
-Wstrict-aliasing \
|
||||
# the next warnings generate to much noise, so they are disabled
|
||||
# -Wlogical-op \
|
||||
# -Wstrict-overflow=2 \
|
||||
# -Wformat=2 \
|
||||
# -Wstrict-overflow=5 \
|
||||
# -Wcast-qual \
|
||||
# -Wcast-qual \
|
||||
|
||||
# The next warnings are not valid for C++
|
||||
CWARNS= $(CWARNSC) \
|
||||
@@ -35,14 +35,17 @@ CWARNS= $(CWARNSC) \
|
||||
# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"'
|
||||
# -g -DLUA_USER_H='"ltests.h"'
|
||||
# -fomit-frame-pointer #-pg -malign-double
|
||||
TESTS= -DLUA_USER_H='"ltests.h"' # -g -O0
|
||||
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
||||
TESTS= -DLUA_USER_H='"ltests.h"'
|
||||
|
||||
# -mtune=native -fomit-frame-pointer
|
||||
LOCAL = $(TESTS) $(CWARNS) -g
|
||||
|
||||
LOCAL = $(TESTS) $(CWARNS)
|
||||
|
||||
|
||||
# enable Linux goodies
|
||||
MYCFLAGS= $(LOCAL) -DLUA_USE_LINUX -DLUA_COMPAT_ALL -g
|
||||
MYLDFLAGS= -Wl,-E -g
|
||||
MYCFLAGS= $(LOCAL) -DLUA_USE_LINUX -DLUA_COMPAT_ALL
|
||||
MYLDFLAGS= $(LOCAL) -Wl,-E
|
||||
MYLIBS= -ldl -lreadline -lhistory -lncurses
|
||||
|
||||
|
||||
@@ -116,6 +119,7 @@ echo:
|
||||
$(ALL_O): makefile
|
||||
|
||||
# DO NOT DELETE
|
||||
# automatically made with 'gcc -MM'
|
||||
|
||||
lapi.o: lapi.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \
|
||||
lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h ltable.h lundump.h \
|
||||
|
||||
Reference in New Issue
Block a user