aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c179
-rw-r--r--lbuiltin.c29
-rw-r--r--ldo.c170
-rw-r--r--ldo.h32
-rw-r--r--lfunc.c18
-rw-r--r--lfunc.h5
-rw-r--r--lgc.c74
-rw-r--r--lgc.h4
-rw-r--r--liolib.c37
-rw-r--r--llex.c372
-rw-r--r--llex.h32
-rw-r--r--lmem.c18
-rw-r--r--lobject.c5
-rw-r--r--lobject.h3
-rw-r--r--lstring.c42
-rw-r--r--lstring.h4
-rw-r--r--ltable.c14
-rw-r--r--ltable.h5
-rw-r--r--ltm.c38
-rw-r--r--ltm.h9
-rw-r--r--lua.c3
-rw-r--r--lua.h4
-rw-r--r--lua.stx245
-rw-r--r--lvm.c224
-rw-r--r--makefile43
25 files changed, 781 insertions, 828 deletions
diff --git a/lapi.c b/lapi.c
index 2b37a72d..a65cc510 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.3 1997/10/24 17:17:24 roberto Exp roberto $ 2** $Id: lapi.c,v 1.4 1997/11/04 15:27:53 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*/
@@ -10,13 +10,12 @@
10 10
11#include "lapi.h" 11#include "lapi.h"
12#include "lauxlib.h" 12#include "lauxlib.h"
13#include "lbuiltin.h"
14#include "ldo.h" 13#include "ldo.h"
15#include "lfunc.h" 14#include "lfunc.h"
16#include "lgc.h" 15#include "lgc.h"
17#include "llex.h"
18#include "lmem.h" 16#include "lmem.h"
19#include "lobject.h" 17#include "lobject.h"
18#include "lstate.h"
20#include "lstring.h" 19#include "lstring.h"
21#include "ltable.h" 20#include "ltable.h"
22#include "ltm.h" 21#include "ltm.h"
@@ -38,41 +37,41 @@ TObject *luaA_Address (lua_Object o)
38 37
39void luaA_packresults (void) 38void luaA_packresults (void)
40{ 39{
41 luaV_pack(luaD_Cstack.lua2C, luaD_Cstack.num, luaD_stack.top); 40 luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top);
42 incr_top; 41 incr_top;
43} 42}
44 43
45 44
46int luaA_passresults (void) 45int luaA_passresults (void)
47{ 46{
48 luaD_checkstack(luaD_Cstack.num); 47 luaD_checkstack(L->Cstack.num);
49 memcpy(luaD_stack.top, luaD_Cstack.lua2C+luaD_stack.stack, 48 memcpy(L->stack.top, L->Cstack.lua2C+L->stack.stack,
50 luaD_Cstack.num*sizeof(TObject)); 49 L->Cstack.num*sizeof(TObject));
51 luaD_stack.top += luaD_Cstack.num; 50 L->stack.top += L->Cstack.num;
52 return luaD_Cstack.num; 51 return L->Cstack.num;
53} 52}
54 53
55 54
56static void checkCparams (int nParams) 55static void checkCparams (int nParams)
57{ 56{
58 if (luaD_stack.top-luaD_stack.stack < luaD_Cstack.base+nParams) 57 if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
59 lua_error("API error - wrong number of arguments in C2lua stack"); 58 lua_error("API error - wrong number of arguments in C2lua stack");
60} 59}
61 60
62 61
63static lua_Object put_luaObject (TObject *o) 62static lua_Object put_luaObject (TObject *o)
64{ 63{
65 luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base); 64 luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
66 luaD_stack.stack[luaD_Cstack.base++] = *o; 65 L->stack.stack[L->Cstack.base++] = *o;
67 return luaD_Cstack.base; /* this is +1 real position (see Ref) */ 66 return L->Cstack.base; /* this is +1 real position (see Ref) */
68} 67}
69 68
70 69
71static lua_Object put_luaObjectonTop (void) 70static lua_Object put_luaObjectonTop (void)
72{ 71{
73 luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base); 72 luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
74 luaD_stack.stack[luaD_Cstack.base++] = *(--luaD_stack.top); 73 L->stack.stack[L->Cstack.base++] = *(--L->stack.top);
75 return luaD_Cstack.base; /* this is +1 real position (see Ref) */ 74 return L->Cstack.base; /* this is +1 real position (see Ref) */
76} 75}
77 76
78 77
@@ -89,20 +88,20 @@ lua_Object lua_pop (void)
89*/ 88*/
90lua_Object lua_lua2C (int number) 89lua_Object lua_lua2C (int number)
91{ 90{
92 if (number <= 0 || number > luaD_Cstack.num) return LUA_NOOBJECT; 91 if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
93 /* Ref(luaD_stack.stack+(luaD_Cstack.lua2C+number-1)) == 92 /* Ref(L->stack.stack+(L->Cstack.lua2C+number-1)) ==
94 luaD_stack.stack+(luaD_Cstack.lua2C+number-1)-luaD_stack.stack+1 == */ 93 L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */
95 return luaD_Cstack.lua2C+number; 94 return L->Cstack.lua2C+number;
96} 95}
97 96
98 97
99lua_Object lua_upvalue (int n) 98lua_Object lua_upvalue (int n)
100{ 99{
101 TObject *f = luaD_stack.stack+luaD_Cstack.lua2C-1; 100 TObject *f = L->stack.stack+L->Cstack.lua2C-1;
102 if (ttype(f) != LUA_T_MARK || n <= 0 || n > clvalue(f)->nelems) 101 if (ttype(f) != LUA_T_MARK || n <= 0 || n > clvalue(f)->nelems)
103 return LUA_NOOBJECT; 102 return LUA_NOOBJECT;
104if (ttype(protovalue(f)) != LUA_T_CPROTO) lua_error("BAD!!"); 103if (ttype(protovalue(f)) != LUA_T_CPROTO) lua_error("BAD!!");
105 *luaD_stack.top = clvalue(f)->consts[n]; 104 *L->stack.top = clvalue(f)->consts[n];
106 incr_top; 105 incr_top;
107 return put_luaObjectonTop(); 106 return put_luaObjectonTop();
108} 107}
@@ -113,8 +112,8 @@ int lua_callfunction (lua_Object function)
113 if (function == LUA_NOOBJECT) 112 if (function == LUA_NOOBJECT)
114 return 1; 113 return 1;
115 else { 114 else {
116 luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base); 115 luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
117 luaD_stack.stack[luaD_Cstack.base] = *Address(function); 116 L->stack.stack[L->Cstack.base] = *Address(function);
118 return luaD_protectedrun(MULT_RET); 117 return luaD_protectedrun(MULT_RET);
119 } 118 }
120} 119}
@@ -129,16 +128,16 @@ lua_Object lua_gettagmethod (int tag, char *event)
129lua_Object lua_settagmethod (int tag, char *event) 128lua_Object lua_settagmethod (int tag, char *event)
130{ 129{
131 checkCparams(1); 130 checkCparams(1);
132 luaT_settagmethod(tag, event, luaD_stack.top-1); 131 luaT_settagmethod(tag, event, L->stack.top-1);
133 return put_luaObjectonTop(); 132 return put_luaObjectonTop();
134} 133}
135 134
136 135
137lua_Object lua_seterrormethod (void) 136lua_Object lua_seterrormethod (void)
138{ 137{
139 TObject temp = luaD_errorim; 138 TObject temp = L->errorim;
140 checkCparams(1); 139 checkCparams(1);
141 luaD_errorim = *(--luaD_stack.top); 140 L->errorim = *(--L->stack.top);
142 return put_luaObject(&temp); 141 return put_luaObject(&temp);
143} 142}
144 143
@@ -154,15 +153,15 @@ lua_Object lua_gettable (void)
154lua_Object lua_rawgettable (void) 153lua_Object lua_rawgettable (void)
155{ 154{
156 checkCparams(2); 155 checkCparams(2);
157 if (ttype(luaD_stack.top-2) != LUA_T_ARRAY) 156 if (ttype(L->stack.top-2) != LUA_T_ARRAY)
158 lua_error("indexed expression not a table in raw gettable"); 157 lua_error("indexed expression not a table in raw gettable");
159 else { 158 else {
160 TObject *h = luaH_get(avalue(luaD_stack.top-2), luaD_stack.top-1); 159 TObject *h = luaH_get(avalue(L->stack.top-2), L->stack.top-1);
161 --luaD_stack.top; 160 --L->stack.top;
162 if (h != NULL) 161 if (h != NULL)
163 *(luaD_stack.top-1) = *h; 162 *(L->stack.top-1) = *h;
164 else 163 else
165 ttype(luaD_stack.top-1) = LUA_T_NIL; 164 ttype(L->stack.top-1) = LUA_T_NIL;
166 } 165 }
167 return put_luaObjectonTop(); 166 return put_luaObjectonTop();
168} 167}
@@ -171,14 +170,14 @@ lua_Object lua_rawgettable (void)
171void lua_settable (void) 170void lua_settable (void)
172{ 171{
173 checkCparams(3); 172 checkCparams(3);
174 luaV_settable(luaD_stack.top-3, 1); 173 luaV_settable(L->stack.top-3, 1);
175} 174}
176 175
177 176
178void lua_rawsettable (void) 177void lua_rawsettable (void)
179{ 178{
180 checkCparams(3); 179 checkCparams(3);
181 luaV_settable(luaD_stack.top-3, 0); 180 luaV_settable(L->stack.top-3, 0);
182} 181}
183 182
184 183
@@ -192,6 +191,12 @@ lua_Object lua_createtable (void)
192} 191}
193 192
194 193
194lua_Object lua_globalbag (void)
195{
196 return put_luaObject(&L->globalbag);
197}
198
199
195lua_Object lua_getglobal (char *name) 200lua_Object lua_getglobal (char *name)
196{ 201{
197 luaD_checkstack(2); /* may need that to call T.M. */ 202 luaD_checkstack(2); /* may need that to call T.M. */
@@ -219,7 +224,7 @@ void lua_rawsetglobal (char *name)
219{ 224{
220 TaggedString *ts = luaS_new(name); 225 TaggedString *ts = luaS_new(name);
221 checkCparams(1); 226 checkCparams(1);
222 luaS_rawsetglobal(ts, --luaD_stack.top); 227 luaS_rawsetglobal(ts, --L->stack.top);
223} 228}
224 229
225 230
@@ -293,24 +298,24 @@ lua_CFunction lua_getcfunction (lua_Object object)
293 298
294void lua_pushnil (void) 299void lua_pushnil (void)
295{ 300{
296 ttype(luaD_stack.top) = LUA_T_NIL; 301 ttype(L->stack.top) = LUA_T_NIL;
297 incr_top; 302 incr_top;
298} 303}
299 304
300void lua_pushnumber (real n) 305void lua_pushnumber (real n)
301{ 306{
302 ttype(luaD_stack.top) = LUA_T_NUMBER; 307 ttype(L->stack.top) = LUA_T_NUMBER;
303 nvalue(luaD_stack.top) = n; 308 nvalue(L->stack.top) = n;
304 incr_top; 309 incr_top;
305} 310}
306 311
307void lua_pushstring (char *s) 312void lua_pushstring (char *s)
308{ 313{
309 if (s == NULL) 314 if (s == NULL)
310 ttype(luaD_stack.top) = LUA_T_NIL; 315 ttype(L->stack.top) = LUA_T_NIL;
311 else { 316 else {
312 tsvalue(luaD_stack.top) = luaS_new(s); 317 tsvalue(L->stack.top) = luaS_new(s);
313 ttype(luaD_stack.top) = LUA_T_STRING; 318 ttype(L->stack.top) = LUA_T_STRING;
314 } 319 }
315 incr_top; 320 incr_top;
316 luaC_checkGC(); 321 luaC_checkGC();
@@ -319,13 +324,13 @@ void lua_pushstring (char *s)
319void lua_pushCclosure (lua_CFunction fn, int n) 324void lua_pushCclosure (lua_CFunction fn, int n)
320{ 325{
321 if (fn == NULL) { 326 if (fn == NULL) {
322 ttype(luaD_stack.top) = LUA_T_NIL; 327 ttype(L->stack.top) = LUA_T_NIL;
323 incr_top; 328 incr_top;
324 } 329 }
325 else { 330 else {
326 checkCparams(n); 331 checkCparams(n);
327 ttype(luaD_stack.top) = LUA_T_CPROTO; 332 ttype(L->stack.top) = LUA_T_CPROTO;
328 fvalue(luaD_stack.top) = fn; 333 fvalue(L->stack.top) = fn;
329 incr_top; 334 incr_top;
330 luaV_closure(n); 335 luaV_closure(n);
331 } 336 }
@@ -335,15 +340,15 @@ void lua_pushusertag (void *u, int tag)
335{ 340{
336 if (tag < 0 && tag != LUA_ANYTAG) 341 if (tag < 0 && tag != LUA_ANYTAG)
337 luaT_realtag(tag); /* error if tag is not valid */ 342 luaT_realtag(tag); /* error if tag is not valid */
338 tsvalue(luaD_stack.top) = luaS_createudata(u, tag); 343 tsvalue(L->stack.top) = luaS_createudata(u, tag);
339 ttype(luaD_stack.top) = LUA_T_USERDATA; 344 ttype(L->stack.top) = LUA_T_USERDATA;
340 incr_top; 345 incr_top;
341 luaC_checkGC(); 346 luaC_checkGC();
342} 347}
343 348
344void luaA_pushobject (TObject *o) 349void luaA_pushobject (TObject *o)
345{ 350{
346 *luaD_stack.top = *o; 351 *L->stack.top = *o;
347 incr_top; 352 incr_top;
348} 353}
349 354
@@ -351,9 +356,9 @@ void lua_pushobject (lua_Object o)
351{ 356{
352 if (o == LUA_NOOBJECT) 357 if (o == LUA_NOOBJECT)
353 lua_error("API error - attempt to push a NOOBJECT"); 358 lua_error("API error - attempt to push a NOOBJECT");
354 *luaD_stack.top = *Address(o); 359 *L->stack.top = *Address(o);
355 if (ttype(luaD_stack.top) == LUA_T_MARK) 360 if (ttype(L->stack.top) == LUA_T_MARK)
356 ttype(luaD_stack.top) = LUA_T_FUNCTION; 361 ttype(L->stack.top) = LUA_T_FUNCTION;
357 incr_top; 362 incr_top;
358} 363}
359 364
@@ -376,18 +381,18 @@ void lua_settag (int tag)
376{ 381{
377 checkCparams(1); 382 checkCparams(1);
378 luaT_realtag(tag); 383 luaT_realtag(tag);
379 switch (ttype(luaD_stack.top-1)) { 384 switch (ttype(L->stack.top-1)) {
380 case LUA_T_ARRAY: 385 case LUA_T_ARRAY:
381 (luaD_stack.top-1)->value.a->htag = tag; 386 (L->stack.top-1)->value.a->htag = tag;
382 break; 387 break;
383 case LUA_T_USERDATA: 388 case LUA_T_USERDATA:
384 (luaD_stack.top-1)->value.ts->u.d.tag = tag; 389 (L->stack.top-1)->value.ts->u.d.tag = tag;
385 break; 390 break;
386 default: 391 default:
387 luaL_verror("cannot change the tag of a %s", 392 luaL_verror("cannot change the tag of a %s",
388 luaO_typenames[-ttype((luaD_stack.top-1))]); 393 luaO_typenames[-ttype((L->stack.top-1))]);
389 } 394 }
390 luaD_stack.top--; 395 L->stack.top--;
391} 396}
392 397
393 398
@@ -406,10 +411,10 @@ lua_LHFunction lua_linehook = NULL;
406lua_Function lua_stackedfunction (int level) 411lua_Function lua_stackedfunction (int level)
407{ 412{
408 StkId i; 413 StkId i;
409 for (i = (luaD_stack.top-1)-luaD_stack.stack; i>=0; i--) 414 for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
410 if (luaD_stack.stack[i].ttype == LUA_T_MARK) 415 if (L->stack.stack[i].ttype == LUA_T_MARK)
411 if (level-- == 0) 416 if (level-- == 0)
412 return Ref(luaD_stack.stack+i); 417 return Ref(L->stack.stack+i);
413 return LUA_NOOBJECT; 418 return LUA_NOOBJECT;
414} 419}
415 420
@@ -417,7 +422,7 @@ lua_Function lua_stackedfunction (int level)
417int lua_currentline (lua_Function func) 422int lua_currentline (lua_Function func)
418{ 423{
419 TObject *f = Address(func); 424 TObject *f = Address(func);
420 return (f+1 < luaD_stack.top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1; 425 return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
421} 426}
422 427
423 428
@@ -447,11 +452,11 @@ int lua_setlocal (lua_Function func, int local_number)
447 TProtoFunc *fp = protovalue(f)->value.tf; 452 TProtoFunc *fp = protovalue(f)->value.tf;
448 char *name = luaF_getlocalname(fp, local_number, lua_currentline(func)); 453 char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
449 checkCparams(1); 454 checkCparams(1);
450 --luaD_stack.top; 455 --L->stack.top;
451 if (name) { 456 if (name) {
452 /* if "name", there must be a LUA_T_LINE */ 457 /* if "name", there must be a LUA_T_LINE */
453 /* therefore, f+2 points to function base */ 458 /* therefore, f+2 points to function base */
454 *((f+2)+(local_number-1)) = *luaD_stack.top; 459 *((f+2)+(local_number-1)) = *L->stack.top;
455 return 1; 460 return 1;
456 } 461 }
457 else 462 else
@@ -478,19 +483,18 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
478} 483}
479 484
480 485
481static TObject *functofind;
482static int checkfunc (TObject *o) 486static int checkfunc (TObject *o)
483{ 487{
484 return (o->ttype == LUA_T_FUNCTION) && 488 return (o->ttype == LUA_T_FUNCTION) &&
485 ((functofind->ttype == LUA_T_FUNCTION) || 489 ((L->functofind->ttype == LUA_T_FUNCTION) ||
486 (functofind->ttype == LUA_T_MARK)) && 490 (L->functofind->ttype == LUA_T_MARK)) &&
487 (functofind->value.cl == o->value.cl); 491 (L->functofind->value.cl == o->value.cl);
488} 492}
489 493
490 494
491char *lua_getobjname (lua_Object o, char **name) 495char *lua_getobjname (lua_Object o, char **name)
492{ /* try to find a name for given function */ 496{ /* try to find a name for given function */
493 functofind = Address(o); 497 L->functofind = Address(o);
494 if ((*name = luaT_travtagmethods(checkfunc)) != NULL) 498 if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
495 return "tag-method"; 499 return "tag-method";
496 else if ((*name = luaS_travsymbol(checkfunc)) != NULL) 500 else if ((*name = luaS_travsymbol(checkfunc)) != NULL)
@@ -504,36 +508,30 @@ char *lua_getobjname (lua_Object o, char **name)
504** ======================================================= 508** =======================================================
505*/ 509*/
506 510
507#define MAX_C_BLOCKS 10
508
509static int numCblocks = 0;
510static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
511 511
512void lua_beginblock (void) 512void lua_beginblock (void)
513{ 513{
514 if (numCblocks >= MAX_C_BLOCKS) 514 if (L->numCblocks >= MAX_C_BLOCKS)
515 lua_error("`lua_beginblock': too many nested blocks"); 515 lua_error("`lua_beginblock': too many nested blocks");
516 Cblocks[numCblocks] = luaD_Cstack; 516 L->Cblocks[L->numCblocks] = L->Cstack;
517 numCblocks++; 517 L->numCblocks++;
518} 518}
519 519
520void lua_endblock (void) 520void lua_endblock (void)
521{ 521{
522 --numCblocks; 522 --L->numCblocks;
523 luaD_Cstack = Cblocks[numCblocks]; 523 L->Cstack = L->Cblocks[L->numCblocks];
524 luaD_adjusttop(luaD_Cstack.base); 524 luaD_adjusttop(L->Cstack.base);
525} 525}
526 526
527 527
528 528
529
530
531int lua_ref (int lock) 529int lua_ref (int lock)
532{ 530{
533 int ref; 531 int ref;
534 checkCparams(1); 532 checkCparams(1);
535 ref = luaC_ref(luaD_stack.top-1, lock); 533 ref = luaC_ref(L->stack.top-1, lock);
536 luaD_stack.top--; 534 L->stack.top--;
537 return ref; 535 return ref;
538} 536}
539 537
@@ -546,19 +544,6 @@ lua_Object lua_getref (int ref)
546} 544}
547 545
548 546
549void lua_open (void)
550{
551 static int firsttime = 1;
552 if (!firsttime) return;
553 firsttime = 0;
554 luaS_init();
555 luaX_init();
556 luaT_init();
557 luaD_init();
558 luaB_predefine();
559}
560
561
562 547
563#if LUA_COMPAT2_5 548#if LUA_COMPAT2_5
564/* 549/*
@@ -567,11 +552,11 @@ void lua_open (void)
567 552
568static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) 553static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
569{ 554{
570 StkId base = (luaD_stack.top-luaD_stack.stack)-nParams; 555 StkId base = (L->stack.top-L->stack.stack)-nParams;
571 luaD_openstack(nParams); 556 luaD_openstack(nParams);
572 luaD_stack.stack[base].ttype = LUA_T_CPROTO; 557 L->stack.stack[base].ttype = LUA_T_CPROTO;
573 luaD_stack.stack[base].value.f = f; 558 L->stack.stack[base].value.f = f;
574 luaF_simpleclosure(luaD_stack.stack+base); 559 luaF_simpleclosure(L->stack.stack+base);
575 luaD_call(base+1, nResults); 560 luaD_call(base+1, nResults);
576} 561}
577 562
diff --git a/lbuiltin.c b/lbuiltin.c
index b9d81621..36432add 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.6 1997/11/04 15:27:53 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.7 1997/11/07 18:19:13 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*/
@@ -15,6 +15,7 @@
15#include "lfunc.h" 15#include "lfunc.h"
16#include "lmem.h" 16#include "lmem.h"
17#include "lobject.h" 17#include "lobject.h"
18#include "lstate.h"
18#include "lstring.h" 19#include "lstring.h"
19#include "ltable.h" 20#include "ltable.h"
20#include "ltm.h" 21#include "ltm.h"
@@ -51,7 +52,7 @@ static void nextvar (void)
51 TObject *o = luaA_Address(luaL_nonnullarg(1)); 52 TObject *o = luaA_Address(luaL_nonnullarg(1));
52 TaggedString *g; 53 TaggedString *g;
53 if (ttype(o) == LUA_T_NIL) 54 if (ttype(o) == LUA_T_NIL)
54 g = (TaggedString *)luaS_root.next; 55 g = (TaggedString *)L->rootglobal.next;
55 else { 56 else {
56 luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); 57 luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
57 g = tsvalue(o); 58 g = tsvalue(o);
@@ -72,21 +73,21 @@ static void foreachvar (void)
72{ 73{
73 TObject f = *luaA_Address(functionarg(1)); 74 TObject f = *luaA_Address(functionarg(1));
74 GCnode *g; 75 GCnode *g;
75 StkId name = luaD_Cstack.base++; /* place to keep var name (to avoid GC) */ 76 StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */
76 ttype(luaD_stack.stack+name) = LUA_T_NIL; 77 ttype(L->stack.stack+name) = LUA_T_NIL;
77 luaD_stack.top++; 78 L->stack.top++;
78 for (g = luaS_root.next; g; g = g->next) { 79 for (g = L->rootglobal.next; g; g = g->next) {
79 TaggedString *s = (TaggedString *)g; 80 TaggedString *s = (TaggedString *)g;
80 if (s->u.globalval.ttype != LUA_T_NIL) { 81 if (s->u.globalval.ttype != LUA_T_NIL) {
81 ttype(luaD_stack.stack+name) = LUA_T_STRING; 82 ttype(L->stack.stack+name) = LUA_T_STRING;
82 tsvalue(luaD_stack.stack+name) = s; /* keep s on stack to avoid GC */ 83 tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */
83 luaA_pushobject(&f); 84 luaA_pushobject(&f);
84 pushstring(s); 85 pushstring(s);
85 luaA_pushobject(&s->u.globalval); 86 luaA_pushobject(&s->u.globalval);
86 luaD_call((luaD_stack.top-luaD_stack.stack)-2, 1); 87 luaD_call((L->stack.top-L->stack.stack)-2, 1);
87 if (ttype(luaD_stack.top-1) != LUA_T_NIL) 88 if (ttype(L->stack.top-1) != LUA_T_NIL)
88 return; 89 return;
89 luaD_stack.top--; 90 L->stack.top--;
90 } 91 }
91 } 92 }
92} 93}
@@ -115,10 +116,10 @@ static void foreach (void)
115 luaA_pushobject(&f); 116 luaA_pushobject(&f);
116 luaA_pushobject(ref(nd)); 117 luaA_pushobject(ref(nd));
117 luaA_pushobject(val(nd)); 118 luaA_pushobject(val(nd));
118 luaD_call((luaD_stack.top-luaD_stack.stack)-2, 1); 119 luaD_call((L->stack.top-L->stack.stack)-2, 1);
119 if (ttype(luaD_stack.top-1) != LUA_T_NIL) 120 if (ttype(L->stack.top-1) != LUA_T_NIL)
120 return; 121 return;
121 luaD_stack.top--; 122 L->stack.top--;
122 } 123 }
123 } 124 }
124} 125}
diff --git a/ldo.c b/ldo.c
index f869f82a..94c2bf3a 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.7 1997/11/04 15:27:53 roberto Exp roberto $ 2** $Id: ldo.c,v 1.8 1997/11/07 15:09:49 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*/
@@ -16,6 +16,7 @@
16#include "lmem.h" 16#include "lmem.h"
17#include "lobject.h" 17#include "lobject.h"
18#include "lparser.h" 18#include "lparser.h"
19#include "lstate.h"
19#include "ltm.h" 20#include "ltm.h"
20#include "lua.h" 21#include "lua.h"
21#include "luadebug.h" 22#include "luadebug.h"
@@ -30,14 +31,6 @@
30#endif 31#endif
31 32
32 33
33struct Stack luaD_stack;
34
35
36struct C_Lua_Stack luaD_Cstack = {0, 0, 0};
37
38static jmp_buf *errorJmp = NULL; /* current error recover point */
39
40
41 34
42/* 35/*
43** Error messages 36** Error messages
@@ -50,8 +43,6 @@ static void stderrorim (void)
50 fprintf(stderr, "lua error: %s\n", lua_getstring(s)); 43 fprintf(stderr, "lua error: %s\n", lua_getstring(s));
51} 44}
52 45
53TObject luaD_errorim;
54
55 46
56static void initCfunc (TObject *o, lua_CFunction f) 47static void initCfunc (TObject *o, lua_CFunction f)
57{ 48{
@@ -67,24 +58,25 @@ static void initCfunc (TObject *o, lua_CFunction f)
67 58
68void luaD_init (void) 59void luaD_init (void)
69{ 60{
70 luaD_stack.stack = luaM_newvector(INIT_STACK_SIZE, TObject); 61 L->stacklimit = STACK_LIMIT;
71 luaD_stack.top = luaD_stack.stack; 62 L->stack.stack = luaM_newvector(INIT_STACK_SIZE, TObject);
72 luaD_stack.last = luaD_stack.stack+(INIT_STACK_SIZE-1); 63 L->stack.top = L->stack.stack;
73 initCfunc(&luaD_errorim, stderrorim); 64 L->stack.last = L->stack.stack+(INIT_STACK_SIZE-1);
65 initCfunc(&L->errorim, stderrorim);
74} 66}
75 67
76 68
77void luaD_checkstack (int n) 69void luaD_checkstack (int n)
78{ 70{
79 if (luaD_stack.last-luaD_stack.top <= n) { 71 if (L->stack.last-L->stack.top <= n) {
80 static int limit = STACK_LIMIT; 72 StkId top = L->stack.top-L->stack.stack;
81 StkId top = luaD_stack.top-luaD_stack.stack; 73 int stacksize = (L->stack.last-L->stack.stack)+1+STACK_EXTRA+n;
82 int stacksize = (luaD_stack.last-luaD_stack.stack)+1+STACK_EXTRA+n; 74 L->stack.stack = luaM_reallocvector(L->stack.stack, stacksize,TObject);
83 luaD_stack.stack = luaM_reallocvector(luaD_stack.stack, stacksize,TObject); 75 L->stack.last = L->stack.stack+(stacksize-1);
84 luaD_stack.last = luaD_stack.stack+(stacksize-1); 76 L->stack.top = L->stack.stack + top;
85 luaD_stack.top = luaD_stack.stack + top; 77 if (stacksize >= L->stacklimit) {
86 if (stacksize >= limit) { 78 /* extra space to run error handler */
87 limit = stacksize+STACK_EXTRA; /* extra space to run error handler */ 79 L->stacklimit = stacksize+STACK_EXTRA;
88 if (lua_stackedfunction(100) == LUA_NOOBJECT) { 80 if (lua_stackedfunction(100) == LUA_NOOBJECT) {
89 /* less than 100 functions on the stack: cannot be recursive loop */ 81 /* less than 100 functions on the stack: cannot be recursive loop */
90 lua_error("Lua2C - C2Lua overflow"); 82 lua_error("Lua2C - C2Lua overflow");
@@ -101,80 +93,80 @@ void luaD_checkstack (int n)
101*/ 93*/
102void luaD_adjusttop (StkId newtop) 94void luaD_adjusttop (StkId newtop)
103{ 95{
104 int diff = newtop-(luaD_stack.top-luaD_stack.stack); 96 int diff = newtop-(L->stack.top-L->stack.stack);
105 if (diff <= 0) 97 if (diff <= 0)
106 luaD_stack.top += diff; 98 L->stack.top += diff;
107 else { 99 else {
108 luaD_checkstack(diff); 100 luaD_checkstack(diff);
109 while (diff--) 101 while (diff--)
110 ttype(luaD_stack.top++) = LUA_T_NIL; 102 ttype(L->stack.top++) = LUA_T_NIL;
111 } 103 }
112} 104}
113 105
114 106
115/* 107/*
116** Open a hole below "nelems" from the luaD_stack.top. 108** Open a hole below "nelems" from the L->stack.top.
117*/ 109*/
118void luaD_openstack (int nelems) 110void luaD_openstack (int nelems)
119{ 111{
120 int i; 112 int i;
121 for (i=0; i<nelems; i++) 113 for (i=0; i<nelems; i++)
122 *(luaD_stack.top-i) = *(luaD_stack.top-i-1); 114 *(L->stack.top-i) = *(L->stack.top-i-1);
123 incr_top; 115 incr_top;
124} 116}
125 117
126 118
127void luaD_lineHook (int line) 119void luaD_lineHook (int line)
128{ 120{
129 struct C_Lua_Stack oldCLS = luaD_Cstack; 121 struct C_Lua_Stack oldCLS = L->Cstack;
130 StkId old_top = luaD_Cstack.lua2C = luaD_Cstack.base = luaD_stack.top-luaD_stack.stack; 122 StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
131 luaD_Cstack.num = 0; 123 L->Cstack.num = 0;
132 (*lua_linehook)(line); 124 (*lua_linehook)(line);
133 luaD_stack.top = luaD_stack.stack+old_top; 125 L->stack.top = L->stack.stack+old_top;
134 luaD_Cstack = oldCLS; 126 L->Cstack = oldCLS;
135} 127}
136 128
137 129
138void luaD_callHook (StkId base, lua_Type type, int isreturn) 130void luaD_callHook (StkId base, lua_Type type, int isreturn)
139{ 131{
140 struct C_Lua_Stack oldCLS = luaD_Cstack; 132 struct C_Lua_Stack oldCLS = L->Cstack;
141 StkId old_top = luaD_Cstack.lua2C = luaD_Cstack.base = luaD_stack.top-luaD_stack.stack; 133 StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
142 luaD_Cstack.num = 0; 134 L->Cstack.num = 0;
143 if (isreturn) 135 if (isreturn)
144 (*lua_callhook)(LUA_NOOBJECT, "(return)", 0); 136 (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
145 else { 137 else {
146 TObject *f = luaD_stack.stack+base-1; 138 TObject *f = L->stack.stack+base-1;
147 if (type == LUA_T_PROTO) 139 if (type == LUA_T_PROTO)
148 (*lua_callhook)(Ref(f), tfvalue(protovalue(f))->fileName->str, 140 (*lua_callhook)(Ref(f), tfvalue(protovalue(f))->fileName->str,
149 tfvalue(protovalue(f))->lineDefined); 141 tfvalue(protovalue(f))->lineDefined);
150 else 142 else
151 (*lua_callhook)(Ref(f), "(C)", -1); 143 (*lua_callhook)(Ref(f), "(C)", -1);
152 } 144 }
153 luaD_stack.top = luaD_stack.stack+old_top; 145 L->stack.top = L->stack.stack+old_top;
154 luaD_Cstack = oldCLS; 146 L->Cstack = oldCLS;
155} 147}
156 148
157 149
158/* 150/*
159** Call a C function. luaD_Cstack.base will point to the top of the stack, 151** Call a C function. L->Cstack.base will point to the top of the stack,
160** and luaD_Cstack.num is the number of parameters. Returns an index 152** and L->Cstack.num is the number of parameters. Returns an index
161** to the first result from C. 153** to the first result from C.
162*/ 154*/
163static StkId callC (lua_CFunction func, StkId base) 155static StkId callC (lua_CFunction func, StkId base)
164{ 156{
165 struct C_Lua_Stack oldCLS = luaD_Cstack; 157 struct C_Lua_Stack oldCLS = L->Cstack;
166 StkId firstResult; 158 StkId firstResult;
167 luaD_Cstack.num = (luaD_stack.top-luaD_stack.stack) - base; 159 L->Cstack.num = (L->stack.top-L->stack.stack) - base;
168 /* incorporate parameters on the luaD_stack.stack */ 160 /* incorporate parameters on the L->stack.stack */
169 luaD_Cstack.lua2C = base; 161 L->Cstack.lua2C = base;
170 luaD_Cstack.base = base+luaD_Cstack.num; /* == top-stack */ 162 L->Cstack.base = base+L->Cstack.num; /* == top-stack */
171 if (lua_callhook) 163 if (lua_callhook)
172 luaD_callHook(base, LUA_T_CPROTO, 0); 164 luaD_callHook(base, LUA_T_CPROTO, 0);
173 (*func)(); 165 (*func)();
174 if (lua_callhook) /* func may have changed lua_callhook */ 166 if (lua_callhook) /* func may have changed lua_callhook */
175 luaD_callHook(base, LUA_T_CPROTO, 1); 167 luaD_callHook(base, LUA_T_CPROTO, 1);
176 firstResult = luaD_Cstack.base; 168 firstResult = L->Cstack.base;
177 luaD_Cstack = oldCLS; 169 L->Cstack = oldCLS;
178 return firstResult; 170 return firstResult;
179} 171}
180 172
@@ -182,21 +174,21 @@ static StkId callC (lua_CFunction func, StkId base)
182void luaD_callTM (TObject *f, int nParams, int nResults) 174void luaD_callTM (TObject *f, int nParams, int nResults)
183{ 175{
184 luaD_openstack(nParams); 176 luaD_openstack(nParams);
185 *(luaD_stack.top-nParams-1) = *f; 177 *(L->stack.top-nParams-1) = *f;
186 luaD_call((luaD_stack.top-luaD_stack.stack)-nParams, nResults); 178 luaD_call((L->stack.top-L->stack.stack)-nParams, nResults);
187} 179}
188 180
189 181
190/* 182/*
191** Call a function (C or Lua). The parameters must be on the luaD_stack.stack, 183** Call a function (C or Lua). The parameters must be on the L->stack.stack,
192** between [luaD_stack.stack+base,luaD_stack.top). The function to be called is at luaD_stack.stack+base-1. 184** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1.
193** When returns, the results are on the luaD_stack.stack, between [luaD_stack.stack+base-1,luaD_stack.top). 185** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top).
194** The number of results is nResults, unless nResults=MULT_RET. 186** The number of results is nResults, unless nResults=MULT_RET.
195*/ 187*/
196void luaD_call (StkId base, int nResults) 188void luaD_call (StkId base, int nResults)
197{ 189{
198 StkId firstResult; 190 StkId firstResult;
199 TObject *func = luaD_stack.stack+base-1; 191 TObject *func = L->stack.stack+base-1;
200 int i; 192 int i;
201 if (ttype(func) == LUA_T_FUNCTION) { 193 if (ttype(func) == LUA_T_FUNCTION) {
202 TObject *proto = protovalue(func); 194 TObject *proto = protovalue(func);
@@ -209,7 +201,7 @@ void luaD_call (StkId base, int nResults)
209 TObject *im = luaT_getimbyObj(func, IM_FUNCTION); 201 TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
210 if (ttype(im) == LUA_T_NIL) 202 if (ttype(im) == LUA_T_NIL)
211 lua_error("call expression not a function"); 203 lua_error("call expression not a function");
212 luaD_callTM(im, (luaD_stack.top-luaD_stack.stack)-(base-1), nResults); 204 luaD_callTM(im, (L->stack.top-L->stack.stack)-(base-1), nResults);
213 return; 205 return;
214 } 206 }
215 /* adjust the number of results */ 207 /* adjust the number of results */
@@ -217,29 +209,29 @@ void luaD_call (StkId base, int nResults)
217 luaD_adjusttop(firstResult+nResults); 209 luaD_adjusttop(firstResult+nResults);
218 /* move results to base-1 (to erase parameters and function) */ 210 /* move results to base-1 (to erase parameters and function) */
219 base--; 211 base--;
220 nResults = luaD_stack.top - (luaD_stack.stack+firstResult); /* actual number of results */ 212 nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */
221 for (i=0; i<nResults; i++) 213 for (i=0; i<nResults; i++)
222 *(luaD_stack.stack+base+i) = *(luaD_stack.stack+firstResult+i); 214 *(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i);
223 luaD_stack.top -= firstResult-base; 215 L->stack.top -= firstResult-base;
224} 216}
225 217
226 218
227 219
228/* 220/*
229** Traverse all objects on luaD_stack.stack 221** Traverse all objects on L->stack.stack
230*/ 222*/
231void luaD_travstack (int (*fn)(TObject *)) 223void luaD_travstack (int (*fn)(TObject *))
232{ 224{
233 StkId i; 225 StkId i;
234 for (i = (luaD_stack.top-1)-luaD_stack.stack; i>=0; i--) 226 for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
235 fn (luaD_stack.stack+i); 227 fn (L->stack.stack+i);
236} 228}
237 229
238 230
239 231
240static void message (char *s) 232static void message (char *s)
241{ 233{
242 TObject im = luaD_errorim; 234 TObject im = L->errorim;
243 if (ttype(&im) != LUA_T_NIL) { 235 if (ttype(&im) != LUA_T_NIL) {
244 lua_pushstring(s); 236 lua_pushstring(s);
245 luaD_callTM(&im, 1, 0); 237 luaD_callTM(&im, 1, 0);
@@ -252,8 +244,8 @@ static void message (char *s)
252void lua_error (char *s) 244void lua_error (char *s)
253{ 245{
254 if (s) message(s); 246 if (s) message(s);
255 if (errorJmp) 247 if (L->errorJmp)
256 longjmp(*errorJmp, 1); 248 longjmp(*((jmp_buf *)L->errorJmp), 1);
257 else { 249 else {
258 fprintf (stderr, "lua: exit(1). Unable to recover\n"); 250 fprintf (stderr, "lua: exit(1). Unable to recover\n");
259 exit(1); 251 exit(1);
@@ -261,40 +253,40 @@ void lua_error (char *s)
261} 253}
262 254
263/* 255/*
264** Call the function at luaD_Cstack.base, and incorporate results on 256** Call the function at L->Cstack.base, and incorporate results on
265** the Lua2C structure. 257** the Lua2C structure.
266*/ 258*/
267static void do_callinc (int nResults) 259static void do_callinc (int nResults)
268{ 260{
269 StkId base = luaD_Cstack.base; 261 StkId base = L->Cstack.base;
270 luaD_call(base+1, nResults); 262 luaD_call(base+1, nResults);
271 luaD_Cstack.lua2C = base; /* position of the luaM_new results */ 263 L->Cstack.lua2C = base; /* position of the luaM_new results */
272 luaD_Cstack.num = (luaD_stack.top-luaD_stack.stack) - base; /* number of results */ 264 L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */
273 luaD_Cstack.base = base + luaD_Cstack.num; /* incorporate results on luaD_stack.stack */ 265 L->Cstack.base = base + L->Cstack.num; /* incorporate results on L->stack.stack */
274} 266}
275 267
276 268
277/* 269/*
278** Execute a protected call. Assumes that function is at luaD_Cstack.base and 270** Execute a protected call. Assumes that function is at L->Cstack.base and
279** parameters are on top of it. Leave nResults on the stack. 271** parameters are on top of it. Leave nResults on the stack.
280*/ 272*/
281int luaD_protectedrun (int nResults) 273int luaD_protectedrun (int nResults)
282{ 274{
283 jmp_buf myErrorJmp; 275 jmp_buf myErrorJmp;
284 int status; 276 int status;
285 struct C_Lua_Stack oldCLS = luaD_Cstack; 277 struct C_Lua_Stack oldCLS = L->Cstack;
286 jmp_buf *oldErr = errorJmp; 278 jmp_buf *oldErr = L->errorJmp;
287 errorJmp = &myErrorJmp; 279 L->errorJmp = &myErrorJmp;
288 if (setjmp(myErrorJmp) == 0) { 280 if (setjmp(myErrorJmp) == 0) {
289 do_callinc(nResults); 281 do_callinc(nResults);
290 status = 0; 282 status = 0;
291 } 283 }
292 else { /* an error occurred: restore luaD_Cstack and luaD_stack.top */ 284 else { /* an error occurred: restore L->Cstack and L->stack.top */
293 luaD_Cstack = oldCLS; 285 L->Cstack = oldCLS;
294 luaD_stack.top = luaD_stack.stack+luaD_Cstack.base; 286 L->stack.top = L->stack.stack+L->Cstack.base;
295 status = 1; 287 status = 1;
296 } 288 }
297 errorJmp = oldErr; 289 L->errorJmp = oldErr;
298 return status; 290 return status;
299} 291}
300 292
@@ -307,8 +299,8 @@ static int protectedparser (ZIO *z, char *chunkname, int bin)
307 int status; 299 int status;
308 TProtoFunc *tf; 300 TProtoFunc *tf;
309 jmp_buf myErrorJmp; 301 jmp_buf myErrorJmp;
310 jmp_buf *oldErr = errorJmp; 302 jmp_buf *oldErr = L->errorJmp;
311 errorJmp = &myErrorJmp; 303 L->errorJmp = &myErrorJmp;
312 if (setjmp(myErrorJmp) == 0) { 304 if (setjmp(myErrorJmp) == 0) {
313 tf = bin ? luaU_undump1(z, chunkname) : luaY_parser(z, chunkname); 305 tf = bin ? luaU_undump1(z, chunkname) : luaY_parser(z, chunkname);
314 status = 0; 306 status = 0;
@@ -317,12 +309,12 @@ static int protectedparser (ZIO *z, char *chunkname, int bin)
317 tf = NULL; 309 tf = NULL;
318 status = 1; 310 status = 1;
319 } 311 }
320 errorJmp = oldErr; 312 L->errorJmp = oldErr;
321 if (status) return 1; /* error code */ 313 if (status) return 1; /* error code */
322 if (tf == NULL) return 2; /* 'natural' end */ 314 if (tf == NULL) return 2; /* 'natural' end */
323 luaD_adjusttop(luaD_Cstack.base+1); /* one slot for the pseudo-function */ 315 luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */
324 luaD_stack.stack[luaD_Cstack.base].ttype = LUA_T_PROTO; 316 L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
325 luaD_stack.stack[luaD_Cstack.base].value.tf = tf; 317 L->stack.stack[L->Cstack.base].value.tf = tf;
326 luaV_closure(0); 318 luaV_closure(0);
327 return 0; 319 return 0;
328} 320}
@@ -332,15 +324,15 @@ static int do_main (ZIO *z, char *chunkname, int bin)
332{ 324{
333 int status; 325 int status;
334 do { 326 do {
335 long old_blocks = (luaC_checkGC(), luaO_nblocks); 327 long old_blocks = (luaC_checkGC(), L->nblocks);
336 status = protectedparser(z, chunkname, bin); 328 status = protectedparser(z, chunkname, bin);
337 if (status == 1) return 1; /* error */ 329 if (status == 1) return 1; /* error */
338 else if (status == 2) return 0; /* 'natural' end */ 330 else if (status == 2) return 0; /* 'natural' end */
339 else { 331 else {
340 unsigned long newelems2 = 2*(luaO_nblocks-old_blocks); 332 unsigned long newelems2 = 2*(L->nblocks-old_blocks);
341 luaC_threshold += newelems2; 333 L->GCthreshold += newelems2;
342 status = luaD_protectedrun(MULT_RET); 334 status = luaD_protectedrun(MULT_RET);
343 luaC_threshold -= newelems2; 335 L->GCthreshold -= newelems2;
344 } 336 }
345 } while (bin && status == 0); 337 } while (bin && status == 0);
346 return status; 338 return status;
@@ -351,7 +343,7 @@ void luaD_gcIM (TObject *o)
351{ 343{
352 TObject *im = luaT_getimbyObj(o, IM_GC); 344 TObject *im = luaT_getimbyObj(o, IM_GC);
353 if (ttype(im) != LUA_T_NIL) { 345 if (ttype(im) != LUA_T_NIL) {
354 *luaD_stack.top = *o; 346 *L->stack.top = *o;
355 incr_top; 347 incr_top;
356 luaD_callTM(im, 1, 0); 348 luaD_callTM(im, 1, 0);
357 } 349 }
diff --git a/ldo.h b/ldo.h
index baa268eb..3b381c01 100644
--- a/ldo.h
+++ b/ldo.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: ldo.h,v 1.2 1997/11/04 15:27:53 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,43 +9,25 @@
9 9
10 10
11#include "lobject.h" 11#include "lobject.h"
12#include "lstate.h"
12 13
13 14
14typedef int StkId; /* index to luaD_stack.stack elements */
15
16#define MULT_RET 255 15#define MULT_RET 255
17 16
18 17
19extern struct Stack {
20 TObject *last;
21 TObject *stack;
22 TObject *top;
23} luaD_stack;
24
25
26extern struct C_Lua_Stack {
27 StkId base; /* when Lua calls C or C calls Lua, points to */
28 /* the first slot after the last parameter. */
29 StkId lua2C; /* points to first element of "array" lua2C */
30 int num; /* size of "array" lua2C */
31} luaD_Cstack;
32
33
34extern TObject luaD_errorim;
35
36 18
37/* 19/*
38** macro to increment stack top. 20** macro to increment stack top.
39** There must be always an empty slot at the luaD_stack.top 21** There must be always an empty slot at the L->stack.top
40*/ 22*/
41#define incr_top { if (luaD_stack.top >= luaD_stack.last) luaD_checkstack(1); \ 23#define incr_top { if (L->stack.top >= L->stack.last) luaD_checkstack(1); \
42 luaD_stack.top++; } 24 L->stack.top++; }
43 25
44 26
45/* macros to convert from lua_Object to (TObject *) and back */ 27/* macros to convert from lua_Object to (TObject *) and back */
46 28
47#define Address(lo) ((lo)+luaD_stack.stack-1) 29#define Address(lo) ((lo)+L->stack.stack-1)
48#define Ref(st) ((st)-luaD_stack.stack+1) 30#define Ref(st) ((st)-L->stack.stack+1)
49 31
50 32
51void luaD_init (void); 33void luaD_init (void);
diff --git a/lfunc.c b/lfunc.c
index 7270e980..2ab76335 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $ 2** $Id: lfunc.c,v 1.5 1997/10/24 17:17:24 roberto Exp roberto $
3** Lua Funcion auxiliar 3** Lua Funcion auxiliar
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -9,20 +9,18 @@
9 9
10#include "lfunc.h" 10#include "lfunc.h"
11#include "lmem.h" 11#include "lmem.h"
12#include "lstate.h"
12 13
13#define gcsizeproto(p) 5 14#define gcsizeproto(p) 5
14#define gcsizeclosure(c) 1 15#define gcsizeclosure(c) 1
15 16
16GCnode luaF_root = {NULL, 0};
17GCnode luaF_rootcl = {NULL, 0};
18
19 17
20 18
21Closure *luaF_newclosure (int nelems) 19Closure *luaF_newclosure (int nelems)
22{ 20{
23 Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject)); 21 Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
24 luaO_insertlist(&luaF_rootcl, (GCnode *)c); 22 luaO_insertlist(&(L->rootcl), (GCnode *)c);
25 luaO_nblocks += gcsizeclosure(c); 23 L->nblocks += gcsizeclosure(c);
26 c->nelems = nelems; 24 c->nelems = nelems;
27 return c; 25 return c;
28} 26}
@@ -46,8 +44,8 @@ TProtoFunc *luaF_newproto (void)
46 f->consts = NULL; 44 f->consts = NULL;
47 f->nconsts = 0; 45 f->nconsts = 0;
48 f->locvars = NULL; 46 f->locvars = NULL;
49 luaO_insertlist(&luaF_root, (GCnode *)f); 47 luaO_insertlist(&(L->rootproto), (GCnode *)f);
50 luaO_nblocks += gcsizeproto(f); 48 L->nblocks += gcsizeproto(f);
51 return f; 49 return f;
52} 50}
53 51
@@ -66,7 +64,7 @@ void luaF_freeproto (TProtoFunc *l)
66{ 64{
67 while (l) { 65 while (l) {
68 TProtoFunc *next = (TProtoFunc *)l->head.next; 66 TProtoFunc *next = (TProtoFunc *)l->head.next;
69 luaO_nblocks -= gcsizeproto(l); 67 L->nblocks -= gcsizeproto(l);
70 freefunc(l); 68 freefunc(l);
71 l = next; 69 l = next;
72 } 70 }
@@ -77,7 +75,7 @@ void luaF_freeclosure (Closure *l)
77{ 75{
78 while (l) { 76 while (l) {
79 Closure *next = (Closure *)l->head.next; 77 Closure *next = (Closure *)l->head.next;
80 luaO_nblocks -= gcsizeclosure(l); 78 L->nblocks -= gcsizeclosure(l);
81 luaM_free(l); 79 luaM_free(l);
82 l = next; 80 l = next;
83 } 81 }
diff --git a/lfunc.h b/lfunc.h
index b6e14e26..72fcafad 100644
--- a/lfunc.h
+++ b/lfunc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.h,v 1.2 1997/09/26 16:46:20 roberto Exp roberto $ 2** $Id: lfunc.h,v 1.3 1997/10/24 17:17:24 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*/
@@ -11,9 +11,6 @@
11#include "lobject.h" 11#include "lobject.h"
12 12
13 13
14extern GCnode luaF_root;
15extern GCnode luaF_rootcl;
16
17 14
18TProtoFunc *luaF_newproto (void); 15TProtoFunc *luaF_newproto (void);
19Closure *luaF_newclosure (int nelems); 16Closure *luaF_newclosure (int nelems);
diff --git a/lgc.c b/lgc.c
index 6fdbacaa..c3225deb 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.6 1997/10/24 17:17:24 roberto Exp roberto $ 2** $Id: lgc.c,v 1.7 1997/11/03 20:45:23 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*/
@@ -10,6 +10,7 @@
10#include "lgc.h" 10#include "lgc.h"
11#include "lmem.h" 11#include "lmem.h"
12#include "lobject.h" 12#include "lobject.h"
13#include "lstate.h"
13#include "lstring.h" 14#include "lstring.h"
14#include "ltable.h" 15#include "ltable.h"
15#include "ltm.h" 16#include "ltm.h"
@@ -27,12 +28,6 @@ static int markobject (TObject *o);
27** ======================================================= 28** =======================================================
28*/ 29*/
29 30
30static struct ref {
31 TObject o;
32 enum {LOCK, HOLD, FREE, COLLECTED} status;
33} *refArray = NULL;
34static int refSize = 0;
35
36 31
37int luaC_ref (TObject *o, int lock) 32int luaC_ref (TObject *o, int lock)
38{ 33{
@@ -40,18 +35,19 @@ int luaC_ref (TObject *o, int lock)
40 if (ttype(o) == LUA_T_NIL) 35 if (ttype(o) == LUA_T_NIL)
41 ref = -1; /* special ref for nil */ 36 ref = -1; /* special ref for nil */
42 else { 37 else {
43 for (ref=0; ref<refSize; ref++) 38 for (ref=0; ref<L->refSize; ref++)
44 if (refArray[ref].status == FREE) 39 if (L->refArray[ref].status == FREE)
45 goto found; 40 goto found;
46 /* no more empty spaces */ { 41 /* no more empty spaces */ {
47 int oldSize = refSize; 42 int oldSize = L->refSize;
48 refSize = luaM_growvector(&refArray, refSize, struct ref, refEM, MAX_WORD); 43 L->refSize = luaM_growvector(&L->refArray, L->refSize, struct ref,
49 for (ref=oldSize; ref<refSize; ref++) 44 refEM, MAX_WORD);
50 refArray[ref].status = FREE; 45 for (ref=oldSize; ref<L->refSize; ref++)
46 L->refArray[ref].status = FREE;
51 ref = oldSize; 47 ref = oldSize;
52 } found: 48 } found:
53 refArray[ref].o = *o; 49 L->refArray[ref].o = *o;
54 refArray[ref].status = lock ? LOCK : HOLD; 50 L->refArray[ref].status = lock ? LOCK : HOLD;
55 } 51 }
56 return ref; 52 return ref;
57} 53}
@@ -59,8 +55,8 @@ int luaC_ref (TObject *o, int lock)
59 55
60void lua_unref (int ref) 56void lua_unref (int ref)
61{ 57{
62 if (ref >= 0 && ref < refSize) 58 if (ref >= 0 && ref < L->refSize)
63 refArray[ref].status = FREE; 59 L->refArray[ref].status = FREE;
64} 60}
65 61
66 62
@@ -68,9 +64,9 @@ TObject* luaC_getref (int ref)
68{ 64{
69 if (ref == -1) 65 if (ref == -1)
70 return &luaO_nilobject; 66 return &luaO_nilobject;
71 if (ref >= 0 && ref < refSize && 67 if (ref >= 0 && ref < L->refSize &&
72 (refArray[ref].status == LOCK || refArray[ref].status == HOLD)) 68 (L->refArray[ref].status == LOCK || L->refArray[ref].status == HOLD))
73 return &refArray[ref].o; 69 return &L->refArray[ref].o;
74 else 70 else
75 return NULL; 71 return NULL;
76} 72}
@@ -79,9 +75,9 @@ TObject* luaC_getref (int ref)
79static void travlock (void) 75static void travlock (void)
80{ 76{
81 int i; 77 int i;
82 for (i=0; i<refSize; i++) 78 for (i=0; i<L->refSize; i++)
83 if (refArray[i].status == LOCK) 79 if (L->refArray[i].status == LOCK)
84 markobject(&refArray[i].o); 80 markobject(&L->refArray[i].o);
85} 81}
86 82
87 83
@@ -105,9 +101,9 @@ static int ismarked (TObject *o)
105static void invalidaterefs (void) 101static void invalidaterefs (void)
106{ 102{
107 int i; 103 int i;
108 for (i=0; i<refSize; i++) 104 for (i=0; i<L->refSize; i++)
109 if (refArray[i].status == HOLD && !ismarked(&refArray[i].o)) 105 if (L->refArray[i].status == HOLD && !ismarked(&L->refArray[i].o))
110 refArray[i].status = COLLECTED; 106 L->refArray[i].status = COLLECTED;
111} 107}
112 108
113 109
@@ -210,7 +206,7 @@ static void hashmark (Hash *h)
210static void globalmark (void) 206static void globalmark (void)
211{ 207{
212 TaggedString *g; 208 TaggedString *g;
213 for (g=(TaggedString *)luaS_root.next; g; g=(TaggedString *)g->head.next) 209 for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
214 if (g->u.globalval.ttype != LUA_T_NIL) { 210 if (g->u.globalval.ttype != LUA_T_NIL) {
215 markobject(&g->u.globalval); 211 markobject(&g->u.globalval);
216 strmark(g); /* cannot collect non nil global variables */ 212 strmark(g); /* cannot collect non nil global variables */
@@ -240,23 +236,19 @@ static int markobject (TObject *o)
240 236
241 237
242 238
243#define GARBAGE_BLOCK 150
244
245unsigned long luaC_threshold = GARBAGE_BLOCK;
246
247
248static void markall (void) 239static void markall (void)
249{ 240{
250 luaD_travstack(markobject); /* mark stack objects */ 241 luaD_travstack(markobject); /* mark stack objects */
251 globalmark(); /* mark global variable values and names */ 242 globalmark(); /* mark global variable values and names */
252 travlock(); /* mark locked objects */ 243 travlock(); /* mark locked objects */
244 markobject(&L->globalbag); /* mark elements in global bag */
253 luaT_travtagmethods(markobject); /* mark fallbacks */ 245 luaT_travtagmethods(markobject); /* mark fallbacks */
254} 246}
255 247
256 248
257long lua_collectgarbage (long limit) 249long lua_collectgarbage (long limit)
258{ 250{
259 unsigned long recovered = luaO_nblocks; /* to subtract nblocks after gc */ 251 unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */
260 Hash *freetable; 252 Hash *freetable;
261 TaggedString *freestr; 253 TaggedString *freestr;
262 TProtoFunc *freefunc; 254 TProtoFunc *freefunc;
@@ -264,10 +256,10 @@ long lua_collectgarbage (long limit)
264 markall(); 256 markall();
265 invalidaterefs(); 257 invalidaterefs();
266 freestr = luaS_collector(); 258 freestr = luaS_collector();
267 freetable = (Hash *)listcollect(&luaH_root); 259 freetable = (Hash *)listcollect(&(L->roottable));
268 freefunc = (TProtoFunc *)listcollect(&luaF_root); 260 freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
269 freeclos = (Closure *)listcollect(&luaF_rootcl); 261 freeclos = (Closure *)listcollect(&(L->rootcl));
270 luaC_threshold *= 4; /* to avoid GC during GC */ 262 L->GCthreshold *= 4; /* to avoid GC during GC */
271 hashcallIM(freetable); /* GC tag methods for tables */ 263 hashcallIM(freetable); /* GC tag methods for tables */
272 strcallIM(freestr); /* GC tag methods for userdata */ 264 strcallIM(freestr); /* GC tag methods for userdata */
273 luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */ 265 luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
@@ -276,16 +268,16 @@ long lua_collectgarbage (long limit)
276 luaF_freeproto(freefunc); 268 luaF_freeproto(freefunc);
277 luaF_freeclosure(freeclos); 269 luaF_freeclosure(freeclos);
278 luaM_clearbuffer(); 270 luaM_clearbuffer();
279 recovered = recovered-luaO_nblocks; 271 recovered = recovered-L->nblocks;
280/*printf("==total %ld coletados %ld\n", luaO_nblocks+recovered, recovered);*/ 272/*printf("==total %ld coletados %ld\n", L->nblocks+recovered, recovered);*/
281 luaC_threshold = (limit == 0) ? 2*luaO_nblocks : luaO_nblocks+limit; 273 L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
282 return recovered; 274 return recovered;
283} 275}
284 276
285 277
286void luaC_checkGC (void) 278void luaC_checkGC (void)
287{ 279{
288 if (luaO_nblocks >= luaC_threshold) 280 if (L->nblocks >= L->GCthreshold)
289 lua_collectgarbage(0); 281 lua_collectgarbage(0);
290} 282}
291 283
diff --git a/lgc.h b/lgc.h
index ea669aa2..a7a9a77a 100644
--- a/lgc.h
+++ b/lgc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: lgc.h,v 1.2 1997/10/23 16:26:37 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,6 @@
11#include "lobject.h" 11#include "lobject.h"
12 12
13 13
14extern unsigned long luaC_threshold;
15
16void luaC_checkGC (void); 14void luaC_checkGC (void);
17TObject* luaC_getref (int ref); 15TObject* luaC_getref (int ref);
18int luaC_ref (TObject *o, int lock); 16int luaC_ref (TObject *o, int lock);
diff --git a/liolib.c b/liolib.c
index a0464671..ffa5b16c 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.3 1997/10/30 20:29:09 roberto Exp roberto $ 2** $Id: liolib.c,v 1.4 1997/11/04 15:27:53 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*/
@@ -41,8 +41,21 @@ int pclose();
41#endif 41#endif
42 42
43 43
44int lua_tagio; 44static void createtag (char *t)
45static int closedtag; 45{
46 lua_pushobject(lua_globalbag());
47 lua_pushstring(t);
48 lua_pushnumber(lua_newtag());
49 lua_settable();
50}
51
52
53static int gettag (char *t)
54{
55 lua_pushobject(lua_globalbag());
56 lua_pushstring(t);
57 return lua_getnumber(lua_gettable());
58}
46 59
47 60
48static void pushresult (int i) 61static void pushresult (int i)
@@ -59,9 +72,9 @@ static void pushresult (int i)
59static int ishandler (lua_Object f) 72static int ishandler (lua_Object f)
60{ 73{
61 if (lua_isuserdata(f)) { 74 if (lua_isuserdata(f)) {
62 if (lua_tag(f) == closedtag) 75 if (lua_tag(f) == gettag("closedtag"))
63 lua_error("trying to access a closed file"); 76 lua_error("trying to access a closed file");
64 return lua_tag(f) == lua_tagio; 77 return lua_tag(f) == gettag("tagio");
65 } 78 }
66 else return 0; 79 else return 0;
67} 80}
@@ -94,13 +107,13 @@ static void closefile (char *name)
94 if (pclose(f) == -1) 107 if (pclose(f) == -1)
95 fclose(f); 108 fclose(f);
96 lua_pushobject(lua_getglobal(name)); 109 lua_pushobject(lua_getglobal(name));
97 lua_settag(closedtag); 110 lua_settag(gettag("closedtag"));
98} 111}
99 112
100 113
101static void setfile (FILE *f, char *name) 114static void setfile (FILE *f, char *name)
102{ 115{
103 lua_pushusertag(f, lua_tagio); 116 lua_pushusertag(f, gettag("tagio"));
104 lua_setglobal(name); 117 lua_setglobal(name);
105} 118}
106 119
@@ -108,7 +121,7 @@ static void setfile (FILE *f, char *name)
108static void setreturn (FILE *f, char *name) 121static void setreturn (FILE *f, char *name)
109{ 122{
110 setfile(f, name); 123 setfile(f, name);
111 lua_pushusertag(f, lua_tagio); 124 lua_pushusertag(f, gettag("tagio"));
112} 125}
113 126
114 127
@@ -120,7 +133,7 @@ static void io_readfrom (void)
120 closefile("_INPUT"); 133 closefile("_INPUT");
121 current = stdin; 134 current = stdin;
122 } 135 }
123 else if (lua_tag(f) == lua_tagio) 136 else if (lua_tag(f) == gettag("tagio"))
124 current = lua_getuserdata(f); 137 current = lua_getuserdata(f);
125 else { 138 else {
126 char *s = luaL_check_string(1); 139 char *s = luaL_check_string(1);
@@ -142,7 +155,7 @@ static void io_writeto (void)
142 closefile("_OUTPUT"); 155 closefile("_OUTPUT");
143 current = stdout; 156 current = stdout;
144 } 157 }
145 else if (lua_tag(f) == lua_tagio) 158 else if (lua_tag(f) == gettag("tagio"))
146 current = lua_getuserdata(f); 159 current = lua_getuserdata(f);
147 else { 160 else {
148 char *s = luaL_check_string(1); 161 char *s = luaL_check_string(1);
@@ -373,8 +386,8 @@ static struct luaL_reg iolib[] = {
373void lua_iolibopen (void) 386void lua_iolibopen (void)
374{ 387{
375 luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); 388 luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
376 lua_tagio = lua_newtag(); 389 createtag("iotag");
377 closedtag = lua_newtag(); 390 createtag("closedtag");
378 setfile(stdin, "_INPUT"); 391 setfile(stdin, "_INPUT");
379 setfile(stdout, "_OUTPUT"); 392 setfile(stdout, "_OUTPUT");
380 setfile(stdin, "_STDIN"); 393 setfile(stdin, "_STDIN");
diff --git a/llex.c b/llex.c
index 5b800011..38e72c5d 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.4 1997/11/04 15:27:53 roberto Exp roberto $ 2** $Id: llex.c,v 1.5 1997/11/07 15:09:49 roberto Exp roberto $
3** Lexical Analizer 3** Lexical Analizer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -12,35 +12,32 @@
12#include "lmem.h" 12#include "lmem.h"
13#include "lobject.h" 13#include "lobject.h"
14#include "lparser.h" 14#include "lparser.h"
15#include "lstate.h"
15#include "lstring.h" 16#include "lstring.h"
16#include "lstx.h" 17#include "lstx.h"
17#include "luadebug.h" 18#include "luadebug.h"
18#include "lzio.h" 19#include "lzio.h"
19 20
20 21
21static int current; /* look ahead character */
22static ZIO *lex_z;
23 22
24
25int luaX_linenumber;
26int lua_debug=0; 23int lua_debug=0;
27 24
28 25
29#define next() (current = zgetc(lex_z)) 26#define next(LL) (LL->current = zgetc(LL->lex_z))
30 27
31 28
29static struct {
30 char *name;
31 int token;
32} reserved [] = {
33 {"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
34 {"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
35 {"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
36 {"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
37};
32 38
33void luaX_init (void) 39void luaX_init (void)
34{ 40{
35 static struct {
36 char *name;
37 int token;
38 } reserved [] = {
39 {"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
40 {"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
41 {"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
42 {"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
43 };
44 int i; 41 int i;
45 for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) { 42 for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
46 TaggedString *ts = luaS_new(reserved[i].name); 43 TaggedString *ts = luaS_new(reserved[i].name);
@@ -49,47 +46,29 @@ void luaX_init (void)
49} 46}
50 47
51 48
52 49static void firstline (LexState *LL)
53#define MAX_IFS 5
54
55/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
56
57static struct {
58 int elsepart; /* true if its in the $else part */
59 int condition; /* true if $if condition is true */
60 int skip; /* true if part must be skiped */
61} ifstate[MAX_IFS];
62
63static int iflevel; /* level of nested $if's */
64
65
66static struct textbuff {
67 char *text;
68 int tokensize;
69 int buffsize;
70} textbuff;
71
72
73static void firstline (void)
74{ 50{
75 int c = zgetc(lex_z); 51 int c = zgetc(LL->lex_z);
76 if (c == '#') 52 if (c == '#')
77 while((c=zgetc(lex_z)) != '\n' && c != EOZ) /* skip first line */; 53 while((c=zgetc(LL->lex_z)) != '\n' && c != EOZ) /* skip first line */;
78 zungetc(lex_z); 54 zungetc(LL->lex_z);
79} 55}
80 56
81 57
82void luaX_setinput (ZIO *z) 58void luaX_setinput (ZIO *z)
83{ 59{
84 current = '\n'; 60 LexState *LL = L->lexstate;
85 luaX_linenumber = 0; 61 LL->current = '\n';
86 iflevel = 0; 62 LL->linelasttoken = 0;
87 ifstate[0].skip = 0; 63 LL->lastline = 0;
88 ifstate[0].elsepart = 1; /* to avoid a free $else */ 64 LL->linenumber = 0;
89 lex_z = z; 65 LL->iflevel = 0;
90 firstline(); 66 LL->ifstate[0].skip = 0;
91 textbuff.buffsize = 20; 67 LL->ifstate[0].elsepart = 1; /* to avoid a free $else */
92 textbuff.text = luaM_buffer(textbuff.buffsize); 68 LL->lex_z = z;
69 firstline(LL);
70 LL->textbuff.buffsize = 20;
71 LL->textbuff.text = luaM_buffer(LL->textbuff.buffsize);
93} 72}
94 73
95 74
@@ -102,9 +81,9 @@ void luaX_setinput (ZIO *z)
102 81
103#define PRAGMASIZE 20 82#define PRAGMASIZE 20
104 83
105static void skipspace (void) 84static void skipspace (LexState *LL)
106{ 85{
107 while (current == ' ' || current == '\t') next(); 86 while (LL->current == ' ' || LL->current == '\t') next(LL);
108} 87}
109 88
110 89
@@ -122,49 +101,49 @@ static int checkcond (char *buff)
122} 101}
123 102
124 103
125static void readname (char *buff) 104static void readname (LexState *LL, char *buff)
126{ 105{
127 int i = 0; 106 int i = 0;
128 skipspace(); 107 skipspace(LL);
129 while (isalnum(current) || current == '_') { 108 while (isalnum(LL->current) || LL->current == '_') {
130 if (i >= PRAGMASIZE) { 109 if (i >= PRAGMASIZE) {
131 buff[PRAGMASIZE] = 0; 110 buff[PRAGMASIZE] = 0;
132 luaY_syntaxerror("pragma too long", buff); 111 luaY_syntaxerror("pragma too long", buff);
133 } 112 }
134 buff[i++] = current; 113 buff[i++] = LL->current;
135 next(); 114 next(LL);
136 } 115 }
137 buff[i] = 0; 116 buff[i] = 0;
138} 117}
139 118
140 119
141static void inclinenumber (void); 120static void inclinenumber (LexState *LL);
142 121
143 122
144static void ifskip (void) 123static void ifskip (LexState *LL)
145{ 124{
146 while (ifstate[iflevel].skip) { 125 while (LL->ifstate[LL->iflevel].skip) {
147 if (current == '\n') 126 if (LL->current == '\n')
148 inclinenumber(); 127 inclinenumber(LL);
149 else if (current == EOZ) 128 else if (LL->current == EOZ)
150 luaY_syntaxerror("input ends inside a $if", ""); 129 luaY_syntaxerror("input ends inside a $if", "");
151 else next(); 130 else next(LL);
152 } 131 }
153} 132}
154 133
155 134
156static void inclinenumber (void) 135static void inclinenumber (LexState *LL)
157{ 136{
158 static char *pragmas [] = 137 static char *pragmas [] =
159 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL}; 138 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
160 next(); /* skip '\n' */ 139 next(LL); /* skip '\n' */
161 ++luaX_linenumber; 140 ++LL->linenumber;
162 if (current == '$') { /* is a pragma? */ 141 if (LL->current == '$') { /* is a pragma? */
163 char buff[PRAGMASIZE+1]; 142 char buff[PRAGMASIZE+1];
164 int ifnot = 0; 143 int ifnot = 0;
165 int skip = ifstate[iflevel].skip; 144 int skip = LL->ifstate[LL->iflevel].skip;
166 next(); /* skip $ */ 145 next(LL); /* skip $ */
167 readname(buff); 146 readname(LL, buff);
168 switch (luaO_findstring(buff, pragmas)) { 147 switch (luaO_findstring(buff, pragmas)) {
169 case 0: /* debug */ 148 case 0: /* debug */
170 if (!skip) lua_debug = 1; 149 if (!skip) lua_debug = 1;
@@ -174,42 +153,42 @@ static void inclinenumber (void)
174 break; 153 break;
175 case 2: /* endinput */ 154 case 2: /* endinput */
176 if (!skip) { 155 if (!skip) {
177 current = EOZ; 156 LL->current = EOZ;
178 iflevel = 0; /* to allow $endinput inside a $if */ 157 LL->iflevel = 0; /* to allow $endinput inside a $if */
179 } 158 }
180 break; 159 break;
181 case 3: /* end */ 160 case 3: /* end */
182 if (iflevel-- == 0) 161 if (LL->iflevel-- == 0)
183 luaY_syntaxerror("unmatched $end", "$end"); 162 luaY_syntaxerror("unmatched $end", "$end");
184 break; 163 break;
185 case 4: /* ifnot */ 164 case 4: /* ifnot */
186 ifnot = 1; 165 ifnot = 1;
187 /* go through */ 166 /* go through */
188 case 5: /* if */ 167 case 5: /* if */
189 if (iflevel == MAX_IFS-1) 168 if (LL->iflevel == MAX_IFS-1)
190 luaY_syntaxerror("too many nested `$ifs'", "$if"); 169 luaY_syntaxerror("too many nested `$ifs'", "$if");
191 readname(buff); 170 readname(LL, buff);
192 iflevel++; 171 LL->iflevel++;
193 ifstate[iflevel].elsepart = 0; 172 LL->ifstate[LL->iflevel].elsepart = 0;
194 ifstate[iflevel].condition = checkcond(buff) ? !ifnot : ifnot; 173 LL->ifstate[LL->iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
195 ifstate[iflevel].skip = skip || !ifstate[iflevel].condition; 174 LL->ifstate[LL->iflevel].skip = skip || !LL->ifstate[LL->iflevel].condition;
196 break; 175 break;
197 case 6: /* else */ 176 case 6: /* else */
198 if (ifstate[iflevel].elsepart) 177 if (LL->ifstate[LL->iflevel].elsepart)
199 luaY_syntaxerror("unmatched $else", "$else"); 178 luaY_syntaxerror("unmatched $else", "$else");
200 ifstate[iflevel].elsepart = 1; 179 LL->ifstate[LL->iflevel].elsepart = 1;
201 ifstate[iflevel].skip = 180 LL->ifstate[LL->iflevel].skip = LL->ifstate[LL->iflevel-1].skip ||
202 ifstate[iflevel-1].skip || ifstate[iflevel].condition; 181 LL->ifstate[LL->iflevel].condition;
203 break; 182 break;
204 default: 183 default:
205 luaY_syntaxerror("invalid pragma", buff); 184 luaY_syntaxerror("invalid pragma", buff);
206 } 185 }
207 skipspace(); 186 skipspace(LL);
208 if (current == '\n') /* pragma must end with a '\n' ... */ 187 if (LL->current == '\n') /* pragma must end with a '\n' ... */
209 inclinenumber(); 188 inclinenumber(LL);
210 else if (current != EOZ) /* or eof */ 189 else if (LL->current != EOZ) /* or eof */
211 luaY_syntaxerror("invalid pragma format", buff); 190 luaY_syntaxerror("invalid pragma format", buff);
212 ifskip(); 191 ifskip(LL);
213 } 192 }
214} 193}
215 194
@@ -222,162 +201,167 @@ static void inclinenumber (void)
222 201
223 202
224 203
225static void save (int c) 204static void save (LexState *LL, int c)
226{ 205{
227 if (textbuff.tokensize >= textbuff.buffsize) 206 if (LL->textbuff.tokensize >= LL->textbuff.buffsize)
228 textbuff.text = luaM_buffer(textbuff.buffsize *= 2); 207 LL->textbuff.text = luaM_buffer(LL->textbuff.buffsize *= 2);
229 textbuff.text[textbuff.tokensize++] = c; 208 LL->textbuff.text[LL->textbuff.tokensize++] = c;
230} 209}
231 210
232 211
233char *luaX_lasttoken (void) 212char *luaX_lasttoken (void)
234{ 213{
235 save(0); 214 save(L->lexstate, 0);
236 return textbuff.text; 215 return L->lexstate->textbuff.text;
237} 216}
238 217
239 218
240#define save_and_next() (save(current), next()) 219#define save_and_next(LL) (save(LL, LL->current), next(LL))
241 220
242 221
243static int read_long_string (void) 222static int read_long_string (LexState *LL, YYSTYPE *l)
244{ 223{
245 int cont = 0; 224 int cont = 0;
246 while (1) { 225 while (1) {
247 switch (current) { 226 switch (LL->current) {
248 case EOZ: 227 case EOZ:
249 save(0); 228 save(LL, 0);
250 return WRONGTOKEN; 229 return WRONGTOKEN;
251 case '[': 230 case '[':
252 save_and_next(); 231 save_and_next(LL);
253 if (current == '[') { 232 if (LL->current == '[') {
254 cont++; 233 cont++;
255 save_and_next(); 234 save_and_next(LL);
256 } 235 }
257 continue; 236 continue;
258 case ']': 237 case ']':
259 save_and_next(); 238 save_and_next(LL);
260 if (current == ']') { 239 if (LL->current == ']') {
261 if (cont == 0) goto endloop; 240 if (cont == 0) goto endloop;
262 cont--; 241 cont--;
263 save_and_next(); 242 save_and_next(LL);
264 } 243 }
265 continue; 244 continue;
266 case '\n': 245 case '\n':
267 save('\n'); 246 save(LL, '\n');
268 inclinenumber(); 247 inclinenumber(LL);
269 continue; 248 continue;
270 default: 249 default:
271 save_and_next(); 250 save_and_next(LL);
272 } 251 }
273 } endloop: 252 } endloop:
274 save_and_next(); /* pass the second ']' */ 253 save_and_next(LL); /* pass the second ']' */
275 textbuff.text[textbuff.tokensize-2] = 0; /* erases ']]' */ 254 LL->textbuff.text[LL->textbuff.tokensize-2] = 0; /* erases ']]' */
276 luaY_lval.pTStr = luaS_new(textbuff.text+2); 255 l->pTStr = luaS_new(LL->textbuff.text+2);
277 textbuff.text[textbuff.tokensize-2] = ']'; /* restores ']]' */ 256 LL->textbuff.text[LL->textbuff.tokensize-2] = ']'; /* restores ']]' */
278 return STRING; 257 return STRING;
279} 258}
280 259
281 260
282int luaY_lex (void) 261/* to avoid warnings; this declaration cannot be public since YYSTYPE
262** cannot be visible in llex.h (otherwise there is an error, since
263** the parser body redefines it!)
264*/
265int luaY_lex (YYSTYPE *l);
266int luaY_lex (YYSTYPE *l)
283{ 267{
284 static int linelasttoken = 0; 268 LexState *LL = L->lexstate;
285 double a; 269 double a;
286 textbuff.tokensize = 0; 270 LL->textbuff.tokensize = 0;
287 if (lua_debug) 271 if (lua_debug)
288 luaY_codedebugline(linelasttoken); 272 luaY_codedebugline(LL->linelasttoken);
289 linelasttoken = luaX_linenumber; 273 LL->linelasttoken = LL->linenumber;
290 while (1) { 274 while (1) {
291 switch (current) { 275 switch (LL->current) {
292 case '\n': 276 case '\n':
293 inclinenumber(); 277 inclinenumber(LL);
294 linelasttoken = luaX_linenumber; 278 LL->linelasttoken = LL->linenumber;
295 continue; 279 continue;
296 280
297 case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */ 281 case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
298 next(); 282 next(LL);
299 continue; 283 continue;
300 284
301 case '-': 285 case '-':
302 save_and_next(); 286 save_and_next(LL);
303 if (current != '-') return '-'; 287 if (LL->current != '-') return '-';
304 do { next(); } while (current != '\n' && current != EOZ); 288 do { next(LL); } while (LL->current != '\n' && LL->current != EOZ);
305 textbuff.tokensize = 0; 289 LL->textbuff.tokensize = 0;
306 continue; 290 continue;
307 291
308 case '[': 292 case '[':
309 save_and_next(); 293 save_and_next(LL);
310 if (current != '[') return '['; 294 if (LL->current != '[') return '[';
311 else { 295 else {
312 save_and_next(); /* pass the second '[' */ 296 save_and_next(LL); /* pass the second '[' */
313 return read_long_string(); 297 return read_long_string(LL, l);
314 } 298 }
315 299
316 case '=': 300 case '=':
317 save_and_next(); 301 save_and_next(LL);
318 if (current != '=') return '='; 302 if (LL->current != '=') return '=';
319 else { save_and_next(); return EQ; } 303 else { save_and_next(LL); return EQ; }
320 304
321 case '<': 305 case '<':
322 save_and_next(); 306 save_and_next(LL);
323 if (current != '=') return '<'; 307 if (LL->current != '=') return '<';
324 else { save_and_next(); return LE; } 308 else { save_and_next(LL); return LE; }
325 309
326 case '>': 310 case '>':
327 save_and_next(); 311 save_and_next(LL);
328 if (current != '=') return '>'; 312 if (LL->current != '=') return '>';
329 else { save_and_next(); return GE; } 313 else { save_and_next(LL); return GE; }
330 314
331 case '~': 315 case '~':
332 save_and_next(); 316 save_and_next(LL);
333 if (current != '=') return '~'; 317 if (LL->current != '=') return '~';
334 else { save_and_next(); return NE; } 318 else { save_and_next(LL); return NE; }
335 319
336 case '"': 320 case '"':
337 case '\'': { 321 case '\'': {
338 int del = current; 322 int del = LL->current;
339 save_and_next(); 323 save_and_next(LL);
340 while (current != del) { 324 while (LL->current != del) {
341 switch (current) { 325 switch (LL->current) {
342 case EOZ: 326 case EOZ:
343 case '\n': 327 case '\n':
344 save(0); 328 save(LL, 0);
345 return WRONGTOKEN; 329 return WRONGTOKEN;
346 case '\\': 330 case '\\':
347 next(); /* do not save the '\' */ 331 next(LL); /* do not save the '\' */
348 switch (current) { 332 switch (LL->current) {
349 case 'n': save('\n'); next(); break; 333 case 'n': save(LL, '\n'); next(LL); break;
350 case 't': save('\t'); next(); break; 334 case 't': save(LL, '\t'); next(LL); break;
351 case 'r': save('\r'); next(); break; 335 case 'r': save(LL, '\r'); next(LL); break;
352 case '\n': save('\n'); inclinenumber(); break; 336 case '\n': save(LL, '\n'); inclinenumber(LL); break;
353 default : save_and_next(); break; 337 default : save_and_next(LL); break;
354 } 338 }
355 break; 339 break;
356 default: 340 default:
357 save_and_next(); 341 save_and_next(LL);
358 } 342 }
359 } 343 }
360 next(); /* skip delimiter */ 344 next(LL); /* skip delimiter */
361 save(0); 345 save(LL, 0);
362 luaY_lval.pTStr = luaS_new(textbuff.text+1); 346 l->pTStr = luaS_new(LL->textbuff.text+1);
363 textbuff.text[textbuff.tokensize-1] = del; /* restore delimiter */ 347 LL->textbuff.text[LL->textbuff.tokensize-1] = del; /* restore delimiter */
364 return STRING; 348 return STRING;
365 } 349 }
366 350
367 case '.': 351 case '.':
368 save_and_next(); 352 save_and_next(LL);
369 if (current == '.') 353 if (LL->current == '.')
370 { 354 {
371 save_and_next(); 355 save_and_next(LL);
372 if (current == '.') 356 if (LL->current == '.')
373 { 357 {
374 save_and_next(); 358 save_and_next(LL);
375 return DOTS; /* ... */ 359 return DOTS; /* ... */
376 } 360 }
377 else return CONC; /* .. */ 361 else return CONC; /* .. */
378 } 362 }
379 else if (!isdigit(current)) return '.'; 363 else if (!isdigit(LL->current)) return '.';
380 /* current is a digit: goes through to number */ 364 /* LL->current is a digit: goes through to number */
381 a=0.0; 365 a=0.0;
382 goto fraction; 366 goto fraction;
383 367
@@ -385,69 +369,69 @@ int luaY_lex (void)
385 case '5': case '6': case '7': case '8': case '9': 369 case '5': case '6': case '7': case '8': case '9':
386 a=0.0; 370 a=0.0;
387 do { 371 do {
388 a=10.0*a+(current-'0'); 372 a=10.0*a+(LL->current-'0');
389 save_and_next(); 373 save_and_next(LL);
390 } while (isdigit(current)); 374 } while (isdigit(LL->current));
391 if (current == '.') { 375 if (LL->current == '.') {
392 save_and_next(); 376 save_and_next(LL);
393 if (current == '.') { 377 if (LL->current == '.') {
394 save(0); 378 save(LL, 0);
395 luaY_error( 379 luaY_error(
396 "ambiguous syntax (decimal point x string concatenation)"); 380 "ambiguous syntax (decimal point x string concatenation)");
397 } 381 }
398 } 382 }
399 fraction: 383 fraction:
400 { double da=0.1; 384 { double da=0.1;
401 while (isdigit(current)) 385 while (isdigit(LL->current))
402 { 386 {
403 a+=(current-'0')*da; 387 a+=(LL->current-'0')*da;
404 da/=10.0; 388 da/=10.0;
405 save_and_next(); 389 save_and_next(LL);
406 } 390 }
407 if (toupper(current) == 'E') { 391 if (toupper(LL->current) == 'E') {
408 int e=0; 392 int e=0;
409 int neg; 393 int neg;
410 double ea; 394 double ea;
411 save_and_next(); 395 save_and_next(LL);
412 neg=(current=='-'); 396 neg=(LL->current=='-');
413 if (current == '+' || current == '-') save_and_next(); 397 if (LL->current == '+' || LL->current == '-') save_and_next(LL);
414 if (!isdigit(current)) { 398 if (!isdigit(LL->current)) {
415 save(0); return WRONGTOKEN; } 399 save(LL, 0); return WRONGTOKEN; }
416 do { 400 do {
417 e=10.0*e+(current-'0'); 401 e=10.0*e+(LL->current-'0');
418 save_and_next(); 402 save_and_next(LL);
419 } while (isdigit(current)); 403 } while (isdigit(LL->current));
420 for (ea=neg?0.1:10.0; e>0; e>>=1) 404 for (ea=neg?0.1:10.0; e>0; e>>=1)
421 { 405 {
422 if (e & 1) a*=ea; 406 if (e & 1) a*=ea;
423 ea*=ea; 407 ea*=ea;
424 } 408 }
425 } 409 }
426 luaY_lval.vReal = a; 410 l->vReal = a;
427 return NUMBER; 411 return NUMBER;
428 } 412 }
429 413
430 case EOZ: 414 case EOZ:
431 save(0); 415 save(LL, 0);
432 if (iflevel > 0) 416 if (LL->iflevel > 0)
433 luaY_error("missing $endif"); 417 luaY_error("missing $endif");
434 return 0; 418 return 0;
435 419
436 default: 420 default:
437 if (current != '_' && !isalpha(current)) { 421 if (LL->current != '_' && !isalpha(LL->current)) {
438 save_and_next(); 422 save_and_next(LL);
439 return textbuff.text[0]; 423 return LL->textbuff.text[0];
440 } 424 }
441 else { /* identifier or reserved word */ 425 else { /* identifier or reserved word */
442 TaggedString *ts; 426 TaggedString *ts;
443 do { 427 do {
444 save_and_next(); 428 save_and_next(LL);
445 } while (isalnum(current) || current == '_'); 429 } while (isalnum(LL->current) || LL->current == '_');
446 save(0); 430 save(LL, 0);
447 ts = luaS_new(textbuff.text); 431 ts = luaS_new(LL->textbuff.text);
448 if (ts->head.marked > 255) 432 if (ts->head.marked > 255)
449 return ts->head.marked; /* reserved word */ 433 return ts->head.marked; /* reserved word */
450 luaY_lval.pTStr = ts; 434 l->pTStr = ts;
451 return NAME; 435 return NAME;
452 } 436 }
453 } 437 }
diff --git a/llex.h b/llex.h
index f6f7c18e..4f28cc24 100644
--- a/llex.h
+++ b/llex.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: llex.h,v 1.2 1997/11/04 15:27:53 roberto Exp roberto $
3** Lexical Analizer 3** Lexical Analizer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -11,11 +11,39 @@
11#include "lzio.h" 11#include "lzio.h"
12 12
13 13
14#define MAX_IFS 5
15
16/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
17
18struct ifState {
19 int elsepart; /* true if its in the $else part */
20 int condition; /* true if $if condition is true */
21 int skip; /* true if part must be skiped */
22};
23
24struct textBuff {
25 char *text;
26 int tokensize;
27 int buffsize;
28};
29
30
31typedef struct LexState {
32 int current; /* look ahead character */
33 struct zio *lex_z;
34 int linenumber;
35 struct ifState ifstate[MAX_IFS];
36 int iflevel; /* level of nested $if's (for lexical analysis) */
37 struct textBuff textbuff;
38 int linelasttoken;
39 int lastline;
40} LexState;
41
42
14extern int luaX_linenumber; 43extern int luaX_linenumber;
15 44
16 45
17void luaX_init (void); 46void luaX_init (void);
18int luaY_lex (void);
19void luaX_setinput (ZIO *z); 47void luaX_setinput (ZIO *z);
20char *luaX_lasttoken (void); 48char *luaX_lasttoken (void);
21 49
diff --git a/lmem.c b/lmem.c
index 8aeb9974..749fca5b 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: $ 2** $Id: lmem.c,v 1.1 1997/09/16 19:25:59 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*/
@@ -8,6 +8,7 @@
8#include <stdlib.h> 8#include <stdlib.h>
9 9
10#include "lmem.h" 10#include "lmem.h"
11#include "lstate.h"
11#include "lua.h" 12#include "lua.h"
12 13
13 14
@@ -25,24 +26,21 @@ int luaM_growaux (void **block, unsigned long nelems, int size,
25} 26}
26 27
27 28
28static unsigned long Mbuffsize = 0;
29static char *Mbuffer = NULL;
30
31 29
32void *luaM_buffer (unsigned long size) 30void *luaM_buffer (unsigned long size)
33{ 31{
34 if (size > Mbuffsize) { 32 if (size > L->Mbuffsize) {
35 Mbuffsize = size; 33 L->Mbuffsize = size;
36 Mbuffer = luaM_realloc(Mbuffer, Mbuffsize); 34 L->Mbuffer = luaM_realloc(L->Mbuffer, L->Mbuffsize);
37 } 35 }
38 return Mbuffer; 36 return L->Mbuffer;
39} 37}
40 38
41 39
42void luaM_clearbuffer (void) 40void luaM_clearbuffer (void)
43{ 41{
44 Mbuffsize /= 2; 42 L->Mbuffsize /= 2;
45 Mbuffer = luaM_realloc(Mbuffer, Mbuffsize); 43 L->Mbuffer = luaM_realloc(L->Mbuffer, L->Mbuffsize);
46} 44}
47 45
48 46
diff --git a/lobject.c b/lobject.c
index b38d3425..c575030d 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.5 1997/10/24 17:17:24 roberto Exp roberto $ 2** $Id: lobject.c,v 1.6 1997/11/03 20:45:23 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*/
@@ -21,9 +21,6 @@ TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
21 21
22 22
23 23
24unsigned long luaO_nblocks = 0;
25
26
27/* hash dimensions values */ 24/* hash dimensions values */
28static long dimensions[] = 25static long dimensions[] =
29 {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L, 26 {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
diff --git a/lobject.h b/lobject.h
index 6cf77303..45e27995 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.7 1997/10/24 17:17:24 roberto Exp roberto $ 2** $Id: lobject.h,v 1.8 1997/11/03 20:45:23 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*/
@@ -162,7 +162,6 @@ typedef struct Hash {
162** a gross estimation of number of memory "blocks" allocated 162** a gross estimation of number of memory "blocks" allocated
163** (a block is *roughly* 32 bytes) 163** (a block is *roughly* 32 bytes)
164*/ 164*/
165extern unsigned long luaO_nblocks;
166 165
167extern char *luaO_typenames[]; 166extern char *luaO_typenames[];
168 167
diff --git a/lstring.c b/lstring.c
index d29aa8f0..57a579f2 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.3 1997/10/23 16:26:37 roberto Exp roberto $ 2** $Id: lstring.c,v 1.4 1997/11/04 15:27:53 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*/
@@ -9,6 +9,7 @@
9 9
10#include "lmem.h" 10#include "lmem.h"
11#include "lobject.h" 11#include "lobject.h"
12#include "lstate.h"
12#include "lstring.h" 13#include "lstring.h"
13#include "lua.h" 14#include "lua.h"
14 15
@@ -19,18 +20,6 @@
19#define gcsizestring(l) (1+(l/64)) 20#define gcsizestring(l) (1+(l/64))
20 21
21 22
22GCnode luaS_root = {NULL, 0}; /* list of global variables */
23
24
25typedef struct {
26 int size;
27 int nuse; /* number of elements (including EMPTYs) */
28 TaggedString **hash;
29} stringtable;
30
31
32static stringtable string_root[NUM_HASHS];
33
34 23
35static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}}; 24static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}};
36 25
@@ -38,10 +27,11 @@ static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}};
38void luaS_init (void) 27void luaS_init (void)
39{ 28{
40 int i; 29 int i;
30 L->string_root = luaM_newvector(NUM_HASHS, stringtable);
41 for (i=0; i<NUM_HASHS; i++) { 31 for (i=0; i<NUM_HASHS; i++) {
42 string_root[i].size = 0; 32 L->string_root[i].size = 0;
43 string_root[i].nuse = 0; 33 L->string_root[i].nuse = 0;
44 string_root[i].hash = NULL; 34 L->string_root[i].hash = NULL;
45 } 35 }
46} 36}
47 37
@@ -93,14 +83,14 @@ static TaggedString *newone(char *buff, int tag, unsigned long h)
93 strcpy(ts->str, buff); 83 strcpy(ts->str, buff);
94 ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */ 84 ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */
95 ts->constindex = 0; 85 ts->constindex = 0;
96 luaO_nblocks += gcsizestring(l); 86 L->nblocks += gcsizestring(l);
97 } 87 }
98 else { 88 else {
99 ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)); 89 ts = (TaggedString *)luaM_malloc(sizeof(TaggedString));
100 ts->u.d.v = buff; 90 ts->u.d.v = buff;
101 ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag; 91 ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag;
102 ts->constindex = -1; /* tag -> this is a userdata */ 92 ts->constindex = -1; /* tag -> this is a userdata */
103 luaO_nblocks++; 93 L->nblocks++;
104 } 94 }
105 ts->head.marked = 0; 95 ts->head.marked = 0;
106 ts->head.next = (GCnode *)ts; /* signal it is in no list */ 96 ts->head.next = (GCnode *)ts; /* signal it is in no list */
@@ -138,12 +128,12 @@ static TaggedString *insert (char *buff, int tag, stringtable *tb)
138 128
139TaggedString *luaS_createudata (void *udata, int tag) 129TaggedString *luaS_createudata (void *udata, int tag)
140{ 130{
141 return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]); 131 return insert(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
142} 132}
143 133
144TaggedString *luaS_new (char *str) 134TaggedString *luaS_new (char *str)
145{ 135{
146 return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]); 136 return insert(str, LUA_T_STRING, &L->string_root[(unsigned)str[0]%NUM_HASHS]);
147} 137}
148 138
149TaggedString *luaS_newfixedstring (char *str) 139TaggedString *luaS_newfixedstring (char *str)
@@ -159,7 +149,7 @@ void luaS_free (TaggedString *l)
159{ 149{
160 while (l) { 150 while (l) {
161 TaggedString *next = (TaggedString *)l->head.next; 151 TaggedString *next = (TaggedString *)l->head.next;
162 luaO_nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str)); 152 L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str));
163 luaM_free(l); 153 luaM_free(l);
164 l = next; 154 l = next;
165 } 155 }
@@ -185,9 +175,9 @@ TaggedString *luaS_collector (void)
185{ 175{
186 TaggedString *frees = NULL; 176 TaggedString *frees = NULL;
187 int i; 177 int i;
188 remove_from_list(&luaS_root); 178 remove_from_list(&(L->rootglobal));
189 for (i=0; i<NUM_HASHS; i++) { 179 for (i=0; i<NUM_HASHS; i++) {
190 stringtable *tb = &string_root[i]; 180 stringtable *tb = &L->string_root[i];
191 int j; 181 int j;
192 for (j=0; j<tb->size; j++) { 182 for (j=0; j<tb->size; j++) {
193 TaggedString *t = tb->hash[j]; 183 TaggedString *t = tb->hash[j];
@@ -209,8 +199,8 @@ void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
209{ 199{
210 ts->u.globalval = *newval; 200 ts->u.globalval = *newval;
211 if (ts->head.next == (GCnode *)ts) { /* is not in list? */ 201 if (ts->head.next == (GCnode *)ts) { /* is not in list? */
212 ts->head.next = luaS_root.next; 202 ts->head.next = L->rootglobal.next;
213 luaS_root.next = (GCnode *)ts; 203 L->rootglobal.next = (GCnode *)ts;
214 } 204 }
215} 205}
216 206
@@ -218,7 +208,7 @@ void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
218char *luaS_travsymbol (int (*fn)(TObject *)) 208char *luaS_travsymbol (int (*fn)(TObject *))
219{ 209{
220 TaggedString *g; 210 TaggedString *g;
221 for (g=(TaggedString *)luaS_root.next; g; g=(TaggedString *)g->head.next) 211 for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
222 if (fn(&g->u.globalval)) 212 if (fn(&g->u.globalval))
223 return g->str; 213 return g->str;
224 return NULL; 214 return NULL;
diff --git a/lstring.h b/lstring.h
index fa606cf5..c842afe8 100644
--- a/lstring.h
+++ b/lstring.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.h,v 1.2 1997/09/26 15:02:26 roberto Exp roberto $ 2** $Id: lstring.h,v 1.3 1997/11/04 15:27:53 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*/
@@ -10,8 +10,6 @@
10 10
11#include "lobject.h" 11#include "lobject.h"
12 12
13extern GCnode luaS_root;
14
15 13
16void luaS_init (void); 14void luaS_init (void);
17TaggedString *luaS_createudata (void *udata, int tag); 15TaggedString *luaS_createudata (void *udata, int tag);
diff --git a/ltable.c b/ltable.c
index 4ac0caab..f0adc627 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $ 2** $Id: ltable.c,v 1.5 1997/10/24 17:17:24 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*/
@@ -9,6 +9,7 @@
9#include "lauxlib.h" 9#include "lauxlib.h"
10#include "lmem.h" 10#include "lmem.h"
11#include "lobject.h" 11#include "lobject.h"
12#include "lstate.h"
12#include "ltable.h" 13#include "ltable.h"
13#include "lua.h" 14#include "lua.h"
14 15
@@ -24,9 +25,6 @@
24#define TagDefault LUA_T_ARRAY; 25#define TagDefault LUA_T_ARRAY;
25 26
26 27
27GCnode luaH_root = {NULL, 0};
28
29
30 28
31static long int hashindex (TObject *ref) 29static long int hashindex (TObject *ref)
32{ 30{
@@ -95,7 +93,7 @@ void luaH_free (Hash *frees)
95{ 93{
96 while (frees) { 94 while (frees) {
97 Hash *next = (Hash *)frees->head.next; 95 Hash *next = (Hash *)frees->head.next;
98 luaO_nblocks -= gcsize(frees->nhash); 96 L->nblocks -= gcsize(frees->nhash);
99 hashdelete(frees); 97 hashdelete(frees);
100 frees = next; 98 frees = next;
101 } 99 }
@@ -110,8 +108,8 @@ Hash *luaH_new (int nhash)
110 nhash(t) = nhash; 108 nhash(t) = nhash;
111 nuse(t) = 0; 109 nuse(t) = 0;
112 t->htag = TagDefault; 110 t->htag = TagDefault;
113 luaO_insertlist(&luaH_root, (GCnode *)t); 111 luaO_insertlist(&(L->roottable), (GCnode *)t);
114 luaO_nblocks += gcsize(nhash); 112 L->nblocks += gcsize(nhash);
115 return t; 113 return t;
116} 114}
117 115
@@ -145,7 +143,7 @@ static void rehash (Hash *t)
145 if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) 143 if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
146 *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */ 144 *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
147 } 145 }
148 luaO_nblocks += gcsize(t->nhash)-gcsize(nold); 146 L->nblocks += gcsize(t->nhash)-gcsize(nold);
149 luaM_free(vold); 147 luaM_free(vold);
150} 148}
151 149
diff --git a/ltable.h b/ltable.h
index 58d09720..4fb66a91 100644
--- a/ltable.h
+++ b/ltable.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.h,v 1.2 1997/09/26 16:46:20 roberto Exp roberto $ 2** $Id: ltable.h,v 1.3 1997/10/18 16:29:15 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,9 +10,6 @@
10#include "lobject.h" 10#include "lobject.h"
11 11
12 12
13extern GCnode luaH_root;
14
15
16#define node(t,i) (&(t)->node[i]) 13#define node(t,i) (&(t)->node[i])
17#define ref(n) (&(n)->ref) 14#define ref(n) (&(n)->ref)
18#define val(n) (&(n)->val) 15#define val(n) (&(n)->val)
diff --git a/ltm.c b/ltm.c
index 1006aaeb..e19aab54 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.6 1997/11/04 15:27:53 roberto Exp roberto $ 2** $Id: ltm.c,v 1.7 1997/11/10 17:47:01 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*/
@@ -9,9 +9,9 @@
9#include <string.h> 9#include <string.h>
10 10
11#include "lauxlib.h" 11#include "lauxlib.h"
12#include "ldo.h"
13#include "lmem.h" 12#include "lmem.h"
14#include "lobject.h" 13#include "lobject.h"
14#include "lstate.h"
15#include "ltm.h" 15#include "ltm.h"
16 16
17 17
@@ -31,10 +31,6 @@ static int luaI_checkevent (char *name, char *list[])
31} 31}
32 32
33 33
34struct IM *luaT_IMtable;
35static int IMtable_size;
36static int last_tag;
37
38 34
39/* events in LUA_T_NIL are all allowed, since this is used as a 35/* events in LUA_T_NIL are all allowed, since this is used as a
40* 'placeholder' for "default" fallbacks 36* 'placeholder' for "default" fallbacks
@@ -67,34 +63,34 @@ static void init_entry (int tag)
67void luaT_init (void) 63void luaT_init (void)
68{ 64{
69 int t; 65 int t;
70 IMtable_size = NUM_TAGS*2; 66 L->IMtable_size = NUM_TAGS*2;
71 last_tag = -(NUM_TAGS-1); 67 L->last_tag = -(NUM_TAGS-1);
72 luaT_IMtable = luaM_newvector(IMtable_size, struct IM); 68 L->IMtable = luaM_newvector(L->IMtable_size, struct IM);
73 for (t=last_tag; t<=0; t++) 69 for (t=L->last_tag; t<=0; t++)
74 init_entry(t); 70 init_entry(t);
75} 71}
76 72
77 73
78int lua_newtag (void) 74int lua_newtag (void)
79{ 75{
80 --last_tag; 76 --L->last_tag;
81 if ((-last_tag) >= IMtable_size) 77 if ((-L->last_tag) >= L->IMtable_size)
82 IMtable_size = luaM_growvector(&luaT_IMtable, IMtable_size, 78 L->IMtable_size = luaM_growvector(&L->IMtable, L->IMtable_size,
83 struct IM, memEM, MAX_INT); 79 struct IM, memEM, MAX_INT);
84 init_entry(last_tag); 80 init_entry(L->last_tag);
85 return last_tag; 81 return L->last_tag;
86} 82}
87 83
88 84
89static void checktag (int tag) 85static void checktag (int tag)
90{ 86{
91 if (!(last_tag <= tag && tag <= 0)) 87 if (!(L->last_tag <= tag && tag <= 0))
92 luaL_verror("%d is not a valid tag", tag); 88 luaL_verror("%d is not a valid tag", tag);
93} 89}
94 90
95void luaT_realtag (int tag) 91void luaT_realtag (int tag)
96{ 92{
97 if (!(last_tag <= tag && tag < LUA_T_NIL)) 93 if (!(L->last_tag <= tag && tag < LUA_T_NIL))
98 luaL_verror("tag %d is not result of `newtag'", tag); 94 luaL_verror("tag %d is not result of `newtag'", tag);
99} 95}
100 96
@@ -145,11 +141,11 @@ void luaT_settagmethod (int t, char *event, TObject *func)
145char *luaT_travtagmethods (int (*fn)(TObject *)) 141char *luaT_travtagmethods (int (*fn)(TObject *))
146{ 142{
147 int e; 143 int e;
148 if (fn(&luaD_errorim)) 144 if (fn(&L->errorim))
149 return "error"; 145 return "error";
150 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */ 146 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
151 int t; 147 int t;
152 for (t=0; t>=last_tag; t--) 148 for (t=0; t>=L->last_tag; t--)
153 if (fn(luaT_getim(t,e))) 149 if (fn(luaT_getim(t,e)))
154 return luaT_eventname[e]; 150 return luaT_eventname[e];
155 } 151 }
@@ -203,8 +199,8 @@ void luaT_setfallback (void)
203 luaL_arg_check(lua_isfunction(func), 2, "function expected"); 199 luaL_arg_check(lua_isfunction(func), 2, "function expected");
204 switch (luaO_findstring(name, oldnames)) { 200 switch (luaO_findstring(name, oldnames)) {
205 case 0: /* old error fallback */ 201 case 0: /* old error fallback */
206 oldfunc = luaD_errorim; 202 oldfunc = L->errorim;
207 luaD_errorim = *luaA_Address(func); 203 L->errorim = *luaA_Address(func);
208 replace = errorFB; 204 replace = errorFB;
209 break; 205 break;
210 case 1: /* old getglobal fallback */ 206 case 1: /* old getglobal fallback */
diff --git a/ltm.h b/ltm.h
index bad12fa9..4b52fe94 100644
--- a/ltm.h
+++ b/ltm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: ltm.h,v 1.2 1997/11/04 15:27:53 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*/
@@ -9,6 +9,7 @@
9 9
10 10
11#include "lobject.h" 11#include "lobject.h"
12#include "lstate.h"
12 13
13/* 14/*
14* WARNING: if you change the order of this enumeration, 15* WARNING: if you change the order of this enumeration,
@@ -38,12 +39,12 @@ typedef enum {
38#define IM_N 18 39#define IM_N 18
39 40
40 41
41extern struct IM { 42struct IM {
42 TObject int_method[IM_N]; 43 TObject int_method[IM_N];
43} *luaT_IMtable; 44};
44 45
45 46
46#define luaT_getim(tag,event) (&luaT_IMtable[-(tag)].int_method[event]) 47#define luaT_getim(tag,event) (&L->IMtable[-(tag)].int_method[event])
47#define luaT_getimbyObj(o,e) (luaT_getim(luaT_efectivetag(o),(e))) 48#define luaT_getimbyObj(o,e) (luaT_getim(luaT_efectivetag(o),(e)))
48 49
49extern char *luaT_eventname[]; 50extern char *luaT_eventname[];
diff --git a/lua.c b/lua.c
index ce3b4ec7..0eb21982 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.2 1997/10/06 14:51:32 roberto Exp roberto $ 2** $Id: lua.c,v 1.3 1997/10/16 18:35:59 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*/
@@ -82,6 +82,7 @@ int main (int argc, char *argv[])
82 } 82 }
83 } 83 }
84 } 84 }
85/* lua_close(); */
85 return 0; 86 return 0;
86} 87}
87 88
diff --git a/lua.h b/lua.h
index 4fa70b57..48fd80cf 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.2 1997/10/24 17:17:24 roberto Exp roberto $ 2** $Id: lua.h,v 1.3 1997/11/04 15:27:53 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
@@ -117,6 +117,8 @@ int lua_ref (int lock); /* In: value */
117lua_Object lua_getref (int ref); 117lua_Object lua_getref (int ref);
118void lua_unref (int ref); 118void lua_unref (int ref);
119 119
120lua_Object lua_globalbag (void);
121
120lua_Object lua_createtable (void); 122lua_Object lua_createtable (void);
121 123
122long lua_collectgarbage (long limit); 124long lua_collectgarbage (long limit);
diff --git a/lua.stx b/lua.stx
index 65d1337f..3d6dbaa7 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2/* 2/*
3** $Id: lua.stx,v 1.16 1997/10/30 18:47:19 roberto Exp roberto $ 3** $Id: lua.stx,v 1.17 1997/11/07 15:09:49 roberto Exp roberto $
4** Syntax analizer and code generator 4** Syntax analizer and code generator
5** See Copyright Notice in lua.h 5** See Copyright Notice in lua.h
6*/ 6*/
@@ -16,13 +16,13 @@
16#include "lmem.h" 16#include "lmem.h"
17#include "lopcodes.h" 17#include "lopcodes.h"
18#include "lparser.h" 18#include "lparser.h"
19#include "lstate.h"
19#include "lstring.h" 20#include "lstring.h"
20#include "lua.h" 21#include "lua.h"
21#include "luadebug.h" 22#include "luadebug.h"
22#include "lzio.h" 23#include "lzio.h"
23 24
24 25
25/* to avoid warnings generated by yacc */
26int luaY_parse (void); 26int luaY_parse (void);
27 27
28 28
@@ -61,7 +61,7 @@ typedef long vardesc;
61#define dotindex(v) ((-(v))-1) 61#define dotindex(v) ((-(v))-1)
62 62
63/* state needed to generate code for a given function */ 63/* state needed to generate code for a given function */
64typedef struct State { 64typedef struct FuncState {
65 TProtoFunc *f; /* current function header */ 65 TProtoFunc *f; /* current function header */
66 int pc; /* next position to code */ 66 int pc; /* next position to code */
67 TaggedString *localvar[MAXLOCALS]; /* store local variable names */ 67 TaggedString *localvar[MAXLOCALS]; /* store local variable names */
@@ -75,11 +75,11 @@ typedef struct State {
75 int maxconsts; /* size of f->consts */ 75 int maxconsts; /* size of f->consts */
76 vardesc varbuffer[MAXVAR]; /* variables in an assignment list */ 76 vardesc varbuffer[MAXVAR]; /* variables in an assignment list */
77 vardesc upvalues[MAXUPVALUES]; /* upvalues */ 77 vardesc upvalues[MAXUPVALUES]; /* upvalues */
78} State; 78} FuncState;
79 79
80static State *mainState, *currState;
81 80
82 81
82#define YYPURE 1
83 83
84 84
85void luaY_syntaxerror (char *s, char *token) 85void luaY_syntaxerror (char *s, char *token)
@@ -87,7 +87,7 @@ void luaY_syntaxerror (char *s, char *token)
87 if (token[0] == 0) 87 if (token[0] == 0)
88 token = "<eof>"; 88 token = "<eof>";
89 luaL_verror("%.100s;\n> last token read: \"%.50s\" at line %d in file %.50s", 89 luaL_verror("%.100s;\n> last token read: \"%.50s\" at line %d in file %.50s",
90 s, token, luaX_linenumber, mainState->f->fileName->str); 90 s, token, L->lexstate->linenumber, L->mainState->f->fileName->str);
91} 91}
92 92
93 93
@@ -99,16 +99,16 @@ void luaY_error (char *s)
99 99
100static void check_pc (int n) 100static void check_pc (int n)
101{ 101{
102 if (currState->pc+n > currState->maxcode) 102 if (L->currState->pc+n > L->currState->maxcode)
103 currState->maxcode = luaM_growvector(&currState->f->code, 103 L->currState->maxcode = luaM_growvector(&L->currState->f->code,
104 currState->maxcode, Byte, codeEM, MAX_INT); 104 L->currState->maxcode, Byte, codeEM, MAX_INT);
105} 105}
106 106
107 107
108static void movecode_up (int d, int s, int n) 108static void movecode_up (int d, int s, int n)
109{ 109{
110 while (n--) 110 while (n--)
111 currState->f->code[d+n] = currState->f->code[s+n]; 111 L->currState->f->code[d+n] = L->currState->f->code[s+n];
112} 112}
113 113
114 114
@@ -116,24 +116,24 @@ static void movecode_down (int d, int s, int n)
116{ 116{
117 int i; 117 int i;
118 for (i=0; i<n; i++) 118 for (i=0; i<n; i++)
119 currState->f->code[d+i] = currState->f->code[s+i]; 119 L->currState->f->code[d+i] = L->currState->f->code[s+i];
120} 120}
121 121
122 122
123static void code_byte (Byte c) 123static void code_byte (Byte c)
124{ 124{
125 check_pc(1); 125 check_pc(1);
126 currState->f->code[currState->pc++] = c; 126 L->currState->f->code[L->currState->pc++] = c;
127} 127}
128 128
129 129
130static void deltastack (int delta) 130static void deltastack (int delta)
131{ 131{
132 currState->stacksize += delta; 132 L->currState->stacksize += delta;
133 if (currState->stacksize > currState->maxstacksize) { 133 if (L->currState->stacksize > L->currState->maxstacksize) {
134 if (currState->stacksize > 255) 134 if (L->currState->stacksize > 255)
135 luaY_error("function/expression too complex (limit 256)"); 135 luaY_error("function/expression too complex (limit 256)");
136 currState->maxstacksize = currState->stacksize; 136 L->currState->maxstacksize = L->currState->stacksize;
137 } 137 }
138} 138}
139 139
@@ -142,18 +142,18 @@ static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
142{ 142{
143 deltastack(delta); 143 deltastack(delta);
144 if (arg < builtin) { 144 if (arg < builtin) {
145 currState->f->code[pc] = op+1+arg; 145 L->currState->f->code[pc] = op+1+arg;
146 return 1; 146 return 1;
147 } 147 }
148 else if (arg <= 255) { 148 else if (arg <= 255) {
149 currState->f->code[pc] = op; 149 L->currState->f->code[pc] = op;
150 currState->f->code[pc+1] = arg; 150 L->currState->f->code[pc+1] = arg;
151 return 2; 151 return 2;
152 } 152 }
153 else if (arg <= MAX_WORD) { 153 else if (arg <= MAX_WORD) {
154 currState->f->code[pc] = op+1+builtin; 154 L->currState->f->code[pc] = op+1+builtin;
155 currState->f->code[pc+1] = arg&0xFF; 155 L->currState->f->code[pc+1] = arg&0xFF;
156 currState->f->code[pc+2] = arg>>8; 156 L->currState->f->code[pc+2] = arg>>8;
157 return 3; 157 return 3;
158 } 158 }
159 else luaY_error("code too long (limit 64K)"); 159 else luaY_error("code too long (limit 64K)");
@@ -164,13 +164,13 @@ static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
164static int fix_opcode (int pc, OpCode op, int builtin, int arg) 164static int fix_opcode (int pc, OpCode op, int builtin, int arg)
165{ 165{
166 if (arg < builtin) { /* close space */ 166 if (arg < builtin) { /* close space */
167 movecode_down(pc+1, pc+2, currState->pc-(pc+2)); 167 movecode_down(pc+1, pc+2, L->currState->pc-(pc+2));
168 currState->pc--; 168 L->currState->pc--;
169 } 169 }
170 else if (arg > 255) { /* open space */ 170 else if (arg > 255) { /* open space */
171 check_pc(1); 171 check_pc(1);
172 movecode_up(pc+1, pc, currState->pc-pc); 172 movecode_up(pc+1, pc, L->currState->pc-pc);
173 currState->pc++; 173 L->currState->pc++;
174 } 174 }
175 return code_oparg_at(pc, op, builtin, arg, 0) - 2; 175 return code_oparg_at(pc, op, builtin, arg, 0) - 2;
176} 176}
@@ -179,7 +179,7 @@ static int fix_opcode (int pc, OpCode op, int builtin, int arg)
179static void code_oparg (OpCode op, int builtin, int arg, int delta) 179static void code_oparg (OpCode op, int builtin, int arg, int delta)
180{ 180{
181 check_pc(3); /* maximum code size */ 181 check_pc(3); /* maximum code size */
182 currState->pc += code_oparg_at(currState->pc, op, builtin, arg, delta); 182 L->currState->pc += code_oparg_at(L->currState->pc, op, builtin, arg, delta);
183} 183}
184 184
185 185
@@ -214,7 +214,7 @@ static void code_constant (int c)
214} 214}
215 215
216 216
217static int next_constant (State *cs) 217static int next_constant (FuncState *cs)
218{ 218{
219 TProtoFunc *f = cs->f; 219 TProtoFunc *f = cs->f;
220 if (f->nconsts >= cs->maxconsts) { 220 if (f->nconsts >= cs->maxconsts) {
@@ -225,7 +225,7 @@ static int next_constant (State *cs)
225} 225}
226 226
227 227
228static int string_constant (TaggedString *s, State *cs) 228static int string_constant (TaggedString *s, FuncState *cs)
229{ 229{
230 TProtoFunc *f = cs->f; 230 TProtoFunc *f = cs->f;
231 int c = s->constindex; 231 int c = s->constindex;
@@ -242,7 +242,7 @@ static int string_constant (TaggedString *s, State *cs)
242 242
243static void code_string (TaggedString *s) 243static void code_string (TaggedString *s)
244{ 244{
245 code_constant(string_constant(s, currState)); 245 code_constant(string_constant(s, L->currState));
246} 246}
247 247
248 248
@@ -250,16 +250,16 @@ static void code_string (TaggedString *s)
250static int real_constant (real r) 250static int real_constant (real r)
251{ 251{
252 /* check whether 'r' has appeared within the last LIM entries */ 252 /* check whether 'r' has appeared within the last LIM entries */
253 TObject *cnt = currState->f->consts; 253 TObject *cnt = L->currState->f->consts;
254 int c = currState->f->nconsts; 254 int c = L->currState->f->nconsts;
255 int lim = c < LIM ? 0 : c-LIM; 255 int lim = c < LIM ? 0 : c-LIM;
256 while (--c >= lim) { 256 while (--c >= lim) {
257 if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r) 257 if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
258 return c; 258 return c;
259 } 259 }
260 /* not found; create a luaM_new entry */ 260 /* not found; create a luaM_new entry */
261 c = next_constant(currState); 261 c = next_constant(L->currState);
262 cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */ 262 cnt = L->currState->f->consts; /* 'next_constant' may reallocate this vector */
263 ttype(&cnt[c]) = LUA_T_NUMBER; 263 ttype(&cnt[c]) = LUA_T_NUMBER;
264 nvalue(&cnt[c]) = r; 264 nvalue(&cnt[c]) = r;
265 return c; 265 return c;
@@ -292,13 +292,13 @@ static void flush_list (int m, int n)
292 292
293static void luaI_registerlocalvar (TaggedString *varname, int line) 293static void luaI_registerlocalvar (TaggedString *varname, int line)
294{ 294{
295 if (currState->maxvars != -1) { /* debug information? */ 295 if (L->currState->maxvars != -1) { /* debug information? */
296 if (currState->nvars >= currState->maxvars) 296 if (L->currState->nvars >= L->currState->maxvars)
297 currState->maxvars = luaM_growvector(&currState->f->locvars, 297 L->currState->maxvars = luaM_growvector(&L->currState->f->locvars,
298 currState->maxvars, LocVar, "", MAX_WORD); 298 L->currState->maxvars, LocVar, "", MAX_WORD);
299 currState->f->locvars[currState->nvars].varname = varname; 299 L->currState->f->locvars[L->currState->nvars].varname = varname;
300 currState->f->locvars[currState->nvars].line = line; 300 L->currState->f->locvars[L->currState->nvars].line = line;
301 currState->nvars++; 301 L->currState->nvars++;
302 } 302 }
303} 303}
304 304
@@ -311,17 +311,17 @@ static void luaI_unregisterlocalvar (int line)
311 311
312static void store_localvar (TaggedString *name, int n) 312static void store_localvar (TaggedString *name, int n)
313{ 313{
314 if (currState->nlocalvar+n < MAXLOCALS) 314 if (L->currState->nlocalvar+n < MAXLOCALS)
315 currState->localvar[currState->nlocalvar+n] = name; 315 L->currState->localvar[L->currState->nlocalvar+n] = name;
316 else 316 else
317 luaY_error("too many local variables (limit 32)"); 317 luaY_error("too many local variables (limit 32)");
318 luaI_registerlocalvar(name, luaX_linenumber); 318 luaI_registerlocalvar(name, L->lexstate->linenumber);
319} 319}
320 320
321static void add_localvar (TaggedString *name) 321static void add_localvar (TaggedString *name)
322{ 322{
323 store_localvar(name, 0); 323 store_localvar(name, 0);
324 currState->nlocalvar++; 324 L->currState->nlocalvar++;
325} 325}
326 326
327 327
@@ -342,11 +342,11 @@ static void add_varbuffer (vardesc var, int n)
342{ 342{
343 if (n >= MAXVAR) 343 if (n >= MAXVAR)
344 luaY_error("variable buffer overflow (limit 32)"); 344 luaY_error("variable buffer overflow (limit 32)");
345 currState->varbuffer[n] = var2store(var); 345 L->currState->varbuffer[n] = var2store(var);
346} 346}
347 347
348 348
349static int aux_localname (TaggedString *n, State *st) 349static int aux_localname (TaggedString *n, FuncState *st)
350{ 350{
351 int i; 351 int i;
352 for (i=st->nlocalvar-1; i >= 0; i--) 352 for (i=st->nlocalvar-1; i >= 0; i--)
@@ -355,12 +355,12 @@ static int aux_localname (TaggedString *n, State *st)
355} 355}
356 356
357 357
358static vardesc singlevar (TaggedString *n, State *st) 358static vardesc singlevar (TaggedString *n, FuncState *st)
359{ 359{
360 int i = aux_localname(n, st); 360 int i = aux_localname(n, st);
361 if (i == -1) { /* check shadowing */ 361 if (i == -1) { /* check shadowing */
362 int l; 362 int l;
363 for (l=1; l<=(st-mainState); l++) 363 for (l=1; l<=(st-L->mainState); l++)
364 if (aux_localname(n, st-l) >= 0) 364 if (aux_localname(n, st-l) >= 0)
365 luaY_syntaxerror("cannot access a variable in outer scope", n->str); 365 luaY_syntaxerror("cannot access a variable in outer scope", n->str);
366 return string_constant(n, st)+MINGLOBAL; /* global value */ 366 return string_constant(n, st)+MINGLOBAL; /* global value */
@@ -371,16 +371,16 @@ static vardesc singlevar (TaggedString *n, State *st)
371 371
372static int indexupvalue (TaggedString *n) 372static int indexupvalue (TaggedString *n)
373{ 373{
374 vardesc v = singlevar(n, currState-1); 374 vardesc v = singlevar(n, L->currState-1);
375 int i; 375 int i;
376 for (i=0; i<currState->nupvalues; i++) { 376 for (i=0; i<L->currState->nupvalues; i++) {
377 if (currState->upvalues[i] == v) 377 if (L->currState->upvalues[i] == v)
378 return i; 378 return i;
379 } 379 }
380 /* new one */ 380 /* new one */
381 if (++(currState->nupvalues) > MAXUPVALUES) 381 if (++(L->currState->nupvalues) > MAXUPVALUES)
382 luaY_error("too many upvalues in a single function (limit 16)"); 382 luaY_error("too many upvalues in a single function (limit 16)");
383 currState->upvalues[i] = v; /* i = currState->nupvalues - 1 */ 383 L->currState->upvalues[i] = v; /* i = L->currState->nupvalues - 1 */
384 return i; 384 return i;
385} 385}
386 386
@@ -388,9 +388,9 @@ static int indexupvalue (TaggedString *n)
388static void pushupvalue (TaggedString *n) 388static void pushupvalue (TaggedString *n)
389{ 389{
390 int i; 390 int i;
391 if (currState == mainState) 391 if (L->currState == L->mainState)
392 luaY_error("cannot access upvalue in main"); 392 luaY_error("cannot access upvalue in main");
393 if (aux_localname(n, currState) >= 0) 393 if (aux_localname(n, L->currState) >= 0)
394 luaY_syntaxerror("cannot access an upvalue in current scope", n->str); 394 luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
395 i = indexupvalue(n); 395 i = indexupvalue(n);
396 code_oparg(PUSHUPVALUE, 2, i, 1); 396 code_oparg(PUSHUPVALUE, 2, i, 1);
@@ -399,10 +399,9 @@ static void pushupvalue (TaggedString *n)
399 399
400void luaY_codedebugline (int line) 400void luaY_codedebugline (int line)
401{ 401{
402 static int lastline = 0; 402 if (lua_debug && line != L->lexstate->lastline) {
403 if (lua_debug && line != lastline) {
404 code_oparg(SETLINE, 0, line, 0); 403 code_oparg(SETLINE, 0, line, 0);
405 lastline = line; 404 L->lexstate->lastline = line;
406 } 405 }
407} 406}
408 407
@@ -421,10 +420,10 @@ static long adjust_functioncall (long exp, int nresults)
421 if (exp <= 0) 420 if (exp <= 0)
422 return -exp; /* exp is -list length */ 421 return -exp; /* exp is -list length */
423 else { 422 else {
424 int temp = currState->f->code[exp]; 423 int temp = L->currState->f->code[exp];
425 int nparams = currState->f->code[exp-1]; 424 int nparams = L->currState->f->code[exp-1];
426 exp += fix_opcode(exp-2, CALLFUNC, 2, nresults); 425 exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
427 currState->f->code[exp] = nparams; 426 L->currState->f->code[exp] = nparams;
428 if (nresults != MULT_RET) 427 if (nresults != MULT_RET)
429 deltastack(nresults); 428 deltastack(nresults);
430 deltastack(-(nparams+1)); 429 deltastack(-(nparams+1));
@@ -436,7 +435,7 @@ static long adjust_functioncall (long exp, int nresults)
436static void adjust_mult_assign (int vars, long exps) 435static void adjust_mult_assign (int vars, long exps)
437{ 436{
438 if (exps > 0) { /* must correct function call */ 437 if (exps > 0) { /* must correct function call */
439 int diff = currState->f->code[exps] - vars; 438 int diff = L->currState->f->code[exps] - vars;
440 if (diff < 0) 439 if (diff < 0)
441 adjust_functioncall(exps, -diff); 440 adjust_functioncall(exps, -diff);
442 else { 441 else {
@@ -450,11 +449,11 @@ static void adjust_mult_assign (int vars, long exps)
450 449
451static void code_args (int nparams, int dots) 450static void code_args (int nparams, int dots)
452{ 451{
453 currState->nlocalvar += nparams; 452 L->currState->nlocalvar += nparams;
454 if (!dots) 453 if (!dots)
455 code_oparg(ARGS, 0, currState->nlocalvar, currState->nlocalvar); 454 code_oparg(ARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar);
456 else { 455 else {
457 code_oparg(VARARGS, 0, currState->nlocalvar, currState->nlocalvar+1); 456 code_oparg(VARARGS, 0, L->currState->nlocalvar, L->currState->nlocalvar+1);
458 add_localvar(luaS_new("arg")); 457 add_localvar(luaS_new("arg"));
459 } 458 }
460} 459}
@@ -487,9 +486,9 @@ static void storevar (vardesc var)
487/* returns how many elements are left as 'garbage' on the stack */ 486/* returns how many elements are left as 'garbage' on the stack */
488static int lua_codestore (int i, int left) 487static int lua_codestore (int i, int left)
489{ 488{
490 if (currState->varbuffer[i] != 0 || /* global or local var or */ 489 if (L->currState->varbuffer[i] != 0 || /* global or local var or */
491 left+i == 0) { /* indexed var without values in between */ 490 left+i == 0) { /* indexed var without values in between */
492 storevar(currState->varbuffer[i]); 491 storevar(L->currState->varbuffer[i]);
493 return left; 492 return left;
494 } 493 }
495 else { /* indexed var with values in between*/ 494 else { /* indexed var with values in between*/
@@ -508,7 +507,7 @@ static int fix_jump (int pc, OpCode op, int n)
508 507
509static void fix_upjmp (OpCode op, int pos) 508static void fix_upjmp (OpCode op, int pos)
510{ 509{
511 int delta = currState->pc+JMPSIZE - pos; /* jump is relative */ 510 int delta = L->currState->pc+JMPSIZE - pos; /* jump is relative */
512 if (delta > 255) delta++; 511 if (delta > 255) delta++;
513 code_oparg(op, 0, delta, 0); 512 code_oparg(op, 0, delta, 0);
514} 513}
@@ -517,38 +516,38 @@ static void fix_upjmp (OpCode op, int pos)
517static void codeIf (int thenAdd, int elseAdd) 516static void codeIf (int thenAdd, int elseAdd)
518{ 517{
519 int elseinit = elseAdd+JMPSIZE; 518 int elseinit = elseAdd+JMPSIZE;
520 if (currState->pc == elseinit) { /* no else part */ 519 if (L->currState->pc == elseinit) { /* no else part */
521 currState->pc -= JMPSIZE; 520 L->currState->pc -= JMPSIZE;
522 elseinit = currState->pc; 521 elseinit = L->currState->pc;
523 } 522 }
524 else 523 else
525 elseinit += fix_jump(elseAdd, JMP, currState->pc); 524 elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
526 fix_jump(thenAdd, IFFJMP, elseinit); 525 fix_jump(thenAdd, IFFJMP, elseinit);
527} 526}
528 527
529 528
530static void code_shortcircuit (int pc, OpCode op) 529static void code_shortcircuit (int pc, OpCode op)
531{ 530{
532 fix_jump(pc, op, currState->pc); 531 fix_jump(pc, op, L->currState->pc);
533} 532}
534 533
535 534
536static void codereturn (void) 535static void codereturn (void)
537{ 536{
538 code_oparg(RETCODE, 0, currState->nlocalvar, 0); 537 code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
539 currState->stacksize = currState->nlocalvar; 538 L->currState->stacksize = L->currState->nlocalvar;
540} 539}
541 540
542 541
543static void func_onstack (TProtoFunc *f) 542static void func_onstack (TProtoFunc *f)
544{ 543{
545 int i; 544 int i;
546 int nupvalues = (currState+1)->nupvalues; 545 int nupvalues = (L->currState+1)->nupvalues;
547 int c = next_constant(currState); 546 int c = next_constant(L->currState);
548 ttype(&currState->f->consts[c]) = LUA_T_PROTO; 547 ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
549 currState->f->consts[c].value.tf = (currState+1)->f; 548 L->currState->f->consts[c].value.tf = (L->currState+1)->f;
550 for (i=0; i<nupvalues; i++) 549 for (i=0; i<nupvalues; i++)
551 lua_pushvar((currState+1)->upvalues[i]); 550 lua_pushvar((L->currState+1)->upvalues[i]);
552 code_constant(c); 551 code_constant(c);
553 code_oparg(CLOSURE, 2, nupvalues, -nupvalues); 552 code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
554} 553}
@@ -557,48 +556,48 @@ static void func_onstack (TProtoFunc *f)
557static void init_state (TaggedString *filename) 556static void init_state (TaggedString *filename)
558{ 557{
559 TProtoFunc *f = luaF_newproto(); 558 TProtoFunc *f = luaF_newproto();
560 currState->stacksize = 0; 559 L->currState->stacksize = 0;
561 currState->maxstacksize = 0; 560 L->currState->maxstacksize = 0;
562 currState->nlocalvar = 0; 561 L->currState->nlocalvar = 0;
563 currState->nupvalues = 0; 562 L->currState->nupvalues = 0;
564 currState->f = f; 563 L->currState->f = f;
565 f->fileName = filename; 564 f->fileName = filename;
566 currState->pc = 0; 565 L->currState->pc = 0;
567 currState->maxcode = 0; 566 L->currState->maxcode = 0;
568 f->code = NULL; 567 f->code = NULL;
569 currState->maxconsts = 0; 568 L->currState->maxconsts = 0;
570 if (lua_debug) { 569 if (lua_debug) {
571 currState->nvars = 0; 570 L->currState->nvars = 0;
572 currState->maxvars = 0; 571 L->currState->maxvars = 0;
573 } 572 }
574 else 573 else
575 currState->maxvars = -1; /* flag no debug information */ 574 L->currState->maxvars = -1; /* flag no debug information */
576 code_byte(0); /* to be filled with stacksize */ 575 code_byte(0); /* to be filled with stacksize */
577} 576}
578 577
579 578
580static void init_func (void) 579static void init_func (void)
581{ 580{
582 if (currState-mainState >= MAXSTATES-1) 581 if (L->currState-L->mainState >= MAXSTATES-1)
583 luaY_error("too many nested functions (limit 6)"); 582 luaY_error("too many nested functions (limit 6)");
584 currState++; 583 L->currState++;
585 init_state(mainState->f->fileName); 584 init_state(L->mainState->f->fileName);
586 luaY_codedebugline(luaX_linenumber); 585 luaY_codedebugline(L->lexstate->linenumber);
587 currState->f->lineDefined = luaX_linenumber; 586 L->currState->f->lineDefined = L->lexstate->linenumber;
588} 587}
589 588
590static TProtoFunc *close_func (void) 589static TProtoFunc *close_func (void)
591{ 590{
592 TProtoFunc *f = currState->f; 591 TProtoFunc *f = L->currState->f;
593 code_neutralop(ENDCODE); 592 code_neutralop(ENDCODE);
594 f->code[0] = currState->maxstacksize; 593 f->code[0] = L->currState->maxstacksize;
595 f->code = luaM_reallocvector(f->code, currState->pc, Byte); 594 f->code = luaM_reallocvector(f->code, L->currState->pc, Byte);
596 f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject); 595 f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
597 if (currState->maxvars != -1) { /* debug information? */ 596 if (L->currState->maxvars != -1) { /* debug information? */
598 luaI_registerlocalvar(NULL, -1); /* flag end of vector */ 597 luaI_registerlocalvar(NULL, -1); /* flag end of vector */
599 f->locvars = luaM_reallocvector(f->locvars, currState->nvars, LocVar); 598 f->locvars = luaM_reallocvector(f->locvars, L->currState->nvars, LocVar);
600 } 599 }
601 currState--; 600 L->currState--;
602 return f; 601 return f;
603} 602}
604 603
@@ -608,8 +607,10 @@ static TProtoFunc *close_func (void)
608*/ 607*/
609TProtoFunc *luaY_parser (ZIO *z, char *chunkname) 608TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
610{ 609{
611 State state[MAXSTATES]; 610 struct LexState lexstate;
612 currState = mainState = &state[0]; 611 FuncState state[MAXSTATES];
612 L->currState = L->mainState = &state[0];
613 L->lexstate = &lexstate;
613 luaX_setinput(z); 614 luaX_setinput(z);
614 init_state(luaS_new(chunkname)); 615 init_state(luaS_new(chunkname));
615 if (luaY_parse ()) lua_error("parse error"); 616 if (luaY_parse ()) lua_error("parse error");
@@ -685,10 +686,10 @@ stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
685 int expsize = $3-$2; 686 int expsize = $3-$2;
686 int newpos = $2+JMPSIZE; 687 int newpos = $2+JMPSIZE;
687 check_pc(expsize); 688 check_pc(expsize);
688 memcpy(&currState->f->code[currState->pc], 689 memcpy(&L->currState->f->code[L->currState->pc],
689 &currState->f->code[$2], expsize); 690 &L->currState->f->code[$2], expsize);
690 movecode_down($2, $3, currState->pc-$2); 691 movecode_down($2, $3, L->currState->pc-$2);
691 newpos += fix_jump($2, JMP, currState->pc-expsize); 692 newpos += fix_jump($2, JMP, L->currState->pc-expsize);
692 fix_upjmp(IFTUPJMP, newpos); 693 fix_upjmp(IFTUPJMP, newpos);
693 }} 694 }}
694 695
@@ -712,18 +713,18 @@ stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
712 713
713 | LOCAL localnamelist decinit 714 | LOCAL localnamelist decinit
714 { 715 {
715 currState->nlocalvar += $2; 716 L->currState->nlocalvar += $2;
716 adjust_mult_assign($2, $3); 717 adjust_mult_assign($2, $3);
717 } 718 }
718 719
719 | FUNCTION funcname body { func_onstack($3); storevar($2); } 720 | FUNCTION funcname body { func_onstack($3); storevar($2); }
720 ; 721 ;
721 722
722block : {$<vInt>$ = currState->nlocalvar;} chunk 723block : {$<vInt>$ = L->currState->nlocalvar;} chunk
723 { 724 {
724 adjuststack(currState->nlocalvar - $<vInt>1); 725 adjuststack(L->currState->nlocalvar - $<vInt>1);
725 for (; currState->nlocalvar > $<vInt>1; currState->nlocalvar--) 726 for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
726 luaI_unregisterlocalvar(luaX_linenumber); 727 luaI_unregisterlocalvar(L->lexstate->linenumber);
727 } 728 }
728 ; 729 ;
729 730
@@ -753,13 +754,13 @@ ret : /* empty */
753 } 754 }
754 ; 755 ;
755 756
756GetPC : /* empty */ { $$ = currState->pc; } 757GetPC : /* empty */ { $$ = L->currState->pc; }
757 ; 758 ;
758 759
759SaveWord : /* empty */ 760SaveWord : /* empty */
760 { $$ = currState->pc; 761 { $$ = L->currState->pc;
761 check_pc(JMPSIZE); 762 check_pc(JMPSIZE);
762 currState->pc += JMPSIZE; /* open space */ 763 L->currState->pc += JMPSIZE; /* open space */
763 } 764 }
764 ; 765 ;
765 766
@@ -808,7 +809,7 @@ functioncall : funcvalue funcParams
808 { 809 {
809 code_byte(0); /* save space for opcode */ 810 code_byte(0); /* save space for opcode */
810 code_byte($1+$2); /* number of parameters */ 811 code_byte($1+$2); /* number of parameters */
811 $$ = currState->pc; 812 $$ = L->currState->pc;
812 code_byte(0); /* must be adjusted by other rules */ 813 code_byte(0); /* must be adjusted by other rules */
813 } 814 }
814 ; 815 ;
@@ -816,7 +817,7 @@ functioncall : funcvalue funcParams
816funcvalue : varexp { $$ = 0; } 817funcvalue : varexp { $$ = 0; }
817 | varexp ':' NAME 818 | varexp ':' NAME
818 { 819 {
819 code_oparg(PUSHSELF, 0, string_constant($3, currState), 1); 820 code_oparg(PUSHSELF, 0, string_constant($3, L->currState), 1);
820 $$ = 1; 821 $$ = 1;
821 } 822 }
822 ; 823 ;
@@ -834,7 +835,7 @@ exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
834 { 835 {
835 if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */ 836 if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
836 else { 837 else {
837 currState->f->code[$4] = $<vLong>3; /* store list length */ 838 L->currState->f->code[$4] = $<vLong>3; /* store list length */
838 $$ = $4; 839 $$ = $4;
839 } 840 }
840 } 841 }
@@ -899,9 +900,9 @@ varlist1 : var { $$ = 1; add_varbuffer($1, 0); }
899 | varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; } 900 | varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; }
900 ; 901 ;
901 902
902var : NAME { $$ = singlevar($1, currState); } 903var : NAME { $$ = singlevar($1, L->currState); }
903 | varexp '[' expr1 ']' { $$ = 0; } /* indexed variable */ 904 | varexp '[' expr1 ']' { $$ = 0; } /* indexed variable */
904 | varexp '.' NAME { $$ = (-string_constant($3, currState))-1; } 905 | varexp '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
905 ; 906 ;
906 907
907varexp : var { lua_pushvar($1); } 908varexp : var { lua_pushvar($1); }
diff --git a/lvm.c b/lvm.c
index 1ba3e310..9e4aa8e0 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.12 1997/10/24 18:40:29 roberto Exp roberto $ 2** $Id: lvm.c,v 1.13 1997/10/27 16:14:37 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,6 +14,7 @@
14#include "lgc.h" 14#include "lgc.h"
15#include "lmem.h" 15#include "lmem.h"
16#include "lopcodes.h" 16#include "lopcodes.h"
17#include "lstate.h"
17#include "lstring.h" 18#include "lstring.h"
18#include "ltable.h" 19#include "ltable.h"
19#include "ltm.h" 20#include "ltm.h"
@@ -79,11 +80,11 @@ int luaV_tostring (TObject *obj)
79void luaV_closure (int nelems) 80void luaV_closure (int nelems)
80{ 81{
81 Closure *c = luaF_newclosure(nelems); 82 Closure *c = luaF_newclosure(nelems);
82 c->consts[0] = *(luaD_stack.top-1); 83 c->consts[0] = *(L->stack.top-1);
83 memcpy(&c->consts[1], luaD_stack.top-(nelems+1), nelems*sizeof(TObject)); 84 memcpy(&c->consts[1], L->stack.top-(nelems+1), nelems*sizeof(TObject));
84 luaD_stack.top -= nelems; 85 L->stack.top -= nelems;
85 ttype(luaD_stack.top-1) = LUA_T_FUNCTION; 86 ttype(L->stack.top-1) = LUA_T_FUNCTION;
86 (luaD_stack.top-1)->value.cl = c; 87 (L->stack.top-1)->value.cl = c;
87} 88}
88 89
89 90
@@ -94,22 +95,22 @@ void luaV_closure (int nelems)
94void luaV_gettable (void) 95void luaV_gettable (void)
95{ 96{
96 TObject *im; 97 TObject *im;
97 if (ttype(luaD_stack.top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */ 98 if (ttype(L->stack.top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
98 im = luaT_getimbyObj(luaD_stack.top-2, IM_GETTABLE); 99 im = luaT_getimbyObj(L->stack.top-2, IM_GETTABLE);
99 else { /* object is a table... */ 100 else { /* object is a table... */
100 int tg = (luaD_stack.top-2)->value.a->htag; 101 int tg = (L->stack.top-2)->value.a->htag;
101 im = luaT_getim(tg, IM_GETTABLE); 102 im = luaT_getim(tg, IM_GETTABLE);
102 if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */ 103 if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
103 TObject *h = luaH_get(avalue(luaD_stack.top-2), luaD_stack.top-1); 104 TObject *h = luaH_get(avalue(L->stack.top-2), L->stack.top-1);
104 if (h != NULL && ttype(h) != LUA_T_NIL) { 105 if (h != NULL && ttype(h) != LUA_T_NIL) {
105 --luaD_stack.top; 106 --L->stack.top;
106 *(luaD_stack.top-1) = *h; 107 *(L->stack.top-1) = *h;
107 } 108 }
108 else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL) 109 else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)
109 luaD_callTM(im, 2, 1); 110 luaD_callTM(im, 2, 1);
110 else { 111 else {
111 --luaD_stack.top; 112 --L->stack.top;
112 ttype(luaD_stack.top-1) = LUA_T_NIL; 113 ttype(L->stack.top-1) = LUA_T_NIL;
113 } 114 }
114 return; 115 return;
115 } 116 }
@@ -124,26 +125,26 @@ void luaV_gettable (void)
124 125
125 126
126/* 127/*
127** Function to store indexed based on values at the luaD_stack.top 128** Function to store indexed based on values at the L->stack.top
128** mode = 0: raw store (without internal methods) 129** mode = 0: raw store (without internal methods)
129** mode = 1: normal store (with internal methods) 130** mode = 1: normal store (with internal methods)
130** mode = 2: "deep luaD_stack.stack" store (with internal methods) 131** mode = 2: "deep L->stack.stack" store (with internal methods)
131*/ 132*/
132void luaV_settable (TObject *t, int mode) 133void luaV_settable (TObject *t, int mode)
133{ 134{
134 TObject *im = (mode == 0) ? NULL : luaT_getimbyObj(t, IM_SETTABLE); 135 TObject *im = (mode == 0) ? NULL : luaT_getimbyObj(t, IM_SETTABLE);
135 if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) { 136 if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) {
136 TObject *h = luaH_set(avalue(t), t+1); 137 TObject *h = luaH_set(avalue(t), t+1);
137 *h = *(luaD_stack.top-1); 138 *h = *(L->stack.top-1);
138 luaD_stack.top -= (mode == 2) ? 1 : 3; 139 L->stack.top -= (mode == 2) ? 1 : 3;
139 } 140 }
140 else { /* object is not a table, and/or has a specific "settable" method */ 141 else { /* object is not a table, and/or has a specific "settable" method */
141 if (im && ttype(im) != LUA_T_NIL) { 142 if (im && ttype(im) != LUA_T_NIL) {
142 if (mode == 2) { 143 if (mode == 2) {
143 *(luaD_stack.top+1) = *(luaD_stack.top-1); 144 *(L->stack.top+1) = *(L->stack.top-1);
144 *(luaD_stack.top) = *(t+1); 145 *(L->stack.top) = *(t+1);
145 *(luaD_stack.top-1) = *t; 146 *(L->stack.top-1) = *t;
146 luaD_stack.top += 2; /* WARNING: caller must assure stack space */ 147 L->stack.top += 2; /* WARNING: caller must assure stack space */
147 } 148 }
148 luaD_callTM(im, 3, 0); 149 luaD_callTM(im, 3, 0);
149 } 150 }
@@ -159,13 +160,13 @@ void luaV_getglobal (TaggedString *ts)
159 TObject *value = &ts->u.globalval; 160 TObject *value = &ts->u.globalval;
160 TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL); 161 TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL);
161 if (ttype(im) == LUA_T_NIL) { /* default behavior */ 162 if (ttype(im) == LUA_T_NIL) { /* default behavior */
162 *luaD_stack.top++ = *value; 163 *L->stack.top++ = *value;
163 } 164 }
164 else { 165 else {
165 ttype(luaD_stack.top) = LUA_T_STRING; 166 ttype(L->stack.top) = LUA_T_STRING;
166 tsvalue(luaD_stack.top) = ts; 167 tsvalue(L->stack.top) = ts;
167 luaD_stack.top++; 168 L->stack.top++;
168 *luaD_stack.top++ = *value; 169 *L->stack.top++ = *value;
169 luaD_callTM(im, 2, 1); 170 luaD_callTM(im, 2, 1);
170 } 171 }
171} 172}
@@ -176,14 +177,14 @@ void luaV_setglobal (TaggedString *ts)
176 TObject *oldvalue = &ts->u.globalval; 177 TObject *oldvalue = &ts->u.globalval;
177 TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL); 178 TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL);
178 if (ttype(im) == LUA_T_NIL) /* default behavior */ 179 if (ttype(im) == LUA_T_NIL) /* default behavior */
179 luaS_rawsetglobal(ts, --luaD_stack.top); 180 luaS_rawsetglobal(ts, --L->stack.top);
180 else { 181 else {
181 /* WARNING: caller must assure stack space */ 182 /* WARNING: caller must assure stack space */
182 TObject newvalue = *(luaD_stack.top-1); 183 TObject newvalue = *(L->stack.top-1);
183 ttype(luaD_stack.top-1) = LUA_T_STRING; 184 ttype(L->stack.top-1) = LUA_T_STRING;
184 tsvalue(luaD_stack.top-1) = ts; 185 tsvalue(L->stack.top-1) = ts;
185 *luaD_stack.top++ = *oldvalue; 186 *L->stack.top++ = *oldvalue;
186 *luaD_stack.top++ = newvalue; 187 *L->stack.top++ = newvalue;
187 luaD_callTM(im, 3, 0); 188 luaD_callTM(im, 3, 0);
188 } 189 }
189} 190}
@@ -191,9 +192,9 @@ void luaV_setglobal (TaggedString *ts)
191 192
192static void call_binTM (IMS event, char *msg) 193static void call_binTM (IMS event, char *msg)
193{ 194{
194 TObject *im = luaT_getimbyObj(luaD_stack.top-2, event);/* try first operand */ 195 TObject *im = luaT_getimbyObj(L->stack.top-2, event);/* try first operand */
195 if (ttype(im) == LUA_T_NIL) { 196 if (ttype(im) == LUA_T_NIL) {
196 im = luaT_getimbyObj(luaD_stack.top-1, event); /* try second operand */ 197 im = luaT_getimbyObj(L->stack.top-1, event); /* try second operand */
197 if (ttype(im) == LUA_T_NIL) { 198 if (ttype(im) == LUA_T_NIL) {
198 im = luaT_getim(0, event); /* try a 'global' i.m. */ 199 im = luaT_getim(0, event); /* try a 'global' i.m. */
199 if (ttype(im) == LUA_T_NIL) 200 if (ttype(im) == LUA_T_NIL)
@@ -214,8 +215,8 @@ static void call_arith (IMS event)
214static void comparison (lua_Type ttype_less, lua_Type ttype_equal, 215static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
215 lua_Type ttype_great, IMS op) 216 lua_Type ttype_great, IMS op)
216{ 217{
217 TObject *l = luaD_stack.top-2; 218 TObject *l = L->stack.top-2;
218 TObject *r = luaD_stack.top-1; 219 TObject *r = L->stack.top-1;
219 int result; 220 int result;
220 if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER) 221 if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
221 result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1; 222 result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
@@ -225,16 +226,16 @@ static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
225 call_binTM(op, "unexpected type at comparison"); 226 call_binTM(op, "unexpected type at comparison");
226 return; 227 return;
227 } 228 }
228 luaD_stack.top--; 229 L->stack.top--;
229 nvalue(luaD_stack.top-1) = 1; 230 nvalue(L->stack.top-1) = 1;
230 ttype(luaD_stack.top-1) = (result < 0) ? ttype_less : 231 ttype(L->stack.top-1) = (result < 0) ? ttype_less :
231 (result == 0) ? ttype_equal : ttype_great; 232 (result == 0) ? ttype_equal : ttype_great;
232} 233}
233 234
234 235
235void luaV_pack (StkId firstel, int nvararg, TObject *tab) 236void luaV_pack (StkId firstel, int nvararg, TObject *tab)
236{ 237{
237 TObject *firstelem = luaD_stack.stack+firstel; 238 TObject *firstelem = L->stack.stack+firstel;
238 int i; 239 int i;
239 if (nvararg < 0) nvararg = 0; 240 if (nvararg < 0) nvararg = 0;
240 avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */ 241 avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */
@@ -260,20 +261,21 @@ static void adjust_varargs (StkId first_extra_arg)
260{ 261{
261 TObject arg; 262 TObject arg;
262 luaV_pack(first_extra_arg, 263 luaV_pack(first_extra_arg,
263 (luaD_stack.top-luaD_stack.stack)-first_extra_arg, &arg); 264 (L->stack.top-L->stack.stack)-first_extra_arg, &arg);
264 luaD_adjusttop(first_extra_arg); 265 luaD_adjusttop(first_extra_arg);
265 *luaD_stack.top++ = arg; 266 *L->stack.top++ = arg;
266} 267}
267 268
268 269
269 270
270/* 271/*
271** Execute the given opcode, until a RET. Parameters are between 272** Execute the given opcode, until a RET. Parameters are between
272** [luaD_stack.stack+base,luaD_stack.top). Returns n such that the the results are between 273** [stack+base,top). Returns n such that the the results are between
273** [luaD_stack.stack+n,luaD_stack.top). 274** [stack+n,top).
274*/ 275*/
275StkId luaV_execute (Closure *cl, StkId base) 276StkId luaV_execute (Closure *cl, StkId base)
276{ 277{
278 LState *LL = L; /* to optimize */
277 Byte *pc = cl->consts[0].value.tf->code; 279 Byte *pc = cl->consts[0].value.tf->code;
278 TObject *consts = cl->consts[0].value.tf->consts; 280 TObject *consts = cl->consts[0].value.tf->consts;
279 if (lua_callhook) 281 if (lua_callhook)
@@ -284,13 +286,13 @@ StkId luaV_execute (Closure *cl, StkId base)
284 switch ((OpCode)(aux = *pc++)) { 286 switch ((OpCode)(aux = *pc++)) {
285 287
286 case PUSHNIL0: 288 case PUSHNIL0:
287 ttype(luaD_stack.top++) = LUA_T_NIL; 289 ttype(LL->stack.top++) = LUA_T_NIL;
288 break; 290 break;
289 291
290 case PUSHNIL: 292 case PUSHNIL:
291 aux = *pc++; 293 aux = *pc++;
292 do { 294 do {
293 ttype(luaD_stack.top++) = LUA_T_NIL; 295 ttype(LL->stack.top++) = LUA_T_NIL;
294 } while (aux--); 296 } while (aux--);
295 break; 297 break;
296 298
@@ -303,9 +305,9 @@ StkId luaV_execute (Closure *cl, StkId base)
303 case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2: 305 case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2:
304 aux -= PUSHNUMBER0; 306 aux -= PUSHNUMBER0;
305 pushnumber: 307 pushnumber:
306 ttype(luaD_stack.top) = LUA_T_NUMBER; 308 ttype(LL->stack.top) = LUA_T_NUMBER;
307 nvalue(luaD_stack.top) = aux; 309 nvalue(LL->stack.top) = aux;
308 luaD_stack.top++; 310 LL->stack.top++;
309 break; 311 break;
310 312
311 case PUSHLOCAL: 313 case PUSHLOCAL:
@@ -315,7 +317,7 @@ StkId luaV_execute (Closure *cl, StkId base)
315 case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7: 317 case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
316 aux -= PUSHLOCAL0; 318 aux -= PUSHLOCAL0;
317 pushlocal: 319 pushlocal:
318 *luaD_stack.top++ = *((luaD_stack.stack+base) + aux); 320 *LL->stack.top++ = *((LL->stack.stack+base) + aux);
319 break; 321 break;
320 322
321 case GETGLOBALW: 323 case GETGLOBALW:
@@ -345,7 +347,7 @@ StkId luaV_execute (Closure *cl, StkId base)
345 case GETDOTTED4: case GETDOTTED5: case GETDOTTED6: case GETDOTTED7: 347 case GETDOTTED4: case GETDOTTED5: case GETDOTTED6: case GETDOTTED7:
346 aux -= GETDOTTED0; 348 aux -= GETDOTTED0;
347 getdotted: 349 getdotted:
348 *luaD_stack.top++ = consts[aux]; 350 *LL->stack.top++ = consts[aux];
349 luaV_gettable(); 351 luaV_gettable();
350 break; 352 break;
351 353
@@ -355,10 +357,10 @@ StkId luaV_execute (Closure *cl, StkId base)
355 case PUSHSELF: 357 case PUSHSELF:
356 aux = *pc++; 358 aux = *pc++;
357 pushself: { 359 pushself: {
358 TObject receiver = *(luaD_stack.top-1); 360 TObject receiver = *(LL->stack.top-1);
359 *luaD_stack.top++ = consts[aux]; 361 *LL->stack.top++ = consts[aux];
360 luaV_gettable(); 362 luaV_gettable();
361 *luaD_stack.top++ = receiver; 363 *LL->stack.top++ = receiver;
362 break; 364 break;
363 } 365 }
364 366
@@ -373,7 +375,7 @@ StkId luaV_execute (Closure *cl, StkId base)
373 case PUSHCONSTANT6: case PUSHCONSTANT7: 375 case PUSHCONSTANT6: case PUSHCONSTANT7:
374 aux -= PUSHCONSTANT0; 376 aux -= PUSHCONSTANT0;
375 pushconstant: 377 pushconstant:
376 *luaD_stack.top++ = consts[aux]; 378 *LL->stack.top++ = consts[aux];
377 break; 379 break;
378 380
379 case PUSHUPVALUE: 381 case PUSHUPVALUE:
@@ -382,7 +384,7 @@ StkId luaV_execute (Closure *cl, StkId base)
382 case PUSHUPVALUE0: case PUSHUPVALUE1: 384 case PUSHUPVALUE0: case PUSHUPVALUE1:
383 aux -= PUSHUPVALUE0; 385 aux -= PUSHUPVALUE0;
384 pushupvalue: 386 pushupvalue:
385 *luaD_stack.top++ = cl->consts[aux+1]; 387 *LL->stack.top++ = cl->consts[aux+1];
386 break; 388 break;
387 389
388 case SETLOCAL: 390 case SETLOCAL:
@@ -392,7 +394,7 @@ StkId luaV_execute (Closure *cl, StkId base)
392 case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7: 394 case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7:
393 aux -= SETLOCAL0; 395 aux -= SETLOCAL0;
394 setlocal: 396 setlocal:
395 *((luaD_stack.stack+base) + aux) = *(--luaD_stack.top); 397 *((LL->stack.stack+base) + aux) = *(--LL->stack.top);
396 break; 398 break;
397 399
398 case SETGLOBALW: 400 case SETGLOBALW:
@@ -409,11 +411,11 @@ StkId luaV_execute (Closure *cl, StkId base)
409 break; 411 break;
410 412
411 case SETTABLE0: 413 case SETTABLE0:
412 luaV_settable(luaD_stack.top-3, 1); 414 luaV_settable(LL->stack.top-3, 1);
413 break; 415 break;
414 416
415 case SETTABLE: 417 case SETTABLE:
416 luaV_settable(luaD_stack.top-3-(*pc++), 2); 418 luaV_settable(LL->stack.top-3-(*pc++), 2);
417 break; 419 break;
418 420
419 case SETLISTW: 421 case SETLISTW:
@@ -426,12 +428,12 @@ StkId luaV_execute (Closure *cl, StkId base)
426 aux = 0; 428 aux = 0;
427 setlist: { 429 setlist: {
428 int n = *(pc++); 430 int n = *(pc++);
429 TObject *arr = luaD_stack.top-n-1; 431 TObject *arr = LL->stack.top-n-1;
430 for (; n; n--) { 432 for (; n; n--) {
431 ttype(luaD_stack.top) = LUA_T_NUMBER; 433 ttype(LL->stack.top) = LUA_T_NUMBER;
432 nvalue(luaD_stack.top) = n+aux; 434 nvalue(LL->stack.top) = n+aux;
433 *(luaH_set (avalue(arr), luaD_stack.top)) = *(luaD_stack.top-1); 435 *(luaH_set (avalue(arr), LL->stack.top)) = *(LL->stack.top-1);
434 luaD_stack.top--; 436 LL->stack.top--;
435 } 437 }
436 break; 438 break;
437 } 439 }
@@ -442,10 +444,10 @@ StkId luaV_execute (Closure *cl, StkId base)
442 case SETMAP: 444 case SETMAP:
443 aux = *pc++; 445 aux = *pc++;
444 setmap: { 446 setmap: {
445 TObject *arr = luaD_stack.top-(2*aux)-3; 447 TObject *arr = LL->stack.top-(2*aux)-3;
446 do { 448 do {
447 *(luaH_set (avalue(arr), luaD_stack.top-2)) = *(luaD_stack.top-1); 449 *(luaH_set (avalue(arr), LL->stack.top-2)) = *(LL->stack.top-1);
448 luaD_stack.top-=2; 450 LL->stack.top-=2;
449 } while (aux--); 451 } while (aux--);
450 break; 452 break;
451 } 453 }
@@ -456,7 +458,7 @@ StkId luaV_execute (Closure *cl, StkId base)
456 case POP0: case POP1: 458 case POP0: case POP1:
457 aux -= POP0; 459 aux -= POP0;
458 pop: 460 pop:
459 luaD_stack.top -= (aux+1); 461 LL->stack.top -= (aux+1);
460 break; 462 break;
461 463
462 case ARGS: 464 case ARGS:
@@ -478,17 +480,17 @@ StkId luaV_execute (Closure *cl, StkId base)
478 aux = *pc++; 480 aux = *pc++;
479 createarray: 481 createarray:
480 luaC_checkGC(); 482 luaC_checkGC();
481 avalue(luaD_stack.top) = luaH_new(aux); 483 avalue(LL->stack.top) = luaH_new(aux);
482 ttype(luaD_stack.top) = LUA_T_ARRAY; 484 ttype(LL->stack.top) = LUA_T_ARRAY;
483 luaD_stack.top++; 485 LL->stack.top++;
484 break; 486 break;
485 487
486 case EQOP: case NEQOP: { 488 case EQOP: case NEQOP: {
487 int res = luaO_equalObj(luaD_stack.top-2, luaD_stack.top-1); 489 int res = luaO_equalObj(LL->stack.top-2, LL->stack.top-1);
488 luaD_stack.top--; 490 LL->stack.top--;
489 if (aux == NEQOP) res = !res; 491 if (aux == NEQOP) res = !res;
490 ttype(luaD_stack.top-1) = res ? LUA_T_NUMBER : LUA_T_NIL; 492 ttype(LL->stack.top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
491 nvalue(luaD_stack.top-1) = 1; 493 nvalue(LL->stack.top-1) = 1;
492 break; 494 break;
493 } 495 }
494 496
@@ -509,49 +511,49 @@ StkId luaV_execute (Closure *cl, StkId base)
509 break; 511 break;
510 512
511 case ADDOP: { 513 case ADDOP: {
512 TObject *l = luaD_stack.top-2; 514 TObject *l = LL->stack.top-2;
513 TObject *r = luaD_stack.top-1; 515 TObject *r = LL->stack.top-1;
514 if (tonumber(r) || tonumber(l)) 516 if (tonumber(r) || tonumber(l))
515 call_arith(IM_ADD); 517 call_arith(IM_ADD);
516 else { 518 else {
517 nvalue(l) += nvalue(r); 519 nvalue(l) += nvalue(r);
518 --luaD_stack.top; 520 --LL->stack.top;
519 } 521 }
520 break; 522 break;
521 } 523 }
522 524
523 case SUBOP: { 525 case SUBOP: {
524 TObject *l = luaD_stack.top-2; 526 TObject *l = LL->stack.top-2;
525 TObject *r = luaD_stack.top-1; 527 TObject *r = LL->stack.top-1;
526 if (tonumber(r) || tonumber(l)) 528 if (tonumber(r) || tonumber(l))
527 call_arith(IM_SUB); 529 call_arith(IM_SUB);
528 else { 530 else {
529 nvalue(l) -= nvalue(r); 531 nvalue(l) -= nvalue(r);
530 --luaD_stack.top; 532 --LL->stack.top;
531 } 533 }
532 break; 534 break;
533 } 535 }
534 536
535 case MULTOP: { 537 case MULTOP: {
536 TObject *l = luaD_stack.top-2; 538 TObject *l = LL->stack.top-2;
537 TObject *r = luaD_stack.top-1; 539 TObject *r = LL->stack.top-1;
538 if (tonumber(r) || tonumber(l)) 540 if (tonumber(r) || tonumber(l))
539 call_arith(IM_MUL); 541 call_arith(IM_MUL);
540 else { 542 else {
541 nvalue(l) *= nvalue(r); 543 nvalue(l) *= nvalue(r);
542 --luaD_stack.top; 544 --LL->stack.top;
543 } 545 }
544 break; 546 break;
545 } 547 }
546 548
547 case DIVOP: { 549 case DIVOP: {
548 TObject *l = luaD_stack.top-2; 550 TObject *l = LL->stack.top-2;
549 TObject *r = luaD_stack.top-1; 551 TObject *r = LL->stack.top-1;
550 if (tonumber(r) || tonumber(l)) 552 if (tonumber(r) || tonumber(l))
551 call_arith(IM_DIV); 553 call_arith(IM_DIV);
552 else { 554 else {
553 nvalue(l) /= nvalue(r); 555 nvalue(l) /= nvalue(r);
554 --luaD_stack.top; 556 --LL->stack.top;
555 } 557 }
556 break; 558 break;
557 } 559 }
@@ -561,32 +563,32 @@ StkId luaV_execute (Closure *cl, StkId base)
561 break; 563 break;
562 564
563 case CONCOP: { 565 case CONCOP: {
564 TObject *l = luaD_stack.top-2; 566 TObject *l = LL->stack.top-2;
565 TObject *r = luaD_stack.top-1; 567 TObject *r = LL->stack.top-1;
566 if (tostring(l) || tostring(r)) 568 if (tostring(l) || tostring(r))
567 call_binTM(IM_CONCAT, "unexpected type for concatenation"); 569 call_binTM(IM_CONCAT, "unexpected type for concatenation");
568 else { 570 else {
569 tsvalue(l) = strconc(svalue(l), svalue(r)); 571 tsvalue(l) = strconc(svalue(l), svalue(r));
570 --luaD_stack.top; 572 --LL->stack.top;
571 } 573 }
572 luaC_checkGC(); 574 luaC_checkGC();
573 break; 575 break;
574 } 576 }
575 577
576 case MINUSOP: 578 case MINUSOP:
577 if (tonumber(luaD_stack.top-1)) { 579 if (tonumber(LL->stack.top-1)) {
578 ttype(luaD_stack.top) = LUA_T_NIL; 580 ttype(LL->stack.top) = LUA_T_NIL;
579 luaD_stack.top++; 581 LL->stack.top++;
580 call_arith(IM_UNM); 582 call_arith(IM_UNM);
581 } 583 }
582 else 584 else
583 nvalue(luaD_stack.top-1) = - nvalue(luaD_stack.top-1); 585 nvalue(LL->stack.top-1) = - nvalue(LL->stack.top-1);
584 break; 586 break;
585 587
586 case NOTOP: 588 case NOTOP:
587 ttype(luaD_stack.top-1) = 589 ttype(LL->stack.top-1) =
588 (ttype(luaD_stack.top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL; 590 (ttype(LL->stack.top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
589 nvalue(luaD_stack.top-1) = 1; 591 nvalue(LL->stack.top-1) = 1;
590 break; 592 break;
591 593
592 case ONTJMPW: 594 case ONTJMPW:
@@ -595,8 +597,8 @@ StkId luaV_execute (Closure *cl, StkId base)
595 case ONTJMP: 597 case ONTJMP:
596 aux = *pc++; 598 aux = *pc++;
597 ontjmp: 599 ontjmp:
598 if (ttype(luaD_stack.top-1) != LUA_T_NIL) pc += aux; 600 if (ttype(LL->stack.top-1) != LUA_T_NIL) pc += aux;
599 else luaD_stack.top--; 601 else LL->stack.top--;
600 break; 602 break;
601 603
602 case ONFJMPW: 604 case ONFJMPW:
@@ -605,8 +607,8 @@ StkId luaV_execute (Closure *cl, StkId base)
605 case ONFJMP: 607 case ONFJMP:
606 aux = *pc++; 608 aux = *pc++;
607 onfjmp: 609 onfjmp:
608 if (ttype(luaD_stack.top-1) == LUA_T_NIL) pc += aux; 610 if (ttype(LL->stack.top-1) == LUA_T_NIL) pc += aux;
609 else luaD_stack.top--; 611 else LL->stack.top--;
610 break; 612 break;
611 613
612 case JMPW: 614 case JMPW:
@@ -624,7 +626,7 @@ StkId luaV_execute (Closure *cl, StkId base)
624 case IFFJMP: 626 case IFFJMP:
625 aux = *pc++; 627 aux = *pc++;
626 iffjmp: 628 iffjmp:
627 if (ttype(--luaD_stack.top) == LUA_T_NIL) pc += aux; 629 if (ttype(--LL->stack.top) == LUA_T_NIL) pc += aux;
628 break; 630 break;
629 631
630 case IFTUPJMPW: 632 case IFTUPJMPW:
@@ -633,7 +635,7 @@ StkId luaV_execute (Closure *cl, StkId base)
633 case IFTUPJMP: 635 case IFTUPJMP:
634 aux = *pc++; 636 aux = *pc++;
635 iftupjmp: 637 iftupjmp:
636 if (ttype(--luaD_stack.top) != LUA_T_NIL) pc -= aux; 638 if (ttype(--LL->stack.top) != LUA_T_NIL) pc -= aux;
637 break; 639 break;
638 640
639 case IFFUPJMPW: 641 case IFFUPJMPW:
@@ -642,7 +644,7 @@ StkId luaV_execute (Closure *cl, StkId base)
642 case IFFUPJMP: 644 case IFFUPJMP:
643 aux = *pc++; 645 aux = *pc++;
644 iffupjmp: 646 iffupjmp:
645 if (ttype(--luaD_stack.top) == LUA_T_NIL) pc -= aux; 647 if (ttype(--LL->stack.top) == LUA_T_NIL) pc -= aux;
646 break; 648 break;
647 649
648 case CLOSURE: 650 case CLOSURE:
@@ -661,13 +663,13 @@ StkId luaV_execute (Closure *cl, StkId base)
661 case CALLFUNC0: case CALLFUNC1: 663 case CALLFUNC0: case CALLFUNC1:
662 aux -= CALLFUNC0; 664 aux -= CALLFUNC0;
663 callfunc: { 665 callfunc: {
664 StkId newBase = (luaD_stack.top-luaD_stack.stack)-(*pc++); 666 StkId newBase = (LL->stack.top-LL->stack.stack)-(*pc++);
665 luaD_call(newBase, aux); 667 luaD_call(newBase, aux);
666 break; 668 break;
667 } 669 }
668 670
669 case ENDCODE: 671 case ENDCODE:
670 luaD_stack.top = luaD_stack.stack + base; 672 LL->stack.top = LL->stack.stack + base;
671 /* goes through */ 673 /* goes through */
672 case RETCODE: 674 case RETCODE:
673 if (lua_callhook) 675 if (lua_callhook)
@@ -680,13 +682,13 @@ StkId luaV_execute (Closure *cl, StkId base)
680 case SETLINE: 682 case SETLINE:
681 aux = *pc++; 683 aux = *pc++;
682 setline: 684 setline:
683 if ((luaD_stack.stack+base-1)->ttype != LUA_T_LINE) { 685 if ((LL->stack.stack+base-1)->ttype != LUA_T_LINE) {
684 /* open space for LINE value */ 686 /* open space for LINE value */
685 luaD_openstack((luaD_stack.top-luaD_stack.stack)-base); 687 luaD_openstack((LL->stack.top-LL->stack.stack)-base);
686 base++; 688 base++;
687 (luaD_stack.stack+base-1)->ttype = LUA_T_LINE; 689 (LL->stack.stack+base-1)->ttype = LUA_T_LINE;
688 } 690 }
689 (luaD_stack.stack+base-1)->value.i = aux; 691 (LL->stack.stack+base-1)->value.i = aux;
690 if (lua_linehook) 692 if (lua_linehook)
691 luaD_lineHook(aux); 693 luaD_lineHook(aux);
692 break; 694 break;
diff --git a/makefile b/makefile
index 6858543e..234cf701 100644
--- a/makefile
+++ b/makefile
@@ -1,5 +1,5 @@
1# 1#
2## $Id: makefile,v 1.4 1997/10/24 17:17:24 roberto Exp roberto $ 2## $Id: makefile,v 1.5 1997/11/07 15:09:49 roberto Exp roberto $
3## Makefile 3## Makefile
4## See Copyright Notice in lua.h 4## See Copyright Notice in lua.h
5# 5#
@@ -18,7 +18,7 @@
18# define LUA_COMPAT2_5=0 if yous system does not need to be compatible with 18# define LUA_COMPAT2_5=0 if yous system does not need to be compatible with
19# version 2.5 (or older) 19# version 2.5 (or older)
20 20
21CONFIG = -DPOPEN -D_POSIX_SOURCE 21CONFIG = -DPOPEN -D_POSIX_SOURCE -DLUA_COMPAT2_5=0
22#CONFIG = -DLUA_COMPAT2_5=0 -DOLD_ANSI -DDEBUG 22#CONFIG = -DLUA_COMPAT2_5=0 -DOLD_ANSI -DDEBUG
23 23
24 24
@@ -43,6 +43,7 @@ LUAOBJS = \
43 llex.o \ 43 llex.o \
44 lmem.o \ 44 lmem.o \
45 lobject.o \ 45 lobject.o \
46 lstate.o \
46 lstx.o \ 47 lstx.o \
47 lstring.o \ 48 lstring.o \
48 ltable.o \ 49 ltable.o \
@@ -90,32 +91,34 @@ clear :
90%.c : RCS/%.c,v 91%.c : RCS/%.c,v
91 co $@ 92 co $@
92 93
93lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h ldo.h \ 94
94 lfunc.h lgc.h llex.h lzio.h lmem.h lstring.h ltable.h ltm.h \ 95lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lstate.h lfunc.h \
95 luadebug.h lvm.h 96 lgc.h lmem.h lstring.h ltable.h ltm.h luadebug.h lvm.h
96lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h 97lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h
97lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \ 98lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \
98 ldo.h lfunc.h lmem.h lstring.h ltable.h ltm.h 99 ldo.h lstate.h lfunc.h lmem.h lstring.h ltable.h ltm.h
99ldo.o: ldo.c ldo.h lobject.h lua.h lfunc.h lgc.h lmem.h lparser.h \ 100ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
100 lzio.h ltm.h luadebug.h lundump.h lvm.h 101 lparser.h lzio.h ltm.h luadebug.h lundump.h lvm.h
101lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h 102lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lstate.h
102lgc.o: lgc.c ldo.h lobject.h lua.h lfunc.h lgc.h lmem.h lstring.h \ 103lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
103 ltable.h ltm.h 104 lstring.h ltable.h ltm.h
104liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h 105liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h
105llex.o: llex.c llex.h lobject.h lua.h lzio.h lmem.h lparser.h \ 106llex.o: llex.c llex.h lobject.h lua.h lzio.h lmem.h lparser.h lstate.h \
106 lstring.h lstx.h luadebug.h 107 lstring.h lstx.h luadebug.h
107lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h 108lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h
108lmem.o: lmem.c lmem.h lua.h 109lmem.o: lmem.c lmem.h lstate.h lobject.h lua.h
109lobject.o: lobject.c lobject.h lua.h 110lobject.o: lobject.c lobject.h lua.h
110lstring.o: lstring.c lmem.h lobject.h lua.h lstring.h 111lstate.o: lstate.c lbuiltin.h ldo.h lobject.h lua.h lstate.h llex.h \
112 lzio.h lmem.h lstring.h ltable.h ltm.h
113lstring.o: lstring.c lmem.h lobject.h lua.h lstate.h lstring.h
111lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h 114lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h
112lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lfunc.h llex.h lzio.h \ 115lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h llex.h \
113 lmem.h lopcodes.h lparser.h lstring.h luadebug.h 116 lzio.h lmem.h lopcodes.h lparser.h lstring.h luadebug.h
114ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h ltable.h 117ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h lstate.h ltable.h
115ltm.o: ltm.c lauxlib.h lua.h ldo.h lobject.h lmem.h ltm.h lapi.h 118ltm.o: ltm.c lauxlib.h lua.h lmem.h lobject.h lstate.h ltm.h lapi.h
116lua.o: lua.c lua.h luadebug.h lualib.h 119lua.o: lua.c lua.h luadebug.h lualib.h
117lundump.o: lundump.c lauxlib.h lua.h lfunc.h lobject.h lmem.h \ 120lundump.o: lundump.c lauxlib.h lua.h lfunc.h lobject.h lmem.h \
118 lstring.h lundump.h lzio.h 121 lstring.h lundump.h lzio.h
119lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lfunc.h lgc.h lmem.h \ 122lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h lgc.h \
120 lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h 123 lmem.h lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h
121lzio.o: lzio.c lzio.h 124lzio.o: lzio.c lzio.h