aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-22 11:12:07 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-22 11:12:07 -0200
commit29ede6aa13144ff7b69c57a87be1ee93f57ae896 (patch)
treeadcfb5dcff7db55481cd675349e23dec0e63c939
parent951897c09319ae5474a4b86bb7d615136577caa0 (diff)
downloadlua-29ede6aa13144ff7b69c57a87be1ee93f57ae896.tar.gz
lua-29ede6aa13144ff7b69c57a87be1ee93f57ae896.tar.bz2
lua-29ede6aa13144ff7b69c57a87be1ee93f57ae896.zip
first implementation of multiple states (reentrant code).
-rw-r--r--lapi.c374
-rw-r--r--lapi.h13
-rw-r--r--lauxlib.c75
-rw-r--r--lauxlib.h85
-rw-r--r--lbuffer.c35
-rw-r--r--lbuiltin.c591
-rw-r--r--lbuiltin.h5
-rw-r--r--ldblib.c186
-rw-r--r--ldo.c127
-rw-r--r--ldo.h28
-rw-r--r--lfunc.c38
-rw-r--r--lfunc.h10
-rw-r--r--lgc.c123
-rw-r--r--lgc.h6
-rw-r--r--linit.c14
-rw-r--r--liolib.c362
-rw-r--r--llex.c151
-rw-r--r--llex.h7
-rw-r--r--lmathlib.c144
-rw-r--r--lmem.c24
-rw-r--r--lmem.h22
-rw-r--r--lobject.c10
-rw-r--r--lobject.h20
-rw-r--r--lparser.c117
-rw-r--r--lparser.h7
-rw-r--r--lref.c26
-rw-r--r--lref.h8
-rw-r--r--lstate.c52
-rw-r--r--lstate.h4
-rw-r--r--lstring.c94
-rw-r--r--lstring.h26
-rw-r--r--lstrlib.c310
-rw-r--r--ltable.c80
-rw-r--r--ltable.h28
-rw-r--r--ltm.c66
-rw-r--r--ltm.h16
-rw-r--r--lua.c17
-rw-r--r--lua.h235
-rw-r--r--luadebug.h26
-rw-r--r--lualib.h33
-rw-r--r--lundump.c152
-rw-r--r--lundump.h8
-rw-r--r--lvm.c224
-rw-r--r--lvm.h26
44 files changed, 2075 insertions, 1930 deletions
diff --git a/lapi.c b/lapi.c
index 33e384d3..685f1ef7 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.55 1999/11/04 17:22:26 roberto Exp roberto $ 2** $Id: lapi.c,v 1.56 1999/11/11 17:02:40 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*/
@@ -7,6 +7,8 @@
7 7
8#include <string.h> 8#include <string.h>
9 9
10#define LUA_REENTRANT
11
10#include "lapi.h" 12#include "lapi.h"
11#include "lauxlib.h" 13#include "lauxlib.h"
12#include "ldo.h" 14#include "ldo.h"
@@ -29,8 +31,8 @@ const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
29 31
30 32
31 33
32TObject *luaA_Address (lua_Object o) { 34TObject *luaA_Address (lua_State *L, lua_Object o) {
33 return (o != LUA_NOOBJECT) ? Address(o) : NULL; 35 return (o != LUA_NOOBJECT) ? Address(L, o) : NULL;
34} 36}
35 37
36 38
@@ -60,27 +62,27 @@ static const TObject *luaA_protovalue (const TObject *o) {
60} 62}
61 63
62 64
63static void checkCparams (int nParams) { 65static void checkCparams (lua_State *L, int nParams) {
64 if (L->stack.top-L->stack.stack < L->Cstack.base+nParams) 66 if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
65 lua_error("API error - wrong number of arguments in C2lua stack"); 67 lua_error(L, "API error - wrong number of arguments in C2lua stack");
66} 68}
67 69
68 70
69static lua_Object put_luaObject (const TObject *o) { 71static lua_Object put_luaObject (lua_State *L, const TObject *o) {
70 luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); 72 luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base);
71 L->stack.stack[L->Cstack.base++] = *o; 73 L->stack.stack[L->Cstack.base++] = *o;
72 return L->Cstack.base; /* this is +1 real position (see Ref) */ 74 return L->Cstack.base; /* this is +1 real position (see Ref) */
73} 75}
74 76
75 77
76lua_Object luaA_putObjectOnTop (void) { 78lua_Object luaA_putObjectOnTop (lua_State *L) {
77 luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); 79 luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base);
78 L->stack.stack[L->Cstack.base++] = *(--L->stack.top); 80 L->stack.stack[L->Cstack.base++] = *(--L->stack.top);
79 return L->Cstack.base; /* this is +1 real position (see Ref) */ 81 return L->Cstack.base; /* this is +1 real position (see Ref) */
80} 82}
81 83
82 84
83static void top2LC (int n) { 85static void top2LC (lua_State *L, int n) {
84 /* Put the 'n' elements on the top as the Lua2C contents */ 86 /* Put the 'n' elements on the top as the Lua2C contents */
85 L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */ 87 L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */
86 L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */ 88 L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */
@@ -88,9 +90,9 @@ static void top2LC (int n) {
88} 90}
89 91
90 92
91lua_Object lua_pop (void) { 93lua_Object lua_pop (lua_State *L) {
92 checkCparams(1); 94 checkCparams(L, 1);
93 return luaA_putObjectOnTop(); 95 return luaA_putObjectOnTop(L);
94} 96}
95 97
96 98
@@ -98,248 +100,248 @@ lua_Object lua_pop (void) {
98** Get a parameter, returning the object handle or LUA_NOOBJECT on error. 100** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
99** 'number' must be 1 to get the first parameter. 101** 'number' must be 1 to get the first parameter.
100*/ 102*/
101lua_Object lua_lua2C (int number) { 103lua_Object lua_lua2C (lua_State *L, int number) {
102 if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT; 104 if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
103 /* Ref(L->stack.stack+(L->Cstack.lua2C+number-1)) == 105 /* Ref(L, L->stack.stack+(L->Cstack.lua2C+number-1)) ==
104 L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */ 106 L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */
105 return L->Cstack.lua2C+number; 107 return L->Cstack.lua2C+number;
106} 108}
107 109
108 110
109int lua_callfunction (lua_Object function) { 111int lua_callfunction (lua_State *L, lua_Object function) {
110 if (function == LUA_NOOBJECT) 112 if (function == LUA_NOOBJECT)
111 return 1; 113 return 1;
112 else { 114 else {
113 luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); 115 luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base);
114 set_normalized(L->stack.stack+L->Cstack.base, Address(function)); 116 set_normalized(L->stack.stack+L->Cstack.base, Address(L, function));
115 return luaD_protectedrun(); 117 return luaD_protectedrun(L);
116 } 118 }
117} 119}
118 120
119 121
120lua_Object lua_gettagmethod (int tag, const char *event) { 122lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
121 return put_luaObject(luaT_gettagmethod(tag, event)); 123 return put_luaObject(L, luaT_gettagmethod(L, tag, event));
122} 124}
123 125
124 126
125lua_Object lua_settagmethod (int tag, const char *event) { 127lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
126 checkCparams(1); 128 checkCparams(L, 1);
127 luaT_settagmethod(tag, event, L->stack.top-1); 129 luaT_settagmethod(L, tag, event, L->stack.top-1);
128 return luaA_putObjectOnTop(); 130 return luaA_putObjectOnTop(L);
129} 131}
130 132
131 133
132lua_Object lua_seterrormethod (void) { 134lua_Object lua_seterrormethod (lua_State *L) {
133 lua_Object temp; 135 lua_Object temp;
134 checkCparams(1); 136 checkCparams(L, 1);
135 temp = lua_getglobal("_ERRORMESSAGE"); 137 temp = lua_getglobal(L, "_ERRORMESSAGE");
136 lua_setglobal("_ERRORMESSAGE"); 138 lua_setglobal(L, "_ERRORMESSAGE");
137 return temp; 139 return temp;
138} 140}
139 141
140 142
141lua_Object lua_gettable (void) { 143lua_Object lua_gettable (lua_State *L) {
142 checkCparams(2); 144 checkCparams(L, 2);
143 luaV_gettable(); 145 luaV_gettable(L);
144 return luaA_putObjectOnTop(); 146 return luaA_putObjectOnTop(L);
145} 147}
146 148
147 149
148lua_Object lua_rawgettable (void) { 150lua_Object lua_rawgettable (lua_State *L) {
149 checkCparams(2); 151 checkCparams(L, 2);
150 if (ttype(L->stack.top-2) != LUA_T_ARRAY) 152 if (ttype(L->stack.top-2) != LUA_T_ARRAY)
151 lua_error("indexed expression not a table in rawgettable"); 153 lua_error(L, "indexed expression not a table in rawgettable");
152 *(L->stack.top-2) = *luaH_get(avalue(L->stack.top-2), L->stack.top-1); 154 *(L->stack.top-2) = *luaH_get(L, avalue(L->stack.top-2), L->stack.top-1);
153 --L->stack.top; 155 --L->stack.top;
154 return luaA_putObjectOnTop(); 156 return luaA_putObjectOnTop(L);
155} 157}
156 158
157 159
158void lua_settable (void) { 160void lua_settable (lua_State *L) {
159 checkCparams(3); 161 checkCparams(L, 3);
160 luaV_settable(L->stack.top-3); 162 luaV_settable(L, L->stack.top-3);
161 L->stack.top -= 2; /* pop table and index */ 163 L->stack.top -= 2; /* pop table and index */
162} 164}
163 165
164 166
165void lua_rawsettable (void) { 167void lua_rawsettable (lua_State *L) {
166 checkCparams(3); 168 checkCparams(L, 3);
167 luaV_rawsettable(L->stack.top-3); 169 luaV_rawsettable(L, L->stack.top-3);
168} 170}
169 171
170 172
171lua_Object lua_createtable (void) { 173lua_Object lua_createtable (lua_State *L) {
172 TObject o; 174 TObject o;
173 luaC_checkGC(); 175 luaC_checkGC(L);
174 avalue(&o) = luaH_new(0); 176 avalue(&o) = luaH_new(L, 0);
175 ttype(&o) = LUA_T_ARRAY; 177 ttype(&o) = LUA_T_ARRAY;
176 return put_luaObject(&o); 178 return put_luaObject(L, &o);
177} 179}
178 180
179 181
180lua_Object lua_getglobal (const char *name) { 182lua_Object lua_getglobal (lua_State *L, const char *name) {
181 luaD_checkstack(2); /* may need that to call T.M. */ 183 luaD_checkstack(L, 2); /* may need that to call T.M. */
182 luaV_getglobal(luaS_assertglobalbyname(name)); 184 luaV_getglobal(L, luaS_assertglobalbyname(L, name));
183 return luaA_putObjectOnTop(); 185 return luaA_putObjectOnTop(L);
184} 186}
185 187
186 188
187lua_Object lua_rawgetglobal (const char *name) { 189lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
188 GlobalVar *gv = luaS_assertglobalbyname(name); 190 GlobalVar *gv = luaS_assertglobalbyname(L, name);
189 return put_luaObject(&gv->value); 191 return put_luaObject(L, &gv->value);
190} 192}
191 193
192 194
193void lua_setglobal (const char *name) { 195void lua_setglobal (lua_State *L, const char *name) {
194 checkCparams(1); 196 checkCparams(L, 1);
195 luaD_checkstack(2); /* may need that to call T.M. */ 197 luaD_checkstack(L, 2); /* may need that to call T.M. */
196 luaV_setglobal(luaS_assertglobalbyname(name)); 198 luaV_setglobal(L, luaS_assertglobalbyname(L, name));
197} 199}
198 200
199 201
200void lua_rawsetglobal (const char *name) { 202void lua_rawsetglobal (lua_State *L, const char *name) {
201 GlobalVar *gv = luaS_assertglobalbyname(name); 203 GlobalVar *gv = luaS_assertglobalbyname(L, name);
202 checkCparams(1); 204 checkCparams(L, 1);
203 gv->value = *(--L->stack.top); 205 gv->value = *(--L->stack.top);
204} 206}
205 207
206 208
207const char *lua_type (lua_Object o) { 209const char *lua_type (lua_State *L, lua_Object o) {
208 return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(Address(o)); 210 return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(L, Address(L, o));
209} 211}
210 212
211int lua_isnil (lua_Object o) { 213int lua_isnil (lua_State *L, lua_Object o) {
212 return (o != LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL); 214 return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_NIL);
213} 215}
214 216
215int lua_istable (lua_Object o) { 217int lua_istable (lua_State *L, lua_Object o) {
216 return (o != LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY); 218 return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_ARRAY);
217} 219}
218 220
219int lua_isuserdata (lua_Object o) { 221int lua_isuserdata (lua_State *L, lua_Object o) {
220 return (o != LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA); 222 return (o != LUA_NOOBJECT) && (ttype(Address(L, o)) == LUA_T_USERDATA);
221} 223}
222 224
223int lua_iscfunction (lua_Object o) { 225int lua_iscfunction (lua_State *L, lua_Object o) {
224 return (lua_tag(o) == LUA_T_CPROTO); 226 return (lua_tag(L, o) == LUA_T_CPROTO);
225} 227}
226 228
227int lua_isnumber (lua_Object o) { 229int lua_isnumber (lua_State *L, lua_Object o) {
228 return (o != LUA_NOOBJECT) && (tonumber(Address(o)) == 0); 230 return (o != LUA_NOOBJECT) && (tonumber(Address(L, o)) == 0);
229} 231}
230 232
231int lua_isstring (lua_Object o) { 233int lua_isstring (lua_State *L, lua_Object o) {
232 int t = lua_tag(o); 234 int t = lua_tag(L, o);
233 return (t == LUA_T_STRING) || (t == LUA_T_NUMBER); 235 return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
234} 236}
235 237
236int lua_isfunction (lua_Object o) { 238int lua_isfunction (lua_State *L, lua_Object o) {
237 int t = lua_tag(o); 239 int t = lua_tag(L, o);
238 return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO); 240 return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO);
239} 241}
240 242
241int lua_equalobj (lua_Object o1, lua_Object o2) { 243int lua_equalobj (lua_State *L, lua_Object o1, lua_Object o2) {
242 if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT) return 0; 244 if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT) return 0;
243 else return luaO_equalObj(Address(o1), Address(o2)); 245 else return luaO_equalObj(Address(L, o1), Address(L, o2));
244} 246}
245 247
246 248
247double lua_getnumber (lua_Object object) { 249double lua_getnumber (lua_State *L, lua_Object object) {
248 if (object == LUA_NOOBJECT) return 0.0; 250 if (object == LUA_NOOBJECT) return 0.0;
249 if (tonumber(Address(object))) return 0.0; 251 if (tonumber(Address(L, object))) return 0.0;
250 else return (nvalue(Address(object))); 252 else return (nvalue(Address(L, object)));
251} 253}
252 254
253const char *lua_getstring (lua_Object object) { 255const char *lua_getstring (lua_State *L, lua_Object object) {
254 luaC_checkGC(); /* "tostring" may create a new string */ 256 luaC_checkGC(L); /* `tostring' may create a new string */
255 if (object == LUA_NOOBJECT || tostring(Address(object))) 257 if (object == LUA_NOOBJECT || tostring(L, Address(L, object)))
256 return NULL; 258 return NULL;
257 else return (svalue(Address(object))); 259 else return (svalue(Address(L, object)));
258} 260}
259 261
260long lua_strlen (lua_Object object) { 262long lua_strlen (lua_State *L, lua_Object object) {
261 luaC_checkGC(); /* "tostring" may create a new string */ 263 luaC_checkGC(L); /* `tostring' may create a new string */
262 if (object == LUA_NOOBJECT || tostring(Address(object))) 264 if (object == LUA_NOOBJECT || tostring(L, Address(L, object)))
263 return 0L; 265 return 0L;
264 else return (tsvalue(Address(object))->u.s.len); 266 else return (tsvalue(Address(L, object))->u.s.len);
265} 267}
266 268
267void *lua_getuserdata (lua_Object object) { 269void *lua_getuserdata (lua_State *L, lua_Object object) {
268 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA) 270 if (object == LUA_NOOBJECT || ttype(Address(L, object)) != LUA_T_USERDATA)
269 return NULL; 271 return NULL;
270 else return tsvalue(Address(object))->u.d.value; 272 else return tsvalue(Address(L, object))->u.d.value;
271} 273}
272 274
273lua_CFunction lua_getcfunction (lua_Object object) { 275lua_CFunction lua_getcfunction (lua_State *L, lua_Object object) {
274 if (!lua_iscfunction(object)) 276 if (!lua_iscfunction(L, object))
275 return NULL; 277 return NULL;
276 else return fvalue(luaA_protovalue(Address(object))); 278 else return fvalue(luaA_protovalue(Address(L, object)));
277} 279}
278 280
279 281
280void lua_pushnil (void) { 282void lua_pushnil (lua_State *L) {
281 ttype(L->stack.top) = LUA_T_NIL; 283 ttype(L->stack.top) = LUA_T_NIL;
282 incr_top; 284 incr_top;
283} 285}
284 286
285void lua_pushnumber (double n) { 287void lua_pushnumber (lua_State *L, double n) {
286 ttype(L->stack.top) = LUA_T_NUMBER; 288 ttype(L->stack.top) = LUA_T_NUMBER;
287 nvalue(L->stack.top) = n; 289 nvalue(L->stack.top) = n;
288 incr_top; 290 incr_top;
289} 291}
290 292
291void lua_pushlstring (const char *s, long len) { 293void lua_pushlstring (lua_State *L, const char *s, long len) {
292 tsvalue(L->stack.top) = luaS_newlstr(s, len); 294 tsvalue(L->stack.top) = luaS_newlstr(L, s, len);
293 ttype(L->stack.top) = LUA_T_STRING; 295 ttype(L->stack.top) = LUA_T_STRING;
294 incr_top; 296 incr_top;
295 luaC_checkGC(); 297 luaC_checkGC(L);
296} 298}
297 299
298void lua_pushstring (const char *s) { 300void lua_pushstring (lua_State *L, const char *s) {
299 if (s == NULL) 301 if (s == NULL)
300 lua_pushnil(); 302 lua_pushnil(L);
301 else 303 else
302 lua_pushlstring(s, strlen(s)); 304 lua_pushlstring(L, s, strlen(s));
303} 305}
304 306
305void lua_pushcclosure (lua_CFunction fn, int n) { 307void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
306 if (fn == NULL) 308 if (fn == NULL)
307 lua_error("API error - attempt to push a NULL Cfunction"); 309 lua_error(L, "API error - attempt to push a NULL Cfunction");
308 checkCparams(n); 310 checkCparams(L, n);
309 ttype(L->stack.top) = LUA_T_CPROTO; 311 ttype(L->stack.top) = LUA_T_CPROTO;
310 fvalue(L->stack.top) = fn; 312 fvalue(L->stack.top) = fn;
311 incr_top; 313 incr_top;
312 luaV_closure(n); 314 luaV_closure(L, n);
313 luaC_checkGC(); 315 luaC_checkGC(L);
314} 316}
315 317
316void lua_pushusertag (void *u, int tag) { 318void lua_pushusertag (lua_State *L, void *u, int tag) {
317 if (tag < 0 && tag != LUA_ANYTAG) 319 if (tag < 0 && tag != LUA_ANYTAG)
318 luaT_realtag(tag); /* error if tag is not valid */ 320 luaT_realtag(L, tag); /* error if tag is not valid */
319 tsvalue(L->stack.top) = luaS_createudata(u, tag); 321 tsvalue(L->stack.top) = luaS_createudata(L, u, tag);
320 ttype(L->stack.top) = LUA_T_USERDATA; 322 ttype(L->stack.top) = LUA_T_USERDATA;
321 incr_top; 323 incr_top;
322 luaC_checkGC(); 324 luaC_checkGC(L);
323} 325}
324 326
325void luaA_pushobject (const TObject *o) { 327void luaA_pushobject (lua_State *L, const TObject *o) {
326 *L->stack.top = *o; 328 *L->stack.top = *o;
327 incr_top; 329 incr_top;
328} 330}
329 331
330void lua_pushobject (lua_Object o) { 332void lua_pushobject (lua_State *L, lua_Object o) {
331 if (o == LUA_NOOBJECT) 333 if (o == LUA_NOOBJECT)
332 lua_error("API error - attempt to push a NOOBJECT"); 334 lua_error(L, "API error - attempt to push a NOOBJECT");
333 set_normalized(L->stack.top, Address(o)); 335 set_normalized(L->stack.top, Address(L, o));
334 incr_top; 336 incr_top;
335} 337}
336 338
337 339
338int lua_tag (lua_Object lo) { 340int lua_tag (lua_State *L, lua_Object lo) {
339 if (lo == LUA_NOOBJECT) 341 if (lo == LUA_NOOBJECT)
340 return LUA_T_NIL; 342 return LUA_T_NIL;
341 else { 343 else {
342 const TObject *o = Address(lo); 344 const TObject *o = Address(L, lo);
343 int t; 345 int t;
344 switch (t = ttype(o)) { 346 switch (t = ttype(o)) {
345 case LUA_T_USERDATA: 347 case LUA_T_USERDATA:
@@ -354,7 +356,7 @@ int lua_tag (lua_Object lo) {
354 return o->value.cl->consts[0].ttype; 356 return o->value.cl->consts[0].ttype;
355#ifdef DEBUG 357#ifdef DEBUG
356 case LUA_T_LINE: 358 case LUA_T_LINE:
357 LUA_INTERNALERROR("invalid type"); 359 LUA_INTERNALERROR(L, "invalid type");
358#endif 360#endif
359 default: 361 default:
360 return t; 362 return t;
@@ -363,9 +365,9 @@ int lua_tag (lua_Object lo) {
363} 365}
364 366
365 367
366void lua_settag (int tag) { 368void lua_settag (lua_State *L, int tag) {
367 checkCparams(1); 369 checkCparams(L, 1);
368 luaT_realtag(tag); 370 luaT_realtag(L, tag);
369 switch (ttype(L->stack.top-1)) { 371 switch (ttype(L->stack.top-1)) {
370 case LUA_T_ARRAY: 372 case LUA_T_ARRAY:
371 (L->stack.top-1)->value.a->htag = tag; 373 (L->stack.top-1)->value.a->htag = tag;
@@ -374,20 +376,20 @@ void lua_settag (int tag) {
374 (L->stack.top-1)->value.ts->u.d.tag = tag; 376 (L->stack.top-1)->value.ts->u.d.tag = tag;
375 break; 377 break;
376 default: 378 default:
377 luaL_verror("cannot change the tag of a %.20s", 379 luaL_verror(L, "cannot change the tag of a %.20s",
378 luaO_typename(L->stack.top-1)); 380 luaO_typename(L, L->stack.top-1));
379 } 381 }
380 L->stack.top--; 382 L->stack.top--;
381} 383}
382 384
383 385
384GlobalVar *luaA_nextvar (TaggedString *ts) { 386GlobalVar *luaA_nextvar (lua_State *L, TaggedString *ts) {
385 GlobalVar *gv; 387 GlobalVar *gv;
386 if (ts == NULL) 388 if (ts == NULL)
387 gv = L->rootglobal; /* first variable */ 389 gv = L->rootglobal; /* first variable */
388 else { 390 else {
389 /* check whether name is in global var list */ 391 /* check whether name is in global var list */
390 luaL_arg_check(ts->u.s.gv, 1, "variable name expected"); 392 luaL_arg_check(L, ts->u.s.gv, 1, "variable name expected");
391 gv = ts->u.s.gv->next; /* get next */ 393 gv = ts->u.s.gv->next; /* get next */
392 } 394 }
393 while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */ 395 while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */
@@ -395,33 +397,33 @@ GlobalVar *luaA_nextvar (TaggedString *ts) {
395 if (gv) { 397 if (gv) {
396 ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = gv->name; 398 ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = gv->name;
397 incr_top; 399 incr_top;
398 luaA_pushobject(&gv->value); 400 luaA_pushobject(L, &gv->value);
399 } 401 }
400 return gv; 402 return gv;
401} 403}
402 404
403 405
404const char *lua_nextvar (const char *varname) { 406const char *lua_nextvar (lua_State *L, const char *varname) {
405 TaggedString *ts = (varname == NULL) ? NULL : luaS_new(varname); 407 TaggedString *ts = (varname == NULL) ? NULL : luaS_new(L, varname);
406 GlobalVar *gv = luaA_nextvar(ts); 408 GlobalVar *gv = luaA_nextvar(L, ts);
407 if (gv) { 409 if (gv) {
408 top2LC(2); 410 top2LC(L, 2);
409 return gv->name->str; 411 return gv->name->str;
410 } 412 }
411 else { 413 else {
412 top2LC(0); 414 top2LC(L, 0);
413 return NULL; 415 return NULL;
414 } 416 }
415} 417}
416 418
417 419
418int luaA_next (const Hash *t, int i) { 420int luaA_next (lua_State *L, const Hash *t, int i) {
419 int tsize = t->size; 421 int tsize = t->size;
420 for (; i<tsize; i++) { 422 for (; i<tsize; i++) {
421 Node *n = node(t, i); 423 Node *n = node(L, t, i);
422 if (ttype(val(n)) != LUA_T_NIL) { 424 if (ttype(val(L, n)) != LUA_T_NIL) {
423 luaA_pushobject(key(n)); 425 luaA_pushobject(L, key(L, n));
424 luaA_pushobject(val(n)); 426 luaA_pushobject(L, val(L, n));
425 return i+1; /* index to be used next time */ 427 return i+1; /* index to be used next time */
426 } 428 }
427 } 429 }
@@ -429,12 +431,12 @@ int luaA_next (const Hash *t, int i) {
429} 431}
430 432
431 433
432int lua_next (lua_Object o, int i) { 434int lua_next (lua_State *L, lua_Object o, int i) {
433 const TObject *t = Address(o); 435 const TObject *t = Address(L, o);
434 if (ttype(t) != LUA_T_ARRAY) 436 if (ttype(t) != LUA_T_ARRAY)
435 lua_error("API error - object is not a table in `lua_next'"); 437 lua_error(L, "API error - object is not a table in `lua_next'");
436 i = luaA_next(avalue(t), i); 438 i = luaA_next(L, avalue(t), i);
437 top2LC((i==0) ? 0 : 2); 439 top2LC(L, (i==0) ? 0 : 2);
438 return i; 440 return i;
439} 441}
440 442
@@ -446,25 +448,21 @@ int lua_next (lua_Object o, int i) {
446** ======================================================= 448** =======================================================
447*/ 449*/
448 450
449lua_State *lua_setstate (lua_State *st) {
450 lua_State *old = lua_state;
451 lua_state = st;
452 return old;
453}
454 451
455lua_LHFunction lua_setlinehook (lua_LHFunction func) { 452
453lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) {
456 lua_LHFunction old = L->linehook; 454 lua_LHFunction old = L->linehook;
457 L->linehook = func; 455 L->linehook = func;
458 return old; 456 return old;
459} 457}
460 458
461lua_CHFunction lua_setcallhook (lua_CHFunction func) { 459lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func) {
462 lua_CHFunction old = L->callhook; 460 lua_CHFunction old = L->callhook;
463 L->callhook = func; 461 L->callhook = func;
464 return old; 462 return old;
465} 463}
466 464
467int lua_setdebug (int debug) { 465int lua_setdebug (lua_State *L, int debug) {
468 int old = L->debug; 466 int old = L->debug;
469 L->debug = debug; 467 L->debug = debug;
470 return old; 468 return old;
@@ -480,44 +478,44 @@ int lua_setdebug (int debug) {
480*/ 478*/
481 479
482 480
483lua_Function lua_stackedfunction (int level) { 481lua_Function lua_stackedfunction (lua_State *L, int level) {
484 StkId i; 482 StkId i;
485 for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) { 483 for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) {
486 int t = L->stack.stack[i].ttype; 484 int t = L->stack.stack[i].ttype;
487 if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK) 485 if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
488 if (level-- == 0) 486 if (level-- == 0)
489 return Ref(L->stack.stack+i); 487 return Ref(L, L->stack.stack+i);
490 } 488 }
491 return LUA_NOOBJECT; 489 return LUA_NOOBJECT;
492} 490}
493 491
494 492
495int lua_nups (lua_Function func) { 493int lua_nups (lua_State *L, lua_Function func) {
496 const TObject *o = luaA_Address(func); 494 const TObject *o = luaA_Address(L, func);
497 return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems; 495 return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems;
498} 496}
499 497
500 498
501int lua_currentline (lua_Function func) { 499int lua_currentline (lua_State *L, lua_Function func) {
502 const TObject *f = Address(func); 500 const TObject *f = Address(L, func);
503 return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ? 501 return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ?
504 (f+1)->value.i : -1; 502 (f+1)->value.i : -1;
505} 503}
506 504
507 505
508lua_Object lua_getlocal (lua_Function func, int local_number, 506lua_Object lua_getlocal (lua_State *L, lua_Function func, int local_number,
509 const char **name) { 507 const char **name) {
510 /* check whether func is a Lua function */ 508 /* check whether func is a Lua function */
511 if (lua_tag(func) != LUA_T_PROTO) 509 if (lua_tag(L, func) != LUA_T_PROTO)
512 return LUA_NOOBJECT; 510 return LUA_NOOBJECT;
513 else { 511 else {
514 TObject *f = Address(func); 512 TObject *f = Address(L, func);
515 TProtoFunc *fp = luaA_protovalue(f)->value.tf; 513 TProtoFunc *fp = luaA_protovalue(f)->value.tf;
516 *name = luaF_getlocalname(fp, local_number, lua_currentline(func)); 514 *name = luaF_getlocalname(fp, local_number, lua_currentline(L, func));
517 if (*name) { 515 if (*name) {
518 /* if "*name", there must be a LUA_T_LINE */ 516 /* if "*name", there must be a LUA_T_LINE */
519 /* therefore, f+2 points to function base */ 517 /* therefore, f+2 points to function base */
520 return put_luaObject((f+2)+(local_number-1)); 518 return put_luaObject(L, (f+2)+(local_number-1));
521 } 519 }
522 else 520 else
523 return LUA_NOOBJECT; 521 return LUA_NOOBJECT;
@@ -525,15 +523,16 @@ lua_Object lua_getlocal (lua_Function func, int local_number,
525} 523}
526 524
527 525
528int lua_setlocal (lua_Function func, int local_number) { 526int lua_setlocal (lua_State *L, lua_Function func, int local_number) {
529 /* check whether func is a Lua function */ 527 /* check whether func is a Lua function */
530 if (lua_tag(func) != LUA_T_PROTO) 528 if (lua_tag(L, func) != LUA_T_PROTO)
531 return 0; 529 return 0;
532 else { 530 else {
533 TObject *f = Address(func); 531 TObject *f = Address(L, func);
534 TProtoFunc *fp = luaA_protovalue(f)->value.tf; 532 TProtoFunc *fp = luaA_protovalue(f)->value.tf;
535 const char *name = luaF_getlocalname(fp, local_number, lua_currentline(func)); 533 const char *name = luaF_getlocalname(fp, local_number,
536 checkCparams(1); 534 lua_currentline(L, func));
535 checkCparams(L, 1);
537 --L->stack.top; 536 --L->stack.top;
538 if (name) { 537 if (name) {
539 /* if "name", there must be a LUA_T_LINE */ 538 /* if "name", there must be a LUA_T_LINE */
@@ -547,11 +546,12 @@ int lua_setlocal (lua_Function func, int local_number) {
547} 546}
548 547
549 548
550void lua_funcinfo (lua_Object func, const char **source, int *linedefined) { 549void lua_funcinfo (lua_State *L, lua_Object func,
551 if (!lua_isfunction(func)) 550 const char **source, int *linedefined) {
552 lua_error("API error - `funcinfo' called with a non-function value"); 551 if (!lua_isfunction(L, func))
552 lua_error(L, "API error - `funcinfo' called with a non-function value");
553 else { 553 else {
554 const TObject *f = luaA_protovalue(Address(func)); 554 const TObject *f = luaA_protovalue(Address(L, func));
555 if (normalized_type(f) == LUA_T_PROTO) { 555 if (normalized_type(f) == LUA_T_PROTO) {
556 *source = tfvalue(f)->source->str; 556 *source = tfvalue(f)->source->str;
557 *linedefined = tfvalue(f)->lineDefined; 557 *linedefined = tfvalue(f)->lineDefined;
@@ -564,23 +564,23 @@ void lua_funcinfo (lua_Object func, const char **source, int *linedefined) {
564} 564}
565 565
566 566
567static int checkfunc (TObject *o) { 567static int checkfunc (lua_State *L, TObject *o) {
568 return luaO_equalObj(o, L->stack.top); 568 return luaO_equalObj(o, L->stack.top);
569} 569}
570 570
571 571
572const char *lua_getobjname (lua_Object o, const char **name) { 572const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
573 /* try to find a name for given function */ 573 /* try to find a name for given function */
574 GlobalVar *g; 574 GlobalVar *g;
575 set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */ 575 set_normalized(L->stack.top, Address(L, o)); /* to be used by `checkfunc' */
576 for (g=L->rootglobal; g; g=g->next) { 576 for (g=L->rootglobal; g; g=g->next) {
577 if (checkfunc(&g->value)) { 577 if (checkfunc(L, &g->value)) {
578 *name = g->name->str; 578 *name = g->name->str;
579 return "global"; 579 return "global";
580 } 580 }
581 } 581 }
582 /* not found: try tag methods */ 582 /* not found: try tag methods */
583 if ((*name = luaT_travtagmethods(checkfunc)) != NULL) 583 if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL)
584 return "tag-method"; 584 return "tag-method";
585 else return ""; /* not found at all */ 585 else return ""; /* not found at all */
586} 586}
@@ -600,34 +600,34 @@ const char *lua_getobjname (lua_Object o, const char **name) {
600#endif 600#endif
601 601
602 602
603void lua_beginblock (void) { 603void lua_beginblock (lua_State *L) {
604 luaM_growvector(L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack, 604 luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
605 "too many nested blocks", MAX_C_BLOCKS); 605 "too many nested blocks", MAX_C_BLOCKS);
606 L->Cblocks[L->numCblocks] = L->Cstack; 606 L->Cblocks[L->numCblocks] = L->Cstack;
607 L->numCblocks++; 607 L->numCblocks++;
608} 608}
609 609
610void lua_endblock (void) { 610void lua_endblock (lua_State *L) {
611 --L->numCblocks; 611 --L->numCblocks;
612 L->Cstack = L->Cblocks[L->numCblocks]; 612 L->Cstack = L->Cblocks[L->numCblocks];
613 luaD_adjusttop(L->Cstack.base); 613 luaD_adjusttop(L, L->Cstack.base);
614} 614}
615 615
616 616
617 617
618int lua_ref (int lock) { 618int lua_ref (lua_State *L, int lock) {
619 int ref; 619 int ref;
620 checkCparams(1); 620 checkCparams(L, 1);
621 ref = luaR_ref(L->stack.top-1, lock); 621 ref = luaR_ref(L, L->stack.top-1, lock);
622 L->stack.top--; 622 L->stack.top--;
623 return ref; 623 return ref;
624} 624}
625 625
626 626
627 627
628lua_Object lua_getref (int ref) { 628lua_Object lua_getref (lua_State *L, int ref) {
629 const TObject *o = luaR_getref(ref); 629 const TObject *o = luaR_getref(L, ref);
630 return (o ? put_luaObject(o) : LUA_NOOBJECT); 630 return (o ? put_luaObject(L, o) : LUA_NOOBJECT);
631} 631}
632 632
633/* }====================================================== */ 633/* }====================================================== */
diff --git a/lapi.h b/lapi.h
index 5d72060d..6db4d16e 100644
--- a/lapi.h
+++ b/lapi.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.h,v 1.7 1999/09/21 16:10:13 roberto Exp roberto $ 2** $Id: lapi.h,v 1.8 1999/11/04 17:22:26 roberto Exp roberto $
3** Auxiliary functions from Lua API 3** Auxiliary functions from Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -8,14 +8,13 @@
8#define lapi_h 8#define lapi_h
9 9
10 10
11#include "lua.h"
12#include "lobject.h" 11#include "lobject.h"
13 12
14 13
15TObject *luaA_Address (lua_Object o); 14TObject *luaA_Address (lua_State *L, lua_Object o);
16void luaA_pushobject (const TObject *o); 15void luaA_pushobject (lua_State *L, const TObject *o);
17GlobalVar *luaA_nextvar (TaggedString *g); 16GlobalVar *luaA_nextvar (lua_State *L, TaggedString *g);
18int luaA_next (const Hash *t, int i); 17int luaA_next (lua_State *L, const Hash *t, int i);
19lua_Object luaA_putObjectOnTop (void); 18lua_Object luaA_putObjectOnTop (lua_State *L);
20 19
21#endif 20#endif
diff --git a/lauxlib.c b/lauxlib.c
index e97a20d5..d713cd2e 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.19 1999/09/06 13:13:03 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.20 1999/10/05 18:33:43 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -14,6 +14,8 @@
14** With care, these functions can be used by other libraries. 14** With care, these functions can be used by other libraries.
15*/ 15*/
16 16
17#define LUA_REENTRANT
18
17#include "lauxlib.h" 19#include "lauxlib.h"
18#include "lua.h" 20#include "lua.h"
19#include "luadebug.h" 21#include "luadebug.h"
@@ -28,87 +30,86 @@ int luaL_findstring (const char *name, const char *const list[]) {
28 return -1; /* name not found */ 30 return -1; /* name not found */
29} 31}
30 32
31void luaL_argerror (int numarg, const char *extramsg) { 33void luaL_argerror (lua_State *L, int numarg, const char *extramsg) {
32 lua_Function f = lua_stackedfunction(0); 34 lua_Function f = lua_stackedfunction(L, 0);
33 const char *funcname; 35 const char *funcname;
34 lua_getobjname(f, &funcname); 36 lua_getobjname(L, f, &funcname);
35 numarg -= lua_nups(f); 37 numarg -= lua_nups(L, f);
36 if (funcname == NULL) 38 if (funcname == NULL)
37 funcname = "?"; 39 funcname = "?";
38 luaL_verror("bad argument #%d to function `%.50s' (%.100s)", 40 luaL_verror(L, "bad argument #%d to function `%.50s' (%.100s)",
39 numarg, funcname, extramsg); 41 numarg, funcname, extramsg);
40} 42}
41 43
42static const char *checkstr (lua_Object o, int numArg, long *len) { 44static const char *checkstr (lua_State *L, lua_Object o, int n, long *len) {
43 const char *s = lua_getstring(o); 45 const char *s = lua_getstring(L, o);
44 luaL_arg_check(s, numArg, "string expected"); 46 luaL_arg_check(L, s, n, "string expected");
45 if (len) *len = lua_strlen(o); 47 if (len) *len = lua_strlen(L, o);
46 return s; 48 return s;
47} 49}
48 50
49const char *luaL_check_lstr (int numArg, long *len) { 51const char *luaL_check_lstr (lua_State *L, int n, long *len) {
50 return checkstr(lua_getparam(numArg), numArg, len); 52 return checkstr(L, lua_getparam(L, n), n, len);
51} 53}
52 54
53const char *luaL_opt_lstr (int numArg, const char *def, long *len) { 55const char *luaL_opt_lstr (lua_State *L, int n, const char *def, long *len) {
54 lua_Object o = lua_getparam(numArg); 56 lua_Object o = lua_getparam(L, n);
55 if (o == LUA_NOOBJECT) { 57 if (o == LUA_NOOBJECT) {
56 if (len) *len = def ? strlen(def) : 0; 58 if (len) *len = def ? strlen(def) : 0;
57 return def; 59 return def;
58 } 60 }
59 else return checkstr(o, numArg, len); 61 else return checkstr(L, o, n, len);
60} 62}
61 63
62double luaL_check_number (int numArg) { 64double luaL_check_number (lua_State *L, int n) {
63 lua_Object o = lua_getparam(numArg); 65 lua_Object o = lua_getparam(L, n);
64 luaL_arg_check(lua_isnumber(o), numArg, "number expected"); 66 luaL_arg_check(L, lua_isnumber(L, o), n, "number expected");
65 return lua_getnumber(o); 67 return lua_getnumber(L, o);
66} 68}
67 69
68 70
69double luaL_opt_number (int numArg, double def) { 71double luaL_opt_number (lua_State *L, int n, double def) {
70 lua_Object o = lua_getparam(numArg); 72 lua_Object o = lua_getparam(L, n);
71 if (o == LUA_NOOBJECT) return def; 73 if (o == LUA_NOOBJECT) return def;
72 else { 74 else {
73 luaL_arg_check(lua_isnumber(o), numArg, "number expected"); 75 luaL_arg_check(L, lua_isnumber(L, o), n, "number expected");
74 return lua_getnumber(o); 76 return lua_getnumber(L, o);
75 } 77 }
76} 78}
77 79
78 80
79lua_Object luaL_tablearg (int arg) { 81lua_Object luaL_tablearg (lua_State *L, int arg) {
80 lua_Object o = lua_getparam(arg); 82 lua_Object o = lua_getparam(L, arg);
81 luaL_arg_check(lua_istable(o), arg, "table expected"); 83 luaL_arg_check(L, lua_istable(L, o), arg, "table expected");
82 return o; 84 return o;
83} 85}
84 86
85lua_Object luaL_functionarg (int arg) { 87lua_Object luaL_functionarg (lua_State *L, int arg) {
86 lua_Object o = lua_getparam(arg); 88 lua_Object o = lua_getparam(L, arg);
87 luaL_arg_check(lua_isfunction(o), arg, "function expected"); 89 luaL_arg_check(L, lua_isfunction(L, o), arg, "function expected");
88 return o; 90 return o;
89} 91}
90 92
91lua_Object luaL_nonnullarg (int numArg) { 93lua_Object luaL_nonnullarg (lua_State *L, int n) {
92 lua_Object o = lua_getparam(numArg); 94 lua_Object o = lua_getparam(L, n);
93 luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected"); 95 luaL_arg_check(L, o != LUA_NOOBJECT, n, "value expected");
94 return o; 96 return o;
95} 97}
96 98
97void luaL_openlib (const struct luaL_reg *l, int n) { 99void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
98 int i; 100 int i;
99 lua_open(); /* make sure lua is already open */
100 for (i=0; i<n; i++) 101 for (i=0; i<n; i++)
101 lua_register(l[i].name, l[i].func); 102 lua_register(L, l[i].name, l[i].func);
102} 103}
103 104
104 105
105void luaL_verror (const char *fmt, ...) { 106void luaL_verror (lua_State *L, const char *fmt, ...) {
106 char buff[500]; 107 char buff[500];
107 va_list argp; 108 va_list argp;
108 va_start(argp, fmt); 109 va_start(argp, fmt);
109 vsprintf(buff, fmt, argp); 110 vsprintf(buff, fmt, argp);
110 va_end(argp); 111 va_end(argp);
111 lua_error(buff); 112 lua_error(L, buff);
112} 113}
113 114
114 115
diff --git a/lauxlib.h b/lauxlib.h
index 7f17da00..708297f5 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.12 1999/03/10 14:19:41 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.13 1999/08/16 20:52:00 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -18,36 +18,77 @@ struct luaL_reg {
18}; 18};
19 19
20 20
21#ifdef LUA_REENTRANT
22
23#define luaL_arg_check(L, cond,numarg,extramsg) if (!(cond)) \
24 luaL_argerror(L, numarg,extramsg)
25#define luaL_check_string(L, n) (luaL_check_lstr(L, (n), NULL))
26#define luaL_opt_string(L, n, d) (luaL_opt_lstr(L, (n), (d), NULL))
27#define luaL_check_int(L, n) ((int)luaL_check_number(L, n))
28#define luaL_check_long(L, n) ((long)luaL_check_number(L, n))
29#define luaL_opt_int(L, n,d) ((int)luaL_opt_number(L, n,d))
30#define luaL_opt_long(L, n,d) ((long)luaL_opt_number(L, n,d))
31
32#else
33
21#define luaL_arg_check(cond,numarg,extramsg) if (!(cond)) \ 34#define luaL_arg_check(cond,numarg,extramsg) if (!(cond)) \
22 luaL_argerror(numarg,extramsg) 35 luaL_argerror(numarg,extramsg)
23 36#define luaL_check_string(n) (luaL_check_lstr((n), NULL))
24void luaL_openlib (const struct luaL_reg *l, int n); 37#define luaL_opt_string(n, d) (luaL_opt_lstr((n), (d), NULL))
25void luaL_argerror (int numarg, const char *extramsg);
26#define luaL_check_string(n) (luaL_check_lstr((n), NULL))
27const char *luaL_check_lstr (int numArg, long *len);
28#define luaL_opt_string(n, d) (luaL_opt_lstr((n), (d), NULL))
29const char *luaL_opt_lstr (int numArg, const char *def, long *len);
30double luaL_check_number (int numArg);
31#define luaL_check_int(n) ((int)luaL_check_number(n)) 38#define luaL_check_int(n) ((int)luaL_check_number(n))
32#define luaL_check_long(n) ((long)luaL_check_number(n)) 39#define luaL_check_long(n) ((long)luaL_check_number(n))
33double luaL_opt_number (int numArg, double def);
34#define luaL_opt_int(n,d) ((int)luaL_opt_number(n,d)) 40#define luaL_opt_int(n,d) ((int)luaL_opt_number(n,d))
35#define luaL_opt_long(n,d) ((long)luaL_opt_number(n,d)) 41#define luaL_opt_long(n,d) ((long)luaL_opt_number(n,d))
36lua_Object luaL_functionarg (int arg); 42
37lua_Object luaL_tablearg (int arg); 43#endif
38lua_Object luaL_nonnullarg (int numArg); 44
39void luaL_verror (const char *fmt, ...); 45
40char *luaL_openspace (int size); 46void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
41void luaL_resetbuffer (void); 47void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
42void luaL_addchar (int c); 48const char *luaL_check_lstr (lua_State *L, int numArg, long *len);
43int luaL_getsize (void); 49const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def, long *len);
44void luaL_addsize (int n); 50double luaL_check_number (lua_State *L, int numArg);
45int luaL_newbuffer (int size); 51double luaL_opt_number (lua_State *L, int numArg, double def);
46void luaL_oldbuffer (int old); 52lua_Object luaL_functionarg (lua_State *L, int arg);
47char *luaL_buffer (void); 53lua_Object luaL_tablearg (lua_State *L, int arg);
54lua_Object luaL_nonnullarg (lua_State *L, int numArg);
55void luaL_verror (lua_State *L, const char *fmt, ...);
56char *luaL_openspace (lua_State *L, int size);
57void luaL_resetbuffer (lua_State *L);
58void luaL_addchar (lua_State *L, int c);
59int luaL_getsize (lua_State *L);
60void luaL_addsize (lua_State *L, int n);
61int luaL_newbuffer (lua_State *L, int size);
62void luaL_oldbuffer (lua_State *L, int old);
63char *luaL_buffer (lua_State *L);
48int luaL_findstring (const char *name, const char *const list[]); 64int luaL_findstring (const char *name, const char *const list[]);
49void luaL_chunkid (char *out, const char *source, int len); 65void luaL_chunkid (char *out, const char *source, int len);
50void luaL_filesource (char *out, const char *filename, int len); 66void luaL_filesource (char *out, const char *filename, int len);
51 67
52 68
69#ifndef LUA_REENTRANT
70
71#define luaL_openlib(l,n) (luaL_openlib)(lua_state,l,n)
72#define luaL_argerror(numarg,extramsg) \
73 (luaL_argerror)(lua_state,numarg,extramsg)
74#define luaL_check_lstr(numArg,len) (luaL_check_lstr)(lua_state,numArg,len)
75#define luaL_opt_lstr(numArg,def,len) \
76 (luaL_opt_lstr)(lua_state,numArg,def,len)
77#define luaL_check_number(numArg) (luaL_check_number)(lua_state,numArg)
78#define luaL_opt_number(numArg,def) (luaL_opt_number)(lua_state,numArg,def)
79#define luaL_functionarg(arg) (luaL_functionarg)(lua_state,arg)
80#define luaL_tablearg(arg) (luaL_tablearg)(lua_state,arg)
81#define luaL_nonnullarg(numArg) (luaL_nonnullarg)(lua_state,numArg)
82#define luaL_openspace(size) (luaL_openspace)(lua_state,size)
83#define luaL_resetbuffer() (luaL_resetbuffer)(lua_state)
84#define luaL_addchar(c) (luaL_addchar)(lua_state,c)
85#define luaL_getsize() (luaL_getsize)(lua_state)
86#define luaL_addsize(n) (luaL_addsize)(lua_state,n)
87#define luaL_newbuffer(size) (luaL_newbuffer)(lua_state,size)
88#define luaL_oldbuffer(old) (luaL_oldbuffer)(lua_state,old)
89#define luaL_buffer() (luaL_buffer)(lua_state)
90
53#endif 91#endif
92
93#endif
94
diff --git a/lbuffer.c b/lbuffer.c
index 181056f8..2525ea02 100644
--- a/lbuffer.c
+++ b/lbuffer.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuffer.c,v 1.9 1999/02/26 15:48:55 roberto Exp roberto $ 2** $Id: lbuffer.c,v 1.10 1999/11/10 15:40:46 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -7,6 +7,8 @@
7 7
8#include <stdio.h> 8#include <stdio.h>
9 9
10#define LUA_REENTRANT
11
10#include "lauxlib.h" 12#include "lauxlib.h"
11#include "lmem.h" 13#include "lmem.h"
12#include "lstate.h" 14#include "lstate.h"
@@ -20,55 +22,54 @@
20#define EXTRABUFF 32 22#define EXTRABUFF 32
21 23
22 24
23#define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size) 25#define openspace(L, size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(L, size)
24 26
25static void Openspace (int size) { 27static void Openspace (lua_State *L, int size) {
26 lua_State *l = L; /* to optimize */ 28 L->Mbuffsize = (L->Mbuffnext+size+EXTRABUFF)*2;
27 l->Mbuffsize = (l->Mbuffnext+size+EXTRABUFF)*2; 29 luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
28 luaM_reallocvector(l->Mbuffer, l->Mbuffsize, char);
29} 30}
30 31
31 32
32char *luaL_openspace (int size) { 33char *luaL_openspace (lua_State *L, int size) {
33 openspace(size); 34 openspace(L, size);
34 return L->Mbuffer+L->Mbuffnext; 35 return L->Mbuffer+L->Mbuffnext;
35} 36}
36 37
37 38
38void luaL_addchar (int c) { 39void luaL_addchar (lua_State *L, int c) {
39 openspace(1); 40 openspace(L, 1);
40 L->Mbuffer[L->Mbuffnext++] = (char)c; 41 L->Mbuffer[L->Mbuffnext++] = (char)c;
41} 42}
42 43
43 44
44void luaL_resetbuffer (void) { 45void luaL_resetbuffer (lua_State *L) {
45 L->Mbuffnext = L->Mbuffbase; 46 L->Mbuffnext = L->Mbuffbase;
46} 47}
47 48
48 49
49void luaL_addsize (int n) { 50void luaL_addsize (lua_State *L, int n) {
50 L->Mbuffnext += n; 51 L->Mbuffnext += n;
51} 52}
52 53
53int luaL_getsize (void) { 54int luaL_getsize (lua_State *L) {
54 return L->Mbuffnext-L->Mbuffbase; 55 return L->Mbuffnext-L->Mbuffbase;
55} 56}
56 57
57int luaL_newbuffer (int size) { 58int luaL_newbuffer (lua_State *L, int size) {
58 int old = L->Mbuffbase; 59 int old = L->Mbuffbase;
59 openspace(size); 60 openspace(L, size);
60 L->Mbuffbase = L->Mbuffnext; 61 L->Mbuffbase = L->Mbuffnext;
61 return old; 62 return old;
62} 63}
63 64
64 65
65void luaL_oldbuffer (int old) { 66void luaL_oldbuffer (lua_State *L, int old) {
66 L->Mbuffnext = L->Mbuffbase; 67 L->Mbuffnext = L->Mbuffbase;
67 L->Mbuffbase = old; 68 L->Mbuffbase = old;
68} 69}
69 70
70 71
71char *luaL_buffer (void) { 72char *luaL_buffer (lua_State *L) {
72 return L->Mbuffer+L->Mbuffbase; 73 return L->Mbuffer+L->Mbuffbase;
73} 74}
74 75
diff --git a/lbuiltin.c b/lbuiltin.c
index 2ebdd66f..da8a573c 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.72 1999/11/11 17:02:40 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.73 1999/11/16 12:50:48 roberto Exp roberto $
3** Built-in functions 3** Built-in functions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,6 +10,8 @@
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12 12
13#define LUA_REENTRANT
14
13#include "lapi.h" 15#include "lapi.h"
14#include "lauxlib.h" 16#include "lauxlib.h"
15#include "lbuiltin.h" 17#include "lbuiltin.h"
@@ -34,7 +36,7 @@
34*/ 36*/
35 37
36 38
37static void pushtagstring (TaggedString *s) { 39static void pushtagstring (lua_State *L, TaggedString *s) {
38 ttype(L->stack.top) = LUA_T_STRING; 40 ttype(L->stack.top) = LUA_T_STRING;
39 tsvalue(L->stack.top) = s; 41 tsvalue(L->stack.top) = s;
40 incr_top; 42 incr_top;
@@ -46,29 +48,29 @@ static real getsize (const Hash *h) {
46 int i = h->size; 48 int i = h->size;
47 Node *n = h->node; 49 Node *n = h->node;
48 while (i--) { 50 while (i--) {
49 if (ttype(key(n)) == LUA_T_NUMBER && 51 if (ttype(key(L, n)) == LUA_T_NUMBER &&
50 ttype(val(n)) != LUA_T_NIL && 52 ttype(val(L, n)) != LUA_T_NIL &&
51 nvalue(key(n)) > max) 53 nvalue(key(L, n)) > max)
52 max = nvalue(key(n)); 54 max = nvalue(key(L, n));
53 n++; 55 n++;
54 } 56 }
55 return max; 57 return max;
56} 58}
57 59
58 60
59static real getnarg (const Hash *a) { 61static real getnarg (lua_State *L, const Hash *a) {
60 TObject index; 62 TObject index;
61 const TObject *value; 63 const TObject *value;
62 /* value = table.n */ 64 /* value = table.n */
63 ttype(&index) = LUA_T_STRING; 65 ttype(&index) = LUA_T_STRING;
64 tsvalue(&index) = luaS_new("n"); 66 tsvalue(&index) = luaS_new(L, "n");
65 value = luaH_get(a, &index); 67 value = luaH_get(L, a, &index);
66 return (ttype(value) == LUA_T_NUMBER) ? nvalue(value) : getsize(a); 68 return (ttype(value) == LUA_T_NUMBER) ? nvalue(value) : getsize(a);
67} 69}
68 70
69 71
70static Hash *gettable (int arg) { 72static Hash *gettable (lua_State *L, int arg) {
71 return avalue(luaA_Address(luaL_tablearg(arg))); 73 return avalue(luaA_Address(L, luaL_tablearg(L, arg)));
72} 74}
73 75
74/* }====================================================== */ 76/* }====================================================== */
@@ -85,8 +87,8 @@ static Hash *gettable (int arg) {
85** If your system does not support "stderr", redefine this function, or 87** If your system does not support "stderr", redefine this function, or
86** redefine _ERRORMESSAGE so that it won't need _ALERT. 88** redefine _ERRORMESSAGE so that it won't need _ALERT.
87*/ 89*/
88static void luaB_alert (void) { 90static void luaB_alert (lua_State *L) {
89 fputs(luaL_check_string(1), stderr); 91 fputs(luaL_check_string(L, 1), stderr);
90} 92}
91 93
92 94
@@ -94,13 +96,13 @@ static void luaB_alert (void) {
94** Standard implementation of _ERRORMESSAGE. 96** Standard implementation of _ERRORMESSAGE.
95** The library "iolib" redefines _ERRORMESSAGE for better error information. 97** The library "iolib" redefines _ERRORMESSAGE for better error information.
96*/ 98*/
97static void error_message (void) { 99static void error_message (lua_State *L) {
98 lua_Object al = lua_rawgetglobal("_ALERT"); 100 lua_Object al = lua_rawgetglobal(L, "_ALERT");
99 if (lua_isfunction(al)) { /* avoid error loop if _ALERT is not defined */ 101 if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */
100 char buff[600]; 102 char buff[600];
101 sprintf(buff, "lua error: %.500s\n", luaL_check_string(1)); 103 sprintf(buff, "lua error: %.500s\n", luaL_check_string(L, 1));
102 lua_pushstring(buff); 104 lua_pushstring(L, buff);
103 lua_callfunction(al); 105 lua_callfunction(L, al);
104 } 106 }
105} 107}
106 108
@@ -115,142 +117,142 @@ static void error_message (void) {
115#define MAXPRINT 40 /* arbitrary limit */ 117#define MAXPRINT 40 /* arbitrary limit */
116#endif 118#endif
117 119
118static void luaB_print (void) { 120static void luaB_print (lua_State *L) {
119 lua_Object args[MAXPRINT]; 121 lua_Object args[MAXPRINT];
120 lua_Object obj; 122 lua_Object obj;
121 int n = 0; 123 int n = 0;
122 int i; 124 int i;
123 while ((obj = lua_getparam(n+1)) != LUA_NOOBJECT) { 125 while ((obj = lua_getparam(L, n+1)) != LUA_NOOBJECT) {
124 luaL_arg_check(n < MAXPRINT, n+1, "too many arguments"); 126 luaL_arg_check(L, n < MAXPRINT, n+1, "too many arguments");
125 args[n++] = obj; 127 args[n++] = obj;
126 } 128 }
127 for (i=0; i<n; i++) { 129 for (i=0; i<n; i++) {
128 lua_pushobject(args[i]); 130 lua_pushobject(L, args[i]);
129 if (lua_call("tostring")) 131 if (lua_call(L, "tostring"))
130 lua_error("error in `tostring' called by `print'"); 132 lua_error(L, "error in `tostring' called by `print'");
131 obj = lua_getresult(1); 133 obj = lua_getresult(L, 1);
132 if (!lua_isstring(obj)) 134 if (!lua_isstring(L, obj))
133 lua_error("`tostring' must return a string to `print'"); 135 lua_error(L, "`tostring' must return a string to `print'");
134 if (i>0) fputs("\t", stdout); 136 if (i>0) fputs("\t", stdout);
135 fputs(lua_getstring(obj), stdout); 137 fputs(lua_getstring(L, obj), stdout);
136 } 138 }
137 fputs("\n", stdout); 139 fputs("\n", stdout);
138} 140}
139 141
140 142
141static void luaB_tonumber (void) { 143static void luaB_tonumber (lua_State *L) {
142 int base = luaL_opt_int(2, 10); 144 int base = luaL_opt_int(L, 2, 10);
143 if (base == 10) { /* standard conversion */ 145 if (base == 10) { /* standard conversion */
144 lua_Object o = lua_getparam(1); 146 lua_Object o = lua_getparam(L, 1);
145 if (lua_isnumber(o)) lua_pushnumber(lua_getnumber(o)); 147 if (lua_isnumber(L, o)) lua_pushnumber(L, lua_getnumber(L, o));
146 else lua_pushnil(); /* not a number */ 148 else lua_pushnil(L); /* not a number */
147 } 149 }
148 else { 150 else {
149 const char *s1 = luaL_check_string(1); 151 const char *s1 = luaL_check_string(L, 1);
150 char *s2; 152 char *s2;
151 real n; 153 real n;
152 luaL_arg_check(0 <= base && base <= 36, 2, "base out of range"); 154 luaL_arg_check(L, 0 <= base && base <= 36, 2, "base out of range");
153 n = strtoul(s1, &s2, base); 155 n = strtoul(s1, &s2, base);
154 if (s1 == s2) return; /* no valid digits: return nil */ 156 if (s1 == s2) return; /* no valid digits: return nil */
155 while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */ 157 while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
156 if (*s2) return; /* invalid trailing character: return nil */ 158 if (*s2) return; /* invalid trailing character: return nil */
157 lua_pushnumber(n); 159 lua_pushnumber(L, n);
158 } 160 }
159} 161}
160 162
161 163
162static void luaB_error (void) { 164static void luaB_error (lua_State *L) {
163 lua_error(lua_getstring(lua_getparam(1))); 165 lua_error(L, lua_getstring(L, lua_getparam(L, 1)));
164} 166}
165 167
166static void luaB_setglobal (void) { 168static void luaB_setglobal (lua_State *L) {
167 const char *n = luaL_check_string(1); 169 const char *n = luaL_check_string(L, 1);
168 lua_Object value = luaL_nonnullarg(2); 170 lua_Object value = luaL_nonnullarg(L, 2);
169 lua_pushobject(value); 171 lua_pushobject(L, value);
170 lua_setglobal(n); 172 lua_setglobal(L, n);
171 lua_pushobject(value); /* return given value */ 173 lua_pushobject(L, value); /* return given value */
172} 174}
173 175
174static void luaB_rawsetglobal (void) { 176static void luaB_rawsetglobal (lua_State *L) {
175 const char *n = luaL_check_string(1); 177 const char *n = luaL_check_string(L, 1);
176 lua_Object value = luaL_nonnullarg(2); 178 lua_Object value = luaL_nonnullarg(L, 2);
177 lua_pushobject(value); 179 lua_pushobject(L, value);
178 lua_rawsetglobal(n); 180 lua_rawsetglobal(L, n);
179 lua_pushobject(value); /* return given value */ 181 lua_pushobject(L, value); /* return given value */
180} 182}
181 183
182static void luaB_getglobal (void) { 184static void luaB_getglobal (lua_State *L) {
183 lua_pushobject(lua_getglobal(luaL_check_string(1))); 185 lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1)));
184} 186}
185 187
186static void luaB_rawgetglobal (void) { 188static void luaB_rawgetglobal (lua_State *L) {
187 lua_pushobject(lua_rawgetglobal(luaL_check_string(1))); 189 lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1)));
188} 190}
189 191
190static void luaB_luatag (void) { 192static void luaB_luatag (lua_State *L) {
191 lua_pushnumber(lua_tag(lua_getparam(1))); 193 lua_pushnumber(L, lua_tag(L, lua_getparam(L, 1)));
192} 194}
193 195
194static void luaB_settag (void) { 196static void luaB_settag (lua_State *L) {
195 lua_Object o = luaL_tablearg(1); 197 lua_Object o = luaL_tablearg(L, 1);
196 lua_pushobject(o); 198 lua_pushobject(L, o);
197 lua_settag(luaL_check_int(2)); 199 lua_settag(L, luaL_check_int(L, 2));
198 lua_pushobject(o); /* return first argument */ 200 lua_pushobject(L, o); /* return first argument */
199} 201}
200 202
201static void luaB_newtag (void) { 203static void luaB_newtag (lua_State *L) {
202 lua_pushnumber(lua_newtag()); 204 lua_pushnumber(L, lua_newtag(L));
203} 205}
204 206
205static void luaB_copytagmethods (void) { 207static void luaB_copytagmethods (lua_State *L) {
206 lua_pushnumber(lua_copytagmethods(luaL_check_int(1), 208 lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
207 luaL_check_int(2))); 209 luaL_check_int(L, 2)));
208} 210}
209 211
210static void luaB_rawgettable (void) { 212static void luaB_rawgettable (lua_State *L) {
211 lua_pushobject(luaL_nonnullarg(1)); 213 lua_pushobject(L, luaL_nonnullarg(L, 1));
212 lua_pushobject(luaL_nonnullarg(2)); 214 lua_pushobject(L, luaL_nonnullarg(L, 2));
213 lua_pushobject(lua_rawgettable()); 215 lua_pushobject(L, lua_rawgettable(L));
214} 216}
215 217
216static void luaB_rawsettable (void) { 218static void luaB_rawsettable (lua_State *L) {
217 lua_pushobject(luaL_nonnullarg(1)); 219 lua_pushobject(L, luaL_nonnullarg(L, 1));
218 lua_pushobject(luaL_nonnullarg(2)); 220 lua_pushobject(L, luaL_nonnullarg(L, 2));
219 lua_pushobject(luaL_nonnullarg(3)); 221 lua_pushobject(L, luaL_nonnullarg(L, 3));
220 lua_rawsettable(); 222 lua_rawsettable(L);
221} 223}
222 224
223static void luaB_settagmethod (void) { 225static void luaB_settagmethod (lua_State *L) {
224 int tag = luaL_check_int(1); 226 int tag = luaL_check_int(L, 1);
225 const char *event = luaL_check_string(2); 227 const char *event = luaL_check_string(L, 2);
226 lua_Object nf = luaL_nonnullarg(3); 228 lua_Object nf = luaL_nonnullarg(L, 3);
227#ifndef LUA_COMPAT_GC 229#ifndef LUA_COMPAT_GC
228 if (strcmp(event, "gc") == 0 && tag != LUA_T_NIL) 230 if (strcmp(event, "gc") == 0 && tag != LUA_T_NIL)
229 lua_error("cannot set this tag method from Lua"); 231 lua_error(L, "cannot set this tag method from Lua");
230#endif 232#endif
231 lua_pushobject(nf); 233 lua_pushobject(L, nf);
232 lua_pushobject(lua_settagmethod(tag, event)); 234 lua_pushobject(L, lua_settagmethod(L, tag, event));
233} 235}
234 236
235static void luaB_gettagmethod (void) { 237static void luaB_gettagmethod (lua_State *L) {
236 lua_pushobject(lua_gettagmethod(luaL_check_int(1), luaL_check_string(2))); 238 lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2)));
237} 239}
238 240
239static void luaB_seterrormethod (void) { 241static void luaB_seterrormethod (lua_State *L) {
240 lua_Object nf = luaL_functionarg(1); 242 lua_Object nf = luaL_functionarg(L, 1);
241 lua_pushobject(nf); 243 lua_pushobject(L, nf);
242 lua_pushobject(lua_seterrormethod()); 244 lua_pushobject(L, lua_seterrormethod(L));
243} 245}
244 246
245static void luaB_collectgarbage (void) { 247static void luaB_collectgarbage (lua_State *L) {
246 lua_pushnumber(lua_collectgarbage(luaL_opt_int(1, 0))); 248 lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
247} 249}
248 250
249 251
250static void luaB_type (void) { 252static void luaB_type (lua_State *L) {
251 lua_Object o = luaL_nonnullarg(1); 253 lua_Object o = luaL_nonnullarg(L, 1);
252 lua_pushstring(lua_type(o)); 254 lua_pushstring(L, lua_type(L, o));
253 lua_pushnumber(lua_tag(o)); 255 lua_pushnumber(L, lua_tag(L, o));
254} 256}
255 257
256/* }====================================================== */ 258/* }====================================================== */
@@ -264,62 +266,62 @@ static void luaB_type (void) {
264*/ 266*/
265 267
266 268
267static void passresults (void) { 269static void passresults (lua_State *L) {
268 L->Cstack.base = L->Cstack.lua2C; /* position of first result */ 270 L->Cstack.base = L->Cstack.lua2C; /* position of first result */
269 if (L->Cstack.num == 0) 271 if (L->Cstack.num == 0)
270 lua_pushuserdata(NULL); /* at least one result to signal no errors */ 272 lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
271} 273}
272 274
273static void luaB_dostring (void) { 275static void luaB_dostring (lua_State *L) {
274 long l; 276 long l;
275 const char *s = luaL_check_lstr(1, &l); 277 const char *s = luaL_check_lstr(L, 1, &l);
276 if (*s == ID_CHUNK) 278 if (*s == ID_CHUNK)
277 lua_error("`dostring' cannot run pre-compiled code"); 279 lua_error(L, "`dostring' cannot run pre-compiled code");
278 if (lua_dobuffer(s, l, luaL_opt_string(2, s)) == 0) 280 if (lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)) == 0)
279 passresults(); 281 passresults(L);
280 /* else return no value */ 282 /* else return no value */
281} 283}
282 284
283 285
284static void luaB_dofile (void) { 286static void luaB_dofile (lua_State *L) {
285 const char *fname = luaL_opt_string(1, NULL); 287 const char *fname = luaL_opt_string(L, 1, NULL);
286 if (lua_dofile(fname) == 0) 288 if (lua_dofile(L, fname) == 0)
287 passresults(); 289 passresults(L);
288 /* else return no value */ 290 /* else return no value */
289} 291}
290 292
291 293
292static void luaB_call (void) { 294static void luaB_call (lua_State *L) {
293 lua_Object f = luaL_nonnullarg(1); 295 lua_Object f = luaL_nonnullarg(L, 1);
294 const Hash *arg = gettable(2); 296 const Hash *arg = gettable(L, 2);
295 const char *options = luaL_opt_string(3, ""); 297 const char *options = luaL_opt_string(L, 3, "");
296 lua_Object err = lua_getparam(4); 298 lua_Object err = lua_getparam(L, 4);
297 int narg = (int)getnarg(arg); 299 int narg = (int)getnarg(L, arg);
298 int i, status; 300 int i, status;
299 if (err != LUA_NOOBJECT) { /* set new error method */ 301 if (err != LUA_NOOBJECT) { /* set new error method */
300 lua_pushobject(err); 302 lua_pushobject(L, err);
301 err = lua_seterrormethod(); 303 err = lua_seterrormethod(L);
302 } 304 }
303 /* push arg[1...n] */ 305 /* push arg[1...n] */
304 luaD_checkstack(narg); 306 luaD_checkstack(L, narg);
305 for (i=0; i<narg; i++) 307 for (i=0; i<narg; i++)
306 *(L->stack.top++) = *luaH_getint(arg, i+1); 308 *(L->stack.top++) = *luaH_getint(L, arg, i+1);
307 status = lua_callfunction(f); 309 status = lua_callfunction(L, f);
308 if (err != LUA_NOOBJECT) { /* restore old error method */ 310 if (err != LUA_NOOBJECT) { /* restore old error method */
309 lua_pushobject(err); 311 lua_pushobject(L, err);
310 lua_seterrormethod(); 312 lua_seterrormethod(L);
311 } 313 }
312 if (status != 0) { /* error in call? */ 314 if (status != 0) { /* error in call? */
313 if (strchr(options, 'x')) { 315 if (strchr(options, 'x')) {
314 lua_pushnil(); 316 lua_pushnil(L);
315 return; /* return nil to signal the error */ 317 return; /* return nil to signal the error */
316 } 318 }
317 else 319 else
318 lua_error(NULL); 320 lua_error(L, NULL);
319 } 321 }
320 else { /* no errors */ 322 else { /* no errors */
321 if (strchr(options, 'p')) { /* pack results? */ 323 if (strchr(options, 'p')) { /* pack results? */
322 luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top); 324 luaV_pack(L, L->Cstack.lua2C, L->Cstack.num, L->stack.top);
323 incr_top; 325 incr_top;
324 } 326 }
325 else 327 else
@@ -328,45 +330,45 @@ static void luaB_call (void) {
328} 330}
329 331
330 332
331static void luaB_nextvar (void) { 333static void luaB_nextvar (lua_State *L) {
332 const TObject *o = luaA_Address(luaL_nonnullarg(1)); 334 const TObject *o = luaA_Address(L, luaL_nonnullarg(L, 1));
333 TaggedString *g; 335 TaggedString *g;
334 if (ttype(o) == LUA_T_NIL) 336 if (ttype(o) == LUA_T_NIL)
335 g = NULL; 337 g = NULL;
336 else { 338 else {
337 luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); 339 luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "variable name expected");
338 g = tsvalue(o); 340 g = tsvalue(o);
339 } 341 }
340 if (!luaA_nextvar(g)) 342 if (!luaA_nextvar(L, g))
341 lua_pushnil(); 343 lua_pushnil(L);
342} 344}
343 345
344 346
345static void luaB_next (void) { 347static void luaB_next (lua_State *L) {
346 const Hash *a = gettable(1); 348 const Hash *a = gettable(L, 1);
347 const TObject *k = luaA_Address(luaL_nonnullarg(2)); 349 const TObject *k = luaA_Address(L, luaL_nonnullarg(L, 2));
348 int i; /* will get first element after `i' */ 350 int i; /* will get first element after `i' */
349 if (ttype(k) == LUA_T_NIL) 351 if (ttype(k) == LUA_T_NIL)
350 i = 0; /* get first */ 352 i = 0; /* get first */
351 else { 353 else {
352 i = luaH_pos(a, k)+1; 354 i = luaH_pos(L, a, k)+1;
353 luaL_arg_check(i != 0, 2, "key not found"); 355 luaL_arg_check(L, i != 0, 2, "key not found");
354 } 356 }
355 if (luaA_next(a, i) == 0) 357 if (luaA_next(L, a, i) == 0)
356 lua_pushnil(); 358 lua_pushnil(L);
357} 359}
358 360
359 361
360static void luaB_tostring (void) { 362static void luaB_tostring (lua_State *L) {
361 lua_Object obj = lua_getparam(1); 363 lua_Object obj = lua_getparam(L, 1);
362 const TObject *o = luaA_Address(obj); 364 const TObject *o = luaA_Address(L, obj);
363 char buff[64]; 365 char buff[64];
364 switch (ttype(o)) { 366 switch (ttype(o)) {
365 case LUA_T_NUMBER: 367 case LUA_T_NUMBER:
366 lua_pushstring(lua_getstring(obj)); 368 lua_pushstring(L, lua_getstring(L, obj));
367 return; 369 return;
368 case LUA_T_STRING: 370 case LUA_T_STRING:
369 lua_pushobject(obj); 371 lua_pushobject(L, obj);
370 return; 372 return;
371 case LUA_T_ARRAY: 373 case LUA_T_ARRAY:
372 sprintf(buff, "table: %p", o->value.a); 374 sprintf(buff, "table: %p", o->value.a);
@@ -384,12 +386,12 @@ static void luaB_tostring (void) {
384 sprintf(buff, "userdata: %p", o->value.ts->u.d.value); 386 sprintf(buff, "userdata: %p", o->value.ts->u.d.value);
385 break; 387 break;
386 case LUA_T_NIL: 388 case LUA_T_NIL:
387 lua_pushstring("nil"); 389 lua_pushstring(L, "nil");
388 return; 390 return;
389 default: 391 default:
390 LUA_INTERNALERROR("invalid type"); 392 LUA_INTERNALERROR(L, "invalid type");
391 } 393 }
392 lua_pushstring(buff); 394 lua_pushstring(L, buff);
393} 395}
394 396
395/* }====================================================== */ 397/* }====================================================== */
@@ -406,28 +408,28 @@ static void luaB_tostring (void) {
406** ======================================================= 408** =======================================================
407*/ 409*/
408 410
409static void luaB_assert (void) { 411static void luaB_assert (lua_State *L) {
410 lua_Object p = lua_getparam(1); 412 lua_Object p = lua_getparam(L, 1);
411 if (p == LUA_NOOBJECT || lua_isnil(p)) 413 if (p == LUA_NOOBJECT || lua_isnil(L, p))
412 luaL_verror("assertion failed! %.90s", luaL_opt_string(2, "")); 414 luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
413} 415}
414 416
415 417
416static void luaB_foreachi (void) { 418static void luaB_foreachi (lua_State *L) {
417 const Hash *t = gettable(1); 419 const Hash *t = gettable(L, 1);
418 int i; 420 int i;
419 int n = (int)getnarg(t); 421 int n = (int)getnarg(L, t);
420 TObject f; 422 TObject f;
421 /* 'f' cannot be a pointer to TObject, because it is on the stack, and the 423 /* 'f' cannot be a pointer to TObject, because it is on the stack, and the
422 stack may be reallocated by the call. Moreover, some C compilers do not 424 stack may be reallocated by the call. Moreover, some C compilers do not
423 initialize structs, so we must do the assignment after the declaration */ 425 initialize structs, so we must do the assignment after the declaration */
424 f = *luaA_Address(luaL_functionarg(2)); 426 f = *luaA_Address(L, luaL_functionarg(L, 2));
425 luaD_checkstack(3); /* for f, key, and val */ 427 luaD_checkstack(L, 3); /* for f, key, and val */
426 for (i=1; i<=n; i++) { 428 for (i=1; i<=n; i++) {
427 *(L->stack.top++) = f; 429 *(L->stack.top++) = f;
428 ttype(L->stack.top) = LUA_T_NUMBER; nvalue(L->stack.top++) = i; 430 ttype(L->stack.top) = LUA_T_NUMBER; nvalue(L->stack.top++) = i;
429 *(L->stack.top++) = *luaH_getint(t, i); 431 *(L->stack.top++) = *luaH_getint(L, t, i);
430 luaD_calln(2, 1); 432 luaD_calln(L, 2, 1);
431 if (ttype(L->stack.top-1) != LUA_T_NIL) 433 if (ttype(L->stack.top-1) != LUA_T_NIL)
432 return; 434 return;
433 L->stack.top--; 435 L->stack.top--;
@@ -435,19 +437,19 @@ static void luaB_foreachi (void) {
435} 437}
436 438
437 439
438static void luaB_foreach (void) { 440static void luaB_foreach (lua_State *L) {
439 const Hash *a = gettable(1); 441 const Hash *a = gettable(L, 1);
440 int i; 442 int i;
441 TObject f; /* see comment in 'foreachi' */ 443 TObject f; /* see comment in 'foreachi' */
442 f = *luaA_Address(luaL_functionarg(2)); 444 f = *luaA_Address(L, luaL_functionarg(L, 2));
443 luaD_checkstack(3); /* for f, key, and val */ 445 luaD_checkstack(L, 3); /* for f, key, and val */
444 for (i=0; i<a->size; i++) { 446 for (i=0; i<a->size; i++) {
445 const Node *nd = &(a->node[i]); 447 const Node *nd = &(a->node[i]);
446 if (ttype(val(nd)) != LUA_T_NIL) { 448 if (ttype(val(L, nd)) != LUA_T_NIL) {
447 *(L->stack.top++) = f; 449 *(L->stack.top++) = f;
448 *(L->stack.top++) = *key(nd); 450 *(L->stack.top++) = *key(L, nd);
449 *(L->stack.top++) = *val(nd); 451 *(L->stack.top++) = *val(L, nd);
450 luaD_calln(2, 1); 452 luaD_calln(L, 2, 1);
451 if (ttype(L->stack.top-1) != LUA_T_NIL) 453 if (ttype(L->stack.top-1) != LUA_T_NIL)
452 return; 454 return;
453 L->stack.top--; /* remove result */ 455 L->stack.top--; /* remove result */
@@ -456,18 +458,18 @@ static void luaB_foreach (void) {
456} 458}
457 459
458 460
459static void luaB_foreachvar (void) { 461static void luaB_foreachvar (lua_State *L) {
460 GlobalVar *gv; 462 GlobalVar *gv;
461 TObject f; /* see comment in 'foreachi' */ 463 TObject f; /* see comment in 'foreachi' */
462 f = *luaA_Address(luaL_functionarg(1)); 464 f = *luaA_Address(L, luaL_functionarg(L, 1));
463 luaD_checkstack(4); /* for extra var name, f, var name, and globalval */ 465 luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */
464 for (gv = L->rootglobal; gv; gv = gv->next) { 466 for (gv = L->rootglobal; gv; gv = gv->next) {
465 if (gv->value.ttype != LUA_T_NIL) { 467 if (gv->value.ttype != LUA_T_NIL) {
466 pushtagstring(gv->name); /* keep (extra) name on stack to avoid GC */ 468 pushtagstring(L, gv->name); /* keep (extra) name on stack to avoid GC */
467 *(L->stack.top++) = f; 469 *(L->stack.top++) = f;
468 pushtagstring(gv->name); 470 pushtagstring(L, gv->name);
469 *(L->stack.top++) = gv->value; 471 *(L->stack.top++) = gv->value;
470 luaD_calln(2, 1); 472 luaD_calln(L, 2, 1);
471 if (ttype(L->stack.top-1) != LUA_T_NIL) { 473 if (ttype(L->stack.top-1) != LUA_T_NIL) {
472 L->stack.top--; 474 L->stack.top--;
473 *(L->stack.top-1) = *L->stack.top; /* remove extra name */ 475 *(L->stack.top-1) = *L->stack.top; /* remove extra name */
@@ -479,39 +481,39 @@ static void luaB_foreachvar (void) {
479} 481}
480 482
481 483
482static void luaB_getn (void) { 484static void luaB_getn (lua_State *L) {
483 lua_pushnumber(getnarg(gettable(1))); 485 lua_pushnumber(L, getnarg(L, gettable(L, 1)));
484} 486}
485 487
486 488
487static void luaB_tinsert (void) { 489static void luaB_tinsert (lua_State *L) {
488 Hash *a = gettable(1); 490 Hash *a = gettable(L, 1);
489 lua_Object v = lua_getparam(3); 491 lua_Object v = lua_getparam(L, 3);
490 int n = (int)getnarg(a); 492 int n = (int)getnarg(L, a);
491 int pos; 493 int pos;
492 if (v != LUA_NOOBJECT) 494 if (v != LUA_NOOBJECT)
493 pos = luaL_check_int(2); 495 pos = luaL_check_int(L, 2);
494 else { /* called with only 2 arguments */ 496 else { /* called with only 2 arguments */
495 v = luaL_nonnullarg(2); 497 v = luaL_nonnullarg(L, 2);
496 pos = n+1; 498 pos = n+1;
497 } 499 }
498 luaV_setn(a, n+1); /* a.n = n+1 */ 500 luaV_setn(L, a, n+1); /* a.n = n+1 */
499 for ( ;n>=pos; n--) 501 for ( ;n>=pos; n--)
500 luaH_move(a, n, n+1); /* a[n+1] = a[n] */ 502 luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */
501 luaH_setint(a, pos, luaA_Address(v)); /* a[pos] = v */ 503 luaH_setint(L, a, pos, luaA_Address(L, v)); /* a[pos] = v */
502} 504}
503 505
504 506
505static void luaB_tremove (void) { 507static void luaB_tremove (lua_State *L) {
506 Hash *a = gettable(1); 508 Hash *a = gettable(L, 1);
507 int n = (int)getnarg(a); 509 int n = (int)getnarg(L, a);
508 int pos = luaL_opt_int(2, n); 510 int pos = luaL_opt_int(L, 2, n);
509 if (n <= 0) return; /* table is "empty" */ 511 if (n <= 0) return; /* table is "empty" */
510 luaA_pushobject(luaH_getint(a, pos)); /* result = a[pos] */ 512 luaA_pushobject(L, luaH_getint(L, a, pos)); /* result = a[pos] */
511 for ( ;pos<n; pos++) 513 for ( ;pos<n; pos++)
512 luaH_move(a, pos+1, pos); /* a[pos] = a[pos+1] */ 514 luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */
513 luaV_setn(a, n-1); /* a.n = n-1 */ 515 luaV_setn(L, a, n-1); /* a.n = n-1 */
514 luaH_setint(a, n, &luaO_nilobject); /* a[n] = nil */ 516 luaH_setint(L, a, n, &luaO_nilobject); /* a[n] = nil */
515} 517}
516 518
517 519
@@ -520,63 +522,63 @@ static void luaB_tremove (void) {
520** Quicksort 522** Quicksort
521*/ 523*/
522 524
523static void swap (Hash *a, int i, int j) { 525static void swap (lua_State *L, Hash *a, int i, int j) {
524 TObject temp; 526 TObject temp;
525 temp = *luaH_getint(a, i); 527 temp = *luaH_getint(L, a, i);
526 luaH_move(a, j, i); 528 luaH_move(L, a, j, i);
527 luaH_setint(a, j, &temp); 529 luaH_setint(L, a, j, &temp);
528} 530}
529 531
530static int sort_comp (lua_Object f, const TObject *a, const TObject *b) { 532static int sort_comp (lua_State *L, lua_Object f, const TObject *a, const TObject *b) {
531 /* notice: the caller (auxsort) must check stack space */ 533 /* notice: the caller (auxsort) must check stack space */
532 if (f != LUA_NOOBJECT) { 534 if (f != LUA_NOOBJECT) {
533 *(L->stack.top) = *luaA_Address(f); 535 *(L->stack.top) = *luaA_Address(L, f);
534 *(L->stack.top+1) = *a; 536 *(L->stack.top+1) = *a;
535 *(L->stack.top+2) = *b; 537 *(L->stack.top+2) = *b;
536 L->stack.top += 3; 538 L->stack.top += 3;
537 luaD_calln(2, 1); 539 luaD_calln(L, 2, 1);
538 } 540 }
539 else { /* a < b? */ 541 else { /* a < b? */
540 *(L->stack.top) = *a; 542 *(L->stack.top) = *a;
541 *(L->stack.top+1) = *b; 543 *(L->stack.top+1) = *b;
542 L->stack.top += 2; 544 L->stack.top += 2;
543 luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT); 545 luaV_comparison(L, LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
544 } 546 }
545 return ttype(--(L->stack.top)) != LUA_T_NIL; 547 return ttype(--(L->stack.top)) != LUA_T_NIL;
546} 548}
547 549
548static void auxsort (Hash *a, int l, int u, lua_Object f) { 550static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) {
549 StkId P = L->stack.top - L->stack.stack; /* temporary place for pivot */ 551 StkId P = L->stack.top - L->stack.stack; /* temporary place for pivot */
550 L->stack.top++; 552 L->stack.top++;
551 ttype(L->stack.stack+P) = LUA_T_NIL; 553 ttype(L->stack.stack+P) = LUA_T_NIL;
552 while (l < u) { /* for tail recursion */ 554 while (l < u) { /* for tail recursion */
553 int i, j; 555 int i, j;
554 /* sort elements a[l], a[(l+u)/2] and a[u] */ 556 /* sort elements a[l], a[(l+u)/2] and a[u] */
555 if (sort_comp(f, luaH_getint(a, u), luaH_getint(a, l))) /* a[u]<a[l]? */ 557 if (sort_comp(L, f, luaH_getint(L, a, u), luaH_getint(L, a, l))) /* a[u]<a[l]? */
556 swap(a, l, u); 558 swap(L, a, l, u);
557 if (u-l == 1) break; /* only 2 elements */ 559 if (u-l == 1) break; /* only 2 elements */
558 i = (l+u)/2; 560 i = (l+u)/2;
559 *(L->stack.stack+P) = *luaH_getint(a, i); /* P = a[i] */ 561 *(L->stack.stack+P) = *luaH_getint(L, a, i); /* P = a[i] */
560 if (sort_comp(f, L->stack.stack+P, luaH_getint(a, l))) /* a[i]<a[l]? */ 562 if (sort_comp(L, f, L->stack.stack+P, luaH_getint(L, a, l))) /* a[i]<a[l]? */
561 swap(a, l, i); 563 swap(L, a, l, i);
562 else if (sort_comp(f, luaH_getint(a, u), L->stack.stack+P)) /* a[u]<a[i]? */ 564 else if (sort_comp(L, f, luaH_getint(L, a, u), L->stack.stack+P)) /* a[u]<a[i]? */
563 swap(a, i, u); 565 swap(L, a, i, u);
564 if (u-l == 2) break; /* only 3 elements */ 566 if (u-l == 2) break; /* only 3 elements */
565 *(L->stack.stack+P) = *luaH_getint(a, i); /* save pivot on stack (for GC) */ 567 *(L->stack.stack+P) = *luaH_getint(L, a, i); /* save pivot on stack (GC) */
566 swap(a, i, u-1); /* put median element as pivot (a[u-1]) */ 568 swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */
567 /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */ 569 /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
568 i = l; j = u-1; 570 i = l; j = u-1;
569 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ 571 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
570 /* repeat i++ until a[i] >= P */ 572 /* repeat i++ until a[i] >= P */
571 while (sort_comp(f, luaH_getint(a, ++i), L->stack.stack+P)) 573 while (sort_comp(L, f, luaH_getint(L, a, ++i), L->stack.stack+P))
572 if (i>u) lua_error("invalid order function for sorting"); 574 if (i>u) lua_error(L, "invalid order function for sorting");
573 /* repeat j-- until a[j] <= P */ 575 /* repeat j-- until a[j] <= P */
574 while (sort_comp(f, (L->stack.stack+P), luaH_getint(a, --j))) 576 while (sort_comp(L, f, (L->stack.stack+P), luaH_getint(L, a, --j)))
575 if (j<l) lua_error("invalid order function for sorting"); 577 if (j<l) lua_error(L, "invalid order function for sorting");
576 if (j<i) break; 578 if (j<i) break;
577 swap(a, i, j); 579 swap(L, a, i, j);
578 } 580 }
579 swap(a, u-1, i); /* swap pivot (a[u-1]) with a[i] */ 581 swap(L, a, u-1, i); /* swap pivot (a[u-1]) with a[i] */
580 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ 582 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
581 /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */ 583 /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
582 if (i-l < u-i) { 584 if (i-l < u-i) {
@@ -585,21 +587,21 @@ static void auxsort (Hash *a, int l, int u, lua_Object f) {
585 else { 587 else {
586 j=i+1; i=u; u=j-2; 588 j=i+1; i=u; u=j-2;
587 } 589 }
588 auxsort(a, j, i, f); /* call recursively the smaller one */ 590 auxsort(L, a, j, i, f); /* call recursively the smaller one */
589 } /* repeat the routine for the larger one */ 591 } /* repeat the routine for the larger one */
590 L->stack.top--; /* remove pivot from stack */ 592 L->stack.top--; /* remove pivot from stack */
591} 593}
592 594
593static void luaB_sort (void) { 595static void luaB_sort (lua_State *L) {
594 lua_Object t = lua_getparam(1); 596 lua_Object t = lua_getparam(L, 1);
595 Hash *a = gettable(1); 597 Hash *a = gettable(L, 1);
596 int n = (int)getnarg(a); 598 int n = (int)getnarg(L, a);
597 lua_Object func = lua_getparam(2); 599 lua_Object func = lua_getparam(L, 2);
598 luaL_arg_check(func == LUA_NOOBJECT || lua_isfunction(func), 2, 600 luaL_arg_check(L, func == LUA_NOOBJECT || lua_isfunction(L, func), 2,
599 "function expected"); 601 "function expected");
600 luaD_checkstack(4); /* for Pivot, f, a, b (sort_comp) */ 602 luaD_checkstack(L, 4); /* for Pivot, f, a, b (sort_comp) */
601 auxsort(a, 1, n, func); 603 auxsort(L, a, 1, n, func);
602 lua_pushobject(t); 604 lua_pushobject(L, t);
603} 605}
604 606
605/* }====================================================== */ 607/* }====================================================== */
@@ -617,138 +619,138 @@ static void luaB_sort (void) {
617** ======================================================= 619** =======================================================
618*/ 620*/
619 621
620static void mem_query (void) { 622static void mem_query (lua_State *L) {
621 lua_pushnumber(totalmem); 623 lua_pushnumber(L, totalmem);
622 lua_pushnumber(numblocks); 624 lua_pushnumber(L, numblocks);
623} 625}
624 626
625 627
626static void hash_query (void) { 628static void hash_query (lua_State *L) {
627 const TObject *o = luaA_Address(luaL_nonnullarg(1)); 629 const TObject *o = luaA_Address(L, luaL_nonnullarg(L, 1));
628 if (lua_getparam(2) == LUA_NOOBJECT) { 630 if (lua_getparam(L, 2) == LUA_NOOBJECT) {
629 luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "string expected"); 631 luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "string expected");
630 lua_pushnumber(tsvalue(o)->hash); 632 lua_pushnumber(L, tsvalue(o)->hash);
631 } 633 }
632 else { 634 else {
633 const Hash *t = avalue(luaA_Address(luaL_tablearg(2))); 635 const Hash *t = avalue(luaA_Address(L, luaL_tablearg(L, 2)));
634 lua_pushnumber(luaH_mainposition(t, o) - t->node); 636 lua_pushnumber(L, luaH_mainposition(L, t, o) - t->node);
635 } 637 }
636} 638}
637 639
638 640
639static void table_query (void) { 641static void table_query (lua_State *L) {
640 const Hash *t = avalue(luaA_Address(luaL_tablearg(1))); 642 const Hash *t = avalue(luaA_Address(L, luaL_tablearg(L, 1)));
641 int i = luaL_opt_int(2, -1); 643 int i = luaL_opt_int(L, 2, -1);
642 if (i == -1) { 644 if (i == -1) {
643 lua_pushnumber(t->size); 645 lua_pushnumber(L, t->size);
644 lua_pushnumber(t->firstfree - t->node); 646 lua_pushnumber(L, t->firstfree - t->node);
645 } 647 }
646 else if (i < t->size) { 648 else if (i < t->size) {
647 luaA_pushobject(&t->node[i].key); 649 luaA_pushobject(L, &t->node[i].key);
648 luaA_pushobject(&t->node[i].val); 650 luaA_pushobject(L, &t->node[i].val);
649 if (t->node[i].next) 651 if (t->node[i].next)
650 lua_pushnumber(t->node[i].next - t->node); 652 lua_pushnumber(L, t->node[i].next - t->node);
651 } 653 }
652} 654}
653 655
654 656
655static void query_strings (void) { 657static void query_strings (lua_State *L) {
656 int h = luaL_check_int(1) - 1; 658 int h = luaL_check_int(L, 1) - 1;
657 int s = luaL_opt_int(2, 0) - 1; 659 int s = luaL_opt_int(L, 2, 0) - 1;
658 if (s==-1) { 660 if (s==-1) {
659 if (h < NUM_HASHS) { 661 if (h < NUM_HASHS) {
660 lua_pushnumber(L->string_root[h].nuse); 662 lua_pushnumber(L, L->string_root[h].nuse);
661 lua_pushnumber(L->string_root[h].size); 663 lua_pushnumber(L, L->string_root[h].size);
662 } 664 }
663 } 665 }
664 else { 666 else {
665 TaggedString *ts = L->string_root[h].hash[s]; 667 TaggedString *ts = L->string_root[h].hash[s];
666 for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) { 668 for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) {
667 if (ts->constindex == -1) lua_pushstring("<USERDATA>"); 669 if (ts->constindex == -1) lua_pushstring(L, "<USERDATA>");
668 else lua_pushstring(ts->str); 670 else lua_pushstring(L, ts->str);
669 } 671 }
670 } 672 }
671} 673}
672 674
673 675
674static void extra_services (void) { 676static void extra_services (lua_State *L) {
675 const char *service = luaL_check_string(1); 677 const char *service = luaL_check_string(L, 1);
676 switch (*service) { 678 switch (*service) {
677 case 'U': /* create a userdata with a given value/tag */ 679 case 'U': /* create a userdata with a given value/tag */
678 lua_pushusertag((void *)luaL_check_int(2), luaL_check_int(3)); 680 lua_pushusertag(L, (void *)luaL_check_int(L, 2), luaL_check_int(L, 3));
679 break; 681 break;
680 682
681 case 'u': /* return the value of a userdata */ 683 case 'u': /* return the value of a userdata */
682 lua_pushnumber((int)lua_getuserdata(lua_getparam(2))); 684 lua_pushnumber(L, (int)lua_getuserdata(L, lua_getparam(L, 2)));
683 break; 685 break;
684 686
685 case 't': /* set `gc' tag method */ 687 case 't': /* set `gc' tag method */
686 lua_pushobject(lua_getparam(3)); 688 lua_pushobject(L, lua_getparam(L, 3));
687 lua_settagmethod(luaL_check_int(2), "gc"); 689 lua_settagmethod(L, luaL_check_int(L, 2), "gc");
688 break; 690 break;
689 691
690 default: luaL_argerror(1, "invalid service"); 692 default: luaL_argerror(L, 1, "invalid service");
691 } 693 }
692} 694}
693 695
694 696
695static void testC (void) { 697static void testC (lua_State *L) {
696#define getnum(s) ((*s++) - '0') 698#define getnum(L, s) ((*s++) - '0')
697#define getname(s) (nome[0] = *s++, nome) 699#define getname(L, s) (nome[0] = *s++, nome)
698 700
699 lua_Object reg[10]; 701 lua_Object reg[10];
700 char nome[2]; 702 char nome[2];
701 const char *s = luaL_check_string(1); 703 const char *s = luaL_check_string(L, 1);
702 nome[1] = 0; 704 nome[1] = 0;
703 for (;;) { 705 for (;;) {
704 switch (*s++) { 706 switch (*s++) {
705 case '0': case '1': case '2': case '3': case '4': 707 case '0': case '1': case '2': case '3': case '4':
706 case '5': case '6': case '7': case '8': case '9': 708 case '5': case '6': case '7': case '8': case '9':
707 lua_pushnumber(*(s-1) - '0'); 709 lua_pushnumber(L, *(s-1) - '0');
708 break; 710 break;
709 711
710 case 'c': reg[getnum(s)] = lua_createtable(); break; 712 case 'c': reg[getnum(L, s)] = lua_createtable(L); break;
711 case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s))); 713 case 'C': { lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(L, s)));
712 lua_pushcclosure(f, getnum(s)); 714 lua_pushcclosure(L, f, getnum(L, s));
713 break; 715 break;
714 } 716 }
715 case 'P': reg[getnum(s)] = lua_pop(); break; 717 case 'P': reg[getnum(L, s)] = lua_pop(L); break;
716 case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; } 718 case 'g': { int n=getnum(L, s); reg[n]=lua_getglobal(L, getname(L, s)); break; }
717 case 'G': { int n = getnum(s); 719 case 'G': { int n = getnum(L, s);
718 reg[n] = lua_rawgetglobal(getname(s)); 720 reg[n] = lua_rawgetglobal(L, getname(L, s));
719 break; 721 break;
720 } 722 }
721 case 'l': lua_pushnumber(lua_ref(1)); reg[getnum(s)] = lua_pop(); break; 723 case 'l': lua_pushnumber(L, lua_ref(L, 1)); reg[getnum(L, s)] = lua_pop(L); break;
722 case 'L': lua_pushnumber(lua_ref(0)); reg[getnum(s)] = lua_pop(); break; 724 case 'L': lua_pushnumber(L, lua_ref(L, 0)); reg[getnum(L, s)] = lua_pop(L); break;
723 case 'r': { int n=getnum(s); 725 case 'r': { int n=getnum(L, s);
724 reg[n]=lua_getref((int)lua_getnumber(reg[getnum(s)])); 726 reg[n]=lua_getref(L, (int)lua_getnumber(L, reg[getnum(L, s)]));
725 break; 727 break;
726 } 728 }
727 case 'u': lua_unref((int)lua_getnumber(reg[getnum(s)])); 729 case 'u': lua_unref(L, (int)lua_getnumber(L, reg[getnum(L, s)]));
728 break; 730 break;
729 case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; } 731 case 'p': { int n = getnum(L, s); reg[n] = lua_getparam(L, getnum(L, s)); break; }
730 case '=': lua_setglobal(getname(s)); break; 732 case '=': lua_setglobal(L, getname(L, s)); break;
731 case 's': lua_pushstring(getname(s)); break; 733 case 's': lua_pushstring(L, getname(L, s)); break;
732 case 'o': lua_pushobject(reg[getnum(s)]); break; 734 case 'o': lua_pushobject(L, reg[getnum(L, s)]); break;
733 case 'f': lua_call(getname(s)); break; 735 case 'f': lua_call(L, getname(L, s)); break;
734 case 'i': reg[getnum(s)] = lua_gettable(); break; 736 case 'i': reg[getnum(L, s)] = lua_gettable(L); break;
735 case 'I': reg[getnum(s)] = lua_rawgettable(); break; 737 case 'I': reg[getnum(L, s)] = lua_rawgettable(L); break;
736 case 't': lua_settable(); break; 738 case 't': lua_settable(L); break;
737 case 'T': lua_rawsettable(); break; 739 case 'T': lua_rawsettable(L); break;
738 case 'N' : lua_pushstring(lua_nextvar(lua_getstring(reg[getnum(s)]))); 740 case 'N' : lua_pushstring(L, lua_nextvar(L, lua_getstring(L, reg[getnum(L, s)])));
739 break; 741 break;
740 case 'n' : { int n=getnum(s); 742 case 'n' : { int n=getnum(L, s);
741 n=lua_next(reg[n], (int)lua_getnumber(reg[getnum(s)])); 743 n=lua_next(L, reg[n], (int)lua_getnumber(L, reg[getnum(L, s)]));
742 lua_pushnumber(n); break; 744 lua_pushnumber(L, n); break;
743 } 745 }
744 case 'q' : { int n1=getnum(s); int n2=getnum(s); 746 case 'q' : { int n1=getnum(L, s); int n2=getnum(L, s);
745 lua_pushnumber(lua_equalobj(reg[n1], reg[n2])); 747 lua_pushnumber(L, lua_equalobj(L, reg[n1], reg[n2]));
746 break; 748 break;
747 } 749 }
748 default: luaL_verror("unknown command in `testC': %c", *(s-1)); 750 default: luaL_verror(L, "unknown command in `testC': %c", *(s-1));
749 } 751 }
750 if (*s == 0) return; 752 if (*s == 0) return;
751 if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'"); 753 if (*s++ != ' ') lua_error(L, "missing ` ' between commands in `testC'");
752 } 754 }
753} 755}
754 756
@@ -804,15 +806,12 @@ static const struct luaL_reg builtin_funcs[] = {
804}; 806};
805 807
806 808
807#define INTFUNCSIZE (sizeof(builtin_funcs)/sizeof(builtin_funcs[0])) 809void luaB_predefine (lua_State *L) {
808
809
810void luaB_predefine (void) {
811 /* pre-register mem error messages, to avoid loop when error arises */ 810 /* pre-register mem error messages, to avoid loop when error arises */
812 luaS_newfixedstring(tableEM); 811 luaS_newfixedstring(L, tableEM);
813 luaS_newfixedstring(memEM); 812 luaS_newfixedstring(L, memEM);
814 luaL_openlib(builtin_funcs, (sizeof(builtin_funcs)/sizeof(builtin_funcs[0]))); 813 luaL_openlib(L, builtin_funcs, (sizeof(builtin_funcs)/sizeof(builtin_funcs[0])));
815 lua_pushstring(LUA_VERSION); 814 lua_pushstring(L, LUA_VERSION);
816 lua_setglobal("_VERSION"); 815 lua_setglobal(L, "_VERSION");
817} 816}
818 817
diff --git a/lbuiltin.h b/lbuiltin.h
index 07210f85..6a4915d0 100644
--- a/lbuiltin.h
+++ b/lbuiltin.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: $ 2** $Id: lbuiltin.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $
3** Built-in functions 3** Built-in functions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -7,8 +7,9 @@
7#ifndef lbuiltin_h 7#ifndef lbuiltin_h
8#define lbuiltin_h 8#define lbuiltin_h
9 9
10#include "lua.h"
10 11
11void luaB_predefine (void); 12void luaB_predefine (lua_State *L);
12 13
13 14
14#endif 15#endif
diff --git a/ldblib.c b/ldblib.c
index 432f8426..0bd3cc52 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.5 1999/03/04 21:17:26 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.6 1999/08/16 20:52:00 roberto Exp roberto $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -8,6 +8,8 @@
8#include <stdlib.h> 8#include <stdlib.h>
9#include <string.h> 9#include <string.h>
10 10
11#define LUA_REENTRANT
12
11#include "lauxlib.h" 13#include "lauxlib.h"
12#include "lua.h" 14#include "lua.h"
13#include "luadebug.h" 15#include "luadebug.h"
@@ -15,129 +17,129 @@
15 17
16 18
17 19
18static void settabss (lua_Object t, const char *i, const char *v) { 20static void settabss (lua_State *L, lua_Object t, const char *i, const char *v) {
19 lua_pushobject(t); 21 lua_pushobject(L, t);
20 lua_pushstring(i); 22 lua_pushstring(L, i);
21 lua_pushstring(v); 23 lua_pushstring(L, v);
22 lua_settable(); 24 lua_settable(L);
23} 25}
24 26
25 27
26static void settabsi (lua_Object t, const char *i, int v) { 28static void settabsi (lua_State *L, lua_Object t, const char *i, int v) {
27 lua_pushobject(t); 29 lua_pushobject(L, t);
28 lua_pushstring(i); 30 lua_pushstring(L, i);
29 lua_pushnumber(v); 31 lua_pushnumber(L, v);
30 lua_settable(); 32 lua_settable(L);
31} 33}
32 34
33 35
34static lua_Object getfuncinfo (lua_Object func) { 36static lua_Object getfuncinfo (lua_State *L, lua_Object func) {
35 lua_Object result = lua_createtable(); 37 lua_Object result = lua_createtable(L);
36 const char *str; 38 const char *str;
37 int line; 39 int line;
38 lua_funcinfo(func, &str, &line); 40 lua_funcinfo(L, func, &str, &line);
39 if (line == -1) /* C function? */ 41 if (line == -1) /* C function? */
40 settabss(result, "kind", "C"); 42 settabss(L, result, "kind", "C");
41 else if (line == 0) { /* "main"? */ 43 else if (line == 0) { /* "main"? */
42 settabss(result, "kind", "chunk"); 44 settabss(L, result, "kind", "chunk");
43 settabss(result, "source", str); 45 settabss(L, result, "source", str);
44 } 46 }
45 else { /* Lua function */ 47 else { /* Lua function */
46 settabss(result, "kind", "Lua"); 48 settabss(L, result, "kind", "Lua");
47 settabsi(result, "def_line", line); 49 settabsi(L, result, "def_line", line);
48 settabss(result, "source", str); 50 settabss(L, result, "source", str);
49 } 51 }
50 if (line != 0) { /* is it not a "main"? */ 52 if (line != 0) { /* is it not a "main"? */
51 const char *kind = lua_getobjname(func, &str); 53 const char *kind = lua_getobjname(L, func, &str);
52 if (*kind) { 54 if (*kind) {
53 settabss(result, "name", str); 55 settabss(L, result, "name", str);
54 settabss(result, "where", kind); 56 settabss(L, result, "where", kind);
55 } 57 }
56 } 58 }
57 return result; 59 return result;
58} 60}
59 61
60 62
61static void getstack (void) { 63static void getstack (lua_State *L) {
62 lua_Object func = lua_stackedfunction(luaL_check_int(1)); 64 lua_Object func = lua_stackedfunction(L, luaL_check_int(L, 1));
63 if (func == LUA_NOOBJECT) /* level out of range? */ 65 if (func == LUA_NOOBJECT) /* level out of range? */
64 return; 66 return;
65 else { 67 else {
66 lua_Object result = getfuncinfo(func); 68 lua_Object result = getfuncinfo(L, func);
67 int currline = lua_currentline(func); 69 int currline = lua_currentline(L, func);
68 if (currline > 0) 70 if (currline > 0)
69 settabsi(result, "current", currline); 71 settabsi(L, result, "current", currline);
70 lua_pushobject(result); 72 lua_pushobject(L, result);
71 lua_pushstring("func"); 73 lua_pushstring(L, "func");
72 lua_pushobject(func); 74 lua_pushobject(L, func);
73 lua_settable(); /* result.func = func */ 75 lua_settable(L); /* result.func = func */
74 lua_pushobject(result); 76 lua_pushobject(L, result);
75 } 77 }
76} 78}
77 79
78 80
79static void funcinfo (void) { 81static void funcinfo (lua_State *L) {
80 lua_pushobject(getfuncinfo(luaL_functionarg(1))); 82 lua_pushobject(L, getfuncinfo(L, luaL_functionarg(L, 1)));
81} 83}
82 84
83 85
84static int findlocal (lua_Object func, int arg) { 86static int findlocal (lua_State *L, lua_Object func, int arg) {
85 lua_Object v = lua_getparam(arg); 87 lua_Object v = lua_getparam(L, arg);
86 if (lua_isnumber(v)) 88 if (lua_isnumber(L, v))
87 return (int)lua_getnumber(v); 89 return (int)lua_getnumber(L, v);
88 else { 90 else {
89 const char *name = luaL_check_string(arg); 91 const char *name = luaL_check_string(L, arg);
90 int i = 0; 92 int i = 0;
91 int result = -1; 93 int result = -1;
92 const char *vname; 94 const char *vname;
93 while (lua_getlocal(func, ++i, &vname) != LUA_NOOBJECT) { 95 while (lua_getlocal(L, func, ++i, &vname) != LUA_NOOBJECT) {
94 if (strcmp(name, vname) == 0) 96 if (strcmp(name, vname) == 0)
95 result = i; /* keep looping to get the last var with this name */ 97 result = i; /* keep looping to get the last var with this name */
96 } 98 }
97 if (result == -1) 99 if (result == -1)
98 luaL_verror("no local variable `%.50s' at given level", name); 100 luaL_verror(L, "no local variable `%.50s' at given level", name);
99 return result; 101 return result;
100 } 102 }
101} 103}
102 104
103 105
104static void getlocal (void) { 106static void getlocal (lua_State *L) {
105 lua_Object func = lua_stackedfunction(luaL_check_int(1)); 107 lua_Object func = lua_stackedfunction(L, luaL_check_int(L, 1));
106 lua_Object val; 108 lua_Object val;
107 const char *name; 109 const char *name;
108 if (func == LUA_NOOBJECT) /* level out of range? */ 110 if (func == LUA_NOOBJECT) /* level out of range? */
109 return; /* return nil */ 111 return; /* return nil */
110 else if (lua_getparam(2) != LUA_NOOBJECT) { /* 2nd argument? */ 112 else if (lua_getparam(L, 2) != LUA_NOOBJECT) { /* 2nd argument? */
111 if ((val = lua_getlocal(func, findlocal(func, 2), &name)) != LUA_NOOBJECT) { 113 if ((val = lua_getlocal(L, func, findlocal(L, func, 2), &name)) != LUA_NOOBJECT) {
112 lua_pushobject(val); 114 lua_pushobject(L, val);
113 lua_pushstring(name); 115 lua_pushstring(L, name);
114 } 116 }
115 /* else return nil */ 117 /* else return nil */
116 } 118 }
117 else { /* collect all locals in a table */ 119 else { /* collect all locals in a table */
118 lua_Object result = lua_createtable(); 120 lua_Object result = lua_createtable(L);
119 int i; 121 int i;
120 for (i=1; ;i++) { 122 for (i=1; ;i++) {
121 if ((val = lua_getlocal(func, i, &name)) == LUA_NOOBJECT) 123 if ((val = lua_getlocal(L, func, i, &name)) == LUA_NOOBJECT)
122 break; 124 break;
123 lua_pushobject(result); 125 lua_pushobject(L, result);
124 lua_pushstring(name); 126 lua_pushstring(L, name);
125 lua_pushobject(val); 127 lua_pushobject(L, val);
126 lua_settable(); /* result[name] = value */ 128 lua_settable(L); /* result[name] = value */
127 } 129 }
128 lua_pushobject(result); 130 lua_pushobject(L, result);
129 } 131 }
130} 132}
131 133
132 134
133static void setlocal (void) { 135static void setlocal (lua_State *L) {
134 lua_Object func = lua_stackedfunction(luaL_check_int(1)); 136 lua_Object func = lua_stackedfunction(L, luaL_check_int(L, 1));
135 int numvar; 137 int numvar;
136 luaL_arg_check(func != LUA_NOOBJECT, 1, "level out of range"); 138 luaL_arg_check(L, func != LUA_NOOBJECT, 1, "level out of range");
137 numvar = findlocal(func, 2); 139 numvar = findlocal(L, func, 2);
138 lua_pushobject(luaL_nonnullarg(3)); 140 lua_pushobject(L, luaL_nonnullarg(L, 3));
139 if (!lua_setlocal(func, numvar)) 141 if (!lua_setlocal(L, func, numvar))
140 lua_error("no such local variable"); 142 lua_error(L, "no such local variable");
141} 143}
142 144
143 145
@@ -146,57 +148,57 @@ static int linehook = -1; /* Lua reference to line hook function */
146static int callhook = -1; /* Lua reference to call hook function */ 148static int callhook = -1; /* Lua reference to call hook function */
147 149
148 150
149static void dohook (int ref) { 151static void dohook (lua_State *L, int ref) {
150 lua_LHFunction oldlinehook = lua_setlinehook(NULL); 152 lua_LHFunction oldlinehook = lua_setlinehook(L, NULL);
151 lua_CHFunction oldcallhook = lua_setcallhook(NULL); 153 lua_CHFunction oldcallhook = lua_setcallhook(L, NULL);
152 lua_callfunction(lua_getref(ref)); 154 lua_callfunction(L, lua_getref(L, ref));
153 lua_setlinehook(oldlinehook); 155 lua_setlinehook(L, oldlinehook);
154 lua_setcallhook(oldcallhook); 156 lua_setcallhook(L, oldcallhook);
155} 157}
156 158
157 159
158static void linef (int line) { 160static void linef (lua_State *L, int line) {
159 lua_pushnumber(line); 161 lua_pushnumber(L, line);
160 dohook(linehook); 162 dohook(L, linehook);
161} 163}
162 164
163 165
164static void callf (lua_Function func, const char *file, int line) { 166static void callf (lua_State *L, lua_Function func, const char *file, int line) {
165 if (func != LUA_NOOBJECT) { 167 if (func != LUA_NOOBJECT) {
166 lua_pushobject(func); 168 lua_pushobject(L, func);
167 lua_pushstring(file); 169 lua_pushstring(L, file);
168 lua_pushnumber(line); 170 lua_pushnumber(L, line);
169 } 171 }
170 dohook(callhook); 172 dohook(L, callhook);
171} 173}
172 174
173 175
174static void setcallhook (void) { 176static void setcallhook (lua_State *L) {
175 lua_Object f = lua_getparam(1); 177 lua_Object f = lua_getparam(L, 1);
176 lua_unref(callhook); 178 lua_unref(L, callhook);
177 if (f == LUA_NOOBJECT) { 179 if (f == LUA_NOOBJECT) {
178 callhook = -1; 180 callhook = -1;
179 lua_setcallhook(NULL); 181 lua_setcallhook(L, NULL);
180 } 182 }
181 else { 183 else {
182 lua_pushobject(f); 184 lua_pushobject(L, f);
183 callhook = lua_ref(1); 185 callhook = lua_ref(L, 1);
184 lua_setcallhook(callf); 186 lua_setcallhook(L, callf);
185 } 187 }
186} 188}
187 189
188 190
189static void setlinehook (void) { 191static void setlinehook (lua_State *L) {
190 lua_Object f = lua_getparam(1); 192 lua_Object f = lua_getparam(L, 1);
191 lua_unref(linehook); 193 lua_unref(L, linehook);
192 if (f == LUA_NOOBJECT) { 194 if (f == LUA_NOOBJECT) {
193 linehook = -1; 195 linehook = -1;
194 lua_setlinehook(NULL); 196 lua_setlinehook(L, NULL);
195 } 197 }
196 else { 198 else {
197 lua_pushobject(f); 199 lua_pushobject(L, f);
198 linehook = lua_ref(1); 200 linehook = lua_ref(L, 1);
199 lua_setlinehook(linef); 201 lua_setlinehook(L, linef);
200 } 202 }
201} 203}
202 204
@@ -211,7 +213,7 @@ static const struct luaL_reg dblib[] = {
211}; 213};
212 214
213 215
214void lua_dblibopen (void) { 216void lua_dblibopen (lua_State *L) {
215 luaL_openlib(dblib, (sizeof(dblib)/sizeof(dblib[0]))); 217 luaL_openlib(L, dblib, (sizeof(dblib)/sizeof(dblib[0])));
216} 218}
217 219
diff --git a/ldo.c b/ldo.c
index c8797a7c..8a534041 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.50 1999/10/14 19:46:57 roberto Exp roberto $ 2** $Id: ldo.c,v 1.51 1999/11/04 17:22:26 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*/
@@ -9,6 +9,8 @@
9#include <stdlib.h> 9#include <stdlib.h>
10#include <string.h> 10#include <string.h>
11 11
12#define LUA_REENTRANT
13
12#include "lauxlib.h" 14#include "lauxlib.h"
13#include "ldo.h" 15#include "ldo.h"
14#include "lgc.h" 16#include "lgc.h"
@@ -41,26 +43,26 @@
41#endif 43#endif
42 44
43 45
44void luaD_init (void) { 46void luaD_init (lua_State *L) {
45 L->stack.stack = luaM_newvector(STACK_UNIT, TObject); 47 L->stack.stack = luaM_newvector(L, STACK_UNIT, TObject);
46 L->stack.top = L->stack.stack; 48 L->stack.top = L->stack.stack;
47 L->stack.last = L->stack.stack+(STACK_UNIT-1); 49 L->stack.last = L->stack.stack+(STACK_UNIT-1);
48} 50}
49 51
50 52
51void luaD_checkstack (int n) { 53void luaD_checkstack (lua_State *L, int n) {
52 struct Stack *S = &L->stack; 54 struct Stack *S = &L->stack;
53 if (S->last-S->top <= n) { 55 if (S->last-S->top <= n) {
54 StkId top = S->top-S->stack; 56 StkId top = S->top-S->stack;
55 int stacksize = (S->last-S->stack)+STACK_UNIT+n; 57 int stacksize = (S->last-S->stack)+STACK_UNIT+n;
56 luaM_reallocvector(S->stack, stacksize, TObject); 58 luaM_reallocvector(L, S->stack, stacksize, TObject);
57 S->last = S->stack+(stacksize-1); 59 S->last = S->stack+(stacksize-1);
58 S->top = S->stack + top; 60 S->top = S->stack + top;
59 if (stacksize >= STACK_LIMIT) { /* stack overflow? */ 61 if (stacksize >= STACK_LIMIT) { /* stack overflow? */
60 if (lua_stackedfunction(100) == LUA_NOOBJECT) /* 100 funcs on stack? */ 62 if (lua_stackedfunction(L, 100) == LUA_NOOBJECT) /* 100 funcs on stack? */
61 lua_error("Lua2C - C2Lua overflow"); /* doesn't look like a rec. loop */ 63 lua_error(L, "Lua2C - C2Lua overflow"); /* doesn't look like a rec. loop */
62 else 64 else
63 lua_error("stack size overflow"); 65 lua_error(L, "stack size overflow");
64 } 66 }
65 } 67 }
66} 68}
@@ -69,12 +71,12 @@ void luaD_checkstack (int n) {
69/* 71/*
70** Adjust stack. Set top to the given value, pushing NILs if needed. 72** Adjust stack. Set top to the given value, pushing NILs if needed.
71*/ 73*/
72void luaD_adjusttop (StkId newtop) { 74void luaD_adjusttop (lua_State *L, StkId newtop) {
73 int diff = newtop-(L->stack.top-L->stack.stack); 75 int diff = newtop-(L->stack.top-L->stack.stack);
74 if (diff <= 0) 76 if (diff <= 0)
75 L->stack.top += diff; 77 L->stack.top += diff;
76 else { 78 else {
77 luaD_checkstack(diff); 79 luaD_checkstack(L, diff);
78 while (diff--) 80 while (diff--)
79 ttype(L->stack.top++) = LUA_T_NIL; 81 ttype(L->stack.top++) = LUA_T_NIL;
80 } 82 }
@@ -84,35 +86,34 @@ void luaD_adjusttop (StkId newtop) {
84/* 86/*
85** Open a hole below "nelems" from the L->stack.top. 87** Open a hole below "nelems" from the L->stack.top.
86*/ 88*/
87void luaD_openstack (int nelems) { 89void luaD_openstack (lua_State *L, int nelems) {
88 luaO_memup(L->stack.top-nelems+1, L->stack.top-nelems, 90 luaO_memup(L->stack.top-nelems+1, L->stack.top-nelems,
89 nelems*sizeof(TObject)); 91 nelems*sizeof(TObject));
90 incr_top; 92 incr_top;
91} 93}
92 94
93 95
94void luaD_lineHook (int line) { 96void luaD_lineHook (lua_State *L, int line) {
95 struct C_Lua_Stack oldCLS = L->Cstack; 97 struct C_Lua_Stack oldCLS = L->Cstack;
96 StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack; 98 StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
97 L->Cstack.num = 0; 99 L->Cstack.num = 0;
98 (*L->linehook)(line); 100 (*L->linehook)(L, line);
99 L->stack.top = L->stack.stack+old_top; 101 L->stack.top = L->stack.stack+old_top;
100 L->Cstack = oldCLS; 102 L->Cstack = oldCLS;
101} 103}
102 104
103 105
104void luaD_callHook (StkId base, const TProtoFunc *tf, int isreturn) { 106void luaD_callHook (lua_State *L, StkId base, const TProtoFunc *tf, int isreturn) {
105 struct C_Lua_Stack oldCLS = L->Cstack; 107 struct C_Lua_Stack oldCLS = L->Cstack;
106 StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack; 108 StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
107 L->Cstack.num = 0; 109 L->Cstack.num = 0;
108 if (isreturn) 110 if (isreturn)
109 (*L->callhook)(LUA_NOOBJECT, "(return)", 0); 111 (*L->callhook)(L, LUA_NOOBJECT, "(return)", 0);
110 else { 112 else {
111 TObject *f = L->stack.stack+base-1; 113 TObject *f = L->stack.stack+base-1;
112 if (tf) 114 if (tf)
113 (*L->callhook)(Ref(f), tf->source->str, tf->lineDefined); 115 (*L->callhook)(L, Ref(L, f), tf->source->str, tf->lineDefined);
114 else 116 else (*L->callhook)(L, Ref(L, f), "(C)", -1);
115 (*L->callhook)(Ref(f), "(C)", -1);
116 } 117 }
117 L->stack.top = L->stack.stack+old_top; 118 L->stack.top = L->stack.stack+old_top;
118 L->Cstack = oldCLS; 119 L->Cstack = oldCLS;
@@ -124,7 +125,7 @@ void luaD_callHook (StkId base, const TProtoFunc *tf, int isreturn) {
124** Cstack.num is the number of arguments; Cstack.lua2C points to the 125** Cstack.num is the number of arguments; Cstack.lua2C points to the
125** first argument. Returns an index to the first result from C. 126** first argument. Returns an index to the first result from C.
126*/ 127*/
127static StkId callC (lua_CFunction f, StkId base) { 128static StkId callC (lua_State *L, lua_CFunction f, StkId base) {
128 struct C_Lua_Stack *cls = &L->Cstack; 129 struct C_Lua_Stack *cls = &L->Cstack;
129 struct C_Lua_Stack oldCLS = *cls; 130 struct C_Lua_Stack oldCLS = *cls;
130 StkId firstResult; 131 StkId firstResult;
@@ -133,35 +134,35 @@ static StkId callC (lua_CFunction f, StkId base) {
133 cls->lua2C = base; 134 cls->lua2C = base;
134 cls->base = base+numarg; /* == top-stack */ 135 cls->base = base+numarg; /* == top-stack */
135 if (L->callhook) 136 if (L->callhook)
136 luaD_callHook(base, NULL, 0); 137 luaD_callHook(L, base, NULL, 0);
137 (*f)(); /* do the actual call */ 138 (*f)(L); /* do the actual call */
138 if (L->callhook) /* func may have changed callhook */ 139 if (L->callhook) /* func may have changed callhook */
139 luaD_callHook(base, NULL, 1); 140 luaD_callHook(L, base, NULL, 1);
140 firstResult = cls->base; 141 firstResult = cls->base;
141 *cls = oldCLS; 142 *cls = oldCLS;
142 return firstResult; 143 return firstResult;
143} 144}
144 145
145 146
146static StkId callCclosure (const struct Closure *cl, 147static StkId callCclosure (lua_State *L, const struct Closure *cl,
147 lua_CFunction f, StkId base) { 148 lua_CFunction f, StkId base) {
148 TObject *pbase; 149 TObject *pbase;
149 int nup = cl->nelems; /* number of upvalues */ 150 int nup = cl->nelems; /* number of upvalues */
150 luaD_checkstack(nup); 151 luaD_checkstack(L, nup);
151 pbase = L->stack.stack+base; /* care: previous call may change this */ 152 pbase = L->stack.stack+base; /* care: previous call may change this */
152 /* open space for upvalues as extra arguments */ 153 /* open space for upvalues as extra arguments */
153 luaO_memup(pbase+nup, pbase, (L->stack.top-pbase)*sizeof(TObject)); 154 luaO_memup(pbase+nup, pbase, (L->stack.top-pbase)*sizeof(TObject));
154 /* copy upvalues into stack */ 155 /* copy upvalues into stack */
155 memcpy(pbase, cl->consts+1, nup*sizeof(TObject)); 156 memcpy(pbase, cl->consts+1, nup*sizeof(TObject));
156 L->stack.top += nup; 157 L->stack.top += nup;
157 return callC(f, base); 158 return callC(L, f, base);
158} 159}
159 160
160 161
161void luaD_callTM (const TObject *f, int nParams, int nResults) { 162void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
162 luaD_openstack(nParams); 163 luaD_openstack(L, nParams);
163 *(L->stack.top-nParams-1) = *f; 164 *(L->stack.top-nParams-1) = *f;
164 luaD_calln(nParams, nResults); 165 luaD_calln(L, nParams, nResults);
165} 166}
166 167
167 168
@@ -172,7 +173,7 @@ void luaD_callTM (const TObject *f, int nParams, int nResults) {
172** When returns, the results are on the stack, between [top-nArgs-1,top). 173** When returns, the results are on the stack, between [top-nArgs-1,top).
173** The number of results is nResults, unless nResults=MULT_RET. 174** The number of results is nResults, unless nResults=MULT_RET.
174*/ 175*/
175void luaD_calln (int nArgs, int nResults) { 176void luaD_calln (lua_State *L, int nArgs, int nResults) {
176 struct Stack *S = &L->stack; /* to optimize */ 177 struct Stack *S = &L->stack; /* to optimize */
177 StkId base = (S->top-S->stack)-nArgs; 178 StkId base = (S->top-S->stack)-nArgs;
178 TObject *func = S->stack+base-1; 179 TObject *func = S->stack+base-1;
@@ -181,27 +182,27 @@ void luaD_calln (int nArgs, int nResults) {
181 switch (ttype(func)) { 182 switch (ttype(func)) {
182 case LUA_T_CPROTO: 183 case LUA_T_CPROTO:
183 ttype(func) = LUA_T_CMARK; 184 ttype(func) = LUA_T_CMARK;
184 firstResult = callC(fvalue(func), base); 185 firstResult = callC(L, fvalue(func), base);
185 break; 186 break;
186 case LUA_T_PROTO: 187 case LUA_T_PROTO:
187 ttype(func) = LUA_T_PMARK; 188 ttype(func) = LUA_T_PMARK;
188 firstResult = luaV_execute(NULL, tfvalue(func), base); 189 firstResult = luaV_execute(L, NULL, tfvalue(func), base);
189 break; 190 break;
190 case LUA_T_CLOSURE: { 191 case LUA_T_CLOSURE: {
191 Closure *c = clvalue(func); 192 Closure *c = clvalue(func);
192 TObject *proto = c->consts; 193 TObject *proto = c->consts;
193 ttype(func) = LUA_T_CLMARK; 194 ttype(func) = LUA_T_CLMARK;
194 firstResult = (ttype(proto) == LUA_T_CPROTO) ? 195 firstResult = (ttype(proto) == LUA_T_CPROTO) ?
195 callCclosure(c, fvalue(proto), base) : 196 callCclosure(L, c, fvalue(proto), base) :
196 luaV_execute(c, tfvalue(proto), base); 197 luaV_execute(L, c, tfvalue(proto), base);
197 break; 198 break;
198 } 199 }
199 default: { /* func is not a function */ 200 default: { /* func is not a function */
200 /* Check the tag method for invalid functions */ 201 /* Check the tag method for invalid functions */
201 const TObject *im = luaT_getimbyObj(func, IM_FUNCTION); 202 const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
202 if (ttype(im) == LUA_T_NIL) 203 if (ttype(im) == LUA_T_NIL)
203 lua_error("call expression not a function"); 204 lua_error(L, "call expression not a function");
204 luaD_callTM(im, (S->top-S->stack)-(base-1), nResults); 205 luaD_callTM(L, im, (S->top-S->stack)-(base-1), nResults);
205 return; 206 return;
206 } 207 }
207 } 208 }
@@ -209,7 +210,7 @@ void luaD_calln (int nArgs, int nResults) {
209 if (nResults == MULT_RET) 210 if (nResults == MULT_RET)
210 nResults = (S->top-S->stack)-firstResult; 211 nResults = (S->top-S->stack)-firstResult;
211 else 212 else
212 luaD_adjusttop(firstResult+nResults); 213 luaD_adjusttop(L, firstResult+nResults);
213 /* move results to base-1 (to erase parameters and function) */ 214 /* move results to base-1 (to erase parameters and function) */
214 base--; 215 base--;
215 for (i=0; i<nResults; i++) 216 for (i=0; i<nResults; i++)
@@ -218,26 +219,26 @@ void luaD_calln (int nArgs, int nResults) {
218} 219}
219 220
220 221
221static void message (const char *s) { 222static void message (lua_State *L, const char *s) {
222 const TObject *em = &(luaS_assertglobalbyname("_ERRORMESSAGE")->value); 223 const TObject *em = &(luaS_assertglobalbyname(L, "_ERRORMESSAGE")->value);
223 if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO || 224 if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO ||
224 ttype(em) == LUA_T_CLOSURE) { 225 ttype(em) == LUA_T_CLOSURE) {
225 *L->stack.top = *em; 226 *L->stack.top = *em;
226 incr_top; 227 incr_top;
227 lua_pushstring(s); 228 lua_pushstring(L, s);
228 luaD_calln(1, 0); 229 luaD_calln(L, 1, 0);
229 } 230 }
230} 231}
231 232
232/* 233/*
233** Reports an error, and jumps up to the available recover label 234** Reports an error, and jumps up to the available recover label
234*/ 235*/
235void lua_error (const char *s) { 236void lua_error (lua_State *L, const char *s) {
236 if (s) message(s); 237 if (s) message(L, s);
237 if (L->errorJmp) 238 if (L->errorJmp)
238 longjmp(L->errorJmp->b, 1); 239 longjmp(L->errorJmp->b, 1);
239 else { 240 else {
240 message("exit(1). Unable to recover.\n"); 241 message(L, "exit(1). Unable to recover.\n");
241 exit(1); 242 exit(1);
242 } 243 }
243} 244}
@@ -247,7 +248,7 @@ void lua_error (const char *s) {
247** Execute a protected call. Assumes that function is at L->Cstack.base and 248** Execute a protected call. Assumes that function is at L->Cstack.base and
248** parameters are on top of it. Leave nResults on the stack. 249** parameters are on top of it. Leave nResults on the stack.
249*/ 250*/
250int luaD_protectedrun (void) { 251int luaD_protectedrun (lua_State *L) {
251 volatile struct C_Lua_Stack oldCLS = L->Cstack; 252 volatile struct C_Lua_Stack oldCLS = L->Cstack;
252 struct lua_longjmp myErrorJmp; 253 struct lua_longjmp myErrorJmp;
253 volatile int status; 254 volatile int status;
@@ -255,7 +256,7 @@ int luaD_protectedrun (void) {
255 L->errorJmp = &myErrorJmp; 256 L->errorJmp = &myErrorJmp;
256 if (setjmp(myErrorJmp.b) == 0) { 257 if (setjmp(myErrorJmp.b) == 0) {
257 StkId base = L->Cstack.base; 258 StkId base = L->Cstack.base;
258 luaD_calln((L->stack.top-L->stack.stack)-base-1, MULT_RET); 259 luaD_calln(L, (L->stack.top-L->stack.stack)-base-1, MULT_RET);
259 L->Cstack.lua2C = base; /* position of the new results */ 260 L->Cstack.lua2C = base; /* position of the new results */
260 L->Cstack.num = (L->stack.top-L->stack.stack) - base; 261 L->Cstack.num = (L->stack.top-L->stack.stack) - base;
261 L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */ 262 L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
@@ -274,7 +275,7 @@ int luaD_protectedrun (void) {
274/* 275/*
275** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load 276** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
276*/ 277*/
277static int protectedparser (ZIO *z, int bin) { 278static int protectedparser (lua_State *L, ZIO *z, int bin) {
278 volatile struct C_Lua_Stack oldCLS = L->Cstack; 279 volatile struct C_Lua_Stack oldCLS = L->Cstack;
279 struct lua_longjmp myErrorJmp; 280 struct lua_longjmp myErrorJmp;
280 volatile int status; 281 volatile int status;
@@ -282,7 +283,7 @@ static int protectedparser (ZIO *z, int bin) {
282 struct lua_longjmp *volatile oldErr = L->errorJmp; 283 struct lua_longjmp *volatile oldErr = L->errorJmp;
283 L->errorJmp = &myErrorJmp; 284 L->errorJmp = &myErrorJmp;
284 if (setjmp(myErrorJmp.b) == 0) { 285 if (setjmp(myErrorJmp.b) == 0) {
285 tf = bin ? luaU_undump1(z) : luaY_parser(z); 286 tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
286 status = 0; 287 status = 0;
287 } 288 }
288 else { /* an error occurred: restore L->Cstack and L->stack.top */ 289 else { /* an error occurred: restore L->Cstack and L->stack.top */
@@ -294,26 +295,26 @@ static int protectedparser (ZIO *z, int bin) {
294 L->errorJmp = oldErr; 295 L->errorJmp = oldErr;
295 if (status) return 1; /* error code */ 296 if (status) return 1; /* error code */
296 if (tf == NULL) return 2; /* 'natural' end */ 297 if (tf == NULL) return 2; /* 'natural' end */
297 luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */ 298 luaD_adjusttop(L, L->Cstack.base+1); /* one slot for the pseudo-function */
298 L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO; 299 L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
299 L->stack.stack[L->Cstack.base].value.tf = tf; 300 L->stack.stack[L->Cstack.base].value.tf = tf;
300 luaV_closure(0); 301 luaV_closure(L, 0);
301 return 0; 302 return 0;
302} 303}
303 304
304 305
305static int do_main (ZIO *z, int bin) { 306static int do_main (lua_State *L, ZIO *z, int bin) {
306 int status; 307 int status;
307 int debug = L->debug; /* save debug status */ 308 int debug = L->debug; /* save debug status */
308 do { 309 do {
309 long old_blocks = (luaC_checkGC(), L->nblocks); 310 long old_blocks = (luaC_checkGC(L), L->nblocks);
310 status = protectedparser(z, bin); 311 status = protectedparser(L, z, bin);
311 if (status == 1) return 1; /* error */ 312 if (status == 1) return 1; /* error */
312 else if (status == 2) return 0; /* 'natural' end */ 313 else if (status == 2) return 0; /* 'natural' end */
313 else { 314 else {
314 unsigned long newelems2 = 2*(L->nblocks-old_blocks); 315 unsigned long newelems2 = 2*(L->nblocks-old_blocks);
315 L->GCthreshold += newelems2; 316 L->GCthreshold += newelems2;
316 status = luaD_protectedrun(); 317 status = luaD_protectedrun(L);
317 L->GCthreshold -= newelems2; 318 L->GCthreshold -= newelems2;
318 } 319 }
319 } while (bin && status == 0); 320 } while (bin && status == 0);
@@ -322,19 +323,19 @@ static int do_main (ZIO *z, int bin) {
322} 323}
323 324
324 325
325void luaD_gcIM (const TObject *o) { 326void luaD_gcIM (lua_State *L, const TObject *o) {
326 const TObject *im = luaT_getimbyObj(o, IM_GC); 327 const TObject *im = luaT_getimbyObj(L, o, IM_GC);
327 if (ttype(im) != LUA_T_NIL) { 328 if (ttype(im) != LUA_T_NIL) {
328 *L->stack.top = *o; 329 *L->stack.top = *o;
329 incr_top; 330 incr_top;
330 luaD_callTM(im, 1, 0); 331 luaD_callTM(L, im, 1, 0);
331 } 332 }
332} 333}
333 334
334 335
335#define MAXFILENAME 260 /* maximum part of a file name kept */ 336#define MAXFILENAME 260 /* maximum part of a file name kept */
336 337
337int lua_dofile (const char *filename) { 338int lua_dofile (lua_State *L, const char *filename) {
338 ZIO z; 339 ZIO z;
339 int status; 340 int status;
340 int c; 341 int c;
@@ -350,22 +351,22 @@ int lua_dofile (const char *filename) {
350 f = freopen(filename, "rb", f); /* set binary mode */ 351 f = freopen(filename, "rb", f); /* set binary mode */
351 luaL_filesource(source, filename, sizeof(source)); 352 luaL_filesource(source, filename, sizeof(source));
352 luaZ_Fopen(&z, f, source); 353 luaZ_Fopen(&z, f, source);
353 status = do_main(&z, bin); 354 status = do_main(L, &z, bin);
354 if (f != stdin) 355 if (f != stdin)
355 fclose(f); 356 fclose(f);
356 return status; 357 return status;
357} 358}
358 359
359 360
360int lua_dostring (const char *str) { 361int lua_dostring (lua_State *L, const char *str) {
361 return lua_dobuffer(str, strlen(str), str); 362 return lua_dobuffer(L, str, strlen(str), str);
362} 363}
363 364
364 365
365int lua_dobuffer (const char *buff, int size, const char *name) { 366int lua_dobuffer (lua_State *L, const char *buff, int size, const char *name) {
366 ZIO z; 367 ZIO z;
367 if (!name) name = "?"; 368 if (!name) name = "?";
368 luaZ_mopen(&z, buff, size, name); 369 luaZ_mopen(&z, buff, size, name);
369 return do_main(&z, buff[0]==ID_CHUNK); 370 return do_main(L, &z, buff[0]==ID_CHUNK);
370} 371}
371 372
diff --git a/ldo.h b/ldo.h
index f3f19cd8..9adc6aba 100644
--- a/ldo.h
+++ b/ldo.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.h,v 1.8 1999/10/04 17:51:04 roberto Exp roberto $ 2** $Id: ldo.h,v 1.9 1999/10/14 19:46:57 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*/
@@ -20,26 +20,26 @@
20** macro to increment stack top. 20** macro to increment stack top.
21** There must be always an empty slot at the L->stack.top 21** There must be always an empty slot at the L->stack.top
22*/ 22*/
23#define incr_top { if (L->stack.top >= L->stack.last) luaD_checkstack(1); \ 23#define incr_top { if (L->stack.top >= L->stack.last) luaD_checkstack(L, 1); \
24 L->stack.top++; } 24 L->stack.top++; }
25 25
26 26
27/* macros to convert from lua_Object to (TObject *) and back */ 27/* macros to convert from lua_Object to (TObject *) and back */
28 28
29#define Address(lo) ((lo)+L->stack.stack-1) 29#define Address(L, lo) ((lo)+L->stack.stack-1)
30#define Ref(st) ((st)-L->stack.stack+1) 30#define Ref(L, st) ((st)-L->stack.stack+1)
31 31
32 32
33void luaD_init (void); 33void luaD_init (lua_State *L);
34void luaD_adjusttop (StkId newtop); 34void luaD_adjusttop (lua_State *L, StkId newtop);
35void luaD_openstack (int nelems); 35void luaD_openstack (lua_State *L, int nelems);
36void luaD_lineHook (int line); 36void luaD_lineHook (lua_State *L, int line);
37void luaD_callHook (StkId base, const TProtoFunc *tf, int isreturn); 37void luaD_callHook (lua_State *L, StkId base, const TProtoFunc *tf, int isreturn);
38void luaD_calln (int nArgs, int nResults); 38void luaD_calln (lua_State *L, int nArgs, int nResults);
39void luaD_callTM (const TObject *f, int nParams, int nResults); 39void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults);
40int luaD_protectedrun (void); 40int luaD_protectedrun (lua_State *L);
41void luaD_gcIM (const TObject *o); 41void luaD_gcIM (lua_State *L, const TObject *o);
42void luaD_checkstack (int n); 42void luaD_checkstack (lua_State *L, int n);
43 43
44 44
45#endif 45#endif
diff --git a/lfunc.c b/lfunc.c
index 67aa23d4..46325dea 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.c,v 1.13 1999/10/14 19:46:57 roberto Exp roberto $ 2** $Id: lfunc.c,v 1.14 1999/11/10 15:39:35 roberto Exp roberto $
3** Auxiliary functions to manipulate prototypes and closures 3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -7,28 +7,30 @@
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9 9
10#define LUA_REENTRANT
11
10#include "lfunc.h" 12#include "lfunc.h"
11#include "lmem.h" 13#include "lmem.h"
12#include "lstate.h" 14#include "lstate.h"
13 15
14#define gcsizeproto(p) numblocks(0, sizeof(TProtoFunc)) 16#define gcsizeproto(L, p) numblocks(L, 0, sizeof(TProtoFunc))
15#define gcsizeclosure(c) numblocks(c->nelems, sizeof(Closure)) 17#define gcsizeclosure(L, c) numblocks(L, c->nelems, sizeof(Closure))
16 18
17 19
18 20
19Closure *luaF_newclosure (int nelems) { 21Closure *luaF_newclosure (lua_State *L, int nelems) {
20 Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject)); 22 Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure)+nelems*sizeof(TObject));
21 c->next = L->rootcl; 23 c->next = L->rootcl;
22 L->rootcl = c; 24 L->rootcl = c;
23 c->marked = 0; 25 c->marked = 0;
24 c->nelems = nelems; 26 c->nelems = nelems;
25 L->nblocks += gcsizeclosure(c); 27 L->nblocks += gcsizeclosure(L, c);
26 return c; 28 return c;
27} 29}
28 30
29 31
30TProtoFunc *luaF_newproto (void) { 32TProtoFunc *luaF_newproto (lua_State *L) {
31 TProtoFunc *f = luaM_new(TProtoFunc); 33 TProtoFunc *f = luaM_new(L, TProtoFunc);
32 f->code = NULL; 34 f->code = NULL;
33 f->lineDefined = 0; 35 f->lineDefined = 0;
34 f->source = NULL; 36 f->source = NULL;
@@ -38,23 +40,23 @@ TProtoFunc *luaF_newproto (void) {
38 f->next = L->rootproto; 40 f->next = L->rootproto;
39 L->rootproto = f; 41 L->rootproto = f;
40 f->marked = 0; 42 f->marked = 0;
41 L->nblocks += gcsizeproto(f); 43 L->nblocks += gcsizeproto(L, f);
42 return f; 44 return f;
43} 45}
44 46
45 47
46void luaF_freeproto (TProtoFunc *f) { 48void luaF_freeproto (lua_State *L, TProtoFunc *f) {
47 L->nblocks -= gcsizeproto(f); 49 L->nblocks -= gcsizeproto(L, f);
48 luaM_free(f->code); 50 luaM_free(L, f->code);
49 luaM_free(f->locvars); 51 luaM_free(L, f->locvars);
50 luaM_free(f->consts); 52 luaM_free(L, f->consts);
51 luaM_free(f); 53 luaM_free(L, f);
52} 54}
53 55
54 56
55void luaF_freeclosure (Closure *c) { 57void luaF_freeclosure (lua_State *L, Closure *c) {
56 L->nblocks -= gcsizeclosure(c); 58 L->nblocks -= gcsizeclosure(L, c);
57 luaM_free(c); 59 luaM_free(L, c);
58} 60}
59 61
60 62
diff --git a/lfunc.h b/lfunc.h
index 5681db43..a7ff5fc7 100644
--- a/lfunc.h
+++ b/lfunc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.h,v 1.7 1999/10/04 17:51:04 roberto Exp roberto $ 2** $Id: lfunc.h,v 1.8 1999/10/14 19:46:57 roberto Exp roberto $
3** Lua Function structures 3** Lua Function structures
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -12,10 +12,10 @@
12 12
13 13
14 14
15TProtoFunc *luaF_newproto (void); 15TProtoFunc *luaF_newproto (lua_State *L);
16Closure *luaF_newclosure (int nelems); 16Closure *luaF_newclosure (lua_State *L, int nelems);
17void luaF_freeproto (TProtoFunc *f); 17void luaF_freeproto (lua_State *L, TProtoFunc *f);
18void luaF_freeclosure (Closure *c); 18void luaF_freeclosure (lua_State *L, Closure *c);
19 19
20const char *luaF_getlocalname (const TProtoFunc *func, 20const char *luaF_getlocalname (const TProtoFunc *func,
21 int local_number, int line); 21 int local_number, int line);
diff --git a/lgc.c b/lgc.c
index a71693e1..ad82c411 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,9 +1,10 @@
1/* 1/*
2** $Id: lgc.c,v 1.30 1999/11/04 17:22:26 roberto Exp roberto $ 2** $Id: lgc.c,v 1.31 1999/11/10 15:40:46 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7#define LUA_REENTRANT
7 8
8#include "ldo.h" 9#include "ldo.h"
9#include "lfunc.h" 10#include "lfunc.h"
@@ -19,91 +20,91 @@
19 20
20 21
21 22
22static int markobject (TObject *o); 23static int markobject (lua_State *L, TObject *o);
23 24
24 25
25/* mark a string; marks bigger than 1 cannot be changed */ 26/* mark a string; marks bigger than 1 cannot be changed */
26#define strmark(s) {if ((s)->marked == 0) (s)->marked = 1;} 27#define strmark(L, s) {if ((s)->marked == 0) (s)->marked = 1;}
27 28
28 29
29 30
30static void protomark (TProtoFunc *f) { 31static void protomark (lua_State *L, TProtoFunc *f) {
31 if (!f->marked) { 32 if (!f->marked) {
32 int i; 33 int i;
33 f->marked = 1; 34 f->marked = 1;
34 strmark(f->source); 35 strmark(L, f->source);
35 for (i=f->nconsts-1; i>=0; i--) 36 for (i=f->nconsts-1; i>=0; i--)
36 markobject(&f->consts[i]); 37 markobject(L, &f->consts[i]);
37 } 38 }
38} 39}
39 40
40 41
41static void closuremark (Closure *f) { 42static void closuremark (lua_State *L, Closure *f) {
42 if (!f->marked) { 43 if (!f->marked) {
43 int i; 44 int i;
44 f->marked = 1; 45 f->marked = 1;
45 for (i=f->nelems; i>=0; i--) 46 for (i=f->nelems; i>=0; i--)
46 markobject(&f->consts[i]); 47 markobject(L, &f->consts[i]);
47 } 48 }
48} 49}
49 50
50 51
51static void hashmark (Hash *h) { 52static void hashmark (lua_State *L, Hash *h) {
52 if (!h->marked) { 53 if (!h->marked) {
53 int i; 54 int i;
54 h->marked = 1; 55 h->marked = 1;
55 for (i=h->size-1; i>=0; i--) { 56 for (i=h->size-1; i>=0; i--) {
56 Node *n = node(h,i); 57 Node *n = node(L, h,i);
57 if (ttype(key(n)) != LUA_T_NIL) { 58 if (ttype(key(L, n)) != LUA_T_NIL) {
58 markobject(&n->key); 59 markobject(L, &n->key);
59 markobject(&n->val); 60 markobject(L, &n->val);
60 } 61 }
61 } 62 }
62 } 63 }
63} 64}
64 65
65 66
66static void travglobal (void) { 67static void travglobal (lua_State *L) {
67 GlobalVar *gv; 68 GlobalVar *gv;
68 for (gv=L->rootglobal; gv; gv=gv->next) { 69 for (gv=L->rootglobal; gv; gv=gv->next) {
69 LUA_ASSERT(gv->name->u.s.gv == gv, "inconsistent global name"); 70 LUA_ASSERT(L, gv->name->u.s.gv == gv, "inconsistent global name");
70 if (gv->value.ttype != LUA_T_NIL) { 71 if (gv->value.ttype != LUA_T_NIL) {
71 strmark(gv->name); /* cannot collect non nil global variables */ 72 strmark(L, gv->name); /* cannot collect non nil global variables */
72 markobject(&gv->value); 73 markobject(L, &gv->value);
73 } 74 }
74 } 75 }
75} 76}
76 77
77 78
78static void travstack (void) { 79static void travstack (lua_State *L) {
79 StkId i; 80 StkId i;
80 for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) 81 for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
81 markobject(L->stack.stack+i); 82 markobject(L, L->stack.stack+i);
82} 83}
83 84
84 85
85static void travlock (void) { 86static void travlock (lua_State *L) {
86 int i; 87 int i;
87 for (i=0; i<L->refSize; i++) { 88 for (i=0; i<L->refSize; i++) {
88 if (L->refArray[i].st == LOCK) 89 if (L->refArray[i].st == LOCK)
89 markobject(&L->refArray[i].o); 90 markobject(L, &L->refArray[i].o);
90 } 91 }
91} 92}
92 93
93 94
94static int markobject (TObject *o) { 95static int markobject (lua_State *L, TObject *o) {
95 switch (ttype(o)) { 96 switch (ttype(o)) {
96 case LUA_T_USERDATA: case LUA_T_STRING: 97 case LUA_T_USERDATA: case LUA_T_STRING:
97 strmark(tsvalue(o)); 98 strmark(L, tsvalue(o));
98 break; 99 break;
99 case LUA_T_ARRAY: 100 case LUA_T_ARRAY:
100 hashmark(avalue(o)); 101 hashmark(L, avalue(o));
101 break; 102 break;
102 case LUA_T_CLOSURE: case LUA_T_CLMARK: 103 case LUA_T_CLOSURE: case LUA_T_CLMARK:
103 closuremark(o->value.cl); 104 closuremark(L, o->value.cl);
104 break; 105 break;
105 case LUA_T_PROTO: case LUA_T_PMARK: 106 case LUA_T_PROTO: case LUA_T_PMARK:
106 protomark(o->value.tf); 107 protomark(L, o->value.tf);
107 break; 108 break;
108 default: break; /* numbers, cprotos, etc */ 109 default: break; /* numbers, cprotos, etc */
109 } 110 }
@@ -111,7 +112,7 @@ static int markobject (TObject *o) {
111} 112}
112 113
113 114
114static void collectproto (void) { 115static void collectproto (lua_State *L) {
115 TProtoFunc **p = &L->rootproto; 116 TProtoFunc **p = &L->rootproto;
116 TProtoFunc *next; 117 TProtoFunc *next;
117 while ((next = *p) != NULL) { 118 while ((next = *p) != NULL) {
@@ -121,13 +122,13 @@ static void collectproto (void) {
121 } 122 }
122 else { 123 else {
123 *p = next->next; 124 *p = next->next;
124 luaF_freeproto(next); 125 luaF_freeproto(L, next);
125 } 126 }
126 } 127 }
127} 128}
128 129
129 130
130static void collectclosure (void) { 131static void collectclosure (lua_State *L) {
131 Closure **p = &L->rootcl; 132 Closure **p = &L->rootcl;
132 Closure *next; 133 Closure *next;
133 while ((next = *p) != NULL) { 134 while ((next = *p) != NULL) {
@@ -137,13 +138,13 @@ static void collectclosure (void) {
137 } 138 }
138 else { 139 else {
139 *p = next->next; 140 *p = next->next;
140 luaF_freeclosure(next); 141 luaF_freeclosure(L, next);
141 } 142 }
142 } 143 }
143} 144}
144 145
145 146
146static void collecttable (void) { 147static void collecttable (lua_State *L) {
147 Hash **p = &L->roottable; 148 Hash **p = &L->roottable;
148 Hash *next; 149 Hash *next;
149 while ((next = *p) != NULL) { 150 while ((next = *p) != NULL) {
@@ -153,7 +154,7 @@ static void collecttable (void) {
153 } 154 }
154 else { 155 else {
155 *p = next->next; 156 *p = next->next;
156 luaH_free(next); 157 luaH_free(L, next);
157 } 158 }
158 } 159 }
159} 160}
@@ -163,7 +164,7 @@ static void collecttable (void) {
163** remove from the global list globals whose names will be collected 164** remove from the global list globals whose names will be collected
164** (the global itself is freed when its name is freed) 165** (the global itself is freed when its name is freed)
165*/ 166*/
166static void clear_global_list (int limit) { 167static void clear_global_list (lua_State *L, int limit) {
167 GlobalVar **p = &L->rootglobal; 168 GlobalVar **p = &L->rootglobal;
168 GlobalVar *next; 169 GlobalVar *next;
169 while ((next = *p) != NULL) { 170 while ((next = *p) != NULL) {
@@ -176,13 +177,13 @@ static void clear_global_list (int limit) {
176/* 177/*
177** collect all elements with `marked' < `limit'. 178** collect all elements with `marked' < `limit'.
178** with limit=1, that means all unmarked elements; 179** with limit=1, that means all unmarked elements;
179** with limit=MAX_INT, that means all elements (but EMPTY). 180** with limit=MAX_INT, that means all elements.
180*/ 181*/
181static void collectstring (int limit) { 182static void collectstring (lua_State *L, int limit) {
182 TObject o; /* to call userdata 'gc' tag method */ 183 TObject o; /* to call userdata 'gc' tag method */
183 int i; 184 int i;
184 ttype(&o) = LUA_T_USERDATA; 185 ttype(&o) = LUA_T_USERDATA;
185 clear_global_list(limit); 186 clear_global_list(L, limit);
186 for (i=0; i<NUM_HASHS; i++) { /* for each hash table */ 187 for (i=0; i<NUM_HASHS; i++) { /* for each hash table */
187 stringtable *tb = &L->string_root[i]; 188 stringtable *tb = &L->string_root[i];
188 int j; 189 int j;
@@ -198,74 +199,74 @@ static void collectstring (int limit) {
198 else { /* collect */ 199 else { /* collect */
199 if (next->constindex == -1) { /* is userdata? */ 200 if (next->constindex == -1) { /* is userdata? */
200 tsvalue(&o) = next; 201 tsvalue(&o) = next;
201 luaD_gcIM(&o); 202 luaD_gcIM(L, &o);
202 } 203 }
203 *p = next->nexthash; 204 *p = next->nexthash;
204 luaS_free(next); 205 luaS_free(L, next);
205 tb->nuse--; 206 tb->nuse--;
206 } 207 }
207 } 208 }
208 } 209 }
209 if ((tb->nuse+1)*6 < tb->size) 210 if ((tb->nuse+1)*6 < tb->size)
210 luaS_grow(tb); /* table is too big; `grow' it to a smaller size */ 211 luaS_grow(L, tb); /* table is too big; `grow' it to a smaller size */
211 } 212 }
212} 213}
213 214
214 215
215#ifdef LUA_COMPAT_GC 216#ifdef LUA_COMPAT_GC
216static void tableTM (void) { 217static void tableTM (lua_State *L) {
217 Hash *p; 218 Hash *p;
218 TObject o; 219 TObject o;
219 ttype(&o) = LUA_T_ARRAY; 220 ttype(&o) = LUA_T_ARRAY;
220 for (p = L->roottable; p; p = p->next) { 221 for (p = L->roottable; p; p = p->next) {
221 if (!p->marked) { 222 if (!p->marked) {
222 avalue(&o) = p; 223 avalue(&o) = p;
223 luaD_gcIM(&o); 224 luaD_gcIM(L, &o);
224 } 225 }
225 } 226 }
226} 227}
227#else 228#else
228#define tableTM() /* do nothing */ 229#define tableTM(L) /* do nothing */
229#endif 230#endif
230 231
231 232
232 233
233static void markall (void) { 234static void markall (lua_State *L) {
234 travstack(); /* mark stack objects */ 235 travstack(L); /* mark stack objects */
235 travglobal(); /* mark global variable values and names */ 236 travglobal(L); /* mark global variable values and names */
236 travlock(); /* mark locked objects */ 237 travlock(L); /* mark locked objects */
237 luaT_travtagmethods(markobject); /* mark tag methods */ 238 luaT_travtagmethods(L, markobject); /* mark tag methods */
238} 239}
239 240
240 241
241void luaC_collect (int all) { 242void luaC_collect (lua_State *L, int all) {
242 L->GCthreshold *= 4; /* to avoid GC during GC */ 243 L->GCthreshold *= 4; /* to avoid GC during GC */
243 tableTM(); /* call TM for tables (if LUA_COMPAT_GC) */ 244 tableTM(L); /* call TM for tables (if LUA_COMPAT_GC) */
244 collecttable(); 245 collecttable(L);
245 collectstring(all?MAX_INT:1); 246 collectstring(L, all?MAX_INT:1);
246 collectproto(); 247 collectproto(L);
247 collectclosure(); 248 collectclosure(L);
248} 249}
249 250
250 251
251long lua_collectgarbage (long limit) { 252long lua_collectgarbage (lua_State *L, long limit) {
252 unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */ 253 unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */
253 markall(); 254 markall(L);
254 luaR_invalidaterefs(); 255 luaR_invalidaterefs(L);
255 luaC_collect(0); 256 luaC_collect(L, 0);
256 luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */ 257 luaD_gcIM(L, &luaO_nilobject); /* GC tag method for nil (signal end of GC) */
257 recovered = recovered - L->nblocks; 258 recovered = recovered - L->nblocks;
258 L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit; 259 L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
259 if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */ 260 if (L->Mbuffsize > L->Mbuffnext*4) { /* is buffer too big? */
260 L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */ 261 L->Mbuffsize /= 2; /* still larger than Mbuffnext*2 */
261 luaM_reallocvector(L->Mbuffer, L->Mbuffsize, char); 262 luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
262 } 263 }
263 return recovered; 264 return recovered;
264} 265}
265 266
266 267
267void luaC_checkGC (void) { 268void luaC_checkGC (lua_State *L) {
268 if (L->nblocks >= L->GCthreshold) 269 if (L->nblocks >= L->GCthreshold)
269 lua_collectgarbage(0); 270 lua_collectgarbage(L, 0);
270} 271}
271 272
diff --git a/lgc.h b/lgc.h
index 4f086da3..7e62f737 100644
--- a/lgc.h
+++ b/lgc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.h,v 1.5 1999/08/16 20:52:00 roberto Exp roberto $ 2** $Id: lgc.h,v 1.6 1999/10/04 17:51:04 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -11,8 +11,8 @@
11#include "lobject.h" 11#include "lobject.h"
12 12
13 13
14void luaC_checkGC (void); 14void luaC_checkGC (lua_State *L);
15void luaC_collect (int all); 15void luaC_collect (lua_State *L, int all);
16 16
17 17
18#endif 18#endif
diff --git a/linit.c b/linit.c
index 4759d49a..601f8354 100644
--- a/linit.c
+++ b/linit.c
@@ -1,17 +1,19 @@
1/* 1/*
2** $Id: linit.c,v 1.1 1999/01/08 16:47:44 roberto Exp $ 2** $Id: linit.c,v 1.1 1999/01/08 16:49:32 roberto Exp roberto $
3** Initialization of libraries for lua.c 3** Initialization of libraries for lua.c
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7#define LUA_REENTRANT
8
7#include "lua.h" 9#include "lua.h"
8#include "lualib.h" 10#include "lualib.h"
9 11
10 12
11void lua_userinit (void) { 13void lua_userinit (lua_State *L) {
12 lua_iolibopen(); 14 lua_iolibopen(L);
13 lua_strlibopen(); 15 lua_strlibopen(L);
14 lua_mathlibopen(); 16 lua_mathlibopen(L);
15 lua_dblibopen(); 17 lua_dblibopen(L);
16} 18}
17 19
diff --git a/liolib.c b/liolib.c
index c1d2a455..1a414a9d 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.49 1999/10/26 11:00:12 roberto Exp roberto $ 2** $Id: liolib.c,v 1.50 1999/11/09 17:59:35 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -12,6 +12,8 @@
12#include <string.h> 12#include <string.h>
13#include <time.h> 13#include <time.h>
14 14
15#define LUA_REENTRANT
16
15#include "lauxlib.h" 17#include "lauxlib.h"
16#include "lua.h" 18#include "lua.h"
17#include "luadebug.h" 19#include "luadebug.h"
@@ -22,7 +24,7 @@
22#include <locale.h> 24#include <locale.h>
23#else 25#else
24/* no support for locale and for strerror: fake them */ 26/* no support for locale and for strerror: fake them */
25#define setlocale(a,b) 0 27#define setlocale(a,b) ((void)a, strcmp((b),"C")==0?"C":NULL)
26#define LC_ALL 0 28#define LC_ALL 0
27#define LC_COLLATE 0 29#define LC_COLLATE 0
28#define LC_CTYPE 0 30#define LC_CTYPE 0
@@ -37,7 +39,7 @@
37 39
38#define FIRSTARG 2 /* 1st is upvalue */ 40#define FIRSTARG 2 /* 1st is upvalue */
39 41
40#define CLOSEDTAG(tag) ((tag)-1) /* assume that CLOSEDTAG = iotag-1 */ 42#define CLOSEDTAG(L, tag) ((tag)-1) /* assume that CLOSEDTAG = iotag-1 */
41 43
42 44
43#define FINPUT "_INPUT" 45#define FINPUT "_INPUT"
@@ -47,22 +49,22 @@
47#ifdef POPEN 49#ifdef POPEN
48/* FILE *popen(); 50/* FILE *popen();
49int pclose(); */ 51int pclose(); */
50#define CLOSEFILE(f) ((pclose(f) == -1) ? fclose(f) : 0) 52#define CLOSEFILE(L, f) ((pclose(f) == -1) ? fclose(f) : 0)
51#else 53#else
52/* no support for popen */ 54/* no support for popen */
53#define popen(x,y) NULL /* that is, popen always fails */ 55#define popen(x,y) NULL /* that is, popen always fails */
54#define CLOSEFILE(f) (fclose(f)) 56#define CLOSEFILE(L, f) (fclose(f))
55#endif 57#endif
56 58
57 59
58 60
59static void pushresult (int i) { 61static void pushresult (lua_State *L, int i) {
60 if (i) 62 if (i)
61 lua_pushuserdata(NULL); 63 lua_pushuserdata(L, NULL);
62 else { 64 else {
63 lua_pushnil(); 65 lua_pushnil(L);
64 lua_pushstring(strerror(errno)); 66 lua_pushstring(L, strerror(errno));
65 lua_pushnumber(errno); 67 lua_pushnumber(L, errno);
66 } 68 }
67} 69}
68 70
@@ -73,144 +75,144 @@ static void pushresult (int i) {
73** ======================================================= 75** =======================================================
74*/ 76*/
75 77
76static int gettag (void) { 78static int gettag (lua_State *L) {
77 return (int)lua_getnumber(lua_getparam(IOTAG)); 79 return (int)lua_getnumber(L, lua_getparam(L, IOTAG));
78} 80}
79 81
80 82
81static int ishandle (lua_Object f) { 83static int ishandle (lua_State *L, lua_Object f) {
82 if (lua_isuserdata(f)) { 84 if (lua_isuserdata(L, f)) {
83 int tag = gettag(); 85 int tag = gettag(L);
84 if (lua_tag(f) == CLOSEDTAG(tag)) 86 if (lua_tag(L, f) == CLOSEDTAG(L, tag))
85 lua_error("cannot access a closed file"); 87 lua_error(L, "cannot access a closed file");
86 return lua_tag(f) == tag; 88 return lua_tag(L, f) == tag;
87 } 89 }
88 else return 0; 90 else return 0;
89} 91}
90 92
91 93
92static FILE *getfilebyname (const char *name) { 94static FILE *getfilebyname (lua_State *L, const char *name) {
93 lua_Object f = lua_rawgetglobal(name); 95 lua_Object f = lua_rawgetglobal(L, name);
94 if (!ishandle(f)) 96 if (!ishandle(L, f))
95 luaL_verror("global variable `%.50s' is not a file handle", name); 97 luaL_verror(L, "global variable `%.50s' is not a file handle", name);
96 return lua_getuserdata(f); 98 return lua_getuserdata(L, f);
97} 99}
98 100
99 101
100static FILE *getfile (int arg) { 102static FILE *getfile (lua_State *L, int arg) {
101 lua_Object f = lua_getparam(arg); 103 lua_Object f = lua_getparam(L, arg);
102 return (ishandle(f)) ? lua_getuserdata(f) : NULL; 104 return (ishandle(L, f)) ? lua_getuserdata(L, f) : NULL;
103} 105}
104 106
105 107
106static FILE *getnonullfile (int arg) { 108static FILE *getnonullfile (lua_State *L, int arg) {
107 FILE *f = getfile(arg); 109 FILE *f = getfile(L, arg);
108 luaL_arg_check(f, arg, "invalid file handle"); 110 luaL_arg_check(L, f, arg, "invalid file handle");
109 return f; 111 return f;
110} 112}
111 113
112 114
113static FILE *getfileparam (const char *name, int *arg) { 115static FILE *getfileparam (lua_State *L, const char *name, int *arg) {
114 FILE *f = getfile(*arg); 116 FILE *f = getfile(L, *arg);
115 if (f) { 117 if (f) {
116 (*arg)++; 118 (*arg)++;
117 return f; 119 return f;
118 } 120 }
119 else 121 else
120 return getfilebyname(name); 122 return getfilebyname(L, name);
121} 123}
122 124
123 125
124static int closefile (FILE *f) { 126static int closefile (lua_State *L, FILE *f) {
125 if (f == stdin || f == stdout) 127 if (f == stdin || f == stdout)
126 return 1; 128 return 1;
127 else { 129 else {
128 int tag = gettag(); 130 int tag = gettag(L);
129 lua_pushusertag(f, tag); 131 lua_pushusertag(L, f, tag);
130 lua_settag(CLOSEDTAG(tag)); 132 lua_settag(L, CLOSEDTAG(L, tag));
131 return (CLOSEFILE(f) == 0); 133 return (CLOSEFILE(L, f) == 0);
132 } 134 }
133} 135}
134 136
135 137
136static void io_close (void) { 138static void io_close (lua_State *L) {
137 pushresult(closefile(getnonullfile(FIRSTARG))); 139 pushresult(L, closefile(L, getnonullfile(L, FIRSTARG)));
138} 140}
139 141
140 142
141static void gc_close (void) { 143static void gc_close (lua_State *L) {
142 FILE *f = getnonullfile(FIRSTARG); 144 FILE *f = getnonullfile(L, FIRSTARG);
143 if (f != stdin && f != stdout && f != stderr) { 145 if (f != stdin && f != stdout && f != stderr) {
144 CLOSEFILE(f); 146 CLOSEFILE(L, f);
145 } 147 }
146} 148}
147 149
148 150
149static void io_open (void) { 151static void io_open (lua_State *L) {
150 FILE *f = fopen(luaL_check_string(FIRSTARG), luaL_check_string(FIRSTARG+1)); 152 FILE *f = fopen(luaL_check_string(L, FIRSTARG), luaL_check_string(L, FIRSTARG+1));
151 if (f) lua_pushusertag(f, gettag()); 153 if (f) lua_pushusertag(L, f, gettag(L));
152 else pushresult(0); 154 else pushresult(L, 0);
153} 155}
154 156
155 157
156static void setfile (FILE *f, const char *name, int tag) { 158static void setfile (lua_State *L, FILE *f, const char *name, int tag) {
157 lua_pushusertag(f, tag); 159 lua_pushusertag(L, f, tag);
158 lua_setglobal(name); 160 lua_setglobal(L, name);
159} 161}
160 162
161 163
162static void setreturn (FILE *f, const char *name) { 164static void setreturn (lua_State *L, FILE *f, const char *name) {
163 if (f == NULL) 165 if (f == NULL)
164 pushresult(0); 166 pushresult(L, 0);
165 else { 167 else {
166 int tag = gettag(); 168 int tag = gettag(L);
167 setfile(f, name, tag); 169 setfile(L, f, name, tag);
168 lua_pushusertag(f, tag); 170 lua_pushusertag(L, f, tag);
169 } 171 }
170} 172}
171 173
172 174
173static void io_readfrom (void) { 175static void io_readfrom (lua_State *L) {
174 FILE *current; 176 FILE *current;
175 lua_Object f = lua_getparam(FIRSTARG); 177 lua_Object f = lua_getparam(L, FIRSTARG);
176 if (f == LUA_NOOBJECT) { 178 if (f == LUA_NOOBJECT) {
177 if (closefile(getfilebyname(FINPUT))) 179 if (closefile(L, getfilebyname(L, FINPUT)))
178 current = stdin; 180 current = stdin;
179 else 181 else
180 current = NULL; /* to signal error */ 182 current = NULL; /* to signal error */
181 } 183 }
182 else if (lua_tag(f) == gettag()) /* deprecated option */ 184 else if (lua_tag(L, f) == gettag(L)) /* deprecated option */
183 current = lua_getuserdata(f); 185 current = lua_getuserdata(L, f);
184 else { 186 else {
185 const char *s = luaL_check_string(FIRSTARG); 187 const char *s = luaL_check_string(L, FIRSTARG);
186 current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); 188 current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
187 } 189 }
188 setreturn(current, FINPUT); 190 setreturn(L, current, FINPUT);
189} 191}
190 192
191 193
192static void io_writeto (void) { 194static void io_writeto (lua_State *L) {
193 FILE *current; 195 FILE *current;
194 lua_Object f = lua_getparam(FIRSTARG); 196 lua_Object f = lua_getparam(L, FIRSTARG);
195 if (f == LUA_NOOBJECT) { 197 if (f == LUA_NOOBJECT) {
196 if (closefile(getfilebyname(FOUTPUT))) 198 if (closefile(L, getfilebyname(L, FOUTPUT)))
197 current = stdout; 199 current = stdout;
198 else 200 else
199 current = NULL; /* to signal error */ 201 current = NULL; /* to signal error */
200 } 202 }
201 else if (lua_tag(f) == gettag()) /* deprecated option */ 203 else if (lua_tag(L, f) == gettag(L)) /* deprecated option */
202 current = lua_getuserdata(f); 204 current = lua_getuserdata(L, f);
203 else { 205 else {
204 const char *s = luaL_check_string(FIRSTARG); 206 const char *s = luaL_check_string(L, FIRSTARG);
205 current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w"); 207 current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w");
206 } 208 }
207 setreturn(current, FOUTPUT); 209 setreturn(L, current, FOUTPUT);
208} 210}
209 211
210 212
211static void io_appendto (void) { 213static void io_appendto (lua_State *L) {
212 FILE *current = fopen(luaL_check_string(FIRSTARG), "a"); 214 FILE *current = fopen(luaL_check_string(L, FIRSTARG), "a");
213 setreturn(current, FOUTPUT); 215 setreturn(L, current, FOUTPUT);
214} 216}
215 217
216 218
@@ -232,7 +234,7 @@ static void io_appendto (void) {
232#define NEED_OTHER (EOF-1) /* just some flag different from EOF */ 234#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
233 235
234 236
235static int read_pattern (FILE *f, const char *p) { 237static int read_pattern (lua_State *L, FILE *f, const char *p) {
236 int inskip = 0; /* {skip} level */ 238 int inskip = 0; /* {skip} level */
237 int c = NEED_OTHER; 239 int c = NEED_OTHER;
238 while (*p != '\0') { 240 while (*p != '\0') {
@@ -242,17 +244,17 @@ static int read_pattern (FILE *f, const char *p) {
242 p++; 244 p++;
243 continue; 245 continue;
244 case '}': 246 case '}':
245 if (!inskip) lua_error("unbalanced braces in read pattern"); 247 if (!inskip) lua_error(L, "unbalanced braces in read pattern");
246 inskip--; 248 inskip--;
247 p++; 249 p++;
248 continue; 250 continue;
249 default: { 251 default: {
250 const char *ep = luaI_classend(p); /* get what is next */ 252 const char *ep = luaI_classend(L, p); /* get what is next */
251 int m; /* match result */ 253 int m; /* match result */
252 if (c == NEED_OTHER) c = getc(f); 254 if (c == NEED_OTHER) c = getc(f);
253 m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); 255 m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
254 if (m) { 256 if (m) {
255 if (!inskip) luaL_addchar(c); 257 if (!inskip) luaL_addchar(L, c);
256 c = NEED_OTHER; 258 c = NEED_OTHER;
257 } 259 }
258 switch (*ep) { 260 switch (*ep) {
@@ -263,7 +265,7 @@ static int read_pattern (FILE *f, const char *p) {
263 while (m) { /* reads the same item until it fails */ 265 while (m) { /* reads the same item until it fails */
264 c = getc(f); 266 c = getc(f);
265 m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); 267 m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
266 if (m && !inskip) luaL_addchar(c); 268 if (m && !inskip) luaL_addchar(L, c);
267 } 269 }
268 /* go through to continue reading the pattern */ 270 /* go through to continue reading the pattern */
269 case '?': /* optional */ 271 case '?': /* optional */
@@ -282,26 +284,26 @@ static int read_pattern (FILE *f, const char *p) {
282 284
283#else 285#else
284 286
285#define read_pattern(f,p) (lua_error("read patterns are deprecated"), 0) 287#define read_pattern(L, f,p) (lua_error(L, "read patterns are deprecated"), 0)
286 288
287#endif 289#endif
288 290
289 291
290static int read_number (FILE *f) { 292static int read_number (lua_State *L, FILE *f) {
291 double d; 293 double d;
292 if (fscanf(f, "%lf", &d) == 1) { 294 if (fscanf(f, "%lf", &d) == 1) {
293 lua_pushnumber(d); 295 lua_pushnumber(L, d);
294 return 1; 296 return 1;
295 } 297 }
296 else return 0; /* read fails */ 298 else return 0; /* read fails */
297} 299}
298 300
299 301
300static void read_word (FILE *f) { 302static void read_word (lua_State *L, FILE *f) {
301 int c; 303 int c;
302 do { c = fgetc(f); } while isspace(c); /* skip spaces */ 304 do { c = fgetc(f); } while isspace(c); /* skip spaces */
303 while (c != EOF && !isspace(c)) { 305 while (c != EOF && !isspace(c)) {
304 luaL_addchar(c); 306 luaL_addchar(L, c);
305 c = fgetc(f); 307 c = fgetc(f);
306 } 308 }
307 ungetc(c, f); 309 ungetc(c, f);
@@ -311,127 +313,127 @@ static void read_word (FILE *f) {
311#define HUNK_LINE 256 313#define HUNK_LINE 256
312#define HUNK_FILE BUFSIZ 314#define HUNK_FILE BUFSIZ
313 315
314static int read_line (FILE *f) { 316static int read_line (lua_State *L, FILE *f) {
315 int n; 317 int n;
316 char *b; 318 char *b;
317 do { 319 do {
318 b = luaL_openspace(HUNK_LINE); 320 b = luaL_openspace(L, HUNK_LINE);
319 if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */ 321 if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */
320 n = strlen(b); 322 n = strlen(b);
321 luaL_addsize(n); 323 luaL_addsize(L, n);
322 } while (b[n-1] != '\n'); 324 } while (b[n-1] != '\n');
323 luaL_addsize(-1); /* remove '\n' */ 325 luaL_addsize(L, -1); /* remove '\n' */
324 return 1; 326 return 1;
325} 327}
326 328
327 329
328static void read_file (FILE *f) { 330static void read_file (lua_State *L, FILE *f) {
329 int n; 331 int n;
330 do { 332 do {
331 char *b = luaL_openspace(HUNK_FILE); 333 char *b = luaL_openspace(L, HUNK_FILE);
332 n = fread(b, sizeof(char), HUNK_FILE, f); 334 n = fread(b, sizeof(char), HUNK_FILE, f);
333 luaL_addsize(n); 335 luaL_addsize(L, n);
334 } while (n==HUNK_FILE); 336 } while (n==HUNK_FILE);
335} 337}
336 338
337 339
338static int read_chars (FILE *f, int n) { 340static int read_chars (lua_State *L, FILE *f, int n) {
339 char *b = luaL_openspace(n); 341 char *b = luaL_openspace(L, n);
340 int n1 = fread(b, sizeof(char), n, f); 342 int n1 = fread(b, sizeof(char), n, f);
341 luaL_addsize(n1); 343 luaL_addsize(L, n1);
342 return (n == n1); 344 return (n == n1);
343} 345}
344 346
345 347
346static void io_read (void) { 348static void io_read (lua_State *L) {
347 int arg = FIRSTARG; 349 int arg = FIRSTARG;
348 FILE *f = getfileparam(FINPUT, &arg); 350 FILE *f = getfileparam(L, FINPUT, &arg);
349 lua_Object op = lua_getparam(arg); 351 lua_Object op = lua_getparam(L, arg);
350 do { /* repeat for each part */ 352 do { /* repeat for each part */
351 long l; 353 long l;
352 int success; 354 int success;
353 luaL_resetbuffer(); 355 luaL_resetbuffer(L);
354 if (lua_isnumber(op)) 356 if (lua_isnumber(L, op))
355 success = read_chars(f, (int)lua_getnumber(op)); 357 success = read_chars(L, f, (int)lua_getnumber(L, op));
356 else { 358 else {
357 const char *p = luaL_opt_string(arg, "*l"); 359 const char *p = luaL_opt_string(L, arg, "*l");
358 if (p[0] != '*') 360 if (p[0] != '*')
359 success = read_pattern(f, p); /* deprecated! */ 361 success = read_pattern(L, f, p); /* deprecated! */
360 else { 362 else {
361 switch (p[1]) { 363 switch (p[1]) {
362 case 'n': /* number */ 364 case 'n': /* number */
363 if (!read_number(f)) return; /* read fails */ 365 if (!read_number(L, f)) return; /* read fails */
364 continue; /* number is already pushed; avoid the "pushstring" */ 366 continue; /* number is already pushed; avoid the "pushstring" */
365 case 'l': /* line */ 367 case 'l': /* line */
366 success = read_line(f); 368 success = read_line(L, f);
367 break; 369 break;
368 case 'a': /* file */ 370 case 'a': /* file */
369 read_file(f); 371 read_file(L, f);
370 success = 1; /* always success */ 372 success = 1; /* always success */
371 break; 373 break;
372 case 'w': /* word */ 374 case 'w': /* word */
373 read_word(f); 375 read_word(L, f);
374 success = 0; /* must read something to succeed */ 376 success = 0; /* must read something to succeed */
375 break; 377 break;
376 default: 378 default:
377 luaL_argerror(arg, "invalid format"); 379 luaL_argerror(L, arg, "invalid format");
378 success = 0; /* to avoid warnings */ 380 success = 0; /* to avoid warnings */
379 } 381 }
380 } 382 }
381 } 383 }
382 l = luaL_getsize(); 384 l = luaL_getsize(L);
383 if (!success && l==0) return; /* read fails */ 385 if (!success && l==0) return; /* read fails */
384 lua_pushlstring(luaL_buffer(), l); 386 lua_pushlstring(L, luaL_buffer(L), l);
385 } while ((op = lua_getparam(++arg)) != LUA_NOOBJECT); 387 } while ((op = lua_getparam(L, ++arg)) != LUA_NOOBJECT);
386} 388}
387 389
388/* }====================================================== */ 390/* }====================================================== */
389 391
390 392
391static void io_write (void) { 393static void io_write (lua_State *L) {
392 int arg = FIRSTARG; 394 int arg = FIRSTARG;
393 FILE *f = getfileparam(FOUTPUT, &arg); 395 FILE *f = getfileparam(L, FOUTPUT, &arg);
394 int status = 1; 396 int status = 1;
395 lua_Object o; 397 lua_Object o;
396 while ((o = lua_getparam(arg++)) != LUA_NOOBJECT) { 398 while ((o = lua_getparam(L, arg++)) != LUA_NOOBJECT) {
397 switch (lua_type(o)[2]) { 399 switch (lua_type(L, o)[2]) {
398 case 'r': { /* stRing? */ 400 case 'r': { /* stRing? */
399 long l = lua_strlen(o); 401 long l = lua_strlen(L, o);
400 status = status && 402 status = status &&
401 ((long)fwrite(lua_getstring(o), sizeof(char), l, f) == l); 403 ((long)fwrite(lua_getstring(L, o), sizeof(char), l, f) == l);
402 break; 404 break;
403 } 405 }
404 case 'm': /* nuMber? */ /* LUA_NUMBER */ 406 case 'm': /* nuMber? */ /* LUA_NUMBER */
405 /* optimization: could be done exactly as for strings */ 407 /* optimization: could be done exactly as for strings */
406 status = status && fprintf(f, "%.16g", lua_getnumber(o)) > 0; 408 status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0;
407 break; 409 break;
408 default: luaL_argerror(arg-1, "string expected"); 410 default: luaL_argerror(L, arg-1, "string expected");
409 } 411 }
410 } 412 }
411 pushresult(status); 413 pushresult(L, status);
412} 414}
413 415
414 416
415static void io_seek (void) { 417static void io_seek (lua_State *L) {
416 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; 418 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
417 static const char *const modenames[] = {"set", "cur", "end", NULL}; 419 static const char *const modenames[] = {"set", "cur", "end", NULL};
418 FILE *f = getnonullfile(FIRSTARG); 420 FILE *f = getnonullfile(L, FIRSTARG);
419 int op = luaL_findstring(luaL_opt_string(FIRSTARG+1, "cur"), modenames); 421 int op = luaL_findstring(luaL_opt_string(L, FIRSTARG+1, "cur"), modenames);
420 long offset = luaL_opt_long(FIRSTARG+2, 0); 422 long offset = luaL_opt_long(L, FIRSTARG+2, 0);
421 luaL_arg_check(op != -1, FIRSTARG+1, "invalid mode"); 423 luaL_arg_check(L, op != -1, FIRSTARG+1, "invalid mode");
422 op = fseek(f, offset, mode[op]); 424 op = fseek(f, offset, mode[op]);
423 if (op) 425 if (op)
424 pushresult(0); /* error */ 426 pushresult(L, 0); /* error */
425 else 427 else
426 lua_pushnumber(ftell(f)); 428 lua_pushnumber(L, ftell(f));
427} 429}
428 430
429 431
430static void io_flush (void) { 432static void io_flush (lua_State *L) {
431 FILE *f = getfile(FIRSTARG); 433 FILE *f = getfile(L, FIRSTARG);
432 luaL_arg_check(f || lua_getparam(FIRSTARG) == LUA_NOOBJECT, FIRSTARG, 434 luaL_arg_check(L, f || lua_getparam(L, FIRSTARG) == LUA_NOOBJECT, FIRSTARG,
433 "invalid file handle"); 435 "invalid file handle");
434 pushresult(fflush(f) == 0); 436 pushresult(L, fflush(f) == 0);
435} 437}
436 438
437/* }====================================================== */ 439/* }====================================================== */
@@ -443,102 +445,102 @@ static void io_flush (void) {
443** ======================================================= 445** =======================================================
444*/ 446*/
445 447
446static void io_execute (void) { 448static void io_execute (lua_State *L) {
447 lua_pushnumber(system(luaL_check_string(1))); 449 lua_pushnumber(L, system(luaL_check_string(L, 1)));
448} 450}
449 451
450 452
451static void io_remove (void) { 453static void io_remove (lua_State *L) {
452 pushresult(remove(luaL_check_string(1)) == 0); 454 pushresult(L, remove(luaL_check_string(L, 1)) == 0);
453} 455}
454 456
455 457
456static void io_rename (void) { 458static void io_rename (lua_State *L) {
457 pushresult(rename(luaL_check_string(1), 459 pushresult(L, rename(luaL_check_string(L, 1),
458 luaL_check_string(2)) == 0); 460 luaL_check_string(L, 2)) == 0);
459} 461}
460 462
461 463
462static void io_tmpname (void) { 464static void io_tmpname (lua_State *L) {
463 lua_pushstring(tmpnam(NULL)); 465 lua_pushstring(L, tmpnam(NULL));
464} 466}
465 467
466 468
467 469
468static void io_getenv (void) { 470static void io_getenv (lua_State *L) {
469 lua_pushstring(getenv(luaL_check_string(1))); /* if NULL push nil */ 471 lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
470} 472}
471 473
472 474
473static void io_clock (void) { 475static void io_clock (lua_State *L) {
474 lua_pushnumber(((double)clock())/CLOCKS_PER_SEC); 476 lua_pushnumber(L, ((double)clock())/CLOCKS_PER_SEC);
475} 477}
476 478
477 479
478static void io_date (void) { 480static void io_date (lua_State *L) {
479 char b[256]; 481 char b[256];
480 const char *s = luaL_opt_string(1, "%c"); 482 const char *s = luaL_opt_string(L, 1, "%c");
481 struct tm *tm; 483 struct tm *tm;
482 time_t t; 484 time_t t;
483 time(&t); tm = localtime(&t); 485 time(&t); tm = localtime(&t);
484 if (strftime(b,sizeof(b),s,tm)) 486 if (strftime(b,sizeof(b),s,tm))
485 lua_pushstring(b); 487 lua_pushstring(L, b);
486 else 488 else
487 lua_error("invalid `date' format"); 489 lua_error(L, "invalid `date' format");
488} 490}
489 491
490 492
491static void setloc (void) { 493static void setloc (lua_State *L) {
492 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 494 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
493 LC_NUMERIC, LC_TIME}; 495 LC_NUMERIC, LC_TIME};
494 static const char *const catnames[] = {"all", "collate", "ctype", "monetary", 496 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
495 "numeric", "time", NULL}; 497 "numeric", "time", NULL};
496 int op = luaL_findstring(luaL_opt_string(2, "all"), catnames); 498 int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
497 luaL_arg_check(op != -1, 2, "invalid option"); 499 luaL_arg_check(L, op != -1, 2, "invalid option");
498 lua_pushstring(setlocale(cat[op], luaL_check_string(1))); 500 lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
499} 501}
500 502
501 503
502static void io_exit (void) { 504static void io_exit (lua_State *L) {
503 exit(luaL_opt_int(1, EXIT_SUCCESS)); 505 exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
504} 506}
505 507
506/* }====================================================== */ 508/* }====================================================== */
507 509
508 510
509 511
510static void io_debug (void) { 512static void io_debug (lua_State *L) {
511 for (;;) { 513 for (;;) {
512 char buffer[250]; 514 char buffer[250];
513 fprintf(stderr, "lua_debug> "); 515 fprintf(stderr, "lua_debug> ");
514 if (fgets(buffer, sizeof(buffer), stdin) == 0 || 516 if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
515 strcmp(buffer, "cont\n") == 0) 517 strcmp(buffer, "cont\n") == 0)
516 return; 518 return;
517 lua_dostring(buffer); 519 lua_dostring(L, buffer);
518 } 520 }
519} 521}
520 522
521 523
522 524
523#define MESSAGESIZE 150 525#define MESSAGESIZE 150
524#define MAXMESSAGE (MESSAGESIZE*10) 526#define MAXMESSAGE (MESSAGESIZE*10)
525 527
526 528
527#define MAXSRC 60 529#define MAXSRC 60
528 530
529 531
530static void errorfb (void) { 532static void errorfb (lua_State *L) {
531 char buff[MAXMESSAGE]; 533 char buff[MAXMESSAGE];
532 int level = 1; /* skip level 0 (it's this function) */ 534 int level = 1; /* skip level 0 (it's this function) */
533 lua_Object func; 535 lua_Object func;
534 sprintf(buff, "lua error: %.200s\n", lua_getstring(lua_getparam(1))); 536 sprintf(buff, "lua error: %.200s\n", lua_getstring(L, lua_getparam(L, 1)));
535 while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) { 537 while ((func = lua_stackedfunction(L, level++)) != LUA_NOOBJECT) {
536 const char *name; 538 const char *name;
537 int currentline; 539 int currentline;
538 const char *chunkname; 540 const char *chunkname;
539 char buffchunk[MAXSRC]; 541 char buffchunk[MAXSRC];
540 int linedefined; 542 int linedefined;
541 lua_funcinfo(func, &chunkname, &linedefined); 543 lua_funcinfo(L, func, &chunkname, &linedefined);
542 luaL_chunkid(buffchunk, chunkname, sizeof(buffchunk)); 544 luaL_chunkid(buffchunk, chunkname, sizeof(buffchunk));
543 if (level == 2) strcat(buff, "Active Stack:\n"); 545 if (level == 2) strcat(buff, "Active Stack:\n");
544 strcat(buff, " "); 546 strcat(buff, " ");
@@ -546,7 +548,7 @@ static void errorfb (void) {
546 strcat(buff, "...\n"); 548 strcat(buff, "...\n");
547 break; /* buffer is full */ 549 break; /* buffer is full */
548 } 550 }
549 switch (*lua_getobjname(func, &name)) { 551 switch (*lua_getobjname(L, func, &name)) {
550 case 'g': 552 case 'g':
551 sprintf(buff+strlen(buff), "function `%.50s'", name); 553 sprintf(buff+strlen(buff), "function `%.50s'", name);
552 break; 554 break;
@@ -564,16 +566,16 @@ static void errorfb (void) {
564 chunkname = NULL; 566 chunkname = NULL;
565 } 567 }
566 } 568 }
567 if ((currentline = lua_currentline(func)) > 0) 569 if ((currentline = lua_currentline(L, func)) > 0)
568 sprintf(buff+strlen(buff), " at line %d", currentline); 570 sprintf(buff+strlen(buff), " at line %d", currentline);
569 if (chunkname) 571 if (chunkname)
570 sprintf(buff+strlen(buff), " [%.70s]", buffchunk); 572 sprintf(buff+strlen(buff), " [%.70s]", buffchunk);
571 strcat(buff, "\n"); 573 strcat(buff, "\n");
572 } 574 }
573 func = lua_rawgetglobal("_ALERT"); 575 func = lua_rawgetglobal(L, "_ALERT");
574 if (lua_isfunction(func)) { /* avoid error loop if _ALERT is not defined */ 576 if (lua_isfunction(L, func)) { /* avoid error loop if _ALERT is not defined */
575 lua_pushstring(buff); 577 lua_pushstring(L, buff);
576 lua_callfunction(func); 578 lua_callfunction(L, func);
577 } 579 }
578} 580}
579 581
@@ -607,31 +609,31 @@ static const struct luaL_reg iolibtag[] = {
607}; 609};
608 610
609 611
610static void openwithtags (void) { 612static void openwithtags (lua_State *L) {
611 unsigned int i; 613 unsigned int i;
612 int iotag = lua_newtag(); 614 int iotag = lua_newtag(L);
613 lua_newtag(); /* alloc CLOSEDTAG: assume that CLOSEDTAG = iotag-1 */ 615 lua_newtag(L); /* alloc CLOSEDTAG: assume that CLOSEDTAG = iotag-1 */
614 for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) { 616 for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
615 /* put iotag as upvalue for these functions */ 617 /* put iotag as upvalue for these functions */
616 lua_pushnumber(iotag); 618 lua_pushnumber(L, iotag);
617 lua_pushcclosure(iolibtag[i].func, 1); 619 lua_pushcclosure(L, iolibtag[i].func, 1);
618 lua_setglobal(iolibtag[i].name); 620 lua_setglobal(L, iolibtag[i].name);
619 } 621 }
620 /* predefined file handles */ 622 /* predefined file handles */
621 setfile(stdin, FINPUT, iotag); 623 setfile(L, stdin, FINPUT, iotag);
622 setfile(stdout, FOUTPUT, iotag); 624 setfile(L, stdout, FOUTPUT, iotag);
623 setfile(stdin, "_STDIN", iotag); 625 setfile(L, stdin, "_STDIN", iotag);
624 setfile(stdout, "_STDOUT", iotag); 626 setfile(L, stdout, "_STDOUT", iotag);
625 setfile(stderr, "_STDERR", iotag); 627 setfile(L, stderr, "_STDERR", iotag);
626 /* close file when collected */ 628 /* close file when collected */
627 lua_pushnumber(iotag); 629 lua_pushnumber(L, iotag);
628 lua_pushcclosure(gc_close, 1); 630 lua_pushcclosure(L, gc_close, 1);
629 lua_settagmethod(iotag, "gc"); 631 lua_settagmethod(L, iotag, "gc");
630} 632}
631 633
632void lua_iolibopen (void) { 634void lua_iolibopen (lua_State *L) {
633 /* register lib functions */ 635 /* register lib functions */
634 luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); 636 luaL_openlib(L, iolib, (sizeof(iolib)/sizeof(iolib[0])));
635 openwithtags(); 637 openwithtags(L);
636} 638}
637 639
diff --git a/llex.c b/llex.c
index ba97cd0d..f9264cf0 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.42 1999/10/19 13:33:22 roberto Exp roberto $ 2** $Id: llex.c,v 1.43 1999/11/09 17:59:35 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -8,6 +8,8 @@
8#include <ctype.h> 8#include <ctype.h>
9#include <string.h> 9#include <string.h>
10 10
11#define LUA_REENTRANT
12
11#include "lauxlib.h" 13#include "lauxlib.h"
12#include "llex.h" 14#include "llex.h"
13#include "lmem.h" 15#include "lmem.h"
@@ -23,8 +25,8 @@
23#define next(LS) (LS->current = zgetc(LS->lex_z)) 25#define next(LS) (LS->current = zgetc(LS->lex_z))
24 26
25 27
26#define save(c) luaL_addchar(c) 28#define save(L, c) luaL_addchar(L, c)
27#define save_and_next(LS) (save(LS->current), next(LS)) 29#define save_and_next(L, LS) (save(L, LS->current), next(LS))
28 30
29 31
30/* ORDER RESERVED */ 32/* ORDER RESERVED */
@@ -33,10 +35,10 @@ static const char *const reserved [] = {"and", "do", "else", "elseif", "end",
33 "until", "while"}; 35 "until", "while"};
34 36
35 37
36void luaX_init (void) { 38void luaX_init (lua_State *L) {
37 unsigned int i; 39 unsigned int i;
38 for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) { 40 for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
39 TaggedString *ts = luaS_new(reserved[i]); 41 TaggedString *ts = luaS_new(L, reserved[i]);
40 ts->marked = (unsigned char)(RESERVEDMARK+i); /* reserved word */ 42 ts->marked = (unsigned char)(RESERVEDMARK+i); /* reserved word */
41 } 43 }
42} 44}
@@ -49,14 +51,14 @@ void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
49 luaL_chunkid(buff, zname(ls->lex_z), sizeof(buff)); 51 luaL_chunkid(buff, zname(ls->lex_z), sizeof(buff));
50 if (token[0] == '\0') 52 if (token[0] == '\0')
51 token = "<eof>"; 53 token = "<eof>";
52 luaL_verror("%.100s;\n last token read: `%.50s' at line %d in %.80s", 54 luaL_verror(ls->L, "%.100s;\n last token read: `%.50s' at line %d in %.80s",
53 s, token, ls->linenumber, buff); 55 s, token, ls->linenumber, buff);
54} 56}
55 57
56 58
57void luaX_error (LexState *ls, const char *s) { 59void luaX_error (LexState *ls, const char *s) {
58 save('\0'); 60 save(ls->L, '\0');
59 luaX_syntaxerror(ls, s, luaL_buffer()); 61 luaX_syntaxerror(ls, s, luaL_buffer(ls->L));
60} 62}
61 63
62 64
@@ -86,8 +88,8 @@ static void firstline (LexState *LS)
86} 88}
87 89
88 90
89void luaX_setinput (LexState *LS, ZIO *z) 91void luaX_setinput (lua_State *L, LexState *LS, ZIO *z) {
90{ 92 LS->L = L;
91 LS->current = '\n'; 93 LS->current = '\n';
92 LS->linenumber = 0; 94 LS->linenumber = 0;
93 LS->iflevel = 0; 95 LS->iflevel = 0;
@@ -96,7 +98,7 @@ void luaX_setinput (LexState *LS, ZIO *z)
96 LS->lex_z = z; 98 LS->lex_z = z;
97 LS->fs = NULL; 99 LS->fs = NULL;
98 firstline(LS); 100 firstline(LS);
99 luaL_resetbuffer(); 101 luaL_resetbuffer(L);
100} 102}
101 103
102 104
@@ -117,12 +119,12 @@ static void skipspace (LexState *LS) {
117} 119}
118 120
119 121
120static int checkcond (LexState *LS, const char *buff) { 122static int checkcond (lua_State *L, LexState *LS, const char *buff) {
121 static const char *const opts[] = {"nil", "1", NULL}; 123 static const char *const opts[] = {"nil", "1", NULL};
122 int i = luaL_findstring(buff, opts); 124 int i = luaL_findstring(buff, opts);
123 if (i >= 0) return i; 125 if (i >= 0) return i;
124 else if (isalpha((unsigned char)buff[0]) || buff[0] == '_') 126 else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
125 return luaS_globaldefined(buff); 127 return luaS_globaldefined(L, buff);
126 else { 128 else {
127 luaX_syntaxerror(LS, "invalid $if condition", buff); 129 luaX_syntaxerror(LS, "invalid $if condition", buff);
128 return 0; /* to avoid warnings */ 130 return 0; /* to avoid warnings */
@@ -145,13 +147,13 @@ static void readname (LexState *LS, char *buff) {
145} 147}
146 148
147 149
148static void inclinenumber (LexState *LS); 150static void inclinenumber (lua_State *L, LexState *LS);
149 151
150 152
151static void ifskip (LexState *LS) { 153static void ifskip (lua_State *L, LexState *LS) {
152 while (LS->ifstate[LS->iflevel].skip) { 154 while (LS->ifstate[LS->iflevel].skip) {
153 if (LS->current == '\n') 155 if (LS->current == '\n')
154 inclinenumber(LS); 156 inclinenumber(L, LS);
155 else if (LS->current == EOZ) 157 else if (LS->current == EOZ)
156 luaX_error(LS, "input ends inside a $if"); 158 luaX_error(LS, "input ends inside a $if");
157 else next(LS); 159 else next(LS);
@@ -159,7 +161,7 @@ static void ifskip (LexState *LS) {
159} 161}
160 162
161 163
162static void inclinenumber (LexState *LS) { 164static void inclinenumber (lua_State *L, LexState *LS) {
163 static const char *const pragmas [] = 165 static const char *const pragmas [] =
164 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL}; 166 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
165 next(LS); /* skip '\n' */ 167 next(LS); /* skip '\n' */
@@ -196,7 +198,7 @@ static void inclinenumber (LexState *LS) {
196 readname(LS, buff); 198 readname(LS, buff);
197 LS->iflevel++; 199 LS->iflevel++;
198 LS->ifstate[LS->iflevel].elsepart = 0; 200 LS->ifstate[LS->iflevel].elsepart = 0;
199 LS->ifstate[LS->iflevel].condition = checkcond(LS, buff) ? !ifnot : ifnot; 201 LS->ifstate[LS->iflevel].condition = checkcond(L, LS, buff) ? !ifnot : ifnot;
200 LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition; 202 LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
201 break; 203 break;
202 case 6: /* else */ 204 case 6: /* else */
@@ -211,10 +213,10 @@ static void inclinenumber (LexState *LS) {
211 } 213 }
212 skipspace(LS); 214 skipspace(LS);
213 if (LS->current == '\n') /* pragma must end with a '\n' ... */ 215 if (LS->current == '\n') /* pragma must end with a '\n' ... */
214 inclinenumber(LS); 216 inclinenumber(L, LS);
215 else if (LS->current != EOZ) /* or eof */ 217 else if (LS->current != EOZ) /* or eof */
216 luaX_syntaxerror(LS, "invalid pragma format", buff); 218 luaX_syntaxerror(LS, "invalid pragma format", buff);
217 ifskip(LS); 219 ifskip(L, LS);
218 } 220 }
219} 221}
220 222
@@ -228,7 +230,7 @@ static void inclinenumber (LexState *LS) {
228 230
229 231
230 232
231static int read_long_string (LexState *LS) { 233static int read_long_string (lua_State *L, LexState *LS) {
232 int cont = 0; 234 int cont = 0;
233 for (;;) { 235 for (;;) {
234 switch (LS->current) { 236 switch (LS->current) {
@@ -236,37 +238,38 @@ static int read_long_string (LexState *LS) {
236 luaX_error(LS, "unfinished long string"); 238 luaX_error(LS, "unfinished long string");
237 return EOS; /* to avoid warnings */ 239 return EOS; /* to avoid warnings */
238 case '[': 240 case '[':
239 save_and_next(LS); 241 save_and_next(L, LS);
240 if (LS->current == '[') { 242 if (LS->current == '[') {
241 cont++; 243 cont++;
242 save_and_next(LS); 244 save_and_next(L, LS);
243 } 245 }
244 continue; 246 continue;
245 case ']': 247 case ']':
246 save_and_next(LS); 248 save_and_next(L, LS);
247 if (LS->current == ']') { 249 if (LS->current == ']') {
248 if (cont == 0) goto endloop; 250 if (cont == 0) goto endloop;
249 cont--; 251 cont--;
250 save_and_next(LS); 252 save_and_next(L, LS);
251 } 253 }
252 continue; 254 continue;
253 case '\n': 255 case '\n':
254 save('\n'); 256 save(L, '\n');
255 inclinenumber(LS); 257 inclinenumber(L, LS);
256 continue; 258 continue;
257 default: 259 default:
258 save_and_next(LS); 260 save_and_next(L, LS);
259 } 261 }
260 } endloop: 262 } endloop:
261 save_and_next(LS); /* skip the second ']' */ 263 save_and_next(L, LS); /* skip the second ']' */
262 LS->seminfo.ts = luaS_newlstr(L->Mbuffer+(L->Mbuffbase+2), 264 LS->seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+2),
263 L->Mbuffnext-L->Mbuffbase-4); 265 L->Mbuffnext-L->Mbuffbase-4);
264 return STRING; 266 return STRING;
265} 267}
266 268
267 269
268int luaX_lex (LexState *LS) { 270int luaX_lex (LexState *LS) {
269 luaL_resetbuffer(); 271 lua_State *L = LS->L;
272 luaL_resetbuffer(L);
270 for (;;) { 273 for (;;) {
271 switch (LS->current) { 274 switch (LS->current) {
272 275
@@ -275,48 +278,48 @@ int luaX_lex (LexState *LS) {
275 continue; 278 continue;
276 279
277 case '\n': 280 case '\n':
278 inclinenumber(LS); 281 inclinenumber(L, LS);
279 continue; 282 continue;
280 283
281 case '-': 284 case '-':
282 save_and_next(LS); 285 save_and_next(L, LS);
283 if (LS->current != '-') return '-'; 286 if (LS->current != '-') return '-';
284 do { next(LS); } while (LS->current != '\n' && LS->current != EOZ); 287 do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
285 luaL_resetbuffer(); 288 luaL_resetbuffer(L);
286 continue; 289 continue;
287 290
288 case '[': 291 case '[':
289 save_and_next(LS); 292 save_and_next(L, LS);
290 if (LS->current != '[') return '['; 293 if (LS->current != '[') return '[';
291 else { 294 else {
292 save_and_next(LS); /* pass the second '[' */ 295 save_and_next(L, LS); /* pass the second '[' */
293 return read_long_string(LS); 296 return read_long_string(L, LS);
294 } 297 }
295 298
296 case '=': 299 case '=':
297 save_and_next(LS); 300 save_and_next(L, LS);
298 if (LS->current != '=') return '='; 301 if (LS->current != '=') return '=';
299 else { save_and_next(LS); return EQ; } 302 else { save_and_next(L, LS); return EQ; }
300 303
301 case '<': 304 case '<':
302 save_and_next(LS); 305 save_and_next(L, LS);
303 if (LS->current != '=') return '<'; 306 if (LS->current != '=') return '<';
304 else { save_and_next(LS); return LE; } 307 else { save_and_next(L, LS); return LE; }
305 308
306 case '>': 309 case '>':
307 save_and_next(LS); 310 save_and_next(L, LS);
308 if (LS->current != '=') return '>'; 311 if (LS->current != '=') return '>';
309 else { save_and_next(LS); return GE; } 312 else { save_and_next(L, LS); return GE; }
310 313
311 case '~': 314 case '~':
312 save_and_next(LS); 315 save_and_next(L, LS);
313 if (LS->current != '=') return '~'; 316 if (LS->current != '=') return '~';
314 else { save_and_next(LS); return NE; } 317 else { save_and_next(L, LS); return NE; }
315 318
316 case '"': 319 case '"':
317 case '\'': { 320 case '\'': {
318 int del = LS->current; 321 int del = LS->current;
319 save_and_next(LS); 322 save_and_next(L, LS);
320 while (LS->current != del) { 323 while (LS->current != del) {
321 switch (LS->current) { 324 switch (LS->current) {
322 case EOZ: 325 case EOZ:
@@ -326,14 +329,14 @@ int luaX_lex (LexState *LS) {
326 case '\\': 329 case '\\':
327 next(LS); /* do not save the '\' */ 330 next(LS); /* do not save the '\' */
328 switch (LS->current) { 331 switch (LS->current) {
329 case 'a': save('\a'); next(LS); break; 332 case 'a': save(L, '\a'); next(LS); break;
330 case 'b': save('\b'); next(LS); break; 333 case 'b': save(L, '\b'); next(LS); break;
331 case 'f': save('\f'); next(LS); break; 334 case 'f': save(L, '\f'); next(LS); break;
332 case 'n': save('\n'); next(LS); break; 335 case 'n': save(L, '\n'); next(LS); break;
333 case 'r': save('\r'); next(LS); break; 336 case 'r': save(L, '\r'); next(LS); break;
334 case 't': save('\t'); next(LS); break; 337 case 't': save(L, '\t'); next(LS); break;
335 case 'v': save('\v'); next(LS); break; 338 case 'v': save(L, '\v'); next(LS); break;
336 case '\n': save('\n'); inclinenumber(LS); break; 339 case '\n': save(L, '\n'); inclinenumber(L, LS); break;
337 default : { 340 default : {
338 if (isdigit(LS->current)) { 341 if (isdigit(LS->current)) {
339 int c = 0; 342 int c = 0;
@@ -344,10 +347,10 @@ int luaX_lex (LexState *LS) {
344 } while (++i<3 && isdigit(LS->current)); 347 } while (++i<3 && isdigit(LS->current));
345 if (c != (unsigned char)c) 348 if (c != (unsigned char)c)
346 luaX_error(LS, "escape sequence too large"); 349 luaX_error(LS, "escape sequence too large");
347 save(c); 350 save(L, c);
348 } 351 }
349 else { /* handles \, ", ', and ? */ 352 else { /* handles \, ", ', and ? */
350 save(LS->current); 353 save(L, LS->current);
351 next(LS); 354 next(LS);
352 } 355 }
353 break; 356 break;
@@ -355,23 +358,23 @@ int luaX_lex (LexState *LS) {
355 } 358 }
356 break; 359 break;
357 default: 360 default:
358 save_and_next(LS); 361 save_and_next(L, LS);
359 } 362 }
360 } 363 }
361 save_and_next(LS); /* skip delimiter */ 364 save_and_next(L, LS); /* skip delimiter */
362 LS->seminfo.ts = luaS_newlstr(L->Mbuffer+(L->Mbuffbase+1), 365 LS->seminfo.ts = luaS_newlstr(L, L->Mbuffer+(L->Mbuffbase+1),
363 L->Mbuffnext-L->Mbuffbase-2); 366 L->Mbuffnext-L->Mbuffbase-2);
364 return STRING; 367 return STRING;
365 } 368 }
366 369
367 case '.': 370 case '.':
368 save_and_next(LS); 371 save_and_next(L, LS);
369 if (LS->current == '.') 372 if (LS->current == '.')
370 { 373 {
371 save_and_next(LS); 374 save_and_next(L, LS);
372 if (LS->current == '.') 375 if (LS->current == '.')
373 { 376 {
374 save_and_next(LS); 377 save_and_next(L, LS);
375 return DOTS; /* ... */ 378 return DOTS; /* ... */
376 } 379 }
377 else return CONC; /* .. */ 380 else return CONC; /* .. */
@@ -382,26 +385,26 @@ int luaX_lex (LexState *LS) {
382 case '0': case '1': case '2': case '3': case '4': 385 case '0': case '1': case '2': case '3': case '4':
383 case '5': case '6': case '7': case '8': case '9': 386 case '5': case '6': case '7': case '8': case '9':
384 do { 387 do {
385 save_and_next(LS); 388 save_and_next(L, LS);
386 } while (isdigit(LS->current)); 389 } while (isdigit(LS->current));
387 if (LS->current == '.') { 390 if (LS->current == '.') {
388 save_and_next(LS); 391 save_and_next(L, LS);
389 if (LS->current == '.') { 392 if (LS->current == '.') {
390 save('.'); 393 save(L, '.');
391 luaX_error(LS, 394 luaX_error(LS,
392 "ambiguous syntax (decimal point x string concatenation)"); 395 "ambiguous syntax (decimal point x string concatenation)");
393 } 396 }
394 } 397 }
395 fraction: /* LUA_NUMBER */ 398 fraction: /* LUA_NUMBER */
396 while (isdigit(LS->current)) 399 while (isdigit(LS->current))
397 save_and_next(LS); 400 save_and_next(L, LS);
398 if (toupper(LS->current) == 'E') { 401 if (toupper(LS->current) == 'E') {
399 save_and_next(LS); /* read 'E' */ 402 save_and_next(L, LS); /* read 'E' */
400 save_and_next(LS); /* read '+', '-' or first digit */ 403 save_and_next(L, LS); /* read '+', '-' or first digit */
401 while (isdigit(LS->current)) 404 while (isdigit(LS->current))
402 save_and_next(LS); 405 save_and_next(L, LS);
403 } 406 }
404 save('\0'); 407 save(L, '\0');
405 if (!luaO_str2d(L->Mbuffer+L->Mbuffbase, &LS->seminfo.r)) 408 if (!luaO_str2d(L->Mbuffer+L->Mbuffbase, &LS->seminfo.r))
406 luaX_error(LS, "invalid numeric format"); 409 luaX_error(LS, "invalid numeric format");
407 return NUMBER; 410 return NUMBER;
@@ -416,16 +419,16 @@ int luaX_lex (LexState *LS) {
416 int c = LS->current; 419 int c = LS->current;
417 if (iscntrl(c)) 420 if (iscntrl(c))
418 luaX_invalidchar(LS, c); 421 luaX_invalidchar(LS, c);
419 save_and_next(LS); 422 save_and_next(L, LS);
420 return c; 423 return c;
421 } 424 }
422 else { /* identifier or reserved word */ 425 else { /* identifier or reserved word */
423 TaggedString *ts; 426 TaggedString *ts;
424 do { 427 do {
425 save_and_next(LS); 428 save_and_next(L, LS);
426 } while (isalnum(LS->current) || LS->current == '_'); 429 } while (isalnum(LS->current) || LS->current == '_');
427 save('\0'); 430 save(L, '\0');
428 ts = luaS_new(L->Mbuffer+L->Mbuffbase); 431 ts = luaS_new(L, L->Mbuffer+L->Mbuffbase);
429 if (ts->marked >= RESERVEDMARK) /* reserved word? */ 432 if (ts->marked >= RESERVEDMARK) /* reserved word? */
430 return ts->marked-RESERVEDMARK+FIRST_RESERVED; 433 return ts->marked-RESERVEDMARK+FIRST_RESERVED;
431 LS->seminfo.ts = ts; 434 LS->seminfo.ts = ts;
diff --git a/llex.h b/llex.h
index 20928075..fbdc875d 100644
--- a/llex.h
+++ b/llex.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.h,v 1.13 1999/07/22 19:29:42 roberto Exp roberto $ 2** $Id: llex.h,v 1.14 1999/08/16 20:52:00 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -47,6 +47,7 @@ typedef struct LexState {
47 int current; /* look ahead character */ 47 int current; /* look ahead character */
48 int token; /* look ahead token */ 48 int token; /* look ahead token */
49 struct FuncState *fs; /* 'FuncState' is private for the parser */ 49 struct FuncState *fs; /* 'FuncState' is private for the parser */
50 struct lua_State *L;
50 union { 51 union {
51 real r; 52 real r;
52 TaggedString *ts; 53 TaggedString *ts;
@@ -58,8 +59,8 @@ typedef struct LexState {
58} LexState; 59} LexState;
59 60
60 61
61void luaX_init (void); 62void luaX_init (lua_State *L);
62void luaX_setinput (LexState *LS, ZIO *z); 63void luaX_setinput (lua_State *L, LexState *LS, ZIO *z);
63int luaX_lex (LexState *LS); 64int luaX_lex (LexState *LS);
64void luaX_syntaxerror (LexState *ls, const char *s, const char *token); 65void luaX_syntaxerror (LexState *ls, const char *s, const char *token);
65void luaX_error (LexState *ls, const char *s); 66void luaX_error (LexState *ls, const char *s);
diff --git a/lmathlib.c b/lmathlib.c
index b6eb16fb..ee688a50 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.18 1999/08/16 20:52:00 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.19 1999/08/18 14:40:51 roberto Exp roberto $
3** Lua standard mathematical library 3** Lua standard mathematical library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -8,14 +8,16 @@
8#include <stdlib.h> 8#include <stdlib.h>
9#include <math.h> 9#include <math.h>
10 10
11#define LUA_REENTRANT
12
11#include "lauxlib.h" 13#include "lauxlib.h"
12#include "lua.h" 14#include "lua.h"
13#include "lualib.h" 15#include "lualib.h"
14 16
15 17
16#undef PI 18#undef PI
17#define PI (3.14159265358979323846) 19#define PI (3.14159265358979323846)
18#define RADIANS_PER_DEGREE (PI/180.0) 20#define RADIANS_PER_DEGREE (PI/180.0)
19 21
20 22
21 23
@@ -32,138 +34,138 @@
32#endif 34#endif
33 35
34 36
35static void math_abs (void) { 37static void math_abs (lua_State *L) {
36 lua_pushnumber(fabs(luaL_check_number(1))); 38 lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
37} 39}
38 40
39static void math_sin (void) { 41static void math_sin (lua_State *L) {
40 lua_pushnumber(sin(TORAD(luaL_check_number(1)))); 42 lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
41} 43}
42 44
43static void math_cos (void) { 45static void math_cos (lua_State *L) {
44 lua_pushnumber(cos(TORAD(luaL_check_number(1)))); 46 lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
45} 47}
46 48
47static void math_tan (void) { 49static void math_tan (lua_State *L) {
48 lua_pushnumber(tan(TORAD(luaL_check_number(1)))); 50 lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
49} 51}
50 52
51static void math_asin (void) { 53static void math_asin (lua_State *L) {
52 lua_pushnumber(FROMRAD(asin(luaL_check_number(1)))); 54 lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
53} 55}
54 56
55static void math_acos (void) { 57static void math_acos (lua_State *L) {
56 lua_pushnumber(FROMRAD(acos(luaL_check_number(1)))); 58 lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
57} 59}
58 60
59static void math_atan (void) { 61static void math_atan (lua_State *L) {
60 lua_pushnumber(FROMRAD(atan(luaL_check_number(1)))); 62 lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
61} 63}
62 64
63static void math_atan2 (void) { 65static void math_atan2 (lua_State *L) {
64 lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2)))); 66 lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
65} 67}
66 68
67static void math_ceil (void) { 69static void math_ceil (lua_State *L) {
68 lua_pushnumber(ceil(luaL_check_number(1))); 70 lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
69} 71}
70 72
71static void math_floor (void) { 73static void math_floor (lua_State *L) {
72 lua_pushnumber(floor(luaL_check_number(1))); 74 lua_pushnumber(L, floor(luaL_check_number(L, 1)));
73} 75}
74 76
75static void math_mod (void) { 77static void math_mod (lua_State *L) {
76 lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2))); 78 lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
77} 79}
78 80
79static void math_sqrt (void) { 81static void math_sqrt (lua_State *L) {
80 lua_pushnumber(sqrt(luaL_check_number(1))); 82 lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
81} 83}
82 84
83static void math_pow (void) { 85static void math_pow (lua_State *L) {
84 lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2))); 86 lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
85} 87}
86 88
87static void math_log (void) { 89static void math_log (lua_State *L) {
88 lua_pushnumber(log(luaL_check_number(1))); 90 lua_pushnumber(L, log(luaL_check_number(L, 1)));
89} 91}
90 92
91static void math_log10 (void) { 93static void math_log10 (lua_State *L) {
92 lua_pushnumber(log10(luaL_check_number(1))); 94 lua_pushnumber(L, log10(luaL_check_number(L, 1)));
93} 95}
94 96
95static void math_exp (void) { 97static void math_exp (lua_State *L) {
96 lua_pushnumber(exp(luaL_check_number(1))); 98 lua_pushnumber(L, exp(luaL_check_number(L, 1)));
97} 99}
98 100
99static void math_deg (void) { 101static void math_deg (lua_State *L) {
100 lua_pushnumber(luaL_check_number(1)/RADIANS_PER_DEGREE); 102 lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
101} 103}
102 104
103static void math_rad (void) { 105static void math_rad (lua_State *L) {
104 lua_pushnumber(luaL_check_number(1)*RADIANS_PER_DEGREE); 106 lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
105} 107}
106 108
107static void math_frexp (void) { 109static void math_frexp (lua_State *L) {
108 int e; 110 int e;
109 lua_pushnumber(frexp(luaL_check_number(1), &e)); 111 lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e));
110 lua_pushnumber(e); 112 lua_pushnumber(L, e);
111} 113}
112 114
113static void math_ldexp (void) { 115static void math_ldexp (lua_State *L) {
114 lua_pushnumber(ldexp(luaL_check_number(1), luaL_check_int(2))); 116 lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
115} 117}
116 118
117 119
118 120
119static void math_min (void) { 121static void math_min (lua_State *L) {
120 int i = 1; 122 int i = 1;
121 double dmin = luaL_check_number(i); 123 double dmin = luaL_check_number(L, i);
122 while (lua_getparam(++i) != LUA_NOOBJECT) { 124 while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
123 double d = luaL_check_number(i); 125 double d = luaL_check_number(L, i);
124 if (d < dmin) 126 if (d < dmin)
125 dmin = d; 127 dmin = d;
126 } 128 }
127 lua_pushnumber(dmin); 129 lua_pushnumber(L, dmin);
128} 130}
129 131
130 132
131static void math_max (void) { 133static void math_max (lua_State *L) {
132 int i = 1; 134 int i = 1;
133 double dmax = luaL_check_number(i); 135 double dmax = luaL_check_number(L, i);
134 while (lua_getparam(++i) != LUA_NOOBJECT) { 136 while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
135 double d = luaL_check_number(i); 137 double d = luaL_check_number(L, i);
136 if (d > dmax) 138 if (d > dmax)
137 dmax = d; 139 dmax = d;
138 } 140 }
139 lua_pushnumber(dmax); 141 lua_pushnumber(L, dmax);
140} 142}
141 143
142 144
143static void math_random (void) { 145static void math_random (lua_State *L) {
144 /* the '%' avoids the (rare) case of r==1, and is needed also because on 146 /* the '%' avoids the (rare) case of r==1, and is needed also because on
145 some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */ 147 some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */
146 double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; 148 double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
147 if (lua_getparam(1) == LUA_NOOBJECT) /* no arguments? */ 149 if (lua_getparam(L, 1) == LUA_NOOBJECT) /* no arguments? */
148 lua_pushnumber(r); /* real between 0 & 1 */ 150 lua_pushnumber(L, r); /* real between 0 & 1 */
149 else { 151 else {
150 int l, u; /* lower & upper limits */ 152 int l, u; /* lower & upper limits */
151 if (lua_getparam(2) == LUA_NOOBJECT) { /* only one argument? */ 153 if (lua_getparam(L, 2) == LUA_NOOBJECT) { /* only one argument? */
152 l = 1; 154 l = 1;
153 u = luaL_check_int(1); 155 u = luaL_check_int(L, 1);
154 } 156 }
155 else { /* two arguments */ 157 else { /* two arguments */
156 l = luaL_check_int(1); 158 l = luaL_check_int(L, 1);
157 u = luaL_check_int(2); 159 u = luaL_check_int(L, 2);
158 } 160 }
159 luaL_arg_check(l<=u, 1, "interval is empty"); 161 luaL_arg_check(L, l<=u, 1, "interval is empty");
160 lua_pushnumber((int)(r*(u-l+1))+l); /* integer between l & u */ 162 lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between l & u */
161 } 163 }
162} 164}
163 165
164 166
165static void math_randomseed (void) { 167static void math_randomseed (lua_State *L) {
166 srand(luaL_check_int(1)); 168 srand(luaL_check_int(L, 1));
167} 169}
168 170
169 171
@@ -196,11 +198,11 @@ static const struct luaL_reg mathlib[] = {
196/* 198/*
197** Open math library 199** Open math library
198*/ 200*/
199void lua_mathlibopen (void) { 201void lua_mathlibopen (lua_State *L) {
200 luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); 202 luaL_openlib(L, mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
201 lua_pushcfunction(math_pow); 203 lua_pushcfunction(L, math_pow);
202 lua_pushnumber(0); /* to get its tag */ 204 lua_pushnumber(L, 0); /* to get its tag */
203 lua_settagmethod(lua_tag(lua_pop()), "pow"); 205 lua_settagmethod(L, lua_tag(L, lua_pop(L)), "pow");
204 lua_pushnumber(PI); lua_setglobal("PI"); 206 lua_pushnumber(L, PI); lua_setglobal(L, "PI");
205} 207}
206 208
diff --git a/lmem.c b/lmem.c
index 3f309ed4..cfdf7ac6 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.18 1999/08/16 20:52:00 roberto Exp roberto $ 2** $Id: lmem.c,v 1.19 1999/10/19 13:33:22 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*/
@@ -7,6 +7,8 @@
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9 9
10#define LUA_REENTRANT
11
10#include "lmem.h" 12#include "lmem.h"
11#include "lstate.h" 13#include "lstate.h"
12#include "lua.h" 14#include "lua.h"
@@ -34,15 +36,15 @@ static unsigned long power2 (unsigned long n) {
34} 36}
35 37
36 38
37void *luaM_growaux (void *block, unsigned long nelems, int inc, int size, 39void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, int inc, int size,
38 const char *errormsg, unsigned long limit) { 40 const char *errormsg, unsigned long limit) {
39 unsigned long newn = nelems+inc; 41 unsigned long newn = nelems+inc;
40 if (newn >= limit) lua_error(errormsg); 42 if (newn >= limit) lua_error(L, errormsg);
41 if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */ 43 if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */
42 (nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */ 44 (nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */
43 return block; /* do not need to reallocate */ 45 return block; /* do not need to reallocate */
44 else /* it crossed a power of 2 boundary; grow to next power */ 46 else /* it crossed a power of 2 boundary; grow to next power */
45 return luaM_realloc(block, power2(newn)*size); 47 return luaM_realloc(L, block, power2(newn)*size);
46} 48}
47 49
48 50
@@ -51,17 +53,17 @@ void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
51/* 53/*
52** generic allocation routine. 54** generic allocation routine.
53*/ 55*/
54void *luaM_realloc (void *block, unsigned long size) { 56void *luaM_realloc (lua_State *L, void *block, unsigned long size) {
55 size_t s = (size_t)size; 57 size_t s = (size_t)size;
56 if (s != size) 58 if (s != size)
57 lua_error("memory allocation error: block too big"); 59 lua_error(L, "memory allocation error: block too big");
58 if (size == 0) { 60 if (size == 0) {
59 free(block); /* block may be NULL, that is OK for free */ 61 free(block); /* block may be NULL, that is OK for free */
60 return NULL; 62 return NULL;
61 } 63 }
62 block = realloc(block, s); 64 block = realloc(block, s);
63 if (block == NULL) 65 if (block == NULL)
64 lua_error(memEM); 66 lua_error(L, memEM);
65 return block; 67 return block;
66} 68}
67 69
@@ -90,7 +92,7 @@ static void *checkblock (void *block) {
90 unsigned long size = *b; 92 unsigned long size = *b;
91 int i; 93 int i;
92 for (i=0;i<MARKSIZE;i++) 94 for (i=0;i<MARKSIZE;i++)
93 LUA_ASSERT(*(((char *)b)+HEADER+size+i) == MARK+i, "corrupted block"); 95 LUA_ASSERT(L, *(((char *)b)+HEADER+size+i) == MARK+i, "corrupted block");
94 numblocks--; 96 numblocks--;
95 totalmem -= size; 97 totalmem -= size;
96 return b; 98 return b;
@@ -106,10 +108,10 @@ static void freeblock (void *block) {
106} 108}
107 109
108 110
109void *luaM_realloc (void *block, unsigned long size) { 111void *luaM_realloc (lua_State *L, void *block, unsigned long size) {
110 unsigned long realsize = HEADER+size+MARKSIZE; 112 unsigned long realsize = HEADER+size+MARKSIZE;
111 if (realsize != (size_t)realsize) 113 if (realsize != (size_t)realsize)
112 lua_error("memory allocation error: block too big"); 114 lua_error(L, "memory allocation error: block too big");
113 if (size == 0) { 115 if (size == 0) {
114 freeblock(block); 116 freeblock(block);
115 return NULL; 117 return NULL;
@@ -124,7 +126,7 @@ void *luaM_realloc (void *block, unsigned long size) {
124 freeblock(block); /* erase (and check) old copy */ 126 freeblock(block); /* erase (and check) old copy */
125 } 127 }
126 if (newblock == NULL) 128 if (newblock == NULL)
127 lua_error(memEM); 129 lua_error(L, memEM);
128 totalmem += size; 130 totalmem += size;
129 numblocks++; 131 numblocks++;
130 *(unsigned long *)newblock = size; 132 *(unsigned long *)newblock = size;
diff --git a/lmem.h b/lmem.h
index ee810ab9..3d292885 100644
--- a/lmem.h
+++ b/lmem.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.h,v 1.8 1999/02/26 15:48:55 roberto Exp roberto $ 2** $Id: lmem.h,v 1.9 1999/08/16 20:52:00 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*/
@@ -10,6 +10,8 @@
10 10
11#include <stdlib.h> 11#include <stdlib.h>
12 12
13#include "lua.h"
14
13/* memory error messages */ 15/* memory error messages */
14#define codeEM "code size overflow" 16#define codeEM "code size overflow"
15#define constantEM "constant table overflow" 17#define constantEM "constant table overflow"
@@ -18,17 +20,17 @@
18#define memEM "not enough memory" 20#define memEM "not enough memory"
19#define arrEM "internal array bigger than `int' limit" 21#define arrEM "internal array bigger than `int' limit"
20 22
21void *luaM_realloc (void *oldblock, unsigned long size); 23void *luaM_realloc (lua_State *L, void *oldblock, unsigned long size);
22void *luaM_growaux (void *block, unsigned long nelems, int inc, int size, 24void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, int inc, int size,
23 const char *errormsg, unsigned long limit); 25 const char *errormsg, unsigned long limit);
24 26
25#define luaM_free(b) luaM_realloc((b), 0) 27#define luaM_free(L, b) luaM_realloc(L, (b), 0)
26#define luaM_malloc(t) luaM_realloc(NULL, (t)) 28#define luaM_malloc(L, t) luaM_realloc(L, NULL, (t))
27#define luaM_new(t) ((t *)luaM_malloc(sizeof(t))) 29#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t)))
28#define luaM_newvector(n,t) ((t *)luaM_malloc((n)*sizeof(t))) 30#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*sizeof(t)))
29#define luaM_growvector(v,nelems,inc,t,e,l) \ 31#define luaM_growvector(L, v,nelems,inc,t,e,l) \
30 ((v)=(t *)luaM_growaux(v,nelems,inc,sizeof(t),e,l)) 32 ((v)=(t *)luaM_growaux(L, v,nelems,inc,sizeof(t),e,l))
31#define luaM_reallocvector(v,n,t) ((v)=(t *)luaM_realloc(v,(n)*sizeof(t))) 33#define luaM_reallocvector(L, v,n,t) ((v)=(t *)luaM_realloc(L, v,(n)*sizeof(t)))
32 34
33 35
34#ifdef DEBUG 36#ifdef DEBUG
diff --git a/lobject.c b/lobject.c
index 2c87b7d0..1e9f99d3 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.23 1999/09/08 20:45:18 roberto Exp roberto $ 2** $Id: lobject.c,v 1.24 1999/10/04 17:51:04 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*/
@@ -7,6 +7,8 @@
7#include <ctype.h> 7#include <ctype.h>
8#include <stdlib.h> 8#include <stdlib.h>
9 9
10#define LUA_REENTRANT
11
10#include "lobject.h" 12#include "lobject.h"
11#include "lua.h" 13#include "lua.h"
12 14
@@ -28,13 +30,13 @@ static const long dimensions[] =
28 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT}; 30 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
29 31
30 32
31int luaO_redimension (int oldsize) { 33int luaO_redimension (lua_State *L, int oldsize) {
32 int i; 34 int i;
33 for (i=0; dimensions[i]<MAX_INT; i++) { 35 for (i=0; dimensions[i]<MAX_INT; i++) {
34 if (dimensions[i] > oldsize) 36 if (dimensions[i] > oldsize)
35 return dimensions[i]; 37 return dimensions[i];
36 } 38 }
37 lua_error("tableEM"); 39 lua_error(L, "tableEM");
38 return 0; /* to avoid warnings */ 40 return 0; /* to avoid warnings */
39} 41}
40 42
@@ -49,7 +51,7 @@ int luaO_equalval (const TObject *t1, const TObject *t2) {
49 case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2); 51 case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2);
50 case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl; 52 case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl;
51 default: 53 default:
52 LUA_INTERNALERROR("invalid type"); 54 LUA_INTERNALERROR(L, "invalid type");
53 return 0; /* UNREACHABLE */ 55 return 0; /* UNREACHABLE */
54 } 56 }
55} 57}
diff --git a/lobject.h b/lobject.h
index 9956d5cf..ea397620 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.35 1999/11/04 17:22:26 roberto Exp roberto $ 2** $Id: lobject.h,v 1.36 1999/11/10 15:39:35 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -18,11 +18,11 @@
18#undef NDEBUG 18#undef NDEBUG
19#endif 19#endif
20#include <assert.h> 20#include <assert.h>
21#define LUA_INTERNALERROR(s) assert(0) 21#define LUA_INTERNALERROR(L,s) assert(0)
22#define LUA_ASSERT(c,s) assert(c) 22#define LUA_ASSERT(L,c,s) assert(c)
23#else 23#else
24#define LUA_INTERNALERROR(s) /* empty */ 24#define LUA_INTERNALERROR(L,s) /* empty */
25#define LUA_ASSERT(c,s) /* empty */ 25#define LUA_ASSERT(L,c,s) /* empty */
26#endif 26#endif
27 27
28 28
@@ -41,11 +41,11 @@ typedef LUA_NUM_TYPE real;
41typedef unsigned char Byte; /* unsigned 8 bits */ 41typedef unsigned char Byte; /* unsigned 8 bits */
42 42
43 43
44#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 44#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
45 45
46 46
47/* convertion of pointer to int (for hashing only) */ 47/* convertion of pointer to int (for hashing only) */
48#define IntPoint(p) ((unsigned int)(p)) 48#define IntPoint(L, p) ((unsigned int)(p))
49 49
50 50
51/* 51/*
@@ -53,7 +53,7 @@ typedef unsigned char Byte; /* unsigned 8 bits */
53** objects count 1, and each 32 bytes of `raw' memory count 1; we add 53** objects count 1, and each 32 bytes of `raw' memory count 1; we add
54** 2 to the total as a minimum (and also to count the overhead of malloc) 54** 2 to the total as a minimum (and also to count the overhead of malloc)
55*/ 55*/
56#define numblocks(o,b) ((o)+(b)/32+2) 56#define numblocks(L, o,b) ((o)+(b)/32+2)
57 57
58 58
59/* 59/*
@@ -195,7 +195,7 @@ typedef struct Hash {
195extern const char *const luaO_typenames[]; 195extern const char *const luaO_typenames[];
196 196
197 197
198#define luaO_typename(o) luaO_typenames[-ttype(o)] 198#define luaO_typename(L, o) luaO_typenames[-ttype(o)]
199 199
200 200
201extern const TObject luaO_nilobject; 201extern const TObject luaO_nilobject;
@@ -204,7 +204,7 @@ extern const TObject luaO_nilobject;
204#define luaO_equalObj(t1,t2) ((ttype(t1) != ttype(t2)) ? 0 \ 204#define luaO_equalObj(t1,t2) ((ttype(t1) != ttype(t2)) ? 0 \
205 : luaO_equalval(t1,t2)) 205 : luaO_equalval(t1,t2))
206int luaO_equalval (const TObject *t1, const TObject *t2); 206int luaO_equalval (const TObject *t1, const TObject *t2);
207int luaO_redimension (int oldsize); 207int luaO_redimension (lua_State *L, int oldsize);
208int luaO_str2d (const char *s, real *result); 208int luaO_str2d (const char *s, real *result);
209 209
210#ifdef OLD_ANSI 210#ifdef OLD_ANSI
diff --git a/lparser.c b/lparser.c
index 740ab03a..c96ea0d0 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.41 1999/09/20 14:15:18 roberto Exp roberto $ 2** $Id: lparser.c,v 1.42 1999/11/04 17:23:12 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -147,14 +147,15 @@ static void checklimit (LexState *ls, int val, int limit, const char *msg) {
147} 147}
148 148
149 149
150static void check_pc (FuncState *fs, int n) { 150static void check_pc (LexState *ls, int n) {
151 luaM_growvector(fs->f->code, fs->pc, n, Byte, codeEM, MAX_INT); 151 luaM_growvector(ls->L, ls->fs->f->code, ls->fs->pc, n,
152 Byte, codeEM, MAX_INT);
152} 153}
153 154
154 155
155static void code_byte (FuncState *fs, Byte c) { 156static void code_byte (LexState *ls, Byte c) {
156 check_pc(fs, 1); 157 check_pc(ls, 1);
157 fs->f->code[fs->pc++] = c; 158 ls->fs->f->code[ls->fs->pc++] = c;
158} 159}
159 160
160 161
@@ -204,7 +205,7 @@ static int fix_opcode (LexState *ls, int pc, OpCode op, int arg) {
204 if (tomove > 0) { /* need to open space? */ 205 if (tomove > 0) { /* need to open space? */
205 FuncState *fs = ls->fs; 206 FuncState *fs = ls->fs;
206 TProtoFunc *f = fs->f; 207 TProtoFunc *f = fs->f;
207 check_pc(fs, tomove); 208 check_pc(ls, tomove);
208 luaO_memup(f->code+pc+tomove, f->code+pc, fs->pc-pc); 209 luaO_memup(f->code+pc+tomove, f->code+pc, fs->pc-pc);
209 fs->pc += tomove; 210 fs->pc += tomove;
210 } 211 }
@@ -215,7 +216,7 @@ static int fix_opcode (LexState *ls, int pc, OpCode op, int arg) {
215 216
216static void code_oparg (LexState *ls, OpCode op, int arg, int delta) { 217static void code_oparg (LexState *ls, OpCode op, int arg, int delta) {
217 int size = codesize(arg); 218 int size = codesize(arg);
218 check_pc(ls->fs, size); 219 check_pc(ls, size);
219 code_oparg_at(ls, ls->fs->pc, op, arg, delta); 220 code_oparg_at(ls, ls->fs->pc, op, arg, delta);
220 ls->fs->pc += size; 221 ls->fs->pc += size;
221} 222}
@@ -223,7 +224,7 @@ static void code_oparg (LexState *ls, OpCode op, int arg, int delta) {
223 224
224static void code_opcode (LexState *ls, OpCode op, int delta) { 225static void code_opcode (LexState *ls, OpCode op, int delta) {
225 deltastack(ls, delta); 226 deltastack(ls, delta);
226 code_byte(ls->fs, (Byte)op); 227 code_byte(ls, (Byte)op);
227} 228}
228 229
229 230
@@ -234,24 +235,24 @@ static void code_constant (LexState *ls, int c) {
234 235
235static void assertglobal (LexState *ls, int index) { 236static void assertglobal (LexState *ls, int index) {
236 TObject *o = &ls->fs->f->consts[index]; 237 TObject *o = &ls->fs->f->consts[index];
237 LUA_ASSERT(ttype(o) == LUA_T_STRING, "global name is not a string"); 238 LUA_ASSERT(ls->L, ttype(o) == LUA_T_STRING, "global name is not a string");
238 luaS_assertglobal(tsvalue(o)); 239 luaS_assertglobal(ls->L, tsvalue(o));
239} 240}
240 241
241 242
242static int next_constant (FuncState *fs) { 243static int next_constant (LexState *ls, TProtoFunc *f) {
243 TProtoFunc *f = fs->f; 244 luaM_growvector(ls->L, f->consts, f->nconsts, 1,
244 luaM_growvector(f->consts, f->nconsts, 1, TObject, constantEM, MAX_ARG); 245 TObject, constantEM, MAX_ARG);
245 return f->nconsts++; 246 return f->nconsts++;
246} 247}
247 248
248 249
249static int string_constant (FuncState *fs, TaggedString *s) { 250static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) {
250 TProtoFunc *f = fs->f; 251 TProtoFunc *f = fs->f;
251 int c = s->constindex; 252 int c = s->constindex;
252 if (!(c < f->nconsts && 253 if (!(c < f->nconsts &&
253 ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { 254 ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
254 c = next_constant(fs); 255 c = next_constant(ls, f);
255 ttype(&f->consts[c]) = LUA_T_STRING; 256 ttype(&f->consts[c]) = LUA_T_STRING;
256 tsvalue(&f->consts[c]) = s; 257 tsvalue(&f->consts[c]) = s;
257 s->constindex = c; /* hint for next time */ 258 s->constindex = c; /* hint for next time */
@@ -261,23 +262,24 @@ static int string_constant (FuncState *fs, TaggedString *s) {
261 262
262 263
263static void code_string (LexState *ls, TaggedString *s) { 264static void code_string (LexState *ls, TaggedString *s) {
264 code_constant(ls, string_constant(ls->fs, s)); 265 code_constant(ls, string_constant(ls, ls->fs, s));
265} 266}
266 267
267 268
268#define LIM 20 269#define LIM 20
269static int real_constant (FuncState *fs, real r) { 270static int real_constant (LexState *ls, real r) {
270 /* check whether 'r' has appeared within the last LIM entries */ 271 /* check whether 'r' has appeared within the last LIM entries */
271 TObject *cnt = fs->f->consts; 272 TProtoFunc *f = ls->fs->f;
272 int c = fs->f->nconsts; 273 TObject *cnt = f->consts;
274 int c = f->nconsts;
273 int lim = c < LIM ? 0 : c-LIM; 275 int lim = c < LIM ? 0 : c-LIM;
274 while (--c >= lim) { 276 while (--c >= lim) {
275 if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r) 277 if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
276 return c; 278 return c;
277 } 279 }
278 /* not found; create a new entry */ 280 /* not found; create a new entry */
279 c = next_constant(fs); 281 c = next_constant(ls, f);
280 cnt = fs->f->consts; /* 'next_constant' may have reallocated this vector */ 282 cnt = f->consts; /* 'next_constant' may reallocate this vector */
281 ttype(&cnt[c]) = LUA_T_NUMBER; 283 ttype(&cnt[c]) = LUA_T_NUMBER;
282 nvalue(&cnt[c]) = r; 284 nvalue(&cnt[c]) = r;
283 return c; 285 return c;
@@ -291,7 +293,7 @@ static void code_number (LexState *ls, real f) {
291 code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1); 293 code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1);
292 } 294 }
293 else 295 else
294 code_constant(ls, real_constant(ls->fs, f)); 296 code_constant(ls, real_constant(ls, f));
295} 297}
296 298
297 299
@@ -304,16 +306,17 @@ static void flush_record (LexState *ls, int n) {
304static void flush_list (LexState *ls, int m, int n) { 306static void flush_list (LexState *ls, int m, int n) {
305 if (n > 0) { 307 if (n > 0) {
306 code_oparg(ls, SETLIST, m, -n); 308 code_oparg(ls, SETLIST, m, -n);
307 code_byte(ls->fs, (Byte)n); 309 code_byte(ls, (Byte)n);
308 } 310 }
309} 311}
310 312
311 313
312static void luaI_registerlocalvar (FuncState *fs, TaggedString *varname, 314static void luaI_registerlocalvar (LexState *ls, TaggedString *varname,
313 int line) { 315 int line) {
316 FuncState *fs = ls->fs;
314 if (fs->nvars != -1) { /* debug information? */ 317 if (fs->nvars != -1) { /* debug information? */
315 TProtoFunc *f = fs->f; 318 TProtoFunc *f = fs->f;
316 luaM_growvector(f->locvars, fs->nvars, 1, LocVar, "", MAX_INT); 319 luaM_growvector(ls->L, f->locvars, fs->nvars, 1, LocVar, "", MAX_INT);
317 f->locvars[fs->nvars].varname = varname; 320 f->locvars[fs->nvars].varname = varname;
318 f->locvars[fs->nvars].line = line; 321 f->locvars[fs->nvars].line = line;
319 fs->nvars++; 322 fs->nvars++;
@@ -321,8 +324,8 @@ static void luaI_registerlocalvar (FuncState *fs, TaggedString *varname,
321} 324}
322 325
323 326
324static void luaI_unregisterlocalvar (FuncState *fs, int line) { 327static void luaI_unregisterlocalvar (LexState *ls, int line) {
325 luaI_registerlocalvar(fs, NULL, line); 328 luaI_registerlocalvar(ls, NULL, line);
326} 329}
327 330
328 331
@@ -330,7 +333,7 @@ static void store_localvar (LexState *ls, TaggedString *name, int n) {
330 FuncState *fs = ls->fs; 333 FuncState *fs = ls->fs;
331 checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables"); 334 checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables");
332 fs->localvar[fs->nlocalvar+n] = name; 335 fs->localvar[fs->nlocalvar+n] = name;
333 luaI_registerlocalvar(fs, name, ls->linenumber); 336 luaI_registerlocalvar(ls, name, ls->linenumber);
334} 337}
335 338
336 339
@@ -371,7 +374,7 @@ static void singlevar (LexState *ls, TaggedString *n, vardesc *var, int prev) {
371 if (aux_localname(level, n) >= 0) 374 if (aux_localname(level, n) >= 0)
372 luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str); 375 luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
373 var->k = VGLOBAL; 376 var->k = VGLOBAL;
374 var->info = string_constant(fs, n); 377 var->info = string_constant(ls, fs, n);
375 } 378 }
376} 379}
377 380
@@ -404,7 +407,7 @@ static void pushupvalue (LexState *ls, TaggedString *n) {
404 407
405 408
406static void check_debugline (LexState *ls) { 409static void check_debugline (LexState *ls) {
407 if (L->debug && ls->linenumber != ls->fs->lastsetline) { 410 if (ls->L->debug && ls->linenumber != ls->fs->lastsetline) {
408 code_oparg(ls, SETLINE, ls->linenumber, 0); 411 code_oparg(ls, SETLINE, ls->linenumber, 0);
409 ls->fs->lastsetline = ls->linenumber; 412 ls->fs->lastsetline = ls->linenumber;
410 } 413 }
@@ -464,7 +467,7 @@ static void code_args (LexState *ls, int nparams, int dots) {
464 else { 467 else {
465 fs->f->code[1] = (Byte)(nparams+ZEROVARARG); 468 fs->f->code[1] = (Byte)(nparams+ZEROVARARG);
466 deltastack(ls, nparams+1); 469 deltastack(ls, nparams+1);
467 add_localvar(ls, luaS_new("arg")); 470 add_localvar(ls, luaS_new(ls->L, "arg"));
468 } 471 }
469} 472}
470 473
@@ -515,7 +518,7 @@ static void storevar (LexState *ls, const vardesc *var) {
515 code_opcode(ls, SETTABLEPOP, -3); 518 code_opcode(ls, SETTABLEPOP, -3);
516 break; 519 break;
517 default: 520 default:
518 LUA_INTERNALERROR("invalid var kind to store"); 521 LUA_INTERNALERROR(ls->L, "invalid var kind to store");
519 } 522 }
520} 523}
521 524
@@ -548,7 +551,7 @@ static void codeIf (LexState *ls, int thenAdd, int elseAdd) {
548static void func_onstack (LexState *ls, FuncState *func) { 551static void func_onstack (LexState *ls, FuncState *func) {
549 FuncState *fs = ls->fs; 552 FuncState *fs = ls->fs;
550 int i; 553 int i;
551 int c = next_constant(fs); 554 int c = next_constant(ls, fs->f);
552 ttype(&fs->f->consts[c]) = LUA_T_PROTO; 555 ttype(&fs->f->consts[c]) = LUA_T_PROTO;
553 fs->f->consts[c].value.tf = func->f; 556 fs->f->consts[c].value.tf = func->f;
554 if (func->nupvalues == 0) 557 if (func->nupvalues == 0)
@@ -558,13 +561,14 @@ static void func_onstack (LexState *ls, FuncState *func) {
558 lua_pushvar(ls, &func->upvalues[i]); 561 lua_pushvar(ls, &func->upvalues[i]);
559 deltastack(ls, 1); /* CLOSURE puts one extra element (before poping) */ 562 deltastack(ls, 1); /* CLOSURE puts one extra element (before poping) */
560 code_oparg(ls, CLOSURE, c, -func->nupvalues); 563 code_oparg(ls, CLOSURE, c, -func->nupvalues);
561 code_byte(fs, (Byte)func->nupvalues); 564 code_byte(ls, (Byte)func->nupvalues);
562 } 565 }
563} 566}
564 567
565 568
566static void init_state (LexState *ls, FuncState *fs, TaggedString *source) { 569static void init_state (LexState *ls, FuncState *fs, TaggedString *source) {
567 TProtoFunc *f = luaF_newproto(); 570 lua_State *L = ls->L;
571 TProtoFunc *f = luaF_newproto(ls->L);
568 fs->prev = ls->fs; /* linked list of funcstates */ 572 fs->prev = ls->fs; /* linked list of funcstates */
569 ls->fs = fs; 573 ls->fs = fs;
570 fs->stacksize = 0; 574 fs->stacksize = 0;
@@ -577,10 +581,11 @@ static void init_state (LexState *ls, FuncState *fs, TaggedString *source) {
577 fs->pc = 0; 581 fs->pc = 0;
578 f->code = NULL; 582 f->code = NULL;
579 fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */ 583 fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */
580 code_byte(fs, 0); /* to be filled with maxstacksize */ 584 code_byte(ls, 0); /* to be filled with maxstacksize */
581 code_byte(fs, 0); /* to be filled with arg information */ 585 code_byte(ls, 0); /* to be filled with arg information */
582 /* push function (to avoid GC) */ 586 /* push function (to avoid GC) */
583 tfvalue(L->stack.top) = f; ttype(L->stack.top) = LUA_T_PROTO; 587 tfvalue(L->stack.top) = f;
588 ttype(L->stack.top) = LUA_T_PROTO;
584 incr_top; 589 incr_top;
585} 590}
586 591
@@ -590,14 +595,14 @@ static void close_func (LexState *ls) {
590 TProtoFunc *f = fs->f; 595 TProtoFunc *f = fs->f;
591 code_opcode(ls, ENDCODE, 0); 596 code_opcode(ls, ENDCODE, 0);
592 f->code[0] = (Byte)fs->maxstacksize; 597 f->code[0] = (Byte)fs->maxstacksize;
593 luaM_reallocvector(f->code, fs->pc, Byte); 598 luaM_reallocvector(ls->L, f->code, fs->pc, Byte);
594 luaM_reallocvector(f->consts, f->nconsts, TObject); 599 luaM_reallocvector(ls->L, f->consts, f->nconsts, TObject);
595 if (fs->nvars != -1) { /* debug information? */ 600 if (fs->nvars != -1) { /* debug information? */
596 luaI_registerlocalvar(fs, NULL, -1); /* flag end of vector */ 601 luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */
597 luaM_reallocvector(f->locvars, fs->nvars, LocVar); 602 luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar);
598 } 603 }
599 ls->fs = fs->prev; 604 ls->fs = fs->prev;
600 L->stack.top--; /* pop function */ 605 ls->L->stack.top--; /* pop function */
601} 606}
602 607
603 608
@@ -664,7 +669,7 @@ static int checkname (LexState *ls) {
664 int sc; 669 int sc;
665 if (ls->token != NAME) 670 if (ls->token != NAME)
666 luaX_error(ls, "`NAME' expected"); 671 luaX_error(ls, "`NAME' expected");
667 sc = string_constant(ls->fs, ls->seminfo.ts); 672 sc = string_constant(ls, ls->fs, ls->seminfo.ts);
668 next(ls); 673 next(ls);
669 return sc; 674 return sc;
670} 675}
@@ -685,11 +690,11 @@ static int optional (LexState *ls, int c) {
685} 690}
686 691
687 692
688TProtoFunc *luaY_parser (ZIO *z) { 693TProtoFunc *luaY_parser (lua_State *L, ZIO *z) {
689 struct LexState lexstate; 694 struct LexState lexstate;
690 struct FuncState funcstate; 695 struct FuncState funcstate;
691 luaX_setinput(&lexstate, z); 696 luaX_setinput(L, &lexstate, z);
692 init_state(&lexstate, &funcstate, luaS_new(zname(z))); 697 init_state(&lexstate, &funcstate, luaS_new(L, zname(z)));
693 next(&lexstate); /* read first token */ 698 next(&lexstate); /* read first token */
694 chunk(&lexstate); 699 chunk(&lexstate);
695 if (lexstate.token != EOS) 700 if (lexstate.token != EOS)
@@ -708,7 +713,7 @@ TProtoFunc *luaY_parser (ZIO *z) {
708static void chunk (LexState *ls) { 713static void chunk (LexState *ls) {
709 /* chunk -> { stat [;] } ret */ 714 /* chunk -> { stat [;] } ret */
710 while (stat(ls)) { 715 while (stat(ls)) {
711 LUA_ASSERT(ls->fs->stacksize == ls->fs->nlocalvar, 716 LUA_ASSERT(ls->L, ls->fs->stacksize == ls->fs->nlocalvar,
712 "stack size != # local vars"); 717 "stack size != # local vars");
713 optional(ls, ';'); 718 optional(ls, ';');
714 } 719 }
@@ -728,7 +733,7 @@ static void whilestat (LexState *ls, int line) {
728 block(ls); 733 block(ls);
729 check_match(ls, END, WHILE, line); 734 check_match(ls, END, WHILE, line);
730 cond_size = cond_end-while_init; 735 cond_size = cond_end-while_init;
731 check_pc(fs, cond_size); 736 check_pc(ls, cond_size);
732 memcpy(f->code+fs->pc, f->code+while_init, cond_size); 737 memcpy(f->code+fs->pc, f->code+while_init, cond_size);
733 luaO_memdown(f->code+while_init, f->code+cond_end, fs->pc-while_init); 738 luaO_memdown(f->code+while_init, f->code+cond_end, fs->pc-while_init);
734 while_init += JMPSIZE + fix_jump(ls, while_init, JMP, fs->pc-cond_size); 739 while_init += JMPSIZE + fix_jump(ls, while_init, JMP, fs->pc-cond_size);
@@ -841,7 +846,7 @@ static int stat (LexState *ls) {
841 846
842static int SaveWord (LexState *ls) { 847static int SaveWord (LexState *ls) {
843 int res = ls->fs->pc; 848 int res = ls->fs->pc;
844 check_pc(ls->fs, JMPSIZE); 849 check_pc(ls, JMPSIZE);
845 ls->fs->pc += JMPSIZE; /* open space */ 850 ls->fs->pc += JMPSIZE; /* open space */
846 return res; 851 return res;
847} 852}
@@ -864,7 +869,7 @@ static void block (LexState *ls) {
864 chunk(ls); 869 chunk(ls);
865 adjuststack(ls, fs->nlocalvar - nlocalvar); 870 adjuststack(ls, fs->nlocalvar - nlocalvar);
866 for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--) 871 for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--)
867 luaI_unregisterlocalvar(fs, fs->lastsetline); 872 luaI_unregisterlocalvar(ls, fs->lastsetline);
868} 873}
869 874
870static int funcname (LexState *ls, vardesc *v) { 875static int funcname (LexState *ls, vardesc *v) {
@@ -888,7 +893,7 @@ static void body (LexState *ls, int needself, int line) {
888 newfs.f->lineDefined = line; 893 newfs.f->lineDefined = line;
889 check(ls, '('); 894 check(ls, '(');
890 if (needself) 895 if (needself)
891 add_localvar(ls, luaS_new("self")); 896 add_localvar(ls, luaS_new(ls->L, "self"));
892 parlist(ls); 897 parlist(ls);
893 check(ls, ')'); 898 check(ls, ')');
894 chunk(ls); 899 chunk(ls);
@@ -1173,9 +1178,9 @@ static int funcparams (LexState *ls, int slf) {
1173 luaX_error(ls, "function arguments expected"); 1178 luaX_error(ls, "function arguments expected");
1174 break; 1179 break;
1175 } 1180 }
1176 code_byte(fs, CALL); 1181 code_byte(ls, CALL);
1177 code_byte(fs, 0); /* save space for nresult */ 1182 code_byte(ls, 0); /* save space for nresult */
1178 code_byte(fs, (Byte)(nparams+slf)); 1183 code_byte(ls, (Byte)(nparams+slf));
1179 return fs->pc-1; 1184 return fs->pc-1;
1180} 1185}
1181 1186
diff --git a/lparser.h b/lparser.h
index 6b243a98..df25c6fa 100644
--- a/lparser.h
+++ b/lparser.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.h,v 1.3 1999/02/25 19:13:56 roberto Exp roberto $ 2** $Id: lparser.h,v 1.4 1999/08/16 20:52:00 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -11,10 +11,7 @@
11#include "lzio.h" 11#include "lzio.h"
12 12
13 13
14void luaY_codedebugline (int line); 14TProtoFunc *luaY_parser (lua_State *L, ZIO *z);
15TProtoFunc *luaY_parser (ZIO *z);
16void luaY_error (const char *s);
17void luaY_syntaxerror (const char *s, const char *token);
18 15
19 16
20#endif 17#endif
diff --git a/lref.c b/lref.c
index 5c22b63b..caaae400 100644
--- a/lref.c
+++ b/lref.c
@@ -1,17 +1,19 @@
1/* 1/*
2** $Id: lref.c,v 1.1 1999/10/04 17:50:24 roberto Exp roberto $ 2** $Id: lref.c,v 1.2 1999/11/10 15:37:50 roberto Exp roberto $
3** REF mechanism 3** REF mechanism
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8#define LUA_REENTRANT
9
8#include "lmem.h" 10#include "lmem.h"
9#include "lref.h" 11#include "lref.h"
10#include "lstate.h" 12#include "lstate.h"
11#include "lua.h" 13#include "lua.h"
12 14
13 15
14int luaR_ref (const TObject *o, int lock) { 16int luaR_ref (lua_State *L, const TObject *o, int lock) {
15 int ref; 17 int ref;
16 if (ttype(o) == LUA_T_NIL) 18 if (ttype(o) == LUA_T_NIL)
17 ref = LUA_REFNIL; 19 ref = LUA_REFNIL;
@@ -21,7 +23,7 @@ int luaR_ref (const TObject *o, int lock) {
21 L->refFree = L->refArray[ref].st; 23 L->refFree = L->refArray[ref].st;
22 } 24 }
23 else { /* no more free places */ 25 else { /* no more free places */
24 luaM_growvector(L->refArray, L->refSize, 1, struct ref, refEM, MAX_INT); 26 luaM_growvector(L, L->refArray, L->refSize, 1, struct ref, refEM, MAX_INT);
25 ref = L->refSize++; 27 ref = L->refSize++;
26 } 28 }
27 L->refArray[ref].o = *o; 29 L->refArray[ref].o = *o;
@@ -31,17 +33,17 @@ int luaR_ref (const TObject *o, int lock) {
31} 33}
32 34
33 35
34void lua_unref (int ref) { 36void lua_unref (lua_State *L, int ref) {
35 if (ref >= 0) { 37 if (ref >= 0) {
36 if (ref >= L->refSize || L->refArray[ref].st >= 0) 38 if (ref >= L->refSize || L->refArray[ref].st >= 0)
37 lua_error("API error - invalid parameter for function `lua_unref'"); 39 lua_error(L, "API error - invalid parameter for function `lua_unref'");
38 L->refArray[ref].st = L->refFree; 40 L->refArray[ref].st = L->refFree;
39 L->refFree = ref; 41 L->refFree = ref;
40 } 42 }
41} 43}
42 44
43 45
44const TObject *luaR_getref (int ref) { 46const TObject *luaR_getref (lua_State *L, int ref) {
45 if (ref == LUA_REFNIL) 47 if (ref == LUA_REFNIL)
46 return &luaO_nilobject; 48 return &luaO_nilobject;
47 else if (0 <= ref && ref < L->refSize && 49 else if (0 <= ref && ref < L->refSize &&
@@ -66,7 +68,7 @@ static int ismarked (const TObject *o) {
66#ifdef DEBUG 68#ifdef DEBUG
67 case LUA_T_LINE: case LUA_T_CLMARK: 69 case LUA_T_LINE: case LUA_T_CLMARK:
68 case LUA_T_CMARK: case LUA_T_PMARK: 70 case LUA_T_CMARK: case LUA_T_PMARK:
69 LUA_INTERNALERROR("invalid type"); 71 LUA_INTERNALERROR(L, "invalid type");
70#endif 72#endif
71 default: /* number or cproto */ 73 default: /* number or cproto */
72 return 1; 74 return 1;
@@ -75,21 +77,21 @@ static int ismarked (const TObject *o) {
75 77
76 78
77/* for internal debugging only; check if a link of free refs is valid */ 79/* for internal debugging only; check if a link of free refs is valid */
78#define VALIDLINK(st,n) (NONEXT <= (st) && (st) < (n)) 80#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
79 81
80void luaR_invalidaterefs (void) { 82void luaR_invalidaterefs (lua_State *L) {
81 int n = L->refSize; 83 int n = L->refSize;
82 int i; 84 int i;
83 for (i=0; i<n; i++) { 85 for (i=0; i<n; i++) {
84 struct ref *r = &L->refArray[i]; 86 struct ref *r = &L->refArray[i];
85 if (r->st == HOLD && !ismarked(&r->o)) 87 if (r->st == HOLD && !ismarked(&r->o))
86 r->st = COLLECTED; 88 r->st = COLLECTED;
87 LUA_ASSERT((r->st == LOCK && ismarked(&r->o)) || 89 LUA_ASSERT(L, (r->st == LOCK && ismarked(&r->o)) ||
88 r->st == COLLECTED || 90 r->st == COLLECTED ||
89 r->st == NONEXT || 91 r->st == NONEXT ||
90 (r->st < n && VALIDLINK(L->refArray[r->st].st, n)), 92 (r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
91 "inconsistent ref table"); 93 "inconsistent ref table");
92 } 94 }
93 LUA_ASSERT(VALIDLINK(L->refFree, n), "inconsistent ref table"); 95 LUA_ASSERT(L, VALIDLINK(L, L->refFree, n), "inconsistent ref table");
94} 96}
95 97
diff --git a/lref.h b/lref.h
index 180daab0..aff9e185 100644
--- a/lref.h
+++ b/lref.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lref.h,v 1.1 1999/10/04 17:50:24 roberto Exp roberto $ 2** $Id: lref.h,v 1.2 1999/11/10 15:37:50 roberto Exp roberto $
3** REF mechanism 3** REF mechanism
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,9 +22,9 @@ struct ref {
22}; 22};
23 23
24 24
25int luaR_ref (const TObject *o, int lock); 25int luaR_ref (lua_State *L, const TObject *o, int lock);
26const TObject *luaR_getref (int ref); 26const TObject *luaR_getref (lua_State *L, int ref);
27void luaR_invalidaterefs (void); 27void luaR_invalidaterefs (lua_State *L);
28 28
29 29
30#endif 30#endif
diff --git a/lstate.c b/lstate.c
index 57aa292d..d996420a 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,10 +1,12 @@
1/* 1/*
2** $Id: lstate.c,v 1.15 1999/10/14 17:53:35 roberto Exp roberto $ 2** $Id: lstate.c,v 1.16 1999/11/10 15:39:35 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*/
6 6
7 7
8#define LUA_REENTRANT
9
8#include "lbuiltin.h" 10#include "lbuiltin.h"
9#include "ldo.h" 11#include "ldo.h"
10#include "lgc.h" 12#include "lgc.h"
@@ -19,9 +21,8 @@
19lua_State *lua_state = NULL; 21lua_State *lua_state = NULL;
20 22
21 23
22void lua_open (void) { 24lua_State *lua_newstate (void) {
23 if (lua_state) return; 25 lua_State *L = luaM_new(NULL, lua_State);
24 lua_state = luaM_new(lua_State);
25 L->Cstack.base = 0; 26 L->Cstack.base = 0;
26 L->Cstack.lua2C = 0; 27 L->Cstack.lua2C = 0;
27 L->Cstack.num = 0; 28 L->Cstack.num = 0;
@@ -45,31 +46,32 @@ void lua_open (void) {
45 L->refFree = NONEXT; 46 L->refFree = NONEXT;
46 L->nblocks = 0; 47 L->nblocks = 0;
47 L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ 48 L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
48 luaD_init(); 49 luaD_init(L);
49 luaS_init(); 50 luaS_init(L);
50 luaX_init(); 51 luaX_init(L);
51 luaT_init(); 52 luaT_init(L);
52 luaB_predefine(); 53 luaB_predefine(L);
54 return L;
53 L->GCthreshold = L->nblocks*4; 55 L->GCthreshold = L->nblocks*4;
54} 56}
55 57
56 58
57void lua_close (void) { 59void lua_close (lua_State *L) {
58 luaC_collect(1); /* collect all elements */ 60 luaC_collect(L, 1); /* collect all elements */
59 LUA_ASSERT(L->rootproto == NULL, "list should be empty"); 61 LUA_ASSERT(L, L->rootproto == NULL, "list should be empty");
60 LUA_ASSERT(L->rootcl == NULL, "list should be empty"); 62 LUA_ASSERT(L, L->rootcl == NULL, "list should be empty");
61 LUA_ASSERT(L->rootglobal == NULL, "list should be empty"); 63 LUA_ASSERT(L, L->rootglobal == NULL, "list should be empty");
62 LUA_ASSERT(L->roottable == NULL, "list should be empty"); 64 LUA_ASSERT(L, L->roottable == NULL, "list should be empty");
63 luaS_freeall(); 65 luaS_freeall(L);
64 luaM_free(L->stack.stack); 66 luaM_free(L, L->stack.stack);
65 luaM_free(L->IMtable); 67 luaM_free(L, L->IMtable);
66 luaM_free(L->refArray); 68 luaM_free(L, L->refArray);
67 luaM_free(L->Mbuffer); 69 luaM_free(L, L->Mbuffer);
68 luaM_free(L->Cblocks); 70 luaM_free(L, L->Cblocks);
69 LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks"); 71 LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks");
70 luaM_free(L); 72 luaM_free(L, L);
71 LUA_ASSERT(numblocks == 0, "memory leak!"); 73 LUA_ASSERT(L, numblocks == 0, "memory leak!");
72 LUA_ASSERT(totalmem == 0,"memory leak!"); 74 LUA_ASSERT(L, totalmem == 0,"memory leak!");
73 L = NULL; 75 L = NULL;
74} 76}
75 77
diff --git a/lstate.h b/lstate.h
index d508120e..2ee31840 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 1.21 1999/11/04 17:22:26 roberto Exp roberto $ 2** $Id: lstate.h,v 1.22 1999/11/10 15:39:35 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*/
@@ -79,7 +79,7 @@ struct lua_State {
79}; 79};
80 80
81 81
82#define L lua_state 82
83 83
84 84
85#endif 85#endif
diff --git a/lstring.c b/lstring.c
index f1353033..e5edc12c 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.26 1999/11/04 17:22:26 roberto Exp roberto $ 2** $Id: lstring.c,v 1.27 1999/11/10 15:39:35 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -7,6 +7,8 @@
7 7
8#include <string.h> 8#include <string.h>
9 9
10#define LUA_REENTRANT
11
10#include "lmem.h" 12#include "lmem.h"
11#include "lobject.h" 13#include "lobject.h"
12#include "lstate.h" 14#include "lstate.h"
@@ -15,8 +17,8 @@
15 17
16 18
17 19
18#define gcsizestring(l) numblocks(0, sizeof(TaggedString)+l) 20#define gcsizestring(L, l) numblocks(L, 0, sizeof(TaggedString)+l)
19#define gcsizeudata gcsizestring(0) 21#define gcsizeudata gcsizestring(L, 0)
20 22
21 23
22 24
@@ -30,9 +32,9 @@
30static TaggedString *init_hash[1] = {NULL}; 32static TaggedString *init_hash[1] = {NULL};
31 33
32 34
33void luaS_init (void) { 35void luaS_init (lua_State *L) {
34 int i; 36 int i;
35 L->string_root = luaM_newvector(NUM_HASHS, stringtable); 37 L->string_root = luaM_newvector(L, NUM_HASHS, stringtable);
36 for (i=0; i<NUM_HASHS; i++) { 38 for (i=0; i<NUM_HASHS; i++) {
37 L->string_root[i].size = 1; 39 L->string_root[i].size = 1;
38 L->string_root[i].nuse = 0; 40 L->string_root[i].nuse = 0;
@@ -41,15 +43,15 @@ void luaS_init (void) {
41} 43}
42 44
43 45
44void luaS_freeall (void) { 46void luaS_freeall (lua_State *L) {
45 int i; 47 int i;
46 for (i=0; i<NUM_HASHS; i++) { 48 for (i=0; i<NUM_HASHS; i++) {
47 LUA_ASSERT(L->string_root[i].nuse==0, "non-empty string table"); 49 LUA_ASSERT(L, L->string_root[i].nuse==0, "non-empty string table");
48 if (L->string_root[i].hash != init_hash) 50 if (L->string_root[i].hash != init_hash)
49 luaM_free(L->string_root[i].hash); 51 luaM_free(L, L->string_root[i].hash);
50 } 52 }
51 luaM_free(L->string_root); 53 luaM_free(L, L->string_root);
52 LUA_ASSERT(init_hash[0] == NULL, "init_hash corrupted"); 54 LUA_ASSERT(L, init_hash[0] == NULL, "init_hash corrupted");
53} 55}
54 56
55 57
@@ -61,9 +63,9 @@ static unsigned long hash_s (const char *s, long l) {
61} 63}
62 64
63 65
64void luaS_grow (stringtable *tb) { 66void luaS_grow (lua_State *L, stringtable *tb) {
65 int ns = luaO_redimension(tb->nuse*2); /* new size */ 67 int ns = luaO_redimension(L, tb->nuse*2); /* new size */
66 TaggedString **newhash = luaM_newvector(ns, TaggedString *); 68 TaggedString **newhash = luaM_newvector(L, ns, TaggedString *);
67 int i; 69 int i;
68 for (i=0; i<ns; i++) newhash[i] = NULL; 70 for (i=0; i<ns; i++) newhash[i] = NULL;
69 /* rehash */ 71 /* rehash */
@@ -77,14 +79,14 @@ void luaS_grow (stringtable *tb) {
77 p = next; 79 p = next;
78 } 80 }
79 } 81 }
80 luaM_free(tb->hash); 82 luaM_free(L, tb->hash);
81 tb->size = ns; 83 tb->size = ns;
82 tb->hash = newhash; 84 tb->hash = newhash;
83} 85}
84 86
85 87
86static TaggedString *newone (long l, unsigned long h) { 88static TaggedString *newone (lua_State *L, long l, unsigned long h) {
87 TaggedString *ts = (TaggedString *)luaM_malloc( 89 TaggedString *ts = (TaggedString *)luaM_malloc(L,
88 sizeof(TaggedString)+l*sizeof(char)); 90 sizeof(TaggedString)+l*sizeof(char));
89 ts->marked = 0; 91 ts->marked = 0;
90 ts->nexthash = NULL; 92 ts->nexthash = NULL;
@@ -93,20 +95,20 @@ static TaggedString *newone (long l, unsigned long h) {
93} 95}
94 96
95 97
96static TaggedString *newone_s (const char *str, long l, unsigned long h) { 98static TaggedString *newone_s (lua_State *L, const char *str, long l, unsigned long h) {
97 TaggedString *ts = newone(l, h); 99 TaggedString *ts = newone(L, l, h);
98 memcpy(ts->str, str, l); 100 memcpy(ts->str, str, l);
99 ts->str[l] = 0; /* ending 0 */ 101 ts->str[l] = 0; /* ending 0 */
100 ts->u.s.gv = NULL; /* no global value */ 102 ts->u.s.gv = NULL; /* no global value */
101 ts->u.s.len = l; 103 ts->u.s.len = l;
102 ts->constindex = 0; 104 ts->constindex = 0;
103 L->nblocks += gcsizestring(l); 105 L->nblocks += gcsizestring(L, l);
104 return ts; 106 return ts;
105} 107}
106 108
107 109
108static TaggedString *newone_u (void *buff, int tag, unsigned long h) { 110static TaggedString *newone_u (lua_State *L, void *buff, int tag, unsigned long h) {
109 TaggedString *ts = newone(0, h); 111 TaggedString *ts = newone(L, 0, h);
110 ts->u.d.value = buff; 112 ts->u.d.value = buff;
111 ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; 113 ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
112 ts->constindex = -1; /* tag -> this is a userdata */ 114 ts->constindex = -1; /* tag -> this is a userdata */
@@ -115,15 +117,15 @@ static TaggedString *newone_u (void *buff, int tag, unsigned long h) {
115} 117}
116 118
117 119
118static void newentry (stringtable *tb, TaggedString *ts, int h) { 120static void newentry (lua_State *L, stringtable *tb, TaggedString *ts, int h) {
119 tb->nuse++; 121 tb->nuse++;
120 if (tb->nuse >= tb->size) { /* no more room? */ 122 if (tb->nuse >= tb->size) { /* no more room? */
121 if (tb->hash == init_hash) { /* cannot change init_hash */ 123 if (tb->hash == init_hash) { /* cannot change init_hash */
122 LUA_ASSERT(h==0, "`init_hash' has size 1"); 124 LUA_ASSERT(L, h==0, "`init_hash' has size 1");
123 tb->hash = luaM_newvector(1, TaggedString *); /* so, `clone' it */ 125 tb->hash = luaM_newvector(L, 1, TaggedString *); /* so, `clone' it */
124 tb->hash[0] = NULL; 126 tb->hash[0] = NULL;
125 } 127 }
126 luaS_grow(tb); 128 luaS_grow(L, tb);
127 h = ts->hash%tb->size; /* new hash position */ 129 h = ts->hash%tb->size; /* new hash position */
128 } 130 }
129 ts->nexthash = tb->hash[h]; /* chain new entry */ 131 ts->nexthash = tb->hash[h]; /* chain new entry */
@@ -131,7 +133,7 @@ static void newentry (stringtable *tb, TaggedString *ts, int h) {
131} 133}
132 134
133 135
134TaggedString *luaS_newlstr (const char *str, long l) { 136TaggedString *luaS_newlstr (lua_State *L, const char *str, long l) {
135 unsigned long h = hash_s(str, l); 137 unsigned long h = hash_s(str, l);
136 stringtable *tb = &L->string_root[h%NUM_HASHSTR]; 138 stringtable *tb = &L->string_root[h%NUM_HASHSTR];
137 int h1 = h%tb->size; 139 int h1 = h%tb->size;
@@ -141,14 +143,14 @@ TaggedString *luaS_newlstr (const char *str, long l) {
141 return ts; 143 return ts;
142 } 144 }
143 /* not found */ 145 /* not found */
144 ts = newone_s(str, l, h); /* create new entry */ 146 ts = newone_s(L, str, l, h); /* create new entry */
145 newentry(tb, ts, h1); /* insert it on table */ 147 newentry(L, tb, ts, h1); /* insert it on table */
146 return ts; 148 return ts;
147} 149}
148 150
149 151
150TaggedString *luaS_createudata (void *udata, int tag) { 152TaggedString *luaS_createudata (lua_State *L, void *udata, int tag) {
151 unsigned long h = IntPoint(udata); 153 unsigned long h = IntPoint(L, udata);
152 stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR]; 154 stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
153 int h1 = h%tb->size; 155 int h1 = h%tb->size;
154 TaggedString *ts; 156 TaggedString *ts;
@@ -157,38 +159,38 @@ TaggedString *luaS_createudata (void *udata, int tag) {
157 return ts; 159 return ts;
158 } 160 }
159 /* not found */ 161 /* not found */
160 ts = newone_u(udata, tag, h); 162 ts = newone_u(L, udata, tag, h);
161 newentry(tb, ts, h1); 163 newentry(L, tb, ts, h1);
162 return ts; 164 return ts;
163} 165}
164 166
165 167
166TaggedString *luaS_new (const char *str) { 168TaggedString *luaS_new (lua_State *L, const char *str) {
167 return luaS_newlstr(str, strlen(str)); 169 return luaS_newlstr(L, str, strlen(str));
168} 170}
169 171
170TaggedString *luaS_newfixedstring (const char *str) { 172TaggedString *luaS_newfixedstring (lua_State *L, const char *str) {
171 TaggedString *ts = luaS_new(str); 173 TaggedString *ts = luaS_new(L, str);
172 if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */ 174 if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
173 return ts; 175 return ts;
174} 176}
175 177
176 178
177void luaS_free (TaggedString *t) { 179void luaS_free (lua_State *L, TaggedString *t) {
178 if (t->constindex == -1) /* is userdata? */ 180 if (t->constindex == -1) /* is userdata? */
179 L->nblocks -= gcsizeudata; 181 L->nblocks -= gcsizeudata;
180 else { /* is string */ 182 else { /* is string */
181 L->nblocks -= gcsizestring(t->u.s.len); 183 L->nblocks -= gcsizestring(L, t->u.s.len);
182 luaM_free(t->u.s.gv); 184 luaM_free(L, t->u.s.gv);
183 } 185 }
184 luaM_free(t); 186 luaM_free(L, t);
185} 187}
186 188
187 189
188GlobalVar *luaS_assertglobal (TaggedString *ts) { 190GlobalVar *luaS_assertglobal (lua_State *L, TaggedString *ts) {
189 GlobalVar *gv = ts->u.s.gv; 191 GlobalVar *gv = ts->u.s.gv;
190 if (!gv) { /* no global value yet? */ 192 if (!gv) { /* no global value yet? */
191 gv = luaM_new(GlobalVar); 193 gv = luaM_new(L, GlobalVar);
192 gv->value.ttype = LUA_T_NIL; /* initial value */ 194 gv->value.ttype = LUA_T_NIL; /* initial value */
193 gv->name = ts; 195 gv->name = ts;
194 gv->next = L->rootglobal; /* chain in global list */ 196 gv->next = L->rootglobal; /* chain in global list */
@@ -199,13 +201,13 @@ GlobalVar *luaS_assertglobal (TaggedString *ts) {
199} 201}
200 202
201 203
202GlobalVar *luaS_assertglobalbyname (const char *name) { 204GlobalVar *luaS_assertglobalbyname (lua_State *L, const char *name) {
203 return luaS_assertglobal(luaS_new(name)); 205 return luaS_assertglobal(L, luaS_new(L, name));
204} 206}
205 207
206 208
207int luaS_globaldefined (const char *name) { 209int luaS_globaldefined (lua_State *L, const char *name) {
208 TaggedString *ts = luaS_new(name); 210 TaggedString *ts = luaS_new(L, name);
209 return ts->u.s.gv && ts->u.s.gv->value.ttype != LUA_T_NIL; 211 return ts->u.s.gv && ts->u.s.gv->value.ttype != LUA_T_NIL;
210} 212}
211 213
diff --git a/lstring.h b/lstring.h
index d39e1d27..be260b89 100644
--- a/lstring.h
+++ b/lstring.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.h,v 1.11 1999/10/14 19:13:31 roberto Exp roberto $ 2** $Id: lstring.h,v 1.12 1999/11/04 17:22:26 roberto Exp roberto $
3** String table (keep all strings handled by Lua) 3** String table (keep all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -14,7 +14,7 @@
14 14
15#define NUM_HASHSTR 31 /* a prime not in array `dimensions' */ 15#define NUM_HASHSTR 31 /* a prime not in array `dimensions' */
16#define NUM_HASHUDATA 31 /* idem */ 16#define NUM_HASHUDATA 31 /* idem */
17#define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA) 17#define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA)
18 18
19 19
20/* 20/*
@@ -25,17 +25,17 @@
25#define RESERVEDMARK 3 25#define RESERVEDMARK 3
26 26
27 27
28void luaS_init (void); 28void luaS_init (lua_State *L);
29void luaS_grow (stringtable *tb); 29void luaS_grow (lua_State *L, stringtable *tb);
30TaggedString *luaS_createudata (void *udata, int tag); 30TaggedString *luaS_createudata (lua_State *L, void *udata, int tag);
31void luaS_freeall (void); 31void luaS_freeall (lua_State *L);
32void luaS_free (TaggedString *ts); 32void luaS_free (lua_State *L, TaggedString *ts);
33TaggedString *luaS_newlstr (const char *str, long l); 33TaggedString *luaS_newlstr (lua_State *L, const char *str, long l);
34TaggedString *luaS_new (const char *str); 34TaggedString *luaS_new (lua_State *L, const char *str);
35TaggedString *luaS_newfixedstring (const char *str); 35TaggedString *luaS_newfixedstring (lua_State *L, const char *str);
36GlobalVar *luaS_assertglobal (TaggedString *ts); 36GlobalVar *luaS_assertglobal (lua_State *L, TaggedString *ts);
37GlobalVar *luaS_assertglobalbyname (const char *name); 37GlobalVar *luaS_assertglobalbyname (lua_State *L, const char *name);
38int luaS_globaldefined (const char *name); 38int luaS_globaldefined (lua_State *L, const char *name);
39 39
40 40
41#endif 41#endif
diff --git a/lstrlib.c b/lstrlib.c
index 27676543..c973d0e4 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.35 1999/10/25 13:35:44 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.36 1999/11/11 16:45:04 roberto Exp roberto $
3** Standard library for strings and pattern-matching 3** Standard library for strings and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,28 +10,30 @@
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12 12
13#define LUA_REENTRANT
14
13#include "lauxlib.h" 15#include "lauxlib.h"
14#include "lua.h" 16#include "lua.h"
15#include "lualib.h" 17#include "lualib.h"
16 18
17 19
18 20
19static void addnchar (const char *s, int n) { 21static void addnchar (lua_State *L, const char *s, int n) {
20 char *b = luaL_openspace(n); 22 char *b = luaL_openspace(L, n);
21 memcpy(b, s, n); 23 memcpy(b, s, n);
22 luaL_addsize(n); 24 luaL_addsize(L, n);
23} 25}
24 26
25 27
26static void str_len (void) { 28static void str_len (lua_State *L) {
27 long l; 29 long l;
28 luaL_check_lstr(1, &l); 30 luaL_check_lstr(L, 1, &l);
29 lua_pushnumber(l); 31 lua_pushnumber(L, l);
30} 32}
31 33
32 34
33static void closeandpush (void) { 35static void closeandpush (lua_State *L) {
34 lua_pushlstring(luaL_buffer(), luaL_getsize()); 36 lua_pushlstring(L, luaL_buffer(L), luaL_getsize(L));
35} 37}
36 38
37 39
@@ -41,69 +43,69 @@ static long posrelat (long pos, long len) {
41} 43}
42 44
43 45
44static void str_sub (void) { 46static void str_sub (lua_State *L) {
45 long l; 47 long l;
46 const char *s = luaL_check_lstr(1, &l); 48 const char *s = luaL_check_lstr(L, 1, &l);
47 long start = posrelat(luaL_check_long(2), l); 49 long start = posrelat(luaL_check_long(L, 2), l);
48 long end = posrelat(luaL_opt_long(3, -1), l); 50 long end = posrelat(luaL_opt_long(L, 3, -1), l);
49 if (start < 1) start = 1; 51 if (start < 1) start = 1;
50 if (end > l) end = l; 52 if (end > l) end = l;
51 if (start <= end) 53 if (start <= end)
52 lua_pushlstring(s+start-1, end-start+1); 54 lua_pushlstring(L, s+start-1, end-start+1);
53 else lua_pushstring(""); 55 else lua_pushstring(L, "");
54} 56}
55 57
56 58
57static void str_lower (void) { 59static void str_lower (lua_State *L) {
58 long l; 60 long l;
59 int i; 61 int i;
60 const char *s = luaL_check_lstr(1, &l); 62 const char *s = luaL_check_lstr(L, 1, &l);
61 luaL_resetbuffer(); 63 luaL_resetbuffer(L);
62 for (i=0; i<l; i++) 64 for (i=0; i<l; i++)
63 luaL_addchar(tolower((unsigned char)(s[i]))); 65 luaL_addchar(L, tolower((unsigned char)(s[i])));
64 closeandpush(); 66 closeandpush(L);
65} 67}
66 68
67 69
68static void str_upper (void) { 70static void str_upper (lua_State *L) {
69 long l; 71 long l;
70 int i; 72 int i;
71 const char *s = luaL_check_lstr(1, &l); 73 const char *s = luaL_check_lstr(L, 1, &l);
72 luaL_resetbuffer(); 74 luaL_resetbuffer(L);
73 for (i=0; i<l; i++) 75 for (i=0; i<l; i++)
74 luaL_addchar(toupper((unsigned char)(s[i]))); 76 luaL_addchar(L, toupper((unsigned char)(s[i])));
75 closeandpush(); 77 closeandpush(L);
76} 78}
77 79
78static void str_rep (void) { 80static void str_rep (lua_State *L) {
79 long l; 81 long l;
80 const char *s = luaL_check_lstr(1, &l); 82 const char *s = luaL_check_lstr(L, 1, &l);
81 int n = luaL_check_int(2); 83 int n = luaL_check_int(L, 2);
82 luaL_resetbuffer(); 84 luaL_resetbuffer(L);
83 while (n-- > 0) 85 while (n-- > 0)
84 addnchar(s, l); 86 addnchar(L, s, l);
85 closeandpush(); 87 closeandpush(L);
86} 88}
87 89
88 90
89static void str_byte (void) { 91static void str_byte (lua_State *L) {
90 long l; 92 long l;
91 const char *s = luaL_check_lstr(1, &l); 93 const char *s = luaL_check_lstr(L, 1, &l);
92 long pos = posrelat(luaL_opt_long(2, 1), l); 94 long pos = posrelat(luaL_opt_long(L, 2, 1), l);
93 luaL_arg_check(0<pos && pos<=l, 2, "out of range"); 95 luaL_arg_check(L, 0<pos && pos<=l, 2, "out of range");
94 lua_pushnumber((unsigned char)s[pos-1]); 96 lua_pushnumber(L, (unsigned char)s[pos-1]);
95} 97}
96 98
97 99
98static void str_char (void) { 100static void str_char (lua_State *L) {
99 int i = 0; 101 int i = 0;
100 luaL_resetbuffer(); 102 luaL_resetbuffer(L);
101 while (lua_getparam(++i) != LUA_NOOBJECT) { 103 while (lua_getparam(L, ++i) != LUA_NOOBJECT) {
102 int c = luaL_check_int(i); 104 int c = luaL_check_int(L, i);
103 luaL_arg_check((unsigned char)c == c, i, "invalid value"); 105 luaL_arg_check(L, (unsigned char)c == c, i, "invalid value");
104 luaL_addchar((unsigned char)c); 106 luaL_addchar(L, (unsigned char)c);
105 } 107 }
106 closeandpush(); 108 closeandpush(L);
107} 109}
108 110
109 111
@@ -133,42 +135,42 @@ struct Capture {
133#define SPECIALS "^$*+?.([%-" 135#define SPECIALS "^$*+?.([%-"
134 136
135 137
136static void push_captures (struct Capture *cap) { 138static void push_captures (lua_State *L, struct Capture *cap) {
137 int i; 139 int i;
138 for (i=0; i<cap->level; i++) { 140 for (i=0; i<cap->level; i++) {
139 int l = cap->capture[i].len; 141 int l = cap->capture[i].len;
140 if (l == -1) lua_error("unfinished capture"); 142 if (l == -1) lua_error(L, "unfinished capture");
141 lua_pushlstring(cap->capture[i].init, l); 143 lua_pushlstring(L, cap->capture[i].init, l);
142 } 144 }
143} 145}
144 146
145 147
146static int check_cap (int l, struct Capture *cap) { 148static int check_cap (lua_State *L, int l, struct Capture *cap) {
147 l -= '1'; 149 l -= '1';
148 if (!(0 <= l && l < cap->level && cap->capture[l].len != -1)) 150 if (!(0 <= l && l < cap->level && cap->capture[l].len != -1))
149 lua_error("invalid capture index"); 151 lua_error(L, "invalid capture index");
150 return l; 152 return l;
151} 153}
152 154
153 155
154static int capture_to_close (struct Capture *cap) { 156static int capture_to_close (lua_State *L, struct Capture *cap) {
155 int level = cap->level; 157 int level = cap->level;
156 for (level--; level>=0; level--) 158 for (level--; level>=0; level--)
157 if (cap->capture[level].len == -1) return level; 159 if (cap->capture[level].len == -1) return level;
158 lua_error("invalid pattern capture"); 160 lua_error(L, "invalid pattern capture");
159 return 0; /* to avoid warnings */ 161 return 0; /* to avoid warnings */
160} 162}
161 163
162 164
163const char *luaI_classend (const char *p) { 165const char *luaI_classend (lua_State *L, const char *p) {
164 switch (*p++) { 166 switch (*p++) {
165 case ESC: 167 case ESC:
166 if (*p == '\0') lua_error("incorrect pattern (ends with `%')"); 168 if (*p == '\0') lua_error(L, "incorrect pattern (ends with `%')");
167 return p+1; 169 return p+1;
168 case '[': 170 case '[':
169 if (*p == '^') p++; 171 if (*p == '^') p++;
170 do { /* look for a ']' */ 172 do { /* look for a ']' */
171 if (*p == '\0') lua_error("incorrect pattern (missing `]')"); 173 if (*p == '\0') lua_error(L, "incorrect pattern (missing `]')");
172 if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */ 174 if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */
173 } while (*p != ']'); 175 } while (*p != ']');
174 return p+1; 176 return p+1;
@@ -236,13 +238,13 @@ int luaI_singlematch (int c, const char *p, const char *ep) {
236} 238}
237 239
238 240
239static const char *match (const char *s, const char *p, struct Capture *cap); 241static const char *match (lua_State *L, const char *s, const char *p, struct Capture *cap);
240 242
241 243
242static const char *matchbalance (const char *s, const char *p, 244static const char *matchbalance (lua_State *L, const char *s, const char *p,
243 struct Capture *cap) { 245 struct Capture *cap) {
244 if (*p == 0 || *(p+1) == 0) 246 if (*p == 0 || *(p+1) == 0)
245 lua_error("unbalanced pattern"); 247 lua_error(L, "unbalanced pattern");
246 if (*s != *p) return NULL; 248 if (*s != *p) return NULL;
247 else { 249 else {
248 int b = *p; 250 int b = *p;
@@ -259,14 +261,14 @@ static const char *matchbalance (const char *s, const char *p,
259} 261}
260 262
261 263
262static const char *max_expand (const char *s, const char *p, const char *ep, 264static const char *max_expand (lua_State *L, const char *s, const char *p, const char *ep,
263 struct Capture *cap) { 265 struct Capture *cap) {
264 int i = 0; /* counts maximum expand for item */ 266 int i = 0; /* counts maximum expand for item */
265 while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) 267 while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
266 i++; 268 i++;
267 /* keeps trying to match mith the maximum repetitions */ 269 /* keeps trying to match mith the maximum repetitions */
268 while (i>=0) { 270 while (i>=0) {
269 const char *res = match((s+i), ep+1, cap); 271 const char *res = match(L, (s+i), ep+1, cap);
270 if (res) return res; 272 if (res) return res;
271 i--; /* else didn't match; reduce 1 repetition to try again */ 273 i--; /* else didn't match; reduce 1 repetition to try again */
272 } 274 }
@@ -274,10 +276,10 @@ static const char *max_expand (const char *s, const char *p, const char *ep,
274} 276}
275 277
276 278
277static const char *min_expand (const char *s, const char *p, const char *ep, 279static const char *min_expand (lua_State *L, const char *s, const char *p, const char *ep,
278 struct Capture *cap) { 280 struct Capture *cap) {
279 for (;;) { 281 for (;;) {
280 const char *res = match(s, ep+1, cap); 282 const char *res = match(L, s, ep+1, cap);
281 if (res != NULL) 283 if (res != NULL)
282 return res; 284 return res;
283 else if (s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep)) 285 else if (s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep))
@@ -287,34 +289,34 @@ static const char *min_expand (const char *s, const char *p, const char *ep,
287} 289}
288 290
289 291
290static const char *start_capt (const char *s, const char *p, 292static const char *start_capt (lua_State *L, const char *s, const char *p,
291 struct Capture *cap) { 293 struct Capture *cap) {
292 const char *res; 294 const char *res;
293 int level = cap->level; 295 int level = cap->level;
294 if (level >= MAX_CAPT) lua_error("too many captures"); 296 if (level >= MAX_CAPT) lua_error(L, "too many captures");
295 cap->capture[level].init = s; 297 cap->capture[level].init = s;
296 cap->capture[level].len = -1; 298 cap->capture[level].len = -1;
297 cap->level = level+1; 299 cap->level = level+1;
298 if ((res=match(s, p+1, cap)) == NULL) /* match failed? */ 300 if ((res=match(L, s, p+1, cap)) == NULL) /* match failed? */
299 cap->level--; /* undo capture */ 301 cap->level--; /* undo capture */
300 return res; 302 return res;
301} 303}
302 304
303 305
304static const char *end_capt (const char *s, const char *p, 306static const char *end_capt (lua_State *L, const char *s, const char *p,
305 struct Capture *cap) { 307 struct Capture *cap) {
306 int l = capture_to_close(cap); 308 int l = capture_to_close(L, cap);
307 const char *res; 309 const char *res;
308 cap->capture[l].len = s - cap->capture[l].init; /* close capture */ 310 cap->capture[l].len = s - cap->capture[l].init; /* close capture */
309 if ((res = match(s, p+1, cap)) == NULL) /* match failed? */ 311 if ((res = match(L, s, p+1, cap)) == NULL) /* match failed? */
310 cap->capture[l].len = -1; /* undo capture */ 312 cap->capture[l].len = -1; /* undo capture */
311 return res; 313 return res;
312} 314}
313 315
314 316
315static const char *match_capture (const char *s, int level, 317static const char *match_capture (lua_State *L, const char *s, int level,
316 struct Capture *cap) { 318 struct Capture *cap) {
317 int l = check_cap(level, cap); 319 int l = check_cap(L, level, cap);
318 int len = cap->capture[l].len; 320 int len = cap->capture[l].len;
319 if (cap->src_end-s >= len && 321 if (cap->src_end-s >= len &&
320 memcmp(cap->capture[l].init, s, len) == 0) 322 memcmp(cap->capture[l].init, s, len) == 0)
@@ -323,23 +325,23 @@ static const char *match_capture (const char *s, int level,
323} 325}
324 326
325 327
326static const char *match (const char *s, const char *p, struct Capture *cap) { 328static const char *match (lua_State *L, const char *s, const char *p, struct Capture *cap) {
327 init: /* using goto's to optimize tail recursion */ 329 init: /* using goto's to optimize tail recursion */
328 switch (*p) { 330 switch (*p) {
329 case '(': /* start capture */ 331 case '(': /* start capture */
330 return start_capt(s, p, cap); 332 return start_capt(L, s, p, cap);
331 case ')': /* end capture */ 333 case ')': /* end capture */
332 return end_capt(s, p, cap); 334 return end_capt(L, s, p, cap);
333 case ESC: /* may be %[0-9] or %b */ 335 case ESC: /* may be %[0-9] or %b */
334 if (isdigit((unsigned char)(*(p+1)))) { /* capture? */ 336 if (isdigit((unsigned char)(*(p+1)))) { /* capture? */
335 s = match_capture(s, *(p+1), cap); 337 s = match_capture(L, s, *(p+1), cap);
336 if (s == NULL) return NULL; 338 if (s == NULL) return NULL;
337 p+=2; goto init; /* else return match(s, p+2, cap) */ 339 p+=2; goto init; /* else return match(L, s, p+2, cap) */
338 } 340 }
339 else if (*(p+1) == 'b') { /* balanced string? */ 341 else if (*(p+1) == 'b') { /* balanced string? */
340 s = matchbalance(s, p+2, cap); 342 s = matchbalance(L, s, p+2, cap);
341 if (s == NULL) return NULL; 343 if (s == NULL) return NULL;
342 p+=4; goto init; /* else return match(s, p+4, cap); */ 344 p+=4; goto init; /* else return match(L, s, p+4, cap); */
343 } 345 }
344 else goto dflt; /* case default */ 346 else goto dflt; /* case default */
345 case '\0': /* end of pattern */ 347 case '\0': /* end of pattern */
@@ -349,24 +351,24 @@ static const char *match (const char *s, const char *p, struct Capture *cap) {
349 return (s == cap->src_end) ? s : NULL; /* check end of string */ 351 return (s == cap->src_end) ? s : NULL; /* check end of string */
350 else goto dflt; 352 else goto dflt;
351 default: dflt: { /* it is a pattern item */ 353 default: dflt: { /* it is a pattern item */
352 const char *ep = luaI_classend(p); /* points to what is next */ 354 const char *ep = luaI_classend(L, p); /* points to what is next */
353 int m = s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep); 355 int m = s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep);
354 switch (*ep) { 356 switch (*ep) {
355 case '?': { /* optional */ 357 case '?': { /* optional */
356 const char *res; 358 const char *res;
357 if (m && ((res=match(s+1, ep+1, cap)) != NULL)) 359 if (m && ((res=match(L, s+1, ep+1, cap)) != NULL))
358 return res; 360 return res;
359 p=ep+1; goto init; /* else return match(s, ep+1, cap); */ 361 p=ep+1; goto init; /* else return match(L, s, ep+1, cap); */
360 } 362 }
361 case '*': /* 0 or more repetitions */ 363 case '*': /* 0 or more repetitions */
362 return max_expand(s, p, ep, cap); 364 return max_expand(L, s, p, ep, cap);
363 case '+': /* 1 or more repetitions */ 365 case '+': /* 1 or more repetitions */
364 return (m ? max_expand(s+1, p, ep, cap) : NULL); 366 return (m ? max_expand(L, s+1, p, ep, cap) : NULL);
365 case '-': /* 0 or more repetitions (minimum) */ 367 case '-': /* 0 or more repetitions (minimum) */
366 return min_expand(s, p, ep, cap); 368 return min_expand(L, s, p, ep, cap);
367 default: 369 default:
368 if (!m) return NULL; 370 if (!m) return NULL;
369 s++; p=ep; goto init; /* else return match(s+1, ep, cap); */ 371 s++; p=ep; goto init; /* else return match(L, s+1, ep, cap); */
370 } 372 }
371 } 373 }
372 } 374 }
@@ -394,19 +396,19 @@ static const char *memfind (const char *s1, long l1, const char *s2, long l2) {
394} 396}
395 397
396 398
397static void str_find (void) { 399static void str_find (lua_State *L) {
398 long l1, l2; 400 long l1, l2;
399 const char *s = luaL_check_lstr(1, &l1); 401 const char *s = luaL_check_lstr(L, 1, &l1);
400 const char *p = luaL_check_lstr(2, &l2); 402 const char *p = luaL_check_lstr(L, 2, &l2);
401 long init = posrelat(luaL_opt_long(3, 1), l1) - 1; 403 long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
402 struct Capture cap; 404 struct Capture cap;
403 luaL_arg_check(0 <= init && init <= l1, 3, "out of range"); 405 luaL_arg_check(L, 0 <= init && init <= l1, 3, "out of range");
404 if (lua_getparam(4) != LUA_NOOBJECT || 406 if (lua_getparam(L, 4) != LUA_NOOBJECT ||
405 strpbrk(p, SPECIALS) == NULL) { /* no special characters? */ 407 strpbrk(p, SPECIALS) == NULL) { /* no special characters? */
406 const char *s2 = memfind(s+init, l1, p, l2); 408 const char *s2 = memfind(s+init, l1, p, l2);
407 if (s2) { 409 if (s2) {
408 lua_pushnumber(s2-s+1); 410 lua_pushnumber(L, s2-s+1);
409 lua_pushnumber(s2-s+l2); 411 lua_pushnumber(L, s2-s+l2);
410 return; 412 return;
411 } 413 }
412 } 414 }
@@ -417,33 +419,33 @@ static void str_find (void) {
417 do { 419 do {
418 const char *res; 420 const char *res;
419 cap.level = 0; 421 cap.level = 0;
420 if ((res=match(s1, p, &cap)) != NULL) { 422 if ((res=match(L, s1, p, &cap)) != NULL) {
421 lua_pushnumber(s1-s+1); /* start */ 423 lua_pushnumber(L, s1-s+1); /* start */
422 lua_pushnumber(res-s); /* end */ 424 lua_pushnumber(L, res-s); /* end */
423 push_captures(&cap); 425 push_captures(L, &cap);
424 return; 426 return;
425 } 427 }
426 } while (s1++<cap.src_end && !anchor); 428 } while (s1++<cap.src_end && !anchor);
427 } 429 }
428 lua_pushnil(); /* not found */ 430 lua_pushnil(L); /* not found */
429} 431}
430 432
431 433
432static void add_s (lua_Object newp, struct Capture *cap) { 434static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) {
433 if (lua_isstring(newp)) { 435 if (lua_isstring(L, newp)) {
434 const char *news = lua_getstring(newp); 436 const char *news = lua_getstring(L, newp);
435 int l = lua_strlen(newp); 437 int l = lua_strlen(L, newp);
436 int i; 438 int i;
437 for (i=0; i<l; i++) { 439 for (i=0; i<l; i++) {
438 if (news[i] != ESC) 440 if (news[i] != ESC)
439 luaL_addchar(news[i]); 441 luaL_addchar(L, news[i]);
440 else { 442 else {
441 i++; /* skip ESC */ 443 i++; /* skip ESC */
442 if (!isdigit((unsigned char)news[i])) 444 if (!isdigit((unsigned char)news[i]))
443 luaL_addchar(news[i]); 445 luaL_addchar(L, news[i]);
444 else { 446 else {
445 int level = check_cap(news[i], cap); 447 int level = check_cap(L, news[i], cap);
446 addnchar(cap->capture[level].init, cap->capture[level].len); 448 addnchar(L, cap->capture[level].init, cap->capture[level].len);
447 } 449 }
448 } 450 }
449 } 451 }
@@ -452,91 +454,91 @@ static void add_s (lua_Object newp, struct Capture *cap) {
452 lua_Object res; 454 lua_Object res;
453 int status; 455 int status;
454 int oldbuff; 456 int oldbuff;
455 lua_beginblock(); 457 lua_beginblock(L);
456 push_captures(cap); 458 push_captures(L, cap);
457 /* function may use buffer, so save it and create a new one */ 459 /* function may use buffer, so save it and create a new one */
458 oldbuff = luaL_newbuffer(0); 460 oldbuff = luaL_newbuffer(L, 0);
459 status = lua_callfunction(newp); 461 status = lua_callfunction(L, newp);
460 /* restore old buffer */ 462 /* restore old buffer */
461 luaL_oldbuffer(oldbuff); 463 luaL_oldbuffer(L, oldbuff);
462 if (status != 0) { 464 if (status != 0) {
463 lua_endblock(); 465 lua_endblock(L);
464 lua_error(NULL); 466 lua_error(L, NULL);
465 } 467 }
466 res = lua_getresult(1); 468 res = lua_getresult(L, 1);
467 if (lua_isstring(res)) 469 if (lua_isstring(L, res))
468 addnchar(lua_getstring(res), lua_strlen(res)); 470 addnchar(L, lua_getstring(L, res), lua_strlen(L, res));
469 lua_endblock(); 471 lua_endblock(L);
470 } 472 }
471} 473}
472 474
473 475
474static void str_gsub (void) { 476static void str_gsub (lua_State *L) {
475 long srcl; 477 long srcl;
476 const char *src = luaL_check_lstr(1, &srcl); 478 const char *src = luaL_check_lstr(L, 1, &srcl);
477 const char *p = luaL_check_string(2); 479 const char *p = luaL_check_string(L, 2);
478 lua_Object newp = lua_getparam(3); 480 lua_Object newp = lua_getparam(L, 3);
479 int max_s = luaL_opt_int(4, srcl+1); 481 int max_s = luaL_opt_int(L, 4, srcl+1);
480 int anchor = (*p == '^') ? (p++, 1) : 0; 482 int anchor = (*p == '^') ? (p++, 1) : 0;
481 int n = 0; 483 int n = 0;
482 struct Capture cap; 484 struct Capture cap;
483 luaL_arg_check(lua_isstring(newp) || lua_isfunction(newp), 3, 485 luaL_arg_check(L, lua_isstring(L, newp) || lua_isfunction(L, newp), 3,
484 "string or function expected"); 486 "string or function expected");
485 luaL_resetbuffer(); 487 luaL_resetbuffer(L);
486 cap.src_end = src+srcl; 488 cap.src_end = src+srcl;
487 while (n < max_s) { 489 while (n < max_s) {
488 const char *e; 490 const char *e;
489 cap.level = 0; 491 cap.level = 0;
490 e = match(src, p, &cap); 492 e = match(L, src, p, &cap);
491 if (e) { 493 if (e) {
492 n++; 494 n++;
493 add_s(newp, &cap); 495 add_s(L, newp, &cap);
494 } 496 }
495 if (e && e>src) /* non empty match? */ 497 if (e && e>src) /* non empty match? */
496 src = e; /* skip it */ 498 src = e; /* skip it */
497 else if (src < cap.src_end) 499 else if (src < cap.src_end)
498 luaL_addchar(*src++); 500 luaL_addchar(L, *src++);
499 else break; 501 else break;
500 if (anchor) break; 502 if (anchor) break;
501 } 503 }
502 addnchar(src, cap.src_end-src); 504 addnchar(L, src, cap.src_end-src);
503 closeandpush(); 505 closeandpush(L);
504 lua_pushnumber(n); /* number of substitutions */ 506 lua_pushnumber(L, n); /* number of substitutions */
505} 507}
506 508
507/* }====================================================== */ 509/* }====================================================== */
508 510
509 511
510static void luaI_addquoted (int arg) { 512static void luaI_addquoted (lua_State *L, int arg) {
511 long l; 513 long l;
512 const char *s = luaL_check_lstr(arg, &l); 514 const char *s = luaL_check_lstr(L, arg, &l);
513 luaL_addchar('"'); 515 luaL_addchar(L, '"');
514 while (l--) { 516 while (l--) {
515 switch (*s) { 517 switch (*s) {
516 case '"': case '\\': case '\n': 518 case '"': case '\\': case '\n':
517 luaL_addchar('\\'); 519 luaL_addchar(L, '\\');
518 luaL_addchar(*s); 520 luaL_addchar(L, *s);
519 break; 521 break;
520 case '\0': addnchar("\\000", 4); break; 522 case '\0': addnchar(L, "\\000", 4); break;
521 default: luaL_addchar(*s); 523 default: luaL_addchar(L, *s);
522 } 524 }
523 s++; 525 s++;
524 } 526 }
525 luaL_addchar('"'); 527 luaL_addchar(L, '"');
526} 528}
527 529
528/* maximum size of each format specification (such as '%-099.99d') */ 530/* maximum size of each format specification (such as '%-099.99d') */
529#define MAX_FORMAT 20 /* arbitrary limit */ 531#define MAX_FORMAT 20 /* arbitrary limit */
530 532
531static void str_format (void) { 533static void str_format (lua_State *L) {
532 int arg = 1; 534 int arg = 1;
533 const char *strfrmt = luaL_check_string(arg); 535 const char *strfrmt = luaL_check_string(L, arg);
534 luaL_resetbuffer(); 536 luaL_resetbuffer(L);
535 while (*strfrmt) { 537 while (*strfrmt) {
536 if (*strfrmt != '%') 538 if (*strfrmt != '%')
537 luaL_addchar(*strfrmt++); 539 luaL_addchar(L, *strfrmt++);
538 else if (*++strfrmt == '%') 540 else if (*++strfrmt == '%')
539 luaL_addchar(*strfrmt++); /* %% */ 541 luaL_addchar(L, *strfrmt++); /* %% */
540 else { /* format item */ 542 else { /* format item */
541 struct Capture cap; 543 struct Capture cap;
542 char form[MAX_FORMAT]; /* to store the format ('%...') */ 544 char form[MAX_FORMAT]; /* to store the format ('%...') */
@@ -550,33 +552,33 @@ static void str_format (void) {
550 arg++; 552 arg++;
551 cap.src_end = strfrmt+strlen(strfrmt)+1; 553 cap.src_end = strfrmt+strlen(strfrmt)+1;
552 cap.level = 0; 554 cap.level = 0;
553 strfrmt = match(initf, "[-+ #0]*(%d*)%.?(%d*)", &cap); 555 strfrmt = match(L, initf, "[-+ #0]*(%d*)%.?(%d*)", &cap);
554 if (cap.capture[0].len > 2 || cap.capture[1].len > 2 || /* < 100? */ 556 if (cap.capture[0].len > 2 || cap.capture[1].len > 2 || /* < 100? */
555 strfrmt-initf > MAX_FORMAT-2) 557 strfrmt-initf > MAX_FORMAT-2)
556 lua_error("invalid format (width or precision too long)"); 558 lua_error(L, "invalid format (width or precision too long)");
557 strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */ 559 strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
558 form[strfrmt-initf+2] = 0; 560 form[strfrmt-initf+2] = 0;
559 buff = luaL_openspace(512); /* 512 > soid luaI_addquot99.99f', -1e308) */ 561 buff = luaL_openspace(L, 512); /* 512 > len(format('%99.99f', -1e308)) */
560 switch (*strfrmt++) { 562 switch (*strfrmt++) {
561 case 'c': case 'd': case 'i': 563 case 'c': case 'd': case 'i':
562 sprintf(buff, form, luaL_check_int(arg)); 564 sprintf(buff, form, luaL_check_int(L, arg));
563 break; 565 break;
564 case 'o': case 'u': case 'x': case 'X': 566 case 'o': case 'u': case 'x': case 'X':
565 sprintf(buff, form, (unsigned int)luaL_check_number(arg)); 567 sprintf(buff, form, (unsigned int)luaL_check_number(L, arg));
566 break; 568 break;
567 case 'e': case 'E': case 'f': case 'g': case 'G': 569 case 'e': case 'E': case 'f': case 'g': case 'G':
568 sprintf(buff, form, luaL_check_number(arg)); 570 sprintf(buff, form, luaL_check_number(L, arg));
569 break; 571 break;
570 case 'q': 572 case 'q':
571 luaI_addquoted(arg); 573 luaI_addquoted(L, arg);
572 continue; /* skip the "addsize" at the end */ 574 continue; /* skip the "addsize" at the end */
573 case 's': { 575 case 's': {
574 long l; 576 long l;
575 const char *s = luaL_check_lstr(arg, &l); 577 const char *s = luaL_check_lstr(L, arg, &l);
576 if (cap.capture[1].len == 0 && l >= 100) { 578 if (cap.capture[1].len == 0 && l >= 100) {
577 /* no precision and string is too big to be formatted; 579 /* no precision and string is too big to be formatted;
578 keep original string */ 580 keep original string */
579 addnchar(s, l); 581 addnchar(L, s, l);
580 continue; /* skip the "addsize" at the end */ 582 continue; /* skip the "addsize" at the end */
581 } 583 }
582 else { 584 else {
@@ -585,12 +587,12 @@ static void str_format (void) {
585 } 587 }
586 } 588 }
587 default: /* also treat cases 'pnLlh' */ 589 default: /* also treat cases 'pnLlh' */
588 lua_error("invalid option in `format'"); 590 lua_error(L, "invalid option in `format'");
589 } 591 }
590 luaL_addsize(strlen(buff)); 592 luaL_addsize(L, strlen(buff));
591 } 593 }
592 } 594 }
593 closeandpush(); /* push the result */ 595 closeandpush(L); /* push the result */
594} 596}
595 597
596 598
@@ -612,7 +614,7 @@ static const struct luaL_reg strlib[] = {
612/* 614/*
613** Open string library 615** Open string library
614*/ 616*/
615void strlib_open (void) 617void lua_strlibopen (lua_State *L)
616{ 618{
617 luaL_openlib(strlib, (sizeof(strlib)/sizeof(strlib[0]))); 619 luaL_openlib(L, strlib, (sizeof(strlib)/sizeof(strlib[0])));
618} 620}
diff --git a/ltable.c b/ltable.c
index eceff082..8a207b80 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.28 1999/10/26 10:53:40 roberto Exp roberto $ 2** $Id: ltable.c,v 1.29 1999/11/10 15:39:35 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*/
@@ -18,6 +18,8 @@
18*/ 18*/
19 19
20 20
21#define LUA_REENTRANT
22
21#include "lauxlib.h" 23#include "lauxlib.h"
22#include "lmem.h" 24#include "lmem.h"
23#include "lobject.h" 25#include "lobject.h"
@@ -26,7 +28,7 @@
26#include "lua.h" 28#include "lua.h"
27 29
28 30
29#define gcsize(n) numblocks(n*2, sizeof(Hash)) 31#define gcsize(L, n) numblocks(L, n*2, sizeof(Hash))
30 32
31 33
32 34
@@ -38,7 +40,7 @@
38** returns the `main' position of an element in a table (that is, the index 40** returns the `main' position of an element in a table (that is, the index
39** of its hash value) 41** of its hash value)
40*/ 42*/
41Node *luaH_mainposition (const Hash *t, const TObject *key) { 43Node *luaH_mainposition (lua_State *L, const Hash *t, const TObject *key) {
42 unsigned long h; 44 unsigned long h;
43 switch (ttype(key)) { 45 switch (ttype(key)) {
44 case LUA_T_NUMBER: 46 case LUA_T_NUMBER:
@@ -48,27 +50,27 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) {
48 h = tsvalue(key)->hash; 50 h = tsvalue(key)->hash;
49 break; 51 break;
50 case LUA_T_ARRAY: 52 case LUA_T_ARRAY:
51 h = IntPoint(avalue(key)); 53 h = IntPoint(L, avalue(key));
52 break; 54 break;
53 case LUA_T_PROTO: 55 case LUA_T_PROTO:
54 h = IntPoint(tfvalue(key)); 56 h = IntPoint(L, tfvalue(key));
55 break; 57 break;
56 case LUA_T_CPROTO: 58 case LUA_T_CPROTO:
57 h = IntPoint(fvalue(key)); 59 h = IntPoint(L, fvalue(key));
58 break; 60 break;
59 case LUA_T_CLOSURE: 61 case LUA_T_CLOSURE:
60 h = IntPoint(clvalue(key)); 62 h = IntPoint(L, clvalue(key));
61 break; 63 break;
62 default: 64 default:
63 lua_error("unexpected type to index table"); 65 lua_error(L, "unexpected type to index table");
64 h = 0; /* to avoid warnings */ 66 h = 0; /* to avoid warnings */
65 } 67 }
66 return &t->node[h%(unsigned int)t->size]; 68 return &t->node[h%(unsigned int)t->size];
67} 69}
68 70
69 71
70const TObject *luaH_get (const Hash *t, const TObject *key) { 72const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
71 Node *n = luaH_mainposition(t, key); 73 Node *n = luaH_mainposition(L, t, key);
72 do { 74 do {
73 if (luaO_equalObj(key, &n->key)) 75 if (luaO_equalObj(key, &n->key))
74 return &n->val; 76 return &n->val;
@@ -78,16 +80,16 @@ const TObject *luaH_get (const Hash *t, const TObject *key) {
78} 80}
79 81
80 82
81int luaH_pos (const Hash *t, const TObject *key) { 83int luaH_pos (lua_State *L, const Hash *t, const TObject *key) {
82 const TObject *v = luaH_get(t, key); 84 const TObject *v = luaH_get(L, t, key);
83 return (v == &luaO_nilobject) ? -1 : /* key not found */ 85 return (v == &luaO_nilobject) ? -1 : /* key not found */
84 ((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node); 86 ((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node);
85} 87}
86 88
87 89
88 90
89static Node *hashnodecreate (int nhash) { 91static Node *hashnodecreate (lua_State *L, int nhash) {
90 Node *v = luaM_newvector(nhash, Node); 92 Node *v = luaM_newvector(L, nhash, Node);
91 int i; 93 int i;
92 for (i=0; i<nhash; i++) { 94 for (i=0; i<nhash; i++) {
93 ttype(&v[i].key) = ttype(&v[i].val) = LUA_T_NIL; 95 ttype(&v[i].key) = ttype(&v[i].val) = LUA_T_NIL;
@@ -97,17 +99,17 @@ static Node *hashnodecreate (int nhash) {
97} 99}
98 100
99 101
100static void setnodevector (Hash *t, int size) { 102static void setnodevector (lua_State *L, Hash *t, int size) {
101 t->node = hashnodecreate(size); 103 t->node = hashnodecreate(L, size);
102 t->size = size; 104 t->size = size;
103 t->firstfree = &t->node[size-1]; /* first free position to be used */ 105 t->firstfree = &t->node[size-1]; /* first free position to be used */
104 L->nblocks += gcsize(size); 106 L->nblocks += gcsize(L, size);
105} 107}
106 108
107 109
108Hash *luaH_new (int size) { 110Hash *luaH_new (lua_State *L, int size) {
109 Hash *t = luaM_new(Hash); 111 Hash *t = luaM_new(L, Hash);
110 setnodevector(t, luaO_redimension(size+1)); 112 setnodevector(L, t, luaO_redimension(L, size+1));
111 t->htag = TagDefault; 113 t->htag = TagDefault;
112 t->next = L->roottable; 114 t->next = L->roottable;
113 L->roottable = t; 115 L->roottable = t;
@@ -116,14 +118,14 @@ Hash *luaH_new (int size) {
116} 118}
117 119
118 120
119void luaH_free (Hash *t) { 121void luaH_free (lua_State *L, Hash *t) {
120 L->nblocks -= gcsize(t->size); 122 L->nblocks -= gcsize(L, t->size);
121 luaM_free(t->node); 123 luaM_free(L, t->node);
122 luaM_free(t); 124 luaM_free(L, t);
123} 125}
124 126
125 127
126static int newsize (const Hash *t) { 128static int newsize (lua_State *L, const Hash *t) {
127 Node *v = t->node; 129 Node *v = t->node;
128 int size = t->size; 130 int size = t->size;
129 int realuse = 0; 131 int realuse = 0;
@@ -132,7 +134,7 @@ static int newsize (const Hash *t) {
132 if (ttype(&v[i].val) != LUA_T_NIL) 134 if (ttype(&v[i].val) != LUA_T_NIL)
133 realuse++; 135 realuse++;
134 } 136 }
135 return luaO_redimension(realuse*2); 137 return luaO_redimension(L, realuse*2);
136} 138}
137 139
138 140
@@ -141,19 +143,19 @@ static int newsize (const Hash *t) {
141** main position is free, to avoid needless collisions. In the second stage, 143** main position is free, to avoid needless collisions. In the second stage,
142** we insert the other elements. 144** we insert the other elements.
143*/ 145*/
144static void rehash (Hash *t) { 146static void rehash (lua_State *L, Hash *t) {
145 int oldsize = t->size; 147 int oldsize = t->size;
146 Node *nold = t->node; 148 Node *nold = t->node;
147 int i; 149 int i;
148 L->nblocks -= gcsize(oldsize); 150 L->nblocks -= gcsize(L, oldsize);
149 setnodevector(t, newsize(t)); /* create new array of nodes */ 151 setnodevector(L, t, newsize(L, t)); /* create new array of nodes */
150 /* first loop; set only elements that can go in their main positions */ 152 /* first loop; set only elements that can go in their main positions */
151 for (i=0; i<oldsize; i++) { 153 for (i=0; i<oldsize; i++) {
152 Node *old = nold+i; 154 Node *old = nold+i;
153 if (ttype(&old->val) == LUA_T_NIL) 155 if (ttype(&old->val) == LUA_T_NIL)
154 old->next = NULL; /* `remove' it for next loop */ 156 old->next = NULL; /* `remove' it for next loop */
155 else { 157 else {
156 Node *mp = luaH_mainposition(t, &old->key); /* new main position */ 158 Node *mp = luaH_mainposition(L, t, &old->key); /* new main position */
157 if (ttype(&mp->key) == LUA_T_NIL) { /* is it empty? */ 159 if (ttype(&mp->key) == LUA_T_NIL) { /* is it empty? */
158 mp->key = old->key; /* put element there */ 160 mp->key = old->key; /* put element there */
159 mp->val = old->val; 161 mp->val = old->val;
@@ -180,7 +182,7 @@ static void rehash (Hash *t) {
180 } while (ttype(&t->firstfree->key) != LUA_T_NIL); 182 } while (ttype(&t->firstfree->key) != LUA_T_NIL);
181 } 183 }
182 } 184 }
183 luaM_free(nold); /* free old array */ 185 luaM_free(L, nold); /* free old array */
184} 186}
185 187
186 188
@@ -197,8 +199,8 @@ static void rehash (Hash *t) {
197** pair; therefore, even when `val' points to an element of this table 199** pair; therefore, even when `val' points to an element of this table
198** (this happens when we use `luaH_move'), there is no problem. 200** (this happens when we use `luaH_move'), there is no problem.
199*/ 201*/
200void luaH_set (Hash *t, const TObject *key, const TObject *val) { 202void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
201 Node *mp = luaH_mainposition(t, key); 203 Node *mp = luaH_mainposition(L, t, key);
202 Node *n = mp; 204 Node *n = mp;
203 do { /* check whether `key' is somewhere in the chain */ 205 do { /* check whether `key' is somewhere in the chain */
204 if (luaO_equalObj(key, &n->key)) { 206 if (luaO_equalObj(key, &n->key)) {
@@ -213,7 +215,7 @@ void luaH_set (Hash *t, const TObject *key, const TObject *val) {
213 n = t->firstfree; /* get a free place */ 215 n = t->firstfree; /* get a free place */
214 /* is colliding node out of its main position? (can only happens if 216 /* is colliding node out of its main position? (can only happens if
215 its position if after "firstfree") */ 217 its position if after "firstfree") */
216 if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) { 218 if (mp > n && (othern=luaH_mainposition(L, t, &mp->key)) != mp) {
217 /* yes; move colliding node into free position */ 219 /* yes; move colliding node into free position */
218 while (othern->next != mp) othern = othern->next; /* find previous */ 220 while (othern->next != mp) othern = othern->next; /* find previous */
219 othern->next = n; /* redo the chain with `n' in place of `mp' */ 221 othern->next = n; /* redo the chain with `n' in place of `mp' */
@@ -235,22 +237,22 @@ void luaH_set (Hash *t, const TObject *key, const TObject *val) {
235 else if (t->firstfree == t->node) break; /* cannot decrement from here */ 237 else if (t->firstfree == t->node) break; /* cannot decrement from here */
236 else (t->firstfree)--; 238 else (t->firstfree)--;
237 } 239 }
238 rehash(t); /* no more free places */ 240 rehash(L, t); /* no more free places */
239} 241}
240 242
241 243
242void luaH_setint (Hash *t, int key, const TObject *val) { 244void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) {
243 TObject index; 245 TObject index;
244 ttype(&index) = LUA_T_NUMBER; 246 ttype(&index) = LUA_T_NUMBER;
245 nvalue(&index) = key; 247 nvalue(&index) = key;
246 luaH_set(t, &index, val); 248 luaH_set(L, t, &index, val);
247} 249}
248 250
249 251
250const TObject *luaH_getint (const Hash *t, int key) { 252const TObject *luaH_getint (lua_State *L, const Hash *t, int key) {
251 TObject index; 253 TObject index;
252 ttype(&index) = LUA_T_NUMBER; 254 ttype(&index) = LUA_T_NUMBER;
253 nvalue(&index) = key; 255 nvalue(&index) = key;
254 return luaH_get(t, &index); 256 return luaH_get(L, t, &index);
255} 257}
256 258
diff --git a/ltable.h b/ltable.h
index e1d567f0..25a08d00 100644
--- a/ltable.h
+++ b/ltable.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.h,v 1.14 1999/10/14 19:13:31 roberto Exp roberto $ 2** $Id: ltable.h,v 1.15 1999/10/26 10:53:40 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*/
@@ -10,23 +10,23 @@
10#include "lobject.h" 10#include "lobject.h"
11 11
12 12
13#define node(t,i) (&(t)->node[i]) 13#define node(L, t,i) (&(t)->node[i])
14#define key(n) (&(n)->key) 14#define key(L, n) (&(n)->key)
15#define val(n) (&(n)->val) 15#define val(L, n) (&(n)->val)
16 16
17#define luaH_move(t,from,to) (luaH_setint(t, to, luaH_getint(t, from))) 17#define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getint(L, t, from)))
18 18
19Hash *luaH_new (int nhash); 19Hash *luaH_new (lua_State *L, int nhash);
20void luaH_free (Hash *t); 20void luaH_free (lua_State *L, Hash *t);
21const TObject *luaH_get (const Hash *t, const TObject *key); 21const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key);
22void luaH_set (Hash *t, const TObject *key, const TObject *val); 22void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val);
23int luaH_pos (const Hash *t, const TObject *r); 23int luaH_pos (lua_State *L, const Hash *t, const TObject *r);
24void luaH_setint (Hash *t, int key, const TObject *val); 24void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val);
25const TObject *luaH_getint (const Hash *t, int key); 25const TObject *luaH_getint (lua_State *L, const Hash *t, int key);
26unsigned long luaH_hash (const TObject *key); 26unsigned long luaH_hash (lua_State *L, const TObject *key);
27 27
28/* exported only for debugging */ 28/* exported only for debugging */
29Node *luaH_mainposition (const Hash *t, const TObject *key); 29Node *luaH_mainposition (lua_State *L, const Hash *t, const TObject *key);
30 30
31 31
32#endif 32#endif
diff --git a/ltm.c b/ltm.c
index 37ef9fef..63d9f578 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.27 1999/09/20 14:57:29 roberto Exp roberto $ 2** $Id: ltm.c,v 1.28 1999/10/04 17:51:04 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*/
@@ -8,6 +8,8 @@
8#include <stdio.h> 8#include <stdio.h>
9#include <string.h> 9#include <string.h>
10 10
11#define LUA_REENTRANT
12
11#include "lauxlib.h" 13#include "lauxlib.h"
12#include "lmem.h" 14#include "lmem.h"
13#include "lobject.h" 15#include "lobject.h"
@@ -22,10 +24,10 @@ const char *const luaT_eventname[] = { /* ORDER IM */
22}; 24};
23 25
24 26
25static int luaI_checkevent (const char *name, const char *const list[]) { 27static int luaI_checkevent (lua_State *L, const char *name, const char *const list[]) {
26 int e = luaL_findstring(name, list); 28 int e = luaL_findstring(name, list);
27 if (e < 0) 29 if (e < 0)
28 luaL_verror("`%.50s' is not a valid event name", name); 30 luaL_verror(L, "`%.50s' is not a valid event name", name);
29 return e; 31 return e;
30} 32}
31 33
@@ -54,48 +56,48 @@ int luaT_validevent (int t, int e) { /* ORDER LUA_T */
54} 56}
55 57
56 58
57static void init_entry (int tag) { 59static void init_entry (lua_State *L, int tag) {
58 int i; 60 int i;
59 for (i=0; i<IM_N; i++) 61 for (i=0; i<IM_N; i++)
60 ttype(luaT_getim(tag, i)) = LUA_T_NIL; 62 ttype(luaT_getim(L, tag, i)) = LUA_T_NIL;
61} 63}
62 64
63 65
64void luaT_init (void) { 66void luaT_init (lua_State *L) {
65 int t; 67 int t;
66 L->last_tag = -(NUM_TAGS-1); 68 L->last_tag = -(NUM_TAGS-1);
67 luaM_growvector(L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT); 69 luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
68 for (t=L->last_tag; t<=0; t++) 70 for (t=L->last_tag; t<=0; t++)
69 init_entry(t); 71 init_entry(L, t);
70} 72}
71 73
72 74
73int lua_newtag (void) { 75int lua_newtag (lua_State *L) {
74 --L->last_tag; 76 --L->last_tag;
75 luaM_growvector(L->IMtable, -(L->last_tag), 1, struct IM, arrEM, MAX_INT); 77 luaM_growvector(L, L->IMtable, -(L->last_tag), 1, struct IM, arrEM, MAX_INT);
76 init_entry(L->last_tag); 78 init_entry(L, L->last_tag);
77 return L->last_tag; 79 return L->last_tag;
78} 80}
79 81
80 82
81static void checktag (int tag) { 83static void checktag (lua_State *L, int tag) {
82 if (!(L->last_tag <= tag && tag <= 0)) 84 if (!(L->last_tag <= tag && tag <= 0))
83 luaL_verror("%d is not a valid tag", tag); 85 luaL_verror(L, "%d is not a valid tag", tag);
84} 86}
85 87
86void luaT_realtag (int tag) { 88void luaT_realtag (lua_State *L, int tag) {
87 if (!(L->last_tag <= tag && tag < LUA_T_NIL)) 89 if (!(L->last_tag <= tag && tag < LUA_T_NIL))
88 luaL_verror("tag %d was not created by `newtag'", tag); 90 luaL_verror(L, "tag %d was not created by `newtag'", tag);
89} 91}
90 92
91 93
92int lua_copytagmethods (int tagto, int tagfrom) { 94int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
93 int e; 95 int e;
94 checktag(tagto); 96 checktag(L, tagto);
95 checktag(tagfrom); 97 checktag(L, tagfrom);
96 for (e=0; e<IM_N; e++) { 98 for (e=0; e<IM_N; e++) {
97 if (luaT_validevent(tagto, e)) 99 if (luaT_validevent(tagto, e))
98 *luaT_getim(tagto, e) = *luaT_getim(tagfrom, e); 100 *luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
99 } 101 }
100 return tagto; 102 return tagto;
101} 103}
@@ -115,7 +117,7 @@ int luaT_effectivetag (const TObject *o) {
115#ifdef DEBUG 117#ifdef DEBUG
116 case LUA_T_PMARK: case LUA_T_CMARK: 118 case LUA_T_PMARK: case LUA_T_CMARK:
117 case LUA_T_CLMARK: case LUA_T_LINE: 119 case LUA_T_CLMARK: case LUA_T_LINE:
118 LUA_INTERNALERROR("invalid type"); 120 LUA_INTERNALERROR(L, "invalid type");
119#endif 121#endif
120 default: 122 default:
121 return t; 123 return t;
@@ -123,37 +125,37 @@ int luaT_effectivetag (const TObject *o) {
123} 125}
124 126
125 127
126const TObject *luaT_gettagmethod (int t, const char *event) { 128const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
127 int e = luaI_checkevent(event, luaT_eventname); 129 int e = luaI_checkevent(L, event, luaT_eventname);
128 checktag(t); 130 checktag(L, t);
129 if (luaT_validevent(t, e)) 131 if (luaT_validevent(t, e))
130 return luaT_getim(t,e); 132 return luaT_getim(L, t,e);
131 else 133 else
132 return &luaO_nilobject; 134 return &luaO_nilobject;
133} 135}
134 136
135 137
136void luaT_settagmethod (int t, const char *event, TObject *func) { 138void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
137 TObject temp; 139 TObject temp;
138 int e = luaI_checkevent(event, luaT_eventname); 140 int e = luaI_checkevent(L, event, luaT_eventname);
139 checktag(t); 141 checktag(L, t);
140 if (!luaT_validevent(t, e)) 142 if (!luaT_validevent(t, e))
141 luaL_verror("cannot change tag method `%.20s' for type `%.20s'%.20s", 143 luaL_verror(L, "cannot change tag method `%.20s' for type `%.20s'%.20s",
142 luaT_eventname[e], luaO_typenames[-t], 144 luaT_eventname[e], luaO_typenames[-t],
143 (t == LUA_T_ARRAY || t == LUA_T_USERDATA) ? " with default tag" 145 (t == LUA_T_ARRAY || t == LUA_T_USERDATA) ? " with default tag"
144 : ""); 146 : "");
145 temp = *func; 147 temp = *func;
146 *func = *luaT_getim(t,e); 148 *func = *luaT_getim(L, t,e);
147 *luaT_getim(t, e) = temp; 149 *luaT_getim(L, t, e) = temp;
148} 150}
149 151
150 152
151const char *luaT_travtagmethods (int (*fn)(TObject *)) { /* ORDER IM */ 153const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *)) { /* ORDER IM */
152 int e; 154 int e;
153 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { 155 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
154 int t; 156 int t;
155 for (t=0; t>=L->last_tag; t--) 157 for (t=0; t>=L->last_tag; t--)
156 if (fn(luaT_getim(t,e))) 158 if (fn(L, luaT_getim(L, t,e)))
157 return luaT_eventname[e]; 159 return luaT_eventname[e];
158 } 160 }
159 return NULL; 161 return NULL;
diff --git a/ltm.h b/ltm.h
index dda28a5a..fde74b84 100644
--- a/ltm.h
+++ b/ltm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.h,v 1.6 1999/08/16 20:52:00 roberto Exp roberto $ 2** $Id: ltm.h,v 1.7 1999/09/20 14:57:29 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*/
@@ -44,18 +44,18 @@ struct IM {
44}; 44};
45 45
46 46
47#define luaT_getim(tag,event) (&L->IMtable[-(tag)].int_method[event]) 47#define luaT_getim(L,tag,event) (&L->IMtable[-(tag)].int_method[event])
48#define luaT_getimbyObj(o,e) (luaT_getim(luaT_effectivetag(o),(e))) 48#define luaT_getimbyObj(L,o,e) (luaT_getim(L, luaT_effectivetag(o),(e)))
49 49
50extern const char *const luaT_eventname[]; 50extern const char *const luaT_eventname[];
51 51
52 52
53void luaT_init (void); 53void luaT_init (lua_State *L);
54void luaT_realtag (int tag); 54void luaT_realtag (lua_State *L, int tag);
55int luaT_effectivetag (const TObject *o); 55int luaT_effectivetag (const TObject *o);
56void luaT_settagmethod (int t, const char *event, TObject *func); 56void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func);
57const TObject *luaT_gettagmethod (int t, const char *event); 57const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event);
58const char *luaT_travtagmethods (int (*fn)(TObject *)); 58const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *));
59 59
60int luaT_validevent (int t, int e); 60int luaT_validevent (int t, int e);
61 61
diff --git a/lua.c b/lua.c
index ac7d05e2..d3d716f2 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.25 1999/11/12 13:54:44 roberto Exp roberto $ 2** $Id: lua.c,v 1.26 1999/11/16 12:50:48 roberto Exp roberto $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -21,7 +21,6 @@
21static int isatty (int x) { return x==0; } /* assume stdin is a tty */ 21static int isatty (int x) { return x==0; } /* assume stdin is a tty */
22#endif 22#endif
23 23
24
25typedef void (*handler)(int); /* type for signal actions */ 24typedef void (*handler)(int); /* type for signal actions */
26 25
27static void laction (int i); 26static void laction (int i);
@@ -37,8 +36,8 @@ static handler lreset (void) {
37 36
38 37
39static void lstop (void) { 38static void lstop (void) {
40 lua_setlinehook(old_linehook); 39 lua_setlinehook(lua_state, old_linehook);
41 lua_setcallhook(old_callhook); 40 lua_setcallhook(lua_state, old_callhook);
42 lreset(); 41 lreset();
43 lua_error("interrupted!"); 42 lua_error("interrupted!");
44} 43}
@@ -48,15 +47,15 @@ static void laction (int i) {
48 (void)i; /* to avoid warnings */ 47 (void)i; /* to avoid warnings */
49 signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop, 48 signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
50 terminate process (default action) */ 49 terminate process (default action) */
51 old_linehook = lua_setlinehook((lua_LHFunction)lstop); 50 old_linehook = lua_setlinehook(lua_state, (lua_LHFunction)lstop);
52 old_callhook = lua_setcallhook((lua_CHFunction)lstop); 51 old_callhook = lua_setcallhook(lua_state, (lua_CHFunction)lstop);
53} 52}
54 53
55 54
56static int ldo (int (*f)(const char *), const char *name) { 55static int ldo (int (*f)(lua_State *L, const char *), const char *name) {
57 int res; 56 int res;
58 handler h = lreset(); 57 handler h = lreset();
59 res = f(name); /* dostring | dofile */ 58 res = f(lua_state, name); /* dostring | dofile */
60 signal(SIGINT, h); /* restore old action */ 59 signal(SIGINT, h); /* restore old action */
61 return res; 60 return res;
62} 61}
@@ -172,7 +171,7 @@ int main (int argc, char *argv[]) {
172 manual_input(0); 171 manual_input(0);
173 break; 172 break;
174 case 'd': 173 case 'd':
175 lua_setdebug(1); 174 lua_setdebug(lua_state, 1);
176 break; 175 break;
177 case 'v': 176 case 'v':
178 printf("%s %s\n(written by %s)\n", 177 printf("%s %s\n(written by %s)\n",
diff --git a/lua.h b/lua.h
index 12cbb626..aed37bad 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.36 1999/10/07 19:04:30 roberto Exp roberto $ 2** $Id: lua.h,v 1.37 1999/11/11 17:02:40 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil 4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
5** e-mail: lua@tecgraf.puc-rio.br 5** e-mail: lua@tecgraf.puc-rio.br
@@ -21,144 +21,199 @@
21#define LUA_NOREF (-2) 21#define LUA_NOREF (-2)
22#define LUA_REFNIL (-1) 22#define LUA_REFNIL (-1)
23 23
24#define LUA_ANYTAG (-1) 24#define LUA_ANYTAG (-1)
25 25
26typedef struct lua_State lua_State; 26typedef struct lua_State lua_State;
27extern lua_State *lua_state;
28 27
29typedef void (*lua_CFunction) (void); 28typedef void (*lua_CFunction) ();
30typedef unsigned int lua_Object; 29typedef unsigned int lua_Object;
31 30
32void lua_open (void); 31lua_State *lua_newstate (void);
33void lua_close (void); 32void lua_close (lua_State *L);
34lua_State *lua_setstate (lua_State *st);
35 33
36lua_Object lua_settagmethod (int tag, const char *event); 34lua_Object lua_settagmethod (lua_State *L, int tag, const char *event);
37 /* In: new method */ 35 /* In: new method */
38lua_Object lua_gettagmethod (int tag, const char *event); 36lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event);
39 37
40int lua_newtag (void); 38int lua_newtag (lua_State *L);
41int lua_copytagmethods (int tagto, int tagfrom); 39int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
42void lua_settag (int tag); /* In: object */ 40void lua_settag (lua_State *L, int tag); /* In: object */
43 41
44void lua_error (const char *s); 42void lua_error (lua_State *L, const char *s);
45int lua_dofile (const char *filename); 43int lua_dofile (lua_State *L, const char *filename);
46 /* Out: returns */ 44 /* Out: returns */
47int lua_dostring (const char *string); 45int lua_dostring (lua_State *L, const char *string);
48 /* Out: returns */ 46 /* Out: returns */
49int lua_dobuffer (const char *buff, int size, 47int lua_dobuffer (lua_State *L, const char *buff, int size,
50 const char *name); /* Out: returns */ 48 const char *name); /* Out: returns */
51int lua_callfunction (lua_Object f); 49int lua_callfunction (lua_State *L, lua_Object f);
52 /* In: parameters; Out: returns */ 50 /* In: parameters; Out: returns */
53 51
54void lua_beginblock (void); 52void lua_beginblock (lua_State *L);
55void lua_endblock (void); 53void lua_endblock (lua_State *L);
56 54
57lua_Object lua_lua2C (int number); 55lua_Object lua_lua2C (lua_State *L, int number);
58#define lua_getparam(_) lua_lua2C(_) 56#define lua_getparam lua_lua2C
59#define lua_getresult(_) lua_lua2C(_) 57#define lua_getresult lua_lua2C
60 58
61const char *lua_type (lua_Object object); 59const char *lua_type (lua_State *L, lua_Object object);
62 60
63int lua_isnil (lua_Object object); 61int lua_isnil (lua_State *L, lua_Object object);
64int lua_istable (lua_Object object); 62int lua_istable (lua_State *L, lua_Object object);
65int lua_isuserdata (lua_Object object); 63int lua_isuserdata (lua_State *L, lua_Object object);
66int lua_iscfunction (lua_Object object); 64int lua_iscfunction (lua_State *L, lua_Object object);
67int lua_isnumber (lua_Object object); 65int lua_isnumber (lua_State *L, lua_Object object);
68int lua_isstring (lua_Object object); 66int lua_isstring (lua_State *L, lua_Object object);
69int lua_isfunction (lua_Object object); 67int lua_isfunction (lua_State *L, lua_Object object);
70 68
71int lua_equalobj (lua_Object o1, lua_Object o2); 69int lua_equalobj (lua_State *L, lua_Object o1, lua_Object o2);
72 70
73double lua_getnumber (lua_Object object); 71double lua_getnumber (lua_State *L, lua_Object object);
74const char *lua_getstring (lua_Object object); 72const char *lua_getstring (lua_State *L, lua_Object object);
75long lua_strlen (lua_Object object); 73long lua_strlen (lua_State *L, lua_Object object);
76lua_CFunction lua_getcfunction (lua_Object object); 74lua_CFunction lua_getcfunction (lua_State *L, lua_Object object);
77void *lua_getuserdata (lua_Object object); 75void *lua_getuserdata (lua_State *L, lua_Object object);
78 76
79 77
80void lua_pushnil (void); 78void lua_pushnil (lua_State *L);
81void lua_pushnumber (double n); 79void lua_pushnumber (lua_State *L, double n);
82void lua_pushlstring (const char *s, long len); 80void lua_pushlstring (lua_State *L, const char *s, long len);
83void lua_pushstring (const char *s); 81void lua_pushstring (lua_State *L, const char *s);
84void lua_pushcclosure (lua_CFunction fn, int n); 82void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
85void lua_pushusertag (void *u, int tag); 83void lua_pushusertag (lua_State *L, void *u, int tag);
86void lua_pushobject (lua_Object object); 84void lua_pushobject (lua_State *L, lua_Object object);
87 85
88lua_Object lua_pop (void); 86lua_Object lua_pop (lua_State *L);
89 87
90lua_Object lua_getglobal (const char *name); 88lua_Object lua_getglobal (lua_State *L, const char *name);
91lua_Object lua_rawgetglobal (const char *name); 89lua_Object lua_rawgetglobal (lua_State *L, const char *name);
92void lua_setglobal (const char *name); /* In: value */ 90void lua_setglobal (lua_State *L, const char *name); /* In: value */
93void lua_rawsetglobal (const char *name); /* In: value */ 91void lua_rawsetglobal (lua_State *L, const char *name);/* In: value */
94 92
95void lua_settable (void); /* In: table, index, value */ 93void lua_settable (lua_State *L); /* In: table, index, value */
96void lua_rawsettable (void); /* In: table, index, value */ 94void lua_rawsettable (lua_State *L); /* In: table, index, value */
97lua_Object lua_gettable (void); /* In: table, index */ 95lua_Object lua_gettable (lua_State *L); /* In: table, index */
98lua_Object lua_rawgettable (void); /* In: table, index */ 96lua_Object lua_rawgettable (lua_State *L); /* In: table, index */
99 97
100int lua_tag (lua_Object object); 98int lua_tag (lua_State *L, lua_Object object);
101 99
102const char *lua_nextvar (const char *varname); /* Out: value */ 100const char *lua_nextvar (lua_State *L, const char *varname); /* Out: value */
103int lua_next (lua_Object o, int i); 101int lua_next (lua_State *L, lua_Object o, int i);
104 /* Out: ref, value */ 102 /* Out: ref, value */
105 103
106int lua_ref (int lock); /* In: value */ 104int lua_ref (lua_State *L, int lock); /* In: value */
107lua_Object lua_getref (int ref); 105lua_Object lua_getref (lua_State *L, int ref);
108void lua_unref (int ref); 106void lua_unref (lua_State *L, int ref);
109 107
110lua_Object lua_createtable (void); 108lua_Object lua_createtable (lua_State *L);
111 109
112long lua_collectgarbage (long limit); 110long lua_collectgarbage (lua_State *L, long limit);
113 111
114 112
115/* =============================================================== */ 113lua_Object lua_seterrormethod (lua_State *L); /* In: new method */
116/* some useful macros/functions */
117 114
118#define lua_call(name) lua_callfunction(lua_getglobal(name)) 115lua_State *lua_setstate (lua_State *st);
119 116
120#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
121 117
122#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l)) 118/*
119** ===============================================================
120** some useful macros
121** ===============================================================
122*/
123 123
124#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n)) 124#ifdef LUA_REENTRANT
125 125
126#define lua_pushuserdata(u) lua_pushusertag(u, 0) 126#define lua_call(L,name) lua_callfunction(L, lua_getglobal(L, name))
127#define lua_pushref(L,ref) lua_pushobject(L, lua_getref(L, ref))
128#define lua_refobject(L,o,l) (lua_pushobject(L, o), lua_ref(L, l))
129#define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n))
130#define lua_pushuserdata(L,u) lua_pushusertag(L, u, 0)
131#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
132#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
127 133
128#define lua_pushcfunction(f) lua_pushcclosure(f, 0) 134#else
129 135
136#define lua_call(name) lua_callfunction(lua_getglobal(name))
137#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
138#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))
139#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n))
140#define lua_pushuserdata(u) lua_pushusertag(u, 0)
141#define lua_pushcfunction(f) lua_pushcclosure(f, 0)
130#define lua_clonetag(t) lua_copytagmethods(lua_newtag(), (t)) 142#define lua_clonetag(t) lua_copytagmethods(lua_newtag(), (t))
131 143
132lua_Object lua_seterrormethod (void); /* In: new method */ 144#endif
133
134/* ==========================================================================
135** for compatibility with old versions. Avoid using these macros/functions
136** If your program does need any of these, define LUA_COMPAT2_5
137*/
138
139
140#ifdef LUA_COMPAT2_5
141
142
143 145
144#define lua_storeglobal lua_setglobal
145 146
146#define lua_lockobject(o) lua_refobject(o,1)
147#define lua_lock() lua_ref(1)
148#define lua_getlocked lua_getref
149#define lua_pushlocked lua_pushref
150#define lua_unlock lua_unref
151 147
152#define lua_pushliteral(o) lua_pushstring(o) 148#ifndef LUA_REENTRANT
149/*
150** ===============================================================
151** Macros for single-state use
152** ===============================================================
153*/
153 154
154#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_gettable()) 155extern lua_State *lua_state;
155#define lua_getfield(o,f) (lua_pushobject(o), lua_pushstring(f), lua_gettable())
156 156
157#define lua_getsubscript lua_gettable 157#define lua_open() ((void)(lua_state?0:(lua_state=lua_newstate())))
158#define lua_storesubscript lua_settable 158
159#define lua_close() (lua_close)(lua_state)
160#define lua_setstate(st) (lua_setstate)(lua_state, st)
161#define lua_settagmethod(tag,event) (lua_settagmethod)(lua_state, tag,event)
162#define lua_gettagmethod(tag,event) (lua_gettagmethod)(lua_state, tag,event)
163#define lua_newtag() (lua_newtag)(lua_state)
164#define lua_copytagmethods(tagto,tagfrom) \
165 (lua_copytagmethods)(lua_state, tagto,tagfrom)
166#define lua_settag(tag) (lua_settag)(lua_state, tag)
167#define lua_error(s) (lua_error)(lua_state, s)
168#define lua_dofile(filename) (lua_dofile)(lua_state, filename)
169#define lua_dostring(string) (lua_dostring)(lua_state, string)
170#define lua_callfunction(f) (lua_callfunction)(lua_state, f)
171#define lua_beginblock() (lua_beginblock)(lua_state)
172#define lua_endblock() (lua_endblock)(lua_state)
173#define lua_lua2C(number) (lua_lua2C)(lua_state, number)
174#define lua_type(object) (lua_type)(lua_state, object)
175#define lua_isnil(object) (lua_isnil)(lua_state, object)
176#define lua_istable(object) (lua_istable)(lua_state, object)
177#define lua_isuserdata(object) (lua_isuserdata)(lua_state, object)
178#define lua_iscfunction(object) (lua_iscfunction)(lua_state, object)
179#define lua_isnumber(object) (lua_isnumber)(lua_state, object)
180#define lua_isstring(object) (lua_isstring)(lua_state, object)
181#define lua_isfunction(object) (lua_isfunction)(lua_state, object)
182#define lua_equalobj(o1,o2) (lua_equalobj)(lua_state, o1,o2)
183#define lua_getnumber(object) (lua_getnumber)(lua_state, object)
184#define lua_getstring(object) (lua_getstring)(lua_state, object)
185#define lua_strlen(object) (lua_strlen)(lua_state, object)
186#define lua_getcfunction(object) (lua_getcfunction)(lua_state, object)
187#define lua_getuserdata(object) (lua_getuserdata)(lua_state, object)
188#define lua_pushnil() (lua_pushnil)(lua_state)
189#define lua_pushnumber(n) (lua_pushnumber)(lua_state, n)
190#define lua_pushlstring(s,len) (lua_pushlstring)(lua_state, s,len)
191#define lua_pushstring(s) (lua_pushstring)(lua_state, s)
192#define lua_pushcclosure(fn,n) (lua_pushcclosure)(lua_state, fn,n)
193#define lua_pushusertag(u,tag) (lua_pushusertag)(lua_state, u,tag)
194#define lua_pushobject(object) (lua_pushobject)(lua_state, object)
195#define lua_pop() (lua_pop)(lua_state)
196#define lua_getglobal(name) (lua_getglobal)(lua_state, name)
197#define lua_rawgetglobal(name) (lua_rawgetglobal)(lua_state, name)
198#define lua_setglobal(name) (lua_setglobal)(lua_state, name)
199#define lua_rawsetglobal(name) (lua_rawsetglobal)(lua_state, name)
200#define lua_settable() (lua_settable)(lua_state)
201#define lua_rawsettable() (lua_rawsettable)(lua_state)
202#define lua_gettable() (lua_gettable)(lua_state)
203#define lua_rawgettable() (lua_rawgettable)(lua_state)
204#define lua_tag(object) (lua_tag)(lua_state, object)
205#define lua_nextvar(varname) (lua_nextvar)(lua_state, varname)
206#define lua_next(o,i) (lua_next)(lua_state, o,i)
207#define lua_ref(lock) (lua_ref)(lua_state, lock)
208#define lua_getref(ref) (lua_getref)(lua_state, ref)
209#define lua_unref(ref) (lua_unref)(lua_state, ref)
210#define lua_createtable() (lua_createtable)(lua_state)
211#define lua_collectgarbage(limit) (lua_collectgarbage)(lua_state, limit)
212#define lua_seterrormethod() (lua_seterrormethod)(lua_state)
159 213
160#endif 214#endif
161 215
216
162#endif 217#endif
163 218
164 219
diff --git a/luadebug.h b/luadebug.h
index 53deba78..29c5a39c 100644
--- a/luadebug.h
+++ b/luadebug.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: luadebug.h,v 1.6 1999/03/04 21:17:26 roberto Exp roberto $ 2** $Id: luadebug.h,v 1.7 1999/08/16 20:52:00 roberto Exp roberto $
3** Debugging API 3** Debugging API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -13,23 +13,23 @@
13 13
14typedef lua_Object lua_Function; 14typedef lua_Object lua_Function;
15 15
16typedef void (*lua_LHFunction) (int line); 16typedef void (*lua_LHFunction) (lua_State *L, int line);
17typedef void (*lua_CHFunction) (lua_Function func, const char *file, int line); 17typedef void (*lua_CHFunction) (lua_State *L, lua_Function func, const char *file, int line);
18 18
19lua_Function lua_stackedfunction (int level); 19lua_Function lua_stackedfunction (lua_State *L, int level);
20void lua_funcinfo (lua_Object func, const char **source, int *linedefined); 20void lua_funcinfo (lua_State *L, lua_Object func, const char **source, int *linedefined);
21int lua_currentline (lua_Function func); 21int lua_currentline (lua_State *L, lua_Function func);
22const char *lua_getobjname (lua_Object o, const char **name); 22const char *lua_getobjname (lua_State *L, lua_Object o, const char **name);
23 23
24lua_Object lua_getlocal (lua_Function func, int local_number, 24lua_Object lua_getlocal (lua_State *L, lua_Function func, int local_number,
25 const char **name); 25 const char **name);
26int lua_setlocal (lua_Function func, int local_number); 26int lua_setlocal (lua_State *L, lua_Function func, int local_number);
27 27
28int lua_nups (lua_Function func); 28int lua_nups (lua_State *L, lua_Function func);
29 29
30lua_LHFunction lua_setlinehook (lua_LHFunction func); 30lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func);
31lua_CHFunction lua_setcallhook (lua_CHFunction func); 31lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func);
32int lua_setdebug (int debug); 32int lua_setdebug (lua_State *L, int debug);
33 33
34 34
35#endif 35#endif
diff --git a/lualib.h b/lualib.h
index f60e72d1..8956e19a 100644
--- a/lualib.h
+++ b/lualib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lualib.h,v 1.6 1999/05/05 19:23:11 roberto Exp roberto $ 2** $Id: lualib.h,v 1.7 1999/08/16 20:52:00 roberto Exp roberto $
3** Lua standard libraries 3** Lua standard libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,27 +10,36 @@
10 10
11#include "lua.h" 11#include "lua.h"
12 12
13void lua_iolibopen (void); 13void lua_iolibopen (lua_State *L);
14void lua_strlibopen (void); 14void lua_strlibopen (lua_State *L);
15void lua_mathlibopen (void); 15void lua_mathlibopen (lua_State *L);
16void lua_dblibopen (void); 16void lua_dblibopen (lua_State *L);
17 17
18 18
19void lua_userinit (void); 19void lua_userinit (lua_State *L);
20 20
21 21
22/* To keep compatibility with old versions */ 22/*
23** ===============================================================
24** Macros for single-state use
25** ===============================================================
26*/
27
28#ifndef LUA_REENTRANT
23 29
24#define iolib_open lua_iolibopen 30#define lua_iolibopen() (lua_iolibopen)(lua_state)
25#define strlib_open lua_strlibopen 31#define lua_strlibopen() (lua_strlibopen)(lua_state)
26#define mathlib_open lua_mathlibopen 32#define lua_mathlibopen() (lua_mathlibopen)(lua_state)
33#define lua_dblibopen() (lua_dblibopen)(lua_state)
34#define lua_userinit() (lua_userinit)(lua_state)
35
36#endif
27 37
28 38
29 39
30/* Auxiliary functions (private) */ 40/* Auxiliary functions (private) */
31 41
32const char *luaI_classend (const char *p); 42const char *luaI_classend (lua_State *L, const char *p);
33int luaI_singlematch (int c, const char *p, const char *ep); 43int luaI_singlematch (int c, const char *p, const char *ep);
34 44
35#endif 45#endif
36
diff --git a/lundump.c b/lundump.c
index 9f6e3dc3..e1405ddf 100644
--- a/lundump.c
+++ b/lundump.c
@@ -1,9 +1,11 @@
1/* 1/*
2** $Id: lundump.c,v 1.13 1999/08/16 20:52:00 roberto Exp roberto $ 2** $Id: lundump.c,v 1.14 1999/09/06 13:55:09 roberto Exp roberto $
3** load bytecodes from files 3** load bytecodes from files
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7#define LUA_REENTRANT
8
7#include <stdio.h> 9#include <stdio.h>
8#include <string.h> 10#include <string.h>
9#include "lauxlib.h" 11#include "lauxlib.h"
@@ -13,195 +15,195 @@
13#include "lstring.h" 15#include "lstring.h"
14#include "lundump.h" 16#include "lundump.h"
15 17
16#define LoadBlock(b,size,Z) ezread(Z,b,size) 18#define LoadBlock(L, b,size,Z) ezread(L, Z,b,size)
17 19
18static void unexpectedEOZ (ZIO* Z) 20static void unexpectedEOZ (lua_State *L, ZIO* Z)
19{ 21{
20 luaL_verror("unexpected end of file in %s",zname(Z)); 22 luaL_verror(L, "unexpected end of file in %s",zname(Z));
21} 23}
22 24
23static int ezgetc (ZIO* Z) 25static int ezgetc (lua_State *L, ZIO* Z)
24{ 26{
25 int c=zgetc(Z); 27 int c=zgetc(Z);
26 if (c==EOZ) unexpectedEOZ(Z); 28 if (c==EOZ) unexpectedEOZ(L, Z);
27 return c; 29 return c;
28} 30}
29 31
30static void ezread (ZIO* Z, void* b, int n) 32static void ezread (lua_State *L, ZIO* Z, void* b, int n)
31{ 33{
32 int r=zread(Z,b,n); 34 int r=zread(Z,b,n);
33 if (r!=0) unexpectedEOZ(Z); 35 if (r!=0) unexpectedEOZ(L, Z);
34} 36}
35 37
36static unsigned int LoadWord (ZIO* Z) 38static unsigned int LoadWord (lua_State *L, ZIO* Z)
37{ 39{
38 unsigned int hi=ezgetc(Z); 40 unsigned int hi=ezgetc(L, Z);
39 unsigned int lo=ezgetc(Z); 41 unsigned int lo=ezgetc(L, Z);
40 return (hi<<8)|lo; 42 return (hi<<8)|lo;
41} 43}
42 44
43static unsigned long LoadLong (ZIO* Z) 45static unsigned long LoadLong (lua_State *L, ZIO* Z)
44{ 46{
45 unsigned long hi=LoadWord(Z); 47 unsigned long hi=LoadWord(L, Z);
46 unsigned long lo=LoadWord(Z); 48 unsigned long lo=LoadWord(L, Z);
47 return (hi<<16)|lo; 49 return (hi<<16)|lo;
48} 50}
49 51
50/* 52/*
51* convert number from text 53* convert number from text
52*/ 54*/
53real luaU_str2d (const char* b, const char* where) 55real luaU_str2d (lua_State *L, const char* b, const char* where)
54{ 56{
55 real x; 57 real x;
56 if (!luaO_str2d(b, &x)) 58 if (!luaO_str2d(b, &x))
57 luaL_verror("cannot convert number '%s' in %s",b,where); 59 luaL_verror(L, "cannot convert number '%s' in %s",b,where);
58 return x; 60 return x;
59} 61}
60 62
61static real LoadNumber (ZIO* Z, int native) 63static real LoadNumber (lua_State *L, ZIO* Z, int native)
62{ 64{
63 real x; 65 real x;
64 if (native) 66 if (native)
65 { 67 {
66 LoadBlock(&x,sizeof(x),Z); 68 LoadBlock(L, &x,sizeof(x),Z);
67 return x; 69 return x;
68 } 70 }
69 else 71 else
70 { 72 {
71 char b[256]; 73 char b[256];
72 int size=ezgetc(Z); 74 int size=ezgetc(L, Z);
73 LoadBlock(b,size,Z); 75 LoadBlock(L, b,size,Z);
74 b[size]=0; 76 b[size]=0;
75 return luaU_str2d(b,zname(Z)); 77 return luaU_str2d(L, b,zname(Z));
76 } 78 }
77} 79}
78 80
79static int LoadInt (ZIO* Z, const char* message) 81static int LoadInt (lua_State *L, ZIO* Z, const char* message)
80{ 82{
81 unsigned long l=LoadLong(Z); 83 unsigned long l=LoadLong(L, Z);
82 unsigned int i=l; 84 unsigned int i=l;
83 if (i!=l) luaL_verror(message,l,zname(Z)); 85 if (i!=l) luaL_verror(L, message,l,zname(Z));
84 return i; 86 return i;
85} 87}
86 88
87#define PAD 5 /* two word operands plus opcode */ 89#define PAD 5 /* two word operands plus opcode */
88 90
89static Byte* LoadCode (ZIO* Z) 91static Byte* LoadCode (lua_State *L, ZIO* Z)
90{ 92{
91 int size=LoadInt(Z,"code too long (%ld bytes) in %s"); 93 int size=LoadInt(L, Z,"code too long (%ld bytes) in %s");
92 Byte* b=luaM_malloc(size+PAD); 94 Byte* b=luaM_malloc(L, size+PAD);
93 LoadBlock(b,size,Z); 95 LoadBlock(L, b,size,Z);
94 if (b[size-1]!=ENDCODE) luaL_verror("bad code in %s",zname(Z)); 96 if (b[size-1]!=ENDCODE) luaL_verror(L, "bad code in %s",zname(Z));
95 memset(b+size,ENDCODE,PAD); /* pad code for safety */ 97 memset(b+size,ENDCODE,PAD); /* pad code for safety */
96 return b; 98 return b;
97} 99}
98 100
99static TaggedString* LoadTString (ZIO* Z) 101static TaggedString* LoadTString (lua_State *L, ZIO* Z)
100{ 102{
101 long size=LoadLong(Z); 103 long size=LoadLong(L, Z);
102 if (size==0) 104 if (size==0)
103 return NULL; 105 return NULL;
104 else 106 else
105 { 107 {
106 char* s=luaL_openspace(size); 108 char* s=luaL_openspace(L, size);
107 LoadBlock(s,size,Z); 109 LoadBlock(L, s,size,Z);
108 return luaS_newlstr(s,size-1); 110 return luaS_newlstr(L, s,size-1);
109 } 111 }
110} 112}
111 113
112static void LoadLocals (TProtoFunc* tf, ZIO* Z) 114static void LoadLocals (lua_State *L, TProtoFunc* tf, ZIO* Z)
113{ 115{
114 int i,n=LoadInt(Z,"too many locals (%ld) in %s"); 116 int i,n=LoadInt(L, Z,"too many locals (%ld) in %s");
115 if (n==0) return; 117 if (n==0) return;
116 tf->locvars=luaM_newvector(n+1,LocVar); 118 tf->locvars=luaM_newvector(L, n+1,LocVar);
117 for (i=0; i<n; i++) 119 for (i=0; i<n; i++)
118 { 120 {
119 tf->locvars[i].line=LoadInt(Z,"too many lines (%ld) in %s"); 121 tf->locvars[i].line=LoadInt(L, Z,"too many lines (%ld) in %s");
120 tf->locvars[i].varname=LoadTString(Z); 122 tf->locvars[i].varname=LoadTString(L, Z);
121 } 123 }
122 tf->locvars[i].line=-1; /* flag end of vector */ 124 tf->locvars[i].line=-1; /* flag end of vector */
123 tf->locvars[i].varname=NULL; 125 tf->locvars[i].varname=NULL;
124} 126}
125 127
126static TProtoFunc* LoadFunction (ZIO* Z, int native); 128static TProtoFunc* LoadFunction (lua_State *L, ZIO* Z, int native);
127 129
128static void LoadConstants (TProtoFunc* tf, ZIO* Z, int native) 130static void LoadConstants (lua_State *L, TProtoFunc* tf, ZIO* Z, int native)
129{ 131{
130 int i,n=LoadInt(Z,"too many constants (%ld) in %s"); 132 int i,n=LoadInt(L, Z,"too many constants (%ld) in %s");
131 tf->nconsts=n; 133 tf->nconsts=n;
132 if (n==0) return; 134 if (n==0) return;
133 tf->consts=luaM_newvector(n,TObject); 135 tf->consts=luaM_newvector(L, n,TObject);
134 for (i=0; i<n; i++) 136 for (i=0; i<n; i++)
135 { 137 {
136 TObject* o=tf->consts+i; 138 TObject* o=tf->consts+i;
137 ttype(o)=-ezgetc(Z); /* ttype(o) is negative - ORDER LUA_T */ 139 ttype(o)=-ezgetc(L, Z); /* ttype(o) is negative - ORDER LUA_T */
138 switch (ttype(o)) 140 switch (ttype(o))
139 { 141 {
140 case LUA_T_NUMBER: 142 case LUA_T_NUMBER:
141 nvalue(o)=LoadNumber(Z,native); 143 nvalue(o)=LoadNumber(L, Z,native);
142 break; 144 break;
143 case LUA_T_STRING: 145 case LUA_T_STRING:
144 tsvalue(o)=LoadTString(Z); 146 tsvalue(o)=LoadTString(L, Z);
145 break; 147 break;
146 case LUA_T_PROTO: 148 case LUA_T_PROTO:
147 tfvalue(o)=LoadFunction(Z,native); 149 tfvalue(o)=LoadFunction(L, Z,native);
148 break; 150 break;
149 case LUA_T_NIL: 151 case LUA_T_NIL:
150 break; 152 break;
151 default: /* cannot happen */ 153 default: /* cannot happen */
152 luaU_badconstant("load",i,o,tf); 154 luaU_badconstant(L, "load",i,o,tf);
153 break; 155 break;
154 } 156 }
155 } 157 }
156} 158}
157 159
158static TProtoFunc* LoadFunction (ZIO* Z, int native) 160static TProtoFunc* LoadFunction (lua_State *L, ZIO* Z, int native)
159{ 161{
160 TProtoFunc* tf=luaF_newproto(); 162 TProtoFunc* tf=luaF_newproto(L);
161 tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s"); 163 tf->lineDefined=LoadInt(L, Z,"lineDefined too large (%ld) in %s");
162 tf->source=LoadTString(Z); 164 tf->source=LoadTString(L, Z);
163 if (tf->source==NULL) tf->source=luaS_new(zname(Z)); 165 if (tf->source==NULL) tf->source=luaS_new(L, zname(Z));
164 tf->code=LoadCode(Z); 166 tf->code=LoadCode(L, Z);
165 LoadLocals(tf,Z); 167 LoadLocals(L, tf,Z);
166 LoadConstants(tf,Z,native); 168 LoadConstants(L, tf,Z,native);
167 return tf; 169 return tf;
168} 170}
169 171
170static void LoadSignature (ZIO* Z) 172static void LoadSignature (lua_State *L, ZIO* Z)
171{ 173{
172 const char* s=SIGNATURE; 174 const char* s=SIGNATURE;
173 while (*s!=0 && ezgetc(Z)==*s) 175 while (*s!=0 && ezgetc(L, Z)==*s)
174 ++s; 176 ++s;
175 if (*s!=0) luaL_verror("bad signature in %s",zname(Z)); 177 if (*s!=0) luaL_verror(L, "bad signature in %s",zname(Z));
176} 178}
177 179
178static int LoadHeader (ZIO* Z) 180static int LoadHeader (lua_State *L, ZIO* Z)
179{ 181{
180 int version,sizeofR; 182 int version,sizeofR;
181 int native; 183 int native;
182 LoadSignature(Z); 184 LoadSignature(L, Z);
183 version=ezgetc(Z); 185 version=ezgetc(L, Z);
184 if (version>VERSION) 186 if (version>VERSION)
185 luaL_verror( 187 luaL_verror(L,
186 "%s too new: version=0x%02x; expected at most 0x%02x", 188 "%s too new: version=0x%02x; expected at most 0x%02x",
187 zname(Z),version,VERSION); 189 zname(Z),version,VERSION);
188 if (version<VERSION0) /* check last major change */ 190 if (version<VERSION0) /* check last major change */
189 luaL_verror( 191 luaL_verror(L,
190 "%s too old: version=0x%02x; expected at least 0x%02x", 192 "%s too old: version=0x%02x; expected at least 0x%02x",
191 zname(Z),version,VERSION0); 193 zname(Z),version,VERSION0);
192 sizeofR=ezgetc(Z); 194 sizeofR=ezgetc(L, Z);
193 native=(sizeofR!=0); 195 native=(sizeofR!=0);
194 if (native) /* test number representation */ 196 if (native) /* test number representation */
195 { 197 {
196 if (sizeofR!=sizeof(real)) 198 if (sizeofR!=sizeof(real))
197 luaL_verror("unknown number size in %s: read %d; expected %d", 199 luaL_verror(L, "unknown number size in %s: read %d; expected %d",
198 zname(Z),sizeofR,sizeof(real)); 200 zname(Z),sizeofR,sizeof(real));
199 else 201 else
200 { 202 {
201 real tf=TEST_NUMBER; 203 real tf=TEST_NUMBER;
202 real f=LoadNumber(Z,native); 204 real f=LoadNumber(L, Z,native);
203 if ((long)f!=(long)tf) 205 if ((long)f!=(long)tf)
204 luaL_verror("unknown number format in %s: " 206 luaL_verror(L, "unknown number format in %s: "
205 "read " NUMBER_FMT "; expected " NUMBER_FMT, 207 "read " NUMBER_FMT "; expected " NUMBER_FMT,
206 zname(Z),f,tf); 208 zname(Z),f,tf);
207 } 209 }
@@ -209,31 +211,31 @@ static int LoadHeader (ZIO* Z)
209 return native; 211 return native;
210} 212}
211 213
212static TProtoFunc* LoadChunk (ZIO* Z) 214static TProtoFunc* LoadChunk (lua_State *L, ZIO* Z)
213{ 215{
214 return LoadFunction(Z,LoadHeader(Z)); 216 return LoadFunction(L, Z,LoadHeader(L, Z));
215} 217}
216 218
217/* 219/*
218** load one chunk from a file or buffer 220** load one chunk from a file or buffer
219** return main if ok and NULL at EOF 221** return main if ok and NULL at EOF
220*/ 222*/
221TProtoFunc* luaU_undump1 (ZIO* Z) 223TProtoFunc* luaU_undump1 (lua_State *L, ZIO* Z)
222{ 224{
223 int c=zgetc(Z); 225 int c=zgetc(Z);
224 if (c==ID_CHUNK) 226 if (c==ID_CHUNK)
225 return LoadChunk(Z); 227 return LoadChunk(L, Z);
226 else if (c!=EOZ) 228 else if (c!=EOZ)
227 luaL_verror("%s is not a Lua binary file",zname(Z)); 229 luaL_verror(L, "%s is not a Lua binary file",zname(Z));
228 return NULL; 230 return NULL;
229} 231}
230 232
231/* 233/*
232* handle constants that cannot happen 234* handle constants that cannot happen
233*/ 235*/
234void luaU_badconstant (const char* s, int i, const TObject* o, TProtoFunc* tf) 236void luaU_badconstant (lua_State *L, const char* s, int i, const TObject* o, TProtoFunc* tf)
235{ 237{
236 int t=ttype(o); 238 int t=ttype(o);
237 const char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t]; 239 const char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t];
238 luaL_verror("cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC); 240 luaL_verror(L, "cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
239} 241}
diff --git a/lundump.h b/lundump.h
index 915464b0..7e310a49 100644
--- a/lundump.h
+++ b/lundump.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.h,v 1.9 1999/07/08 12:43:23 roberto Exp roberto $ 2** $Id: lundump.h,v 1.10 1999/08/16 20:52:00 roberto Exp roberto $
3** load pre-compiled Lua chunks 3** load pre-compiled Lua chunks
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,10 +10,10 @@
10#include "lobject.h" 10#include "lobject.h"
11#include "lzio.h" 11#include "lzio.h"
12 12
13TProtoFunc* luaU_undump1 (ZIO* Z); /* load one chunk */ 13TProtoFunc* luaU_undump1 (lua_State *L, ZIO* Z); /* load one chunk */
14void luaU_badconstant (const char* s, int i, const TObject* o, TProtoFunc* tf); 14void luaU_badconstant (lua_State *L, const char* s, int i, const TObject* o, TProtoFunc* tf);
15 /* handle cases that cannot happen */ 15 /* handle cases that cannot happen */
16double luaU_str2d (const char* b, const char* where); 16double luaU_str2d (lua_State *L, const char* b, const char* where);
17 /* convert number from text */ 17 /* convert number from text */
18 18
19/* definitions for headers of binary files */ 19/* definitions for headers of binary files */
diff --git a/lvm.c b/lvm.c
index 5d6ab840..05840302 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.64 1999/10/14 19:46:57 roberto Exp roberto $ 2** $Id: lvm.c,v 1.65 1999/11/04 17:22:26 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*/
@@ -9,6 +9,8 @@
9#include <stdlib.h> 9#include <stdlib.h>
10#include <string.h> 10#include <string.h>
11 11
12#define LUA_REENTRANT
13
12#include "lauxlib.h" 14#include "lauxlib.h"
13#include "ldo.h" 15#include "ldo.h"
14#include "lfunc.h" 16#include "lfunc.h"
@@ -27,7 +29,7 @@
27#endif 29#endif
28 30
29 31
30#define highbyte(x) ((x)<<8) 32#define highbyte(L, x) ((x)<<8)
31 33
32 34
33/* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */ 35/* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */
@@ -35,13 +37,13 @@
35 37
36 38
37 39
38static TaggedString *strconc (const TaggedString *l, const TaggedString *r) { 40static TaggedString *strconc (lua_State *L, const TaggedString *l, const TaggedString *r) {
39 long nl = l->u.s.len; 41 long nl = l->u.s.len;
40 long nr = r->u.s.len; 42 long nr = r->u.s.len;
41 char *buffer = luaL_openspace(nl+nr); 43 char *buffer = luaL_openspace(L, nl+nr);
42 memcpy(buffer, l->str, nl); 44 memcpy(buffer, l->str, nl);
43 memcpy(buffer+nl, r->str, nr); 45 memcpy(buffer+nl, r->str, nr);
44 return luaS_newlstr(buffer, nl+nr); 46 return luaS_newlstr(L, buffer, nl+nr);
45} 47}
46 48
47 49
@@ -57,31 +59,31 @@ int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
57} 59}
58 60
59 61
60int luaV_tostring (TObject *obj) { /* LUA_NUMBER */ 62int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
61 if (ttype(obj) != LUA_T_NUMBER) 63 if (ttype(obj) != LUA_T_NUMBER)
62 return 1; 64 return 1;
63 else { 65 else {
64 char s[32]; /* 16 digits, signal, point and \0 (+ some extra...) */ 66 char s[32]; /* 16 digits, signal, point and \0 (+ some extra...) */
65 sprintf(s, "%.16g", (double)nvalue(obj)); 67 sprintf(s, "%.16g", (double)nvalue(obj));
66 tsvalue(obj) = luaS_new(s); 68 tsvalue(obj) = luaS_new(L, s);
67 ttype(obj) = LUA_T_STRING; 69 ttype(obj) = LUA_T_STRING;
68 return 0; 70 return 0;
69 } 71 }
70} 72}
71 73
72 74
73void luaV_setn (Hash *t, int val) { 75void luaV_setn (lua_State *L, Hash *t, int val) {
74 TObject index, value; 76 TObject index, value;
75 ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new("n"); 77 ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new(L, "n");
76 ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val; 78 ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val;
77 luaH_set(t, &index, &value); 79 luaH_set(L, t, &index, &value);
78} 80}
79 81
80 82
81void luaV_closure (int nelems) { 83void luaV_closure (lua_State *L, int nelems) {
82 if (nelems > 0) { 84 if (nelems > 0) {
83 struct Stack *S = &L->stack; 85 struct Stack *S = &L->stack;
84 Closure *c = luaF_newclosure(nelems); 86 Closure *c = luaF_newclosure(L, nelems);
85 c->consts[0] = *(S->top-1); 87 c->consts[0] = *(S->top-1);
86 memcpy(&c->consts[1], S->top-(nelems+1), nelems*sizeof(TObject)); 88 memcpy(&c->consts[1], S->top-(nelems+1), nelems*sizeof(TObject));
87 S->top -= nelems; 89 S->top -= nelems;
@@ -95,23 +97,23 @@ void luaV_closure (int nelems) {
95** Function to index a table. 97** Function to index a table.
96** Receives the table at top-2 and the index at top-1. 98** Receives the table at top-2 and the index at top-1.
97*/ 99*/
98void luaV_gettable (void) { 100void luaV_gettable (lua_State *L) {
99 TObject *table = L->stack.top-2; 101 TObject *table = L->stack.top-2;
100 const TObject *im; 102 const TObject *im;
101 if (ttype(table) != LUA_T_ARRAY) { /* not a table, get gettable method */ 103 if (ttype(table) != LUA_T_ARRAY) { /* not a table, get gettable method */
102 im = luaT_getimbyObj(table, IM_GETTABLE); 104 im = luaT_getimbyObj(L, table, IM_GETTABLE);
103 if (ttype(im) == LUA_T_NIL) 105 if (ttype(im) == LUA_T_NIL)
104 lua_error("indexed expression not a table"); 106 lua_error(L, "indexed expression not a table");
105 } 107 }
106 else { /* object is a table... */ 108 else { /* object is a table... */
107 int tg = table->value.a->htag; 109 int tg = table->value.a->htag;
108 im = luaT_getim(tg, IM_GETTABLE); 110 im = luaT_getim(L, tg, IM_GETTABLE);
109 if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */ 111 if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
110 const TObject *h = luaH_get(avalue(table), table+1); 112 const TObject *h = luaH_get(L, avalue(table), table+1);
111 if (ttype(h) == LUA_T_NIL && 113 if (ttype(h) == LUA_T_NIL &&
112 (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)) { 114 (ttype(im=luaT_getim(L, tg, IM_INDEX)) != LUA_T_NIL)) {
113 /* result is nil and there is an "index" tag method */ 115 /* result is nil and there is an "index" tag method */
114 luaD_callTM(im, 2, 1); /* calls it */ 116 luaD_callTM(L, im, 2, 1); /* calls it */
115 } 117 }
116 else { 118 else {
117 L->stack.top--; 119 L->stack.top--;
@@ -122,25 +124,25 @@ void luaV_gettable (void) {
122 /* else it has a "gettable" method, go through to next command */ 124 /* else it has a "gettable" method, go through to next command */
123 } 125 }
124 /* object is not a table, or it has a "gettable" method */ 126 /* object is not a table, or it has a "gettable" method */
125 luaD_callTM(im, 2, 1); 127 luaD_callTM(L, im, 2, 1);
126} 128}
127 129
128 130
129/* 131/*
130** Receives table at *t, index at *(t+1) and value at top. 132** Receives table at *t, index at *(t+1) and value at top.
131*/ 133*/
132void luaV_settable (const TObject *t) { 134void luaV_settable (lua_State *L, const TObject *t) {
133 struct Stack *S = &L->stack; 135 struct Stack *S = &L->stack;
134 const TObject *im; 136 const TObject *im;
135 if (ttype(t) != LUA_T_ARRAY) { /* not a table, get "settable" method */ 137 if (ttype(t) != LUA_T_ARRAY) { /* not a table, get "settable" method */
136 im = luaT_getimbyObj(t, IM_SETTABLE); 138 im = luaT_getimbyObj(L, t, IM_SETTABLE);
137 if (ttype(im) == LUA_T_NIL) 139 if (ttype(im) == LUA_T_NIL)
138 lua_error("indexed expression not a table"); 140 lua_error(L, "indexed expression not a table");
139 } 141 }
140 else { /* object is a table... */ 142 else { /* object is a table... */
141 im = luaT_getim(avalue(t)->htag, IM_SETTABLE); 143 im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE);
142 if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */ 144 if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */
143 luaH_set(avalue(t), t+1, S->top-1); 145 luaH_set(L, avalue(t), t+1, S->top-1);
144 S->top--; /* pop value */ 146 S->top--; /* pop value */
145 return; 147 return;
146 } 148 }
@@ -152,35 +154,35 @@ void luaV_settable (const TObject *t) {
152 *(S->top) = *(t+1); 154 *(S->top) = *(t+1);
153 *(S->top-1) = *t; 155 *(S->top-1) = *t;
154 S->top += 2; /* WARNING: caller must assure stack space */ 156 S->top += 2; /* WARNING: caller must assure stack space */
155 luaD_callTM(im, 3, 0); 157 luaD_callTM(L, im, 3, 0);
156} 158}
157 159
158 160
159void luaV_rawsettable (const TObject *t) { 161void luaV_rawsettable (lua_State *L, const TObject *t) {
160 if (ttype(t) != LUA_T_ARRAY) 162 if (ttype(t) != LUA_T_ARRAY)
161 lua_error("indexed expression not a table"); 163 lua_error(L, "indexed expression not a table");
162 else { 164 else {
163 struct Stack *S = &L->stack; 165 struct Stack *S = &L->stack;
164 luaH_set(avalue(t), t+1, S->top-1); 166 luaH_set(L, avalue(t), t+1, S->top-1);
165 S->top -= 3; 167 S->top -= 3;
166 } 168 }
167} 169}
168 170
169 171
170void luaV_getglobal (GlobalVar *gv) { 172void luaV_getglobal (lua_State *L, GlobalVar *gv) {
171 /* WARNING: caller must assure stack space */ 173 /* WARNING: caller must assure stack space */
172 const TObject *value = &gv->value; 174 const TObject *value = &gv->value;
173 switch (ttype(value)) { 175 switch (ttype(value)) {
174 /* only userdata, tables and nil can have getglobal tag methods */ 176 /* only userdata, tables and nil can have getglobal tag methods */
175 case LUA_T_USERDATA: case LUA_T_ARRAY: case LUA_T_NIL: { 177 case LUA_T_USERDATA: case LUA_T_ARRAY: case LUA_T_NIL: {
176 TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL); 178 TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
177 if (ttype(im) != LUA_T_NIL) { /* is there a tag method? */ 179 if (ttype(im) != LUA_T_NIL) { /* is there a tag method? */
178 struct Stack *S = &L->stack; 180 struct Stack *S = &L->stack;
179 ttype(S->top) = LUA_T_STRING; 181 ttype(S->top) = LUA_T_STRING;
180 tsvalue(S->top) = gv->name; /* global name */ 182 tsvalue(S->top) = gv->name; /* global name */
181 S->top++; 183 S->top++;
182 *S->top++ = *value; 184 *S->top++ = *value;
183 luaD_callTM(im, 2, 1); 185 luaD_callTM(L, im, 2, 1);
184 return; 186 return;
185 } 187 }
186 /* else no tag method: go through to default behavior */ 188 /* else no tag method: go through to default behavior */
@@ -190,9 +192,9 @@ void luaV_getglobal (GlobalVar *gv) {
190} 192}
191 193
192 194
193void luaV_setglobal (GlobalVar *gv) { 195void luaV_setglobal (lua_State *L, GlobalVar *gv) {
194 const TObject *oldvalue = &gv->value; 196 const TObject *oldvalue = &gv->value;
195 const TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL); 197 const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
196 if (ttype(im) == LUA_T_NIL) /* is there a tag method? */ 198 if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
197 gv->value = *(--L->stack.top); 199 gv->value = *(--L->stack.top);
198 else { 200 else {
@@ -204,29 +206,29 @@ void luaV_setglobal (GlobalVar *gv) {
204 tsvalue(S->top-1) = gv->name; 206 tsvalue(S->top-1) = gv->name;
205 *S->top++ = *oldvalue; 207 *S->top++ = *oldvalue;
206 *S->top++ = newvalue; 208 *S->top++ = newvalue;
207 luaD_callTM(im, 3, 0); 209 luaD_callTM(L, im, 3, 0);
208 } 210 }
209} 211}
210 212
211 213
212static void call_binTM (IMS event, const char *msg) { 214static void call_binTM (lua_State *L, IMS event, const char *msg) {
213 /* try first operand */ 215 /* try first operand */
214 const TObject *im = luaT_getimbyObj(L->stack.top-2, event); 216 const TObject *im = luaT_getimbyObj(L, L->stack.top-2, event);
215 if (ttype(im) == LUA_T_NIL) { 217 if (ttype(im) == LUA_T_NIL) {
216 im = luaT_getimbyObj(L->stack.top-1, event); /* try second operand */ 218 im = luaT_getimbyObj(L, L->stack.top-1, event); /* try second operand */
217 if (ttype(im) == LUA_T_NIL) { 219 if (ttype(im) == LUA_T_NIL) {
218 im = luaT_getim(0, event); /* try a 'global' i.m. */ 220 im = luaT_getim(L, 0, event); /* try a 'global' i.m. */
219 if (ttype(im) == LUA_T_NIL) 221 if (ttype(im) == LUA_T_NIL)
220 lua_error(msg); 222 lua_error(L, msg);
221 } 223 }
222 } 224 }
223 lua_pushstring(luaT_eventname[event]); 225 lua_pushstring(L, luaT_eventname[event]);
224 luaD_callTM(im, 3, 1); 226 luaD_callTM(L, im, 3, 1);
225} 227}
226 228
227 229
228static void call_arith (IMS event) { 230static void call_arith (lua_State *L, IMS event) {
229 call_binTM(event, "unexpected type in arithmetic operation"); 231 call_binTM(L, event, "unexpected type in arithmetic operation");
230} 232}
231 233
232 234
@@ -246,7 +248,7 @@ static int luaV_strcomp (const char *l, long ll, const char *r, long lr) {
246 } 248 }
247} 249}
248 250
249void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal, 251void luaV_comparison (lua_State *L, lua_Type ttype_less, lua_Type ttype_equal,
250 lua_Type ttype_great, IMS op) { 252 lua_Type ttype_great, IMS op) {
251 struct Stack *S = &L->stack; 253 struct Stack *S = &L->stack;
252 const TObject *l = S->top-2; 254 const TObject *l = S->top-2;
@@ -258,7 +260,7 @@ void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal,
258 result = luaV_strcomp(svalue(l), tsvalue(l)->u.s.len, 260 result = luaV_strcomp(svalue(l), tsvalue(l)->u.s.len,
259 svalue(r), tsvalue(r)->u.s.len); 261 svalue(r), tsvalue(r)->u.s.len);
260 else { 262 else {
261 call_binTM(op, "unexpected type in comparison"); 263 call_binTM(L, op, "unexpected type in comparison");
262 return; 264 return;
263 } 265 }
264 S->top--; 266 S->top--;
@@ -268,24 +270,24 @@ void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal,
268} 270}
269 271
270 272
271void luaV_pack (StkId firstel, int nvararg, TObject *tab) { 273void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab) {
272 TObject *firstelem = L->stack.stack+firstel; 274 TObject *firstelem = L->stack.stack+firstel;
273 int i; 275 int i;
274 Hash *htab; 276 Hash *htab;
275 if (nvararg < 0) nvararg = 0; 277 if (nvararg < 0) nvararg = 0;
276 htab = avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */ 278 htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field 'n' */
277 ttype(tab) = LUA_T_ARRAY; 279 ttype(tab) = LUA_T_ARRAY;
278 for (i=0; i<nvararg; i++) 280 for (i=0; i<nvararg; i++)
279 luaH_setint(htab, i+1, firstelem+i); 281 luaH_setint(L, htab, i+1, firstelem+i);
280 luaV_setn(htab, nvararg); /* store counter in field "n" */ 282 luaV_setn(L, htab, nvararg); /* store counter in field "n" */
281} 283}
282 284
283 285
284static void adjust_varargs (StkId first_extra_arg) { 286static void adjust_varargs (lua_State *L, StkId first_extra_arg) {
285 TObject arg; 287 TObject arg;
286 luaV_pack(first_extra_arg, 288 luaV_pack(L, first_extra_arg,
287 (L->stack.top-L->stack.stack)-first_extra_arg, &arg); 289 (L->stack.top-L->stack.stack)-first_extra_arg, &arg);
288 luaD_adjusttop(first_extra_arg); 290 luaD_adjusttop(L, first_extra_arg);
289 *L->stack.top++ = arg; 291 *L->stack.top++ = arg;
290} 292}
291 293
@@ -296,18 +298,18 @@ static void adjust_varargs (StkId first_extra_arg) {
296** [stack+base,top). Returns n such that the the results are between 298** [stack+base,top). Returns n such that the the results are between
297** [stack+n,top). 299** [stack+n,top).
298*/ 300*/
299StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) { 301StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId base) {
300 struct Stack *S = &L->stack; /* to optimize */ 302 struct Stack *S = &L->stack; /* to optimize */
301 register const Byte *pc = tf->code; 303 register const Byte *pc = tf->code;
302 const TObject *consts = tf->consts; 304 const TObject *consts = tf->consts;
303 if (L->callhook) 305 if (L->callhook)
304 luaD_callHook(base, tf, 0); 306 luaD_callHook(L, base, tf, 0);
305 luaD_checkstack((*pc++)+EXTRA_STACK); 307 luaD_checkstack(L, (*pc++)+EXTRA_STACK);
306 if (*pc < ZEROVARARG) 308 if (*pc < ZEROVARARG)
307 luaD_adjusttop(base+*(pc++)); 309 luaD_adjusttop(L, base+*(pc++));
308 else { /* varargs */ 310 else { /* varargs */
309 luaC_checkGC(); 311 luaC_checkGC(L);
310 adjust_varargs(base+(*pc++)-ZEROVARARG); 312 adjust_varargs(L, base+(*pc++)-ZEROVARARG);
311 } 313 }
312 for (;;) { 314 for (;;) {
313 register int aux = 0; 315 register int aux = 0;
@@ -323,11 +325,11 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
323 goto ret; 325 goto ret;
324 326
325 case CALL: aux = *pc++; 327 case CALL: aux = *pc++;
326 luaD_calln(*pc++, aux); 328 luaD_calln(L, *pc++, aux);
327 break; 329 break;
328 330
329 case TAILCALL: aux = *pc++; 331 case TAILCALL: aux = *pc++;
330 luaD_calln(*pc++, MULT_RET); 332 luaD_calln(L, *pc++, MULT_RET);
331 base += aux; 333 base += aux;
332 goto ret; 334 goto ret;
333 335
@@ -341,21 +343,21 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
341 S->top -= aux; 343 S->top -= aux;
342 break; 344 break;
343 345
344 case PUSHNUMBERW: aux += highbyte(*pc++); 346 case PUSHNUMBERW: aux += highbyte(L, *pc++);
345 case PUSHNUMBER: aux += *pc++; 347 case PUSHNUMBER: aux += *pc++;
346 ttype(S->top) = LUA_T_NUMBER; 348 ttype(S->top) = LUA_T_NUMBER;
347 nvalue(S->top) = aux; 349 nvalue(S->top) = aux;
348 S->top++; 350 S->top++;
349 break; 351 break;
350 352
351 case PUSHNUMBERNEGW: aux += highbyte(*pc++); 353 case PUSHNUMBERNEGW: aux += highbyte(L, *pc++);
352 case PUSHNUMBERNEG: aux += *pc++; 354 case PUSHNUMBERNEG: aux += *pc++;
353 ttype(S->top) = LUA_T_NUMBER; 355 ttype(S->top) = LUA_T_NUMBER;
354 nvalue(S->top) = -aux; 356 nvalue(S->top) = -aux;
355 S->top++; 357 S->top++;
356 break; 358 break;
357 359
358 case PUSHCONSTANTW: aux += highbyte(*pc++); 360 case PUSHCONSTANTW: aux += highbyte(L, *pc++);
359 case PUSHCONSTANT: aux += *pc++; 361 case PUSHCONSTANT: aux += *pc++;
360 *S->top++ = consts[aux]; 362 *S->top++ = consts[aux];
361 break; 363 break;
@@ -368,35 +370,35 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
368 *S->top++ = *((S->stack+base) + aux); 370 *S->top++ = *((S->stack+base) + aux);
369 break; 371 break;
370 372
371 case GETGLOBALW: aux += highbyte(*pc++); 373 case GETGLOBALW: aux += highbyte(L, *pc++);
372 case GETGLOBAL: aux += *pc++; 374 case GETGLOBAL: aux += *pc++;
373 luaV_getglobal(tsvalue(&consts[aux])->u.s.gv); 375 luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv);
374 break; 376 break;
375 377
376 case GETTABLE: 378 case GETTABLE:
377 luaV_gettable(); 379 luaV_gettable(L);
378 break; 380 break;
379 381
380 case GETDOTTEDW: aux += highbyte(*pc++); 382 case GETDOTTEDW: aux += highbyte(L, *pc++);
381 case GETDOTTED: aux += *pc++; 383 case GETDOTTED: aux += *pc++;
382 *S->top++ = consts[aux]; 384 *S->top++ = consts[aux];
383 luaV_gettable(); 385 luaV_gettable(L);
384 break; 386 break;
385 387
386 case PUSHSELFW: aux += highbyte(*pc++); 388 case PUSHSELFW: aux += highbyte(L, *pc++);
387 case PUSHSELF: aux += *pc++; { 389 case PUSHSELF: aux += *pc++; {
388 TObject receiver; 390 TObject receiver;
389 receiver = *(S->top-1); 391 receiver = *(S->top-1);
390 *S->top++ = consts[aux]; 392 *S->top++ = consts[aux];
391 luaV_gettable(); 393 luaV_gettable(L);
392 *S->top++ = receiver; 394 *S->top++ = receiver;
393 break; 395 break;
394 } 396 }
395 397
396 case CREATEARRAYW: aux += highbyte(*pc++); 398 case CREATEARRAYW: aux += highbyte(L, *pc++);
397 case CREATEARRAY: aux += *pc++; 399 case CREATEARRAY: aux += *pc++;
398 luaC_checkGC(); 400 luaC_checkGC(L);
399 avalue(S->top) = luaH_new(aux); 401 avalue(S->top) = luaH_new(L, aux);
400 ttype(S->top) = LUA_T_ARRAY; 402 ttype(S->top) = LUA_T_ARRAY;
401 S->top++; 403 S->top++;
402 break; 404 break;
@@ -405,34 +407,34 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
405 *((S->stack+base) + aux) = *(--S->top); 407 *((S->stack+base) + aux) = *(--S->top);
406 break; 408 break;
407 409
408 case SETGLOBALW: aux += highbyte(*pc++); 410 case SETGLOBALW: aux += highbyte(L, *pc++);
409 case SETGLOBAL: aux += *pc++; 411 case SETGLOBAL: aux += *pc++;
410 luaV_setglobal(tsvalue(&consts[aux])->u.s.gv); 412 luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv);
411 break; 413 break;
412 414
413 case SETTABLEPOP: 415 case SETTABLEPOP:
414 luaV_settable(S->top-3); 416 luaV_settable(L, S->top-3);
415 S->top -= 2; /* pop table and index */ 417 S->top -= 2; /* pop table and index */
416 break; 418 break;
417 419
418 case SETTABLE: 420 case SETTABLE:
419 luaV_settable(S->top-3-(*pc++)); 421 luaV_settable(L, S->top-3-(*pc++));
420 break; 422 break;
421 423
422 case SETLISTW: aux += highbyte(*pc++); 424 case SETLISTW: aux += highbyte(L, *pc++);
423 case SETLIST: aux += *pc++; { 425 case SETLIST: aux += *pc++; {
424 int n = *(pc++); 426 int n = *(pc++);
425 Hash *arr = avalue(S->top-n-1); 427 Hash *arr = avalue(S->top-n-1);
426 aux *= LFIELDS_PER_FLUSH; 428 aux *= LFIELDS_PER_FLUSH;
427 for (; n; n--) 429 for (; n; n--)
428 luaH_setint(arr, n+aux, --S->top); 430 luaH_setint(L, arr, n+aux, --S->top);
429 break; 431 break;
430 } 432 }
431 433
432 case SETMAP: aux = *pc++; { 434 case SETMAP: aux = *pc++; {
433 Hash *arr = avalue(S->top-(2*aux)-3); 435 Hash *arr = avalue(S->top-(2*aux)-3);
434 do { 436 do {
435 luaH_set(arr, S->top-2, S->top-1); 437 luaH_set(L, arr, S->top-2, S->top-1);
436 S->top-=2; 438 S->top-=2;
437 } while (aux--); 439 } while (aux--);
438 break; 440 break;
@@ -449,26 +451,26 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
449 } 451 }
450 452
451 case LTOP: 453 case LTOP:
452 luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT); 454 luaV_comparison(L, LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
453 break; 455 break;
454 456
455 case LEOP: 457 case LEOP:
456 luaV_comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE); 458 luaV_comparison(L, LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
457 break; 459 break;
458 460
459 case GTOP: 461 case GTOP:
460 luaV_comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT); 462 luaV_comparison(L, LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
461 break; 463 break;
462 464
463 case GEOP: 465 case GEOP:
464 luaV_comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE); 466 luaV_comparison(L, LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
465 break; 467 break;
466 468
467 case ADDOP: { 469 case ADDOP: {
468 TObject *l = S->top-2; 470 TObject *l = S->top-2;
469 TObject *r = S->top-1; 471 TObject *r = S->top-1;
470 if (tonumber(r) || tonumber(l)) 472 if (tonumber(r) || tonumber(l))
471 call_arith(IM_ADD); 473 call_arith(L, IM_ADD);
472 else { 474 else {
473 nvalue(l) += nvalue(r); 475 nvalue(l) += nvalue(r);
474 --S->top; 476 --S->top;
@@ -480,7 +482,7 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
480 TObject *l = S->top-2; 482 TObject *l = S->top-2;
481 TObject *r = S->top-1; 483 TObject *r = S->top-1;
482 if (tonumber(r) || tonumber(l)) 484 if (tonumber(r) || tonumber(l))
483 call_arith(IM_SUB); 485 call_arith(L, IM_SUB);
484 else { 486 else {
485 nvalue(l) -= nvalue(r); 487 nvalue(l) -= nvalue(r);
486 --S->top; 488 --S->top;
@@ -492,7 +494,7 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
492 TObject *l = S->top-2; 494 TObject *l = S->top-2;
493 TObject *r = S->top-1; 495 TObject *r = S->top-1;
494 if (tonumber(r) || tonumber(l)) 496 if (tonumber(r) || tonumber(l))
495 call_arith(IM_MUL); 497 call_arith(L, IM_MUL);
496 else { 498 else {
497 nvalue(l) *= nvalue(r); 499 nvalue(l) *= nvalue(r);
498 --S->top; 500 --S->top;
@@ -504,7 +506,7 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
504 TObject *l = S->top-2; 506 TObject *l = S->top-2;
505 TObject *r = S->top-1; 507 TObject *r = S->top-1;
506 if (tonumber(r) || tonumber(l)) 508 if (tonumber(r) || tonumber(l))
507 call_arith(IM_DIV); 509 call_arith(L, IM_DIV);
508 else { 510 else {
509 nvalue(l) /= nvalue(r); 511 nvalue(l) /= nvalue(r);
510 --S->top; 512 --S->top;
@@ -513,19 +515,19 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
513 } 515 }
514 516
515 case POWOP: 517 case POWOP:
516 call_binTM(IM_POW, "undefined operation"); 518 call_binTM(L, IM_POW, "undefined operation");
517 break; 519 break;
518 520
519 case CONCOP: { 521 case CONCOP: {
520 TObject *l = S->top-2; 522 TObject *l = S->top-2;
521 TObject *r = S->top-1; 523 TObject *r = S->top-1;
522 if (tostring(l) || tostring(r)) 524 if (tostring(L, l) || tostring(L, r))
523 call_binTM(IM_CONCAT, "unexpected type for concatenation"); 525 call_binTM(L, IM_CONCAT, "unexpected type for concatenation");
524 else { 526 else {
525 tsvalue(l) = strconc(tsvalue(l), tsvalue(r)); 527 tsvalue(l) = strconc(L, tsvalue(l), tsvalue(r));
526 --S->top; 528 --S->top;
527 } 529 }
528 luaC_checkGC(); 530 luaC_checkGC(L);
529 break; 531 break;
530 } 532 }
531 533
@@ -533,7 +535,7 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
533 if (tonumber(S->top-1)) { 535 if (tonumber(S->top-1)) {
534 ttype(S->top) = LUA_T_NIL; 536 ttype(S->top) = LUA_T_NIL;
535 S->top++; 537 S->top++;
536 call_arith(IM_UNM); 538 call_arith(L, IM_UNM);
537 } 539 }
538 else 540 else
539 nvalue(S->top-1) = - nvalue(S->top-1); 541 nvalue(S->top-1) = - nvalue(S->top-1);
@@ -545,72 +547,72 @@ StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base) {
545 nvalue(S->top-1) = 1; 547 nvalue(S->top-1) = 1;
546 break; 548 break;
547 549
548 case ONTJMPW: aux += highbyte(*pc++); 550 case ONTJMPW: aux += highbyte(L, *pc++);
549 case ONTJMP: aux += *pc++; 551 case ONTJMP: aux += *pc++;
550 if (ttype(S->top-1) != LUA_T_NIL) pc += aux; 552 if (ttype(S->top-1) != LUA_T_NIL) pc += aux;
551 else S->top--; 553 else S->top--;
552 break; 554 break;
553 555
554 case ONFJMPW: aux += highbyte(*pc++); 556 case ONFJMPW: aux += highbyte(L, *pc++);
555 case ONFJMP: aux += *pc++; 557 case ONFJMP: aux += *pc++;
556 if (ttype(S->top-1) == LUA_T_NIL) pc += aux; 558 if (ttype(S->top-1) == LUA_T_NIL) pc += aux;
557 else S->top--; 559 else S->top--;
558 break; 560 break;
559 561
560 case JMPW: aux += highbyte(*pc++); 562 case JMPW: aux += highbyte(L, *pc++);
561 case JMP: aux += *pc++; 563 case JMP: aux += *pc++;
562 pc += aux; 564 pc += aux;
563 break; 565 break;
564 566
565 case IFFJMPW: aux += highbyte(*pc++); 567 case IFFJMPW: aux += highbyte(L, *pc++);
566 case IFFJMP: aux += *pc++; 568 case IFFJMP: aux += *pc++;
567 if (ttype(--S->top) == LUA_T_NIL) pc += aux; 569 if (ttype(--S->top) == LUA_T_NIL) pc += aux;
568 break; 570 break;
569 571
570 case IFTUPJMPW: aux += highbyte(*pc++); 572 case IFTUPJMPW: aux += highbyte(L, *pc++);
571 case IFTUPJMP: aux += *pc++; 573 case IFTUPJMP: aux += *pc++;
572 if (ttype(--S->top) != LUA_T_NIL) pc -= aux; 574 if (ttype(--S->top) != LUA_T_NIL) pc -= aux;
573 break; 575 break;
574 576
575 case IFFUPJMPW: aux += highbyte(*pc++); 577 case IFFUPJMPW: aux += highbyte(L, *pc++);
576 case IFFUPJMP: aux += *pc++; 578 case IFFUPJMP: aux += *pc++;
577 if (ttype(--S->top) == LUA_T_NIL) pc -= aux; 579 if (ttype(--S->top) == LUA_T_NIL) pc -= aux;
578 break; 580 break;
579 581
580 case CLOSUREW: aux += highbyte(*pc++); 582 case CLOSUREW: aux += highbyte(L, *pc++);
581 case CLOSURE: aux += *pc++; 583 case CLOSURE: aux += *pc++;
582 *S->top++ = consts[aux]; 584 *S->top++ = consts[aux];
583 luaV_closure(*pc++); 585 luaV_closure(L, *pc++);
584 luaC_checkGC(); 586 luaC_checkGC(L);
585 break; 587 break;
586 588
587 case SETLINEW: aux += highbyte(*pc++); 589 case SETLINEW: aux += highbyte(L, *pc++);
588 case SETLINE: aux += *pc++; 590 case SETLINE: aux += *pc++;
589 if ((S->stack+base-1)->ttype != LUA_T_LINE) { 591 if ((S->stack+base-1)->ttype != LUA_T_LINE) {
590 /* open space for LINE value */ 592 /* open space for LINE value */
591 luaD_openstack((S->top-S->stack)-base); 593 luaD_openstack(L, (S->top-S->stack)-base);
592 base++; 594 base++;
593 (S->stack+base-1)->ttype = LUA_T_LINE; 595 (S->stack+base-1)->ttype = LUA_T_LINE;
594 } 596 }
595 (S->stack+base-1)->value.i = aux; 597 (S->stack+base-1)->value.i = aux;
596 if (L->linehook) 598 if (L->linehook)
597 luaD_lineHook(aux); 599 luaD_lineHook(L, aux);
598 break; 600 break;
599 601
600 case LONGARGW: aux += highbyte(*pc++); 602 case LONGARGW: aux += highbyte(L, *pc++);
601 case LONGARG: aux += *pc++; 603 case LONGARG: aux += *pc++;
602 aux = highbyte(highbyte(aux)); 604 aux = highbyte(L, highbyte(L, aux));
603 goto switchentry; /* do not reset "aux" */ 605 goto switchentry; /* do not reset "aux" */
604 606
605 case CHECKSTACK: aux = *pc++; 607 case CHECKSTACK: aux = *pc++;
606 LUA_ASSERT((S->top-S->stack)-base == aux && S->last >= S->top, 608 LUA_ASSERT(L, (S->top-S->stack)-base == aux && S->last >= S->top,
607 "wrong stack size"); 609 "wrong stack size");
608 break; 610 break;
609 611
610 } 612 }
611 } ret: 613 } ret:
612 if (L->callhook) 614 if (L->callhook)
613 luaD_callHook(0, NULL, 1); 615 luaD_callHook(L, 0, NULL, 1);
614 return base; 616 return base;
615} 617}
616 618
diff --git a/lvm.h b/lvm.h
index 77fd3c53..8ddfd361 100644
--- a/lvm.h
+++ b/lvm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.h,v 1.10 1999/10/14 19:46:57 roberto Exp roberto $ 2** $Id: lvm.h,v 1.11 1999/11/04 17:22:26 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*/
@@ -14,21 +14,21 @@
14 14
15 15
16#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (luaV_tonumber(o) != 0)) 16#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (luaV_tonumber(o) != 0))
17#define tostring(o) ((ttype(o) != LUA_T_STRING) && (luaV_tostring(o) != 0)) 17#define tostring(L, o) ((ttype(o) != LUA_T_STRING) && (luaV_tostring(L, o) != 0))
18 18
19 19
20void luaV_pack (StkId firstel, int nvararg, TObject *tab); 20void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab);
21int luaV_tonumber (TObject *obj); 21int luaV_tonumber (TObject *obj);
22int luaV_tostring (TObject *obj); 22int luaV_tostring (lua_State *L, TObject *obj);
23void luaV_setn (Hash *t, int val); 23void luaV_setn (lua_State *L, Hash *t, int val);
24void luaV_gettable (void); 24void luaV_gettable (lua_State *L);
25void luaV_settable (const TObject *t); 25void luaV_settable (lua_State *L, const TObject *t);
26void luaV_rawsettable (const TObject *t); 26void luaV_rawsettable (lua_State *L, const TObject *t);
27void luaV_getglobal (GlobalVar *gv); 27void luaV_getglobal (lua_State *L, GlobalVar *gv);
28void luaV_setglobal (GlobalVar *gv); 28void luaV_setglobal (lua_State *L, GlobalVar *gv);
29StkId luaV_execute (const Closure *cl, const TProtoFunc *tf, StkId base); 29StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId base);
30void luaV_closure (int nelems); 30void luaV_closure (lua_State *L, int nelems);
31void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal, 31void luaV_comparison (lua_State *L, lua_Type ttype_less, lua_Type ttype_equal,
32 lua_Type ttype_great, IMS op); 32 lua_Type ttype_great, IMS op);
33 33
34#endif 34#endif