aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-01-24 13:45:33 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-01-24 13:45:33 -0200
commit71ae4801d66d9592b0fb08e4e78138b7a6e993d5 (patch)
treea683b5b3757efb979329bf513f1bd9fde3fb9d1f
parent6fda6a530265268c01a83c31f8fc30e34753bbf1 (diff)
downloadlua-71ae4801d66d9592b0fb08e4e78138b7a6e993d5.tar.gz
lua-71ae4801d66d9592b0fb08e4e78138b7a6e993d5.tar.bz2
lua-71ae4801d66d9592b0fb08e4e78138b7a6e993d5.zip
macros LUA_ENTRY/LUA_EXIT to control exclusive access to Lua core
-rw-r--r--lapi.c321
-rw-r--r--ldebug.c73
-rw-r--r--ldo.c32
-rw-r--r--ldo.h3
-rw-r--r--lmem.c6
-rw-r--r--lobject.c5
-rw-r--r--lstate.c60
-rw-r--r--lstate.h22
-rw-r--r--ltable.c9
-rw-r--r--ltm.c19
-rw-r--r--lvm.c19
11 files changed, 420 insertions, 149 deletions
diff --git a/lapi.c b/lapi.c
index bf5eea52..0ca9bad6 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.117 2001/01/18 15:59:09 roberto Exp roberto $ 2** $Id: lapi.c,v 1.118 2001/01/19 13:20:30 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -54,7 +54,11 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
54} 54}
55 55
56LUA_API int lua_stackspace (lua_State *L) { 56LUA_API int lua_stackspace (lua_State *L) {
57 return (L->stack_last - L->top); 57 int i;
58 LUA_ENTRY;
59 i = (L->stack_last - L->top);
60 LUA_EXIT;
61 return i;
58} 62}
59 63
60 64
@@ -65,36 +69,50 @@ LUA_API int lua_stackspace (lua_State *L) {
65 69
66 70
67LUA_API int lua_gettop (lua_State *L) { 71LUA_API int lua_gettop (lua_State *L) {
68 return (L->top - L->Cbase); 72 int i;
73 LUA_ENTRY;
74 i = (L->top - L->Cbase);
75 LUA_EXIT;
76 return i;
69} 77}
70 78
71 79
72LUA_API void lua_settop (lua_State *L, int index) { 80LUA_API void lua_settop (lua_State *L, int index) {
81 LUA_ENTRY;
73 if (index >= 0) 82 if (index >= 0)
74 luaD_adjusttop(L, L->Cbase, index); 83 luaD_adjusttop(L, L->Cbase, index);
75 else 84 else
76 L->top = L->top+index+1; /* index is negative */ 85 L->top = L->top+index+1; /* index is negative */
86 LUA_EXIT;
77} 87}
78 88
79 89
80LUA_API void lua_remove (lua_State *L, int index) { 90LUA_API void lua_remove (lua_State *L, int index) {
81 StkId p = luaA_index(L, index); 91 StkId p;
92 LUA_ENTRY;
93 p = luaA_index(L, index);
82 while (++p < L->top) setobj(p-1, p); 94 while (++p < L->top) setobj(p-1, p);
83 L->top--; 95 L->top--;
96 LUA_EXIT;
84} 97}
85 98
86 99
87LUA_API void lua_insert (lua_State *L, int index) { 100LUA_API void lua_insert (lua_State *L, int index) {
88 StkId p = luaA_index(L, index); 101 StkId p;
89 StkId q; 102 StkId q;
103 LUA_ENTRY;
104 p = luaA_index(L, index);
90 for (q = L->top; q>p; q--) setobj(q, q-1); 105 for (q = L->top; q>p; q--) setobj(q, q-1);
91 setobj(p, L->top); 106 setobj(p, L->top);
107 LUA_EXIT;
92} 108}
93 109
94 110
95LUA_API void lua_pushvalue (lua_State *L, int index) { 111LUA_API void lua_pushvalue (lua_State *L, int index) {
112 LUA_ENTRY;
96 setobj(L->top, luaA_index(L, index)); 113 setobj(L->top, luaA_index(L, index));
97 api_incr_top(L); 114 api_incr_top(L);
115 LUA_EXIT;
98} 116}
99 117
100 118
@@ -105,24 +123,43 @@ LUA_API void lua_pushvalue (lua_State *L, int index) {
105 123
106 124
107LUA_API int lua_type (lua_State *L, int index) { 125LUA_API int lua_type (lua_State *L, int index) {
108 StkId o = luaA_indexAcceptable(L, index); 126 StkId o;
109 return (o == NULL) ? LUA_TNONE : ttype(o); 127 int i;
128 LUA_ENTRY;
129 o = luaA_indexAcceptable(L, index);
130 i = (o == NULL) ? LUA_TNONE : ttype(o);
131 LUA_EXIT;
132 return i;
110} 133}
111 134
112LUA_API const char *lua_typename (lua_State *L, int t) { 135LUA_API const char *lua_typename (lua_State *L, int t) {
136 const char *s;
137 LUA_ENTRY;
113 UNUSED(L); 138 UNUSED(L);
114 return (t == LUA_TNONE) ? "no value" : luaO_typenames[t]; 139 s = (t == LUA_TNONE) ? "no value" : luaO_typenames[t];
140 LUA_EXIT;
141 return s;
115} 142}
116 143
117 144
118LUA_API int lua_iscfunction (lua_State *L, int index) { 145LUA_API int lua_iscfunction (lua_State *L, int index) {
119 StkId o = luaA_indexAcceptable(L, index); 146 StkId o;
120 return (o == NULL) ? 0 : iscfunction(o); 147 int i;
148 LUA_ENTRY;
149 o = luaA_indexAcceptable(L, index);
150 i = (o == NULL) ? 0 : iscfunction(o);
151 LUA_EXIT;
152 return i;
121} 153}
122 154
123LUA_API int lua_isnumber (lua_State *L, int index) { 155LUA_API int lua_isnumber (lua_State *L, int index) {
124 TObject *o = luaA_indexAcceptable(L, index); 156 TObject *o;
125 return (o == NULL) ? 0 : (tonumber(o) == 0); 157 int i;
158 LUA_ENTRY;
159 o = luaA_indexAcceptable(L, index);
160 i = (o == NULL) ? 0 : (tonumber(o) == 0);
161 LUA_EXIT;
162 return i;
126} 163}
127 164
128LUA_API int lua_isstring (lua_State *L, int index) { 165LUA_API int lua_isstring (lua_State *L, int index) {
@@ -132,62 +169,113 @@ LUA_API int lua_isstring (lua_State *L, int index) {
132 169
133 170
134LUA_API int lua_tag (lua_State *L, int index) { 171LUA_API int lua_tag (lua_State *L, int index) {
135 StkId o = luaA_indexAcceptable(L, index); 172 StkId o;
136 return (o == NULL) ? LUA_NOTAG : luaT_tag(o); 173 int i;
174 LUA_ENTRY;
175 o = luaA_indexAcceptable(L, index);
176 i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
177 LUA_EXIT;
178 return i;
137} 179}
138 180
139LUA_API int lua_equal (lua_State *L, int index1, int index2) { 181LUA_API int lua_equal (lua_State *L, int index1, int index2) {
140 StkId o1 = luaA_indexAcceptable(L, index1); 182 StkId o1, o2;
141 StkId o2 = luaA_indexAcceptable(L, index2); 183 int i;
142 if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */ 184 LUA_ENTRY;
143 else return luaO_equalObj(o1, o2); 185 o1 = luaA_indexAcceptable(L, index1);
186 o2 = luaA_indexAcceptable(L, index2);
187 i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
188 : luaO_equalObj(o1, o2);
189 LUA_EXIT;
190 return i;
144} 191}
145 192
146LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { 193LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
147 StkId o1 = luaA_indexAcceptable(L, index1); 194 StkId o1, o2;
148 StkId o2 = luaA_indexAcceptable(L, index2); 195 int i;
149 if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */ 196 LUA_ENTRY;
150 else return luaV_lessthan(L, o1, o2, L->top); 197 o1 = luaA_indexAcceptable(L, index1);
198 o2 = luaA_indexAcceptable(L, index2);
199 i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
200 : luaV_lessthan(L, o1, o2, L->top);
201 LUA_EXIT;
202 return i;
151} 203}
152 204
153 205
154 206
155LUA_API lua_Number lua_tonumber (lua_State *L, int index) { 207LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
156 StkId o = luaA_indexAcceptable(L, index); 208 StkId o;
157 return (o == NULL || tonumber(o)) ? 0 : nvalue(o); 209 lua_Number n;
210 LUA_ENTRY;
211 o = luaA_indexAcceptable(L, index);
212 n = (o == NULL || tonumber(o)) ? 0 : nvalue(o);
213 LUA_EXIT;
214 return n;
158} 215}
159 216
160LUA_API const char *lua_tostring (lua_State *L, int index) { 217LUA_API const char *lua_tostring (lua_State *L, int index) {
161 StkId o = luaA_indexAcceptable(L, index); 218 StkId o;
162 return (o == NULL || tostring(L, o)) ? NULL : svalue(o); 219 const char *s;
220 LUA_ENTRY;
221 o = luaA_indexAcceptable(L, index);
222 s = (o == NULL || tostring(L, o)) ? NULL : svalue(o);
223 LUA_EXIT;
224 return s;
163} 225}
164 226
165LUA_API size_t lua_strlen (lua_State *L, int index) { 227LUA_API size_t lua_strlen (lua_State *L, int index) {
166 StkId o = luaA_indexAcceptable(L, index); 228 StkId o;
167 return (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len; 229 size_t l;
230 LUA_ENTRY;
231 o = luaA_indexAcceptable(L, index);
232 l = (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len;
233 LUA_EXIT;
234 return l;
168} 235}
169 236
170LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) { 237LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
171 StkId o = luaA_indexAcceptable(L, index); 238 StkId o;
172 return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c; 239 lua_CFunction f;
240 LUA_ENTRY;
241 o = luaA_indexAcceptable(L, index);
242 f = (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
243 LUA_EXIT;
244 return f;
173} 245}
174 246
175LUA_API void *lua_touserdata (lua_State *L, int index) { 247LUA_API void *lua_touserdata (lua_State *L, int index) {
176 StkId o = luaA_indexAcceptable(L, index); 248 StkId o;
177 return (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL : 249 void *p;
250 LUA_ENTRY;
251 o = luaA_indexAcceptable(L, index);
252 p = (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL :
178 tsvalue(o)->u.d.value; 253 tsvalue(o)->u.d.value;
254 LUA_EXIT;
255 return p;
179} 256}
180 257
181LUA_API const void *lua_topointer (lua_State *L, int index) { 258LUA_API const void *lua_topointer (lua_State *L, int index) {
182 StkId o = luaA_indexAcceptable(L, index); 259 StkId o;
183 if (o == NULL) return NULL; 260 const void *p;
184 switch (ttype(o)) { 261 LUA_ENTRY;
185 case LUA_TTABLE: 262 o = luaA_indexAcceptable(L, index);
186 return hvalue(o); 263 if (o == NULL) p = NULL;
187 case LUA_TFUNCTION: 264 else {
188 return clvalue(o); 265 switch (ttype(o)) {
189 default: return NULL; 266 case LUA_TTABLE:
267 p = hvalue(o);
268 break;
269 case LUA_TFUNCTION:
270 p = clvalue(o);
271 break;
272 default:
273 p = NULL;
274 break;
275 }
190 } 276 }
277 LUA_EXIT;
278 return p;
191} 279}
192 280
193 281
@@ -198,20 +286,26 @@ LUA_API const void *lua_topointer (lua_State *L, int index) {
198 286
199 287
200LUA_API void lua_pushnil (lua_State *L) { 288LUA_API void lua_pushnil (lua_State *L) {
289 LUA_ENTRY;
201 setnilvalue(L->top); 290 setnilvalue(L->top);
202 api_incr_top(L); 291 api_incr_top(L);
292 LUA_EXIT;
203} 293}
204 294
205 295
206LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { 296LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
297 LUA_ENTRY;
207 setnvalue(L->top, n); 298 setnvalue(L->top, n);
208 api_incr_top(L); 299 api_incr_top(L);
300 LUA_EXIT;
209} 301}
210 302
211 303
212LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { 304LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
305 LUA_ENTRY;
213 setsvalue(L->top, luaS_newlstr(L, s, len)); 306 setsvalue(L->top, luaS_newlstr(L, s, len));
214 api_incr_top(L); 307 api_incr_top(L);
308 LUA_EXIT;
215} 309}
216 310
217 311
@@ -224,16 +318,20 @@ LUA_API void lua_pushstring (lua_State *L, const char *s) {
224 318
225 319
226LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { 320LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
321 LUA_ENTRY;
227 luaV_Cclosure(L, fn, n); 322 luaV_Cclosure(L, fn, n);
323 LUA_EXIT;
228} 324}
229 325
230 326
231LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) { 327LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
328 LUA_ENTRY;
232 /* ORDER LUA_T */ 329 /* ORDER LUA_T */
233 if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(G(L), tag))) 330 if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(G(L), tag)))
234 luaO_verror(L, "invalid tag for a userdata (%d)", tag); 331 luaO_verror(L, "invalid tag for a userdata (%d)", tag);
235 setuvalue(L->top, luaS_createudata(L, u, tag)); 332 setuvalue(L->top, luaS_createudata(L, u, tag));
236 api_incr_top(L); 333 api_incr_top(L);
334 LUA_EXIT;
237} 335}
238 336
239 337
@@ -244,60 +342,80 @@ LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
244 342
245 343
246LUA_API void lua_getglobal (lua_State *L, const char *name) { 344LUA_API void lua_getglobal (lua_State *L, const char *name) {
247 StkId top = L->top; 345 StkId top;
346 LUA_ENTRY;
347 top = L->top;
248 setobj(top, luaV_getglobal(L, luaS_new(L, name))); 348 setobj(top, luaV_getglobal(L, luaS_new(L, name)));
249 L->top = top; 349 L->top = top;
250 api_incr_top(L); 350 api_incr_top(L);
351 LUA_EXIT;
251} 352}
252 353
253 354
254LUA_API void lua_gettable (lua_State *L, int index) { 355LUA_API void lua_gettable (lua_State *L, int index) {
255 StkId t = Index(L, index); 356 StkId t, top;
256 StkId top = L->top; 357 LUA_ENTRY;
358 t = Index(L, index);
359 top = L->top;
257 setobj(top-1, luaV_gettable(L, t)); 360 setobj(top-1, luaV_gettable(L, t));
258 L->top = top; /* tag method may change top */ 361 L->top = top; /* tag method may change top */
362 LUA_EXIT;
259} 363}
260 364
261 365
262LUA_API void lua_rawget (lua_State *L, int index) { 366LUA_API void lua_rawget (lua_State *L, int index) {
263 StkId t = Index(L, index); 367 StkId t;
368 LUA_ENTRY;
369 t = Index(L, index);
264 lua_assert(ttype(t) == LUA_TTABLE); 370 lua_assert(ttype(t) == LUA_TTABLE);
265 setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1)); 371 setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
372 LUA_EXIT;
266} 373}
267 374
268 375
269LUA_API void lua_rawgeti (lua_State *L, int index, int n) { 376LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
270 StkId o = Index(L, index); 377 StkId o;
378 LUA_ENTRY;
379 o = Index(L, index);
271 lua_assert(ttype(o) == LUA_TTABLE); 380 lua_assert(ttype(o) == LUA_TTABLE);
272 setobj(L->top, luaH_getnum(hvalue(o), n)); 381 setobj(L->top, luaH_getnum(hvalue(o), n));
273 api_incr_top(L); 382 api_incr_top(L);
383 LUA_EXIT;
274} 384}
275 385
276 386
277LUA_API void lua_getglobals (lua_State *L) { 387LUA_API void lua_getglobals (lua_State *L) {
388 LUA_ENTRY;
278 sethvalue(L->top, L->gt); 389 sethvalue(L->top, L->gt);
279 api_incr_top(L); 390 api_incr_top(L);
391 LUA_EXIT;
280} 392}
281 393
282 394
283LUA_API int lua_getref (lua_State *L, int ref) { 395LUA_API int lua_getref (lua_State *L, int ref) {
396 int status = 1;
397 LUA_ENTRY;
284 if (ref == LUA_REFNIL) { 398 if (ref == LUA_REFNIL) {
285 setnilvalue(L->top); 399 setnilvalue(L->top);
400 api_incr_top(L);
286 } 401 }
287 else if (0 <= ref && ref < G(L)->nref && 402 else if (0 <= ref && ref < G(L)->nref &&
288 (G(L)->refArray[ref].st == LOCK || G(L)->refArray[ref].st == HOLD)) { 403 (G(L)->refArray[ref].st == LOCK || G(L)->refArray[ref].st == HOLD)) {
289 setobj(L->top, &G(L)->refArray[ref].o); 404 setobj(L->top, &G(L)->refArray[ref].o);
405 api_incr_top(L);
290 } 406 }
291 else 407 else
292 return 0; 408 status = 0;
293 api_incr_top(L); 409 LUA_EXIT;
294 return 1; 410 return status;
295} 411}
296 412
297 413
298LUA_API void lua_newtable (lua_State *L) { 414LUA_API void lua_newtable (lua_State *L) {
415 LUA_ENTRY;
299 sethvalue(L->top, luaH_new(L, 0)); 416 sethvalue(L->top, luaH_new(L, 0));
300 api_incr_top(L); 417 api_incr_top(L);
418 LUA_EXIT;
301} 419}
302 420
303 421
@@ -308,45 +426,61 @@ LUA_API void lua_newtable (lua_State *L) {
308 426
309 427
310LUA_API void lua_setglobal (lua_State *L, const char *name) { 428LUA_API void lua_setglobal (lua_State *L, const char *name) {
311 StkId top = L->top; 429 StkId top;
430 LUA_ENTRY;
431 top = L->top;
312 luaV_setglobal(L, luaS_new(L, name)); 432 luaV_setglobal(L, luaS_new(L, name));
313 L->top = top-1; /* remove element from the top */ 433 L->top = top-1; /* remove element from the top */
434 LUA_EXIT;
314} 435}
315 436
316 437
317LUA_API void lua_settable (lua_State *L, int index) { 438LUA_API void lua_settable (lua_State *L, int index) {
318 StkId t = Index(L, index); 439 StkId t, top;
319 StkId top = L->top; 440 LUA_ENTRY;
441 t = Index(L, index);
442 top = L->top;
320 luaV_settable(L, t, top-2); 443 luaV_settable(L, t, top-2);
321 L->top = top-2; /* pop index and value */ 444 L->top = top-2; /* pop index and value */
445 LUA_EXIT;
322} 446}
323 447
324 448
325LUA_API void lua_rawset (lua_State *L, int index) { 449LUA_API void lua_rawset (lua_State *L, int index) {
326 StkId t = Index(L, index); 450 StkId t;
451 LUA_ENTRY;
452 t = Index(L, index);
327 lua_assert(ttype(t) == LUA_TTABLE); 453 lua_assert(ttype(t) == LUA_TTABLE);
328 setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1)); 454 setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
329 L->top -= 2; 455 L->top -= 2;
456 LUA_EXIT;
330} 457}
331 458
332 459
333LUA_API void lua_rawseti (lua_State *L, int index, int n) { 460LUA_API void lua_rawseti (lua_State *L, int index, int n) {
334 StkId o = Index(L, index); 461 StkId o;
462 LUA_ENTRY;
463 o = Index(L, index);
335 lua_assert(ttype(o) == LUA_TTABLE); 464 lua_assert(ttype(o) == LUA_TTABLE);
336 setobj(luaH_setnum(L, hvalue(o), n), (L->top-1)); 465 setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
337 L->top--; 466 L->top--;
467 LUA_EXIT;
338} 468}
339 469
340 470
341LUA_API void lua_setglobals (lua_State *L) { 471LUA_API void lua_setglobals (lua_State *L) {
342 StkId newtable = --L->top; 472 StkId newtable;
473 LUA_ENTRY;
474 newtable = --L->top;
343 lua_assert(ttype(newtable) == LUA_TTABLE); 475 lua_assert(ttype(newtable) == LUA_TTABLE);
344 L->gt = hvalue(newtable); 476 L->gt = hvalue(newtable);
477 LUA_EXIT;
345} 478}
346 479
347 480
348LUA_API int lua_ref (lua_State *L, int lock) { 481LUA_API int lua_ref (lua_State *L, int lock) {
349 int ref; 482 int ref;
483 LUA_ENTRY;
350 if (ttype(L->top-1) == LUA_TNIL) 484 if (ttype(L->top-1) == LUA_TNIL)
351 ref = LUA_REFNIL; 485 ref = LUA_REFNIL;
352 else { 486 else {
@@ -363,6 +497,7 @@ LUA_API int lua_ref (lua_State *L, int lock) {
363 G(L)->refArray[ref].st = lock ? LOCK : HOLD; 497 G(L)->refArray[ref].st = lock ? LOCK : HOLD;
364 } 498 }
365 L->top--; 499 L->top--;
500 LUA_EXIT;
366 return ref; 501 return ref;
367} 502}
368 503
@@ -373,7 +508,9 @@ LUA_API int lua_ref (lua_State *L, int lock) {
373*/ 508*/
374 509
375LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) { 510LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
511 LUA_ENTRY;
376 luaD_call(L, L->top-(nargs+1), nresults); 512 luaD_call(L, L->top-(nargs+1), nresults);
513 LUA_EXIT;
377} 514}
378 515
379 516
@@ -386,19 +523,29 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
386#define GCunscale(x) ((mem_int)(x)<<10) 523#define GCunscale(x) ((mem_int)(x)<<10)
387 524
388LUA_API int lua_getgcthreshold (lua_State *L) { 525LUA_API int lua_getgcthreshold (lua_State *L) {
389 return GCscale(G(L)->GCthreshold); 526 int threshold;
527 LUA_ENTRY;
528 threshold = GCscale(G(L)->GCthreshold);
529 LUA_EXIT;
530 return threshold;
390} 531}
391 532
392LUA_API int lua_getgccount (lua_State *L) { 533LUA_API int lua_getgccount (lua_State *L) {
393 return GCscale(G(L)->nblocks); 534 int count;
535 LUA_ENTRY;
536 count = GCscale(G(L)->nblocks);
537 LUA_EXIT;
538 return count;
394} 539}
395 540
396LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) { 541LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
542 LUA_ENTRY;
397 if (newthreshold > GCscale(ULONG_MAX)) 543 if (newthreshold > GCscale(ULONG_MAX))
398 G(L)->GCthreshold = ULONG_MAX; 544 G(L)->GCthreshold = ULONG_MAX;
399 else 545 else
400 G(L)->GCthreshold = GCunscale(newthreshold); 546 G(L)->GCthreshold = GCunscale(newthreshold);
401 luaC_checkGC(L); 547 luaC_checkGC(L);
548 LUA_EXIT;
402} 549}
403 550
404 551
@@ -407,6 +554,7 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
407*/ 554*/
408 555
409LUA_API void lua_settag (lua_State *L, int tag) { 556LUA_API void lua_settag (lua_State *L, int tag) {
557 LUA_ENTRY;
410 luaT_realtag(L, tag); 558 luaT_realtag(L, tag);
411 switch (ttype(L->top-1)) { 559 switch (ttype(L->top-1)) {
412 case LUA_TTABLE: 560 case LUA_TTABLE:
@@ -419,69 +567,98 @@ LUA_API void lua_settag (lua_State *L, int tag) {
419 luaO_verror(L, "cannot change the tag of a %.20s", 567 luaO_verror(L, "cannot change the tag of a %.20s",
420 luaO_typename(L->top-1)); 568 luaO_typename(L->top-1));
421 } 569 }
570 LUA_EXIT;
571}
572
573
574LUA_API void lua_error (lua_State *L, const char *s) {
575 LUA_ENTRY;
576 luaD_error(L, s);
577 LUA_EXIT;
422} 578}
423 579
424 580
425LUA_API void lua_unref (lua_State *L, int ref) { 581LUA_API void lua_unref (lua_State *L, int ref) {
582 LUA_ENTRY;
426 if (ref >= 0) { 583 if (ref >= 0) {
427 lua_assert(ref < G(L)->nref && G(L)->refArray[ref].st < 0); 584 lua_assert(ref < G(L)->nref && G(L)->refArray[ref].st < 0);
428 G(L)->refArray[ref].st = G(L)->refFree; 585 G(L)->refArray[ref].st = G(L)->refFree;
429 G(L)->refFree = ref; 586 G(L)->refFree = ref;
430 } 587 }
588 LUA_EXIT;
431} 589}
432 590
433 591
434LUA_API int lua_next (lua_State *L, int index) { 592LUA_API int lua_next (lua_State *L, int index) {
435 StkId t = luaA_index(L, index); 593 StkId t;
436 Node *n; 594 Node *n;
595 int more;
596 LUA_ENTRY;
597 t = luaA_index(L, index);
437 lua_assert(ttype(t) == LUA_TTABLE); 598 lua_assert(ttype(t) == LUA_TTABLE);
438 n = luaH_next(L, hvalue(t), luaA_index(L, -1)); 599 n = luaH_next(L, hvalue(t), luaA_index(L, -1));
439 if (n) { 600 if (n) {
440 setobj(L->top-1, key(n)); 601 setobj(L->top-1, key(n));
441 setobj(L->top, val(n)); 602 setobj(L->top, val(n));
442 api_incr_top(L); 603 api_incr_top(L);
443 return 1; 604 more = 1;
444 } 605 }
445 else { /* no more elements */ 606 else { /* no more elements */
446 L->top -= 1; /* remove key */ 607 L->top -= 1; /* remove key */
447 return 0; 608 more = 0;
448 } 609 }
610 LUA_EXIT;
611 return more;
449} 612}
450 613
451 614
452LUA_API int lua_getn (lua_State *L, int index) { 615LUA_API int lua_getn (lua_State *L, int index) {
453 Hash *h = hvalue(luaA_index(L, index)); 616 Hash *h;
454 const TObject *value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */ 617 const TObject *value;
618 int n;
619 LUA_ENTRY;
620 h = hvalue(luaA_index(L, index));
621 value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */
455 if (ttype(value) == LUA_TNUMBER) 622 if (ttype(value) == LUA_TNUMBER)
456 return (int)nvalue(value); 623 n = (int)nvalue(value);
457 else { 624 else {
458 lua_Number max = 0; 625 lua_Number max = 0;
459 int i = h->size; 626 int i = h->size;
460 Node *n = h->node; 627 Node *nd = h->node;
461 while (i--) { 628 while (i--) {
462 if (ttype(key(n)) == LUA_TNUMBER && 629 if (ttype(key(nd)) == LUA_TNUMBER &&
463 ttype(val(n)) != LUA_TNIL && 630 ttype(val(nd)) != LUA_TNIL &&
464 nvalue(key(n)) > max) 631 nvalue(key(nd)) > max)
465 max = nvalue(key(n)); 632 max = nvalue(key(nd));
466 n++; 633 nd++;
467 } 634 }
468 return (int)max; 635 n = (int)max;
469 } 636 }
637 LUA_EXIT;
638 return n;
470} 639}
471 640
472 641
473LUA_API void lua_concat (lua_State *L, int n) { 642LUA_API void lua_concat (lua_State *L, int n) {
474 StkId top = L->top; 643 StkId top;
644 LUA_ENTRY;
645 top = L->top;
475 luaV_strconc(L, n, top); 646 luaV_strconc(L, n, top);
476 L->top = top-(n-1); 647 L->top = top-(n-1);
477 luaC_checkGC(L); 648 luaC_checkGC(L);
649 LUA_EXIT;
478} 650}
479 651
480 652
481LUA_API void *lua_newuserdata (lua_State *L, size_t size) { 653LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
482 TString *ts = luaS_newudata(L, size, NULL); 654 TString *ts;
655 void *p;
656 LUA_ENTRY;
657 ts = luaS_newudata(L, size, NULL);
483 setuvalue(L->top, ts); 658 setuvalue(L->top, ts);
484 api_incr_top(L); 659 api_incr_top(L);
485 return ts->u.d.value; 660 p = ts->u.d.value;
661 LUA_EXIT;
662 return p;
486} 663}
487 664
diff --git a/ldebug.c b/ldebug.c
index 3efe2613..7d5a150d 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 1.53 2001/01/18 15:59:09 roberto Exp roberto $ 2** $Id: ldebug.c,v 1.54 2001/01/19 13:20:30 roberto Exp roberto $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -41,15 +41,21 @@ static int isLmark (StkId o) {
41 41
42 42
43LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) { 43LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
44 lua_Hook oldhook = L->callhook; 44 lua_Hook oldhook;
45 LUA_ENTRY;
46 oldhook = L->callhook;
45 L->callhook = func; 47 L->callhook = func;
48 LUA_EXIT;
46 return oldhook; 49 return oldhook;
47} 50}
48 51
49 52
50LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) { 53LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
51 lua_Hook oldhook = L->linehook; 54 lua_Hook oldhook;
55 LUA_ENTRY;
56 oldhook = L->linehook;
52 L->linehook = func; 57 L->linehook = func;
58 LUA_EXIT;
53 return oldhook; 59 return oldhook;
54} 60}
55 61
@@ -68,12 +74,17 @@ static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
68 74
69 75
70LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { 76LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
71 StkId f = aux_stackedfunction(L, level, L->top); 77 StkId f;
72 if (f == NULL) return 0; /* there is no such level */ 78 int status;
79 LUA_ENTRY;
80 f = aux_stackedfunction(L, level, L->top);
81 if (f == NULL) status = 0; /* there is no such level */
73 else { 82 else {
74 ar->_func = f; 83 ar->_func = f;
75 return 1; 84 status = 1;
76 } 85 }
86 LUA_EXIT;
87 return status;
77} 88}
78 89
79 90
@@ -149,25 +160,39 @@ static Proto *getluaproto (StkId f) {
149 160
150LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { 161LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
151 const char *name; 162 const char *name;
152 StkId f = ar->_func; 163 StkId f;
153 Proto *fp = getluaproto(f); 164 Proto *fp;
154 if (!fp) return NULL; /* `f' is not a Lua function? */ 165 LUA_ENTRY;
155 name = luaF_getlocalname(fp, n, currentpc(f)); 166 name = NULL;
156 if (!name) return NULL; 167 f = ar->_func;
157 luaA_pushobject(L, (f+1)+(n-1)); /* push value */ 168 fp = getluaproto(f);
169 if (fp) { /* `f' is a Lua function? */
170 name = luaF_getlocalname(fp, n, currentpc(f));
171 if (name)
172 luaA_pushobject(L, (f+1)+(n-1)); /* push value */
173 }
174 LUA_EXIT;
158 return name; 175 return name;
159} 176}
160 177
161 178
162LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { 179LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
163 const char *name; 180 const char *name;
164 StkId f = ar->_func; 181 StkId f;
165 Proto *fp = getluaproto(f); 182 Proto *fp;
183 LUA_ENTRY;
184 name = NULL;
185 f = ar->_func;
186 fp = getluaproto(f);
166 L->top--; /* pop new value */ 187 L->top--; /* pop new value */
167 if (!fp) return NULL; /* `f' is not a Lua function? */ 188 if (fp) { /* `f' is a Lua function? */
168 name = luaF_getlocalname(fp, n, currentpc(f)); 189 name = luaF_getlocalname(fp, n, currentpc(f));
169 if (!name || name[0] == '(') return NULL; /* `(' starts private locals */ 190 if (!name || name[0] == '(') /* `(' starts private locals */
170 setobj((f+1)+(n-1), L->top); 191 name = NULL;
192 else
193 setobj((f+1)+(n-1), L->top);
194 }
195 LUA_EXIT;
171 return name; 196 return name;
172} 197}
173 198
@@ -189,7 +214,7 @@ static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
189 cl = infovalue(func)->func; 214 cl = infovalue(func)->func;
190 break; 215 break;
191 default: 216 default:
192 lua_error(L, "value for `lua_getinfo' is not a function"); 217 luaD_error(L, "value for `lua_getinfo' is not a function");
193 } 218 }
194 if (cl->isC) { 219 if (cl->isC) {
195 ar->source = "=C"; 220 ar->source = "=C";
@@ -245,7 +270,10 @@ static void getname (lua_State *L, StkId f, lua_Debug *ar) {
245 270
246LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { 271LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
247 StkId func; 272 StkId func;
248 int isactive = (*what != '>'); 273 int isactive;
274 int status = 1;
275 LUA_ENTRY;
276 isactive = (*what != '>');
249 if (isactive) 277 if (isactive)
250 func = ar->_func; 278 func = ar->_func;
251 else { 279 else {
@@ -277,11 +305,12 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
277 incr_top; /* push function */ 305 incr_top; /* push function */
278 break; 306 break;
279 } 307 }
280 default: return 0; /* invalid option */ 308 default: status = 0; /* invalid option */
281 } 309 }
282 } 310 }
283 if (!isactive) L->top--; /* pop function */ 311 if (!isactive) L->top--; /* pop function */
284 return 1; 312 LUA_EXIT;
313 return status;
285} 314}
286 315
287 316
diff --git a/ldo.c b/ldo.c
index 911c544f..ec7897f9 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.114 2001/01/18 15:59:09 roberto Exp roberto $ 2** $Id: ldo.c,v 1.115 2001/01/19 13:20:30 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -56,7 +56,7 @@ void luaD_checkstack (lua_State *L, int n) {
56 else { 56 else {
57 L->stack_last += EXTRA_STACK; /* to be used by error message */ 57 L->stack_last += EXTRA_STACK; /* to be used by error message */
58 lua_assert(L->stack_last == L->stack+L->stacksize-1); 58 lua_assert(L->stack_last == L->stack+L->stacksize-1);
59 lua_error(L, "stack overflow"); 59 luaD_error(L, "stack overflow");
60 } 60 }
61 } 61 }
62} 62}
@@ -94,7 +94,9 @@ static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
94 StkId old_top = L->Cbase = L->top; 94 StkId old_top = L->Cbase = L->top;
95 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 95 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
96 L->allowhooks = 0; /* cannot call hooks inside a hook */ 96 L->allowhooks = 0; /* cannot call hooks inside a hook */
97 LUA_EXIT;
97 (*hook)(L, ar); 98 (*hook)(L, ar);
99 LUA_ENTRY;
98 lua_assert(L->allowhooks == 0); 100 lua_assert(L->allowhooks == 0);
99 L->allowhooks = 1; 101 L->allowhooks = 1;
100 L->top = old_top; 102 L->top = old_top;
@@ -133,7 +135,9 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
133 luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ 135 luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
134 for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ 136 for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
135 setobj(L->top++, &cl->upvalue[n]); 137 setobj(L->top++, &cl->upvalue[n]);
138 LUA_EXIT;
136 n = (*cl->f.c)(L); /* do the actual call */ 139 n = (*cl->f.c)(L); /* do the actual call */
140 LUA_ENTRY;
137 L->Cbase = old_Cbase; /* restore old C base */ 141 L->Cbase = old_Cbase; /* restore old C base */
138 return L->top - n; /* return index of first result */ 142 return L->top - n; /* return index of first result */
139} 143}
@@ -212,13 +216,16 @@ static void f_call (lua_State *L, void *ud) {
212 216
213 217
214LUA_API int lua_call (lua_State *L, int nargs, int nresults) { 218LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
215 StkId func = L->top - (nargs+1); /* function to be called */ 219 StkId func;
216 struct CallS c; 220 struct CallS c;
217 int status; 221 int status;
222 LUA_ENTRY;
223 func = L->top - (nargs+1); /* function to be called */
218 c.func = func; c.nresults = nresults; 224 c.func = func; c.nresults = nresults;
219 status = luaD_runprotected(L, f_call, &c); 225 status = luaD_runprotected(L, f_call, &c);
220 if (status != 0) /* an error occurred? */ 226 if (status != 0) /* an error occurred? */
221 L->top = func; /* remove parameters from the stack */ 227 L->top = func; /* remove parameters from the stack */
228 LUA_EXIT;
222 return status; 229 return status;
223} 230}
224 231
@@ -242,6 +249,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) {
242 struct ParserS p; 249 struct ParserS p;
243 mem_int old_blocks; 250 mem_int old_blocks;
244 int status; 251 int status;
252 LUA_ENTRY;
245 p.z = z; p.bin = bin; 253 p.z = z; p.bin = bin;
246 luaC_checkGC(L); 254 luaC_checkGC(L);
247 old_blocks = G(L)->nblocks; 255 old_blocks = G(L)->nblocks;
@@ -253,6 +261,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) {
253 } 261 }
254 else if (status == LUA_ERRRUN) /* an error occurred: correct error code */ 262 else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
255 status = LUA_ERRSYNTAX; 263 status = LUA_ERRSYNTAX;
264 LUA_EXIT;
256 return status; 265 return status;
257} 266}
258 267
@@ -275,9 +284,9 @@ static int parse_file (lua_State *L, const char *filename) {
275 lua_pushstring(L, (filename == NULL) ? "(stdin)" : filename); 284 lua_pushstring(L, (filename == NULL) ? "(stdin)" : filename);
276 lua_concat(L, 2); 285 lua_concat(L, 2);
277 filename = lua_tostring(L, -1); /* filename = '@'..filename */ 286 filename = lua_tostring(L, -1); /* filename = '@'..filename */
278 lua_pop(L, 1); /* OK: there is no GC during parser */
279 luaZ_Fopen(&z, f, filename); 287 luaZ_Fopen(&z, f, filename);
280 status = protectedparser(L, &z, bin); 288 status = protectedparser(L, &z, bin);
289 lua_remove(L, -2); /* remove filename */
281 if (f != stdin) 290 if (f != stdin)
282 fclose(f); 291 fclose(f);
283 return status; 292 return status;
@@ -285,7 +294,8 @@ static int parse_file (lua_State *L, const char *filename) {
285 294
286 295
287LUA_API int lua_dofile (lua_State *L, const char *filename) { 296LUA_API int lua_dofile (lua_State *L, const char *filename) {
288 int status = parse_file(L, filename); 297 int status;
298 status = parse_file(L, filename);
289 if (status == 0) /* parse OK? */ 299 if (status == 0) /* parse OK? */
290 status = lua_call(L, 0, LUA_MULTRET); /* call main */ 300 status = lua_call(L, 0, LUA_MULTRET); /* call main */
291 return status; 301 return status;
@@ -295,14 +305,17 @@ LUA_API int lua_dofile (lua_State *L, const char *filename) {
295static int parse_buffer (lua_State *L, const char *buff, size_t size, 305static int parse_buffer (lua_State *L, const char *buff, size_t size,
296 const char *name) { 306 const char *name) {
297 ZIO z; 307 ZIO z;
308 int status;
298 if (!name) name = "?"; 309 if (!name) name = "?";
299 luaZ_mopen(&z, buff, size, name); 310 luaZ_mopen(&z, buff, size, name);
300 return protectedparser(L, &z, buff[0]==ID_CHUNK); 311 status = protectedparser(L, &z, buff[0]==ID_CHUNK);
312 return status;
301} 313}
302 314
303 315
304LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name) { 316LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name) {
305 int status = parse_buffer(L, buff, size, name); 317 int status;
318 status = parse_buffer(L, buff, size, name);
306 if (status == 0) /* parse OK? */ 319 if (status == 0) /* parse OK? */
307 status = lua_call(L, 0, LUA_MULTRET); /* call main */ 320 status = lua_call(L, 0, LUA_MULTRET); /* call main */
308 return status; 321 return status;
@@ -333,7 +346,8 @@ static void message (lua_State *L, const char *s) {
333 if (ttype(em) == LUA_TFUNCTION) { 346 if (ttype(em) == LUA_TFUNCTION) {
334 setobj(L->top, em); 347 setobj(L->top, em);
335 incr_top; 348 incr_top;
336 lua_pushstring(L, s); 349 setsvalue(L->top, luaS_new(L, s));
350 incr_top;
337 luaD_call(L, L->top-2, 0); 351 luaD_call(L, L->top-2, 0);
338 } 352 }
339} 353}
@@ -342,7 +356,7 @@ static void message (lua_State *L, const char *s) {
342/* 356/*
343** Reports an error, and jumps up to the available recovery label 357** Reports an error, and jumps up to the available recovery label
344*/ 358*/
345LUA_API void lua_error (lua_State *L, const char *s) { 359void luaD_error (lua_State *L, const char *s) {
346 if (s) message(L, s); 360 if (s) message(L, s);
347 luaD_breakrun(L, LUA_ERRRUN); 361 luaD_breakrun(L, LUA_ERRRUN);
348} 362}
diff --git a/ldo.h b/ldo.h
index 7cf5af38..42497736 100644
--- a/ldo.h
+++ b/ldo.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.h,v 1.27 2000/10/05 13:00:17 roberto Exp roberto $ 2** $Id: ldo.h,v 1.28 2000/10/06 12:45:25 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -26,6 +26,7 @@ void luaD_call (lua_State *L, StkId func, int nResults);
26void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults); 26void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults);
27void luaD_checkstack (lua_State *L, int n); 27void luaD_checkstack (lua_State *L, int n);
28 28
29void luaD_error (lua_State *L, const char *s);
29void luaD_breakrun (lua_State *L, int errcode); 30void luaD_breakrun (lua_State *L, int errcode);
30int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud); 31int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud);
31 32
diff --git a/lmem.c b/lmem.c
index 0756fe91..a631f4b1 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.42 2000/12/28 12:55:41 roberto Exp roberto $ 2** $Id: lmem.c,v 1.43 2001/01/19 13:20:30 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -134,7 +134,7 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
134 else if (*size >= limit/2) { /* cannot double it? */ 134 else if (*size >= limit/2) { /* cannot double it? */
135 if (*size < limit - MINPOWER2) /* try something smaller... */ 135 if (*size < limit - MINPOWER2) /* try something smaller... */
136 newsize = limit; /* still have at least MINPOWER2 free places */ 136 newsize = limit; /* still have at least MINPOWER2 free places */
137 else lua_error(L, errormsg); 137 else luaD_error(L, errormsg);
138 } 138 }
139 newblock = luaM_realloc(L, block, (luint32)(*size)*(luint32)size_elems, 139 newblock = luaM_realloc(L, block, (luint32)(*size)*(luint32)size_elems,
140 (luint32)newsize*(luint32)size_elems); 140 (luint32)newsize*(luint32)size_elems);
@@ -152,7 +152,7 @@ void *luaM_realloc (lua_State *L, void *block, luint32 oldsize, luint32 size) {
152 block = NULL; 152 block = NULL;
153 } 153 }
154 else if (size >= MAX_SIZET) 154 else if (size >= MAX_SIZET)
155 lua_error(L, "memory allocation error: block too big"); 155 luaD_error(L, "memory allocation error: block too big");
156 else { 156 else {
157 block = basicrealloc(block, oldsize, size); 157 block = basicrealloc(block, oldsize, size);
158 if (block == NULL) { 158 if (block == NULL) {
diff --git a/lobject.c b/lobject.c
index a341f055..d709d442 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.58 2000/12/28 12:55:41 roberto Exp roberto $ 2** $Id: lobject.c,v 1.59 2001/01/19 13:20:30 roberto Exp roberto $
3** Some generic functions over Lua objects 3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -12,6 +12,7 @@
12 12
13#include "lua.h" 13#include "lua.h"
14 14
15#include "ldo.h"
15#include "lmem.h" 16#include "lmem.h"
16#include "lobject.h" 17#include "lobject.h"
17#include "lstate.h" 18#include "lstate.h"
@@ -85,7 +86,7 @@ void luaO_verror (lua_State *L, const char *fmt, ...) {
85 va_start(argp, fmt); 86 va_start(argp, fmt);
86 vsprintf(buff, fmt, argp); 87 vsprintf(buff, fmt, argp);
87 va_end(argp); 88 va_end(argp);
88 lua_error(L, buff); 89 luaD_error(L, buff);
89} 90}
90 91
91 92
diff --git a/lstate.c b/lstate.c
index 5783f639..672aaba7 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.51 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: lstate.c,v 1.52 2001/01/22 18:01:38 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,6 +22,7 @@
22#ifdef LUA_DEBUG 22#ifdef LUA_DEBUG
23static lua_State *lua_state = NULL; 23static lua_State *lua_state = NULL;
24void luaB_opentests (lua_State *L); 24void luaB_opentests (lua_State *L);
25int islocked = 0;
25#endif 26#endif
26 27
27 28
@@ -44,6 +45,9 @@ struct Sopen {
44}; 45};
45 46
46 47
48static void close_state (lua_State *L);
49
50
47/* 51/*
48** open parts that may cause memory-allocation errors 52** open parts that may cause memory-allocation errors
49*/ 53*/
@@ -80,12 +84,13 @@ static void f_luaopen (lua_State *L, void *ud) {
80 G(L)->sizeref = 0; 84 G(L)->sizeref = 0;
81 G(L)->refFree = NONEXT; 85 G(L)->refFree = NONEXT;
82 G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); 86 G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
83 G(L)->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
84 luaD_init(L, so->stacksize); /* init stack */ 87 luaD_init(L, so->stacksize); /* init stack */
85 L->gt = luaH_new(L, 10); /* table of globals */ 88 L->gt = luaH_new(L, 10); /* table of globals */
86 luaS_init(L); 89 luaS_init(L);
87 luaX_init(L); 90 luaX_init(L);
88 luaT_init(L); 91 luaT_init(L);
92 G(L)->GCthreshold = 4*G(L)->nblocks;
93 LUA_EXIT; /* temporary exit to use the API */
89 lua_newtable(L); 94 lua_newtable(L);
90 lua_ref(L, 1); /* create registry */ 95 lua_ref(L, 1); /* create registry */
91 lua_register(L, LUA_ERRORMESSAGE, errormessage); 96 lua_register(L, LUA_ERRORMESSAGE, errormessage);
@@ -94,37 +99,41 @@ static void f_luaopen (lua_State *L, void *ud) {
94 if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ 99 if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
95 lua_assert(lua_gettop(L) == 0); 100 lua_assert(lua_gettop(L) == 0);
96#endif 101#endif
97 G(L)->GCthreshold = 2*G(L)->nblocks; 102 LUA_ENTRY; /* go back inside */
98 } 103 }
99} 104}
100 105
101 106
102LUA_API lua_State *lua_open (lua_State *OL, int stacksize) { 107LUA_API lua_State *lua_open (lua_State *OL, int stacksize) {
103 struct Sopen so; 108 struct Sopen so;
104 lua_State *L = luaM_new(OL, lua_State); 109 lua_State *L;
105 if (L == NULL) return NULL; /* memory allocation error */ 110 LUA_ENTRY;
106 L->G = NULL; 111 L = luaM_new(OL, lua_State);
107 L->stack = NULL; 112 if (L) { /* allocation OK? */
108 L->stacksize = 0; 113 L->G = NULL;
109 L->errorJmp = NULL; 114 L->stack = NULL;
110 L->callhook = NULL; 115 L->stacksize = 0;
111 L->linehook = NULL; 116 L->errorJmp = NULL;
112 L->allowhooks = 1; 117 L->callhook = NULL;
113 L->next = L->previous = L; 118 L->linehook = NULL;
114 so.stacksize = stacksize; 119 L->allowhooks = 1;
115 so.L = OL; 120 L->next = L->previous = L;
116 if (luaD_runprotected(L, f_luaopen, &so) != 0) { 121 so.stacksize = stacksize;
117 /* memory allocation error: free partial state */ 122 so.L = OL;
118 lua_close(L); 123 if (luaD_runprotected(L, f_luaopen, &so) != 0) {
119 return NULL; 124 /* memory allocation error: free partial state */
125 close_state(L);
126 L = NULL;
127 }
120 } 128 }
129 LUA_EXIT;
121 return L; 130 return L;
122} 131}
123 132
124 133
125LUA_API void lua_close (lua_State *L) { 134static void close_state (lua_State *L) {
126 lua_State *L1 = L->next; /* any surviving thread (if there is one) */ 135 lua_State *L1;
127 lua_assert(L != lua_state || lua_gettop(L) == 0); 136 L1 = L->next; /* any surviving thread (if there is one) */
128 if (L1 == L) L1 = NULL; /* no surviving threads */ 137 if (L1 == L) L1 = NULL; /* no surviving threads */
129 if (L1 != NULL) { /* are there other threads? */ 138 if (L1 != NULL) { /* are there other threads? */
130 lua_assert(L->previous != L); 139 lua_assert(L->previous != L);
@@ -144,6 +153,13 @@ LUA_API void lua_close (lua_State *L) {
144 } 153 }
145 luaM_freearray(L1, L->stack, L->stacksize, TObject); 154 luaM_freearray(L1, L->stack, L->stacksize, TObject);
146 luaM_freelem(L1, L, lua_State); 155 luaM_freelem(L1, L, lua_State);
156}
157
158LUA_API void lua_close (lua_State *L) {
159 lua_assert(L != lua_state || lua_gettop(L) == 0);
160 LUA_ENTRY;
161 close_state(L);
162 LUA_EXIT;
147 lua_assert(L != lua_state || memdebug_numblocks == 0); 163 lua_assert(L != lua_state || memdebug_numblocks == 0);
148 lua_assert(L != lua_state || memdebug_total == 0); 164 lua_assert(L != lua_state || memdebug_total == 0);
149} 165}
diff --git a/lstate.h b/lstate.h
index 84fadd2f..a4c95e60 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 1.44 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: lstate.h,v 1.45 2001/01/22 18:01:38 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -13,6 +13,26 @@
13 13
14 14
15 15
16#ifdef LUA_DEBUG
17extern int islocked;
18#define LUA_ENTRY lua_assert(islocked++ == 0)
19#define LUA_EXIT lua_assert(--islocked == 0)
20#endif
21
22
23/*
24** macros that control all entries and exits from Lua core machine
25** (mainly for thread syncronization)
26*/
27#ifndef LUA_ENTRY
28#define LUA_ENTRY
29#endif
30
31#ifndef LUA_EXIT
32#define LUA_EXIT
33#endif
34
35
16typedef TObject *StkId; /* index to stack elements */ 36typedef TObject *StkId; /* index to stack elements */
17 37
18 38
diff --git a/ltable.c b/ltable.c
index edf9d320..81406e05 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.64 2001/01/18 15:59:09 roberto Exp roberto $ 2** $Id: ltable.c,v 1.65 2001/01/19 13:20:30 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -20,6 +20,7 @@
20 20
21#include "lua.h" 21#include "lua.h"
22 22
23#include "ldo.h"
23#include "lmem.h" 24#include "lmem.h"
24#include "lobject.h" 25#include "lobject.h"
25#include "lstate.h" 26#include "lstate.h"
@@ -112,7 +113,7 @@ Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
112 else { 113 else {
113 const TObject *v = luaH_get(t, key); 114 const TObject *v = luaH_get(t, key);
114 if (v == &luaO_nilobject) 115 if (v == &luaO_nilobject)
115 lua_error(L, "invalid key for `next'"); 116 luaD_error(L, "invalid key for `next'");
116 i = (int)(((const char *)v - 117 i = (int)(((const char *)v -
117 (const char *)(&t->node[0].val)) / sizeof(Node)) + 1; 118 (const char *)(&t->node[0].val)) / sizeof(Node)) + 1;
118 } 119 }
@@ -152,7 +153,7 @@ void luaH_remove (Hash *t, TObject *key) {
152static void setnodevector (lua_State *L, Hash *t, luint32 size) { 153static void setnodevector (lua_State *L, Hash *t, luint32 size) {
153 int i; 154 int i;
154 if (size > MAX_INT) 155 if (size > MAX_INT)
155 lua_error(L, "table overflow"); 156 luaD_error(L, "table overflow");
156 t->node = luaM_newvector(L, size, Node); 157 t->node = luaM_newvector(L, size, Node);
157 for (i=0; i<(int)size; i++) { 158 for (i=0; i<(int)size; i++) {
158 setnilvalue(&t->node[i].key); 159 setnilvalue(&t->node[i].key);
@@ -259,7 +260,7 @@ static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
259 Node *mp = luaH_mainposition(t, key); 260 Node *mp = luaH_mainposition(t, key);
260 Node *n = mp; 261 Node *n = mp;
261 if (!mp) 262 if (!mp)
262 lua_error(L, "table index is nil"); 263 luaD_error(L, "table index is nil");
263 do { /* check whether `key' is somewhere in the chain */ 264 do { /* check whether `key' is somewhere in the chain */
264 if (luaO_equalObj(key, &n->key)) 265 if (luaO_equalObj(key, &n->key))
265 return &n->val; /* that's all */ 266 return &n->val; /* that's all */
diff --git a/ltm.c b/ltm.c
index 21fd7662..3f10a7b1 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.60 2001/01/18 15:59:09 roberto Exp roberto $ 2** $Id: ltm.c,v 1.61 2001/01/19 13:20:30 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -84,10 +84,14 @@ void luaT_init (lua_State *L) {
84 84
85 85
86LUA_API int lua_newtag (lua_State *L) { 86LUA_API int lua_newtag (lua_State *L) {
87 int tag;
88 LUA_ENTRY;
87 luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM, 89 luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM,
88 MAX_INT, "tag table overflow"); 90 MAX_INT, "tag table overflow");
89 init_entry(L, G(L)->ntag); 91 init_entry(L, G(L)->ntag);
90 return G(L)->ntag++; 92 tag = G(L)->ntag++;
93 LUA_EXIT;
94 return tag;
91} 95}
92 96
93 97
@@ -104,12 +108,14 @@ void luaT_realtag (lua_State *L, int tag) {
104 108
105LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) { 109LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
106 int e; 110 int e;
111 LUA_ENTRY;
107 checktag(L, tagto); 112 checktag(L, tagto);
108 checktag(L, tagfrom); 113 checktag(L, tagfrom);
109 for (e=0; e<TM_N; e++) { 114 for (e=0; e<TM_N; e++) {
110 if (luaT_validevent(tagto, e)) 115 if (luaT_validevent(tagto, e))
111 luaT_gettm(G(L), tagto, e) = luaT_gettm(G(L), tagfrom, e); 116 luaT_gettm(G(L), tagto, e) = luaT_gettm(G(L), tagfrom, e);
112 } 117 }
118 LUA_EXIT;
113 return tagto; 119 return tagto;
114} 120}
115 121
@@ -126,6 +132,7 @@ int luaT_tag (const TObject *o) {
126 132
127LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) { 133LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
128 int e; 134 int e;
135 LUA_ENTRY;
129 e = luaI_checkevent(L, event, t); 136 e = luaI_checkevent(L, event, t);
130 checktag(L, t); 137 checktag(L, t);
131 if (luaT_validevent(t, e) && luaT_gettm(G(L), t, e)) { 138 if (luaT_validevent(t, e) && luaT_gettm(G(L), t, e)) {
@@ -134,11 +141,14 @@ LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
134 else 141 else
135 setnilvalue(L->top); 142 setnilvalue(L->top);
136 incr_top; 143 incr_top;
144 LUA_EXIT;
137} 145}
138 146
139 147
140LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) { 148LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
141 int e = luaI_checkevent(L, event, t); 149 int e;
150 LUA_ENTRY;
151 e = luaI_checkevent(L, event, t);
142 checktag(L, t); 152 checktag(L, t);
143 if (!luaT_validevent(t, e)) 153 if (!luaT_validevent(t, e))
144 luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s", 154 luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
@@ -153,8 +163,9 @@ LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
153 luaT_gettm(G(L), t, e) = clvalue(L->top - 1); 163 luaT_gettm(G(L), t, e) = clvalue(L->top - 1);
154 break; 164 break;
155 default: 165 default:
156 lua_error(L, "tag method must be a function (or nil)"); 166 luaD_error(L, "tag method must be a function (or nil)");
157 } 167 }
158 L->top--; 168 L->top--;
169 LUA_EXIT;
159} 170}
160 171
diff --git a/lvm.c b/lvm.c
index 1dcfa5fd..db4ba548 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.154 2001/01/18 15:59:09 roberto Exp roberto $ 2** $Id: lvm.c,v 1.155 2001/01/19 13:20:30 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -219,7 +219,8 @@ static int call_binTM (lua_State *L, StkId top, TMS event) {
219 return 0; /* error */ 219 return 0; /* error */
220 } 220 }
221 } 221 }
222 lua_pushstring(L, luaT_eventname[event]); 222 setsvalue(L->top, luaS_new(L, luaT_eventname[event]));
223 incr_top;
223 luaD_callTM(L, tm, 3, 1); 224 luaD_callTM(L, tm, 3, 1);
224 return 1; 225 return 1;
225} 226}
@@ -287,7 +288,7 @@ void luaV_strconc (lua_State *L, int total, StkId top) {
287 tl += tsvalue(top-n-1)->len; 288 tl += tsvalue(top-n-1)->len;
288 n++; 289 n++;
289 } 290 }
290 if (tl > MAX_SIZET) lua_error(L, "string size overflow"); 291 if (tl > MAX_SIZET) luaD_error(L, "string size overflow");
291 buffer = luaO_openspace(L, tl); 292 buffer = luaO_openspace(L, tl);
292 tl = 0; 293 tl = 0;
293 for (i=n; i>0; i--) { /* concat all strings */ 294 for (i=n; i>0; i--) { /* concat all strings */
@@ -520,7 +521,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
520 } 521 }
521 case OP_POW: { 522 case OP_POW: {
522 if (!call_binTM(L, top, TM_POW)) 523 if (!call_binTM(L, top, TM_POW))
523 lua_error(L, "undefined operation"); 524 luaD_error(L, "undefined operation");
524 top--; 525 top--;
525 break; 526 break;
526 } 527 }
@@ -606,11 +607,11 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
606 } 607 }
607 case OP_FORPREP: { 608 case OP_FORPREP: {
608 if (tonumber(top-1)) 609 if (tonumber(top-1))
609 lua_error(L, "`for' step must be a number"); 610 luaD_error(L, "`for' step must be a number");
610 if (tonumber(top-2)) 611 if (tonumber(top-2))
611 lua_error(L, "`for' limit must be a number"); 612 luaD_error(L, "`for' limit must be a number");
612 if (tonumber(top-3)) 613 if (tonumber(top-3))
613 lua_error(L, "`for' initial value must be a number"); 614 luaD_error(L, "`for' initial value must be a number");
614 if (nvalue(top-1) > 0 ? 615 if (nvalue(top-1) > 0 ?
615 nvalue(top-3) > nvalue(top-2) : 616 nvalue(top-3) > nvalue(top-2) :
616 nvalue(top-3) < nvalue(top-2)) { /* `empty' loop? */ 617 nvalue(top-3) < nvalue(top-2)) { /* `empty' loop? */
@@ -623,7 +624,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
623 lua_assert(ttype(top-1) == LUA_TNUMBER); 624 lua_assert(ttype(top-1) == LUA_TNUMBER);
624 lua_assert(ttype(top-2) == LUA_TNUMBER); 625 lua_assert(ttype(top-2) == LUA_TNUMBER);
625 if (ttype(top-3) != LUA_TNUMBER) 626 if (ttype(top-3) != LUA_TNUMBER)
626 lua_error(L, "`for' index must be a number"); 627 luaD_error(L, "`for' index must be a number");
627 nvalue(top-3) += nvalue(top-1); /* increment index */ 628 nvalue(top-3) += nvalue(top-1); /* increment index */
628 if (nvalue(top-1) > 0 ? 629 if (nvalue(top-1) > 0 ?
629 nvalue(top-3) > nvalue(top-2) : 630 nvalue(top-3) > nvalue(top-2) :
@@ -636,7 +637,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
636 case OP_LFORPREP: { 637 case OP_LFORPREP: {
637 Node *node; 638 Node *node;
638 if (ttype(top-1) != LUA_TTABLE) 639 if (ttype(top-1) != LUA_TTABLE)
639 lua_error(L, "`for' table must be a table"); 640 luaD_error(L, "`for' table must be a table");
640 node = luaH_next(L, hvalue(top-1), &luaO_nilobject); 641 node = luaH_next(L, hvalue(top-1), &luaO_nilobject);
641 if (node == NULL) { /* `empty' loop? */ 642 if (node == NULL) { /* `empty' loop? */
642 top--; /* remove table */ 643 top--; /* remove table */