Mon Apr 17 20:33:12 2023 UTC ()
lua: apply ustream bugfix for "C-stack overflow with deep nesting of coroutine.close."


(nikita)
diff -r1.8 -r1.9 src/external/mit/lua/dist/src/lcorolib.c
diff -r1.10 -r1.11 src/external/mit/lua/dist/src/lstate.c
diff -r1.12 -r1.13 src/external/mit/lua/dist/src/lua.h

cvs diff -r1.8 -r1.9 src/external/mit/lua/dist/src/lcorolib.c (switch to unified diff)

--- src/external/mit/lua/dist/src/lcorolib.c 2023/04/16 20:46:17 1.8
+++ src/external/mit/lua/dist/src/lcorolib.c 2023/04/17 20:33:12 1.9
@@ -1,214 +1,214 @@ @@ -1,214 +1,214 @@
1/* $NetBSD: lcorolib.c,v 1.8 2023/04/16 20:46:17 nikita Exp $ */ 1/* $NetBSD: lcorolib.c,v 1.9 2023/04/17 20:33:12 nikita Exp $ */
2 2
3/* 3/*
4** Id: lcorolib.c  4** Id: lcorolib.c
5** Coroutine Library 5** Coroutine Library
6** See Copyright Notice in lua.h 6** See Copyright Notice in lua.h
7*/ 7*/
8 8
9#define lcorolib_c 9#define lcorolib_c
10#define LUA_LIB 10#define LUA_LIB
11 11
12#include "lprefix.h" 12#include "lprefix.h"
13 13
14 14
15#ifndef _KERNEL 15#ifndef _KERNEL
16#include <stdlib.h> 16#include <stdlib.h>
17#endif /* _KERNEL */ 17#endif /* _KERNEL */
18 18
19#include "lua.h" 19#include "lua.h"
20 20
21#include "lauxlib.h" 21#include "lauxlib.h"
22#include "lualib.h" 22#include "lualib.h"
23 23
24 24
25static lua_State *getco (lua_State *L) { 25static lua_State *getco (lua_State *L) {
26 lua_State *co = lua_tothread(L, 1); 26 lua_State *co = lua_tothread(L, 1);
27 luaL_argexpected(L, co, 1, "thread"); 27 luaL_argexpected(L, co, 1, "thread");
28 return co; 28 return co;
29} 29}
30 30
31 31
32/* 32/*
33** Resumes a coroutine. Returns the number of results for non-error 33** Resumes a coroutine. Returns the number of results for non-error
34** cases or -1 for errors. 34** cases or -1 for errors.
35*/ 35*/
36static int auxresume (lua_State *L, lua_State *co, int narg) { 36static int auxresume (lua_State *L, lua_State *co, int narg) {
37 int status, nres; 37 int status, nres;
38 if (l_unlikely(!lua_checkstack(co, narg))) { 38 if (l_unlikely(!lua_checkstack(co, narg))) {
39 lua_pushliteral(L, "too many arguments to resume"); 39 lua_pushliteral(L, "too many arguments to resume");
40 return -1; /* error flag */ 40 return -1; /* error flag */
41 } 41 }
42 lua_xmove(L, co, narg); 42 lua_xmove(L, co, narg);
43 status = lua_resume(co, L, narg, &nres); 43 status = lua_resume(co, L, narg, &nres);
44 if (l_likely(status == LUA_OK || status == LUA_YIELD)) { 44 if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
45 if (l_unlikely(!lua_checkstack(L, nres + 1))) { 45 if (l_unlikely(!lua_checkstack(L, nres + 1))) {
46 lua_pop(co, nres); /* remove results anyway */ 46 lua_pop(co, nres); /* remove results anyway */
47 lua_pushliteral(L, "too many results to resume"); 47 lua_pushliteral(L, "too many results to resume");
48 return -1; /* error flag */ 48 return -1; /* error flag */
49 } 49 }
50 lua_xmove(co, L, nres); /* move yielded values */ 50 lua_xmove(co, L, nres); /* move yielded values */
51 return nres; 51 return nres;
52 } 52 }
53 else { 53 else {
54 lua_xmove(co, L, 1); /* move error message */ 54 lua_xmove(co, L, 1); /* move error message */
55 return -1; /* error flag */ 55 return -1; /* error flag */
56 } 56 }
57} 57}
58 58
59 59
60static int luaB_coresume (lua_State *L) { 60static int luaB_coresume (lua_State *L) {
61 lua_State *co = getco(L); 61 lua_State *co = getco(L);
62 int r; 62 int r;
63 r = auxresume(L, co, lua_gettop(L) - 1); 63 r = auxresume(L, co, lua_gettop(L) - 1);
64 if (l_unlikely(r < 0)) { 64 if (l_unlikely(r < 0)) {
65 lua_pushboolean(L, 0); 65 lua_pushboolean(L, 0);
66 lua_insert(L, -2); 66 lua_insert(L, -2);
67 return 2; /* return false + error message */ 67 return 2; /* return false + error message */
68 } 68 }
69 else { 69 else {
70 lua_pushboolean(L, 1); 70 lua_pushboolean(L, 1);
71 lua_insert(L, -(r + 1)); 71 lua_insert(L, -(r + 1));
72 return r + 1; /* return true + 'resume' returns */ 72 return r + 1; /* return true + 'resume' returns */
73 } 73 }
74} 74}
75 75
76 76
77static int luaB_auxwrap (lua_State *L) { 77static int luaB_auxwrap (lua_State *L) {
78 lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 78 lua_State *co = lua_tothread(L, lua_upvalueindex(1));
79 int r = auxresume(L, co, lua_gettop(L)); 79 int r = auxresume(L, co, lua_gettop(L));
80 if (l_unlikely(r < 0)) { /* error? */ 80 if (l_unlikely(r < 0)) { /* error? */
81 int stat = lua_status(co); 81 int stat = lua_status(co);
82 if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ 82 if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
83 stat = lua_resetthread(co); /* close its tbc variables */ 83 stat = lua_resetthread(co, L); /* close its tbc variables */
84 lua_assert(stat != LUA_OK); 84 lua_assert(stat != LUA_OK);
85 lua_xmove(co, L, 1); /* move error message to the caller */ 85 lua_xmove(co, L, 1); /* move error message to the caller */
86 } 86 }
87 if (stat != LUA_ERRMEM && /* not a memory error and ... */ 87 if (stat != LUA_ERRMEM && /* not a memory error and ... */
88 lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ 88 lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
89 luaL_where(L, 1); /* add extra info, if available */ 89 luaL_where(L, 1); /* add extra info, if available */
90 lua_insert(L, -2); 90 lua_insert(L, -2);
91 lua_concat(L, 2); 91 lua_concat(L, 2);
92 } 92 }
93 return lua_error(L); /* propagate error */ 93 return lua_error(L); /* propagate error */
94 } 94 }
95 return r; 95 return r;
96} 96}
97 97
98 98
99static int luaB_cocreate (lua_State *L) { 99static int luaB_cocreate (lua_State *L) {
100 lua_State *NL; 100 lua_State *NL;
101 luaL_checktype(L, 1, LUA_TFUNCTION); 101 luaL_checktype(L, 1, LUA_TFUNCTION);
102 NL = lua_newthread(L); 102 NL = lua_newthread(L);
103 lua_pushvalue(L, 1); /* move function to top */ 103 lua_pushvalue(L, 1); /* move function to top */
104 lua_xmove(L, NL, 1); /* move function from L to NL */ 104 lua_xmove(L, NL, 1); /* move function from L to NL */
105 return 1; 105 return 1;
106} 106}
107 107
108 108
109static int luaB_cowrap (lua_State *L) { 109static int luaB_cowrap (lua_State *L) {
110 luaB_cocreate(L); 110 luaB_cocreate(L);
111 lua_pushcclosure(L, luaB_auxwrap, 1); 111 lua_pushcclosure(L, luaB_auxwrap, 1);
112 return 1; 112 return 1;
113} 113}
114 114
115 115
116static int luaB_yield (lua_State *L) { 116static int luaB_yield (lua_State *L) {
117 return lua_yield(L, lua_gettop(L)); 117 return lua_yield(L, lua_gettop(L));
118} 118}
119 119
120 120
121#define COS_RUN 0 121#define COS_RUN 0
122#define COS_DEAD 1 122#define COS_DEAD 1
123#define COS_YIELD 2 123#define COS_YIELD 2
124#define COS_NORM 3 124#define COS_NORM 3
125 125
126 126
127static const char *const statname[] = 127static const char *const statname[] =
128 {"running", "dead", "suspended", "normal"}; 128 {"running", "dead", "suspended", "normal"};
129 129
130 130
131static int auxstatus (lua_State *L, lua_State *co) { 131static int auxstatus (lua_State *L, lua_State *co) {
132 if (L == co) return COS_RUN; 132 if (L == co) return COS_RUN;
133 else { 133 else {
134 switch (lua_status(co)) { 134 switch (lua_status(co)) {
135 case LUA_YIELD: 135 case LUA_YIELD:
136 return COS_YIELD; 136 return COS_YIELD;
137 case LUA_OK: { 137 case LUA_OK: {
138 lua_Debug ar; 138 lua_Debug ar;
139 if (lua_getstack(co, 0, &ar)) /* does it have frames? */ 139 if (lua_getstack(co, 0, &ar)) /* does it have frames? */
140 return COS_NORM; /* it is running */ 140 return COS_NORM; /* it is running */
141 else if (lua_gettop(co) == 0) 141 else if (lua_gettop(co) == 0)
142 return COS_DEAD; 142 return COS_DEAD;
143 else 143 else
144 return COS_YIELD; /* initial state */ 144 return COS_YIELD; /* initial state */
145 } 145 }
146 default: /* some error occurred */ 146 default: /* some error occurred */
147 return COS_DEAD; 147 return COS_DEAD;
148 } 148 }
149 } 149 }
150} 150}
151 151
152 152
153static int luaB_costatus (lua_State *L) { 153static int luaB_costatus (lua_State *L) {
154 lua_State *co = getco(L); 154 lua_State *co = getco(L);
155 lua_pushstring(L, statname[auxstatus(L, co)]); 155 lua_pushstring(L, statname[auxstatus(L, co)]);
156 return 1; 156 return 1;
157} 157}
158 158
159 159
160static int luaB_yieldable (lua_State *L) { 160static int luaB_yieldable (lua_State *L) {
161 lua_State *co = lua_isnone(L, 1) ? L : getco(L); 161 lua_State *co = lua_isnone(L, 1) ? L : getco(L);
162 lua_pushboolean(L, lua_isyieldable(co)); 162 lua_pushboolean(L, lua_isyieldable(co));
163 return 1; 163 return 1;
164} 164}
165 165
166 166
167static int luaB_corunning (lua_State *L) { 167static int luaB_corunning (lua_State *L) {
168 int ismain = lua_pushthread(L); 168 int ismain = lua_pushthread(L);
169 lua_pushboolean(L, ismain); 169 lua_pushboolean(L, ismain);
170 return 2; 170 return 2;
171} 171}
172 172
173 173
174static int luaB_close (lua_State *L) { 174static int luaB_close (lua_State *L) {
175 lua_State *co = getco(L); 175 lua_State *co = getco(L);
176 int status = auxstatus(L, co); 176 int status = auxstatus(L, co);
177 switch (status) { 177 switch (status) {
178 case COS_DEAD: case COS_YIELD: { 178 case COS_DEAD: case COS_YIELD: {
179 status = lua_resetthread(co); 179 status = lua_resetthread(co, L);
180 if (status == LUA_OK) { 180 if (status == LUA_OK) {
181 lua_pushboolean(L, 1); 181 lua_pushboolean(L, 1);
182 return 1; 182 return 1;
183 } 183 }
184 else { 184 else {
185 lua_pushboolean(L, 0); 185 lua_pushboolean(L, 0);
186 lua_xmove(co, L, 1); /* move error message */ 186 lua_xmove(co, L, 1); /* move error message */
187 return 2; 187 return 2;
188 } 188 }
189 } 189 }
190 default: /* normal or running coroutine */ 190 default: /* normal or running coroutine */
191 return luaL_error(L, "cannot close a %s coroutine", statname[status]); 191 return luaL_error(L, "cannot close a %s coroutine", statname[status]);
192 } 192 }
193} 193}
194 194
195 195
196static const luaL_Reg co_funcs[] = { 196static const luaL_Reg co_funcs[] = {
197 {"create", luaB_cocreate}, 197 {"create", luaB_cocreate},
198 {"resume", luaB_coresume}, 198 {"resume", luaB_coresume},
199 {"running", luaB_corunning}, 199 {"running", luaB_corunning},
200 {"status", luaB_costatus}, 200 {"status", luaB_costatus},
201 {"wrap", luaB_cowrap}, 201 {"wrap", luaB_cowrap},
202 {"yield", luaB_yield}, 202 {"yield", luaB_yield},
203 {"isyieldable", luaB_yieldable}, 203 {"isyieldable", luaB_yieldable},
204 {"close", luaB_close}, 204 {"close", luaB_close},
205 {NULL, NULL} 205 {NULL, NULL}
206}; 206};
207 207
208 208
209 209
210LUAMOD_API int luaopen_coroutine (lua_State *L) { 210LUAMOD_API int luaopen_coroutine (lua_State *L) {
211 luaL_newlib(L, co_funcs); 211 luaL_newlib(L, co_funcs);
212 return 1; 212 return 1;
213} 213}
214 214

cvs diff -r1.10 -r1.11 src/external/mit/lua/dist/src/lstate.c (switch to unified diff)

--- src/external/mit/lua/dist/src/lstate.c 2023/04/16 20:46:17 1.10
+++ src/external/mit/lua/dist/src/lstate.c 2023/04/17 20:33:12 1.11
@@ -1,444 +1,445 @@ @@ -1,444 +1,445 @@
1/* $NetBSD: lstate.c,v 1.10 2023/04/16 20:46:17 nikita Exp $ */ 1/* $NetBSD: lstate.c,v 1.11 2023/04/17 20:33:12 nikita Exp $ */
2 2
3/* 3/*
4** Id: lstate.c  4** Id: lstate.c
5** Global State 5** Global State
6** See Copyright Notice in lua.h 6** See Copyright Notice in lua.h
7*/ 7*/
8 8
9#define lstate_c 9#define lstate_c
10#define LUA_CORE 10#define LUA_CORE
11 11
12#include "lprefix.h" 12#include "lprefix.h"
13 13
14 14
15#ifndef _KERNEL 15#ifndef _KERNEL
16#include <stddef.h> 16#include <stddef.h>
17#include <string.h> 17#include <string.h>
18#endif /* _KERNEL */ 18#endif /* _KERNEL */
19 19
20#include "lua.h" 20#include "lua.h"
21 21
22#include "lapi.h" 22#include "lapi.h"
23#include "ldebug.h" 23#include "ldebug.h"
24#include "ldo.h" 24#include "ldo.h"
25#include "lfunc.h" 25#include "lfunc.h"
26#include "lgc.h" 26#include "lgc.h"
27#include "llex.h" 27#include "llex.h"
28#include "lmem.h" 28#include "lmem.h"
29#include "lstate.h" 29#include "lstate.h"
30#include "lstring.h" 30#include "lstring.h"
31#include "ltable.h" 31#include "ltable.h"
32#include "ltm.h" 32#include "ltm.h"
33 33
34 34
35 35
36/* 36/*
37** thread state + extra space 37** thread state + extra space
38*/ 38*/
39typedef struct LX { 39typedef struct LX {
40 lu_byte extra_[LUA_EXTRASPACE]; 40 lu_byte extra_[LUA_EXTRASPACE];
41 lua_State l; 41 lua_State l;
42} LX; 42} LX;
43 43
44 44
45/* 45/*
46** Main thread combines a thread state and the global state 46** Main thread combines a thread state and the global state
47*/ 47*/
48typedef struct LG { 48typedef struct LG {
49 LX l; 49 LX l;
50 global_State g; 50 global_State g;
51} LG; 51} LG;
52 52
53 53
54 54
55#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) 55#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
56 56
57 57
58/* 58/*
59** A macro to create a "random" seed when a state is created; 59** A macro to create a "random" seed when a state is created;
60** the seed is used to randomize string hashes. 60** the seed is used to randomize string hashes.
61*/ 61*/
62#if !defined(luai_makeseed) 62#if !defined(luai_makeseed)
63 63
64#include <time.h> 64#include <time.h>
65 65
66/* 66/*
67** Compute an initial seed with some level of randomness. 67** Compute an initial seed with some level of randomness.
68** Rely on Address Space Layout Randomization (if present) and 68** Rely on Address Space Layout Randomization (if present) and
69** current time. 69** current time.
70*/ 70*/
71#define addbuff(b,p,e) \ 71#define addbuff(b,p,e) \
72 { size_t t = cast_sizet(e); \ 72 { size_t t = cast_sizet(e); \
73 memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } 73 memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
74 74
75static unsigned int luai_makeseed (lua_State *L) { 75static unsigned int luai_makeseed (lua_State *L) {
76 char buff[3 * sizeof(size_t)]; 76 char buff[3 * sizeof(size_t)];
77 unsigned int h = cast_uint(time(NULL)); 77 unsigned int h = cast_uint(time(NULL));
78 int p = 0; 78 int p = 0;
79 addbuff(buff, p, L); /* heap variable */ 79 addbuff(buff, p, L); /* heap variable */
80 addbuff(buff, p, &h); /* local variable */ 80 addbuff(buff, p, &h); /* local variable */
81 addbuff(buff, p, &lua_newstate); /* public function */ 81 addbuff(buff, p, &lua_newstate); /* public function */
82 lua_assert(p == sizeof(buff)); 82 lua_assert(p == sizeof(buff));
83 return luaS_hash(buff, p, h); 83 return luaS_hash(buff, p, h);
84} 84}
85 85
86#endif 86#endif
87 87
88 88
89/* 89/*
90** set GCdebt to a new value keeping the value (totalbytes + GCdebt) 90** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
91** invariant (and avoiding underflows in 'totalbytes') 91** invariant (and avoiding underflows in 'totalbytes')
92*/ 92*/
93void luaE_setdebt (global_State *g, l_mem debt) { 93void luaE_setdebt (global_State *g, l_mem debt) {
94 l_mem tb = gettotalbytes(g); 94 l_mem tb = gettotalbytes(g);
95 lua_assert(tb > 0); 95 lua_assert(tb > 0);
96 if (debt < tb - MAX_LMEM) 96 if (debt < tb - MAX_LMEM)
97 debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ 97 debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
98 g->totalbytes = tb - debt; 98 g->totalbytes = tb - debt;
99 g->GCdebt = debt; 99 g->GCdebt = debt;
100} 100}
101 101
102 102
103LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { 103LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
104 UNUSED(L); UNUSED(limit); 104 UNUSED(L); UNUSED(limit);
105 return LUAI_MAXCCALLS; /* warning?? */ 105 return LUAI_MAXCCALLS; /* warning?? */
106} 106}
107 107
108 108
109CallInfo *luaE_extendCI (lua_State *L) { 109CallInfo *luaE_extendCI (lua_State *L) {
110 CallInfo *ci; 110 CallInfo *ci;
111 lua_assert(L->ci->next == NULL); 111 lua_assert(L->ci->next == NULL);
112 ci = luaM_new(L, CallInfo); 112 ci = luaM_new(L, CallInfo);
113 lua_assert(L->ci->next == NULL); 113 lua_assert(L->ci->next == NULL);
114 L->ci->next = ci; 114 L->ci->next = ci;
115 ci->previous = L->ci; 115 ci->previous = L->ci;
116 ci->next = NULL; 116 ci->next = NULL;
117 ci->u.l.trap = 0; 117 ci->u.l.trap = 0;
118 L->nci++; 118 L->nci++;
119 return ci; 119 return ci;
120} 120}
121 121
122 122
123/* 123/*
124** free all CallInfo structures not in use by a thread 124** free all CallInfo structures not in use by a thread
125*/ 125*/
126void luaE_freeCI (lua_State *L) { 126void luaE_freeCI (lua_State *L) {
127 CallInfo *ci = L->ci; 127 CallInfo *ci = L->ci;
128 CallInfo *next = ci->next; 128 CallInfo *next = ci->next;
129 ci->next = NULL; 129 ci->next = NULL;
130 while ((ci = next) != NULL) { 130 while ((ci = next) != NULL) {
131 next = ci->next; 131 next = ci->next;
132 luaM_free(L, ci); 132 luaM_free(L, ci);
133 L->nci--; 133 L->nci--;
134 } 134 }
135} 135}
136 136
137 137
138/* 138/*
139** free half of the CallInfo structures not in use by a thread, 139** free half of the CallInfo structures not in use by a thread,
140** keeping the first one. 140** keeping the first one.
141*/ 141*/
142void luaE_shrinkCI (lua_State *L) { 142void luaE_shrinkCI (lua_State *L) {
143 CallInfo *ci = L->ci->next; /* first free CallInfo */ 143 CallInfo *ci = L->ci->next; /* first free CallInfo */
144 CallInfo *next; 144 CallInfo *next;
145 if (ci == NULL) 145 if (ci == NULL)
146 return; /* no extra elements */ 146 return; /* no extra elements */
147 while ((next = ci->next) != NULL) { /* two extra elements? */ 147 while ((next = ci->next) != NULL) { /* two extra elements? */
148 CallInfo *next2 = next->next; /* next's next */ 148 CallInfo *next2 = next->next; /* next's next */
149 ci->next = next2; /* remove next from the list */ 149 ci->next = next2; /* remove next from the list */
150 L->nci--; 150 L->nci--;
151 luaM_free(L, next); /* free next */ 151 luaM_free(L, next); /* free next */
152 if (next2 == NULL) 152 if (next2 == NULL)
153 break; /* no more elements */ 153 break; /* no more elements */
154 else { 154 else {
155 next2->previous = ci; 155 next2->previous = ci;
156 ci = next2; /* continue */ 156 ci = next2; /* continue */
157 } 157 }
158 } 158 }
159} 159}
160 160
161 161
162/* 162/*
163** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. 163** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
164** If equal, raises an overflow error. If value is larger than 164** If equal, raises an overflow error. If value is larger than
165** LUAI_MAXCCALLS (which means it is handling an overflow) but 165** LUAI_MAXCCALLS (which means it is handling an overflow) but
166** not much larger, does not report an error (to allow overflow 166** not much larger, does not report an error (to allow overflow
167** handling to work). 167** handling to work).
168*/ 168*/
169void luaE_checkcstack (lua_State *L) { 169void luaE_checkcstack (lua_State *L) {
170 if (getCcalls(L) == LUAI_MAXCCALLS) 170 if (getCcalls(L) == LUAI_MAXCCALLS)
171 luaG_runerror(L, "C stack overflow"); 171 luaG_runerror(L, "C stack overflow");
172 else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) 172 else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
173 luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ 173 luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
174} 174}
175 175
176 176
177LUAI_FUNC void luaE_incCstack (lua_State *L) { 177LUAI_FUNC void luaE_incCstack (lua_State *L) {
178 L->nCcalls++; 178 L->nCcalls++;
179 if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) 179 if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
180 luaE_checkcstack(L); 180 luaE_checkcstack(L);
181} 181}
182 182
183 183
184static void stack_init (lua_State *L1, lua_State *L) { 184static void stack_init (lua_State *L1, lua_State *L) {
185 int i; CallInfo *ci; 185 int i; CallInfo *ci;
186 /* initialize stack array */ 186 /* initialize stack array */
187 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); 187 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
188 L1->tbclist = L1->stack; 188 L1->tbclist = L1->stack;
189 for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) 189 for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
190 setnilvalue(s2v(L1->stack + i)); /* erase new stack */ 190 setnilvalue(s2v(L1->stack + i)); /* erase new stack */
191 L1->top = L1->stack; 191 L1->top = L1->stack;
192 L1->stack_last = L1->stack + BASIC_STACK_SIZE; 192 L1->stack_last = L1->stack + BASIC_STACK_SIZE;
193 /* initialize first ci */ 193 /* initialize first ci */
194 ci = &L1->base_ci; 194 ci = &L1->base_ci;
195 ci->next = ci->previous = NULL; 195 ci->next = ci->previous = NULL;
196 ci->callstatus = CIST_C; 196 ci->callstatus = CIST_C;
197 ci->func = L1->top; 197 ci->func = L1->top;
198 ci->u.c.k = NULL; 198 ci->u.c.k = NULL;
199 ci->nresults = 0; 199 ci->nresults = 0;
200 setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ 200 setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */
201 L1->top++; 201 L1->top++;
202 ci->top = L1->top + LUA_MINSTACK; 202 ci->top = L1->top + LUA_MINSTACK;
203 L1->ci = ci; 203 L1->ci = ci;
204} 204}
205 205
206 206
207static void freestack (lua_State *L) { 207static void freestack (lua_State *L) {
208 if (L->stack == NULL) 208 if (L->stack == NULL)
209 return; /* stack not completely built yet */ 209 return; /* stack not completely built yet */
210 L->ci = &L->base_ci; /* free the entire 'ci' list */ 210 L->ci = &L->base_ci; /* free the entire 'ci' list */
211 luaE_freeCI(L); 211 luaE_freeCI(L);
212 lua_assert(L->nci == 0); 212 lua_assert(L->nci == 0);
213 luaM_freearray(L, L->stack, stacksize(L) + EXTRA_STACK); /* free stack */ 213 luaM_freearray(L, L->stack, stacksize(L) + EXTRA_STACK); /* free stack */
214} 214}
215 215
216 216
217/* 217/*
218** Create registry table and its predefined values 218** Create registry table and its predefined values
219*/ 219*/
220static void init_registry (lua_State *L, global_State *g) { 220static void init_registry (lua_State *L, global_State *g) {
221 /* create registry */ 221 /* create registry */
222 Table *registry = luaH_new(L); 222 Table *registry = luaH_new(L);
223 sethvalue(L, &g->l_registry, registry); 223 sethvalue(L, &g->l_registry, registry);
224 luaH_resize(L, registry, LUA_RIDX_LAST, 0); 224 luaH_resize(L, registry, LUA_RIDX_LAST, 0);
225 /* registry[LUA_RIDX_MAINTHREAD] = L */ 225 /* registry[LUA_RIDX_MAINTHREAD] = L */
226 setthvalue(L, &registry->array[LUA_RIDX_MAINTHREAD - 1], L); 226 setthvalue(L, &registry->array[LUA_RIDX_MAINTHREAD - 1], L);
227 /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ 227 /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
228 sethvalue(L, &registry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L)); 228 sethvalue(L, &registry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L));
229} 229}
230 230
231 231
232/* 232/*
233** open parts of the state that may cause memory-allocation errors. 233** open parts of the state that may cause memory-allocation errors.
234*/ 234*/
235static void f_luaopen (lua_State *L, void *ud) { 235static void f_luaopen (lua_State *L, void *ud) {
236 global_State *g = G(L); 236 global_State *g = G(L);
237 UNUSED(ud); 237 UNUSED(ud);
238 stack_init(L, L); /* init stack */ 238 stack_init(L, L); /* init stack */
239 init_registry(L, g); 239 init_registry(L, g);
240 luaS_init(L); 240 luaS_init(L);
241 luaT_init(L); 241 luaT_init(L);
242 luaX_init(L); 242 luaX_init(L);
243 g->gcstp = 0; /* allow gc */ 243 g->gcstp = 0; /* allow gc */
244 setnilvalue(&g->nilvalue); /* now state is complete */ 244 setnilvalue(&g->nilvalue); /* now state is complete */
245 luai_userstateopen(L); 245 luai_userstateopen(L);
246} 246}
247 247
248 248
249/* 249/*
250** preinitialize a thread with consistent values without allocating 250** preinitialize a thread with consistent values without allocating
251** any memory (to avoid errors) 251** any memory (to avoid errors)
252*/ 252*/
253static void preinit_thread (lua_State *L, global_State *g) { 253static void preinit_thread (lua_State *L, global_State *g) {
254 G(L) = g; 254 G(L) = g;
255 L->stack = NULL; 255 L->stack = NULL;
256 L->ci = NULL; 256 L->ci = NULL;
257 L->nci = 0; 257 L->nci = 0;
258 L->twups = L; /* thread has no upvalues */ 258 L->twups = L; /* thread has no upvalues */
259 L->nCcalls = 0; 259 L->nCcalls = 0;
260 L->errorJmp = NULL; 260 L->errorJmp = NULL;
261 L->hook = NULL; 261 L->hook = NULL;
262 L->hookmask = 0; 262 L->hookmask = 0;
263 L->basehookcount = 0; 263 L->basehookcount = 0;
264 L->allowhook = 1; 264 L->allowhook = 1;
265 resethookcount(L); 265 resethookcount(L);
266 L->openupval = NULL; 266 L->openupval = NULL;
267 L->status = LUA_OK; 267 L->status = LUA_OK;
268 L->errfunc = 0; 268 L->errfunc = 0;
269 L->oldpc = 0; 269 L->oldpc = 0;
270} 270}
271 271
272 272
273static void close_state (lua_State *L) { 273static void close_state (lua_State *L) {
274 global_State *g = G(L); 274 global_State *g = G(L);
275 if (!completestate(g)) /* closing a partially built state? */ 275 if (!completestate(g)) /* closing a partially built state? */
276 luaC_freeallobjects(L); /* just collect its objects */ 276 luaC_freeallobjects(L); /* just collect its objects */
277 else { /* closing a fully built state */ 277 else { /* closing a fully built state */
278 L->ci = &L->base_ci; /* unwind CallInfo list */ 278 L->ci = &L->base_ci; /* unwind CallInfo list */
279 luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ 279 luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */
280 luaC_freeallobjects(L); /* collect all objects */ 280 luaC_freeallobjects(L); /* collect all objects */
281 luai_userstateclose(L); 281 luai_userstateclose(L);
282 } 282 }
283 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); 283 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
284 freestack(L); 284 freestack(L);
285 lua_assert(gettotalbytes(g) == sizeof(LG)); 285 lua_assert(gettotalbytes(g) == sizeof(LG));
286 (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ 286 (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
287} 287}
288 288
289 289
290LUA_API lua_State *lua_newthread (lua_State *L) { 290LUA_API lua_State *lua_newthread (lua_State *L) {
291 global_State *g; 291 global_State *g;
292 lua_State *L1; 292 lua_State *L1;
293 lua_lock(L); 293 lua_lock(L);
294 g = G(L); 294 g = G(L);
295 luaC_checkGC(L); 295 luaC_checkGC(L);
296 /* create new thread */ 296 /* create new thread */
297 L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; 297 L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
298 L1->marked = luaC_white(g); 298 L1->marked = luaC_white(g);
299 L1->tt = LUA_VTHREAD; 299 L1->tt = LUA_VTHREAD;
300 /* link it on list 'allgc' */ 300 /* link it on list 'allgc' */
301 L1->next = g->allgc; 301 L1->next = g->allgc;
302 g->allgc = obj2gco(L1); 302 g->allgc = obj2gco(L1);
303 /* anchor it on L stack */ 303 /* anchor it on L stack */
304 setthvalue2s(L, L->top, L1); 304 setthvalue2s(L, L->top, L1);
305 api_incr_top(L); 305 api_incr_top(L);
306 preinit_thread(L1, g); 306 preinit_thread(L1, g);
307 L1->hookmask = L->hookmask; 307 L1->hookmask = L->hookmask;
308 L1->basehookcount = L->basehookcount; 308 L1->basehookcount = L->basehookcount;
309 L1->hook = L->hook; 309 L1->hook = L->hook;
310 resethookcount(L1); 310 resethookcount(L1);
311 /* initialize L1 extra space */ 311 /* initialize L1 extra space */
312 memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), 312 memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
313 LUA_EXTRASPACE); 313 LUA_EXTRASPACE);
314 luai_userstatethread(L, L1); 314 luai_userstatethread(L, L1);
315 stack_init(L1, L); /* init stack */ 315 stack_init(L1, L); /* init stack */
316 lua_unlock(L); 316 lua_unlock(L);
317 return L1; 317 return L1;
318} 318}
319 319
320 320
321void luaE_freethread (lua_State *L, lua_State *L1) { 321void luaE_freethread (lua_State *L, lua_State *L1) {
322 LX *l = fromstate(L1); 322 LX *l = fromstate(L1);
323 luaF_closeupval(L1, L1->stack); /* close all upvalues */ 323 luaF_closeupval(L1, L1->stack); /* close all upvalues */
324 lua_assert(L1->openupval == NULL); 324 lua_assert(L1->openupval == NULL);
325 luai_userstatefree(L, L1); 325 luai_userstatefree(L, L1);
326 freestack(L1); 326 freestack(L1);
327 luaM_free(L, l); 327 luaM_free(L, l);
328} 328}
329 329
330 330
331int luaE_resetthread (lua_State *L, int status) { 331int luaE_resetthread (lua_State *L, int status) {
332 CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ 332 CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */
333 setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ 333 setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */
334 ci->func = L->stack; 334 ci->func = L->stack;
335 ci->callstatus = CIST_C; 335 ci->callstatus = CIST_C;
336 if (status == LUA_YIELD) 336 if (status == LUA_YIELD)
337 status = LUA_OK; 337 status = LUA_OK;
338 L->status = LUA_OK; /* so it can run __close metamethods */ 338 L->status = LUA_OK; /* so it can run __close metamethods */
339 status = luaD_closeprotected(L, 1, status); 339 status = luaD_closeprotected(L, 1, status);
340 if (status != LUA_OK) /* errors? */ 340 if (status != LUA_OK) /* errors? */
341 luaD_seterrorobj(L, status, L->stack + 1); 341 luaD_seterrorobj(L, status, L->stack + 1);
342 else 342 else
343 L->top = L->stack + 1; 343 L->top = L->stack + 1;
344 ci->top = L->top + LUA_MINSTACK; 344 ci->top = L->top + LUA_MINSTACK;
345 luaD_reallocstack(L, cast_int(ci->top - L->stack), 0); 345 luaD_reallocstack(L, cast_int(ci->top - L->stack), 0);
346 return status; 346 return status;
347} 347}
348 348
349 349
350LUA_API int lua_resetthread (lua_State *L) { 350LUA_API int lua_resetthread (lua_State *L, lua_State *from) {
351 int status; 351 int status;
352 lua_lock(L); 352 lua_lock(L);
 353 L->nCcalls = (from) ? getCcalls(from) : 0;
353 status = luaE_resetthread(L, L->status); 354 status = luaE_resetthread(L, L->status);
354 lua_unlock(L); 355 lua_unlock(L);
355 return status; 356 return status;
356} 357}
357 358
358 359
359LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 360LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
360 int i; 361 int i;
361 lua_State *L; 362 lua_State *L;
362 global_State *g; 363 global_State *g;
363 LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); 364 LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
364 if (l == NULL) return NULL; 365 if (l == NULL) return NULL;
365 L = &l->l.l; 366 L = &l->l.l;
366 g = &l->g; 367 g = &l->g;
367 L->tt = LUA_VTHREAD; 368 L->tt = LUA_VTHREAD;
368 g->currentwhite = bitmask(WHITE0BIT); 369 g->currentwhite = bitmask(WHITE0BIT);
369 L->marked = luaC_white(g); 370 L->marked = luaC_white(g);
370 preinit_thread(L, g); 371 preinit_thread(L, g);
371 g->allgc = obj2gco(L); /* by now, only object is the main thread */ 372 g->allgc = obj2gco(L); /* by now, only object is the main thread */
372 L->next = NULL; 373 L->next = NULL;
373 incnny(L); /* main thread is always non yieldable */ 374 incnny(L); /* main thread is always non yieldable */
374 g->frealloc = f; 375 g->frealloc = f;
375 g->ud = ud; 376 g->ud = ud;
376 g->warnf = NULL; 377 g->warnf = NULL;
377 g->ud_warn = NULL; 378 g->ud_warn = NULL;
378 g->mainthread = L; 379 g->mainthread = L;
379 g->seed = luai_makeseed(L); 380 g->seed = luai_makeseed(L);
380 g->gcstp = GCSTPGC; /* no GC while building state */ 381 g->gcstp = GCSTPGC; /* no GC while building state */
381 g->strt.size = g->strt.nuse = 0; 382 g->strt.size = g->strt.nuse = 0;
382 g->strt.hash = NULL; 383 g->strt.hash = NULL;
383 setnilvalue(&g->l_registry); 384 setnilvalue(&g->l_registry);
384 g->panic = NULL; 385 g->panic = NULL;
385 g->gcstate = GCSpause; 386 g->gcstate = GCSpause;
386 g->gckind = KGC_INC; 387 g->gckind = KGC_INC;
387 g->gcstopem = 0; 388 g->gcstopem = 0;
388 g->gcemergency = 0; 389 g->gcemergency = 0;
389 g->finobj = g->tobefnz = g->fixedgc = NULL; 390 g->finobj = g->tobefnz = g->fixedgc = NULL;
390 g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; 391 g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
391 g->finobjsur = g->finobjold1 = g->finobjrold = NULL; 392 g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
392 g->sweepgc = NULL; 393 g->sweepgc = NULL;
393 g->gray = g->grayagain = NULL; 394 g->gray = g->grayagain = NULL;
394 g->weak = g->ephemeron = g->allweak = NULL; 395 g->weak = g->ephemeron = g->allweak = NULL;
395 g->twups = NULL; 396 g->twups = NULL;
396 g->totalbytes = sizeof(LG); 397 g->totalbytes = sizeof(LG);
397 g->GCdebt = 0; 398 g->GCdebt = 0;
398 g->lastatomic = 0; 399 g->lastatomic = 0;
399 setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ 400 setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
400 setgcparam(g->gcpause, LUAI_GCPAUSE); 401 setgcparam(g->gcpause, LUAI_GCPAUSE);
401 setgcparam(g->gcstepmul, LUAI_GCMUL); 402 setgcparam(g->gcstepmul, LUAI_GCMUL);
402 g->gcstepsize = LUAI_GCSTEPSIZE; 403 g->gcstepsize = LUAI_GCSTEPSIZE;
403 setgcparam(g->genmajormul, LUAI_GENMAJORMUL); 404 setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
404 g->genminormul = LUAI_GENMINORMUL; 405 g->genminormul = LUAI_GENMINORMUL;
405 for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; 406 for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
406 if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { 407 if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
407 /* memory allocation error: free partial state */ 408 /* memory allocation error: free partial state */
408 close_state(L); 409 close_state(L);
409 L = NULL; 410 L = NULL;
410 } 411 }
411 return L; 412 return L;
412} 413}
413 414
414 415
415LUA_API void lua_close (lua_State *L) { 416LUA_API void lua_close (lua_State *L) {
416 lua_lock(L); 417 lua_lock(L);
417 L = G(L)->mainthread; /* only the main thread can be closed */ 418 L = G(L)->mainthread; /* only the main thread can be closed */
418 close_state(L); 419 close_state(L);
419} 420}
420 421
421 422
422void luaE_warning (lua_State *L, const char *msg, int tocont) { 423void luaE_warning (lua_State *L, const char *msg, int tocont) {
423 lua_WarnFunction wf = G(L)->warnf; 424 lua_WarnFunction wf = G(L)->warnf;
424 if (wf != NULL) 425 if (wf != NULL)
425 wf(G(L)->ud_warn, msg, tocont); 426 wf(G(L)->ud_warn, msg, tocont);
426} 427}
427 428
428 429
429/* 430/*
430** Generate a warning from an error message 431** Generate a warning from an error message
431*/ 432*/
432void luaE_warnerror (lua_State *L, const char *where) { 433void luaE_warnerror (lua_State *L, const char *where) {
433 TValue *errobj = s2v(L->top - 1); /* error object */ 434 TValue *errobj = s2v(L->top - 1); /* error object */
434 const char *msg = (ttisstring(errobj)) 435 const char *msg = (ttisstring(errobj))
435 ? svalue(errobj) 436 ? svalue(errobj)
436 : "error object is not a string"; 437 : "error object is not a string";
437 /* produce warning "error in %s (%s)" (where, msg) */ 438 /* produce warning "error in %s (%s)" (where, msg) */
438 luaE_warning(L, "error in ", 1); 439 luaE_warning(L, "error in ", 1);
439 luaE_warning(L, where, 1); 440 luaE_warning(L, where, 1);
440 luaE_warning(L, " (", 1); 441 luaE_warning(L, " (", 1);
441 luaE_warning(L, msg, 1); 442 luaE_warning(L, msg, 1);
442 luaE_warning(L, ")", 0); 443 luaE_warning(L, ")", 0);
443} 444}
444 445

cvs diff -r1.12 -r1.13 src/external/mit/lua/dist/src/lua.h (switch to unified diff)

--- src/external/mit/lua/dist/src/lua.h 2023/04/16 20:46:17 1.12
+++ src/external/mit/lua/dist/src/lua.h 2023/04/17 20:33:12 1.13
@@ -1,544 +1,544 @@ @@ -1,544 +1,544 @@
1/* $NetBSD: lua.h,v 1.12 2023/04/16 20:46:17 nikita Exp $ */ 1/* $NetBSD: lua.h,v 1.13 2023/04/17 20:33:12 nikita Exp $ */
2 2
3/* 3/*
4** Id: lua.h  4** Id: lua.h
5** Lua - A Scripting Language 5** Lua - A Scripting Language
6** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 6** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
7** See Copyright Notice at the end of this file 7** See Copyright Notice at the end of this file
8*/ 8*/
9 9
10 10
11#ifndef lua_h 11#ifndef lua_h
12#define lua_h 12#define lua_h
13 13
14#include <stdarg.h> 14#include <stdarg.h>
15#ifndef _KERNEL 15#ifndef _KERNEL
16#include <stddef.h> 16#include <stddef.h>
17#endif /* _KERNEL */ 17#endif /* _KERNEL */
18 18
19 19
20#include "luaconf.h" 20#include "luaconf.h"
21 21
22 22
23#define LUA_VERSION_MAJOR "5" 23#define LUA_VERSION_MAJOR "5"
24#define LUA_VERSION_MINOR "4" 24#define LUA_VERSION_MINOR "4"
25#define LUA_VERSION_RELEASE "4" 25#define LUA_VERSION_RELEASE "4"
26 26
27#define LUA_VERSION_NUM 504 27#define LUA_VERSION_NUM 504
28#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 4) 28#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 4)
29 29
30#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR 30#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
31#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE 31#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
32#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2022 Lua.org, PUC-Rio" 32#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2022 Lua.org, PUC-Rio"
33#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" 33#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
34 34
35 35
36/* mark for precompiled code ('<esc>Lua') */ 36/* mark for precompiled code ('<esc>Lua') */
37#define LUA_SIGNATURE "\x1bLua" 37#define LUA_SIGNATURE "\x1bLua"
38 38
39/* option for multiple returns in 'lua_pcall' and 'lua_call' */ 39/* option for multiple returns in 'lua_pcall' and 'lua_call' */
40#define LUA_MULTRET (-1) 40#define LUA_MULTRET (-1)
41 41
42 42
43/* 43/*
44** Pseudo-indices 44** Pseudo-indices
45** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty 45** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty
46** space after that to help overflow detection) 46** space after that to help overflow detection)
47*/ 47*/
48#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) 48#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000)
49#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) 49#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
50 50
51 51
52/* thread status */ 52/* thread status */
53#define LUA_OK 0 53#define LUA_OK 0
54#define LUA_YIELD 1 54#define LUA_YIELD 1
55#define LUA_ERRRUN 2 55#define LUA_ERRRUN 2
56#define LUA_ERRSYNTAX 3 56#define LUA_ERRSYNTAX 3
57#define LUA_ERRMEM 4 57#define LUA_ERRMEM 4
58#define LUA_ERRERR 5 58#define LUA_ERRERR 5
59 59
60 60
61typedef struct lua_State lua_State; 61typedef struct lua_State lua_State;
62 62
63 63
64/* 64/*
65** basic types 65** basic types
66*/ 66*/
67#define LUA_TNONE (-1) 67#define LUA_TNONE (-1)
68 68
69#define LUA_TNIL 0 69#define LUA_TNIL 0
70#define LUA_TBOOLEAN 1 70#define LUA_TBOOLEAN 1
71#define LUA_TLIGHTUSERDATA 2 71#define LUA_TLIGHTUSERDATA 2
72#define LUA_TNUMBER 3 72#define LUA_TNUMBER 3
73#define LUA_TSTRING 4 73#define LUA_TSTRING 4
74#define LUA_TTABLE 5 74#define LUA_TTABLE 5
75#define LUA_TFUNCTION 6 75#define LUA_TFUNCTION 6
76#define LUA_TUSERDATA 7 76#define LUA_TUSERDATA 7
77#define LUA_TTHREAD 8 77#define LUA_TTHREAD 8
78 78
79#define LUA_NUMTYPES 9 79#define LUA_NUMTYPES 9
80 80
81 81
82 82
83/* minimum Lua stack available to a C function */ 83/* minimum Lua stack available to a C function */
84#define LUA_MINSTACK 20 84#define LUA_MINSTACK 20
85 85
86 86
87/* predefined values in the registry */ 87/* predefined values in the registry */
88#define LUA_RIDX_MAINTHREAD 1 88#define LUA_RIDX_MAINTHREAD 1
89#define LUA_RIDX_GLOBALS 2 89#define LUA_RIDX_GLOBALS 2
90#define LUA_RIDX_LAST LUA_RIDX_GLOBALS 90#define LUA_RIDX_LAST LUA_RIDX_GLOBALS
91 91
92 92
93/* type of numbers in Lua */ 93/* type of numbers in Lua */
94typedef LUA_NUMBER lua_Number; 94typedef LUA_NUMBER lua_Number;
95 95
96 96
97/* type for integer functions */ 97/* type for integer functions */
98typedef LUA_INTEGER lua_Integer; 98typedef LUA_INTEGER lua_Integer;
99 99
100/* unsigned integer type */ 100/* unsigned integer type */
101typedef LUA_UNSIGNED lua_Unsigned; 101typedef LUA_UNSIGNED lua_Unsigned;
102 102
103/* type for continuation-function contexts */ 103/* type for continuation-function contexts */
104typedef LUA_KCONTEXT lua_KContext; 104typedef LUA_KCONTEXT lua_KContext;
105 105
106 106
107/* 107/*
108** Type for C functions registered with Lua 108** Type for C functions registered with Lua
109*/ 109*/
110typedef int (*lua_CFunction) (lua_State *L); 110typedef int (*lua_CFunction) (lua_State *L);
111 111
112/* 112/*
113** Type for continuation functions 113** Type for continuation functions
114*/ 114*/
115typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); 115typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
116 116
117 117
118/* 118/*
119** Type for functions that read/write blocks when loading/dumping Lua chunks 119** Type for functions that read/write blocks when loading/dumping Lua chunks
120*/ 120*/
121typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); 121typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
122 122
123typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); 123typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud);
124 124
125 125
126/* 126/*
127** Type for memory-allocation functions 127** Type for memory-allocation functions
128*/ 128*/
129typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); 129typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
130 130
131 131
132/* 132/*
133** Type for warning functions 133** Type for warning functions
134*/ 134*/
135typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); 135typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);
136 136
137 137
138 138
139 139
140/* 140/*
141** generic extra include file 141** generic extra include file
142*/ 142*/
143#if defined(LUA_USER_H) 143#if defined(LUA_USER_H)
144#include LUA_USER_H 144#include LUA_USER_H
145#endif 145#endif
146 146
147 147
148/* 148/*
149** RCS ident string 149** RCS ident string
150*/ 150*/
151extern const char lua_ident[]; 151extern const char lua_ident[];
152 152
153 153
154/* 154/*
155** state manipulation 155** state manipulation
156*/ 156*/
157LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); 157LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
158LUA_API void (lua_close) (lua_State *L); 158LUA_API void (lua_close) (lua_State *L);
159LUA_API lua_State *(lua_newthread) (lua_State *L); 159LUA_API lua_State *(lua_newthread) (lua_State *L);
160LUA_API int (lua_resetthread) (lua_State *L); 160LUA_API int (lua_resetthread) (lua_State *L, lua_State *from);
161 161
162LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); 162LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
163 163
164 164
165LUA_API lua_Number (lua_version) (lua_State *L); 165LUA_API lua_Number (lua_version) (lua_State *L);
166 166
167 167
168/* 168/*
169** basic stack manipulation 169** basic stack manipulation
170*/ 170*/
171LUA_API int (lua_absindex) (lua_State *L, int idx); 171LUA_API int (lua_absindex) (lua_State *L, int idx);
172LUA_API int (lua_gettop) (lua_State *L); 172LUA_API int (lua_gettop) (lua_State *L);
173LUA_API void (lua_settop) (lua_State *L, int idx); 173LUA_API void (lua_settop) (lua_State *L, int idx);
174LUA_API void (lua_pushvalue) (lua_State *L, int idx); 174LUA_API void (lua_pushvalue) (lua_State *L, int idx);
175LUA_API void (lua_rotate) (lua_State *L, int idx, int n); 175LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
176LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); 176LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
177LUA_API int (lua_checkstack) (lua_State *L, int n); 177LUA_API int (lua_checkstack) (lua_State *L, int n);
178 178
179LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); 179LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
180 180
181 181
182/* 182/*
183** access functions (stack -> C) 183** access functions (stack -> C)
184*/ 184*/
185 185
186LUA_API int (lua_isnumber) (lua_State *L, int idx); 186LUA_API int (lua_isnumber) (lua_State *L, int idx);
187LUA_API int (lua_isstring) (lua_State *L, int idx); 187LUA_API int (lua_isstring) (lua_State *L, int idx);
188LUA_API int (lua_iscfunction) (lua_State *L, int idx); 188LUA_API int (lua_iscfunction) (lua_State *L, int idx);
189LUA_API int (lua_isinteger) (lua_State *L, int idx); 189LUA_API int (lua_isinteger) (lua_State *L, int idx);
190LUA_API int (lua_isuserdata) (lua_State *L, int idx); 190LUA_API int (lua_isuserdata) (lua_State *L, int idx);
191LUA_API int (lua_type) (lua_State *L, int idx); 191LUA_API int (lua_type) (lua_State *L, int idx);
192LUA_API const char *(lua_typename) (lua_State *L, int tp); 192LUA_API const char *(lua_typename) (lua_State *L, int tp);
193 193
194#ifndef _KERNEL 194#ifndef _KERNEL
195LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); 195LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
196#else /* _KERNEL */ 196#else /* _KERNEL */
197#define lua_tonumberx (lua_Integer) lua_tointegerx 197#define lua_tonumberx (lua_Integer) lua_tointegerx
198#endif /* _KERNEL */ 198#endif /* _KERNEL */
199LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); 199LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
200LUA_API int (lua_toboolean) (lua_State *L, int idx); 200LUA_API int (lua_toboolean) (lua_State *L, int idx);
201LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); 201LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
202LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx); 202LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx);
203LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); 203LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
204LUA_API void *(lua_touserdata) (lua_State *L, int idx); 204LUA_API void *(lua_touserdata) (lua_State *L, int idx);
205LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); 205LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
206LUA_API const void *(lua_topointer) (lua_State *L, int idx); 206LUA_API const void *(lua_topointer) (lua_State *L, int idx);
207 207
208 208
209/* 209/*
210** Comparison and arithmetic functions 210** Comparison and arithmetic functions
211*/ 211*/
212 212
213#define LUA_OPADD 0 /* ORDER TM, ORDER OP */ 213#define LUA_OPADD 0 /* ORDER TM, ORDER OP */
214#define LUA_OPSUB 1 214#define LUA_OPSUB 1
215#define LUA_OPMUL 2 215#define LUA_OPMUL 2
216#define LUA_OPMOD 3 216#define LUA_OPMOD 3
217#ifndef _KERNEL 217#ifndef _KERNEL
218#define LUA_OPPOW 4 218#define LUA_OPPOW 4
219#define LUA_OPDIV 5 219#define LUA_OPDIV 5
220#define LUA_OPIDIV 6 220#define LUA_OPIDIV 6
221#define LUA_OPBAND 7 221#define LUA_OPBAND 7
222#define LUA_OPBOR 8 222#define LUA_OPBOR 8
223#define LUA_OPBXOR 9 223#define LUA_OPBXOR 9
224#define LUA_OPSHL 10 224#define LUA_OPSHL 10
225#define LUA_OPSHR 11 225#define LUA_OPSHR 11
226#define LUA_OPUNM 12 226#define LUA_OPUNM 12
227#define LUA_OPBNOT 13 227#define LUA_OPBNOT 13
228#else /* _KERNEL */ 228#else /* _KERNEL */
229#define LUA_OPIDIV 4 229#define LUA_OPIDIV 4
230#define LUA_OPBAND 5 230#define LUA_OPBAND 5
231#define LUA_OPBOR 6 231#define LUA_OPBOR 6
232#define LUA_OPBXOR 7 232#define LUA_OPBXOR 7
233#define LUA_OPSHL 8 233#define LUA_OPSHL 8
234#define LUA_OPSHR 9 234#define LUA_OPSHR 9
235#define LUA_OPUNM 10 235#define LUA_OPUNM 10
236#define LUA_OPBNOT 11 236#define LUA_OPBNOT 11
237#endif /* _KERNEL */ 237#endif /* _KERNEL */
238 238
239LUA_API void (lua_arith) (lua_State *L, int op); 239LUA_API void (lua_arith) (lua_State *L, int op);
240 240
241#define LUA_OPEQ 0 241#define LUA_OPEQ 0
242#define LUA_OPLT 1 242#define LUA_OPLT 1
243#define LUA_OPLE 2 243#define LUA_OPLE 2
244 244
245LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); 245LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
246LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); 246LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op);
247 247
248 248
249/* 249/*
250** push functions (C -> stack) 250** push functions (C -> stack)
251*/ 251*/
252LUA_API void (lua_pushnil) (lua_State *L); 252LUA_API void (lua_pushnil) (lua_State *L);
253#ifndef _KERNEL 253#ifndef _KERNEL
254LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); 254LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
255#else /* _KERNEL */ 255#else /* _KERNEL */
256#define lua_pushnumber(L, n) lua_pushinteger(L, (lua_Integer)(n)) 256#define lua_pushnumber(L, n) lua_pushinteger(L, (lua_Integer)(n))
257#endif /* _KERNEL */ 257#endif /* _KERNEL */
258LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); 258LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
259LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); 259LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len);
260LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); 260LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
261LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, 261LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
262 va_list argp); 262 va_list argp);
263LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); 263LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
264LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); 264LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
265LUA_API void (lua_pushboolean) (lua_State *L, int b); 265LUA_API void (lua_pushboolean) (lua_State *L, int b);
266LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); 266LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
267LUA_API int (lua_pushthread) (lua_State *L); 267LUA_API int (lua_pushthread) (lua_State *L);
268 268
269 269
270/* 270/*
271** get functions (Lua -> stack) 271** get functions (Lua -> stack)
272*/ 272*/
273LUA_API int (lua_getglobal) (lua_State *L, const char *name); 273LUA_API int (lua_getglobal) (lua_State *L, const char *name);
274LUA_API int (lua_gettable) (lua_State *L, int idx); 274LUA_API int (lua_gettable) (lua_State *L, int idx);
275LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); 275LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
276LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); 276LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n);
277LUA_API int (lua_rawget) (lua_State *L, int idx); 277LUA_API int (lua_rawget) (lua_State *L, int idx);
278LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); 278LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
279LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); 279LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
280 280
281LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); 281LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
282LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); 282LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
283LUA_API int (lua_getmetatable) (lua_State *L, int objindex); 283LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
284LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n); 284LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n);
285 285
286 286
287/* 287/*
288** set functions (stack -> Lua) 288** set functions (stack -> Lua)
289*/ 289*/
290LUA_API void (lua_setglobal) (lua_State *L, const char *name); 290LUA_API void (lua_setglobal) (lua_State *L, const char *name);
291LUA_API void (lua_settable) (lua_State *L, int idx); 291LUA_API void (lua_settable) (lua_State *L, int idx);
292LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); 292LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
293LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); 293LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n);
294LUA_API void (lua_rawset) (lua_State *L, int idx); 294LUA_API void (lua_rawset) (lua_State *L, int idx);
295LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); 295LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n);
296LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); 296LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
297LUA_API int (lua_setmetatable) (lua_State *L, int objindex); 297LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
298LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n); 298LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n);
299 299
300 300
301/* 301/*
302** 'load' and 'call' functions (load and run Lua code) 302** 'load' and 'call' functions (load and run Lua code)
303*/ 303*/
304LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, 304LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults,
305 lua_KContext ctx, lua_KFunction k); 305 lua_KContext ctx, lua_KFunction k);
306#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) 306#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
307 307
308LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, 308LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
309 lua_KContext ctx, lua_KFunction k); 309 lua_KContext ctx, lua_KFunction k);
310#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) 310#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
311 311
312LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, 312LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
313 const char *chunkname, const char *mode); 313 const char *chunkname, const char *mode);
314 314
315LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); 315LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
316 316
317 317
318/* 318/*
319** coroutine functions 319** coroutine functions
320*/ 320*/
321LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, 321LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
322 lua_KFunction k); 322 lua_KFunction k);
323LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg, 323LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg,
324 int *nres); 324 int *nres);
325LUA_API int (lua_status) (lua_State *L); 325LUA_API int (lua_status) (lua_State *L);
326LUA_API int (lua_isyieldable) (lua_State *L); 326LUA_API int (lua_isyieldable) (lua_State *L);
327 327
328#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) 328#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
329 329
330 330
331/* 331/*
332** Warning-related functions 332** Warning-related functions
333*/ 333*/
334LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud); 334LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud);
335LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); 335LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont);
336 336
337 337
338/* 338/*
339** garbage-collection function and options 339** garbage-collection function and options
340*/ 340*/
341 341
342#define LUA_GCSTOP 0 342#define LUA_GCSTOP 0
343#define LUA_GCRESTART 1 343#define LUA_GCRESTART 1
344#define LUA_GCCOLLECT 2 344#define LUA_GCCOLLECT 2
345#define LUA_GCCOUNT 3 345#define LUA_GCCOUNT 3
346#define LUA_GCCOUNTB 4 346#define LUA_GCCOUNTB 4
347#define LUA_GCSTEP 5 347#define LUA_GCSTEP 5
348#define LUA_GCSETPAUSE 6 348#define LUA_GCSETPAUSE 6
349#define LUA_GCSETSTEPMUL 7 349#define LUA_GCSETSTEPMUL 7
350#define LUA_GCISRUNNING 9 350#define LUA_GCISRUNNING 9
351#define LUA_GCGEN 10 351#define LUA_GCGEN 10
352#define LUA_GCINC 11 352#define LUA_GCINC 11
353 353
354LUA_API int (lua_gc) (lua_State *L, int what, ...); 354LUA_API int (lua_gc) (lua_State *L, int what, ...);
355 355
356 356
357/* 357/*
358** miscellaneous functions 358** miscellaneous functions
359*/ 359*/
360 360
361LUA_API int (lua_error) (lua_State *L); 361LUA_API int (lua_error) (lua_State *L);
362 362
363LUA_API int (lua_next) (lua_State *L, int idx); 363LUA_API int (lua_next) (lua_State *L, int idx);
364 364
365LUA_API void (lua_concat) (lua_State *L, int n); 365LUA_API void (lua_concat) (lua_State *L, int n);
366LUA_API void (lua_len) (lua_State *L, int idx); 366LUA_API void (lua_len) (lua_State *L, int idx);
367 367
368LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); 368LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
369 369
370LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 370LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
371LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); 371LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
372 372
373LUA_API void (lua_toclose) (lua_State *L, int idx); 373LUA_API void (lua_toclose) (lua_State *L, int idx);
374LUA_API void (lua_closeslot) (lua_State *L, int idx); 374LUA_API void (lua_closeslot) (lua_State *L, int idx);
375 375
376 376
377/* 377/*
378** {============================================================== 378** {==============================================================
379** some useful macros 379** some useful macros
380** =============================================================== 380** ===============================================================
381*/ 381*/
382 382
383#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) 383#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE))
384 384
385#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) 385#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL)
386#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) 386#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL)
387 387
388#define lua_pop(L,n) lua_settop(L, -(n)-1) 388#define lua_pop(L,n) lua_settop(L, -(n)-1)
389 389
390#define lua_newtable(L) lua_createtable(L, 0, 0) 390#define lua_newtable(L) lua_createtable(L, 0, 0)
391 391
392#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) 392#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
393 393
394#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) 394#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
395 395
396#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) 396#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION)
397#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) 397#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
398#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) 398#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
399#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) 399#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
400#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) 400#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
401#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) 401#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
402#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) 402#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
403#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) 403#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
404 404
405#define lua_pushliteral(L, s) lua_pushstring(L, "" s) 405#define lua_pushliteral(L, s) lua_pushstring(L, "" s)
406 406
407#define lua_pushglobaltable(L) \ 407#define lua_pushglobaltable(L) \
408 ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) 408 ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS))
409 409
410#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) 410#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
411 411
412 412
413#define lua_insert(L,idx) lua_rotate(L, (idx), 1) 413#define lua_insert(L,idx) lua_rotate(L, (idx), 1)
414 414
415#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) 415#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1))
416 416
417#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) 417#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1))
418 418
419/* }============================================================== */ 419/* }============================================================== */
420 420
421 421
422/* 422/*
423** {============================================================== 423** {==============================================================
424** compatibility macros 424** compatibility macros
425** =============================================================== 425** ===============================================================
426*/ 426*/
427#if defined(LUA_COMPAT_APIINTCASTS) 427#if defined(LUA_COMPAT_APIINTCASTS)
428 428
429#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 429#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
430#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) 430#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is))
431#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) 431#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
432 432
433#endif 433#endif
434 434
435#define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1) 435#define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1)
436#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) 436#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1)
437#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) 437#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1)
438 438
439#define LUA_NUMTAGS LUA_NUMTYPES 439#define LUA_NUMTAGS LUA_NUMTYPES
440 440
441/* }============================================================== */ 441/* }============================================================== */
442 442
443/* 443/*
444** {====================================================================== 444** {======================================================================
445** Debug API 445** Debug API
446** ======================================================================= 446** =======================================================================
447*/ 447*/
448 448
449 449
450/* 450/*
451** Event codes 451** Event codes
452*/ 452*/
453#define LUA_HOOKCALL 0 453#define LUA_HOOKCALL 0
454#define LUA_HOOKRET 1 454#define LUA_HOOKRET 1
455#define LUA_HOOKLINE 2 455#define LUA_HOOKLINE 2
456#define LUA_HOOKCOUNT 3 456#define LUA_HOOKCOUNT 3
457#define LUA_HOOKTAILCALL 4 457#define LUA_HOOKTAILCALL 4
458 458
459 459
460/* 460/*
461** Event masks 461** Event masks
462*/ 462*/
463#define LUA_MASKCALL (1 << LUA_HOOKCALL) 463#define LUA_MASKCALL (1 << LUA_HOOKCALL)
464#define LUA_MASKRET (1 << LUA_HOOKRET) 464#define LUA_MASKRET (1 << LUA_HOOKRET)
465#define LUA_MASKLINE (1 << LUA_HOOKLINE) 465#define LUA_MASKLINE (1 << LUA_HOOKLINE)
466#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) 466#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
467 467
468typedef struct lua_Debug lua_Debug; /* activation record */ 468typedef struct lua_Debug lua_Debug; /* activation record */
469 469
470 470
471/* Functions to be called by the debugger in specific events */ 471/* Functions to be called by the debugger in specific events */
472typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); 472typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
473 473
474 474
475LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); 475LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
476LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); 476LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
477LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); 477LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n);
478LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); 478LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n);
479LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); 479LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n);
480LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); 480LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n);
481 481
482LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); 482LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n);
483LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, 483LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1,
484 int fidx2, int n2); 484 int fidx2, int n2);
485 485
486LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); 486LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count);
487LUA_API lua_Hook (lua_gethook) (lua_State *L); 487LUA_API lua_Hook (lua_gethook) (lua_State *L);
488LUA_API int (lua_gethookmask) (lua_State *L); 488LUA_API int (lua_gethookmask) (lua_State *L);
489LUA_API int (lua_gethookcount) (lua_State *L); 489LUA_API int (lua_gethookcount) (lua_State *L);
490 490
491LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit); 491LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit);
492 492
493struct lua_Debug { 493struct lua_Debug {
494 int event; 494 int event;
495 const char *name; /* (n) */ 495 const char *name; /* (n) */
496 const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ 496 const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */
497 const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ 497 const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */
498 const char *source; /* (S) */ 498 const char *source; /* (S) */
499 size_t srclen; /* (S) */ 499 size_t srclen; /* (S) */
500 int currentline; /* (l) */ 500 int currentline; /* (l) */
501 int linedefined; /* (S) */ 501 int linedefined; /* (S) */
502 int lastlinedefined; /* (S) */ 502 int lastlinedefined; /* (S) */
503 unsigned char nups; /* (u) number of upvalues */ 503 unsigned char nups; /* (u) number of upvalues */
504 unsigned char nparams;/* (u) number of parameters */ 504 unsigned char nparams;/* (u) number of parameters */
505 char isvararg; /* (u) */ 505 char isvararg; /* (u) */
506 char istailcall; /* (t) */ 506 char istailcall; /* (t) */
507 unsigned short ftransfer; /* (r) index of first value transferred */ 507 unsigned short ftransfer; /* (r) index of first value transferred */
508 unsigned short ntransfer; /* (r) number of transferred values */ 508 unsigned short ntransfer; /* (r) number of transferred values */
509 char short_src[LUA_IDSIZE]; /* (S) */ 509 char short_src[LUA_IDSIZE]; /* (S) */
510 /* private part */ 510 /* private part */
511 struct CallInfo *i_ci; /* active function */ 511 struct CallInfo *i_ci; /* active function */
512}; 512};
513 513
514/* }====================================================================== */ 514/* }====================================================================== */
515 515
516 516
517/****************************************************************************** 517/******************************************************************************
518#ifdef _KERNEL 518#ifdef _KERNEL
519* Copyright (c) 2016-2017, Lourival Vieira Neto <lneto@NetBSD.org>. 519* Copyright (c) 2016-2017, Lourival Vieira Neto <lneto@NetBSD.org>.
520#endif 520#endif
521* Copyright (C) 1994-2022 Lua.org, PUC-Rio. 521* Copyright (C) 1994-2022 Lua.org, PUC-Rio.
522* 522*
523* Permission is hereby granted, free of charge, to any person obtaining 523* Permission is hereby granted, free of charge, to any person obtaining
524* a copy of this software and associated documentation files (the 524* a copy of this software and associated documentation files (the
525* "Software"), to deal in the Software without restriction, including 525* "Software"), to deal in the Software without restriction, including
526* without limitation the rights to use, copy, modify, merge, publish, 526* without limitation the rights to use, copy, modify, merge, publish,
527* distribute, sublicense, and/or sell copies of the Software, and to 527* distribute, sublicense, and/or sell copies of the Software, and to
528* permit persons to whom the Software is furnished to do so, subject to 528* permit persons to whom the Software is furnished to do so, subject to
529* the following conditions: 529* the following conditions:
530* 530*
531* The above copyright notice and this permission notice shall be 531* The above copyright notice and this permission notice shall be
532* included in all copies or substantial portions of the Software. 532* included in all copies or substantial portions of the Software.
533* 533*
534* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 534* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
535* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 535* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
536* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 536* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
537* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 537* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
538* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 538* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
539* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 539* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
540* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 540* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
541******************************************************************************/ 541******************************************************************************/
542 542
543 543
544#endif 544#endif