mirror of
https://github.com/lua/lua.git
synced 2026-07-26 16:09:07 +00:00
Compare commits
168 Commits
| 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 |
341
bugs
341
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.108 2010/05/14 15:34:57 roberto Exp roberto $
|
||||
+** $Id: bugs,v 1.108 2010/05/14 15:34:57 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
|
||||
*/
|
||||
@@ -2359,3 +2359,340 @@ patch = [[
|
||||
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 = [[
|
||||
]]
|
||||
}
|
||||
]=]
|
||||
|
||||
147
lapi.c
147
lapi.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 2.148 2011/06/02 19:31:40 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 {
|
||||
CClosure *func = clCvalue(ci->func);
|
||||
return (idx <= func->nupvalues)
|
||||
? &func->upvalue[idx-1]
|
||||
: cast(TValue *, luaO_nilobject);
|
||||
return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,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);
|
||||
}
|
||||
|
||||
|
||||
@@ -275,8 +277,7 @@ 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
|
||||
: luaV_rawequalobj(o1, o2);
|
||||
return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -305,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;
|
||||
@@ -595,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);
|
||||
@@ -628,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);
|
||||
}
|
||||
@@ -700,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);
|
||||
@@ -732,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);
|
||||
@@ -739,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);
|
||||
}
|
||||
@@ -896,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;
|
||||
@@ -914,13 +968,13 @@ 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? */
|
||||
LClosure *f = clLvalue(L->top - 1); /* get newly created function */
|
||||
if (f->nupvalues == 1) { /* does it have one upvalue? */
|
||||
@@ -991,17 +1045,17 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
|
||||
}
|
||||
case LUA_GCSTEP: {
|
||||
if (g->gckind == KGC_GEN) { /* generational mode? */
|
||||
res = (g->lastmajormem == 0); /* 1 if will do major collection */
|
||||
res = (g->GCestimate == 0); /* true if it will do major collection */
|
||||
luaC_forcestep(L); /* do a single step */
|
||||
}
|
||||
else {
|
||||
while (data-- >= 0) {
|
||||
luaC_forcestep(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 */
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -1128,7 +1182,6 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
|
||||
|
||||
|
||||
|
||||
|
||||
static const char *aux_upvalue (StkId fi, int n, TValue **val,
|
||||
GCObject **owner) {
|
||||
switch (ttype(fi)) {
|
||||
@@ -1141,15 +1194,13 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val,
|
||||
}
|
||||
case LUA_TLCL: { /* Lua closure */
|
||||
LClosure *f = clLvalue(fi);
|
||||
const char *name;
|
||||
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 = getstr(p->upvalues[n-1].name);
|
||||
if (name == NULL) /* no debug information? */
|
||||
name = "";
|
||||
return name;
|
||||
name = p->upvalues[n-1].name;
|
||||
return (name == NULL) ? "" : getstr(name);
|
||||
}
|
||||
default: return NULL; /* not a closure */
|
||||
}
|
||||
@@ -1158,7 +1209,7 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val,
|
||||
|
||||
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) {
|
||||
@@ -1172,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);
|
||||
|
||||
52
lauxlib.c
52
lauxlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.232 2011/05/03 16:01:57 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 */
|
||||
@@ -270,7 +269,7 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) {
|
||||
*/
|
||||
|
||||
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);
|
||||
@@ -291,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 */
|
||||
@@ -331,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
|
||||
@@ -519,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 */
|
||||
@@ -566,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 */
|
||||
@@ -615,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;
|
||||
@@ -646,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) {
|
||||
@@ -666,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;
|
||||
@@ -674,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);
|
||||
}
|
||||
|
||||
|
||||
@@ -841,6 +845,7 @@ 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->name != NULL; l++) { /* fill the table with given functions */
|
||||
int i;
|
||||
@@ -861,8 +866,8 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
|
||||
lua_getfield(L, idx, fname);
|
||||
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 */
|
||||
@@ -887,10 +892,8 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
|
||||
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 */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -913,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;
|
||||
@@ -943,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);
|
||||
|
||||
39
lauxlib.h
39
lauxlib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.116 2011/04/08 19:17:36 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
|
||||
*/
|
||||
@@ -72,9 +72,13 @@ LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
|
||||
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);
|
||||
@@ -127,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)
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
@@ -163,6 +169,31 @@ 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)
|
||||
|
||||
|
||||
115
lbaselib.c
115
lbaselib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lbaselib.c,v 1.263 2011/07/02 15:56:43 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
|
||||
*/
|
||||
@@ -46,20 +46,22 @@ static int luaB_print (lua_State *L) {
|
||||
#define SPACECHARS " \f\n\r\t\v"
|
||||
|
||||
static int luaB_tonumber (lua_State *L) {
|
||||
int base = luaL_optint(L, 2, 10);
|
||||
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
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 */
|
||||
} /* else not a number; must be something */
|
||||
luaL_checkany(L, 1);
|
||||
}
|
||||
else {
|
||||
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");
|
||||
s += strspn(s, SPACECHARS); /* skip initial spaces */
|
||||
if (*s == '-') { s++; neg = 1; } /* handle signal */
|
||||
else if (*s == '+') s++;
|
||||
@@ -158,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};
|
||||
@@ -253,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);
|
||||
}
|
||||
|
||||
|
||||
@@ -264,26 +273,6 @@ static int luaB_loadfile (lua_State *L) {
|
||||
*/
|
||||
|
||||
|
||||
typedef struct {
|
||||
char c;
|
||||
const char *mode;
|
||||
} loaddata;
|
||||
|
||||
|
||||
/*
|
||||
** check whether a chunk (prefix in 's') satisfies given 'mode'
|
||||
** ('t' for text, 'b' for binary). Returns error message (also
|
||||
** pushed on the stack) in case of errors.
|
||||
*/
|
||||
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 */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** reserved slot, above all arguments, to hold a copy of the returned
|
||||
** string to avoid it being collected while parsed. 'load' has four
|
||||
@@ -299,28 +288,19 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) {
|
||||
** reserved slot inside the stack.
|
||||
*/
|
||||
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
|
||||
const char *s;
|
||||
loaddata *ld = (loaddata *)ud;
|
||||
(void)(ud); /* not used */
|
||||
luaL_checkstack(L, 2, "too many nested functions");
|
||||
lua_pushvalue(L, 1); /* get function */
|
||||
lua_call(L, 0, 1); /* call it */
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1); /* pop result */
|
||||
*size = 0;
|
||||
return NULL;
|
||||
}
|
||||
else if ((s = lua_tostring(L, -1)) != NULL) {
|
||||
if (ld->mode != NULL) { /* first time? */
|
||||
s = checkrights(L, ld->mode, s); /* check mode */
|
||||
ld->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 */
|
||||
}
|
||||
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
|
||||
return lua_tolstring(L, RESERVEDSLOT, size);
|
||||
}
|
||||
|
||||
|
||||
@@ -332,16 +312,13 @@ static int luaB_load (lua_State *L) {
|
||||
const char *mode = luaL_optstring(L, 3, "bt");
|
||||
if (s != NULL) { /* loading a string? */
|
||||
const char *chunkname = luaL_optstring(L, 2, s);
|
||||
status = (checkrights(L, mode, s) != NULL)
|
||||
|| luaL_loadbuffer(L, s, l, chunkname);
|
||||
status = luaL_loadbufferx(L, s, l, chunkname, mode);
|
||||
}
|
||||
else { /* loading from a reader function */
|
||||
const char *chunkname = luaL_optstring(L, 2, "=(load)");
|
||||
loaddata ld;
|
||||
ld.mode = mode;
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
lua_settop(L, RESERVEDSLOT); /* create reserved slot */
|
||||
status = lua_load(L, generic_reader, &ld, chunkname);
|
||||
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 */
|
||||
@@ -390,27 +367,32 @@ static int luaB_select (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int pcallcont (lua_State *L) {
|
||||
int errfunc = 0; /* =0 to avoid warnings */
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
22
lcode.c
22
lcode.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcode.c,v 2.55 2011/05/31 18:24:36 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
|
||||
*/
|
||||
@@ -44,8 +44,8 @@ void luaK_nil (FuncState *fs, int from, int n) {
|
||||
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) */
|
||||
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;
|
||||
@@ -213,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++;
|
||||
@@ -289,7 +289,7 @@ 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;
|
||||
@@ -304,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++]);
|
||||
@@ -316,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 */
|
||||
@@ -350,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);
|
||||
}
|
||||
|
||||
@@ -641,7 +643,7 @@ 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) {
|
||||
|
||||
3
lcode.h
3
lcode.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcode.h,v 1.56 2011/02/01 18:03:10 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
|
||||
*/
|
||||
@@ -62,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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
5
lctype.c
5
lctype.c
@@ -1,9 +1,12 @@
|
||||
/*
|
||||
** $Id: lctype.c,v 1.9 2011/06/23 16:00:43 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
|
||||
*/
|
||||
|
||||
#define lctype_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lctype.h"
|
||||
|
||||
#if !LUA_USE_CTYPE /* { */
|
||||
|
||||
4
lctype.h
4
lctype.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lctype.h,v 1.10 2011/06/24 12:25:33 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
|
||||
*/
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
|
||||
/*
|
||||
** WARNING: the functions defined here do not necessarily correspond
|
||||
** 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
|
||||
*/
|
||||
|
||||
18
ldblib.c
18
ldblib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.129 2011/01/26 16:30:02 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
|
||||
*/
|
||||
@@ -253,14 +253,14 @@ static int db_upvaluejoin (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
#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_pushlightuserdata(L, L);
|
||||
lua_pushthread(L);
|
||||
lua_rawget(L, -2);
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushstring(L, hooknames[(int)ar->event]);
|
||||
@@ -307,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;
|
||||
}
|
||||
@@ -327,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 */
|
||||
}
|
||||
|
||||
215
ldebug.c
215
ldebug.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 2.81 2011/04/28 14:00:11 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,6 +30,9 @@
|
||||
|
||||
|
||||
|
||||
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
|
||||
|
||||
|
||||
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
|
||||
|
||||
|
||||
@@ -94,6 +97,13 @@ 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)
|
||||
@@ -166,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;
|
||||
@@ -184,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) {
|
||||
@@ -215,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;
|
||||
}
|
||||
@@ -230,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;
|
||||
@@ -284,17 +300,16 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
|
||||
static const char *getobjname (Proto *p, int lastpc, int reg,
|
||||
const char **name);
|
||||
|
||||
|
||||
/*
|
||||
** find a "name" for the RK value 'c'
|
||||
*/
|
||||
static void kname (lua_State *L, CallInfo *ci, int c, int oreg,
|
||||
const char *what, const char **name) {
|
||||
static void kname (Proto *p, int pc, int c, const char **name) {
|
||||
if (ISK(c)) { /* is 'c' a constant? */
|
||||
TValue *kvalue = &ci_func(ci)->p->k[INDEXK(c)];
|
||||
TValue *kvalue = &p->k[INDEXK(c)];
|
||||
if (ttisstring(kvalue)) { /* literal constant? */
|
||||
*name = svalue(kvalue); /* it is its own name */
|
||||
return;
|
||||
@@ -302,8 +317,7 @@ static void kname (lua_State *L, CallInfo *ci, int c, int oreg,
|
||||
/* else no reasonable name found */
|
||||
}
|
||||
else { /* 'c' is a register */
|
||||
if (c != oreg) /* not the original register? */
|
||||
what = getobjname(L, ci, c, name); /* search for 'c' */
|
||||
const char *what = getobjname(p, pc, c, name); /* search for 'c' */
|
||||
if (what && *what == 'c') { /* found a constant name? */
|
||||
return; /* 'name' already filled */
|
||||
}
|
||||
@@ -313,85 +327,30 @@ static void kname (lua_State *L, CallInfo *ci, int c, int oreg,
|
||||
}
|
||||
|
||||
|
||||
static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
|
||||
const char **name) {
|
||||
Proto *p = ci_func(ci)->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(L, ci, 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:
|
||||
case OP_LOADKX: {
|
||||
if (reg == a) {
|
||||
int b = (op == OP_LOADK) ? GETARG_Bx(i)
|
||||
: 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);
|
||||
if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
|
||||
what = NULL;
|
||||
break;
|
||||
}
|
||||
case OP_SELF: {
|
||||
if (reg == a) {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
kname(L, ci, k, a, what, name);
|
||||
what = "method";
|
||||
}
|
||||
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: {
|
||||
@@ -403,35 +362,87 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
|
||||
break;
|
||||
}
|
||||
case OP_TEST: {
|
||||
if (reg == a) what = NULL; /* jumped code can change 'a' */
|
||||
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)->p->code[currentpc(ci)];
|
||||
if (GET_OPCODE(i) == OP_EXTRAARG) /* extra argument? */
|
||||
i = ci_func(ci)->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;
|
||||
@@ -473,12 +484,12 @@ static int isinstack (CallInfo *ci, const TValue *o) {
|
||||
|
||||
|
||||
static const char *getupvalname (CallInfo *ci, const TValue *o,
|
||||
const char **name) {
|
||||
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";
|
||||
}
|
||||
}
|
||||
@@ -486,7 +497,7 @@ 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);
|
||||
@@ -494,7 +505,8 @@ void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
|
||||
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);
|
||||
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)",
|
||||
@@ -504,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 */
|
||||
@@ -519,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;
|
||||
}
|
||||
|
||||
|
||||
@@ -546,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);
|
||||
@@ -559,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));
|
||||
|
||||
20
ldebug.h
20
ldebug.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldebug.h,v 2.5 2009/06/10 16:57:53 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
|
||||
*/
|
||||
@@ -21,14 +21,14 @@
|
||||
#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
|
||||
|
||||
91
ldo.c
91
ldo.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 2.97 2011/06/20 16:36:03 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;
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||
}
|
||||
default: { /* not a function */
|
||||
func = tryfuncTM(L, func); /* retry with 'function' tag method */
|
||||
return luaD_precall(L, func, nresults);
|
||||
return luaD_precall(L, func, nresults); /* now it must be a function */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -382,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);
|
||||
}
|
||||
|
||||
@@ -403,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 */
|
||||
@@ -454,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;
|
||||
@@ -473,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);
|
||||
@@ -485,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? */
|
||||
@@ -500,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 */
|
||||
@@ -514,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);
|
||||
@@ -546,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;
|
||||
}
|
||||
@@ -564,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);
|
||||
}
|
||||
@@ -611,32 +610,48 @@ struct SParser { /* data to `f_parser' */
|
||||
ZIO *z;
|
||||
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 = zgetc(p->z); /* read first character */
|
||||
tf = (c == LUA_SIGNATURE[0])
|
||||
? luaU_undump(L, p->z, &p->buff, p->name)
|
||||
: luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
|
||||
setptvalue2s(L, L->top, tf);
|
||||
incr_top(L);
|
||||
cl = luaF_newLclosure(L, tf);
|
||||
setclLvalue(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.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;
|
||||
|
||||
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
|
||||
|
||||
11
ldump.c
11
ldump.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ldump.c,v 1.18 2011/05/06 13:35:17 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
|
||||
*/
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
588
lgc.c
588
lgc.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 2.108 2010/12/29 18:00:23 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 2.132 2012/05/31 20:26:14 roberto Exp $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -24,34 +24,40 @@
|
||||
|
||||
|
||||
|
||||
/* how much to allocate before next GC step */
|
||||
#define GCSTEPSIZE 1024
|
||||
/*
|
||||
** cost of sweeping one element (the size of a small object divided
|
||||
** by some adjust for the sweep speed)
|
||||
*/
|
||||
#define GCSWEEPCOST ((sizeof(TString) + 4) / 4)
|
||||
|
||||
/* maximum number of elements to sweep in each single step */
|
||||
#define GCSWEEPMAX 40
|
||||
|
||||
/* cost of sweeping one element */
|
||||
#define GCSWEEPCOST 1
|
||||
#define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4))
|
||||
|
||||
/* maximum number of finalizers to call in each GC step */
|
||||
#define GCFINALIZENUM 4
|
||||
|
||||
/* cost of marking the root set */
|
||||
#define GCROOTCOST 10
|
||||
|
||||
/* cost of atomic step */
|
||||
#define GCATOMICCOST 1000
|
||||
/*
|
||||
** macro to adjust 'stepmul': 'stepmul' is actually used like
|
||||
** 'stepmul / STEPMULADJ' (value chosen by tests)
|
||||
*/
|
||||
#define STEPMULADJ 200
|
||||
|
||||
/*
|
||||
** macro to adjust 'pause': 'pause' is actually used like
|
||||
** 'pause / PAUSEADJ' (value chosen by tests)
|
||||
*/
|
||||
#define PAUSEADJ 200
|
||||
|
||||
|
||||
/* basic cost to traverse one object (to be added to the links the
|
||||
object may have) */
|
||||
#define TRAVCOST 5
|
||||
|
||||
|
||||
/*
|
||||
** standard negative debt for GC; a reasonable "time" to wait before
|
||||
** starting a new cycle
|
||||
*/
|
||||
#define stddebt(g) (-cast(l_mem, gettotalbytes(g)/100) * g->gcpause)
|
||||
#define stddebtest(g,e) (-cast(l_mem, (e)/PAUSEADJ) * g->gcpause)
|
||||
#define stddebt(g) stddebtest(g, gettotalbytes(g))
|
||||
|
||||
|
||||
/*
|
||||
@@ -65,8 +71,6 @@
|
||||
#define white2gray(x) resetbits(gch(x)->marked, WHITEBITS)
|
||||
#define black2gray(x) resetbit(gch(x)->marked, BLACKBIT)
|
||||
|
||||
#define stringmark(s) ((void)((s) && resetbits((s)->tsv.marked, WHITEBITS)))
|
||||
|
||||
|
||||
#define isfinalized(x) testbit(gch(x)->marked, FINALIZEDBIT)
|
||||
|
||||
@@ -93,6 +97,12 @@ static void reallymarkobject (global_State *g, GCObject *o);
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** one after last element in a hash array
|
||||
*/
|
||||
#define gnodelast(h) gnode(h, cast(size_t, sizenode(h)))
|
||||
|
||||
|
||||
/*
|
||||
** link table 'h' into list pointed by 'p'
|
||||
*/
|
||||
@@ -100,12 +110,13 @@ static void reallymarkobject (global_State *g, GCObject *o);
|
||||
|
||||
|
||||
/*
|
||||
** mark a table entry as dead (therefore removing it from the table)
|
||||
** if key is not marked, mark its entry as dead (therefore removing it
|
||||
** from the table)
|
||||
*/
|
||||
static void removeentry (Node *n) {
|
||||
lua_assert(ttisnil(gval(n)));
|
||||
if (iscollectable(gkey(n)))
|
||||
setdeadvalue(gkey(n)); /* dead key; remove it */
|
||||
if (valiswhite(gkey(n)))
|
||||
setdeadvalue(gkey(n)); /* unused and unmarked key; remove it */
|
||||
}
|
||||
|
||||
|
||||
@@ -116,13 +127,13 @@ static void removeentry (Node *n) {
|
||||
** other objects: if really collected, cannot keep them; for objects
|
||||
** being finalized, keep them in keys, but not in values
|
||||
*/
|
||||
static int iscleared (const TValue *o, int iskey) {
|
||||
static int iscleared (global_State *g, const TValue *o) {
|
||||
if (!iscollectable(o)) return 0;
|
||||
else if (ttisstring(o)) {
|
||||
stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */
|
||||
markobject(g, rawtsvalue(o)); /* strings are `values', so are never weak */
|
||||
return 0;
|
||||
}
|
||||
else return iswhite(gcvalue(o)) || (!iskey && isfinalized(gcvalue(o)));
|
||||
else return iswhite(gcvalue(o));
|
||||
}
|
||||
|
||||
|
||||
@@ -152,7 +163,7 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
|
||||
*/
|
||||
void luaC_barrierback_ (lua_State *L, GCObject *o) {
|
||||
global_State *g = G(L);
|
||||
lua_assert(isblack(o) && !isdead(g, o));
|
||||
lua_assert(isblack(o) && !isdead(g, o) && gch(o)->tt == LUA_TTABLE);
|
||||
black2gray(o); /* make object gray (again) */
|
||||
gco2t(o)->gclist = g->grayagain;
|
||||
g->grayagain = o;
|
||||
@@ -210,7 +221,8 @@ void luaC_checkupvalcolor (global_State *g, UpVal *uv) {
|
||||
GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list,
|
||||
int offset) {
|
||||
global_State *g = G(L);
|
||||
GCObject *o = obj2gco(cast(char *, luaM_newobject(L, tt, sz)) + offset);
|
||||
char *raw = cast(char *, luaM_newobject(L, novariant(tt), sz));
|
||||
GCObject *o = obj2gco(raw + offset);
|
||||
if (list == NULL)
|
||||
list = &g->allgc; /* standard list for collectable objects */
|
||||
gch(o)->marked = luaC_white(g);
|
||||
@@ -232,59 +244,68 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list,
|
||||
|
||||
|
||||
/*
|
||||
** mark an object. Userdata and closed upvalues are visited and turned
|
||||
** black here. Strings remain gray (it is the same as making them
|
||||
** black). Other objects are marked gray and added to appropriate list
|
||||
** to be visited (and turned black) later. (Open upvalues are already
|
||||
** linked in 'headuv' list.)
|
||||
** mark an object. Userdata, strings, and closed upvalues are visited
|
||||
** and turned black here. Other objects are marked gray and added
|
||||
** to appropriate list to be visited (and turned black) later. (Open
|
||||
** upvalues are already linked in 'headuv' list.)
|
||||
*/
|
||||
static void reallymarkobject (global_State *g, GCObject *o) {
|
||||
lua_assert(iswhite(o) && !isdead(g, o));
|
||||
lu_mem size;
|
||||
white2gray(o);
|
||||
switch (gch(o)->tt) {
|
||||
case LUA_TSTRING: {
|
||||
return; /* for strings, gray is as good as black */
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_TLNGSTR: {
|
||||
size = sizestring(gco2ts(o));
|
||||
break; /* nothing else to mark; make it black */
|
||||
}
|
||||
case LUA_TUSERDATA: {
|
||||
Table *mt = gco2u(o)->metatable;
|
||||
markobject(g, mt);
|
||||
markobject(g, gco2u(o)->env);
|
||||
gray2black(o); /* all pointers marked */
|
||||
return;
|
||||
size = sizeudata(gco2u(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TUPVAL: {
|
||||
UpVal *uv = gco2uv(o);
|
||||
markvalue(g, uv->v);
|
||||
if (uv->v == &uv->u.value) /* closed? (open upvalues remain gray) */
|
||||
gray2black(o); /* make it black */
|
||||
if (uv->v != &uv->u.value) /* open? */
|
||||
return; /* open upvalues remain gray */
|
||||
size = sizeof(UpVal);
|
||||
break;
|
||||
}
|
||||
case LUA_TLCL: {
|
||||
gco2lcl(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
return;
|
||||
}
|
||||
case LUA_TFUNCTION: {
|
||||
gco2cl(o)->c.gclist = g->gray;
|
||||
case LUA_TCCL: {
|
||||
gco2ccl(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
break;
|
||||
return;
|
||||
}
|
||||
case LUA_TTABLE: {
|
||||
linktable(gco2t(o), &g->gray);
|
||||
break;
|
||||
return;
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
gco2th(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
break;
|
||||
return;
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
gco2p(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
break;
|
||||
return;
|
||||
}
|
||||
default: lua_assert(0);
|
||||
default: lua_assert(0); return;
|
||||
}
|
||||
gray2black(o);
|
||||
g->GCmemtrav += size;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** mark tag methods for basic types
|
||||
** mark metamethods for basic types
|
||||
*/
|
||||
static void markmt (global_State *g) {
|
||||
int i;
|
||||
@@ -341,7 +362,10 @@ static void markroot (global_State *g) {
|
||||
*/
|
||||
|
||||
static void traverseweakvalue (global_State *g, Table *h) {
|
||||
Node *n, *limit = gnode(h, sizenode(h));
|
||||
Node *n, *limit = gnodelast(h);
|
||||
/* if there is array part, assume it may have white values (do not
|
||||
traverse it just to check) */
|
||||
int hasclears = (h->sizearray > 0);
|
||||
for (n = gnode(h, 0); n < limit; n++) {
|
||||
checkdeadkey(n);
|
||||
if (ttisnil(gval(n))) /* entry is empty? */
|
||||
@@ -349,16 +373,22 @@ static void traverseweakvalue (global_State *g, Table *h) {
|
||||
else {
|
||||
lua_assert(!ttisnil(gkey(n)));
|
||||
markvalue(g, gkey(n)); /* mark key */
|
||||
if (!hasclears && iscleared(g, gval(n))) /* is there a white value? */
|
||||
hasclears = 1; /* table will have to be cleared */
|
||||
}
|
||||
}
|
||||
linktable(h, &g->weak); /* link into appropriate list */
|
||||
if (hasclears)
|
||||
linktable(h, &g->weak); /* has to be cleared later */
|
||||
else /* no white values */
|
||||
linktable(h, &g->grayagain); /* no need to clean */
|
||||
}
|
||||
|
||||
|
||||
static int traverseephemeron (global_State *g, Table *h) {
|
||||
int marked = 0; /* true if an object is marked in this traversal */
|
||||
int hasclears = 0; /* true if table has unmarked pairs */
|
||||
Node *n, *limit = gnode(h, sizenode(h));
|
||||
int hasclears = 0; /* true if table has white keys */
|
||||
int prop = 0; /* true if table has entry "white-key -> white-value" */
|
||||
Node *n, *limit = gnodelast(h);
|
||||
int i;
|
||||
/* traverse array part (numeric keys are 'strong') */
|
||||
for (i = 0; i < h->sizearray; i++) {
|
||||
@@ -372,25 +402,28 @@ static int traverseephemeron (global_State *g, Table *h) {
|
||||
checkdeadkey(n);
|
||||
if (ttisnil(gval(n))) /* entry is empty? */
|
||||
removeentry(n); /* remove it */
|
||||
else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */
|
||||
hasclears = 1; /* table must be cleared */
|
||||
if (valiswhite(gval(n))) /* value not marked yet? */
|
||||
prop = 1; /* must propagate again */
|
||||
}
|
||||
else if (valiswhite(gval(n))) { /* value not marked yet? */
|
||||
if (iscleared(gkey(n), 1)) /* key is not marked (yet)? */
|
||||
hasclears = 1; /* may have to propagate mark from key to value */
|
||||
else { /* key is marked, so mark value */
|
||||
marked = 1; /* value was not marked */
|
||||
reallymarkobject(g, gcvalue(gval(n)));
|
||||
}
|
||||
marked = 1;
|
||||
reallymarkobject(g, gcvalue(gval(n))); /* mark it now */
|
||||
}
|
||||
}
|
||||
if (hasclears) /* does table have unmarked pairs? */
|
||||
linktable(h, &g->ephemeron); /* will have to propagate again */
|
||||
else /* nothing to propagate */
|
||||
linktable(h, &g->weak); /* avoid convergence phase */
|
||||
if (prop)
|
||||
linktable(h, &g->ephemeron); /* have to propagate again */
|
||||
else if (hasclears) /* does table have white keys? */
|
||||
linktable(h, &g->allweak); /* may have to clean white keys */
|
||||
else /* no white keys */
|
||||
linktable(h, &g->grayagain); /* no need to clean */
|
||||
return marked;
|
||||
}
|
||||
|
||||
|
||||
static void traversestrongtable (global_State *g, Table *h) {
|
||||
Node *n, *limit = gnode(h, sizenode(h));
|
||||
Node *n, *limit = gnodelast(h);
|
||||
int i;
|
||||
for (i = 0; i < h->sizearray; i++) /* traverse array part */
|
||||
markvalue(g, &h->array[i]);
|
||||
@@ -407,30 +440,26 @@ static void traversestrongtable (global_State *g, Table *h) {
|
||||
}
|
||||
|
||||
|
||||
static int traversetable (global_State *g, Table *h) {
|
||||
static lu_mem traversetable (global_State *g, Table *h) {
|
||||
const char *weakkey, *weakvalue;
|
||||
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
|
||||
markobject(g, h->metatable);
|
||||
if (mode && ttisstring(mode)) { /* is there a weak mode? */
|
||||
int weakkey = (strchr(svalue(mode), 'k') != NULL);
|
||||
int weakvalue = (strchr(svalue(mode), 'v') != NULL);
|
||||
if (weakkey || weakvalue) { /* is really weak? */
|
||||
black2gray(obj2gco(h)); /* keep table gray */
|
||||
if (!weakkey) { /* strong keys? */
|
||||
traverseweakvalue(g, h);
|
||||
return TRAVCOST + sizenode(h);
|
||||
}
|
||||
else if (!weakvalue) { /* strong values? */
|
||||
traverseephemeron(g, h);
|
||||
return TRAVCOST + h->sizearray + sizenode(h);
|
||||
}
|
||||
else {
|
||||
linktable(h, &g->allweak); /* nothing to traverse now */
|
||||
return TRAVCOST;
|
||||
}
|
||||
} /* else go through */
|
||||
if (mode && ttisstring(mode) && /* is there a weak mode? */
|
||||
((weakkey = strchr(svalue(mode), 'k')),
|
||||
(weakvalue = strchr(svalue(mode), 'v')),
|
||||
(weakkey || weakvalue))) { /* is really weak? */
|
||||
black2gray(obj2gco(h)); /* keep table gray */
|
||||
if (!weakkey) /* strong keys? */
|
||||
traverseweakvalue(g, h);
|
||||
else if (!weakvalue) /* strong values? */
|
||||
traverseephemeron(g, h);
|
||||
else /* all weak */
|
||||
linktable(h, &g->allweak); /* nothing to traverse now */
|
||||
}
|
||||
traversestrongtable(g, h);
|
||||
return TRAVCOST + h->sizearray + (2 * sizenode(h));
|
||||
else /* not weak */
|
||||
traversestrongtable(g, h);
|
||||
return sizeof(Table) + sizeof(TValue) * h->sizearray +
|
||||
sizeof(Node) * sizenode(h);
|
||||
}
|
||||
|
||||
|
||||
@@ -438,86 +467,101 @@ static int traverseproto (global_State *g, Proto *f) {
|
||||
int i;
|
||||
if (f->cache && iswhite(obj2gco(f->cache)))
|
||||
f->cache = NULL; /* allow cache to be collected */
|
||||
stringmark(f->source);
|
||||
markobject(g, f->source);
|
||||
for (i = 0; i < f->sizek; i++) /* mark literals */
|
||||
markvalue(g, &f->k[i]);
|
||||
for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
|
||||
stringmark(f->upvalues[i].name);
|
||||
markobject(g, f->upvalues[i].name);
|
||||
for (i = 0; i < f->sizep; i++) /* mark nested protos */
|
||||
markobject(g, f->p[i]);
|
||||
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
|
||||
stringmark(f->locvars[i].varname);
|
||||
return TRAVCOST + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars;
|
||||
markobject(g, f->locvars[i].varname);
|
||||
return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
|
||||
sizeof(Proto *) * f->sizep +
|
||||
sizeof(TValue) * f->sizek +
|
||||
sizeof(int) * f->sizelineinfo +
|
||||
sizeof(LocVar) * f->sizelocvars +
|
||||
sizeof(Upvaldesc) * f->sizeupvalues;
|
||||
}
|
||||
|
||||
|
||||
static int traverseclosure (global_State *g, Closure *cl) {
|
||||
if (cl->c.isC) {
|
||||
int i;
|
||||
for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
|
||||
markvalue(g, &cl->c.upvalue[i]);
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
|
||||
markobject(g, cl->l.p); /* mark its prototype */
|
||||
for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */
|
||||
markobject(g, cl->l.upvals[i]);
|
||||
}
|
||||
return TRAVCOST + cl->c.nupvalues;
|
||||
static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
|
||||
int i;
|
||||
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
|
||||
markvalue(g, &cl->upvalue[i]);
|
||||
return sizeCclosure(cl->nupvalues);
|
||||
}
|
||||
|
||||
static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
|
||||
int i;
|
||||
markobject(g, cl->p); /* mark its prototype */
|
||||
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
|
||||
markobject(g, cl->upvals[i]);
|
||||
return sizeLclosure(cl->nupvalues);
|
||||
}
|
||||
|
||||
|
||||
static int traversestack (global_State *g, lua_State *L) {
|
||||
StkId o = L->stack;
|
||||
static lu_mem traversestack (global_State *g, lua_State *th) {
|
||||
StkId o = th->stack;
|
||||
if (o == NULL)
|
||||
return 1; /* stack not completely built yet */
|
||||
for (; o < L->top; o++)
|
||||
for (; o < th->top; o++)
|
||||
markvalue(g, o);
|
||||
if (g->gcstate == GCSatomic) { /* final traversal? */
|
||||
StkId lim = L->stack + L->stacksize; /* real end of stack */
|
||||
StkId lim = th->stack + th->stacksize; /* real end of stack */
|
||||
for (; o < lim; o++) /* clear not-marked stack slice */
|
||||
setnilvalue(o);
|
||||
}
|
||||
return TRAVCOST + cast_int(o - L->stack);
|
||||
return sizeof(lua_State) + sizeof(TValue) * th->stacksize;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** traverse one gray object, turning it to black (except for threads,
|
||||
** which are always gray).
|
||||
** Returns number of values traversed.
|
||||
*/
|
||||
static int propagatemark (global_State *g) {
|
||||
static void propagatemark (global_State *g) {
|
||||
lu_mem size;
|
||||
GCObject *o = g->gray;
|
||||
lua_assert(isgray(o));
|
||||
gray2black(o);
|
||||
switch (gch(o)->tt) {
|
||||
case LUA_TTABLE: {
|
||||
Table *h = gco2t(o);
|
||||
g->gray = h->gclist;
|
||||
return traversetable(g, h);
|
||||
g->gray = h->gclist; /* remove from 'gray' list */
|
||||
size = traversetable(g, h);
|
||||
break;
|
||||
}
|
||||
case LUA_TFUNCTION: {
|
||||
Closure *cl = gco2cl(o);
|
||||
g->gray = cl->c.gclist;
|
||||
return traverseclosure(g, cl);
|
||||
case LUA_TLCL: {
|
||||
LClosure *cl = gco2lcl(o);
|
||||
g->gray = cl->gclist; /* remove from 'gray' list */
|
||||
size = traverseLclosure(g, cl);
|
||||
break;
|
||||
}
|
||||
case LUA_TCCL: {
|
||||
CClosure *cl = gco2ccl(o);
|
||||
g->gray = cl->gclist; /* remove from 'gray' list */
|
||||
size = traverseCclosure(g, cl);
|
||||
break;
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
lua_State *th = gco2th(o);
|
||||
g->gray = th->gclist;
|
||||
g->gray = th->gclist; /* remove from 'gray' list */
|
||||
th->gclist = g->grayagain;
|
||||
g->grayagain = o;
|
||||
g->grayagain = o; /* insert into 'grayagain' list */
|
||||
black2gray(o);
|
||||
return traversestack(g, th);
|
||||
size = traversestack(g, th);
|
||||
break;
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
Proto *p = gco2p(o);
|
||||
g->gray = p->gclist;
|
||||
return traverseproto(g, p);
|
||||
g->gray = p->gclist; /* remove from 'gray' list */
|
||||
size = traverseproto(g, p);
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0); return 0;
|
||||
default: lua_assert(0); return;
|
||||
}
|
||||
g->GCmemtrav += size;
|
||||
}
|
||||
|
||||
|
||||
@@ -526,11 +570,26 @@ static void propagateall (global_State *g) {
|
||||
}
|
||||
|
||||
|
||||
static void traverselistofgrays (global_State *g, GCObject **l) {
|
||||
static void propagatelist (global_State *g, GCObject *l) {
|
||||
lua_assert(g->gray == NULL); /* no grays left */
|
||||
g->gray = *l; /* now 'l' is new gray list */
|
||||
*l = NULL;
|
||||
propagateall(g);
|
||||
g->gray = l;
|
||||
propagateall(g); /* traverse all elements from 'l' */
|
||||
}
|
||||
|
||||
/*
|
||||
** retraverse all gray lists. Because tables may be reinserted in other
|
||||
** lists when traversed, traverse the original lists to avoid traversing
|
||||
** twice the same table (which is not wrong, but inefficient)
|
||||
*/
|
||||
static void retraversegrays (global_State *g) {
|
||||
GCObject *weak = g->weak; /* save original lists */
|
||||
GCObject *grayagain = g->grayagain;
|
||||
GCObject *ephemeron = g->ephemeron;
|
||||
g->weak = g->grayagain = g->ephemeron = NULL;
|
||||
propagateall(g); /* traverse main gray list */
|
||||
propagatelist(g, grayagain);
|
||||
propagatelist(g, weak);
|
||||
propagatelist(g, ephemeron);
|
||||
}
|
||||
|
||||
|
||||
@@ -562,21 +621,39 @@ static void convergeephemerons (global_State *g) {
|
||||
|
||||
|
||||
/*
|
||||
** clear collected entries from all weaktables in list 'l'
|
||||
** clear entries with unmarked keys from all weaktables in list 'l' up
|
||||
** to element 'f'
|
||||
*/
|
||||
static void cleartable (GCObject *l) {
|
||||
for (; l != NULL; l = gco2t(l)->gclist) {
|
||||
static void clearkeys (global_State *g, GCObject *l, GCObject *f) {
|
||||
for (; l != f; l = gco2t(l)->gclist) {
|
||||
Table *h = gco2t(l);
|
||||
Node *n, *limit = gnode(h, sizenode(h));
|
||||
Node *n, *limit = gnodelast(h);
|
||||
for (n = gnode(h, 0); n < limit; n++) {
|
||||
if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) {
|
||||
setnilvalue(gval(n)); /* remove value ... */
|
||||
removeentry(n); /* and remove entry from table */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** clear entries with unmarked values from all weaktables in list 'l' up
|
||||
** to element 'f'
|
||||
*/
|
||||
static void clearvalues (global_State *g, GCObject *l, GCObject *f) {
|
||||
for (; l != f; l = gco2t(l)->gclist) {
|
||||
Table *h = gco2t(l);
|
||||
Node *n, *limit = gnodelast(h);
|
||||
int i;
|
||||
for (i = 0; i < h->sizearray; i++) {
|
||||
TValue *o = &h->array[i];
|
||||
if (iscleared(o, 0)) /* value was collected? */
|
||||
if (iscleared(g, o)) /* value was collected? */
|
||||
setnilvalue(o); /* remove value */
|
||||
}
|
||||
for (n = gnode(h, 0); n < limit; n++) {
|
||||
if (!ttisnil(gval(n)) && /* non-empty entry? */
|
||||
(iscleared(gkey(n), 1) || iscleared(gval(n), 0))) {
|
||||
if (!ttisnil(gval(n)) && iscleared(g, gval(n))) {
|
||||
setnilvalue(gval(n)); /* remove value ... */
|
||||
removeentry(n); /* and remove entry from table */
|
||||
}
|
||||
@@ -588,13 +665,22 @@ static void cleartable (GCObject *l) {
|
||||
static void freeobj (lua_State *L, GCObject *o) {
|
||||
switch (gch(o)->tt) {
|
||||
case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
|
||||
case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break;
|
||||
case LUA_TLCL: {
|
||||
luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues));
|
||||
break;
|
||||
}
|
||||
case LUA_TCCL: {
|
||||
luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues));
|
||||
break;
|
||||
}
|
||||
case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
|
||||
case LUA_TTABLE: luaH_free(L, gco2t(o)); break;
|
||||
case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break;
|
||||
case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
|
||||
case LUA_TSTRING: {
|
||||
case LUA_TSHRSTR:
|
||||
G(L)->strt.nuse--;
|
||||
/* go through */
|
||||
case LUA_TLNGSTR: {
|
||||
luaM_freemem(L, o, sizestring(gco2ts(o)));
|
||||
break;
|
||||
}
|
||||
@@ -637,7 +723,6 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
||||
int ow = otherwhite(g);
|
||||
int toclear, toset; /* bits to clear and to set in all live objects */
|
||||
int tostop; /* stop sweep when this is true */
|
||||
l_mem debt = g->GCdebt; /* current debt */
|
||||
if (isgenerational(g)) { /* generational mode? */
|
||||
toclear = ~0; /* clear nothing */
|
||||
toset = bitmask(OLDBIT); /* set the old bit of all surviving objects */
|
||||
@@ -656,19 +741,30 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
||||
freeobj(L, curr); /* erase 'curr' */
|
||||
}
|
||||
else {
|
||||
if (testbits(marked, tostop))
|
||||
return NULL; /* stop sweeping this list */
|
||||
if (gch(curr)->tt == LUA_TTHREAD)
|
||||
sweepthread(L, gco2th(curr)); /* sweep thread's upvalues */
|
||||
if (testbits(marked, tostop)) {
|
||||
static GCObject *nullp = NULL;
|
||||
p = &nullp; /* stop sweeping this list */
|
||||
break;
|
||||
}
|
||||
/* update marks */
|
||||
gch(curr)->marked = cast_byte((marked & toclear) | toset);
|
||||
p = &gch(curr)->next; /* go to next element */
|
||||
}
|
||||
}
|
||||
luaE_setdebt(g, debt); /* sweeping should not change debt */
|
||||
return (*p == NULL) ? NULL : p;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** sweep a list until a live object (or end of list)
|
||||
*/
|
||||
static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) {
|
||||
GCObject ** old = p;
|
||||
int i = 0;
|
||||
do {
|
||||
i++;
|
||||
p = sweeplist(L, p, 1);
|
||||
} while (p == old);
|
||||
if (n) *n += i;
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -722,7 +818,7 @@ static void GCTM (lua_State *L, int propagateerrors) {
|
||||
int status;
|
||||
lu_byte oldah = L->allowhook;
|
||||
int running = g->gcrunning;
|
||||
L->allowhook = 0; /* stop debug hooks during GC tag method */
|
||||
L->allowhook = 0; /* stop debug hooks during GC metamethod */
|
||||
g->gcrunning = 0; /* avoid GC steps */
|
||||
setobj2s(L, L->top, tm); /* push finalizer... */
|
||||
setobj2s(L, L->top + 1, &v); /* ... and its argument */
|
||||
@@ -731,22 +827,24 @@ static void GCTM (lua_State *L, int propagateerrors) {
|
||||
L->allowhook = oldah; /* restore hooks */
|
||||
g->gcrunning = running; /* restore state */
|
||||
if (status != LUA_OK && propagateerrors) { /* error while running __gc? */
|
||||
if (status == LUA_ERRRUN) { /* is there an error msg.? */
|
||||
luaO_pushfstring(L, "error in __gc tag method (%s)",
|
||||
lua_tostring(L, -1));
|
||||
if (status == LUA_ERRRUN) { /* is there an error object? */
|
||||
const char *msg = (ttisstring(L->top - 1))
|
||||
? svalue(L->top - 1)
|
||||
: "no message";
|
||||
luaO_pushfstring(L, "error in __gc metamethod (%s)", msg);
|
||||
status = LUA_ERRGCMM; /* error in __gc metamethod */
|
||||
}
|
||||
luaD_throw(L, status); /* re-send error */
|
||||
luaD_throw(L, status); /* re-throw error */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** move all unreachable objects that need finalization from list 'finobj'
|
||||
** to list 'tobefnz'
|
||||
** move all unreachable objects (or 'all' objects) that need
|
||||
** finalization from list 'finobj' to list 'tobefnz' (to be finalized)
|
||||
*/
|
||||
void luaC_separateudata (lua_State *L, int all) {
|
||||
static void separatetobefnz (lua_State *L, int all) {
|
||||
global_State *g = G(L);
|
||||
GCObject **p = &g->finobj;
|
||||
GCObject *curr;
|
||||
@@ -782,12 +880,21 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
||||
return; /* nothing to be done */
|
||||
else { /* move 'o' to 'finobj' list */
|
||||
GCObject **p;
|
||||
for (p = &g->allgc; *p != o; p = &gch(*p)->next) ;
|
||||
*p = gch(o)->next; /* remove 'o' from root list */
|
||||
gch(o)->next = g->finobj; /* link it in list 'finobj' */
|
||||
GCheader *ho = gch(o);
|
||||
if (g->sweepgc == &ho->next) { /* avoid removing current sweep object */
|
||||
lua_assert(issweepphase(g));
|
||||
g->sweepgc = sweeptolive(L, g->sweepgc, NULL);
|
||||
}
|
||||
/* search for pointer pointing to 'o' */
|
||||
for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ }
|
||||
*p = ho->next; /* remove 'o' from root list */
|
||||
ho->next = g->finobj; /* link it in list 'finobj' */
|
||||
g->finobj = o;
|
||||
l_setbit(gch(o)->marked, SEPARATED); /* mark it as such */
|
||||
resetoldbit(o); /* see MOVE OLD rule */
|
||||
l_setbit(ho->marked, SEPARATED); /* mark it as such */
|
||||
if (!keepinvariant(g)) /* not keeping invariant? */
|
||||
makewhite(g, o); /* "sweep" object */
|
||||
else
|
||||
resetoldbit(o); /* see MOVE OLD rule */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -804,6 +911,28 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
||||
#define sweepphases \
|
||||
(bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep))
|
||||
|
||||
|
||||
/*
|
||||
** enter first sweep phase (strings) and prepare pointers for other
|
||||
** sweep phases. The calls to 'sweeptolive' make pointers point to an
|
||||
** object inside the list (instead of to the header), so that the real
|
||||
** sweep do not need to skip objects created between "now" and the start
|
||||
** of the real sweep.
|
||||
** Returns how many objects it sweeped.
|
||||
*/
|
||||
static int entersweep (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
int n = 0;
|
||||
g->gcstate = GCSsweepstring;
|
||||
lua_assert(g->sweepgc == NULL && g->sweepfin == NULL);
|
||||
/* prepare to sweep strings, finalizable objects, and regular objects */
|
||||
g->sweepstrgc = 0;
|
||||
g->sweepfin = sweeptolive(L, &g->finobj, &n);
|
||||
g->sweepgc = sweeptolive(L, &g->allgc, &n);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** change GC mode
|
||||
*/
|
||||
@@ -813,15 +942,14 @@ void luaC_changemode (lua_State *L, int mode) {
|
||||
if (mode == KGC_GEN) { /* change to generational mode */
|
||||
/* make sure gray lists are consistent */
|
||||
luaC_runtilstate(L, bitmask(GCSpropagate));
|
||||
g->lastmajormem = gettotalbytes(g);
|
||||
g->GCestimate = gettotalbytes(g);
|
||||
g->gckind = KGC_GEN;
|
||||
}
|
||||
else { /* change to incremental mode */
|
||||
/* sweep all objects to turn them back to white
|
||||
(as white has not changed, nothing extra will be collected) */
|
||||
g->sweepstrgc = 0;
|
||||
g->gcstate = GCSsweepstring;
|
||||
g->gckind = KGC_NORMAL;
|
||||
entersweep(L);
|
||||
luaC_runtilstate(L, ~sweepphases);
|
||||
}
|
||||
}
|
||||
@@ -842,22 +970,23 @@ static void callallpendingfinalizers (lua_State *L, int propagateerrors) {
|
||||
void luaC_freeallobjects (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
int i;
|
||||
callallpendingfinalizers(L, 0);
|
||||
/* following "white" makes all objects look dead */
|
||||
g->currentwhite = WHITEBITS;
|
||||
g->gckind = KGC_NORMAL;
|
||||
sweepwholelist(L, &g->finobj);
|
||||
separatetobefnz(L, 1); /* separate all objects with finalizers */
|
||||
lua_assert(g->finobj == NULL);
|
||||
callallpendingfinalizers(L, 0);
|
||||
g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */
|
||||
g->gckind = KGC_NORMAL;
|
||||
sweepwholelist(L, &g->finobj); /* finalizers can create objs. in 'finobj' */
|
||||
sweepwholelist(L, &g->allgc);
|
||||
lua_assert(g->allgc == NULL);
|
||||
for (i = 0; i < g->strt.size; i++) /* free all string lists */
|
||||
sweepwholelist(L, &g->strt.hash[i]);
|
||||
lua_assert(g->strt.nuse == 0);
|
||||
}
|
||||
|
||||
|
||||
static void atomic (lua_State *L) {
|
||||
static l_mem atomic (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
l_mem work = -g->GCmemtrav; /* start counting work */
|
||||
GCObject *origweak, *origall;
|
||||
lua_assert(!iswhite(obj2gco(g->mainthread)));
|
||||
markobject(g, L); /* mark running thread */
|
||||
/* registry and global metatables may be changed by API */
|
||||
@@ -865,73 +994,87 @@ static void atomic (lua_State *L) {
|
||||
markmt(g); /* mark basic metatables */
|
||||
/* remark occasional upvalues of (maybe) dead threads */
|
||||
remarkupvals(g);
|
||||
propagateall(g); /* propagate changes */
|
||||
work += g->GCmemtrav; /* stop counting (do not (re)count grays) */
|
||||
/* traverse objects caught by write barrier and by 'remarkupvals' */
|
||||
propagateall(g);
|
||||
traverselistofgrays(g, &g->weak); /* remark weak tables */
|
||||
traverselistofgrays(g, &g->ephemeron); /* remark ephemeron tables */
|
||||
traverselistofgrays(g, &g->grayagain); /* remark gray again */
|
||||
retraversegrays(g);
|
||||
work -= g->GCmemtrav; /* restart counting */
|
||||
convergeephemerons(g);
|
||||
/* at this point, all strongly accessible objects are marked. */
|
||||
luaC_separateudata(L, 0); /* separate userdata to be finalized */
|
||||
markbeingfnz(g); /* mark userdata that will be finalized */
|
||||
/* clear values from weak tables, before checking finalizers */
|
||||
clearvalues(g, g->weak, NULL);
|
||||
clearvalues(g, g->allweak, NULL);
|
||||
origweak = g->weak; origall = g->allweak;
|
||||
work += g->GCmemtrav; /* stop counting (objects being finalized) */
|
||||
separatetobefnz(L, 0); /* separate objects to be finalized */
|
||||
markbeingfnz(g); /* mark objects that will be finalized */
|
||||
propagateall(g); /* remark, to propagate `preserveness' */
|
||||
work -= g->GCmemtrav; /* restart counting */
|
||||
convergeephemerons(g);
|
||||
/* remove collected objects from weak tables */
|
||||
cleartable(g->weak);
|
||||
cleartable(g->ephemeron);
|
||||
cleartable(g->allweak);
|
||||
g->sweepstrgc = 0; /* prepare to sweep strings */
|
||||
g->gcstate = GCSsweepstring;
|
||||
/* at this point, all resurrected objects are marked. */
|
||||
/* remove dead objects from weak tables */
|
||||
clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */
|
||||
clearkeys(g, g->allweak, NULL); /* clear keys from all allweak tables */
|
||||
/* clear values from resurrected weak tables */
|
||||
clearvalues(g, g->weak, origweak);
|
||||
clearvalues(g, g->allweak, origall);
|
||||
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
|
||||
/*lua_checkmemory(L);*/
|
||||
work += g->GCmemtrav; /* complete counting */
|
||||
return work; /* estimate of memory marked by 'atomic' */
|
||||
}
|
||||
|
||||
|
||||
static l_mem singlestep (lua_State *L) {
|
||||
static lu_mem singlestep (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
switch (g->gcstate) {
|
||||
case GCSpause: {
|
||||
g->GCmemtrav = 0; /* start to count memory traversed */
|
||||
if (!isgenerational(g))
|
||||
markroot(g); /* start a new collection */
|
||||
/* in any case, root must be marked */
|
||||
/* in any case, root must be marked at this point */
|
||||
lua_assert(!iswhite(obj2gco(g->mainthread))
|
||||
&& !iswhite(gcvalue(&g->l_registry)));
|
||||
g->gcstate = GCSpropagate;
|
||||
return GCROOTCOST;
|
||||
return g->GCmemtrav;
|
||||
}
|
||||
case GCSpropagate: {
|
||||
if (g->gray)
|
||||
return propagatemark(g);
|
||||
if (g->gray) {
|
||||
lu_mem oldtrav = g->GCmemtrav;
|
||||
propagatemark(g);
|
||||
return g->GCmemtrav - oldtrav; /* memory traversed in this step */
|
||||
}
|
||||
else { /* no more `gray' objects */
|
||||
lu_mem work;
|
||||
int sw;
|
||||
g->gcstate = GCSatomic; /* finish mark phase */
|
||||
atomic(L);
|
||||
return GCATOMICCOST;
|
||||
g->GCestimate = g->GCmemtrav; /* save what was counted */;
|
||||
work = atomic(L); /* add what was traversed by 'atomic' */
|
||||
g->GCestimate += work; /* estimate of total memory traversed */
|
||||
sw = entersweep(L);
|
||||
return work + sw * GCSWEEPCOST;
|
||||
}
|
||||
}
|
||||
case GCSsweepstring: {
|
||||
if (g->sweepstrgc < g->strt.size) {
|
||||
sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]);
|
||||
return GCSWEEPCOST;
|
||||
}
|
||||
else { /* no more strings to sweep */
|
||||
g->sweepgc = &g->finobj; /* prepare to sweep finalizable objects */
|
||||
int i;
|
||||
for (i = 0; i < GCSWEEPMAX && g->sweepstrgc + i < g->strt.size; i++)
|
||||
sweepwholelist(L, &g->strt.hash[g->sweepstrgc + i]);
|
||||
g->sweepstrgc += i;
|
||||
if (g->sweepstrgc >= g->strt.size) /* no more strings to sweep? */
|
||||
g->gcstate = GCSsweepudata;
|
||||
return 0;
|
||||
}
|
||||
return i * GCSWEEPCOST;
|
||||
}
|
||||
case GCSsweepudata: {
|
||||
if (*g->sweepgc) {
|
||||
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
|
||||
if (g->sweepfin) {
|
||||
g->sweepfin = sweeplist(L, g->sweepfin, GCSWEEPMAX);
|
||||
return GCSWEEPMAX*GCSWEEPCOST;
|
||||
}
|
||||
else {
|
||||
g->sweepgc = &g->allgc; /* go to next phase */
|
||||
g->gcstate = GCSsweep;
|
||||
return GCSWEEPCOST;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
case GCSsweep: {
|
||||
if (*g->sweepgc) {
|
||||
if (g->sweepgc) {
|
||||
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
|
||||
return GCSWEEPMAX*GCSWEEPCOST;
|
||||
}
|
||||
@@ -962,43 +1105,52 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
|
||||
|
||||
static void generationalcollection (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
if (g->lastmajormem == 0) { /* signal for another major collection? */
|
||||
if (g->GCestimate == 0) { /* signal for another major collection? */
|
||||
luaC_fullgc(L, 0); /* perform a full regular collection */
|
||||
g->lastmajormem = gettotalbytes(g); /* update control */
|
||||
g->GCestimate = gettotalbytes(g); /* update control */
|
||||
}
|
||||
else {
|
||||
lu_mem estimate = g->GCestimate;
|
||||
luaC_runtilstate(L, ~bitmask(GCSpause)); /* run complete cycle */
|
||||
luaC_runtilstate(L, bitmask(GCSpause));
|
||||
if (gettotalbytes(g) > g->lastmajormem/100 * g->gcmajorinc)
|
||||
g->lastmajormem = 0; /* signal for a major collection */
|
||||
if (gettotalbytes(g) > (estimate / 100) * g->gcmajorinc)
|
||||
g->GCestimate = 0; /* signal for a major collection */
|
||||
}
|
||||
luaE_setdebt(g, stddebt(g));
|
||||
}
|
||||
|
||||
|
||||
static void step (lua_State *L) {
|
||||
static void incstep (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
l_mem lim = g->gcstepmul; /* how much to work */
|
||||
l_mem debt = g->GCdebt;
|
||||
int stepmul = g->gcstepmul;
|
||||
if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values */
|
||||
/* convert debt from Kb to 'work units' (avoid zero debt and overflows) */
|
||||
debt = (debt / STEPMULADJ) + 1;
|
||||
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
|
||||
do { /* always perform at least one single step */
|
||||
lim -= singlestep(L);
|
||||
} while (lim > 0 && g->gcstate != GCSpause);
|
||||
if (g->gcstate != GCSpause)
|
||||
luaE_setdebt(g, g->GCdebt - GCSTEPSIZE);
|
||||
lu_mem work = singlestep(L); /* do some work */
|
||||
debt -= work;
|
||||
} while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);
|
||||
if (g->gcstate == GCSpause)
|
||||
debt = stddebtest(g, g->GCestimate); /* pause until next cycle */
|
||||
else
|
||||
luaE_setdebt(g, stddebt(g));
|
||||
debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */
|
||||
luaE_setdebt(g, debt);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** performs a basic GC step even if the collector is stopped
|
||||
** performs a basic GC step
|
||||
*/
|
||||
void luaC_forcestep (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
int i;
|
||||
if (isgenerational(g)) generationalcollection(L);
|
||||
else step(L);
|
||||
for (i = 0; i < GCFINALIZENUM && g->tobefnz; i++)
|
||||
GCTM(L, 1); /* Call a few pending finalizers */
|
||||
else incstep(L);
|
||||
/* run a few finalizers (or all of them at the end of a collect cycle) */
|
||||
for (i = 0; g->tobefnz && (i < GCFINALIZENUM || g->gcstate == GCSpause); i++)
|
||||
GCTM(L, 1); /* call one finalizer */
|
||||
}
|
||||
|
||||
|
||||
@@ -1006,10 +1158,13 @@ void luaC_forcestep (lua_State *L) {
|
||||
** performs a basic GC step only if collector is running
|
||||
*/
|
||||
void luaC_step (lua_State *L) {
|
||||
if (G(L)->gcrunning) luaC_forcestep(L);
|
||||
global_State *g = G(L);
|
||||
if (g->gcrunning) luaC_forcestep(L);
|
||||
else luaE_setdebt(g, -GCSTEPSIZE); /* avoid being called too often */
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** performs a full GC cycle; if "isemergency", does not call
|
||||
** finalizers (which could change stack positions)
|
||||
@@ -1017,16 +1172,19 @@ void luaC_step (lua_State *L) {
|
||||
void luaC_fullgc (lua_State *L, int isemergency) {
|
||||
global_State *g = G(L);
|
||||
int origkind = g->gckind;
|
||||
int someblack = keepinvariant(g);
|
||||
lua_assert(origkind != KGC_EMERGENCY);
|
||||
if (!isemergency) /* do not run finalizers during emergency GC */
|
||||
if (isemergency) /* do not run finalizers during emergency GC */
|
||||
g->gckind = KGC_EMERGENCY;
|
||||
else {
|
||||
g->gckind = KGC_NORMAL;
|
||||
callallpendingfinalizers(L, 1);
|
||||
if (keepinvariant(g)) { /* marking phase? */
|
||||
}
|
||||
if (someblack) { /* may there be some black objects? */
|
||||
/* must sweep all objects to turn them back to white
|
||||
(as white has not changed, nothing will be collected) */
|
||||
g->sweepstrgc = 0;
|
||||
g->gcstate = GCSsweepstring;
|
||||
entersweep(L);
|
||||
}
|
||||
g->gckind = isemergency ? KGC_EMERGENCY : KGC_NORMAL;
|
||||
/* finish any pending sweep phase to start a new cycle */
|
||||
luaC_runtilstate(L, bitmask(GCSpause));
|
||||
/* run entire collector */
|
||||
|
||||
14
lgc.h
14
lgc.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lgc.h,v 2.49 2010/12/29 18:00:23 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
|
||||
*/
|
||||
@@ -63,9 +71,6 @@
|
||||
#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: */
|
||||
@@ -125,7 +130,6 @@
|
||||
#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);
|
||||
|
||||
106
liolib.c
106
liolib.c
@@ -1,10 +1,20 @@
|
||||
/*
|
||||
** $Id: liolib.c,v 2.100 2011/06/21 13:43:48 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>
|
||||
@@ -21,9 +31,12 @@
|
||||
|
||||
|
||||
/*
|
||||
** lua_popen spawns a new process connected to the current one through
|
||||
** the file streams.
|
||||
** {======================================================
|
||||
** lua_popen spawns a new process connected to the current
|
||||
** one through the file streams.
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#if !defined(lua_popen) /* { */
|
||||
|
||||
#if defined(LUA_USE_POPEN) /* { */
|
||||
@@ -48,22 +61,50 @@
|
||||
|
||||
#endif /* } */
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** lua_fseek/lua_ftell: configuration for longer offsets
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#if !defined(lua_fseek) /* { */
|
||||
|
||||
#if defined(LUA_USE_POSIX)
|
||||
|
||||
#define l_fseek(f,o,w) fseeko(f,o,w)
|
||||
#define l_ftell(f) ftello(f)
|
||||
#define l_seeknum off_t
|
||||
|
||||
#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 l_fseek(f,o,w) fseek(f,o,w)
|
||||
#define l_ftell(f) ftell(f)
|
||||
#define l_seeknum long
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* } */
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
#define IO_PREFIX "_IO_"
|
||||
#define IO_INPUT (IO_PREFIX "input")
|
||||
#define IO_OUTPUT (IO_PREFIX "output")
|
||||
|
||||
|
||||
typedef struct LStream {
|
||||
FILE *f; /* stream */
|
||||
lua_CFunction closef; /* to close stream (NULL for closed streams) */
|
||||
} LStream;
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
typedef luaL_Stream LStream;
|
||||
|
||||
|
||||
#define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
|
||||
@@ -159,6 +200,14 @@ static LStream *newfile (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
@@ -169,7 +218,7 @@ static int io_open (lua_State *L) {
|
||||
(mode[i] != '+' || ++i) && /* skip if char is '+' */
|
||||
(mode[i] != 'b' || ++i) && /* skip if char is 'b' */
|
||||
(mode[i] == '\0')))
|
||||
return luaL_error(L, "invalid mode " LUA_QL("%s")
|
||||
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;
|
||||
@@ -215,12 +264,8 @@ static FILE *getiofile (lua_State *L, const char *findex) {
|
||||
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) {
|
||||
LStream *p = newfile(L);
|
||||
p->f = fopen(filename, mode);
|
||||
if (p->f == 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);
|
||||
@@ -277,10 +322,7 @@ static int io_lines (lua_State *L) {
|
||||
}
|
||||
else { /* open a new file */
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
LStream *p = newfile(L);
|
||||
p->f = fopen(filename, "r");
|
||||
if (p->f == NULL)
|
||||
fileerror(L, 1, filename);
|
||||
opencheck(L, filename, "r");
|
||||
lua_replace(L, 1); /* put file at index 1 */
|
||||
toclose = 1; /* close it after iteration */
|
||||
}
|
||||
@@ -444,9 +486,10 @@ static int io_readline (lua_State *L) {
|
||||
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));
|
||||
@@ -496,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 luaL_fileresult(L, 0, NULL); /* error */
|
||||
else {
|
||||
lua_pushinteger(L, ftell(f));
|
||||
lua_pushnumber(L, (lua_Number)l_ftell(f));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
118
llex.c
118
llex.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.c,v 2.52 2011/07/08 19:17:30 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
|
||||
*/
|
||||
@@ -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,7 +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 */
|
||||
ts->tsv.reserved = cast_byte(i+1); /* reserved word */
|
||||
ts->tsv.extra = cast_byte(i+1); /* reserved word */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,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);
|
||||
@@ -111,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);
|
||||
}
|
||||
|
||||
@@ -126,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);
|
||||
}
|
||||
@@ -220,13 +222,24 @@ 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 (!buff2d(ls->buff, &seminfo->r)) /* format error? */
|
||||
@@ -297,39 +310,29 @@ static void escerror (LexState *ls, int *c, int n, const char *msg) {
|
||||
|
||||
|
||||
static int readhexaesc (LexState *ls) {
|
||||
int c[3]; /* keep input for error message */
|
||||
int i = 2; /* at least 'x?' will go to error message */
|
||||
c[0] = 'x';
|
||||
c[1] = next(ls); /* first hexa digit */
|
||||
if (lisxdigit(c[1])) {
|
||||
c[i++] = next(ls); /* second hexa digit */
|
||||
if (lisxdigit(c[2]))
|
||||
return (luaO_hexavalue(c[1]) << 4) + luaO_hexavalue(c[2]);
|
||||
/* else go through to error */
|
||||
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]);
|
||||
}
|
||||
escerror(ls, c, i, "hexadecimal digit expected");
|
||||
return 0; /* to avoid warnings */
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
static int readdecesc (LexState *ls) {
|
||||
int c[3], r;
|
||||
int i = 2; /* at least two chars will be read */
|
||||
c[0] = ls->current; /* first char must be a digit */
|
||||
c[1] = next(ls); /* read second char */
|
||||
r = c[0] - '0'; /* partial result */
|
||||
if (lisdigit(c[1])) {
|
||||
c[i++] = next(ls); /* read third char */
|
||||
r = 10*r + c[1] - '0'; /* update result */
|
||||
if (lisdigit(c[2])) {
|
||||
r = 10*r + c[2] - '0'; /* update result */
|
||||
if (r > UCHAR_MAX)
|
||||
escerror(ls, c, i, "decimal escape too large");
|
||||
return r;
|
||||
}
|
||||
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 */
|
||||
if (r > UCHAR_MAX)
|
||||
escerror(ls, c, i, "decimal escape too large");
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -349,37 +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 '\\': case '\"': case '\'': c = ls->current; break;
|
||||
case EOZ: continue; /* will raise an error next loop */
|
||||
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))
|
||||
escerror(ls, &ls->current, 1, "invalid escape sequence");
|
||||
/* digital escape \ddd */
|
||||
c = readdecesc(ls);
|
||||
break;
|
||||
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);
|
||||
@@ -487,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;
|
||||
}
|
||||
|
||||
4
llex.h
4
llex.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.70 2011/05/03 15:51:16 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
|
||||
*/
|
||||
@@ -71,7 +71,7 @@ LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
|
||||
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);
|
||||
|
||||
|
||||
|
||||
39
llimits.h
39
llimits.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.89 2011/05/05 19:43:14 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) */
|
||||
@@ -62,9 +63,9 @@ typedef LUAI_UACNUMBER l_uacNumber;
|
||||
/* 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) /* empty */
|
||||
#define lua_longassert(c) ((void)0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
@@ -97,6 +98,19 @@ typedef LUAI_UACNUMBER l_uacNumber;
|
||||
#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
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** maximum depth for nested C calls and syntactical nested non-terminals
|
||||
** in a program. (Value must fit in an unsigned short int.)
|
||||
@@ -197,31 +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] + u.l_p[1]; } /* add double bits for his hash */
|
||||
(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 /* } */
|
||||
|
||||
|
||||
@@ -257,7 +276,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
|
||||
|
||||
|
||||
|
||||
#if (defined(ltable_c) || defined(luaall_c)) && !defined(luai_hashnum)
|
||||
#if defined(ltable_c) && !defined(luai_hashnum)
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
12
lmathlib.c
12
lmathlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.79 2010/11/18 18:38:27 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)));
|
||||
|
||||
21
lmem.c
21
lmem.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.80 2010/12/20 18:17:46 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 */
|
||||
}
|
||||
|
||||
|
||||
@@ -95,22 +94,6 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
||||
}
|
||||
lua_assert((nsize == 0) == (newblock == NULL));
|
||||
g->GCdebt = (g->GCdebt + nsize) - realosize;
|
||||
#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, g->GCdebt, g->gcstate * 1000);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
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,
|
||||
|
||||
220
loadlib.c
220
loadlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: loadlib.c,v 1.99 2011/06/28 17:13:28 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
|
||||
|
||||
/*
|
||||
@@ -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 */
|
||||
@@ -322,10 +348,12 @@ static const char *pushnexttemplate (lua_State *L, const char *path) {
|
||||
|
||||
static const char *searchpath (lua_State *L, const char *name,
|
||||
const char *path,
|
||||
const char *sep) {
|
||||
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, LUA_DIRSEP); /* replace it by proper one */
|
||||
lua_pushliteral(L, ""); /* error accumulator */
|
||||
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);
|
||||
@@ -334,8 +362,9 @@ 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 */
|
||||
}
|
||||
|
||||
@@ -343,7 +372,8 @@ static const char *searchpath (lua_State *L, const char *name,
|
||||
static int ll_searchpath (lua_State *L) {
|
||||
const char *f = searchpath(L, luaL_checkstring(L, 1),
|
||||
luaL_checkstring(L, 2),
|
||||
luaL_optstring(L, 3, "."));
|
||||
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);
|
||||
@@ -354,13 +384,14 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -379,7 +410,7 @@ static int checkload (lua_State *L, int stat, const char *filename) {
|
||||
static int searcher_Lua (lua_State *L) {
|
||||
const char *filename;
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
filename = findfile(L, name, "path");
|
||||
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);
|
||||
}
|
||||
@@ -393,19 +424,19 @@ 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 searcher_C (lua_State *L) {
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
const char *filename = findfile(L, name, "cpath");
|
||||
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);
|
||||
}
|
||||
@@ -418,7 +449,7 @@ static int searcher_Croot (lua_State *L) {
|
||||
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)
|
||||
@@ -444,35 +475,46 @@ static int searcher_preload (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int ll_require (lua_State *L) {
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
static void findloader (lua_State *L, const char *name) {
|
||||
int i;
|
||||
lua_settop(L, 1); /* _LOADED table will be at index 2 */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
lua_getfield(L, 2, name);
|
||||
if (lua_toboolean(L, -1)) /* is it there? */
|
||||
return 1; /* package is already loaded */
|
||||
/* else must load it; iterate over available seachers to find a loader */
|
||||
lua_getfield(L, lua_upvalueindex(1), "searchers");
|
||||
if (!lua_istable(L, -1))
|
||||
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");
|
||||
lua_pushliteral(L, ""); /* error message accumulator */
|
||||
for (i=1; ; i++) {
|
||||
lua_rawgeti(L, -2, i); /* get a seacher */
|
||||
if (lua_isnil(L, -1)) /* no more searchers? */
|
||||
/* 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, -2));
|
||||
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? */
|
||||
break; /* module loader found */
|
||||
return; /* module loader found */
|
||||
else if (lua_isstring(L, -2)) { /* searcher returned error message? */
|
||||
lua_pop(L, 1); /* remove extra return */
|
||||
lua_concat(L, 2); /* accumulate error message */
|
||||
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);
|
||||
lua_settop(L, 1); /* _LOADED table will be at index 2 */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
lua_getfield(L, 2, name); /* _LOADED[name] */
|
||||
if (lua_toboolean(L, -1)) /* is it there? */
|
||||
return 1; /* package is already loaded */
|
||||
/* 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 */
|
||||
@@ -516,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -577,12 +621,25 @@ static int ll_seeall (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 */
|
||||
@@ -615,18 +672,10 @@ static const luaL_Reg ll_funcs[] = {
|
||||
};
|
||||
|
||||
|
||||
static const lua_CFunction searchers[] =
|
||||
{searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_package (lua_State *L) {
|
||||
static void createsearcherstable (lua_State *L) {
|
||||
static const lua_CFunction searchers[] =
|
||||
{searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
|
||||
int i;
|
||||
/* create new type _LOADLIB */
|
||||
luaL_newmetatable(L, "_LOADLIB");
|
||||
lua_pushcfunction(L, gctm);
|
||||
lua_setfield(L, -2, "__gc");
|
||||
/* create `package' table */
|
||||
luaL_newlib(L, pk_funcs);
|
||||
/* create 'searchers' table */
|
||||
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
|
||||
/* fill it with pre-defined searchers */
|
||||
@@ -635,6 +684,19 @@ LUAMOD_API int luaopen_package (lua_State *L) {
|
||||
lua_pushcclosure(L, searchers[i], 1);
|
||||
lua_rawseti(L, -2, i+1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_package (lua_State *L) {
|
||||
/* 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"); /* set finalizer for CLIBS table */
|
||||
lua_setmetatable(L, -2);
|
||||
/* create `package' table */
|
||||
luaL_newlib(L, pk_funcs);
|
||||
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' */
|
||||
|
||||
13
lobject.c
13
lobject.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 2.51 2011/06/23 16:01:06 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) {
|
||||
@@ -103,8 +103,8 @@ static int isneg (const char **s) {
|
||||
|
||||
|
||||
static lua_Number readhexa (const char **s, lua_Number r, int *count) {
|
||||
while (lisxdigit(cast_uchar(**s))) { /* read integer part */
|
||||
r = (r * 16.0) + cast_num(luaO_hexavalue(cast_uchar(*(*s)++)));
|
||||
for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
|
||||
r = (r * 16.0) + cast_num(luaO_hexavalue(cast_uchar(**s)));
|
||||
(*count)++;
|
||||
}
|
||||
return r;
|
||||
@@ -157,7 +157,9 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
||||
|
||||
int luaO_str2d (const char *s, size_t len, lua_Number *result) {
|
||||
char *endptr;
|
||||
if (strpbrk(s, "xX")) /* hexa? */
|
||||
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);
|
||||
@@ -219,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;
|
||||
|
||||
127
lobject.h
127
lobject.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 2.60 2011/06/13 14:13:06 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
|
||||
*/
|
||||
@@ -36,6 +36,9 @@
|
||||
** bit 6: whether value is collectable
|
||||
*/
|
||||
|
||||
#define VARBITS (3 << 4)
|
||||
|
||||
|
||||
/*
|
||||
** LUA_TFUNCTION variants:
|
||||
** 0 - Lua function
|
||||
@@ -49,6 +52,12 @@
|
||||
#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)
|
||||
|
||||
@@ -109,30 +118,35 @@ typedef struct lua_TValue TValue;
|
||||
/* 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) (rttype(o) & 0x0F)
|
||||
#define ttypenv(o) (novariant(rttype(o)))
|
||||
|
||||
|
||||
/* Macros to test type */
|
||||
#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) checktag((o), ctb(LUA_TSTRING))
|
||||
#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) (ttypenv(o) == LUA_TFUNCTION)
|
||||
#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), ctb(LUA_TDEADKEY))
|
||||
#define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
|
||||
|
||||
#define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
|
||||
|
||||
@@ -151,6 +165,8 @@ typedef struct lua_TValue TValue;
|
||||
#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))
|
||||
|
||||
@@ -159,7 +175,7 @@ typedef struct lua_TValue TValue;
|
||||
|
||||
|
||||
/* Macros for internal tests */
|
||||
#define righttt(obj) (ttypenv(obj) == gcvalue(obj)->gch.tt)
|
||||
#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)
|
||||
|
||||
#define checkliveness(g,obj) \
|
||||
lua_longassert(!iscollectable(obj) || \
|
||||
@@ -191,7 +207,8 @@ typedef struct lua_TValue TValue;
|
||||
|
||||
#define setsvalue(L,obj,x) \
|
||||
{ TValue *io=(obj); \
|
||||
val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TSTRING)); \
|
||||
TString *x_ = (x); \
|
||||
val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
|
||||
checkliveness(G(L),io); }
|
||||
|
||||
#define setuvalue(L,obj,x) \
|
||||
@@ -219,12 +236,7 @@ typedef struct lua_TValue TValue;
|
||||
val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
|
||||
checkliveness(G(L),io); }
|
||||
|
||||
#define setptvalue(L,obj,x) \
|
||||
{ TValue *io=(obj); \
|
||||
val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TPROTO)); \
|
||||
checkliveness(G(L),io); }
|
||||
|
||||
#define setdeadvalue(obj) settt_(obj, ctb(LUA_TDEADKEY))
|
||||
#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
|
||||
|
||||
|
||||
|
||||
@@ -254,6 +266,8 @@ typedef struct lua_TValue TValue;
|
||||
#define setsvalue2n setsvalue
|
||||
|
||||
|
||||
/* check whether a number is valid (useful only for NaN trick) */
|
||||
#define luai_checknum(L,o,c) { /* empty */ }
|
||||
|
||||
|
||||
/*
|
||||
@@ -261,52 +275,78 @@ typedef struct lua_TValue TValue;
|
||||
** NaN Trick
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#if defined(LUA_NANTRICKLE) || defined(LUA_NANTRICKBE)
|
||||
#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
|
||||
** 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')
|
||||
*/
|
||||
#if !defined(NNMARK)
|
||||
#define NNMARK 0x7FF7A500
|
||||
|
||||
/* 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 defined(LUA_NANTRICKLE)
|
||||
|
||||
#if (LUA_IEEEENDIAN == 0) /* { */
|
||||
|
||||
/* little endian */
|
||||
#define TValuefields \
|
||||
union { struct { Value v_; int tt_; } i; double d_; } u
|
||||
union { struct { Value v__; int tt__; } i; double d__; } u
|
||||
#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
|
||||
#else
|
||||
/* 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
|
||||
union { struct { int tt__; Value v__; } i; double d__; } u
|
||||
#define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}
|
||||
#endif
|
||||
/* 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) (((o)->u.i.tt_ & 0x7fffff00) != NNMARK)
|
||||
#define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK)
|
||||
|
||||
#define tag2tt(t) (NNMARK | (t))
|
||||
|
||||
#undef val_
|
||||
#define val_(o) ((o)->u.i.v_)
|
||||
#undef num_
|
||||
#define num_(o) ((o)->u.d_)
|
||||
|
||||
#undef rttype
|
||||
#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : (o)->u.i.tt_ & 0xff)
|
||||
#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
|
||||
|
||||
#undef settt_
|
||||
#define settt_(o,t) ((o)->u.i.tt_=tag2tt(t))
|
||||
#define settt_(o,t) (tt_(o) = tag2tt(t))
|
||||
|
||||
#undef setnvalue
|
||||
#define setnvalue(obj,x) \
|
||||
@@ -324,21 +364,18 @@ typedef struct lua_TValue TValue;
|
||||
*/
|
||||
|
||||
#undef checktag
|
||||
#define checktag(o,t) ((o)->u.i.tt_ == tag2tt(t))
|
||||
#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) : ((o1)->u.i.tt_ == (o2)->u.i.tt_))
|
||||
|
||||
(ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
|
||||
|
||||
|
||||
#undef luai_checknum
|
||||
#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
|
||||
|
||||
|
||||
#else
|
||||
|
||||
#define luai_checknum(L,o,c) { /* empty */ }
|
||||
|
||||
#endif
|
||||
/* }====================================================== */
|
||||
|
||||
@@ -377,7 +414,7 @@ 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; /* number of characters in string */
|
||||
} tsv;
|
||||
@@ -435,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;
|
||||
@@ -477,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;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
** $Id: lopcodes.c,v 1.47 2011/04/12 17:27:35 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
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.140 2011/04/07 18:14:12 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
|
||||
*/
|
||||
@@ -196,7 +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,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) + 1 */
|
||||
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++ */
|
||||
|
||||
43
loslib.c
43
loslib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: loslib.c,v 1.34 2011/03/03 16:34:46 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
|
||||
*/
|
||||
@@ -26,11 +26,12 @@
|
||||
#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,8 +53,25 @@
|
||||
|
||||
#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
|
||||
|
||||
@@ -177,13 +195,13 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) {
|
||||
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) {
|
||||
@@ -274,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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
321
lparser.c
321
lparser.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lparser.c,v 2.112 2011/06/27 18:18:59 roberto Exp roberto $
|
||||
** $Id: lparser.c,v 2.127 2012/05/08 13:53:33 roberto Exp roberto $
|
||||
** Lua Parser
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -41,8 +41,8 @@
|
||||
*/
|
||||
typedef struct BlockCnt {
|
||||
struct BlockCnt *previous; /* chain */
|
||||
int firstlabel; /* index of first label in this block */
|
||||
int firstgoto; /* index of first pending goto in this block */
|
||||
short firstlabel; /* index of first label in this block */
|
||||
short firstgoto; /* index of first pending goto in this block */
|
||||
lu_byte nactvar; /* # active locals outside the block */
|
||||
lu_byte upval; /* true if some variable in the block is an upvalue */
|
||||
lu_byte isloop; /* true if `block' is a loop */
|
||||
@@ -68,26 +68,27 @@ static void anchor_token (LexState *ls) {
|
||||
|
||||
|
||||
/* semantic error */
|
||||
static void semerror (LexState *ls, const char *msg) {
|
||||
static l_noret semerror (LexState *ls, const char *msg) {
|
||||
ls->t.token = 0; /* remove 'near to' from final message */
|
||||
luaX_syntaxerror(ls, msg);
|
||||
}
|
||||
|
||||
|
||||
static void error_expected (LexState *ls, int token) {
|
||||
static l_noret error_expected (LexState *ls, int token) {
|
||||
luaX_syntaxerror(ls,
|
||||
luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token)));
|
||||
}
|
||||
|
||||
|
||||
static void errorlimit (FuncState *fs, int limit, const char *what) {
|
||||
static l_noret errorlimit (FuncState *fs, int limit, const char *what) {
|
||||
lua_State *L = fs->ls->L;
|
||||
const char *msg;
|
||||
int line = fs->f->linedefined;
|
||||
const char *where = (line == 0)
|
||||
? "main function"
|
||||
: luaO_pushfstring(fs->L, "function at line %d", line);
|
||||
msg = luaO_pushfstring(fs->L, "too many %s (limit is %d) in %s",
|
||||
what, limit, where);
|
||||
: luaO_pushfstring(L, "function at line %d", line);
|
||||
msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s",
|
||||
what, limit, where);
|
||||
luaX_syntaxerror(fs->ls, msg);
|
||||
}
|
||||
|
||||
@@ -182,7 +183,7 @@ static void new_localvar (LexState *ls, TString *name) {
|
||||
MAXVARS, "local variables");
|
||||
luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,
|
||||
dyd->actvar.size, Vardesc, MAX_INT, "local variables");
|
||||
dyd->actvar.arr[dyd->actvar.n++].idx = cast(unsigned short, reg);
|
||||
dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg);
|
||||
}
|
||||
|
||||
|
||||
@@ -221,7 +222,7 @@ static int searchupvalue (FuncState *fs, TString *name) {
|
||||
int i;
|
||||
Upvaldesc *up = fs->f->upvalues;
|
||||
for (i = 0; i < fs->nups; i++) {
|
||||
if (eqstr(up[i].name, name)) return i;
|
||||
if (luaS_eqstr(up[i].name, name)) return i;
|
||||
}
|
||||
return -1; /* not found */
|
||||
}
|
||||
@@ -231,13 +232,13 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
|
||||
Proto *f = fs->f;
|
||||
int oldsize = f->sizeupvalues;
|
||||
checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
|
||||
luaM_growvector(fs->L, f->upvalues, fs->nups, f->sizeupvalues,
|
||||
luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
|
||||
Upvaldesc, MAXUPVAL, "upvalues");
|
||||
while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL;
|
||||
f->upvalues[fs->nups].instack = (v->k == VLOCAL);
|
||||
f->upvalues[fs->nups].idx = cast_byte(v->u.info);
|
||||
f->upvalues[fs->nups].name = name;
|
||||
luaC_objbarrier(fs->L, f, name);
|
||||
luaC_objbarrier(fs->ls->L, f, name);
|
||||
return fs->nups++;
|
||||
}
|
||||
|
||||
@@ -245,7 +246,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
|
||||
static int searchvar (FuncState *fs, TString *n) {
|
||||
int i;
|
||||
for (i=fs->nactvar-1; i >= 0; i--) {
|
||||
if (eqstr(n, getlocvar(fs, i)->varname))
|
||||
if (luaS_eqstr(n, getlocvar(fs, i)->varname))
|
||||
return i;
|
||||
}
|
||||
return -1; /* not found */
|
||||
@@ -327,13 +328,13 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
|
||||
|
||||
|
||||
static void enterlevel (LexState *ls) {
|
||||
global_State *g = G(ls->L);
|
||||
++g->nCcalls;
|
||||
checklimit(ls->fs, g->nCcalls, LUAI_MAXCCALLS, "syntax levels");
|
||||
lua_State *L = ls->L;
|
||||
++L->nCcalls;
|
||||
checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels");
|
||||
}
|
||||
|
||||
|
||||
#define leavelevel(ls) (G((ls)->L)->nCcalls--)
|
||||
#define leavelevel(ls) ((ls)->L->nCcalls--)
|
||||
|
||||
|
||||
static void closegoto (LexState *ls, int g, Labeldesc *label) {
|
||||
@@ -341,11 +342,12 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) {
|
||||
FuncState *fs = ls->fs;
|
||||
Labellist *gl = &ls->dyd->gt;
|
||||
Labeldesc *gt = &gl->arr[g];
|
||||
lua_assert(eqstr(gt->name, label->name));
|
||||
lua_assert(luaS_eqstr(gt->name, label->name));
|
||||
if (gt->nactvar < label->nactvar) {
|
||||
TString *vname = getlocvar(fs, gt->nactvar)->varname;
|
||||
const char *msg = luaO_pushfstring(ls->L,
|
||||
"<goto %s> at line %d jumps into the scope of local " LUA_QS,
|
||||
getstr(gt->name), gt->line, getstr(getlocvar(fs, gt->nactvar)->varname));
|
||||
getstr(gt->name), gt->line, getstr(vname));
|
||||
semerror(ls, msg);
|
||||
}
|
||||
luaK_patchlist(fs, gt->pc, label->pc);
|
||||
@@ -367,7 +369,7 @@ static int findlabel (LexState *ls, int g) {
|
||||
/* check labels in current block for a match */
|
||||
for (i = bl->firstlabel; i < dyd->label.n; i++) {
|
||||
Labeldesc *lb = &dyd->label.arr[i];
|
||||
if (eqstr(lb->name, gt->name)) { /* correct label? */
|
||||
if (luaS_eqstr(lb->name, gt->name)) { /* correct label? */
|
||||
if (gt->nactvar > lb->nactvar &&
|
||||
(bl->upval || dyd->label.n > bl->firstlabel))
|
||||
luaK_patchclose(ls->fs, gt->pc, lb->nactvar);
|
||||
@@ -382,7 +384,8 @@ static int findlabel (LexState *ls, int g) {
|
||||
static int newlabelentry (LexState *ls, Labellist *l, TString *name,
|
||||
int line, int pc) {
|
||||
int n = l->n;
|
||||
luaM_growvector(ls->L, l->arr, n, l->size, Labeldesc, MAX_INT, "labels");
|
||||
luaM_growvector(ls->L, l->arr, n, l->size,
|
||||
Labeldesc, SHRT_MAX, "labels/gotos");
|
||||
l->arr[n].name = name;
|
||||
l->arr[n].line = line;
|
||||
l->arr[n].nactvar = ls->fs->nactvar;
|
||||
@@ -400,7 +403,7 @@ static void findgotos (LexState *ls, Labeldesc *lb) {
|
||||
Labellist *gl = &ls->dyd->gt;
|
||||
int i = ls->fs->bl->firstgoto;
|
||||
while (i < gl->n) {
|
||||
if (eqstr(gl->arr[i].name, lb->name))
|
||||
if (luaS_eqstr(gl->arr[i].name, lb->name))
|
||||
closegoto(ls, i, lb);
|
||||
else
|
||||
i++;
|
||||
@@ -418,7 +421,7 @@ static void movegotosout (FuncState *fs, BlockCnt *bl) {
|
||||
int i = bl->firstgoto;
|
||||
Labellist *gl = &fs->ls->dyd->gt;
|
||||
/* correct pending gotos to current block and try to close it
|
||||
with visible labels */
|
||||
with visible labels */
|
||||
while (i < gl->n) {
|
||||
Labeldesc *gt = &gl->arr[i];
|
||||
if (gt->nactvar > bl->nactvar) {
|
||||
@@ -457,8 +460,8 @@ static void breaklabel (LexState *ls) {
|
||||
** generates an error for an undefined 'goto'; choose appropriate
|
||||
** message when label name is a reserved word (which can only be 'break')
|
||||
*/
|
||||
static void undefgoto (LexState *ls, Labeldesc *gt) {
|
||||
const char *msg = (gt->name->tsv.reserved > 0)
|
||||
static l_noret undefgoto (LexState *ls, Labeldesc *gt) {
|
||||
const char *msg = isreserved(gt->name)
|
||||
? "<%s> at line %d not inside a loop"
|
||||
: "no visible label " LUA_QS " for <goto> at line %d";
|
||||
msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line);
|
||||
@@ -490,21 +493,30 @@ static void leaveblock (FuncState *fs) {
|
||||
|
||||
|
||||
/*
|
||||
** adds prototype being created into its parent list of prototypes
|
||||
** and codes instruction to create new closure
|
||||
** adds a new prototype into list of prototypes
|
||||
*/
|
||||
static void codeclosure (LexState *ls, Proto *clp, expdesc *v) {
|
||||
FuncState *fs = ls->fs->prev;
|
||||
Proto *f = fs->f; /* prototype of function creating new closure */
|
||||
static Proto *addprototype (LexState *ls) {
|
||||
Proto *clp;
|
||||
lua_State *L = ls->L;
|
||||
FuncState *fs = ls->fs;
|
||||
Proto *f = fs->f; /* prototype of current function */
|
||||
if (fs->np >= f->sizep) {
|
||||
int oldsize = f->sizep;
|
||||
luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
|
||||
MAXARG_Bx, "functions");
|
||||
luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions");
|
||||
while (oldsize < f->sizep) f->p[oldsize++] = NULL;
|
||||
}
|
||||
f->p[fs->np++] = clp;
|
||||
luaC_objbarrier(ls->L, f, clp);
|
||||
init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
|
||||
f->p[fs->np++] = clp = luaF_newproto(L);
|
||||
luaC_objbarrier(L, f, clp);
|
||||
return clp;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** codes instruction to create new closure in parent function
|
||||
*/
|
||||
static void codeclosure (LexState *ls, expdesc *v) {
|
||||
FuncState *fs = ls->fs->prev;
|
||||
init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1));
|
||||
luaK_exp2nextreg(fs, v); /* fix it at stack top (for GC) */
|
||||
}
|
||||
|
||||
@@ -514,7 +526,6 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
|
||||
Proto *f;
|
||||
fs->prev = ls->fs; /* linked list of funcstates */
|
||||
fs->ls = ls;
|
||||
fs->L = L;
|
||||
ls->fs = fs;
|
||||
fs->pc = 0;
|
||||
fs->lasttarget = 0;
|
||||
@@ -527,13 +538,9 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
|
||||
fs->nactvar = 0;
|
||||
fs->firstlocal = ls->dyd->actvar.n;
|
||||
fs->bl = NULL;
|
||||
f = luaF_newproto(L);
|
||||
fs->f = f;
|
||||
f = fs->f;
|
||||
f->source = ls->source;
|
||||
f->maxstacksize = 2; /* registers 0/1 are always valid */
|
||||
/* anchor prototype (to avoid being collected) */
|
||||
setptvalue2s(L, L->top, f);
|
||||
incr_top(L);
|
||||
fs->h = luaH_new(L);
|
||||
/* anchor table of constants (to avoid being collected) */
|
||||
sethvalue2s(L, L->top, fs->h);
|
||||
@@ -566,20 +573,6 @@ static void close_func (LexState *ls) {
|
||||
anchor_token(ls);
|
||||
L->top--; /* pop table of constants */
|
||||
luaC_checkGC(L);
|
||||
L->top--; /* pop prototype (after possible collection) */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** opens the main function, which is a regular vararg function with an
|
||||
** upvalue named LUA_ENV
|
||||
*/
|
||||
static void open_mainfunc (LexState *ls, FuncState *fs, BlockCnt *bl) {
|
||||
expdesc v;
|
||||
open_func(ls, fs, bl);
|
||||
fs->f->is_vararg = 1; /* main function is always vararg */
|
||||
init_exp(&v, VLOCAL, 0);
|
||||
newupvalue(fs, ls->envn, &v); /* create environment upvalue */
|
||||
}
|
||||
|
||||
|
||||
@@ -793,8 +786,9 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
||||
/* body -> `(' parlist `)' block END */
|
||||
FuncState new_fs;
|
||||
BlockCnt bl;
|
||||
open_func(ls, &new_fs, &bl);
|
||||
new_fs.f = addprototype(ls);
|
||||
new_fs.f->linedefined = line;
|
||||
open_func(ls, &new_fs, &bl);
|
||||
checknext(ls, '(');
|
||||
if (ismethod) {
|
||||
new_localvarliteral(ls, "self"); /* create 'self' parameter */
|
||||
@@ -805,7 +799,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
||||
statlist(ls);
|
||||
new_fs.f->lastlinedefined = ls->linenumber;
|
||||
check_match(ls, TK_END, TK_FUNCTION, line);
|
||||
codeclosure(ls, new_fs.f, e);
|
||||
codeclosure(ls, e);
|
||||
close_func(ls);
|
||||
}
|
||||
|
||||
@@ -850,7 +844,6 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
||||
}
|
||||
default: {
|
||||
luaX_syntaxerror(ls, "function arguments expected");
|
||||
return;
|
||||
}
|
||||
}
|
||||
lua_assert(f->k == VNONRELOC);
|
||||
@@ -878,8 +871,8 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
||||
*/
|
||||
|
||||
|
||||
static void prefixexp (LexState *ls, expdesc *v) {
|
||||
/* prefixexp -> NAME | '(' expr ')' */
|
||||
static void primaryexp (LexState *ls, expdesc *v) {
|
||||
/* primaryexp -> NAME | '(' expr ')' */
|
||||
switch (ls->t.token) {
|
||||
case '(': {
|
||||
int line = ls->linenumber;
|
||||
@@ -895,18 +888,17 @@ static void prefixexp (LexState *ls, expdesc *v) {
|
||||
}
|
||||
default: {
|
||||
luaX_syntaxerror(ls, "unexpected symbol");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void primaryexp (LexState *ls, expdesc *v) {
|
||||
/* primaryexp ->
|
||||
prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
|
||||
static void suffixedexp (LexState *ls, expdesc *v) {
|
||||
/* suffixedexp ->
|
||||
primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */
|
||||
FuncState *fs = ls->fs;
|
||||
int line = ls->linenumber;
|
||||
prefixexp(ls, v);
|
||||
primaryexp(ls, v);
|
||||
for (;;) {
|
||||
switch (ls->t.token) {
|
||||
case '.': { /* fieldsel */
|
||||
@@ -941,7 +933,7 @@ static void primaryexp (LexState *ls, expdesc *v) {
|
||||
|
||||
static void simpleexp (LexState *ls, expdesc *v) {
|
||||
/* simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... |
|
||||
constructor | FUNCTION body | primaryexp */
|
||||
constructor | FUNCTION body | suffixedexp */
|
||||
switch (ls->t.token) {
|
||||
case TK_NUMBER: {
|
||||
init_exp(v, VKNUM, 0);
|
||||
@@ -981,7 +973,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
|
||||
return;
|
||||
}
|
||||
default: {
|
||||
primaryexp(ls, v);
|
||||
suffixedexp(ls, v);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1039,7 +1031,7 @@ static const struct {
|
||||
** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
|
||||
** where `binop' is any binary operator with a priority higher than `limit'
|
||||
*/
|
||||
static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
|
||||
static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
|
||||
BinOpr op;
|
||||
UnOpr uop;
|
||||
enterlevel(ls);
|
||||
@@ -1105,31 +1097,34 @@ struct LHS_assign {
|
||||
|
||||
|
||||
/*
|
||||
** check whether, in an assignment to a local variable, the local variable
|
||||
** is needed in a previous assignment (to a table). If so, save original
|
||||
** local value in a safe place and use this safe copy in the previous
|
||||
** assignment.
|
||||
** check whether, in an assignment to an upvalue/local variable, the
|
||||
** upvalue/local variable is begin used in a previous assignment to a
|
||||
** table. If so, save original upvalue/local value in a safe place and
|
||||
** use this safe copy in the previous assignment.
|
||||
*/
|
||||
static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
|
||||
FuncState *fs = ls->fs;
|
||||
int extra = fs->freereg; /* eventual position to save local variable */
|
||||
int conflict = 0;
|
||||
for (; lh; lh = lh->prev) {
|
||||
/* conflict in table 't'? */
|
||||
if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) {
|
||||
conflict = 1;
|
||||
lh->v.u.ind.vt = VLOCAL;
|
||||
lh->v.u.ind.t = extra; /* previous assignment will use safe copy */
|
||||
}
|
||||
/* conflict in index 'idx'? */
|
||||
if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) {
|
||||
conflict = 1;
|
||||
lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */
|
||||
for (; lh; lh = lh->prev) { /* check all previous assignments */
|
||||
if (lh->v.k == VINDEXED) { /* assigning to a table? */
|
||||
/* table is the upvalue/local being assigned now? */
|
||||
if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) {
|
||||
conflict = 1;
|
||||
lh->v.u.ind.vt = VLOCAL;
|
||||
lh->v.u.ind.t = extra; /* previous assignment will use safe copy */
|
||||
}
|
||||
/* index is the local being assigned? (index cannot be upvalue) */
|
||||
if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) {
|
||||
conflict = 1;
|
||||
lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */
|
||||
}
|
||||
}
|
||||
}
|
||||
if (conflict) {
|
||||
/* copy upvalue/local value to a temporary (in position 'extra') */
|
||||
OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
|
||||
luaK_codeABC(fs, op, fs->freereg, v->u.info, 0); /* make copy */
|
||||
luaK_codeABC(fs, op, extra, v->u.info, 0);
|
||||
luaK_reserveregs(fs, 1);
|
||||
}
|
||||
}
|
||||
@@ -1138,14 +1133,14 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
|
||||
static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
|
||||
expdesc e;
|
||||
check_condition(ls, vkisvar(lh->v.k), "syntax error");
|
||||
if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
|
||||
if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */
|
||||
struct LHS_assign nv;
|
||||
nv.prev = lh;
|
||||
primaryexp(ls, &nv.v);
|
||||
suffixedexp(ls, &nv.v);
|
||||
if (nv.v.k != VINDEXED)
|
||||
check_conflict(ls, lh, &nv.v);
|
||||
checklimit(ls->fs, nvars, LUAI_MAXCCALLS - G(ls->L)->nCcalls,
|
||||
"variable names");
|
||||
checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS,
|
||||
"C levels");
|
||||
assignment(ls, &nv, nvars+1);
|
||||
}
|
||||
else { /* assignment -> `=' explist */
|
||||
@@ -1178,10 +1173,18 @@ static int cond (LexState *ls) {
|
||||
}
|
||||
|
||||
|
||||
static void gotostat (LexState *ls, TString *label, int line) {
|
||||
/* create new entry for this goto */
|
||||
int g = newlabelentry(ls, &ls->dyd->gt, label, line, luaK_jump(ls->fs));
|
||||
findlabel(ls, g);
|
||||
static void gotostat (LexState *ls, int pc) {
|
||||
int line = ls->linenumber;
|
||||
TString *label;
|
||||
int g;
|
||||
if (testnext(ls, TK_GOTO))
|
||||
label = str_checkname(ls);
|
||||
else {
|
||||
luaX_next(ls); /* skip break */
|
||||
label = luaS_new(ls->L, "break");
|
||||
}
|
||||
g = newlabelentry(ls, &ls->dyd->gt, label, line, pc);
|
||||
findlabel(ls, g); /* close it if label already defined */
|
||||
}
|
||||
|
||||
|
||||
@@ -1189,8 +1192,8 @@ static void gotostat (LexState *ls, TString *label, int line) {
|
||||
static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) {
|
||||
int i;
|
||||
for (i = fs->bl->firstlabel; i < ll->n; i++) {
|
||||
if (eqstr(label, ll->arr[i].name)) {
|
||||
const char *msg = luaO_pushfstring(fs->ls->L,
|
||||
if (luaS_eqstr(label, ll->arr[i].name)) {
|
||||
const char *msg = luaO_pushfstring(fs->ls->L,
|
||||
"label " LUA_QS " already defined on line %d",
|
||||
getstr(label), ll->arr[i].line);
|
||||
semerror(fs->ls, msg);
|
||||
@@ -1199,6 +1202,13 @@ static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) {
|
||||
}
|
||||
|
||||
|
||||
/* skip no-op statements */
|
||||
static void skipnoopstat (LexState *ls) {
|
||||
while (ls->t.token == ';' || ls->t.token == TK_DBCOLON)
|
||||
statement(ls);
|
||||
}
|
||||
|
||||
|
||||
static void labelstat (LexState *ls, TString *label, int line) {
|
||||
/* label -> '::' NAME '::' */
|
||||
FuncState *fs = ls->fs;
|
||||
@@ -1208,9 +1218,7 @@ static void labelstat (LexState *ls, TString *label, int line) {
|
||||
checknext(ls, TK_DBCOLON); /* skip double colon */
|
||||
/* create new entry for this label */
|
||||
l = newlabelentry(ls, ll, label, line, fs->pc);
|
||||
/* skip other no-op statements */
|
||||
while (ls->t.token == ';' || ls->t.token == TK_DBCOLON)
|
||||
statement(ls);
|
||||
skipnoopstat(ls); /* skip other no-op statements */
|
||||
if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */
|
||||
/* assume that locals are already out of scope */
|
||||
ll->arr[l].nactvar = fs->bl->nactvar;
|
||||
@@ -1360,38 +1368,52 @@ static void forstat (LexState *ls, int line) {
|
||||
}
|
||||
|
||||
|
||||
static int test_then_block (LexState *ls) {
|
||||
static void test_then_block (LexState *ls, int *escapelist) {
|
||||
/* test_then_block -> [IF | ELSEIF] cond THEN block */
|
||||
int condexit;
|
||||
BlockCnt bl;
|
||||
FuncState *fs = ls->fs;
|
||||
expdesc v;
|
||||
int jf; /* instruction to skip 'then' code (if condition is false) */
|
||||
luaX_next(ls); /* skip IF or ELSEIF */
|
||||
condexit = cond(ls);
|
||||
expr(ls, &v); /* read condition */
|
||||
checknext(ls, TK_THEN);
|
||||
block(ls); /* `then' part */
|
||||
return condexit;
|
||||
if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) {
|
||||
luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */
|
||||
enterblock(fs, &bl, 0); /* must enter block before 'goto' */
|
||||
gotostat(ls, v.t); /* handle goto/break */
|
||||
skipnoopstat(ls); /* skip other no-op statements */
|
||||
if (block_follow(ls, 0)) { /* 'goto' is the entire block? */
|
||||
leaveblock(fs);
|
||||
return; /* and that is it */
|
||||
}
|
||||
else /* must skip over 'then' part if condition is false */
|
||||
jf = luaK_jump(fs);
|
||||
}
|
||||
else { /* regular case (not goto/break) */
|
||||
luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */
|
||||
enterblock(fs, &bl, 0);
|
||||
jf = v.f;
|
||||
}
|
||||
statlist(ls); /* `then' part */
|
||||
leaveblock(fs);
|
||||
if (ls->t.token == TK_ELSE ||
|
||||
ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
|
||||
luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */
|
||||
luaK_patchtohere(fs, jf);
|
||||
}
|
||||
|
||||
|
||||
static void ifstat (LexState *ls, int line) {
|
||||
/* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
|
||||
FuncState *fs = ls->fs;
|
||||
int flist;
|
||||
int escapelist = NO_JUMP;
|
||||
flist = test_then_block(ls); /* IF cond THEN block */
|
||||
while (ls->t.token == TK_ELSEIF) {
|
||||
luaK_concat(fs, &escapelist, luaK_jump(fs));
|
||||
luaK_patchtohere(fs, flist);
|
||||
flist = test_then_block(ls); /* ELSEIF cond THEN block */
|
||||
}
|
||||
if (ls->t.token == TK_ELSE) {
|
||||
luaK_concat(fs, &escapelist, luaK_jump(fs));
|
||||
luaK_patchtohere(fs, flist);
|
||||
luaX_next(ls); /* skip ELSE (after patch, for correct line info) */
|
||||
int escapelist = NO_JUMP; /* exit list for finished parts */
|
||||
test_then_block(ls, &escapelist); /* IF cond THEN block */
|
||||
while (ls->t.token == TK_ELSEIF)
|
||||
test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */
|
||||
if (testnext(ls, TK_ELSE))
|
||||
block(ls); /* `else' part */
|
||||
}
|
||||
else
|
||||
luaK_concat(fs, &escapelist, flist);
|
||||
luaK_patchtohere(fs, escapelist);
|
||||
check_match(ls, TK_END, TK_IF, line);
|
||||
luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */
|
||||
}
|
||||
|
||||
|
||||
@@ -1456,13 +1478,15 @@ static void exprstat (LexState *ls) {
|
||||
/* stat -> func | assignment */
|
||||
FuncState *fs = ls->fs;
|
||||
struct LHS_assign v;
|
||||
primaryexp(ls, &v.v);
|
||||
if (v.v.k == VCALL) /* stat -> func */
|
||||
SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */
|
||||
else { /* stat -> assignment */
|
||||
suffixedexp(ls, &v.v);
|
||||
if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */
|
||||
v.prev = NULL;
|
||||
assignment(ls, &v, 1);
|
||||
}
|
||||
else { /* stat -> func */
|
||||
check_condition(ls, v.v.k == VCALL, "syntax error");
|
||||
SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1551,15 +1575,9 @@ static void statement (LexState *ls) {
|
||||
retstat(ls);
|
||||
break;
|
||||
}
|
||||
case TK_BREAK: { /* stat -> breakstat */
|
||||
luaX_next(ls); /* skip BREAK */
|
||||
/* code it as "goto 'break'" */
|
||||
gotostat(ls, luaS_new(ls->L, "break"), line);
|
||||
break;
|
||||
}
|
||||
case TK_BREAK: /* stat -> breakstat */
|
||||
case TK_GOTO: { /* stat -> 'goto' NAME */
|
||||
luaX_next(ls); /* skip GOTO */
|
||||
gotostat(ls, str_checkname(ls), line);
|
||||
gotostat(ls, luaK_jump(ls->fs));
|
||||
break;
|
||||
}
|
||||
default: { /* stat -> func | assignment */
|
||||
@@ -1576,27 +1594,42 @@ static void statement (LexState *ls) {
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
Dyndata *dyd, const char *name, int firstchar) {
|
||||
/*
|
||||
** compiles the main function, which is a regular vararg function with an
|
||||
** upvalue named LUA_ENV
|
||||
*/
|
||||
static void mainfunc (LexState *ls, FuncState *fs) {
|
||||
BlockCnt bl;
|
||||
expdesc v;
|
||||
open_func(ls, fs, &bl);
|
||||
fs->f->is_vararg = 1; /* main function is always vararg */
|
||||
init_exp(&v, VLOCAL, 0); /* create and... */
|
||||
newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */
|
||||
luaX_next(ls); /* read first token */
|
||||
statlist(ls); /* parse main body */
|
||||
check(ls, TK_EOS);
|
||||
close_func(ls);
|
||||
}
|
||||
|
||||
|
||||
Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
Dyndata *dyd, const char *name, int firstchar) {
|
||||
LexState lexstate;
|
||||
FuncState funcstate;
|
||||
BlockCnt bl;
|
||||
TString *tname = luaS_new(L, name);
|
||||
setsvalue2s(L, L->top, tname); /* push name to protect it */
|
||||
Closure *cl = luaF_newLclosure(L, 1); /* create main closure */
|
||||
/* anchor closure (to avoid being collected) */
|
||||
setclLvalue(L, L->top, cl);
|
||||
incr_top(L);
|
||||
funcstate.f = cl->l.p = luaF_newproto(L);
|
||||
funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
|
||||
lexstate.buff = buff;
|
||||
lexstate.dyd = dyd;
|
||||
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
|
||||
luaX_setinput(L, &lexstate, z, tname, firstchar);
|
||||
open_mainfunc(&lexstate, &funcstate, &bl);
|
||||
luaX_next(&lexstate); /* read first token */
|
||||
statlist(&lexstate); /* main body */
|
||||
check(&lexstate, TK_EOS);
|
||||
close_func(&lexstate);
|
||||
L->top--; /* pop name */
|
||||
luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar);
|
||||
mainfunc(&lexstate, &funcstate);
|
||||
lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
|
||||
return funcstate.f;
|
||||
/* all scopes should be correctly finished */
|
||||
lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
|
||||
return cl; /* it's on the stack too */
|
||||
}
|
||||
|
||||
|
||||
17
lparser.h
17
lparser.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.67 2011/02/07 17:14:50 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
|
||||
*/
|
||||
@@ -55,7 +55,7 @@ typedef struct expdesc {
|
||||
|
||||
/* description of active local variable */
|
||||
typedef struct Vardesc {
|
||||
unsigned short idx; /* variable index in stack */
|
||||
short idx; /* variable index in stack */
|
||||
} Vardesc;
|
||||
|
||||
|
||||
@@ -98,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,
|
||||
Dyndata *dyd, const char *name, int firstchar);
|
||||
LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
Dyndata *dyd, const char *name, int firstchar);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
53
lstate.c
53
lstate.c
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 2.88 2010/12/20 18:17:46 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,6 +77,28 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
|
||||
** invariant
|
||||
@@ -136,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);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,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;
|
||||
@@ -191,7 +226,7 @@ static void close_state (lua_State *L) {
|
||||
luaZ_freebuffer(L, &g->buff);
|
||||
freestack(L);
|
||||
lua_assert(gettotalbytes(g) == sizeof(LG));
|
||||
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);
|
||||
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
|
||||
}
|
||||
|
||||
|
||||
@@ -237,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;
|
||||
g->gcrunning = 0; /* no GC while building state */
|
||||
g->lastmajormem = 0;
|
||||
g->GCestimate = 0;
|
||||
g->strt.size = 0;
|
||||
g->strt.nuse = 0;
|
||||
g->strt.hash = NULL;
|
||||
@@ -257,6 +292,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
g->allgc = 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);
|
||||
@@ -279,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);
|
||||
}
|
||||
|
||||
24
lstate.h
24
lstate.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 2.71 2010/12/20 19:40:07 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
|
||||
*/
|
||||
@@ -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,6 +100,7 @@ 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 isLua(ci) ((ci)->callstatus & CIST_LUA)
|
||||
@@ -115,10 +114,11 @@ typedef struct global_State {
|
||||
void *ud; /* auxiliary data to `frealloc' */
|
||||
lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
||||
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
||||
lu_mem lastmajormem; /* memory in use after last major collection */
|
||||
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 */
|
||||
@@ -126,7 +126,8 @@ typedef struct global_State {
|
||||
int sweepstrgc; /* position of sweep in `strt' */
|
||||
GCObject *allgc; /* list of all collectable objects */
|
||||
GCObject *finobj; /* list of collectable objects with finalizers */
|
||||
GCObject **sweepgc; /* current position of sweep */
|
||||
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))
|
||||
|
||||
115
lstring.c
115
lstring.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 2.18 2010/05/10 18:23:45 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,37 +92,49 @@ 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);
|
||||
@@ -92,10 +146,27 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||
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);
|
||||
|
||||
43
lstrlib.c
43
lstrlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstrlib.c,v 1.169 2011/06/16 14:14:05 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))
|
||||
|
||||
|
||||
|
||||
@@ -119,7 +119,9 @@ static int str_rep (lua_State *L) {
|
||||
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;
|
||||
memcpy(p, sep, lsep * sizeof(char)); p += lsep;
|
||||
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);
|
||||
@@ -138,7 +140,7 @@ 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? */
|
||||
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++)
|
||||
@@ -154,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);
|
||||
@@ -745,13 +747,13 @@ static int str_gsub (lua_State *L) {
|
||||
#if !defined(LUA_INTFRMLEN) /* { */
|
||||
#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 /* } */
|
||||
@@ -764,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
|
||||
|
||||
@@ -865,13 +867,24 @@ 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':
|
||||
|
||||
98
ltable.c
98
ltable.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 2.59 2011/06/09 18:23:27 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
|
||||
*/
|
||||
@@ -48,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)
|
||||
|
||||
|
||||
/*
|
||||
@@ -64,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)
|
||||
@@ -105,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));
|
||||
@@ -148,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 (luaV_rawequalobj(gkey(n), key) ||
|
||||
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
|
||||
gcvalue(gkey(n)) == gcvalue(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 */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,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);
|
||||
@@ -322,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 */
|
||||
}
|
||||
|
||||
|
||||
@@ -374,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);
|
||||
}
|
||||
@@ -398,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));
|
||||
@@ -452,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,9 +479,9 @@ const TValue *luaH_getstr (Table *t, TString *key) {
|
||||
** main search function
|
||||
*/
|
||||
const TValue *luaH_get (Table *t, const TValue *key) {
|
||||
switch (ttypenv(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);
|
||||
@@ -493,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);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltablib.c,v 1.60 2011/07/02 16:01:44 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
|
||||
*/
|
||||
@@ -86,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);
|
||||
}
|
||||
|
||||
|
||||
@@ -129,8 +129,7 @@ static int pack (lua_State *L) {
|
||||
for (i = n; i >= 2; i--) /* assign other elements */
|
||||
lua_rawseti(L, 1, i);
|
||||
}
|
||||
lua_pushinteger(L, n);
|
||||
return 2; /* return table and number of elements */
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
|
||||
|
||||
91
ltests.c
91
ltests.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 2.120 2011/06/24 14:36:21 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
|
||||
*/
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -267,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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -320,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));
|
||||
@@ -347,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);
|
||||
}
|
||||
}
|
||||
@@ -371,7 +381,8 @@ static void checkgraylist (GCObject *l) {
|
||||
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 */
|
||||
@@ -416,6 +427,7 @@ 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)));
|
||||
@@ -426,17 +438,21 @@ int lua_checkmemory (lua_State *L) {
|
||||
/* 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 'finobj' list */
|
||||
checkold(g, g->finobj);
|
||||
for (o = g->finobj; o != NULL; o = gch(o)->next) {
|
||||
lua_assert(!isdead(g, o) && testbit(o->gch.marked, SEPARATED));
|
||||
lua_assert(testbit(o->gch.marked, SEPARATED));
|
||||
lua_assert(gch(o)->tt == LUA_TUSERDATA ||
|
||||
gch(o)->tt == LUA_TTABLE);
|
||||
checkobject(g, o);
|
||||
checkobject(g, o, 0);
|
||||
}
|
||||
/* check 'tobefnz' list */
|
||||
checkold(g, g->tobefnz);
|
||||
@@ -504,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
|
||||
|
||||
|
||||
@@ -595,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;
|
||||
}
|
||||
|
||||
@@ -846,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},
|
||||
@@ -1210,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);
|
||||
@@ -1254,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));
|
||||
@@ -1397,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);
|
||||
|
||||
71
lua.c
71
lua.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.199 2011/05/26 16:09:40 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
|
||||
|
||||
@@ -118,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"
|
||||
,
|
||||
@@ -222,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);
|
||||
}
|
||||
|
||||
@@ -348,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? */
|
||||
@@ -359,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' */
|
||||
@@ -430,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);
|
||||
|
||||
24
lua.h
24
lua.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.277 2011/04/18 14:15:48 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
|
||||
@@ -19,11 +19,11 @@
|
||||
#define LUA_VERSION_MAJOR "5"
|
||||
#define LUA_VERSION_MINOR "2"
|
||||
#define LUA_VERSION_NUM 502
|
||||
#define LUA_VERSION_RELEASE "0"
|
||||
#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_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2011 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-2011 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
|
||||
|
||||
134
luaconf.h
134
luaconf.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: luaconf.h,v 1.160 2011/06/28 17:14:12 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,8 +29,8 @@
|
||||
#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)
|
||||
@@ -70,6 +70,7 @@
|
||||
#define LUA_USE_ISATTY
|
||||
#define LUA_USE_POPEN
|
||||
#define LUA_USE_ULONGJMP
|
||||
#define LUA_USE_GMTIME_R
|
||||
#endif
|
||||
|
||||
|
||||
@@ -173,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 */
|
||||
@@ -210,10 +206,14 @@
|
||||
|
||||
/*
|
||||
@@ 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.
|
||||
@@ -223,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
|
||||
|
||||
|
||||
|
||||
@@ -418,7 +425,7 @@
|
||||
*/
|
||||
|
||||
/* 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))
|
||||
@@ -453,65 +460,76 @@
|
||||
#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(__X86__) || \
|
||||
defined (__x86_64)
|
||||
#define LUA_IEEEENDIAN 0
|
||||
#elif defined(__POWERPC__) || defined(__ppc__)
|
||||
#define LUA_IEEEENDIAN 1
|
||||
#endif
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif /* } */
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_NANTRICKLE/LUA_NANTRICKBE 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).
|
||||
*/
|
||||
#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).
|
||||
*/
|
||||
|
||||
/* little-endian architectures that satisfy those conditions */
|
||||
#if defined(__i386__) || defined(__i386) || defined(__X86__) || \
|
||||
defined(_M_IX86)
|
||||
/* Microsoft compiler on a Pentium (32 bit) ? */
|
||||
#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */
|
||||
|
||||
#define LUA_NANTRICKLE
|
||||
#define MS_ASMTRICK
|
||||
#define LUA_IEEEENDIAN 0
|
||||
#define LUA_NANTRICK
|
||||
|
||||
#endif
|
||||
|
||||
/* 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 /* } */
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
5
lualib.h
5
lualib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lualib.h,v 1.41 2010/10/25 14:32:36 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);
|
||||
|
||||
|
||||
44
lundump.c
44
lundump.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.69 2011/05/06 13:35:17 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);
|
||||
@@ -39,12 +39,12 @@ static void error(LoadState* S, const char* why)
|
||||
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
|
||||
|
||||
#if !defined(luai_verifycode)
|
||||
#define luai_verifycode(L,b,f) (f)
|
||||
#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)
|
||||
@@ -91,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)
|
||||
{
|
||||
@@ -118,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)
|
||||
@@ -136,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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,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);
|
||||
@@ -176,8 +179,6 @@ 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 */
|
||||
@@ -202,9 +203,10 @@ static void LoadHeader(LoadState* S)
|
||||
/*
|
||||
** 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])
|
||||
@@ -215,7 +217,19 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
|
||||
S.Z=Z;
|
||||
S.b=buff;
|
||||
LoadHeader(&S);
|
||||
return luai_verifycode(L,buff,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')
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.44 2011/05/06 13:35:17 lhf Exp $
|
||||
** $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,7 +11,7 @@
|
||||
#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 (lu_byte* h);
|
||||
|
||||
106
lvm.c
106
lvm.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.c,v 2.140 2011/06/02 19:31:40 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
|
||||
*/
|
||||
@@ -60,10 +60,15 @@ 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)->p;
|
||||
int npc = pcRel(ci->u.l.savedpc, p);
|
||||
@@ -71,11 +76,15 @@ static void traceexec (lua_State *L) {
|
||||
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");
|
||||
}
|
||||
@@ -217,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;
|
||||
}
|
||||
|
||||
|
||||
@@ -229,11 +247,11 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -249,7 +267,8 @@ int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *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;
|
||||
@@ -284,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 */
|
||||
@@ -385,7 +404,8 @@ 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);
|
||||
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? */
|
||||
@@ -491,7 +511,11 @@ void luaV_finishOp (lua_State *L) {
|
||||
|
||||
#define Protect(x) { {x;}; base = ci->u.l.base; }
|
||||
|
||||
#define checkGC(L,c) Protect(luaC_condGC(L, c); 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) { \
|
||||
@@ -506,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;
|
||||
@@ -525,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);
|
||||
@@ -583,11 +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,
|
||||
L->top = ra + 1; /* limit of live values */
|
||||
luaC_step(L);
|
||||
L->top = ci->top; /* restore top */
|
||||
)
|
||||
checkGC(L, ra + 1);
|
||||
)
|
||||
vmcase(OP_SELF,
|
||||
StkId rb = RB(i);
|
||||
@@ -639,10 +660,7 @@ void luaV_execute (lua_State *L) {
|
||||
ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
|
||||
rb = b + base;
|
||||
setobjs2s(L, ra, rb);
|
||||
checkGC(L,
|
||||
L->top = (ra >= rb ? ra + 1 : rb); /* limit of live values */
|
||||
luaC_step(L);
|
||||
)
|
||||
checkGC(L, (ra >= rb ? ra + 1 : rb));
|
||||
L->top = ci->top; /* restore top */
|
||||
)
|
||||
vmcase(OP_JMP,
|
||||
@@ -652,7 +670,7 @@ void luaV_execute (lua_State *L) {
|
||||
TValue *rb = RKB(i);
|
||||
TValue *rc = RKC(i);
|
||||
Protect(
|
||||
if (equalobj(L, rb, rc) != GETARG_A(i))
|
||||
if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i))
|
||||
ci->u.l.savedpc++;
|
||||
else
|
||||
donextjump(ci);
|
||||
@@ -732,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);
|
||||
@@ -771,7 +789,7 @@ void luaV_execute (lua_State *L) {
|
||||
setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
|
||||
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);
|
||||
@@ -808,7 +826,7 @@ void luaV_execute (lua_State *L) {
|
||||
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) */
|
||||
@@ -820,11 +838,7 @@ void luaV_execute (lua_State *L) {
|
||||
pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
|
||||
else
|
||||
setclLvalue(L, ra, ncl); /* push cashed closure */
|
||||
checkGC(L,
|
||||
L->top = ra + 1; /* limit of live values */
|
||||
luaC_step(L);
|
||||
L->top = ci->top; /* restore top */
|
||||
)
|
||||
checkGC(L, ra + 1);
|
||||
)
|
||||
vmcase(OP_VARARG,
|
||||
int b = GETARG_B(i) - 1;
|
||||
|
||||
6
lzio.c
6
lzio.c
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.32 2011/02/17 17:34:16 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
|
||||
*/
|
||||
|
||||
@@ -29,7 +29,7 @@ int luaZ_fill (ZIO *z) {
|
||||
return EOZ;
|
||||
z->n = size - 1; /* discount char being returned */
|
||||
z->p = buff;
|
||||
return char2int(*(z->p++));
|
||||
return cast_uchar(*(z->p++));
|
||||
}
|
||||
|
||||
|
||||
|
||||
8
lzio.h
8
lzio.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.h,v 1.23 2011/02/17 17:34:16 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 {
|
||||
|
||||
Reference in New Issue
Block a user