aboutsummaryrefslogtreecommitdiff
path: root/src/lua/ldblib.c
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-06-22 16:50:40 +0800
committerLi Jin <dragon-fly@qq.com>2020-06-22 16:50:40 +0800
commitcd2b60b101a398cb9356d746364e70eaed1860f1 (patch)
treea1fe71b76faabc4883f16905a94164ce5c23e692 /src/lua/ldblib.c
parent88c1052e700f38cf3d8ad82d469da4c487760b7e (diff)
downloadyuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.gz
yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.bz2
yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.zip
add support for local variable declared with attribute 'close' and 'const' for Lua 5.4.
Diffstat (limited to '')
-rw-r--r--src/lua/ldblib.c (renamed from src/lua-5.3/ldblib.c)73
1 files changed, 47 insertions, 26 deletions
diff --git a/src/lua-5.3/ldblib.c b/src/lua/ldblib.c
index 9d29afb..59eb8f0 100644
--- a/src/lua-5.3/ldblib.c
+++ b/src/lua/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.151.1.1 2017/04/19 17:20:42 roberto Exp $ 2** $Id: ldblib.c $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -21,10 +21,10 @@
21 21
22 22
23/* 23/*
24** The hook table at registry[&HOOKKEY] maps threads to their current 24** The hook table at registry[HOOKKEY] maps threads to their current
25** hook function. (We only need the unique address of 'HOOKKEY'.) 25** hook function.
26*/ 26*/
27static const int HOOKKEY = 0; 27static const char *const HOOKKEY = "_HOOKKEY";
28 28
29 29
30/* 30/*
@@ -55,8 +55,7 @@ static int db_getmetatable (lua_State *L) {
55 55
56static int db_setmetatable (lua_State *L) { 56static int db_setmetatable (lua_State *L) {
57 int t = lua_type(L, 2); 57 int t = lua_type(L, 2);
58 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 58 luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
59 "nil or table expected");
60 lua_settop(L, 2); 59 lua_settop(L, 2);
61 lua_setmetatable(L, 1); 60 lua_setmetatable(L, 1);
62 return 1; /* return 1st argument */ 61 return 1; /* return 1st argument */
@@ -64,19 +63,24 @@ static int db_setmetatable (lua_State *L) {
64 63
65 64
66static int db_getuservalue (lua_State *L) { 65static int db_getuservalue (lua_State *L) {
66 int n = (int)luaL_optinteger(L, 2, 1);
67 if (lua_type(L, 1) != LUA_TUSERDATA) 67 if (lua_type(L, 1) != LUA_TUSERDATA)
68 lua_pushnil(L); 68 luaL_pushfail(L);
69 else 69 else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
70 lua_getuservalue(L, 1); 70 lua_pushboolean(L, 1);
71 return 2;
72 }
71 return 1; 73 return 1;
72} 74}
73 75
74 76
75static int db_setuservalue (lua_State *L) { 77static int db_setuservalue (lua_State *L) {
78 int n = (int)luaL_optinteger(L, 3, 1);
76 luaL_checktype(L, 1, LUA_TUSERDATA); 79 luaL_checktype(L, 1, LUA_TUSERDATA);
77 luaL_checkany(L, 2); 80 luaL_checkany(L, 2);
78 lua_settop(L, 2); 81 lua_settop(L, 2);
79 lua_setuservalue(L, 1); 82 if (!lua_setiuservalue(L, 1, n))
83 luaL_pushfail(L);
80 return 1; 84 return 1;
81} 85}
82 86
@@ -146,7 +150,7 @@ static int db_getinfo (lua_State *L) {
146 lua_Debug ar; 150 lua_Debug ar;
147 int arg; 151 int arg;
148 lua_State *L1 = getthread(L, &arg); 152 lua_State *L1 = getthread(L, &arg);
149 const char *options = luaL_optstring(L, arg+2, "flnStu"); 153 const char *options = luaL_optstring(L, arg+2, "flnSrtu");
150 checkstack(L, L1, 3); 154 checkstack(L, L1, 3);
151 if (lua_isfunction(L, arg + 1)) { /* info about a function? */ 155 if (lua_isfunction(L, arg + 1)) { /* info about a function? */
152 options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ 156 options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
@@ -155,7 +159,7 @@ static int db_getinfo (lua_State *L) {
155 } 159 }
156 else { /* stack level */ 160 else { /* stack level */
157 if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { 161 if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
158 lua_pushnil(L); /* level out of range */ 162 luaL_pushfail(L); /* level out of range */
159 return 1; 163 return 1;
160 } 164 }
161 } 165 }
@@ -163,7 +167,8 @@ static int db_getinfo (lua_State *L) {
163 return luaL_argerror(L, arg+2, "invalid option"); 167 return luaL_argerror(L, arg+2, "invalid option");
164 lua_newtable(L); /* table to collect results */ 168 lua_newtable(L); /* table to collect results */
165 if (strchr(options, 'S')) { 169 if (strchr(options, 'S')) {
166 settabss(L, "source", ar.source); 170 lua_pushlstring(L, ar.source, ar.srclen);
171 lua_setfield(L, -2, "source");
167 settabss(L, "short_src", ar.short_src); 172 settabss(L, "short_src", ar.short_src);
168 settabsi(L, "linedefined", ar.linedefined); 173 settabsi(L, "linedefined", ar.linedefined);
169 settabsi(L, "lastlinedefined", ar.lastlinedefined); 174 settabsi(L, "lastlinedefined", ar.lastlinedefined);
@@ -180,6 +185,10 @@ static int db_getinfo (lua_State *L) {
180 settabss(L, "name", ar.name); 185 settabss(L, "name", ar.name);
181 settabss(L, "namewhat", ar.namewhat); 186 settabss(L, "namewhat", ar.namewhat);
182 } 187 }
188 if (strchr(options, 'r')) {
189 settabsi(L, "ftransfer", ar.ftransfer);
190 settabsi(L, "ntransfer", ar.ntransfer);
191 }
183 if (strchr(options, 't')) 192 if (strchr(options, 't'))
184 settabsb(L, "istailcall", ar.istailcall); 193 settabsb(L, "istailcall", ar.istailcall);
185 if (strchr(options, 'L')) 194 if (strchr(options, 'L'))
@@ -193,8 +202,6 @@ static int db_getinfo (lua_State *L) {
193static int db_getlocal (lua_State *L) { 202static int db_getlocal (lua_State *L) {
194 int arg; 203 int arg;
195 lua_State *L1 = getthread(L, &arg); 204 lua_State *L1 = getthread(L, &arg);
196 lua_Debug ar;
197 const char *name;
198 int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ 205 int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
199 if (lua_isfunction(L, arg + 1)) { /* function argument? */ 206 if (lua_isfunction(L, arg + 1)) { /* function argument? */
200 lua_pushvalue(L, arg + 1); /* push function */ 207 lua_pushvalue(L, arg + 1); /* push function */
@@ -202,6 +209,8 @@ static int db_getlocal (lua_State *L) {
202 return 1; /* return only name (there is no value) */ 209 return 1; /* return only name (there is no value) */
203 } 210 }
204 else { /* stack-level argument */ 211 else { /* stack-level argument */
212 lua_Debug ar;
213 const char *name;
205 int level = (int)luaL_checkinteger(L, arg + 1); 214 int level = (int)luaL_checkinteger(L, arg + 1);
206 if (!lua_getstack(L1, level, &ar)) /* out of range? */ 215 if (!lua_getstack(L1, level, &ar)) /* out of range? */
207 return luaL_argerror(L, arg+1, "level out of range"); 216 return luaL_argerror(L, arg+1, "level out of range");
@@ -214,7 +223,7 @@ static int db_getlocal (lua_State *L) {
214 return 2; 223 return 2;
215 } 224 }
216 else { 225 else {
217 lua_pushnil(L); /* no name (nor value) */ 226 luaL_pushfail(L); /* no name (nor value) */
218 return 1; 227 return 1;
219 } 228 }
220 } 229 }
@@ -305,7 +314,7 @@ static int db_upvaluejoin (lua_State *L) {
305static void hookf (lua_State *L, lua_Debug *ar) { 314static void hookf (lua_State *L, lua_Debug *ar) {
306 static const char *const hooknames[] = 315 static const char *const hooknames[] =
307 {"call", "return", "line", "count", "tail call"}; 316 {"call", "return", "line", "count", "tail call"};
308 lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); 317 lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
309 lua_pushthread(L); 318 lua_pushthread(L);
310 if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ 319 if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
311 lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ 320 lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
@@ -358,14 +367,12 @@ static int db_sethook (lua_State *L) {
358 count = (int)luaL_optinteger(L, arg + 3, 0); 367 count = (int)luaL_optinteger(L, arg + 3, 0);
359 func = hookf; mask = makemask(smask, count); 368 func = hookf; mask = makemask(smask, count);
360 } 369 }
361 if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { 370 if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
362 lua_createtable(L, 0, 2); /* create a hook table */ 371 /* table just created; initialize it */
363 lua_pushvalue(L, -1);
364 lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */
365 lua_pushstring(L, "k"); 372 lua_pushstring(L, "k");
366 lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ 373 lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
367 lua_pushvalue(L, -1); 374 lua_pushvalue(L, -1);
368 lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ 375 lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
369 } 376 }
370 checkstack(L, L1, 1); 377 checkstack(L, L1, 1);
371 lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ 378 lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
@@ -382,12 +389,14 @@ static int db_gethook (lua_State *L) {
382 char buff[5]; 389 char buff[5];
383 int mask = lua_gethookmask(L1); 390 int mask = lua_gethookmask(L1);
384 lua_Hook hook = lua_gethook(L1); 391 lua_Hook hook = lua_gethook(L1);
385 if (hook == NULL) /* no hook? */ 392 if (hook == NULL) { /* no hook? */
386 lua_pushnil(L); 393 luaL_pushfail(L);
394 return 1;
395 }
387 else if (hook != hookf) /* external hook? */ 396 else if (hook != hookf) /* external hook? */
388 lua_pushliteral(L, "external hook"); 397 lua_pushliteral(L, "external hook");
389 else { /* hook table must exist */ 398 else { /* hook table must exist */
390 lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); 399 lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
391 checkstack(L, L1, 1); 400 checkstack(L, L1, 1);
392 lua_pushthread(L1); lua_xmove(L1, L, 1); 401 lua_pushthread(L1); lua_xmove(L1, L, 1);
393 lua_rawget(L, -2); /* 1st result = hooktable[L1] */ 402 lua_rawget(L, -2); /* 1st result = hooktable[L1] */
@@ -408,7 +417,7 @@ static int db_debug (lua_State *L) {
408 return 0; 417 return 0;
409 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || 418 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
410 lua_pcall(L, 0, 0, 0)) 419 lua_pcall(L, 0, 0, 0))
411 lua_writestringerror("%s\n", lua_tostring(L, -1)); 420 lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
412 lua_settop(L, 0); /* remove eventual returns */ 421 lua_settop(L, 0); /* remove eventual returns */
413 } 422 }
414} 423}
@@ -428,6 +437,17 @@ static int db_traceback (lua_State *L) {
428} 437}
429 438
430 439
440static int db_setcstacklimit (lua_State *L) {
441 int limit = (int)luaL_checkinteger(L, 1);
442 int res = lua_setcstacklimit(L, limit);
443 if (res == 0)
444 lua_pushboolean(L, 0);
445 else
446 lua_pushinteger(L, res);
447 return 1;
448}
449
450
431static const luaL_Reg dblib[] = { 451static const luaL_Reg dblib[] = {
432 {"debug", db_debug}, 452 {"debug", db_debug},
433 {"getuservalue", db_getuservalue}, 453 {"getuservalue", db_getuservalue},
@@ -445,6 +465,7 @@ static const luaL_Reg dblib[] = {
445 {"setmetatable", db_setmetatable}, 465 {"setmetatable", db_setmetatable},
446 {"setupvalue", db_setupvalue}, 466 {"setupvalue", db_setupvalue},
447 {"traceback", db_traceback}, 467 {"traceback", db_traceback},
468 {"setcstacklimit", db_setcstacklimit},
448 {NULL, NULL} 469 {NULL, NULL}
449}; 470};
450 471